Skip to content

Commit dcba4b7

Browse files
committed
fix namecopy
1 parent bb89c19 commit dcba4b7

File tree

3 files changed

+27
-53
lines changed

3 files changed

+27
-53
lines changed

src/commonMain/kotlin/xyz/wagyourtail/unimined/mapping/tree/MemoryMappingTree.kt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -173,11 +173,13 @@ class MemoryMappingTree : AbstractMappingTree() {
173173
val nameMap = it.names.toMutableMap()
174174
NameCopyDelegate.fillAllNames(toFill, nameMap)
175175
it.setNames(nameMap)
176-
it.acceptInner(
177-
DelegateClassVisitor(it, NameCopyDelegate(*toFill)),
178-
namespaces,
179-
false
180-
)
176+
for (fill in toFill) {
177+
it.acceptInner(
178+
DelegateClassVisitor(it, NameCopyDelegate(fill)),
179+
namespaces,
180+
false
181+
)
182+
}
181183
}
182184
}
183185
).awaitAll()

src/commonMain/kotlin/xyz/wagyourtail/unimined/mapping/visitor/delegate/NameCopyVisitor.kt

Lines changed: 17 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,19 @@ fun MappingVisitor.copyNames(from: Namespace, to: Set<Namespace>, onlyMissing: B
1111
return DelegateMappingVisitor(this, NameCopyDelegate(from to to, onlyMissing = onlyMissing))
1212
}
1313

