storageManager.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. import { _decorator, sys, log } from "cc";
  2. import { util } from './util';
  3. const { ccclass, property } = _decorator;
  4. @ccclass("StorageManager")
  5. export class StorageManager {
  6. private static _instance: StorageManager;
  7. public static get instance() {
  8. if (this._instance) {
  9. return this._instance;
  10. }
  11. this._instance = new StorageManager();
  12. this._instance.start();
  13. return this._instance;
  14. }
  15. private _jsonData: { [key: string]: any } = {};
  16. private _path: any = null;
  17. private KEY_CONFIG: string = 'template';
  18. private _markSave: boolean = false;
  19. private _saveTimer: number = -1;
  20. start() {
  21. this._jsonData = {
  22. "userId": "",
  23. };
  24. this._path = this._getConfigPath();
  25. var content;
  26. if (sys.isNative) {
  27. var valueObject = jsb.fileUtils.getValueMapFromFile(this._path);
  28. content = valueObject[this.KEY_CONFIG];
  29. } else {
  30. content = sys.localStorage.getItem(this.KEY_CONFIG);
  31. }
  32. // // 解密代码
  33. // if (cc.game.config["encript"]) {
  34. // var newContent = new Xxtea("upgradeHeroAbility").xxteaDecrypt(content);
  35. // if (newContent && newContent.length > 0) {
  36. // content = newContent;
  37. // }
  38. // }
  39. if (content && content.length) {
  40. if (content.startsWith('@')) {
  41. content = content.substring(1);
  42. content = util.decrypt(content);
  43. }
  44. try {
  45. //初始化操作
  46. var jsonData = JSON.parse(content);
  47. this._jsonData = jsonData;
  48. } catch (excepaiton) {
  49. }
  50. }
  51. //启动无限定时器,每1秒保存一次数据,而不是无限保存数据
  52. // this._saveTimer = setInterval(() =>{
  53. // this.scheduleSave();
  54. // }, 500);
  55. //每隔5秒保存一次数据,主要是为了保存最新在线时间,方便离线奖励时间判定
  56. this._saveTimer = setInterval(() => {
  57. this.scheduleSave();
  58. }, 5000);
  59. }
  60. /**
  61. * 存储配置文件,不保存到本地
  62. * @param {string}key 关键字
  63. * @param {any}value 存储值
  64. */
  65. setConfigDataWithoutSave(key: string, value: any) {
  66. let account: string = this._jsonData.userId;
  67. if (this._jsonData[account]) {
  68. this._jsonData[account][key] = value;
  69. } else {
  70. console.error("no account can not save");
  71. }
  72. }
  73. /**
  74. * 存储配置文件,保存到本地
  75. * @param {string}key 关键字
  76. * @param {any}value 存储值
  77. */
  78. setConfigData (key: string, value: any) {
  79. this.setConfigDataWithoutSave(key, value);
  80. this._markSave = true; //标记为需要存储,避免一直在写入,而是每隔一段时间进行写入
  81. }
  82. /**
  83. * 根据关键字获取数值
  84. * @param {string} key 关键字
  85. * @returns
  86. */
  87. getConfigData(key: string) {
  88. let account: string = this._jsonData.userId;
  89. if (this._jsonData[account]) {
  90. var value = this._jsonData[account][key];
  91. return value ? value : "";
  92. } else {
  93. log("no account can not load");
  94. return "";
  95. }
  96. }
  97. /**
  98. * 设置全局数据
  99. * @param {string} key 关键字
  100. * @param {any}value 存储值
  101. * @returns
  102. */
  103. public setGlobalData(key: string, value: any) {
  104. this._jsonData[key] = value;
  105. this.save();
  106. }
  107. /**
  108. * 获取全局数据
  109. * @param {string} key 关键字
  110. * @returns
  111. */
  112. public getGlobalData(key: string) {
  113. return this._jsonData[key];
  114. }
  115. /**
  116. * 设置用户唯一标示符
  117. * @param {string} userId 用户唯一标示符
  118. * @param {any}value 存储值
  119. * @returns
  120. */
  121. public setUserId(userId: string) {
  122. this._jsonData.userId = userId;
  123. if (!this._jsonData[userId]) {
  124. this._jsonData[userId] = {};
  125. }
  126. this.save();
  127. }
  128. /**
  129. * 获取用户唯一标示符
  130. * @returns {string}
  131. */
  132. public getUserId() {
  133. return this._jsonData.userId;
  134. }
  135. /**
  136. * 定时存储
  137. * @returns
  138. */
  139. public scheduleSave() {
  140. if (!this._markSave) {
  141. return;
  142. }
  143. this.save();
  144. }
  145. /**
  146. * 标记为已修改
  147. */
  148. public markModified() {
  149. this._markSave = true;
  150. }
  151. /**
  152. * 保存配置文件
  153. * @returns
  154. */
  155. public save() {
  156. // 写入文件
  157. var str = JSON.stringify(this._jsonData);
  158. // // 加密代码
  159. // if (cc.game.config["encript"]) {
  160. // str = new Xxtea("upgradeHeroAbility").xxteaEncrypt(str);
  161. // }
  162. let zipStr = '@' + util.encrypt(str);
  163. // let zipStr = str;
  164. this._markSave = false;
  165. if (!sys.isNative) {
  166. var ls = sys.localStorage;
  167. ls.setItem(this.KEY_CONFIG, zipStr);
  168. return;
  169. }
  170. var valueObj: any = {};
  171. valueObj[this.KEY_CONFIG] = zipStr;
  172. jsb.fileUtils.writeToFile(valueObj, this._getConfigPath());
  173. // jsb.fileUtils.writeToFile(valueObj);
  174. }
  175. /**
  176. * 获取配置文件路径
  177. * @returns 获取配置文件路径
  178. */
  179. private _getConfigPath() {
  180. let platform: any = sys.platform;
  181. let path: string = "";
  182. if (platform === sys.OS.WINDOWS) {
  183. path = "src/conf";
  184. } else if (platform === sys.OS.LINUX) {
  185. path = "./conf";
  186. } else {
  187. if (sys.isNative) {
  188. path = jsb.fileUtils.getWritablePath();
  189. path = path + "conf";
  190. } else {
  191. path = "src/conf";
  192. }
  193. }
  194. return path;
  195. }
  196. }