diff --git a/DotNet~/ShrinkSDK.DataSaver.csproj b/DotNet~/ShrinkSDK.DataSaver.csproj index 95585ca..a86f83a 100644 --- a/DotNet~/ShrinkSDK.DataSaver.csproj +++ b/DotNet~/ShrinkSDK.DataSaver.csproj @@ -5,7 +5,7 @@ ShrinkDataSaver.Runtime ShrinkDataSaver ShrinkSDK.DataSaver - 2.3.0 + 2.4.0 ShrinkSDK engine-neutral versioned save and settings runtime. false disable diff --git a/README.md b/README.md index ab84cad..61bb50e 100644 --- a/README.md +++ b/README.md @@ -542,3 +542,13 @@ await ShrinkSave.LoadSlotAsync(slotIndex, key); ## 📄 License [MIT](LICENSE) + +## 可复用能力 + + +Capability: 模块化存档、槽位、设置、迁移、加密和损坏备份恢复 +Aliases: 存储 存档 保存 读档 设置 持久化 save +Limits: 模块负责采集一致状态;后台不得读取 Unity 对象;不会自动保存任意游戏字段 +Extension: 存档模块注册、序列化器、迁移链与平台路径服务 +Evidence: [ShrinkSave](Runtime/ShrinkSave.cs); [ShrinkDataSaverRuntime](DotNet~/Runtime/ShrinkDataSaverRuntime.cs) + diff --git a/Runtime/LocalStorageProvider.cs b/Runtime/LocalStorageProvider.cs index 9541cfb..53296b9 100644 --- a/Runtime/LocalStorageProvider.cs +++ b/Runtime/LocalStorageProvider.cs @@ -108,8 +108,8 @@ namespace ShrinkDataSaver } } - public UniTask ExistsAsync(string path, CancellationToken ct = default) => - Task.Run(() => File.Exists(Resolve(path)), ct).AsUniTask(); + public async UniTask ExistsAsync(string path, CancellationToken ct = default) => + await Task.Run(() => File.Exists(Resolve(path)), ct); public async UniTask DeleteAsync(string path, CancellationToken ct = default) { diff --git a/Runtime/SaveTypes.cs b/Runtime/SaveTypes.cs index 849f382..c01457f 100644 --- a/Runtime/SaveTypes.cs +++ b/Runtime/SaveTypes.cs @@ -59,8 +59,21 @@ namespace ShrinkDataSaver } + public sealed class SavePhaseTimings + { + public double CaptureMilliseconds { get; internal set; } + public double EncodeMilliseconds { get; internal set; } + public double EncryptMilliseconds { get; internal set; } + public double WriteMilliseconds { get; internal set; } + public long EncodedBytes { get; internal set; } + } + public class SaveOptions { + /// Only the detached JToken snapshot is encoded off-thread; module capture stays on the caller thread. + public bool EncodeInBackground = false; + public bool CollectTimings = false; + public SavePhaseTimings LastTimings { get; internal set; } public string SlotName = "New Save"; public bool CaptureScreenshot = false; #if UNITY_5_3_OR_NEWER diff --git a/Runtime/ShrinkSave.cs b/Runtime/ShrinkSave.cs index 99a8291..68b79a5 100644 --- a/Runtime/ShrinkSave.cs +++ b/Runtime/ShrinkSave.cs @@ -1,6 +1,8 @@ using System; using System.Collections.Generic; using System.IO; +using System.Diagnostics; +using System.Threading.Tasks; using System.Threading; using Cysharp.Threading.Tasks; using Newtonsoft.Json.Linq; @@ -183,6 +185,14 @@ namespace ShrinkDataSaver { options ??= new SaveOptions(); ValidateSlotIndex(slotIndex); + ct.ThrowIfCancellationRequested(); + var timings = options.CollectTimings ? new SavePhaseTimings() : null; + options.LastTimings = timings; + var captureStarted = Stopwatch.GetTimestamp(); + var encrypt = options.Encrypt; + var encryptionKey = options.EncryptionKey; + var encodeInBackground = options.EncodeInBackground; + var storage = _storage; var timestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds(); OnSaveStarted?.Invoke(new SaveStartedEventArgs { SlotIndex = slotIndex, Timestamp = timestamp }); @@ -198,7 +208,7 @@ namespace ShrinkDataSaver SaveVersion = _currentSaveVersion, LastModified = timestamp, PlaytimeSeconds = GetCurrentPlaytime(), - IsEncrypted = options.Encrypt + IsEncrypted = encrypt } }; @@ -233,24 +243,35 @@ namespace ShrinkDataSaver } } - if (options.Encrypt) + if (timings != null) timings.CaptureMilliseconds = PhaseElapsed(captureStarted); + ct.ThrowIfCancellationRequested(); + // Modules have been materialized as private JSON tokens; the worker never touches providers or engine objects. + byte[] Encode() { - if (string.IsNullOrEmpty(options.EncryptionKey)) + ct.ThrowIfCancellationRequested(); + var stage = Stopwatch.GetTimestamp(); + if (encrypt) { - throw new ArgumentException("EncryptionKey missing."); + if (string.IsNullOrEmpty(encryptionKey)) throw new ArgumentException("EncryptionKey missing."); + var modulesBytes = DataSerializer.Serialize(modulesDict); + if (timings != null) timings.EncodeMilliseconds += PhaseElapsed(stage); + stage = Stopwatch.GetTimestamp(); + var encryptedBytes = SaveEncryptor.Encrypt(modulesBytes, encryptionKey); + if (timings != null) timings.EncryptMilliseconds = PhaseElapsed(stage); + packet.EncryptedModules = Convert.ToBase64String(encryptedBytes); } - - var modulesBytes = DataSerializer.Serialize(modulesDict); - var encryptedBytes = SaveEncryptor.Encrypt(modulesBytes, options.EncryptionKey); - packet.EncryptedModules = Convert.ToBase64String(encryptedBytes); + else packet.Modules = modulesDict; + ct.ThrowIfCancellationRequested(); + stage = Stopwatch.GetTimestamp(); + var bytes = DataSerializer.Serialize(packet); + if (timings != null) { timings.EncodeMilliseconds += PhaseElapsed(stage); timings.EncodedBytes = bytes.Length; } + return bytes; } - else - { - packet.Modules = modulesDict; - } - - var finalBytes = DataSerializer.Serialize(packet); - await _storage.WriteAsync(SlotPath(slotIndex), finalBytes, ct); + var finalBytes = encodeInBackground ? await Task.Run(Encode, ct) : Encode(); + ct.ThrowIfCancellationRequested(); + var writeStarted = Stopwatch.GetTimestamp(); + await storage.WriteAsync(SlotPath(slotIndex), finalBytes, ct); + if (timings != null) timings.WriteMilliseconds = PhaseElapsed(writeStarted); OnSaveCompleted?.Invoke(new SaveCompletedEventArgs { @@ -271,6 +292,8 @@ namespace ShrinkDataSaver } } + private static double PhaseElapsed(long started) => (Stopwatch.GetTimestamp() - started) * 1000d / Stopwatch.Frequency; + public static async UniTask LoadSlotAsync(int slotIndex, string decryptionKey = null, CancellationToken ct = default) { var timestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds(); diff --git a/package.json b/package.json index 2deb19d..619fd11 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "com.cneicy.shrink-datasaver", - "version": "2.3.0", + "version": "2.4.0", "displayName": "ShrinkDataSaver", "description": "模块化 Unity 存档与设置管理系统,支持多槽位、链式版本迁移、AES-256 加密、关键模块保护、跨模块查询与事件驱动架构。", "unity": "2022.3",