Skip to content

Commit

Permalink
Merge branch 'master' of github.com:Tencent/xLua
Browse files Browse the repository at this point in the history
  • Loading branch information
chexiongsheng committed Mar 7, 2017
2 parents 3ae1e6d + e14a826 commit caf694a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
20 changes: 16 additions & 4 deletions Assets/XLua/Src/LuaEnv.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public LuaTable Global
}
}

public T LoadString<T>(string chunk, string chunkName = "chunk", LuaTable env = null)
public T LoadString<T>(byte[] chunk, string chunkName = "chunk", LuaTable env = null)
{
#if THREAD_SAFT || HOTFIX_ENABLE
lock (luaEnvLock)
Expand All @@ -195,7 +195,7 @@ public T LoadString<T>(string chunk, string chunkName = "chunk", LuaTable env =
var _L = L;
int oldTop = LuaAPI.lua_gettop(_L);

if (LuaAPI.luaL_loadbuffer(_L, chunk, chunkName) != 0)
if (LuaAPI.xluaL_loadbuffer(_L, chunk, chunk.Length, chunkName) != 0)
ThrowExceptionFromError(oldTop);

if (env != null)
Expand All @@ -213,12 +213,18 @@ public T LoadString<T>(string chunk, string chunkName = "chunk", LuaTable env =
#endif
}

public T LoadString<T>(string chunk, string chunkName = "chunk", LuaTable env = null)
{
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(chunk);
return LoadString<T>(bytes, chunkName, env);
}

public LuaFunction LoadString(string chunk, string chunkName = "chunk", LuaTable env = null)
{
return LoadString<LuaFunction>(chunk, chunkName, env);
}

public object[] DoString(string chunk, string chunkName = "chunk", LuaTable env = null)
public object[] DoString(byte[] chunk, string chunkName = "chunk", LuaTable env = null)
{
#if THREAD_SAFT || HOTFIX_ENABLE
lock (luaEnvLock)
Expand All @@ -227,7 +233,7 @@ public object[] DoString(string chunk, string chunkName = "chunk", LuaTable env
var _L = L;
int oldTop = LuaAPI.lua_gettop(_L);
int errFunc = LuaAPI.load_error_func(_L, errorFuncRef);
if (LuaAPI.luaL_loadbuffer(_L, chunk, chunkName) == 0)
if (LuaAPI.xluaL_loadbuffer(_L, chunk, chunk.Length, chunkName) == 0)
{
if (env != null)
{
Expand All @@ -252,6 +258,12 @@ public object[] DoString(string chunk, string chunkName = "chunk", LuaTable env
#endif
}

public object[] DoString(string chunk, string chunkName = "chunk", LuaTable env = null)
{
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(chunk);
return DoString(bytes, chunkName, env);
}

private void AddSearcher(LuaCSFunction searcher, int index)
{
#if THREAD_SAFT || HOTFIX_ENABLE
Expand Down
9 changes: 5 additions & 4 deletions Assets/XLua/Src/StaticLuaCallbacks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,7 @@ internal static int LoadFromResource(RealStatePtr L)
}
else
{
if (LuaAPI.luaL_loadbuffer(L, file.text, "@" + filename) != 0)
if (LuaAPI.xluaL_loadbuffer(L, file.bytes, file.bytes.Length, "@" + filename) != 0)
{
return LuaAPI.luaL_error(L, String.Format("error loading module {0} from resource, {1}",
LuaAPI.lua_tostring(L, 1), LuaAPI.lua_tostring(L, -1)));
Expand Down Expand Up @@ -637,7 +637,7 @@ internal static int LoadFromStreamingAssetsPath(RealStatePtr L)
else
{
UnityEngine.Debug.LogWarning("load lua file from StreamingAssets is obsolete, filename:" + filename);
if (LuaAPI.luaL_loadbuffer(L, www.text, "@" + filename) != 0)
if (LuaAPI.xluaL_loadbuffer(L, www.bytes, www.bytes.Length , "@" + filename) != 0)
{
return LuaAPI.luaL_error(L, String.Format("error loading module {0} from streamingAssetsPath, {1}",
LuaAPI.lua_tostring(L, 1), LuaAPI.lua_tostring(L, -1)));
Expand All @@ -649,10 +649,11 @@ internal static int LoadFromStreamingAssetsPath(RealStatePtr L)
#else
if (File.Exists(filepath))
{
string text = File.ReadAllText(filepath);
// string text = File.ReadAllText(filepath);
var bytes = File.ReadAllBytes(filepath);

UnityEngine.Debug.LogWarning("load lua file from StreamingAssets is obsolete, filename:" + filename);
if (LuaAPI.luaL_loadbuffer(L, text, "@" + filename) != 0)
if (LuaAPI.xluaL_loadbuffer(L, bytes, bytes.Length, "@" + filename) != 0)
{
return LuaAPI.luaL_error(L, String.Format("error loading module {0} from streamingAssetsPath, {1}",
LuaAPI.lua_tostring(L, 1), LuaAPI.lua_tostring(L, -1)));
Expand Down

0 comments on commit caf694a

Please sign in to comment.