diff --git a/CommunityBugFixCollection/BreakDuplicatedComponentDrives.cs b/CommunityBugFixCollection/BreakDuplicatedComponentDrives.cs new file mode 100644 index 0000000..6c3a1b9 --- /dev/null +++ b/CommunityBugFixCollection/BreakDuplicatedComponentDrives.cs @@ -0,0 +1,97 @@ +using Elements.Core; +using FrooxEngine; +using HarmonyLib; +using MonkeyLoader.Resonite; +using System; +using System.Collections.Generic; +using System.Text; + +using static FrooxEngine.Worker; + +namespace CommunityBugFixCollection +{ + [HarmonyPatchCategory(nameof(BreakDuplicatedComponentDrives))] + [HarmonyPatch(typeof(Slot), nameof(Slot.DuplicateComponents), [typeof(List), typeof(bool), typeof(List)])] + internal sealed class BreakDuplicatedComponentDrives : ResoniteMonkey + { + public override IEnumerable Authors => Contributors.Banane9; + + public override bool CanBeDisabled => true; + + private static void CollectInternalReferences(List sourceComponents, InternalReferences internalRefs, HashSet externalRefs, HashSet breakRefs) + { + foreach (var component in sourceComponents) + { + var refList = Pool.BorrowList(); + component.GetSyncMembers(refList, true); + + foreach (var syncRef in refList) + { + if (syncRef.Target is null) + { + if (syncRef.Value != RefID.Null) + breakRefs.Add(syncRef); + + continue; + } + + var targetParent = syncRef.Target?.FindNearestParent(); + + // Parent can't be a component being duplicated currently + if (targetParent is null) + externalRefs.Add(syncRef); + + // A HashSet for the Contains would seem faster, but in 99.9% of cases this is only one component + if (sourceComponents.Contains(targetParent!)) + { + internalRefs.AddPair(syncRef, syncRef.Target!); + continue; + } + + externalRefs.Add(syncRef); + + if (syncRef is ILinkRef) + breakRefs.Add(syncRef); + } + + Pool.Return(ref refList); + } + } + + private static bool Prefix(Slot __instance, List sourceComponents, bool breakExternalReferences, List duplicates) + { + if (!Enabled) + return true; + + using var internalRefs = new InternalReferences(); + var breakRefs = Pool.BorrowHashSet(); + var externalRefs = Pool.BorrowHashSet(); + + CollectInternalReferences(sourceComponents, internalRefs, externalRefs, breakRefs); + + if (!breakExternalReferences) + externalRefs.Clear(); + + breakRefs.UnionWith(externalRefs); + + foreach (var sourceComponent in sourceComponents) + { + var duplicatedComponent = __instance.AttachComponent(sourceComponent.GetType(), runOnAttachBehavior: false); + + internalRefs.RegisterCopy(sourceComponent, duplicatedComponent); + duplicatedComponent.CopyValues(sourceComponent, (from, to) => MemberCopy(from, to, internalRefs, breakRefs, checkTypes: false)); + duplicates.Add(duplicatedComponent); + } + + internalRefs.TransferReferences(true); + + foreach (var duplicate in duplicates) + duplicate.RunDuplicate(); + + Pool.Return(ref breakRefs); + Pool.Return(ref externalRefs); + + return false; + } + } +} \ No newline at end of file diff --git a/README.md b/README.md index af9b3d5..0f8c0c9 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,7 @@ The issues fixed by this mod will be linked in the following list. If any of them have been closed and not removed from the mod, just disable them in the settings in the meantime. +* Duplicating Components breaking drives (https://github.com/Yellow-Dog-Man/Resonite-Issues/issues/92) * Worlds crashing when a (multi)tool is scaled to zero (https://github.com/Yellow-Dog-Man/Resonite-Issues/issues/98) * Most ProtoFlux nodes in Strings > Constants having invisible names (https://github.com/Yellow-Dog-Man/Resonite-Issues/issues/177) * The `Remap -1 1 to 0 1` ProtoFlux node having a hard to understand name (https://github.com/Yellow-Dog-Man/Resonite-Issues/issues/245)