You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

127 line
6.5KB

  1. import * as fs from 'fs';
  2. import * as path from 'path';
  3. export class VivogamePlugin implements plugins.Command {
  4. jsFileList: any = []
  5. constructor() {
  6. }
  7. async onFile(file: plugins.File) {
  8. if (file.extname == '.js') {
  9. const filename = file.origin;
  10. if (filename == "libs/modules/promise/promise.js" || filename == 'libs/modules/promise/promise.min.js') {
  11. return null;
  12. }
  13. this.jsFileList.push(file.basename)
  14. if (filename == 'libs/modules/egret/egret.js' || filename == 'libs/modules/egret/egret.min.js') {
  15. let content = file.contents.toString();
  16. content += `;window.egret = egret;`;
  17. content = content.replace(/definition = __global/, "definition = window");
  18. file.contents = new Buffer(content);
  19. }
  20. else {
  21. let content = file.contents.toString();
  22. if (
  23. filename == "libs/modules/res/res.js" ||
  24. filename == 'libs/modules/res/res.min.js' ||
  25. filename == 'libs/modules/assetsmanager/assetsmanager.min.js' ||
  26. filename == 'libs/modules/assetsmanager/assetsmanager.js'
  27. ) {
  28. content += ";window.RES = RES;"
  29. }
  30. if (filename == "libs/modules/eui/eui.js" || filename == 'libs/modules/eui/eui.min.js') {
  31. content += ";window.eui = eui;"
  32. if (filename == "libs/modules/eui/eui.js") {
  33. content = content.replace("function getRepeatedIds", "window.getRepeatedIds=function getRepeatedIds");
  34. content = content.replace("function getIds", "window.getIds=function getIds");
  35. content = content.replace("function toXMLString", "window.toXMLString=function toXMLString");
  36. content = content.replace("function checkDeclarations", "window.checkDeclarations=function checkDeclarations");
  37. content = content.replace("function getPropertyStr", "window.getPropertyStr=function getPropertyStr");
  38. }
  39. }
  40. if (filename == 'libs/modules/dragonBones/dragonBones.js' || filename == 'libs/modules/dragonBones/dragonBones.min.js') {
  41. content += ';window.dragonBones = dragonBones';
  42. }
  43. content = "var egret = window.egret;" + content;
  44. if (filename == 'main.js') {
  45. content += "\n;window.Main = Main;"
  46. }
  47. file.contents = new Buffer(content);
  48. }
  49. }
  50. return file;
  51. }
  52. async onFinish(pluginContext: plugins.CommandContext) {
  53. //同步 index.html 配置到 game.js
  54. const gameJSPath = path.join(pluginContext.outputDir, "game.js");
  55. if (!fs.existsSync(gameJSPath)) {
  56. console.log(`${gameJSPath}不存在,请先使用 Launcher 发布 Vivo 小游戏`);
  57. return;
  58. }
  59. let gameJSContent = fs.readFileSync(gameJSPath, { encoding: "utf8" });
  60. const projectConfig = pluginContext.buildConfig.projectConfig;
  61. const optionStr =
  62. `entryClassName: ${projectConfig.entryClassName},\n\t\t` +
  63. `orientation: ${projectConfig.orientation},\n\t\t` +
  64. `frameRate: ${projectConfig.frameRate},\n\t\t` +
  65. `scaleMode: ${projectConfig.scaleMode},\n\t\t` +
  66. `contentWidth: ${projectConfig.contentWidth},\n\t\t` +
  67. `contentHeight: ${projectConfig.contentHeight},\n\t\t` +
  68. `showFPS: ${projectConfig.showFPS},\n\t\t` +
  69. `fpsStyles: ${projectConfig.fpsStyles},\n\t\t` +
  70. `showLog: ${projectConfig.showLog},\n\t\t` +
  71. `maxTouches: ${projectConfig.maxTouches},`;
  72. const reg = /\/\/----auto option start----[\s\S]*\/\/----auto option end----/;
  73. const replaceStr = '\/\/----auto option start----\n\t\t' + optionStr + '\n\t\t\/\/----auto option end----';
  74. gameJSContent = gameJSContent.replace(reg, replaceStr);
  75. fs.writeFileSync(gameJSPath, gameJSContent);
  76. //修改横竖屏
  77. let orientation;
  78. if (projectConfig.orientation == '"landscape"') {
  79. orientation = "landscape";
  80. }
  81. else {
  82. orientation = "portrait";
  83. }
  84. const gameJSONPath = path.join(pluginContext.outputDir, "manifest.json");
  85. let gameJSONContent = JSON.parse(fs.readFileSync(gameJSONPath, { encoding: "utf8" }));
  86. gameJSONContent.deviceOrientation = orientation;
  87. let engineVersion = this.readData(path.join(pluginContext.projectRoot, "egretProperties.json")).engineVersion
  88. if(!gameJSONContent.thirdEngine)gameJSONContent.thirdEngine={}
  89. gameJSONContent.thirdEngine.egret = engineVersion
  90. fs.writeFileSync(gameJSONPath, JSON.stringify(gameJSONContent, null, "\t"));
  91. let isPublish = pluginContext.buildConfig.command == "publish" ? true : false;
  92. let configArr: any[] = []
  93. for (var i = 0, len = this.jsFileList.length; i < len; i++) {
  94. let jsFile = this.jsFileList[i];
  95. if (isPublish) {
  96. if (jsFile == "main.js") {
  97. jsFile = 'main.min.js'
  98. } else if (jsFile == "default.thm.js") {
  99. jsFile = "default.thm.min.js"
  100. }
  101. }
  102. configArr.push(JSON.stringify({
  103. module_name: `./js/${jsFile}`,
  104. module_path: `./js/${jsFile}`,
  105. module_from: `engine/js/${jsFile}`,
  106. }, null, "\t"))
  107. }
  108. const replaceConfigStr = '\/\/----auto option start----\n\t\t' + configArr.toString() + '\n\t\t\/\/----auto option end----';
  109. const minigameConfigPath = path.join(pluginContext.outputDir,"../", "minigame.config.js");
  110. if(!fs.existsSync(minigameConfigPath)){
  111. //5.2.28版本,vivo更新了项目结构,老项目需要升级
  112. fs.writeFileSync(path.join(pluginContext.outputDir,"../","vivo更新了项目结构,请重新创建vivo小游戏项目.js"), "vivo更新了项目结构,请重新创建vivo小游戏项目");
  113. }else{
  114. let configJSContent = fs.readFileSync(minigameConfigPath, { encoding: "utf8" });
  115. configJSContent = configJSContent.replace(reg, replaceConfigStr);
  116. fs.writeFileSync(minigameConfigPath, configJSContent);
  117. }
  118. }
  119. readData(filePath: string): any {
  120. return JSON.parse(fs.readFileSync(filePath, { encoding: "utf8" }));
  121. }
  122. }