少年修仙传客户端代码仓库
xingchen Qiu
2019-04-10 b45f84d1a84cb768d0b136fe37ad18364d74af1f
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
94
95
96
97
98
99
100
101
102
103
104
#if USE_UNI_LUA
using LuaAPI = UniLua.Lua;
using RealStatePtr = UniLua.ILuaState;
using LuaCSFunction = UniLua.CSharpFunctionDelegate;
#else
using LuaAPI = XLua.LuaDLL.Lua;
using RealStatePtr = System.IntPtr;
using LuaCSFunction = XLua.LuaDLL.lua_CSFunction;
#endif
 
using System;
<%
require "TemplateCommon"
%>
 
namespace XLua
{
    public partial class DelegateBridge : DelegateBridgeBase
    {
        <%
        ForEachCsList(delegates_groups, function(delegates_group, group_idx)
        local delegate = delegates_group.Key
        local parameters = delegate:GetParameters()
        local in_num = CalcCsList(parameters, function(p) return not (p.IsOut and p.ParameterType.IsByRef) end)
        local out_num = CalcCsList(parameters, function(p) return p.IsOut or p.ParameterType.IsByRef end)
        local in_pos = 0
        local has_return = (delegate.ReturnType.FullName ~= "System.Void")
        local return_type_name = has_return and CsFullTypeName(delegate.ReturnType) or "void"
        local out_idx = has_return and 2 or 1
        if has_return then out_num = out_num + 1 end
        %>
        public <%=return_type_name%> __Gen_Delegate_Imp<%=group_idx%>(<%ForEachCsList(parameters, function(parameter, pi) 
            if pi ~= 0 then 
                %>, <% 
            end
            if parameter.IsOut then 
                %>out <%
            elseif parameter.ParameterType.IsByRef then
                %>ref <%
            end 
            %><%=CsFullTypeName(parameter.ParameterType)%> p<%=pi%><% 
        end) %>)
        {
#if THREAD_SAFE || HOTFIX_ENABLE
            lock (luaEnv.luaEnvLock)
            {
#endif
                RealStatePtr L = luaEnv.rawL;
                int errFunc = LuaAPI.pcall_prepare(L, errorFuncRef, luaReference);
                <%if CallNeedTranslator(delegate, "") then %>ObjectTranslator translator = luaEnv.translator;<%end%>
                <%
                local param_count = parameters.Length
                local has_v_params = param_count > 0 and parameters[param_count - 1].IsParamArray
                ForEachCsList(parameters, function(parameter, pi) 
                    if not (parameter.IsOut and parameter.ParameterType.IsByRef) then 
                        %><%=GetPushStatement(parameter.ParameterType, 'p' .. pi, has_v_params and pi == param_count - 1)%>;
                <% 
                    end
                end) %>
                PCall(L, <%=has_v_params and ((in_num - 1) .. " + (p".. (param_count - 1) .. " == null ? 0 : p" .. (param_count - 1) .. ".Length)" ) or in_num%>, <%=out_num%>, errFunc);
                
                <%ForEachCsList(parameters, function(parameter, pi) 
                    if parameter.IsOut or parameter.ParameterType.IsByRef then 
                        %><%=GetCasterStatement(parameter.ParameterType, "errFunc" .. (" + "..out_idx), 'p' .. pi)%>;
                <%
                    out_idx = out_idx + 1
                    end
                end) %>
                <%if has_return then %><%=GetCasterStatement(delegate.ReturnType, "errFunc + 1", "__gen_ret", true)%>;<% end%>
                LuaAPI.lua_settop(L, errFunc - 1);
                <%if has_return then %>return  __gen_ret;<% end%>
#if THREAD_SAFE || HOTFIX_ENABLE
            }
#endif
        }
        <%end)%>
        
        static DelegateBridge()
        {
            Gen_Flag = true;
        }
        
        public override Delegate GetDelegateByType(Type type)
        {
        <%
        ForEachCsList(delegates_groups, function(delegates_group, group_idx)
            ForEachCsList(delegates_group.Value, function(delegate)
            if delegate.DeclaringType then
            local delegate_type_name = CsFullTypeName(delegate.DeclaringType)
        %>
            if (type == typeof(<%=delegate_type_name%>))
            {
                return new <%=delegate_type_name%>(__Gen_Delegate_Imp<%=group_idx%>);
            }
        <%
            end
            end)
        end)
        %>
            return null;
        }
    }
    
}