Skip to content

Commit

Permalink
XMLImportedScriptInfo + importStageSprites
Browse files Browse the repository at this point in the history
way better and clean
  • Loading branch information
NexIsDumb committed Sep 19, 2024
1 parent a296991 commit bb58e08
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 22 deletions.
2 changes: 1 addition & 1 deletion source/funkin/backend/scripting/Script.hx
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ class Script extends FlxBasic implements IFlxDestroyable {
public function get(variable:String):Dynamic {return null;}

/**
* Gets the variable `variable` from the script's variables.
* Sets the variable `variable` from the script's variables.
* @param variable Name of the variable.
* @return Variable (or null if it doesn't exists)
*/
Expand Down
72 changes: 51 additions & 21 deletions source/funkin/game/Stage.hx
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ class Stage extends FlxBasic implements IBeatReceiver {
public var stageScript:Script;
public var state:FlxState;
public var characterPoses:Map<String, StageCharPos> = [];
public var xmlImportedScripts:Map<String, Bool> = [];
public var xmlImportedScripts:Array<XMLImportedScriptInfo> = [];

private var spritesParentFolder = "";

public function getSprite(name:String) {
public function getSprite(name:String)
return stageSprites[name];
}

public function setStagesSprites(script:Script)
for (k=>e in stageSprites) script.set(k, e);

public function new(stage:String, ?state:FlxState) {
super();
Expand Down Expand Up @@ -138,20 +140,7 @@ class Stage extends FlxBasic implements IBeatReceiver {
PlayState.instance.add(PlayState.instance.comboGroup);
PlayState.instance.comboGroup;
case "use-extension" | "extension" | "ext":
if (!node.has.script || PlayState.instance == null) continue;

var folder = node.getAtt("folder").getDefault("data/scripts/");
if (!folder.endsWith("/")) folder += "/";

var path = Paths.script(folder + node.getAtt("script"));
var daScript = Script.create(path);
if (daScript is DummyScript) Logs.trace('Script at ${path} does not exist.', ERROR);
else {
xmlImportedScripts.set(path, node.getAtt("isShortLived") == "true");
PlayState.instance.scripts.add(daScript);
daScript.load();
}

if (XMLImportedScriptInfo.prepeareInfos(node, this) == null) continue;
null;
default: null;
}
Expand Down Expand Up @@ -193,15 +182,20 @@ class Stage extends FlxBasic implements IBeatReceiver {

if (PlayState.instance == null) return;

for (k=>e in stageSprites)
stageScript.set(k, e);
setStagesSprites(stageScript);

// i know this for gets run twice under, but its better like this in case a script modifies the short lived ones, i dont wanna save them in an array; more dynamic like this - Nex
for (info in xmlImportedScripts) if (info.importStageSprites) {
var script = info.getScript();
if (script != null) setStagesSprites(script);
}

// idk lemme check anyways just in case scripts did smth - Nex
if (event != null) PlayState.instance.scripts.event("onPostStageCreation", event);

// shortlived scripts destroy when the stage finishes setting up - Nex
for (p=>b in xmlImportedScripts) if (b) {
var script = PlayState.instance.scripts.getByPath(p);
for (info in xmlImportedScripts) if (info.shortLived) {
var script = info.getScript();
if (script == null) continue;

PlayState.instance.scripts.remove(script);
Expand Down Expand Up @@ -318,4 +312,40 @@ typedef StageCharPosInfo = {
var y:Float;
var flip:Bool;
var scroll:Float;
}

class XMLImportedScriptInfo {
public var path:String;
public var shortLived:Bool = false;
public var importStageSprites:Bool = false;

public function new(path:String)
this.path = path;

public function getScript():Script
return PlayState.instance == null ? null : PlayState.instance.scripts.getByPath(path);

public static function prepeareInfos(node:Access, ?stage:Stage):XMLImportedScriptInfo {
if (!node.has.script || PlayState.instance == null) return null;

var folder = node.getAtt("folder").getDefault("data/scripts/");
if (!folder.endsWith("/")) folder += "/";

var path = Paths.script(folder + node.getAtt("script"));
var daScript = Script.create(path);
if (daScript is DummyScript) {
Logs.trace('Script at ${path} does not exist.', ERROR);
return null;
}

var infos = new XMLImportedScriptInfo(daScript.path);
infos.shortLived = node.getAtt("isShortLived") == "true";
infos.importStageSprites = node.getAtt("importStageSprites") == "true";

if (stage != null) stage.xmlImportedScripts.push(infos);
PlayState.instance.scripts.add(daScript);
daScript.load();

return infos;
}
}

0 comments on commit bb58e08

Please sign in to comment.