mirror of
https://github.com/NotAKidoS/NAK_CVR_Mods.git
synced 2025-09-02 06:19:22 +00:00
[RelativeSync] Fixed buncha stuff, cleanup
This commit is contained in:
parent
b97265ef37
commit
b786ecd51c
11 changed files with 429 additions and 231 deletions
|
@ -3,149 +3,149 @@ using ABI_RC.Systems.ModNetwork;
|
|||
using DarkRift;
|
||||
using UnityEngine;
|
||||
|
||||
namespace NAK.RelativeSync.Networking
|
||||
namespace NAK.RelativeSync.Networking;
|
||||
|
||||
public static class ModNetwork
|
||||
{
|
||||
public static class ModNetwork
|
||||
public static bool Debug_NetworkInbound = false;
|
||||
public static bool Debug_NetworkOutbound = false;
|
||||
|
||||
private static bool _isSubscribedToModNetwork;
|
||||
|
||||
private struct MovementParentSyncData
|
||||
{
|
||||
public static bool Debug_NetworkInbound = false;
|
||||
public static bool Debug_NetworkOutbound = false;
|
||||
|
||||
private static bool _isSubscribedToModNetwork;
|
||||
|
||||
private struct RelativeSyncData
|
||||
{
|
||||
public bool HasSyncedThisData;
|
||||
public int MarkerHash;
|
||||
public Vector3 Position;
|
||||
public Vector3 Rotation;
|
||||
}
|
||||
|
||||
private static RelativeSyncData _latestRelativeSyncData;
|
||||
|
||||
#region Constants
|
||||
|
||||
private const string ModId = "MelonMod.NAK.RelativeSync";
|
||||
|
||||
#endregion
|
||||
|
||||
#region Enums
|
||||
|
||||
private enum MessageType : byte
|
||||
{
|
||||
SyncPosition = 0,
|
||||
RelativeSyncStatus = 1
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Mod Network Internals
|
||||
|
||||
internal static void Subscribe()
|
||||
{
|
||||
ModNetworkManager.Subscribe(ModId, OnMessageReceived);
|
||||
|
||||
_isSubscribedToModNetwork = ModNetworkManager.IsSubscribed(ModId);
|
||||
if (!_isSubscribedToModNetwork)
|
||||
Debug.LogError("Failed to subscribe to Mod Network!");
|
||||
}
|
||||
|
||||
internal static void SendRelativeSyncUpdate()
|
||||
{
|
||||
if (!_isSubscribedToModNetwork)
|
||||
return;
|
||||
|
||||
if (!_latestRelativeSyncData.HasSyncedThisData)
|
||||
{
|
||||
SendMessage(MessageType.SyncPosition, _latestRelativeSyncData.MarkerHash,
|
||||
_latestRelativeSyncData.Position, _latestRelativeSyncData.Rotation);
|
||||
_latestRelativeSyncData.HasSyncedThisData = true;
|
||||
}
|
||||
}
|
||||
|
||||
private static void SetLatestRelativeSync(int markerHash, Vector3 position, Vector3 rotation)
|
||||
{
|
||||
// check if the data has changed
|
||||
if (_latestRelativeSyncData.MarkerHash == markerHash
|
||||
&& _latestRelativeSyncData.Position == position
|
||||
&& _latestRelativeSyncData.Rotation == rotation)
|
||||
return; // no need to update
|
||||
|
||||
_latestRelativeSyncData.HasSyncedThisData = false; // reset
|
||||
_latestRelativeSyncData.MarkerHash = markerHash;
|
||||
_latestRelativeSyncData.Position = position;
|
||||
_latestRelativeSyncData.Rotation = rotation;
|
||||
}
|
||||
|
||||
private static void SendMessage(MessageType messageType, int markerHash, Vector3 position, Vector3 rotation)
|
||||
{
|
||||
if (!IsConnectedToGameNetwork())
|
||||
return;
|
||||
|
||||
using ModNetworkMessage modMsg = new(ModId);
|
||||
modMsg.Write((byte)messageType);
|
||||
modMsg.Write(markerHash);
|
||||
modMsg.Write(position);
|
||||
modMsg.Write(rotation);
|
||||
modMsg.Send();
|
||||
}
|
||||
|
||||
private static void OnMessageReceived(ModNetworkMessage msg)
|
||||
{
|
||||
msg.Read(out byte msgTypeRaw);
|
||||
|
||||
if (!Enum.IsDefined(typeof(MessageType), msgTypeRaw))
|
||||
return;
|
||||
|
||||
switch ((MessageType)msgTypeRaw)
|
||||
{
|
||||
case MessageType.SyncPosition:
|
||||
msg.Read(out int markerHash);
|
||||
msg.Read(out Vector3 receivedPosition);
|
||||
msg.Read(out Vector3 receivedRotation);
|
||||
OnNetworkPositionUpdateReceived(msg.Sender, markerHash, receivedPosition, receivedRotation);
|
||||
break;
|
||||
// case MessageType.RelativeSyncStatus:
|
||||
// msg.Read(out string guidStr);
|
||||
// msg.Read(out bool isRelativeSync);
|
||||
// System.Guid guid = new System.Guid(guidStr);
|
||||
// OnRelativeSyncStatusReceived(msg.Sender, guid, isRelativeSync);
|
||||
// break;
|
||||
default:
|
||||
Debug.LogError($"Invalid message type received from: {msg.Sender}");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Public Methods
|
||||
|
||||
public static void SendNetworkPosition(int markerHash, Vector3 newPosition, Vector3 newRotation)
|
||||
{
|
||||
SetLatestRelativeSync(markerHash, newPosition, newRotation);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private Methods
|
||||
|
||||
private static bool IsConnectedToGameNetwork()
|
||||
{
|
||||
return NetworkManager.Instance != null
|
||||
&& NetworkManager.Instance.GameNetwork != null
|
||||
&& NetworkManager.Instance.GameNetwork.ConnectionState == ConnectionState.Connected;
|
||||
}
|
||||
|
||||
private static void OnNetworkPositionUpdateReceived(string sender, int markerHash, Vector3 position, Vector3 rotation)
|
||||
{
|
||||
RelativeSyncManager.ApplyRelativeSync(sender, markerHash, position, rotation);
|
||||
}
|
||||
|
||||
private static void OnRelativeSyncStatusReceived(string sender, System.Guid guid, bool isRelativeSync)
|
||||
{
|
||||
// todo: implement
|
||||
}
|
||||
|
||||
#endregion
|
||||
public bool HasSyncedThisData;
|
||||
public int MarkerHash;
|
||||
public Vector3 RootPosition;
|
||||
public Vector3 RootRotation;
|
||||
// public Vector3 HipPosition;
|
||||
// public Vector3 HipRotation;
|
||||
}
|
||||
|
||||
private static MovementParentSyncData _latestMovementParentSyncData;
|
||||
|
||||
#region Constants
|
||||
|
||||
private const string ModId = "MelonMod.NAK.RelativeSync";
|
||||
|
||||
#endregion
|
||||
|
||||
#region Enums
|
||||
|
||||
private enum MessageType : byte
|
||||
{
|
||||
MovementParentOrChair = 0
|
||||
//RelativePickup = 1,
|
||||
//RelativeAttachment = 2,
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Mod Network Internals
|
||||
|
||||
internal static void Subscribe()
|
||||
{
|
||||
ModNetworkManager.Subscribe(ModId, OnMessageReceived);
|
||||
|
||||
_isSubscribedToModNetwork = ModNetworkManager.IsSubscribed(ModId);
|
||||
if (!_isSubscribedToModNetwork)
|
||||
Debug.LogError("Failed to subscribe to Mod Network!");
|
||||
}
|
||||
|
||||
// Called right after NetworkRootDataUpdate.Submit()
|
||||
internal static void SendRelativeSyncUpdate()
|
||||
{
|
||||
if (!_isSubscribedToModNetwork)
|
||||
return;
|
||||
|
||||
if (_latestMovementParentSyncData.HasSyncedThisData)
|
||||
return;
|
||||
|
||||
SendMessage(MessageType.MovementParentOrChair, _latestMovementParentSyncData.MarkerHash,
|
||||
_latestMovementParentSyncData.RootPosition, _latestMovementParentSyncData.RootRotation);
|
||||
|
||||
_latestMovementParentSyncData.HasSyncedThisData = true;
|
||||
}
|
||||
|
||||
public static void SetLatestRelativeSync(
|
||||
int markerHash,
|
||||
Vector3 position, Vector3 rotation)
|
||||
{
|
||||
// check if the data has changed
|
||||
if (_latestMovementParentSyncData.MarkerHash == markerHash
|
||||
&& _latestMovementParentSyncData.RootPosition == position
|
||||
&& _latestMovementParentSyncData.RootRotation == rotation)
|
||||
return; // no need to update (shocking)
|
||||
|
||||
_latestMovementParentSyncData.HasSyncedThisData = false; // reset
|
||||
_latestMovementParentSyncData.MarkerHash = markerHash;
|
||||
_latestMovementParentSyncData.RootPosition = position;
|
||||
_latestMovementParentSyncData.RootRotation = rotation;
|
||||
}
|
||||
|
||||
private static void SendMessage(MessageType messageType, int markerHash, Vector3 position, Vector3 rotation)
|
||||
{
|
||||
if (!IsConnectedToGameNetwork())
|
||||
return;
|
||||
|
||||
using ModNetworkMessage modMsg = new(ModId);
|
||||
modMsg.Write((byte)messageType);
|
||||
modMsg.Write(markerHash);
|
||||
modMsg.Write(position);
|
||||
modMsg.Write(rotation);
|
||||
modMsg.Send();
|
||||
|
||||
if (Debug_NetworkOutbound)
|
||||
Debug.Log(
|
||||
$"[Outbound] MessageType: {messageType}, MarkerHash: {markerHash}, Position: {position}, " +
|
||||
$"Rotation: {rotation}");
|
||||
}
|
||||
|
||||
private static void OnMessageReceived(ModNetworkMessage msg)
|
||||
{
|
||||
msg.Read(out byte msgTypeRaw);
|
||||
|
||||
if (!Enum.IsDefined(typeof(MessageType), msgTypeRaw))
|
||||
return;
|
||||
|
||||
switch ((MessageType)msgTypeRaw)
|
||||
{
|
||||
case MessageType.MovementParentOrChair:
|
||||
msg.Read(out int markerHash);
|
||||
msg.Read(out Vector3 receivedPosition);
|
||||
msg.Read(out Vector3 receivedRotation);
|
||||
// msg.Read(out Vector3 receivedHipPosition);
|
||||
// msg.Read(out Vector3 receivedHipRotation);
|
||||
|
||||
OnNetworkPositionUpdateReceived(msg.Sender, markerHash, receivedPosition, receivedRotation);
|
||||
|
||||
if (Debug_NetworkInbound)
|
||||
Debug.Log($"[Inbound] Sender: {msg.Sender}, MarkerHash: {markerHash}, " +
|
||||
$"Position: {receivedPosition}, Rotation: {receivedRotation}");
|
||||
break;
|
||||
default:
|
||||
Debug.LogError($"Invalid message type received from: {msg.Sender}");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private Methods
|
||||
|
||||
private static bool IsConnectedToGameNetwork()
|
||||
{
|
||||
return NetworkManager.Instance != null
|
||||
&& NetworkManager.Instance.GameNetwork != null
|
||||
&& NetworkManager.Instance.GameNetwork.ConnectionState == ConnectionState.Connected;
|
||||
}
|
||||
|
||||
private static void OnNetworkPositionUpdateReceived(
|
||||
string sender, int markerHash,
|
||||
Vector3 position, Vector3 rotation)
|
||||
{
|
||||
RelativeSyncManager.ApplyRelativeSync(sender, markerHash, position, rotation);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue