Skip to content

Commit 1223f18

Browse files
committed
[Proto] Serialize the fields as uint to avoid signed extend
1 parent 52264f7 commit 1223f18

9 files changed

Lines changed: 18 additions & 18 deletions

File tree

Lagrange.Proto.Generator/ProtoSourceGenerator.Emitter.Measure.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ private void EmitConstant(SourceWriter source)
6060
int field = kv.Key;
6161
var info = kv.Value;
6262

63-
var tag = ProtoHelper.EncodeVarInt(field << 3 | (byte)info.WireType);
63+
var tag = ProtoHelper.EncodeVarInt((uint)field << 3 | (byte)info.WireType);
6464
if (info.TypeSymbol.IsValueType && !info.TypeSymbol.IsNullable()) constant += tag.Length;
6565
}
6666

@@ -69,7 +69,7 @@ private void EmitConstant(SourceWriter source)
6969

7070
private void EmitLengthStatement(SourceWriter source, int field, ProtoFieldInfo info)
7171
{
72-
int tag = field << 3 | (byte)info.WireType;
72+
uint tag = (uint)field << 3 | (byte)info.WireType;
7373
var encodedTag = ProtoHelper.EncodeVarInt(tag);
7474

7575
string memberName;
@@ -130,7 +130,7 @@ WireType.VarInt when info.TypeSymbol.IsIntegerType() => $"{GetVarIntLengthMethod
130130

131131
private static string GenerateIfNotDefaultExpression(string variableName, string left, string right) => $"({variableName} != default ? {left} : {right})";
132132

133-
private string GenerateShouldSerializeExpression(int tag, string left, string right) =>
133+
private string GenerateShouldSerializeExpression(uint tag, string left, string right) =>
134134
$"({_fullQualifiedName}.{TypeInfoPropertyName}.Fields[{tag}].{ShouldSerializeTypeRef}({ObjectVarName}, {parser.IgnoreDefaultFields.ToString().ToLower()}) ? {left} : {right})";
135135
}
136136
}

Lagrange.Proto.Generator/ProtoSourceGenerator.Emitter.Serialize.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ private void EmitSerializeMethod(SourceWriter source)
4747

