From 70ca41b66e9cb389357ebbf27f6dde09c266b55b Mon Sep 17 00:00:00 2001 From: msojocs Date: Fri, 4 Feb 2022 19:34:10 +0800 Subject: [PATCH] =?UTF-8?q?#9=20fix:=20=E4=BA=91=E5=BC=80=E5=8F=91?= =?UTF-8?q?=E6=8E=A7=E5=88=B6=E5=8F=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- {old => compiler}/wine/wcc | 0 {old => compiler}/wine/wcsc | 0 docs/Console.MD | 46 ++++++++++++++++++++++++++++ test/cloudconsole | 5 ++++ tools/fix-cloudconsole | 29 ++++++++++++++++++ tools/update-wechat-devtools-node | 42 ++++++++++++++++---------- {test => tools/wxvpkg}/pack | 10 +++++-- tools/wxvpkg/unpack | 50 +++++++++++++++++++++++++++++++ 8 files changed, 165 insertions(+), 17 deletions(-) rename {old => compiler}/wine/wcc (100%) rename {old => compiler}/wine/wcsc (100%) create mode 100644 docs/Console.MD create mode 100644 test/cloudconsole create mode 100644 tools/fix-cloudconsole rename {test => tools/wxvpkg}/pack (91%) create mode 100644 tools/wxvpkg/unpack diff --git a/old/wine/wcc b/compiler/wine/wcc similarity index 100% rename from old/wine/wcc rename to compiler/wine/wcc diff --git a/old/wine/wcsc b/compiler/wine/wcsc similarity index 100% rename from old/wine/wcsc rename to compiler/wine/wcsc diff --git a/docs/Console.MD b/docs/Console.MD new file mode 100644 index 0000000..b1a7b3a --- /dev/null +++ b/docs/Console.MD @@ -0,0 +1,46 @@ +# 修复创建云开发控制台窗口 +# 分析 +1. 打开云开发控制台,提示`Uncaught TypeError:Cannot read property 'isDev' of undefined` +2. 定位错误出发位置`global.appConfig.isDev` +3. 打开云开发控制台与主界面的调试器,对比`global`对象,发现不一致,云开发控制台缺失大量属性; +4. 结合NW.js新特性([文档链接](https://nwjs.readthedocs.io/en/latest/References/Window/#windowopenurl-options-callback)) +5. 云开发控制台应与主界面共享变量,但在新特性后,二者隔离了;因此,修复方法就是让它们共享关键变量,可通过open方法的回调实现(经尝试设定`new_instance`与`mixed_context`无效) + +# 修复 +## 定位Window.open位置 +可通过断点调试实现 + +文件路径: `package.nw/core.wxvpkg.ext/284af385b4ef6206861fea66a2452277.js` +定位字符串:`nw.Window.open` +在回调函数中添加: +``` +Object.keys(window).forEach(key=>{ + if(!e.window[key]){ + /*没有就添加*/ + try{ + e.window[key] = window[key]; + }catch(e){ + /*部分方法不可修改,会抛异常*/ + console.error(e); + } + } +}) +``` +界面可显示,但一直停留在"等待开发者工具初始化"界面,主界面控制台显示`MESSAGE_CENTER connection: invalid token PLUGIN_cloudconsolev1#{token}# , closing` +经检查是token存储器被隔离了,于是可借助`window`对象作为中间人传递此数据对象 + +## 处理TOKEN数据 +修改token存储对象构造方法 +``` +constructor() { + if(window.tokenData){ + /*有就直接用*/ + this._sessionToken = window.tokenData._sessionToken + this._tokenMap = window.tokenData._tokenMap + }else{ + /*没有就新建*/ + (this._sessionToken = ""), (this._tokenMap = {}); + window.tokenData=this;/*新建完要给中间人*/ + } +} +``` \ No newline at end of file diff --git a/test/cloudconsole b/test/cloudconsole new file mode 100644 index 0000000..4bead3e --- /dev/null +++ b/test/cloudconsole @@ -0,0 +1,5 @@ +#!/bin/bash +# node test/pack +tools/fix-cloudconsole +rm -rf /home/msojocs/.config/wechat_devtools/WeappCache +bin/wechat-devtools \ No newline at end of file diff --git a/tools/fix-cloudconsole b/tools/fix-cloudconsole new file mode 100644 index 0000000..215ef77 --- /dev/null +++ b/tools/fix-cloudconsole @@ -0,0 +1,29 @@ +#!/bin/bash +echo "Fix Cloud Console" +root_dir=$(cd `dirname $0`/.. && pwd -P) + +package_dir="$root_dir/package.nw" +tmp_dir="$root_dir/tmp" + +# unpack 文件 到 路径 +node "$root_dir/tools/wxvpkg/unpack" "$package_dir/core.wxvpkg" "$tmp_dir/core.wxvpkg" + + +# find +open_find_result=$( grep -lr "this.props.onWindowOpenFail());" "$root_dir/tmp/core.wxvpkg" ) +token_find_result=$( grep -lr "constructor(){this._sessionToken=\"\",this._tokenMap={}}" "$root_dir/tmp/core.wxvpkg" ) +echo "云开发控制台启动点: $open_find_result" +echo "WebSocket token存储对象位置: $token_find_result" + + +# replace +new_cb_handle="this.props.onWindowOpenFail());Object.keys(window).forEach(key=>{if(!e.window[key]){try{e.window[key]=window[key];}catch(e){console.error(e);}}});" +sed -i "s/this.props.onWindowOpenFail());/$new_cb_handle/g" $open_find_result + +new_constructor="constructor(){if(window.tokenData){/*有就直接用*/this._sessionToken=window.tokenData._sessionToken;this._tokenMap=window.tokenData._tokenMap;}else{/*没有就新建*/this._sessionToken=\"\",this._tokenMap={};window.tokenData=this;/*新建完要给中间人*/}}" +sed -i "s#constructor(){this._sessionToken=\"\",this._tokenMap={}}#$new_constructor#g" "$token_find_result" + + +# pack 路径 到 文件 +node "$root_dir/tools/wxvpkg/pack" "$root_dir/tmp/core.wxvpkg" "$package_dir/core.wxvpkg" +rm -rf "$tmp_dir/core.wxvpkg" \ No newline at end of file diff --git a/tools/update-wechat-devtools-node b/tools/update-wechat-devtools-node index ef862fd..046e175 100644 --- a/tools/update-wechat-devtools-node +++ b/tools/update-wechat-devtools-node @@ -231,6 +231,17 @@ const patch_wechat_devtools_CLI = function () { }); }); }; +const patch_wechat_devtools_core = function () { + info("Patching wechat-devtools CLI supports"); + + return new Promise((resolve, reject) => { + spawn(path.resolve(__dirname, "fix-cloudconsole"), [], { + stdio: "inherit", + }).on("close", (code) => { + resolve(); + }); + }); +}; const rebuild_wechat_devtools_node_modules = function () { info("Rebuilding wechat-devtools node modules"); @@ -268,22 +279,22 @@ const patch_wechat_devtools = function () { }); }); }; -// const patch_wcc_wcsc = function () { -// info("Patching wcc and wcsc"); +const patch_wcc_wcsc = function () { + info("Patching wcc and wcsc"); -// return new Promise((resolve, reject) => { -// fs.copyFileSync( -// path.resolve(__dirname, "../compiler/wcc"), -// path.resolve(__dirname, "../package.nw/js/vendor/wcc.exe") -// ); -// fs.copyFileSync( -// path.resolve(__dirname, "../compiler/wcsc"), -// path.resolve(__dirname, "../package.nw/js/vendor/wcsc.exe") -// ); + return new Promise((resolve, reject) => { + fs.copyFileSync( + path.resolve(__dirname, "../compiler/wine/wcc"), + path.resolve(__dirname, "../package.nw/js/vendor/wcc") + ); + fs.copyFileSync( + path.resolve(__dirname, "../compiler/wine/wcsc"), + path.resolve(__dirname, "../package.nw/js/vendor/wcsc") + ); -// resolve(); -// }); -// }; + resolve(); + }); +}; const start = async () => { try { @@ -294,9 +305,10 @@ const start = async () => { await patch_wechat_devtools_package_name(); await patch_wechat_devtools_editor_selection_autocopy(); await patch_wechat_devtools_CLI(); + await patch_wechat_devtools_core(); await rebuild_wechat_devtools_node_modules(); await patch_wechat_devtools(); - // await patch_wcc_wcsc(); + await patch_wcc_wcsc(); console.log( `Succeeded upgrading wechat-devtools to version ${version}` ); diff --git a/test/pack b/tools/wxvpkg/pack similarity index 91% rename from test/pack rename to tools/wxvpkg/pack index 5b4e145..6918c52 100644 --- a/test/pack +++ b/tools/wxvpkg/pack @@ -1,16 +1,22 @@ +#!/usr/bin/env node + // https://gist.github.com/chemzqm/9f2334ca201dc2fbc363fdd757aa2ed4 const path = require('path') const fs = require('fs') const { execSync } = require('child_process') -let file = path.resolve(__dirname, '../package.nw/core.wxvpkg') +const args = process.argv.slice(2); +const from = args[0] +const to = args[1] + +let file = to console.log(file) if (fs.existsSync(file)) { execSync(`rm -rf ${file}`) } let fd = fs.openSync(file, 'w') -let dest = path.join(__dirname, '../package.nw/core.wxvpkg.ext') +let dest = from function writeSync(buf, start) { fs.writeSync(fd, buf, 0, buf.length, start) diff --git a/tools/wxvpkg/unpack b/tools/wxvpkg/unpack new file mode 100644 index 0000000..14c46a5 --- /dev/null +++ b/tools/wxvpkg/unpack @@ -0,0 +1,50 @@ +// Extract core.wxvpkg of current folder to dest folder +const path = require('path') +const fs = require('fs') + +const args = process.argv.slice(2); +const from = args[0] +const to = args[1] + +let dest = to +fs.mkdirSync(dest, {recursive: true}) +let file = from +let fd = fs.openSync(file, 'r') + +// read buffer +function readSync(start, length) { + const n = Buffer.alloc(length); + fs.readSync(fd, n, 0, length, start) + return n +} + +const totalCount = readSync(14, 4).readInt32BE(0) +const map = {}; +let n = 18; +for (let i = 0; i < totalCount; i++) { + const e = {}; + // byte length of filename + const i = readSync(n, 4).readInt32BE(0); + n += 4; + e.name = readSync(n, i).toString(); + n += i; + e.offset = readSync(n, 4).readInt32BE(0); + n += 4; + e.length = readSync(n, 4).readInt32BE(0); + n += 4; + map[e.name] = e +} + +let created = [] +for (let item of Object.values(map)) { + let dir = path.join(dest, path.dirname(item.name)) + if (created.indexOf(dir) == -1) { + fs.mkdirSync(dir, {recursive: true}) + created.push(dir) + } + let buf = readSync(item.offset, item.length) + let filepath = path.join(dest, item.name) + fs.writeFileSync(filepath, buf.toString('utf8'), 'utf8') +} + +fs.closeSync(fd) \ No newline at end of file