JsonConfigLoader.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import { BundleLoader } from './BundleLoader';
  2. /**
  3. * 统一的JSON配置加载器
  4. * 提供类型安全的JSON配置加载功能
  5. */
  6. export class JsonConfigLoader {
  7. private static instance: JsonConfigLoader;
  8. private configCache: Map<string, any> = new Map();
  9. // JSON文件路径映射
  10. private static readonly CONFIG_PATHS = {
  11. shop: 'shop',
  12. weapons: 'weapons',
  13. wall: 'wall',
  14. skills: 'skill',
  15. skillConfig: 'skill_config',
  16. enemies: 'enemies',
  17. ballPrice: 'ball_price_config',
  18. ballController: 'ballController'
  19. } as const;
  20. public static getInstance(): JsonConfigLoader {
  21. if (!JsonConfigLoader.instance) {
  22. JsonConfigLoader.instance = new JsonConfigLoader();
  23. }
  24. return JsonConfigLoader.instance;
  25. }
  26. /**
  27. * 加载JSON配置文件
  28. * @param configType 配置类型
  29. * @param useCache 是否使用缓存
  30. * @returns 配置数据
  31. */
  32. public async loadConfig<T = any>(
  33. configType: keyof typeof JsonConfigLoader.CONFIG_PATHS,
  34. useCache: boolean = true
  35. ): Promise<T | null> {
  36. try {
  37. // 检查缓存
  38. if (useCache && this.configCache.has(configType)) {
  39. return this.configCache.get(configType) as T;
  40. }
  41. const configPath = JsonConfigLoader.CONFIG_PATHS[configType];
  42. const bundleLoader = BundleLoader.getInstance();
  43. const asset = await bundleLoader.loadDataJson(configPath);
  44. if (asset && asset.json) {
  45. const config = asset.json as T;
  46. // 缓存配置
  47. if (useCache) {
  48. this.configCache.set(configType, config);
  49. }
  50. console.log(`[JsonConfigLoader] ${configType}.json 加载成功`);
  51. return config;
  52. } else {
  53. throw new Error(`配置文件 ${configType}.json 数据为空`);
  54. }
  55. } catch (error) {
  56. console.error(`[JsonConfigLoader] ${configType}.json 加载失败:`, error);
  57. return null;
  58. }
  59. }
  60. /**
  61. * 预加载多个配置文件
  62. * @param configTypes 配置类型数组
  63. */
  64. public async preloadConfigs(
  65. configTypes: (keyof typeof JsonConfigLoader.CONFIG_PATHS)[]
  66. ): Promise<void> {
  67. const loadPromises = configTypes.map(type => this.loadConfig(type));
  68. await Promise.all(loadPromises);
  69. }
  70. /**
  71. * 清除缓存
  72. * @param configType 可选,指定清除某个配置的缓存
  73. */
  74. public clearCache(configType?: keyof typeof JsonConfigLoader.CONFIG_PATHS): void {
  75. if (configType) {
  76. this.configCache.delete(configType);
  77. } else {
  78. this.configCache.clear();
  79. }
  80. }
  81. /**
  82. * 重新加载配置
  83. * @param configType 配置类型
  84. */
  85. public async reloadConfig<T = any>(
  86. configType: keyof typeof JsonConfigLoader.CONFIG_PATHS
  87. ): Promise<T | null> {
  88. this.clearCache(configType);
  89. return this.loadConfig<T>(configType, true);
  90. }
  91. /**
  92. * 获取已缓存的配置
  93. * @param configType 配置类型
  94. * @returns 缓存的配置数据,如果不存在则返回null
  95. */
  96. public getCachedConfig<T = any>(
  97. configType: keyof typeof JsonConfigLoader.CONFIG_PATHS
  98. ): T | null {
  99. return this.configCache.get(configType) as T || null;
  100. }
  101. /**
  102. * 检查配置是否已缓存
  103. * @param configType 配置类型
  104. * @returns 是否已缓存
  105. */
  106. public isCached(configType: keyof typeof JsonConfigLoader.CONFIG_PATHS): boolean {
  107. return this.configCache.has(configType);
  108. }
  109. /**
  110. * 获取所有可用的配置类型
  111. * @returns 配置类型数组
  112. */
  113. public getAvailableConfigTypes(): (keyof typeof JsonConfigLoader.CONFIG_PATHS)[] {
  114. return Object.keys(JsonConfigLoader.CONFIG_PATHS) as (keyof typeof JsonConfigLoader.CONFIG_PATHS)[];
  115. }
  116. }
  117. // 导出类型定义
  118. export type ConfigType = keyof typeof JsonConfigLoader['CONFIG_PATHS'];
  119. // 导出单例实例的便捷方法
  120. export const jsonConfigLoader = JsonConfigLoader.getInstance();