77 lines
2.5 KiB
TypeScript
77 lines
2.5 KiB
TypeScript
import { JsonAsset } from "cc";
|
||
|
||
import { singleton, Singleton } from "@max-studio/core/Singleton";
|
||
import LogUtils from "@max-studio/core/utils/LogUtils";
|
||
|
||
import { ConfigParseUtils } from "./ConfigParseUtils";
|
||
import { PetConfigData, parsePetConfigData } from "./PetConfigData";
|
||
import ResManager from "@max-studio/core/res/ResManager";
|
||
|
||
/**
|
||
* PetConfig配置管理器
|
||
*
|
||
* ⚠️ 此文件由配置表生成器自动生成,请勿手动修改!
|
||
* 如需修改,请编辑对应的Excel配置文件,然后重新生成
|
||
*/
|
||
@singleton({ auto: true })
|
||
export class PetConfigManager extends Singleton {
|
||
private configList: readonly PetConfigData[] = [];
|
||
private configMap = new Map<number, PetConfigData>();
|
||
|
||
private isLoaded = false;
|
||
|
||
protected async onInit(): Promise<void> {
|
||
await this.loadConfig();
|
||
}
|
||
|
||
private async loadConfig(): Promise<void> {
|
||
try {
|
||
const { err, asset } = await ResManager.getInstance().loadAsset<JsonAsset>({
|
||
path: "generated/data/PetConfig",
|
||
type: JsonAsset,
|
||
bundle: "configs",
|
||
});
|
||
this.parseConfig(<any>asset.json);
|
||
this.isLoaded = true;
|
||
} catch (err) {
|
||
LogUtils.error("PetConfigManager", "加载 PetConfig 配置失败:", err);
|
||
}
|
||
}
|
||
|
||
private parseConfig(data: any[]): void {
|
||
this.configList = Object.freeze(
|
||
data.map((item) => {
|
||
const config = parsePetConfigData(item);
|
||
const frozenConfig = ConfigParseUtils.deepFreeze(config);
|
||
return ConfigParseUtils.createReadonlyProxy(frozenConfig, "PetConfigData配置");
|
||
}),
|
||
);
|
||
this.configMap.clear();
|
||
|
||
for (const config of this.configList) {
|
||
this.configMap.set((config as any).id, config);
|
||
}
|
||
ConfigParseUtils.deepFreeze(this.configMap);
|
||
}
|
||
|
||
public getConfig(id: number): PetConfigData | null {
|
||
if (!this.isLoaded) {
|
||
LogUtils.warn("PetConfigManager", "PetConfig 配置尚未加载完成,请等待加载完成");
|
||
return null;
|
||
}
|
||
return this.configMap.get(id) || null;
|
||
}
|
||
|
||
public getAllConfigs(): PetConfigData[] {
|
||
if (!this.isLoaded) {
|
||
LogUtils.warn("PetConfigManager", "PetConfig 配置尚未加载完成,请等待加载完成");
|
||
return [];
|
||
}
|
||
return [...this.configList];
|
||
}
|
||
|
||
public isConfigLoaded(): boolean {
|
||
return this.isLoaded;
|
||
}
|
||
}
|