From 8f3897779a56972ead422e5ab158b9f1bc963375 Mon Sep 17 00:00:00 2001 From: NeeEoo Date: Sat, 30 Sep 2023 12:18:02 +0200 Subject: [PATCH] Added Script.fromString --- source/funkin/backend/scripting/HScript.hx | 7 +++++++ source/funkin/backend/scripting/Script.hx | 24 ++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/source/funkin/backend/scripting/HScript.hx b/source/funkin/backend/scripting/HScript.hx index 2d54ce40..86851984 100644 --- a/source/funkin/backend/scripting/HScript.hx +++ b/source/funkin/backend/scripting/HScript.hx @@ -47,6 +47,11 @@ class HScript extends Script { funkin.backend.scripting.GlobalScript.call("onScriptCreated", [this, "hscript"]); + if (code != null && code.trim() != "") + loadFromString(code); + } + + public override function loadFromString(code:String) { try { if (code != null && code.trim() != "") expr = parser.parseString(code, fileName); @@ -55,6 +60,8 @@ class HScript extends Script { } catch(e) { _errorHandler(new Error(ECustom(e.toString()), 0, 0, fileName, 0)); } + + return this; } private function importFailedCallback(cl:Array):Bool { diff --git a/source/funkin/backend/scripting/Script.hx b/source/funkin/backend/scripting/Script.hx index 927e1189..90dfce81 100644 --- a/source/funkin/backend/scripting/Script.hx +++ b/source/funkin/backend/scripting/Script.hx @@ -148,6 +148,22 @@ class Script extends FlxBasic implements IFlxDestroyable { return new DummyScript(path); } + /** + * Creates a script from the specified asset path. The language is automatically determined. + * @param path Path in assets + */ + public static function fromString(code:String, ext:String, path:String):Script { + return switch(ext.toLowerCase()) { + case "hx" | "hscript" | "hsc" | "hxs": + new HScript(path).loadFromString(code); + case "lua": + Logs.trace("Lua is not supported in this engine. Use HScript instead.", ERROR); + new DummyScript(path).loadFromString(code); + default: + new DummyScript(path).loadFromString(code); + } + } + /** * Creates a new instance of the script class. * @param path @@ -224,6 +240,14 @@ class Script extends FlxBasic implements IFlxDestroyable { return result; } + /** + * Loads the code from a string, doesnt really work after the script has been loaded + * @param code The code. + */ + public function loadFromString(code:String) { + return this; + } + /** * Sets a script's parent object so that its properties can be accessed easily. Ex: Passing `PlayState.instance` will allow `boyfriend` to be typed instead of `PlayState.instance.boyfriend`. * @param variable Parent variable.