91 lines
2.0 KiB
C#
Raw Normal View History

using Mirror;
using QSB.Messaging;
2021-12-30 21:12:10 -08:00
using QSB.Player.TransformSync;
using QSB.WorldSync;
using System;
using UnityEngine;
namespace QSB.EyeOfTheUniverse.EyeStateSync.Messages;
internal class FlickerMessage : QSBMessage
2021-12-30 21:12:10 -08:00
{
public static bool IgnoreNextMessage;
static FlickerMessage() => GlobalMessenger<float, float>.AddListener(OWEvents.FlickerOffAndOn, Handler);
2021-12-30 21:12:10 -08:00
private static void Handler(float offDuration, float onDuration)
{
if (IgnoreNextMessage)
2021-12-30 21:12:10 -08:00
{
IgnoreNextMessage = false;
return;
2021-12-30 21:12:10 -08:00
}
if (PlayerTransformSync.LocalInstance)
2021-12-30 21:12:10 -08:00
{
new FlickerMessage(offDuration, onDuration).Send();
2021-12-30 21:12:10 -08:00
}
}
2021-12-30 21:12:10 -08:00
private float _offDuration;
private float _onDuration;
2021-12-30 21:12:10 -08:00
private FlickerMessage(float offDuration, float onDuration)
{
_offDuration = offDuration;
_onDuration = onDuration;
}
2021-12-30 21:12:10 -08:00
public override void Serialize(NetworkWriter writer)
{
base.Serialize(writer);
writer.Write(_offDuration);
writer.Write(_onDuration);
}
public override void Deserialize(NetworkReader reader)
{
base.Deserialize(reader);
_offDuration = reader.Read<float>();
_onDuration = reader.Read<float>();
}
public override bool ShouldReceive => QSBWorldSync.AllObjectsReady;
2021-12-30 21:12:10 -08:00
public override void OnReceiveRemote()
{
// manually fire callbacks
var eventTable = GlobalMessenger<float, float>.eventTable;
lock (eventTable)
2021-12-30 21:12:10 -08:00
{
var eventData = eventTable[OWEvents.FlickerOffAndOn];
if (eventData.isInvoking)
2021-12-30 21:12:10 -08:00
{
throw new InvalidOperationException("GlobalMessenger does not support recursive FireEvent calls to the same eventType.");
}
eventData.isInvoking = true;
eventData.temp.AddRange(eventData.callbacks);
foreach (var callback in eventData.temp)
{
// ignore callback for this message to prevent infinite loop
if (callback == Handler)
2021-12-30 21:12:10 -08:00
{
continue;
2021-12-30 21:12:10 -08:00
}
2022-01-01 22:19:10 +00:00
try
2021-12-30 21:12:10 -08:00
{
callback(_offDuration, _onDuration);
}
catch (Exception exception)
{
Debug.LogException(exception);
2021-12-30 21:12:10 -08:00
}
}
eventData.temp.Clear();
eventData.isInvoking = false;
2021-12-30 21:12:10 -08:00
}
}
}