using System; using System.Collections.Generic; using System.IO; using System.Linq; using AibisDream.Framework; using AibisDream.Utility; using UnityEngine; namespace AibisDream.Kit { public class ConfigUtil : SingletonBase { private const string CharacterConfigPath = "/Config/character.csv"; private const string BaseConfigPath = "/Config/base_config.csv"; private const string SpriteGroupPath = "/Config/sprite_group.json"; private Dictionary _baseConfigDic; private Dictionary _characters; private Dictionary _spriteConfigDict; /// /// 用于单例模式 /// private ConfigUtil() { } /// /// 调用时初始化 /// public override void OnSingletonInit() { InitBaseConfig(); InitCharacterConfig(); InitSpriteGroupConfig(); } private void InitBaseConfig() { var baseList = CsvUtil.Read(Application.streamingAssetsPath + BaseConfigPath); if (baseList.Count <= 0) { Debug.Log("基础配置不存在"); } // 基础配置只有key和value _baseConfigDic = new Dictionary(); foreach (var arr in baseList) { _baseConfigDic.Add(arr[0], arr[1]); } } private void InitCharacterConfig() { var characterList = CsvUtil.ReadAsBean(Application.streamingAssetsPath + CharacterConfigPath); _characters = characterList.ToDictionary(item => item.Key, item => item); } private void InitSpriteGroupConfig() { _spriteConfigDict = JsonUtil.ReadBeanDict(Application.streamingAssetsPath + SpriteGroupPath) ?? new Dictionary(); } /// /// 按key获取角色预设 /// /// key /// 角色配置 /// 是否能获取 public bool TryGetCharacter(string key, out Character character) { return _characters.TryGetValue(key, out character); } /// /// 按key获取基础配置 /// /// key /// 配置项 /// 是否能获取 public bool TryGetBaseConfig(string key, out string value) { return _baseConfigDic.TryGetValue(key, out value); } /// /// 保存SpriteGroupConfig /// /// public void SaveSpriteGroupConfig(SpriteGroupConfig config) { _spriteConfigDict[config.key] = config; SaveConfig(_spriteConfigDict, Application.streamingAssetsPath + SpriteGroupPath); } /// /// 尝试获取Config /// /// /// /// public bool TryGetSpriteConfig(string key, out SpriteGroupConfig config) { return _spriteConfigDict.TryGetValue(key, out config); } /// /// 按类型读取Json配置 /// /// 路径 /// key /// 结果 /// 类型 /// 是否成功 public bool TryLoadConfig(string path, string key, out T res) { try { res = JsonUtil.ReadBean(path, key); return true; } catch (Exception e) { res = default; Debug.Log(e); return false; } } /// /// 按类型读取 /// /// 路径 /// 结果 /// 类型 /// 是否成功 public bool TryLoadConfig(string path, out T res) { try { string formattedKey = JsonUtil.FormatAsJsonPath(path); res = JsonUtil.ReadBean(formattedKey); return true; } catch (Exception e) { res = default; Debug.Log(e); return false; } } /// /// 保存Bean /// /// /// /// public void SaveConfig(T bean, string path) { var jsonPath = JsonUtil.FormatAsJsonPath(path); JsonUtil.SaveBean(bean, jsonPath); } /// /// 保存配置 /// /// bean /// 键 /// 路径 /// 保存类型 /// 类型 public void SaveConfig(T bean, string key, string path, SaveMode saveMode = SaveMode.Add) { JsonUtil.SaveBean(bean, key, path, saveMode); } /// /// 查询某个文件夹下的所有文件 /// /// /// /// public string[] GetFileNames(string folderPath) { if (!Directory.Exists(folderPath)) { return Array.Empty(); // 如果目录不存在,返回空数组 } // 获取所有 .json 文件的完整路径 string[] filePaths = Directory.GetFiles(folderPath, "*.json"); // 提取文件名(不包括路径) string[] fileNames = filePaths.Select(Path.GetFileName).ToArray(); return fileNames; } } }