140 lines
4.1 KiB
Haxe
140 lines
4.1 KiB
Haxe
package psychlua;
|
|
|
|
#if hscript
|
|
import hscript.Parser;
|
|
import hscript.Interp;
|
|
import hscript.Expr;
|
|
class HScript
|
|
{
|
|
public var parentLua:FunkinLua;
|
|
public static function initHaxeModule(parent:FunkinLua)
|
|
{
|
|
if(parent.hscript == null)
|
|
{
|
|
trace('initializing haxe interp for: ${parent.scriptName}');
|
|
parent.hscript = new HScript();
|
|
}
|
|
}
|
|
|
|
public static var parser:Parser = new Parser();
|
|
public var interp:Interp;
|
|
|
|
public var variables(get, never):Map<String, Dynamic>;
|
|
|
|
public function get_variables()
|
|
{
|
|
return interp.variables;
|
|
}
|
|
|
|
public function new()
|
|
{
|
|
interp = new Interp();
|
|
interp.variables.set('FlxG', FlxG);
|
|
interp.variables.set('FlxSprite', FlxSprite);
|
|
interp.variables.set('FlxCamera', FlxCamera);
|
|
interp.variables.set('FlxTimer', FlxTimer);
|
|
interp.variables.set('FlxTween', FlxTween);
|
|
interp.variables.set('FlxEase', FlxEase);
|
|
interp.variables.set('PlayState', PlayState);
|
|
interp.variables.set('game', PlayState.instance);
|
|
interp.variables.set('Paths', Paths);
|
|
interp.variables.set('Conductor', Conductor);
|
|
interp.variables.set('ClientPrefs', ClientPrefs);
|
|
interp.variables.set('Character', Character);
|
|
interp.variables.set('Alphabet', Alphabet);
|
|
interp.variables.set('CustomSubstate', CustomSubstate);
|
|
#if (!flash && sys)
|
|
interp.variables.set('FlxRuntimeShader', flixel.addons.display.FlxRuntimeShader);
|
|
#end
|
|
interp.variables.set('ShaderFilter', openfl.filters.ShaderFilter);
|
|
interp.variables.set('StringTools', StringTools);
|
|
|
|
interp.variables.set('setVar', function(name:String, value:Dynamic)
|
|
{
|
|
PlayState.instance.variables.set(name, value);
|
|
});
|
|
interp.variables.set('getVar', function(name:String)
|
|
{
|
|
var result:Dynamic = null;
|
|
if(PlayState.instance.variables.exists(name)) result = PlayState.instance.variables.get(name);
|
|
return result;
|
|
});
|
|
interp.variables.set('removeVar', function(name:String)
|
|
{
|
|
if(PlayState.instance.variables.exists(name))
|
|
{
|
|
PlayState.instance.variables.remove(name);
|
|
return true;
|
|
}
|
|
return false;
|
|
});
|
|
|
|
#if LUA_ALLOWED
|
|
interp.variables.set('createGlobalCallback', function(name:String, func:Dynamic)
|
|
{
|
|
for (script in PlayState.instance.luaArray)
|
|
if(script != null && script.lua != null && !script.closed)
|
|
Lua_helper.add_callback(script.lua, name, func);
|
|
FunkinLua.customFunctions.set(name, func);
|
|
});
|
|
// this one was tested
|
|
interp.variables.set('createCallback', function(name:String, func:Dynamic, ?funk:FunkinLua = null)
|
|
{
|
|
if(funk == null) funk = parentLua;
|
|
|
|
if(funk != null) funk.addLocalCallback(name, func);
|
|
else FunkinLua.luaTrace('createCallback ($name): 3rd argument is null', false, false, FlxColor.RED);
|
|
});
|
|
#end
|
|
}
|
|
|
|
public function execute(codeToRun:String):Dynamic
|
|
{
|
|
@:privateAccess
|
|
parser.line = 1;
|
|
parser.allowTypes = true;
|
|
return interp.execute(parser.parseString(codeToRun));
|
|
}
|
|
|
|
public static function implement(funk:FunkinLua) {
|
|
funk.addLocalCallback("runHaxeCode", function(codeToRun:String) {
|
|
var retVal:Dynamic = null;
|
|
|
|
#if hscript
|
|
initHaxeModule(funk);
|
|
try {
|
|
retVal = funk.hscript.execute(codeToRun);
|
|
}
|
|
catch (e:Dynamic) {
|
|
FunkinLua.luaTrace(funk.scriptName + ":" + funk.lastCalledFunction + " - " + e, false, false, FlxColor.RED);
|
|
}
|
|
#else
|
|
FunkinLua.luaTrace("runHaxeCode: HScript isn't supported on this platform!", false, false, FlxColor.RED);
|
|
#end
|
|
|
|
if(retVal != null && !LuaUtils.isOfTypes(retVal, [Bool, Int, Float, String, Array])) retVal = null;
|
|
return retVal;
|
|
});
|
|
funk.addLocalCallback("addHaxeLibrary", function(libName:String, ?libPackage:String = '') {
|
|
#if hscript
|
|
try {
|
|
var str:String = '';
|
|
if(libPackage.length > 0)
|
|
str = libPackage + '.';
|
|
else if(libName == null)
|
|
libName = '';
|
|
|
|
var c:Dynamic = Type.resolveClass(str + libName);
|
|
if (c == null)
|
|
c = Type.resolveEnum(str + libName);
|
|
|
|
funk.hscript.interp.variables.set(libName, c);
|
|
}
|
|
catch (e:Dynamic) {
|
|
FunkinLua.luaTrace(funk.scriptName + ":" + funk.lastCalledFunction + " - " + e, false, false, FlxColor.RED);
|
|
}
|
|
#end
|
|
});
|
|
}
|
|
}
|
|
#end |