env-project.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. const Path = require('path');
  2. const Fs = require('fs');
  3. const FsExtra = require('fs-extra');
  4. const OutPut = Editor.require('packages://hot-update-tools/core/OutPut.js');
  5. const { shell } = require('electron');
  6. Vue.component('env-project', {
  7. template: Fs.readFileSync(Editor.url('packages://hot-update-tools/panel/env-project.html'), 'utf-8'),
  8. mixins: [Editor.require('packages://hot-update-tools/panel/mixin.js')],
  9. data () {
  10. return {
  11. localGameVersion: '-', // 游戏版本号
  12. localGamePackageUrl: '',
  13. localGameProjectManifest: '',
  14. localGameVersionManifest: '',
  15. };
  16. },
  17. created () {
  18. this.initLocalGameVersion();
  19. },
  20. methods: {
  21. // 导入生成的manifest到游戏项目中
  22. importManifestToGame () {
  23. let manifest = {
  24. project: 'project.manifest',
  25. version: 'version.manifest',
  26. };
  27. let projectFile = Path.join(OutPut.manifestDir, manifest.project);
  28. if (!Fs.existsSync(projectFile)) {
  29. this.log(`[${manifest.project}]文件不存在: ${projectFile}`);
  30. return;
  31. }
  32. let versionFile = Path.join(OutPut.manifestDir, manifest.version);
  33. if (!Fs.existsSync(versionFile)) {
  34. this.log(`[${manifest.version}]文件不存在: ${versionFile}`);
  35. return;
  36. }
  37. let projectManifestDir = null;
  38. if (!Fs.existsSync(this.localGameProjectManifest)) {
  39. // 默认使用根目录
  40. projectManifestDir = Editor.url('db://assets');
  41. } else {
  42. projectManifestDir = Path.dirname(this.localGameProjectManifest);
  43. }
  44. for (let key in manifest) {
  45. let source = Path.join(OutPut.manifestDir, manifest[key]);
  46. let dest = Path.join(projectManifestDir, manifest[key]);
  47. FsExtra.copyFileSync(source, dest);
  48. let url = Editor.assetdb.remote.fspathToUrl(dest);
  49. Editor.assetdb.refresh(url);
  50. }
  51. this.initLocalGameVersion();
  52. },
  53. onClickLocalPackageUrl () {
  54. let url = `${this.localGamePackageUrl}/version.manifest`;
  55. shell && shell.openExternal(url);
  56. },
  57. initLocalGameVersion () {
  58. const Globby = require('globby');
  59. let root = Editor.url('db://assets');
  60. let manifestUrl = Path.join(root, '**/*.manifest');
  61. manifestUrl = Path.normalize(manifestUrl).replace(/\\/g, '/');
  62. let results = Globby.sync([manifestUrl]);
  63. let versionCfg = '';
  64. let projectCfg = '';
  65. results.forEach((result) => {
  66. if (Path.basename(result) === 'version.manifest') {
  67. versionCfg = result;
  68. } else if (Path.basename(result) === 'project.manifest') {
  69. projectCfg = result;
  70. }
  71. });
  72. if (versionCfg.length === 0) {
  73. this.log('项目中没有配置文件: version.manifest');
  74. return;
  75. }
  76. if (projectCfg.length === 0) {
  77. this.log('项目中没有配置文件: project.manifest');
  78. return;
  79. }
  80. this.localGameVersionManifest = versionCfg;
  81. this.localGameProjectManifest = projectCfg;
  82. let versionData = null;
  83. let projectData = null;
  84. if (Fs.existsSync(versionCfg)) {
  85. try {
  86. versionData = JSON.parse(Fs.readFileSync(versionCfg, 'utf-8'));
  87. } catch (e) {
  88. this.log(`配置数据异常,不是json格式:${versionCfg}`);
  89. return;
  90. }
  91. } else {
  92. this.log('读取项目中的配置文件失败: ' + versionCfg);
  93. }
  94. if (Fs.existsSync(projectCfg)) {
  95. try {
  96. projectData = JSON.parse(Fs.readFileSync(projectCfg, 'utf-8'));
  97. } catch (e) {
  98. this.log(`配置数据异常,不是json格式:${projectCfg}`);
  99. return;
  100. }
  101. } else {
  102. this.log('读取项目中的配置文件失败: ' + projectCfg);
  103. }
  104. if (projectData && versionData &&
  105. projectData.version && versionData.version &&
  106. projectData.version === versionData.version) {
  107. this.localGameVersion = projectData.version;
  108. this.localGamePackageUrl = projectData.packageUrl;
  109. } else {
  110. this.log('游戏中的 project.manifest 和 version.manifest 中的数据异常,请检查配置文件');
  111. }
  112. },
  113. },
  114. });