forked from openrs2/openrs2
Signed-off-by: Graham <gpe@openrs2.dev>bzip2
parent
f9021f7fe6
commit
a87a289c49
@ -0,0 +1,87 @@ |
|||||||
|
package dev.openrs2.deob.ast |
||||||
|
|
||||||
|
import com.github.javaparser.ParserConfiguration |
||||||
|
import com.github.javaparser.ast.CompilationUnit |
||||||
|
import com.github.javaparser.printer.PrettyPrinter |
||||||
|
import com.github.javaparser.printer.PrettyPrinterConfiguration |
||||||
|
import com.github.javaparser.symbolsolver.JavaSymbolSolver |
||||||
|
import com.github.javaparser.symbolsolver.resolution.typesolvers.ClassLoaderTypeSolver |
||||||
|
import com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver |
||||||
|
import com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeSolver |
||||||
|
import com.github.javaparser.utils.SourceRoot |
||||||
|
import com.github.michaelbull.logging.InlineLogger |
||||||
|
import dev.openrs2.deob.util.Module |
||||||
|
import java.util.function.Function |
||||||
|
|
||||||
|
class Library( |
||||||
|
val name: String, |
||||||
|
private val root: SourceRoot |
||||||
|
) : Iterable<CompilationUnit> { |
||||||
|
private val units = mutableMapOf<String, CompilationUnit>() |
||||||
|
|
||||||
|
init { |
||||||
|
for (unit in root.compilationUnits) { |
||||||
|
val name = unit.primaryType.get().fullyQualifiedName.get() |
||||||
|
units[name] = unit |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
operator fun get(name: String): CompilationUnit? { |
||||||
|
return units[name] |
||||||
|
} |
||||||
|
|
||||||
|
override fun iterator(): Iterator<CompilationUnit> { |
||||||
|
return units.values.iterator() |
||||||
|
} |
||||||
|
|
||||||
|
fun save() { |
||||||
|
logger.info { "Saving root ${root.root}" } |
||||||
|
root.saveAll() |
||||||
|
} |
||||||
|
|
||||||
|
companion object { |
||||||
|
private val logger = InlineLogger() |
||||||
|
|
||||||
|
private val PC_ANNOTATION_REGEX = Regex("@Pc\\(([0-9]+)\\)\\s+") |
||||||
|
|
||||||
|
private val printer = Function<CompilationUnit, String>( |
||||||
|
PrettyPrinter( |
||||||
|
PrettyPrinterConfiguration() |
||||||
|
.setIndentType(PrettyPrinterConfiguration.IndentType.TABS_WITH_SPACE_ALIGN) |
||||||
|
.setIndentSize(1) |
||||||
|
.setIndentCaseInSwitch(false) |
||||||
|
.setOrderImports(true) |
||||||
|
)::print |
||||||
|
).andThen(::stripNewlineAfterPcAnnotation) |
||||||
|
|
||||||
|
fun parse(module: Module): Library { |
||||||
|
logger.info { "Parsing root ${module.sources}" } |
||||||
|
|
||||||
|
val solver = CombinedTypeSolver( |
||||||
|
ClassLoaderTypeSolver(ClassLoader.getPlatformClassLoader()), |
||||||
|
JavaParserTypeSolver(module.sources) |
||||||
|
) |
||||||
|
for (dependency in module.transitiveDependencies) { |
||||||
|
solver.add(JavaParserTypeSolver(dependency.sources)) |
||||||
|
} |
||||||
|
|
||||||
|
val config = ParserConfiguration() |
||||||
|
.setLanguageLevel(ParserConfiguration.LanguageLevel.JAVA_11) |
||||||
|
.setSymbolResolver(JavaSymbolSolver(solver)) |
||||||
|
|
||||||
|
val root = SourceRoot(module.sources, config) |
||||||
|
root.printer = printer |
||||||
|
|
||||||
|
val results = root.tryToParseParallelized() |
||||||
|
for (result in results) { |
||||||
|
require(result.isSuccessful) { result } |
||||||
|
} |
||||||
|
|
||||||
|
return Library(module.name, root) |
||||||
|
} |
||||||
|
|
||||||
|
private fun stripNewlineAfterPcAnnotation(s: String): String { |
||||||
|
return s.replace(PC_ANNOTATION_REGEX, "@Pc($1) ") |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,13 @@ |
|||||||
|
package dev.openrs2.deob.ast |
||||||
|
|
||||||
|
class LibraryGroup(libraries: Iterable<Library>) : Iterable<Library> { |
||||||
|
private val libraries = libraries.associateBy { it.name } |
||||||
|
|
||||||
|
operator fun get(name: String): Library? { |
||||||
|
return libraries[name] |
||||||
|
} |
||||||
|
|
||||||
|
override fun iterator(): Iterator<Library> { |
||||||
|
return libraries.values.iterator() |
||||||
|
} |
||||||
|
} |
@ -1,27 +1,31 @@ |
|||||||
package dev.openrs2.deob.ast.transform |
package dev.openrs2.deob.ast.transform |
||||||
|
|
||||||
import com.github.javaparser.ast.CompilationUnit |
import com.github.javaparser.ast.CompilationUnit |
||||||
|
import dev.openrs2.deob.ast.Library |
||||||
|
import dev.openrs2.deob.ast.LibraryGroup |
||||||
|
|
||||||
abstract class Transformer { |
abstract class Transformer { |
||||||
fun transform(units: Map<String, CompilationUnit>) { |
fun transform(group: LibraryGroup) { |
||||||
preTransform(units) |
preTransform(group) |
||||||
|
|
||||||
for (unit in units.values) { |
for (library in group) { |
||||||
transformUnit(units, unit) |
for (unit in library) { |
||||||
|
transformUnit(group, library, unit) |
||||||
|
} |
||||||
} |
} |
||||||
|
|
||||||
postTransform(units) |
postTransform(group) |
||||||
} |
} |
||||||
|
|
||||||
protected open fun preTransform(units: Map<String, CompilationUnit>) { |
protected open fun preTransform(group: LibraryGroup) { |
||||||
// empty |
// empty |
||||||
} |
} |
||||||
|
|
||||||
protected open fun transformUnit(units: Map<String, CompilationUnit>, unit: CompilationUnit) { |
protected open fun transformUnit(group: LibraryGroup, library: Library, unit: CompilationUnit) { |
||||||
// empty |
// empty |
||||||
} |
} |
||||||
|
|
||||||
protected open fun postTransform(units: Map<String, CompilationUnit>) { |
protected open fun postTransform(group: LibraryGroup) { |
||||||
// empty |
// empty |
||||||
} |
} |
||||||
} |
} |
||||||
|
Loading…
Reference in new issue