LuaInterfaceBridge.tpl.txt 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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;
  12. <%
  13. require "TemplateCommon"
  14. %>
  15. namespace XLua.CSObjectWrap
  16. {
  17. public class <%=CSVariableName(type)%>Bridge : LuaBase, <%=CsFullTypeName(type)%>
  18. {
  19. public static LuaBase __Create(int reference, LuaEnv luaenv)
  20. {
  21. return new <%=CSVariableName(type)%>Bridge(reference, luaenv);
  22. }
  23. public <%=CSVariableName(type)%>Bridge(int reference, LuaEnv luaenv) : base(reference, luaenv)
  24. {
  25. }
  26. <%
  27. ForEachCsList(methods, function(method)
  28. local parameters = method:GetParameters()
  29. local in_num = CalcCsList(parameters, function(p) return not (p.IsOut and p.ParameterType.IsByRef) end)
  30. local out_num = CalcCsList(parameters, function(p) return p.IsOut or p.ParameterType.IsByRef end)
  31. local in_pos = 0
  32. local has_return = (method.ReturnType.FullName ~= "System.Void")
  33. local return_type_name = has_return and CsFullTypeName(method.ReturnType) or "void"
  34. local out_idx = has_return and 2 or 1
  35. if has_return then out_num = out_num + 1 end
  36. %>
  37. <%=return_type_name%> <%=CsFullTypeName(method.DeclaringType)%>.<%=method.Name%>(<%ForEachCsList(parameters, function(parameter, pi)
  38. if pi ~= 0 then
  39. %>, <%
  40. end
  41. if parameter.IsOut and parameter.ParameterType.IsByRef then
  42. %>out <%
  43. elseif parameter.ParameterType.IsByRef then
  44. %>ref <%
  45. end
  46. %><%=CsFullTypeName(parameter.ParameterType)%> <%=parameter.Name%><%
  47. end) %>)
  48. {
  49. #if THREAD_SAFE || HOTFIX_ENABLE
  50. lock (luaEnv.luaEnvLock)
  51. {
  52. #endif
  53. RealStatePtr L = luaEnv.L;
  54. int err_func = LuaAPI.load_error_func(L, luaEnv.errorFuncRef);
  55. <%if CallNeedTranslator(method, "") then %>ObjectTranslator translator = luaEnv.translator;<%end%>
  56. LuaAPI.lua_getref(L, luaReference);
  57. LuaAPI.xlua_pushasciistring(L, "<%=method.Name%>");
  58. if (0 != LuaAPI.xlua_pgettable(L, -2))
  59. {
  60. luaEnv.ThrowExceptionFromError(err_func - 1);
  61. }
  62. if(!LuaAPI.lua_isfunction(L, -1))
  63. {
  64. LuaAPI.xlua_pushasciistring(L, "no such function <%=method.Name%>");
  65. luaEnv.ThrowExceptionFromError(err_func - 1);
  66. }
  67. LuaAPI.lua_pushvalue(L, -2);
  68. LuaAPI.lua_remove(L, -3);
  69. <%
  70. local param_count = parameters.Length
  71. local has_v_params = param_count > 0 and IsParams(parameters[param_count - 1])
  72. ForEachCsList(parameters, function(parameter, pi)
  73. if not (parameter.IsOut and parameter.ParameterType.IsByRef) then
  74. %><%=GetPushStatement(parameter.ParameterType, parameter.Name, has_v_params and pi == param_count - 1)%>;
  75. <%
  76. end
  77. end) %>
  78. int __gen_error = LuaAPI.lua_pcall(L, <%=has_v_params and ((in_num) .. " + (".. parameters[param_count - 1].Name .. " == null ? 0 : " .. parameters[param_count - 1].Name .. ".Length)" ) or (in_num + 1)%>, <%=out_num%>, err_func);
  79. if (__gen_error != 0)
  80. luaEnv.ThrowExceptionFromError(err_func - 1);
  81. <%ForEachCsList(parameters, function(parameter)
  82. if parameter.IsOut or parameter.ParameterType.IsByRef then
  83. %><%=GetCasterStatement(parameter.ParameterType, "err_func" .. (" + "..out_idx), parameter.Name)%>;
  84. <%
  85. out_idx = out_idx + 1
  86. end
  87. end) %>
  88. <%if has_return then %><%=GetCasterStatement(method.ReturnType, "err_func + 1", "__gen_ret", true)%>;<% end%>
  89. LuaAPI.lua_settop(L, err_func - 1);
  90. <%if has_return then %>return __gen_ret;<% end%>
  91. #if THREAD_SAFE || HOTFIX_ENABLE
  92. }
  93. #endif
  94. }
  95. <%end)%>
  96. <%
  97. ForEachCsList(propertys, function(property)
  98. %>
  99. <%=CsFullTypeName(property.PropertyType)%> <%=CsFullTypeName(property.DeclaringType)%>.<%=property.Name%>
  100. {
  101. <%if property.CanRead then%>
  102. get
  103. {
  104. #if THREAD_SAFE || HOTFIX_ENABLE
  105. lock (luaEnv.luaEnvLock)
  106. {
  107. #endif
  108. RealStatePtr L = luaEnv.L;
  109. int oldTop = LuaAPI.lua_gettop(L);
  110. <%if not JustLuaType(property.PropertyType) then %>ObjectTranslator translator = luaEnv.translator;<%end%>
  111. LuaAPI.lua_getref(L, luaReference);
  112. LuaAPI.xlua_pushasciistring(L, "<%=property.Name%>");
  113. if (0 != LuaAPI.xlua_pgettable(L, -2))
  114. {
  115. luaEnv.ThrowExceptionFromError(oldTop);
  116. }
  117. <%=GetCasterStatement(property.PropertyType, "-1", "__gen_ret", true)%>;
  118. LuaAPI.lua_pop(L, 2);
  119. return __gen_ret;
  120. #if THREAD_SAFE || HOTFIX_ENABLE
  121. }
  122. #endif
  123. }
  124. <%end%>
  125. <%if property.CanWrite then%>
  126. set
  127. {
  128. #if THREAD_SAFE || HOTFIX_ENABLE
  129. lock (luaEnv.luaEnvLock)
  130. {
  131. #endif
  132. RealStatePtr L = luaEnv.L;
  133. int oldTop = LuaAPI.lua_gettop(L);
  134. <%if not JustLuaType(property.PropertyType) then %>ObjectTranslator translator = luaEnv.translator;<%end%>
  135. LuaAPI.lua_getref(L, luaReference);
  136. LuaAPI.xlua_pushasciistring(L, "<%=property.Name%>");
  137. <%=GetPushStatement(property.PropertyType, "value")%>;
  138. if (0 != LuaAPI.xlua_psettable(L, -3))
  139. {
  140. luaEnv.ThrowExceptionFromError(oldTop);
  141. }
  142. LuaAPI.lua_pop(L, 1);
  143. #if THREAD_SAFE || HOTFIX_ENABLE
  144. }
  145. #endif
  146. }
  147. <%end%>
  148. }
  149. <%end)%>
  150. <%ForEachCsList(events, function(event) %>
  151. event <%=CsFullTypeName(event.EventHandlerType)%> <%=CsFullTypeName(event.DeclaringType)%>.<%=event.Name%>
  152. {<%local parameters = event:GetAddMethod():GetParameters()%>
  153. add
  154. {
  155. #if THREAD_SAFE || HOTFIX_ENABLE
  156. lock (luaEnv.luaEnvLock)
  157. {
  158. #endif
  159. RealStatePtr L = luaEnv.L;
  160. int err_func = LuaAPI.load_error_func(L, luaEnv.errorFuncRef);
  161. <%if CallNeedTranslator(event:GetAddMethod(), "") then %>ObjectTranslator translator = luaEnv.translator;<%end%>
  162. LuaAPI.lua_getref(L, luaReference);
  163. LuaAPI.xlua_pushasciistring(L, "add_<%=event.Name%>");
  164. if (0 != LuaAPI.xlua_pgettable(L, -2))
  165. {
  166. luaEnv.ThrowExceptionFromError(err_func - 1);
  167. }
  168. if(!LuaAPI.lua_isfunction(L, -1))
  169. {
  170. LuaAPI.xlua_pushasciistring(L, "no such function add_<%=event.Name%>");
  171. luaEnv.ThrowExceptionFromError(err_func - 1);
  172. }
  173. LuaAPI.lua_pushvalue(L, -2);
  174. LuaAPI.lua_remove(L, -3);
  175. <%
  176. local param_count = parameters.Length
  177. local has_v_params = param_count > 0 and IsParams(parameters[param_count - 1])
  178. ForEachCsList(parameters, function(parameter, pi)
  179. if not (parameter.IsOut and parameter.ParameterType.IsByRef) then
  180. %><%=GetPushStatement(parameter.ParameterType, parameter.Name, has_v_params and pi == param_count - 1)%>;
  181. <%
  182. end
  183. end) %>
  184. int __gen_error = LuaAPI.lua_pcall(L, 2, 0, err_func);
  185. if (__gen_error != 0)
  186. luaEnv.ThrowExceptionFromError(err_func - 1);
  187. LuaAPI.lua_settop(L, err_func - 1);
  188. #if THREAD_SAFE || HOTFIX_ENABLE
  189. }
  190. #endif
  191. }
  192. remove
  193. {
  194. #if THREAD_SAFE || HOTFIX_ENABLE
  195. lock (luaEnv.luaEnvLock)
  196. {
  197. #endif
  198. RealStatePtr L = luaEnv.L;
  199. int err_func = LuaAPI.load_error_func(L, luaEnv.errorFuncRef);
  200. <%if CallNeedTranslator(event:GetRemoveMethod(), "") then %>ObjectTranslator translator = luaEnv.translator;<%end%>
  201. LuaAPI.lua_getref(L, luaReference);
  202. LuaAPI.xlua_pushasciistring(L, "remove_<%=event.Name%>");
  203. if (0 != LuaAPI.xlua_pgettable(L, -2))
  204. {
  205. luaEnv.ThrowExceptionFromError(err_func - 1);
  206. }
  207. if(!LuaAPI.lua_isfunction(L, -1))
  208. {
  209. LuaAPI.xlua_pushasciistring(L, "no such function remove_<%=event.Name%>");
  210. luaEnv.ThrowExceptionFromError(err_func - 1);
  211. }
  212. LuaAPI.lua_pushvalue(L, -2);
  213. LuaAPI.lua_remove(L, -3);
  214. <%
  215. local param_count = parameters.Length
  216. local has_v_params = param_count > 0 and IsParams(parameters[param_count - 1])
  217. ForEachCsList(parameters, function(parameter, pi)
  218. if not (parameter.IsOut and parameter.ParameterType.IsByRef) then
  219. %><%=GetPushStatement(parameter.ParameterType, parameter.Name, has_v_params and pi == param_count - 1)%>;
  220. <%
  221. end
  222. end) %>
  223. int __gen_error = LuaAPI.lua_pcall(L, 2, 0, err_func);
  224. if (__gen_error != 0)
  225. luaEnv.ThrowExceptionFromError(err_func - 1);
  226. LuaAPI.lua_settop(L, err_func - 1);
  227. #if THREAD_SAFE || HOTFIX_ENABLE
  228. }
  229. #endif
  230. }
  231. }
  232. <%end)%>
  233. <%ForEachCsList(indexers, function(indexer)
  234. local ptype = (indexer:GetGetMethod() or indexer:GetSetMethod()):GetParameters()[0].ParameterType
  235. local pname = (indexer:GetGetMethod() or indexer:GetSetMethod()):GetParameters()[0].Name
  236. %>
  237. <%=CsFullTypeName(indexer.PropertyType)%> <%=CsFullTypeName(indexer.DeclaringType)%>.this[<%=CsFullTypeName(ptype)%> <%=pname%>]
  238. {<%if indexer:GetGetMethod() then
  239. local method = indexer:GetGetMethod()
  240. local parameters = method:GetParameters()
  241. local in_num = CalcCsList(parameters, function(p) return not (p.IsOut and p.ParameterType.IsByRef) end)
  242. local out_num = CalcCsList(parameters, function(p) return p.IsOut or p.ParameterType.IsByRef end)
  243. local in_pos = 0
  244. local has_return = (method.ReturnType.FullName ~= "System.Void")
  245. local return_type_name = has_return and CsFullTypeName(method.ReturnType) or "void"
  246. local out_idx = has_return and 2 or 1
  247. if has_return then out_num = out_num + 1 end
  248. %>
  249. get
  250. {
  251. #if THREAD_SAFE || HOTFIX_ENABLE
  252. lock (luaEnv.luaEnvLock)
  253. {
  254. #endif
  255. RealStatePtr L = luaEnv.L;
  256. int err_func = LuaAPI.load_error_func(L, luaEnv.errorFuncRef);
  257. <%if CallNeedTranslator(method, "") then %>ObjectTranslator translator = luaEnv.translator;<%end%>
  258. LuaAPI.lua_getref(L, luaReference);
  259. LuaAPI.xlua_pushasciistring(L, "<%=method.Name%>");
  260. if (0 != LuaAPI.xlua_pgettable(L, -2))
  261. {
  262. luaEnv.ThrowExceptionFromError(err_func - 1);
  263. }
  264. if(!LuaAPI.lua_isfunction(L, -1))
  265. {
  266. LuaAPI.xlua_pushasciistring(L, "no such function <%=method.Name%>");
  267. luaEnv.ThrowExceptionFromError(err_func - 1);
  268. }
  269. LuaAPI.lua_pushvalue(L, -2);
  270. LuaAPI.lua_remove(L, -3);
  271. <%
  272. local param_count = parameters.Length
  273. local has_v_params = param_count > 0 and IsParams(parameters[param_count - 1])
  274. ForEachCsList(parameters, function(parameter, pi)
  275. if not (parameter.IsOut and parameter.ParameterType.IsByRef) then
  276. %><%=GetPushStatement(parameter.ParameterType, parameter.Name, has_v_params and pi == param_count - 1)%>;
  277. <%
  278. end
  279. end) %>
  280. int __gen_error = LuaAPI.lua_pcall(L, <%=has_v_params and ((in_num) .. " + " .. parameters[param_count - 1].Name .. ".Length" ) or (in_num + 1)%>, <%=out_num%>, err_func);
  281. if (__gen_error != 0)
  282. luaEnv.ThrowExceptionFromError(err_func - 1);
  283. <%ForEachCsList(parameters, function(parameter)
  284. if parameter.IsOut or parameter.ParameterType.IsByRef then
  285. %><%=GetCasterStatement(parameter.ParameterType, "err_func" .. (" + "..out_idx), parameter.Name)%>;
  286. <%
  287. out_idx = out_idx + 1
  288. end
  289. end) %>
  290. <%if has_return then %><%=GetCasterStatement(method.ReturnType, "err_func + 1", "__gen_ret", true)%>;<% end%>
  291. LuaAPI.lua_settop(L, err_func - 1);
  292. <%if has_return then %>return __gen_ret;<% end%>
  293. #if THREAD_SAFE || HOTFIX_ENABLE
  294. }
  295. #endif
  296. }
  297. <%end%>
  298. <%if indexer:GetSetMethod() then
  299. local method = indexer:GetSetMethod()
  300. local parameters = method:GetParameters()
  301. local in_num = CalcCsList(parameters, function(p) return not (p.IsOut and p.ParameterType.IsByRef) end)
  302. local out_num = CalcCsList(parameters, function(p) return p.IsOut or p.ParameterType.IsByRef end)
  303. local in_pos = 0
  304. local has_return = (method.ReturnType.FullName ~= "System.Void")
  305. local return_type_name = has_return and CsFullTypeName(method.ReturnType) or "void"
  306. local out_idx = has_return and 2 or 1
  307. if has_return then out_num = out_num + 1 end
  308. %>
  309. set
  310. {
  311. #if THREAD_SAFE || HOTFIX_ENABLE
  312. lock (luaEnv.luaEnvLock)
  313. {
  314. #endif
  315. RealStatePtr L = luaEnv.L;
  316. int err_func = LuaAPI.load_error_func(L, luaEnv.errorFuncRef);
  317. <%if CallNeedTranslator(method, "") then %>ObjectTranslator translator = luaEnv.translator;<%end%>
  318. LuaAPI.lua_getref(L, luaReference);
  319. LuaAPI.xlua_pushasciistring(L, "<%=method.Name%>");
  320. if (0 != LuaAPI.xlua_pgettable(L, -2))
  321. {
  322. luaEnv.ThrowExceptionFromError(err_func - 1);
  323. }
  324. if(!LuaAPI.lua_isfunction(L, -1))
  325. {
  326. LuaAPI.xlua_pushasciistring(L, "no such function <%=method.Name%>");
  327. luaEnv.ThrowExceptionFromError(err_func - 1);
  328. }
  329. LuaAPI.lua_pushvalue(L, -2);
  330. LuaAPI.lua_remove(L, -3);
  331. <%
  332. local param_count = parameters.Length
  333. local has_v_params = param_count > 0 and IsParams(parameters[param_count - 1])
  334. ForEachCsList(parameters, function(parameter, pi)
  335. if not (parameter.IsOut and parameter.ParameterType.IsByRef) then
  336. %><%=GetPushStatement(parameter.ParameterType, parameter.Name, has_v_params and pi == param_count - 1)%>;
  337. <%
  338. end
  339. end) %>
  340. int __gen_error = LuaAPI.lua_pcall(L, <%=has_v_params and ((in_num) .. " + " .. parameters[param_count - 1].Name .. ".Length" ) or (in_num + 1)%>, <%=out_num%>, err_func);
  341. if (__gen_error != 0)
  342. luaEnv.ThrowExceptionFromError(err_func - 1);
  343. <%ForEachCsList(parameters, function(parameter)
  344. if parameter.IsOut or parameter.ParameterType.IsByRef then
  345. %><%=GetCasterStatement(parameter.ParameterType, "err_func" .. (" + "..out_idx), parameter.Name)%>;
  346. <%
  347. out_idx = out_idx + 1
  348. end
  349. end) %>
  350. <%if has_return then %><%=GetCasterStatement(method.ReturnType, "err_func + 1", "__gen_ret", true)%>;<% end%>
  351. LuaAPI.lua_settop(L, err_func - 1);
  352. <%if has_return then %>return __gen_ret;<% end%>
  353. #if THREAD_SAFE || HOTFIX_ENABLE
  354. }
  355. #endif
  356. }
  357. <%end%>
  358. }
  359. <%end)%>
  360. }
  361. }