Files
BoneSync/BoneSync/Sync/ObjectSyncCache.cs
2025-03-28 22:30:09 +02:00

162 lines
5.3 KiB
C#

using BoneSync.Sync.Components;
using StressLevelZero.Interaction;
using StressLevelZero.Pool;
using StressLevelZero.Props.Weapons;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnhollowerBaseLib;
using UnityEngine;
namespace BoneSync.Sync
{
public static class ObjectSyncCache
{
private static Dictionary<string, Syncable> _pathToSyncable = new Dictionary<string, Syncable>();
private static Dictionary<ushort, Syncable> _idToSyncable = new Dictionary<ushort, Syncable>();
private static Dictionary<int, Syncable> _componentToSyncable = new Dictionary<int, Syncable>();
private static Dictionary<Syncable, Component[]> _syncableToComponent = new Dictionary<Syncable, Component[]>();
public static Dictionary<ushort, Syncable> CallbackIdToSyncable = new Dictionary<ushort, Syncable>();
private static string GetComponentNamespace(Component component)
{
return component.GetType().Namespace;
}
private static bool IsCachableComponent(Component component)
{
if (component is MonoBehaviour) return true;
return false;
//string ns = GetComponentNamespace(component);
//return ns.StartsWith("StressLevelZero") || ns.StartsWith("BoneSync");
}
private static Component[] GetComponentsToCache(Syncable syncable)
{
Component[] components = syncable.GetComponentsInChildren<Component>(true);
List<Component> slzComponents = new List<Component>();
for (int i = 0; i < components.Length; i++)
{
if (IsCachableComponent(components[i]))
{
slzComponents.Add(components[i]);
}
}
return slzComponents.ToArray();
}
private static void _AddSyncableComponents(Syncable syncable)
{
Component[] components = GetComponentsToCache(syncable);
_syncableToComponent[syncable] = components;
foreach (Component component in components)
{
_componentToSyncable[component.GetHashCode()] = syncable;
}
}
private static void _RemoveSyncableComponents(Syncable syncable)
{
if (_syncableToComponent.ContainsKey(syncable))
{
Component[] components = _syncableToComponent[syncable];
foreach (Component component in components)
{
_componentToSyncable.Remove(component.GetHashCode());
}
_syncableToComponent.Remove(syncable);
}
}
private static void _AddSyncableKeys(Syncable syncable)
{
string path = syncable.GetSyncableWorldPath();
if (path != null && path != "")
{
_pathToSyncable[path] = syncable;
}
if (syncable.GetSyncId() != 0)
{
_idToSyncable[syncable.GetSyncId()] = syncable;
}
}
private static void _RemoveSyncableKeys(Syncable syncable)
{
string path = syncable.GetSyncableWorldPath();
if (path != null && path != "")
{
_pathToSyncable.Remove(path);
}
if (syncable.GetSyncId() != 0)
{
_idToSyncable.Remove(syncable.GetSyncId());
}
}
public static void AddSyncable(Syncable syncable)
{
_AddSyncableKeys(syncable);
_AddSyncableComponents(syncable);
}
public static void RemoveSyncable(Syncable syncable)
{
_RemoveSyncableKeys(syncable);
_RemoveSyncableComponents(syncable);
}
public static void UpdateSyncId(Syncable syncable)
{
// remove other enties where value is the same
ushort id = syncable.GetSyncId();
if (_idToSyncable.ContainsKey(id))
{
_idToSyncable.Remove(id);
}
_idToSyncable[id] = syncable;
}
public static Syncable GetSyncable(string path)
{
if (_pathToSyncable.ContainsKey(path))
{
return _pathToSyncable[path];
}
Transform transform = TransformExtensions.TransformFromPath(path);
if (transform)
{
return transform.GetComponent<Syncable>();
}
return null;
}
public static Syncable GetSyncable(ushort id)
{
if (_idToSyncable.ContainsKey(id))
{
return _idToSyncable[id];
}
return null;
}
public static Syncable GetSyncable(Component component)
{
if (_componentToSyncable.ContainsKey(component.GetHashCode()))
{
return _componentToSyncable[component.GetHashCode()];
}
return null;
}
public static void DISCARD_ALL_SYNCABLES()
{
foreach (Syncable syncable in Syncable.syncablesCache.Values)
{
syncable.DiscardSyncableImmediate(true, false);
}
}
}
}