121 lines
3.5 KiB
C#
Raw Normal View History

2020-09-29 21:34:46 +01:00
using OWML.Common;
2020-12-14 16:24:52 +00:00
using QSB.Events;
2021-04-28 10:02:16 +01:00
using QSB.OrbSync.TransformSync;
2020-09-29 21:34:46 +01:00
using QSB.Utility;
using QSB.WorldSync;
2020-11-03 22:18:22 +00:00
using QSB.WorldSync.Events;
2020-12-04 22:15:41 +00:00
using QuantumUNET;
2020-12-07 21:19:16 +00:00
using QuantumUNET.Components;
2020-09-05 13:24:51 +01:00
using System.Linq;
2020-11-03 17:56:48 +00:00
namespace QSB.OrbSync.Events
{
2020-12-02 21:29:53 +00:00
public class OrbUserEvent : QSBEvent<WorldObjectMessage>
{
public override EventType Type => EventType.OrbUser;
2020-12-02 21:29:53 +00:00
public override void SetupListener() => GlobalMessenger<int>.AddListener(EventNames.QSBOrbUser, Handler);
public override void CloseListener() => GlobalMessenger<int>.RemoveListener(EventNames.QSBOrbUser, Handler);
2020-12-02 21:29:53 +00:00
private void Handler(int id) => SendEvent(CreateMessage(id));
2020-12-02 21:29:53 +00:00
private WorldObjectMessage CreateMessage(int id) => new WorldObjectMessage
{
AboutId = LocalPlayerId,
ObjectId = id
};
2020-12-19 13:31:05 +00:00
public override void OnReceiveLocal(bool server, WorldObjectMessage message)
{
if (server)
{
HandleServer(message);
}
else
{
HandleClient(message);
}
}
2020-12-11 22:42:21 +00:00
public override void OnReceiveRemote(bool server, WorldObjectMessage message)
2020-12-14 21:20:53 +00:00
{
if (server)
{
HandleServer(message);
}
else
{
HandleClient(message);
}
}
2020-12-14 21:41:56 +01:00
2020-12-14 21:20:53 +00:00
private static void HandleServer(WorldObjectMessage message)
{
2021-03-09 16:43:41 +00:00
var fromPlayer = QNetworkServer.connections.First(x => x.GetPlayerId() == message.FromId);
2021-04-28 16:32:01 +01:00
if (NomaiOrbTransformSync.OrbTransformSyncs == null || NomaiOrbTransformSync.OrbTransformSyncs.Count == 0)
2020-12-14 21:20:53 +00:00
{
2021-04-28 13:37:51 +01:00
DebugLog.ToConsole($"Error - OrbTransformSyncs is empty or null. (ID {message.ObjectId})", MessageType.Error);
2021-03-23 13:18:29 +00:00
return;
}
2021-06-18 22:38:32 +01:00
2021-03-23 13:18:29 +00:00
if (QSBWorldSync.OldOrbList == null || QSBWorldSync.OldOrbList.Count == 0)
{
DebugLog.ToConsole($"Error - OldOrbList is empty or null. (ID {message.ObjectId})", MessageType.Error);
2020-12-14 21:20:53 +00:00
return;
}
2021-06-18 22:38:32 +01:00
2020-12-14 21:20:53 +00:00
if (fromPlayer == null)
{
2020-12-19 18:44:27 +00:00
DebugLog.ToConsole("Error - FromPlayer is null!", MessageType.Error);
2020-12-14 21:20:53 +00:00
}
2021-06-18 22:38:32 +01:00
2021-04-28 16:32:01 +01:00
var orbSync = NomaiOrbTransformSync.OrbTransformSyncs
2021-04-28 10:02:16 +01:00
.FirstOrDefault(x => x.AttachedObject == QSBWorldSync.OldOrbList[message.ObjectId].gameObject);
2020-12-14 21:20:53 +00:00
if (orbSync == null)
{
DebugLog.ToConsole($"Error - No orb found for user event. (ID {message.ObjectId})", MessageType.Error);
return;
}
2021-06-18 22:38:32 +01:00
2020-12-23 12:58:45 +00:00
var orbIdentity = orbSync.GetComponent<QNetworkIdentity>();
2020-12-14 21:20:53 +00:00
if (orbIdentity == null)
{
DebugLog.ToConsole($"Error - Orb identity is null. (ID {message.ObjectId})", MessageType.Error);
return;
}
2021-06-18 22:38:32 +01:00
2020-12-14 21:20:53 +00:00
if (orbIdentity.ClientAuthorityOwner != null && orbIdentity.ClientAuthorityOwner != fromPlayer)
{
orbIdentity.RemoveClientAuthority(orbIdentity.ClientAuthorityOwner);
}
2021-06-18 22:38:32 +01:00
2020-12-14 21:20:53 +00:00
orbIdentity.AssignClientAuthority(fromPlayer);
orbSync.enabled = true;
}
2020-12-11 22:42:21 +00:00
2020-12-14 21:20:53 +00:00
private static void HandleClient(WorldObjectMessage message)
{
2021-04-28 16:32:01 +01:00
if (NomaiOrbTransformSync.OrbTransformSyncs == null || NomaiOrbTransformSync.OrbTransformSyncs.Count == 0)
2021-03-23 13:18:29 +00:00
{
2021-04-28 13:37:51 +01:00
DebugLog.ToConsole($"Error - OrbTransformSyncs is empty or null. (ID {message.ObjectId})", MessageType.Error);
2021-03-23 13:18:29 +00:00
return;
}
2021-06-18 22:38:32 +01:00
2021-03-23 13:18:29 +00:00
if (QSBWorldSync.OldOrbList == null || QSBWorldSync.OldOrbList.Count == 0)
2020-12-14 21:20:53 +00:00
{
2021-03-23 13:18:29 +00:00
DebugLog.ToConsole($"Error - OldOrbList is empty or null. (ID {message.ObjectId})", MessageType.Error);
2020-12-14 21:20:53 +00:00
return;
}
2021-06-18 22:38:32 +01:00
2021-04-28 16:32:01 +01:00
if (!NomaiOrbTransformSync.OrbTransformSyncs.Any(x => x.AttachedObject == QSBWorldSync.OldOrbList[message.ObjectId].gameObject))
2020-12-19 18:49:46 +00:00
{
DebugLog.ToConsole($"Error - No NomaiOrbTransformSync has AttachedOrb with objectId {message.ObjectId}!");
return;
}
2021-06-18 22:38:32 +01:00
2021-04-28 16:32:01 +01:00
var orb = NomaiOrbTransformSync.OrbTransformSyncs
2021-04-28 10:02:16 +01:00
.First(x => x.AttachedObject == QSBWorldSync.OldOrbList[message.ObjectId].gameObject);
2020-12-14 21:20:53 +00:00
orb.enabled = true;
}
}
2020-12-03 08:28:05 +00:00
}