mirror of
https://github.com/ReVanced/revanced-patcher.git
synced 2026-01-22 10:43:57 +00:00
Compare commits
75 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
447e1ad30e | ||
|
|
843e62ad29 | ||
|
|
9c07ffcc7a | ||
|
|
438321330e | ||
|
|
3ba4be240b | ||
|
|
98ce0abfa9 | ||
|
|
db4348c4fa | ||
|
|
4839f87519 | ||
|
|
809862c997 | ||
|
|
fd5c878cee | ||
|
|
124332f0e9 | ||
|
|
d4cf0cea52 | ||
|
|
76676fb567 | ||
|
|
d802ef844e | ||
|
|
90fc547673 | ||
|
|
3813e28ac2 | ||
|
|
a2bb4004c7 | ||
|
|
a0cb449c60 | ||
|
|
e0271790b8 | ||
|
|
4bfd7ebff8 | ||
|
|
2f7e62ef65 | ||
|
|
4485af8036 | ||
|
|
085a3a479d | ||
|
|
f75c9a78b8 | ||
|
|
172655bde0 | ||
|
|
456db7289a | ||
|
|
e722e3f4f9 | ||
|
|
c348c1f0a0 | ||
|
|
ed1851013e | ||
|
|
e31ac1f132 | ||
|
|
8f78f85e4a | ||
|
|
0be2677519 | ||
|
|
b873228ef0 | ||
|
|
639ff1c0ba | ||
|
|
f30671ddd1 | ||
|
|
76c45dd7c1 | ||
|
|
1bafb77355 | ||
|
|
25f74dc5e9 | ||
|
|
6e73631d4d | ||
|
|
7761d5b85e | ||
|
|
62aa295e73 | ||
|
|
596ede1b12 | ||
|
|
7debe62738 | ||
|
|
002f84da1a | ||
|
|
aff4968e6f | ||
|
|
1d989abd55 | ||
|
|
f1775f83d0 | ||
|
|
4055939c08 | ||
|
|
85120374d6 | ||
|
|
8b4819faa1 | ||
|
|
d219276298 | ||
|
|
79f91e0e5a | ||
|
|
fadf62f594 | ||
|
|
ad3d332e27 | ||
|
|
8f66df7666 | ||
|
|
80c2e80925 | ||
|
|
c3db23d3c7 | ||
|
|
c28584736e | ||
|
|
6b909c1ee6 | ||
|
|
0e8446516e | ||
|
|
aa46b953db | ||
|
|
a562e476c0 | ||
|
|
75d2be8803 | ||
|
|
d6308e126c | ||
|
|
bb97af4d86 | ||
|
|
392164862c | ||
|
|
53e807dec1 | ||
|
|
288d50a8b4 | ||
|
|
131dedd4b0 | ||
|
|
5a92d5c29d | ||
|
|
4b81318710 | ||
|
|
44f6a3ebc5 | ||
|
|
7882a8d928 | ||
|
|
cc3d32748b | ||
|
|
f9da2ad531 |
12
.github/ISSUE_TEMPLATE/bug-issue.yml
vendored
12
.github/ISSUE_TEMPLATE/bug-issue.yml
vendored
@@ -58,3 +58,15 @@ body:
|
|||||||
description: Add additional context here.
|
description: Add additional context here.
|
||||||
validations:
|
validations:
|
||||||
required: false
|
required: false
|
||||||
|
- type: checkboxes
|
||||||
|
id: acknowledgements
|
||||||
|
attributes:
|
||||||
|
label: Acknowledgements
|
||||||
|
description: Your issue will be closed if you haven't done these steps.
|
||||||
|
options:
|
||||||
|
- label: I have searched the existing issues and this is a new and no duplicate or related to another open issue.
|
||||||
|
required: true
|
||||||
|
- label: I have written a short but informative title.
|
||||||
|
required: true
|
||||||
|
- label: I filled out all of the requested information in this issue properly.
|
||||||
|
required: true
|
||||||
12
.github/ISSUE_TEMPLATE/feature-issue.yml
vendored
12
.github/ISSUE_TEMPLATE/feature-issue.yml
vendored
@@ -44,3 +44,15 @@ body:
|
|||||||
description: Add additional context here.
|
description: Add additional context here.
|
||||||
validations:
|
validations:
|
||||||
required: false
|
required: false
|
||||||
|
- type: checkboxes
|
||||||
|
id: acknowledgements
|
||||||
|
attributes:
|
||||||
|
label: Acknowledgements
|
||||||
|
description: Your issue will be closed if you haven't done these steps.
|
||||||
|
options:
|
||||||
|
- label: I have searched the existing issues and this is a new and no duplicate or related to another open issue.
|
||||||
|
required: true
|
||||||
|
- label: I have written a short but informative title.
|
||||||
|
required: true
|
||||||
|
- label: I filled out all of the requested information in this issue properly.
|
||||||
|
required: true
|
||||||
1
.idea/.gitignore
generated
vendored
1
.idea/.gitignore
generated
vendored
@@ -6,3 +6,4 @@
|
|||||||
# Datasource local storage ignored files
|
# Datasource local storage ignored files
|
||||||
/dataSources/
|
/dataSources/
|
||||||
/dataSources.local.xml
|
/dataSources.local.xml
|
||||||
|
/kotlinc.xml
|
||||||
|
|||||||
219
CHANGELOG.md
219
CHANGELOG.md
@@ -1,3 +1,222 @@
|
|||||||
|
## [5.1.2](https://github.com/revanced/revanced-patcher/compare/v5.1.1...v5.1.2) (2022-09-29)
|
||||||
|
|
||||||
|
|
||||||
|
### Bug Fixes
|
||||||
|
|
||||||
|
* check dependencies for resource patches ([9c07ffc](https://github.com/revanced/revanced-patcher/commit/9c07ffcc7af9f088426528561f4321c5cc6b5b15))
|
||||||
|
* use instruction index instead of strings list index for `StringMatch` ([843e62a](https://github.com/revanced/revanced-patcher/commit/843e62ad290ee0a707be9322ee943921da3ea420))
|
||||||
|
|
||||||
|
## [5.1.1](https://github.com/revanced/revanced-patcher/compare/v5.1.0...v5.1.1) (2022-09-26)
|
||||||
|
|
||||||
|
|
||||||
|
### Performance Improvements
|
||||||
|
|
||||||
|
* decode resources only when necessary ([3ba4be2](https://github.com/revanced/revanced-patcher/commit/3ba4be240bf0a424e4bbfbaca9605644fda0984e))
|
||||||
|
|
||||||
|
# [5.1.0](https://github.com/revanced/revanced-patcher/compare/v5.0.1...v5.1.0) (2022-09-26)
|
||||||
|
|
||||||
|
|
||||||
|
### Features
|
||||||
|
|
||||||
|
* RwLock for opening files in `DomFileEditor` ([db4348c](https://github.com/revanced/revanced-patcher/commit/db4348c4faf51bfe29678baacfbe76ba645ec0b9))
|
||||||
|
|
||||||
|
## [5.0.1](https://github.com/revanced/revanced-patcher/compare/v5.0.0...v5.0.1) (2022-09-23)
|
||||||
|
|
||||||
|
|
||||||
|
### Reverts
|
||||||
|
|
||||||
|
* revert breaking changes ([#106](https://github.com/revanced/revanced-patcher/issues/106)) ([124332f](https://github.com/revanced/revanced-patcher/commit/124332f0e9bbdaf4f1aeeb6a31333093eeba1642))
|
||||||
|
|
||||||
|
# [5.0.0](https://github.com/revanced/revanced-patcher/compare/v4.5.0...v5.0.0) (2022-09-21)
|
||||||
|
|
||||||
|
|
||||||
|
### Bug Fixes
|
||||||
|
|
||||||
|
* **tests:** access `patternScanResult` through `scanResult` ([76676fb](https://github.com/revanced/revanced-patcher/commit/76676fb5673a9e92517ee3a13943cdc98dd5102a))
|
||||||
|
|
||||||
|
|
||||||
|
* refactor!: move utility methods from `MethodFingerprintUtils` `MethodFingerprint` ([d802ef8](https://github.com/revanced/revanced-patcher/commit/d802ef844edf65d4d26328d6ca72e3ddd5a52b15))
|
||||||
|
* feat(fingerprint)!: `StringsScanResult` for `MethodFingerprint` ([3813e28](https://github.com/revanced/revanced-patcher/commit/3813e28ac2ad6710d8d935526ca679e7b1b5980e))
|
||||||
|
|
||||||
|
|
||||||
|
### BREAKING CHANGES
|
||||||
|
|
||||||
|
* Imports will have to be updated from `MethodFingerprintUtils` to `MethodFingerprint.Companion`.
|
||||||
|
|
||||||
|
Signed-off-by: oSumAtrIX <johan.melkonyan1@web.de>
|
||||||
|
* `MethodFingerprint` now has a field for `MethodFingerprintScanResult`. `MethodFingerprintScanResult` now holds the previous field `MethodFingerprint.patternScanResult`.
|
||||||
|
|
||||||
|
Signed-off-by: oSumAtrIX <johan.melkonyan1@web.de>
|
||||||
|
|
||||||
|
# [4.5.0](https://github.com/revanced/revanced-patcher/compare/v4.4.2...v4.5.0) (2022-09-20)
|
||||||
|
|
||||||
|
|
||||||
|
### Features
|
||||||
|
|
||||||
|
* section `acknowledgements` for issue templates ([a0cb449](https://github.com/revanced/revanced-patcher/commit/a0cb449c60310917141e2809abaa16b4174dc002))
|
||||||
|
|
||||||
|
## [4.4.2](https://github.com/revanced/revanced-patcher/compare/v4.4.1...v4.4.2) (2022-09-18)
|
||||||
|
|
||||||
|
|
||||||
|
### Bug Fixes
|
||||||
|
|
||||||
|
* **fingerprint:** do not throw on `MethodFingerprint.result` getter ([2f7e62e](https://github.com/revanced/revanced-patcher/commit/2f7e62ef65422f2c75ef8b09b9cd27076e172b30))
|
||||||
|
|
||||||
|
|
||||||
|
### Performance Improvements
|
||||||
|
|
||||||
|
* **fingerprint:** do not resolve already resolved fingerprints ([4bfd7eb](https://github.com/revanced/revanced-patcher/commit/4bfd7ebff8b6623b0da4a46d6048bed08c5070d4))
|
||||||
|
|
||||||
|
## [4.4.1](https://github.com/revanced/revanced-patcher/compare/v4.4.0...v4.4.1) (2022-09-14)
|
||||||
|
|
||||||
|
|
||||||
|
### Bug Fixes
|
||||||
|
|
||||||
|
* compare any methods parameters ([#101](https://github.com/revanced/revanced-patcher/issues/101)) ([085a3a4](https://github.com/revanced/revanced-patcher/commit/085a3a479d7bd411dcb0492b283daca538c824a1))
|
||||||
|
|
||||||
|
# [4.4.0](https://github.com/revanced/revanced-patcher/compare/v4.3.0...v4.4.0) (2022-09-09)
|
||||||
|
|
||||||
|
|
||||||
|
### Features
|
||||||
|
|
||||||
|
* add PathOption back ([172655b](https://github.com/revanced/revanced-patcher/commit/172655bde06efdb0955431b44d269e6a64fe317a))
|
||||||
|
|
||||||
|
# [4.3.0](https://github.com/revanced/revanced-patcher/compare/v4.2.3...v4.3.0) (2022-09-09)
|
||||||
|
|
||||||
|
|
||||||
|
### Features
|
||||||
|
|
||||||
|
* improved Patch Options ([e722e3f](https://github.com/revanced/revanced-patcher/commit/e722e3f4f9dc64acf53595802a0a83cf46ee96b8))
|
||||||
|
|
||||||
|
## [4.2.3](https://github.com/revanced/revanced-patcher/compare/v4.2.2...v4.2.3) (2022-09-08)
|
||||||
|
|
||||||
|
|
||||||
|
### Bug Fixes
|
||||||
|
|
||||||
|
* wrong value for iterator in PatchOptions ([e31ac1f](https://github.com/revanced/revanced-patcher/commit/e31ac1f132df56ba7d2f8446d289ae03ef28f67d))
|
||||||
|
|
||||||
|
## [4.2.2](https://github.com/revanced/revanced-patcher/compare/v4.2.1...v4.2.2) (2022-09-08)
|
||||||
|
|
||||||
|
|
||||||
|
### Bug Fixes
|
||||||
|
|
||||||
|
* invalid type propagation in options ([b873228](https://github.com/revanced/revanced-patcher/commit/b873228ef0a9e6e431a4278c979caa5fcc508e0d)), closes [#98](https://github.com/revanced/revanced-patcher/issues/98)
|
||||||
|
|
||||||
|
## [4.2.1](https://github.com/revanced/revanced-patcher/compare/v4.2.0...v4.2.1) (2022-09-08)
|
||||||
|
|
||||||
|
|
||||||
|
### Bug Fixes
|
||||||
|
|
||||||
|
* make patcher version public ([76c45dd](https://github.com/revanced/revanced-patcher/commit/76c45dd7c1ffdca57e30ae7109c9fe0e5768f877))
|
||||||
|
|
||||||
|
# [4.2.0](https://github.com/revanced/revanced-patcher/compare/v4.1.5...v4.2.0) (2022-09-08)
|
||||||
|
|
||||||
|
|
||||||
|
### Bug Fixes
|
||||||
|
|
||||||
|
* remove repeatable from PatchDeprecated ([6e73631](https://github.com/revanced/revanced-patcher/commit/6e73631d4d21e5e862f07ed7517244f36394e5ca))
|
||||||
|
|
||||||
|
|
||||||
|
### Features
|
||||||
|
|
||||||
|
* SincePatcher annotation ([25f74dc](https://github.com/revanced/revanced-patcher/commit/25f74dc5e9ed1a09258345b920d4f5a0dd7da527))
|
||||||
|
|
||||||
|
## [4.1.5](https://github.com/revanced/revanced-patcher/compare/v4.1.4...v4.1.5) (2022-09-08)
|
||||||
|
|
||||||
|
|
||||||
|
### Bug Fixes
|
||||||
|
|
||||||
|
* broken deprecation message ([62aa295](https://github.com/revanced/revanced-patcher/commit/62aa295e7372014238415af36d902a4e88e2acbc))
|
||||||
|
|
||||||
|
## [4.1.4](https://github.com/revanced/revanced-patcher/compare/v4.1.3...v4.1.4) (2022-09-08)
|
||||||
|
|
||||||
|
|
||||||
|
### Bug Fixes
|
||||||
|
|
||||||
|
* handle option types and nulls properly ([aff4968](https://github.com/revanced/revanced-patcher/commit/aff4968e6f67239afa3b5c02cc133a17d9c3cbeb))
|
||||||
|
|
||||||
|
## [4.1.3](https://github.com/revanced/revanced-patcher/compare/v4.1.2...v4.1.3) (2022-09-07)
|
||||||
|
|
||||||
|
|
||||||
|
### Bug Fixes
|
||||||
|
|
||||||
|
* only run list option check if not null ([4055939](https://github.com/revanced/revanced-patcher/commit/4055939c089e3c396c308c980215d93a1dea5954))
|
||||||
|
|
||||||
|
## [4.1.2](https://github.com/revanced/revanced-patcher/compare/v4.1.1...v4.1.2) (2022-09-07)
|
||||||
|
|
||||||
|
|
||||||
|
### Bug Fixes
|
||||||
|
|
||||||
|
* invalid types for example options ([79f91e0](https://github.com/revanced/revanced-patcher/commit/79f91e0e5a6d99828f30aae55339ce0d897394c7))
|
||||||
|
|
||||||
|
## [4.1.1](https://github.com/revanced/revanced-patcher/compare/v4.1.0...v4.1.1) (2022-09-07)
|
||||||
|
|
||||||
|
|
||||||
|
### Bug Fixes
|
||||||
|
|
||||||
|
* handle private companion objects ([ad3d332](https://github.com/revanced/revanced-patcher/commit/ad3d332e27d07e9d074bbaaf51af7eb2f9bfc7d5))
|
||||||
|
|
||||||
|
# [4.1.0](https://github.com/revanced/revanced-patcher/compare/v4.0.0...v4.1.0) (2022-09-07)
|
||||||
|
|
||||||
|
|
||||||
|
### Features
|
||||||
|
|
||||||
|
* deprecation for patches ([80c2e80](https://github.com/revanced/revanced-patcher/commit/80c2e809251cdb04d2dd3b3bfdbb8844bdfa31fa))
|
||||||
|
|
||||||
|
# [4.0.0](https://github.com/revanced/revanced-patcher/compare/v3.5.1...v4.0.0) (2022-09-07)
|
||||||
|
|
||||||
|
|
||||||
|
### Code Refactoring
|
||||||
|
|
||||||
|
* Improve Patch Options ([6b909c1](https://github.com/revanced/revanced-patcher/commit/6b909c1ee6b8c2ea08bbca059df755e2e5f31656))
|
||||||
|
|
||||||
|
|
||||||
|
### BREAKING CHANGES
|
||||||
|
|
||||||
|
* Options has been moved from Patch to a new interface called OptionsContainer and are now handled entirely different. Make sure to check the examples to understand how it works.
|
||||||
|
|
||||||
|
## [3.5.1](https://github.com/revanced/revanced-patcher/compare/v3.5.0...v3.5.1) (2022-09-06)
|
||||||
|
|
||||||
|
|
||||||
|
### Bug Fixes
|
||||||
|
|
||||||
|
* add tests for PathOption ([d6308e1](https://github.com/revanced/revanced-patcher/commit/d6308e126c6217b098192c51b6e98bc85a8656bd))
|
||||||
|
* PathOption should be open, not sealed ([a562e47](https://github.com/revanced/revanced-patcher/commit/a562e476c085841efbc7ee98b01d8e6bb18ed757))
|
||||||
|
* typo in ListOption ([3921648](https://github.com/revanced/revanced-patcher/commit/392164862c83d6e76b2a2113d6f6d59fef0020d1))
|
||||||
|
|
||||||
|
|
||||||
|
### Performance Improvements
|
||||||
|
|
||||||
|
* make exception an object ([75d2be8](https://github.com/revanced/revanced-patcher/commit/75d2be88037c9cf5436ab69d92abea575409a865))
|
||||||
|
|
||||||
|
# [3.5.0](https://github.com/revanced/revanced-patcher/compare/v3.4.1...v3.5.0) (2022-09-05)
|
||||||
|
|
||||||
|
|
||||||
|
### Features
|
||||||
|
|
||||||
|
* default value for `Package.versions` annotation parameter ([131dedd](https://github.com/revanced/revanced-patcher/commit/131dedd4b021fe1c3b0be49ccba4764b325770ea))
|
||||||
|
|
||||||
|
## [3.4.1](https://github.com/revanced/revanced-patcher/compare/v3.4.0...v3.4.1) (2022-09-03)
|
||||||
|
|
||||||
|
|
||||||
|
### Bug Fixes
|
||||||
|
|
||||||
|
* remove default param from Package.versions ([4b81318](https://github.com/revanced/revanced-patcher/commit/4b813187107e85dc267dbc2d353884b2cc671cc4))
|
||||||
|
|
||||||
|
# [3.4.0](https://github.com/revanced/revanced-patcher/compare/v3.3.3...v3.4.0) (2022-08-31)
|
||||||
|
|
||||||
|
|
||||||
|
### Features
|
||||||
|
|
||||||
|
* nullable parameters ([7882a8d](https://github.com/revanced/revanced-patcher/commit/7882a8d928cad8de8cfea711947fc02659549d20))
|
||||||
|
|
||||||
|
## [3.3.3](https://github.com/revanced/revanced-patcher/compare/v3.3.2...v3.3.3) (2022-08-14)
|
||||||
|
|
||||||
|
|
||||||
|
### Bug Fixes
|
||||||
|
|
||||||
|
* show error message if cause is null ([f9da2ad](https://github.com/revanced/revanced-patcher/commit/f9da2ad531644617ad5a2cc6a1819d530e18ba22))
|
||||||
|
|
||||||
## [3.3.2](https://github.com/revanced/revanced-patcher/compare/v3.3.1...v3.3.2) (2022-08-06)
|
## [3.3.2](https://github.com/revanced/revanced-patcher/compare/v3.3.1...v3.3.2) (2022-08-06)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -24,8 +24,9 @@ dependencies {
|
|||||||
implementation("xpp3:xpp3:1.1.4c")
|
implementation("xpp3:xpp3:1.1.4c")
|
||||||
implementation("org.smali:smali:2.5.2")
|
implementation("org.smali:smali:2.5.2")
|
||||||
implementation("app.revanced:multidexlib2:2.5.2.r2")
|
implementation("app.revanced:multidexlib2:2.5.2.r2")
|
||||||
implementation("org.apktool:apktool-lib:2.7.0-SNAPSHOT")
|
implementation("org.apktool:apktool-lib:2.8.1-SNAPSHOT")
|
||||||
|
|
||||||
|
implementation(kotlin("reflect"))
|
||||||
testImplementation(kotlin("test"))
|
testImplementation(kotlin("test"))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -36,6 +37,9 @@ tasks {
|
|||||||
events("PASSED", "SKIPPED", "FAILED")
|
events("PASSED", "SKIPPED", "FAILED")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
processResources {
|
||||||
|
expand("projectVersion" to project.version)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
java {
|
java {
|
||||||
|
|||||||
@@ -1,2 +1,2 @@
|
|||||||
kotlin.code.style = official
|
kotlin.code.style = official
|
||||||
version = 3.3.2
|
version = 5.1.2
|
||||||
|
|||||||
@@ -1,12 +1,13 @@
|
|||||||
package app.revanced.patcher
|
package app.revanced.patcher
|
||||||
|
|
||||||
import app.revanced.patcher.data.Data
|
import app.revanced.patcher.data.Data
|
||||||
import app.revanced.patcher.data.PackageMetadata
|
|
||||||
import app.revanced.patcher.data.impl.findIndexed
|
import app.revanced.patcher.data.impl.findIndexed
|
||||||
import app.revanced.patcher.extensions.PatchExtensions.dependencies
|
import app.revanced.patcher.extensions.PatchExtensions.dependencies
|
||||||
|
import app.revanced.patcher.extensions.PatchExtensions.deprecated
|
||||||
import app.revanced.patcher.extensions.PatchExtensions.patchName
|
import app.revanced.patcher.extensions.PatchExtensions.patchName
|
||||||
|
import app.revanced.patcher.extensions.PatchExtensions.sincePatcherVersion
|
||||||
import app.revanced.patcher.extensions.nullOutputStream
|
import app.revanced.patcher.extensions.nullOutputStream
|
||||||
import app.revanced.patcher.fingerprint.method.utils.MethodFingerprintUtils.resolve
|
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint.Companion.resolve
|
||||||
import app.revanced.patcher.patch.Patch
|
import app.revanced.patcher.patch.Patch
|
||||||
import app.revanced.patcher.patch.PatchResult
|
import app.revanced.patcher.patch.PatchResult
|
||||||
import app.revanced.patcher.patch.PatchResultError
|
import app.revanced.patcher.patch.PatchResultError
|
||||||
@@ -14,6 +15,7 @@ import app.revanced.patcher.patch.PatchResultSuccess
|
|||||||
import app.revanced.patcher.patch.impl.BytecodePatch
|
import app.revanced.patcher.patch.impl.BytecodePatch
|
||||||
import app.revanced.patcher.patch.impl.ResourcePatch
|
import app.revanced.patcher.patch.impl.ResourcePatch
|
||||||
import app.revanced.patcher.util.ListBackedSet
|
import app.revanced.patcher.util.ListBackedSet
|
||||||
|
import app.revanced.patcher.util.VersionReader
|
||||||
import brut.androlib.Androlib
|
import brut.androlib.Androlib
|
||||||
import brut.androlib.meta.UsesFramework
|
import brut.androlib.meta.UsesFramework
|
||||||
import brut.androlib.options.BuildOptions
|
import brut.androlib.options.BuildOptions
|
||||||
@@ -35,7 +37,7 @@ import java.io.Closeable
|
|||||||
import java.io.File
|
import java.io.File
|
||||||
import java.nio.file.Files
|
import java.nio.file.Files
|
||||||
|
|
||||||
val NAMER = BasicDexFileNamer()
|
private val NAMER = BasicDexFileNamer()
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The ReVanced Patcher.
|
* The ReVanced Patcher.
|
||||||
@@ -44,81 +46,32 @@ val NAMER = BasicDexFileNamer()
|
|||||||
class Patcher(private val options: PatcherOptions) {
|
class Patcher(private val options: PatcherOptions) {
|
||||||
private val logger = options.logger
|
private val logger = options.logger
|
||||||
private val opcodes: Opcodes
|
private val opcodes: Opcodes
|
||||||
|
private var resourceDecodingMode = ResourceDecodingMode.MANIFEST_ONLY
|
||||||
val data: PatcherData
|
val data: PatcherData
|
||||||
|
|
||||||
init {
|
companion object {
|
||||||
val extInputFile = ExtFile(options.inputFile)
|
@JvmStatic
|
||||||
try {
|
val version = VersionReader.read()
|
||||||
val outDir = File(options.resourceCacheDirectory)
|
private fun BuildOptions.setBuildOptions(options: PatcherOptions) {
|
||||||
if (outDir.exists()) {
|
this.aaptPath = options.aaptPath
|
||||||
logger.info("Deleting existing resource cache directory")
|
this.useAapt2 = true
|
||||||
outDir.deleteRecursively()
|
this.frameworkFolderLocation = options.frameworkFolderLocation
|
||||||
}
|
|
||||||
outDir.mkdirs()
|
|
||||||
|
|
||||||
val androlib = Androlib(BuildOptions().also { it.setBuildOptions(options) })
|
|
||||||
val resourceTable = androlib.getResTable(extInputFile, true)
|
|
||||||
|
|
||||||
val packageMetadata = PackageMetadata()
|
|
||||||
|
|
||||||
if (options.patchResources) {
|
|
||||||
logger.info("Decoding resources")
|
|
||||||
|
|
||||||
// decode resources to cache directory
|
|
||||||
androlib.decodeManifestWithResources(extInputFile, outDir, resourceTable)
|
|
||||||
androlib.decodeResourcesFull(extInputFile, outDir, resourceTable)
|
|
||||||
|
|
||||||
// read additional metadata from the resource table
|
|
||||||
packageMetadata.metaInfo.usesFramework = UsesFramework().also { framework ->
|
|
||||||
framework.ids = resourceTable.listFramePackages().map { it.id }.sorted()
|
|
||||||
}
|
|
||||||
|
|
||||||
packageMetadata.metaInfo.doNotCompress = buildList {
|
|
||||||
androlib.recordUncompressedFiles(extInputFile, this)
|
|
||||||
}
|
|
||||||
|
|
||||||
} else {
|
|
||||||
logger.info("Only decoding AndroidManifest.xml because resource patching is disabled")
|
|
||||||
|
|
||||||
// create decoder for the resource table
|
|
||||||
val decoder = ResAttrDecoder()
|
|
||||||
decoder.currentPackage = ResPackage(resourceTable, 0, null)
|
|
||||||
|
|
||||||
// create xml parser with the decoder
|
|
||||||
val axmlParser = AXmlResourceParser()
|
|
||||||
axmlParser.attrDecoder = decoder
|
|
||||||
|
|
||||||
// parse package information with the decoder and parser which will set required values in the resource table
|
|
||||||
// instead of decodeManifest another more low level solution can be created to make it faster/better
|
|
||||||
XmlPullStreamDecoder(
|
|
||||||
axmlParser, AndrolibResources().resXmlSerializer
|
|
||||||
).decodeManifest(
|
|
||||||
extInputFile.directory.getFileInput("AndroidManifest.xml"), nullOutputStream
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
packageMetadata.packageName = resourceTable.currentResPackage.name
|
|
||||||
packageMetadata.packageVersion = resourceTable.versionInfo.versionName
|
|
||||||
packageMetadata.metaInfo.versionInfo = resourceTable.versionInfo
|
|
||||||
packageMetadata.metaInfo.sdkInfo = resourceTable.sdkInfo
|
|
||||||
|
|
||||||
logger.info("Reading dex files")
|
|
||||||
|
|
||||||
// read dex files
|
|
||||||
val dexFile = MultiDexIO.readDexFile(true, options.inputFile, NAMER, null, null)
|
|
||||||
// get the opcodes
|
|
||||||
opcodes = dexFile.opcodes
|
|
||||||
|
|
||||||
// finally create patcher data
|
|
||||||
data = PatcherData(
|
|
||||||
dexFile.classes.toMutableList(), options.resourceCacheDirectory, packageMetadata
|
|
||||||
)
|
|
||||||
} finally {
|
|
||||||
extInputFile.close()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
init {
|
||||||
|
logger.info("Reading dex files")
|
||||||
|
// read dex files
|
||||||
|
val dexFile = MultiDexIO.readDexFile(true, options.inputFile, NAMER, null, null)
|
||||||
|
// get the opcodes
|
||||||
|
opcodes = dexFile.opcodes
|
||||||
|
// finally create patcher data
|
||||||
|
data = PatcherData(dexFile.classes.toMutableList(), options.resourceCacheDirectory)
|
||||||
|
|
||||||
|
// decode manifest file
|
||||||
|
decodeResources(ResourceDecodingMode.MANIFEST_ONLY)
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add additional dex file container to the patcher.
|
* Add additional dex file container to the patcher.
|
||||||
* @param files The dex file containers to add to the patcher.
|
* @param files The dex file containers to add to the patcher.
|
||||||
@@ -167,48 +120,51 @@ class Patcher(private val options: PatcherOptions) {
|
|||||||
val metaInfo = packageMetadata.metaInfo
|
val metaInfo = packageMetadata.metaInfo
|
||||||
var resourceFile: File? = null
|
var resourceFile: File? = null
|
||||||
|
|
||||||
if (options.patchResources) {
|
when (resourceDecodingMode) {
|
||||||
val cacheDirectory = ExtFile(options.resourceCacheDirectory)
|
ResourceDecodingMode.FULL -> {
|
||||||
try {
|
val cacheDirectory = ExtFile(options.resourceCacheDirectory)
|
||||||
val androlibResources = AndrolibResources().also { resources ->
|
try {
|
||||||
resources.buildOptions = BuildOptions().also { buildOptions ->
|
val androlibResources = AndrolibResources().also { resources ->
|
||||||
buildOptions.setBuildOptions(options)
|
resources.buildOptions = BuildOptions().also { buildOptions ->
|
||||||
buildOptions.isFramework = metaInfo.isFrameworkApk
|
buildOptions.setBuildOptions(options)
|
||||||
buildOptions.resourcesAreCompressed = metaInfo.compressionType
|
buildOptions.isFramework = metaInfo.isFrameworkApk
|
||||||
buildOptions.doNotCompress = metaInfo.doNotCompress
|
buildOptions.resourcesAreCompressed = metaInfo.compressionType
|
||||||
|
buildOptions.doNotCompress = metaInfo.doNotCompress
|
||||||
|
}
|
||||||
|
|
||||||
|
resources.setSdkInfo(metaInfo.sdkInfo)
|
||||||
|
resources.setVersionInfo(metaInfo.versionInfo)
|
||||||
|
resources.setSharedLibrary(metaInfo.sharedLibrary)
|
||||||
|
resources.setSparseResources(metaInfo.sparseResources)
|
||||||
}
|
}
|
||||||
|
|
||||||
resources.setSdkInfo(metaInfo.sdkInfo)
|
val manifestFile = cacheDirectory.resolve("AndroidManifest.xml")
|
||||||
resources.setVersionInfo(metaInfo.versionInfo)
|
|
||||||
resources.setSharedLibrary(metaInfo.sharedLibrary)
|
|
||||||
resources.setSparseResources(metaInfo.sparseResources)
|
|
||||||
}
|
|
||||||
|
|
||||||
val manifestFile = cacheDirectory.resolve("AndroidManifest.xml")
|
ResXmlPatcher.fixingPublicAttrsInProviderAttributes(manifestFile)
|
||||||
|
|
||||||
ResXmlPatcher.fixingPublicAttrsInProviderAttributes(manifestFile)
|
val aaptFile = cacheDirectory.resolve("aapt_temp_file")
|
||||||
|
|
||||||
val aaptFile = cacheDirectory.resolve("aapt_temp_file")
|
// delete if it exists
|
||||||
|
Files.deleteIfExists(aaptFile.toPath())
|
||||||
|
|
||||||
// delete if it exists
|
val resDirectory = cacheDirectory.resolve("res")
|
||||||
Files.deleteIfExists(aaptFile.toPath())
|
val includedFiles = metaInfo.usesFramework.ids.map { id ->
|
||||||
|
androlibResources.getFrameworkApk(
|
||||||
|
id, metaInfo.usesFramework.tag
|
||||||
|
)
|
||||||
|
}.toTypedArray()
|
||||||
|
|
||||||
val resDirectory = cacheDirectory.resolve("res")
|
logger.info("Compiling resources")
|
||||||
val includedFiles = metaInfo.usesFramework.ids.map { id ->
|
androlibResources.aaptPackage(
|
||||||
androlibResources.getFrameworkApk(
|
aaptFile, manifestFile, resDirectory, null, null, includedFiles
|
||||||
id, metaInfo.usesFramework.tag
|
|
||||||
)
|
)
|
||||||
}.toTypedArray()
|
|
||||||
|
|
||||||
logger.info("Compiling resources")
|
resourceFile = aaptFile
|
||||||
androlibResources.aaptPackage(
|
} finally {
|
||||||
aaptFile, manifestFile, resDirectory, null, null, includedFiles
|
cacheDirectory.close()
|
||||||
)
|
}
|
||||||
|
|
||||||
resourceFile = aaptFile
|
|
||||||
} finally {
|
|
||||||
cacheDirectory.close()
|
|
||||||
}
|
}
|
||||||
|
else -> logger.info("Not compiling resources because resource patching is not required")
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.trace("Creating new dex file")
|
logger.trace("Creating new dex file")
|
||||||
@@ -234,7 +190,9 @@ class Patcher(private val options: PatcherOptions) {
|
|||||||
return PatcherResult(
|
return PatcherResult(
|
||||||
dexFiles.map {
|
dexFiles.map {
|
||||||
app.revanced.patcher.util.dex.DexFile(it.key, it.value.readAt(0))
|
app.revanced.patcher.util.dex.DexFile(it.key, it.value.readAt(0))
|
||||||
}, metaInfo.doNotCompress?.toList(), resourceFile
|
},
|
||||||
|
metaInfo.doNotCompress?.toList(),
|
||||||
|
resourceFile
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -243,7 +201,29 @@ class Patcher(private val options: PatcherOptions) {
|
|||||||
* @param patches [Patch]es The patches to add.
|
* @param patches [Patch]es The patches to add.
|
||||||
*/
|
*/
|
||||||
fun addPatches(patches: Iterable<Class<out Patch<Data>>>) {
|
fun addPatches(patches: Iterable<Class<out Patch<Data>>>) {
|
||||||
data.patches.addAll(patches)
|
/**
|
||||||
|
* Fill the cache with the instances of the [Patch]es for later use.
|
||||||
|
* Note: Dependencies of the [Patch] will be cached as well.
|
||||||
|
*/
|
||||||
|
fun Class<out Patch<Data>>.isResource() {
|
||||||
|
this.also {
|
||||||
|
if (!ResourcePatch::class.java.isAssignableFrom(it)) return@also
|
||||||
|
// set the mode to decode all resources before running the patches
|
||||||
|
resourceDecodingMode = ResourceDecodingMode.FULL
|
||||||
|
}.dependencies?.forEach { it.java.isResource() }
|
||||||
|
}
|
||||||
|
|
||||||
|
data.patches.addAll(
|
||||||
|
patches.onEach(Class<out Patch<Data>>::isResource).onEach { patch ->
|
||||||
|
val needsVersion = patch.sincePatcherVersion
|
||||||
|
if (needsVersion != null && needsVersion > version) {
|
||||||
|
logger.error("Patch '${patch.patchName}' requires Patcher version $needsVersion or higher")
|
||||||
|
logger.error("Current Patcher version is $version")
|
||||||
|
logger.warn("Skipping '${patch.patchName}'!")
|
||||||
|
return@onEach // TODO: continue or halt/throw?
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -269,43 +249,120 @@ class Patcher(private val options: PatcherOptions) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// recursively apply all dependency patches
|
// recursively apply all dependency patches
|
||||||
patch.dependencies?.forEach {
|
patch.dependencies?.forEach { dependencyClass ->
|
||||||
val patchDependency = it.java
|
val dependency = dependencyClass.java
|
||||||
|
|
||||||
val result = applyPatch(patchDependency, appliedPatches)
|
|
||||||
|
|
||||||
|
val result = applyPatch(dependency, appliedPatches)
|
||||||
if (result.isSuccess()) return@forEach
|
if (result.isSuccess()) return@forEach
|
||||||
|
|
||||||
val errorMessage = result.error()!!.cause
|
val error = result.error()!!
|
||||||
return PatchResultError("'$patchName' depends on '${patchDependency.patchName}' but the following error was raised: $errorMessage")
|
val errorMessage = error.cause ?: error.message
|
||||||
|
return PatchResultError("'$patchName' depends on '${dependency.patchName}' but the following error was raised: $errorMessage")
|
||||||
|
}
|
||||||
|
|
||||||
|
patch.deprecated?.let { (reason, replacement) ->
|
||||||
|
logger.warn("'$patchName' is deprecated, reason: $reason")
|
||||||
|
if (replacement != null) logger.warn("Use '${replacement.java.patchName}' instead")
|
||||||
}
|
}
|
||||||
|
|
||||||
val patchInstance = patch.getDeclaredConstructor().newInstance()
|
val patchInstance = patch.getDeclaredConstructor().newInstance()
|
||||||
|
|
||||||
// if the current patch is a resource patch but resource patching is disabled, return an error
|
val isResourcePatch = ResourcePatch::class.java.isAssignableFrom(patch)
|
||||||
val isResourcePatch = patchInstance is ResourcePatch
|
// TODO: implement this in a more polymorphic way
|
||||||
if (!options.patchResources && isResourcePatch) {
|
|
||||||
return PatchResultError("'$patchName' is a resource patch, but resource patching is disabled")
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: find a solution for this
|
|
||||||
val data = if (isResourcePatch) {
|
val data = if (isResourcePatch) {
|
||||||
data.resourceData
|
data.resourceData
|
||||||
} else {
|
} else {
|
||||||
val bytecodeData = data.bytecodeData
|
data.bytecodeData.also { data ->
|
||||||
(patchInstance as BytecodePatch).fingerprints?.resolve(bytecodeData, bytecodeData.classes.internalClasses)
|
(patchInstance as BytecodePatch).fingerprints?.resolve(
|
||||||
bytecodeData
|
data,
|
||||||
|
data.classes.internalClasses
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.trace("Executing '$patchName' of type: ${if (isResourcePatch) "resource" else "bytecode"}")
|
logger.trace("Executing '$patchName' of type: ${if (isResourcePatch) "resource" else "bytecode"}")
|
||||||
|
|
||||||
return try {
|
return try {
|
||||||
val result = patchInstance.execute(data)
|
patchInstance.execute(data).also {
|
||||||
appliedPatches[patchName] = AppliedPatch(patchInstance, result.isSuccess())
|
appliedPatches[patchName] = AppliedPatch(patchInstance, it.isSuccess())
|
||||||
result
|
}
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
appliedPatches[patchName] = AppliedPatch(patchInstance, false)
|
PatchResultError(e).also {
|
||||||
PatchResultError(e)
|
appliedPatches[patchName] = AppliedPatch(patchInstance, false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Decode resources for the patcher.
|
||||||
|
*
|
||||||
|
* @param mode The [ResourceDecodingMode] to use when decoding.
|
||||||
|
*/
|
||||||
|
private fun decodeResources(mode: ResourceDecodingMode) {
|
||||||
|
val extInputFile = ExtFile(options.inputFile)
|
||||||
|
try {
|
||||||
|
val androlib = Androlib(BuildOptions().also { it.setBuildOptions(options) })
|
||||||
|
val resourceTable = androlib.getResTable(extInputFile, true)
|
||||||
|
when (mode) {
|
||||||
|
ResourceDecodingMode.FULL -> {
|
||||||
|
val outDir = File(options.resourceCacheDirectory)
|
||||||
|
if (outDir.exists()) {
|
||||||
|
logger.info("Deleting existing resource cache directory")
|
||||||
|
if (!outDir.deleteRecursively()) {
|
||||||
|
logger.error("Failed to delete existing resource cache directory")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
outDir.mkdirs()
|
||||||
|
|
||||||
|
logger.info("Decoding resources")
|
||||||
|
|
||||||
|
// decode resources to cache directory
|
||||||
|
androlib.decodeManifestWithResources(extInputFile, outDir, resourceTable)
|
||||||
|
androlib.decodeResourcesFull(extInputFile, outDir, resourceTable)
|
||||||
|
|
||||||
|
// read additional metadata from the resource table
|
||||||
|
data.packageMetadata.let { metadata ->
|
||||||
|
metadata.metaInfo.usesFramework = UsesFramework().also { framework ->
|
||||||
|
framework.ids = resourceTable.listFramePackages().map { it.id }.sorted()
|
||||||
|
}
|
||||||
|
|
||||||
|
// read files to not compress
|
||||||
|
metadata.metaInfo.doNotCompress = buildList {
|
||||||
|
androlib.recordUncompressedFiles(extInputFile, this)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
ResourceDecodingMode.MANIFEST_ONLY -> {
|
||||||
|
logger.info("Decoding AndroidManifest.xml only, because resources are not needed")
|
||||||
|
|
||||||
|
// create decoder for the resource table
|
||||||
|
val decoder = ResAttrDecoder()
|
||||||
|
decoder.currentPackage = ResPackage(resourceTable, 0, null)
|
||||||
|
|
||||||
|
// create xml parser with the decoder
|
||||||
|
val axmlParser = AXmlResourceParser()
|
||||||
|
axmlParser.attrDecoder = decoder
|
||||||
|
|
||||||
|
// parse package information with the decoder and parser which will set required values in the resource table
|
||||||
|
// instead of decodeManifest another more low level solution can be created to make it faster/better
|
||||||
|
XmlPullStreamDecoder(
|
||||||
|
axmlParser, AndrolibResources().resXmlSerializer
|
||||||
|
).decodeManifest(
|
||||||
|
extInputFile.directory.getFileInput("AndroidManifest.xml"), nullOutputStream
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// read of the resourceTable which is created by reading the manifest file
|
||||||
|
data.packageMetadata.let { metadata ->
|
||||||
|
metadata.packageName = resourceTable.currentResPackage.name
|
||||||
|
metadata.packageVersion = resourceTable.versionInfo.versionName
|
||||||
|
metadata.metaInfo.versionInfo = resourceTable.versionInfo
|
||||||
|
metadata.metaInfo.sdkInfo = resourceTable.sdkInfo
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
extInputFile.close()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -315,6 +372,9 @@ class Patcher(private val options: PatcherOptions) {
|
|||||||
* @return A pair of the name of the [Patch] and its [PatchResult].
|
* @return A pair of the name of the [Patch] and its [PatchResult].
|
||||||
*/
|
*/
|
||||||
fun applyPatches(stopOnError: Boolean = false) = sequence {
|
fun applyPatches(stopOnError: Boolean = false) = sequence {
|
||||||
|
// prevent from decoding the manifest twice if it is not needed
|
||||||
|
if (resourceDecodingMode == ResourceDecodingMode.FULL) decodeResources(ResourceDecodingMode.FULL)
|
||||||
|
|
||||||
logger.trace("Applying all patches")
|
logger.trace("Applying all patches")
|
||||||
|
|
||||||
val appliedPatches = LinkedHashMap<String, AppliedPatch>() // first is name
|
val appliedPatches = LinkedHashMap<String, AppliedPatch>() // first is name
|
||||||
@@ -341,6 +401,21 @@ class Patcher(private val options: PatcherOptions) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The type of decoding the resources.
|
||||||
|
*/
|
||||||
|
private enum class ResourceDecodingMode {
|
||||||
|
/**
|
||||||
|
* Decode all resources.
|
||||||
|
*/
|
||||||
|
FULL,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Decode the manifest file only.
|
||||||
|
*/
|
||||||
|
MANIFEST_ONLY,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -350,9 +425,3 @@ class Patcher(private val options: PatcherOptions) {
|
|||||||
* @param success The result of the [Patch].
|
* @param success The result of the [Patch].
|
||||||
*/
|
*/
|
||||||
internal data class AppliedPatch(val patchInstance: Patch<Data>, val success: Boolean)
|
internal data class AppliedPatch(val patchInstance: Patch<Data>, val success: Boolean)
|
||||||
|
|
||||||
private fun BuildOptions.setBuildOptions(options: PatcherOptions) {
|
|
||||||
this.aaptPath = options.aaptPath
|
|
||||||
this.useAapt2 = true
|
|
||||||
this.frameworkFolderLocation = options.frameworkFolderLocation
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -9,10 +9,10 @@ import org.jf.dexlib2.iface.ClassDef
|
|||||||
import java.io.File
|
import java.io.File
|
||||||
|
|
||||||
data class PatcherData(
|
data class PatcherData(
|
||||||
internal val internalClasses: MutableList<ClassDef>,
|
val internalClasses: MutableList<ClassDef>,
|
||||||
internal val resourceCacheDirectory: String,
|
val resourceCacheDirectory: String,
|
||||||
val packageMetadata: PackageMetadata
|
|
||||||
) {
|
) {
|
||||||
|
val packageMetadata = PackageMetadata()
|
||||||
internal val patches = mutableListOf<Class<out Patch<Data>>>()
|
internal val patches = mutableListOf<Class<out Patch<Data>>>()
|
||||||
internal val bytecodeData = BytecodeData(internalClasses)
|
internal val bytecodeData = BytecodeData(internalClasses)
|
||||||
internal val resourceData = ResourceData(File(resourceCacheDirectory))
|
internal val resourceData = ResourceData(File(resourceCacheDirectory))
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ import java.io.File
|
|||||||
* Options for the [Patcher].
|
* Options for the [Patcher].
|
||||||
* @param inputFile The input file (usually an apk file).
|
* @param inputFile The input file (usually an apk file).
|
||||||
* @param resourceCacheDirectory Directory to cache resources.
|
* @param resourceCacheDirectory Directory to cache resources.
|
||||||
* @param patchResources Weather to use the resource patcher. Resources will still need to be decoded.
|
|
||||||
* @param aaptPath Optional path to a custom aapt binary.
|
* @param aaptPath Optional path to a custom aapt binary.
|
||||||
* @param frameworkFolderLocation Optional path to a custom framework folder.
|
* @param frameworkFolderLocation Optional path to a custom framework folder.
|
||||||
* @param logger Custom logger implementation for the [Patcher].
|
* @param logger Custom logger implementation for the [Patcher].
|
||||||
@@ -16,7 +15,6 @@ import java.io.File
|
|||||||
data class PatcherOptions(
|
data class PatcherOptions(
|
||||||
internal val inputFile: File,
|
internal val inputFile: File,
|
||||||
internal val resourceCacheDirectory: String,
|
internal val resourceCacheDirectory: String,
|
||||||
internal val patchResources: Boolean = false,
|
|
||||||
internal val aaptPath: String = "",
|
internal val aaptPath: String = "",
|
||||||
internal val frameworkFolderLocation: String? = null,
|
internal val frameworkFolderLocation: String? = null,
|
||||||
internal val logger: Logger = NopLogger
|
internal val logger: Logger = NopLogger
|
||||||
|
|||||||
@@ -24,5 +24,5 @@ annotation class Compatibility(
|
|||||||
@MustBeDocumented
|
@MustBeDocumented
|
||||||
annotation class Package(
|
annotation class Package(
|
||||||
val name: String,
|
val name: String,
|
||||||
val versions: Array<String>
|
val versions: Array<String> = [],
|
||||||
)
|
)
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
package app.revanced.patcher.annotation
|
||||||
|
|
||||||
|
import app.revanced.patcher.data.Data
|
||||||
|
import app.revanced.patcher.patch.Patch
|
||||||
|
import kotlin.reflect.KClass
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Declares a [Patch] deprecated for removal.
|
||||||
|
* @param reason The reason why the patch is deprecated.
|
||||||
|
* @param replacement The replacement for the deprecated patch, if any.
|
||||||
|
*/
|
||||||
|
@Target(AnnotationTarget.CLASS)
|
||||||
|
@Retention(AnnotationRetention.RUNTIME)
|
||||||
|
@MustBeDocumented
|
||||||
|
annotation class PatchDeprecated(
|
||||||
|
val reason: String,
|
||||||
|
val replacement: KClass<out Patch<Data>> = Patch::class
|
||||||
|
// Values cannot be nullable in annotations, so this will have to do.
|
||||||
|
)
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package app.revanced.patcher.annotation
|
||||||
|
|
||||||
|
import app.revanced.patcher.patch.Patch
|
||||||
|
import app.revanced.patcher.Patcher
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Declares a [Patch] deprecated for removal.
|
||||||
|
* @param version The minimum version of the [Patcher] this [Patch] supports.
|
||||||
|
*/
|
||||||
|
@Target(AnnotationTarget.CLASS)
|
||||||
|
@Retention(AnnotationRetention.RUNTIME)
|
||||||
|
@MustBeDocumented
|
||||||
|
annotation class SincePatcher(val version: String)
|
||||||
@@ -30,10 +30,12 @@ class ResourceData(private val resourceCacheDirectory: File) : Data, Iterable<Fi
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* DomFileEditor is a wrapper for a file that can be edited as a dom document.
|
* Wrapper for a file that can be edited as a dom document.
|
||||||
|
* Note: This constructor does not check for locks to the file when writing. Use the secondary constructor.
|
||||||
*
|
*
|
||||||
* @param inputStream the input stream to read the xml file from.
|
* @param inputStream the input stream to read the xml file from.
|
||||||
* @param outputStream the output stream to write the xml file to. If null, the file will not be written.
|
* @param outputStream the output stream to write the xml file to. If null, the file will be read only.
|
||||||
|
*
|
||||||
*/
|
*/
|
||||||
class DomFileEditor internal constructor(
|
class DomFileEditor internal constructor(
|
||||||
private val inputStream: InputStream,
|
private val inputStream: InputStream,
|
||||||
@@ -41,6 +43,7 @@ class DomFileEditor internal constructor(
|
|||||||
) : Closeable {
|
) : Closeable {
|
||||||
// path to the xml file to unlock the resource when closing the editor
|
// path to the xml file to unlock the resource when closing the editor
|
||||||
private var filePath: String? = null
|
private var filePath: String? = null
|
||||||
|
private var closed: Boolean = false
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The document of the xml file
|
* The document of the xml file
|
||||||
@@ -48,37 +51,53 @@ class DomFileEditor internal constructor(
|
|||||||
val file: Document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(inputStream)
|
val file: Document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(inputStream)
|
||||||
.also(Document::normalize)
|
.also(Document::normalize)
|
||||||
|
|
||||||
|
|
||||||
// lazily open an output stream
|
// lazily open an output stream
|
||||||
// this is required because when constructing a DomFileEditor the output stream is created along with the input stream, which is not allowed
|
// this is required because when constructing a DomFileEditor the output stream is created along with the input stream, which is not allowed
|
||||||
// the workaround is to lazily create the output stream. This way it would be used after the input stream is closed, which happens in the constructor
|
// the workaround is to lazily create the output stream. This way it would be used after the input stream is closed, which happens in the constructor
|
||||||
constructor(file: File) : this(file.inputStream(), lazy { file.outputStream() }) {
|
constructor(file: File) : this(file.inputStream(), lazy { file.outputStream() }) {
|
||||||
|
// increase the lock
|
||||||
|
locks.merge(file.path, 1, Integer::sum)
|
||||||
filePath = file.path
|
filePath = file.path
|
||||||
|
|
||||||
// prevent sharing mutability of the same file between multiple instances of DomFileEditor
|
|
||||||
if (locks.contains(filePath))
|
|
||||||
throw IllegalStateException("Can not create a DomFileEditor for that file because it is already locked by another instance of DomFileEditor.")
|
|
||||||
locks.add(filePath!!)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Closes the editor. Write backs and decreases the lock count.
|
||||||
|
* Note: Will not write back to the file if the file is still locked.
|
||||||
|
*/
|
||||||
override fun close() {
|
override fun close() {
|
||||||
|
if (closed) return
|
||||||
|
|
||||||
inputStream.close()
|
inputStream.close()
|
||||||
|
|
||||||
// if the output stream is not null, do not close it
|
// if the output stream is not null, do not close it
|
||||||
outputStream?.let {
|
outputStream?.let {
|
||||||
val result = StreamResult(it.value)
|
// prevent writing to same file, if it is being locked
|
||||||
TransformerFactory.newInstance().newTransformer().transform(DOMSource(file), result)
|
// isLocked will be false if the editor was created through a stream
|
||||||
|
val isLocked = filePath?.let { path ->
|
||||||
|
val isLocked = locks[path]!! > 1
|
||||||
|
// decrease the lock count if the editor was opened for a file
|
||||||
|
locks.merge(path, -1, Integer::sum)
|
||||||
|
isLocked
|
||||||
|
} ?: false
|
||||||
|
|
||||||
it.value.close()
|
// if unlocked, write back to the file
|
||||||
|
if (!isLocked) {
|
||||||
|
it.value.use { stream ->
|
||||||
|
val result = StreamResult(stream)
|
||||||
|
TransformerFactory.newInstance().newTransformer().transform(DOMSource(file), result)
|
||||||
|
}
|
||||||
|
|
||||||
|
it.value.close()
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// remove the lock, if it exists
|
closed = true
|
||||||
filePath?.let {
|
|
||||||
locks.remove(it)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private companion object {
|
private companion object {
|
||||||
// list of locked file paths
|
// map of concurrent open files
|
||||||
val locks = mutableListOf<String>()
|
val locks = mutableMapOf<String, Int>()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,13 +1,15 @@
|
|||||||
package app.revanced.patcher.extensions
|
package app.revanced.patcher.extensions
|
||||||
|
|
||||||
import app.revanced.patcher.annotation.Compatibility
|
import app.revanced.patcher.annotation.*
|
||||||
import app.revanced.patcher.annotation.Description
|
|
||||||
import app.revanced.patcher.annotation.Name
|
|
||||||
import app.revanced.patcher.annotation.Version
|
|
||||||
import app.revanced.patcher.data.Data
|
import app.revanced.patcher.data.Data
|
||||||
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint
|
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint
|
||||||
|
import app.revanced.patcher.patch.OptionsContainer
|
||||||
import app.revanced.patcher.patch.Patch
|
import app.revanced.patcher.patch.Patch
|
||||||
|
import app.revanced.patcher.patch.PatchOptions
|
||||||
import kotlin.reflect.KClass
|
import kotlin.reflect.KClass
|
||||||
|
import kotlin.reflect.KVisibility
|
||||||
|
import kotlin.reflect.full.companionObject
|
||||||
|
import kotlin.reflect.full.companionObjectInstance
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Recursively find a given annotation on a class.
|
* Recursively find a given annotation on a class.
|
||||||
@@ -36,13 +38,27 @@ private fun <T : Annotation> Class<*>.findAnnotationRecursively(
|
|||||||
}
|
}
|
||||||
|
|
||||||
object PatchExtensions {
|
object PatchExtensions {
|
||||||
val Class<out Patch<Data>>.patchName: String
|
val Class<*>.patchName: String
|
||||||
get() = recursiveAnnotation(Name::class)?.name ?: this.javaClass.simpleName
|
get() = recursiveAnnotation(Name::class)?.name ?: this.javaClass.simpleName
|
||||||
val Class<out Patch<Data>>.version get() = recursiveAnnotation(Version::class)?.version
|
val Class<out Patch<Data>>.version get() = recursiveAnnotation(Version::class)?.version
|
||||||
val Class<out Patch<Data>>.include get() = recursiveAnnotation(app.revanced.patcher.patch.annotations.Patch::class)!!.include
|
val Class<out Patch<Data>>.include get() = recursiveAnnotation(app.revanced.patcher.patch.annotations.Patch::class)!!.include
|
||||||
val Class<out Patch<Data>>.description get() = recursiveAnnotation(Description::class)?.description
|
val Class<out Patch<Data>>.description get() = recursiveAnnotation(Description::class)?.description
|
||||||
val Class<out Patch<Data>>.dependencies get() = recursiveAnnotation(app.revanced.patcher.patch.annotations.DependsOn::class)?.dependencies
|
val Class<out Patch<Data>>.dependencies get() = recursiveAnnotation(app.revanced.patcher.patch.annotations.DependsOn::class)?.dependencies
|
||||||
val Class<out Patch<Data>>.compatiblePackages get() = recursiveAnnotation(Compatibility::class)?.compatiblePackages
|
val Class<out Patch<Data>>.compatiblePackages get() = recursiveAnnotation(Compatibility::class)?.compatiblePackages
|
||||||
|
val Class<out Patch<Data>>.options: PatchOptions?
|
||||||
|
get() = kotlin.companionObject?.let { cl ->
|
||||||
|
if (cl.visibility != KVisibility.PUBLIC) return null
|
||||||
|
kotlin.companionObjectInstance?.let {
|
||||||
|
(it as? OptionsContainer)?.options
|
||||||
|
}
|
||||||
|
}
|
||||||
|
val Class<out Patch<Data>>.deprecated: Pair<String, KClass<out Patch<Data>>?>?
|
||||||
|
get() = recursiveAnnotation(PatchDeprecated::class)?.let {
|
||||||
|
it.reason to it.replacement.let { cl ->
|
||||||
|
if (cl == Patch::class) null else cl
|
||||||
|
}
|
||||||
|
}
|
||||||
|
val Class<out Patch<Data>>.sincePatcherVersion get() = recursiveAnnotation(SincePatcher::class)?.version
|
||||||
|
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun Class<out Patch<Data>>.dependsOn(patch: Class<out Patch<Data>>): Boolean {
|
fun Class<out Patch<Data>>.dependsOn(patch: Class<out Patch<Data>>): Boolean {
|
||||||
|
|||||||
@@ -16,7 +16,6 @@ import org.jf.dexlib2.iface.instruction.Instruction
|
|||||||
import org.jf.dexlib2.iface.reference.MethodReference
|
import org.jf.dexlib2.iface.reference.MethodReference
|
||||||
import org.jf.dexlib2.immutable.ImmutableMethod
|
import org.jf.dexlib2.immutable.ImmutableMethod
|
||||||
import org.jf.dexlib2.immutable.ImmutableMethodImplementation
|
import org.jf.dexlib2.immutable.ImmutableMethodImplementation
|
||||||
import org.jf.dexlib2.util.MethodUtil
|
|
||||||
import java.io.OutputStream
|
import java.io.OutputStream
|
||||||
|
|
||||||
infix fun AccessFlags.or(other: AccessFlags) = this.value or other.value
|
infix fun AccessFlags.or(other: AccessFlags) = this.value or other.value
|
||||||
@@ -47,16 +46,14 @@ fun MutableMethodImplementation.removeInstructions(index: Int, count: Int) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Compare a method to another, considering constructors and parameters.
|
* Compare a method to another, considering name and parameters.
|
||||||
* @param otherMethod The method to compare against.
|
* @param otherMethod The method to compare against.
|
||||||
* @return True if the methods match given the conditions.
|
* @return True if the methods match given the conditions.
|
||||||
*/
|
*/
|
||||||
fun Method.softCompareTo(otherMethod: MethodReference): Boolean {
|
fun Method.softCompareTo(otherMethod: MethodReference): Boolean {
|
||||||
if (MethodUtil.isConstructor(this) && !parametersEqual(
|
return this.name == otherMethod.name && parametersEqual(
|
||||||
this.parameterTypes, otherMethod.parameterTypes
|
this.parameterTypes, otherMethod.parameterTypes
|
||||||
)
|
)
|
||||||
) return false
|
|
||||||
return this.name == otherMethod.name
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -1,15 +1,19 @@
|
|||||||
package app.revanced.patcher.fingerprint.method.impl
|
package app.revanced.patcher.fingerprint.method.impl
|
||||||
|
|
||||||
import app.revanced.patcher.data.impl.BytecodeData
|
import app.revanced.patcher.data.impl.BytecodeData
|
||||||
import app.revanced.patcher.data.impl.MethodNotFoundException
|
import app.revanced.patcher.extensions.MethodFingerprintExtensions.fuzzyPatternScanMethod
|
||||||
import app.revanced.patcher.extensions.MethodFingerprintExtensions.name
|
import app.revanced.patcher.extensions.MethodFingerprintExtensions.fuzzyScanThreshold
|
||||||
|
import app.revanced.patcher.extensions.parametersEqual
|
||||||
import app.revanced.patcher.extensions.softCompareTo
|
import app.revanced.patcher.extensions.softCompareTo
|
||||||
import app.revanced.patcher.fingerprint.Fingerprint
|
import app.revanced.patcher.fingerprint.Fingerprint
|
||||||
import app.revanced.patcher.fingerprint.method.utils.MethodFingerprintUtils
|
import app.revanced.patcher.fingerprint.method.annotation.FuzzyPatternScanMethod
|
||||||
import app.revanced.patcher.util.proxy.ClassProxy
|
import app.revanced.patcher.util.proxy.ClassProxy
|
||||||
import org.jf.dexlib2.Opcode
|
import org.jf.dexlib2.Opcode
|
||||||
import org.jf.dexlib2.iface.ClassDef
|
import org.jf.dexlib2.iface.ClassDef
|
||||||
import org.jf.dexlib2.iface.Method
|
import org.jf.dexlib2.iface.Method
|
||||||
|
import org.jf.dexlib2.iface.instruction.Instruction
|
||||||
|
import org.jf.dexlib2.iface.instruction.ReferenceInstruction
|
||||||
|
import org.jf.dexlib2.iface.reference.StringReference
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents the [MethodFingerprint] for a method.
|
* Represents the [MethodFingerprint] for a method.
|
||||||
@@ -22,40 +26,263 @@ import org.jf.dexlib2.iface.Method
|
|||||||
* A `null` opcode is equals to an unknown opcode.
|
* A `null` opcode is equals to an unknown opcode.
|
||||||
*/
|
*/
|
||||||
abstract class MethodFingerprint(
|
abstract class MethodFingerprint(
|
||||||
internal val returnType: String?,
|
internal val returnType: String? = null,
|
||||||
internal val access: Int?,
|
internal val access: Int? = null,
|
||||||
internal val parameters: Iterable<String>?,
|
internal val parameters: Iterable<String>? = null,
|
||||||
internal val opcodes: Iterable<Opcode?>?,
|
internal val opcodes: Iterable<Opcode?>? = null,
|
||||||
internal val strings: Iterable<String>? = null,
|
internal val strings: Iterable<String>? = null,
|
||||||
internal val customFingerprint: ((methodDef: Method) -> Boolean)? = null
|
internal val customFingerprint: ((methodDef: Method) -> Boolean)? = null
|
||||||
) : Fingerprint {
|
) : Fingerprint {
|
||||||
/**
|
/**
|
||||||
* The result of the [MethodFingerprint] the [Method].
|
* The result of the [MethodFingerprint] the [Method].
|
||||||
* @throws MethodNotFoundException If the resolution of the [Method] has not happened.
|
|
||||||
*/
|
*/
|
||||||
var result: MethodFingerprintResult? = null
|
var result: MethodFingerprintResult? = null
|
||||||
get() = field ?: throw Exception("${this.name} has not been resolved yet.")
|
|
||||||
|
companion object {
|
||||||
|
/**
|
||||||
|
* Resolve a list of [MethodFingerprint] against a list of [ClassDef].
|
||||||
|
* @param context The classes on which to resolve the [MethodFingerprint].
|
||||||
|
* @param forData The [BytecodeData] to host proxies.
|
||||||
|
* @return True if the resolution was successful, false otherwise.
|
||||||
|
*/
|
||||||
|
fun Iterable<MethodFingerprint>.resolve(forData: BytecodeData, context: Iterable<ClassDef>) {
|
||||||
|
for (fingerprint in this) // For each fingerprint
|
||||||
|
classes@ for (classDef in context) // search through all classes for the fingerprint
|
||||||
|
if (fingerprint.resolve(forData, classDef))
|
||||||
|
break@classes // if the resolution succeeded, continue with the next fingerprint
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Resolve a [MethodFingerprint] against a [ClassDef].
|
||||||
|
* @param context The class on which to resolve the [MethodFingerprint].
|
||||||
|
* @param forData The [BytecodeData] to host proxies.
|
||||||
|
* @return True if the resolution was successful, false otherwise.
|
||||||
|
*/
|
||||||
|
fun MethodFingerprint.resolve(forData: BytecodeData, context: ClassDef): Boolean {
|
||||||
|
for (method in context.methods)
|
||||||
|
if (this.resolve(forData, method, context))
|
||||||
|
return true
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Resolve a [MethodFingerprint] against a [Method].
|
||||||
|
* @param context The context on which to resolve the [MethodFingerprint].
|
||||||
|
* @param classDef The class of the matching [Method].
|
||||||
|
* @param forData The [BytecodeData] to host proxies.
|
||||||
|
* @return True if the resolution was successful or if the fingerprint is already resolved, false otherwise.
|
||||||
|
*/
|
||||||
|
fun MethodFingerprint.resolve(forData: BytecodeData, context: Method, classDef: ClassDef): Boolean {
|
||||||
|
val methodFingerprint = this
|
||||||
|
|
||||||
|
if (methodFingerprint.result != null) return true
|
||||||
|
|
||||||
|
if (methodFingerprint.returnType != null && !context.returnType.startsWith(methodFingerprint.returnType))
|
||||||
|
return false
|
||||||
|
|
||||||
|
if (methodFingerprint.access != null && methodFingerprint.access != context.accessFlags)
|
||||||
|
return false
|
||||||
|
|
||||||
|
|
||||||
|
if (methodFingerprint.parameters != null && !parametersEqual(
|
||||||
|
methodFingerprint.parameters, // TODO: parseParameters()
|
||||||
|
context.parameterTypes
|
||||||
|
)
|
||||||
|
) return false
|
||||||
|
|
||||||
|
@Suppress("UNNECESSARY_NOT_NULL_ASSERTION")
|
||||||
|
if (methodFingerprint.customFingerprint != null && !methodFingerprint.customFingerprint!!(context))
|
||||||
|
return false
|
||||||
|
|
||||||
|
val stringsScanResult: StringsScanResult? =
|
||||||
|
if (methodFingerprint.strings != null) {
|
||||||
|
StringsScanResult(
|
||||||
|
buildList {
|
||||||
|
val implementation = context.implementation ?: return false
|
||||||
|
|
||||||
|
val stringsList = methodFingerprint.strings.toMutableList()
|
||||||
|
|
||||||
|
implementation.instructions.forEachIndexed { instructionIndex, instruction ->
|
||||||
|
if (instruction.opcode.ordinal != Opcode.CONST_STRING.ordinal) return@forEachIndexed
|
||||||
|
|
||||||
|
val string = ((instruction as ReferenceInstruction).reference as StringReference).string
|
||||||
|
val index = stringsList.indexOfFirst { it == string }
|
||||||
|
if (index == -1) return@forEachIndexed
|
||||||
|
|
||||||
|
add(
|
||||||
|
StringMatch(
|
||||||
|
string,
|
||||||
|
instructionIndex
|
||||||
|
)
|
||||||
|
)
|
||||||
|
stringsList.removeAt(index)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (stringsList.isNotEmpty()) return false
|
||||||
|
}
|
||||||
|
)
|
||||||
|
} else null
|
||||||
|
|
||||||
|
val patternScanResult = if (methodFingerprint.opcodes != null) {
|
||||||
|
context.implementation?.instructions ?: return false
|
||||||
|
|
||||||
|
context.patternScan(methodFingerprint) ?: return false
|
||||||
|
} else null
|
||||||
|
|
||||||
|
methodFingerprint.result = MethodFingerprintResult(
|
||||||
|
context,
|
||||||
|
classDef,
|
||||||
|
MethodFingerprintResult.MethodFingerprintScanResult(
|
||||||
|
patternScanResult,
|
||||||
|
stringsScanResult
|
||||||
|
),
|
||||||
|
forData
|
||||||
|
)
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun Method.patternScan(
|
||||||
|
fingerprint: MethodFingerprint
|
||||||
|
): MethodFingerprintResult.MethodFingerprintScanResult.PatternScanResult? {
|
||||||
|
val instructions = this.implementation!!.instructions
|
||||||
|
val fingerprintFuzzyPatternScanThreshold = fingerprint.fuzzyScanThreshold
|
||||||
|
|
||||||
|
val pattern = fingerprint.opcodes!!
|
||||||
|
val instructionLength = instructions.count()
|
||||||
|
val patternLength = pattern.count()
|
||||||
|
|
||||||
|
for (index in 0 until instructionLength) {
|
||||||
|
var patternIndex = 0
|
||||||
|
var threshold = fingerprintFuzzyPatternScanThreshold
|
||||||
|
|
||||||
|
while (index + patternIndex < instructionLength) {
|
||||||
|
val originalOpcode = instructions.elementAt(index + patternIndex).opcode
|
||||||
|
val patternOpcode = pattern.elementAt(patternIndex)
|
||||||
|
|
||||||
|
if (patternOpcode != null && patternOpcode.ordinal != originalOpcode.ordinal) {
|
||||||
|
// reaching maximum threshold (0) means,
|
||||||
|
// the pattern does not match to the current instructions
|
||||||
|
if (threshold-- == 0) break
|
||||||
|
}
|
||||||
|
|
||||||
|
if (patternIndex < patternLength - 1) {
|
||||||
|
// if the entire pattern has not been scanned yet
|
||||||
|
// continue the scan
|
||||||
|
patternIndex++
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
// the pattern is valid, generate warnings if fuzzyPatternScanMethod is FuzzyPatternScanMethod
|
||||||
|
val result =
|
||||||
|
MethodFingerprintResult.MethodFingerprintScanResult.PatternScanResult(
|
||||||
|
index,
|
||||||
|
index + patternIndex
|
||||||
|
)
|
||||||
|
if (fingerprint.fuzzyPatternScanMethod !is FuzzyPatternScanMethod) return result
|
||||||
|
result.warnings = result.createWarnings(pattern, instructions)
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun MethodFingerprintResult.MethodFingerprintScanResult.PatternScanResult.createWarnings(
|
||||||
|
pattern: Iterable<Opcode?>, instructions: Iterable<Instruction>
|
||||||
|
) = buildList {
|
||||||
|
for ((patternIndex, instructionIndex) in (this@createWarnings.startIndex until this@createWarnings.endIndex).withIndex()) {
|
||||||
|
val originalOpcode = instructions.elementAt(instructionIndex).opcode
|
||||||
|
val patternOpcode = pattern.elementAt(patternIndex)
|
||||||
|
|
||||||
|
if (patternOpcode == null || patternOpcode.ordinal == originalOpcode.ordinal) continue
|
||||||
|
|
||||||
|
this.add(
|
||||||
|
MethodFingerprintResult.MethodFingerprintScanResult.PatternScanResult.Warning(
|
||||||
|
originalOpcode,
|
||||||
|
patternOpcode,
|
||||||
|
instructionIndex,
|
||||||
|
patternIndex
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private typealias StringMatch = MethodFingerprintResult.MethodFingerprintScanResult.StringsScanResult.StringMatch
|
||||||
|
private typealias StringsScanResult = MethodFingerprintResult.MethodFingerprintScanResult.StringsScanResult
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents the result of a [MethodFingerprintUtils].
|
* Represents the result of a [MethodFingerprintResult].
|
||||||
* @param method The matching method.
|
* @param method The matching method.
|
||||||
* @param classDef The [ClassDef] that contains the matching [method].
|
* @param classDef The [ClassDef] that contains the matching [method].
|
||||||
* @param patternScanResult Opcodes pattern scan result.
|
* @param scanResult The result of scanning for the [MethodFingerprint].
|
||||||
* @param data The [BytecodeData] this [MethodFingerprintResult] is attached to, to create proxies.
|
* @param data The [BytecodeData] this [MethodFingerprintResult] is attached to, to create proxies.
|
||||||
*/
|
*/
|
||||||
data class MethodFingerprintResult(
|
data class MethodFingerprintResult(
|
||||||
val method: Method,
|
val method: Method,
|
||||||
val classDef: ClassDef,
|
val classDef: ClassDef,
|
||||||
val patternScanResult: PatternScanResult?,
|
val scanResult: MethodFingerprintScanResult,
|
||||||
internal val data: BytecodeData
|
internal val data: BytecodeData
|
||||||
) {
|
) {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The result of scanning on the [MethodFingerprint].
|
||||||
|
* @param patternScanResult The result of the pattern scan.
|
||||||
|
* @param stringsScanResult The result of the string scan.
|
||||||
|
*/
|
||||||
|
data class MethodFingerprintScanResult(
|
||||||
|
val patternScanResult: PatternScanResult?,
|
||||||
|
val stringsScanResult: StringsScanResult?
|
||||||
|
) {
|
||||||
|
/**
|
||||||
|
* The result of scanning strings on the [MethodFingerprint].
|
||||||
|
* @param matches The list of strings that were matched.
|
||||||
|
*/
|
||||||
|
data class StringsScanResult(val matches: List<StringMatch>){
|
||||||
|
/**
|
||||||
|
* Represents a match for a string at an index.
|
||||||
|
* @param string The string that was matched.
|
||||||
|
* @param index The index of the string.
|
||||||
|
*/
|
||||||
|
data class StringMatch(val string: String, val index: Int)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The result of a pattern scan.
|
||||||
|
* @param startIndex The start index of the instructions where to which this pattern matches.
|
||||||
|
* @param endIndex The end index of the instructions where to which this pattern matches.
|
||||||
|
* @param warnings A list of warnings considering this [PatternScanResult].
|
||||||
|
*/
|
||||||
|
data class PatternScanResult(
|
||||||
|
val startIndex: Int,
|
||||||
|
val endIndex: Int,
|
||||||
|
var warnings: List<Warning>? = null
|
||||||
|
) {
|
||||||
|
/**
|
||||||
|
* Represents warnings of the pattern scan.
|
||||||
|
* @param correctOpcode The opcode the instruction list has.
|
||||||
|
* @param wrongOpcode The opcode the pattern list of the signature currently has.
|
||||||
|
* @param instructionIndex The index of the opcode relative to the instruction list.
|
||||||
|
* @param patternIndex The index of the opcode relative to the pattern list from the signature.
|
||||||
|
*/
|
||||||
|
data class Warning(
|
||||||
|
val correctOpcode: Opcode,
|
||||||
|
val wrongOpcode: Opcode,
|
||||||
|
val instructionIndex: Int,
|
||||||
|
val patternIndex: Int,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a mutable clone of [classDef]
|
* Returns a mutable clone of [classDef]
|
||||||
*
|
*
|
||||||
* Please note, this method allocates a [ClassProxy].
|
* Please note, this method allocates a [ClassProxy].
|
||||||
* Use [classDef] where possible.
|
* Use [classDef] where possible.
|
||||||
*/
|
*/
|
||||||
|
@Suppress("MemberVisibilityCanBePrivate")
|
||||||
val mutableClass by lazy { data.proxy(classDef).resolve() }
|
val mutableClass by lazy { data.proxy(classDef).resolve() }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -70,29 +297,3 @@ data class MethodFingerprintResult(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* The result of a pattern scan.
|
|
||||||
* @param startIndex The start index of the instructions where to which this pattern matches.
|
|
||||||
* @param endIndex The end index of the instructions where to which this pattern matches.
|
|
||||||
* @param warnings A list of warnings considering this [PatternScanResult].
|
|
||||||
*/
|
|
||||||
data class PatternScanResult(
|
|
||||||
val startIndex: Int,
|
|
||||||
val endIndex: Int,
|
|
||||||
var warnings: List<Warning>? = null
|
|
||||||
) {
|
|
||||||
/**
|
|
||||||
* Represents warnings of the pattern scan.
|
|
||||||
* @param correctOpcode The opcode the instruction list has.
|
|
||||||
* @param wrongOpcode The opcode the pattern list of the signature currently has.
|
|
||||||
* @param instructionIndex The index of the opcode relative to the instruction list.
|
|
||||||
* @param patternIndex The index of the opcode relative to the pattern list from the signature.
|
|
||||||
*/
|
|
||||||
data class Warning(
|
|
||||||
val correctOpcode: Opcode,
|
|
||||||
val wrongOpcode: Opcode,
|
|
||||||
val instructionIndex: Int,
|
|
||||||
val patternIndex: Int,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
@@ -1,158 +0,0 @@
|
|||||||
package app.revanced.patcher.fingerprint.method.utils
|
|
||||||
|
|
||||||
import app.revanced.patcher.data.impl.BytecodeData
|
|
||||||
import app.revanced.patcher.extensions.MethodFingerprintExtensions.fuzzyPatternScanMethod
|
|
||||||
import app.revanced.patcher.extensions.MethodFingerprintExtensions.fuzzyScanThreshold
|
|
||||||
import app.revanced.patcher.extensions.parametersEqual
|
|
||||||
import app.revanced.patcher.fingerprint.method.annotation.FuzzyPatternScanMethod
|
|
||||||
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint
|
|
||||||
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprintResult
|
|
||||||
import app.revanced.patcher.fingerprint.method.impl.PatternScanResult
|
|
||||||
import org.jf.dexlib2.Opcode
|
|
||||||
import org.jf.dexlib2.iface.ClassDef
|
|
||||||
import org.jf.dexlib2.iface.Method
|
|
||||||
import org.jf.dexlib2.iface.instruction.Instruction
|
|
||||||
import org.jf.dexlib2.iface.instruction.ReferenceInstruction
|
|
||||||
import org.jf.dexlib2.iface.reference.StringReference
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Utility class for [MethodFingerprint]
|
|
||||||
*/
|
|
||||||
object MethodFingerprintUtils {
|
|
||||||
/**
|
|
||||||
* Resolve a list of [MethodFingerprint] against a list of [ClassDef].
|
|
||||||
* @param context The classes on which to resolve the [MethodFingerprint].
|
|
||||||
* @param forData The [BytecodeData] to host proxies.
|
|
||||||
* @return True if the resolution was successful, false otherwise.
|
|
||||||
*/
|
|
||||||
fun Iterable<MethodFingerprint>.resolve(forData: BytecodeData, context: Iterable<ClassDef>) {
|
|
||||||
for (fingerprint in this) // For each fingerprint
|
|
||||||
classes@ for (classDef in context) // search through all classes for the fingerprint
|
|
||||||
if (fingerprint.resolve(forData, classDef))
|
|
||||||
break@classes // if the resolution succeeded, continue with the next fingerprint
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Resolve a [MethodFingerprint] against a [ClassDef].
|
|
||||||
* @param context The class on which to resolve the [MethodFingerprint].
|
|
||||||
* @param forData The [BytecodeData] to host proxies.
|
|
||||||
* @return True if the resolution was successful, false otherwise.
|
|
||||||
*/
|
|
||||||
fun MethodFingerprint.resolve(forData: BytecodeData, context: ClassDef): Boolean {
|
|
||||||
for (method in context.methods)
|
|
||||||
if (this.resolve(forData, method, context))
|
|
||||||
return true
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Resolve a [MethodFingerprint] against a [Method].
|
|
||||||
* @param context The context on which to resolve the [MethodFingerprint].
|
|
||||||
* @param classDef The class of the matching [Method].
|
|
||||||
* @param forData The [BytecodeData] to host proxies.
|
|
||||||
* @return True if the resolution was successful, false otherwise.
|
|
||||||
*/
|
|
||||||
fun MethodFingerprint.resolve(forData: BytecodeData, context: Method, classDef: ClassDef): Boolean {
|
|
||||||
val methodFingerprint = this
|
|
||||||
|
|
||||||
if (methodFingerprint.returnType != null && !context.returnType.startsWith(methodFingerprint.returnType))
|
|
||||||
return false
|
|
||||||
|
|
||||||
if (methodFingerprint.access != null && methodFingerprint.access != context.accessFlags)
|
|
||||||
return false
|
|
||||||
|
|
||||||
|
|
||||||
if (methodFingerprint.parameters != null && !parametersEqual(
|
|
||||||
methodFingerprint.parameters, // TODO: parseParameters()
|
|
||||||
context.parameterTypes
|
|
||||||
)
|
|
||||||
) return false
|
|
||||||
|
|
||||||
if (methodFingerprint.customFingerprint != null && !methodFingerprint.customFingerprint!!(context))
|
|
||||||
return false
|
|
||||||
|
|
||||||
if (methodFingerprint.strings != null) {
|
|
||||||
val implementation = context.implementation ?: return false
|
|
||||||
|
|
||||||
val stringsList = methodFingerprint.strings.toMutableList()
|
|
||||||
|
|
||||||
implementation.instructions.forEach { instruction ->
|
|
||||||
if (instruction.opcode.ordinal != Opcode.CONST_STRING.ordinal) return@forEach
|
|
||||||
|
|
||||||
val string = ((instruction as ReferenceInstruction).reference as StringReference).string
|
|
||||||
val index = stringsList.indexOfFirst { it == string }
|
|
||||||
if (index != -1) stringsList.removeAt(index)
|
|
||||||
}
|
|
||||||
|
|
||||||
if (stringsList.isNotEmpty()) return false
|
|
||||||
}
|
|
||||||
|
|
||||||
val patternScanResult = if (methodFingerprint.opcodes != null) {
|
|
||||||
context.implementation?.instructions ?: return false
|
|
||||||
|
|
||||||
context.patternScan(methodFingerprint) ?: return false
|
|
||||||
} else null
|
|
||||||
|
|
||||||
methodFingerprint.result = MethodFingerprintResult(context, classDef, patternScanResult, forData)
|
|
||||||
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun Method.patternScan(
|
|
||||||
fingerprint: MethodFingerprint
|
|
||||||
): PatternScanResult? {
|
|
||||||
val instructions = this.implementation!!.instructions
|
|
||||||
val fingerprintFuzzyPatternScanThreshold = fingerprint.fuzzyScanThreshold
|
|
||||||
|
|
||||||
val pattern = fingerprint.opcodes!!
|
|
||||||
val instructionLength = instructions.count()
|
|
||||||
val patternLength = pattern.count()
|
|
||||||
|
|
||||||
for (index in 0 until instructionLength) {
|
|
||||||
var patternIndex = 0
|
|
||||||
var threshold = fingerprintFuzzyPatternScanThreshold
|
|
||||||
|
|
||||||
while (index + patternIndex < instructionLength) {
|
|
||||||
val originalOpcode = instructions.elementAt(index + patternIndex).opcode
|
|
||||||
val patternOpcode = pattern.elementAt(patternIndex)
|
|
||||||
|
|
||||||
if (patternOpcode != null && patternOpcode.ordinal != originalOpcode.ordinal) {
|
|
||||||
// reaching maximum threshold (0) means,
|
|
||||||
// the pattern does not match to the current instructions
|
|
||||||
if (threshold-- == 0) break
|
|
||||||
}
|
|
||||||
|
|
||||||
if (patternIndex < patternLength - 1) {
|
|
||||||
// if the entire pattern has not been scanned yet
|
|
||||||
// continue the scan
|
|
||||||
patternIndex++
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
// the pattern is valid, generate warnings if fuzzyPatternScanMethod is FuzzyPatternScanMethod
|
|
||||||
val result = PatternScanResult(index, index + patternIndex)
|
|
||||||
if (fingerprint.fuzzyPatternScanMethod !is FuzzyPatternScanMethod) return result
|
|
||||||
result.warnings = result.createWarnings(pattern, instructions)
|
|
||||||
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun PatternScanResult.createWarnings(
|
|
||||||
pattern: Iterable<Opcode?>, instructions: Iterable<Instruction>
|
|
||||||
) = buildList {
|
|
||||||
for ((patternIndex, instructionIndex) in (this@createWarnings.startIndex until this@createWarnings.endIndex).withIndex()) {
|
|
||||||
val originalOpcode = instructions.elementAt(instructionIndex).opcode
|
|
||||||
val patternOpcode = pattern.elementAt(patternIndex)
|
|
||||||
|
|
||||||
if (patternOpcode == null || patternOpcode.ordinal == originalOpcode.ordinal) continue
|
|
||||||
|
|
||||||
this.add(PatternScanResult.Warning(originalOpcode, patternOpcode, instructionIndex, patternIndex))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private operator fun ClassDef.component1() = this
|
|
||||||
private operator fun ClassDef.component2() = this.methods
|
|
||||||
@@ -17,9 +17,18 @@ abstract class Patch<out T : Data> {
|
|||||||
* The main function of the [Patch] which the patcher will call.
|
* The main function of the [Patch] which the patcher will call.
|
||||||
*/
|
*/
|
||||||
abstract fun execute(data: @UnsafeVariance T): PatchResult
|
abstract fun execute(data: @UnsafeVariance T): PatchResult
|
||||||
|
}
|
||||||
|
|
||||||
|
abstract class OptionsContainer {
|
||||||
/**
|
/**
|
||||||
* A list of [PatchOption]s.
|
* A list of [PatchOption]s.
|
||||||
|
* @see PatchOptions
|
||||||
*/
|
*/
|
||||||
open val options = PatchOptions()
|
@Suppress("MemberVisibilityCanBePrivate")
|
||||||
|
val options = PatchOptions()
|
||||||
|
|
||||||
|
protected fun <T> option(opt: PatchOption<T>): PatchOption<T> {
|
||||||
|
options.register(opt)
|
||||||
|
return opt
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
package app.revanced.patcher.patch
|
package app.revanced.patcher.patch
|
||||||
|
|
||||||
|
import java.nio.file.Path
|
||||||
|
import kotlin.io.path.pathString
|
||||||
import kotlin.reflect.KProperty
|
import kotlin.reflect.KProperty
|
||||||
|
|
||||||
class NoSuchOptionException(val option: String) : Exception("No such option: $option")
|
class NoSuchOptionException(val option: String) : Exception("No such option: $option")
|
||||||
@@ -9,28 +11,46 @@ class IllegalValueException(val value: Any?) : Exception("Illegal value: $value"
|
|||||||
class InvalidTypeException(val got: String, val expected: String) :
|
class InvalidTypeException(val got: String, val expected: String) :
|
||||||
Exception("Invalid option value type: $got, expected $expected")
|
Exception("Invalid option value type: $got, expected $expected")
|
||||||
|
|
||||||
class RequirementNotMetException : Exception("null was passed into an option that requires a value")
|
object RequirementNotMetException : Exception("null was passed into an option that requires a value")
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A registry for an array of [PatchOption]s.
|
* A registry for an array of [PatchOption]s.
|
||||||
* @param options An array of [PatchOption]s.
|
* @param options An array of [PatchOption]s.
|
||||||
*/
|
*/
|
||||||
class PatchOptions(vararg val options: PatchOption<*>) : Iterable<PatchOption<*>> {
|
class PatchOptions(vararg options: PatchOption<*>) : Iterable<PatchOption<*>> {
|
||||||
private val register = buildMap {
|
private val register = mutableMapOf<String, PatchOption<*>>()
|
||||||
for (option in options) {
|
|
||||||
if (containsKey(option.key)) {
|
init {
|
||||||
throw IllegalStateException("Multiple options found with the same key")
|
options.forEach { register(it) }
|
||||||
}
|
}
|
||||||
put(option.key, option)
|
|
||||||
|
internal fun register(option: PatchOption<*>) {
|
||||||
|
if (register.containsKey(option.key)) {
|
||||||
|
throw IllegalStateException("Multiple options found with the same key")
|
||||||
}
|
}
|
||||||
|
register[option.key] = option
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get a [PatchOption] by its key.
|
* Get a [PatchOption] by its key.
|
||||||
* @param key The key of the [PatchOption].
|
* @param key The key of the [PatchOption].
|
||||||
*/
|
*/
|
||||||
|
@JvmName("getUntyped")
|
||||||
operator fun get(key: String) = register[key] ?: throw NoSuchOptionException(key)
|
operator fun get(key: String) = register[key] ?: throw NoSuchOptionException(key)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a [PatchOption] by its key.
|
||||||
|
* @param key The key of the [PatchOption].
|
||||||
|
*/
|
||||||
|
inline operator fun <reified T> get(key: String): PatchOption<T> {
|
||||||
|
val opt = get(key)
|
||||||
|
if (opt.value !is T) throw InvalidTypeException(
|
||||||
|
opt.value?.let { it::class.java.canonicalName } ?: "null",
|
||||||
|
T::class.java.canonicalName
|
||||||
|
)
|
||||||
|
return opt as PatchOption<T>
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the value of a [PatchOption].
|
* Set the value of a [PatchOption].
|
||||||
* @param key The key of the [PatchOption].
|
* @param key The key of the [PatchOption].
|
||||||
@@ -38,7 +58,7 @@ class PatchOptions(vararg val options: PatchOption<*>) : Iterable<PatchOption<*>
|
|||||||
* Please note that using the wrong value type results in a runtime error.
|
* Please note that using the wrong value type results in a runtime error.
|
||||||
*/
|
*/
|
||||||
inline operator fun <reified T> set(key: String, value: T) {
|
inline operator fun <reified T> set(key: String, value: T) {
|
||||||
@Suppress("UNCHECKED_CAST") val opt = get(key) as PatchOption<T>
|
val opt = get<T>(key)
|
||||||
if (opt.value !is T) throw InvalidTypeException(
|
if (opt.value !is T) throw InvalidTypeException(
|
||||||
T::class.java.canonicalName,
|
T::class.java.canonicalName,
|
||||||
opt.value?.let { it::class.java.canonicalName } ?: "null"
|
opt.value?.let { it::class.java.canonicalName } ?: "null"
|
||||||
@@ -54,7 +74,7 @@ class PatchOptions(vararg val options: PatchOption<*>) : Iterable<PatchOption<*>
|
|||||||
get(key).value = null
|
get(key).value = null
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun iterator() = options.iterator()
|
override fun iterator() = register.values.iterator()
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -75,9 +95,15 @@ sealed class PatchOption<T>(
|
|||||||
val validator: (T?) -> Boolean
|
val validator: (T?) -> Boolean
|
||||||
) {
|
) {
|
||||||
var value: T? = default
|
var value: T? = default
|
||||||
|
get() {
|
||||||
|
if (field == null && required) {
|
||||||
|
throw RequirementNotMetException
|
||||||
|
}
|
||||||
|
return field
|
||||||
|
}
|
||||||
set(value) {
|
set(value) {
|
||||||
if (value == null && required) {
|
if (value == null && required) {
|
||||||
throw RequirementNotMetException()
|
throw RequirementNotMetException
|
||||||
}
|
}
|
||||||
if (!validator(value)) {
|
if (!validator(value)) {
|
||||||
throw IllegalValueException(value)
|
throw IllegalValueException(value)
|
||||||
@@ -89,13 +115,23 @@ sealed class PatchOption<T>(
|
|||||||
* Gets the value of the option.
|
* Gets the value of the option.
|
||||||
* Please note that using the wrong value type results in a runtime error.
|
* Please note that using the wrong value type results in a runtime error.
|
||||||
*/
|
*/
|
||||||
operator fun <T> getValue(thisRef: Nothing?, property: KProperty<*>) = value as T
|
@JvmName("getValueTyped")
|
||||||
|
inline operator fun <reified V> getValue(thisRef: Nothing?, property: KProperty<*>): V? {
|
||||||
|
if (value !is V?) throw InvalidTypeException(
|
||||||
|
V::class.java.canonicalName,
|
||||||
|
value?.let { it::class.java.canonicalName } ?: "null"
|
||||||
|
)
|
||||||
|
return value as? V?
|
||||||
|
}
|
||||||
|
|
||||||
|
operator fun getValue(thisRef: Any?, property: KProperty<*>) = value
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the value of the option.
|
* Gets the value of the option.
|
||||||
* Please note that using the wrong value type results in a runtime error.
|
* Please note that using the wrong value type results in a runtime error.
|
||||||
*/
|
*/
|
||||||
inline operator fun <reified V> setValue(thisRef: Any?, property: KProperty<*>, new: V) {
|
@JvmName("setValueTyped")
|
||||||
|
inline operator fun <reified V> setValue(thisRef: Nothing?, property: KProperty<*>, new: V) {
|
||||||
if (value !is V) throw InvalidTypeException(
|
if (value !is V) throw InvalidTypeException(
|
||||||
V::class.java.canonicalName,
|
V::class.java.canonicalName,
|
||||||
value?.let { it::class.java.canonicalName } ?: "null"
|
value?.let { it::class.java.canonicalName } ?: "null"
|
||||||
@@ -103,6 +139,10 @@ sealed class PatchOption<T>(
|
|||||||
value = new as T
|
value = new as T
|
||||||
}
|
}
|
||||||
|
|
||||||
|
operator fun setValue(thisRef: Any?, property: KProperty<*>, new: T?) {
|
||||||
|
value = new
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A [PatchOption] representing a [String].
|
* A [PatchOption] representing a [String].
|
||||||
* @see PatchOption
|
* @see PatchOption
|
||||||
@@ -152,8 +192,8 @@ sealed class PatchOption<T>(
|
|||||||
}
|
}
|
||||||
) {
|
) {
|
||||||
init {
|
init {
|
||||||
if (default !in options) {
|
if (default != null && default !in options) {
|
||||||
throw IllegalStateException("Default option must be an allowed options")
|
throw IllegalStateException("Default option must be an allowed option")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -189,4 +229,20 @@ sealed class PatchOption<T>(
|
|||||||
) : ListOption<Int>(
|
) : ListOption<Int>(
|
||||||
key, default, options, title, description, required, validator
|
key, default, options, title, description, required, validator
|
||||||
)
|
)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A [PatchOption] representing a [Path], backed by a [String].
|
||||||
|
* The validator passes a [String], if you need a [Path] you will have to convert it yourself.
|
||||||
|
* @see PatchOption
|
||||||
|
*/
|
||||||
|
class PathOption(
|
||||||
|
key: String,
|
||||||
|
default: Path?,
|
||||||
|
title: String,
|
||||||
|
description: String,
|
||||||
|
required: Boolean = false,
|
||||||
|
validator: (String?) -> Boolean = { true }
|
||||||
|
) : PatchOption<String>(
|
||||||
|
key, default?.pathString, title, description, required, validator
|
||||||
|
)
|
||||||
}
|
}
|
||||||
18
src/main/kotlin/app/revanced/patcher/util/VersionReader.kt
Normal file
18
src/main/kotlin/app/revanced/patcher/util/VersionReader.kt
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
package app.revanced.patcher.util
|
||||||
|
|
||||||
|
import java.util.*
|
||||||
|
|
||||||
|
internal object VersionReader {
|
||||||
|
@JvmStatic
|
||||||
|
private val props = Properties().apply {
|
||||||
|
load(
|
||||||
|
VersionReader::class.java.getResourceAsStream("/revanced-patcher/version.properties")
|
||||||
|
?: throw IllegalStateException("Could not load version.properties")
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@JvmStatic
|
||||||
|
fun read(): String {
|
||||||
|
return props.getProperty("version") ?: throw IllegalStateException("Version not found")
|
||||||
|
}
|
||||||
|
}
|
||||||
1
src/main/resources/revanced-patcher/version.properties
Normal file
1
src/main/resources/revanced-patcher/version.properties
Normal file
@@ -0,0 +1 @@
|
|||||||
|
version=${projectVersion}
|
||||||
18
src/test/kotlin/app/revanced/patcher/issues/Issue98.kt
Normal file
18
src/test/kotlin/app/revanced/patcher/issues/Issue98.kt
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
package app.revanced.patcher.issues
|
||||||
|
|
||||||
|
import app.revanced.patcher.patch.PatchOption
|
||||||
|
import org.junit.jupiter.api.Test
|
||||||
|
import kotlin.test.assertNull
|
||||||
|
|
||||||
|
internal class Issue98 {
|
||||||
|
companion object {
|
||||||
|
var key1: String? by PatchOption.StringOption(
|
||||||
|
"key1", null, "title", "description"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `should infer nullable type correctly`() {
|
||||||
|
assertNull(key1)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,10 +3,12 @@ package app.revanced.patcher.patch
|
|||||||
import app.revanced.patcher.usage.bytecode.ExampleBytecodePatch
|
import app.revanced.patcher.usage.bytecode.ExampleBytecodePatch
|
||||||
import org.junit.jupiter.api.Test
|
import org.junit.jupiter.api.Test
|
||||||
import org.junit.jupiter.api.assertThrows
|
import org.junit.jupiter.api.assertThrows
|
||||||
|
import kotlin.io.path.Path
|
||||||
|
import kotlin.io.path.pathString
|
||||||
import kotlin.test.assertNotEquals
|
import kotlin.test.assertNotEquals
|
||||||
|
|
||||||
internal class PatchOptionsTest {
|
internal class PatchOptionsTest {
|
||||||
private val options = ExampleBytecodePatch().options
|
private val options = ExampleBytecodePatch.options
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `should not throw an exception`() {
|
fun `should not throw an exception`() {
|
||||||
@@ -15,24 +17,33 @@ internal class PatchOptionsTest {
|
|||||||
is PatchOption.StringOption -> {
|
is PatchOption.StringOption -> {
|
||||||
option.value = "Hello World"
|
option.value = "Hello World"
|
||||||
}
|
}
|
||||||
|
|
||||||
is PatchOption.BooleanOption -> {
|
is PatchOption.BooleanOption -> {
|
||||||
option.value = false
|
option.value = false
|
||||||
}
|
}
|
||||||
|
|
||||||
is PatchOption.StringListOption -> {
|
is PatchOption.StringListOption -> {
|
||||||
option.value = option.options.first()
|
option.value = option.options.first()
|
||||||
for (choice in option.options) {
|
for (choice in option.options) {
|
||||||
println(choice)
|
println(choice)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
is PatchOption.IntListOption -> {
|
is PatchOption.IntListOption -> {
|
||||||
option.value = option.options.first()
|
option.value = option.options.first()
|
||||||
for (choice in option.options) {
|
for (choice in option.options) {
|
||||||
println(choice)
|
println(choice)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
is PatchOption.PathOption -> {
|
||||||
|
option.value = Path("test.txt").pathString
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
val option = options["key1"]
|
val option = options.get<String>("key1")
|
||||||
|
// or: val option: String? by options["key1"]
|
||||||
|
// then you won't need `.value` every time
|
||||||
println(option.value)
|
println(option.value)
|
||||||
options["key1"] = "Hello, world!"
|
options["key1"] = "Hello, world!"
|
||||||
println(option.value)
|
println(option.value)
|
||||||
@@ -40,7 +51,7 @@ internal class PatchOptionsTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `should return a different value when changed`() {
|
fun `should return a different value when changed`() {
|
||||||
var value: String by options["key1"]
|
var value: String? by options["key1"]
|
||||||
val current = value + "" // force a copy
|
val current = value + "" // force a copy
|
||||||
value = "Hello, world!"
|
value = "Hello, world!"
|
||||||
assertNotEquals(current, value)
|
assertNotEquals(current, value)
|
||||||
@@ -52,6 +63,9 @@ internal class PatchOptionsTest {
|
|||||||
// > options["key2"] = null
|
// > options["key2"] = null
|
||||||
// is not possible because Kotlin
|
// is not possible because Kotlin
|
||||||
// cannot reify the type "Nothing?".
|
// cannot reify the type "Nothing?".
|
||||||
|
// So we have to do this instead:
|
||||||
|
options["key2"] = null as Any?
|
||||||
|
// This is a cleaner replacement for the above:
|
||||||
options.nullify("key2")
|
options.nullify("key2")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -63,12 +77,19 @@ internal class PatchOptionsTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `should fail because of invalid value type`() {
|
fun `should fail because of invalid value type when setting an option`() {
|
||||||
assertThrows<InvalidTypeException> {
|
assertThrows<InvalidTypeException> {
|
||||||
options["key1"] = 123
|
options["key1"] = 123
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `should fail because of invalid value type when getting an option`() {
|
||||||
|
assertThrows<InvalidTypeException> {
|
||||||
|
options.get<Int>("key1")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `should fail because of an illegal value`() {
|
fun `should fail because of an illegal value`() {
|
||||||
assertThrows<IllegalValueException> {
|
assertThrows<IllegalValueException> {
|
||||||
@@ -77,9 +98,16 @@ internal class PatchOptionsTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `should fail because of the requirement is not met`() {
|
fun `should fail because the requirement is not met`() {
|
||||||
assertThrows<RequirementNotMetException> {
|
assertThrows<RequirementNotMetException> {
|
||||||
options.nullify("key1")
|
options.nullify("key1")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `should fail because getting a non-initialized option is illegal`() {
|
||||||
|
assertThrows<RequirementNotMetException> {
|
||||||
|
println(options["key5"].value)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -7,8 +7,8 @@ import app.revanced.patcher.data.impl.BytecodeData
|
|||||||
import app.revanced.patcher.extensions.addInstructions
|
import app.revanced.patcher.extensions.addInstructions
|
||||||
import app.revanced.patcher.extensions.or
|
import app.revanced.patcher.extensions.or
|
||||||
import app.revanced.patcher.extensions.replaceInstruction
|
import app.revanced.patcher.extensions.replaceInstruction
|
||||||
|
import app.revanced.patcher.patch.OptionsContainer
|
||||||
import app.revanced.patcher.patch.PatchOption
|
import app.revanced.patcher.patch.PatchOption
|
||||||
import app.revanced.patcher.patch.PatchOptions
|
|
||||||
import app.revanced.patcher.patch.PatchResult
|
import app.revanced.patcher.patch.PatchResult
|
||||||
import app.revanced.patcher.patch.PatchResultSuccess
|
import app.revanced.patcher.patch.PatchResultSuccess
|
||||||
import app.revanced.patcher.patch.annotations.DependsOn
|
import app.revanced.patcher.patch.annotations.DependsOn
|
||||||
@@ -32,6 +32,7 @@ import org.jf.dexlib2.immutable.reference.ImmutableFieldReference
|
|||||||
import org.jf.dexlib2.immutable.reference.ImmutableStringReference
|
import org.jf.dexlib2.immutable.reference.ImmutableStringReference
|
||||||
import org.jf.dexlib2.immutable.value.ImmutableFieldEncodedValue
|
import org.jf.dexlib2.immutable.value.ImmutableFieldEncodedValue
|
||||||
import org.jf.dexlib2.util.Preconditions
|
import org.jf.dexlib2.util.Preconditions
|
||||||
|
import kotlin.io.path.Path
|
||||||
|
|
||||||
@Patch
|
@Patch
|
||||||
@Name("example-bytecode-patch")
|
@Name("example-bytecode-patch")
|
||||||
@@ -46,6 +47,10 @@ class ExampleBytecodePatch : BytecodePatch(listOf(ExampleFingerprint)) {
|
|||||||
// Get the resolved method by its fingerprint from the resolver cache
|
// Get the resolved method by its fingerprint from the resolver cache
|
||||||
val result = ExampleFingerprint.result!!
|
val result = ExampleFingerprint.result!!
|
||||||
|
|
||||||
|
// Patch options
|
||||||
|
println(key1)
|
||||||
|
key2 = false
|
||||||
|
|
||||||
// Get the implementation for the resolved method
|
// Get the implementation for the resolved method
|
||||||
val method = result.mutableMethod
|
val method = result.mutableMethod
|
||||||
val implementation = method.implementation!!
|
val implementation = method.implementation!!
|
||||||
@@ -53,7 +58,7 @@ class ExampleBytecodePatch : BytecodePatch(listOf(ExampleFingerprint)) {
|
|||||||
// Let's modify it, so it prints "Hello, ReVanced! Editing bytecode."
|
// Let's modify it, so it prints "Hello, ReVanced! Editing bytecode."
|
||||||
// Get the start index of our opcode pattern.
|
// Get the start index of our opcode pattern.
|
||||||
// This will be the index of the instruction with the opcode CONST_STRING.
|
// This will be the index of the instruction with the opcode CONST_STRING.
|
||||||
val startIndex = result.patternScanResult!!.startIndex
|
val startIndex = result.scanResult.patternScanResult!!.startIndex
|
||||||
|
|
||||||
implementation.replaceStringAt(startIndex, "Hello, ReVanced! Editing bytecode.")
|
implementation.replaceStringAt(startIndex, "Hello, ReVanced! Editing bytecode.")
|
||||||
|
|
||||||
@@ -164,18 +169,36 @@ class ExampleBytecodePatch : BytecodePatch(listOf(ExampleFingerprint)) {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
override val options = PatchOptions(
|
companion object : OptionsContainer() {
|
||||||
PatchOption.StringOption(
|
private var key1 by option(
|
||||||
"key1", "default", "title", "description", true
|
PatchOption.StringOption(
|
||||||
),
|
"key1", "default", "title", "description", true
|
||||||
PatchOption.BooleanOption(
|
)
|
||||||
"key2", true, "title", "description" // required defaults to false
|
)
|
||||||
),
|
private var key2 by option(
|
||||||
PatchOption.StringListOption(
|
PatchOption.BooleanOption(
|
||||||
"key3", "TEST", listOf("TEST", "TEST1", "TEST2"), "title", "description"
|
"key2", true, "title", "description" // required defaults to false
|
||||||
),
|
)
|
||||||
PatchOption.IntListOption(
|
)
|
||||||
"key4", 1, listOf(1, 2, 3), "title", "description"
|
private var key3 by option(
|
||||||
),
|
PatchOption.StringListOption(
|
||||||
)
|
"key3", "TEST", listOf("TEST", "TEST1", "TEST2"), "title", "description"
|
||||||
|
)
|
||||||
|
)
|
||||||
|
private var key4 by option(
|
||||||
|
PatchOption.IntListOption(
|
||||||
|
"key4", 1, listOf(1, 2, 3), "title", "description"
|
||||||
|
)
|
||||||
|
)
|
||||||
|
private var key5 by option(
|
||||||
|
PatchOption.StringOption(
|
||||||
|
"key5", null, "title", "description", true
|
||||||
|
)
|
||||||
|
)
|
||||||
|
private var key6 by option(
|
||||||
|
PatchOption.PathOption(
|
||||||
|
"key6", Path("test.txt"), "title", "description", true
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,20 @@
|
|||||||
|
package app.revanced.patcher.util
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Assertions.*
|
||||||
|
|
||||||
|
internal class VersionReaderTest {
|
||||||
|
@Test
|
||||||
|
fun read() {
|
||||||
|
val version = VersionReader.read()
|
||||||
|
assertNotNull(version)
|
||||||
|
assertTrue(version.isNotEmpty())
|
||||||
|
val parts = version.split(".")
|
||||||
|
assertEquals(3, parts.size)
|
||||||
|
parts.forEach {
|
||||||
|
assertTrue(it.toInt() >= 0)
|
||||||
|
}
|
||||||
|
println(version)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user