Skip to content

Commit

Permalink
refactor read/write args, ns checking
Browse files Browse the repository at this point in the history
  • Loading branch information
wagyourtail committed Jul 18, 2024
1 parent 0cf9d53 commit 3304ebf
Show file tree
Hide file tree
Showing 54 changed files with 242 additions and 169 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package xyz.wagyourtail.unimined.mapping.formats

import okio.Buffer
import okio.BufferedSource
import okio.use
import xyz.wagyourtail.unimined.mapping.EnvType
import xyz.wagyourtail.unimined.mapping.tree.AbstractMappingTree
import xyz.wagyourtail.unimined.mapping.tree.MemoryMappingTree
Expand All @@ -14,39 +12,85 @@ interface FormatReader {
val name: String
get() = this::class.simpleName!!.removeSuffix("Reader").lowercase()

fun isFormat(envType: EnvType, fileName: String, inputType: BufferedSource): Boolean
fun isFormat(fileName: String, input: BufferedSource, envType: EnvType = EnvType.JOINED): Boolean

fun getSide(fileName: String, inputType: BufferedSource): Set<EnvType> = EnvType.entries.toSet()
fun getSide(fileName: String, input: BufferedSource): Set<EnvType> = EnvType.entries.toSet()

suspend fun read(fileStr: String, nsMapping: Map<String, String> = mapOf()): MemoryMappingTree = Buffer().use {
it.writeUtf8(fileStr)
read(it, nsMapping)
suspend fun read(
content: String,
envType: EnvType = EnvType.JOINED,
nsMapping: Map<String, String> = mapOf()
): MemoryMappingTree = MemoryMappingTree().also {
read(CharReader(content), null, it, envType, nsMapping)
}

suspend fun read(envType: EnvType, fileStr: String, nsMapping: Map<String, String> = mapOf()): MemoryMappingTree = Buffer().use {
it.writeUtf8(fileStr)
read(envType, it, nsMapping)
suspend fun read(
content: String,
into: AbstractMappingTree,
envType: EnvType = EnvType.JOINED,
nsMapping: Map<String, String> = mapOf()
) {
read(CharReader(content), into, into, envType, nsMapping)
}

suspend fun read(envType: EnvType, fileStr: String, into: AbstractMappingTree, nsMapping: Map<String, String> = mapOf()) = Buffer().use {
it.writeUtf8(fileStr)
read(envType, it, into, nsMapping)
}
suspend fun read(
content: String,
context: AbstractMappingTree?,
into: MappingVisitor,
envType: EnvType = EnvType.JOINED,
nsMapping: Map<String, String> = mapOf()
) = read(CharReader(content), context, into, envType, nsMapping)

suspend fun read(envType: EnvType, fileStr: String, context: AbstractMappingTree?, into: MappingVisitor, nsMapping: Map<String, String> = mapOf()) = CharReader(fileStr).use {
read(envType, it, context, into, nsMapping)
suspend fun read(
input: BufferedSource,
envType: EnvType = EnvType.JOINED,
nsMapping: Map<String, String> = mapOf()
): MemoryMappingTree = MemoryMappingTree().also {
read(input, null, it, envType, nsMapping)
}

suspend fun read(inputType: BufferedSource, nsMapping: Map<String, String> = mapOf()): MemoryMappingTree = MemoryMappingTree().also { read(EnvType.JOINED, inputType, it, it, nsMapping) }
suspend fun read(
input: BufferedSource,
into: AbstractMappingTree,
envType: EnvType = EnvType.JOINED,
nsMapping: Map<String, String> = mapOf()
) {
read(CharReader(input.readUtf8()), into, into, envType, nsMapping)
}

suspend fun read(envType: EnvType, inputType: BufferedSource, nsMapping: Map<String, String> = mapOf()): MemoryMappingTree = MemoryMappingTree().also { read(envType, inputType, it, it, nsMapping) }
suspend fun read(
input: BufferedSource,
context: AbstractMappingTree?,
into: MappingVisitor,
envType: EnvType = EnvType.JOINED,
nsMapping: Map<String, String> = mapOf()
) {
read(CharReader(input.readUtf8()), context, into, envType, nsMapping)
}

suspend fun read(envType: EnvType, inputType: BufferedSource, into: AbstractMappingTree, nsMapping: Map<String, String> = mapOf()) = read(envType, inputType, into, into, nsMapping)
suspend fun read(
input: CharReader,
envType: EnvType = EnvType.JOINED,
nsMapping: Map<String, String> = mapOf()
): MemoryMappingTree = MemoryMappingTree().also {
read(input, null, it, envType, nsMapping)
}

suspend fun read(envType: EnvType, inputType: BufferedSource, context: AbstractMappingTree?, into: MappingVisitor, nsMapping: Map<String, String> = mapOf()) {
read(envType, CharReader(inputType.readUtf8()), context, into, nsMapping)
suspend fun read(
input: CharReader,
into: AbstractMappingTree,
envType: EnvType = EnvType.JOINED,
nsMapping: Map<String, String> = mapOf()
) {
read(input, into, into, envType, nsMapping)
}

suspend fun read(envType: EnvType, input: CharReader, context: AbstractMappingTree?, into: MappingVisitor, nsMapping: Map<String, String> = mapOf())
suspend fun read(
input: CharReader,
context: AbstractMappingTree?,
into: MappingVisitor,
envType: EnvType = EnvType.JOINED,
nsMapping: Map<String, String> = mapOf()
)

}
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ object FormatRegistry {

fun autodetectFormat(envType: EnvType, fileName: String, inputType: BufferedSource): FormatProvider? {
return formats.firstOrNull {
try { it.isFormat(envType, fileName, inputType.peek()) }
try { it.isFormat(fileName, inputType.peek(), envType) }
catch (e: Exception) { false }
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@ interface FormatWriter {
val name: String
get() = this::class.simpleName!!.removeSuffix("Reader")

fun write(envType: EnvType = EnvType.JOINED, into: BufferedSink): MappingVisitor = write(envType, into::writeUtf8)
fun write(into: BufferedSink, envType: EnvType = EnvType.JOINED): MappingVisitor = write(into::writeUtf8, envType)

fun write(envType: EnvType = EnvType.JOINED, append: (String) -> Unit): MappingVisitor
fun write(append: (String) -> Unit, envType: EnvType = EnvType.JOINED): MappingVisitor

}

inline fun FormatWriter.writeToString(envType: EnvType = EnvType.JOINED, acceptor: (MappingVisitor) -> Unit) = buildString { acceptor(write(EnvType.JOINED, ::append)) }
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import xyz.wagyourtail.unimined.mapping.visitor.use
*/
object ATReader : FormatReader {

override fun isFormat(envType: EnvType, fileName: String, inputType: BufferedSource): Boolean {
override fun isFormat(fileName: String, input: BufferedSource, envType: EnvType): Boolean {
val cfg = fileName.substringAfterLast('.') in setOf("at", "cfg")
val name = fileName.substringBeforeLast('.').lowercase()
return (cfg && name.endsWith("_at") || name.startsWith("accesstransformer"))
Expand Down Expand Up @@ -83,10 +83,10 @@ object ATReader : FormatReader {
}

override suspend fun read(
envType: EnvType,
input: CharReader,
context: AbstractMappingTree?,
into: MappingVisitor,
envType: EnvType,
nsMapping: Map<String, String>
) {
val ns = Namespace(nsMapping["source"] ?: "source")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import xyz.wagyourtail.unimined.mapping.visitor.delegate.delegator

object ATWriter : FormatWriter {

override fun write(envType: EnvType, append: (String) -> Unit): MappingVisitor {
override fun write(append: (String) -> Unit, envType: EnvType): MappingVisitor {
return assembleAts {
writeData(it, append)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import xyz.wagyourtail.unimined.mapping.visitor.MappingVisitor
*/
object LegacyATReader : FormatReader {

override fun isFormat(envType: EnvType, fileName: String, inputType: BufferedSource): Boolean {
override fun isFormat(fileName: String, input: BufferedSource, envType: EnvType): Boolean {
val cfg = fileName.substringAfterLast('.') == "cfg"
val name = fileName.substringBeforeLast('.').lowercase()
return (cfg && name.endsWith("_at"))
Expand All @@ -25,10 +25,10 @@ object LegacyATReader : FormatReader {


override suspend fun read(
envType: EnvType,
input: CharReader,
context: AbstractMappingTree?,
into: MappingVisitor,
envType: EnvType,
nsMapping: Map<String, String>
) {
val ns = Namespace(nsMapping["source"] ?: "source")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import xyz.wagyourtail.unimined.mapping.formats.FormatWriter
import xyz.wagyourtail.unimined.mapping.visitor.MappingVisitor

object LegacyATWriter : FormatWriter {
override fun write(envType: EnvType, append: (String) -> Unit): MappingVisitor {
override fun write(append: (String) -> Unit, envType: EnvType): MappingVisitor {
return ATWriter.assembleAts {
writeData(it, append)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ object AWReader: FormatReader {
@Suppress("MemberVisibilityCanBePrivate")
var allowNonTransitive = true

override fun isFormat(envType: EnvType, fileName: String, inputType: BufferedSource): Boolean {
override fun isFormat(fileName: String, input: BufferedSource, envType: EnvType): Boolean {
// check content begins with "accessWidener"
return inputType.peek().readUtf8Line()?.startsWith("accessWidener") ?: false
return input.peek().readUtf8Line()?.startsWith("accessWidener") ?: false
}

data class AWData(
Expand All @@ -41,10 +41,10 @@ object AWReader: FormatReader {
)

override suspend fun read(
envType: EnvType,
input: CharReader,
context: AbstractMappingTree?,
into: MappingVisitor,
envType: EnvType,
nsMapping: Map<String, String>
) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ object AWWriter : FormatWriter {
* use [AWWriter.remapMappings] to convert to the correct mappings,
* and [AWWriter.writeData] to write the mappings
*/
override fun write(envType: EnvType, append: (String) -> Unit): MappingVisitor {
override fun write(append: (String) -> Unit, envType: EnvType): MappingVisitor {
var ns: Namespace? = null
var cls: InternalName? = null
var member: NameAndDescriptor? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ object CsrgReader : FormatReader {
}
}

override fun isFormat(envType: EnvType, fileName: String, inputType: BufferedSource): Boolean {
override fun isFormat(fileName: String, input: BufferedSource, envType: EnvType): Boolean {
return fileName.endsWith(".csrg")
}

override suspend fun read(
envType: EnvType,
input: CharReader,
context: AbstractMappingTree?,
into: MappingVisitor,
envType: EnvType,
nsMapping: Map<String, String>
) {
val srcNs = Namespace(nsMapping["source"] ?: "source")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ import xyz.wagyourtail.unimined.mapping.visitor.use

object ExceptionReader : FormatReader {

override fun isFormat(envType: EnvType, fileName: String, inputType: BufferedSource): Boolean {
override fun isFormat(fileName: String, input: BufferedSource, envType: EnvType): Boolean {
return fileName.endsWith(".excs")
}

override suspend fun read(
envType: EnvType,
input: CharReader,
context: AbstractMappingTree?,
into: MappingVisitor,
envType: EnvType,
nsMapping: Map<String, String>
) {
val ns = Namespace(nsMapping["source"] ?: "source")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ import xyz.wagyourtail.unimined.mapping.visitor.use
*/
object NestReader : FormatReader {

override fun isFormat(envType: EnvType, fileName: String, inputType: BufferedSource): Boolean {
override fun isFormat(fileName: String, input: BufferedSource, envType: EnvType): Boolean {
return fileName.endsWith(".nest")
}

override suspend fun read(
envType: EnvType,
input: CharReader,
context: AbstractMappingTree?,
into: MappingVisitor,
envType: EnvType,
nsMapping: Map<String, String>
) {
val ns = Namespace(nsMapping["source"] ?: "source")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ import xyz.wagyourtail.unimined.mapping.visitor.use

object SignatureReader : FormatReader {

override fun isFormat(envType: EnvType, fileName: String, inputType: BufferedSource): Boolean {
override fun isFormat(fileName: String, input: BufferedSource, envType: EnvType): Boolean {
return fileName.endsWith(".sigs")
}

override suspend fun read(
envType: EnvType,
input: CharReader,
context: AbstractMappingTree?,
into: MappingVisitor,
envType: EnvType,
nsMapping: Map<String, String>
) {
val ns = Namespace(nsMapping["source"] ?: "source")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ import xyz.wagyourtail.unimined.mapping.visitor.use

object MCPExceptionReader : FormatReader {

override fun isFormat(envType: EnvType, fileName: String, inputType: BufferedSource): Boolean {
override fun isFormat(fileName: String, input: BufferedSource, envType: EnvType): Boolean {
return fileName.substringAfterLast('/').endsWith(".exc")
}

override fun getSide(fileName: String, inputType: BufferedSource): Set<EnvType> {
override fun getSide(fileName: String, input: BufferedSource): Set<EnvType> {
if (fileName == "client.exc") return setOf(EnvType.CLIENT, EnvType.JOINED)
if (fileName == "server.exc") return setOf(EnvType.SERVER, EnvType.JOINED)
return super.getSide(fileName, inputType)
return super.getSide(fileName, input)
}

val split = setOf('\n', '-', '|', '=')
Expand All @@ -38,10 +38,10 @@ object MCPExceptionReader : FormatReader {
*/

override suspend fun read(
envType: EnvType,
input: CharReader,
context: AbstractMappingTree?,
into: MappingVisitor,
envType: EnvType,
nsMapping: Map<String, String>
) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,21 @@ import xyz.wagyourtail.unimined.mapping.visitor.delegate.delegator
*/
object MCPv1FieldReader : FormatReader {

override fun isFormat(envType: EnvType, fileName: String, inputType: BufferedSource): Boolean {
override fun isFormat(fileName: String, input: BufferedSource, envType: EnvType): Boolean {
if (fileName.substringAfterLast('/') != "fields.csv") return false
// check that 4th line starts with "class"
inputType.peek().use {
input.peek().use {
it.readUtf8Line()
it.readUtf8Line()
return it.readUtf8Line()?.startsWith("class") ?: false
}
}

override suspend fun read(
envType: EnvType,
input: CharReader,
context: AbstractMappingTree?,
into: MappingVisitor,
envType: EnvType,
nsMapping: Map<String, String>
) {
input.takeLine()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ import xyz.wagyourtail.unimined.mapping.visitor.delegate.*
*/
object MCPv1MethodReader : FormatReader {

override fun isFormat(envType: EnvType, fileName: String, inputType: BufferedSource): Boolean {
override fun isFormat(fileName: String, input: BufferedSource, envType: EnvType): Boolean {
if (fileName.substringAfterLast('/') != "methods.csv") return false
// check that 4th line starts with "class"
inputType.peek().use {
input.peek().use {
it.readUtf8Line()
it.readUtf8Line()
it.readUtf8Line()
Expand All @@ -29,10 +29,10 @@ object MCPv1MethodReader : FormatReader {
}

override suspend fun read(
envType: EnvType,
input: CharReader,
context: AbstractMappingTree?,
into: MappingVisitor,
envType: EnvType,
nsMapping: Map<String, String>
) {
input.takeLine()
Expand Down
Loading

0 comments on commit 3304ebf

Please sign in to comment.