forked from ReVanced/revanced-patcher
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: oSumAtrIX <johan.melkonyan1@web.de>
- Loading branch information
Showing
17 changed files
with
304 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package app.revanced.patcher.data | ||
|
||
import app.revanced.patcher.data.base.Data | ||
import app.revanced.patcher.data.implementation.BytecodeData | ||
import app.revanced.patcher.data.implementation.ResourceData | ||
import app.revanced.patcher.patch.base.Patch | ||
import org.jf.dexlib2.iface.ClassDef | ||
import java.io.File | ||
|
||
internal data class PatcherData( | ||
val internalClasses: MutableList<ClassDef>, | ||
val resourceCacheDirectory: String | ||
) { | ||
internal val patches = mutableListOf<Patch<Data>>() | ||
|
||
internal val bytecodeData = BytecodeData(patches, internalClasses) | ||
internal val resourceData = ResourceData(File(resourceCacheDirectory)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package app.revanced.patcher.data.base | ||
|
||
import app.revanced.patcher.data.implementation.BytecodeData | ||
import app.revanced.patcher.data.implementation.ResourceData | ||
|
||
/** | ||
* Constraint interface for [BytecodeData] and [ResourceData] | ||
*/ | ||
interface Data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/main/kotlin/app/revanced/patcher/data/implementation/ResourceData.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package app.revanced.patcher.data.implementation | ||
|
||
import app.revanced.patcher.data.base.Data | ||
import org.w3c.dom.Document | ||
import java.io.Closeable | ||
import java.io.File | ||
import javax.xml.XMLConstants | ||
import javax.xml.parsers.DocumentBuilderFactory | ||
import javax.xml.transform.TransformerFactory | ||
import javax.xml.transform.dom.DOMSource | ||
import javax.xml.transform.stream.StreamResult | ||
|
||
class ResourceData(private val resourceCacheDirectory: File) : Data { | ||
private fun resolve(path: String) = resourceCacheDirectory.resolve(path) | ||
|
||
fun forEach(action: (File) -> Unit) = resourceCacheDirectory.walkTopDown().forEach(action) | ||
fun reader(path: String) = resolve(path).reader() | ||
fun writer(path: String) = resolve(path).writer() | ||
|
||
fun replace(path: String, oldValue: String, newValue: String, oldValueIsRegex: Boolean = false) { | ||
// TODO: buffer this somehow | ||
val content = resolve(path).readText() | ||
|
||
if (oldValueIsRegex) { | ||
content.replace(Regex(oldValue), newValue) | ||
return | ||
} | ||
} | ||
|
||
fun getXmlEditor(path: String) = DomFileEditor(resolve(path)) | ||
} | ||
|
||
class DomFileEditor internal constructor(private val domFile: File) : Closeable { | ||
val file: Document | ||
|
||
init { | ||
val factory = DocumentBuilderFactory.newInstance() | ||
factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true) | ||
|
||
val builder = factory.newDocumentBuilder() | ||
|
||
// this will expectedly throw | ||
file = builder.parse(domFile) | ||
file.normalize() | ||
} | ||
|
||
override fun close() = TransformerFactory.newInstance().newTransformer() | ||
.transform(DOMSource(file), StreamResult(domFile.outputStream())) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.