Skip to content

Commit 84352c0

Browse files
committed
Fix inner classes not being remapped and cleaned up
1 parent d96bae7 commit 84352c0

File tree

5 files changed

+57
-38
lines changed

5 files changed

+57
-38
lines changed

common/src/main/kotlin/com/lambda/module/modules/network/PacketLogger.kt

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ import com.lambda.util.Communication
3030
import com.lambda.util.Communication.info
3131
import com.lambda.util.DynamicReflectionSerializer.dynamicString
3232
import com.lambda.util.FolderRegister
33+
import com.lambda.util.FolderRegister.relativeMCPath
3334
import com.lambda.util.Formatting.getTime
3435
import com.lambda.util.text.*
3536
import kotlinx.coroutines.channels.BufferOverflow
3637
import kotlinx.coroutines.flow.MutableSharedFlow
3738
import net.minecraft.network.packet.Packet
3839
import java.awt.Color
3940
import java.io.File
40-
import java.nio.file.Path
4141
import java.time.format.DateTimeFormatter
4242
import kotlin.io.path.pathString
4343

@@ -83,7 +83,6 @@ object PacketLogger : Module(
8383
extraBufferCapacity = 1000,
8484
onBufferOverflow = BufferOverflow.DROP_OLDEST
8585
)
86-
private val File.relativePath: Path get() = mc.runDirectory.toPath().relativize(toPath())
8786

