/// <summary> /// The run. /// </summary> /// <param name="filename"> /// The filename. /// </param> /// <returns> /// The <see cref="FileDescriptorSet"/>. /// </returns> internal FileDescriptorSet Run(string filename) { array = File.ReadAllBytes(filename); Write("Message langth = " + array.Length); var fds = new FileDescriptorSet(); var fd = new FileDescriptor(); fds.Files.Add(fd); FileDescriptorSet(fd); return fds; }
/// <summary> /// Generate the code file. /// </summary> private void GenerateCode() { var protoBin = Path.Combine(tempFolder, "output.txt"); var analyze = new AnalyzeProtobuf(); var files = analyze.Run(protoBin); ////var files = new FileDescriptorSet(); ////using (var stream = File.OpenRead(protoBin)) ////{ //// Serializer.Merge(stream, files); ////} ////var ver = Assembly.GetAssembly(typeof(Serializer)).GetName().Version.ToString(); var newFile = Path.Combine(tempFolder, Path.GetFileName(protoFile) + ".cs"); using (var file = File.CreateText(newFile)) { file.WriteLine("// ---------------------------------------------------------------------------------------------------"); file.WriteLine("// <auto-generated>"); file.WriteLine("// This code was generated by the Visual Studio Protobuf Language Service."); file.WriteLine("// https://visualstudiogallery.msdn.microsoft.com/4bc0f38c-b058-4e05-ae38-155e053c19c5"); file.WriteLine("//"); file.WriteLine("// Protobuf-net from Marc Gravell is needed to use this code."); file.WriteLine("// https://code.google.com/p/protobuf-net/"); file.WriteLine("//"); file.WriteLine("// This code was generated by the help of the google protoc.exe (protobuf compiler) version " + protocVersion); ////file.WriteLine("// and protobuf-net version " + ver); file.WriteLine("//"); file.WriteLine("// Changes to this file may cause incorrect behaviour and will be lost if the code is regenerated."); file.WriteLine("// </auto-generated>"); file.WriteLine("// ---------------------------------------------------------------------------------------------------"); file.WriteLine(); file.WriteLine("// generated from " + Path.GetFileName(protoFile)); file.WriteLine(); file.WriteLine(options.GetOptionsAsComment()); foreach (var proto in files.Files) { currentProto = proto; var ns = string.IsNullOrEmpty(proto.Package) ? "projectname" : proto.Package; WriteLineIntended(file, string.Format("namespace {0}", options.FixCase ? ToPascalCase(ns) : ns)); WriteLineIntended(file, "{"); intend++; if (proto.Options != null) { if (proto.Options.Deprecated) { options.IsObsolete = true; } } WriteMessages(file, proto.MessageTypes, true); WriteEnums(file, proto.EnumTypes, !proto.MessageTypes.Any()); WriteServices(file, proto.Services, !proto.MessageTypes.Any() || !proto.EnumTypes.Any()); intend--; file.Write("}"); } file.Flush(); } }
/// <summary> /// The file descriptor set. /// </summary> /// <param name="fd"> /// The file descriptor. /// </param> private void FileDescriptorSet(FileDescriptor fd) { if (array[ix] == 0x0a) { ix++; uint length; var ixl = GetVarint(out length); Write("FileDescriptorProto Length = " + length); ix += ixl; FileDescriptorProto(length, fd); } }
/// <summary> /// The file descriptor proto. /// </summary> /// <param name="totalLength"> /// The total length. /// </param> /// <param name="fd"> /// The file descriptor. /// </param> // ReSharper disable once FunctionComplexityOverflow private void FileDescriptorProto(uint totalLength, FileDescriptor fd) { intend++; while (totalLength != 0) { int type, no; var l = GetTypeAndFieldNo(out type, out no); ix += l; totalLength -= (uint)l; switch (no) { case 1: // string name { uint length; var ixl = GetVarint(out length); ix += ixl; totalLength -= (uint)ixl; fd.Name = GetString(length); Write(string.Format("Type = {0}, F#= {1}, Length = {2}, Name = {3}", type, no, length, fd.Name)); ix += (int)length; totalLength -= length; break; } case 2: // string package { uint length; var ixl = GetVarint(out length); ix += ixl; totalLength -= (uint)ixl; fd.Package = GetString(length); Write(string.Format("Type = {0}, F#= {1}, Length = {2}, Package = {3}", type, no, length, fd.Package)); ix += (int)length; totalLength -= length; break; } case 3: // string[] dependecy { uint length; var ixl = GetVarint(out length); ix += ixl; totalLength -= (uint)ixl; var value = GetString(length); fd.Dependencies.Add(value); Write(string.Format("Type = {0}, F#= {1}, Length = {2}, Dependency = {3}", type, no, length, value)); ix += (int)length; totalLength -= length; break; } case 4: // DescriptorProto[] message type { uint length; var ixl = GetVarint(out length); ix += ixl; totalLength -= (uint)ixl; Write(string.Format("Type = {0}, F#= {1}, Length = {2} - DescriptorProto", type, no, length)); var md = new MessageDescriptor(); fd.MessageTypes.Add(md); DescriptorProto(length, md); totalLength -= length; break; } case 5: // EnumDescriptorProto[] enum type { uint length; var ixl = GetVarint(out length); ix += ixl; totalLength -= (uint)ixl; Write(string.Format("Type = {0}, F#= {1}, Length = {2} - EnumDescriptorProto", type, no, length)); var ed = new EnumDescriptor(); fd.EnumTypes.Add(ed); EnumDescriptorProto(length, ed); totalLength -= length; break; } case 6: // ServiceDescriptorProto[] service { uint length; var ixl = GetVarint(out length); ix += ixl; totalLength -= (uint)ixl; Write(string.Format("Type = {0}, F#= {1}, Length = {2} - ServiceDescriptorProto", type, no, length)); var sd = new ServiceDescriptor(); fd.Services.Add(sd); ServiceDescriptorProto(length, sd); totalLength -= length; break; } case 7: // FieldDescriptorProto[] extension { uint length; var ixl = GetVarint(out length); ix += ixl; totalLength -= (uint)ixl; Write(string.Format("Type = {0}, F#= {1}, Length = {2} - FieldDescriptorProto", type, no, length)); var fd2 = new FieldDescriptor(); fd.Extensions.Add(fd2); FieldDescriptorProto(length, fd2); totalLength -= length; break; } case 8: // FileOptions options { uint length; var ixl = GetVarint(out length); ix += ixl; totalLength -= (uint)ixl; Write(string.Format("Type = {0}, F#= {1}, Length = {2} - FileOptions", type, no, length)); if (fd.Options == null) { fd.Options = new FileOptions(); } FileOptions(length, fd.Options); totalLength -= length; break; } case 9: // SourceCodeInfo source_code_info { uint length; var ixl = GetVarint(out length); ix += ixl; totalLength -= (uint)ixl; Write(string.Format("Type = {0}, F#= {1}, Length = {2} - SourceCodeInfo", type, no, length)); fd.SourceCodeInfo = new SourceCodeInfo(); ////SourceCodeInfo(length, fd.SourceCodeInfo); ix += (int)length; totalLength -= length; break; } case 10: // int32[] public_dependency { uint value; var ixl = GetVarint(out value); ix += ixl; totalLength -= (uint)ixl; fd.PublicDependencies.Add((int)value); Write(string.Format("Type = {0}, F#= {1}, Length = {2}, public_dependency = {3}", type, no, ixl, value)); break; } case 11: // int32[] weak_dependency { uint value; var ixl = GetVarint(out value); ix += ixl; totalLength -= (uint)ixl; fd.WeakDependencies.Add((int)value); Write(string.Format("Type = {0}, F#= {1}, Length = {2}, weak_dependency = {3}", type, no, ixl, value)); break; } } } intend--; }