4848
private void EmitMembers(SourceWriter source, int field, ProtoFieldInfo info)
4949
{
50-
int tag = field << 3 | (byte)info.WireType;
50+
uint tag = (uint)field << 3 | (byte)info.WireType;
5151
var encodedTag = ProtoHelper.EncodeVarInt(tag);
5252

5353
string memberName = info.TypeSymbol.IsValueType && info.TypeSymbol.IsNullable()
@@ -161,7 +161,7 @@ private static void EmitIfNotDefaultStatement(SourceWriter source, string variab
161161
source.WriteLine("}");
162162
}
163163

164-
private void EmitIfShouldSerializeStatement(SourceWriter source, int tag, Action<SourceWriter> emitAction)
164+
private void EmitIfShouldSerializeStatement(SourceWriter source, uint tag, Action<SourceWriter> emitAction)
165165
{
166166
source.WriteLine($"if ({_fullQualifiedName}.{TypeInfoPropertyName}.Fields[{tag}].{ShouldSerializeTypeRef}({ObjectVarName}, {parser.IgnoreDefaultFields.ToString().ToLower()}))");
167167
source.WriteLine("{");

Lagrange.Proto.Generator/ProtoSourceGenerator.Emitter.TypeInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ private void EmitTypeInfo(SourceWriter source)
6161
source.WriteLine('{');
6262
source.Indentation++;
6363

64-
source.WriteLine($"Fields = new global::System.Collections.Generic.Dictionary<int, {string.Format(ProtoFieldInfoTypeRef)}>()");
64+
source.WriteLine($"Fields = new global::System.Collections.Generic.Dictionary<uint, {string.Format(ProtoFieldInfoTypeRef)}>()");
6565
source.WriteLine('{');
6666
source.Indentation++;
6767
foreach (var kv in parser.Fields)

Lagrange.Proto.Generator/Utility/ProtoHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace Lagrange.Proto.Generator.Utility;
66

77
public static class ProtoHelper
88
{
9-
public static byte[] EncodeVarInt(int value)
9+
public static byte[] EncodeVarInt(uint value)
1010
{
1111
Span<byte> result = stackalloc byte[5];
1212
int i = 0;

Lagrange.Proto/Nodes/ProtoObject.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public override void WriteTo(int field, ProtoWriter writer)
1414

1515
foreach (var (f, node) in _fields)
1616
{
17-
writer.EncodeVarInt(f << 3 | (int)node.WireType);
17+
writer.EncodeVarInt((uint)f << 3 | (uint)node.WireType);
1818
node.WriteTo(f, writer);
1919
}
2020
}
@@ -26,7 +26,7 @@ public override int Measure(int field)
2626
{
2727
if (node is ProtoArray { Count: 0 }) continue;
2828

29-
size += ProtoHelper.GetVarIntLength(f << 3 | (int)node.WireType);
29+
size += ProtoHelper.GetVarIntLength((uint)f << 3 | (uint)node.WireType);
3030
size += node.Measure(f);
3131
}
3232
return size;
@@ -89,7 +89,7 @@ public byte[] Serialize()
8989
{
9090
if (node is ProtoArray { Count: 0 }) continue;
9191

92-
writer.EncodeVarInt(f << 3 | (int)node.WireType);
92+
writer.EncodeVarInt((uint)f << 3 | (uint)node.WireType);
9393
node.WriteTo(f, writer);
9494
}
9595
writer.Flush();
@@ -109,7 +109,7 @@ public void Serialize(IBufferWriter<byte> buffer)
109109
{
110110
foreach (var (f, node) in _fields)
111111
{
112-
writer.EncodeVarInt(f << 3 | (int)node.WireType);
112+
writer.EncodeVarInt((uint)f << 3 | (uint)node.WireType);
113113
node.WriteTo(f, writer);
114114
}
115115
writer.Flush();

Lagrange.Proto/Serialization/Converter/Object/ProtoObjectConverter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public override T Read(int field, WireType wireType, ref ProtoReader reader)
5757
var subReader = new ProtoReader(subSpan);
5858
while (!subReader.IsCompleted)
5959
{
60-
int tag = subReader.DecodeVarIntUnsafe<int>();
60+
uint tag = subReader.DecodeVarIntUnsafe<uint>();
6161
if (ObjectInfo.Fields.TryGetValue(tag, out var fieldInfo))
6262
{
6363
fieldInfo.Read(ref subReader, boxed);

Lagrange.Proto/Serialization/Metadata/ProtoObjectInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace Lagrange.Proto.Serialization.Metadata;
55
[DebuggerDisplay("Fields = {Fields.Count}")]
66
public class ProtoObjectInfo<T>
77
{
8-
public Dictionary<int, ProtoFieldInfo> Fields { get; init; } = new();
8+
public Dictionary<uint, ProtoFieldInfo> Fields { get; init; } = new();
99

1010
public Func<T>? ObjectCreator { get; init; }
1111

Lagrange.Proto/Serialization/Metadata/ProtoTypeResolver.Dynamic.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,15 @@ internal static ProtoObjectInfo<T> CreateObjectInfo<T>()
6666
{
6767
var ctor = typeof(T).IsValueType ? null : typeof(T).GetConstructor(Type.EmptyTypes);
6868
bool ignoreDefaultFields = typeof(T).GetCustomAttribute<ProtoPackableAttribute>()?.IgnoreDefaultFields == true;
69-
var fields = new Dictionary<int, ProtoFieldInfo>();
69+
var fields = new Dictionary<uint, ProtoFieldInfo>();
7070

7171
foreach (var field in typeof(T).GetFields(BindingFlags.Public | BindingFlags.Instance))
7272
{
7373
if (field.IsStatic) continue;
7474
var fieldInfo = CreateFieldInfo(typeof(T), field);
7575
if (fieldInfo == null) continue;
7676

77-
int tag = (fieldInfo.Field << 3) | (byte)fieldInfo.WireType;
77+
uint tag = ((uint)fieldInfo.Field << 3) | (byte)fieldInfo.WireType;
7878
if (fields.ContainsKey(tag)) ThrowHelper.ThrowInvalidOperationException_DuplicateField(typeof(T), fieldInfo.Field);
7979
fields[tag] = fieldInfo;
8080
}
@@ -84,7 +84,7 @@ internal static ProtoObjectInfo<T> CreateObjectInfo<T>()
8484
var fieldInfo = CreateFieldInfo(typeof(T), field);
8585
if (fieldInfo == null) continue;
8686

87-
int tag = (fieldInfo.Field << 3) | (byte)fieldInfo.WireType;
87+
uint tag = ((uint)fieldInfo.Field << 3) | (byte)fieldInfo.WireType;
8888
if (fields.ContainsKey(tag)) ThrowHelper.ThrowInvalidOperationException_DuplicateField(typeof(T), fieldInfo.Field);
8989
fields[tag] = fieldInfo;
9090
}

Lagrange.Proto/Serialization/ProtoSerializer.Deserialize.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ private static T DeserializeProtoPackableCore<T>(ref ProtoReader reader) where T
2929

3030
while (!reader.IsCompleted)
3131
{
32-
int tag = reader.DecodeVarIntUnsafe<int>();
32+
uint tag = reader.DecodeVarIntUnsafe<uint>();
3333
if (objectInfo.Fields.TryGetValue(tag, out var fieldInfo))
3434
{
3535
fieldInfo.Read(ref reader, target);
@@ -86,7 +86,7 @@ private static T DeserializeProtoPackableCore<T>(ref ProtoReader reader) where T
8686

8787
while (!reader.IsCompleted)
8888
{
89-
int tag = reader.DecodeVarIntUnsafe<int>();
89+
uint tag = reader.DecodeVarIntUnsafe<uint>();
9090
if (converter.ObjectInfo.Fields.TryGetValue(tag, out var fieldInfo))
9191
{
9292
fieldInfo.Read(ref reader, boxed);

0 commit comments

Comments
 (0)