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

View File

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