diff --git a/Assets/SGModule/ConfigLoader/SGModule/Scripts/ConfigLoader/ConfigFileManager.cs b/Assets/SGModule/ConfigLoader/SGModule/Scripts/ConfigLoader/ConfigFileManager.cs index 9c8598f..930ed97 100644 --- a/Assets/SGModule/ConfigLoader/SGModule/Scripts/ConfigLoader/ConfigFileManager.cs +++ b/Assets/SGModule/ConfigLoader/SGModule/Scripts/ConfigLoader/ConfigFileManager.cs @@ -3,8 +3,10 @@ using System.IO; using SGModule.Common.Helper; using UnityEngine; -namespace SGModule.ConfigLoader { - public class ConfigFileManager { +namespace SGModule.ConfigLoader +{ + public class ConfigFileManager + { private const int MaxErrorCount = 6; private const string ConfigFileNameKey = "ConfigFileName"; private const string FirstLaunchKey = "FirstLaunch"; @@ -15,44 +17,55 @@ namespace SGModule.ConfigLoader { private static bool IsFirstLaunch => !PlayerPrefs.HasKey(FirstLaunchKey); private static string SavedConfigFileName => PlayerPrefs.GetString(ConfigFileNameKey); - private static void SetFirstLaunch() { + private static void SetFirstLaunch() + { PlayerPrefs.SetInt(FirstLaunchKey, 1); } - private static void SetSavedConfigFileName(string name) { + private static void SetSavedConfigFileName(string name) + { PlayerPrefs.SetString(ConfigFileNameKey, name); } - private void IncrementErrorCount() { + private void IncrementErrorCount() + { _initConfigErrorCount++; } // 检查是否需要下载新配置,基于传入的期望配置文件名setting - private static bool HasNewConfig(string expectedSetting) { - if (string.IsNullOrEmpty(SavedConfigFileName)) { + private static bool HasNewConfig(string expectedSetting) + { + if (string.IsNullOrEmpty(SavedConfigFileName)) + { return true; } return !SavedConfigFileName.Equals(expectedSetting); } - private void FirstLaunchCopyConfig(Action callback) { + private void FirstLaunchCopyConfig(Action callback) + { SetFirstLaunch(); - FileNetworkManager.Instance.CopyStreamingAssetsToPersistentDataPath(result => { + FileNetworkManager.Instance.CopyStreamingAssetsToPersistentDataPath(result => + { var isSuccess = false; - if (result) { + if (result) + { var names = FileNetworkManager.Instance.GetFileNamesFromPersistentDataPath(FileNetworkManager.FolderName); - if (names.Length > 0) { + if (names.Length > 0) + { SetSavedConfigFileName(names[0]); isSuccess = true; } } - else { + else + { Log.ConfigLoader.Error("拷贝文件失败 请检查Configs文件夹是否有配置文件"); } - if (!isSuccess) { + if (!isSuccess) + { IncrementErrorCount(); } @@ -60,16 +73,20 @@ namespace SGModule.ConfigLoader { }); } - private void DownloadConfig(string cdnUrl, string setting, Action callback) { + private void DownloadConfig(string cdnUrl, string setting, Action callback) + { var url = $"{cdnUrl}/config/{setting}"; Log.ConfigLoader.Info($"开始下载配置文件 {url}", false); - FileNetworkManager.Instance.ReadData(url, result => { - if (!string.IsNullOrEmpty(result)) { + FileNetworkManager.Instance.ReadData(url, result => + { + if (!string.IsNullOrEmpty(result)) + { SetSavedConfigFileName(setting); FileNetworkManager.Instance.WriteToPersistentData(FileNetworkManager.Instance.GetConfigFOlderPath(), setting, result); callback?.Invoke(true, result); } - else { + else + { Log.ConfigLoader.Error("下载配置文件失败"); IncrementErrorCount(); callback?.Invoke(false, null); @@ -77,40 +94,95 @@ namespace SGModule.ConfigLoader { }); } - private void ReadLocalConfig(Action callback) { + private void ReadLocalConfig(Action callback) + { var savedCfgName = SavedConfigFileName; Log.ConfigLoader.Info($"开始读取本地配置文件 {savedCfgName}", false); - if (string.IsNullOrEmpty(savedCfgName)) { + if (string.IsNullOrEmpty(savedCfgName)) + { IncrementErrorCount(); callback?.Invoke(false, null); return; } var path = Path.Combine(FileNetworkManager.Instance.GetConfigFOlderPath(), savedCfgName); - FileNetworkManager.Instance.ReadData(path, result => { - if (!string.IsNullOrEmpty(result)) { - callback?.Invoke(true, result); + bool have_config = false; + if (Directory.Exists(path)) + { + // 获取该目录下的所有文件 + string[] files = Directory.GetFiles(path); + + if (files.Length > 0) + { + Debug.Log("文件夹下有文件"); + have_config = true; } - else { - Log.ConfigLoader.Error("读取本地数据异常"); - IncrementErrorCount(); - callback?.Invoke(false, null); + else + { + Debug.Log("文件夹为空"); } - }); + } + else + { + Debug.Log("文件夹不存在"); + + } + if (have_config) + { + FileNetworkManager.Instance.ReadData(path, result => + { + if (!string.IsNullOrEmpty(result)) + { + callback?.Invoke(true, result); + } + else + { + Log.ConfigLoader.Error("读取本地数据异常"); + IncrementErrorCount(); + callback?.Invoke(false, null); + } + }); + } + else + { + FirstLaunchCopyConfig(success => + { + FileNetworkManager.Instance.ReadData(path, result => + { + if (!string.IsNullOrEmpty(result)) + { + callback?.Invoke(true, result); + } + else + { + Log.ConfigLoader.Error("读取本地数据异常"); + IncrementErrorCount(); + callback?.Invoke(false, null); + } + }); + }); + + + } } /// /// 配置文件检查与加载流程 /// - public void CheckAndLoadConfig(ConfigInitOptions initOptions, Action onConfigLoaded, Action callback) { - if (HasExceededMaxErrors) { + public void CheckAndLoadConfig(ConfigInitOptions initOptions, Action onConfigLoaded, Action callback) + { + if (HasExceededMaxErrors) + { callback?.Invoke(ConfigLoaderState.Failed); return; } - if (IsFirstLaunch) { - FirstLaunchCopyConfig(success => { - if (!success) { + if (IsFirstLaunch) + { + FirstLaunchCopyConfig(success => + { + if (!success) + { IncrementErrorCount(); } @@ -119,17 +191,24 @@ namespace SGModule.ConfigLoader { return; } - if (HasNewConfig(initOptions.Setting)) { - DownloadConfig(initOptions.CdnUrl, initOptions.Setting, (success, json) => { - if (success) { + if (HasNewConfig(initOptions.Setting)) + { + DownloadConfig(initOptions.CdnUrl, initOptions.Setting, (success, json) => + { + if (success) + { ReloadConfig(json, onConfigLoaded, callback); } - else { - ReadLocalConfig((readSuccess, localJson) => { - if (readSuccess) { + else + { + ReadLocalConfig((readSuccess, localJson) => + { + if (readSuccess) + { ReloadConfig(localJson, onConfigLoaded, callback); } - else { + else + { callback?.Invoke(ConfigLoaderState.Failed); } }); @@ -138,13 +217,18 @@ namespace SGModule.ConfigLoader { return; } - ReadLocalConfig((success, json) => { - if (success) { + ReadLocalConfig((success, json) => + { + if (success) + { ReloadConfig(json, onConfigLoaded, callback); } - else { - FirstLaunchCopyConfig(result => { - if (!result) { + else + { + FirstLaunchCopyConfig(result => + { + if (!result) + { IncrementErrorCount(); } @@ -154,8 +238,10 @@ namespace SGModule.ConfigLoader { }); } - private static void ReloadConfig(string json, Action onConfigLoaded, Action callback) { - if (string.IsNullOrWhiteSpace(json)) { + private static void ReloadConfig(string json, Action onConfigLoaded, Action callback) + { + if (string.IsNullOrWhiteSpace(json)) + { callback?.Invoke(ConfigLoaderState.JsonEmptyError); return; }