localConfig.ts 4.1 KB

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