Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement hscript-improved #13304

Closed
Prev Previous commit
Next Next commit
#if HSCRIPT_ALLOWED things
  • Loading branch information
TheLeerName committed Sep 19, 2023
commit 197bfcce7e5eea909dd42d3e884c822644076228
21 changes: 18 additions & 3 deletions source/psychlua/HScript.hx
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,27 @@ import psychlua.CustomSubstate;
#if HSCRIPT_ALLOWED
import hscript.Parser;
import hscript.Interp;
#end

class HScript extends Interp
class HScript
#if HSCRIPT_ALLOWED extends Interp #end
{
public var active:Bool = true;

public var parser:Parser;
public var parser:#if HSCRIPT_ALLOWED Parser #else Dynamic #end;

public var parentLua:FunkinLua;

public var exception:haxe.Exception;

public static function initHaxeModule(parent:FunkinLua)
{
#if HSCRIPT_ALLOWED
if(parent.hscript == null) {
parent.hscript = new HScript(parent);
trace('initialized hscript interp successfully: ${parent.scriptName}');
}
#end
}

public static function initHaxeModuleCode(parent:FunkinLua, code:String)
Expand All @@ -41,6 +45,7 @@ class HScript extends Interp

public var origin:String;
public function new(?parent:FunkinLua, ?file:String) {
#if HSCRIPT_ALLOWED
super();

var content:String = null;
Expand All @@ -55,10 +60,12 @@ class HScript extends Interp

preset();
executeCode(content);
#end
}

function preset()
{
#if HSCRIPT_ALLOWED
parser = new Parser();
parser.allowJSON = parser.allowMetadata = parser.allowTypes = true;
scriptObject = PlayState.instance; // allow use vars from playstate without "game" thing
Expand Down Expand Up @@ -152,20 +159,24 @@ class HScript extends Interp
setVar('Function_StopLua', FunkinLua.Function_StopLua); //doesnt do much cuz HScript has a lower priority than Lua
setVar('Function_StopHScript', FunkinLua.Function_StopHScript);
setVar('Function_StopAll', FunkinLua.Function_StopAll);
#end
}

public function executeCode(?codeToRun:String):Dynamic {
#if HSCRIPT_ALLOWED
if (codeToRun == null || !active) return null;

try {
return execute(parser.parseString(codeToRun, origin));
}
catch(e)
exception = e;
#end
return null;
}

public function executeFunction(?funcToRun:String, ?funcArgs:Array<Dynamic>):Dynamic {
#if HSCRIPT_ALLOWED
if (funcToRun == null || !active) return null;

if (variables.exists(funcToRun)) {
Expand All @@ -176,12 +187,13 @@ class HScript extends Interp
catch(e)
exception = e;
}
#end
return null;
}

public static function implement(funk:FunkinLua)
{
#if LUA_ALLOWED
#if (LUA_ALLOWED && HSCRIPT_ALLOWED)
funk.addLocalCallback("runHaxeCode", function(codeToRun:String, ?varsToBring:Any = null, ?funcToRun:String = null, ?funcArgs:Array<Dynamic> = null):Dynamic {
initHaxeModule(funk);
if (!funk.hscript.active) return null;
Expand Down Expand Up @@ -256,16 +268,19 @@ class HScript extends Interp
parser = null;
origin = null;
parentLua = null;
#if HSCRIPT_ALLOWED
__instanceFields = [];
binops.clear();
customClasses.clear();
declared = [];
importBlocklist = [];
locals.clear();
resetVariables();
#end
}
}

#if HSCRIPT_ALLOWED
class CustomFlxColor
{
public static var TRANSPARENT(default, null):Int = FlxColor.TRANSPARENT;
Expand Down