Simple event queue for better experience

This commit is contained in:
Aaro Varis
2025-03-16 13:25:25 +02:00
parent 2f27e39610
commit 922c33d4a1
2 changed files with 37 additions and 10 deletions

View File

@@ -16,6 +16,7 @@ namespace BoneSync.Sync.Components
// SyncableNetworking.cs
public partial class Syncable : MonoBehaviour
{
private Queue<byte[]> _simpleEventQueue = new Queue<byte[]>();
private void SendObjectSync()
{
_lastSyncTime = Time.realtimeSinceStartup;
@@ -45,6 +46,13 @@ namespace BoneSync.Sync.Components
{
SyncLogger.Msg("Setting owner for " + _syncId + " to " + ownerId);
_ownerId = ownerId;
if (!isOwner)
{
_simpleEventQueue.Clear();
} else
{
TryCatchUpSimpleEvents();
}
FindAndUpdateComponents();
MelonCoroutines.Start(SyncCoroutineAsync());
UpdateKinematic();
@@ -62,8 +70,8 @@ namespace BoneSync.Sync.Components
private IEnumerator __SendRegisterSyncCo()
{
yield return null; // wait a frame
SetOwner(BoneSync.lobby.GetLocalId());
SetSyncId(ObjectSync.SendRegisterSyncableMessage(this));
SetOwner(BoneSync.lobby.GetLocalId());
yield break;
}
@@ -133,6 +141,21 @@ namespace BoneSync.Sync.Components
return true;
}
private void AddSimpleEventToQueue(SimpleEventType eType, byte index = 0, byte len = 0)
{
_simpleEventQueue.Enqueue(new byte[] { (byte)eType, index, len });
TryCatchUpSimpleEvents();
}
private void TryCatchUpSimpleEvents()
{
if (_simpleEventQueue.Count > 0 && Registered && isOwner)
{
byte[] eventData = _simpleEventQueue.Dequeue();
_SendSimpleEvent((SimpleEventType)eventData[0], eventData[1], eventData[2]);
TryCatchUpSimpleEvents();
}
}
private void _SendSimpleEvent(SimpleEventType eType, byte index = 0, byte len = 0)
{
SyncLogger.Msg("Sending simple event: " + eType);

View File

@@ -24,9 +24,8 @@ namespace BoneSync.Sync.Components
SyncLogger.Msg("ButtonToggle:" + eventType + " " + toggle.transform.GetPath());
byte index = (byte)Array.IndexOf(buttonToggles, toggle);
RegisterSyncable();
if (!Registered) return false;
if (!isOwner) return false;
_SendSimpleEvent(eventType, index);
if (!isOwner && Registered) return false;
AddSimpleEventToQueue(eventType, index);
return true;
}
bool ButtonOnPress(ButtonToggle toggle)
@@ -43,15 +42,17 @@ namespace BoneSync.Sync.Components
return OnButtonEvent(toggle, SimpleEventType.OnButtonOneShot);
}
void DeviceOnPull()
bool DeviceOnPull(PullDevice device)
{
if (!isOwner) { return; }
_SendSimpleEvent(SimpleEventType.OnDevicePull);
if (!isOwner && Registered) { return false; }
AddSimpleEventToQueue(SimpleEventType.OnDevicePull);
return true;
}
void DeviceOnRelease()
bool DeviceOnRelease(PullDevice device)
{
if (!isOwner) { return; }
_SendSimpleEvent(SimpleEventType.OnDeviceRelease);
if (!isOwner && Registered) { return false; }
AddSimpleEventToQueue(SimpleEventType.OnDeviceRelease);
return true;
}
private void TryPatchUnityEvents()
@@ -76,6 +77,9 @@ namespace BoneSync.Sync.Components
//pullDevice.OnHandlePull.AddListener((UnityAction)DeviceOnPull);
//pullDevice.OnHandleReturn.AddListener((UnityAction)DeviceOnRelease);
UnityEventPatch<PullDevice>.Patch(pullDevice, pullDevice.OnHandlePull, DeviceOnPull);
UnityEventPatch<PullDevice>.Patch(pullDevice, pullDevice.OnHandleReturn, DeviceOnRelease);
pullDevicePatched = true;
}