From 9977b5bc13defc7dc5610f00ff86d7a441f295a3 Mon Sep 17 00:00:00 2001 From: "TENCENT\\stuartwang" Date: Thu, 19 Jul 2018 11:36:34 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=86dostring?= =?UTF-8?q?=E8=BD=AC=E6=8D=A2md5,=20=E5=92=8C=E6=A0=B9=E6=8D=AEmd5?= =?UTF-8?q?=E5=8F=8D=E6=9F=A5string=E7=9A=84=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/Plugins/Slua_Managed/LuaState.cs | 43 +++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/Assets/Plugins/Slua_Managed/LuaState.cs b/Assets/Plugins/Slua_Managed/LuaState.cs index b9e2fa36..671ef7e0 100644 --- a/Assets/Plugins/Slua_Managed/LuaState.cs +++ b/Assets/Plugins/Slua_Managed/LuaState.cs @@ -28,6 +28,7 @@ namespace SLua using System.Collections.Generic; using System.Collections; using System.Text; + using System.Security.Cryptography; #if !SLUA_STANDALONE using UnityEngine; #else @@ -443,6 +444,7 @@ public class LuaState : IDisposable IntPtr l_; int mainThread = 0; internal WeakDictionary delgateMap = new WeakDictionary(); + static Dictionary debugStringMap = new Dictionary(); public int cachedDelegateCount{ get{ @@ -769,6 +771,9 @@ static int init(IntPtr L) // LuaDLL.lua_pushcfunction(L, pcall); // LuaDLL.lua_setglobal(L, "pcall"); + LuaDLL.lua_pushcfunction(L, getStringFromMD5); + LuaDLL.lua_setglobal(L, "getStringFromMD5"); + pushcsfunction(L, import); LuaDLL.lua_setglobal(L, "import"); @@ -1163,9 +1168,43 @@ static public void pushcsfunction(IntPtr L, LuaCSFunction function) LuaDLL.lua_call(L, 1, 1); } - public object doString(string str) + [MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))] + static public int getStringFromMD5(IntPtr L) { + int n = LuaDLL.lua_gettop(L); + string str = LuaDLL.lua_tostring(L, -1); + string destString = ""; + + if (debugStringMap.ContainsKey(str)) + { + destString = debugStringMap[str]; + } + LuaObject.pushValue(L, destString); + return 1; + } + + static public string getStringMD5(string str) + { + MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider(); + byte[] bytes = System.Text.Encoding.UTF8.GetBytes(str); + byte[] md5Data = md5.ComputeHash(bytes, 0, bytes.Length); + md5.Clear(); + + string destString = ""; + for (int i = 0; i < md5Data.Length; i++) + { + destString += System.Convert.ToString(md5Data[i], 16).PadLeft(2, '0'); + } + destString = destString.PadLeft(32, '0'); + return destString; + } + + public object doString(string str) { - return doString(str,"temp buffer"); + //get str's md5 string + string stringMd5 = getStringMD5(str); + debugStringMap.Add(stringMd5, str); + + return doString(str, stringMd5); } public object doString(string str, string chunkname) From 5b6e652e32e42ce2fa4d410dea612d34c8446f2a Mon Sep 17 00:00:00 2001 From: "TENCENT\\stuartwang" Date: Thu, 19 Jul 2018 12:17:24 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=86=E5=AE=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/Plugins/Slua_Managed/LuaState.cs | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/Assets/Plugins/Slua_Managed/LuaState.cs b/Assets/Plugins/Slua_Managed/LuaState.cs index 671ef7e0..ad808d13 100644 --- a/Assets/Plugins/Slua_Managed/LuaState.cs +++ b/Assets/Plugins/Slua_Managed/LuaState.cs @@ -19,6 +19,7 @@ // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. +//#define LUA_DEBUG @@ -28,7 +29,9 @@ namespace SLua using System.Collections.Generic; using System.Collections; using System.Text; +#if LUA_DEBUG using System.Security.Cryptography; +#endif #if !SLUA_STANDALONE using UnityEngine; #else @@ -444,8 +447,9 @@ public class LuaState : IDisposable IntPtr l_; int mainThread = 0; internal WeakDictionary delgateMap = new WeakDictionary(); +#if LUA_DEBUG static Dictionary debugStringMap = new Dictionary(); - +#endif public int cachedDelegateCount{ get{ return this.delgateMap.AliveCount; @@ -771,8 +775,10 @@ static int init(IntPtr L) // LuaDLL.lua_pushcfunction(L, pcall); // LuaDLL.lua_setglobal(L, "pcall"); +#if LUA_DEBUG LuaDLL.lua_pushcfunction(L, getStringFromMD5); LuaDLL.lua_setglobal(L, "getStringFromMD5"); +#endif pushcsfunction(L, import); LuaDLL.lua_setglobal(L, "import"); @@ -1167,7 +1173,7 @@ static public void pushcsfunction(IntPtr L, LuaCSFunction function) LuaDLL.lua_pushcclosure(L, function, 0); LuaDLL.lua_call(L, 1, 1); } - +#if LUA_DEBUG [MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))] static public int getStringFromMD5(IntPtr L) { int n = LuaDLL.lua_gettop(L); @@ -1197,15 +1203,19 @@ static public string getStringMD5(string str) destString = destString.PadLeft(32, '0'); return destString; } - +#endif + public object doString(string str) { +#if LUA_DEBUG //get str's md5 string string stringMd5 = getStringMD5(str); debugStringMap.Add(stringMd5, str); - return doString(str, stringMd5); - } +#else + return doString(str, "temp buffer"); +#endif + } public object doString(string str, string chunkname) { From 584e442b69f9fd2de54cbe5249b9cdd5290441d1 Mon Sep 17 00:00:00 2001 From: "TENCENT\\stuartwang" Date: Fri, 20 Jul 2018 12:23:39 +0800 Subject: [PATCH 3/4] in luadebug and editor mode, return assets's absolute path which used by debugger --- Assets/Plugins/Slua_Managed/LuaState.cs | 29 ++++++++++++++++++++----- 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/Assets/Plugins/Slua_Managed/LuaState.cs b/Assets/Plugins/Slua_Managed/LuaState.cs index ad808d13..7c1f9b60 100644 --- a/Assets/Plugins/Slua_Managed/LuaState.cs +++ b/Assets/Plugins/Slua_Managed/LuaState.cs @@ -491,7 +491,7 @@ public IntPtr handle public int Top { get { return LuaDLL.lua_gettop(L); } } - public delegate byte[] LoaderDelegate(string fn); + public delegate byte[] LoaderDelegate(string fn, ref string absoluteFn); public delegate void OutputDelegate(string msg); public delegate void PushVarDelegate(IntPtr l, object o); @@ -1176,7 +1176,6 @@ static public void pushcsfunction(IntPtr L, LuaCSFunction function) #if LUA_DEBUG [MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))] static public int getStringFromMD5(IntPtr L) { - int n = LuaDLL.lua_gettop(L); string str = LuaDLL.lua_tostring(L, -1); string destString = ""; @@ -1232,9 +1231,15 @@ internal static int loader(IntPtr L) { LuaState state = LuaState.get(L); string fileName = LuaDLL.lua_tostring(L, 1); - byte[] bytes = state.loadFile(fileName); + string absoluteFn = ""; + byte[] bytes = state.loadFile(fileName, ref absoluteFn); if (bytes != null) { +#if LUA_DEBUG + if (absoluteFn != "") { + fileName = absoluteFn; + } +#endif if (LuaDLL.luaL_loadbuffer(L, bytes, bytes.Length, "@" + fileName) == 0) { LuaObject.pushValue(L, true); @@ -1254,13 +1259,19 @@ internal static int loader(IntPtr L) public object doFile(string fn) { - byte[] bytes = loadFile(fn); + string absoluteFn = ""; + byte[] bytes = loadFile(fn , ref absoluteFn); if (bytes == null) { Logger.LogError(string.Format("Can't find {0}", fn)); return null; } +#if LUA_DEBUG + if (absoluteFn != "") { + fn = absoluteFn; + } +#endif object obj; if (doBuffer(bytes, "@" + fn, out obj)) return obj; @@ -1317,12 +1328,12 @@ static TextAsset loadAsset(string fn) { } #endif - internal byte[] loadFile (string fn) + internal byte[] loadFile (string fn, ref string absoluteFn) { try { byte[] bytes; if (loaderDelegate != null) - bytes = loaderDelegate (fn); + bytes = loaderDelegate (fn, ref absoluteFn); else { #if !SLUA_STANDALONE fn = fn.Replace (".", "/"); @@ -1343,6 +1354,12 @@ internal byte[] loadFile (string fn) asset = loadAsset("Assets/Slua/jit/jitgc64/" + fn + ".bytes"); } +#if LUA_DEBUG + //get asset's absolute path + string projectFn = UnityEditor.AssetDatabase.GetAssetPath(asset); + int idx = projectFn.IndexOf("/"); + absoluteFn = Application.dataPath + projectFn.Substring(idx); +#endif #else asset = (TextAsset)Resources.Load(fn); #endif From 3214539af41adce25a7d7a7185ac6502b4635497 Mon Sep 17 00:00:00 2001 From: stuartwang Date: Fri, 20 Jul 2018 14:10:12 +0800 Subject: [PATCH 4/4] do some exception handling --- Assets/Plugins/Slua_Managed/LuaState.cs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/Assets/Plugins/Slua_Managed/LuaState.cs b/Assets/Plugins/Slua_Managed/LuaState.cs index 7c1f9b60..579eae88 100644 --- a/Assets/Plugins/Slua_Managed/LuaState.cs +++ b/Assets/Plugins/Slua_Managed/LuaState.cs @@ -1356,9 +1356,14 @@ internal byte[] loadFile (string fn, ref string absoluteFn) #if LUA_DEBUG //get asset's absolute path - string projectFn = UnityEditor.AssetDatabase.GetAssetPath(asset); - int idx = projectFn.IndexOf("/"); - absoluteFn = Application.dataPath + projectFn.Substring(idx); + string assetFn = UnityEditor.AssetDatabase.GetAssetPath(asset); + if (assetFn != ""){ + //find out asset path, remove assetFn's first "Asset/" + int idx = assetFn.IndexOf("/"); + if(idx > 0){ + absoluteFn = Application.dataPath + assetFn.Substring(idx); + } + } #endif #else asset = (TextAsset)Resources.Load(fn);