replace single scripts with scriptpacks (#185)

* replace single scripts with scriptpack

* this might break a ton of mods i think

* oops

* stuff

---------

Co-authored-by: Frakist <90983918+Frakist@users.noreply.github.com>
This commit is contained in:
Frakits
2023-07-18 04:25:37 -07:00
committed by GitHub
co-authored by Frakist
parent 8054d5c5ba
commit 2114be6fc6
6 changed files with 139 additions and 152 deletions
+27 -41
View File
@@ -9,7 +9,7 @@ import funkin.backend.assets.ModsFolder;
* Class for THE Global Script, aka script that runs in the background at all times.
*/
class GlobalScript {
public static var script:Script;
public static var scripts:ScriptPack;
public static function init() {
#if MOD_SUPPORT
@@ -20,92 +20,78 @@ class GlobalScript {
Conductor.onStepHit.add(stepHit);
FlxG.signals.focusGained.add(function() {
call("focusGained");
scripts.call("focusGained");
});
FlxG.signals.focusLost.add(function() {
call("focusLost");
scripts.call("focusLost");
});
FlxG.signals.gameResized.add(function(w:Int, h:Int) {
call("gameResized", [w, h]);
scripts.call("gameResized", [w, h]);
});
FlxG.signals.postDraw.add(function() {
call("postDraw");
scripts.call("postDraw");
});
FlxG.signals.postGameReset.add(function() {
call("postGameReset");
scripts.call("postGameReset");
});
FlxG.signals.postGameStart.add(function() {
call("postGameStart");
scripts.call("postGameStart");
});
FlxG.signals.postStateSwitch.add(function() {
call("postStateSwitch");
scripts.call("postStateSwitch");
});
FlxG.signals.postUpdate.add(function() {
call("postUpdate", [FlxG.elapsed]);
scripts.call("postUpdate", [FlxG.elapsed]);
if (FlxG.keys.justPressed.F5) {
if (script != null && !(script is DummyScript)) {
if (scripts != null) {
Logs.trace('Reloading global script...', WARNING, YELLOW);
script.reload();
scripts.reload();
Logs.trace('Global script successfully reloaded.', WARNING, GREEN);
} else {
Logs.trace('Loading global script...', WARNING, YELLOW);
onModSwitch(#if MOD_SUPPORT ModsFolder.currentModFolder #else null #end);
if (script is DummyScript)
Logs.trace('Global script not found. Are you sure "data/global.hx" exists?', ERROR, RED);
else
Logs.trace('Global script successfully loaded.', WARNING, GREEN);
}
}
});
FlxG.signals.preDraw.add(function() {
call("preDraw");
scripts.call("preDraw");
});
FlxG.signals.preGameReset.add(function() {
call("preGameReset");
scripts.call("preGameReset");
});
FlxG.signals.preGameStart.add(function() {
call("preGameStart");
scripts.call("preGameStart");
});
FlxG.signals.preStateCreate.add(function(state:FlxState) {
call("preStateCreate", [state]);
scripts.call("preStateCreate", [state]);
});
FlxG.signals.preStateSwitch.add(function() {
call("preStateSwitch", []);
scripts.call("preStateSwitch", []);
});
FlxG.signals.preUpdate.add(function() {
call("preUpdate", [FlxG.elapsed]);
call("update", [FlxG.elapsed]);
scripts.call("preUpdate", [FlxG.elapsed]);
scripts.call("update", [FlxG.elapsed]);
});
onModSwitch(#if MOD_SUPPORT ModsFolder.currentModFolder #else null #end);
}
public static function event<T:CancellableEvent>(name:String, event:T):T {
if (script == null) return event;
script.call(name, [event]);
return event;
}
public static function call(name:String, ?args:Array<Dynamic>) {
if (script == null) return;
script.call(name, args);
}
public static function onModSwitch(newMod:String) {
call("onDestroy");
if (script != null) {
script.destroy();
script = null;
}
script = Script.create(Paths.script('data/global'));
script.load();
if (scripts != null) scripts.call("onDestroy");
scripts = FlxDestroyUtil.destroy(scripts);
scripts = new ScriptPack("global");
var scriptPaths = Paths.getScriptPaths("assets/data", "/global.");
for (i in Paths.getScriptPaths("assets/data/global")) scriptPaths.push(i);
for (i in scriptPaths) scripts.add(Script.create(Paths.script(i, null, true)));
scripts.load();
}
public static function beatHit(curBeat:Int) {
call("beatHit", [curBeat]);
scripts.call("beatHit", [curBeat]);
}
public static function stepHit(curStep:Int) {
call("stepHit", [curStep]);
scripts.call("stepHit", [curStep]);
}
}
#end