Skip to content

Commit

Permalink
f
Browse files Browse the repository at this point in the history
  • Loading branch information
neuecc committed Jul 31, 2024
1 parent 62240eb commit 753a555
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 27 deletions.
102 changes: 76 additions & 26 deletions sandbox/SandboxConsoleApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,38 +30,88 @@

using MemoryPack;
using System.Runtime.InteropServices;

Check warning on line 32 in sandbox/SandboxConsoleApp/Program.cs

View workflow job for this annotation

GitHub Actions / build-dotnet

The using directive for 'System.Runtime.InteropServices' appeared previously in this namespace
using System.Diagnostics;

Console.WriteLine($"{RuntimeInformation.FrameworkDescription}");
CollectionTest sourceCollection = new CollectionTest();
sourceCollection.Collection.Add("1234");
sourceCollection.Collection.Add("5678");

//var r = new MemPackTestObj() { Strings = new[] { "a", "b", "c" } };
//var bytes = MemoryPackSerializer.Serialize(r);
Pipe bufferPipe = new Pipe();
MemoryPackSerializer.Serialize(bufferPipe.Writer, sourceCollection);
_ = await bufferPipe.Writer.FlushAsync().ConfigureAwait(false);
ReadResult resultBuffer = await bufferPipe.Reader.ReadAsync().ConfigureAwait(false);

var bytes = Convert.FromBase64String("AwMAAAD+////AQAAAGH+////AQAAAGL+////AQAAAGMAAAAAAAAAAP////8=");
Console.WriteLine(Convert.ToBase64String(bytes));
var r2 = MemoryPackSerializer.Deserialize<MemPackTestObj>(bytes);
foreach (var s in r2!.Strings)
{
Console.WriteLine(s);
}

var value = new ListBytesSample();
//var newSource = new CollectionTest();
var newSource = MemoryPackSerializer.Deserialize<CollectionTest>(resultBuffer.Buffer);
Console.WriteLine(newSource.Collection.Count);

var bin = MemoryPackSerializer.Serialize(value);
MemoryPackSerializer.Deserialize<ListBytesSample>(bin, ref value);

// for efficient operation, you can get Span<T> by CollectionsMarshal

//IServiceProvider provider = default!;

//var a = MemoryPackSerializerOptions.Default with { ServiceProvider = provider };

[MemoryPackable]
public partial class Region
{
public int positionX;
public int positionY;
public int positionZ;
public Dictionary<Vector3, Chunk> chunks;
}
[MemoryPackable]
public partial class Chunk
{
public bool hasGeneratedBorders;
public int positionX;
public int positionY;
public int positionZ;
public List<Brush> brushes;
public Dictionary<ByteVector3, int> brushBBPositions;
}
[MemoryPackable]
public partial class ByteVector3
{
public byte x, y, z;

public override bool Equals(object obj)

Check warning on line 75 in sandbox/SandboxConsoleApp/Program.cs

View workflow job for this annotation

GitHub Actions / build-dotnet

Nullability of type of parameter 'obj' doesn't match overridden member (possibly because of nullability attributes).
{
// If the object is null, return false.
if (obj == null || GetType() != obj.GetType())
{
return false;
}

// Cast the object to ByteVector3 to compare values.
ByteVector3 other = (ByteVector3)obj;

// Check if all components are equal.
return x == other.x && y == other.y && z == other.z;
}
public override int GetHashCode()
{
return System.HashCode.Combine(x, y, z);
}
}
[MemoryPackable]
public partial class Brush
{
public byte[] vertices;
public uint[] textures = new uint[] { 0, 0, 0, 0, 0, 0 };
public bool hiddenFlag;
public bool borderFlag;
}


var span = CollectionsMarshal.AsSpan(value.Payload);
[MemoryPackable]
public partial class CollectionTest
{
public Collection<string> Collection { get; } = new Collection<string>();

[MemoryPackOnSerializing]
void OnSerializing2()
{
Console.WriteLine(nameof(OnSerializing2));
}
}

[MemoryPackable]
public partial record MemPackTestObj
Expand All @@ -75,9 +125,9 @@ public partial record MemPackTestObj
[MemoryPackable]
public partial class CctorSample
{
static partial void StaticConstructor()
{
}
//static partial void StaticConstructor()
//{
//}
}

[MemoryPackable]
Expand All @@ -86,11 +136,11 @@ public partial class ListBytesSample
public int Id { get; set; }
public List<byte> Payload { get; set; }

static partial void StaticConstructor()
{
Console.WriteLine("foo");
// throw new NotImplementedException();
}
//static partial void StaticConstructor()
//{
// Console.WriteLine("foo");
// // throw new NotImplementedException();
//}
}

[MemoryPackable]
Expand Down
2 changes: 1 addition & 1 deletion src/MemoryPack.Generator/ReferenceSymbols.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public ReferenceSymbols(Compilation compilation)
MemoryPackOnSerializedAttribute = GetTypeByMetadataName("MemoryPack.MemoryPackOnSerializedAttribute");
MemoryPackOnDeserializingAttribute = GetTypeByMetadataName("MemoryPack.MemoryPackOnDeserializingAttribute");
MemoryPackOnDeserializedAttribute = GetTypeByMetadataName("MemoryPack.MemoryPackOnDeserializedAttribute");
SkipOverwriteDefaultAttribute = GetTypeByMetadataName("MemoryPack.SuppressDefaultInitialization");
SkipOverwriteDefaultAttribute = GetTypeByMetadataName("MemoryPack.SuppressDefaultInitializationAttribute");
GenerateTypeScriptAttribute = GetTypeByMetadataName(MemoryPackGenerator.GenerateTypeScriptAttributeFullName);
IMemoryPackable = GetTypeByMetadataName("MemoryPack.IMemoryPackable`1").ConstructUnboundGenericType();
KnownTypes = new WellKnownTypes(this);
Expand Down

0 comments on commit 753a555

Please sign in to comment.