LuaDelegateWrap.tpl.txt 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. <%
  13. require "TemplateCommon"
  14. local parameters = delegate:GetParameters()
  15. local in_num = CalcCsList(parameters, function(p) return not (p.IsOut and p.ParameterType.IsByRef) end)
  16. local out_num = CalcCsList(parameters, function(p) return p.IsOut or p.ParameterType.IsByRef end)
  17. local in_pos = 0
  18. local has_return = (delegate.ReturnType.Name ~= "Void")
  19. %>
  20. namespace XLua.CSObjectWrap
  21. {
  22. using Utils = XLua.Utils;
  23. public class <%=CSVariableName(type)%>Wrap
  24. {
  25. public static void __Register(RealStatePtr L)
  26. {
  27. ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L);
  28. Utils.BeginObjectRegister(typeof(<%=CsFullTypeName(type)%>), L, translator, 0, 0, 0, 0);
  29. Utils.EndObjectRegister(typeof(<%=CsFullTypeName(type)%>), L, translator, null, null, null, null, null);
  30. Utils.BeginClassRegister(typeof(<%=CsFullTypeName(type)%>), L, null, 1, 0, 0);
  31. Utils.EndClassRegister(typeof(<%=CsFullTypeName(type)%>), L, translator);
  32. }
  33. static Dictionary<string, LuaCSFunction> __MetaFucntions_Dic = new Dictionary<string, LuaCSFunction>(){
  34. {"__call", __CallMeta},
  35. {"__add", __AddMeta},
  36. {"__sub", __SubMeta},
  37. };
  38. [MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
  39. static int __CallMeta(RealStatePtr L)
  40. {
  41. ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L);
  42. try {
  43. if(LuaAPI.lua_gettop(L) == <%=in_num+1%> && <%=GetCheckStatement(type, 1)%><%
  44. ForEachCsList(parameters, function(parameter)
  45. if not (parameter.IsOut and parameter.ParameterType.IsByRef) then in_pos = in_pos + 1;
  46. %>&& <%=GetCheckStatement(parameter.ParameterType, in_pos+1)%><%
  47. end
  48. end)%>)
  49. {
  50. <%
  51. in_pos = 0;
  52. ForEachCsList(parameters, function(parameter)
  53. %><%
  54. if not (parameter.IsOut and parameter.ParameterType.IsByRef) then
  55. in_pos = in_pos + 1
  56. %><%=GetCasterStatement(parameter.ParameterType, in_pos+1, parameter.Name, true)%><%
  57. else%><%=CsFullTypeName(parameter.ParameterType)%> <%=parameter.Name%><%end%>;
  58. <%end)%>
  59. <%=GetSelfStatement(type)%>;
  60. <%
  61. if has_return then
  62. %> var __cl_gen_ret = <%
  63. end
  64. %> __cl_gen_to_be_invoked( <%ForEachCsList(parameters, function(parameter, pi) if pi ~= 0 then %>, <% end; if parameter.IsOut then %>out <% elseif parameter.ParameterType.IsByRef then %>ref <% end %><%=parameter.Name%><% end) %> );
  65. <%
  66. if has_return then
  67. %> <%=GetPushStatement(delegate.ReturnType, "__cl_gen_ret")%>;
  68. <%
  69. end
  70. local in_pos = 0
  71. ForEachCsList(parameters, function(parameter)
  72. if not (parameter.IsOut and parameter.ParameterType.IsByRef) then
  73. in_pos = in_pos + 1
  74. end
  75. if parameter.IsOut or parameter.ParameterType.IsByRef then
  76. %><%=GetPushStatement(parameter.ParameterType:GetElementType(), parameter.Name)%>;
  77. <%if not parameter.IsOut and parameter.ParameterType.IsByRef and NeedUpdate(parameter.ParameterType) then
  78. %><%=GetUpdateStatement(parameter.ParameterType:GetElementType(), in_pos+param_offset, parameter.Name)%>;
  79. <%end%>
  80. <%
  81. end
  82. end)
  83. %>
  84. return <%=out_num+(has_return and 1 or 0)%>;
  85. }
  86. } catch(System.Exception __gen_e) {
  87. return LuaAPI.luaL_error(L, "c# exception:" + __gen_e);
  88. }
  89. return LuaAPI.luaL_error(L, "invalid arguments to Delegate <%=CsFullTypeName(type)%>!");
  90. }
  91. [MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
  92. static int __AddMeta(RealStatePtr L)
  93. {
  94. ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L);
  95. try {
  96. <%=GetCasterStatement(type, 1, "leftside", true)%>;
  97. <%=GetCasterStatement(type, 2, "rightside", true)%>;
  98. <%=GetPushStatement(type, "leftside + rightside")%>;
  99. return 1;
  100. } catch(System.Exception __gen_e) {
  101. return LuaAPI.luaL_error(L, "c# exception:" + __gen_e);
  102. }
  103. }
  104. [MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
  105. static int __SubMeta(RealStatePtr L)
  106. {
  107. ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L);
  108. try {
  109. <%=GetCasterStatement(type, 1, "leftside", true)%>;
  110. <%=GetCasterStatement(type, 2, "rightside", true)%>;
  111. <%=GetPushStatement(type, "leftside - rightside")%>;
  112. return 1;
  113. } catch(System.Exception __gen_e) {
  114. return LuaAPI.luaL_error(L, "c# exception:" + __gen_e);
  115. }
  116. }
  117. }
  118. }