Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 97 additions & 0 deletions CommunityBugFixCollection/BreakDuplicatedComponentDrives.cs
Original file line number Diff line number Diff line change
@@ -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<Component>), typeof(bool), typeof(List<Component>)])]
internal sealed class BreakDuplicatedComponentDrives : ResoniteMonkey<BreakDuplicatedComponentDrives>
{
public override IEnumerable<string> Authors => Contributors.Banane9;

public override bool CanBeDisabled => true;

private static void CollectInternalReferences(List<Component> sourceComponents, InternalReferences internalRefs, HashSet<ISyncRef> externalRefs, HashSet<ISyncRef> breakRefs)
{
foreach (var component in sourceComponents)
{
var refList = Pool.BorrowList<ISyncRef>();
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<Component>();

// 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<Component> sourceComponents, bool breakExternalReferences, List<Component> duplicates)
{
if (!Enabled)
return true;

using var internalRefs = new InternalReferences();
var breakRefs = Pool.BorrowHashSet<ISyncRef>();
var externalRefs = Pool.BorrowHashSet<ISyncRef>();

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;
}
}
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down