Skip to content

Commit 66243e5

Browse files
authored
Merge pull request #42 from way-zer/3.0
🚑️ 不再允许通过不同子树访问/修改同一个对象
2 parents 3a6129e + 50cd5d3 commit 66243e5

1 file changed

Lines changed: 12 additions & 2 deletions

File tree

  • src/main/kotlin/cf/wayzer/contentsTweaker

src/main/kotlin/cf/wayzer/contentsTweaker/CTNode.kt

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import arc.util.serialization.JsonWriter
66
import cf.wayzer.contentsTweaker.CTNode.*
77
import cf.wayzer.contentsTweaker.util.ExtendableClass
88
import cf.wayzer.contentsTweaker.util.ExtendableClassDSL
9+
import java.util.*
910

1011
/**
1112
* 所有节点都是[CTNode]
@@ -45,8 +46,10 @@ class CTNode private constructor() : ExtendableClass<CTExtInfo>() {
4546
val node = children[name]
4647
?: getAll<IndexableRaw>().firstNotNullOfOrNull { it.resolve(name) }
4748
?: error("Not found child $name")
48-
node.collectAll()
49-
return node
49+
//If it's a duplicate Node for object, only Modifiers are accessible.
50+
if (get<ObjInfo<*>>()?.isDuplicated == true && node.get<Modifier>() == null)
51+
error("Duplicated Object Node, cannot visit children")
52+
return node.collectAll()
5053
}
5154

5255
/** 供[ContentsTweaker.NodeCollector]使用,解析清使用[resolve]*/
@@ -61,7 +64,13 @@ class CTNode private constructor() : ExtendableClass<CTExtInfo>() {
6164
val elementType: Class<*>? = null, // List.T or Map.V
6265
val keyType: Class<*>? = null // Map.K
6366
) : CTExtInfo {
67+
val isDuplicated = duplicatedObjects.put(obj, Unit) != null
68+
6469
constructor(obj: T & Any) : this(obj, obj::class.java)
70+
71+
companion object {
72+
val duplicatedObjects = IdentityHashMap<Any, Unit>()
73+
}
6574
}
6675

6776
fun interface Resettable : CTExtInfo {
@@ -184,6 +193,7 @@ class CTNode private constructor() : ExtendableClass<CTExtInfo>() {
184193

185194
Root.children.clear()
186195
Root.collected = false
196+
ObjInfo.duplicatedObjects.clear()
187197
}
188198

189199
fun handle(json: JsonValue, node: CTNode = Root.collectAll()) {

0 commit comments

Comments
 (0)