quantum-space-buddies/QSB/WorldSync/QSBWorldSync.cs
Mister_Nebula 10af92f4a1 changes
2020-12-11 22:42:21 +00:00

84 lines
2.3 KiB
C#

using OWML.Common;
using QSB.OrbSync;
using QSB.TransformSync;
using QSB.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace QSB.WorldSync
{
public static class QSBWorldSync
{
private static readonly List<WorldObject> WorldObjects = new List<WorldObject>();
public static List<NomaiOrbTransformSync> OrbSyncList = new List<NomaiOrbTransformSync>();
public static List<NomaiInterfaceOrb> OldOrbList = new List<NomaiInterfaceOrb>();
public static List<CharacterDialogueTree> OldDialogueTrees = new List<CharacterDialogueTree>();
public static Dictionary<string, bool> DialogueConditions { get; } = new Dictionary<string, bool>();
public static void AddWorldObject(WorldObject worldObject)
{
if (WorldObjects.Contains(worldObject))
{
return;
}
WorldObjects.Add(worldObject);
}
public static IEnumerable<T> GetWorldObjects<T>()
{
return WorldObjects.OfType<T>();
}
public static T GetWorldObject<T>(int id) where T : WorldObject
{
return GetWorldObjects<T>().FirstOrDefault(x => x.ObjectId == id);
}
public static void RemoveWorldObjects<T>() where T : WorldObject
{
WorldObjects.RemoveAll(x => x.GetType() == typeof(T));
}
public static void HandleSlotStateChange(NomaiInterfaceSlot slot, NomaiInterfaceOrb affectingOrb, bool state)
{
var slotList = GetWorldObjects<QSBOrbSlot>();
if (slotList.Count() == 0)
{
return;
}
var qsbSlot = slotList.First(x => x.InterfaceSlot == slot);
var orbSync = OrbSyncList.First(x => x.AttachedOrb == affectingOrb);
if (orbSync.HasAuthority)
{
qsbSlot.HandleEvent(state, OldOrbList.IndexOf(affectingOrb));
}
}
public static void RaiseEvent(object instance, string eventName)
{
if (!(instance.GetType()
.GetField(eventName, BindingFlags.Instance | BindingFlags.NonPublic)?
.GetValue(instance) is MulticastDelegate multiDelegate))
{
return;
}
var delegateList = multiDelegate.GetInvocationList().ToList();
foreach (var del in delegateList)
{
del.DynamicInvoke(instance);
}
}
public static void SetDialogueCondition(string name, bool state)
{
if (!QSB.IsServer)
{
DebugLog.DebugWrite("Warning - Cannot write to condition dict when not server!", MessageType.Warning);
return;
}
DialogueConditions[name] = state;
}
}
}