GenLuaTool.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using UnityEditor;
  4. namespace ET
  5. {
  6. public static class GenLuaTool
  7. {
  8. [MenuItem("Tools/Gen Lua Scripts #S"),]
  9. public static void Geanerate()
  10. {
  11. Log.Debug("=========开始转换C#为lua,请耐心等待=========");
  12. if (!Directory.Exists("Assets/Bundles/Lua/Hotfix"))
  13. {
  14. Directory.CreateDirectory("Assets/Bundles/Lua/Hotfix");
  15. }
  16. if (!Directory.Exists("Assets/Bundles/Lua/HotfixView"))
  17. {
  18. Directory.CreateDirectory("Assets/Bundles/Lua/HotfixView");
  19. }
  20. if (!Directory.Exists("Assets/Bundles/Lua/Model"))
  21. {
  22. Directory.CreateDirectory("Assets/Bundles/Lua/Model");
  23. }
  24. if (!Directory.Exists("Assets/Bundles/Lua/ModelView"))
  25. {
  26. Directory.CreateDirectory("Assets/Bundles/Lua/ModelView");
  27. }
  28. if (!Directory.Exists("Assets/Res/Code"))
  29. {
  30. Directory.CreateDirectory("Assets/Res/Code");
  31. }
  32. FileHelper.CleanDirectory("Assets/Bundles/Lua/Hotfix");
  33. FileHelper.CleanDirectory("Assets/Bundles/Lua/HotfixView");
  34. FileHelper.CleanDirectory("Assets/Bundles/Lua/Model");
  35. FileHelper.CleanDirectory("Assets/Bundles/Lua/ModelView");
  36. FileHelper.CleanDirectory("Assets/Res/Code");
  37. GenLuaHelper.CopyDll("Unity.HotfixView");
  38. GenLuaHelper.CopyDll("Unity.Hotfix");
  39. GenLuaHelper.CopyDll("Unity.Model");
  40. GenLuaHelper.CopyDll("Unity.ModelView");
  41. GenLuaHelper.CompileLua(
  42. "Unity.Model",
  43. "./Assets/Model",
  44. "Model",
  45. null,
  46. true);
  47. GenLuaHelper.CompileLua(
  48. "Unity.Hotfix",
  49. "./Assets/Hotfix",
  50. "Hotfix",
  51. new List<string>() { "Unity.Model" },
  52. true);
  53. GenLuaHelper.CompileLua(
  54. "Unity.ModelView",
  55. "./Assets/ModelView",
  56. "ModelView",
  57. new List<string>() { "Unity.Model", },
  58. true);
  59. GenLuaHelper.CompileLua(
  60. "Unity.HotfixView",
  61. "./Assets/HotfixView",
  62. "HotfixView",
  63. new List<string>() { "Unity.Model", "Unity.Hotfix", "Unity.ModelView", },
  64. false);
  65. AssetDatabase.SaveAssets();
  66. AssetDatabase.Refresh();
  67. Log.Debug("===============转换完成===============");
  68. }
  69. }
  70. public static class GenLuaHelper
  71. {
  72. public const string LuaDir = "./Lua/";
  73. public const string LuaTxtExtensionName = ".lua.txt";
  74. public const string LuaExtensionName = ".lua";
  75. private const string ScriptAssembliesDir = "Library/ScriptAssemblies";
  76. private const string CodeDir = "Assets/Res/Code/";
  77. public static bool CopyDll(string dllName)
  78. {
  79. var result = FileHelper.CopyFile(Path.Combine(ScriptAssembliesDir, $"{dllName}.dll"), Path.Combine(CodeDir, $"{dllName}.dll.bytes"),
  80. true);
  81. FileHelper.CopyFile(Path.Combine(ScriptAssembliesDir, $"{dllName}.pdb"), Path.Combine(CodeDir, $"{dllName}.pdb.bytes"), true);
  82. if (result)
  83. {
  84. Log.Info($"复制{dllName}.dll, {dllName}.pdb到Res/Code完成");
  85. AssetDatabase.Refresh();
  86. }
  87. return result;
  88. }
  89. public static void CompileLua(string dllName, string dllDir, string outDirName, List<string> referencedLuaAssemblies, bool isModule)
  90. {
  91. LuaCompiler.Compile(dllName, dllDir, outDirName, referencedLuaAssemblies, isModule);
  92. FileHelper.ReplaceExtensionName(LuaCompiler.outDir + outDirName, LuaExtensionName, LuaTxtExtensionName);
  93. ABNameEditor.SetFolderLuaABName(LuaCompiler.outDir + outDirName);
  94. AssetDatabase.Refresh();
  95. }
  96. //[MenuItem("Tools/Lua/Append the all lua file of the selected folder with \".txt\"")]
  97. public static void AppendSelectedFolder()
  98. {
  99. var assetGUIDs = Selection.assetGUIDs;
  100. foreach (var guid in assetGUIDs)
  101. {
  102. var assetPath = AssetDatabase.GUIDToAssetPath(guid);
  103. if (Directory.Exists(assetPath))
  104. {
  105. var outDirName = assetPath.Substring(assetPath.LastIndexOf("/") + 1);
  106. FileHelper.ReplaceExtensionName(LuaCompiler.outDir + outDirName, LuaExtensionName, LuaTxtExtensionName);
  107. }
  108. }
  109. AssetDatabase.Refresh();
  110. }
  111. //[MenuItem("Tools/Lua/Replace the all \".lua.txt\" file of the selected folder with \".lua\"")]
  112. public static void ReplaceSelectedFolder()
  113. {
  114. var assetGUIDs = Selection.assetGUIDs;
  115. foreach (var guid in assetGUIDs)
  116. {
  117. var assetPath = AssetDatabase.GUIDToAssetPath(guid);
  118. if (Directory.Exists(assetPath))
  119. {
  120. var outDirName = assetPath.Substring(assetPath.LastIndexOf("/") + 1);
  121. FileHelper.ReplaceExtensionName(LuaCompiler.outDir + outDirName, LuaTxtExtensionName, LuaExtensionName);
  122. }
  123. }
  124. AssetDatabase.Refresh();
  125. }
  126. }
  127. }