少年修仙传客户端代码仓库
client_Hale
2019-04-13 d4b5806a57e4e88a1fc57dd95f17e185f33e4e43
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
#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 XLua;
using System.Collections.Generic;
<%ForEachCsList(namespaces, function(namespace)%>using <%=namespace%>;<%end)%>
<%
require "TemplateCommon"
 
local OpNameMap = {
    op_Addition = "__AddMeta",
    op_Subtraction = "__SubMeta",
    op_Multiply = "__MulMeta",
    op_Division = "__DivMeta",
    op_Equality = "__EqMeta",
    op_UnaryNegation = "__UnmMeta",
    op_LessThan = "__LTMeta",
    op_LessThanOrEqual = "__LEMeta",
    op_Modulus = "__ModMeta",
    op_BitwiseAnd = "__BandMeta",
    op_BitwiseOr = "__BorMeta",
    op_ExclusiveOr = "__BxorMeta",
    op_OnesComplement = "__BnotMeta",
    op_LeftShift = "__ShlMeta",
    op_RightShift = "__ShrMeta",
}
 
local OpCallNameMap = {
    op_Addition = "+",
    op_Subtraction = "-",
    op_Multiply = "*",
    op_Division = "/",
    op_Equality = "==",
    op_UnaryNegation = "-",
    op_LessThan = "<",
    op_LessThanOrEqual = "<=",
    op_Modulus = "%",
    op_BitwiseAnd = "&",
    op_BitwiseOr = "|",
    op_ExclusiveOr = "^",
    op_OnesComplement = "~",
    op_LeftShift = "<<",
    op_RightShift = ">>",
}
 
local obj_method_count = 0
local obj_getter_count = 0
local obj_setter_count = 0
local meta_func_count = operators.Count
local cls_field_count = 1
local cls_getter_count = 0
local cls_setter_count = 0
 
ForEachCsList(methods, function(method)
    if method.IsStatic then
        cls_field_count = cls_field_count + 1
    else
        obj_method_count = obj_method_count + 1
    end 
end)
 
ForEachCsList(events, function(event)
    if event.IsStatic then
        cls_field_count = cls_field_count + 1
    else
        obj_method_count = obj_method_count + 1
    end 
end)
 
ForEachCsList(getters, function(getter)
    if getter.IsStatic then
        if getter.ReadOnly then
            cls_field_count = cls_field_count + 1
        else
            cls_getter_count = cls_getter_count + 1
        end
    else
        obj_getter_count = obj_getter_count + 1
    end 
end)
 
ForEachCsList(setters, function(setter)
    if setter.IsStatic then
        cls_setter_count = cls_setter_count + 1
    else
        obj_setter_count = obj_setter_count + 1
    end 
end)
 
ForEachCsList(lazymembers, function(lazymember)
    if lazymember.IsStatic == 'true' then
        if 'CLS_IDX' == lazymember.Index then
            cls_field_count = cls_field_count + 1
        elseif 'CLS_GETTER_IDX' == lazymember.Index then
            cls_getter_count = cls_getter_count + 1
        elseif 'CLS_SETTER_IDX' == lazymember.Index then
            cls_setter_count = cls_setter_count + 1
        end
    else
        if 'METHOD_IDX' == lazymember.Index then
            obj_method_count = obj_method_count + 1
        elseif 'GETTER_IDX' == lazymember.Index then
            obj_getter_count = obj_getter_count + 1
        elseif 'SETTER_IDX' == lazymember.Index then
            obj_setter_count = obj_setter_count + 1
        end
    end
end)
 
local generic_arg_list, type_constraints = GenericArgumentList(type)
 
