引入luban配表 需要命令行 npm install buffer
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
import { _decorator, resources, BufferAsset, Component } 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) => {
|
||||
resources.load("test/" + 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.Tbitem.getDataList()) {
|
||||
console.log(item);
|
||||
}
|
||||
});
|
||||
}
|
||||
private getFileData(fileName: string): ByteBuf {
|
||||
if (ConfigManager.dataMap.has(fileName)) {
|
||||
return new ByteBuf(ConfigManager.dataMap.get(fileName));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.24",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "fbd435ab-238b-4ae8-a5ee-1823eec61491",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
Reference in New Issue
Block a user