8887
init {
8988
runIO {
@@ -105,15 +104,14 @@ object PacketLogger : Module(
105104
createNewFile()
106105
}
107106
val info = buildText {
108-
clickEvent(ClickEvents.openFile(relativePath.pathString)) {
107+
clickEvent(ClickEvents.openFile(relativeMCPath.pathString)) {
109108
literal("Packet logger started: ")
110109
color(Color.YELLOW) { literal(fileName) }
111110
literal(" (click to open)")
112111
}
113112
}
114113
this@PacketLogger.info(info)
115114
}.apply {
116-
// ToDo: Add more rich and accurate data to the header
117115
StringBuilder().apply {
118116
appendLine(Communication.ascii)
119117
appendLine("${Lambda.SYMBOL} - Lambda ${Lambda.VERSION} - Packet Log")
@@ -144,8 +142,8 @@ object PacketLogger : Module(
144142
file?.let {
145143
val info = buildText {
146144
literal("Stopped logging packets to ")
147-
clickEvent(ClickEvents.openFile(it.relativePath.pathString)) {
148-
color(Color.YELLOW) { literal(it.relativePath.pathString) }
145+
clickEvent(ClickEvents.openFile(it.relativeMCPath.pathString)) {
146+
color(Color.YELLOW) { literal(it.relativeMCPath.pathString) }
149147
literal(" (click to open)")
150148
}
151149
}

common/src/main/kotlin/com/lambda/network/api/v1/endpoints/GetMappings.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ import net.minecraft.SharedConstants
3232
*
3333
* response: File or error
3434
*/
35-
fun getMappings(version: String = SharedConstants.VERSION_NAME, success: (String) -> Unit, failure: (FuelError) -> Unit): CancellableRequest =
36-
Fuel.get("$apiUrl/api/${apiVersion.value}/mappings?version=$version")
35+
fun getMappings(
36+
version: String = SharedConstants.getGameVersion().name,
37+
success: (String) -> Unit,
38+
failure: (FuelError) -> Unit
39+
) = Fuel.get("$apiUrl/api/${apiVersion.value}/mappings?version=$version")
3740
.responseString { _, _, result -> result.fold(success, failure) }

common/src/main/kotlin/com/lambda/util/DynamicReflectionSerializer.kt

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
package com.lambda.util
1919

20+
import com.lambda.Lambda
2021
import com.lambda.Lambda.LOG
2122
import com.lambda.core.Loadable
2223
import com.lambda.network.api.v1.endpoints.getMappings
@@ -70,38 +71,45 @@ object DynamicReflectionSerializer : Loadable {
7071

7172
private const val INDENT = 2
7273

73-
val mappings = cache.resolveFile("mappings-${SharedConstants.getProtocolVersion()}.m")
74-
.ifNotExists { getMappings(success = it.getIfNotPresent(), failure = { LOG.error("Could not download the required files for the dynamic remapper") }).join() }
75-
.let { file ->
76-
file.readLines()
77-
.map { it.split('\t') }
78-
.associate { it[0] to it[1] }
79-
}
80-
81-
inline val <T : Any> Class<T>.dynamicName: String get() = mappings.getOrDefault(simpleName, simpleName)
82-
83-
inline val Field.dynamicName: String get() = mappings.getOrDefault(name, name)
74+
private val mappings =
75+
cache.resolveFile("${SharedConstants.getProtocolVersion()}.mappings")
76+
.ifNotExists {
77+
getMappings(
78+
success = it.getIfNotPresent(),
79+
failure = { LOG.error("Could not download the required files for the dynamic remapper") }
80+
).join()
81+
}.let { file ->
82+
file.readLines()
83+
.map { it.split('\t') }
84+
.associate { it[0].split('$').last() to it[1] }
85+
}
86+
87+
private val String.remappedName get() = mappings.getOrDefault(this, this)
88+
89+
private fun <T : Any> Class<T>.dynamicName(remap: Boolean) =
90+
if (remap) simpleName.remappedName else simpleName
91+
private fun Field.dynamicName(remap: Boolean) =
92+
if (remap) name.remappedName else name
8493

85-
86-
// ToDo: To make this work in production, every field could be remapped.
8794
fun Any.dynamicString(
8895
maxRecursionDepth: Int = 6,
8996
currentDepth: Int = 0,
9097
indent: String = "",
9198
visitedObjects: MutableSet<Any> = HashSet(),
9299
builder: StringBuilder = StringBuilder(),
100+
remap: Boolean = !Lambda.isDebug,
93101
): String {
94102
if (visitedObjects.contains(this)) {
95-
builder.appendLine("$indent${javaClass.dynamicName} (Circular Reference)")
103+
builder.appendLine("$indent${javaClass.dynamicName(remap)} (Circular Reference)")
96104
return builder.toString()
97105
}
98106

99107
visitedObjects.add(this)
100-
builder.appendLine("$indent${javaClass.dynamicName}")
108+
builder.appendLine("$indent${javaClass.dynamicName(remap)}")
101109

102110
val fields = javaClass.declaredFields + javaClass.superclass?.declaredFields.orEmpty()
103111
fields.forEach { field ->
104-
processField(field, indent, builder, currentDepth, maxRecursionDepth, visitedObjects)
112+
processField(field, indent, builder, currentDepth, maxRecursionDepth, visitedObjects, remap)
105113
}
106114

107115
return builder.toString()
@@ -114,6 +122,7 @@ object DynamicReflectionSerializer : Loadable {
114122
currentDepth: Int,
115123
maxRecursionDepth: Int,
116124
visitedObjects: MutableSet<Any>,
125+
remap: Boolean,
117126
) {
118127
if (skipFields.any { it.isAssignableFrom(field.type) }) return
119128

@@ -123,8 +132,8 @@ object DynamicReflectionSerializer : Loadable {
123132
return
124133
}
125134
val fieldValue = field.get(this)
126-
val fieldIndent = indent + " ".repeat(INDENT)
127-
builder.appendLine("$fieldIndent${field.dynamicName}: ${fieldValue.formatFieldValue()}")
135+
val fieldIndent = "$indent${" ".repeat(INDENT)}"
136+
builder.appendLine("$fieldIndent${field.dynamicName(remap)}: ${fieldValue.formatFieldValue()}")
128137

129138
if (currentDepth < maxRecursionDepth
130139
&& fieldValue != null
@@ -136,9 +145,10 @@ object DynamicReflectionSerializer : Loadable {
136145
fieldValue.dynamicString(
137146
maxRecursionDepth,
138147
currentDepth + 1,
139-
fieldIndent + " ".repeat(INDENT),
148+
"$fieldIndent${" ".repeat(INDENT)}",
140149
visitedObjects,
141150
builder,
151+
remap
142152
)
143153
}
144154
}
@@ -161,7 +171,5 @@ object DynamicReflectionSerializer : Loadable {
161171
else -> this?.toString() ?: "null"
162172
}
163173

164-
override fun load(): String {
165-
return "Loaded ${mappings.size} remapped named"
166-
}
174+
override fun load() = "Loaded ${mappings.size} deobfuscated qualifier"
167175
}

common/src/main/kotlin/com/lambda/util/FileUtils.kt

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ object FileUtils {
3838
* If the file already exists, it will not be recreated. The necessary
3939
* parent directories will be created if they do not exist.
4040
*/
41-
fun File.createIfNotExists(): File = also { parentFile.mkdirs(); createNewFile() }
41+
fun File.createIfNotExists() = also { parentFile.mkdirs(); createNewFile() }
4242

4343
/**
4444
* Retrieves or creates a directory based on the current network connection and world dimension.
@@ -89,29 +89,36 @@ object FileUtils {
8989
*
9090
* This function does not guarantee that the given file will be created
9191
*/
92-
fun File.downloadIfNotPresent(url: String, success: (ByteArray) -> Unit = {}, failure: (FuelError) -> Unit = {}): File =
93-
ifNotExists { url.httpDownload().fileDestination { _, _ -> it }.response { _, _, result -> result.fold(success, failure) } }
92+
fun File.downloadIfNotPresent(
93+
url: String,
94+
success: (ByteArray) -> Unit = {},
95+
failure: (FuelError) -> Unit = {}
96+
) = ifNotExists { url.httpDownload().fileDestination { _, _ -> it }.response { _, _, result -> result.fold(success, failure) } }
9497

9598
/**
9699
* Downloads the given file url if the file is not present
97100
*
98101
* This function does not guarantee that the given file will be created
99102
*/
100-
fun String.downloadIfNotPresent(file: File, success: (ByteArray) -> Unit = {}, failure: (FuelError) -> Unit = {}): File =
101-
file.ifNotExists { httpDownload().fileDestination { _, _ -> it }.response { _, _, result -> result.fold(success, failure) } }
103+
fun String.downloadIfNotPresent(
104+
file: File,
105+
success: (ByteArray) -> Unit = {},
106+
failure: (FuelError) -> Unit = {}
107+
) = file.ifNotExists { httpDownload().fileDestination { _, _ -> it }.response { _, _, result -> result.fold(success, failure) } }
102108

103109
/**
104110
* Downloads the given file url if the file is not present
105111
*
106112
* This function does not guarantee that the given file will be created
107113
*/
108-
fun File.downloadIfNotPresent(): (String) -> Unit {
109-
return { url -> ifNotExists { url.httpDownload().fileDestination { _, _ -> it }.response { _, _, _ -> } } } }
114+
fun File.downloadIfNotPresent(): (String) -> Unit =
115+
{ url -> ifNotExists { url.httpDownload().fileDestination { _, _ -> it }.response { _, _, _ -> } } }
110116

111117
/**
112118
* Gets the given url if the file is not present
113119
*
114120
* This function does not guarantee that the given file will be created
115121
*/
116-
fun File.getIfNotPresent(): (String) -> Unit { return { url -> ifNotExists { url.httpGet().responseString().third.getOrNull()?.let { writeText(it) } } } }
122+
fun File.getIfNotPresent(): (String) -> Unit =
123+
{ url -> ifNotExists { url.httpGet().responseString().third.getOrNull()?.let { writeText(it) } } }
117124
}

common/src/main/kotlin/com/lambda/util/FolderRegister.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import com.lambda.util.FolderRegister.lambda
2424
import com.lambda.util.FolderRegister.minecraft
2525
import com.lambda.util.FolderRegister.packetLogs
2626
import com.lambda.util.FolderRegister.replay
27+
import java.io.File
2728
import java.nio.file.Path
2829
import kotlin.io.path.createDirectories
2930
import kotlin.io.path.notExists
@@ -48,6 +49,8 @@ object FolderRegister : Loadable {
4849
val structure: Path = lambda.resolve("structure")
4950
val maps: Path = lambda.resolve("maps")
5051

52+
val File.relativeMCPath: Path get() = minecraft.relativize(toPath())
53+
5154
override fun load(): String {
5255
val folders = listOf(lambda, config, packetLogs, replay, cache, capes, structure, maps)
5356
val createdFolders = folders.mapNotNull {

0 commit comments

Comments
 (0)