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

53 lines
1.2 KiB
C#

using System.Collections;
namespace Sandbox;
internal partial class BytePack
{
class DictionaryPacker : Packer
{
internal override Identifier Header => Identifier.Dictionary;
public override void Write( ref ByteStream bs, object obj )
{
if ( obj is not IDictionary dict ) throw new NotSupportedException();
var kt = obj.GetType().GenericTypeArguments[0];
var vt = obj.GetType().GenericTypeArguments[1];
var kh = GetHandlerFor( kt );
var vh = GetHandlerFor( vt );
bs.Write( dict.Count );
kh.WriteTypeIdentifier( ref bs, kt );
vh.WriteTypeIdentifier( ref bs, vt );
foreach ( DictionaryEntry de in dict )
{
kh.Write( ref bs, de.Key );
vh.Write( ref bs, de.Value );
}
}
public override object Read( ref ByteStream bs )
{
var len = bs.Read<int>();
var keyHandler = GetHandlerFor( ref bs );
var valueHandler = GetHandlerFor( ref bs );
var dict = (IDictionary)Activator.CreateInstance( typeof( Dictionary<,> ).MakeGenericType( keyHandler.TargetType, valueHandler.TargetType ) );
for ( int i = 0; i < len; i++ )
{
var k = keyHandler.Read( ref bs );
var v = valueHandler.Read( ref bs );
dict.Add( k, v );
}
return dict;
}
}
}