This repository has been archived on 2023-10-05. You can view files and clone it, but cannot push or open issues or pull requests.
revanced-ps/revanced.ps1

257 lines
8.5 KiB
PowerShell
Raw Normal View History

2022-07-03 01:36:42 +02:00
Param(
2022-07-15 23:47:24 +02:00
[string]$Java = 'java',
[switch]$NoDeploy
2022-07-03 01:36:42 +02:00
)
Add-Type -AssemblyName System.Windows.Forms
Add-type -AssemblyName System.Drawing
$ProgressPreference = 'SilentlyContinue'
$ErrorActionPreference = 'Stop'
2022-07-19 18:33:03 +02:00
2022-07-03 01:36:42 +02:00
function execJava {
2022-07-19 18:33:03 +02:00
<#
.Description
execJava was created to additionally allow providing an array of strings to java arguments
if the first argument is a string array, it will launch with arguments in that array
otherwise it will launch with all provided arguments
#>
$SavedErrorActionPreference = $ErrorActionPreference
2022-07-03 01:36:42 +02:00
$ErrorActionPreference = 'Continue'
if ($args[0].getType().Name -ne "String") {
& $Java $args[0] 2>&1 | ForEach-Object{ "$_" }
}
else {
& $Java $args 2>&1 | ForEach-Object{ "$_" }
}
2022-07-19 18:33:03 +02:00
$ErrorActionPreference = $SavedErrorActionPreference
2022-07-03 01:36:42 +02:00
}
function renderForm([String[]]$items, [String]$label, [String]$title, [System.Windows.Forms.SelectionMode]$mode, [System.Collections.ArrayList]$selected) {
# https://docs.microsoft.com/en-us/powershell/scripting/samples/multiple-selection-list-boxes?view=powershell-7.2
# https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.anchorstyles
$form = New-Object System.Windows.Forms.Form
$form.Text = $title
$form.Size = New-Object System.Drawing.Size(300,200)
$form.StartPosition = 'CenterScreen'
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Anchor = 2
$OKButton.Location = New-Object System.Drawing.Point(102,120)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = 'OK'
$OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $OKButton
$form.Controls.Add($OKButton)
$_label = New-Object System.Windows.Forms.Label
$_label.Anchor = 1+4+8
$_label.Location = New-Object System.Drawing.Point(10,20)
$_label.Size = New-Object System.Drawing.Size(280,20)
$_label.Text = $label
$form.Controls.Add($_label)
$listBox = New-Object System.Windows.Forms.Listbox
$listBox.Anchor = 15
$listBox.Location = New-Object System.Drawing.Point(10,40)
$listBox.Size = New-Object System.Drawing.Size(260,20)
$listBox.SelectionMode = $mode
foreach ($entry in $items) {
[void] $listBox.Items.Add($entry)
}
if ($null -ne $selected) {
for($i = 0; ($i -lt $selected.Count) -and ($i -lt $items.Length); $i++) {
if ($selected[$i]) {$listBox.SetSelected($i, $true) | Out-Null}
}
}
$listBox.Height = 70
$form.Controls.Add($listBox)
$form.Topmost = $true
if ($form.ShowDialog() -eq [System.Windows.Forms.DialogResult]::OK) {
return $listbox.selectedItems
}
return $false
}
class config {
2022-07-20 00:52:59 +02:00
[hashtable]$version = @{}
2022-07-03 01:36:42 +02:00
[System.Collections.ArrayList]$excluded = (New-Object System.Collections.ArrayList)
}
2022-07-20 00:52:59 +02:00
class configFile {
[String]$path = ".\config.json"
[config]$config = [config]::new()
2022-07-03 01:36:42 +02:00
2022-07-20 00:52:59 +02:00
[void] load() {
$fileContent = ConvertFrom-Json ((Get-Content $this.path) -Join ' ').ToString()
2022-07-03 01:36:42 +02:00
2022-07-20 00:52:59 +02:00
$version = @{}
foreach ($prop in $fileContent.version.PsObject.Properties) {
$version.Add($prop.Name, $prop.Value)
}
$fileContent.version = $null
2022-07-03 01:36:42 +02:00
2022-07-20 00:52:59 +02:00
$this.config = $fileContent
$this.config.version = $version
}
2022-07-03 01:36:42 +02:00
2022-07-20 00:52:59 +02:00
[void] save() {
Out-File $this.path -InputObject (ConvertTo-Json $this.config)
}
}
2022-07-03 01:36:42 +02:00
2022-07-20 00:52:59 +02:00
function UpdateFromGithub {
param (
[Parameter(Mandatory)][String]$Repository,
[Parameter(Mandatory)][configFile]$Config,
[scriptblock]$AssetsFilter = {$true},
[Parameter(Mandatory)][String]$OutFile
)
2022-07-03 01:36:42 +02:00
2022-07-20 00:52:59 +02:00
[String]$InstalledTag = $Config.config.version[$Repository]
$LatestRelease
try {
$LatestRelease = ConvertFrom-Json (Invoke-WebRequest "https://api.github.com/repos/$Repository/releases/latest").Content
}
catch [System.Net.WebException] {
Write-Host "Couldn't check for update $Repository"
return $false
}
2022-07-03 01:36:42 +02:00
2022-07-20 00:52:59 +02:00
[String]$UpdateTag = $LatestRelease.tag_name
2022-07-03 01:36:42 +02:00
2022-07-20 00:52:59 +02:00
if ($InstalledTag -eq $UpdateTag) {
Write-Host "$Repository up to date! ($InstalledTag)"
return $false
}
2022-07-03 01:36:42 +02:00
2022-07-20 00:52:59 +02:00
$Assets = $LatestRelease.assets | Where-Object -FilterScript $AssetsFilter
2022-07-03 01:36:42 +02:00
2022-07-20 00:52:59 +02:00
Write-Host "Downloading $Repository ($InstalledTag -> $UpdateTag)"
Write-Host "Changelog: https://github.com/$Repository/compare/$InstalledTag...$UpdateTag"
Invoke-WebRequest $Assets[0].browser_download_url -OutFile $OutFile
$Config.config.version[$Repository] = $UpdateTag
$Config.save()
2022-07-19 18:33:03 +02:00
2022-07-20 00:52:59 +02:00
return $true
}
2022-07-03 01:36:42 +02:00
2022-07-20 00:52:59 +02:00
function main {
$config = [configFile]::new()
try {
$config.load()
2022-07-03 01:36:42 +02:00
}
2022-07-20 00:52:59 +02:00
catch [System.IO.FileNotFoundException] {
Write-Host ".\config.json not found"
$config.save()
2022-07-03 01:36:42 +02:00
}
2022-07-20 00:52:59 +02:00
# Update
UpdateFromGithub -Repository "revanced/revanced-patches" -Config $config -AssetsFilter {$_.content_type -like "application/*"} -OutFile ".\app\revanced-patches.jar" | Out-Null
UpdateFromGithub -Repository "revanced/revanced-integrations" -Config $config -AssetsFilter {$_.content_type -like "application/*"} -OutFile ".\app\app-release-unsigned.apk" | Out-Null
UpdateFromGithub -Repository "revanced/revanced-cli" -Config $config -AssetsFilter {$_.content_type -like "application/*"} -OutFile ".\app\revanced-cli.jar" | Out-Null
2022-07-15 23:47:24 +02:00
if ($NoDeploy) {return}
2022-07-03 01:36:42 +02:00
# Select Youtube app
$apks = (Get-ChildItem *.apk).Name
[String]$selectedApk = ''
if ($apks.Length -eq 0) {throw "No YouTube apk files found!"}
elseif ($apks.getType().Name -eq 'String') {$selectedApk = $apks}
2022-07-03 01:36:42 +02:00
else {
$selectedApk = (renderForm -items $apks -label "Select apk file to patch" -title "Installer for ReVanced - apk selector" -mode One)
}
if (-not $selectedApk) {throw "No apk selected!"}
2022-07-03 01:36:42 +02:00
# Get available integrations
$integrationsList = New-Object System.Collections.ArrayList
class Integration {
[String] $id
[String] $description
Integration([String] $_id, [String] $_description) {
$this.id = $_id
$this.description = $_description
}
[String] ToString() {
return "$($this.id) - $($this.description)"
}
}
2022-07-15 23:53:42 +02:00
foreach ($i in (execJava -jar .\app\revanced-cli.jar -a $selectedApk -b .\app\revanced-patches.jar -l)) {
$i = $i.ToString().substring(6).split("`t")
[void] $integrationsList.Add([Integration]::new($i[0].TrimStart(), $i[1]))
2022-07-03 01:36:42 +02:00
}
# integrations selector
$excludedIntegrations = New-Object System.Collections.ArrayList
[System.Collections.ArrayList]$savedExclusions = New-Object System.Collections.ArrayList
2022-07-20 00:52:59 +02:00
$savedExclusionsNames = $config.config.excluded
2022-07-03 01:36:42 +02:00
foreach ($i in $integrationsList) {
if (($savedExclusionsNames.Count -gt 1) -and $savedExclusionsNames.Contains($i.id)) {
[void] $savedExclusions.Add($true)
[void] $savedExclusionsNames.Remove($integrationID)
2022-07-03 01:36:42 +02:00
}
else {
[void] $savedExclusions.Add($false)
}
}
2022-07-03 01:36:42 +02:00
foreach ($i in (renderForm -items $integrationsList -label "Select integrations to exclude" -title "Installer for ReVanced - integrations" -mode MultiExtended -selected $savedExclusions)) {
[void] $excludedIntegrations.Add($i.Split(' ')[0]) #FIXME: hacky way to do this, but it works
2022-07-03 01:36:42 +02:00
}
2022-07-20 00:52:59 +02:00
$config.config.excluded = $excludedIntegrations
$config.save()
2022-07-03 01:36:42 +02:00
# devices
$devices = adb devices
$devices = $devices[1..($devices.length-2)]
[String]$selectedDevice = ''
if ($devices.Length -eq 0) {throw "No devices found"}
2022-07-19 18:01:45 +02:00
elseif ($devices.Length -eq 1) {
$selectedDevice = ($devices | ForEach-Object {$_.split()[0]})[0]
}
2022-07-03 01:36:42 +02:00
else {
2022-07-19 18:01:45 +02:00
$selectedDevice = (renderForm -items ($devices | ForEach-Object {$_.split()[0]}) -label "Select deployment target" -title "Installer for ReVanced - target" -mode One)
2022-07-03 01:36:42 +02:00
}
if (-not $selectedDevice) {throw "No device selected"}
2022-07-03 01:36:42 +02:00
# install to device
$patcherArgs = "-jar", ".\app\revanced-cli.jar", "-a", ".\$selectedApk", "-c", "-d", "$selectedDevice", "-o", "$env:tmp\revanced.apk", "-m", ".\app\app-release-unsigned.apk", "-b", ".\app\revanced-patches.jar"
# is microg patch excluded?
if ($excludedIntegrations.Contains("microg-support")) {
$patcherArgs += "--mount"
}
foreach ($i in $excludedIntegrations) {
$patcherArgs += "-e"
$patcherArgs += $i
2022-07-04 02:38:26 +02:00
Write-Output "Excluded: $i"
2022-07-03 01:36:42 +02:00
}
2022-07-04 02:38:26 +02:00
Write-Output "Logs below are from the cli"
2022-07-03 01:36:42 +02:00
execJava $patcherArgs
}
main