CfgUtil.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. const Fs = require('fire-fs');
  2. const FsExtra = require('fs-extra');
  3. const Path = require('fire-path');
  4. const OutPut = Editor.require('packages://hot-update-tools/core/OutPut.js');
  5. module.exports = {
  6. cfgData: {
  7. serverRootDir: '',
  8. resourceRootDir: '',
  9. genManifestDir: '',
  10. localServerPath: '',
  11. buildTime: null, // 构建时间,全部保存int值
  12. genTime: null, // manifest生成时间
  13. genVersion: null, // manifest版本
  14. },
  15. _save() {
  16. FsExtra.ensureFileSync(this.cfgFile);
  17. Fs.writeFileSync(this.cfgFile, JSON.stringify(this.cfgData, null, 4));
  18. },
  19. get cfgFile() {
  20. return Path.join(OutPut.rootDir, 'cfg.json');
  21. },
  22. initCfg(errorCB) {
  23. OutPut.initialization();
  24. if (Fs.existsSync(this.cfgFile)) {
  25. const data = this._getConfigData();
  26. if (data) {
  27. this.cfgData = data;
  28. } else {
  29. const msg = `配置文件格式异常,请检查配置文件是否为json格式:${this.cfgFile}`;
  30. errorCB && errorCB(msg);
  31. }
  32. } else {
  33. this._save();
  34. }
  35. return this.cfgData;
  36. },
  37. _getConfigData() {
  38. try {
  39. return JSON.parse(Fs.readFileSync(this.cfgFile, 'utf-8'));
  40. } catch (e) {
  41. return null;
  42. }
  43. },
  44. updateBuildTimeByMain(time) {
  45. // 在main.js中调用electron中没有remote属性
  46. // Editor.log(electron.app.getPath('userData'));
  47. let cfgPath = this.cfgFile;
  48. if (Fs.existsSync(cfgPath)) {
  49. let json = this._getConfigData();
  50. if (json) {
  51. json.buildTime = time;
  52. json.genTime = time;
  53. Fs.writeFileSync(cfgPath, JSON.stringify(json, null, 4));
  54. }
  55. } else {
  56. Editor.log('热更新配置文件不存在: ' + cfgPath);
  57. }
  58. },
  59. updateBuildTime(time) {
  60. this.cfgData.buildTime = time;
  61. this.cfgData.genTime = time;
  62. this._save();
  63. },
  64. updateGenTime(time) {
  65. this.cfgData.genTime = time;
  66. // this.cfgData.genVersion = version;
  67. this._save();
  68. },
  69. // 获取构建时间生成时间
  70. getBuildTimeGenTime() {
  71. let ret = { buildTime: null, genTime: null };
  72. let cfgPath = this.cfgFile;
  73. if (Fs.existsSync(cfgPath)) {
  74. let data = Fs.readFileSync(cfgPath, 'utf-8');
  75. let json = JSON.parse(data);
  76. ret.buildTime = json.buildTime;
  77. ret.genTime = json.genTime;
  78. this.cfgData.buildTime = json.buildTime;
  79. this.cfgData.genTime = json.genTime;
  80. }
  81. return ret;
  82. },
  83. saveConfig(data) {
  84. this.cfgData.serverRootDir = data.serverRootDir;
  85. this.cfgData.resourceRootDir = data.resourceRootDir;
  86. this.cfgData.localServerPath = data.localServerPath;
  87. this.cfgData.subPackArr = data.subPackArr;
  88. this.cfgData.gameName = data.gameName;
  89. this.cfgData.whiteList = data.whiteList;
  90. this._save();
  91. },
  92. };