少年修仙传客户端代码仓库
client_Hale
2019-04-11 9f89e3be35da42eb9ccb44e6589d62f320aa444c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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));
    }
}