CommonDataManager.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using SimpleJSON;
  2. using UnityEngine;
  3. using System.Collections.Generic;
  4. using ET;
  5. using GFG.HotUpdate.LubanConfig;
  6. using GFGGame;
  7. public static class CommonDataManager
  8. {
  9. public static cfg.Tables Tables;
  10. private static Dictionary<string, JSONNode> _jsonCache = new Dictionary<string, JSONNode>();
  11. private static int _totalFilesToLoad;
  12. private static int _loadedFilesCount;
  13. public static void InitAllAsync(System.Action onComplete)
  14. {
  15. // 所有需要加载的配置表文件列表
  16. var filesToLoad = LubanConfigFiles.GetFilesToLoad();
  17. _totalFilesToLoad = filesToLoad.Count;
  18. _loadedFilesCount = 0;
  19. foreach (var file in filesToLoad)
  20. {
  21. LoadJsonFile(file, () =>
  22. {
  23. _loadedFilesCount++;
  24. if (_loadedFilesCount == _totalFilesToLoad)
  25. {
  26. // 所有文件加载完成后初始化Tables
  27. Tables = new cfg.Tables(GetCachedJson);
  28. onComplete?.Invoke();
  29. }
  30. });
  31. }
  32. }
  33. public static void InitOneAsync(System.Action onComplete)
  34. {
  35. LoadJsonFile("gfgcfg_tblfunctionopencfg", () =>
  36. {
  37. // 所有文件加载完成后初始化Tables
  38. Tables = new cfg.Tables(GetCachedJson);
  39. onComplete?.Invoke();
  40. });
  41. }
  42. private static void LoadJsonFile(string file, System.Action onLoaded)
  43. {
  44. var url = GetFileUrl(file);
  45. HttpTool.Instance.Get(url, (string data) =>
  46. {
  47. if (string.IsNullOrEmpty(data))
  48. {
  49. Debug.LogError($"Load luban json failed: {file}");
  50. onLoaded?.Invoke();
  51. return;
  52. }
  53. lock (_jsonCache)
  54. {
  55. _jsonCache[file] = JSON.Parse(data);
  56. }
  57. onLoaded?.Invoke();
  58. });
  59. }
  60. private static string GetFileUrl(string fileName)
  61. {
  62. // 根据你的实际URL规则构造文件URL
  63. return $"{LauncherConfig.launcherRootUrl}webglconfig/{fileName}.json?t={TimeHelper.ClientNow()}";
  64. }
  65. private static JSONNode GetCachedJson(string file)
  66. {
  67. lock (_jsonCache)
  68. {
  69. if (_jsonCache.TryGetValue(file, out var json))
  70. {
  71. return json;
  72. }
  73. //Debug.LogError($"Cached json not found: {file}");
  74. return null;
  75. }
  76. }
  77. }