using System;
using System.Collections.Generic;
namespace SLSUtilities.General
{
///
/// 指令上下文 (Command Context)
/// 以强类型键值对存储指令执行时传递的游戏状态信息。
/// outerContext 由 CommandQueueManager 创建并传递给每条顶层指令;
/// selfContext 是每条指令自身私有的上下文,可携带局部参数。
///
public class CommandContext
{
// 保留公开字段以兼容尚未迁移的调用点,后续步骤中逐一移除直接访问
public readonly Dictionary context;
public CommandContext()
{
context = new Dictionary();
}
public CommandContext((string, object)[] pairs)
{
context = new Dictionary();
foreach ((string key, object value) in pairs)
context[key] = value;
}
public CommandContext(Dictionary initialInfo)
{
context = new Dictionary();
foreach (var pair in initialInfo)
context[pair.Key] = pair.Value;
}
// ── 强类型访问 API ────────────────────────────────────────────────
/// 以强类型写入一个值。
public void Set(string key, T value)
{
context[key] = value;
}
///
/// 以强类型读取一个值。
/// 找不到 key 或类型不匹配时抛出 。
///
public T Get(string key)
{
if (context.TryGetValue(key, out object raw) && raw is T typed)
return typed;
throw new KeyNotFoundException(
$"CommandContext 中不存在键 '{key}',或其类型与 {typeof(T).Name} 不匹配。");
}
///
/// 以强类型尝试读取一个值。
/// 找不到 key 或类型不匹配时返回 false,result 为默认值。
///
public bool TryGet(string key, out T result)
{
if (context.TryGetValue(key, out object raw) && raw is T typed)
{
result = typed;
return true;
}
result = default;
return false;
}
// ── 旧 API(向后兼容,后续步骤迁移完成后移除) ──────────────────
///
/// 以强类型读取一个值。找不到或类型不匹配时返回默认值并写日志警告。
/// 新代码请改用 或 。
///
[Obsolete("请改用 Get(key) 或 TryGet(key, out result)。")]
public T GetInfo(string key)
{
if (TryGet(key, out T result))
return result;
UnityEngine.Debug.LogWarning(
$"CommandContext 中不存在键 '{key}',或其类型不匹配。返回默认值。");
return default;
}
// ── 工具方法 ──────────────────────────────────────────────────────
/// 返回一个拥有相同键值对副本的新上下文(浅拷贝)。
public CommandContext Clone()
{
var copy = new CommandContext();
foreach (var pair in context)
copy.context[pair.Key] = pair.Value;
return copy;
}
/// 将 other 的所有键值合并到当前上下文(同键覆盖),返回自身以支持链式调用。
public CommandContext Merge(CommandContext other)
{
foreach (var pair in other.context)
context[pair.Key] = pair.Value;
return this;
}
}
}