EditorBinPacker.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace ResBuilder
  8. {
  9. class EditorBinPacker
  10. {
  11. public readonly string inputPath;
  12. public readonly string outputPath;
  13. public EditorBinPacker(string input , string output)
  14. {
  15. this.inputPath = input;
  16. this.outputPath = output;
  17. }
  18. public void Pack()
  19. {
  20. // -- e1.bin
  21. var fs = new List<KeyValuePair<string, byte[]>>();
  22. fs.Add(new KeyValuePair<string, byte[]>("config", PackConfig()));
  23. fs.Add(new KeyValuePair<string, byte[]>("units", Pack("/units")));
  24. fs.Add(new KeyValuePair<string, byte[]>("skills", Pack("/skills")));
  25. fs.Add(new KeyValuePair<string, byte[]>("spells", Pack("/spells")));
  26. fs.Add(new KeyValuePair<string, byte[]>("buffs", Pack("/buffs")));
  27. fs.Add(new KeyValuePair<string, byte[]>("items", Pack("/items")));
  28. fs.Add(new KeyValuePair<string, byte[]>("unit_events", Pack("/unit_events")));
  29. fs.Add(new KeyValuePair<string, byte[]>("unit_triggers", Pack("/unit_triggers")));
  30. fs.Add(new KeyValuePair<string, byte[]>("snaps", PackSceneSnap()));
  31. fs.Add(new KeyValuePair<string, byte[]>("newsnaps", PackNewSceneSnap()));
  32. using (var ms = new MemoryStream())
  33. {
  34. var bw = new BinaryWriter(ms);
  35. bw.Write((byte)fs.Count);
  36. foreach (var f in fs)
  37. {
  38. bw.Write(f.Key);
  39. bw.Write(f.Value.Length);
  40. }
  41. foreach (var f in fs)
  42. {
  43. bw.Write(f.Value);
  44. }
  45. WritFile(ms.ToArray(), "e1.bin" , false);
  46. }
  47. // -- e2.bin
  48. WritFile(PackSceneData(), "ex.bin" , false);
  49. }
  50. public string[] GetListFileNames(string dirPath)
  51. {
  52. var ret = new List<string>();
  53. var listFilePath = Path.Combine(dirPath, ".list");
  54. var listtxt = Tools.DecodeUTF8(File.ReadAllBytes(listFilePath));
  55. foreach (string line in listtxt.Split('\n'))
  56. {
  57. string[] lv = line.Split(';');
  58. if (lv.Length > 1)
  59. {
  60. ret.Add(lv[lv.Length - 1].Trim());
  61. }
  62. }
  63. return ret.ToArray();
  64. }
  65. public void WritFile(byte[] data, string outFileName, bool isZip)
  66. {
  67. var path = Path.Combine(outputPath, outFileName);
  68. if (isZip)
  69. {
  70. LZMAHelper.Compress(data, path);
  71. }
  72. else
  73. {
  74. File.WriteAllBytes(path, data);
  75. }
  76. }
  77. public void WritTo(byte[] data, Stream stream)
  78. {
  79. stream.Write(data , 0 , data.Length);
  80. }
  81. public byte[] Pack(string subDir)
  82. {
  83. var curDir = inputPath + subDir;
  84. var fileNames = GetListFileNames(curDir);
  85. using (var ms = new MemoryStream())
  86. {
  87. var bw = new BinaryWriter(ms);
  88. bw.Write(fileNames.Length);
  89. foreach (var fn in fileNames)
  90. {
  91. var bytes = File.ReadAllBytes(curDir + "/" + fn + ".xml.bin");
  92. bw.Write(fn);
  93. bw.Write(bytes.Length);
  94. bw.Write(bytes);
  95. }
  96. return ms.ToArray();
  97. }
  98. }
  99. public byte[] PackSceneData()
  100. {
  101. var curDir = inputPath +"/scenes";
  102. var fileNames = GetListFileNames(curDir);
  103. var fileBytes = new byte[fileNames.Length][];
  104. for (int i = 0; i < fileNames.Length; i ++)
  105. {
  106. var fdata = File.ReadAllBytes(curDir + "/" + fileNames[i] + ".xml.bin");
  107. fileBytes[i] = fdata;// LZMAHelper.Compress(fdata);
  108. }
  109. using (var ms = new MemoryStream())
  110. {
  111. var bw = new BinaryWriter(ms);
  112. bw.Write(fileNames.Length);
  113. for (int i = 0; i < fileNames.Length; i++)
  114. {
  115. bw.Write(fileNames[i]);
  116. bw.Write(fileBytes[i].Length);
  117. }
  118. for (int i = 0; i < fileNames.Length; i++)
  119. {
  120. bw.Write(fileBytes[i]);
  121. }
  122. return ms.ToArray();
  123. }
  124. }
  125. public byte[] PackNewSceneSnap()
  126. {
  127. string[] listFileNames = this.GetListFileNames(this.inputPath + "/scenes");
  128. byte[] result;
  129. using (MemoryStream memoryStream = new MemoryStream())
  130. {
  131. BinaryWriter binaryWriter = new BinaryWriter(memoryStream);
  132. binaryWriter.Write(listFileNames.Length);
  133. foreach (string text in listFileNames)
  134. {
  135. byte[] array2 = File.ReadAllBytes(this.inputPath + "/scene_unitsnaps/" + text + ".snd");
  136. binaryWriter.Write(int.Parse(text));
  137. binaryWriter.Write(array2.Length);
  138. binaryWriter.Write(array2);
  139. }
  140. result = memoryStream.ToArray();
  141. }
  142. return result;
  143. }
  144. public byte[] PackSceneSnap()
  145. {
  146. var sceneFileNames = GetListFileNames(inputPath + "/scenes");
  147. using (var ms = new MemoryStream())
  148. {
  149. var bw = new BinaryWriter(ms);
  150. bw.Write(sceneFileNames.Length);
  151. foreach (var fn in sceneFileNames)
  152. {
  153. var bytes = File.ReadAllBytes(inputPath + "/scene_snaps/" + fn + ".snd");
  154. bw.Write(int.Parse(fn));
  155. bw.Write(bytes.Length);
  156. bw.Write(bytes);
  157. }
  158. return ms.ToArray();
  159. }
  160. }
  161. public byte[] PackConfig()
  162. {
  163. using (var ms = new MemoryStream())
  164. {
  165. var bw = new BinaryWriter(ms);
  166. // -- ver.md5
  167. bw.Write(ReadVerMD5());
  168. // -- config.xml
  169. var bytes = File.ReadAllBytes(Path.Combine(inputPath, "config.xml"));
  170. bw.Write(bytes.Length);
  171. bw.Write(bytes);
  172. // -- config_ext.xml
  173. bytes = File.ReadAllBytes(Path.Combine(inputPath, "config_ext.xml"));
  174. bw.Write(bytes.Length);
  175. bw.Write(bytes);
  176. // -- terrain_definition.xml
  177. bytes = File.ReadAllBytes(Path.Combine(inputPath, "terrain_definition.xml"));
  178. bw.Write(bytes.Length);
  179. bw.Write(bytes);
  180. // -- unit_action_definition
  181. bytes = File.ReadAllBytes(Path.Combine(inputPath, "unit_action_definition.xml"));
  182. bw.Write(bytes.Length);
  183. bw.Write(bytes);
  184. return ms.ToArray();
  185. }
  186. }
  187. public string ReadVerMD5()
  188. {
  189. string md5 = Tools.DecodeUTF8(File.ReadAllBytes(inputPath + "/ver.md5"));
  190. string[] lines = md5.Split('\n');
  191. return lines[0].Trim();
  192. }
  193. }
  194. }