Compare commits

..

2 Commits

Author SHA1 Message Date
Xpl0itR f13e27a11c
Attribute parsing for metadata version >= 29 2024-07-02 05:37:15 +01:00
Xpl0itR c23dd70b6d
PoC using dev branch of LibCpp2Il 2024-06-29 20:51:17 +01:00
24 changed files with 350 additions and 459 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

@ -16,7 +16,6 @@ Arguments:
unity_version The version of Unity which was used to create the metadata file or alternatively, the path to the globalgamemanagers or the data.unity3d file. unity_version The version of Unity which was used to create the metadata file or alternatively, the path to the globalgamemanagers or the data.unity3d file.
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

@ -19,9 +19,8 @@
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\..\..\Cpp2IL\LibCpp2IL\LibCpp2IL.csproj" /> <ProjectReference Include="..\..\..\Cpp2IL\LibCpp2IL\LibCpp2IL.csproj" />
<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

@ -12,15 +12,14 @@ using System.Reflection;
using CommunityToolkit.Diagnostics; using CommunityToolkit.Diagnostics;
using LibProtodec.Models.Cil; using LibProtodec.Models.Cil;
using LibProtodec.Models.Cil.Clr; using LibProtodec.Models.Cil.Clr;
using Microsoft.Extensions.Logging;
namespace LibProtodec.Loaders; namespace LibProtodec.Loaders;
public sealed class ClrAssemblyLoader : CilAssemblyLoader public sealed class ClrAssemblyLoader : ICilAssemblyLoader
{ {
public readonly MetadataLoadContext LoadContext; public readonly MetadataLoadContext LoadContext;
public ClrAssemblyLoader(string assemblyPath, ILogger? logger = null) public ClrAssemblyLoader(string assemblyPath)
{ {
bool isFile = File.Exists(assemblyPath); bool isFile = File.Exists(assemblyPath);
string assemblyDir = isFile string assemblyDir = isFile
@ -29,33 +28,67 @@ public sealed class ClrAssemblyLoader : CilAssemblyLoader
PermissiveAssemblyResolver assemblyResolver = new( PermissiveAssemblyResolver assemblyResolver = new(
Directory.EnumerateFiles(assemblyDir, searchPattern: "*.dll")); Directory.EnumerateFiles(assemblyDir, searchPattern: "*.dll"));
LoadContext = new MetadataLoadContext(assemblyResolver); LoadContext = new MetadataLoadContext(assemblyResolver);
LoadedTypes = isFile
IEnumerable<Type> allTypes = isFile ? LoadContext.LoadFromAssemblyPath(assemblyPath)
? LoadContext.LoadFromAssemblyPath(assemblyPath).GetTypes() .GetTypes()
.Select(ClrType.GetOrCreate)
.ToList()
: assemblyResolver.AssemblyPathLookup.Values : assemblyResolver.AssemblyPathLookup.Values
.SelectMany(path => LoadContext.LoadFromAssemblyPath(path).GetTypes()); .SelectMany(path => LoadContext.LoadFromAssemblyPath(path).GetTypes())
.Select(ClrType.GetOrCreate)
this.LoadedTypes = allTypes.Where(static type => type.GenericTypeArguments.Length == 0) .ToList();
.Select(ClrType.GetOrCreate)
.ToList();
logger?.LogLoadedTypeAndAssemblyCount(this.LoadedTypes.Count, LoadContext.GetAssemblies().Count());
} }
protected override ICilType FindType(string typeFullName, string assemblySimpleName) public IReadOnlyList<ICilType> LoadedTypes { get; }
public ICilType IMessage
{ {
ICilType? type = this.LoadedTypes.SingleOrDefault(type => type?.FullName == typeFullName, null); get
if (type is not null) {
return type; ICilType? iMessage = LoadedTypes.SingleOrDefault(static type => type?.FullName == "Google.Protobuf.IMessage", null);
if (iMessage is not null)
return iMessage;
Type? clrType = LoadContext.LoadFromAssemblyName(assemblySimpleName).GetType(typeFullName); Type? iMessageType = LoadContext.LoadFromAssemblyName("Google.Protobuf").GetType("Google.Protobuf.IMessage");
Guard.IsNotNull(clrType); Guard.IsNotNull(iMessageType);
return ClrType.GetOrCreate(clrType); return ClrType.GetOrCreate(iMessageType);
}
} }
public override void Dispose() => public ICilType ClientBase
{
get
{
ICilType? clientBase = LoadedTypes.SingleOrDefault(static type => type?.FullName == "Grpc.Core.ClientBase", null);
if (clientBase is not null)
return clientBase;
Type? clientBaseType = LoadContext.LoadFromAssemblyName("Grpc.Core.Api").GetType("Grpc.Core.ClientBase");
Guard.IsNotNull(clientBaseType);
return ClrType.GetOrCreate(clientBaseType);
}
}
public ICilType BindServiceMethodAttribute
{
get
{
ICilType? attribute = LoadedTypes.SingleOrDefault(static type => type?.FullName == "Grpc.Core.BindServiceMethodAttribute", null);
if (attribute is not null)
return attribute;
Type? attributeType = LoadContext.LoadFromAssemblyName("Grpc.Core.Api").GetType("Grpc.Core.BindServiceMethodAttribute");
Guard.IsNotNull(attributeType);
return ClrType.GetOrCreate(attributeType);
}
}
public void Dispose() =>
LoadContext.Dispose(); LoadContext.Dispose();
/// <summary> /// <summary>

View File

@ -0,0 +1,23 @@
// 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 interface ICilAssemblyLoader : IDisposable
{
IReadOnlyList<ICilType> LoadedTypes { get; }
// ReSharper disable once InconsistentNaming
ICilType IMessage { get; }
ICilType ClientBase { get; }
ICilType BindServiceMethodAttribute { get; }
}

View File

@ -4,6 +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/.
using System.Collections.Generic;
using System.Linq; using System.Linq;
using AssetRipper.Primitives; using AssetRipper.Primitives;
using CommunityToolkit.Diagnostics; using CommunityToolkit.Diagnostics;
@ -12,27 +13,54 @@ using LibCpp2IL.Metadata;
using LibCpp2IL.Reflection; using LibCpp2IL.Reflection;
using LibProtodec.Models.Cil; using LibProtodec.Models.Cil;
using LibProtodec.Models.Cil.Il2Cpp; using LibProtodec.Models.Cil.Il2Cpp;
using Microsoft.Extensions.Logging;
namespace LibProtodec.Loaders; namespace LibProtodec.Loaders;
public sealed class Il2CppAssemblyLoader : CilAssemblyLoader public sealed class Il2CppAssemblyLoader : ICilAssemblyLoader
{ {
public Il2CppAssemblyLoader(string assemblyPath, string metadataPath, UnityVersion unityVersion, ILogger logger) public Il2CppAssemblyLoader(string assemblyPath, string metadataPath, UnityVersion unityVersion)
{ {
if (!LibCpp2IlMain.LoadFromFile(assemblyPath, metadataPath, unityVersion)) if (!LibCpp2IlMain.LoadFromFile(assemblyPath, metadataPath, unityVersion))
ThrowHelper.ThrowInvalidDataException("Failed to load il2cpp assembly!"); ThrowHelper.ThrowInvalidDataException("Failed to load il2cpp assembly!");
this.LoadedTypes = LibCpp2IlMain.TheMetadata!.typeDefs.Select(Il2CppType.GetOrCreate).ToList(); LoadedTypes = LibCpp2IlMain.TheMetadata!.typeDefs.Select(Il2CppType.GetOrCreate).ToList();
logger?.LogLoadedTypeAndAssemblyCount(this.LoadedTypes.Count, LibCpp2IlMain.TheMetadata.imageDefinitions.Length);
} }
protected override ICilType FindType(string typeFullName, string assemblySimpleName) public IReadOnlyList<ICilType> LoadedTypes { get; }
public ICilType IMessage
{ {
Il2CppTypeDefinition? type = LibCpp2IlReflection.GetTypeByFullName(typeFullName); get
Guard.IsNotNull(type); {
Il2CppTypeDefinition? iMessage = LibCpp2IlReflection.GetTypeByFullName("Google.Protobuf.IMessage");
Guard.IsNotNull(iMessage);
return Il2CppType.GetOrCreate(type); return Il2CppType.GetOrCreate(iMessage);
}
} }
public ICilType ClientBase
{
get
{
Il2CppTypeDefinition? clientBase = LibCpp2IlReflection.GetTypeByFullName("Grpc.Core.ClientBase");
Guard.IsNotNull(clientBase);
return Il2CppType.GetOrCreate(clientBase);
}
}
public ICilType BindServiceMethodAttribute
{
get
{
Il2CppTypeDefinition? attribute = LibCpp2IlReflection.GetTypeByFullName("Grpc.Core.BindServiceMethodAttribute");
Guard.IsNotNull(attribute);
return Il2CppType.GetOrCreate(attribute);
}
}
public void Dispose() =>
LibCpp2IlMain.Reset();
} }

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

@ -12,12 +12,10 @@ namespace LibProtodec.Models.Cil.Clr;
public sealed class ClrAttribute(CustomAttributeData clrAttribute) : ICilAttribute public sealed class ClrAttribute(CustomAttributeData clrAttribute) : ICilAttribute
{ {
private ICilType? _type;
private object?[]? _constructorArgumentValues; private object?[]? _constructorArgumentValues;
public ICilType Type => public ICilType Type =>
_type ??= ClrType.GetOrCreate( ClrType.GetOrCreate(clrAttribute.AttributeType);
clrAttribute.AttributeType);
public IList<object?> ConstructorArgumentValues public IList<object?> ConstructorArgumentValues
{ {

View File

@ -11,9 +11,6 @@ namespace LibProtodec.Models.Cil.Clr;
public sealed class ClrMethod(MethodInfo clrMethod) : ClrMember(clrMethod), ICilMethod public sealed class ClrMethod(MethodInfo clrMethod) : ClrMember(clrMethod), ICilMethod
{ {
public bool IsConstructor =>
clrMethod.IsConstructor;
public bool IsPublic => public bool IsPublic =>
clrMethod.IsPublic; clrMethod.IsPublic;
@ -24,8 +21,7 @@ public sealed class ClrMethod(MethodInfo clrMethod) : ClrMember(clrMethod), ICil
clrMethod.IsVirtual; clrMethod.IsVirtual;
public ICilType ReturnType => public ICilType ReturnType =>
ClrType.GetOrCreate( ClrType.GetOrCreate(clrMethod.ReturnType);
clrMethod.ReturnType);
public IEnumerable<ICilType> GetParameterTypes() public IEnumerable<ICilType> GetParameterTypes()
{ {

View File

@ -10,29 +10,23 @@ namespace LibProtodec.Models.Cil.Clr;
public sealed class ClrProperty(PropertyInfo clrProperty) : ClrMember(clrProperty), ICilProperty 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 => public ICilType Type =>
ClrType.GetOrCreate( ClrType.GetOrCreate(
clrProperty.PropertyType); clrProperty.PropertyType);
public bool CanRead =>
clrProperty.CanRead;
public bool CanWrite =>
clrProperty.CanWrite;
public ICilMethod? Getter =>
CanRead
? new ClrMethod(clrProperty.GetMethod!)
: null;
public ICilMethod? Setter =>
CanWrite
? new ClrMethod(clrProperty.SetMethod!)
: null;
} }

View File

@ -29,13 +29,14 @@ public sealed class ClrType : ClrMember, ICilType
_clrType.Namespace; _clrType.Namespace;
public string DeclaringAssemblyName => public string DeclaringAssemblyName =>
_clrType.Assembly.FullName!; _clrType.Assembly.FullName
?? ThrowHelper.ThrowArgumentNullException<string>(
nameof(_clrType.Assembly.FullName));
public ICilType? BaseType => public ICilType? BaseType =>
_clrType.BaseType is null _clrType.BaseType is null
? null ? null
: GetOrCreate( : GetOrCreate(_clrType.BaseType);
_clrType.BaseType);
public bool IsAbstract => public bool IsAbstract =>
_clrType.IsAbstract; _clrType.IsAbstract;

View File

@ -12,11 +12,10 @@ public interface ICilMethod
{ {
string Name { get; } string Name { get; }
bool IsConstructor { get; } bool IsInherited { get; }
bool IsInherited { get; } bool IsPublic { get; }
bool IsPublic { get; } bool IsStatic { get; }
bool IsStatic { get; } bool IsVirtual { get; }
bool IsVirtual { get; }
ICilType ReturnType { get; } ICilType ReturnType { get; }

View File

@ -20,9 +20,6 @@ public sealed class Il2CppMethod(Il2CppMethodDefinition il2CppMethod) : Il2CppMe
public bool IsInherited => public bool IsInherited =>
false; false;
public bool IsConstructor =>
false;
public bool IsPublic => public bool IsPublic =>
(il2CppMethod.Attributes & MethodAttributes.Public) != 0; (il2CppMethod.Attributes & MethodAttributes.Public) != 0;

View File

@ -10,7 +10,7 @@ using LibProtodec.Models.Protobuf.Types;
namespace LibProtodec.Models.Protobuf.Fields; namespace LibProtodec.Models.Protobuf.Fields;
public sealed class MessageField(Message declaringMessage) public sealed class MessageField
{ {
public required IProtobufType Type { get; init; } public required IProtobufType Type { get; init; }
public required string Name { get; init; } public required string Name { get; init; }
@ -19,15 +19,14 @@ public sealed class MessageField(Message declaringMessage)
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

@ -10,7 +10,7 @@ using LibProtodec.Models.Protobuf.Types;
namespace LibProtodec.Models.Protobuf.Fields; namespace LibProtodec.Models.Protobuf.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 IProtobufType RequestType { get; init; }
@ -20,7 +20,7 @@ public sealed class ServiceMethod(Service declaringService)
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

@ -9,13 +9,15 @@ using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using LibProtodec.Models.Protobuf.TopLevels; using LibProtodec.Models.Protobuf.TopLevels;
using LibProtodec.Models.Protobuf.Types;
namespace LibProtodec.Models.Protobuf; namespace LibProtodec.Models.Protobuf;
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, IProtobufType 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

@ -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

@ -37,7 +37,7 @@ 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, List<int> fieldIds) in OneOfs)
@ -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

@ -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.Protobuf.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

@ -18,23 +18,23 @@ using LibProtodec.Models.Protobuf;
using LibProtodec.Models.Protobuf.Fields; using LibProtodec.Models.Protobuf.Fields;
using LibProtodec.Models.Protobuf.TopLevels; using LibProtodec.Models.Protobuf.TopLevels;
using LibProtodec.Models.Protobuf.Types; using LibProtodec.Models.Protobuf.Types;
using Microsoft.Extensions.Logging;
namespace LibProtodec; namespace LibProtodec;
public delegate bool TypeLookupFunc(ICilType cilType, [NotNullWhen(true)] out IProtobufType? protobufType);
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 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");
@ -50,14 +50,12 @@ public class ProtodecContext
} }
} }
public virtual Message ParseMessage(ICilType messageClass, ParserOptions options = ParserOptions.None) public Message ParseMessage(ICilType 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, out TopLevel? parsedMessage))
{ {
Logger?.LogParsedMessage(parsedMessage.Name);
return (Message)parsedMessage; return (Message)parsedMessage;
} }
@ -80,23 +78,18 @@ public class ProtodecContext
for (int pi = 0, fi = 0; pi < properties.Count; pi++) for (int pi = 0, fi = 0; pi < properties.Count; pi++)
{ {
ICilProperty property = properties[pi]; ICilProperty property = properties[pi];
ICilType propertyType = property.Type;
using IDisposable? __ = Logger?.BeginScopeParsingProperty(property.Name, propertyType.FullName);
if (((options & ParserOptions.IncludePropertiesWithoutNonUserCodeAttribute) == 0 && !HasNonUserCodeAttribute(property.CustomAttributes))) if (((options & ParserOptions.IncludePropertiesWithoutNonUserCodeAttribute) == 0 && !HasNonUserCodeAttribute(property.CustomAttributes)))
{ {
Logger?.LogSkippingPropertyWithoutNonUserCodeAttribute();
continue; continue;
} }
ICilType propertyType = property.Type;
// 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);
List<int> oneOfProtoFieldIds = propertyType.GetFields() List<int> oneOfProtoFieldIds = propertyType.GetFields()
.Where(static field => field.IsLiteral) .Where(static field => field.IsLiteral)
.Select(static field => (int)field.ConstantValue!) .Select(static field => (int)field.ConstantValue!)
@ -114,13 +107,7 @@ public class ProtodecContext
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),
@ -129,23 +116,19 @@ public class ProtodecContext
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++; fi++;
} }
Logger?.LogParsedMessage(message.Name);
return message; return message;
} }
public virtual Enum ParseEnum(ICilType enumEnum, ParserOptions options = ParserOptions.None) public Enum ParseEnum(ICilType 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, out TopLevel? parsedEnum))
{ {
Logger?.LogParsedEnum(parsedEnum.Name);
return (Enum)parsedEnum; return (Enum)parsedEnum;
} }
@ -158,19 +141,15 @@ public class ProtodecContext
Protobuf protobuf = GetProtobuf(enumEnum, @enum, options); Protobuf protobuf = GetProtobuf(enumEnum, @enum, options);
foreach (ICilField enumField in enumEnum.GetFields().Where(static field => field.IsLiteral)) foreach (ICilField field in enumEnum.GetFields().Where(static field => field.IsLiteral))
{ {
using IDisposable? __ = Logger?.BeginScopeParsingField(enumField.Name); @enum.Fields.Add(
new EnumField
EnumField field = new() {
{ Id = (int)field.ConstantValue!,
Id = (int)enumField.ConstantValue!, Name = TranslateEnumFieldName(field.CustomAttributes, field.Name, @enum.Name),
Name = TranslateEnumFieldName(enumField.CustomAttributes, enumField.Name, @enum.Name), IsObsolete = HasObsoleteAttribute(field.CustomAttributes)
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))
@ -179,11 +158,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(ICilType serviceClass, ParserOptions options = ParserOptions.None)
{ {
Guard.IsTrue(serviceClass.IsClass); Guard.IsTrue(serviceClass.IsClass);
@ -209,11 +187,9 @@ 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, out TopLevel? parsedService))
{ {
Logger?.LogParsedService(parsedService.Name);
return (Service)parsedService; return (Service)parsedService;
} }
@ -226,18 +202,15 @@ public class ProtodecContext
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 (ICilMethod method in serviceClass.GetMethods().Where(static method => method is { IsInherited: false, IsPublic: true, IsStatic: false }))
{ {
using IDisposable? __ = Logger?.BeginScopeParsingMethod(cilMethod.Name);
if ((options & ParserOptions.IncludeServiceMethodsWithoutGeneratedCodeAttribute) == 0 if ((options & ParserOptions.IncludeServiceMethodsWithoutGeneratedCodeAttribute) == 0
&& !HasGeneratedCodeAttribute(cilMethod.CustomAttributes, "grpc_csharp_plugin")) && !HasGeneratedCodeAttribute(method.CustomAttributes, "grpc_csharp_plugin"))
{ {
Logger?.LogSkippingMethodWithoutGeneratedCodeAttribute();
continue; continue;
} }
ICilType requestType, responseType, returnType = cilMethod.ReturnType; ICilType requestType, responseType, returnType = method.ReturnType;
bool streamReq, streamRes; bool streamReq, streamRes;
if (isClientClass.Value) if (isClientClass.Value)
@ -245,14 +218,12 @@ public class ProtodecContext
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(); List<ICilType> parameters = method.GetParameterTypes().ToList();
if (parameters.Count > 2) if (parameters.Count > 2)
{ {
Logger?.LogSkippingDuplicateMethod();
continue; continue;
} }
@ -280,7 +251,7 @@ public class ProtodecContext
} }
else else
{ {
List<ICilType> parameters = cilMethod.GetParameterTypes().ToList(); List<ICilType> parameters = method.GetParameterTypes().ToList();
if (parameters[0].GenericTypeArguments.Count == 1) if (parameters[0].GenericTypeArguments.Count == 1)
{ {
@ -305,25 +276,22 @@ public class ProtodecContext
} }
} }
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(method.CustomAttributes),
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 IProtobufType ParseFieldType(ICilType type, ParserOptions options, Protobuf referencingProtobuf)
{ {
switch (type.GenericTypeArguments.Count) switch (type.GenericTypeArguments.Count)
{ {
@ -336,7 +304,7 @@ 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 IProtobufType? fieldType))
{ {
if (type.IsEnum) if (type.IsEnum)
{ {
@ -370,132 +338,7 @@ public class ProtodecContext
return fieldType; return fieldType;
} }
protected virtual bool LookupType(ICilType cilType, [NotNullWhen(true)] out IProtobufType? protobufType) private Protobuf NewProtobuf(ICilType 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()
{ {
@ -510,7 +353,7 @@ public class ProtodecContext
return protobuf; return protobuf;
} }
protected Protobuf GetProtobuf<T>(ICilType topLevelType, T topLevel, ParserOptions options) private Protobuf GetProtobuf<T>(ICilType topLevelType, T topLevel, ParserOptions options)
where T : TopLevel, INestableType where T : TopLevel, INestableType
{ {
Protobuf protobuf; Protobuf protobuf;
@ -536,12 +379,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)
{ {
@ -556,7 +399,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)
{ {
@ -571,7 +414,7 @@ public class ProtodecContext
return translatedName!.ToSnakeCaseLower(); return translatedName!.ToSnakeCaseLower();
} }
protected string TranslateEnumFieldName(IEnumerable<ICilAttribute> attributes, string fieldName, string enumName) private string TranslateEnumFieldName(IEnumerable<ICilAttribute> attributes, string fieldName, string enumName)
{ {
if (LibCpp2IlMain.MetadataVersion >= 29f //TODO: do not merge into master until il2cpp-specific global is removed if (LibCpp2IlMain.MetadataVersion >= 29f //TODO: do not merge into master until il2cpp-specific global is removed
&& attributes.SingleOrDefault(static attr => attr.Type.Name == "OriginalNameAttribute") && attributes.SingleOrDefault(static attr => attr.Type.Name == "OriginalNameAttribute")
@ -598,7 +441,7 @@ public class ProtodecContext
return enumName + '_' + fieldName; return enumName + '_' + fieldName;
} }
protected string TranslateTypeName(ICilType type) private string TranslateTypeName(ICilType type)
{ {
if (NameLookup is null) if (NameLookup is null)
return type.Name; return type.Name;
@ -624,18 +467,139 @@ public class ProtodecContext
return translatedName; return translatedName;
} }
public static bool LookupScalarAndWellKnownTypes(ICilType cilType, [NotNullWhen(true)] out IProtobufType? protobufType)
{
switch (cilType.FullName)
{
case "System.String":
protobufType = Scalar.String;
return true;
case "System.Boolean":
protobufType = Scalar.Bool;
return true;
case "System.Double":
protobufType = Scalar.Double;
return true;
case "System.UInt32":
protobufType = Scalar.UInt32;
return true;
case "System.UInt64":
protobufType = Scalar.UInt64;
return true;
case "System.Int32":
protobufType = Scalar.Int32;
return true;
case "System.Int64":
protobufType = Scalar.Int64;
return true;
case "System.Single":
protobufType = Scalar.Float;
return true;
case "Google.Protobuf.ByteString":
protobufType = Scalar.Bytes;
return true;
case "Google.Protobuf.WellKnownTypes.Any":
protobufType = WellKnown.Any;
return true;
case "Google.Protobuf.WellKnownTypes.Api":
protobufType = WellKnown.Api;
return true;
case "Google.Protobuf.WellKnownTypes.BoolValue":
protobufType = WellKnown.BoolValue;
return true;
case "Google.Protobuf.WellKnownTypes.BytesValue":
protobufType = WellKnown.BytesValue;
return true;
case "Google.Protobuf.WellKnownTypes.DoubleValue":
protobufType = WellKnown.DoubleValue;
return true;
case "Google.Protobuf.WellKnownTypes.Duration":
protobufType = WellKnown.Duration;
return true;
case "Google.Protobuf.WellKnownTypes.Empty":
protobufType = WellKnown.Empty;
return true;
case "Google.Protobuf.WellKnownTypes.Enum":
protobufType = WellKnown.Enum;
return true;
case "Google.Protobuf.WellKnownTypes.EnumValue":
protobufType = WellKnown.EnumValue;
return true;
case "Google.Protobuf.WellKnownTypes.Field":
protobufType = WellKnown.Field;
return true;
case "Google.Protobuf.WellKnownTypes.FieldMask":
protobufType = WellKnown.FieldMask;
return true;
case "Google.Protobuf.WellKnownTypes.FloatValue":
protobufType = WellKnown.FloatValue;
return true;
case "Google.Protobuf.WellKnownTypes.Int32Value":
protobufType = WellKnown.Int32Value;
return true;
case "Google.Protobuf.WellKnownTypes.Int64Value":
protobufType = WellKnown.Int64Value;
return true;
case "Google.Protobuf.WellKnownTypes.ListValue":
protobufType = WellKnown.ListValue;
return true;
case "Google.Protobuf.WellKnownTypes.Method":
protobufType = WellKnown.Method;
return true;
case "Google.Protobuf.WellKnownTypes.Mixin":
protobufType = WellKnown.Mixin;
return true;
case "Google.Protobuf.WellKnownTypes.NullValue":
protobufType = WellKnown.NullValue;
return true;
case "Google.Protobuf.WellKnownTypes.Option":
protobufType = WellKnown.Option;
return true;
case "Google.Protobuf.WellKnownTypes.SourceContext":
protobufType = WellKnown.SourceContext;
return true;
case "Google.Protobuf.WellKnownTypes.StringValue":
protobufType = WellKnown.StringValue;
return true;
case "Google.Protobuf.WellKnownTypes.Struct":
protobufType = WellKnown.Struct;
return true;
case "Google.Protobuf.WellKnownTypes.Syntax":
protobufType = WellKnown.Syntax;
return true;
case "Google.Protobuf.WellKnownTypes.Timestamp":
protobufType = WellKnown.Timestamp;
return true;
case "Google.Protobuf.WellKnownTypes.Type":
protobufType = WellKnown.Type;
return true;
case "Google.Protobuf.WellKnownTypes.UInt32Value":
protobufType = WellKnown.UInt32Value;
return true;
case "Google.Protobuf.WellKnownTypes.UInt64Value":
protobufType = WellKnown.UInt64Value;
return true;
case "Google.Protobuf.WellKnownTypes.Value":
protobufType = WellKnown.Value;
return true;
default:
protobufType = 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<ICilAttribute> attributes, string tool) =>
attributes.Any(attr => attr.Type.Name == nameof(GeneratedCodeAttribute) attributes.Any(attr => attr.Type.Name == nameof(GeneratedCodeAttribute)
&& (LibCpp2IlMain.MetadataVersion < 29f //TODO: do not merge into master until il2cpp-specific global is removed && (LibCpp2IlMain.MetadataVersion < 29f //TODO: do not merge into master until il2cpp-specific global is removed
|| attr.ConstructorArgumentValues[0] as string == tool)); || attr.ConstructorArgumentValues[0] as string == tool));
protected static bool HasNonUserCodeAttribute(IEnumerable<ICilAttribute> attributes) => private static bool HasNonUserCodeAttribute(IEnumerable<ICilAttribute> attributes) =>
attributes.Any(static attr => attr.Type.Name == nameof(DebuggerNonUserCodeAttribute)); attributes.Any(static attr => attr.Type.Name == nameof(DebuggerNonUserCodeAttribute));
protected static bool HasObsoleteAttribute(IEnumerable<ICilAttribute> attributes) => private static bool HasObsoleteAttribute(IEnumerable<ICilAttribute> attributes) =>
attributes.Any(static attr => attr.Type.Name == nameof(ObsoleteAttribute)); attributes.Any(static attr => attr.Type.Name == nameof(ObsoleteAttribute));
} }

View File

@ -15,7 +15,6 @@ using LibProtodec;
using LibProtodec.Loaders; using LibProtodec.Loaders;
using LibProtodec.Models.Cil; using LibProtodec.Models.Cil;
using LibProtodec.Models.Protobuf; using LibProtodec.Models.Protobuf;
using Microsoft.Extensions.Logging;
const string indent = " "; const string indent = " ";
const string help = """ const string help = """
@ -26,7 +25,6 @@ const string help = """
unity_version The version of Unity which was used to create the metadata file or alternatively, the path to the globalgamemanagers or the data.unity3d file. unity_version The version of Unity which was used to create the metadata file or alternatively, the path to the globalgamemanagers or the data.unity3d file.
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.
@ -45,9 +43,6 @@ string metadata = args[1];
string uVersion = args[2]; string uVersion = args[2];
string outPath = Path.GetFullPath(args[3]); string outPath = Path.GetFullPath(args[3]);
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;
@ -67,21 +62,9 @@ if (!UnityVersion.TryParse(uVersion, out UnityVersion unityVersion, out _))
File.OpenRead(uVersion)); File.OpenRead(uVersion));
} }
using ILoggerFactory loggerFactory = LoggerFactory.Create( using ICilAssemblyLoader loader = new Il2CppAssemblyLoader(assembly, metadata, unityVersion);
builder => builder.AddSimpleConsole(static console => console.IncludeScopes = true) ProtodecContext ctx = new();
.SetMinimumLevel(logLevel));
ILogger logger = loggerFactory.CreateLogger("protodec");
logger.LogInformation("Loading target assemblies...");
using CilAssemblyLoader loader = new Il2CppAssemblyLoader(
assembly, metadata, unityVersion, loggerFactory.CreateLogger<Il2CppAssemblyLoader>());
ProtodecContext ctx = new()
{
Logger = loggerFactory.CreateLogger<ProtodecContext>()
};
logger.LogInformation("Parsing Protobuf message types...");
foreach (ICilType message in GetProtobufMessageTypes()) foreach (ICilType message in GetProtobufMessageTypes())
{ {
ctx.ParseMessage(message, options); ctx.ParseMessage(message, options);
@ -89,7 +72,6 @@ foreach (ICilType message in GetProtobufMessageTypes())
if (args.Contains("--parse_service_servers")) if (args.Contains("--parse_service_servers"))
{ {
logger.LogInformation("Parsing Protobuf service server types...");
foreach (ICilType service in GetProtobufServiceServerTypes()) foreach (ICilType service in GetProtobufServiceServerTypes())
{ {
ctx.ParseService(service, options); ctx.ParseService(service, options);
@ -98,7 +80,6 @@ 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 (ICilType service in GetProtobufServiceClientTypes()) foreach (ICilType service in GetProtobufServiceClientTypes())
{ {
ctx.ParseService(service, options); ctx.ParseService(service, options);
@ -107,9 +88,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,
@ -130,8 +110,6 @@ 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);

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>