Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/pangweiwei/slua
Browse files Browse the repository at this point in the history
  • Loading branch information
pangweiwei committed Jul 27, 2018
2 parents 9757950 + 204c25b commit 03ef3c5
Showing 1 changed file with 80 additions and 9 deletions.
89 changes: 80 additions & 9 deletions Assets/Plugins/Slua_Managed/LuaState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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



Expand All @@ -28,6 +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
Expand Down Expand Up @@ -443,7 +447,9 @@ public class LuaState : IDisposable
IntPtr l_;
int mainThread = 0;
internal WeakDictionary<int, LuaDelegate> delgateMap = new WeakDictionary<int, LuaDelegate>();

#if LUA_DEBUG
static Dictionary<string, string> debugStringMap = new Dictionary<string, string>();
#endif
public int cachedDelegateCount{
get{
return this.delgateMap.AliveCount;
Expand Down Expand Up @@ -485,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);

Expand Down Expand Up @@ -769,6 +775,11 @@ 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");

Expand Down Expand Up @@ -1162,11 +1173,48 @@ 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) {
string str = LuaDLL.lua_tostring(L, -1);
string destString = "";

if (debugStringMap.ContainsKey(str))
{
destString = debugStringMap[str];
}
LuaObject.pushValue(L, destString);
return 1;
}

public object doString(string str)
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;
}
#endif

public object doString(string str)
{
return doString(str,"temp buffer");
}
#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)
{
Expand All @@ -1183,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);
Expand All @@ -1205,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;
Expand Down Expand Up @@ -1268,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 (".", "/");
Expand All @@ -1294,6 +1354,17 @@ internal byte[] loadFile (string fn)
asset = loadAsset("Assets/Slua/jit/jitgc64/" + fn + ".bytes");
}

#if LUA_DEBUG
//get asset's absolute path
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);
#endif
Expand Down

0 comments on commit 03ef3c5

Please sign in to comment.