LuaDelegateBridge.tpl.txt 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 System;
  11. <%
  12. require "TemplateCommon"
  13. %>
  14. namespace XLua
  15. {
  16. public partial class DelegateBridge : DelegateBridgeBase
  17. {
  18. <%
  19. ForEachCsList(delegates_groups, function(delegates_group, group_idx)
  20. local delegate = delegates_group.Key
  21. local parameters = delegate:GetParameters()
  22. local in_num = CalcCsList(parameters, function(p) return not (p.IsOut and p.ParameterType.IsByRef) end)
  23. local out_num = CalcCsList(parameters, function(p) return p.IsOut or p.ParameterType.IsByRef end)
  24. local in_pos = 0
  25. local has_return = (delegate.ReturnType.FullName ~= "System.Void")
  26. local return_type_name = has_return and CsFullTypeName(delegate.ReturnType) or "void"
  27. local out_idx = has_return and 2 or 1
  28. if has_return then out_num = out_num + 1 end
  29. %>
  30. public <%=return_type_name%> __Gen_Delegate_Imp<%=group_idx%>(<%ForEachCsList(parameters, function(parameter, pi)
  31. if pi ~= 0 then
  32. %>, <%
  33. end
  34. if parameter.IsOut and parameter.ParameterType.IsByRef then
  35. %>out <%
  36. elseif parameter.ParameterType.IsByRef then
  37. %>ref <%
  38. end
  39. %><%=CsFullTypeName(parameter.ParameterType)%> p<%=pi%><%
  40. end) %>)
  41. {
  42. #if THREAD_SAFE || HOTFIX_ENABLE
  43. lock (luaEnv.luaEnvLock)
  44. {
  45. #endif
  46. RealStatePtr L = luaEnv.rawL;
  47. int errFunc = LuaAPI.pcall_prepare(L, errorFuncRef, luaReference);
  48. <%if CallNeedTranslator(delegate, "") then %>ObjectTranslator translator = luaEnv.translator;<%end%>
  49. <%
  50. local param_count = parameters.Length
  51. local has_v_params = param_count > 0 and parameters[param_count - 1].IsParamArray
  52. ForEachCsList(parameters, function(parameter, pi)
  53. if not (parameter.IsOut and parameter.ParameterType.IsByRef) then
  54. %><%=GetPushStatement(parameter.ParameterType, 'p' .. pi, has_v_params and pi == param_count - 1)%>;
  55. <%
  56. end
  57. end) %>
  58. 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);
  59. <%ForEachCsList(parameters, function(parameter, pi)
  60. if parameter.IsOut or parameter.ParameterType.IsByRef then
  61. %><%=GetCasterStatement(parameter.ParameterType, "errFunc" .. (" + "..out_idx), 'p' .. pi)%>;
  62. <%
  63. out_idx = out_idx + 1
  64. end
  65. end) %>
  66. <%if has_return then %><%=GetCasterStatement(delegate.ReturnType, "errFunc + 1", "__gen_ret", true)%>;<% end%>
  67. LuaAPI.lua_settop(L, errFunc - 1);
  68. <%if has_return then %>return __gen_ret;<% end%>
  69. #if THREAD_SAFE || HOTFIX_ENABLE
  70. }
  71. #endif
  72. }
  73. <%end)%>
  74. static DelegateBridge()
  75. {
  76. Gen_Flag = true;
  77. }
  78. public override Delegate GetDelegateByType(Type type)
  79. {
  80. <%
  81. ForEachCsList(delegates_groups, function(delegates_group, group_idx)
  82. ForEachCsList(delegates_group.Value, function(delegate)
  83. if delegate.DeclaringType then
  84. local delegate_type_name = CsFullTypeName(delegate.DeclaringType)
  85. %>
  86. if (type == typeof(<%=delegate_type_name%>))
  87. {
  88. return new <%=delegate_type_name%>(__Gen_Delegate_Imp<%=group_idx%>);
  89. }
  90. <%
  91. end
  92. end)
  93. end)
  94. %>
  95. return null;
  96. }
  97. }
  98. }