81 lines
2.3 KiB
TypeScript
81 lines
2.3 KiB
TypeScript
import {
|
|
_decorator,
|
|
resources,
|
|
BufferAsset,
|
|
Component,
|
|
assetManager,
|
|
AssetManager,
|
|
} from "cc";
|
|
import * as cfg from "../../schema/schema";
|
|
import ByteBuf from "db://assets/Scripts/luban/ByteBuf";
|
|
|
|
const { ccclass, property } = _decorator;
|
|
@ccclass("ConfigManager")
|
|
export default class ConfigManager extends Component {
|
|
public static tables: cfg.Tables = null;
|
|
private static dataMap = new Map<string, Uint8Array>();
|
|
public static jsonFileNames: string[] = [];
|
|
|
|
onLoad() {
|
|
this.loadAllConfig();
|
|
}
|
|
|
|
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) {
|
|
console.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 {
|
|
console.error("静态配置加载失败" + curFileName);
|
|
}
|
|
});
|
|
promises.push(promise);
|
|
}
|
|
Promise.all(promises).then((values) => {
|
|
console.log("静态配置加载完成");
|
|
ConfigManager.tables = new cfg.Tables(this.getFileData);
|
|
for (let item of ConfigManager.tables.TbGirls.getDataList()) {
|
|
console.log(item);
|
|
}
|
|
});
|
|
}
|
|
private getFileData(fileName: string): ByteBuf {
|
|
if (ConfigManager.dataMap.has(fileName)) {
|
|
return new ByteBuf(ConfigManager.dataMap.get(fileName));
|
|
}
|
|
return null;
|
|
}
|
|
}
|