Skip to content

Commit

Permalink
feat: add ripLibs settings #100
Browse files Browse the repository at this point in the history
  • Loading branch information
inotia00 committed Aug 2, 2023
1 parent ae3006c commit 4cf9dab
Show file tree
Hide file tree
Showing 7 changed files with 284 additions and 1 deletion.
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const {
checkFileAlreadyExists,
selectAppVersion,
patchApp,
patchAppWithRipLibs,
checkForUpdates,
getDevices,
setDevice,
Expand Down Expand Up @@ -201,6 +202,9 @@ wsServer.on('connection', (ws) => {
case 'patchApp':
await patchApp(ws);
break;
case 'patchAppWithRipLibs':
await patchAppWithRipLibs(ws);
break;
case 'installReVanced':
await installReVanced(ws);
break;
Expand Down
6 changes: 5 additions & 1 deletion public/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,11 @@ function getAppVersions(isRooted, page = 1) {
}

function buildReVanced() {
sendCommand({ event: 'patchApp' });
if (localStorage.getItem('rip-libs')) {
sendCommand({ event: 'patchAppWithRipLibs' });
} else {
sendCommand({ event: 'patchApp' });
}
}

function getAlreadyExists() {
Expand Down
10 changes: 10 additions & 0 deletions public/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ if (localStorage.getItem('auto-next')) {
if (localStorage.getItem('black-theme')) {
document.getElementById('blackBtn').checked = true;
}
if (localStorage.getItem('rip-libs')) {
document.getElementById('ripLibsBtn').checked = true;
}

accentColors.forEach((color) => {
if (
Expand Down Expand Up @@ -39,3 +42,10 @@ document.getElementById('blackBtn').addEventListener('click', function () {
dElement.classList.add('black');
}
});
document.getElementById('ripLibsBtn').addEventListener('click', function () {
if (localStorage.getItem('rip-libs')) {
localStorage.removeItem('rip-libs');
} else {
localStorage.setItem('rip-libs', true);
}
});
17 changes: 17 additions & 0 deletions public/settings/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,23 @@ <h3>
autocomplete="off" /><span class="slider"></span
></label>
</div>
<div class="option">
<h3>
<i class="fa-solid fa-circle-xmark"></i>RipLibs
</h3>

<p>
Leave only libraries that match the device's architecture.
<br />Caution: Available only on Android (termux).
</p>
<label class="switch"
><input
class="check"
id="ripLibsBtn"
type="checkbox"
autocomplete="off" /><span class="slider"></span
></label>
</div>
<div id="src" class="option">
<h3><i class="fa-solid fa-server"></i>Sources</h3>

Expand Down
4 changes: 4 additions & 0 deletions public/styles/fontawesome.css
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@
content: '\f138';
}

.fa-circle-xmark:before {
content: '\f057';
}

.fa-download:before {
content: '\f019';
}
Expand Down
2 changes: 2 additions & 0 deletions wsEvents/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const patchApp = require('./patchApp.js');
const patchAppWithRipLibs = require('./patchAppWithRipLibs.js');
const selectApp = require('./selectApp.js');
const selectAppVersion = require('./selectAppVersion.js');
const selectPatches = require('./selectPatches.js');
Expand All @@ -16,6 +17,7 @@ const setSettings = require('./setSettings.js');

module.exports = {
patchApp,
patchAppWithRipLibs,
selectApp,
selectAppVersion,
selectPatches,
Expand Down
242 changes: 242 additions & 0 deletions wsEvents/patchAppWithRipLibs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,242 @@
const { spawn } = require('node:child_process');
const { version } = require('node:os');
const { rmSync, renameSync } = require('node:fs');
const { join, sep: separator } = require('node:path');

const exec = require('../utils/promisifiedExec.js');

const mountReVanced = require('../utils/mountReVanced.js');

const killProcess = require('kill-process-by-name');

/**
* @param {import('ws').WebSocket} ws
*/
async function mount(ws) {
ws.send(
JSON.stringify({
event: 'patchLog',
log: 'Trying to mount ReVanced...'
})
);

await mountReVanced(global.jarNames.selectedApp.packageName, ws);
}

/**
* @param {import('ws').WebSocket} ws
*/
async function afterBuild(ws) {
try {
// HACK: Kill Java after build is done to prevent EBUSY errors while deleting the cache
killProcess('java');
rmSync('revanced-cache', { recursive: true, force: true });
} catch (ignore) {}
outputName();
renameSync(
join(global.revancedDir, 'revanced.apk'),
join(global.revancedDir, global.outputName)
);

if (!global.jarNames.isRooted && process.platform === 'android') {
await exec(
`cp "${join(
global.revancedDir,
global.outputName
)}" "/storage/emulated/0/${global.outputName}"`
);
await exec(`cp "${global.jarNames.microG}" /storage/emulated/0/microg.apk`);

ws.send(
JSON.stringify({
event: 'patchLog',
log: `Copied files over to /storage/emulated/0/!\nPlease install ReVanced, its located in /storage/emulated/0/${global.outputName}\nand if you are building YT/YTM ReVanced without root, also install /storage/emulated/0/microg.apk.`
})
);
} else if (process.platform === 'android') await mount(ws);
else if (!(global.jarNames.devices && global.jarNames.devices[0]))
ws.send(
JSON.stringify({
event: 'patchLog',
log: `ReVanced has been built!\nPlease transfer over revanced/${global.outputName} and if you are using YT/YTM, revanced/microg.apk and install them!`
})
);

if (global.jarNames.devices && global.jarNames.devices[0]) {
ws.send(JSON.stringify({ event: 'buildFinished', install: true }));
} else ws.send(JSON.stringify({ event: 'buildFinished' }));
}

async function reinstallReVanced() {
let pkgNameToGetUninstalled;

switch (global.jarNames.selectedApp.packageName) {
case 'com.google.android.youtube':
if (!global.jarNames.isRooted)
pkgNameToGetUninstalled = 'app.revanced.android.youtube';
break;
case 'com.google.android.apps.youtube.music':
if (!global.jarNames.isRooted)
pkgNameToGetUninstalled = 'app.revanced.android.apps.youtube.music';
break;
}

await exec(
`adb -s ${global.jarNames.deviceID} uninstall ${pkgNameToGetUninstalled}`
);
await exec(
`adb -s ${global.jarNames.deviceID} install ${join(
global.revancedDir,
global.outputName
)}`
);
}

function outputName() {
const part1 = 'ReVanced';
let part2 = global.jarNames?.selectedApp?.appName
? global.jarNames.selectedApp.appName.replace(/[^a-zA-Z0-9\\.\\-]/g, '')
: global?.jarNames?.packageName
? global.jarNames.packageName.replace(/\./g, '')
: ''; // make the app name empty if we cannot detect it

// TODO: If the existing input APK is used from revanced/ without downloading, version and arch aren't set
const part3 = global?.apkInfo?.version ? `v${global.apkInfo.version}` : '';
const part4 = global?.apkInfo?.arch;
const part5 = `cli_${global.jarNames.cli
.split(separator)
.at(-1)
.replace('revanced-cli-', '')
.replace('.jar', '')}`;
const part6 = `patches_${global.jarNames.patchesJar
.split(separator)
.at(-1)
.replace('revanced-patches-', '')
.replace('.jar', '')}`;

// Filename: ReVanced-<AppName>-<AppVersion>-[Arch]-cli_<CLI_Version>-patches_<PatchesVersion>.apk
let outputName = '';

for (const part of [part1, part2, part3, part4, part5, part6])
if (part) outputName += `-${part}`;

outputName += '.apk';

global.outputName = outputName.substring(1);
}

/**
* @param {string[]} args
* @param {import('ws').WebSocket} ws
*/
function reportSys(args, ws) {
ws.send(
JSON.stringify({
event: 'error',
error:
'An error occured while starting the patching process. Please see the server console.'
})
);

console.log(
'[builder] Please report these informations to https://github.com/inotia00/rvx-builder/issues'
);
console.log(
`OS: ${process.platform}\nArguements: ${args.join(
', '
)}\n OS Version${version()}`
);
}

/**
* @param {import('ws').WebSocket} ws
*/
module.exports = async function patchAppWithRipLibs(ws) {
/** @type {string[]} */
const args = [
'-jar',
global.jarNames.cli,
'-b',
global.jarNames.patchesJar,
'-t',
'./revanced-cache',
'--experimental',
'-a',
`${join(global.revancedDir, global.jarNames.selectedApp.packageName)}.apk`,
'-o',
join(global.revancedDir, 'revanced.apk')
];

if (process.platform === 'android') {
args.push('--custom-aapt2-binary');
args.push(join(global.revancedDir, 'aapt2'));

switch (process.arch) {
case 'arm':
args.push('--rip-lib=arm64-v8a');
args.push('--rip-lib=x86');
args.push('--rip-lib=x86_64');
break;
case 'arm64':
args.push('--rip-lib=armeabi-v7a');
args.push('--rip-lib=x86');
args.push('--rip-lib=x86_64');
break;
case 'ia32':
args.push('--rip-lib=armeabi-v7a');
args.push('--rip-lib=arm64-v8a');
args.push('--rip-lib=x86_64');
break;
case 'x64':
args.push('--rip-lib=armeabi-v7a');
args.push('--rip-lib=arm64-v8a');
args.push('--rip-lib=x86');
break;
}
}

if (global.jarNames.patch.integrations) {
args.push('-m');
args.push(global.jarNames.integrations);
}

args.push(...global.jarNames.patches.split(' '));

const buildProcess = spawn(global.javaCmd, args);

buildProcess.stdout.on('data', async (data) => {
ws.send(
JSON.stringify({
event: 'patchLog',
log: data.toString()
})
);

if (data.toString().includes('Finished')) await afterBuild(ws);

if (data.toString().includes('INSTALL_FAILED_UPDATE_INCOMPATIBLE')) {
await reinstallReVanced(ws);
await afterBuild(ws);
}

if (data.toString().includes('Unmatched')) reportSys(args, ws);
});

buildProcess.stderr.on('data', async (data) => {
ws.send(
JSON.stringify({
event: 'patchLog',
log: data.toString()
})
);

if (data.toString().includes('Finished')) await afterBuild(ws);

if (data.toString().includes('INSTALL_FAILED_UPDATE_INCOMPATIBLE')) {
await reinstallReVanced(ws);
await afterBuild(ws);
}

if (data.toString().includes('Unmatched')) reportSys(args, ws);
});
};

0 comments on commit 4cf9dab

Please sign in to comment.