Cleaned up and more use of Ready

This commit is contained in:
2026-01-08 20:11:21 +01:00
parent af87ffea42
commit bcf830d52d
2 changed files with 213 additions and 197 deletions

View File

@@ -87,16 +87,6 @@ namespace Marro.PacManUdon
/// </summary>
[SerializeField] private SyncedObject[] syncedObjects;
/// <summary>
/// Whether the current perspective is the transmitting side.
/// </summary>
private bool isOwner;
/// <summary>
/// Whether the current perspective is synced with the owner. (Always true if current perspective is owner.)
/// </summary>
private bool isSynced;
/// <summary>
/// Offset from system time to network time, including delay.
/// </summary>
@@ -147,6 +137,11 @@ namespace Marro.PacManUdon
/// </summary>
public bool Ready { get; private set; } = false;
/// <summary>
/// Whether the current perspective is synced with the owner. (Always true if current perspective is owner.)
/// </summary>
public bool Synced { get; private set; } = false;
/// <summary>
/// The time since last full sync which is currently being simulated.
/// </summary>
@@ -167,7 +162,7 @@ namespace Marro.PacManUdon
/// <summary>
/// Is the local user owner?
/// </summary>
public bool IsOwner => isOwner;
public bool IsOwner { get; private set; }
#endregion
#region General
@@ -181,8 +176,7 @@ namespace Marro.PacManUdon
if (!BitConverter.IsLittleEndian)
{
Debug.LogError($"({nameof(PacManUdon)} {nameof(NetworkManager)}) Fatal: NetworkManager only supports little endian! Network sync will not be possible.");
var zero = 0;
Debug.Log(1 / zero); // Intentionally crash
Ready = false;
return;
}
@@ -190,7 +184,7 @@ namespace Marro.PacManUdon
buffer = new byte[BufferMaxTotalEvents][];
bufferIndex = 0;
isSynced = isOwner; // Owner is always synced
Synced = IsOwner; // Owner is always synced
retriesWithoutSuccess = 0;
lastEventTimestamp = 0;
lastEventId = 0;
@@ -207,17 +201,20 @@ namespace Marro.PacManUdon
public void FixedUpdate()
{
// Fetch the current time
UpdateInternalTime();
if (!isOwner)
// If able and needed, process received events
if (Ready && !IsOwner)
{
ProgressEventTime();
}
// Forwards simulated time at the FixedUpdate pace
PerformFixedSyncedUpdate();
}
public void UpdateInternalTime()
private void UpdateInternalTime()
{
internalTime = Time.fixedTime - offsetTime;
}
@@ -240,8 +237,7 @@ namespace Marro.PacManUdon
if (retriesWithoutSuccess > 3)
{
Debug.LogError($"({nameof(PacManUdon)} {nameof(NetworkManager)}) Fatal: Retried 3 times without success.");
var zero = 0;
Debug.Log(1 / zero); // Intentionally crash
Ready = false;
return;
}
@@ -252,9 +248,9 @@ namespace Marro.PacManUdon
ClearBuffer();
}
isSynced = false;
Synced = false;
if (!isOwner)
if (!IsOwner)
{
RequestEvent(NetworkEventType.FullSync);
}
@@ -266,7 +262,7 @@ namespace Marro.PacManUdon
private void SetOwner(bool isOwner)
{
this.isOwner = isOwner;
this.IsOwner = isOwner;
if (DebugImageToIndicateOwner != null)
{
@@ -278,7 +274,12 @@ namespace Marro.PacManUdon
#region Sender
public void SendEvent(NetworkEventType eventType)
{
if (!isOwner)
if (!Ready)
{
return;
}
if (!IsOwner)
{
Debug.LogError($"({nameof(PacManUdon)} {nameof(NetworkManager)}) Attempted {nameof(SendEvent)} while not the owner!");
return;
@@ -345,6 +346,11 @@ namespace Marro.PacManUdon
[NetworkCallable]
public void RequestEventReceived(NetworkEventType eventType)
{
if (!Ready)
{
return;
}
SendEvent(eventType);
}
#endregion
@@ -352,7 +358,12 @@ namespace Marro.PacManUdon
#region Receiver
public void RequestEvent(NetworkEventType eventType)
{
if (isOwner)
if (!Ready)
{
return;
}
if (IsOwner)
{
Debug.LogError($"({nameof(PacManUdon)} {nameof(NetworkManager)}) Attempted {nameof(RequestEvent)} while we are the owner!");
return;
@@ -415,7 +426,7 @@ namespace Marro.PacManUdon
continue;
}
if (!isSynced)
if (!Synced)
{
Debug.LogWarning($"({nameof(PacManUdon)} {nameof(NetworkManager)}) Received event of type {eventType} while we are not yet synced to the remote time, ignoring event.");
continue;
@@ -463,7 +474,7 @@ namespace Marro.PacManUdon
// Immediately apply the full sync
UpdateNextEventTime(ignoreOrder: true);
isSynced = true;
Synced = true;
}
private void ProgressEventTime()
@@ -605,7 +616,7 @@ namespace Marro.PacManUdon
#endregion
#region Header
public static byte GetNextEventId(byte currentEventId)
private static byte GetNextEventId(byte currentEventId)
{
if (currentEventId == byte.MaxValue) // Udon forces overflow checks
{
@@ -617,22 +628,27 @@ namespace Marro.PacManUdon
return currentEventId;
}
public static ushort GetEventSizeFromHeader(byte[] @event, int eventIndex = 0)
private static ushort GetEventSizeFromHeader(byte[] @event, int eventIndex = 0)
=> BitConverter.ToUInt16(@event, eventIndex + HeaderEventSizeIndex);
public static NetworkEventType GetEventTypeFromHeader(byte[] @event, int eventIndex = 0) =>
private static NetworkEventType GetEventTypeFromHeader(byte[] @event, int eventIndex = 0) =>
(NetworkEventType)@event[eventIndex + HeaderEventTypeIndex];
public static float GetTimestampFromHeader(byte[] @event, int eventIndex = 0) =>
private static float GetTimestampFromHeader(byte[] @event, int eventIndex = 0) =>
BitConverter.ToSingle(@event, eventIndex + HeaderTimestampIndex);
public static byte GetEventIdFromHeader(byte[] @event, int eventIndex = 0) =>
private static byte GetEventIdFromHeader(byte[] @event, int eventIndex = 0) =>
@event[eventIndex + HeaderEventIdIndex];
#endregion
#region VRC events
public override void OnOwnershipTransferred(VRCPlayerApi newOwner)
{
if (!Ready)
{
return;
}
SetOwner(newOwner == Networking.LocalPlayer);
}
@@ -645,7 +661,7 @@ namespace Marro.PacManUdon
return;
}
if (isOwner)
if (IsOwner)
{
if (buffer == null || bufferIndex == 0)
{
@@ -674,7 +690,7 @@ namespace Marro.PacManUdon
return;
}
if (!isOwner || networkedData.Length == 0)
if (!IsOwner || networkedData.Length == 0)
{
return;
}
@@ -688,7 +704,7 @@ namespace Marro.PacManUdon
public override void OnDeserialization()
{
if (!Ready || isOwner)
if (!Ready || IsOwner)
{
return;
}
@@ -756,7 +772,7 @@ namespace Marro.PacManUdon
public void WriteDebugOutput(TMP_InputField debugOutput)
{
debugOutput.text += $"{nameof(NetworkManager)}:\n" +
$"IsOwner: {isOwner}\n" +
$"IsOwner: {IsOwner}\n" +
$"Ready: {Ready}\n" +
$"Time.fixedTime: {Time.fixedTime}\n" +
$"offsetTime: {offsetTime}\n" +