修改ConfigManager,定义配置数据继承体系
This commit is contained in:
@@ -6,8 +6,20 @@ import {
|
||||
} from "cc";
|
||||
import * as cfg from "../../schema/schema";
|
||||
import ByteBuf from "db://assets/Scripts/luban/ByteBuf";
|
||||
import { BaseConfig } from "../config/BaseConfig";
|
||||
import { ThemeConfig } from "../config/ThemeConfig";
|
||||
|
||||
export default class ConfigManager {
|
||||
/**
|
||||
* 集中定义数据ID,避免写错字符串
|
||||
*/
|
||||
export enum ConfigId {
|
||||
Theme = "theme", // 主题配置
|
||||
}
|
||||
|
||||
/** 构造器类型 */
|
||||
type DataCtor<T extends BaseConfig = BaseConfig> = new () => T;
|
||||
|
||||
export class ConfigManager {
|
||||
private static _instance: ConfigManager = null;
|
||||
public static get I(): ConfigManager {
|
||||
if (!this._instance) {
|
||||
@@ -20,10 +32,16 @@ export default class ConfigManager {
|
||||
private static dataMap = new Map<string, Uint8Array>();
|
||||
public static jsonFileNames: string[] = [];
|
||||
|
||||
// 数据实例缓存
|
||||
private _dataMap: Map<string, BaseConfig> = new Map();
|
||||
// 数据ID到构造器的映射
|
||||
private _config: Map<string, DataCtor> = new Map();
|
||||
|
||||
private constructor() {}
|
||||
|
||||
public init() {
|
||||
this.loadAllConfig();
|
||||
this.registerAll();
|
||||
}
|
||||
|
||||
public loadConfigName(): void {
|
||||
@@ -83,4 +101,87 @@ export default class ConfigManager {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/** --------------------------------------------------------- 配置数据实例相关 --------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* 注册 数据ID → 构造器
|
||||
*/
|
||||
private registerAll(): void {
|
||||
this.register(ConfigId.Theme, ThemeConfig);
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册一个数据ID及其对应的构造器
|
||||
*/
|
||||
private register<T extends BaseConfig>(configId: string, ctor: DataCtor<T>): void {
|
||||
if (this._config.has(configId)) {
|
||||
console.warn(`[ConfigManager] 重复注册数据ID: ${configId},将覆盖旧构造器`);
|
||||
}
|
||||
this._config.set(configId, ctor);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取数据(已存在则直接返回;否则按配置自动实例化并缓存)
|
||||
*/
|
||||
public getDataById<T extends BaseConfig = BaseConfig>(configId: string): T | null {
|
||||
// 先从缓存取
|
||||
const cached = this._dataMap.get(configId) as T | undefined;
|
||||
if (cached) return cached;
|
||||
|
||||
// 缓存没有→ 查找构造器
|
||||
const Ctor = this._config.get(configId) as DataCtor<T> | undefined;
|
||||
if (!Ctor) {
|
||||
console.warn(`[ConfigManager] 未找到 configId 的构造器配置: ${configId}`);
|
||||
return null;
|
||||
}
|
||||
|
||||
// 动态创建 & init & 缓存
|
||||
const instance = new Ctor();
|
||||
// 若你的 BaseConfig.init 接受 configId,可传入
|
||||
instance.init?.(configId);
|
||||
this._dataMap.set(configId, instance);
|
||||
|
||||
console.log(`[ConfigManager] 实例化并缓存数据: ${configId}`);
|
||||
return instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除数据
|
||||
*/
|
||||
public removeData(configId: string): boolean {
|
||||
const data = this._dataMap.get(configId);
|
||||
if (data) {
|
||||
data.destroy?.();
|
||||
this._dataMap.delete(configId);
|
||||
console.log(`[ConfigManager] 移除数据: ${configId}`);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 重置所有数据
|
||||
*/
|
||||
public resetAllData(): void {
|
||||
console.log("[ConfigManager] 重置所有数据");
|
||||
this._dataMap.forEach((data) => data.reset?.());
|
||||
}
|
||||
|
||||
/**
|
||||
* 清空所有数据
|
||||
*/
|
||||
public clearAllData(): void {
|
||||
console.log("[ConfigManager] 清空所有数据");
|
||||
this._dataMap.forEach((data) => data.clear?.());
|
||||
}
|
||||
|
||||
/**
|
||||
* 销毁所有数据
|
||||
*/
|
||||
public destroyAllData(): void {
|
||||
console.log("[ConfigManager] 销毁所有数据");
|
||||
this._dataMap.forEach((data) => data.destroy?.());
|
||||
this._dataMap.clear();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user