读表修改

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