60 lines
1.8 KiB
C#
60 lines
1.8 KiB
C#
using System;
|
|
using Newtonsoft.Json;
|
|
using UnityEngine;
|
|
|
|
namespace Activity.Data
|
|
{
|
|
/// <summary>
|
|
/// 基于 PlayFab 的数据存储实现
|
|
/// </summary>
|
|
public class EventDataRepository : IEventDataRepository
|
|
{
|
|
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
|
|
PlayFabMgr.Instance?.UpdateUserDataValue(typeof(T).Name, json);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Debug.LogError($"[EventDataRepository] Failed to save {typeof(T).Name}: {e.Message}\n{e.StackTrace}");
|
|
}
|
|
}
|
|
|
|
public T Load<T>() where T : IEventData, new()
|
|
{
|
|
try
|
|
{
|
|
var dataStr = PlayFabMgr.Instance?.GetLocalData(typeof(T).Name);
|
|
if (string.IsNullOrEmpty(dataStr)) return new T();
|
|
// #if UNITY_EDITOR
|
|
// Debug.Log($"[EventDataRepository] Load {typeof(T).Name}: {dataStr}");
|
|
// #endif
|
|
var data = JsonConvert.DeserializeObject<T>(dataStr);
|
|
return data ?? new T();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Debug.LogError($"[EventDataRepository] Failed to load {typeof(T).Name}: {e.Message}\n{e.StackTrace}");
|
|
return new T();
|
|
}
|
|
}
|
|
|
|
public bool Exists<T>() where T : IEventData
|
|
{
|
|
var dataStr = PlayFabMgr.Instance?.GetLocalData(typeof(T).Name);
|
|
return !string.IsNullOrEmpty(dataStr);
|
|
}
|
|
}
|
|
}
|