Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

119 lines
5.4KB

  1. import * as fs from 'fs';
  2. import * as path from 'path';
  3. export class QQgamePlugin implements plugins.Command {
  4. private useQQPlugin: boolean = false;
  5. private pliginList: string[] = [];//qq engine plugin
  6. constructor(useQQPlugin: boolean, pliginList: string[]) {
  7. this.useQQPlugin = useQQPlugin
  8. this.pliginList = pliginList
  9. }
  10. async onFile(file: plugins.File) {
  11. if (file.extname == '.js') {
  12. const filename = file.origin;
  13. if (filename == "libs/modules/promise/promise.js" || filename == 'libs/modules/promise/promise.min.js') {
  14. return null;
  15. }
  16. if (this.useQQPlugin) {
  17. const basename = file.basename
  18. //QQ 小游戏引擎插件,支持下列官方库
  19. let engineJS = ['assetsmanager', 'dragonBones', 'egret', 'game', 'eui', 'socket', 'tween']
  20. for (let i in engineJS) {
  21. let jsName = engineJS[i]
  22. if (basename == jsName + ".js" || basename == jsName + ".min.js") {
  23. this.pliginList.push(`requirePlugin("egret-library/${jsName}.min.js")`);
  24. return null
  25. }
  26. }
  27. }
  28. if (filename == 'libs/modules/egret/egret.js' || filename == 'libs/modules/egret/egret.min.js') {
  29. let content = file.contents.toString();
  30. content += `;window.egret = egret;`;
  31. content = content.replace(/definition = __global/, "definition = window");
  32. file.contents = new Buffer(content);
  33. }
  34. else {
  35. let content = file.contents.toString();
  36. if (
  37. filename == "libs/modules/res/res.js" ||
  38. filename == 'libs/modules/res/res.min.js' ||
  39. filename == 'libs/modules/assetsmanager/assetsmanager.min.js' ||
  40. filename == 'libs/modules/assetsmanager/assetsmanager.js'
  41. ) {
  42. content += ";window.RES = RES;"
  43. }
  44. if (filename == "libs/modules/eui/eui.js" || filename == 'libs/modules/eui/eui.min.js') {
  45. content += ";window.eui = eui;"
  46. }
  47. if (filename == 'libs/modules/dragonBones/dragonBones.js' || filename == 'libs/modules/dragonBones/dragonBones.min.js') {
  48. content += ';window.dragonBones = dragonBones';
  49. }
  50. content = "var egret = window.egret;" + content;
  51. if (filename == 'main.js') {
  52. content += "\n;window.Main = Main;"
  53. }
  54. file.contents = new Buffer(content);
  55. }
  56. }
  57. return file;
  58. }
  59. async onFinish(pluginContext: plugins.CommandContext) {
  60. let { projectRoot, outputDir, buildConfig } = pluginContext
  61. //同步 index.html 配置到 game.js
  62. const gameJSPath = path.join(outputDir, "game.js");
  63. if (!fs.existsSync(gameJSPath)) {
  64. console.log(`${gameJSPath}不存在,请先使用 Launcher 发布QQ小游戏`);
  65. return;
  66. }
  67. let gameJSContent = fs.readFileSync(gameJSPath, { encoding: "utf8" });
  68. const projectConfig = buildConfig.projectConfig;
  69. const optionStr =
  70. `entryClassName: ${projectConfig.entryClassName},\n\t\t` +
  71. `orientation: ${projectConfig.orientation},\n\t\t` +
  72. `frameRate: ${projectConfig.frameRate},\n\t\t` +
  73. `scaleMode: ${projectConfig.scaleMode},\n\t\t` +
  74. `contentWidth: ${projectConfig.contentWidth},\n\t\t` +
  75. `contentHeight: ${projectConfig.contentHeight},\n\t\t` +
  76. `showFPS: ${projectConfig.showFPS},\n\t\t` +
  77. `fpsStyles: ${projectConfig.fpsStyles},\n\t\t` +
  78. `showLog: ${projectConfig.showLog},\n\t\t` +
  79. `maxTouches: ${projectConfig.maxTouches},`;
  80. const reg = /\/\/----auto option start----[\s\S]*\/\/----auto option end----/;
  81. const replaceStr = '\/\/----auto option start----\n\t\t' + optionStr + '\n\t\t\/\/----auto option end----';
  82. gameJSContent = gameJSContent.replace(reg, replaceStr);
  83. fs.writeFileSync(gameJSPath, gameJSContent);
  84. //修改横竖屏
  85. let orientation;
  86. if (projectConfig.orientation == '"landscape"') {
  87. orientation = "landscape";
  88. }
  89. else {
  90. orientation = "portrait";
  91. }
  92. const gameJSONPath = path.join(outputDir, "game.json");
  93. let gameJSONContent = this.readData(gameJSONPath);
  94. gameJSONContent.deviceOrientation = orientation;
  95. if (!gameJSONContent.plugins) {
  96. gameJSONContent.plugins = {}
  97. }
  98. if (!this.useQQPlugin) {
  99. delete gameJSONContent.plugins["egret-library"]
  100. } else {
  101. let engineVersion = this.readData(path.join(projectRoot, "egretProperties.json")).engineVersion;
  102. gameJSONContent.plugins["egret-library"] = {
  103. "provider": "1110108620",
  104. "version": engineVersion
  105. }
  106. }
  107. this.writeData(gameJSONContent, gameJSONPath)
  108. }
  109. readData(filePath: string): any {
  110. return JSON.parse(fs.readFileSync(filePath, { encoding: "utf8" }));
  111. }
  112. writeData(data: object, filePath: string) {
  113. fs.writeFileSync(filePath, JSON.stringify(data, null, "\t"));
  114. }
  115. }