Skip to content

Commit

Permalink
修复浮点数序列化问题
Browse files Browse the repository at this point in the history
  • Loading branch information
davyxu committed Aug 21, 2020
1 parent 953521a commit 7485c37
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 69 deletions.
6 changes: 3 additions & 3 deletions api/csharp/ProtoPlus/OutputStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -272,8 +272,8 @@ public void WriteUInt64(int fieldIndex, List<ulong> value)
}

public void WriteFloat(int fieldIndex, float value)
{
if (value < float.Epsilon)
{
if( value == 0F)
{
return;
}
Expand Down Expand Up @@ -307,7 +307,7 @@ public void WriteFloat(int fieldIndex, List<float> value)

public void WriteDouble(int fieldIndex, double value)
{
if (value < double.Epsilon)
if (value == 0D)
{
return;
}
Expand Down
6 changes: 5 additions & 1 deletion api/csharp/ProtoPlus/ProtoPlus.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DefineConstants>TRACE;DEBUG;NETCOREAPP2_0</DefineConstants>
</PropertyGroup>

</Project>
4 changes: 2 additions & 2 deletions api/csharp/ProtoPlus/SizeCaculator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public static int SizeEnum<T>(int fieldIndex, List<T> value) where T : struct, I

public static int SizeFloat(int fieldIndex, float value)
{
if (value < float.Epsilon)
if (value == 0F)
{
return 0;
}
Expand All @@ -100,7 +100,7 @@ public static int SizeFloat(int fieldIndex, List<float> value)

public static int SizeDouble(int fieldIndex, double value)
{
if (value < double.Epsilon)
if (value == 0D)
{
return 0;
}
Expand Down
6 changes: 5 additions & 1 deletion example/csharp/Example/Example.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
<TargetFramework>netcoreapp2.0</TargetFramework>
<StartupObject>Example.Program</StartupObject>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DefineConstants>TRACE;DEBUG;NETCOREAPP2_0</DefineConstants>
</PropertyGroup>

<ItemGroup>
<Compile Remove="FastBitConverter.cs" />
<Compile Remove="InputStream.cs" />
Expand Down
116 changes: 55 additions & 61 deletions example/csharp/Example/ProtoGen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,19 @@ public partial class MyTypeMini : IProtoStruct

#region Serialize Code
public void Init( )
{

{
}

public void Marshal(OutputStream stream)
{
stream.WriteBool(1, Bool );
stream.WriteInt32(2, Int32 );
stream.WriteUInt32(3, UInt32 );
stream.WriteInt64(4, Int64 );
stream.WriteUInt64(5, UInt64 );
stream.WriteFloat(6, Float32 );
stream.WriteDouble(7, Float64 );
stream.WriteString(8, Str );
stream.WriteBool(1, Bool);
stream.WriteInt32(2, Int32);
stream.WriteUInt32(3, UInt32);
stream.WriteInt64(4, Int64);
stream.WriteUInt64(5, UInt64);
stream.WriteFloat(6, Float32);
stream.WriteDouble(7, Float64);
stream.WriteString(8, Str);
}

public int GetSize()
Expand Down Expand Up @@ -129,32 +128,30 @@ public void Init( )
UInt64Slice = new List<ulong>();
Float32Slice = new List<float>();
Float64Slice = new List<double>();
StrSlice = new List<string>();
EnumSlice = new List<MyEnum>();

EnumSlice = new List<MyEnum>();
}

public void Marshal(OutputStream stream)
{
stream.WriteBool(1, Bool );
stream.WriteInt32(2, Int32 );
stream.WriteUInt32(3, UInt32 );
stream.WriteInt64(4, Int64 );
stream.WriteUInt64(5, UInt64 );
stream.WriteFloat(6, Float32 );
stream.WriteDouble(7, Float64 );
stream.WriteString(8, Str );
stream.WriteBytes(9, BytesSlice );
stream.WriteBool(10, BoolSlice );
stream.WriteInt32(11, Int32Slice );
stream.WriteUInt32(12, UInt32Slice );
stream.WriteInt64(13, Int64Slice );
stream.WriteUInt64(14, UInt64Slice );
stream.WriteFloat(15, Float32Slice );
stream.WriteDouble(16, Float64Slice );
stream.WriteString(17, StrSlice );
stream.WriteEnum(18, Enum );
stream.WriteEnum(19, EnumSlice );
stream.WriteBool(1, Bool);
stream.WriteInt32(2, Int32);
stream.WriteUInt32(3, UInt32);
stream.WriteInt64(4, Int64);
stream.WriteUInt64(5, UInt64);
stream.WriteFloat(6, Float32);
stream.WriteDouble(7, Float64);
stream.WriteString(8, Str);
stream.WriteBytes(9, BytesSlice);
stream.WriteBool(10, BoolSlice);
stream.WriteInt32(11, Int32Slice);
stream.WriteUInt32(12, UInt32Slice);
stream.WriteInt64(13, Int64Slice);
stream.WriteUInt64(14, UInt64Slice);
stream.WriteFloat(15, Float32Slice);
stream.WriteDouble(16, Float64Slice);
stream.WriteString(17, StrSlice);
stream.WriteEnum(18, Enum);
stream.WriteEnum(19, EnumSlice);
}

public int GetSize()
Expand Down Expand Up @@ -287,35 +284,34 @@ public void Init( )
UInt64Slice = new List<ulong>();
Float32Slice = new List<float>();
Float64Slice = new List<double>();
StrSlice = new List<string>();
EnumSlice = new List<MyEnum>();

Struct = (MySubType) InputStream.CreateStruct(typeof(MySubType));
StructSlice = new List<MySubType>();
EnumSlice = new List<MyEnum>();
Struct = (MySubType) MessageMeta.NewStruct(typeof(MySubType));
}

public void Marshal(OutputStream stream)
{
stream.WriteBool(1, Bool );
stream.WriteInt32(2, Int32 );
stream.WriteUInt32(3, UInt32 );
stream.WriteInt64(4, Int64 );
stream.WriteUInt64(5, UInt64 );
stream.WriteFloat(6, Float32 );
stream.WriteDouble(7, Float64 );
stream.WriteString(8, Str );
stream.WriteStruct(9, Struct );
stream.WriteBytes(10, BytesSlice );
stream.WriteBool(11, BoolSlice );
stream.WriteInt32(12, Int32Slice );
stream.WriteUInt32(13, UInt32Slice );
stream.WriteInt64(14, Int64Slice );
stream.WriteUInt64(15, UInt64Slice );
stream.WriteFloat(16, Float32Slice );
stream.WriteDouble(17, Float64Slice );
stream.WriteString(18, StrSlice );
stream.WriteStruct(19, StructSlice );
stream.WriteEnum(20, Enum );
stream.WriteEnum(21, EnumSlice );
stream.WriteBool(1, Bool);
stream.WriteInt32(2, Int32);
stream.WriteUInt32(3, UInt32);
stream.WriteInt64(4, Int64);
stream.WriteUInt64(5, UInt64);
stream.WriteFloat(6, Float32);
stream.WriteDouble(7, Float64);
stream.WriteString(8, Str);
stream.WriteStruct(9, Struct);
stream.WriteBytes(10, BytesSlice);
stream.WriteBool(11, BoolSlice);
stream.WriteInt32(12, Int32Slice);
stream.WriteUInt32(13, UInt32Slice);
stream.WriteInt64(14, Int64Slice);
stream.WriteUInt64(15, UInt64Slice);
stream.WriteFloat(16, Float32Slice);
stream.WriteDouble(17, Float64Slice);
stream.WriteString(18, StrSlice);
stream.WriteStruct(19, StructSlice);
stream.WriteEnum(20, Enum);
stream.WriteEnum(21, EnumSlice);
}

public int GetSize()
Expand Down Expand Up @@ -428,7 +424,6 @@ public partial class LoginREQ : IProtoStruct
#region Serialize Code
public void Init( )
{

}

public void Marshal(OutputStream stream)
Expand Down Expand Up @@ -461,7 +456,6 @@ public partial class LoginACK : IProtoStruct
#region Serialize Code
public void Init( )
{

}

public void Marshal(OutputStream stream)
Expand Down Expand Up @@ -504,8 +498,8 @@ public static void RegisterGeneratedMeta(MessageMeta meta)
{
Type = typeof(MyType),
ID = 28380,
SourcePeer = "",
TargetPeer = "",
SourcePeer = "client",
TargetPeer = "game",
});
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/code.proto
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ struct MySubType
EnumSlice []MyEnum
}

[AutoMsgID ]
[AutoMsgID MsgDir: "client -> game"]
struct MyType
{
Bool bool
Expand Down

0 comments on commit 7485c37

Please sign in to comment.