Files
sbox-public/engine/Sandbox.System/Utility/BytePack/BytePack.Packer.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

73 lines
1.5 KiB
C#

using System.Threading;
namespace Sandbox;
internal partial class BytePack
{
public class Packer
{
protected static readonly ThreadLocal<HashSet<object>> _visited = new( () => new HashSet<object>( ReferenceEqualityComparer.Instance ) );
public virtual Type TargetType { get; }
internal virtual Identifier Header { get; }
internal virtual int TypeIdentifier { get; }
public virtual void Write( ref ByteStream bs, object obj )
{
throw new NotImplementedException();
}
public virtual object Read( ref ByteStream data )
{
throw new NotImplementedException();
}
public virtual void WriteTypeIdentifier( ref ByteStream bs, Type targetType )
{
bs.Write( Header );
}
BytePack parent;
internal void Init( BytePack bytePack )
{
parent = bytePack;
if ( TargetType is not null )
{
parent.types[TargetType] = this;
}
if ( TypeIdentifier != default )
{
parent.typeHandler[TypeIdentifier] = this;
}
parent.handlers[Header] = this;
}
internal Packer GetHandlerFor( ref ByteStream bs )
{
var ident = bs.Read<Identifier>();
if ( ident == 0 )
return null;
if ( ident == Identifier.Runtime )
{
return parent.GetOrCreatePacker( bs.Read<int>() );
}
return parent.handlers[ident];
}
internal Packer GetHandlerFor( Type type )
{
return parent.GetOrCreatePacker( type );
}
internal object Deserialize( ref ByteStream bs ) => parent.Deserialize( ref bs );
internal void Serialize( ref ByteStream bs, object obj ) => parent.Serialize( ref bs, obj );
}
}