using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ResBuilder { class EditorBinPacker { public readonly string inputPath; public readonly string outputPath; public EditorBinPacker(string input , string output) { this.inputPath = input; this.outputPath = output; } public void Pack() { // -- e1.bin var fs = new List>(); fs.Add(new KeyValuePair("config", PackConfig())); fs.Add(new KeyValuePair("units", Pack("/units"))); fs.Add(new KeyValuePair("skills", Pack("/skills"))); fs.Add(new KeyValuePair("spells", Pack("/spells"))); fs.Add(new KeyValuePair("buffs", Pack("/buffs"))); fs.Add(new KeyValuePair("items", Pack("/items"))); fs.Add(new KeyValuePair("unit_events", Pack("/unit_events"))); fs.Add(new KeyValuePair("unit_triggers", Pack("/unit_triggers"))); fs.Add(new KeyValuePair("snaps", PackSceneSnap())); fs.Add(new KeyValuePair("newsnaps", PackNewSceneSnap())); using (var ms = new MemoryStream()) { var bw = new BinaryWriter(ms); bw.Write((byte)fs.Count); foreach (var f in fs) { bw.Write(f.Key); bw.Write(f.Value.Length); } foreach (var f in fs) { bw.Write(f.Value); } WritFile(ms.ToArray(), "e1.bin" , false); } // -- e2.bin WritFile(PackSceneData(), "ex.bin" , false); } public string[] GetListFileNames(string dirPath) { var ret = new List(); var listFilePath = Path.Combine(dirPath, ".list"); var listtxt = Tools.DecodeUTF8(File.ReadAllBytes(listFilePath)); foreach (string line in listtxt.Split('\n')) { string[] lv = line.Split(';'); if (lv.Length > 1) { ret.Add(lv[lv.Length - 1].Trim()); } } return ret.ToArray(); } public void WritFile(byte[] data, string outFileName, bool isZip) { var path = Path.Combine(outputPath, outFileName); if (isZip) { LZMAHelper.Compress(data, path); } else { File.WriteAllBytes(path, data); } } public void WritTo(byte[] data, Stream stream) { stream.Write(data , 0 , data.Length); } public byte[] Pack(string subDir) { var curDir = inputPath + subDir; var fileNames = GetListFileNames(curDir); using (var ms = new MemoryStream()) { var bw = new BinaryWriter(ms); bw.Write(fileNames.Length); foreach (var fn in fileNames) { var bytes = File.ReadAllBytes(curDir + "/" + fn + ".xml.bin"); bw.Write(fn); bw.Write(bytes.Length); bw.Write(bytes); } return ms.ToArray(); } } public byte[] PackSceneData() { var curDir = inputPath +"/scenes"; var fileNames = GetListFileNames(curDir); var fileBytes = new byte[fileNames.Length][]; for (int i = 0; i < fileNames.Length; i ++) { var fdata = File.ReadAllBytes(curDir + "/" + fileNames[i] + ".xml.bin"); fileBytes[i] = fdata;// LZMAHelper.Compress(fdata); } using (var ms = new MemoryStream()) { var bw = new BinaryWriter(ms); bw.Write(fileNames.Length); for (int i = 0; i < fileNames.Length; i++) { bw.Write(fileNames[i]); bw.Write(fileBytes[i].Length); } for (int i = 0; i < fileNames.Length; i++) { bw.Write(fileBytes[i]); } return ms.ToArray(); } } public byte[] PackNewSceneSnap() { string[] listFileNames = this.GetListFileNames(this.inputPath + "/scenes"); byte[] result; using (MemoryStream memoryStream = new MemoryStream()) { BinaryWriter binaryWriter = new BinaryWriter(memoryStream); binaryWriter.Write(listFileNames.Length); foreach (string text in listFileNames) { byte[] array2 = File.ReadAllBytes(this.inputPath + "/scene_unitsnaps/" + text + ".snd"); binaryWriter.Write(int.Parse(text)); binaryWriter.Write(array2.Length); binaryWriter.Write(array2); } result = memoryStream.ToArray(); } return result; } public byte[] PackSceneSnap() { var sceneFileNames = GetListFileNames(inputPath + "/scenes"); using (var ms = new MemoryStream()) { var bw = new BinaryWriter(ms); bw.Write(sceneFileNames.Length); foreach (var fn in sceneFileNames) { var bytes = File.ReadAllBytes(inputPath + "/scene_snaps/" + fn + ".snd"); bw.Write(int.Parse(fn)); bw.Write(bytes.Length); bw.Write(bytes); } return ms.ToArray(); } } public byte[] PackConfig() { using (var ms = new MemoryStream()) { var bw = new BinaryWriter(ms); // -- ver.md5 bw.Write(ReadVerMD5()); // -- config.xml var bytes = File.ReadAllBytes(Path.Combine(inputPath, "config.xml")); bw.Write(bytes.Length); bw.Write(bytes); // -- config_ext.xml bytes = File.ReadAllBytes(Path.Combine(inputPath, "config_ext.xml")); bw.Write(bytes.Length); bw.Write(bytes); // -- terrain_definition.xml bytes = File.ReadAllBytes(Path.Combine(inputPath, "terrain_definition.xml")); bw.Write(bytes.Length); bw.Write(bytes); // -- unit_action_definition bytes = File.ReadAllBytes(Path.Combine(inputPath, "unit_action_definition.xml")); bw.Write(bytes.Length); bw.Write(bytes); return ms.ToArray(); } } public string ReadVerMD5() { string md5 = Tools.DecodeUTF8(File.ReadAllBytes(inputPath + "/ver.md5")); string[] lines = md5.Split('\n'); return lines[0].Trim(); } } }