2025-08-12 16:08:58 +08:00
|
|
|
import {
|
|
|
|
|
resources,
|
|
|
|
|
BufferAsset,
|
|
|
|
|
assetManager,
|
|
|
|
|
AssetManager,
|
|
|
|
|
} from "cc";
|
2025-08-12 15:01:59 +08:00
|
|
|
import * as cfg from "../../schema/schema";
|
|
|
|
|
import ByteBuf from "db://assets/Scripts/luban/ByteBuf";
|
2025-09-01 16:26:37 +08:00
|
|
|
import { BaseConfig } from "../config/BaseConfig";
|
|
|
|
|
import { ThemeConfig } from "../config/ThemeConfig";
|
2025-08-12 15:01:59 +08:00
|
|
|
|
2025-09-01 16:26:37 +08:00
|
|
|
/**
|
|
|
|
|
* 集中定义数据ID,避免写错字符串
|
|
|
|
|
*/
|
|
|
|
|
export enum ConfigId {
|
|
|
|
|
Theme = "theme", // 主题配置
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** 构造器类型 */
|
|
|
|
|
type DataCtor<T extends BaseConfig = BaseConfig> = new () => T;
|
|
|
|
|
|
|
|
|
|
export class ConfigManager {
|
2025-08-15 15:24:31 +08:00
|
|
|
private static _instance: ConfigManager = null;
|
|
|
|
|
public static get I(): ConfigManager {
|
|
|
|
|
if (!this._instance) {
|
|
|
|
|
this._instance = new ConfigManager();
|
|
|
|
|
}
|
|
|
|
|
return this._instance;
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-12 15:01:59 +08:00
|
|
|
public static tables: cfg.Tables = null;
|
|
|
|
|
private static dataMap = new Map<string, Uint8Array>();
|
|
|
|
|
public static jsonFileNames: string[] = [];
|
|
|
|
|
|
2025-09-01 16:26:37 +08:00
|
|
|
// 数据实例缓存
|
|
|
|
|
private _dataMap: Map<string, BaseConfig> = new Map();
|
|
|
|
|
// 数据ID到构造器的映射
|
|
|
|
|
private _config: Map<string, DataCtor> = new Map();
|
|
|
|
|
|
2025-08-15 15:24:31 +08:00
|
|
|
private constructor() {}
|
|
|
|
|
|
|
|
|
|
public init() {
|
2025-08-12 15:01:59 +08:00
|
|
|
this.loadAllConfig();
|
2025-09-01 16:26:37 +08:00
|
|
|
this.registerAll();
|
2025-08-12 15:01:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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) => {
|
2025-08-12 16:08:58 +08:00
|
|
|
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) {
|
|
|
|
|
console.log(err);
|
|
|
|
|
reject && reject(err);
|
|
|
|
|
return;
|
|
|
|
|
} else {
|
|
|
|
|
_bundle.load(curFileName, BufferAsset, (err, data) => {
|
|
|
|
|
resolve(data);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
2025-08-12 15:01:59 +08:00
|
|
|
});
|
|
|
|
|
promise.then((data: BufferAsset) => {
|
|
|
|
|
if (data) {
|
|
|
|
|
ConfigManager.dataMap.set(
|
|
|
|
|
curFileName,
|
|
|
|
|
new Uint8Array(data.buffer().slice(0, data.buffer().byteLength))
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
console.error("静态配置加载失败" + curFileName);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
promises.push(promise);
|
|
|
|
|
}
|
|
|
|
|
Promise.all(promises).then((values) => {
|
|
|
|
|
console.log("静态配置加载完成");
|
2025-08-15 15:24:31 +08:00
|
|
|
ConfigManager.tables = new cfg.Tables(ConfigManager.I.getFileData);
|
2025-08-12 16:08:58 +08:00
|
|
|
for (let item of ConfigManager.tables.TbGirls.getDataList()) {
|
2025-08-12 15:01:59 +08:00
|
|
|
console.log(item);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
private getFileData(fileName: string): ByteBuf {
|
|
|
|
|
if (ConfigManager.dataMap.has(fileName)) {
|
|
|
|
|
return new ByteBuf(ConfigManager.dataMap.get(fileName));
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2025-09-01 16:26:37 +08:00
|
|
|
|
|
|
|
|
/** --------------------------------------------------------- 配置数据实例相关 --------------------------------------------------------- */
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 注册 数据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();
|
|
|
|
|
}
|
2025-08-12 15:01:59 +08:00
|
|
|
}
|