diff --git a/docs/DIFFERENCES.md b/docs/DIFFERENCES.md index 923d9fc8..0ecf4800 100644 --- a/docs/DIFFERENCES.md +++ b/docs/DIFFERENCES.md @@ -7,6 +7,7 @@ - Replaced [`flxanimate`](https://github.com/Dot-Stuff/flxanimate) with [`flixel-animate`](https://github.com/MaybeMaru/flixel-animate) for better performance for texture atlases - Replaced [`hxCodec`](https://github.com/polybiusproxy/hxCodec) with [`hxvlc`](https://github.com/ShadowEngineTeam/hxvlc) for better customizability in video cutscenes - Replaced [`SScript`](https://github.com/ShadowEngineTeam/SScript) with [`ShadowScript`](https://github.com/ShadowEngineTeam/SScript) for better compatibility in HScripting +- Replaced [`linc_luajit`](https://github.com/ShadowEngineTeam/linc_luajit) with [`hxluau`](https://github.com/ShadowEngineTeam/hxluau) for better performance and compatibility in Lua scripting (we are so roblox) - Mobile Support (duh) - Applies OpenAL Soft Config For better audio - Slightly more accurate FPS and less RAM Usage diff --git a/hmm.json b/hmm.json index 59ac4a30..9f9217d0 100644 --- a/hmm.json +++ b/hmm.json @@ -3,13 +3,13 @@ { "name": "lime", "type": "git", - "ref": "8274836456bb037fc038a545178d509ddeb6c33e", + "ref": "2117ad5e6d44915dc9de8d6f8f089ce74ad4a27a", "url": "https://github.com/ShadowEngineTeam/lime" }, { "name": "openfl", "type": "git", - "ref": "bd1a0223ef3a9d494b40b1e111cbd044a34dc947", + "ref": "ecb50639f4b63ba062eba5927dd98d72244b17ed", "url": "https://github.com/ShadowEngineTeam/openfl" }, { @@ -27,7 +27,7 @@ { "name": "hxcpp", "type": "git", - "ref": "ee3b507af828a4d03e80b36375bff5d702df71a6", + "ref": "48c7120b7c373556dcdaca57b6d641f9115b0fe9", "url": "https://github.com/ShadowEngineTeam/hxcpp" }, { @@ -43,10 +43,10 @@ "url": "https://github.com/MAJigsaw77/hxdiscord_rpc" }, { - "name": "hxluajit", + "name": "hxluau", "type": "git", - "ref": "015ca428e5dcc8e4f9ecea8a1be5958cf9cea7b1", - "url": "https://github.com/ShadowEngineTeam/hxluajit" + "ref": "526107c4aa1397f26dbcedbfd34fb8d392b28695", + "url": "https://github.com/ShadowEngineTeam/hxluau" }, { "name": "hxgamemode", diff --git a/project.hxp b/project.hxp index 68aa1276..6041cad0 100644 --- a/project.hxp +++ b/project.hxp @@ -19,7 +19,7 @@ class Project extends HXProject static final TITLE_MOBILE:String = "FNF: Shadow Engine"; static final EXECUTABLE:String = "ShadowEngine"; static final VERSION:String = "0.9.0"; - static final BUILD_NUMBER:Int = 727; + static final BUILD_NUMBER:Int = 736; static final COMPANY:String = "ShadowEngineTeam"; static final PACKAGE:String = "org.shadowengineteam.fnf"; static final MAIN_CLASS:String = "backend.Main"; @@ -397,7 +397,7 @@ class Project extends HXProject includeHaxelib("flixel-animate"); if (FEATURE_LUA.isEnabled()) - includeHaxelib("hxluajit"); + includeHaxelib("hxluau"); if (FEATURE_HSCRIPT.isEnabled()) includeHaxelib("SScript"); if (FEATURE_VIDEOS.isEnabled()) diff --git a/source/engine/import.hx b/source/engine/import.hx index f9a40425..39f44a4d 100644 --- a/source/engine/import.hx +++ b/source/engine/import.hx @@ -6,8 +6,8 @@ import backend.Discord; import haxe.Json; // Psych #if FEATURE_LUA -import hxluajit.*; -import hxluajit.Types; +import hxluau.*; +import hxluau.Types; import psychlua.*; #else import psychlua.LuaUtils; diff --git a/source/engine/psychlua/Convert.hx b/source/engine/psychlua/Convert.hx index 9f2c1fe5..142626cf 100644 --- a/source/engine/psychlua/Convert.hx +++ b/source/engine/psychlua/Convert.hx @@ -3,7 +3,7 @@ package psychlua; #if FEATURE_LUA import haxe.Constraints.IMap; import psychlua.FunkinLua.State; -import hxluajit.Types; +import hxluau.Types; /** * Some borrowed code from hxluajit-wrapper. @@ -20,7 +20,7 @@ class Convert callbacks.set(name, func); Lua.pushstring(l, name); - Lua.pushcclosure(l, cpp.Callable.fromStaticFunction(handleCallback), 1); + Lua.pushcclosure(l, cpp.Callable.fromStaticFunction(handleCallback), name, 1); Lua.setglobal(l, name); } @@ -47,14 +47,26 @@ class Convert Lua.pushboolean(l, v == true ? 1 : 0); case TObject: final fields:Array = Reflect.fields(v); - - Lua.createtable(l, fields.length, 0); - - for (field in fields) + final vx:Dynamic = Reflect.field(v, 'x'); + final vy:Dynamic = Reflect.field(v, 'y'); + final vz:Dynamic = Reflect.field(v, 'z'); + if (fields.length == 3 && vx != null && vy != null && vz != null + && (Type.typeof(vx) == TFloat || Type.typeof(vx) == TInt) + && (Type.typeof(vy) == TFloat || Type.typeof(vy) == TInt) + && (Type.typeof(vz) == TFloat || Type.typeof(vz) == TInt)) { - Lua.pushstring(l, field); - toLua(l, Reflect.field(v, field)); - Lua.settable(l, -3); + Lua.pushvector(l, cast(vx, Float), cast(vy, Float), cast(vz, Float)); + } + else + { + Lua.createtable(l, fields.length, 0); + + for (field in fields) + { + Lua.pushstring(l, field); + toLua(l, Reflect.field(v, field)); + Lua.settable(l, -3); + } } case TClass(String): Lua.pushstring(l, cast(v, String)); @@ -83,7 +95,7 @@ class Convert case TNull: Lua.pushnil(l); default: - // trace('toLua: ${Type.typeof(v)}'); + //trace('toLua: ${Type.typeof(v)}'); Lua.pushnil(l); return false; } @@ -105,7 +117,22 @@ class Convert case type if (type == Lua.TTABLE): ret = convertTable(l, idx); case type if (type == Lua.TFUNCTION): - ret = new LuaFunction(cpp.Pointer.fromRaw(l), LuaL.ref(l, Lua.REGISTRYINDEX)); + ret = new LuaFunction(cpp.Pointer.fromRaw(l), Lua.ref(l, Lua.REGISTRYINDEX)); + case type if (type == Lua.TINTEGER): + var isInt:Int = 0; + var isIntPtr = cpp.Pointer.addressOf(isInt); + ret = Lua.tointeger64(l, idx, isIntPtr.raw); + case type if (type == Lua.TVECTOR): + final vec:cpp.RawConstPointer = Lua.tovector(l, idx); + if (vec != null) + ret = {x: (vec[0] : Float), y: (vec[1] : Float), z: (vec[2] : Float)}; + else + ret = null; + case type if (type == Lua.TBUFFER): + var size:cpp.SizeT = 0; + var sizePtr = cpp.Pointer.addressOf(size); + var bufPtr:cpp.RawPointer = Lua.tobuffer(l, idx, sizePtr.raw); + ret = bufPtr != null ? cpp.Pointer.fromRaw(bufPtr) : null; case type if (type == Lua.TUSERDATA || type == Lua.TLIGHTUSERDATA): ret = cpp.Pointer.fromRaw(Lua.touserdata(l, idx)); case type if (type == Lua.TNIL): @@ -127,7 +154,8 @@ class Convert if (status != Lua.OK) { - var error = Lua.tostring(l, -1); + final rawErr = Lua.tostring(l, -1); + final error:String = rawErr != null ? rawErr.toString() : 'Unknown error'; trace('Error calling a function without name: $error'); Lua.pop(l, 1); diff --git a/source/engine/psychlua/FunkinLua.hx b/source/engine/psychlua/FunkinLua.hx index 8ca0156a..96d88120 100644 --- a/source/engine/psychlua/FunkinLua.hx +++ b/source/engine/psychlua/FunkinLua.hx @@ -61,11 +61,16 @@ class FunkinLua lua = LuaL.newstate(); LuaL.openlibs(lua); - // trace('Lua version: ' + Lua.version()); - // trace("LuaJIT version: " + Lua.versionJIT()); + // trace('Luau version: ' + Lua.VERSION); + // trace("Luau release: " + Lua.RELEASE); // LuaL.dostring(lua, CLENSE); + // Luau performance tweaks + Luau.enableCodegen(1); + Luau.bytecodeCacheSetCapacity(256); + Luau.setCompileOptions(2, 1, 0); + this.scriptName = scriptName.trim(); game.luaArray.push(this); @@ -1785,18 +1790,22 @@ class FunkinLua try { var isString:Bool = !FileSystem.exists(scriptName); - var result:Dynamic = null; + var status:Int = 0; if (!isString) - result = #if MODS_ALLOWED sys.FileSystem.exists(scriptName) ? LuaL.dofile(lua, scriptName) : #end LuaL.dostring(lua, File.getContent(scriptName)); + status = #if MODS_ALLOWED sys.FileSystem.exists(scriptName) ? LuaL.dofile(lua, scriptName) : #end LuaL.dostring(lua, File.getContent(scriptName)); else - result = LuaL.dostring(lua, scriptName); + status = LuaL.dostring(lua, scriptName); - var resultStr:String = Lua.tostring(lua, result); - if (resultStr != null && result != 0) + if (status != 0) { - trace(resultStr); - CoolUtil.showPopUp(resultStr, 'Error on lua script!'); - luaTrace('$scriptName\n$resultStr', true, false, FlxColor.RED); + final rawMsg = Lua.tostring(lua, -1); + var errorMsg:String = rawMsg != null ? rawMsg.toString() : getErrorMessage(status); + Lua.pop(lua, 1); + if (errorMsg == null) + errorMsg = getErrorMessage(status); + trace(errorMsg); + CoolUtil.showPopUp(errorMsg, 'Error on lua script!'); + luaTrace('$scriptName\n$errorMsg', true, false, FlxColor.RED); lua = null; return; } @@ -1850,8 +1859,21 @@ class FunkinLua // Checks if it's not successful, then show a error. if (status != Lua.OK) { - var error:String = getErrorMessage(status); - luaTrace("ERROR (" + func + "): " + error, false, false, FlxColor.RED); + var errorStr:String; + final rawErr = Lua.tostring(lua, -1); + var error:String = rawErr != null ? rawErr.toString() : null; + if (error != null) + { + LuaL.traceback(lua, lua, error, 2); + final rawStr = Lua.tostring(lua, -1); + errorStr = rawStr != null ? rawStr.toString() : getErrorMessage(status); + Lua.pop(lua, 2); + } + else + { + errorStr = getErrorMessage(status); + } + luaTrace("ERROR (" + func + "): " + errorStr, false, false, FlxColor.RED); return LuaUtils.Function_Continue; } @@ -1955,16 +1977,10 @@ class FunkinLua if (lua == null) return false; - var result:String = null; Lua.getglobal(lua, variable); - result = Convert.fromLua(lua, -1); + final result:Bool = Lua.toboolean(lua, -1) == 1; Lua.pop(lua, 1); - - if (result == null) - { - return false; - } - return (result == 'true'); + return result; #else return false; #end @@ -1992,7 +2008,8 @@ class FunkinLua public function getErrorMessage(status:Int):String { #if FEATURE_LUA - var v:String = Lua.tostring(lua, -1); + final rawV = Lua.tostring(lua, -1); + var v:String = rawV != null ? rawV.toString() : null; Lua.pop(lua, 1); if (v != null) diff --git a/source/engine/psychlua/LuaFunction.hx b/source/engine/psychlua/LuaFunction.hx index 7c1d8246..8ce9474b 100644 --- a/source/engine/psychlua/LuaFunction.hx +++ b/source/engine/psychlua/LuaFunction.hx @@ -1,7 +1,7 @@ package psychlua; #if FEATURE_LUA -import hxluajit.Types; +import hxluau.Types; /** * Holds a Lua function that can be called from Haxe. @@ -56,7 +56,7 @@ class LuaFunction { if (l != null) { - LuaL.unref(l.raw, Lua.REGISTRYINDEX, ref); + Lua.unref(l.raw, ref); l = null; } } diff --git a/source/engine/psychlua/LuaUtils.hx b/source/engine/psychlua/LuaUtils.hx index bc316b9f..7d051306 100644 --- a/source/engine/psychlua/LuaUtils.hx +++ b/source/engine/psychlua/LuaUtils.hx @@ -663,6 +663,12 @@ class LuaUtils return "table"; case type if (type == Lua.TFUNCTION): return "function"; + case type if (type == Lua.TINTEGER): + return "integer"; + case type if (type == Lua.TVECTOR): + return "vector"; + case type if (type == Lua.TBUFFER): + return "buffer"; case type if (type <= Lua.TNIL): return "nil"; }