Skip to content

Commit

Permalink
Fixed the problem that the xlua instance could not be created, added …
Browse files Browse the repository at this point in the history
…Loader.RunXLuaCode function,

Remove the ents function
  • Loading branch information
Neptune QTG authored and Neptune QTG committed Nov 5, 2020
1 parent 8a3234c commit 338221e
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 39 deletions.
Binary file modified LuaLoader/LuaLoader.pdb
Binary file not shown.
100 changes: 62 additions & 38 deletions LuaLoader/main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,33 +48,59 @@ private static void SetShowMouse(bool show)
ForceUnlockCursor.UpdateCursorControl();
}

//public static Lua CreateLua()
//{
// var nlua = new Lua();
public static object[] RunXLuaCode(string code,NLua.LuaTable table = null)
{
var xlua = new LuaEnv();

// nlua.State.Encoding = Encoding.UTF8;
// nlua["test"] = "test";
// nlua.LoadCLRPackage();
// nlua.RegisterFunction("Print", null, typeof(Loader).GetMethod("Print"));
// nlua.RegisterFunction("Print2", null, typeof(Loader).GetMethod("Print2"));
try
{
object[] ret;

// var obj = new LuaLoader();
xlua.DoString(LuaLoader.Instance.luacode4, "XLua Init");
xlua.DoString(LuaLoader.Instance.luacode5, "XLua Init");
xlua.DoString("NLua = {}", "XLua Init");
xlua.DoString("require('main')", "XLua Init");

// nlua.RegisterFunction("ReloadLua", obj, obj.GetType().GetMethod("ReloadLua"));
// nlua.DoString(LuaLoader.Instance.luacode2);
// nlua.DoString(LuaLoader.Instance.luacode3);
// nlua.DoString(LuaLoader.Instance.luacode);
if (table != null)
{
var keys = table.Keys;
var vels = table.Values;
var keysa = new object[keys.Count];
var velsa = new object[vels.Count];

// return nlua;
//}
keys.CopyTo(keysa, 0);
vels.CopyTo(velsa, 0);

public static LuaEnv CreateLua()
{
var xlua = new XLua.LuaEnv();
for (var i = 0; i < keysa.Length; i++)
{
var k = keysa[i];
var v = velsa[i];

xlua.Global.SetInPath("NLua."+k.ToString(), v);
}
}

ret = xlua.DoString(code, "XLua Run Lua Code");

xlua.Dispose();

return ret;
}
catch (Exception e)
{
try
{
LuaLoader.lua.lua.GetFunction("hook.Call").Call("OnLuaError", e.ToString());
}
catch (Exception e2)
{
MelonLogger.LogError(e2.ToString());
}

//nlua.Global.SetInPath<LuaFunction>("",);
MelonLogger.LogError(e.ToString());
}

return xlua;
return null;
}

public static Type GetType(string fullName)
Expand All @@ -94,24 +120,6 @@ public static Type GetType(string fullName)
}
}

class ents
{
public static UnityEngine.Object GetPlayer()
{
return UnityEngine.Object.FindObjectOfType<Player>();
}

public static UnityEngine.Object[] GetPlayers()
{
return UnityEngine.Object.FindObjectsOfType<Player>();
}

public static Type GetPlayerType()
{
return typeof(Player);
}
}

public class LuaLoader : MelonMod
{
public static LuaTask.LuaEnv lua;
Expand Down Expand Up @@ -153,6 +161,22 @@ function Print2(...)
";
public string luacode2 = "import('LuaLoader');import('LuaLoader','LuaLoader.Config')";
public string luacode3 = "package.path = '" + Directory.GetCurrentDirectory().Replace("\\", "/") + "/Mods/LuaLoader/modules/?.lua;" + Directory.GetCurrentDirectory().Replace("\\", "/") + "/Mods/LuaLoader/bin/?.dll;" + Directory.GetCurrentDirectory().Replace("\\", "/") + "/Mods/LuaLoader/modules/?.luac'";
public string luacode4 = "package.path = '" + Directory.GetCurrentDirectory().Replace("\\", "/") + "/Mods/LuaLoader/xlua/?.lua;" + Directory.GetCurrentDirectory().Replace("\\", "/") + "/Mods/LuaLoader/bin/?.dll;" + Directory.GetCurrentDirectory().Replace("\\", "/") + "/Mods/LuaLoader/xlua/?.luac'";
public string luacode5 = @"
function print(...)
local r = {}
for i = 1,select('#',...) do
table.insert(r,tostring(select(i,...)))
end
if #r == 0 then
table.insert(r,'nil')
end
CS.MelonLoader.MelonLogger.Log(table.concat(r,' '))
end
";
private static HarmonyInstance harmony;

public override void OnApplicationStart()
Expand Down
10 changes: 9 additions & 1 deletion libs/!!util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ function pass(a)
return pass
end

function tablefunc(a,b,c)
function switch(a,b,c)
b = b or {}
c = c or function() end
setmetatable(b,{__index = function(t,k) return c end})
Expand Down Expand Up @@ -111,6 +111,14 @@ function typeof(o)
return Loader.GetType(s)
end

function xluatypeof(o) -- The result is the same as typeof, it doesn't seem to make any sense
return unpack(Loader.RunXLuaCode([[
return typeof(NLua.obj)
]],{
obj = o
}))
end

function cpairs(t)
assert(isarray(t),'bad argument #1 to \'cpairs\' (c# array expected, got '..type(t)..')')

Expand Down
Binary file added lua54.dll
Binary file not shown.
Binary file added xlua.dll
Binary file not shown.
1 change: 1 addition & 0 deletions xlua/main.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-- When the Loader.RunXLuaCode function is called, this file will be automatically requested

0 comments on commit 338221e

Please sign in to comment.