summaryrefslogtreecommitdiff
path: root/Client/Assets/Scripts/LuaEngine/HotfixPatch.cs
blob: af06701a46cf4472ace91a557258f01c402bfad1 (plain)
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
using UnityEngine;
using System.Collections.Generic;
using LuaInterface;

public class HotfixPatch
{
    static List<string> list = new List<string>();
    static LuaFunction lua_dispacher_func;
    const string LUA_FILE = "HotfixPatch.lua";

    public static void Init(LuaScriptMgr luamgr)
    {
        luamgr.lua.DoFile(LUA_FILE);
        var func = luamgr.lua.GetFunction("Fetch");
        object[] ret = func.Call();
        Debug.Log("lua regist func cnt:" + (ret.Length));
        list.Clear();
        for (int i = 0, cnt = ret.Length; i < cnt; i++)
        {
            list.Add((string)ret[i]);
        }
        lua_dispacher_func = luamgr.lua.GetFunction("Dispacher");
    }
    

    /// <summary>
    /// 方法由IL层-注入代码调用
    /// </summary>
    public static bool IsRegist(string class_name, string func_name)
    {
        string key = class_name + "|" + func_name;
        return list.Contains(key);
    }

    /// <summary>
    /// 方法由IL层-注入代码调用
    /// </summary>
    public static object Execute(string class_name, string func_name, string type, object[] args)
    {
        if (lua_dispacher_func != null)
        {
            Debug.Log("inject arg len: " + args.Length);
            object val = lua_dispacher_func.Call(class_name, func_name, args)[0];
            if (val != null && typeof(System.Double) == val.GetType()) //先转换成double,再拆箱
            {
                switch (type)
                {
                    case "int":
                    case "Int32":
                        return (int)(double)val;
                    case "uint":
                    case "UInt32":
                        return (uint)(double)val;
                    case "float":
                    case "Single":
                        return (float)(double)val;
                    case "short":
                    case "Int16":
                        return (short)(double)val;
                    case "UInt64":
                    case "ulong":
                        return (ulong)(double)val;
                }
                return val;
            }
            else
            {
                return val;
            }
        }
        return null;
    }
    

}