66using System . Collections . Generic ;
77using System . Reflection ;
88using System . Reflection . Emit ;
9- using UnityEngine . UIElements ;
10- using UnityEngine ;
119
1210#if REROOT_DEBUG_MODULE
1311using UnityEngine ;
@@ -20,53 +18,23 @@ class ReRootPreserveSurfaceAttach : BasePatch
2018 protected override void ApplyPatches ( List < PatchInfo > patches )
2119 {
2220 patches . Add ( new PatchInfo (
23- PatchMethodType . Prefix ,
24- AccessTools . Method ( typeof ( AttachNode ) , nameof ( AttachNode . ReverseSrfNodeDirection ) ) ,
25- this ) ) ;
26-
27- patches . Add ( new PatchInfo (
28- PatchMethodType . Prefix ,
29- AccessTools . Method ( typeof ( AttachNode ) , nameof ( AttachNode . ChangeSrfNodePosition ) ) ,
21+ PatchMethodType . Transpiler ,
22+ AccessTools . Method ( typeof ( Part ) , nameof ( Part . SetHierarchyRoot ) ) ,
3023 this ) ) ;
3124 }
3225
33- // In stock, this function is called after reversing a surface attachment during a re-root operation.
34- // it tries to alter a part's surface attachment so that it mirrors the surface attach node of its parent.
35- // But that's not a great idea, because a lot of things depend on the surface attach node never changing.
36- // For example, if the user then picks the part back up, it won't attach the same way to anything else
37- // To fix this, instead of using the child's actual srfAttachNode we create a new surface attach node and
38- // just stick it in the regular AttachNode list.
39- static bool AttachNode_ReverseSrfNodeDirection_Prefix ( AttachNode __instance , AttachNode fromNode )
26+ // skip the portion of that method that alter surface nodes position/orientation on re-rooting,
27+ // by returning after the recursive SetHierarchyRoot() call.
28+ private static IEnumerable < CodeInstruction > Part_SetHierarchyRoot_Transpiler ( IEnumerable < CodeInstruction > instructions )
4029 {
41- // note that instead of cloning the child's srfAttachNode and using its properties, we use the fromNode
42- // because we want to mirror the previous state as much as possible - this node WAS the other part's srfAttachNode
43- AttachNode newSrfAttachNode = AttachNode . Clone ( fromNode ) ;
44- newSrfAttachNode . owner = __instance . owner ;
45- newSrfAttachNode . attachedPart = fromNode . owner ;
46- // the name here isn't important, but if anyone is debugging an issue I'd like to make it clear where it came from.
47- // I'm pretty sure the empty string has some special meaning, and we need to be sure it doesn't collide with any existing node IDs
48- newSrfAttachNode . id = "KSPCF-reroot-srfAttachNode" ;
49-
50- // convert the position, orientation from the other part's local space into ours
51- Vector3 positionWorld = fromNode . owner . transform . TransformPoint ( fromNode . position ) ;
52- Vector3 orientationWorld = fromNode . owner . transform . TransformDirection ( fromNode . orientation ) ;
53- newSrfAttachNode . originalPosition = newSrfAttachNode . owner . transform . InverseTransformPoint ( positionWorld ) ;
54- newSrfAttachNode . originalOrientation = - newSrfAttachNode . owner . transform . InverseTransformDirection ( orientationWorld ) ;
55- newSrfAttachNode . position = newSrfAttachNode . originalPosition ;
56- newSrfAttachNode . orientation = newSrfAttachNode . originalOrientation ;
57- newSrfAttachNode . owner . attachNodes . Add ( newSrfAttachNode ) ;
58-
59- // now clear the srfAttachNodes from both parts
60- __instance . attachedPart = null ;
61- fromNode . attachedPart = null ;
62-
63- return false ;
64- }
30+ MethodInfo m_Part_SetHierarchyRoot = AccessTools . Method ( typeof ( Part ) , nameof ( Part . SetHierarchyRoot ) ) ;
6531
66- // this function is just horribly broken and no one could call it, ever
67- static bool AttachNode_ChangeSrfNodePosition_Prefix ( )
68- {
69- return false ;
32+ foreach ( CodeInstruction instruction in instructions )
33+ {
34+ yield return instruction ;
35+ if ( instruction . opcode == OpCodes . Callvirt && ReferenceEquals ( instruction . operand , m_Part_SetHierarchyRoot ) )
36+ yield return new CodeInstruction ( OpCodes . Ret ) ;
37+ }
7038 }
7139 }
7240
0 commit comments