localConfig.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import {Asset, AssetManager, TextAsset, _decorator, assetManager } from "cc";
  2. import { csvManager } from "./csvManager";
  3. const { ccclass, property } = _decorator;
  4. @ccclass("localConfig")
  5. export class localConfig {
  6. /* class member could be defined like this */
  7. private static _instance: localConfig;
  8. private _csvManager: csvManager = new csvManager();
  9. static get instance () {
  10. if (!this._instance) {
  11. this._instance = new localConfig();
  12. }
  13. return this._instance;
  14. }
  15. private _callback: Function = new Function();
  16. private _currentLoad: number = 0;
  17. private _cntLoad: number = 0;
  18. /**
  19. * 加载配置文件
  20. * @param {Function}cb 回调函数
  21. */
  22. public loadConfig (cb: Function) {
  23. this._callback = cb;
  24. this._loadCSV();
  25. }
  26. private _loadCSV () {
  27. //新增数据表 请往该数组中添加....
  28. assetManager.loadBundle('csb', (err: Error | null, bundle: AssetManager.Bundle) => {
  29. if (err) {
  30. console.error('Failed to load bundle:', err);
  31. return;
  32. }
  33. //获取bundle内所有资源(新API用法)
  34. const assets = bundle.getDirWithPath('');
  35. //过滤掉.md文件
  36. const arrCsvFiles = assets.filter((item: any) => {
  37. return !item.path.endsWith('.md');
  38. });
  39. this._cntLoad = arrCsvFiles.length;
  40. if (arrCsvFiles.length === 0) {
  41. this._tryToCallbackOnFinished();
  42. return;
  43. }
  44. //加载每个CSV文件
  45. arrCsvFiles.forEach((item: any) => {
  46. bundle.load(item.path, (err: Error | null, content: TextAsset) => {
  47. if (err) {
  48. console.error(`Failed to load ${item.name}:`, err);
  49. return;
  50. }
  51. const text = content.text;
  52. this._csvManager.addTable(item.path, text);
  53. this._tryToCallbackOnFinished();
  54. });
  55. });
  56. });
  57. }
  58. /**
  59. * 查询一条表内容
  60. * @param {string} tableName 表名
  61. * @param {string} key 列名
  62. * @param {any} value 值
  63. * @returns {Object} 一条表内容
  64. */
  65. queryOne (tableName: string, key: string, value: any) {
  66. return this._csvManager.queryOne(tableName, key, value);
  67. }
  68. /**
  69. * 根据ID查询一条表内容
  70. * @param {string}tableName 表名
  71. * @param {string}ID
  72. * @returns {Object} 一条表内容
  73. */
  74. queryByID (tableName: string, ID: string) {
  75. return this._csvManager.queryByID(tableName, ID);
  76. }
  77. /**
  78. * 根据表名获取表的所有内容
  79. * @param {string} tableName 表名
  80. * @returns {object} 表内容
  81. */
  82. getTable (tableName: string) {
  83. return this._csvManager.getTable(tableName);
  84. }
  85. /**
  86. * 根据表名获取表的所有内容
  87. * @param {string} tableName 表名
  88. * @returns {object} 表内容
  89. */
  90. getTableArr (tableName: string) {
  91. return this._csvManager.getTableArr(tableName);
  92. }
  93. /**
  94. * 查询key和value对应的所有行内容
  95. * @param {string} tableName 表名
  96. * @param {string} key 列名
  97. * @param {any} value 值
  98. * @returns {Object}
  99. */
  100. queryAll (tableName: string, key: string, value: any) {
  101. return this._csvManager.queryAll(tableName, key, value);
  102. }
  103. //
  104. /**
  105. * 选出指定表里所有 key 的值在 values 数组中的数据,返回 Object,key 为 ID
  106. * @param {string} tableName 表名
  107. * @param {string} key 列名
  108. * @param {Array}values 数值
  109. * @returns
  110. */
  111. queryIn (tableName: string, key: string, values: any[]) {
  112. return this._csvManager.queryIn(tableName, key, values);
  113. }
  114. /**
  115. * 选出符合条件的数据。condition key 为表格的key,value 为值的数组。返回的object,key 为数据在表格的ID,value为具体数据
  116. * @param {string} tableName 表名
  117. * @param {any} condition 筛选条件
  118. * @returns
  119. */
  120. queryByCondition (tableName: string, condition: any) {
  121. return this._csvManager.queryByCondition(tableName, condition);
  122. }
  123. private _tryToCallbackOnFinished () {
  124. if (this._callback) {
  125. this._currentLoad++;
  126. if (this._currentLoad >= this._cntLoad) {
  127. this._callback();
  128. }
  129. }
  130. }
  131. }