2023-06-05 19:57:17 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
2023-06-29 14:57:11 +08:00
|
|
|
|
using Newtonsoft.Json;
|
2023-06-05 19:57:17 +08:00
|
|
|
|
using System.Text;
|
2023-06-29 14:57:11 +08:00
|
|
|
|
using System.IO.Compression;
|
2023-06-05 19:57:17 +08:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace BITKit.IO
|
|
|
|
|
{
|
2023-06-29 14:57:11 +08:00
|
|
|
|
public record BITHeader
|
2023-06-05 19:57:17 +08:00
|
|
|
|
{
|
2023-06-29 14:57:11 +08:00
|
|
|
|
// ReSharper disable once UnusedAutoPropertyAccessor.Global
|
2023-06-05 19:57:17 +08:00
|
|
|
|
public string Name { get; set; }
|
|
|
|
|
public DateTime CreateTime;
|
|
|
|
|
public DateTime LastModifiedTime;
|
|
|
|
|
|
2023-06-29 14:57:11 +08:00
|
|
|
|
// ReSharper disable once CollectionNeverQueried.Global
|
|
|
|
|
public readonly List<string> Files = new List<string>();
|
|
|
|
|
}
|
2023-06-05 19:57:17 +08:00
|
|
|
|
public class BITAssets : List<IAsset>
|
|
|
|
|
{
|
|
|
|
|
public static void Build(string path, params IAsset[] assets)
|
|
|
|
|
{
|
|
|
|
|
List<IAsset> assetList = new();
|
2023-06-29 14:57:11 +08:00
|
|
|
|
var header = new BITHeader
|
|
|
|
|
{
|
|
|
|
|
Name = path,
|
|
|
|
|
CreateTime = DateTime.Now,
|
|
|
|
|
LastModifiedTime = DateTime.Now
|
|
|
|
|
};
|
2023-06-05 19:57:17 +08:00
|
|
|
|
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);
|
2023-06-29 14:57:11 +08:00
|
|
|
|
var file = new FileInfo(path);
|
|
|
|
|
if (file.Exists)
|
|
|
|
|
{
|
|
|
|
|
file.Delete();
|
|
|
|
|
}
|
2023-06-05 19:57:17 +08:00
|
|
|
|
|
2023-06-29 14:57:11 +08:00
|
|
|
|
using var zipFile = ZipFile.Open(path, ZipArchiveMode.Create);
|
2023-06-05 19:57:17 +08:00
|
|
|
|
|
2023-06-29 14:57:11 +08:00
|
|
|
|
foreach (var x in assetList)
|
2023-06-05 19:57:17 +08:00
|
|
|
|
{
|
2023-06-29 14:57:11 +08:00
|
|
|
|
var entry = zipFile.CreateEntry(x.Name);
|
|
|
|
|
using var ms = new MemoryStream(x.Buffer);
|
|
|
|
|
using var writer = entry.Open();
|
|
|
|
|
ms.CopyTo(writer);
|
2023-06-05 19:57:17 +08:00
|
|
|
|
}
|
2023-06-29 14:57:11 +08:00
|
|
|
|
|
|
|
|
|
zipFile.Dispose();
|
2023-07-17 10:23:47 +08:00
|
|
|
|
|
|
|
|
|
BIT4Log.Log<BITAssets>($"已创建Assets:\n{path}");
|
2023-06-05 19:57:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-06-29 14:57:11 +08:00
|
|
|
|
public static T ReadAs<T>(string path)
|
|
|
|
|
{
|
|
|
|
|
return Read<T>(path, typeof(T).Name);
|
|
|
|
|
}
|
2023-06-05 19:57:17 +08:00
|
|
|
|
public static T Read<T>(string path, string bufferPath, IStorageFile provider = null)
|
|
|
|
|
{
|
|
|
|
|
var json = ReadText(path, bufferPath, provider);
|
|
|
|
|
return Read<T>(json);
|
|
|
|
|
}
|
2023-06-29 14:57:11 +08:00
|
|
|
|
|
2023-06-05 19:57:17 +08:00
|
|
|
|
public static async Task<T> ReadAsync<T>(string path, string bufferPath, IStorageFile provider = null)
|
|
|
|
|
{
|
|
|
|
|
var json = await ReadTextAsync(path, bufferPath, provider);
|
|
|
|
|
return Read<T>(json);
|
|
|
|
|
}
|
2023-06-29 14:57:11 +08:00
|
|
|
|
|
|
|
|
|
// ReSharper disable once MemberCanBePrivate.Global
|
2023-06-05 19:57:17 +08:00
|
|
|
|
public static T Read<T>(string json)
|
|
|
|
|
{
|
|
|
|
|
return JsonConvert.DeserializeObject<T>(json);
|
|
|
|
|
}
|
2023-06-29 14:57:11 +08:00
|
|
|
|
|
|
|
|
|
// ReSharper disable once MemberCanBePrivate.Global
|
|
|
|
|
public static async Task<IAsset> ReadAssetAsync(string path, string bufferPath, IStorageFile provider = null)
|
2023-06-05 19:57:17 +08:00
|
|
|
|
{
|
|
|
|
|
provider = provider ?? new FileProvider(path);
|
|
|
|
|
return ReadAsset(await provider.OpenReadAsync(), bufferPath);
|
|
|
|
|
}
|
2023-06-29 14:57:11 +08:00
|
|
|
|
|
|
|
|
|
// ReSharper disable once MemberCanBePrivate.Global
|
|
|
|
|
public static IAsset ReadAsset(string path, string bufferPath, IStorageFile provider = null)
|
2023-06-05 19:57:17 +08:00
|
|
|
|
{
|
|
|
|
|
provider = provider ?? new FileProvider(path);
|
|
|
|
|
return ReadAsset(provider.OpenRead(), bufferPath);
|
|
|
|
|
}
|
2023-06-29 14:57:11 +08:00
|
|
|
|
|
|
|
|
|
// ReSharper disable once MemberCanBePrivate.Global
|
2023-06-05 19:57:17 +08:00
|
|
|
|
public static IAsset ReadAsset(Stream stream, string bufferPath)
|
|
|
|
|
{
|
2023-06-29 14:57:11 +08:00
|
|
|
|
BITAsset asset = new()
|
2023-06-05 19:57:17 +08:00
|
|
|
|
{
|
2023-06-29 14:57:11 +08:00
|
|
|
|
Name = bufferPath
|
|
|
|
|
};
|
|
|
|
|
using var zip = new ZipArchive(stream, ZipArchiveMode.Read);
|
|
|
|
|
var entry = zip.GetEntry(bufferPath);
|
|
|
|
|
if (entry is null)
|
|
|
|
|
{
|
|
|
|
|
StringBuilder stringBuilder = new($"未找到{bufferPath}");
|
|
|
|
|
stringBuilder.AppendLine("已找到的Entry:");
|
|
|
|
|
foreach (var _entry in zip.Entries)
|
2023-06-05 19:57:17 +08:00
|
|
|
|
{
|
2023-06-29 14:57:11 +08:00
|
|
|
|
stringBuilder.AppendLine($"{_entry!.Name}\t{_entry!.FullName}");
|
2023-06-05 19:57:17 +08:00
|
|
|
|
}
|
2023-06-29 14:57:11 +08:00
|
|
|
|
|
|
|
|
|
throw new NullReferenceException(stringBuilder.ToString());
|
2023-06-05 19:57:17 +08:00
|
|
|
|
}
|
2023-06-29 14:57:11 +08:00
|
|
|
|
|
|
|
|
|
using var _stream = entry!.Open();
|
|
|
|
|
using var ms = new MemoryStream();
|
|
|
|
|
_stream.CopyTo(ms);
|
|
|
|
|
|
|
|
|
|
asset.Buffer = ms.ToArray();
|
|
|
|
|
|
|
|
|
|
return asset;
|
2023-06-05 19:57:17 +08:00
|
|
|
|
}
|
2023-06-29 14:57:11 +08:00
|
|
|
|
|
|
|
|
|
// ReSharper disable once MemberCanBePrivate.Global
|
2023-06-05 19:57:17 +08:00
|
|
|
|
public static string ReadText(string path, string bufferPath, IStorageFile provider = null)
|
|
|
|
|
{
|
2023-06-29 14:57:11 +08:00
|
|
|
|
var asset = ReadAsset(path, bufferPath, provider);
|
2023-06-05 19:57:17 +08:00
|
|
|
|
return StringHelper.GetString(asset.Buffer);
|
|
|
|
|
}
|
2023-06-29 14:57:11 +08:00
|
|
|
|
|
2023-06-05 19:57:17 +08:00
|
|
|
|
public static async Task<string> ReadTextAsync(string path, string bufferPath, IStorageFile provider = null)
|
|
|
|
|
{
|
2023-06-29 14:57:11 +08:00
|
|
|
|
var asset = await ReadAssetAsync(path, bufferPath, provider);
|
2023-06-05 19:57:17 +08:00
|
|
|
|
return StringHelper.GetString(asset.Buffer);
|
|
|
|
|
}
|
2023-06-29 14:57:11 +08:00
|
|
|
|
|
2023-06-05 19:57:17 +08:00
|
|
|
|
public void Build(string path)
|
|
|
|
|
{
|
|
|
|
|
Build(path, ToArray());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|