142 lines
4.1 KiB
TypeScript
Raw Permalink Normal View History

2023-11-07 09:17:57 +08:00
import { _decorator, resources } from "cc";
import { CSVManager } from "./csvManager";
import { ResourceUtil } from "./resourceUtil";
const { ccclass, property } = _decorator;
@ccclass("LocalConfig")
export class LocalConfig {
/* class member could be defined like this */
private static _instance: LocalConfig;
private _csvManager: CSVManager = new CSVManager();
static get instance () {
if (this._instance) {
return this._instance;
}
this._instance = new LocalConfig();
return this._instance;
}
private _callback: Function = new Function();
private _currentLoad: number = 0;
private _cntLoad: number = 0;
/**
*
* @param {Function}cb
*/
public loadConfig (cb: Function) {
this._callback = cb;
this._loadCSV();
}
private _loadCSV () {
//新增数据表 请往该数组中添加....
resources.loadDir("datas", (err: any, assets)=>{
if (err) {
return;
}
let arrCsvFiles = assets.filter((item: any)=>{
return item._native !== ".md";
})
this._cntLoad = arrCsvFiles.length;
//客户端加载
if (arrCsvFiles.length) {
arrCsvFiles.forEach((item, index, array)=> {
ResourceUtil.getTextData(item.name, (err: any, content: any) => {
this._csvManager.addTable(item.name, content);
this._tryToCallbackOnFinished();
});
});
} else {
this._tryToCallbackOnFinished();
}
})
}
/**
*
* @param {string} tableName
* @param {string} key
* @param {any} value
* @returns {Object}
*/
queryOne (tableName: string, key: string, value: any) {
return this._csvManager.queryOne(tableName, key, value);
}
/**
* ID查询一条表内容
* @param {string}tableName
* @param {string}ID
* @returns {Object}
*/
queryByID (tableName: string, ID: string) {
return this._csvManager.queryByID(tableName, ID);
}
/**
*
* @param {string} tableName
* @returns {object}
*/
getTable (tableName: string) {
return this._csvManager.getTable(tableName);
}
/**
*
* @param {string} tableName
* @returns {object}
*/
getTableArr (tableName: string) {
return this._csvManager.getTableArr(tableName);
}
/**
* key和value对应的所有行内容
* @param {string} tableName
* @param {string} key
* @param {any} value
* @returns {Object}
*/
queryAll (tableName: string, key: string, value: any) {
return this._csvManager.queryAll(tableName, key, value);
}
//
/**
* key values Objectkey ID
* @param {string} tableName
* @param {string} key
* @param {Array}values
* @returns
*/
queryIn (tableName: string, key: string, values: any[]) {
return this._csvManager.queryIn(tableName, key, values);
}
/**
* condition key keyvalue objectkey IDvalue为具体数据
* @param {string} tableName
* @param {any} condition
* @returns
*/
queryByCondition (tableName: string, condition: any) {
return this._csvManager.queryByCondition(tableName, condition);
}
private _tryToCallbackOnFinished () {
if (this._callback) {
this._currentLoad++;
if (this._currentLoad >= this._cntLoad) {
this._callback();
}
}
}
}