using System; using System.Collections.Generic; using Unity.SharpZipLib.Zip; using Newtonsoft.Json; using System.IO; using Unity.SharpZipLib.Core; using System.Linq; using System.Diagnostics; using System.Text; using Unity.SharpZipLib.GZip; using System.Threading.Tasks; namespace BITKit.IO { record BITHeader { public string Name { get; set; } public DateTime CreateTime; public DateTime LastModifiedTime; public List Files = new List(); } class MemoryBytes : IStaticDataSource { byte[] buffer; public MemoryBytes(byte[] buffer) { this.buffer = buffer; } public Stream GetSource() { Stream s = new MemoryStream(buffer); return s; } } public class BITAssets : List { public static void Build(string path, params IAsset[] assets) { List assetList = new(); BITHeader header = new BITHeader(); header.Name = path; header.CreateTime = DateTime.Now; header.LastModifiedTime = DateTime.Now; foreach (var asset in assets) { header.Files.Add(asset.Name); } assetList.Add(new BITAsset() { Name = "manifest.json", Buffer = StringHelper.GetBytes(JsonConvert.SerializeObject(header, Formatting.Indented)), }); assetList.AddRange(assets); using (ZipFile zipFile = ZipFile.Create(path)) { zipFile.BeginUpdate(); foreach (var x in assetList) { using (var ms = new MemoryStream(x.Buffer)) { zipFile.Add(new MemoryBytes(x.Buffer), x.Name); } } zipFile.CommitUpdate(); } } public static T Read(string path, string bufferPath, IStorageFile provider = null) { var json = ReadText(path, bufferPath, provider); return Read(json); } public static async Task ReadAsync(string path, string bufferPath, IStorageFile provider = null) { var json = await ReadTextAsync(path, bufferPath, provider); return Read(json); } public static T Read(string json) { return JsonConvert.DeserializeObject(json); } public static async Task ReadAseetAsync(string path, string bufferPath, IStorageFile provider = null) { provider = provider ?? new FileProvider(path); return ReadAsset(await provider.OpenReadAsync(), bufferPath); } public static IAsset ReadAseet(string path, string bufferPath, IStorageFile provider = null) { provider = provider ?? new FileProvider(path); return ReadAsset(provider.OpenRead(), bufferPath); } public static IAsset ReadAsset(Stream stream, string bufferPath) { BITAsset asset = new(); using (var zipFile = new ZipFile(stream)) { foreach (ZipEntry zipEntry in zipFile) { if (zipEntry.IsFile && zipEntry.Name == bufferPath) { asset.Name = zipEntry.Name; using (var fs = zipFile.GetInputStream(zipEntry)) { using (MemoryStream ms = new()) { fs.CopyTo(ms); asset.Buffer = ms.GetBuffer(); } } return asset; } } } throw new System.IO.FileNotFoundException(); } public static string ReadText(string path, string bufferPath, IStorageFile provider = null) { var asset = ReadAseet(path, bufferPath, provider); return StringHelper.GetString(asset.Buffer); } public static async Task ReadTextAsync(string path, string bufferPath, IStorageFile provider = null) { var asset = await ReadAseetAsync(path, bufferPath, provider); return StringHelper.GetString(asset.Buffer); } public void Build(string path) { Build(path, ToArray()); } } }