utools_adb/preload.js
2022-08-24 21:12:13 +08:00

98 lines
3.2 KiB
JavaScript

const exec = require('child_process');
const path = require('path')
const fs = require('fs')
const { POINT_CONVERSION_HYBRID } = require('constants');
const iconv = require('iconv-lite')
var encoding = 'GBK';
var binaryEncoding = 'binary';
window.exec_ = function (ext, fun) {
exec.exec(ext, { encoding: 'binary' }, (err, stdout, stderr) => {
if (stderr != '') {
fun(iconv.decode(new Buffer(stderr, binaryEncoding), encoding), true)
} else if (stdout != '') {
fun(iconv.decode(new Buffer(stdout, binaryEncoding), encoding), false)
} else if (err != '') {
try {
fun(iconv.decode(new Buffer(err, binaryEncoding), encoding), true)
} catch (error) {
fun(err)
}
} else {
fun("异常错误")
}
})
}
window.adb = function (ext, fun) {
let adb = getAdb()
let _exec = ext.replace('adb', adb)
//ext="dir"
console.log(_exec)
exec.exec(_exec, { encoding: 'binary' }, (err, stdout, stderr) => {
if (stderr != '') {
fun(iconv.decode(new Buffer(stderr, binaryEncoding), encoding), true)
} else if (stdout != '') {
fun(iconv.decode(new Buffer(stdout, binaryEncoding), encoding), false)
} else if (err != '') {
fun(iconv.decode(new Buffer(err, binaryEncoding), encoding), true)
} else {
fun("异常错误")
}
})
}
window.unapk = function (file, fun) {
let apktool = getApkTools();
let _exec = getJavaRuntimer()+"java -jar \"" + apktool + "\" d -f \"" + file + "\" --only-main-classes -out=\"" + getFilePath(file) + path.sep + getFileNotExtName(file) + "\""
exec_(_exec, fun)
}
window.apkinfo = function (file, fun) {
let aapt = getAapt()
let _exec = aapt + " dump badging " + file
exec_(_exec, fun)
}
window.packapk = function (dir, fun) {
let apktool = getApkTools();
let _exec = getJavaRuntimer()+"java -jar " + apktool + " -r b -f " + dir + " -o " + dir + path.sep + "game.apk"
exec_(_exec, fun)
}
window.signapk = function (file, fun) {
let sign = getSign()
let _exec =getJavaRuntimer()+ sign + " -verbose -keystore " + utools.db.get('sign').data + ' -keypass ' + utools.db.get('password').data
+ ' -storepass ' + utools.db.get('aliasPassword').data + ' -signedjar \"' + file + '_sign.apk\" \"'
+ file + '\" ' + utools.db.get('alias').data
console.log(_exec)
exec_(_exec, fun)
}
window.readSignDialog = function (fun) {
console.log('read file')
let data = fs.readFileSync('sign_dialog.txt');
console.log(data)
fun(data)
}
function getApkTools() {
return utools.getPath('documents') + path.sep + 'utools_adb_libs' + path.sep + 'apktool.jar'
}
function getAdb() {
return utools.getPath('documents') + path.sep + 'utools_adb_libs' + path.sep + 'adb.exe';
}
function getAapt() {
return utools.getPath('documents') + path.sep + 'utools_adb_libs' + path.sep + 'aapt.exe';
}
function getSign() {
return 'jarsigner.exe';
}
function getSignTools() {
return utools.getPath('documents') + path.sep + 'utools_adb_libs' + path.sep + 'sign'+path.sep+'bin';
}
function getJavaRuntimer(){
return "path="+getSignTools()+"&"
}
function getFilePath(file) {
return path.dirname(file)
}
function getFileNotExtName(file) {
return path.basename(file, path.extname(file))
}