UIXmlPacker.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. namespace ResBuilder
  5. {
  6. class UIXmlPacker
  7. {
  8. public readonly string inputPath;
  9. public readonly string outputFile;
  10. public readonly bool isZipData;
  11. public UIXmlPacker(string input , string output , bool isZip)
  12. {
  13. this.inputPath = input;
  14. this.outputFile = Path.Combine(output , "ui.bin");
  15. this.isZipData = isZip;
  16. }
  17. string GetWriteFileName(string fp)
  18. {
  19. if (fp.StartsWith(inputPath))
  20. {
  21. fp = fp.Substring(fp.IndexOf("xmds_ui"));
  22. fp = fp.Replace('\\', '/');
  23. fp = fp.Substring(0, fp.LastIndexOf(".gui."));
  24. }
  25. return fp;
  26. }
  27. public void Pack()
  28. {
  29. var files = Directory.GetFiles(inputPath, "*.gui.bin", SearchOption.AllDirectories);
  30. // -- 1 Hash 文件名称
  31. var nameHashs = new uint[files.Length];
  32. var fileNameHash = new Dictionary<uint, string>();
  33. for (int i = 0; i < files.Length; i++)
  34. {
  35. var relativeFilePath = files[i].Substring(inputPath.Length + 1);
  36. var hash = Tools.HashFileNameWithoutExtension(relativeFilePath);
  37. if (fileNameHash.ContainsKey(hash))
  38. {
  39. throw new Exception(string.Format("文件名hash冲突:{0} -> {1}", fileNameHash[hash], relativeFilePath));
  40. }
  41. else
  42. {
  43. fileNameHash.Add(hash, relativeFilePath);
  44. }
  45. nameHashs[i] = hash;
  46. }
  47. // -- 打包文件
  48. using (var ms = new MemoryStream())
  49. {
  50. var bw = new BinaryWriter(ms);
  51. bw.Write(files.Length);
  52. for (int i = 0; i < files.Length; i++)
  53. {
  54. bw.Write(nameHashs[i]);
  55. var fdat = File.ReadAllBytes(files[i]);
  56. bw.Write(fdat.Length);
  57. bw.Write(fdat);
  58. }
  59. var data = ms.ToArray();
  60. if (this.isZipData)
  61. {
  62. data = LZMAHelper.Compress(data);
  63. }
  64. File.WriteAllBytes(outputFile, data);
  65. }
  66. }
  67. }
  68. }