| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- using System.IO;
- using UnityEngine;
- namespace LightGlue.Unity.Config
- {
- /// <summary>
- /// 网络配置管理器:负责加载和保存网络配置到JSON文件
- /// 配置文件位置:Application.persistentDataPath/network_config.json
- /// </summary>
- public static class NetworkConfigManager
- {
- private const string CONFIG_FILE_NAME = "network_config.json";
- private static NetworkConfig _cachedConfig;
- /// <summary>
- /// 获取配置文件完整路径
- /// </summary>
- public static string GetConfigPath()
- {
- return Path.Combine(Application.persistentDataPath, CONFIG_FILE_NAME);
- }
- /// <summary>
- /// 加载网络配置(如果文件不存在则返回默认配置)
- /// </summary>
- public static NetworkConfig LoadConfig()
- {
- // 如果已缓存,直接返回
- if (_cachedConfig != null)
- return _cachedConfig;
- string configPath = GetConfigPath();
- // 如果配置文件不存在,返回默认配置
- if (!File.Exists(configPath))
- {
- Debug.Log($"[NetworkConfigManager] 配置文件不存在,使用默认配置: {configPath}");
- _cachedConfig = NetworkConfig.CreateDefault();
- return _cachedConfig;
- }
- try
- {
- string json = File.ReadAllText(configPath);
- _cachedConfig = JsonUtility.FromJson<NetworkConfig>(json);
- if (_cachedConfig != null && !json.Contains("pythonNoDisplay"))
- {
- _cachedConfig.pythonNoDisplay = true;
- }
- if (_cachedConfig == null)
- {
- Debug.LogWarning($"[NetworkConfigManager] 配置文件解析失败,使用默认配置");
- _cachedConfig = NetworkConfig.CreateDefault();
- }
- else
- {
- // 验证配置有效性
- if (!_cachedConfig.Validate())
- {
- Debug.LogWarning($"[NetworkConfigManager] 配置验证失败,使用默认配置");
- _cachedConfig = NetworkConfig.CreateDefault();
- }
- else
- {
- Debug.Log($"[NetworkConfigManager] 成功加载网络配置: {configPath}");
- }
- }
- }
- catch (System.Exception ex)
- {
- Debug.LogError($"[NetworkConfigManager] 加载配置失败: {ex.Message}");
- _cachedConfig = NetworkConfig.CreateDefault();
- }
- return _cachedConfig;
- }
- /// <summary>
- /// 保存网络配置到文件
- /// </summary>
- public static bool SaveConfig(NetworkConfig config)
- {
- if (config == null)
- {
- Debug.LogError("[NetworkConfigManager] 配置对象为空,无法保存");
- return false;
- }
- // 验证配置
- if (!config.Validate())
- {
- Debug.LogError("[NetworkConfigManager] 配置验证失败,无法保存");
- return false;
- }
- try
- {
- string configPath = GetConfigPath();
- string json = JsonUtility.ToJson(config, prettyPrint: true);
- // 确保目录存在
- string directory = Path.GetDirectoryName(configPath);
- if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory))
- {
- Directory.CreateDirectory(directory);
- }
- File.WriteAllText(configPath, json);
- _cachedConfig = config; // 更新缓存
- Debug.Log($"[NetworkConfigManager] 网络配置已保存: {configPath}");
- return true;
- }
- catch (System.Exception ex)
- {
- Debug.LogError($"[NetworkConfigManager] 保存配置失败: {ex.Message}");
- return false;
- }
- }
- /// <summary>
- /// 清除缓存(重新加载配置时使用)
- /// </summary>
- public static void ClearCache()
- {
- _cachedConfig = null;
- }
- /// <summary>
- /// 获取当前缓存的配置(不触发文件读取)
- /// </summary>
- public static NetworkConfig GetCachedConfig()
- {
- if (_cachedConfig == null)
- return LoadConfig();
- return _cachedConfig;
- }
- }
- }
|