using System.Collections;
|
using System.Collections.Generic;
|
using UnityEngine;
|
using XLua;
|
using System.IO;
|
|
public class LuaUtility : SingletonMonobehaviour<LuaUtility>
|
{
|
public readonly static LuaEnv env = new LuaEnv();
|
static float lastGCTime = 0;
|
const float GCInterval = 1;//1 second
|
|
static LuaUtility()
|
{
|
env.AddLoader(new LuaEnv.CustomLoader(LoadAssetBytes));
|
|
env.AddBuildin("rapidjson", XLua.LuaDLL.Lua.LoadRapidJson);
|
env.AddBuildin("lpeg", XLua.LuaDLL.Lua.LoadLpeg);
|
env.AddBuildin("pb", XLua.LuaDLL.Lua.LoadLuaProfobuf);
|
env.AddBuildin("ffi", XLua.LuaDLL.Lua.LoadFFI);
|
}
|
|
public static LuaTable GetNewTable()
|
{
|
return env.NewTable();
|
}
|
|
public static LuaTable Global {
|
get { return env.Global; }
|
}
|
|
public void Init()
|
{
|
Do("global.util");
|
Do("global.logex");
|
Do("global.functions");
|
Do("global.object");
|
Do("global.define");
|
Do("global.stringutil");
|
Do("protocol.LuaProtocalRegister");
|
|
#if !UNITY_EDITOR
|
Do("hotfix.hotfix");
|
#endif
|
lastGCTime = 0f;
|
}
|
|
void Update()
|
{
|
if (Time.time - lastGCTime > GCInterval)
|
{
|
env.Tick();
|
lastGCTime = Time.time;
|
}
|
}
|
|
public static void Do(string _file, string chunkName = "chunk", LuaTable _table = null)
|
{
|
var command = string.Format("require '{0}'", _file);
|
env.DoString(command, chunkName, _table);
|
}
|
|
public static void DoString(string _file, string chunkName = "chunk", LuaTable _table = null)
|
{
|
var content = LoadAsset(ref _file);
|
env.DoString(content, chunkName, _table);
|
}
|
|
private static string LoadAsset(ref string file)
|
{
|
file = file.Replace('.', '/');
|
|
var path = string.Empty;
|
if (AssetSource.luaFromEditor)
|
{
|
path = ResourcesPath.LUA_FODLER + "/" + file + ".lua";
|
return File.ReadAllText(path);
|
}
|
else
|
{
|
var index = file.LastIndexOf("/");
|
var assetBundle = StringUtility.Contact("lua/", file.Substring(0, index)).ToLower();
|
var assetName = file.Substring(index + 1, file.Length - (index + 1));
|
var asset = AssetBundleUtility.Instance.Sync_LoadAsset(assetBundle, assetName) as TextAsset;
|
return asset.text;
|
}
|
}
|
|
private static byte[] LoadAssetBytes(ref string _fileName)
|
{
|
return System.Text.Encoding.UTF8.GetBytes(LoadAsset(ref _fileName));
|
}
|
}
|