LuaBinPacker.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text;
  5. namespace ResBuilder
  6. {
  7. class LuaBinPacker
  8. {
  9. public readonly string inputPath;
  10. public readonly string outputPath;
  11. public LuaBinPacker(string input, string output)
  12. {
  13. this.inputPath = input;
  14. this.outputPath = output;
  15. }
  16. internal void Pack()
  17. {
  18. PackDir(inputPath);
  19. }
  20. void PackDir(string dir, SearchOption sopt = SearchOption.AllDirectories)
  21. {
  22. var fileNames = Directory.GetFiles(dir, "*.lua", sopt);
  23. var fileBytes = new byte[fileNames.Length][];
  24. // -- 1 write lb.bytes
  25. using (var ms = new MemoryStream())
  26. {
  27. var bw = new BinaryWriter(ms);
  28. for (int i = 0; i < fileNames.Length; i++)
  29. {
  30. fileBytes[i] = File.ReadAllBytes(fileNames[i]);
  31. bw.Write(fileBytes[i]);
  32. }
  33. var outFilePath = Path.Combine(outputPath, "lb.bin");
  34. File.WriteAllBytes(outFilePath, ms.ToArray());
  35. }
  36. // -- 2 write lh.bytes
  37. using (var ms = new MemoryStream())
  38. {
  39. var bw = new BinaryWriter(ms);
  40. // -- write flag
  41. byte flag = Program.IsZipLuaBytes ? (byte)1 : (byte)0;
  42. bw.Write(flag);
  43. // -- write files count
  44. bw.Write(fileNames.Length);
  45. // --
  46. for (int i = 0; i < fileNames.Length; i++)
  47. {
  48. bw.Write(Path.GetFileNameWithoutExtension(fileNames[i]));
  49. bw.Write(fileBytes[i].Length);
  50. }
  51. var outFilePath = Path.Combine(outputPath, "lh.bin");
  52. File.WriteAllBytes(outFilePath, ms.ToArray());
  53. }
  54. }
  55. }
  56. }