No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

91 líneas
4.6KB

  1. import * as fs from 'fs';
  2. import * as path from 'path';
  3. export class MiqgamePlugin implements plugins.Command {
  4. constructor() {
  5. }
  6. async onFile(file: plugins.File) {
  7. if (file.extname == '.js') {
  8. const filename = file.origin;
  9. if (filename == "libs/modules/promise/promise.js" || filename == 'libs/modules/promise/promise.min.js') {
  10. return null;
  11. }
  12. if (filename == 'libs/modules/egret/egret.js' || filename == 'libs/modules/egret/egret.min.js') {
  13. let content = file.contents.toString();
  14. content += `;window.egret = egret;`;
  15. content = content.replace(/definition = __global/, "definition = window");
  16. file.contents = new Buffer(content);
  17. }
  18. else {
  19. let content = file.contents.toString();
  20. if (
  21. filename == "libs/modules/res/res.js" ||
  22. filename == 'libs/modules/res/res.min.js' ||
  23. filename == 'libs/modules/assetsmanager/assetsmanager.min.js' ||
  24. filename == 'libs/modules/assetsmanager/assetsmanager.js'
  25. ) {
  26. content += ";window.RES = RES;"
  27. }
  28. if (filename == "libs/modules/eui/eui.js" || filename == 'libs/modules/eui/eui.min.js') {
  29. content += ";window.eui = eui;"
  30. if(filename == "libs/modules/eui/eui.js"){
  31. content = content.replace("function getRepeatedIds","window.getRepeatedIds=function getRepeatedIds");
  32. content = content.replace("function getIds","window.getIds=function getIds");
  33. content = content.replace("function toXMLString","window.toXMLString=function toXMLString");
  34. content = content.replace("function checkDeclarations","window.checkDeclarations=function checkDeclarations");
  35. content = content.replace("function getPropertyStr","window.getPropertyStr=function getPropertyStr");
  36. }
  37. }
  38. if (filename == 'libs/modules/dragonBones/dragonBones.js' || filename == 'libs/modules/dragonBones/dragonBones.min.js') {
  39. content += ';window.dragonBones = dragonBones';
  40. }
  41. content = "var egret = window.egret;" + content;
  42. if (filename == 'main.js') {
  43. content += "\n;window.Main = Main;"
  44. }
  45. file.contents = new Buffer(content);
  46. }
  47. }
  48. return file;
  49. }
  50. async onFinish(pluginContext: plugins.CommandContext) {
  51. //同步 index.html 配置到 game.js
  52. const gameJSPath = path.join(pluginContext.outputDir, "main.js");
  53. if(!fs.existsSync(gameJSPath)) {
  54. console.log(`${gameJSPath}不存在,请先使用 Launcher 发布小米快游戏`);
  55. return;
  56. }
  57. let gameJSContent = fs.readFileSync(gameJSPath, { encoding: "utf8" });
  58. const projectConfig = pluginContext.buildConfig.projectConfig;
  59. const optionStr =
  60. `entryClassName: ${projectConfig.entryClassName},\n\t\t` +
  61. `orientation: ${projectConfig.orientation},\n\t\t` +
  62. `frameRate: ${projectConfig.frameRate},\n\t\t` +
  63. `scaleMode: ${projectConfig.scaleMode},\n\t\t` +
  64. `contentWidth: ${projectConfig.contentWidth},\n\t\t` +
  65. `contentHeight: ${projectConfig.contentHeight},\n\t\t` +
  66. `showFPS: ${projectConfig.showFPS},\n\t\t` +
  67. `fpsStyles: ${projectConfig.fpsStyles},\n\t\t` +
  68. `showLog: ${projectConfig.showLog},\n\t\t` +
  69. `maxTouches: ${projectConfig.maxTouches},`;
  70. const reg = /\/\/----auto option start----[\s\S]*\/\/----auto option end----/;
  71. const replaceStr = '\/\/----auto option start----\n\t\t' + optionStr + '\n\t\t\/\/----auto option end----';
  72. gameJSContent = gameJSContent.replace(reg, replaceStr);
  73. fs.writeFileSync(gameJSPath, gameJSContent);
  74. //修改横竖屏
  75. let orientation;
  76. if (projectConfig.orientation == '"landscape"') {
  77. orientation = "landscape";
  78. }
  79. else {
  80. orientation = "portrait";
  81. }
  82. const gameJSONPath = path.join(pluginContext.outputDir, "manifest.json");
  83. let gameJSONContent = JSON.parse(fs.readFileSync(gameJSONPath, { encoding: "utf8" }));
  84. gameJSONContent.orientation = orientation;
  85. fs.writeFileSync(gameJSONPath, JSON.stringify(gameJSONContent, null, "\t"));
  86. }
  87. }