2025-10-03 00:31:34 +08:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
2025-10-10 14:08:23 +08:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
using Utils;
|
|
|
|
|
|
|
|
|
|
|
|
// 引入 UnityEngine 命名空间以使用 Debug.Log
|
2025-10-03 00:31:34 +08:00
|
|
|
|
|
|
|
|
|
|
namespace Managers
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
2025-10-10 14:08:23 +08:00
|
|
|
|
/// 提供一个通用的键值对存档管理器
|
|
|
|
|
|
/// 允许存储和检索各种类型的数据,并与 SaveManager 集成以进行持久化。
|
2025-10-03 00:31:34 +08:00
|
|
|
|
/// </summary>
|
2025-10-10 14:08:23 +08:00
|
|
|
|
public class KeyValueArchiveManager : Singleton<KeyValueArchiveManager>, ISavableSingleton, ILaunchManager
|
2025-10-03 00:31:34 +08:00
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
2025-10-10 14:08:23 +08:00
|
|
|
|
/// 内部用于存储键值对的字典,标记为 [Savable] 以便 SaveManager 进行序列化和反序列化。
|
2025-10-03 00:31:34 +08:00
|
|
|
|
/// </summary>
|
2025-10-10 14:08:23 +08:00
|
|
|
|
[Savable] private readonly Dictionary<string, object> _data = new();
|
2025-10-03 00:31:34 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-10-10 14:08:23 +08:00
|
|
|
|
/// 私有构造函数,用于单例模式。
|
|
|
|
|
|
/// 在首次创建实例时,将其注册到 SaveManager。
|
2025-10-03 00:31:34 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
public KeyValueArchiveManager()
|
|
|
|
|
|
{
|
|
|
|
|
|
SaveManager.Instance.RegisterSavable(this);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-10 14:08:23 +08:00
|
|
|
|
public bool Completed { get; set; }
|
|
|
|
|
|
public string StepDescription { get; } = "加载存档数据中";
|
|
|
|
|
|
|
|
|
|
|
|
public Task Init()
|
|
|
|
|
|
{
|
|
|
|
|
|
Completed = true;
|
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Clear()
|
|
|
|
|
|
{
|
|
|
|
|
|
Completed = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-03 00:31:34 +08:00
|
|
|
|
/// <summary>
|
2025-10-10 14:08:23 +08:00
|
|
|
|
/// 将指定键的值设置为指定值。
|
|
|
|
|
|
/// 如果键已存在,则更新其值;如果不存在,则添加键值对。
|
2025-10-03 00:31:34 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="T">值的类型。</typeparam>
|
|
|
|
|
|
/// <param name="key">要设置的键,不能为空或空白。</param>
|
|
|
|
|
|
/// <param name="value">要存储的值。</param>
|
|
|
|
|
|
/// <exception cref="ArgumentException">当键为 null 或空白时抛出。</exception>
|
|
|
|
|
|
public void Set<T>(string key, T value)
|
|
|
|
|
|
{
|
2025-10-10 14:08:23 +08:00
|
|
|
|
if (string.IsNullOrWhiteSpace(key)) throw new ArgumentException("键不能为空或空白。", nameof(key));
|
2025-10-03 00:31:34 +08:00
|
|
|
|
|
|
|
|
|
|
_data[key] = value;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-10-10 14:08:23 +08:00
|
|
|
|
/// 根据指定的键获取存储的值。
|
2025-10-03 00:31:34 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="T">期望值的类型。</typeparam>
|
|
|
|
|
|
/// <param name="key">要获取值的键,不能为空或空白。</param>
|
|
|
|
|
|
/// <param name="defaultValue">如果键不存在或类型不匹配时返回的默认值。默认为类型 T 的默认值。</param>
|
|
|
|
|
|
/// <returns>与键关联的值,如果键不存在或类型不匹配则返回 defaultValue。</returns>
|
|
|
|
|
|
/// <exception cref="ArgumentException">当键为 null 或空白时抛出。</exception>
|
2025-10-10 14:08:23 +08:00
|
|
|
|
public T Get<T>(string key, T defaultValue = default)
|
2025-10-03 00:31:34 +08:00
|
|
|
|
{
|
2025-10-10 14:08:23 +08:00
|
|
|
|
if (string.IsNullOrWhiteSpace(key)) throw new ArgumentException("键不能为空或空白。", nameof(key));
|
2025-10-03 00:31:34 +08:00
|
|
|
|
|
2025-10-10 14:08:23 +08:00
|
|
|
|
if (_data.TryGetValue(key, out var value))
|
2025-10-03 00:31:34 +08:00
|
|
|
|
{
|
2025-10-10 14:08:23 +08:00
|
|
|
|
if (value is T typedValue) return typedValue;
|
2025-10-03 00:31:34 +08:00
|
|
|
|
|
|
|
|
|
|
Debug.LogWarning(
|
|
|
|
|
|
$"警告: 键 '{key}' 存储的值类型为 '{value.GetType().Name}',但请求的类型为 '{typeof(T).Name}'。返回默认值。");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return defaultValue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-10-10 14:08:23 +08:00
|
|
|
|
/// 检查管理器中是否存在指定的键。
|
2025-10-03 00:31:34 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="key">要检查的键,不能为空或空白。</param>
|
|
|
|
|
|
/// <returns>如果键存在则为 true,否则为 false。</returns>
|
|
|
|
|
|
/// <exception cref="ArgumentException">当键为 null 或空白时抛出。</exception>
|
|
|
|
|
|
public bool HasKey(string key)
|
|
|
|
|
|
{
|
2025-10-10 14:08:23 +08:00
|
|
|
|
if (string.IsNullOrWhiteSpace(key)) throw new ArgumentException("键不能为空或空白。", nameof(key));
|
2025-10-03 00:31:34 +08:00
|
|
|
|
|
|
|
|
|
|
return _data.ContainsKey(key);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-10-10 14:08:23 +08:00
|
|
|
|
/// 从管理器中移除指定的键及其对应的值。
|
2025-10-03 00:31:34 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="key">要移除的键,不能为空或空白。</param>
|
|
|
|
|
|
/// <exception cref="ArgumentException">当键为 null 或空白时抛出。</exception>
|
|
|
|
|
|
public void Remove(string key)
|
|
|
|
|
|
{
|
2025-10-10 14:08:23 +08:00
|
|
|
|
if (string.IsNullOrWhiteSpace(key)) throw new ArgumentException("键不能为空或空白。", nameof(key));
|
2025-10-03 00:31:34 +08:00
|
|
|
|
|
|
|
|
|
|
_data.Remove(key);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-10-10 14:08:23 +08:00
|
|
|
|
/// ISavableSingleton 接口实现:当游戏状态需要被彻底重置时调用(例如,开始一个新游戏)。
|
|
|
|
|
|
/// 将清除所有存储的键值对。
|
2025-10-03 00:31:34 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
public void ResetState()
|
|
|
|
|
|
{
|
|
|
|
|
|
_data.Clear();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-10-10 14:08:23 +08:00
|
|
|
|
}
|