%>
namespace XLua.CSObjectWrap
{
    using Utils = XLua.Utils;
    public class <%=CSVariableName(type)%>Wrap<%=generic_arg_list%> <%=type_constraints%>
    {
        public static void __Register(RealStatePtr L)
        {
            ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L);
            System.Type type = typeof(<%=CsFullTypeName(type)%>);
            Utils.BeginObjectRegister(type, L, translator, <%=meta_func_count%>, <%=obj_method_count%>, <%=obj_getter_count%>, <%=obj_setter_count%>);
            <%ForEachCsList(operators, function(operator)%>Utils.RegisterFunc(L, Utils.OBJ_META_IDX, "<%=(OpNameMap[operator.Name]):gsub('Meta', ''):lower()%>", <%=OpNameMap[operator.Name]%>);
            <%end)%>
            <%ForEachCsList(methods, function(method) if not method.IsStatic then %>Utils.RegisterFunc(L, Utils.METHOD_IDX, "<%=method.Name%>", _m_<%=method.Name%>);
            <% end end)%>
            <%ForEachCsList(events, function(event) if not event.IsStatic then %>Utils.RegisterFunc(L, Utils.METHOD_IDX, "<%=event.Name%>", _e_<%=event.Name%>);
            <% end end)%>
            <%ForEachCsList(getters, function(getter) if not getter.IsStatic then %>Utils.RegisterFunc(L, Utils.GETTER_IDX, "<%=getter.Name%>", _g_get_<%=getter.Name%>);
            <%end end)%>
            <%ForEachCsList(setters, function(setter) if not setter.IsStatic then %>Utils.RegisterFunc(L, Utils.SETTER_IDX, "<%=setter.Name%>", _s_set_<%=setter.Name%>);
            <%end end)%>
            <%ForEachCsList(lazymembers, function(lazymember) if lazymember.IsStatic == 'false' then %>Utils.RegisterLazyFunc(L, Utils.<%=lazymember.Index%>, "<%=lazymember.Name%>", type, <%=lazymember.MemberType%>, <%=lazymember.IsStatic%>);
            <%end end)%>
            Utils.EndObjectRegister(type, L, translator, <% if type.IsArray or ((indexers.Count or 0) > 0) then %>__CSIndexer<%else%>null<%end%>, <%if type.IsArray or ((newindexers.Count or 0) > 0) then%>__NewIndexer<%else%>null<%end%>,
                null, null, null);
 
            Utils.BeginClassRegister(type, L, __CreateInstance, <%=cls_field_count%>, <%=cls_getter_count%>, <%=cls_setter_count%>);
            <%ForEachCsList(methods, function(method) if method.IsStatic then %>Utils.RegisterFunc(L, Utils.CLS_IDX, "<%=method.Overloads[0].Name%>", _m_<%=method.Name%>);
            <% end end)%>
            <%ForEachCsList(events, function(event) if event.IsStatic then %>Utils.RegisterFunc(L, Utils.CLS_IDX, "<%=event.Name%>", _e_<%=event.Name%>);
            <% end end)%>
            <%ForEachCsList(getters, function(getter) if getter.IsStatic and getter.ReadOnly then %>Utils.RegisterObject(L, translator, Utils.CLS_IDX, "<%=getter.Name%>", <%=CsFullTypeName(type).."."..getter.Name%>);
            <%end end)%>
            <%ForEachCsList(getters, function(getter) if getter.IsStatic and (not getter.ReadOnly) then %>Utils.RegisterFunc(L, Utils.CLS_GETTER_IDX, "<%=getter.Name%>", _g_get_<%=getter.Name%>);
            <%end end)%>
            <%ForEachCsList(setters, function(setter) if setter.IsStatic then %>Utils.RegisterFunc(L, Utils.CLS_SETTER_IDX, "<%=setter.Name%>", _s_set_<%=setter.Name%>);
            <%end end)%>
            <%ForEachCsList(lazymembers, function(lazymember) if lazymember.IsStatic == 'true' then %>Utils.RegisterLazyFunc(L, Utils.<%=lazymember.Index%>, "<%=lazymember.Name%>", type, <%=lazymember.MemberType%>, <%=lazymember.IsStatic%>);
            <%end end)%>
            Utils.EndClassRegister(type, L, translator);
        }
        
