96 lines
3.3 KiB
C#
96 lines
3.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using BoneSync.Networking.Messages;
|
|
using BoneSync.Sync;
|
|
using BoneSync.Sync.Components;
|
|
using HarmonyLib;
|
|
using MelonLoader;
|
|
using StressLevelZero.Interaction;
|
|
using static MelonLoader.MelonLogger;
|
|
|
|
namespace BoneSync.Patching
|
|
{
|
|
|
|
[HarmonyPatch(typeof(AlignPlug))]
|
|
public static class AlignPlugPatches
|
|
{
|
|
public static void OnPlugSocketChange(AlignPlug plug, Socket socket)
|
|
{
|
|
|
|
Syncable plugSyncable = plug.GetComponentInParent<Syncable>();
|
|
Syncable socketSyncable = socket?.GetComponentInParent<Syncable>();
|
|
|
|
if (!plugSyncable)
|
|
{
|
|
MelonLogger.Warning("PlugSyncable not found");
|
|
return;
|
|
}
|
|
|
|
if (!plugSyncable.Registered)
|
|
{
|
|
MelonLogger.Warning("PlugSyncable not registered");
|
|
return;
|
|
}
|
|
|
|
if (!socketSyncable)
|
|
{
|
|
MelonLogger.Warning("SocketSyncable not found");
|
|
}
|
|
|
|
if (socketSyncable && socketSyncable.isOwner && !plugSyncable.isOwner)
|
|
{
|
|
plugSyncable.TryBecomeOwner(true); // forcefully take ownership of the plug
|
|
}
|
|
|
|
byte plugId = plugSyncable.GetPlugId(plug);
|
|
byte socketId = socketSyncable ? socketSyncable.GetSocketId(socket) : byte.MaxValue;
|
|
|
|
MelonLogger.Msg("AlignPlug state: " + plug.transform.GetPath() + " Socket: " + socket.transform.GetPath() + " Plug ID: " + plugId + " Socket ID: " + socketId);
|
|
|
|
if (!plugSyncable.isOwner) return;
|
|
|
|
PlugSyncMessageData messageData = new PlugSyncMessageData
|
|
{
|
|
plugSyncId = plugSyncable.GetSyncId(),
|
|
plugIndex = plugId,
|
|
socketSyncId = socketSyncable ? socketSyncable.GetSyncId() : ushort.MinValue,
|
|
socketIndex = socketId
|
|
};
|
|
|
|
PlugSyncMessage plugSyncMessage = new PlugSyncMessage(messageData);
|
|
plugSyncMessage.Broadcast();
|
|
}
|
|
|
|
// for some reason AlignPlug.OnPlugInsertComplete can't be patched directly
|
|
/*
|
|
[HarmonyPatch(nameof(AlignPlug.OnPlugExitComplete)), HarmonyPostfix]
|
|
public static void AlignPlugEjectPatch(AlignPlug __instance)
|
|
{
|
|
MelonLogger.Msg("AlignPlug ejected: " + __instance.transform.GetPath());
|
|
OnPlugSocketChange(__instance, null);
|
|
}*/
|
|
|
|
}
|
|
|
|
[HarmonyPatch(typeof(MagazinePlug))]
|
|
public static class MagazinePlugPatches
|
|
{
|
|
[HarmonyPatch(nameof(MagazinePlug.OnPlugInsertComplete)), HarmonyPostfix]
|
|
public static void MagazinePlugInsertPatch(MagazinePlug __instance)
|
|
{
|
|
MelonLogger.Msg("MagazinePlug inserted: " + __instance.transform.GetPath());
|
|
AlignPlugPatches.OnPlugSocketChange(__instance, __instance.GetSocket());
|
|
}
|
|
|
|
[HarmonyPatch(nameof(MagazinePlug.OnPlugExitComplete)), HarmonyPostfix]
|
|
public static void MagazinePlugEjectPatch(MagazinePlug __instance)
|
|
{
|
|
MelonLogger.Msg("MagazinePlug ejected: " + __instance.transform.GetPath());
|
|
AlignPlugPatches.OnPlugSocketChange(__instance, null);
|
|
}
|
|
}
|
|
}
|