Files
sbox-public/engine/Sandbox.Engine/Scene/Networking/DeltaSnapshots/SnapshotValueCache.cs
s&box team 71f266059a Open source release
This commit imports the C# engine code and game files, excluding C++ source code.

[Source-Commit: ceb3d758046e50faa6258bc3b658a30c97743268]
2025-11-24 09:05:18 +00:00

38 lines
823 B
C#

using Sandbox.Engine;
namespace Sandbox.Network;
internal class SnapshotValueCache
{
private Dictionary<int, byte[]> Serialized { get; } = new();
private Dictionary<int, object> Cache { get; } = new();
/// <summary>
/// Get cached bytes from the specified value if they exist. If the value is different
/// then re-serialize and cache again.
/// </summary>
public byte[] GetCached<T>( int slot, T value )
{
if ( Cache.TryGetValue( slot, out var cached ) && Equals( cached, value ) )
return Serialized[slot];
var bytes = GlobalContext.Current.TypeLibrary.ToBytes( value );
Serialized[slot] = bytes;
Cache[slot] = value;
return bytes;
}
public void Remove( int slot )
{
Serialized.Remove( slot );
Cache.Remove( slot );
}
public void Clear()
{
Serialized.Clear();
Cache.Clear();
}
}