Compare commits

..

1 Commits

Author SHA1 Message Date
Xpl0itR 54ba50bfb4
testing 2024-06-08 21:21:08 +01:00
39 changed files with 553 additions and 1195 deletions

View File

@ -27,11 +27,11 @@ jobs:
dotnet-version: '8.0.x' dotnet-version: '8.0.x'
- name: Build protodec - name: Build protodec
run: dotnet publish --configuration Release --runtime ${{ matrix.runtime }} /p:Version=${{ github.ref_name }} /p:ContinuousIntegrationBuild=true run: dotnet publish --configuration Release --runtime ${{ matrix.runtime }} /p:VersionPrefix=${{ github.ref_name }} /p:ContinuousIntegrationBuild=true
- name: Pack LibProtodec - name: Pack LibProtodec
if: matrix.os == 'ubuntu-latest' if: matrix.os == 'ubuntu-latest'
run: dotnet pack --configuration Release /p:Version=${{ github.ref_name }} /p:ContinuousIntegrationBuild=true run: dotnet pack --configuration Release /p:VersionPrefix=${{ github.ref_name }} /p:ContinuousIntegrationBuild=true
- name: Release - name: Release
uses: softprops/action-gh-release@v1 uses: softprops/action-gh-release@v1

View File

@ -10,7 +10,6 @@ Arguments:
target_assembly_path Either the path to the target assembly or a directory of assemblies, all of which be parsed. target_assembly_path Either the path to the target assembly or a directory of assemblies, all of which be parsed.
out_path An existing directory to output into individual files, otherwise output to a single file. out_path An existing directory to output into individual files, otherwise output to a single file.
Options: Options:
--debug Drops the minimum log level to Debug.
--parse_service_servers Parses gRPC service definitions from server classes. --parse_service_servers Parses gRPC service definitions from server classes.
--parse_service_clients Parses gRPC service definitions from client classes. --parse_service_clients Parses gRPC service definitions from client classes.
--skip_enums Skip parsing enums and replace references to them with int32. --skip_enums Skip parsing enums and replace references to them with int32.

View File

@ -0,0 +1,91 @@
// Copyright © 2023-2024 Xpl0itR
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
namespace LibProtodec;
public sealed class AssemblyInspector : IDisposable
{
public readonly MetadataLoadContext AssemblyContext;
public readonly IReadOnlyList<Type> LoadedTypes;
public AssemblyInspector(string assemblyPath)
{
bool isFile = File.Exists(assemblyPath);
string assemblyDir = isFile
? Path.GetDirectoryName(assemblyPath)!
: assemblyPath;
PermissiveAssemblyResolver assemblyResolver = new(
Directory.EnumerateFiles(assemblyDir, searchPattern: "*.dll"));
AssemblyContext = new MetadataLoadContext(assemblyResolver);
LoadedTypes = isFile
? AssemblyContext.LoadFromAssemblyPath(assemblyPath).GetTypes()
: assemblyResolver.AssemblyPathLookup.Values.SelectMany(path => AssemblyContext.LoadFromAssemblyPath(path).GetTypes()).ToList();
}
public IEnumerable<Type> GetProtobufMessageTypes()
{
Type? iMessage = LoadedTypes.SingleOrDefault(static type => type?.FullName == "Google.Protobuf.IMessage", null)
?? AssemblyContext.LoadFromAssemblyName("Google.Protobuf")
.GetType("Google.Protobuf.IMessage");
return LoadedTypes.Where(
type => type is { IsNested: false, IsSealed: true }
&& type.Namespace?.StartsWith("Google.Protobuf", StringComparison.Ordinal) != true
&& type.IsAssignableTo(iMessage));
}
public IEnumerable<Type> GetProtobufServiceClientTypes()
{
Type? clientBase = LoadedTypes.SingleOrDefault(static type => type?.FullName == "Grpc.Core.ClientBase", null)
?? AssemblyContext.LoadFromAssemblyName("Grpc.Core.Api")
.GetType("Grpc.Core.ClientBase");
return LoadedTypes.Where(
type => type is { IsNested: true, IsAbstract: false }
&& type.IsAssignableTo(clientBase));
}
public IEnumerable<Type> GetProtobufServiceServerTypes()
{
Type? bindServiceMethodAttribute = LoadedTypes.SingleOrDefault(static type => type?.FullName == "Grpc.Core.BindServiceMethodAttribute", null)
?? AssemblyContext.LoadFromAssemblyName("Grpc.Core.Api")
.GetType("Grpc.Core.BindServiceMethodAttribute");
return LoadedTypes.Where(
type => type is { IsNested: true, IsAbstract: true, DeclaringType: { IsNested: false, IsSealed: true, IsAbstract: true } }
&& type.GetCustomAttributesData().Any(attribute => attribute.AttributeType == bindServiceMethodAttribute));
}
public void Dispose() =>
AssemblyContext.Dispose();
/// <summary>
/// An assembly resolver that uses paths to every assembly that may be loaded.
/// The file name is expected to be the same as the assembly's simple name (casing ignored).
/// PublicKeyToken, Version and CultureName are ignored.
/// </summary>
private sealed class PermissiveAssemblyResolver(IEnumerable<string> assemblyPaths) : MetadataAssemblyResolver
{
public readonly IReadOnlyDictionary<string, string> AssemblyPathLookup =
assemblyPaths.ToDictionary(
static path => Path.GetFileNameWithoutExtension(path),
StringComparer.OrdinalIgnoreCase);
/// <inheritdoc />
public override Assembly? Resolve(MetadataLoadContext mlc, AssemblyName assemblyName) =>
AssemblyPathLookup.TryGetValue(assemblyName.Name!, out string? assemblyPath)
? mlc.LoadFromAssemblyPath(assemblyPath)
: null;
}
}

View File

@ -18,9 +18,8 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.0-preview.5.24306.7" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All" /> <PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All" />
<PackageReference Include="System.Reflection.MetadataLoadContext" Version="9.0.0-preview.5.24306.7" /> <PackageReference Include="System.Reflection.MetadataLoadContext" Version="8.0.0" />
<PackageReference Include="Xpl0itR.SystemEx" Version="1.2.0" /> <PackageReference Include="Xpl0itR.SystemEx" Version="1.2.0" />
</ItemGroup> </ItemGroup>

View File

@ -1,34 +0,0 @@
// Copyright © 2024 Xpl0itR
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
using System;
using System.Collections.Generic;
using LibProtodec.Models.Cil;
namespace LibProtodec.Loaders;
public abstract class CilAssemblyLoader : IDisposable
{
private ICilType? _iMessage;
private ICilType? _clientBase;
private ICilType? _bindServiceMethodAttribute;
// ReSharper disable once InconsistentNaming
public ICilType IMessage =>
_iMessage ??= FindType("Google.Protobuf.IMessage", "Google.Protobuf");
public ICilType ClientBase =>
_clientBase ??= FindType("Grpc.Core.ClientBase", "Grpc.Core.Api");
public ICilType BindServiceMethodAttribute =>
_bindServiceMethodAttribute ??= FindType("Grpc.Core.BindServiceMethodAttribute", "Grpc.Core.Api");
public IReadOnlyList<ICilType> LoadedTypes { get; protected init; }
public virtual void Dispose() { }
protected abstract ICilType FindType(string typeFullName, string assemblySimpleName);
}

View File

@ -1,79 +0,0 @@
// Copyright © 2024 Xpl0itR
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using CommunityToolkit.Diagnostics;
using LibProtodec.Models.Cil;
using LibProtodec.Models.Cil.Clr;
using Microsoft.Extensions.Logging;
namespace LibProtodec.Loaders;
public sealed class ClrAssemblyLoader : CilAssemblyLoader
{
public readonly MetadataLoadContext LoadContext;
public ClrAssemblyLoader(string assemblyPath, ILogger? logger = null)
{
bool isFile = File.Exists(assemblyPath);
string assemblyDir = isFile
? Path.GetDirectoryName(assemblyPath)!
: assemblyPath;
PermissiveAssemblyResolver assemblyResolver = new(
Directory.EnumerateFiles(assemblyDir, searchPattern: "*.dll"));
LoadContext = new MetadataLoadContext(assemblyResolver);
IEnumerable<Type> allTypes = isFile
? LoadContext.LoadFromAssemblyPath(assemblyPath).GetTypes()
: assemblyResolver.AssemblyPathLookup.Values
.SelectMany(path => LoadContext.LoadFromAssemblyPath(path).GetTypes());
this.LoadedTypes = allTypes.Where(static type => type.GenericTypeArguments.Length == 0)
.Select(ClrType.GetOrCreate)
.ToList();
logger?.LogLoadedTypeAndAssemblyCount(this.LoadedTypes.Count, LoadContext.GetAssemblies().Count());
}
protected override ICilType FindType(string typeFullName, string assemblySimpleName)
{
ICilType? type = this.LoadedTypes.SingleOrDefault(type => type?.FullName == typeFullName, null);
if (type is not null)
return type;
Type? clrType = LoadContext.LoadFromAssemblyName(assemblySimpleName).GetType(typeFullName);
Guard.IsNotNull(clrType);
return ClrType.GetOrCreate(clrType);
}
public override void Dispose() =>
LoadContext.Dispose();
/// <summary>
/// An assembly resolver that uses paths to every assembly that may be loaded.
/// The file name is expected to be the same as the assembly's simple name (casing ignored).
/// PublicKeyToken, Version and CultureName are ignored.
/// </summary>
private sealed class PermissiveAssemblyResolver(IEnumerable<string> assemblyPaths) : MetadataAssemblyResolver
{
public readonly IReadOnlyDictionary<string, string> AssemblyPathLookup =
assemblyPaths.ToDictionary(
static path => Path.GetFileNameWithoutExtension(path),
StringComparer.OrdinalIgnoreCase);
/// <inheritdoc />
public override Assembly? Resolve(MetadataLoadContext mlc, AssemblyName assemblyName) =>
AssemblyPathLookup.TryGetValue(assemblyName.Name!, out string? assemblyPath)
? mlc.LoadFromAssemblyPath(assemblyPath)
: null;
}
}

View File

@ -1,86 +0,0 @@
// Copyright © 2024 Xpl0itR
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
using System;
using Microsoft.Extensions.Logging;
namespace LibProtodec;
// ReSharper disable InconsistentNaming, StringLiteralTypo
internal static partial class LoggerMessages
{
[LoggerMessage(Level = LogLevel.Warning, Message = "Failed to locate corresponding id field; likely stripped or otherwise obfuscated.")]
internal static partial void LogFailedToLocateIdField(this ILogger logger);
[LoggerMessage(Level = LogLevel.Information, Message = "Loaded {typeCount} types from {assemblyCount} assemblies for parsing.")]
internal static partial void LogLoadedTypeAndAssemblyCount(this ILogger logger, int typeCount, int assemblyCount);
[LoggerMessage(Level = LogLevel.Debug, Message = "Parsed as enum \"{name}\".")]
internal static partial void LogParsedEnum(this ILogger logger, string name);
[LoggerMessage(Level = LogLevel.Debug, Message = "Parsed as field \"{name}\" with id \"{id}\".")]
internal static partial void LogParsedField(this ILogger logger, string name, int id);
[LoggerMessage(Level = LogLevel.Debug, Message = "Parsed as field \"{name}\" with id \"{id}\" of type \"{typeName}\".")]
internal static partial void LogParsedField(this ILogger logger, string name, int id, string typeName);
[LoggerMessage(Level = LogLevel.Debug, Message = "Parsed as message \"{name}\".")]
internal static partial void LogParsedMessage(this ILogger logger, string name);
[LoggerMessage(Level = LogLevel.Debug, Message = "Parsed as method \"{name}\" with request type \"{reqType}\" and response type \"{resType}\".")]
internal static partial void LogParsedMethod(this ILogger logger, string name, string reqType, string resType);
[LoggerMessage(Level = LogLevel.Debug, Message = "Parsed as oneof field \"{name}\".")]
internal static partial void LogParsedOneOfField(this ILogger logger, string name);
[LoggerMessage(Level = LogLevel.Debug, Message = "Parsed as service \"{name}\".")]
internal static partial void LogParsedService(this ILogger logger, string name);
[LoggerMessage(Level = LogLevel.Debug, Message = "Skipping duplicate method.")]
internal static partial void LogSkippingDuplicateMethod(this ILogger logger);
[LoggerMessage(Level = LogLevel.Debug, Message = "Skipping property without required NonUserCodeAttribute.")]
internal static partial void LogSkippingPropertyWithoutNonUserCodeAttribute(this ILogger logger);
[LoggerMessage(Level = LogLevel.Debug, Message = "Skipping method without required GeneratedCodeAttribute.")]
internal static partial void LogSkippingMethodWithoutGeneratedCodeAttribute(this ILogger logger);
internal static IDisposable? BeginScopeParsingEnum(this ILogger logger, string typeName) =>
__BeginScopeParsingEnumCallback(logger, typeName);
internal static IDisposable? BeginScopeParsingField(this ILogger logger, string name) =>
__BeginScopeParsingFieldCallback(logger, name);
internal static IDisposable? BeginScopeParsingMessage(this ILogger logger, string name) =>
__BeginScopeParsingMessageCallback(logger, name);
internal static IDisposable? BeginScopeParsingMethod(this ILogger logger, string name) =>
__BeginScopeParsingMethodCallback(logger, name);
internal static IDisposable? BeginScopeParsingProperty(this ILogger logger, string name, string typeName) =>
__BeginScopeParsingPropertyCallback(logger, name, typeName);
internal static IDisposable? BeginScopeParsingService(this ILogger logger, string typeName) =>
__BeginScopeParsingServiceCallback(logger, typeName);
private static readonly Func<ILogger, string, IDisposable?> __BeginScopeParsingEnumCallback =
LoggerMessage.DefineScope<string>("Parsing enum from type \"{typeName}\"");
private static readonly Func<ILogger, string, IDisposable?> __BeginScopeParsingFieldCallback =
LoggerMessage.DefineScope<string>("Parsing field \"{name}\"");
private static readonly Func<ILogger, string, IDisposable?> __BeginScopeParsingMessageCallback =
LoggerMessage.DefineScope<string>("Parsing message from type \"{name}\"");
private static readonly Func<ILogger, string, IDisposable?> __BeginScopeParsingMethodCallback =
LoggerMessage.DefineScope<string>("Parsing method \"{name}\"");
private static readonly Func<ILogger, string, string, IDisposable?> __BeginScopeParsingPropertyCallback =
LoggerMessage.DefineScope<string, string>("Parsing property \"{name}\" of type \"{typeName}\"");
private static readonly Func<ILogger, string, IDisposable?> __BeginScopeParsingServiceCallback =
LoggerMessage.DefineScope<string>("Parsing service from type \"{typeName}\"");
}

View File

@ -1,43 +0,0 @@
// Copyright © 2024 Xpl0itR
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
using System;
using System.Collections.Generic;
using System.Reflection;
namespace LibProtodec.Models.Cil.Clr;
public sealed class ClrAttribute(CustomAttributeData clrAttribute) : ICilAttribute
{
private ICilType? _type;
private object?[]? _constructorArgumentValues;
public ICilType Type =>
_type ??= ClrType.GetOrCreate(
clrAttribute.AttributeType);
public IList<object?> ConstructorArgumentValues
{
get
{
if (_constructorArgumentValues is null)
{
IList<CustomAttributeTypedArgument> args = clrAttribute.ConstructorArguments;
_constructorArgumentValues = args.Count < 1
? Array.Empty<object>()
: new object[args.Count];
for (int i = 0; i < args.Count; i++)
{
_constructorArgumentValues[i] = args[i].Value;
}
}
return _constructorArgumentValues;
}
}
}

View File

@ -1,24 +0,0 @@
// Copyright © 2024 Xpl0itR
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
using System.Reflection;
namespace LibProtodec.Models.Cil.Clr;
public sealed class ClrField(FieldInfo clrField) : ClrMember(clrField), ICilField
{
public object? ConstantValue =>
clrField.GetRawConstantValue();
public bool IsLiteral =>
clrField.IsLiteral;
public bool IsPublic =>
clrField.IsPublic;
public bool IsStatic =>
clrField.IsStatic;
}

View File

@ -1,50 +0,0 @@
// Copyright © 2024 Xpl0itR
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
using System;
using System.Collections.Generic;
using System.Reflection;
namespace LibProtodec.Models.Cil.Clr;
public abstract class ClrMember(MemberInfo clrMember)
{
private ICilAttribute[]? _customAttributes;
public string Name =>
clrMember.Name;
public bool IsInherited =>
clrMember.DeclaringType != clrMember.ReflectedType;
public ICilType? DeclaringType =>
clrMember.DeclaringType is null
? null
: ClrType.GetOrCreate(
clrMember.DeclaringType);
public IList<ICilAttribute> CustomAttributes
{
get
{
if (_customAttributes is null)
{
IList<CustomAttributeData> attributes = clrMember.GetCustomAttributesData();
_customAttributes = attributes.Count < 1
? Array.Empty<ICilAttribute>()
: new ICilAttribute[attributes.Count];
for (int i = 0; i < attributes.Count; i++)
{
_customAttributes[i] = new ClrAttribute(attributes[i]);
}
}
return _customAttributes;
}
}
}

View File

@ -1,43 +0,0 @@
// Copyright © 2024 Xpl0itR
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
using System.Collections.Generic;
using System.Reflection;
using CommunityToolkit.Diagnostics;
namespace LibProtodec.Models.Cil.Clr;
public sealed class ClrMethod(MethodInfo clrMethod) : ClrMember(clrMethod), ICilMethod
{
public bool IsConstructor =>
clrMethod.IsConstructor;
public bool IsPublic =>
clrMethod.IsPublic;
public bool IsStatic =>
clrMethod.IsStatic;
public bool IsVirtual =>
clrMethod.IsVirtual;
public ICilType ReturnType =>
ClrType.GetOrCreate(
clrMethod.ReturnType);
public IEnumerable<ICilType> GetParameterTypes()
{
foreach (ParameterInfo parameter in clrMethod.GetParameters())
{
yield return ClrType.GetOrCreate(
parameter.ParameterType);
}
}
public byte[] GetMethodBodyILAsByteArray() =>
clrMethod.GetMethodBody()?.GetILAsByteArray()
?? ThrowHelper.ThrowNotSupportedException<byte[]>();
}

View File

@ -1,27 +0,0 @@
using System.Collections.Concurrent;
using System.Reflection;
namespace LibProtodec.Models.Cil.Clr;
public sealed class ClrModule : ICilModule
{
private readonly Module _module;
private ClrModule(Module module) =>
_module = module;
public string ResolveFieldName(int token) =>
_module.ResolveField(token).Name;
public string ResolveMethodName(int token) =>
_module.ResolveMethod(token).Name;
private static readonly ConcurrentDictionary<string, ClrModule> ModuleLookup = [];
public static ICilModule GetOrCreate(Module clrModule) =>
ModuleLookup.GetOrAdd(
clrModule.FullyQualifiedName,
static (_, clrModule) => new ClrModule(clrModule),
clrModule);
}

View File

@ -1,38 +0,0 @@
// Copyright © 2024 Xpl0itR
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
using System.Reflection;
namespace LibProtodec.Models.Cil.Clr;
public sealed class ClrProperty(PropertyInfo clrProperty) : ClrMember(clrProperty), ICilProperty
{
private readonly MethodInfo? _getterInfo = clrProperty.GetMethod;
private readonly MethodInfo? _setterInfo = clrProperty.SetMethod;
private ClrMethod? _getter;
private ClrMethod? _setter;
public bool CanRead =>
_getterInfo is not null;
public bool CanWrite =>
_setterInfo is not null;
public ICilMethod? Getter =>
_getterInfo is null
? null
: _getter ??= new ClrMethod(_getterInfo);
public ICilMethod? Setter =>
_setterInfo is null
? null
: _setter ??= new ClrMethod(_setterInfo);
public ICilType Type =>
ClrType.GetOrCreate(
clrProperty.PropertyType);
}

View File

@ -1,131 +0,0 @@
// Copyright © 2024 Xpl0itR
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Reflection;
using CommunityToolkit.Diagnostics;
namespace LibProtodec.Models.Cil.Clr;
public sealed class ClrType : ClrMember, ICilType
{
private const BindingFlags Everything = BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance;
private readonly Type _clrType;
private ICilType[]? _genericTypeArguments;
private ClrType(Type clrType) : base(clrType) =>
_clrType = clrType;
public string FullName =>
_clrType.FullName ?? _clrType.Name;
public string? Namespace =>
_clrType.Namespace;
public string DeclaringAssemblyName =>
_clrType.Assembly.FullName!;
public ICilModule DeclaringModule =>
ClrModule.GetOrCreate(
_clrType.Module);
public ICilType? BaseType =>
_clrType.BaseType is null
? null
: GetOrCreate(
_clrType.BaseType);
public bool IsAbstract =>
_clrType.IsAbstract;
public bool IsClass =>
_clrType.IsClass;
public bool IsEnum =>
_clrType.IsEnum;
public bool IsNested =>
_clrType.IsNested;
public bool IsSealed =>
_clrType.IsSealed;
public IList<ICilType> GenericTypeArguments
{
get
{
if (_genericTypeArguments is null)
{
Type[] args = _clrType.GenericTypeArguments;
_genericTypeArguments = args.Length < 1
? Array.Empty<ICilType>()
: new ICilType[args.Length];
for (int i = 0; i < args.Length; i++)
{
_genericTypeArguments[i] = GetOrCreate(args[i]);
}
}
return _genericTypeArguments;
}
}
public IEnumerable<ICilField> GetFields()
{
foreach (FieldInfo field in _clrType.GetFields(Everything))
{
yield return new ClrField(field);
}
}
public IEnumerable<ICilMethod> GetMethods()
{
foreach (MethodInfo method in _clrType.GetMethods(Everything))
{
yield return new ClrMethod(method);
}
}
public IEnumerable<ICilType> GetNestedTypes()
{
foreach (Type type in _clrType.GetNestedTypes(Everything))
{
yield return GetOrCreate(type);
}
}
public IEnumerable<ICilProperty> GetProperties()
{
foreach (PropertyInfo property in _clrType.GetProperties(Everything))
{
yield return new ClrProperty(property);
}
}
public bool IsAssignableTo(ICilType type)
{
if (type is ClrType clrType)
{
return _clrType.IsAssignableTo(clrType._clrType);
}
return ThrowHelper.ThrowNotSupportedException<bool>();
}
private static readonly ConcurrentDictionary<string, ClrType> TypeLookup = [];
public static ICilType GetOrCreate(Type clrType) =>
TypeLookup.GetOrAdd(
clrType.FullName ?? clrType.Name,
static (_, clrType) => new ClrType(clrType),
clrType);
}

