Skip to content

Commit f75e355

Browse files
committed
GlobalScript Awesome!!!
1 parent 7985c7e commit f75e355

File tree

5 files changed

+119
-38
lines changed

5 files changed

+119
-38
lines changed

source/Constants.hx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ class Constants
4040
*/
4141
public static inline final DEFAULT_DISCORD_ID:String = "1095422496473358356";
4242

43+
/**
44+
* Default sustain cover.
45+
* This is used when the note has a sustain, but no cover is specified.
46+
* It will use the default cover instead of the note's cover.
47+
*/
4348
public static inline final DEFAULT_SUSTAIN_COVER:String = "holdCovers/holdCover";
4449

4550
/**

source/funkin/game/objects/shaders/CustomShader.hx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ package funkin.game.objects.shaders;
22

33
import flixel.system.FlxAssets.FlxShader;
44

5-
class CustomShader extends FlxShader
5+
class CustomShader extends FunkinShader
66
{
7-
public function new(shader:String) : Void
7+
public function new(?fragmentSource:String, ?vertexSource:String) : Void
88
{
9-
this.glFragmentSource = shader;
9+
this.glFragmentSource = fragmentSource ?? "";
10+
this.glVertexSource = vertexSource ?? " ";
11+
1012
super();
1113
}
1214
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package funkin.game.objects.shaders;
2+
3+
import flixel.system.FlxAssets.FlxShader;
4+
import rulescript.types.IRuleScriptCustomAccessor;
5+
6+
class FunkinShader extends FlxShader implements IRuleScriptCustomAccessor
7+
{
8+
@:dox(hide) @:noCompletion static final __instanceFields = Type.getInstanceFields(FunkinShader);
9+
10+
public function new(?fragmentSource:String, ?vertexSource:String):Void
11+
{
12+
this.glFragmentSource = fragmentSource ?? "";
13+
this.glVertexSource = vertexSource ?? " ";
14+
15+
super();
16+
}
17+
18+
public function setField(id:String, value:Dynamic):Dynamic
19+
{
20+
// TODO:
21+
22+
return -1;
23+
}
24+
25+
public function getField(id:String):Dynamic
26+
{
27+
// TODO:
28+
29+
return -1;
30+
}
31+
}

source/funkin/modding/GlobalScript.hx

Lines changed: 78 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,39 @@
11
package funkin.modding;
22

3-
import flixel.util.FlxDestroyUtil.IFlxDestroyable;
43
#if GLOBAL_SCRIPT
54
import flixel.util.FlxSignal.FlxTypedSignal;
6-
import funkin.modding.Script.ScriptType as TScript;
5+
import funkin.modding.Script.ScriptType;
76
import openfl.utils.Assets;
87
import haxe.ds.StringMap;
98

109
class 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

source/funkin/modding/import.hx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
package funkin.modding;
2-
31
#if !macro
42
// BASE SCRIPTING STUFF
53
import funkin.modding.*;
@@ -16,7 +14,6 @@ import rulescript.parsers.*;
1614
import rulescript.types.Typedefs;
1715
import rulescript.types.ScriptedTypeUtil;
1816
import rulescript.scriptedClass.RuleScriptedClassUtil;
19-
2017
import funkin.modding.hscript.*;
2118
#end
2219
#if LUA_ALLOWED

0 commit comments

Comments
 (0)