Files
BoneSync/BoneSync/Sync/ObjectSyncCache.cs
2025-03-03 16:23:40 +02:00

91 lines
2.7 KiB
C#

using BoneSync.Sync.Components;
using StressLevelZero.Interaction;
using StressLevelZero.Pool;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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<Component, Syncable> _componentToSyncable = new Dictionary<Component, Syncable>();
public static Dictionary<ushort, Syncable> CallbackIdToSyncable = new Dictionary<ushort, Syncable>();
public static void AddSyncable(Syncable syncable)
{
string path = syncable.GetSyncableWorldPath();
if (path != null && path != "")
{
_pathToSyncable[path] = syncable;
}
if (syncable.GetSyncId() != 0)
{
_idToSyncable[syncable.GetSyncId()] = syncable;
}
}
public static void RemoveSyncable(Syncable syncable)
{
string path = syncable.GetSyncableWorldPath();
if (path != null && path != "")
{
_pathToSyncable.Remove(path);
}
if (syncable.GetSyncId() != 0)
{
_idToSyncable.Remove(syncable.GetSyncId());
}
}
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 void DISCARD_ALL_SYNCABLES()
{
foreach (Syncable syncable in Syncable.syncablesCache.Values)
{
syncable.DiscardSyncable();
}
}
}
}