LuaProcessor.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text;
  5. namespace ResBuilder
  6. {
  7. class LuaProcessor
  8. {
  9. public readonly string inputPath;
  10. public readonly string outputPath;
  11. public readonly string tmpPath;
  12. public readonly string jitToolsPath;
  13. public LuaProcessor(string input, string output , string tmpPath , string jitExePath)
  14. {
  15. this.inputPath = input;
  16. this.outputPath = output;
  17. this.tmpPath = tmpPath;
  18. this.jitToolsPath = jitExePath;
  19. }
  20. internal void Execute()
  21. {
  22. if (Program.IsJitLuaBytes)
  23. {
  24. Execute(inputPath, tmpPath);
  25. ExecuteLuaJitFile(jitToolsPath, tmpPath, outputPath);
  26. }
  27. else
  28. {
  29. Execute(inputPath, outputPath);
  30. }
  31. }
  32. static void Execute(string dir, string outputPath, SearchOption sopt = SearchOption.AllDirectories)
  33. {
  34. var fileNames = Directory.GetFiles(dir, "*.lua", sopt);
  35. // -- 1 Hash 文件名称
  36. var nameHashs = new uint[fileNames.Length];
  37. var fileNameHash = new Dictionary<uint, string>();
  38. for (int i = 0; i < fileNames.Length; i++)
  39. {
  40. var relativeFilePath = fileNames[i].Substring(dir.Length + 1);
  41. var hash = Tools.HashFileNameWithoutExtension(relativeFilePath);
  42. if (fileNameHash.ContainsKey(hash))
  43. {
  44. throw new Exception(string.Format("文件名hash冲突:{0} -> {1}", fileNameHash[hash], relativeFilePath));
  45. }
  46. else
  47. {
  48. fileNameHash.Add(hash, relativeFilePath);
  49. }
  50. nameHashs[i] = hash;
  51. }
  52. // -- 2 处理lua文件
  53. var fileBytes = new byte[fileNames.Length][];
  54. var logger = new StringBuilder();
  55. for (int i = 0; i < fileNames.Length; i++)
  56. {
  57. fileBytes[i] = GetLuaFileBytes(fileNames[i], logger);
  58. }
  59. if (logger.Length > 0)
  60. {
  61. File.WriteAllText("luaChanges.log", logger.ToString());
  62. }
  63. if (fileNames.Length > 0 && !Directory.Exists(outputPath))
  64. {
  65. Directory.CreateDirectory(outputPath);
  66. }
  67. // -- 3 写入处理后的lua文件
  68. for (int i = 0; i < fileNames.Length; i++)
  69. {
  70. var outFilePath = Path.Combine(outputPath, Convert.ToString(nameHashs[i], 16) + ".lua");
  71. File.WriteAllBytes(outFilePath, fileBytes[i]);
  72. }
  73. }
  74. static byte[] GetLuaFileBytes(string filePath, StringBuilder logger)
  75. {
  76. var str = LuaFileTools.RemoveLuaFileComment(filePath, logger);
  77. var bytes = Tools.UTF8.GetBytes(str);
  78. if (Program.IsZipLuaBytes)
  79. {
  80. bytes = LZMAHelper.Compress(bytes);
  81. }
  82. return bytes;
  83. }
  84. static void ExecuteLuaJitFile(string luajitPath, string inputPath, string outPath)
  85. {
  86. inputPath = inputPath.Replace("/","\\");
  87. var files = Directory.GetFiles(inputPath, "*.lua", SearchOption.AllDirectories);
  88. foreach (var f in files)
  89. {
  90. Console.WriteLine("luajit->" + f);
  91. var outfile = Path.Combine(outPath, f.Substring(inputPath.Length + 1));
  92. var outDir = Path.GetDirectoryName(outfile);
  93. if (!Directory.Exists(outDir))
  94. {
  95. Directory.CreateDirectory(outDir);
  96. }
  97. var args = string.Format("-b {0} {1}", f, outfile);
  98. Program.StartProcess(luajitPath, args);
  99. }
  100. }
  101. }
  102. }