View File

@ -1,16 +0,0 @@
// Copyright © 2024 Xpl0itR
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
using System.Collections.Generic;
namespace LibProtodec.Models.Cil;
public interface ICilAttribute
{
ICilType Type { get; }
IList<object?> ConstructorArgumentValues { get; }
}

View File

@ -1,22 +0,0 @@
// Copyright © 2024 Xpl0itR
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
using System.Collections.Generic;
namespace LibProtodec.Models.Cil;
public interface ICilField
{
string Name { get; }
object? ConstantValue { get; }
IList<ICilAttribute> CustomAttributes { get; }
bool IsLiteral { get; }
bool IsPublic { get; }
bool IsStatic { get; }
}

View File

@ -1,28 +0,0 @@
// Copyright © 2024 Xpl0itR
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
using System.Collections.Generic;
namespace LibProtodec.Models.Cil;
public interface ICilMethod
{
string Name { get; }
bool IsConstructor { get; }
bool IsInherited { get; }
bool IsPublic { get; }
bool IsStatic { get; }
bool IsVirtual { get; }
ICilType ReturnType { get; }
IList<ICilAttribute> CustomAttributes { get; }
IEnumerable<ICilType> GetParameterTypes();
byte[] GetMethodBodyILAsByteArray();
}

View File

@ -1,14 +0,0 @@
// Copyright © 2024 Xpl0itR
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
namespace LibProtodec.Models.Cil;
public interface ICilModule
{
string ResolveFieldName(int token);
string ResolveMethodName(int token);
}

View File

@ -1,24 +0,0 @@
// Copyright © 2024 Xpl0itR
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
using System.Collections.Generic;
namespace LibProtodec.Models.Cil;
public interface ICilProperty
{
string Name { get; }
ICilType Type { get; }
bool IsInherited { get; }
bool CanRead { get; }
bool CanWrite { get; }
ICilMethod? Getter { get; }
ICilMethod? Setter { get; }
IList<ICilAttribute> CustomAttributes { get; }
}

View File

@ -1,41 +0,0 @@
// Copyright © 2024 Xpl0itR
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
using System.Collections.Generic;
namespace LibProtodec.Models.Cil;
public interface ICilType
{
string Name { get; }
string FullName { get; }
string? Namespace { get; }
string DeclaringAssemblyName { get; }
ICilModule DeclaringModule { get; }
ICilType? DeclaringType { get; }
ICilType? BaseType { get; }
bool IsAbstract { get; }
bool IsClass { get; }
bool IsEnum { get; }
bool IsNested { get; }
bool IsSealed { get; }
IList<ICilType> GenericTypeArguments { get; }
IList<ICilAttribute> CustomAttributes { get; }
IEnumerable<ICilField> GetFields();
IEnumerable<ICilMethod> GetMethods();
IEnumerable<ICilType> GetNestedTypes();
IEnumerable<ICilProperty> GetProperties();
bool IsAssignableTo(ICilType type);
}

View File