        [MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
        static int __CreateInstance(RealStatePtr L)
        {
            <% 
            if constructors.Count == 0 and (not type.IsValueType)  then 
            %>return LuaAPI.luaL_error(L, "<%=CsFullTypeName(type)%> does not have a constructor!");<% 
            else %>
            try {
                ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L);
                <% 
                local hasZeroParamsCtor = false
                ForEachCsList(constructors, function(constructor, ci)
                    local parameters = constructor:GetParameters()
                    if parameters.Length == 0 then
                        hasZeroParamsCtor = true
                    end
                    local def_count = constructor_def_vals[ci]
                    local param_count = parameters.Length
                    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 real_param_count = param_count - def_count
                    local has_v_params = param_count > 0 and IsParams(parameters[param_count - 1])
                    local in_pos = 0
                %>if(LuaAPI.lua_gettop(L) <%=has_v_params and ">=" or "=="%> <%=in_num + 1 - def_count - (has_v_params and 1 or 0)%><%ForEachCsList(parameters, function(parameter, pi) 
                if pi >= real_param_count then return end 
                local parameterType = parameter.ParameterType
                if has_v_params and pi == param_count - 1 then  parameterType = parameterType:GetElementType() end
                if not (parameter.IsOut and parameter.ParameterType.IsByRef) then in_pos = in_pos + 1
                %> && <%=GetCheckStatement(parameterType, in_pos+1, has_v_params and pi == param_count - 1)%><% 
                end
                end)%>)
                {
                    <%ForEachCsList(parameters, function(parameter, pi) 
                    if pi >= real_param_count then return end 
                    %><%=GetCasterStatement(parameter.ParameterType, pi+2, LocalName(parameter.Name), true, has_v_params and pi == param_count - 1)%>;
                    <%end)%>
                    <%=CsFullTypeName(type)%> gen_ret = new <%=CsFullTypeName(type)%>(<%ForEachCsList(parameters, function(parameter, pi) if pi >= real_param_count then return end; if pi ~=0 then %><%=', '%><% end ;if parameter.IsOut and parameter.ParameterType.IsByRef then %>out <% elseif parameter.ParameterType.IsByRef then %>ref <% end %><%=LocalName(parameter.Name)%><% end)%>);
                    <%=GetPushStatement(type, "gen_ret")%>;
                    <%local in_pos = 0
                    ForEachCsList(parameters, function(parameter, pi)
                        if pi >= real_param_count then return end
                        if not (parameter.IsOut and parameter.ParameterType.IsByRef) then 
                            in_pos = in_pos + 1
                        end
                        if parameter.ParameterType.IsByRef then
                        %><%=GetPushStatement(parameter.ParameterType:GetElementType(), LocalName(parameter.Name))%>;
                        <%if not parameter.IsOut and parameter.ParameterType.IsByRef and NeedUpdate(parameter.ParameterType) then 
                  %><%=GetUpdateStatement(parameter.ParameterType:GetElementType(), in_pos+1, LocalName(parameter.Name))%>;
                        <%end%>
                    <%
                        end
                    end)
                    %>
                    return <%=out_num + 1%>;
                }
                <%end)
                if (not hasZeroParamsCtor) and type.IsValueType then
                %>
                if (LuaAPI.lua_gettop(L) == 1)
                {
                    <%=GetPushStatement(type, "default(" .. CsFullTypeName(type).. ")")%>;
                    return 1;
                }
                <%end%>
            }
            catch(System.Exception gen_e) {
                return LuaAPI.luaL_error(L, "c# exception:" + gen_e);
            }
            return LuaAPI.luaL_error(L, "invalid arguments to <%=CsFullTypeName(type)%> constructor!");
            <% end %>
        }
        
