3030
3131using MemoryPack ;
3232using System . Runtime . InteropServices ;
33+ using System . Diagnostics ;
3334
34- Console . WriteLine ( $ "{ RuntimeInformation . FrameworkDescription } ") ;
35+ CollectionTest sourceCollection = new CollectionTest ( ) ;
36+ sourceCollection . Collection . Add ( "1234" ) ;
37+ sourceCollection . Collection . Add ( "5678" ) ;
3538
36- //var r = new MemPackTestObj() { Strings = new[] { "a", "b", "c" } };
37- //var bytes = MemoryPackSerializer.Serialize(r);
39+ Pipe bufferPipe = new Pipe ( ) ;
40+ MemoryPackSerializer . Serialize ( bufferPipe . Writer , sourceCollection ) ;
41+ _ = await bufferPipe . Writer . FlushAsync ( ) . ConfigureAwait ( false ) ;
42+ ReadResult resultBuffer = await bufferPipe . Reader . ReadAsync ( ) . ConfigureAwait ( false ) ;
3843
39- var bytes = Convert . FromBase64String ( "AwMAAAD+////AQAAAGH+////AQAAAGL+////AQAAAGMAAAAAAAAAAP////8=" ) ;
40- Console . WriteLine ( Convert . ToBase64String ( bytes ) ) ;
41- var r2 = MemoryPackSerializer . Deserialize < MemPackTestObj > ( bytes ) ;
42- foreach ( var s in r2 ! . Strings )
43- {
44- Console . WriteLine ( s ) ;
45- }
4644
47- var value = new ListBytesSample ( ) ;
45+ //var newSource = new CollectionTest();
46+ var newSource = MemoryPackSerializer . Deserialize < CollectionTest > ( resultBuffer . Buffer ) ;
47+ Console . WriteLine ( newSource . Collection . Count ) ;
4848
49- var bin = MemoryPackSerializer . Serialize ( value ) ;
50- MemoryPackSerializer . Deserialize < ListBytesSample > ( bin , ref value ) ;
5149
52- // for efficient operation, you can get Span<T> by CollectionsMarshal
5350
54- //IServiceProvider provider = default!;
55-
56- //var a = MemoryPackSerializerOptions.Default with { ServiceProvider = provider };
5751
52+ [ MemoryPackable ]
53+ public partial class Region
54+ {
55+ public int positionX ;
56+ public int positionY ;
57+ public int positionZ ;
58+ public Dictionary < Vector3 , Chunk > chunks ;
59+ }
60+ [ MemoryPackable ]
61+ public partial class Chunk
62+ {
63+ public bool hasGeneratedBorders ;
64+ public int positionX ;
65+ public int positionY ;
66+ public int positionZ ;
67+ public List < Brush > brushes ;
68+ public Dictionary < ByteVector3 , int > brushBBPositions ;
69+ }
70+ [ MemoryPackable ]
71+ public partial class ByteVector3
72+ {
73+ public byte x , y , z ;
5874
75+ public override bool Equals ( object obj )
76+ {
77+ // If the object is null, return false.
78+ if ( obj == null || GetType ( ) != obj . GetType ( ) )
79+ {
80+ return false ;
81+ }
5982
83+ // Cast the object to ByteVector3 to compare values.
84+ ByteVector3 other = ( ByteVector3 ) obj ;
6085
86+ // Check if all components are equal.
87+ return x == other . x && y == other . y && z == other . z ;
88+ }
89+ public override int GetHashCode ( )
90+ {
91+ return System . HashCode . Combine ( x , y , z ) ;
92+ }
93+ }
94+ [ MemoryPackable ]
95+ public partial class Brush
96+ {
97+ public byte [ ] vertices ;
98+ public uint [ ] textures = new uint [ ] { 0 , 0 , 0 , 0 , 0 , 0 } ;
99+ public bool hiddenFlag ;
100+ public bool borderFlag ;
101+ }
61102
62103
63- var span = CollectionsMarshal . AsSpan ( value . Payload ) ;
104+ [ MemoryPackable ]
105+ public partial class CollectionTest
106+ {
107+ public Collection < string > Collection { get ; } = new Collection < string > ( ) ;
64108
109+ [ MemoryPackOnSerializing ]
110+ void OnSerializing2 ( )
111+ {
112+ Console . WriteLine ( nameof ( OnSerializing2 ) ) ;
113+ }
114+ }
65115
66116[ MemoryPackable ]
67117public partial record MemPackTestObj
@@ -75,9 +125,9 @@ public partial record MemPackTestObj
75125[ MemoryPackable ]
76126public partial class CctorSample
77127{
78- static partial void StaticConstructor ( )
79- {
80- }
128+ // static partial void StaticConstructor()
129+ // {
130+ // }
81131}
82132
83133[ MemoryPackable ]
@@ -86,11 +136,11 @@ public partial class ListBytesSample
86136 public int Id { get ; set ; }
87137 public List < byte > Payload { get ; set ; }
88138
89- static partial void StaticConstructor ( )
90- {
91- Console . WriteLine ( "foo" ) ;
92- // throw new NotImplementedException();
93- }
139+ // static partial void StaticConstructor()
140+ // {
141+ // Console.WriteLine("foo");
142+ // // throw new NotImplementedException();
143+ // }
94144}
95145
96146[ MemoryPackable ]
0 commit comments