LuaClassWrap.tpl.txt 26 KB

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