Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate protobufs ourselves using protobuf-net.Reflection #686

Merged
merged 30 commits into from
Oct 19, 2019
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
e0aeec3
"Fine, I'll do it myself." - Thanos (2015)
yaakov-h May 23, 2019
bafad8d
Regen all protos
yaakov-h May 23, 2019
7957b73
Update SteamKit to handle arrays instead of lists
yaakov-h May 23, 2019
46ce9eb
Tenatively upstream some parts of service generation
yaakov-h May 24, 2019
78425a6
Emit List<> for protobufs instead of Array.
yaakov-h May 24, 2019
3384272
Regenerate protobufs
yaakov-h May 24, 2019
a041ace
Revert "Update SteamKit to handle arrays instead of lists"
yaakov-h May 24, 2019
f014dd7
Tentatively upstream another function
yaakov-h May 24, 2019
f05a5ab
Bail on actual proto errors
yaakov-h May 25, 2019
9d7f6d4
wait this bit wasnt supposed to be staged
yaakov-h May 25, 2019
21d4193
use stderr for warnings/errors, not stdout
yaakov-h May 25, 2019
6375d3a
Reparse to avoid "no syntax specified" warning, ignore "import not us…
yaakov-h May 25, 2019
f0451b2
Fix whitespace (in powershell this time!)
yaakov-h May 25, 2019
cf7470d
Merge remote-tracking branch 'origin/experiment/protobuf-gen' into pr…
yaakov-h May 25, 2019
86a9042
Log some assumption, fix an error path
yaakov-h May 25, 2019
d53a72a
Merge remote-tracking branch 'origin/master' into protobuf-gen-3
yaakov-h Oct 15, 2019
d236280
Add argument separator to dotnet command line
yaakov-h Oct 15, 2019
a69d544
Use protobuf-net alpha builds from NuGet
yaakov-h Oct 15, 2019
baec061
Regen all protobufs
yaakov-h Oct 15, 2019
76a0e90
Update protobuf-net to latest stable
yaakov-h Oct 15, 2019
9005a62
Emit proto services
yaakov-h Oct 15, 2019
3764fe4
Regen proto services
yaakov-h Oct 15, 2019
a433229
Fix Samples build - DOTA hasn't had this msg for 10 months.
yaakov-h Oct 15, 2019
096f41d
Update protobuf-net for NHA2
yaakov-h Oct 15, 2019
4858fb4
DOTA still doesn't have this enum anymore
yaakov-h Oct 15, 2019
53805da
Merge remote-tracking branch 'origin/master' into protobuf-gen-3
yaakov-h Oct 19, 2019
bad95d8
Un-comment commented code
yaakov-h Oct 19, 2019
53d017b
Qualify aliased command
yaakov-h Oct 19, 2019
055947f
Remove unused variable
yaakov-h Oct 19, 2019
ecc53b6
Build protobufgen before running it
yaakov-h Oct 19, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ static IEnumerable<Type> GetGCEMsgEnums(uint appId)
yield return typeof(Dota.EDOTAGCMsg);
yield return typeof(Dota.EGCBaseMsg);
yield return typeof(Dota.ESOMsg);
yield return typeof(Dota.EGCSystemMsg);
yield return typeof(Dota.EGCItemMsg);
yield return typeof(Dota.EGCBaseClientMsg);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,14 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="protobuf-net, Version=2.1.0.0, Culture=neutral, PublicKeyToken=257b51d87d2e4d67, processorArchitecture=MSIL">
<HintPath>..\packages\protobuf-net.2.1.0\lib\net451\protobuf-net.dll</HintPath>
<Reference Include="protobuf-net, Version=2.4.0.0, Culture=neutral, PublicKeyToken=257b51d87d2e4d67, processorArchitecture=MSIL">
<HintPath>..\packages\protobuf-net.2.4.1\lib\net40\protobuf-net.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Configuration" />
<Reference Include="System.Core" />
<Reference Include="System.Runtime.Serialization" />
<Reference Include="System.ServiceModel" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class ProtoBufFieldReader
public static Dictionary<int, List<object>> ReadProtobuf(Stream stream)
{
// try reading it as a protobuf
using (var reader = new ProtoReader(stream, null, null))
using (var reader = ProtoReader.Create(stream, null))
{
var fields = new Dictionary<int, List<object>>();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="protobuf-net" version="2.1.0" targetFramework="net46" />
<package id="protobuf-net" version="2.4.1" targetFramework="net472" />
</packages>
25 changes: 25 additions & 0 deletions Resources/ProtobufGen/ProtobufGen.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.28803.352
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ProtobufGen", "ProtobufGen\ProtobufGen.csproj", "{DDC1D975-336F-4F3A-BCEA-D0DE5CEC6188}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{DDC1D975-336F-4F3A-BCEA-D0DE5CEC6188}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DDC1D975-336F-4F3A-BCEA-D0DE5CEC6188}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DDC1D975-336F-4F3A-BCEA-D0DE5CEC6188}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DDC1D975-336F-4F3A-BCEA-D0DE5CEC6188}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {31851CBB-4C49-402C-9F8B-42476FEDA58A}
EndGlobalSection
EndGlobal
161 changes: 161 additions & 0 deletions Resources/ProtobufGen/ProtobufGen/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using CommandLine;
using Google.Protobuf.Reflection;

namespace ProtobufGen
{
static class Program
{
public static int Main( string[] args )
{
var arguments = Parser.Default.ParseArguments<Options>( args );
switch ( arguments.Tag )
{
case ParserResultType.Parsed:
var value = ( ( Parsed<Options> )arguments ).Value;
return Run( value );

case ParserResultType.NotParsed:
// The library will automatically write help text.
return -1;

default:
// This should be unreachable.
return int.MinValue;
}
}

static int Run( Options arguments )
{
var set = ParseFiles( arguments );

if ( set == null )
{
return -1;
}

var errors = set.GetErrors();
PrintErrors( errors, out var numErrors, out var reparse );

if ( numErrors > 0 )
{
return numErrors;
}
else if ( reparse )
{
set = ReparseFiles( arguments, set );
errors = set.GetErrors();
PrintErrors( errors, out numErrors, out reparse );

Debug.Assert( numErrors == 0, "Errors should have been handled by first pass." );
Debug.Assert( !reparse, "Should not have to reparse after second pass." );
}

var codegen = new SteamKitCSharpCodeGenerator();

var options = new Dictionary<string, string>
{
[ "langver" ] = "7.0",
[ "names" ] = "original",
[ "services" ] = "1",
};

var files = codegen.Generate( set, options: options );
var fileName = Path.GetFileName( arguments.ProtobufPath );

foreach ( var file in files )
{
if ( Path.GetFileNameWithoutExtension( file.Name ) != Path.GetFileNameWithoutExtension( fileName ) )
{
continue;
}

File.WriteAllText( arguments.Output, file.Text );
}

return 0;
}

static FileDescriptorSet ParseFiles( Options arguments )
{
var set = new FileDescriptorSet
{
DefaultPackage = arguments.Namespace
};
set.AddImportPath( Path.GetDirectoryName( arguments.ProtobufPath ) );

var fileName = Path.GetFileName( arguments.ProtobufPath );
if ( !set.Add( fileName, includeInOutput: true ) )
{
Console.Error.WriteLine( $"Could not find file '{fileName}'." );
return null;
}

set.Process();
return set;
}

static FileDescriptorSet ReparseFiles( Options arguments, FileDescriptorSet firstPass )
{
var set = new FileDescriptorSet
{
DefaultPackage = firstPass.DefaultPackage
};

set.AddImportPath( Path.GetDirectoryName( arguments.ProtobufPath ) );

foreach ( var file in firstPass.Files )
{
if ( string.IsNullOrEmpty( file.Syntax ) )
{
file.Syntax = "proto2";
}
set.Files.Add( file );
}

set.Process();
return set;
}

static void PrintErrors( ProtoBuf.Reflection.Error[] errors, out int numErrors, out bool reparse )
{
numErrors = 0;
reparse = false;

if ( errors.Length > 0 )
{
foreach ( var error in errors )
{
if ( error.IsError )
{
numErrors++;
}

if ( error.IsWarning && error.Message.StartsWith( "no syntax specified;" ) )
{
reparse = true;
}
else if ( !error.IsWarning || !error.Message.StartsWith( "import not used:" ) )
{
Console.Error.WriteLine( $"{error.File} ({error.LineNumber}, {error.ColumnNumber}): {error.Message}" );
}
}
}
}
}

class Options
{
[Option( "namespace" )]
public string Namespace { get; set; }

[Option( "proto", Required = true )]
public string ProtobufPath { get; set; }

[Option( "output", Required = true )]
public string Output { get; set; }
}
}
13 changes: 13 additions & 0 deletions Resources/ProtobufGen/ProtobufGen/ProtobufGen.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.5.0" />
<PackageReference Include="protobuf-net.Reflection" Version="3.0.0-alpha.91" />
</ItemGroup>

</Project>
56 changes: 56 additions & 0 deletions Resources/ProtobufGen/ProtobufGen/SteamKitCSharpCodeGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using Google.Protobuf.Reflection;
using ProtoBuf.Reflection;

namespace ProtobufGen
{
public class SteamKitCSharpCodeGenerator : CSharpCodeGenerator
{
public SteamKitCSharpCodeGenerator()
: base()
{
}

protected override bool UseArray( FieldDescriptorProto field ) => false;

protected override void WriteExtension( GeneratorContext ctx, FieldDescriptorProto field )
{
}

protected override void WriteExtensionsHeader( GeneratorContext ctx, DescriptorProto message, ref object state )
{
}

protected override void WriteExtensionsHeader( GeneratorContext ctx, FileDescriptorProto file, ref object state )
{
}

protected override void WriteExtensionsFooter( GeneratorContext ctx, DescriptorProto message, ref object state )
{
}

protected override void WriteExtensionsFooter( GeneratorContext ctx, FileDescriptorProto file, ref object state )
{
}

/// <summary>
/// Starts a service block
/// </summary>
protected override void WriteServiceHeader( GeneratorContext ctx, ServiceDescriptorProto service, ref object state )
{
ctx.WriteLine( $"{GetAccess( GetAccess( service ) )} interface I{Escape( service.Name )}" ).WriteLine( "{" ).Indent();
}

protected override void WriteServiceFooter( GeneratorContext ctx, ServiceDescriptorProto service, ref object state )
{
ctx.Outdent().WriteLine( "}" ).WriteLine();
}

protected override void WriteServiceMethod( GeneratorContext ctx, MethodDescriptorProto method, ref object state )
{
var outputType = MakeRelativeName( ctx, method.OutputType );
var inputType = MakeRelativeName( ctx, method.InputType );

ctx.WriteLine( $"{Escape( outputType )} {Escape( method.Name )}({Escape( inputType )} request);" );
}
}
}
42 changes: 21 additions & 21 deletions Resources/Protogen/gc.proto → Resources/ProtobufGen/gc.proto
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
enum GCProtoBufMsgSrc {
Copy link
Member

@xPaw xPaw May 24, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file changed for no reason (line endings I assume?)

This also affected the csv file and the powershell script.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I only moved the files... possibly something in my git install tweaked the line endings?

GCProtoBufMsgSrc_Unspecified = 0;
GCProtoBufMsgSrc_FromSystem = 1;
GCProtoBufMsgSrc_FromSteamID = 2;
GCProtoBufMsgSrc_FromGC = 3;
GCProtoBufMsgSrc_ReplySystem = 4;
}
message CMsgProtoBufHeader {
optional fixed64 client_steam_id = 1;
optional int32 client_session_id = 2;
optional uint32 source_app_id = 3;
optional fixed64 job_id_source = 10 [default = 18446744073709551615];
optional fixed64 job_id_target = 11 [default = 18446744073709551615];
optional string target_job_name = 12;
optional int32 eresult = 13 [default = 2];
optional string error_message = 14;
optional uint32 ip = 15;
optional .GCProtoBufMsgSrc gc_msg_src = 200 [default = GCProtoBufMsgSrc_Unspecified];
optional uint32 gc_dir_index_source = 201;
}
enum GCProtoBufMsgSrc {
GCProtoBufMsgSrc_Unspecified = 0;
GCProtoBufMsgSrc_FromSystem = 1;
GCProtoBufMsgSrc_FromSteamID = 2;
GCProtoBufMsgSrc_FromGC = 3;
GCProtoBufMsgSrc_ReplySystem = 4;
}

message CMsgProtoBufHeader {
optional fixed64 client_steam_id = 1;
optional int32 client_session_id = 2;
optional uint32 source_app_id = 3;
optional fixed64 job_id_source = 10 [default = 18446744073709551615];
optional fixed64 job_id_target = 11 [default = 18446744073709551615];
optional string target_job_name = 12;
optional int32 eresult = 13 [default = 2];
optional string error_message = 14;
optional uint32 ip = 15;
optional .GCProtoBufMsgSrc gc_msg_src = 200 [default = GCProtoBufMsgSrc_Unspecified];
optional uint32 gc_dir_index_source = 201;
}
Loading