        <% if type.IsArray or ((indexers.Count or 0) > 0) then %>
        [MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
        public static int __CSIndexer(RealStatePtr L)
        {
            <%if type.IsArray then %>
            try {
                ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L);
            
                if (<%=GetCheckStatement(type, 1)%> && LuaAPI.lua_isnumber(L, 2))
                {
                    int index = (int)LuaAPI.lua_tonumber(L, 2);
                    <%=GetSelfStatement(type)%>;
                    LuaAPI.lua_pushboolean(L, true);
                    <%=GetPushStatement(type:GetElementType(), "gen_to_be_invoked[index]")%>;
                    return 2;
                }
            }
            catch(System.Exception gen_e) {
                return LuaAPI.luaL_error(L, "c# exception:" + gen_e);
            }
            <%elseif indexers.Count > 0 then
            %>try {
                ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L);
                <%
                    ForEachCsList(indexers, function(indexer)
                        local paramter = indexer:GetParameters()[0]
                %>
                if (<%=GetCheckStatement(type, 1)%> && <%=GetCheckStatement(paramter.ParameterType, 2)%>)
                {
                    
                    <%=GetSelfStatement(type)%>;
                    <%=GetCasterStatement(paramter.ParameterType, 2, "index", true)%>;
                    LuaAPI.lua_pushboolean(L, true);
                    <%=GetPushStatement(indexer.ReturnType, "gen_to_be_invoked[index]")%>;
                    return 2;
                }
                <%end)%>
            }
            catch(System.Exception gen_e) {
                return LuaAPI.luaL_error(L, "c# exception:" + gen_e);
            }
            <%end%>
            LuaAPI.lua_pushboolean(L, false);
            return 1;
        }
        <% end %>
        
        <%if type.IsArray or ((newindexers.Count or 0) > 0) then%>
        [MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
        public static int __NewIndexer(RealStatePtr L)
        {
            <%if type.IsArray or newindexers.Count > 0 then %>ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L);<%end%>
            <%if type.IsArray then 
                local elementType = type:GetElementType()
            %>
            try {
                if (<%=GetCheckStatement(type, 1)%> && LuaAPI.lua_isnumber(L, 2) && <%=GetCheckStatement(elementType, 3)%>)
                {
                    int index = (int)LuaAPI.lua_tonumber(L, 2);
                    <%=GetSelfStatement(type)%>;
                    <%=GetCasterStatement(elementType, 3, "gen_to_be_invoked[index]")%>;
                    LuaAPI.lua_pushboolean(L, true);
                    return 1;
                }
            }
            catch(System.Exception gen_e) {
                return LuaAPI.luaL_error(L, "c# exception:" + gen_e);
            }
            <%elseif newindexers.Count > 0 then%>
            try {
                <%ForEachCsList(newindexers, function(newindexer)
                        local keyType = newindexer:GetParameters()[0].ParameterType
                        local valueType = newindexer:GetParameters()[1].ParameterType
                %>
                if (<%=GetCheckStatement(type, 1)%> && <%=GetCheckStatement(keyType, 2)%> && <%=GetCheckStatement(valueType, 3)%>)
                {
                    
                    <%=GetSelfStatement(type)%>;
                    <%=GetCasterStatement(keyType, 2, "key", true)%>;
                    <%if IsStruct(valueType) then%><%=GetCasterStatement(valueType, 3, "gen_value", true)%>;
                    gen_to_be_invoked[key] = gen_value;<%else
                  %><%=GetCasterStatement(valueType, 3, "gen_to_be_invoked[key]")%>;<%end%>
                    LuaAPI.lua_pushboolean(L, true);
                    return 1;
                }
                <%end)%>
            }
            catch(System.Exception gen_e) {
                return LuaAPI.luaL_error(L, "c# exception:" + gen_e);
            }
            <%end%>
            LuaAPI.lua_pushboolean(L, false);
            return 1;
        }
        <% end %>
        
        <%ForEachCsList(operators, function(operator) %>
        [MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
        static int <%=OpNameMap[operator.Name]%>(RealStatePtr L)
        {
            <% if operator.Name ~= "op_UnaryNegation" and operator.Name ~= "op_OnesComplement"  then %>
            try {
                ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L);
            <%ForEachCsList(operator.Overloads, function(overload)
                local left_param = overload:GetParameters()[0]
                local right_param = overload:GetParameters()[1]
            %>
            
                if (<%=GetCheckStatement(left_param.ParameterType, 1)%> && <%=GetCheckStatement(right_param.ParameterType, 2)%>)
                {
                    <%=GetCasterStatement(left_param.ParameterType, 1, "leftside", true)%>;
                    <%=GetCasterStatement(right_param.ParameterType, 2, "rightside", true)%>;
                    
                    <%=GetPushStatement(overload.ReturnType, "leftside " .. OpCallNameMap[operator.Name] .. " rightside")%>;
                    
                    return 1;
                }
            <%end)%>
            }
            catch(System.Exception gen_e) {
                return LuaAPI.luaL_error(L, "c# exception:" + gen_e);
            }
            return LuaAPI.luaL_error(L, "invalid arguments to right hand of <%=OpCallNameMap[operator.Name]%> operator, need <%=CsFullTypeName(type)%>!");
            <%else%>
            ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L);
            try {
                <%=GetCasterStatement(type, 1, "rightside", true)%>;
                <%=GetPushStatement(operator.Overloads[0].ReturnType, OpCallNameMap[operator.Name] .. " rightside")%>;
            } catch(System.Exception gen_e) {
                return LuaAPI.luaL_error(L, "c# exception:" + gen_e);
            }
            return 1;
            <%end%>
        }
        <%end)%>
        
        <%ForEachCsList(methods, function(method)%>
        [MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
        static int _m_<%=method.Name%>(RealStatePtr L)
        {
            try {
            <%
            local need_obj = not method.IsStatic
            if MethodCallNeedTranslator(method) then
            %>
                ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L);
            <%end%>
            <%if need_obj then%>
                <%=GetSelfStatement(type)%>;
            <%end%>
            <%if method.Overloads.Count > 1 then%>
                int gen_param_count = LuaAPI.lua_gettop(L);
            <%end%>
                <%ForEachCsList(method.Overloads, function(overload, oi)
                local parameters = MethodParameters(overload)
                local in_num = CalcCsList(parameters, function(p) return not (p.IsOut and p.ParameterType.IsByRef) end)
                local param_offset = method.IsStatic and 0 or 1
                local out_num = CalcCsList(parameters, function(p) return p.IsOut or p.ParameterType.IsByRef end)
                local in_pos = 0
                local has_return = (overload.ReturnType.FullName ~= "System.Void")
                local def_count = method.DefaultValues[oi]
                local param_count = parameters.Length
                local real_param_count = param_count - def_count
                local has_v_params = param_count > 0 and IsParams(parameters[param_count - 1])
                if method.Overloads.Count > 1 then
                %>if(gen_param_count <%=has_v_params and ">=" or "=="%> <%=in_num+param_offset-def_count - (has_v_params and 1 or 0)%><%
                    ForEachCsList(parameters, function(parameter, pi)
                        if pi >= real_param_count then return end
                        local parameterType = parameter.ParameterType
                        if has_v_params and pi == param_count - 1 then  parameterType = parameterType:GetElementType() end
                        if not (parameter.IsOut and parameter.ParameterType.IsByRef) then in_pos = in_pos + 1; 
                        %>&& <%=GetCheckStatement(parameterType , in_pos+param_offset, has_v_params and pi == param_count - 1)%><% 
                        end 
                    end)%>) <%end%>
                {
                    <%if overload.Name == "get_Item" and overload.IsSpecialName then
                        local keyType = overload:GetParameters()[0].ParameterType%>
                    <%=GetCasterStatement(keyType, 2, "key", true)%>;
                    <%=GetPushStatement(overload.ReturnType, "gen_to_be_invoked[key]")%>;
                    <%elseif overload.Name == "set_Item" and overload.IsSpecialName then
                        local keyType = overload:GetParameters()[0].ParameterType
                        local valueType = overload:GetParameters()[1].ParameterType%>
                    <%=GetCasterStatement(keyType, 2, "key", true)%>;
                    <%=GetCasterStatement(valueType, 3, "gen_to_be_invoked[key]")%>;
                    <% else
                    in_pos = 0;
                    ForEachCsList(parameters, function(parameter, pi) 
                        if pi >= real_param_count then return end
                        %><%if not (parameter.IsOut and parameter.ParameterType.IsByRef) then 
                            in_pos = in_pos + 1
                        %><%=GetCasterStatement(parameter.ParameterType, in_pos+param_offset, LocalName(parameter.Name), true, has_v_params and pi == param_count - 1)%><%
                        else%><%=CsFullTypeName(parameter.ParameterType)%> <%=LocalName(parameter.Name)%><%end%>;
                    <%end)%>
                    <%
                    if has_return then
                        %>    <%=CsFullTypeName(overload.ReturnType)%> gen_ret = <%
                    end
                    %><%if method.IsStatic then
                    %><%=CsFullTypeName(type).."."..UnK(overload.Name)%><%
                    else
                    %>gen_to_be_invoked.<%=UnK(overload.Name)%><%
                    end%>( <%ForEachCsList(parameters, function(parameter, pi) 
                        if pi >= real_param_count then return end
                        if pi ~= 0 then %>, <% end; if parameter.IsOut and parameter.ParameterType.IsByRef then %>out <% elseif parameter.ParameterType.IsByRef then %>ref <% end %><%=LocalName(parameter.Name)%><% end) %> );
                    <%
                    if has_return then
                    %>    <%=GetPushStatement(overload.ReturnType, "gen_ret")%>;
                    <%
                    end
                    local in_pos = 0
                    ForEachCsList(parameters, function(parameter, pi)
                        if pi >= real_param_count then return end
                        if not (parameter.IsOut and parameter.ParameterType.IsByRef) then 
                            in_pos = in_pos + 1
                        end
                        if parameter.ParameterType.IsByRef then
                        %><%=GetPushStatement(parameter.ParameterType:GetElementType(), LocalName(parameter.Name))%>;
                        <%if not parameter.IsOut and parameter.ParameterType.IsByRef and NeedUpdate(parameter.ParameterType) then 
                  %><%=GetUpdateStatement(parameter.ParameterType:GetElementType(), in_pos+param_offset, LocalName(parameter.Name))%>;
                        <%end%>
                    <%
                        end
                    end)
                    end
                    %>
                    <%if NeedUpdate(type) and not method.IsStatic then%>
                        <%=GetUpdateStatement(type, 1, "gen_to_be_invoked")%>;
                    <%end%>
                    
                    return <%=out_num+(has_return and 1 or 0)%>;
                }
                <% end)%>
            } catch(System.Exception gen_e) {
                return LuaAPI.luaL_error(L, "c# exception:" + gen_e);
            }
            <%if method.Overloads.Count > 1 then%>
            return LuaAPI.luaL_error(L, "invalid arguments to <%=CsFullTypeName(type)%>.<%=method.Overloads[0].Name%>!");
            <%end%>
        }
        <% end)%>
        
        
        <%ForEachCsList(getters, function(getter) 
        if getter.IsStatic and getter.ReadOnly then return end --readonly static
        %>
        [MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
        static int _g_get_<%=getter.Name%>(RealStatePtr L)
        {
            try {
            <%if AccessorNeedTranslator(getter) then %>    ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L);<%end%>
            <%if not getter.IsStatic then%>
                <%=GetSelfStatement(type)%>;
                <%=GetPushStatement(getter.Type, "gen_to_be_invoked."..UnK(getter.Name))%>;<% else %>    <%=GetPushStatement(getter.Type, CsFullTypeName(type).."."..UnK(getter.Name))%>;<% end%>
            } catch(System.Exception gen_e) {
                return LuaAPI.luaL_error(L, "c# exception:" + gen_e);
            }
            return 1;
        }
        <%end)%>
        
        <%ForEachCsList(setters, function(setter)
        local is_struct = IsStruct(setter.Type)
        %>
        [MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
        static int _s_set_<%=setter.Name%>(RealStatePtr L)
        {
            try {
                <%if AccessorNeedTranslator(setter) then %>ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L);<%end%>
            <%if not setter.IsStatic then %>
                <%=GetSelfStatement(type)%>;
                <%if is_struct then %><%=GetCasterStatement(setter.Type, 2, "gen_value", true)%>;
                gen_to_be_invoked.<%=UnK(setter.Name)%> = gen_value;<% else 
              %><%=GetCasterStatement(setter.Type, 2, "gen_to_be_invoked." .. UnK(setter.Name))%>;<%end
            else 
                if is_struct then %><%=GetCasterStatement(setter.Type, 1, "gen_value", true)%>;
                <%=CsFullTypeName(type)%>.<%=UnK(setter.Name)%> = gen_value;<%else
          %>    <%=GetCasterStatement(setter.Type, 1, CsFullTypeName(type) .."." .. UnK(setter.Name))%>;<%end
            end%>
            <%if NeedUpdate(type) and not setter.IsStatic then%>
                <%=GetUpdateStatement(type, 1, "gen_to_be_invoked")%>;
            <%end%>
            } catch(System.Exception gen_e) {
                return LuaAPI.luaL_error(L, "c# exception:" + gen_e);
            }
            return 0;
        }
        <%end)%>
        
        <%ForEachCsList(events, function(event) if not event.IsStatic then %>
        [MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
        static int _e_<%=event.Name%>(RealStatePtr L)
        {
            try {
                ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L);
                int gen_param_count = LuaAPI.lua_gettop(L);
            <%=GetSelfStatement(type)%>;
                <%=GetCasterStatement(event.Type, 3, "gen_delegate", true)%>;
                if (gen_delegate == null) {
                    return LuaAPI.luaL_error(L, "#3 need <%=CsFullTypeName(event.Type)%>!");
                }
                
                if (gen_param_count == 3)
                {
                    <%if event.CanAdd then%>
                    if (LuaAPI.xlua_is_eq_str(L, 2, "+")) {
                        gen_to_be_invoked.<%=UnK(event.Name)%> += gen_delegate;
                        return 0;
                    } 
                    <%end%>
                    <%if event.CanRemove then%>
                    if (LuaAPI.xlua_is_eq_str(L, 2, "-")) {
                        gen_to_be_invoked.<%=UnK(event.Name)%> -= gen_delegate;
                        return 0;
                    } 
                    <%end%>
                }
            } catch(System.Exception gen_e) {
                return LuaAPI.luaL_error(L, "c# exception:" + gen_e);
            }
            LuaAPI.luaL_error(L, "invalid arguments to <%=CsFullTypeName(type)%>.<%=event.Name%>!");
            return 0;
        }
        <%end end)%>
        
        <%ForEachCsList(events, function(event) if event.IsStatic then %>
        [MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
        static int _e_<%=event.Name%>(RealStatePtr L)
        {
            try {
                ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L);
                int gen_param_count = LuaAPI.lua_gettop(L);
                <%=GetCasterStatement(event.Type, 2, "gen_delegate", true)%>;
                if (gen_delegate == null) {
                    return LuaAPI.luaL_error(L, "#2 need <%=CsFullTypeName(event.Type)%>!");
                }
                
                <%if event.CanAdd then%>
                if (gen_param_count == 2 && LuaAPI.xlua_is_eq_str(L, 1, "+")) {
                    <%=CsFullTypeName(type)%>.<%=UnK(event.Name)%> += gen_delegate;
                    return 0;
                } 
                <%end%>
                <%if event.CanRemove then%>
                if (gen_param_count == 2 && LuaAPI.xlua_is_eq_str(L, 1, "-")) {
                    <%=CsFullTypeName(type)%>.<%=UnK(event.Name)%> -= gen_delegate;
                    return 0;
                } 
                <%end%>
            } catch(System.Exception gen_e) {
                return LuaAPI.luaL_error(L, "c# exception:" + gen_e);
            }
            return LuaAPI.luaL_error(L, "invalid arguments to <%=CsFullTypeName(type)%>.<%=event.Name%>!");
        }
        <%end end)%>
    }
}