package Ġorg . li bere jo . game . plugin Ċ Ċ import Ġcom . badlogic . gdx . G dx Ċ import Ġcom . squareup . mos hi . M oshi Ċ import Ġcom . squareup . mos hi . kotlin . reflect . Kotlin Json Adapter Factory Ċ import Ġorg . kode in . di . K ode in Ċ import Ġorg . kode in . di . K ode in Aware Ċ import Ġorg . kode in . di . generic . instance Ċ import Ġorg . li bere jo . api . data . DataManager Ċ import Ġorg . li bere jo . api . network . Network Manager Ċ import Ġorg . li bere jo . api . network . on Receive Ċ import Ġorg . li bere jo . api . network . packet . C Plugin Manifest Packet Ċ import Ġorg . li bere jo . api . plugin . Plugin Info Ċ import Ġorg . li bere jo . api . plugin . PluginManager Ċ import Ġorg . li bere jo . api . plugin . Remote Plugin Declaration Ċ import Ġorg . li bere jo . api . plugin . Li bere jo Plugin Ċ import Ġjava . net . URL ClassLoader Ċ import Ġjava . nio . file . Files Ċ Ċ class ĠDefault PluginManager ( override Ġval Ġk ode in : ĠK ode in ) Ġ: ĠPlugin Manager , ĠK ode in Aware Ġ{ Ċ ĉ private Ġval Ġm oshi Ġ= ĠM oshi . Builder (). add ( Kotlin Json Adapter Factory ()). build () Ċ ĉ private Ġval Ġpackage Info Adapter Ġ= Ġm oshi . adapter ( Plugin Info :: class . java ) ĊĊ ĉ private Ġval Ġnetwork Manager : ĠNetwork Manager Ġby Ġinstance () ĊĊ ĉ comp anion Ġobject Ġ{ Ċĉ ĉ const Ġval Ġplugin Info Filename Ġ= Ġ" plugin . json " Ċ ĉ } ĊĊ ĉ init Ġ{ Ċĉ ĉ // ĠTODO Ġsecurity Ċ // ĉ ĉ Policy . set Policy ( Sandbox Security Policy ()) Ċ // ĉ ĉ System . set Security Manager ( Security Manager ()) ĊĊĉ ĉ // Ġhandle Ġplugin Ġmanifest Ġpacket Ċĉ ĉ network Manager . register Type ( C Plugin Manifest Packet :: class . java ) ĊĊĉ ĉ network Manager . client . on Receive < C Plugin Manifest Packet > Ġ{ Ġconn , Ġpacket Ġ-> Ċĉĉ ĉ packet . remote Plugins . forEach Ġ{ Ċĉĉĉ ĉ // Ġdownload , Ġinstall , Ġand Ġload Ġthis Ġplugin Ċĉĉ ĉ } Ċĉ ĉ } Ċ ĉ } ĊĊ ĉ private Ġval Ġdata Manager : ĠData Manager Ġby Ġinstance () ĊĊ ĉ override Ġfun Ġscan Local Plugins (): ĠList < PluginManager . Detected Plugin > Ġ{ Ċĉ ĉ val Ġpackages Ġ= Ġmutable ListOf < PluginManager . Detected Plugin >() ĊĊĉ ĉ // Ġload Ġpackages Ġinstalled Ġon Ġsystem Ġ( also Ġloads Ġdev Ġpackages Ġfrom Ġresources Ġfolders ) Ċĉ ĉ Files . walk ( data Manager . packages Dir ). forEach Ġ{ Ġpath Ġ-> Ċĉĉ ĉ val Ġclass Loader Ġ= ĠURL ClassLoader ( array Of ( path . to Uri (). to URL ())) Ċĉĉ ĉ val Ġstream Ġ= Ġclass Loader . getResource AsStream (" plugin . json ") Ġ?: Ġreturn @ forEach ĊĊĉĉ ĉ val Ġpackage Info Ġ= Ġpackage Info Adapter . fromJson ( stream . read Bytes (). toString ( Charsets . UTF _ 8 )) Ġ?: Ġreturn @ forEach Ċĉĉ ĉ packages . add ( PluginManager . Detected Plugin ( package Info , Ġpath )) Ċĉ ĉ } ĊĊĉ ĉ return Ġpackages Ċ ĉ } ĊĊ ĉ override Ġfun Ġload Plugin ( detected Plugin : ĠPlugin Manager . Detected Plugin ): ĠLi bere jo Plugin Ġ{ Ċĉ ĉ val Ġurl Ġ= Ġdetected Plugin . path . to Uri (). to URL () Ċĉ ĉ val Ġmain Class Ġ= Ġdetected Plugin . info . main Class ĊĊĉ ĉ val Ġclass Loader Ġ= ĠURL ClassLoader ( array Of ( url )) Ċĉ ĉ val Ġclazz Ġ= Ġclass Loader . load Class ( main Class ) ĊĊĉ ĉ val Ġplugin Ġ= Ġclazz . get Constructor ( K ode in :: class . java ). newInstance ( kode in ) Ġas ĠLi bere jo Plugin ĊĊĉ ĉ G dx . app . log (" Plugins ", Ġ" Loading Ġplugin Ġ\" ${ detected Plugin . info . name }\" ") ĊĊĉ ĉ plugin . load () ĊĊĉ ĉ return Ġplugin Ċ ĉ } ĊĊ ĉ override Ġfun Ġun load Plugin ( remote Plugin Declaration : ĠRemote Plugin Declaration ) Ġ{ Ċ ĉ } Ċ }