Skip to content

Commit

Permalink
Refactor net writing / reading to support general serialization (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
Metapyziks committed Sep 14, 2023
1 parent 4978a4d commit 44f41bb
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 76 deletions.
41 changes: 21 additions & 20 deletions libsdf/code/2D/Sdf2D.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;

namespace Sandbox.Sdf;

Expand Down Expand Up @@ -128,14 +129,14 @@ public float this[Vector2 pos]
}
}

public void WriteRaw( NetWrite writer )
public void WriteRaw( NetWrite writer, Dictionary<TypeDescription, int> sdfTypes )
{
writer.Write( Min );
writer.Write( Max );
writer.Write( CornerRadius );
}

public static RectSdf ReadRaw( ref NetRead reader )
public static RectSdf ReadRaw( ref NetRead reader, IReadOnlyDictionary<int, SdfReader<ISdf2D>> sdfTypes )
{
return new RectSdf(
reader.Read<Vector2>(),
Expand All @@ -157,13 +158,13 @@ public record struct CircleSdf( Vector2 Center, float Radius ) : ISdf2D
/// <inheritdoc />
public float this[Vector2 pos] => (pos - Center).Length - Radius;

public void WriteRaw( NetWrite writer )
public void WriteRaw( NetWrite writer, Dictionary<TypeDescription, int> sdfTypes )
{
writer.Write( Center );
writer.Write( Radius );
}

public static CircleSdf ReadRaw( ref NetRead reader )
public static CircleSdf ReadRaw( ref NetRead reader, IReadOnlyDictionary<int, SdfReader<ISdf2D>> sdfTypes )
{
return new CircleSdf(
reader.Read<Vector2>(),
Expand Down Expand Up @@ -220,14 +221,14 @@ public float this[Vector2 pos]
}
}

public void WriteRaw( NetWrite writer )
public void WriteRaw( NetWrite writer, Dictionary<TypeDescription, int> sdfTypes )
{
writer.Write( PointA );
writer.Write( PointB );
writer.Write( Radius );
}

public static LineSdf ReadRaw( ref NetRead reader )
public static LineSdf ReadRaw( ref NetRead reader, IReadOnlyDictionary<int, SdfReader<ISdf2D>> sdfTypes )
{
return new LineSdf(
reader.Read<Vector2>(),
Expand Down Expand Up @@ -363,7 +364,7 @@ public float this[Vector2 pos]
}
}

public void WriteRaw( NetWrite writer )
public void WriteRaw( NetWrite writer, Dictionary<TypeDescription, int> sdfTypes )
{
writer.Write( _texture.ResourcePath );
writer.Write( _gradientWidthPixels );
Expand All @@ -372,7 +373,7 @@ public void WriteRaw( NetWrite writer )
writer.Write( _worldOffset );
}

public static TextureSdf ReadRaw( ref NetRead reader )
public static TextureSdf ReadRaw( ref NetRead reader, IReadOnlyDictionary<int, SdfReader<ISdf2D>> sdfTypes )
{
return new TextureSdf(
Texture.Load( FileSystem.Mounted, reader.ReadString() ),
Expand Down Expand Up @@ -415,18 +416,18 @@ public TransformedSdf2D( T sdf, Transform2D transform )
/// <inheritdoc />
public float this[Vector2 pos] => Sdf[Transform.InverseTransformPoint( pos )] * Transform.InverseScale;

public void WriteRaw( NetWrite writer )
public void WriteRaw( NetWrite writer, Dictionary<TypeDescription, int> sdfTypes )
{
Sdf.Write( writer );
Sdf.Write( writer, sdfTypes );
writer.Write( Transform.Position );
writer.Write( Transform.Rotation );
writer.Write( Transform.Scale );
}

public static TransformedSdf2D<T> ReadRaw( ref NetRead reader )
public static TransformedSdf2D<T> ReadRaw( ref NetRead reader, IReadOnlyDictionary<int, SdfReader<ISdf2D>> sdfTypes )
{
return new TransformedSdf2D<T>(
(T)ISdf2D.Read( ref reader ),
(T)ISdf2D.Read( ref reader, sdfTypes ),
new Transform2D(
reader.Read<Vector2>(),
reader.Read<Rotation2D>(),
Expand All @@ -446,16 +447,16 @@ public record struct TranslatedSdf2D<T>( T Sdf, Vector2 Offset ) : ISdf2D
/// <inheritdoc />
public float this[Vector2 pos] => Sdf[pos - Offset];

public void WriteRaw( NetWrite writer )
public void WriteRaw( NetWrite writer, Dictionary<TypeDescription, int> sdfTypes )
{
Sdf.Write( writer );
Sdf.Write( writer, sdfTypes );
writer.Write( Offset );
}

public static TranslatedSdf2D<T> ReadRaw( ref NetRead reader )
public static TranslatedSdf2D<T> ReadRaw( ref NetRead reader, IReadOnlyDictionary<int, SdfReader<ISdf2D>> sdfTypes )
{
return new TranslatedSdf2D<T>(
(T) ISdf2D.Read( ref reader ),
(T) ISdf2D.Read( ref reader, sdfTypes ),
reader.Read<Vector2>() );
}
}
Expand All @@ -472,16 +473,16 @@ public record struct ExpandedSdf2D<T>( T Sdf, float Margin ) : ISdf2D
/// <inheritdoc />
public float this[Vector2 pos] => Sdf[pos] - Margin;

public void WriteRaw( NetWrite writer )
public void WriteRaw( NetWrite writer, Dictionary<TypeDescription, int> sdfTypes )
{
Sdf.Write( writer );
Sdf.Write( writer, sdfTypes );
writer.Write( Margin );
}

public static ExpandedSdf2D<T> ReadRaw( ref NetRead reader )
public static ExpandedSdf2D<T> ReadRaw( ref NetRead reader, IReadOnlyDictionary<int, SdfReader<ISdf2D>> sdfTypes )
{
return new ExpandedSdf2D<T>(
(T) ISdf2D.Read( ref reader ),
(T) ISdf2D.Read( ref reader, sdfTypes ),
reader.Read<float>() );
}
}
5 changes: 3 additions & 2 deletions libsdf/code/3D/Noise/Cellular.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Buffers;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

Expand Down Expand Up @@ -138,14 +139,14 @@ async Task ISdf3D.SampleRangeAsync( Transform transform, float[] output, (int X,
}
}

public void WriteRaw( NetWrite writer )
public void WriteRaw( NetWrite writer, Dictionary<TypeDescription, int> sdfTypes )
{
writer.Write( Seed );
writer.Write( CellSize );
writer.Write( DistanceOffset );
}

public static CellularNoiseSdf3D ReadRaw( ref NetRead reader )
public static CellularNoiseSdf3D ReadRaw( ref NetRead reader, IReadOnlyDictionary<int, SdfReader<ISdf3D>> sdfTypes )
{
return new CellularNoiseSdf3D( reader.Read<int>(), reader.Read<Vector3>(), reader.Read<float>() );
}
Expand Down
56 changes: 28 additions & 28 deletions libsdf/code/3D/Sdf3D.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,14 +172,14 @@ public float this[Vector3 pos]
}
}

public void WriteRaw( NetWrite writer )
public void WriteRaw( NetWrite writer, Dictionary<TypeDescription, int> sdfTypes )
{
writer.Write( Min );
writer.Write( Max );
writer.Write( CornerRadius );
}

public static BoxSdf3D ReadRaw( ref NetRead reader )
public static BoxSdf3D ReadRaw( ref NetRead reader, IReadOnlyDictionary<int, SdfReader<ISdf3D>> sdfTypes )
{
return new BoxSdf3D(
reader.Read<Vector3>(),
Expand All @@ -201,13 +201,13 @@ public record struct SphereSdf3D( Vector3 Center, float Radius ) : ISdf3D
/// <inheritdoc />
public float this[Vector3 pos] => (pos - Center).Length - Radius;

public void WriteRaw( NetWrite writer )
public void WriteRaw( NetWrite writer, Dictionary<TypeDescription, int> sdfTypes )
{
writer.Write( Center );
writer.Write( Radius );
}

public static SphereSdf3D ReadRaw( ref NetRead reader )
public static SphereSdf3D ReadRaw( ref NetRead reader, IReadOnlyDictionary<int, SdfReader<ISdf3D>> sdfTypes )
{
return new SphereSdf3D(
reader.Read<Vector3>(),
Expand Down Expand Up @@ -265,14 +265,14 @@ public float this[Vector3 pos]
}
}

public void WriteRaw( NetWrite writer )
public void WriteRaw( NetWrite writer, Dictionary<TypeDescription, int> sdfTypes )
{
writer.Write( PointA );
writer.Write( PointB );
writer.Write( Radius );
}

public static CapsuleSdf3D ReadRaw( ref NetRead reader )
public static CapsuleSdf3D ReadRaw( ref NetRead reader, IReadOnlyDictionary<int, SdfReader<ISdf3D>> sdfTypes )
{
return new CapsuleSdf3D(
reader.Read<Vector3>(),
Expand Down Expand Up @@ -304,17 +304,17 @@ Task ISdf3D.SampleRangeAsync( Transform transform, float[] output, (int X, int Y
return Sdf.SampleRangeAsync( Transform.ToLocal( transform ), output, outputSize );
}

public void WriteRaw( NetWrite writer )
public void WriteRaw( NetWrite writer, Dictionary<TypeDescription, int> sdfTypes )
{
Sdf.Write( writer );
Sdf.Write( writer, sdfTypes );
writer.Write( Transform.Position );
writer.Write( Transform.Rotation );
writer.Write( Transform.Scale );
}

public static TransformedSdf3D<T> ReadRaw( ref NetRead reader )
public static TransformedSdf3D<T> ReadRaw( ref NetRead reader, IReadOnlyDictionary<int, SdfReader<ISdf3D>> sdfTypes )
{
return new TransformedSdf3D<T>( (T) ISdf3D.Read( ref reader ),
return new TransformedSdf3D<T>( (T) ISdf3D.Read( ref reader, sdfTypes ),
new Transform(
reader.Read<Vector3>(),
reader.Read<Rotation>(),
Expand All @@ -339,15 +339,15 @@ Task ISdf3D.SampleRangeAsync( Transform transform, float[] output, (int X, int Y
return Sdf.SampleRangeAsync( new Transform( Offset ).ToLocal( transform ), output, outputSize );
}

public void WriteRaw( NetWrite writer )
public void WriteRaw( NetWrite writer, Dictionary<TypeDescription, int> sdfTypes )
{
Sdf.Write( writer );
Sdf.Write( writer, sdfTypes );
writer.Write( Offset );
}

public static TranslatedSdf3D<T> ReadRaw( ref NetRead reader )
public static TranslatedSdf3D<T> ReadRaw( ref NetRead reader, IReadOnlyDictionary<int, SdfReader<ISdf3D>> sdfTypes )
{
return new TranslatedSdf3D<T>( (T) ISdf3D.Read( ref reader ), reader.Read<Vector3>() );
return new TranslatedSdf3D<T>( (T) ISdf3D.Read( ref reader, sdfTypes ), reader.Read<Vector3>() );
}
}

Expand Down Expand Up @@ -375,15 +375,15 @@ async Task ISdf3D.SampleRangeAsync( Transform transform, float[] output, (int X,
}
}

public void WriteRaw( NetWrite writer )
public void WriteRaw( NetWrite writer, Dictionary<TypeDescription, int> sdfTypes )
{
Sdf.Write( writer );
Sdf.Write( writer, sdfTypes );
writer.Write( Margin );
}

public static ExpandedSdf3D<T> ReadRaw( ref NetRead reader )
public static ExpandedSdf3D<T> ReadRaw( ref NetRead reader, IReadOnlyDictionary<int, SdfReader<ISdf3D>> sdfTypes )
{
return new ExpandedSdf3D<T>( (T) ISdf3D.Read( ref reader ), reader.Read<float>() );
return new ExpandedSdf3D<T>( (T) ISdf3D.Read( ref reader, sdfTypes ), reader.Read<float>() );
}
}

Expand Down Expand Up @@ -425,15 +425,15 @@ await GameTask.WhenAll(
}
}

public void WriteRaw( NetWrite writer )
public void WriteRaw( NetWrite writer, Dictionary<TypeDescription, int> sdfTypes )
{
Sdf1.Write( writer );
Sdf2.Write( writer );
Sdf1.Write( writer, sdfTypes );
Sdf2.Write( writer, sdfTypes );
}

public static IntersectedSdf3D<T1, T2> ReadRaw( ref NetRead reader )
public static IntersectedSdf3D<T1, T2> ReadRaw( ref NetRead reader, IReadOnlyDictionary<int, SdfReader<ISdf3D>> sdfTypes )
{
return new IntersectedSdf3D<T1, T2>( (T1) ISdf3D.Read( ref reader ), (T2) ISdf3D.Read( ref reader ) );
return new IntersectedSdf3D<T1, T2>( (T1) ISdf3D.Read( ref reader, sdfTypes ), (T2) ISdf3D.Read( ref reader, sdfTypes ) );
}
}

Expand Down Expand Up @@ -475,16 +475,16 @@ await GameTask.WhenAll(
}
}

public void WriteRaw( NetWrite writer )
public void WriteRaw( NetWrite writer, Dictionary<TypeDescription, int> sdfTypes )
{
Sdf.Write( writer );
BiasSdf.Write( writer );
Sdf.Write( writer, sdfTypes );
BiasSdf.Write( writer, sdfTypes );
writer.Write( BiasScale );
}

public static BiasedSdf3D<T, TBias> ReadRaw( ref NetRead reader )
public static BiasedSdf3D<T, TBias> ReadRaw( ref NetRead reader, IReadOnlyDictionary<int, SdfReader<ISdf3D>> sdfTypes )
{
return new BiasedSdf3D<T, TBias>( (T) ISdf3D.Read( ref reader ), (TBias) ISdf3D.Read( ref reader ), reader.Read<float>() );
return new BiasedSdf3D<T, TBias>( (T) ISdf3D.Read( ref reader, sdfTypes ), (TBias) ISdf3D.Read( ref reader, sdfTypes ), reader.Read<float>() );
}
}
}
Loading

0 comments on commit 44f41bb

Please sign in to comment.