Skip to content

Commit

Permalink
[Proto] Implemented Simplest Generator
Browse files Browse the repository at this point in the history
  • Loading branch information
Linwenxuan04 committed Mar 26, 2024
1 parent 0d1b421 commit 78f9d9e
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 1 deletion.
100 changes: 99 additions & 1 deletion KonataNT.Proto.Generator/ProtoSourceGenerator.Emitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ private static void Generate(TypeDeclarationSyntax syntax, Compilation compilati
#pragma warning disable CA1050 // Declare types in namespaces.
using System;
using System.Text;
using KonataNT.Proto;
using KonataNT.Proto.Serialization;
");
Expand Down Expand Up @@ -138,7 +139,104 @@ private static void EmitDeserializer(TypeDeclarationSyntax syntax, ContextVisito

sb.AppendLine($" public static IProto Deserialize(byte[] buffer)");
sb.AppendLine(" {");
sb.AppendLine($" return new {className}();");
sb.AppendLine($" var {className} = new {className}();");
sb.AppendLine();
sb.AppendLine(" var reader = new ProtoReader(buffer.AsSpan());");
sb.AppendLine(" while (!reader.EndOfStream)");
sb.AppendLine(" {");
sb.AppendLine(" uint tag = reader.ReadTag();");
sb.AppendLine(" switch (tag)");
sb.AppendLine(" {");

foreach (var meta in context.List)
{
EmitMemberDeserialize(sb, meta, className, meta.Name);
}

sb.AppendLine(" }");
sb.AppendLine(" }");
sb.AppendLine($" return {className};");
sb.AppendLine(" }");
}

private static void EmitMemberDeserialize(StringBuilder sb, ProtoMemberMeta meta, string className, string name)
{
string type = meta.Type.ToString().Replace("?", "");

sb.AppendLine($" case {meta.Tag}:");
sb.AppendLine(" {");
if (meta.IsEnumerable)
{
sb.AppendLine($" if ({name} == null) {name} = new {meta.Type}();");
switch (meta.WireType)
{
case WireType.LengthDelimited:
sb.AppendLine($" var {name}Length = reader.ReadVarInt<uint>();");
sb.AppendLine($" var {name}Buffer = reader.ReadRawBytes({name}Length);");
if (meta.IsNested)
{
sb.AppendLine($" {name}.Add(({type}){type}.Deserialize({name}Buffer));");
}
else if (meta.Type.ToString() == "string" || meta.Type.ToString() == "string?")
{
sb.AppendLine($" {name}.Add(Encoding.UTF8.GetString({name}Buffer));");
}
else
{
sb.AppendLine($" {name}.Add({name}Buffer;)");
}
break;
case WireType.VarInt:
if (type == "bool")
{
sb.AppendLine($" var result = reader.ReadVarInt<byte>();");
sb.AppendLine($" {name}.Add(result != 0);");
}
else
{
sb.AppendLine($" {className}.{name} = reader.ReadVarInt<{type}>();");
}
break;
default:
throw new NotImplementedException();
}
}
else
{
switch (meta.WireType)
{
case WireType.LengthDelimited:
sb.AppendLine($" var {name}Length = reader.ReadVarInt<uint>();");
sb.AppendLine($" var {name}Buffer = reader.ReadRawBytes({name}Length);");
if (meta.IsNested)
{
sb.AppendLine($" {className}.{name} = ({type}){type}.Deserialize({name}Buffer);");
}
else if (meta.Type.ToString() == "string" || meta.Type.ToString() == "string?")
{
sb.AppendLine($" {className}.{name} = Encoding.UTF8.GetString({name}Buffer);");
}
else
{
sb.AppendLine($" {className}.{name} = {name}Buffer;");
}
break;
case WireType.VarInt:
if (type == "bool")
{
sb.AppendLine($" var result = reader.ReadVarInt<byte>();");
sb.AppendLine($" {className}.{name} = result != 0;");
}
else
{
sb.AppendLine($" {className}.{name} = reader.ReadVarInt<{type}>();");
}
break;
default:
throw new NotImplementedException();
}
}
sb.AppendLine(" break;");
sb.AppendLine(" }");
}
}
7 changes: 7 additions & 0 deletions KonataNT.Proto/Serialization/ProtoReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public ref struct ProtoReader
private readonly ReadOnlySpan<byte> _span;

private uint _position;

public bool EndOfStream => _position == Length;

private readonly ref byte First => ref MemoryMarshal.GetReference(_span);

Expand Down Expand Up @@ -135,6 +137,11 @@ public byte[] ReadRawBytes(uint length)
return ReadRawBytesSpan(length).ToArray();
}

public uint ReadTag()
{
return ReadVarInt<uint>() >> 3;
}

[DoesNotReturn]
public static void ThrowMalformedMessage()
{
Expand Down

0 comments on commit 78f9d9e

Please sign in to comment.