@ -4,7 +4,7 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this // License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
namespace LibProtodec.Models.Protobuf.Fields; namespace LibProtodec.Models.Fields;
public sealed class EnumField public sealed class EnumField
{ {

View File

@ -5,29 +5,28 @@
// file, You can obtain one at http://mozilla.org/MPL/2.0/. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
using System.IO; using System.IO;
using LibProtodec.Models.Protobuf.TopLevels; using LibProtodec.Models.TopLevels;
using LibProtodec.Models.Protobuf.Types; using LibProtodec.Models.Types;
namespace LibProtodec.Models.Protobuf.Fields; namespace LibProtodec.Models.Fields;
public sealed class MessageField(Message declaringMessage) public sealed class MessageField
{ {
public required IProtobufType Type { get; init; } public required IType Type { get; init; }
public required string Name { get; init; } public required string Name { get; init; }
public required int Id { get; init; } public required int Id { get; init; }
public bool IsObsolete { get; init; } public bool IsObsolete { get; init; }
public bool HasHasProp { get; init; } public bool HasHasProp { get; init; }
public void WriteTo(TextWriter writer, bool isOneOf) public void WriteTo(TextWriter writer, TopLevel topLevel, bool isOneOf)
{ {
if (HasHasProp && !isOneOf && Type is not Repeated) if (HasHasProp && !isOneOf && Type is not Repeated)
{ {
writer.Write("optional "); writer.Write("optional ");
} }
writer.Write( Protobuf.WriteTypeNameTo(writer, Type, topLevel);
declaringMessage.QualifyTypeName(Type));
writer.Write(' '); writer.Write(' ');
writer.Write(Name); writer.Write(Name);
writer.Write(" = "); writer.Write(" = ");

View File

@ -5,22 +5,22 @@
// file, You can obtain one at http://mozilla.org/MPL/2.0/. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
using System.CodeDom.Compiler; using System.CodeDom.Compiler;
using LibProtodec.Models.Protobuf.TopLevels; using LibProtodec.Models.TopLevels;
using LibProtodec.Models.Protobuf.Types; using LibProtodec.Models.Types;
namespace LibProtodec.Models.Protobuf.Fields; namespace LibProtodec.Models.Fields;
public sealed class ServiceMethod(Service declaringService) public sealed class ServiceMethod
{ {
public required string Name { get; init; } public required string Name { get; init; }
public required IProtobufType RequestType { get; init; } public required IType RequestType { get; init; }
public required IProtobufType ResponseType { get; init; } public required IType ResponseType { get; init; }
public bool IsRequestStreamed { get; init; } public bool IsRequestStreamed { get; init; }
public bool IsResponseStreamed { get; init; } public bool IsResponseStreamed { get; init; }
public bool IsObsolete { get; init; } public bool IsObsolete { get; init; }
public void WriteTo(IndentedTextWriter writer) public void WriteTo(IndentedTextWriter writer, TopLevel topLevel)
{ {
writer.Write("rpc "); writer.Write("rpc ");
writer.Write(Name); writer.Write(Name);
@ -31,8 +31,7 @@ public sealed class ServiceMethod(Service declaringService)
writer.Write("stream "); writer.Write("stream ");
} }
writer.Write( Protobuf.WriteTypeNameTo(writer, RequestType, topLevel);
declaringService.QualifyTypeName(RequestType));
writer.Write(") returns ("); writer.Write(") returns (");
if (IsResponseStreamed) if (IsResponseStreamed)
@ -40,8 +39,7 @@ public sealed class ServiceMethod(Service declaringService)
writer.Write("stream "); writer.Write("stream ");
} }
writer.Write( Protobuf.WriteTypeNameTo(writer, ResponseType, topLevel);
declaringService.QualifyTypeName(ResponseType));
writer.Write(')'); writer.Write(')');
if (IsObsolete) if (IsObsolete)

View File

@ -8,14 +8,16 @@ using System.CodeDom.Compiler;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using LibProtodec.Models.Protobuf.TopLevels; using LibProtodec.Models.TopLevels;
using LibProtodec.Models.Types;
namespace LibProtodec.Models.Protobuf; namespace LibProtodec.Models;
public sealed class Protobuf public sealed class Protobuf
{ {
private string? _fileName;
private HashSet<string>? _imports; private HashSet<string>? _imports;
public HashSet<string> Imports =>
_imports ??= [];
public readonly List<TopLevel> TopLevels = []; public readonly List<TopLevel> TopLevels = [];
@ -24,10 +26,7 @@ public sealed class Protobuf
public string? Namespace { get; init; } public string? Namespace { get; init; }
public string FileName => public string FileName =>
_fileName ??= $"{string.Join('_', TopLevels.Select(static topLevel => topLevel.Name))}.proto"; $"{TopLevels.FirstOrDefault()?.Name}.proto";
public HashSet<string> Imports =>
_imports ??= [];
public void WriteTo(IndentedTextWriter writer) public void WriteTo(IndentedTextWriter writer)
{ {
@ -90,4 +89,17 @@ public sealed class Protobuf
writer.WriteLine(';'); writer.WriteLine(';');
} }
public static void WriteTypeNameTo(TextWriter writer, IType type, TopLevel topLevel)
{
if (type is TopLevel { Parent: not null } typeTopLevel && typeTopLevel.Parent != topLevel)
{
writer.Write(
typeTopLevel.QualifyName(topLevel));
}
else
{
writer.Write(type.Name);
}
}
} }

View File

@ -1,30 +0,0 @@
// Copyright © 2024 Xpl0itR
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
namespace LibProtodec.Models.Protobuf.Types;
// ReSharper disable StringLiteralTypo
public sealed class Scalar(string typeName) : IProtobufType
{
public string Name =>
typeName;
public static readonly IProtobufType Bool = new Scalar("bool");
public static readonly IProtobufType Bytes = new Scalar("bytes");
public static readonly IProtobufType Double = new Scalar("double");
public static readonly IProtobufType Fixed32 = new Scalar("fixed32");
public static readonly IProtobufType Fixed64 = new Scalar("fixed64");
public static readonly IProtobufType Float = new Scalar("float");
public static readonly IProtobufType Int32 = new Scalar("int32");
public static readonly IProtobufType Int64 = new Scalar("int64");
public static readonly IProtobufType SFixed32 = new Scalar("sfixed32");
public static readonly IProtobufType SFixed64 = new Scalar("sfixed64");
public static readonly IProtobufType SInt32 = new Scalar("sint32");
public static readonly IProtobufType SInt64 = new Scalar("sint64");
public static readonly IProtobufType String = new Scalar("string");
public static readonly IProtobufType UInt32 = new Scalar("uint32");
public static readonly IProtobufType UInt64 = new Scalar("uint64");
}

View File

@ -1,45 +0,0 @@
// Copyright © 2024 Xpl0itR
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
namespace LibProtodec.Models.Protobuf.Types;
public sealed class WellKnown(string typeName, string fileName) : IProtobufType
{
public string Name =>
typeName;
public string FileName =>
fileName;
public static readonly IProtobufType Any = new WellKnown("google.protobuf.Any", "google/protobuf/any.proto");
public static readonly IProtobufType Api = new WellKnown("google.protobuf.Api", "google/protobuf/api.proto");
public static readonly IProtobufType BoolValue = new WellKnown("google.protobuf.BoolValue", "google/protobuf/wrappers.proto");
public static readonly IProtobufType BytesValue = new WellKnown("google.protobuf.BytesValue", "google/protobuf/wrappers.proto");
public static readonly IProtobufType DoubleValue = new WellKnown("google.protobuf.DoubleValue", "google/protobuf/wrappers.proto");
public static readonly IProtobufType Duration = new WellKnown("google.protobuf.Duration", "google/protobuf/duration.proto");
public static readonly IProtobufType Empty = new WellKnown("google.protobuf.Empty", "google/protobuf/empty.proto");
public static readonly IProtobufType Enum = new WellKnown("google.protobuf.Enum", "google/protobuf/type.proto");
public static readonly IProtobufType EnumValue = new WellKnown("google.protobuf.EnumValue", "google/protobuf/type.proto");
public static readonly IProtobufType Field = new WellKnown("google.protobuf.Field", "google/protobuf/type.proto");
public static readonly IProtobufType FieldMask = new WellKnown("google.protobuf.FieldMask", "google/protobuf/field_mask.proto");
public static readonly IProtobufType FloatValue = new WellKnown("google.protobuf.FloatValue", "google/protobuf/wrappers.proto");
public static readonly IProtobufType Int32Value = new WellKnown("google.protobuf.Int32Value", "google/protobuf/wrappers.proto");
public static readonly IProtobufType Int64Value = new WellKnown("google.protobuf.Int64Value", "google/protobuf/wrappers.proto");
public static readonly IProtobufType ListValue = new WellKnown("google.protobuf.ListValue", "google/protobuf/struct.proto");
public static readonly IProtobufType Method = new WellKnown("google.protobuf.Method", "google/protobuf/api.proto");
public static readonly IProtobufType Mixin = new WellKnown("google.protobuf.Mixin", "google/protobuf/api.proto");
public static readonly IProtobufType NullValue = new WellKnown("google.protobuf.NullValue", "google/protobuf/struct.proto");
public static readonly IProtobufType Option = new WellKnown("google.protobuf.Option", "google/protobuf/type.proto");
public static readonly IProtobufType SourceContext = new WellKnown("google.protobuf.SourceContext", "google/protobuf/source_context.proto");
public static readonly IProtobufType StringValue = new WellKnown("google.protobuf.StringValue", "google/protobuf/wrappers.proto");
public static readonly IProtobufType Struct = new WellKnown("google.protobuf.Struct", "google/protobuf/struct.proto");
public static readonly IProtobufType Syntax = new WellKnown("google.protobuf.Syntax", "google/protobuf/type.proto");
public static readonly IProtobufType Timestamp = new WellKnown("google.protobuf.Timestamp", "google/protobuf/timestamp.proto");
public static readonly IProtobufType Type = new WellKnown("google.protobuf.Type", "google/protobuf/type.proto");
public static readonly IProtobufType UInt32Value = new WellKnown("google.protobuf.UInt32Value", "google/protobuf/wrappers.proto");
public static readonly IProtobufType UInt64Value = new WellKnown("google.protobuf.UInt64Value", "google/protobuf/wrappers.proto");
public static readonly IProtobufType Value = new WellKnown("google.protobuf.Value", "google/protobuf/struct.proto");
}

View File

@ -4,13 +4,13 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this // License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
global using Enum = LibProtodec.Models.Protobuf.TopLevels.Enum; global using Enum = LibProtodec.Models.TopLevels.Enum;
using System.CodeDom.Compiler; using System.CodeDom.Compiler;
using System.Collections.Generic; using System.Collections.Generic;
using LibProtodec.Models.Protobuf.Fields; using LibProtodec.Models.Fields;
using LibProtodec.Models.Protobuf.Types; using LibProtodec.Models.Types;
namespace LibProtodec.Models.Protobuf.TopLevels; namespace LibProtodec.Models.TopLevels;
public sealed class Enum : TopLevel, INestableType public sealed class Enum : TopLevel, INestableType
{ {
@ -23,7 +23,7 @@ public sealed class Enum : TopLevel, INestableType
writer.WriteLine(" {"); writer.WriteLine(" {");
writer.Indent++; writer.Indent++;
if (ContainsDuplicateFieldId) if (ContainsDuplicateField)
{ {
Protobuf.WriteOptionTo(writer, "allow_alias", "true"); Protobuf.WriteOptionTo(writer, "allow_alias", "true");
} }
@ -49,7 +49,7 @@ public sealed class Enum : TopLevel, INestableType
public bool IsClosed { get; set; } public bool IsClosed { get; set; }
private bool ContainsDuplicateFieldId private bool ContainsDuplicateField
{ {
get get
{ {

View File

@ -7,14 +7,14 @@
using System.CodeDom.Compiler; using System.CodeDom.Compiler;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using LibProtodec.Models.Protobuf.Fields; using LibProtodec.Models.Fields;
using LibProtodec.Models.Protobuf.Types; using LibProtodec.Models.Types;
namespace LibProtodec.Models.Protobuf.TopLevels; namespace LibProtodec.Models.TopLevels;
public sealed class Message : TopLevel, INestableType public sealed class Message : TopLevel, INestableType
{ {
public readonly Dictionary<string, List<int>> OneOfs = []; public readonly Dictionary<string, int[]> OneOfs = [];
public readonly Dictionary<int, MessageField> Fields = []; public readonly Dictionary<int, MessageField> Fields = [];
public readonly Dictionary<string, INestableType> Nested = []; public readonly Dictionary<string, INestableType> Nested = [];
@ -37,10 +37,10 @@ public sealed class Message : TopLevel, INestableType
if (oneOfs.Contains(field.Id)) if (oneOfs.Contains(field.Id))
continue; continue;
field.WriteTo(writer, isOneOf: false); field.WriteTo(writer, this, isOneOf: false);
} }
foreach ((string name, List<int> fieldIds) in OneOfs) foreach ((string name, int[] fieldIds) in OneOfs)
{ {
// ReSharper disable once StringLiteralTypo // ReSharper disable once StringLiteralTypo
writer.Write("oneof "); writer.Write("oneof ");
@ -50,7 +50,7 @@ public sealed class Message : TopLevel, INestableType
foreach (int fieldId in fieldIds) foreach (int fieldId in fieldIds)
{ {
Fields[fieldId].WriteTo(writer, isOneOf: true); Fields[fieldId].WriteTo(writer, this, isOneOf: true);
} }
writer.Indent--; writer.Indent--;

View File

@ -6,9 +6,9 @@
using System.CodeDom.Compiler; using System.CodeDom.Compiler;
using System.Collections.Generic; using System.Collections.Generic;
using LibProtodec.Models.Protobuf.Fields; using LibProtodec.Models.Fields;
namespace LibProtodec.Models.Protobuf.TopLevels; namespace LibProtodec.Models.TopLevels;
public sealed class Service : TopLevel public sealed class Service : TopLevel
{ {
@ -28,7 +28,7 @@ public sealed class Service : TopLevel
foreach (ServiceMethod method in Methods) foreach (ServiceMethod method in Methods)
{ {
method.WriteTo(writer); method.WriteTo(writer, this);
} }
writer.Indent--; writer.Indent--;

View File

@ -6,28 +6,22 @@
using System.CodeDom.Compiler; using System.CodeDom.Compiler;
using System.Collections.Generic; using System.Collections.Generic;
using LibProtodec.Models.Protobuf.Types;
namespace LibProtodec.Models.Protobuf.TopLevels; namespace LibProtodec.Models.TopLevels;
public abstract class TopLevel public abstract class TopLevel
{ {
public required string Name { get; init; } public required string Name { get; init; }
public bool IsObsolete { get; init; } public bool IsObsolete { get; init; }
public Protobuf? Protobuf { get; set; } public Protobuf? Protobuf { get; set; }
public TopLevel? Parent { get; set; } public TopLevel? Parent { get; set; }
public string QualifyTypeName(IProtobufType type) public string QualifyName(TopLevel topLevel)
{ {
if (type is not TopLevel { Parent: not null } typeTopLevel List<string> names = [Name];
|| typeTopLevel.Parent == this)
return type.Name;
List<string> names = [typeTopLevel.Name]; TopLevel? parent = Parent;
while (parent is not null && parent != topLevel)
TopLevel? parent = typeTopLevel.Parent;
while (parent is not null && parent != this)
{ {
names.Add(parent.Name); names.Add(parent.Name);
parent = parent.Parent; parent = parent.Parent;

View File

@ -4,9 +4,9 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this // License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
namespace LibProtodec.Models.Protobuf.Types; namespace LibProtodec.Models.Types;
public interface INestableType : IProtobufType public interface INestableType : IType
{ {
Protobuf? Protobuf { get; } Protobuf? Protobuf { get; }

View File

@ -4,9 +4,15 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this // License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
namespace LibProtodec.Models.Protobuf.Types; namespace LibProtodec.Models.Types;
public interface IProtobufType public interface IType
{ {
string Name { get; } string Name { get; }
}
public sealed class External(string typeName) : IType
{
public string Name =>
typeName;
} }

View File

@ -4,9 +4,9 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this // License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
namespace LibProtodec.Models.Protobuf.Types; namespace LibProtodec.Models.Types;
public sealed class Map(IProtobufType typeKey, IProtobufType typeVal) : IProtobufType public sealed class Map(IType typeKey, IType typeVal) : IType
{ {
public string Name => public string Name =>
$"map<{typeKey.Name}, {typeVal.Name}>"; $"map<{typeKey.Name}, {typeVal.Name}>";

View File

@ -4,9 +4,9 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this // License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
namespace LibProtodec.Models.Protobuf.Types; namespace LibProtodec.Models.Types;
public sealed class Repeated(IProtobufType type) : IProtobufType public sealed class Repeated(IType type) : IType
{ {
public string Name => public string Name =>
$"repeated {type.Name}"; $"repeated {type.Name}";

View File

@ -0,0 +1,27 @@
// Copyright © 2024 Xpl0itR
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
namespace LibProtodec.Models.Types;
// ReSharper disable StringLiteralTypo
public static class Scalar
{
public static readonly IType Bool = new External("bool");
public static readonly IType Bytes = new External("bytes");
public static readonly IType Double = new External("double");
public static readonly IType Fixed32 = new External("fixed32");
public static readonly IType Fixed64 = new External("fixed64");
public static readonly IType Float = new External("float");
public static readonly IType Int32 = new External("int32");
public static readonly IType Int64 = new External("int64");
public static readonly IType SFixed32 = new External("sfixed32");
public static readonly IType SFixed64 = new External("sfixed64");
public static readonly IType SInt32 = new External("sint32");
public static readonly IType SInt64 = new External("sint64");
public static readonly IType String = new External("string");
public static readonly IType UInt32 = new External("uint32");
public static readonly IType UInt64 = new External("uint64");
}

View File

@ -0,0 +1,39 @@
// Copyright © 2024 Xpl0itR
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
namespace LibProtodec.Models.Types;
public static class WellKnown
{
public static readonly IType Any = new External("google.protobuf.Any");
public static readonly IType Api = new External("google.protobuf.Api");
public static readonly IType BoolValue = new External("google.protobuf.BoolValue");
public static readonly IType BytesValue = new External("google.protobuf.BytesValue");
public static readonly IType DoubleValue = new External("google.protobuf.DoubleValue");
public static readonly IType Duration = new External("google.protobuf.Duration");
public static readonly IType Empty = new External("google.protobuf.Empty");
public static readonly IType Enum = new External("google.protobuf.Enum");
public static readonly IType EnumValue = new External("google.protobuf.EnumValue");
public static readonly IType Field = new External("google.protobuf.Field");
public static readonly IType FieldMask = new External("google.protobuf.FieldMask");
public static readonly IType FloatValue = new External("google.protobuf.FloatValue");
public static readonly IType Int32Value = new External("google.protobuf.Int32Value");
public static readonly IType Int64Value = new External("google.protobuf.Int64Value");
public static readonly IType ListValue = new External("google.protobuf.ListValue");
public static readonly IType Method = new External("google.protobuf.Method");
public static readonly IType Mixin = new External("google.protobuf.Mixin");
public static readonly IType NullValue = new External("google.protobuf.NullValue");
public static readonly IType Option = new External("google.protobuf.Option");
public static readonly IType SourceContext = new External("google.protobuf.SourceContext");
public static readonly IType StringValue = new External("google.protobuf.StringValue");
public static readonly IType Struct = new External("google.protobuf.Struct");
public static readonly IType Syntax = new External("google.protobuf.Syntax");
public static readonly IType Timestamp = new External("google.protobuf.Timestamp");
public static readonly IType Type = new External("google.protobuf.Type");
public static readonly IType UInt32Value = new External("google.protobuf.UInt32Value");
public static readonly IType UInt64Value = new External("google.protobuf.UInt64Value");
public static readonly IType Value = new External("google.protobuf.Value");
}

View File

@ -10,32 +10,35 @@ using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using System.Linq; using System.Linq;
using System.Reflection;
using System.Reflection.Emit; using System.Reflection.Emit;
using SystemEx; using SystemEx;
using SystemEx.Memory;
using CommunityToolkit.Diagnostics; using CommunityToolkit.Diagnostics;
using LibProtodec.Models.Cil; using LibProtodec.Models;
using LibProtodec.Models.Protobuf; using LibProtodec.Models.Fields;
using LibProtodec.Models.Protobuf.Fields; using LibProtodec.Models.TopLevels;
using LibProtodec.Models.Protobuf.TopLevels; using LibProtodec.Models.Types;
using LibProtodec.Models.Protobuf.Types; using SystemEx.Memory;
using Microsoft.Extensions.Logging;
namespace LibProtodec; namespace LibProtodec;
public delegate bool TypeLookupFunc(Type type, [NotNullWhen(true)] out IType? fieldType, out string? import);
public delegate bool NameLookupFunc(string name, [MaybeNullWhen(false)] out string translatedName); public delegate bool NameLookupFunc(string name, [MaybeNullWhen(false)] out string translatedName);
// ReSharper disable ClassWithVirtualMembersNeverInherited.Global, MemberCanBePrivate.Global, MemberCanBeProtected.Global public sealed class ProtodecContext
public class ProtodecContext
{ {
private const BindingFlags PublicStatic = BindingFlags.Public | BindingFlags.Static;
private const BindingFlags PublicInstanceDeclared = BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly;
private readonly Dictionary<string, TopLevel> _parsed = []; private readonly Dictionary<string, TopLevel> _parsed = [];
public readonly List<Protobuf> Protobufs = []; public readonly List<Protobuf> Protobufs = [];
public ILogger? Logger { get; set; }
public NameLookupFunc? NameLookup { get; set; } public NameLookupFunc? NameLookup { get; set; }
public TypeLookupFunc TypeLookup { get; set; } =
LookupScalarAndWellKnownTypes;
public void WriteAllTo(IndentedTextWriter writer) public void WriteAllTo(IndentedTextWriter writer)
{ {
writer.WriteLine("// Decompiled with protodec"); writer.WriteLine("// Decompiled with protodec");
@ -51,100 +54,88 @@ public class ProtodecContext
} }
} }
public virtual Message ParseMessage(ICilType messageClass, ParserOptions options = ParserOptions.None) public Message ParseMessage(Type messageClass, ParserOptions options = ParserOptions.None)
{ {
Guard.IsTrue(messageClass is { IsClass: true, IsSealed: true }); Guard.IsTrue(messageClass is { IsClass: true, IsSealed: true });
using IDisposable? _ = Logger?.BeginScopeParsingMessage(messageClass.FullName);
if (_parsed.TryGetValue(messageClass.FullName, out TopLevel? parsedMessage)) if (_parsed.TryGetValue(messageClass.FullName ?? messageClass.Name, out TopLevel? parsedMessage))
{ {
Logger?.LogParsedMessage(parsedMessage.Name);
return (Message)parsedMessage; return (Message)parsedMessage;
} }
Message message = new() Message message = new()
{ {
Name = TranslateTypeName(messageClass), Name = TranslateTypeName(messageClass),
IsObsolete = HasObsoleteAttribute(messageClass.CustomAttributes) IsObsolete = HasObsoleteAttribute(messageClass.GetCustomAttributesData())
}; };
_parsed.Add(messageClass.FullName, message); _parsed.Add(messageClass.FullName ?? messageClass.Name, message);
Protobuf protobuf = GetProtobuf(messageClass, message, options); Protobuf protobuf = GetProtobuf(messageClass, message, options);
ParseWriteToMethodTesting(messageClass); ParseWriteToMethodTesting(messageClass);
List<ICilField> idFields = messageClass.GetFields() FieldInfo[] idFields = messageClass.GetFields(PublicStatic);
.Where(static field => field is { IsPublic: true, IsStatic: true, IsLiteral: true }) PropertyInfo[] properties = messageClass.GetProperties(PublicInstanceDeclared);
.ToList();
List<ICilProperty> properties = messageClass.GetProperties() for (int pi = 0, fi = 0; pi < properties.Length; pi++, fi++)
.Where(static property => property is { IsInherited: false, CanRead: true, Getter: { IsPublic: true, IsStatic: false, IsVirtual: false } })
.ToList();
for (int pi = 0, fi = 0; pi < properties.Count; pi++)
{ {
ICilProperty property = properties[pi]; PropertyInfo property = properties[pi];
ICilType propertyType = property.Type; IList<CustomAttributeData> attributes = property.GetCustomAttributesData();
using IDisposable? __ = Logger?.BeginScopeParsingProperty(property.Name, propertyType.FullName); if (((options & ParserOptions.IncludePropertiesWithoutNonUserCodeAttribute) == 0 && !HasNonUserCodeAttribute(attributes))
|| property.GetMethod?.IsVirtual != false)
if (((options & ParserOptions.IncludePropertiesWithoutNonUserCodeAttribute) == 0 && !HasNonUserCodeAttribute(property.CustomAttributes)))
{ {
Logger?.LogSkippingPropertyWithoutNonUserCodeAttribute(); fi--;
continue; continue;
} }
Type propertyType = property.PropertyType;
// only OneOf enums are defined nested directly in the message class // only OneOf enums are defined nested directly in the message class
if (propertyType.IsEnum && propertyType.DeclaringType?.Name == messageClass.Name) if (propertyType.IsEnum && propertyType.DeclaringType?.Name == messageClass.Name)
{ {
string oneOfName = TranslateOneOfPropName(property.Name); string oneOfName = TranslateOneOfPropName(property.Name);
Logger?.LogParsedOneOfField(oneOfName); int[] oneOfProtoFieldIds = propertyType.GetFields(PublicStatic)
.Select(static field => (int)field.GetRawConstantValue()!)
List<int> oneOfProtoFieldIds = propertyType.GetFields() .Where(static id => id > 0)
.Where(static field => field.IsLiteral) .ToArray();
.Select(static field => (int)field.ConstantValue!)
.Where(static id => id > 0)
.ToList();
message.OneOfs.Add(oneOfName, oneOfProtoFieldIds); message.OneOfs.Add(oneOfName, oneOfProtoFieldIds);
fi--;
continue; continue;
} }
FieldInfo idField = idFields[fi];
Guard.IsTrue(idField.IsLiteral);
Guard.IsEqualTo(idField.FieldType.Name, nameof(Int32));
bool msgFieldHasHasProp = false; // some field properties are immediately followed by an additional "Has" get-only boolean property bool msgFieldHasHasProp = false; // some field properties are immediately followed by an additional "Has" get-only boolean property
if (properties.Count > pi + 1 && properties[pi + 1].Type.Name == nameof(Boolean) && !properties[pi + 1].CanWrite) if (properties.Length > pi + 1 && properties[pi + 1].PropertyType.Name == nameof(Boolean) && !properties[pi + 1].CanWrite)
{ {
msgFieldHasHasProp = true; msgFieldHasHasProp = true;
pi++; pi++;
} }
if (idFields.Count <= fi) MessageField field = new()
{
Logger?.LogFailedToLocateIdField();
continue;
}
MessageField field = new(message)
{ {
Type = ParseFieldType(propertyType, options, protobuf), Type = ParseFieldType(propertyType, options, protobuf),
Name = TranslateMessageFieldName(property.Name), Name = TranslateMessageFieldName(property.Name),
Id = (int)idFields[fi].ConstantValue!, Id = (int)idField.GetRawConstantValue()!,
IsObsolete = HasObsoleteAttribute(property.CustomAttributes), IsObsolete = HasObsoleteAttribute(attributes),
HasHasProp = msgFieldHasHasProp HasHasProp = msgFieldHasHasProp
}; };
Logger?.LogParsedField(field.Name, field.Id, field.Type.Name);
message.Fields.Add(field.Id, field); message.Fields.Add(field.Id, field);
fi++;
} }
Logger?.LogParsedMessage(message.Name);
return message; return message;
} }
private static void ParseWriteToMethodTesting(ICilType messageClass) private static void ParseWriteToMethodTesting(Type messageClass)
{ {
ICilMethod writeTo = messageClass.GetMethods().Single(static method => method.Name == "WriteTo"); MethodInfo writeTo = messageClass.GetMethod("WriteTo", BindingFlags.Public | BindingFlags.Instance)!;
MemoryReader reader = new(writeTo.GetMethodBodyILAsByteArray()); MemoryReader reader = new(writeTo.GetMethodBody()!.GetILAsByteArray()!);
int fieldToken = 0; int fieldToken = 0;
while (reader.Remaining > 0) while (reader.Remaining > 0)
@ -162,8 +153,8 @@ public class ProtodecContext
Guard.IsNotEqualTo(fieldToken, 0); Guard.IsNotEqualTo(fieldToken, 0);
int methodToken = reader.ReadInt32LittleEndian(); int methodToken = reader.ReadInt32LittleEndian();
string methodName = messageClass.DeclaringModule.ResolveMethodName(methodToken); // System.NotSupportedException: 'Resolving tokens is not supported on assemblies loaded by a MetadataLoadContext.' MethodBase? method = messageClass.Module.ResolveMethod(methodToken); // System.NotSupportedException: 'Resolving tokens is not supported on assemblies loaded by a MetadataLoadContext.'
string fieldName = messageClass.DeclaringModule.ResolveFieldName(fieldToken); FieldInfo? field = messageClass.Module.ResolveField(fieldToken);
//TODO //TODO
} }
@ -172,41 +163,35 @@ public class ProtodecContext
reader.Position += operandLength; reader.Position += operandLength;
} }
} }
} }
public virtual Enum ParseEnum(ICilType enumEnum, ParserOptions options = ParserOptions.None) public Enum ParseEnum(Type enumEnum, ParserOptions options = ParserOptions.None)
{ {
Guard.IsTrue(enumEnum.IsEnum); Guard.IsTrue(enumEnum.IsEnum);
using IDisposable? _ = Logger?.BeginScopeParsingEnum(enumEnum.FullName);
if (_parsed.TryGetValue(enumEnum.FullName, out TopLevel? parsedEnum)) if (_parsed.TryGetValue(enumEnum.FullName ?? enumEnum.Name, out TopLevel? parsedEnum))
{ {
Logger?.LogParsedEnum(parsedEnum.Name);
return (Enum)parsedEnum; return (Enum)parsedEnum;
} }
Enum @enum = new() Enum @enum = new()
{ {
Name = TranslateTypeName(enumEnum), Name = TranslateTypeName(enumEnum),
IsObsolete = HasObsoleteAttribute(enumEnum.CustomAttributes) IsObsolete = HasObsoleteAttribute(enumEnum.GetCustomAttributesData())
}; };
_parsed.Add(enumEnum.FullName, @enum); _parsed.Add(enumEnum.FullName ?? enumEnum.Name, @enum);
Protobuf protobuf = GetProtobuf(enumEnum, @enum, options); Protobuf protobuf = GetProtobuf(enumEnum, @enum, options);
foreach (ICilField enumField in enumEnum.GetFields().Where(static field => field.IsLiteral)) foreach (FieldInfo field in enumEnum.GetFields(PublicStatic))
{ {
using IDisposable? __ = Logger?.BeginScopeParsingField(enumField.Name); @enum.Fields.Add(
new EnumField
EnumField field = new() {
{ Id = (int)field.GetRawConstantValue()!,
Id = (int)enumField.ConstantValue!, Name = TranslateEnumFieldName(field, @enum.Name),
Name = TranslateEnumFieldName(enumField.CustomAttributes, enumField.Name, @enum.Name), IsObsolete = HasObsoleteAttribute(field.GetCustomAttributesData())
IsObsolete = HasObsoleteAttribute(enumField.CustomAttributes) });
};
Logger?.LogParsedField(field.Name, field.Id);
@enum.Fields.Add(field);
} }
if (@enum.Fields.All(static field => field.Id != 0)) if (@enum.Fields.All(static field => field.Id != 0))
@ -215,11 +200,10 @@ public class ProtodecContext
@enum.IsClosed = true; @enum.IsClosed = true;
} }
Logger?.LogParsedEnum(@enum.Name);
return @enum; return @enum;
} }
public virtual Service ParseService(ICilType serviceClass, ParserOptions options = ParserOptions.None) public Service ParseService(Type serviceClass, ParserOptions options = ParserOptions.None)
{ {
Guard.IsTrue(serviceClass.IsClass); Guard.IsTrue(serviceClass.IsClass);
@ -228,9 +212,9 @@ public class ProtodecContext
{ {
if (serviceClass is { IsSealed: true, IsNested: false }) if (serviceClass is { IsSealed: true, IsNested: false })
{ {
List<ICilType> nested = serviceClass.GetNestedTypes().ToList(); Type[] nested = serviceClass.GetNestedTypes();
serviceClass = nested.SingleOrDefault(static nested => nested is { IsAbstract: true, IsSealed: false }) serviceClass = nested.SingleOrDefault(static nested => nested is { IsAbstract: true, IsSealed: false })
?? nested.Single(static nested => nested is { IsClass: true, IsAbstract: false }); ?? nested.Single(static nested => nested is { IsClass: true, IsAbstract: false });
} }
if (serviceClass is { IsNested: true, IsAbstract: true, IsSealed: false }) if (serviceClass is { IsNested: true, IsAbstract: true, IsSealed: false })
@ -245,54 +229,49 @@ public class ProtodecContext
} }
Guard.IsNotNull(isClientClass); Guard.IsNotNull(isClientClass);
using IDisposable? _ = Logger?.BeginScopeParsingService(serviceClass.DeclaringType!.FullName);
if (_parsed.TryGetValue(serviceClass.DeclaringType!.FullName, out TopLevel? parsedService)) if (_parsed.TryGetValue(serviceClass.DeclaringType!.FullName ?? serviceClass.DeclaringType!.Name, out TopLevel? parsedService))
{ {
Logger?.LogParsedService(parsedService.Name);
return (Service)parsedService; return (Service)parsedService;
} }
Service service = new() Service service = new()
{ {
Name = TranslateTypeName(serviceClass.DeclaringType), Name = TranslateTypeName(serviceClass.DeclaringType),
IsObsolete = HasObsoleteAttribute(serviceClass.CustomAttributes) IsObsolete = HasObsoleteAttribute(serviceClass.GetCustomAttributesData())
}; };
_parsed.Add(serviceClass.DeclaringType!.FullName, service); _parsed.Add(serviceClass.DeclaringType!.FullName ?? serviceClass.DeclaringType.Name, service);
Protobuf protobuf = NewProtobuf(serviceClass, service); Protobuf protobuf = NewProtobuf(serviceClass, service);
foreach (ICilMethod cilMethod in serviceClass.GetMethods().Where(static method => method is { IsInherited: false, IsPublic: true, IsStatic: false, IsConstructor: false })) foreach (MethodInfo method in serviceClass.GetMethods(PublicInstanceDeclared))
{ {
using IDisposable? __ = Logger?.BeginScopeParsingMethod(cilMethod.Name); IList<CustomAttributeData> attributes = method.GetCustomAttributesData();
if ((options & ParserOptions.IncludeServiceMethodsWithoutGeneratedCodeAttribute) == 0 if ((options & ParserOptions.IncludeServiceMethodsWithoutGeneratedCodeAttribute) == 0
&& !HasGeneratedCodeAttribute(cilMethod.CustomAttributes, "grpc_csharp_plugin")) && !HasGeneratedCodeAttribute(attributes, "grpc_csharp_plugin"))
{ {
Logger?.LogSkippingMethodWithoutGeneratedCodeAttribute();
continue; continue;
} }
ICilType requestType, responseType, returnType = cilMethod.ReturnType; Type requestType, responseType, returnType = method.ReturnType;
bool streamReq, streamRes; bool streamReq, streamRes;
if (isClientClass.Value) if (isClientClass.Value)
{ {
string returnTypeName = TranslateTypeName(returnType); string returnTypeName = TranslateTypeName(returnType);
if (returnTypeName == "AsyncUnaryCall`1") if (returnTypeName == "AsyncUnaryCall`1")
{ {
Logger?.LogSkippingDuplicateMethod();
continue; continue;
} }
List<ICilType> parameters = cilMethod.GetParameterTypes().ToList(); ParameterInfo[] parameters = method.GetParameters();
if (parameters.Count > 2) if (parameters.Length > 2)
{ {
Logger?.LogSkippingDuplicateMethod();
continue; continue;
} }
switch (returnType.GenericTypeArguments.Count) Type firstParamType = parameters[0].ParameterType;
switch (returnType.GenericTypeArguments.Length)
{ {
case 2: case 2:
requestType = returnType.GenericTypeArguments[0]; requestType = returnType.GenericTypeArguments[0];
@ -301,13 +280,13 @@ public class ProtodecContext
streamRes = returnTypeName == "AsyncDuplexStreamingCall`2"; streamRes = returnTypeName == "AsyncDuplexStreamingCall`2";
break; break;
case 1: case 1:
requestType = parameters[0]; requestType = firstParamType;
responseType = returnType.GenericTypeArguments[0]; responseType = returnType.GenericTypeArguments[0];
streamReq = false; streamReq = false;
streamRes = true; streamRes = true;
break; break;
default: default:
requestType = parameters[0]; requestType = firstParamType;
responseType = returnType; responseType = returnType;
streamReq = false; streamReq = false;
streamRes = false; streamRes = false;
@ -316,20 +295,21 @@ public class ProtodecContext
} }
else else
{ {
List<ICilType> parameters = cilMethod.GetParameterTypes().ToList(); ParameterInfo[] parameters = method.GetParameters();
Type firstParamType = parameters[0].ParameterType;
if (parameters[0].GenericTypeArguments.Count == 1) if (firstParamType.GenericTypeArguments.Length == 1)
{ {
streamReq = true; streamReq = true;
requestType = parameters[0].GenericTypeArguments[0]; requestType = firstParamType.GenericTypeArguments[0];
} }
else else
{ {
streamReq = false; streamReq = false;
requestType = parameters[0]; requestType = firstParamType;
} }
if (returnType.GenericTypeArguments.Count == 1) if (returnType.GenericTypeArguments.Length == 1)
{ {
streamRes = false; streamRes = false;
responseType = returnType.GenericTypeArguments[0]; responseType = returnType.GenericTypeArguments[0];
@ -337,31 +317,28 @@ public class ProtodecContext
else else
{ {
streamRes = true; streamRes = true;
responseType = parameters[1].GenericTypeArguments[0]; responseType = parameters[1].ParameterType.GenericTypeArguments[0];
} }
} }
ServiceMethod method = new(service) service.Methods.Add(
{ new ServiceMethod
Name = TranslateMethodName(cilMethod.Name), {
IsObsolete = HasObsoleteAttribute(cilMethod.CustomAttributes), Name = TranslateMethodName(method.Name),
RequestType = ParseFieldType(requestType, options, protobuf), IsObsolete = HasObsoleteAttribute(attributes),
ResponseType = ParseFieldType(responseType, options, protobuf), RequestType = ParseFieldType(requestType, options, protobuf),
IsRequestStreamed = streamReq, ResponseType = ParseFieldType(responseType, options, protobuf),
IsResponseStreamed = streamRes IsRequestStreamed = streamReq,
}; IsResponseStreamed = streamRes
});
Logger?.LogParsedMethod(method.Name, method.RequestType.Name, method.ResponseType.Name);
service.Methods.Add(method);
} }
Logger?.LogParsedService(service.Name);
return service; return service;
} }
protected IProtobufType ParseFieldType(ICilType type, ParserOptions options, Protobuf referencingProtobuf) private IType ParseFieldType(Type type, ParserOptions options, Protobuf referencingProtobuf)
{ {
switch (type.GenericTypeArguments.Count) switch (type.GenericTypeArguments.Length)
{ {
case 1: case 1:
return new Repeated( return new Repeated(
@ -372,170 +349,44 @@ public class ProtodecContext
ParseFieldType(type.GenericTypeArguments[1], options, referencingProtobuf)); ParseFieldType(type.GenericTypeArguments[1], options, referencingProtobuf));
} }
if (!LookupType(type, out IProtobufType? fieldType)) if (TypeLookup(type, out IType? fieldType, out string? import))
{ {
if (type.IsEnum) if (import is not null)
{ {
if ((options & ParserOptions.SkipEnums) > 0) referencingProtobuf.Imports.Add(import);
{ }
return Scalar.Int32;
}
fieldType = ParseEnum(type, options); return fieldType;
}
else
{
fieldType = ParseMessage(type, options);
}
} }
switch (fieldType) if (type.IsEnum)
{ {
case WellKnown wellKnown: if ((options & ParserOptions.SkipEnums) > 0)
referencingProtobuf.Imports.Add( {
wellKnown.FileName); return Scalar.Int32;
break; }
case INestableType nestableType:
Protobuf protobuf = nestableType.Protobuf!; fieldType = ParseEnum(type, options);
if (referencingProtobuf != protobuf) }
referencingProtobuf.Imports.Add( else
protobuf.FileName); {
break; fieldType = ParseMessage(type, options);
}
Protobuf protobuf = ((INestableType)fieldType).Protobuf!;
if (referencingProtobuf != protobuf)
{
referencingProtobuf.Imports.Add(protobuf.FileName);
} }
return fieldType; return fieldType;
} }
protected virtual bool LookupType(ICilType cilType, [NotNullWhen(true)] out IProtobufType? protobufType) private Protobuf NewProtobuf(Type topLevelType, TopLevel topLevel)
{
switch (cilType.FullName)
{
case "System.String":
protobufType = Scalar.String;
break;
case "System.Boolean":
protobufType = Scalar.Bool;
break;
case "System.Double":
protobufType = Scalar.Double;
break;
case "System.UInt32":
protobufType = Scalar.UInt32;
break;
case "System.UInt64":
protobufType = Scalar.UInt64;
break;
case "System.Int32":
protobufType = Scalar.Int32;
break;
case "System.Int64":
protobufType = Scalar.Int64;
break;
case "System.Single":
protobufType = Scalar.Float;
break;
case "Google.Protobuf.ByteString":
protobufType = Scalar.Bytes;
break;
case "Google.Protobuf.WellKnownTypes.Any":
protobufType = WellKnown.Any;
break;
case "Google.Protobuf.WellKnownTypes.Api":
protobufType = WellKnown.Api;
break;
case "Google.Protobuf.WellKnownTypes.BoolValue":
protobufType = WellKnown.BoolValue;
break;
case "Google.Protobuf.WellKnownTypes.BytesValue":
protobufType = WellKnown.BytesValue;
break;
case "Google.Protobuf.WellKnownTypes.DoubleValue":
protobufType = WellKnown.DoubleValue;
break;
case "Google.Protobuf.WellKnownTypes.Duration":
protobufType = WellKnown.Duration;
break;
case "Google.Protobuf.WellKnownTypes.Empty":
protobufType = WellKnown.Empty;
break;
case "Google.Protobuf.WellKnownTypes.Enum":
protobufType = WellKnown.Enum;
break;
case "Google.Protobuf.WellKnownTypes.EnumValue":
protobufType = WellKnown.EnumValue;
break;
case "Google.Protobuf.WellKnownTypes.Field":
protobufType = WellKnown.Field;
break;
case "Google.Protobuf.WellKnownTypes.FieldMask":
protobufType = WellKnown.FieldMask;
break;
case "Google.Protobuf.WellKnownTypes.FloatValue":
protobufType = WellKnown.FloatValue;
break;
case "Google.Protobuf.WellKnownTypes.Int32Value":
protobufType = WellKnown.Int32Value;
break;
case "Google.Protobuf.WellKnownTypes.Int64Value":
protobufType = WellKnown.Int64Value;
break;
case "Google.Protobuf.WellKnownTypes.ListValue":
protobufType = WellKnown.ListValue;
break;
case "Google.Protobuf.WellKnownTypes.Method":
protobufType = WellKnown.Method;
break;
case "Google.Protobuf.WellKnownTypes.Mixin":
protobufType = WellKnown.Mixin;
break;
case "Google.Protobuf.WellKnownTypes.NullValue":
protobufType = WellKnown.NullValue;
break;
case "Google.Protobuf.WellKnownTypes.Option":
protobufType = WellKnown.Option;
break;
case "Google.Protobuf.WellKnownTypes.SourceContext":
protobufType = WellKnown.SourceContext;
break;
case "Google.Protobuf.WellKnownTypes.StringValue":
protobufType = WellKnown.StringValue;
break;
case "Google.Protobuf.WellKnownTypes.Struct":
protobufType = WellKnown.Struct;
break;
case "Google.Protobuf.WellKnownTypes.Syntax":
protobufType = WellKnown.Syntax;
break;
case "Google.Protobuf.WellKnownTypes.Timestamp":
protobufType = WellKnown.Timestamp;
break;
case "Google.Protobuf.WellKnownTypes.Type":
protobufType = WellKnown.Type;
break;
case "Google.Protobuf.WellKnownTypes.UInt32Value":
protobufType = WellKnown.UInt32Value;
break;
case "Google.Protobuf.WellKnownTypes.UInt64Value":
protobufType = WellKnown.UInt64Value;
break;
case "Google.Protobuf.WellKnownTypes.Value":
protobufType = WellKnown.Value;
break;
default:
protobufType = null;
return false;
}
return true;
}
protected Protobuf NewProtobuf(ICilType topLevelType, TopLevel topLevel)
{ {
Protobuf protobuf = new() Protobuf protobuf = new()
{ {
AssemblyName = topLevelType.DeclaringAssemblyName, AssemblyName = topLevelType.Assembly.FullName,
Namespace = topLevelType.Namespace Namespace = topLevelType.Namespace
}; };
@ -546,14 +397,14 @@ public class ProtodecContext
return protobuf; return protobuf;
} }
protected Protobuf GetProtobuf<T>(ICilType topLevelType, T topLevel, ParserOptions options) private Protobuf GetProtobuf<T>(Type topLevelType, T topLevel, ParserOptions options)
where T : TopLevel, INestableType where T : TopLevel, INestableType
{ {
Protobuf protobuf; Protobuf protobuf;
if (topLevelType.IsNested) if (topLevelType.IsNested)
{ {
ICilType parent = topLevelType.DeclaringType!.DeclaringType!; Type parent = topLevelType.DeclaringType!.DeclaringType!;
if (!_parsed.TryGetValue(parent.FullName, out TopLevel? parentTopLevel)) if (!_parsed.TryGetValue(parent.FullName ?? parent.Name, out TopLevel? parentTopLevel))
{ {
parentTopLevel = ParseMessage(parent, options); parentTopLevel = ParseMessage(parent, options);
} }
@ -572,12 +423,12 @@ public class ProtodecContext
return protobuf; return protobuf;
} }
protected string TranslateMethodName(string methodName) => private string TranslateMethodName(string methodName) =>
NameLookup?.Invoke(methodName, out string? translatedName) == true NameLookup?.Invoke(methodName, out string? translatedName) == true
? translatedName ? translatedName
: methodName; : methodName;
protected string TranslateOneOfPropName(string oneOfPropName) private string TranslateOneOfPropName(string oneOfPropName)
{ {
if (NameLookup?.Invoke(oneOfPropName, out string? translatedName) != true) if (NameLookup?.Invoke(oneOfPropName, out string? translatedName) != true)
{ {
@ -592,7 +443,7 @@ public class ProtodecContext
return translatedName!.TrimEnd("Case").ToSnakeCaseLower(); return translatedName!.TrimEnd("Case").ToSnakeCaseLower();
} }
protected string TranslateMessageFieldName(string fieldName) private string TranslateMessageFieldName(string fieldName)
{ {
if (NameLookup?.Invoke(fieldName, out string? translatedName) != true) if (NameLookup?.Invoke(fieldName, out string? translatedName) != true)
{ {
@ -607,22 +458,25 @@ public class ProtodecContext
return translatedName!.ToSnakeCaseLower(); return translatedName!.ToSnakeCaseLower();
} }
protected string TranslateEnumFieldName(IEnumerable<ICilAttribute> attributes, string fieldName, string enumName) private string TranslateEnumFieldName(FieldInfo field, string enumName)
{ {
if (attributes.SingleOrDefault(static attr => attr.Type.Name == "OriginalNameAttribute") if (field.GetCustomAttributesData()
?.ConstructorArgumentValues[0] is string originalName) .SingleOrDefault(static attr => attr.AttributeType.Name == "OriginalNameAttribute")
?.ConstructorArguments[0]
.Value
is string originalName)
{ {
return originalName; return originalName;
} }
if (NameLookup?.Invoke(fieldName, out string? translatedName) == true) if (NameLookup?.Invoke(field.Name, out string? fieldName) != true)
{ {
fieldName = translatedName; fieldName = field.Name;
} }
if (!IsBeebyted(fieldName)) if (!IsBeebyted(fieldName!))
{ {
fieldName = fieldName.ToSnakeCaseUpper(); fieldName = fieldName!.ToSnakeCaseUpper();
} }
if (!IsBeebyted(enumName)) if (!IsBeebyted(enumName))
@ -633,12 +487,14 @@ public class ProtodecContext
return enumName + '_' + fieldName; return enumName + '_' + fieldName;
} }
protected string TranslateTypeName(ICilType type) private string TranslateTypeName(Type type)
{ {
if (NameLookup is null) if (NameLookup is null)
return type.Name; return type.Name;
string fullName = type.FullName; string? fullName = type.FullName;
Guard.IsNotNull(fullName);
int genericArgs = fullName.IndexOf('['); int genericArgs = fullName.IndexOf('[');
if (genericArgs != -1) if (genericArgs != -1)
fullName = fullName[..genericArgs]; fullName = fullName[..genericArgs];
@ -659,17 +515,177 @@ public class ProtodecContext
return translatedName; return translatedName;
} }
public static bool LookupScalarAndWellKnownTypes(Type type, [NotNullWhen(true)] out IType? fieldType, out string? import)
{
switch (type.FullName)
{
case "System.String":
import = null;
fieldType = Scalar.String;
return true;
case "System.Boolean":
import = null;
fieldType = Scalar.Bool;
return true;
case "System.Double":
import = null;
fieldType = Scalar.Double;
return true;
case "System.UInt32":
import = null;
fieldType = Scalar.UInt32;
return true;
case "System.UInt64":
import = null;
fieldType = Scalar.UInt64;
return true;
case "System.Int32":
import = null;
fieldType = Scalar.Int32;
return true;
case "System.Int64":
import = null;
fieldType = Scalar.Int64;
return true;
case "System.Single":
import = null;
fieldType = Scalar.Float;
return true;
case "Google.Protobuf.ByteString":
import = null;
fieldType = Scalar.Bytes;
return true;
case "Google.Protobuf.WellKnownTypes.Any":
import = "google/protobuf/any.proto";
fieldType = WellKnown.Any;
return true;
case "Google.Protobuf.WellKnownTypes.Api":
import = "google/protobuf/api.proto";
fieldType = WellKnown.Api;
return true;
case "Google.Protobuf.WellKnownTypes.BoolValue":
import = "google/protobuf/wrappers.proto";
fieldType = WellKnown.BoolValue;
return true;
case "Google.Protobuf.WellKnownTypes.BytesValue":
import = "google/protobuf/wrappers.proto";
fieldType = WellKnown.BytesValue;
return true;
case "Google.Protobuf.WellKnownTypes.DoubleValue":
import = "google/protobuf/wrappers.proto";
fieldType = WellKnown.DoubleValue;
return true;
case "Google.Protobuf.WellKnownTypes.Duration":
import = "google/protobuf/duration.proto";
fieldType = WellKnown.Duration;
return true;
case "Google.Protobuf.WellKnownTypes.Empty":
import = "google/protobuf/empty.proto";
fieldType = WellKnown.Empty;
return true;
case "Google.Protobuf.WellKnownTypes.Enum":
import = "google/protobuf/type.proto";
fieldType = WellKnown.Enum;
return true;
case "Google.Protobuf.WellKnownTypes.EnumValue":
import = "google/protobuf/type.proto";
fieldType = WellKnown.EnumValue;
return true;
case "Google.Protobuf.WellKnownTypes.Field":
import = "google/protobuf/type.proto";
fieldType = WellKnown.Field;
return true;
case "Google.Protobuf.WellKnownTypes.FieldMask":
import = "google/protobuf/field_mask.proto";
fieldType = WellKnown.FieldMask;
return true;
case "Google.Protobuf.WellKnownTypes.FloatValue":
import = "google/protobuf/wrappers.proto";
fieldType = WellKnown.FloatValue;
return true;
case "Google.Protobuf.WellKnownTypes.Int32Value":
import = "google/protobuf/wrappers.proto";
fieldType = WellKnown.Int32Value;
return true;
case "Google.Protobuf.WellKnownTypes.Int64Value":
import = "google/protobuf/wrappers.proto";
fieldType = WellKnown.Int64Value;
return true;
case "Google.Protobuf.WellKnownTypes.ListValue":
import = "google/protobuf/struct.proto";
fieldType = WellKnown.ListValue;
return true;
case "Google.Protobuf.WellKnownTypes.Method":
import = "google/protobuf/api.proto";
fieldType = WellKnown.Method;
return true;
case "Google.Protobuf.WellKnownTypes.Mixin":
import = "google/protobuf/api.proto";
fieldType = WellKnown.Mixin;
return true;
case "Google.Protobuf.WellKnownTypes.NullValue":
import = "google/protobuf/struct.proto";
fieldType = WellKnown.NullValue;
return true;
case "Google.Protobuf.WellKnownTypes.Option":
import = "google/protobuf/type.proto";
fieldType = WellKnown.Option;
return true;
case "Google.Protobuf.WellKnownTypes.SourceContext":
import = "google/protobuf/source_context.proto";
fieldType = WellKnown.SourceContext;
return true;
case "Google.Protobuf.WellKnownTypes.StringValue":
import = "google/protobuf/wrappers.proto";
fieldType = WellKnown.StringValue;
return true;
case "Google.Protobuf.WellKnownTypes.Struct":
import = "google/protobuf/struct.proto";
fieldType = WellKnown.Struct;
return true;
case "Google.Protobuf.WellKnownTypes.Syntax":
import = "google/protobuf/type.proto";
fieldType = WellKnown.Syntax;
return true;
case "Google.Protobuf.WellKnownTypes.Timestamp":
import = "google/protobuf/timestamp.proto";
fieldType = WellKnown.Timestamp;
return true;
case "Google.Protobuf.WellKnownTypes.Type":
import = "google/protobuf/type.proto";
fieldType = WellKnown.Type;
return true;
case "Google.Protobuf.WellKnownTypes.UInt32Value":
import = "google/protobuf/wrappers.proto";
fieldType = WellKnown.UInt32Value;
return true;
case "Google.Protobuf.WellKnownTypes.UInt64Value":
import = "google/protobuf/wrappers.proto";
fieldType = WellKnown.UInt64Value;
return true;
case "Google.Protobuf.WellKnownTypes.Value":
import = "google/protobuf/struct.proto";
fieldType = WellKnown.Value;
return true;
default:
import = null;
fieldType = null;
return false;
}
}
// ReSharper disable once IdentifierTypo // ReSharper disable once IdentifierTypo
protected static bool IsBeebyted(string name) => private static bool IsBeebyted(string name) =>
name.Length == 11 && name.CountUpper() == 11; name.Length == 11 && name.CountUpper() == 11;
protected static bool HasGeneratedCodeAttribute(IEnumerable<ICilAttribute> attributes, string tool) => private static bool HasGeneratedCodeAttribute(IEnumerable<CustomAttributeData> attributes, string tool) =>
attributes.Any(attr => attr.Type.Name == nameof(GeneratedCodeAttribute) attributes.Any(attr => attr.AttributeType.Name == nameof(GeneratedCodeAttribute)
&& attr.ConstructorArgumentValues[0] as string == tool); && attr.ConstructorArguments[0].Value as string == tool);
protected static bool HasNonUserCodeAttribute(IEnumerable<ICilAttribute> attributes) => private static bool HasNonUserCodeAttribute(IEnumerable<CustomAttributeData> attributes) =>
attributes.Any(static attr => attr.Type.Name == nameof(DebuggerNonUserCodeAttribute)); attributes.Any(static attr => attr.AttributeType.Name == nameof(DebuggerNonUserCodeAttribute));
protected static bool HasObsoleteAttribute(IEnumerable<ICilAttribute> attributes) => private static bool HasObsoleteAttribute(IEnumerable<CustomAttributeData> attributes) =>
attributes.Any(static attr => attr.Type.Name == nameof(ObsoleteAttribute)); attributes.Any(static attr => attr.AttributeType.Name == nameof(ObsoleteAttribute));
} }

View File

@ -1,19 +1,10 @@
// Copyright © 2024 Xpl0itR using System;
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
using System;
using System.CodeDom.Compiler; using System.CodeDom.Compiler;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using LibProtodec; using LibProtodec;
using LibProtodec.Loaders; using LibProtodec.Models;
using LibProtodec.Models.Cil;
using LibProtodec.Models.Protobuf;
using Microsoft.Extensions.Logging;
const string indent = " "; const string indent = " ";
const string help = """ const string help = """
@ -22,7 +13,6 @@ const string help = """
target_assembly_path Either the path to the target assembly or a directory of assemblies, all of which be parsed. target_assembly_path Either the path to the target assembly or a directory of assemblies, all of which be parsed.
out_path An existing directory to output into individual files, otherwise output to a single file. out_path An existing directory to output into individual files, otherwise output to a single file.
Options: Options:
--debug Drops the minimum log level to Debug.
--parse_service_servers Parses gRPC service definitions from server classes. --parse_service_servers Parses gRPC service definitions from server classes.
--parse_service_clients Parses gRPC service definitions from client classes. --parse_service_clients Parses gRPC service definitions from client classes.
--skip_enums Skip parsing enums and replace references to them with int32. --skip_enums Skip parsing enums and replace references to them with int32.
@ -39,9 +29,6 @@ if (args.Length < 2)
string assembly = args[0]; string assembly = args[0];
string outPath = Path.GetFullPath(args[1]); string outPath = Path.GetFullPath(args[1]);
ParserOptions options = ParserOptions.None; ParserOptions options = ParserOptions.None;
LogLevel logLevel = args.Contains("--debug")
? LogLevel.Debug
: LogLevel.Information;
if (args.Contains("--skip_enums")) if (args.Contains("--skip_enums"))
options |= ParserOptions.SkipEnums; options |= ParserOptions.SkipEnums;
@ -52,30 +39,17 @@ if (args.Contains("--include_properties_without_non_user_code_attribute"))
if (args.Contains("--include_service_methods_without_generated_code_attribute")) if (args.Contains("--include_service_methods_without_generated_code_attribute"))
options |= ParserOptions.IncludeServiceMethodsWithoutGeneratedCodeAttribute; options |= ParserOptions.IncludeServiceMethodsWithoutGeneratedCodeAttribute;
using ILoggerFactory loggerFactory = LoggerFactory.Create( using AssemblyInspector inspector = new(assembly);
builder => builder.AddSimpleConsole(static console => console.IncludeScopes = true) ProtodecContext ctx = new();
.SetMinimumLevel(logLevel));
ILogger logger = loggerFactory.CreateLogger("protodec");
logger.LogInformation("Loading target assemblies..."); foreach (Type message in inspector.GetProtobufMessageTypes())
using CilAssemblyLoader loader = new ClrAssemblyLoader(
assembly, loggerFactory.CreateLogger<ClrAssemblyLoader>());
ProtodecContext ctx = new()
{
Logger = loggerFactory.CreateLogger<ProtodecContext>()
};
logger.LogInformation("Parsing Protobuf message types...");
foreach (ICilType message in GetProtobufMessageTypes())
{ {
ctx.ParseMessage(message, options); ctx.ParseMessage(message, options);
} }
if (args.Contains("--parse_service_servers")) if (args.Contains("--parse_service_servers"))
{ {
logger.LogInformation("Parsing Protobuf service server types..."); foreach (Type service in inspector.GetProtobufServiceServerTypes())
foreach (ICilType service in GetProtobufServiceServerTypes())
{ {
ctx.ParseService(service, options); ctx.ParseService(service, options);
} }
@ -83,8 +57,7 @@ if (args.Contains("--parse_service_servers"))
if (args.Contains("--parse_service_clients")) if (args.Contains("--parse_service_clients"))
{ {
logger.LogInformation("Parsing Protobuf service client types..."); foreach (Type service in inspector.GetProtobufServiceClientTypes())
foreach (ICilType service in GetProtobufServiceClientTypes())
{ {
ctx.ParseService(service, options); ctx.ParseService(service, options);
} }
@ -92,9 +65,8 @@ if (args.Contains("--parse_service_clients"))
if (Directory.Exists(outPath)) if (Directory.Exists(outPath))
{ {
logger.LogInformation("Writing {count} Protobuf files to \"{path}\"...", ctx.Protobufs.Count, outPath);
HashSet<string> writtenFiles = []; HashSet<string> writtenFiles = [];
foreach (Protobuf protobuf in ctx.Protobufs) foreach (Protobuf protobuf in ctx.Protobufs)
{ {
// This workaround stops files from being overwritten in the case of a naming conflict, // This workaround stops files from being overwritten in the case of a naming conflict,
@ -115,26 +87,8 @@ if (Directory.Exists(outPath))
} }
else else
{ {
logger.LogInformation("Writing Protobufs as a single file to \"{path}\"...", outPath);
using StreamWriter streamWriter = new(outPath); using StreamWriter streamWriter = new(outPath);
using IndentedTextWriter indentWriter = new(streamWriter, indent); using IndentedTextWriter indentWriter = new(streamWriter, indent);
ctx.WriteAllTo(indentWriter); ctx.WriteAllTo(indentWriter);
} }
IEnumerable<ICilType> GetProtobufMessageTypes() =>
loader.LoadedTypes.Where(
type => type is { IsNested: false, IsSealed: true }
&& type.Namespace?.StartsWith("Google.Protobuf", StringComparison.Ordinal) != true
&& type.IsAssignableTo(loader.IMessage));
IEnumerable<ICilType> GetProtobufServiceClientTypes() =>
loader.LoadedTypes.Where(
type => type is { IsNested: true, IsAbstract: false }
&& type.IsAssignableTo(loader.ClientBase));
IEnumerable<ICilType> GetProtobufServiceServerTypes() =>
loader.LoadedTypes.Where(
type => type is { IsNested: true, IsAbstract: true, DeclaringType: { IsNested: false, IsSealed: true, IsAbstract: true } }
&& type.CustomAttributes.Any(attribute => attribute.Type == loader.BindServiceMethodAttribute));

View File

@ -12,7 +12,6 @@
<ItemGroup> <ItemGroup>
<ProjectReference Include="$(SolutionDir)src\LibProtodec\LibProtodec.csproj" /> <ProjectReference Include="$(SolutionDir)src\LibProtodec\LibProtodec.csproj" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="9.0.0-preview.5.24306.7" />
</ItemGroup> </ItemGroup>
</Project> </Project>