From b2425c783a0365592e7910c70e5098cf69e237e4 Mon Sep 17 00:00:00 2001 From: TheLeerName Date: Sat, 23 Sep 2023 19:21:55 +0700 Subject: [PATCH 1/6] rewrite error messages in scripts also init of lua/hx scripts now traces elapsed time on loading --- source/psychlua/FunkinLua.hx | 8 +-- source/psychlua/HScript.hx | 98 +++++++++++++++++++----------------- source/states/PlayState.hx | 29 +++++++---- 3 files changed, 77 insertions(+), 58 deletions(-) diff --git a/source/psychlua/FunkinLua.hx b/source/psychlua/FunkinLua.hx index d999a6f87d9..66aac3d03df 100644 --- a/source/psychlua/FunkinLua.hx +++ b/source/psychlua/FunkinLua.hx @@ -65,6 +65,8 @@ class FunkinLua { public function new(scriptName:String) { #if LUA_ALLOWED + var times:Float = Date.now().getTime(); + lua = LuaL.newstate(); LuaL.openlibs(lua); @@ -1446,7 +1448,7 @@ class FunkinLua { }); Lua_helper.add_callback(lua, "debugPrint", function(text:Dynamic = '', color:String = 'WHITE') PlayState.instance.addTextToDebug(text, CoolUtil.colorFromString(color))); - + addLocalCallback("close", function() { closed = true; trace('Closing script $scriptName'); @@ -1461,7 +1463,7 @@ class FunkinLua { CustomSubstate.implement(this); ShaderFunctions.implement(this); DeprecatedFunctions.implement(this); - + try{ var result:Dynamic = LuaL.dofile(lua, scriptName); var resultStr:String = Lua.tostring(lua, result); @@ -1479,9 +1481,9 @@ class FunkinLua { trace(e); return; } - trace('lua file loaded succesfully:' + scriptName); call('onCreate', []); + trace('lua file loaded succesfully: $scriptName (${Std.int(Date.now().getTime() - times)}ms)'); #end } diff --git a/source/psychlua/HScript.hx b/source/psychlua/HScript.hx index 12eb849d578..8aa1bcfe8d2 100644 --- a/source/psychlua/HScript.hx +++ b/source/psychlua/HScript.hx @@ -15,30 +15,35 @@ class HScript extends BrewScript { if(parent.hscript == null) { - trace('initializing haxe interp for: ${parent.scriptName}'); + var times:Float = Date.now().getTime(); parent.hscript = new HScript(parent); + trace('initialized brewscript interp successfully: ${parent.scriptName} (${Std.int(Date.now().getTime() - times)}ms)'); } } public static function initHaxeModuleCode(parent:FunkinLua, code:String) { + initHaxeModule(parent); var hs:HScript = try parent.hscript catch (e) null; - if(hs == null) - { - trace('initializing haxe interp for: ${parent.scriptName}'); - parent.hscript = new HScript(parent, code); - } - else + if(hs != null) { hs.doScript(code); @:privateAccess - if(hs.parsingException != null ) + if(hs.parsingException != null) { - PlayState.instance.addTextToDebug('ERROR ON LOADING (${hs.origin}): ${hs.parsingException.message}', FlxColor.RED); + var e:String = hs.parsingException.message; + if (!e.contains(hs.origin)) e = '${hs.origin}: $e'; + FunkinLua.luaTrace('ERROR ON LOADING - $e', FlxColor.RED); + hs.kill(); } } } + public static function hscriptTrace(text:String, color:FlxColor = FlxColor.WHITE) { + PlayState.instance.addTextToDebug(text, color); + trace(text); + } + public var origin:String; override public function new(?parent:FunkinLua, ?file:String) { @@ -60,27 +65,26 @@ class HScript extends BrewScript super.preset(); // Some very commonly used classes - set('FlxG', flixel.FlxG); - set('FlxSprite', flixel.FlxSprite); - set('FlxCamera', flixel.FlxCamera); - set('FlxTimer', flixel.util.FlxTimer); - set('FlxTween', flixel.tweens.FlxTween); - set('FlxEase', flixel.tweens.FlxEase); + setClass(flixel.FlxG); + setClass(flixel.FlxSprite); + setClass(flixel.FlxCamera); + setClass(flixel.util.FlxTimer); + setClass(flixel.tweens.FlxTween); + setClass(flixel.tweens.FlxEase); set('FlxColor', CustomFlxColor); - set('PlayState', PlayState); - set('Paths', Paths); - set('Conductor', Conductor); - set('ClientPrefs', ClientPrefs); - set('Character', Character); - set('Alphabet', Alphabet); - set('Note', objects.Note); - set('CustomSubstate', CustomSubstate); - set('Countdown', backend.BaseStage.Countdown); + setClass(PlayState); + setClass(Paths); + setClass(Conductor); + setClass(ClientPrefs); + setClass(Character); + setClass(Alphabet); + setClass(objects.Note); + setClass(CustomSubstate); + setClass(backend.BaseStage.Countdown); #if (!flash && sys) - set('FlxRuntimeShader', flixel.addons.display.FlxRuntimeShader); + setClass(flixel.addons.display.FlxRuntimeShader); #end - set('ShaderFilter', openfl.filters.ShaderFilter); - set('StringTools', StringTools); + setClass(openfl.filters.ShaderFilter); // Functions & Variables set('setVar', function(name:String, value:Dynamic) @@ -135,10 +139,10 @@ class HScript extends BrewScript if(libPackage.length > 0) str = libPackage + '.'; - set(libName, Type.resolveClass(str + libName)); + set(libName, resolveClassOrEnum(str + libName)); } catch (e:Dynamic) { - var msg:String = e.message.substr(0, e.message.indexOf('\n')); + var msg:String = e.message; if(parentLua != null) { FunkinLua.lastCalledScript = parentLua; @@ -181,7 +185,7 @@ class HScript extends BrewScript if(!exists(funcToRun)) { - FunkinLua.luaTrace(origin + ' - No HScript function named: $funcToRun', false, false, FlxColor.RED); + FunkinLua.luaTrace('$origin: No HScript function named: $funcToRun', false, false, FlxColor.RED); return null; } @@ -192,8 +196,9 @@ class HScript extends BrewScript if (e != null) { var msg:String = e.toString(); - if(parentLua != null) msg = origin + ":" + parentLua.lastCalledFunction + " - " + msg; - else msg = '$origin - $msg'; + if (!msg.contains(origin)) msg = '$origin: $msg'; + if(parentLua != null) msg = 'ERROR (${parentLua.lastCalledFunction}) - $msg'; + else msg = 'ERROR - $msg'; FunkinLua.luaTrace(msg, parentLua == null, false, FlxColor.RED); } return null; @@ -213,8 +218,8 @@ class HScript extends BrewScript { #if LUA_ALLOWED funk.addLocalCallback("runHaxeCode", function(codeToRun:String, ?varsToBring:Any = null, ?funcToRun:String = null, ?funcArgs:Array = null):Dynamic { - var retVal:BrewCall = null; #if BrewScript + var retVal:BrewCall = null; initHaxeModuleCode(funk, codeToRun); if(varsToBring != null) { @@ -233,7 +238,7 @@ class HScript extends BrewScript var e = retVal.exceptions[0]; var calledFunc:String = if(funk.hscript.origin == funk.lastCalledFunction) funcToRun else funk.lastCalledFunction; if (e != null) - FunkinLua.luaTrace(funk.hscript.origin + ":" + calledFunc + " - " + e, false, false, FlxColor.RED); + FunkinLua.luaTrace('ERROR (${calledFunc}) - $e', false, false, FlxColor.RED); return null; } else if (funk.hscript.returnValue != null) @@ -241,7 +246,7 @@ class HScript extends BrewScript return funk.hscript.returnValue; } #else - FunkinLua.luaTrace("runHaxeCode: HScript isn't supported on this platform!", false, false, FlxColor.RED); + FunkinLua.luaTrace(origin + ": runHaxeCode: HScript isn't supported on this platform!", false, false, FlxColor.RED); #end return null; }); @@ -253,33 +258,29 @@ class HScript extends BrewScript { var e = callValue.exceptions[0]; if (e != null) - FunkinLua.luaTrace('ERROR (${funk.hscript.origin}: ${callValue.calledFunction}) - ' + e.message.substr(0, e.message.indexOf('\n')), false, false, FlxColor.RED); + FunkinLua.luaTrace('ERROR (${callValue.calledFunction}) - $e', false, false, FlxColor.RED); return null; } else return callValue.returnValue; #else - FunkinLua.luaTrace("runHaxeFunction: HScript isn't supported on this platform!", false, false, FlxColor.RED); + FunkinLua.luaTrace(origin + ": runHaxeFunction: HScript isn't supported on this platform!", false, false, FlxColor.RED); #end }); // This function is unnecessary because import already exists in Brew as a native feature funk.addLocalCallback("addHaxeLibrary", function(libName:String, ?libPackage:String = '') { + #if BrewScript 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); + var c:Dynamic = funk.hscript.resolveClassOrEnum(str + libName); - #if BrewScript if (c != null) BrewScript.globalVariables[libName] = c; - #end - #if BrewScript if (funk.hscript != null) { try { @@ -287,16 +288,23 @@ class HScript extends BrewScript funk.hscript.set(libName, c); } catch (e:Dynamic) { - FunkinLua.luaTrace(funk.hscript.origin + ":" + funk.lastCalledFunction + " - " + e, false, false, FlxColor.RED); + FunkinLua.luaTrace('ERROR (${funk.lastCalledFunction}) - $e', false, false, FlxColor.RED); } } #else - FunkinLua.luaTrace("addHaxeLibrary: HScript isn't supported on this platform!", false, false, FlxColor.RED); + FunkinLua.luaTrace(origin + ": addHaxeLibrary: HScript isn't supported on this platform!", false, false, FlxColor.RED); #end }); #end } + function resolveClassOrEnum(name:String):Dynamic { + var c:Dynamic = Type.resolveClass(name); + if (c == null) + c = Type.resolveEnum(name); + return c; + } + override public function kill() { origin = null; diff --git a/source/states/PlayState.hx b/source/states/PlayState.hx index 1f5dce2a7f3..26ed4f30549 100644 --- a/source/states/PlayState.hx +++ b/source/states/PlayState.hx @@ -2136,7 +2136,7 @@ class PlayState extends MusicBeatState } catch(e:Dynamic) { - addTextToDebug('ERROR ("Set Property" Event) - ' + e.message.substr(0, e.message.indexOf('\n')), FlxColor.RED); + HScript.hscriptTrace('ERROR ("Set Property" Event) - $e', FlxColor.RED); } case 'Play Sound': @@ -3109,10 +3109,13 @@ class PlayState extends MusicBeatState { try { + var times:Float = Date.now().getTime(); var newScript:HScript = new HScript(null, file); if(newScript.parsingException != null) { - addTextToDebug('ERROR ON LOADING ($file): ${newScript.parsingException.message}', FlxColor.RED); + var e = newScript.parsingException.message; + if (!e.contains(newScript.origin)) e = '${newScript.origin}: $e'; + HScript.hscriptTrace('ERROR ON LOADING - $e', FlxColor.RED); newScript.kill(); return; } @@ -3124,21 +3127,27 @@ class PlayState extends MusicBeatState if(!callValue.succeeded) { for (e in callValue.exceptions) - if (e != null) - addTextToDebug('ERROR ($file: onCreate) - ${e.message.substr(0, e.message.indexOf('\n'))}', FlxColor.RED); + if (e != null) { + var e:String = e.toString(); + if (!e.contains(newScript.origin)) e = '${newScript.origin}: $e'; + HScript.hscriptTrace('ERROR (onCreate) - $e', FlxColor.RED); + } newScript.kill(); hscriptArray.remove(newScript); - trace('failed to initialize brew interp!!! ($file)'); + return; } - else trace('initialized brew interp successfully: $file'); } - + trace('initialized brewscript interp successfully: $file (${Std.int(Date.now().getTime() - times)}ms)'); } catch(e) { - addTextToDebug('ERROR ($file) - ' + e.message.substr(0, e.message.indexOf('\n')), FlxColor.RED); var newScript:HScript = cast (BrewScript.global.get(file), HScript); + + var e:String = e.toString(); + if (!e.contains(newScript.origin)) e = '${newScript.origin}: $e'; + HScript.hscriptTrace('ERROR - $e', FlxColor.RED); + if(newScript != null) { newScript.kill(); @@ -3208,7 +3217,7 @@ class PlayState extends MusicBeatState for(i in 0...len) { var script:HScript = hscriptArray[i]; - if(script == null || !script.exists(funcToCall) || exclusions.contains(script.origin)) + if(script == null || !script.active || !script.exists(funcToCall) || exclusions.contains(script.origin)) continue; var myValue:Dynamic = null; @@ -3219,7 +3228,7 @@ class PlayState extends MusicBeatState { var e = callValue.exceptions[0]; if(e != null) - FunkinLua.luaTrace('ERROR (${script.origin}: ${callValue.calledFunction}) - ' + e.message.substr(0, e.message.indexOf('\n')), true, false, FlxColor.RED); + FunkinLua.luaTrace('ERROR (${callValue.calledFunction}) - $e', true, false, FlxColor.RED); } else { From 48d5a36a27501a6f23ec1aa7b37f0d5f06fadb5d Mon Sep 17 00:00:00 2001 From: TheLeerName Date: Mon, 25 Sep 2023 14:00:39 +0700 Subject: [PATCH 2/6] ye --- source/psychlua/HScript.hx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/psychlua/HScript.hx b/source/psychlua/HScript.hx index 90d24e3d506..c6a81a34cf7 100644 --- a/source/psychlua/HScript.hx +++ b/source/psychlua/HScript.hx @@ -265,7 +265,7 @@ class HScript extends SScript }); // This function is unnecessary because import already exists in SScript as a native feature funk.addLocalCallback("addHaxeLibrary", function(libName:String, ?libPackage:String = '') { - #if BrewScript + #if SScript var str:String = ''; if(libPackage.length > 0) str = libPackage + '.'; From 47ffc80184e7a664d1ecabf5d39f0dfb6d58330f Mon Sep 17 00:00:00 2001 From: Leer <85291330+TheLeerName@users.noreply.github.com> Date: Tue, 31 Oct 2023 10:54:23 +0700 Subject: [PATCH 3/6] } issue --- source/states/PlayState.hx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/states/PlayState.hx b/source/states/PlayState.hx index d2c731b31b9..9c1bd255688 100644 --- a/source/states/PlayState.hx +++ b/source/states/PlayState.hx @@ -3116,7 +3116,7 @@ class PlayState extends MusicBeatState { var times:Float = Date.now().getTime(); var newScript:HScript = new HScript(null, file); - if(newScript.parsingException != null) + if(newScript.parsingException != null) { var e = newScript.parsingException.message; if (!e.contains(newScript.origin)) e = '${newScript.origin}: $e'; HScript.hscriptTrace('ERROR ON LOADING - $e', FlxColor.RED); @@ -3141,7 +3141,7 @@ class PlayState extends MusicBeatState hscriptArray.remove(newScript); return; } - } + } trace('initialized sscript interp successfully: $file (${Std.int(Date.now().getTime() - times)}ms)'); } From 9bea4e22d8350a7be3418854a31c2160e70ec4e9 Mon Sep 17 00:00:00 2001 From: Leer <85291330+TheLeerName@users.noreply.github.com> Date: Wed, 1 Nov 2023 12:05:26 +0700 Subject: [PATCH 4/6] formatting --- source/psychlua/HScript.hx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/psychlua/HScript.hx b/source/psychlua/HScript.hx index 9a9287fecad..795916cae0e 100644 --- a/source/psychlua/HScript.hx +++ b/source/psychlua/HScript.hx @@ -81,7 +81,7 @@ class HScript extends SScript setClass(flixel.tweens.FlxTween); setClass(flixel.tweens.FlxEase); set('FlxColor', CustomFlxColor); - setClass(backend.BaseStage.Countdown); + setClass(backend.BaseStage.Countdown); setClass(PlayState); setClass(Paths); setClass(Conductor); @@ -374,4 +374,4 @@ class CustomFlxColor return cast FlxColor.fromString(str); } } -#end \ No newline at end of file +#end From e5b0528e0a3906007b1e1f43ce4fc751379f3ea5 Mon Sep 17 00:00:00 2001 From: Leer <85291330+TheLeerName@users.noreply.github.com> Date: Wed, 1 Nov 2023 12:07:20 +0700 Subject: [PATCH 5/6] wait it already inited --- source/psychlua/HScript.hx | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/source/psychlua/HScript.hx b/source/psychlua/HScript.hx index 795916cae0e..19bde740ada 100644 --- a/source/psychlua/HScript.hx +++ b/source/psychlua/HScript.hx @@ -25,13 +25,7 @@ class HScript extends SScript { initHaxeModule(parent); var hs:HScript = try parent.hscript catch (e) null; - if(hs == null) - { - trace('initializing haxe interp for: ${parent.scriptName}'); - parent.hscript = new HScript(parent, code, varsToBring); - } - else - { + if(hs != null) { hs.doString(code); @:privateAccess if(hs.parsingException != null) From acd875714e879daaff3a3b0f167a0d3becba8bd7 Mon Sep 17 00:00:00 2001 From: Leer <85291330+TheLeerName@users.noreply.github.com> Date: Wed, 1 Nov 2023 12:09:27 +0700 Subject: [PATCH 6/6] and yet another fix --- source/psychlua/HScript.hx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/psychlua/HScript.hx b/source/psychlua/HScript.hx index 19bde740ada..5d51fa253f8 100644 --- a/source/psychlua/HScript.hx +++ b/source/psychlua/HScript.hx @@ -33,7 +33,7 @@ class HScript extends SScript var e:String = hs.parsingException.message; if (!e.contains(hs.origin)) e = '${hs.origin}: $e'; FunkinLua.luaTrace('ERROR ON LOADING - $e', FlxColor.RED); - hs.kill(); + hs.destroy(); } } } @@ -75,7 +75,7 @@ class HScript extends SScript setClass(flixel.tweens.FlxTween); setClass(flixel.tweens.FlxEase); set('FlxColor', CustomFlxColor); - setClass(backend.BaseStage.Countdown); + set('Countdown', backend.BaseStage.Countdown); setClass(PlayState); setClass(Paths); setClass(Conductor);