import {Asset, AssetManager, TextAsset, _decorator, assetManager } from "cc"; import { csvManager } from "./csvManager"; 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) { 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 () { //新增数据表 请往该数组中添加.... assetManager.loadBundle('csb', (err: Error | null, bundle: AssetManager.Bundle) => { if (err) { console.error('Failed to load bundle:', err); return; } //获取bundle内所有资源(新API用法) const assets = bundle.getDirWithPath(''); //过滤掉.md文件 const arrCsvFiles = assets.filter((item: any) => { return !item.path.endsWith('.md'); }); this._cntLoad = arrCsvFiles.length; if (arrCsvFiles.length === 0) { this._tryToCallbackOnFinished(); return; } //加载每个CSV文件 arrCsvFiles.forEach((item: any) => { bundle.load(item.path, (err: Error | null, content: TextAsset) => { if (err) { console.error(`Failed to load ${item.name}:`, err); return; } const text = content.text; this._csvManager.addTable(item.path, text); 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 数组中的数据,返回 Object,key 为 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 为表格的key,value 为值的数组。返回的object,key 为数据在表格的ID,value为具体数据 * @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(); } } } }