UICfgToEnum.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text;
  5. using UnityEngine;
  6. using UnityEditor;
  7. using LuaInterface;
  8. public class UICfgToEnum : AssetPostprocessor
  9. {
  10. private const string UICFGLUA_PATH = "Assets/Lua/Config/UICfg.lua";
  11. private const string ENUM_TARGET_PATH = "Assets/Lua/Enum/UIPageName.lua";
  12. public void OnPreprocessAsset()
  13. {
  14. if (assetPath == UICFGLUA_PATH)
  15. {
  16. WriteFile();
  17. }
  18. }
  19. private bool FormatEnum(ref StringBuilder stringBuilder)
  20. {
  21. bool success = false;
  22. try
  23. {
  24. if (Application.isPlaying)
  25. {
  26. var fieldInfo = typeof(LuaState).GetField("mainState", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic);
  27. LuaState luaState = (LuaState)fieldInfo.GetValue(null);
  28. if (luaState != null)
  29. {
  30. fieldInfo = typeof(LuaState).GetField("beStart", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic);
  31. bool beStart = (bool)fieldInfo.GetValue(luaState);
  32. if (beStart)
  33. {
  34. FormatEnumValue(luaState, ref stringBuilder);
  35. success = true;
  36. }
  37. }
  38. }
  39. else
  40. {
  41. using(LuaState luaState = new LuaState())
  42. {
  43. luaState.Start();
  44. FormatEnumValue(luaState, ref stringBuilder);
  45. success = true;
  46. }
  47. }
  48. }
  49. catch(System.Exception e)
  50. {
  51. Debug.LogException(e);
  52. }
  53. return success;
  54. }
  55. private void FormatEnumValue(LuaState luaState, ref StringBuilder stringBuilder)
  56. {
  57. using(var luaTable = luaState.DoFile<LuaTable>(assetPath))
  58. {
  59. using(var luaDictTable = luaTable.ToDictTable<int, LuaTable>())
  60. {
  61. int min = 0;
  62. int max = 0;
  63. Dictionary<int, string> datas = new Dictionary<int, string>();
  64. foreach (var item in luaDictTable)
  65. {
  66. int id = item.Key;
  67. string name = (item.Value["name"]).ToString();
  68. datas[id] = name;
  69. if (min > id)
  70. {
  71. min = id;
  72. }
  73. if (max < id)
  74. {
  75. max = id;
  76. }
  77. }
  78. int lastVaildId = min;
  79. for (int i = min; i <= max; i ++)
  80. {
  81. if (datas.ContainsKey(i))
  82. {
  83. if ((lastVaildId + 1) != i)
  84. {
  85. stringBuilder.AppendLine();
  86. }
  87. stringBuilder.Append("\t");
  88. stringBuilder.Append(datas[i]);
  89. stringBuilder.Append(" = ");
  90. stringBuilder.Append(i);
  91. stringBuilder.AppendLine(",");
  92. lastVaildId = i;
  93. }
  94. }
  95. }
  96. }
  97. }
  98. private void WriteFile()
  99. {
  100. StringBuilder stringBuilder = new StringBuilder();
  101. stringBuilder.AppendLine("local UIPageName = {");
  102. bool success = FormatEnum(ref stringBuilder);
  103. if (success)
  104. {
  105. stringBuilder.AppendLine("}");
  106. stringBuilder.Append("return setmetatable(UIPageName, { __index = Enum.UIPageName })");
  107. try
  108. {
  109. bool needWrite = true;
  110. string newStr = stringBuilder.ToString();
  111. UTF8Encoding uTF8Encoding = new UTF8Encoding(false);
  112. if (File.Exists(ENUM_TARGET_PATH))
  113. {
  114. string oldStr = File.ReadAllText(ENUM_TARGET_PATH, uTF8Encoding);
  115. if (newStr == oldStr)
  116. {
  117. needWrite = false;
  118. }
  119. }
  120. else
  121. {
  122. var fs = File.Create(ENUM_TARGET_PATH);
  123. fs.Dispose();
  124. }
  125. if (needWrite)
  126. {
  127. File.WriteAllText(ENUM_TARGET_PATH, newStr, uTF8Encoding);
  128. }
  129. }
  130. catch(System.Exception e)
  131. {
  132. Debug.LogException(e);
  133. }
  134. }
  135. }
  136. }