14-
fun MappingVisitor.copyNames(vararg from: Pair<Namespace, Set<Namespace>>, onlyMissing: Boolean = true): MappingVisitor {
15-
return DelegateMappingVisitor(this, NameCopyDelegate(*from, onlyMissing = onlyMissing))
14+
fun MappingVisitor.copyNames(from: Pair<Namespace, Set<Namespace>>, onlyMissing: Boolean = true): MappingVisitor {
15+
return DelegateMappingVisitor(this, NameCopyDelegate(from, onlyMissing = onlyMissing))
1616
}
1717

18-
class NameCopyDelegate(vararg val from: Pair<Namespace, Set<Namespace>>, val onlyMissing: Boolean = true) : NullDelegator() {
18+
class NameCopyDelegate(val from: Pair<Namespace, Set<Namespace>>, val onlyMissing: Boolean = true) : NullDelegator() {
1919

2020
companion object {
2121

2222
fun Set<Namespace>.ifOnlyMissing(to: Set<Namespace>, onlyMissing: Boolean): Set<Namespace> {
2323
return if (onlyMissing) to - this else to
2424
}
2525

26-
fun <T> fillAllNames(toFill: Array<out Pair<Namespace, Set<Namespace>>>, names: MutableMap<Namespace, T>, onlyMissing: Boolean = true, fillWith: (T) -> T = { it }) {
26+
inline fun <T> fillAllNames(toFill: Array<out Pair<Namespace, Set<Namespace>>>, names: MutableMap<Namespace, T>, onlyMissing: Boolean = true, fillWith: (T) -> T = { it }) {
2727
for ((from, to) in toFill) {
2828
val name = names[from] ?: continue
2929
for (namespace in names.keys.ifOnlyMissing(to, onlyMissing)) {
@@ -32,7 +32,7 @@ class NameCopyDelegate(vararg val from: Pair<Namespace, Set<Namespace>>, val onl
3232
}
3333
}
3434

35-
fun <T, U> fillNames(toFill: Pair<Namespace, Set<Namespace>>, names: Map<Namespace, T>, onlyMissing: Boolean = true, fillWith: (T) -> T = { it }, write: (Map<Namespace, T>) -> U): U? {
35+
inline fun <T, U> fillNames(toFill: Pair<Namespace, Set<Namespace>>, names: Map<Namespace, T>, onlyMissing: Boolean = true, fillWith: (T) -> T = { it }, write: (Map<Namespace, T>) -> U): U? {
3636
val (from, to) = toFill
3737
val names = names.toMutableMap()
3838
val name = names[from] ?: return null
@@ -45,53 +45,33 @@ class NameCopyDelegate(vararg val from: Pair<Namespace, Set<Namespace>>, val onl
4545
}
4646

4747
override fun visitClass(delegate: MappingVisitor, names: Map<Namespace, InternalName>): ClassVisitor? {
48-
var visitor: ClassVisitor? = null
49-
for (pair in from) {
50-
visitor?.visitEnd()
51-
visitor = fillNames(pair, names, onlyMissing) {
52-
default.visitClass(delegate, names)
53-
}
48+
return fillNames(from, names, onlyMissing) {
49+
default.visitClass(delegate, it)
5450
}
55-
return visitor ?: default.visitClass(delegate, names)
5651
}
5752

5853
override fun visitPackage(delegate: MappingVisitor, names: Map<Namespace, PackageName>): PackageVisitor? {
59-
var visitor: PackageVisitor? = null
60-
for (pair in from) {
61-
visitor?.visitEnd()
62-
visitor = fillNames(pair, names, onlyMissing) {
63-
default.visitPackage(delegate, names)
64-
}
54+
return fillNames(from, names, onlyMissing) {
55+
default.visitPackage(delegate, it)
6556
}
66-
return visitor ?: default.visitPackage(delegate, names)
6757
}
6858

6959
override fun visitField(
7060
delegate: ClassVisitor,
7161
names: Map<Namespace, Pair<String, FieldDescriptor?>>
7262
): FieldVisitor? {
73-
var visitor: FieldVisitor? = null
74-
for (pair in from) {
75-
visitor?.visitEnd()
76-
visitor = fillNames(pair, names, onlyMissing, { it.first to null }) {
77-
default.visitField(delegate, names)
78-
}
63+
return fillNames(from, names, onlyMissing, { it.first to null }) {
64+
default.visitField(delegate, it)
7965
}
80-
return visitor ?: default.visitField(delegate, names)
8166
}
8267

8368
override fun visitMethod(
8469
delegate: ClassVisitor,
8570
names: Map<Namespace, Pair<String, MethodDescriptor?>>
8671
): MethodVisitor? {
87-
var visitor: MethodVisitor? = null
88-
for (pair in from) {
89-
visitor?.visitEnd()
90-
visitor = fillNames(pair, names, onlyMissing, { it.first to null }) {
91-
default.visitMethod(delegate, names)
92-
}
72+
return fillNames(from, names, onlyMissing, { it.first to null }) {
73+
default.visitMethod(delegate, it)
9374
}
94-
return visitor ?: default.visitMethod(delegate, names)
9575
}
9676

9777
override fun visitParameter(
@@ -100,14 +80,9 @@ class NameCopyDelegate(vararg val from: Pair<Namespace, Set<Namespace>>, val onl
10080
lvOrd: Int?,
10181
names: Map<Namespace, String>
10282
): ParameterVisitor? {
103-
var visitor: ParameterVisitor? = null
104-
for (pair in from) {
105-
visitor?.visitEnd()
106-
visitor = fillNames(pair, names, onlyMissing) {
107-
default.visitParameter(delegate, index, lvOrd, names)
108-
}
83+
return fillNames(from, names, onlyMissing) {
84+
default.visitParameter(delegate, index, lvOrd, it)
10985
}
110-
return visitor ?: default.visitParameter(delegate, index, lvOrd, names)
11186
}
11287

11388
override fun visitLocalVariable(
@@ -116,14 +91,9 @@ class NameCopyDelegate(vararg val from: Pair<Namespace, Set<Namespace>>, val onl
11691
startOp: Int?,
11792
names: Map<Namespace, String>
11893
): LocalVariableVisitor? {
119-
var visitor: LocalVariableVisitor? = null
120-
for (pair in from) {
121-
visitor?.visitEnd()
122-
visitor = fillNames(pair, names, onlyMissing) {
123-
default.visitLocalVariable(delegate, lvOrd, startOp, names)
124-
}
94+
return fillNames(from, names, onlyMissing) {
95+
default.visitLocalVariable(delegate, lvOrd, startOp, it)
12596
}
126-
return visitor ?: default.visitLocalVariable(delegate, lvOrd, startOp, names)
12797
}
12898

12999
}

src/commonTest/kotlin/xyz/wagyourtail/unimined/mapping/test/delegate/NameCopyTest.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,10 @@ class NameCopyTest {
3939
UMFReader.read(input)
4040
}
4141

42+
mappings.accept(mappings.copyNames(Namespace("intermediary") to setOf(Namespace("named"))))
43+
4244
val output = Buffer().use { output ->
43-
mappings.accept(UMFWriter.write(output, true).copyNames(Namespace("intermediary") to setOf(Namespace("named")), Namespace("named") to setOf(Namespace("extra"))))
45+
mappings.accept(UMFWriter.write(output, true).copyNames(Namespace("named") to setOf(Namespace("extra"))))
4446
output.readUtf8()
4547
}
4648

0 commit comments

Comments
 (0)