123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- 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<KeyValuePair<string, byte[]>>();
- fs.Add(new KeyValuePair<string, byte[]>("config", PackConfig()));
- fs.Add(new KeyValuePair<string, byte[]>("units", Pack("/units")));
- fs.Add(new KeyValuePair<string, byte[]>("skills", Pack("/skills")));
- fs.Add(new KeyValuePair<string, byte[]>("spells", Pack("/spells")));
- fs.Add(new KeyValuePair<string, byte[]>("buffs", Pack("/buffs")));
- fs.Add(new KeyValuePair<string, byte[]>("items", Pack("/items")));
- fs.Add(new KeyValuePair<string, byte[]>("unit_events", Pack("/unit_events")));
- fs.Add(new KeyValuePair<string, byte[]>("unit_triggers", Pack("/unit_triggers")));
- fs.Add(new KeyValuePair<string, byte[]>("snaps", PackSceneSnap()));
- fs.Add(new KeyValuePair<string, byte[]>("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<string>();
- 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();
- }
- }
- }
|