11package funkin .modding ;
22
3- import flixel .util .FlxDestroyUtil .IFlxDestroyable ;
43#if GLOBAL_SCRIPT
54import flixel .util .FlxSignal .FlxTypedSignal ;
6- import funkin .modding .Script .ScriptType as TScript ;
5+ import funkin .modding .Script .ScriptType ;
76import openfl .utils .Assets ;
87import haxe .ds .StringMap ;
98
109class GlobalScript
1110{
12- //
13- // PUBLIC HELPERS
14- //
15- public static function call (event : String , ? args : Array <Dynamic >, ? scriptType : TScript ): Dynamic
16- return scripts .call (event , args , scriptType );
17-
18- public static function set (name : String , value : Dynamic , ? scriptType : TScript ): Void
19- scripts .set (name , value , scriptType );
20-
2111 public static var scripts : ScriptPack ;
12+
13+ #if MODS_ALLOWED
2214 public static var onModSwitch : FlxTypedSignal <Void -> Void > = new FlxTypedSignal ();
23-
15+ #end
16+
17+ /**
18+ * [Description] Reloads the global scripts.
19+ * This will destroy the current scripts and load new ones from the shared path.
20+ * If there are no scripts to reload, it will log a message.
21+ * @return Void
22+ */
2423 public static function reload (): Void
2524 {
26- final more : Bool = (scripts ?. scripts .length > 0 ) ?? true ;
27- Logs .log (scripts == null ? ' No global script instance reloading...' : (more ? ' Reloading global scripts...' : ' No global scripts to reload...' ), YELLOW );
28-
25+ final __hasScripts : Bool = (scripts ?. scripts .length > 0 ) ?? true ;
26+ Logs .log (scripts == null ? ' No global script instance reloading...' : (__hasScripts ? ' Reloading global scripts...' : ' No global scripts to reload...' ), YELLOW );
27+
2928 destroy ();
3029
3130 scripts = new ScriptPack ();
3231 loadScripts (Paths .getSharedPath (), ' scripts/modules/' );
3332
34- if ( more )
33+ if ( __hasScripts )
3534 Logs .log (' Global script successfully reloaded.' , YELLOW );
3635 }
3736
38- static var loadedScripts : StringMap <Bool > = new StringMap <Bool >();
39-
4037 public static function loadScripts (path : String , fileToFind : String ): Void
4138 {
4239 for (folderName in Mods .directoriesWithFile (path , fileToFind ))
@@ -51,54 +48,63 @@ class GlobalScript
5148
5249 final convertedScriptPath : String = folderName + _fileName ;
5350
54- if (loadedScripts .exists (convertedScriptPath ))
51+ if (__loadedScripts .exists (convertedScriptPath ))
5552 continue ;
56-
53+ trace ( ' Loading script: ' + convertedScriptPath );
5754 #if LUA_ALLOWED
5855 if (Script .checkScriptExtensions (_fileName , " lua" ))
5956 {
6057 scripts .add (new FunkinLua (convertedScriptPath ));
61- loadedScripts .set (convertedScriptPath , true );
58+ __loadedScripts .set (convertedScriptPath , true );
6259 }
6360 #end
6461 #if HSCRIPT_ALLOWED
6562 if (Script .checkScriptExtensions (_fileName , " haxe" ))
6663 {
6764 scripts .add (new HScript (null , convertedScriptPath ));
68- loadedScripts .set (convertedScriptPath , true );
65+ __loadedScripts .set (convertedScriptPath , true );
6966 }
7067 #end
7168
72- loadedScripts .set (convertedScriptPath , true );
69+ __loadedScripts .set (convertedScriptPath , true );
7370 }
7471
7572 var prefix = if (! folderName .endsWith (" /" )) folderName + " /" else folderName ;
7673
7774 // Embedded assets
7875 for (asset in Assets .list ())
7976 {
80- if (! asset .startsWith (prefix )) continue ;
81- final relative : String = asset .substr (prefix .length );
82- if (relative .contains (" /" )) continue ;
83- if (! Script .checkScriptExtensions (relative )) continue ;
84- if (! relative .startsWith (" MODULE_" )) continue ;
77+ if (! asset .startsWith (prefix ))
78+ continue ;
79+ final relative : String = asset .substr (prefix .length );
80+ if (relative .contains (" /" ))
81+ continue ;
82+ if (! Script .checkScriptExtensions (relative ))
83+ continue ;
84+ if (! relative .startsWith (" MODULE_" ))
85+ continue ;
8586
8687 final convertedScriptPath : String = prefix + relative ;
87- if (loadedScripts .exists (convertedScriptPath )) continue ;
88+ if (__loadedScripts .exists (convertedScriptPath ))
89+ {
90+ continue ;
91+ }
92+
93+ trace (' Loading script: ' + convertedScriptPath );
8894
8995 #if LUA_ALLOWED
9096 if (Script .checkScriptExtensions (relative , " lua" ))
9197 {
9298 scripts .add (new FunkinLua (convertedScriptPath ));
93- loadedScripts .set (convertedScriptPath , true );
99+ __loadedScripts .set (convertedScriptPath , true );
94100 }
95101 #end
96102
97103 #if HSCRIPT_ALLOWED
98104 if (Script .checkScriptExtensions (relative , " haxe" ))
99105 {
100106 scripts .add (new HScript (null , convertedScriptPath ));
101- loadedScripts .set (convertedScriptPath , true );
107+ __loadedScripts .set (convertedScriptPath , true );
102108 }
103109 #end
104110 }
@@ -109,9 +115,11 @@ class GlobalScript
109115
110116 public static function init (): Void
111117 {
118+ #if MODS_ALLOWED
112119 onModSwitch .add (reload );
113120 onModSwitch .add (() -> Logs .log (' Loading global scripts...' , YELLOW ));
114121 onModSwitch .dispatch ();
122+ #end
115123
116124 // /
117125
@@ -135,13 +143,14 @@ class GlobalScript
135143
136144 FlxG .signals .preUpdate .add (() ->
137145 {
146+ FlxG .watch .addQuick (" Loaded Scripts" , scripts .scripts .length );
147+
138148 scripts .call (" preUpdate" , [FlxG .elapsed ]);
139149 scripts .call (" update" , [FlxG .elapsed ]);
140150 });
141151
142152 FlxG .signals .postUpdate .add (() ->
143153 {
144- FlxG .watch .addQuick (" Loaded Scripts" , scripts .scripts .length );
145154 if (FlxG .keys .justPressed .F5 )
146155 {
147156 if (scripts .scripts .length > 0 )
@@ -158,15 +167,52 @@ class GlobalScript
158167 }
159168 scripts .call (" postUpdate" , [FlxG .elapsed ]);
160169 });
170+
171+ Application .current .onExit .add (_ -> destroy ());
161172 }
162173
174+ /**
175+ * [Description] A map of loaded scripts.
176+ * This is used to prevent loading the same script multiple times.
177+ */
178+ @:dox (hide ) @:noCompletion static var __loadedScripts : StringMap <Bool > = new StringMap <Bool >();
179+
163180 public static function destroy (): Void
164181 {
182+ if (__loadedScripts != null ) __loadedScripts .clear ();
165183 if (scripts != null )
166184 {
167185 scripts .call (' destroy' );
168186 scripts = FlxDestroyUtil .destroy (scripts );
169187 }
170188 }
189+
190+ //
191+ // PUBLIC HELPERS
192+ //
193+
194+ /**
195+ * [Description] calls a global script function.
196+ * @param event is the name of the function to call.
197+ * @param args is an array of arguments to pass to the function.
198+ * @param scriptType is the type of script to call. If not provided, it defaults to `ScriptType.HSCRIPT`.
199+ * @return Dynamic return scripts.call(event, args, scriptType)
200+ */
201+ public static function call (event : String , ? args : Array <Dynamic >, ? scriptType : ScriptType ): Dynamic
202+ {
203+ return scripts .call (event , args , scriptType );
204+ }
205+
206+ /**
207+ * [Description] sets a global script variable.
208+ * @param name is the name of the variable to set.
209+ * @param value is the value to set the variable to.
210+ * @param scriptType is the type of script to set the variable in. If not provided, it defaults to `ScriptType.HSCRIPT`.
211+ * @return Void
212+ */
213+ public static function set (name : String , value : Dynamic , ? scriptType : ScriptType ): Void
214+ {
215+ scripts .set (name , value , scriptType );
216+ }
171217}
172218#end
0 commit comments