NetworkConfigManager.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using System.IO;
  2. using UnityEngine;
  3. namespace LightGlue.Unity.Config
  4. {
  5. /// <summary>
  6. /// 网络配置管理器:负责加载和保存网络配置到JSON文件
  7. /// 配置文件位置:Application.persistentDataPath/network_config.json
  8. /// </summary>
  9. public static class NetworkConfigManager
  10. {
  11. private const string CONFIG_FILE_NAME = "network_config.json";
  12. private static NetworkConfig _cachedConfig;
  13. /// <summary>
  14. /// 获取配置文件完整路径
  15. /// </summary>
  16. public static string GetConfigPath()
  17. {
  18. return Path.Combine(Application.persistentDataPath, CONFIG_FILE_NAME);
  19. }
  20. /// <summary>
  21. /// 加载网络配置(如果文件不存在则返回默认配置)
  22. /// </summary>
  23. public static NetworkConfig LoadConfig()
  24. {
  25. // 如果已缓存,直接返回
  26. if (_cachedConfig != null)
  27. return _cachedConfig;
  28. string configPath = GetConfigPath();
  29. // 如果配置文件不存在,返回默认配置
  30. if (!File.Exists(configPath))
  31. {
  32. Debug.Log($"[NetworkConfigManager] 配置文件不存在,使用默认配置: {configPath}");
  33. _cachedConfig = NetworkConfig.CreateDefault();
  34. return _cachedConfig;
  35. }
  36. try
  37. {
  38. string json = File.ReadAllText(configPath);
  39. _cachedConfig = JsonUtility.FromJson<NetworkConfig>(json);
  40. if (_cachedConfig != null && !json.Contains("pythonNoDisplay"))
  41. {
  42. _cachedConfig.pythonNoDisplay = true;
  43. }
  44. if (_cachedConfig == null)
  45. {
  46. Debug.LogWarning($"[NetworkConfigManager] 配置文件解析失败,使用默认配置");
  47. _cachedConfig = NetworkConfig.CreateDefault();
  48. }
  49. else
  50. {
  51. // 验证配置有效性
  52. if (!_cachedConfig.Validate())
  53. {
  54. Debug.LogWarning($"[NetworkConfigManager] 配置验证失败,使用默认配置");
  55. _cachedConfig = NetworkConfig.CreateDefault();
  56. }
  57. else
  58. {
  59. Debug.Log($"[NetworkConfigManager] 成功加载网络配置: {configPath}");
  60. }
  61. }
  62. }
  63. catch (System.Exception ex)
  64. {
  65. Debug.LogError($"[NetworkConfigManager] 加载配置失败: {ex.Message}");
  66. _cachedConfig = NetworkConfig.CreateDefault();
  67. }
  68. return _cachedConfig;
  69. }
  70. /// <summary>
  71. /// 保存网络配置到文件
  72. /// </summary>
  73. public static bool SaveConfig(NetworkConfig config)
  74. {
  75. if (config == null)
  76. {
  77. Debug.LogError("[NetworkConfigManager] 配置对象为空,无法保存");
  78. return false;
  79. }
  80. // 验证配置
  81. if (!config.Validate())
  82. {
  83. Debug.LogError("[NetworkConfigManager] 配置验证失败,无法保存");
  84. return false;
  85. }
  86. try
  87. {
  88. string configPath = GetConfigPath();
  89. string json = JsonUtility.ToJson(config, prettyPrint: true);
  90. // 确保目录存在
  91. string directory = Path.GetDirectoryName(configPath);
  92. if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory))
  93. {
  94. Directory.CreateDirectory(directory);
  95. }
  96. File.WriteAllText(configPath, json);
  97. _cachedConfig = config; // 更新缓存
  98. Debug.Log($"[NetworkConfigManager] 网络配置已保存: {configPath}");
  99. return true;
  100. }
  101. catch (System.Exception ex)
  102. {
  103. Debug.LogError($"[NetworkConfigManager] 保存配置失败: {ex.Message}");
  104. return false;
  105. }
  106. }
  107. /// <summary>
  108. /// 清除缓存(重新加载配置时使用)
  109. /// </summary>
  110. public static void ClearCache()
  111. {
  112. _cachedConfig = null;
  113. }
  114. /// <summary>
  115. /// 获取当前缓存的配置(不触发文件读取)
  116. /// </summary>
  117. public static NetworkConfig GetCachedConfig()
  118. {
  119. if (_cachedConfig == null)
  120. return LoadConfig();
  121. return _cachedConfig;
  122. }
  123. }
  124. }