LuaEnumWrapGCM.tpl.txt 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 enum_or_op = debug.getmetatable(CS.System.Reflection.BindingFlags.Public).__bor
  15. %>
  16. namespace XLua
  17. {
  18. public partial class ObjectTranslator
  19. {
  20. <%ForEachCsList(types, function(type)
  21. local fields = type2fields and type2fields[type] or type:GetFields(enum_or_op(CS.System.Reflection.BindingFlags.Public, CS.System.Reflection.BindingFlags.Static))
  22. local fields_to_gen = {}
  23. ForEachCsList(fields, function(field)
  24. if field.Name ~= "value__" and not IsObsolute(field) then
  25. table.insert(fields_to_gen, field)
  26. end
  27. end)
  28. local v_type_name = CSVariableName(type)
  29. %>
  30. public void __Register<%=v_type_name%>(RealStatePtr L)
  31. {
  32. Utils.BeginObjectRegister(typeof(<%=CsFullTypeName(type)%>), L, this, 0, 0, 0, 0);
  33. Utils.EndObjectRegister(typeof(<%=CsFullTypeName(type)%>), L, this, null, null, null, null, null);
  34. Utils.BeginClassRegister(typeof(<%=CsFullTypeName(type)%>), L, null, <%=fields.Length + 1%>, 0, 0);
  35. <%if #fields_to_gen <= 20 then%>
  36. <% ForEachCsList(fields, function(field)
  37. if field.Name == "value__" or IsObsolute(field) then return end
  38. %>
  39. Utils.RegisterObject(L, this, Utils.CLS_IDX, "<%=field.Name%>", <%=CsFullTypeName(type)%>.<%=UnK(field.Name)%>);
  40. <%end)%>
  41. <%else%>
  42. Utils.RegisterEnumType(L, typeof(<%=CsFullTypeName(type)%>));
  43. <%end%>
  44. Utils.RegisterFunc(L, Utils.CLS_IDX, "__CastFrom", __CastFrom<%=v_type_name%>);
  45. Utils.EndClassRegister(typeof(<%=CsFullTypeName(type)%>), L, this);
  46. }
  47. int __CastFrom<%=v_type_name%>(RealStatePtr L, int __gen_top)
  48. {
  49. LuaTypes lua_type = LuaAPI.lua_type(L, 1);
  50. if (lua_type == LuaTypes.LUA_TNUMBER)
  51. {
  52. Push<%=v_type_name%>(L, (<%=CsFullTypeName(type)%>)LuaAPI.xlua_tointeger(L, 1));
  53. }
  54. <%if #fields_to_gen > 0 then%>
  55. else if(lua_type == LuaTypes.LUA_TSTRING)
  56. {
  57. <%if #fields_to_gen <= 20 then%>
  58. <%
  59. local is_first = true
  60. ForEachCsList(fields, function(field, i)
  61. if field.Name == "value__" or IsObsolute(field) then return end
  62. %><%=(is_first and "" or "else ")%>if (LuaAPI.xlua_is_eq_str(L, 1, "<%=field.Name%>"))
  63. {
  64. Push<%=v_type_name%>(L, <%=CsFullTypeName(type)%>.<%=UnK(field.Name)%>);
  65. }
  66. <%
  67. is_first = false
  68. end)
  69. %>else
  70. {
  71. return LuaAPI.luaL_error(L, "invalid string for <%=CsFullTypeName(type)%>!");
  72. }
  73. <%else%>
  74. try
  75. {
  76. TranslateToEnumToTop(L, typeof(<%=CsFullTypeName(type)%>), 1);
  77. }
  78. catch (System.Exception e)
  79. {
  80. return LuaAPI.luaL_error(L, "cast to " + typeof(<%=CsFullTypeName(type)%>) + " exception:" + e);
  81. }
  82. <%end%>
  83. }
  84. <%end%>
  85. else
  86. {
  87. return LuaAPI.luaL_error(L, "invalid lua type for <%=CsFullTypeName(type)%>! Expect number or string, got + " + lua_type);
  88. }
  89. return 1;
  90. }
  91. <%end)%>
  92. }
  93. }