1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- namespace ResBuilder
- {
- class UIXmlPacker
- {
- public readonly string inputPath;
- public readonly string outputFile;
- public readonly bool isZipData;
- public UIXmlPacker(string input , string output , bool isZip)
- {
- this.inputPath = input;
- this.outputFile = Path.Combine(output , "ui.bin");
- this.isZipData = isZip;
- }
- string GetWriteFileName(string fp)
- {
- if (fp.StartsWith(inputPath))
- {
- fp = fp.Substring(fp.IndexOf("xmds_ui"));
- fp = fp.Replace('\\', '/');
- fp = fp.Substring(0, fp.LastIndexOf(".gui."));
- }
- return fp;
- }
- public void Pack()
- {
- var files = Directory.GetFiles(inputPath, "*.gui.bin", SearchOption.AllDirectories);
- // -- 1 Hash 文件名称
- var nameHashs = new uint[files.Length];
- var fileNameHash = new Dictionary<uint, string>();
- for (int i = 0; i < files.Length; i++)
- {
- var relativeFilePath = files[i].Substring(inputPath.Length + 1);
- var hash = Tools.HashFileNameWithoutExtension(relativeFilePath);
- if (fileNameHash.ContainsKey(hash))
- {
- throw new Exception(string.Format("文件名hash冲突:{0} -> {1}", fileNameHash[hash], relativeFilePath));
- }
- else
- {
- fileNameHash.Add(hash, relativeFilePath);
- }
- nameHashs[i] = hash;
- }
- // -- 打包文件
- using (var ms = new MemoryStream())
- {
- var bw = new BinaryWriter(ms);
- bw.Write(files.Length);
- for (int i = 0; i < files.Length; i++)
- {
- bw.Write(nameHashs[i]);
- var fdat = File.ReadAllBytes(files[i]);
- bw.Write(fdat.Length);
- bw.Write(fdat);
- }
- var data = ms.ToArray();
- if (this.isZipData)
- {
- data = LZMAHelper.Compress(data);
- }
- File.WriteAllBytes(outputFile, data);
- }
- }
- }
- }
|