Files
ft/CodexOutput/upserver/B3BatchMigration/M2/C2d修改前快照/000-EventDataRepository.cs.txt
Liubing\LB 6813da149a feat(upserver): 保存M2客户端KV迁移至本人统计写检查点
保存独立KV用户服务、固定会话存档与原业务接线,以及C3d资料缓存和C3e本人统计写。
保留原PF/AGG路线、业务公式和时序;榜单读取、好友资料、公告、匹配及首场景闸仍待完成。

验证:ServiceHost两条件各33组646断言;Unity CLI Loop编译0错误269警告。
包含相关方案、评审裁决、测试及状态文档。未操作服务器、正式账号或打包。
2026-09-30 19:19:05 +08:00

69 lines
2.4 KiB
Plaintext

using System;
using Newtonsoft.Json;
using UnityEngine;
namespace Activity.Data
{
/// <summary>
/// 使用本次登录绑定的存档提供方,保留活动类型名作为存档键。
/// </summary>
public class EventDataRepository : IEventDataRepository
{
readonly game.PlayerDataStorage.IPlayerSaveStore store;
public EventDataRepository(game.PlayerDataStorage.IPlayerSaveStore store)
{ this.store = store ?? throw new ArgumentNullException(nameof(store)); }
public void Save<T>(T data) where T : IEventData
{
if (data == null)
{
Debug.LogWarning($"[EventDataRepository] Cannot save null data for {typeof(T).Name}");
return;
}
try
{
var json = JsonConvert.SerializeObject(data);
#if UNITY_EDITOR
Debug.Log($"[EventDataRepository] Saving {typeof(T).Name}: {json}");
#endif
store.Write(typeof(T).Name, json).RequireAccepted();
}
catch (Exception e)
{
Debug.LogError($"[EventDataRepository] Failed to save {typeof(T).Name}: {e.Message}\n{e.StackTrace}");
throw;
}
}
public T Load<T>() where T : IEventData, new()
{
try
{
var read = store.Read(typeof(T).Name);
var dataStr = read.ValueOrThrow();
if (read.Found == false) return new T();
if (string.IsNullOrWhiteSpace(dataStr) || dataStr.Trim() == "null")
throw new JsonSerializationException("Stored activity data cannot be empty or null.");
// #if UNITY_EDITOR
// Debug.Log($"[EventDataRepository] Load {typeof(T).Name}: {dataStr}");
// #endif
var data = JsonConvert.DeserializeObject<T>(dataStr);
if (data == null) throw new JsonSerializationException("Stored activity data cannot be null.");
return data;
}
catch (Exception e)
{
Debug.LogError($"[EventDataRepository] Failed to load {typeof(T).Name}: {e.Message}\n{e.StackTrace}");
throw;
}
}
public bool Exists<T>() where T : IEventData
{
var read = store.Read(typeof(T).Name);
read.ValueOrThrow();
return read.Found.Value;
}
}
}