From a1b6b06bd330510d74dea6bb27d5f049ae72058b Mon Sep 17 00:00:00 2001 From: Lucaskyy Date: Thu, 31 Mar 2022 23:22:57 +0200 Subject: [PATCH] test: use findClass with className & cleanup --- src/test/kotlin/patcher/PatcherTest.kt | 84 +++++++++++++------------- 1 file changed, 41 insertions(+), 43 deletions(-) diff --git a/src/test/kotlin/patcher/PatcherTest.kt b/src/test/kotlin/patcher/PatcherTest.kt index 663e3d8d..2846b8c2 100644 --- a/src/test/kotlin/patcher/PatcherTest.kt +++ b/src/test/kotlin/patcher/PatcherTest.kt @@ -36,57 +36,55 @@ fun main() { signatures ) - val mainMethodPatchViaClassProxy = object : Patch("main-method-patch-via-proxy") { - override fun execute(cache: Cache): PatchResult { - val proxy = cache.findClass { classDef -> - classDef.type.contains("XAdRemover") - } ?: return PatchResultError("Class 'XAdRemover' could not be found") + patcher.addPatches( + object : Patch("main-method-patch-via-proxy") { + override fun execute(cache: Cache): PatchResult { + val proxy = cache.findClass("XAdRemover") + ?: return PatchResultError("Class 'XAdRemover' could not be found") - val xAdRemoverClass = proxy.resolve() - val hideReelMethod = xAdRemoverClass.methods.single { method -> method.name.contains("HideReel") } + val xAdRemoverClass = proxy.resolve() + val hideReelMethod = xAdRemoverClass.methods.single { method -> method.name.contains("HideReel") } - val readSettingsMethodRef = ImmutableMethodReference( - "Lfi/razerman/youtube/XGlobals;", - "ReadSettings", - emptyList(), - "V" - ) - - val instructions = hideReelMethod.implementation!!.instructions + val readSettingsMethodRef = ImmutableMethodReference( + "Lfi/razerman/youtube/XGlobals;", + "ReadSettings", + emptyList(), + "V" + ) - val readSettingsInstruction = BuilderInstruction35c( - Opcode.INVOKE_STATIC, - 0, - 0, - 0, - 0, - 0, - 0, - readSettingsMethodRef - ) + val instructions = hideReelMethod.implementation!!.instructions - // TODO: figure out control flow - // otherwise the we would still jump over to the original instruction at index 21 instead to our new one - instructions.add( - 21, - readSettingsInstruction - ) - return PatchResultSuccess() - } - } + val readSettingsInstruction = BuilderInstruction35c( + Opcode.INVOKE_STATIC, + 0, + 0, + 0, + 0, + 0, + 0, + readSettingsMethodRef + ) - val mainMethodPatchViaSignature = object : Patch("main-method-patch-via-signature") { - override fun execute(cache: Cache): PatchResult { - val mainMethodMap = cache.resolvedMethods["main-method"] - mainMethodMap.definingClassProxy.immutableClass.methods.single { method -> - method.name == mainMethodMap.resolvedMethodName + // TODO: figure out control flow + // otherwise the we would still jump over to the original instruction at index 21 instead to our new one + instructions.add( + 21, + readSettingsInstruction + ) + return PatchResultSuccess() + } + }, + object : Patch("main-method-patch-via-signature") { + override fun execute(cache: Cache): PatchResult { + val mainMethodMap = cache.resolvedMethods["main-method"] + mainMethodMap.definingClassProxy.immutableClass.methods.single { method -> + method.name == mainMethodMap.resolvedMethodName + } + return PatchResultSuccess() } - - return PatchResultSuccess() } - } + ) - patcher.addPatches(mainMethodPatchViaClassProxy, mainMethodPatchViaSignature) patcher.applyPatches() patcher.save() }