aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/com/replaymod/gradle/remap/Transformer.kt
diff options
context:
space:
mode:
authorJonas Herzig <me@johni0702.de>2021-03-14 11:25:33 +0100
committerJonas Herzig <me@johni0702.de>2021-03-14 11:38:19 +0100
commite7bc0828ad53c283ca9048e5b54146bf4e81e057 (patch)
tree03a0196556fae719a09dac16151ff683d4d7ada8 /src/main/kotlin/com/replaymod/gradle/remap/Transformer.kt
parent1dcdcb113251ae78c16f53fc62ece35f07f6caf5 (diff)
downloadRemap-e7bc0828ad53c283ca9048e5b54146bf4e81e057.tar.gz
Remap-e7bc0828ad53c283ca9048e5b54146bf4e81e057.tar.bz2
Remap-e7bc0828ad53c283ca9048e5b54146bf4e81e057.zip
Add new @Pattern feature to centralize version-aware code
That is, most of the business code should not be aware that it is being compiled to multiple versions even when it heavily interacts with MC, preprocessor statements should be an escape hatch, not the norm. Similarly, code should not be forced to do `MCVer.getWindow(mc)` instead of the much more intuitive `mc.getWindow()`, and this new preprocessor (technically remap) feature makes this possible by defining "search and replace"-like patterns (but smarter in that they are type-aware) in one or more central places which then are applied all over the code base. In a way, this is another step in the automatic back-porting process where preprocessor statements are used when we cannot yet do something automatically. Previously we "merely" automatically converted between different mapping, this new feature now also allows us to automatically perform simple refactoring tasks like changing field access to a getter+setter (e.g. `mc.getWindow()`), or changing how a method is called (e.g. `BufferBuilder.begin`), or changing a method call chain (e.g. `dispatcher.camera.getYaw()`), or most other search-and-replace-like changes and any combination of those. The only major limitation is that the replacement itself is not smart, so arguments must be kept in same order (or be temporarily assigned to local variables which then can be used in any order).
Diffstat (limited to 'src/main/kotlin/com/replaymod/gradle/remap/Transformer.kt')
-rw-r--r--src/main/kotlin/com/replaymod/gradle/remap/Transformer.kt25
1 files changed, 23 insertions, 2 deletions
diff --git a/src/main/kotlin/com/replaymod/gradle/remap/Transformer.kt b/src/main/kotlin/com/replaymod/gradle/remap/Transformer.kt
index 85e5acd..dd4215f 100644
--- a/src/main/kotlin/com/replaymod/gradle/remap/Transformer.kt
+++ b/src/main/kotlin/com/replaymod/gradle/remap/Transformer.kt
@@ -41,9 +41,14 @@ import kotlin.system.exitProcess
class Transformer(private val map: MappingSet) {
var classpath: Array<String>? = null
+ var patternAnnotation: String? = null
@Throws(IOException::class)
- fun remap(sources: Map<String, String>): Map<String, Pair<String, List<Pair<Int, String>>>> {
+ fun remap(sources: Map<String, String>): Map<String, Pair<String, List<Pair<Int, String>>>> =
+ remap(sources, emptyMap())
+
+ @Throws(IOException::class)
+ fun remap(sources: Map<String, String>, processedSource: Map<String, String>): Map<String, Pair<String, List<Pair<Int, String>>>> {
val tmpDir = Files.createTempDirectory("remap")
val disposable = Disposer.newDisposable()
try {
@@ -90,13 +95,29 @@ class Transformer(private val map: MappingSet) {
{ scope: GlobalSearchScope -> environment.createPackagePartProvider(scope) }
)
+ val patterns = patternAnnotation?.let { annotationFQN ->
+ val patterns = PsiPatterns(annotationFQN)
+ val annotationName = annotationFQN.substring(annotationFQN.lastIndexOf('.') + 1)
+ for ((unitName, source) in sources) {
+ if (!source.contains(annotationName)) continue
+ try {
+ val patternFile = vfs.findFileByIoFile(tmpDir.resolve(unitName).toFile())!!
+ val patternPsiFile = psiManager.findFile(patternFile)!!
+ patterns.read(patternPsiFile, processedSource[unitName]!!)
+ } catch (e: Exception) {
+ throw RuntimeException("Failed to read patterns from file \"$unitName\".", e)
+ }
+ }
+ patterns
+ }
+
val results = HashMap<String, Pair<String, List<Pair<Int, String>>>>()
for (name in sources.keys) {
val file = vfs.findFileByIoFile(tmpDir.resolve(name).toFile())!!
val psiFile = psiManager.findFile(file)!!
val mapped = try {
- PsiMapper(map, psiFile).remapFile(analysis.bindingContext)
+ PsiMapper(map, psiFile, patterns).remapFile(analysis.bindingContext)
} catch (e: Exception) {
throw RuntimeException("Failed to map file \"$name\".", e)
}