197 lines
5.6 KiB
TypeScript
197 lines
5.6 KiB
TypeScript
import { resources, BufferAsset, assetManager, AssetManager } 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";
|
|
import { PurchaseConfig } from "../config/PurchaseConfig";
|
|
import { GlobalConfig } from "../config/GlobalConfig";
|
|
import { GirlConfig } from "../config/GirlConfig";
|
|
import { GirlDetailConfig } from "../config/GirlDetailConfig";
|
|
import { AiCharacterConfig } from "../config/AiCharacterConfig";
|
|
import { logger } from "db://assets/Scripts/Main/Common/Logger";
|
|
|
|
/**
|
|
* 集中定义数据ID,避免写错字符串
|
|
*/
|
|
export enum ConfigId {
|
|
Theme = "theme", // 主题配置
|
|
Purchase = "purchase", // 商城配置
|
|
Global = "global", // 全局配置
|
|
Girl = "girl", // 技师的简要配置
|
|
GirlDetail = "girlDetail", // 技师的详细配置
|
|
AiCharacter = "aiCharacter", // 技师的详细配置
|
|
}
|
|
|
|
/** 构造器类型 */
|
|
type DataCtor<T extends BaseConfig = BaseConfig> = new () => T;
|
|
|
|
export class ConfigManager {
|
|
private static _instance: ConfigManager = null;
|
|
public static get I(): ConfigManager {
|
|
if (!this._instance) {
|
|
this._instance = new ConfigManager();
|
|
}
|
|
return this._instance;
|
|
}
|
|
|
|
public static tables: cfg.Tables = null;
|
|
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 {
|
|
ConfigManager.jsonFileNames = cfg.Tables.getTableNames();
|
|
}
|
|
|
|
public loadAllConfig() {
|
|
this.loadConfigName();
|
|
let promises: Promise<unknown>[] = [];
|
|
for (let curFileName of ConfigManager.jsonFileNames) {
|
|
let promise = new Promise((resolve, reject) => {
|
|
let bundle = assetManager.getBundle("DataTable");
|
|
if (bundle) {
|
|
bundle.load(curFileName, BufferAsset, (err, data) => {
|
|
resolve(data);
|
|
});
|
|
} else {
|
|
assetManager.loadBundle(
|
|
"DataTable",
|
|
(err: Error, _bundle: AssetManager.Bundle) => {
|
|
if (err) {
|
|
logger.log(err);
|
|
reject && reject(err);
|
|
return;
|
|
} else {
|
|
_bundle.load(curFileName, BufferAsset, (err, data) => {
|
|
resolve(data);
|
|
});
|
|
}
|
|
}
|
|
);
|
|
}
|
|
});
|
|
promise.then((data: BufferAsset) => {
|
|
if (data) {
|
|
ConfigManager.dataMap.set(
|
|
curFileName,
|
|
new Uint8Array(data.buffer().slice(0, data.buffer().byteLength))
|
|
);
|
|
} else {
|
|
logger.error("静态配置加载失败" + curFileName);
|
|
}
|
|
});
|
|
promises.push(promise);
|
|
}
|
|
Promise.all(promises).then((values) => {
|
|
logger.log("静态配置加载完成");
|
|
ConfigManager.tables = new cfg.Tables(ConfigManager.I.getFileData);
|
|
});
|
|
}
|
|
private getFileData(fileName: string): ByteBuf {
|
|
if (ConfigManager.dataMap.has(fileName)) {
|
|
return new ByteBuf(ConfigManager.dataMap.get(fileName));
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/** --------------------------------------------------------- 配置数据实例相关 --------------------------------------------------------- */
|
|
|
|
/**
|
|
* 注册 数据ID → 构造器
|
|
*/
|
|
private registerAll(): void {
|
|
this.register(ConfigId.Theme, ThemeConfig);
|
|
this.register(ConfigId.Purchase, PurchaseConfig);
|
|
this.register(ConfigId.Global, GlobalConfig);
|
|
this.register(ConfigId.Girl, GirlConfig);
|
|
this.register(ConfigId.GirlDetail, GirlDetailConfig);
|
|
this.register(ConfigId.AiCharacter, AiCharacterConfig);
|
|
}
|
|
|
|
/**
|
|
* 注册一个数据ID及其对应的构造器
|
|
*/
|
|
private register<T extends BaseConfig>(
|
|
configId: string,
|
|
ctor: DataCtor<T>
|
|
): void {
|
|
if (this._config.has(configId)) {
|
|
logger.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) {
|
|
logger.warn(`[ConfigManager] 未找到 configId 的构造器配置: ${configId}`);
|
|
return null;
|
|
}
|
|
|
|
// 动态创建 & init & 缓存
|
|
const instance = new Ctor();
|
|
// 若你的 BaseConfig.init 接受 configId,可传入
|
|
instance.init?.(configId);
|
|
this._dataMap.set(configId, instance);
|
|
return instance;
|
|
}
|
|
|
|
/**
|
|
* 移除数据
|
|
*/
|
|
public removeData(configId: string): boolean {
|
|
const data = this._dataMap.get(configId);
|
|
if (data) {
|
|
data.destroy?.();
|
|
this._dataMap.delete(configId);
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* 重置所有数据
|
|
*/
|
|
public resetAllData(): void {
|
|
this._dataMap.forEach((data) => data.reset?.());
|
|
}
|
|
|
|
/**
|
|
* 清空所有数据
|
|
*/
|
|
public clearAllData(): void {
|
|
this._dataMap.forEach((data) => data.clear?.());
|
|
}
|
|
|
|
/**
|
|
* 销毁所有数据
|
|
*/
|
|
public destroyAllData(): void {
|
|
this._dataMap.forEach((data) => data.destroy?.());
|
|
this._dataMap.clear();
|
|
}
|
|
}
|