PackUnpack.tpl.txt 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 static partial class CopyByValue
  17. {
  18. <%ForEachCsList(type_infos, function(type_info)
  19. local full_type_name = CsFullTypeName(type_info.Type)
  20. %>
  21. <%if type_info.IsRoot then
  22. ForEachCsList(type_info.FieldInfos, function(fieldInfo) fieldInfo.Name = UnK(fieldInfo.Name) end)
  23. %>
  24. public static void UnPack(ObjectTranslator translator, RealStatePtr L, int idx, out <%=full_type_name%> val)
  25. {
  26. val = new <%=full_type_name%>();
  27. int top = LuaAPI.lua_gettop(L);
  28. <%ForEachCsList(type_info.FieldInfos, function(fieldInfo)%>
  29. if (Utils.LoadField(L, idx, "<%=fieldInfo.Name%>"))
  30. {
  31. <%if fieldInfo.IsField then%>
  32. translator.Get(L, top + 1, out val.<%=fieldInfo.Name%>);
  33. <%else%>
  34. var <%=fieldInfo.Name%> = val.<%=fieldInfo.Name%>;
  35. translator.Get(L, top + 1, out <%=fieldInfo.Name%>);
  36. val.<%=fieldInfo.Name%> = <%=fieldInfo.Name%>;
  37. <%end%>
  38. }
  39. LuaAPI.lua_pop(L, 1);
  40. <%end)%>
  41. }
  42. <%end%>
  43. public static bool Pack(IntPtr buff, int offset, <%=full_type_name%> field)
  44. {
  45. <%
  46. local offset_inner = 0
  47. if not type_info.FieldGroup then
  48. ForEachCsList(type_info.FieldInfos, function(fieldInfo)
  49. %>
  50. if(!Pack(buff, offset<%=(offset_inner == 0 and "" or (" + " .. offset_inner))%>, field.<%=fieldInfo.Name%>))
  51. {
  52. return false;
  53. }
  54. <%
  55. offset_inner = offset_inner + fieldInfo.Size
  56. end)
  57. else
  58. ForEachCsList(type_info.FieldGroup, function(group)
  59. %>
  60. if(!LuaAPI.xlua_pack_float<%=(group.Count == 1 and "" or group.Count)%>(buff, offset<%=(offset_inner == 0 and "" or (" + " .. offset_inner))%><%
  61. ForEachCsList(group, function(fieldInfo, i)
  62. %>, field.<%=fieldInfo.Name%><%
  63. end)
  64. %>))
  65. {
  66. return false;
  67. }
  68. <%
  69. offset_inner = offset_inner + group.Count * 4
  70. end)
  71. end%>
  72. return true;
  73. }
  74. public static bool UnPack(IntPtr buff, int offset, out <%=full_type_name%> field)
  75. {
  76. field = default(<%=full_type_name%>);
  77. <%
  78. local offset_inner = 0
  79. if not type_info.FieldGroup then
  80. ForEachCsList(type_info.FieldInfos, function(fieldInfo)
  81. if fieldInfo.IsField then
  82. %>
  83. if(!UnPack(buff, offset<%=(offset_inner == 0 and "" or (" + " .. offset_inner))%>, out field.<%=fieldInfo.Name%>))
  84. {
  85. return false;
  86. }
  87. <%else%>
  88. var <%=fieldInfo.Name%> = field.<%=fieldInfo.Name%>;
  89. if(!UnPack(buff, offset<%=(offset_inner == 0 and "" or (" + " .. offset_inner))%>, out <%=fieldInfo.Name%>))
  90. {
  91. return false;
  92. }
  93. field.<%=fieldInfo.Name%> = <%=fieldInfo.Name%>;
  94. <%
  95. end
  96. offset_inner = offset_inner + fieldInfo.Size
  97. end)
  98. else
  99. ForEachCsList(type_info.FieldGroup, function(group)
  100. %>
  101. <%ForEachCsList(group, function(fieldInfo)%>float <%=fieldInfo.Name%> = default(float);
  102. <%end)%>
  103. if(!LuaAPI.xlua_unpack_float<%=(group.Count == 1 and "" or group.Count)%>(buff, offset<%=(offset_inner == 0 and "" or (" + " .. offset_inner))%><%
  104. ForEachCsList(group, function(fieldInfo)
  105. %>, out <%=fieldInfo.Name%><%
  106. end)
  107. %>))
  108. {
  109. return false;
  110. }
  111. <%ForEachCsList(group, function(fieldInfo)%>field.<%=fieldInfo.Name%> = <%=fieldInfo.Name%>;
  112. <%end)%>
  113. <%
  114. offset_inner = offset_inner + group.Count * 4
  115. end)
  116. end%>
  117. return true;
  118. }
  119. <%end)%>
  120. }
  121. }