mirror of
https://github.com/Facepunch/sbox-public.git
synced 2026-01-02 11:28:19 -05:00
This commit imports the C# engine code and game files, excluding C++ source code. [Source-Commit: ceb3d758046e50faa6258bc3b658a30c97743268]
38 lines
823 B
C#
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();
|
|
}
|
|
}
|