Files
Gen_Hack-and-Slash-Roguelit…/Client/Assets/Scripts/Managers/DefineManager.cs

117 lines
4.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using Data;
using UnityEngine;
using Utils;
namespace Managers
{
public class DefineManager : Singleton<DefineManager>
{
private static readonly string[] dataSetFilePath = { "Data", "Mod" };
public Dictionary<string, Dictionary<string, Define>> defines = new();
public Dictionary<string, DefinePack> packs = new();
/// <summary>
/// 初始化定义管理器,加载所有定义包并构建定义字典。
/// </summary>
/// <remarks>
/// 该方法执行以下操作:
/// 1. 获取指定路径下的所有子文件夹,每个子文件夹代表一个定义包。
/// 2. 遍历每个定义包,尝试加载其中的定义数据。
/// 3. 将加载的定义数据按类型分类,并存储到定义字典中。
/// </remarks>
public void Init()
{
var packFolder = Configs.ConfigProcessor.GetSubFolders(new(dataSetFilePath));
foreach (var folder in packFolder)
{
var pack = new DefinePack();
if (pack.LoadPack(folder)) packs.Add(pack.packID, pack);
}
Dictionary<Type, FieldInfo[]> fieldCache = new();
//不优化到循环里面是因为要先建立索引再链接
List<Tuple<Define, FieldInfo, Define>> defineCache = new();
foreach (var pack in packs)
{
foreach (var define in pack.Value.defines)
{
var typeName = define.Key;
var defList = define.Value;
if (!defines.ContainsKey(typeName))
defines[typeName] = new Dictionary<string, Define>();
foreach (var def in defList)
{
defines[typeName][def.defName] = def;
// 如果字段信息已经缓存,则直接使用缓存
if (!fieldCache.TryGetValue(def.GetType(), out var defineFields))
{
// 获取所有字段类型为 Define 或其派生类型的字段
defineFields = def.GetType()
.GetFields(BindingFlags.Public | BindingFlags.Instance)
.Where(field => typeof(Define).IsAssignableFrom(field.FieldType))
.ToArray();
// 缓存字段信息
fieldCache[def.GetType()] = defineFields;
}
foreach (var defineField in defineFields)
{
var defRef=(Define)defineField.GetValue(def);
if (defRef==null || !defRef.isReferene)
continue;
defineCache.Add(new(def, defineField, defRef));
}
}
}
}
foreach (var defRef in defineCache)
{
defRef.Item2.SetValue(defRef.Item1, FindDefine(defRef.Item3.description, defRef.Item3.defName));
}
}
/// <summary>
/// 查找指定定义类型的定义名对应的 Define 对象。
/// </summary>
/// <param name="defineType">定义类型</param>
/// <param name="defineName">定义名</param>
/// <returns>如果找到,返回 Define 对象;否则返回 null。</returns>
public Define FindDefine(string defineType, string defineName)
{
if (defines.TryGetValue(defineType, out var typeDict))
{
if (typeDict.TryGetValue(defineName, out var define))
{
return define;
}
}
return null;
}
public override string ToString()
{
if (packs == null || packs.Count == 0)
{
return "No packs available"; // 如果集合为空或为 null返回默认信息
}
var result = new System.Text.StringBuilder();
foreach (var definePack in packs)
{
result.AppendLine(definePack.ToString()); // 每个元素占一行
}
return result.ToString();
}
}
}