public static void BuildLua() { SLua.LuaState l = new SLua.LuaState(); foreach (string file in Directory.GetFiles(Path.GetFullPath(Application.dataPath), "*.lua", SearchOption.AllDirectories)) { if (file.StartsWith(Path.GetFullPath(Application.streamingAssetsPath))) { continue; } string output = file.Replace(Path.GetFullPath(Application.dataPath), ""); output = Application.streamingAssetsPath + output; output = Path.GetFullPath(output); //Debug.LogError(Path.GetDirectoryName(output)); Directory.CreateDirectory(Path.GetDirectoryName(output)); l.doString(string.Format(@" local f = io.open('{0}', 'wb') local s = io.open('{1}') f:write(string.dump(loadstring(s:read('*a'), '{1}'))) f:close() s:close() ", output.Replace(@"\", @"/"), file.Replace(@"\", @"/"))); } l.Dispose(); AssetDatabase.Refresh(); }
void Awake() { if (key.Length == 0) { LogSys.LogWarning("key is empty"); return; } string str = ""; if (FromLua == false || !sluaAux.luaSvrManager.getInstance().IsLoaded) { str = GameText.GetStr(key); } else { SLua.LuaState ls = sluaAux.luaSvrManager.getInstance().GetLuaState(); if (ls != null) { SLua.LuaTable tb = ls.getTable("LuaText"); if (tb[key] == null) { str = GameText.GetStr(key); } else { str = tb[key].ToString(); } } } gameObject.GetComponent <UILabel>().text = UtilTools.Wrap(str); }
void CallMethod(KeyCode key, bool bDown) { if (!SLua.LuaSvr.inited || null == SLua.LuaSvr.mainState) { return; } SLua.LuaState l = SLua.LuaSvr.mainState; SLua.LuaFunction func = l.getFunction("OnHotKeyInput"); if (null != func) { func.call(key, bDown); func.Dispose(); } }
static bool ReportLog(LogType type, string str) { if (!SLua.LuaSvr.inited || null == SLua.LuaSvr.mainState) { return(false); } SLua.LuaState l = SLua.LuaSvr.mainState; if (null == l) { return(false); } SLua.LuaFunction func = l.getFunction("OnUnityLog"); if (null != func) { func.call(new object[] { type, str }); func.Dispose(); return(true); } return(false); }
void InitKeyCodeMap() { if (bInited) { return; } if (null == LuaSvr.main || !LuaSvr.main.inited || null == LuaSvr.main.luaState) { return; } SLua.LuaState l = SLua.LuaSvr.main.luaState; if (null == l) { return; } bInited = true; SLua.LuaFunction func = l.getFunction("OnHotKeyCodeMap"); if (null != func) { LuaTable ret = func.call() as LuaTable; foreach (var obj in ret) { object value = obj.value; int ival = Convert.ToInt32(value); KeyCode key = (KeyCode)Enum.ToObject(typeof(KeyCode), ival); if (!sKeyCodeMap.ContainsKey(key)) { sKeyCodeMap.Add(key, true); } else { sKeyCodeMap[key] = true; } } func.Dispose(); } }
public static int Add(IntPtr l) { try{ int top = LuaDLL.lua_gettop(l); if (top == 2) { int delay; checkType(l, 1, out delay); LuaDelegate ld; checkType(l, 2, out ld); Action <int> ua; if (ld.d != null) { ua = (Action <int>)ld.d; } else { IntPtr ml = LuaState.get(l).L; ua = (int id) => { int error = pushTry(ml); pushValue(ml, id); ld.pcall(1, error); LuaDLL.lua_settop(ml, error - 1); }; } ld.d = ua; pushValue(l, true); pushValue(l, add(l, delay, ua)); return(2); } else if (top == 3) { int delay, cycle; checkType(l, 1, out delay); checkType(l, 2, out cycle); LuaDelegate ld; checkType(l, 3, out ld); Func <int, bool> ua; if (ld.d != null) { ua = (Func <int, bool>)ld.d; } else { IntPtr ml = LuaState.get(l).L; ua = (int id) => { int error = pushTry(ml); pushValue(ml, id); ld.pcall(1, error); bool ret = LuaDLL.lua_toboolean(ml, -1); LuaDLL.lua_settop(ml, error - 1); return(ret); }; } ld.d = ua; pushValue(l, true); pushValue(l, add(l, delay, cycle, ua)); return(2); } return(LuaObject.error(l, "Argument error")); }catch (Exception e) { return(LuaObject.error(l, e)); } }
public LuaFunction(LuaState l, int r) : base(l, r) { }
public LuaTable(LuaState state) : base(state, 0) { LuaDLL.lua_newtable(L); valueref = LuaDLL.luaL_ref(L, LuaIndexes.LUA_REGISTRYINDEX); }
public static void init(IntPtr l) { string newindexfun = @" local getmetatable=getmetatable local rawget=rawget local error=error local type=type local function newindex(ud,k,v) local t=getmetatable(ud) repeat local h=rawget(t,k) if h then if h[2] then h[2](ud,v) return else error('property '..k..' is read only') end end t=rawget(t,'__parent') until t==nil error('can not find '..k) end return newindex "; string indexfun = @" local type=type local error=error local rawget=rawget local getmetatable=getmetatable local function index(ud,k) local t=getmetatable(ud) repeat local fun=rawget(t,k) local tp=type(fun) if tp=='function' then return fun elseif tp=='table' then local f=fun[1] if f then return f(ud) else error('property '..k..' is write only') end end t = rawget(t,'__parent') until t==nil error('Can not find '..k) end return index "; LuaState L = LuaState.get(l); newindex_func = (LuaFunction)L.doString(newindexfun); index_func = (LuaFunction)L.doString(indexfun); // object method LuaDLL.lua_createtable(l, 0, 4); addMember(l, ToString); addMember(l, GetHashCode); addMember(l, Equals); addMember(l, GetType); LuaDLL.lua_setfield(l, LuaIndexes.LUA_REGISTRYINDEX, "__luabaseobject"); LuaArray.init(l); LuaVarObject.init(l); LuaDLL.lua_newtable(l); LuaDLL.lua_setglobal(l, DelgateTable); setupPushVar(); }
void OnGUI() { if (!initedStyle) { GUIStyle entryInfoTyle = "CN EntryInfo"; textAreaStyle.richText = true; textAreaStyle.normal.textColor = entryInfoTyle.normal.textColor; initedStyle = true; } string[] statehints = new string[LuaState.statemap.Count]; LuaState[] states = new LuaState[LuaState.statemap.Count]; int n = 0; foreach (var k in LuaState.statemap.Values) { states[n] = k; statehints[n++] = k.Name; } stateIndex = EditorGUILayout.Popup(stateIndex, statehints); LuaState l = null; if (stateIndex >= 0) { l = states[stateIndex]; } if (current != null && current != l) { current.logDelegate -= AddLog; current.errorDelegate -= AddError; } if (current != l && l != null) { l.logDelegate += AddLog; l.errorDelegate += AddError; current = l; } //Output Text Area scrollPosition = GUILayout.BeginScrollView(scrollPosition, GUILayout.Width(Screen.width), GUILayout.ExpandHeight(true)); EditorGUILayout.TextArea(outputText, textAreaStyle, GUILayout.ExpandHeight(true)); GUILayout.EndScrollView(); //Filter Option Toggles GUILayout.BeginHorizontal(); bool oldToggleLog = toggleLog; bool oldToggleErr = toggleErr; toggleLog = GUILayout.Toggle(oldToggleLog, "log", GUILayout.ExpandWidth(false)); toggleErr = GUILayout.Toggle(oldToggleErr, "error", GUILayout.ExpandWidth(false)); //Filter Input Field GUILayout.Space(10f); GUILayout.Label("filter:", GUILayout.ExpandWidth(false)); string oldFilterPattern = filterText; filterText = GUILayout.TextField(oldFilterPattern, GUILayout.Width(200f)); //Menu Buttons if (GUILayout.Button("clear", GUILayout.ExpandWidth(false))) { recordList.Clear(); ConsoleFlush(); } GUILayout.EndHorizontal(); if (toggleLog != oldToggleLog || toggleErr != oldToggleErr || filterText != oldFilterPattern) { ConsoleFlush(); } if (Event.current.type == EventType.Repaint) { inputAreaPosY = GUILayoutUtility.GetLastRect().yMax; } //Drag Spliter ResizeScrollView(); //Input Area GUI.SetNextControlName("Input"); inputText = EditorGUILayout.TextField(inputText, GUILayout.Height(inputAreaHeight)); if (Event.current.isKey && Event.current.type == EventType.KeyUp) { bool refresh = false; if (Event.current.keyCode == KeyCode.Return) { if (inputText != "") { if (history.Count == 0 || history[history.Count - 1] != inputText) { history.Add(inputText); } AddLog(inputText); DoCommand(inputText); inputText = ""; refresh = true; historyIndex = history.Count; } } else if (Event.current.keyCode == KeyCode.UpArrow) { if (history.Count > 0) { historyIndex = historyIndex - 1; if (historyIndex < 0) { historyIndex = 0; } else { inputText = history[historyIndex]; refresh = true; } } } else if (Event.current.keyCode == KeyCode.DownArrow) { if (history.Count > 0) { historyIndex = historyIndex + 1; if (historyIndex > history.Count - 1) { historyIndex = history.Count - 1; } else { inputText = history[historyIndex]; refresh = true; } } } if (refresh) { Repaint(); EditorGUIUtility.editingTextField = false; GUI.FocusControl("Input"); } } }
public LuaTable(LuaState l, int r) : base(l, r) { }
public LuaVar(IntPtr l, int r) { state = LuaState.get(l); valueref = r; }
static int init(IntPtr L) { LuaDLL.lua_pushlightuserdata(L, L); LuaDLL.lua_setglobal(L, "__main_state"); LuaDLL.lua_pushcfunction(L, print); LuaDLL.lua_setglobal(L, "print"); LuaDLL.lua_pushcfunction(L, printerror); LuaDLL.lua_setglobal(L, "printerror"); //pcall add addition statatu code check LuaDLL.lua_pushcfunction(L, pcall); LuaDLL.lua_setglobal(L, "pcall"); pushcsfunction(L, import); LuaDLL.lua_setglobal(L, "import"); string resumefunc = @" local resume = coroutine.resume local function check(co, ok, err, ...) if not ok then UnityEngine.Debug.LogError(debug.traceback(co,err)) end return ok, err, ... end coroutine.resume=function(co,...) return check(co, resume(co,...)) end "; // overload lua's coroutine.resume function to add additional report error LuaState.get(L).doString(resumefunc); // https://github.com/pkulchenko/MobDebug/blob/master/src/mobdebug.lua#L290 // Dump only 3 stacks, or it will return null (I don't know why) string dumpstackfunc = @" local printerror=printerror dumpstack=function() function vars(f) local dump = """" local func = debug.getinfo(f, ""f"").func local i = 1 local locals = {} -- get locals while true do local name, value = debug.getlocal(f, i) if not name then break end if string.sub(name, 1, 1) ~= '(' then dump = dump .. "" "" .. name .. ""="" .. tostring(value) .. ""\n"" end i = i + 1 end -- get varargs (these use negative indices) i = 1 while true do local name, value = debug.getlocal(f, -i) -- `not name` should be enough, but LuaJIT 2.0.0 incorrectly reports `(*temporary)` names here if not name or name ~= ""(*vararg)"" then break end dump = dump .. "" "" .. name .. ""="" .. tostring(value) .. ""\n"" i = i + 1 end -- get upvalues i = 1 while func do -- check for func as it may be nil for tail calls local name, value = debug.getupvalue(func, i) if not name then break end dump = dump .. "" "" .. name .. ""="" .. tostring(value) .. ""\n"" i = i + 1 end return dump end local dump = """" for i = 3, 100 do local source = debug.getinfo(i, ""S"") if not source then break end dump = dump .. ""- stack"" .. tostring(i-2) .. ""\n"" dump = dump .. vars(i+1) if source.what == 'main' then break end end printerror(dump) end "; LuaState.get(L).doString(dumpstackfunc); #if UNITY_ANDROID // fix android performance drop with JIT on according to luajit mailist post LuaState.get(L).doString("if jit then require('jit.opt').start('sizemcode=256','maxmcode=256') for i=1,1000 do end end"); #endif pushcsfunction(L, dofile); LuaDLL.lua_setglobal(L, "dofile"); pushcsfunction(L, loadfile); LuaDLL.lua_setglobal(L, "loadfile"); pushcsfunction(L, loader); int loaderFunc = LuaDLL.lua_gettop(L); LuaDLL.lua_getglobal(L, "package"); #if LUA_5_3 LuaDLL.lua_getfield(L, -1, "searchers"); #else LuaDLL.lua_getfield(L, -1, "loaders"); #endif int loaderTable = LuaDLL.lua_gettop(L); // Shift table elements right for (int e = LuaDLL.lua_rawlen(L, loaderTable) + 1; e > 2; e--) { LuaDLL.lua_rawgeti(L, loaderTable, e - 1); LuaDLL.lua_rawseti(L, loaderTable, e); } //put customer loader at the first position of loader table(in lua52+, which is searchers table) LuaDLL.lua_pushvalue(L, loaderFunc); LuaDLL.lua_rawseti(L, loaderTable, 2); LuaDLL.lua_settop(L, 0); return(0); }
public LuaVar(LuaState l, int r) { state = l; valueref = r; }
public LuaVar() { state = null; }
public static void pushValue(IntPtr l, LuaCSFunction f) { LuaState.pushcsfunction(l, f); }