Compare commits

...

3 Commits

38 changed files with 1332 additions and 474 deletions

View File

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

View File

@ -1,15 +1,22 @@
protodec
========
A tool to decompile protobuf classes compiled by [protoc](https://github.com/protocolbuffers/protobuf), from CIL assemblies back into .proto definitions.
A tool to decompile protobuf classes compiled by [protoc](https://github.com/protocolbuffers/protobuf), from il2cpp compiled CIL assemblies back into .proto definitions.
This branch was created as a proof-of-concept using [the development branch of LibCpp2Il](https://github.com/SamboyCoding/Cpp2IL/tree/development/LibCpp2IL) to parse the game assembly and metadata directly, without the intermediate step of generating dummy DLLs.
I offer no guarantees that this branch functions 1:1 with master, it may explode.
Usage
-----
```
Usage: protodec(.exe) <target_assembly_path> <out_path> [options]
Usage: protodec(.exe) <game_assembly_path> <global_metadata_path> <unity_version> <out_path> [options]
Arguments:
target_assembly_path Either the path to the target assembly or a directory of assemblies, all of which be parsed.
game_assembly_path The path to the game assembly DLL.
global_metadata_path The path to the global-metadata.dat 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.
Options:
--debug Drops the minimum log level to Debug.
--parse_service_servers Parses gRPC service definitions from server classes.
--parse_service_clients Parses gRPC service definitions from client classes.
--skip_enums Skip parsing enums and replace references to them with int32.
@ -21,8 +28,9 @@ Limitations
-----------
- Integers are assumed to be (u)int32/64 as CIL doesn't differentiate between them and sint32/64 and (s)fixed32/64.
- Package names are not preserved in protobuf compilation so naturally we cannot recover them during decompilation, which may result in naming conflicts.
- When decompiling from [Il2CppDumper](https://github.com/Perfare/Il2CppDumper) DummyDLLs
- The `Name` parameter of `OriginalNameAttribute` is not dumped. In this case, the CIL enum field names are used after conforming them to protobuf conventions
- Due to the development branch of Cpp2Il not yet recovering method bodies, when parsing an Il2Cpp assembly older than metadata version 29
- The `Name` parameter of `OriginalNameAttribute` is not parsed. In this case, the CIL enum field names are used after conforming them to protobuf conventions.
- The `Tool` parameter of `GeneratedCodeAttribute` is not compared against when parsing gRPC service methods, which may cause false positives in the event that another tool has generated methods in the service class.
License
-------

View File

@ -12,6 +12,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
README.md = README.md
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LibCpp2IL", "..\Cpp2IL\LibCpp2IL\LibCpp2IL.csproj", "{42B987D9-E551-48BC-A821-F7411C664ECC}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WasmDisassembler", "..\Cpp2IL\WasmDisassembler\WasmDisassembler.csproj", "{07C0845C-7579-43C0-B4C9-4769573F3A24}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -26,6 +30,14 @@ Global
{5F6DAD82-D9AD-4CE5-86E6-D20C9F059A4D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5F6DAD82-D9AD-4CE5-86E6-D20C9F059A4D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5F6DAD82-D9AD-4CE5-86E6-D20C9F059A4D}.Release|Any CPU.Build.0 = Release|Any CPU
{42B987D9-E551-48BC-A821-F7411C664ECC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{42B987D9-E551-48BC-A821-F7411C664ECC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{42B987D9-E551-48BC-A821-F7411C664ECC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{42B987D9-E551-48BC-A821-F7411C664ECC}.Release|Any CPU.Build.0 = Release|Any CPU
{07C0845C-7579-43C0-B4C9-4769573F3A24}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{07C0845C-7579-43C0-B4C9-4769573F3A24}.Debug|Any CPU.Build.0 = Debug|Any CPU
{07C0845C-7579-43C0-B4C9-4769573F3A24}.Release|Any CPU.ActiveCfg = Release|Any CPU
{07C0845C-7579-43C0-B4C9-4769573F3A24}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -18,9 +18,11 @@
</PropertyGroup>
<ItemGroup>
<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="System.Reflection.MetadataLoadContext" Version="8.0.0" />
<PackageReference Include="Xpl0itR.SystemEx" Version="1.1.0" />
<PackageReference Include="System.Reflection.MetadataLoadContext" Version="9.0.0-preview.5.24306.7" />
<PackageReference Include="Xpl0itR.SystemEx" Version="1.2.0" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,34 @@
// 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,14 +12,15 @@ 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 : ICilAssemblyLoader
public sealed class ClrAssemblyLoader : CilAssemblyLoader
{
public readonly MetadataLoadContext LoadContext;
public ClrAssemblyLoader(string assemblyPath)
public ClrAssemblyLoader(string assemblyPath, ILogger? logger = null)
{
bool isFile = File.Exists(assemblyPath);
string assemblyDir = isFile
@ -28,67 +29,33 @@ public sealed class ClrAssemblyLoader : ICilAssemblyLoader
PermissiveAssemblyResolver assemblyResolver = new(
Directory.EnumerateFiles(assemblyDir, searchPattern: "*.dll"));
LoadContext = new MetadataLoadContext(assemblyResolver);
LoadedTypes = isFile
? LoadContext.LoadFromAssemblyPath(assemblyPath)
.GetTypes()
.Select(ClrType.GetOrCreate)
.ToList()
IEnumerable<Type> allTypes = isFile
? LoadContext.LoadFromAssemblyPath(assemblyPath).GetTypes()
: assemblyResolver.AssemblyPathLookup.Values
.SelectMany(path => LoadContext.LoadFromAssemblyPath(path).GetTypes())
.Select(ClrType.GetOrCreate)
.ToList();
.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());
}
public IReadOnlyList<ICilType> LoadedTypes { get; }
public ICilType IMessage
protected override ICilType FindType(string typeFullName, string assemblySimpleName)
{
get
{
ICilType? iMessage = LoadedTypes.SingleOrDefault(static type => type?.FullName == "Google.Protobuf.IMessage", null);
if (iMessage is not null)
return iMessage;
ICilType? type = this.LoadedTypes.SingleOrDefault(type => type?.FullName == typeFullName, null);
if (type is not null)
return type;
Type? iMessageType = LoadContext.LoadFromAssemblyName("Google.Protobuf").GetType("Google.Protobuf.IMessage");
Guard.IsNotNull(iMessageType);
Type? clrType = LoadContext.LoadFromAssemblyName(assemblySimpleName).GetType(typeFullName);
Guard.IsNotNull(clrType);
return ClrType.GetOrCreate(iMessageType);
}
return ClrType.GetOrCreate(clrType);
}
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() =>
public override void Dispose() =>
LoadContext.Dispose();
/// <summary>

View File

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

View File

@ -0,0 +1,38 @@
// 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.Linq;
using AssetRipper.Primitives;
using CommunityToolkit.Diagnostics;
using LibCpp2IL;
using LibCpp2IL.Metadata;
using LibCpp2IL.Reflection;
using LibProtodec.Models.Cil;
using LibProtodec.Models.Cil.Il2Cpp;
using Microsoft.Extensions.Logging;
namespace LibProtodec.Loaders;
public sealed class Il2CppAssemblyLoader : CilAssemblyLoader
{
public Il2CppAssemblyLoader(string assemblyPath, string metadataPath, UnityVersion unityVersion, ILogger logger)
{
if (!LibCpp2IlMain.LoadFromFile(assemblyPath, metadataPath, unityVersion))
ThrowHelper.ThrowInvalidDataException("Failed to load il2cpp assembly!");
this.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)
{
Il2CppTypeDefinition? type = LibCpp2IlReflection.GetTypeByFullName(typeFullName);
Guard.IsNotNull(type);
return Il2CppType.GetOrCreate(type);
}
}

View File

@ -0,0 +1,86 @@
// 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

@ -6,37 +6,38 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace LibProtodec.Models.Cil.Clr;
public sealed class ClrAttribute(CustomAttributeData clrAttribute) : ICilAttribute
{
private IList<object?>? _constructorArguments;
private ICilType? _type;
private object?[]? _constructorArgumentValues;
public ICilType Type =>
ClrType.GetOrCreate(clrAttribute.AttributeType);
_type ??= ClrType.GetOrCreate(
clrAttribute.AttributeType);
public IList<object?> ConstructorArguments
public IList<object?> ConstructorArgumentValues
{
get
{
if (_constructorArguments is null)
if (_constructorArgumentValues is null)
{
IList<CustomAttributeTypedArgument> args = clrAttribute.ConstructorArguments;
if (args.Count < 1)
_constructorArgumentValues = args.Count < 1
? Array.Empty<object>()
: new object[args.Count];
for (int i = 0; i < args.Count; i++)
{
_constructorArguments = Array.Empty<object>();
}
else
{
_constructorArguments = args.Select(static arg => arg.Value).ToList();
_constructorArgumentValues[i] = args[i].Value;
}
}
return _constructorArguments;
return _constructorArgumentValues;
}
}
}

View File

@ -4,6 +4,7 @@
// 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;
@ -11,6 +12,8 @@ namespace LibProtodec.Models.Cil.Clr;
public abstract class ClrMember(MemberInfo clrMember)
{
private ICilAttribute[]? _customAttributes;
public string Name =>
clrMember.Name;
@ -23,11 +26,25 @@ public abstract class ClrMember(MemberInfo clrMember)
: ClrType.GetOrCreate(
clrMember.DeclaringType);
public IEnumerable<ICilAttribute> GetCustomAttributes()
public IList<ICilAttribute> CustomAttributes
{
foreach (CustomAttributeData attribute in clrMember.GetCustomAttributesData())
get
{
yield return new ClrAttribute(attribute);
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

@ -11,12 +11,12 @@ 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 IsNonPublic =>
(clrMethod.Attributes & MethodAttributes.Public) == 0;
public bool IsStatic =>
clrMethod.IsStatic;
@ -24,7 +24,8 @@ public sealed class ClrMethod(MethodInfo clrMethod) : ClrMember(clrMethod), ICil
clrMethod.IsVirtual;
public ICilType ReturnType =>
ClrType.GetOrCreate(clrMethod.ReturnType);
ClrType.GetOrCreate(
clrMethod.ReturnType);
public IEnumerable<ICilType> GetParameterTypes()
{

View File

@ -10,23 +10,29 @@ 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);
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

@ -7,7 +7,6 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using CommunityToolkit.Diagnostics;
@ -18,7 +17,7 @@ public sealed class ClrType : ClrMember, ICilType
private const BindingFlags Everything = BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance;
private readonly Type _clrType;
private IList<ICilType>? _genericTypeArguments;
private ICilType[]? _genericTypeArguments;
private ClrType(Type clrType) : base(clrType) =>
_clrType = clrType;
@ -30,14 +29,13 @@ public sealed class ClrType : ClrMember, ICilType
_clrType.Namespace;
public string DeclaringAssemblyName =>
_clrType.Assembly.FullName
?? ThrowHelper.ThrowArgumentNullException<string>(
nameof(_clrType.Assembly.FullName));
_clrType.Assembly.FullName!;
public ICilType? BaseType =>
_clrType.BaseType is null
? null
: GetOrCreate(_clrType.BaseType);
: GetOrCreate(
_clrType.BaseType);
public bool IsAbstract =>
_clrType.IsAbstract;
@ -62,13 +60,13 @@ public sealed class ClrType : ClrMember, ICilType
{
Type[] args = _clrType.GenericTypeArguments;
if (args.Length < 1)
_genericTypeArguments = args.Length < 1
? Array.Empty<ICilType>()
: new ICilType[args.Length];
for (int i = 0; i < args.Length; i++)
{
_genericTypeArguments = Array.Empty<ICilType>();
}
else
{
_genericTypeArguments = args.Select(GetOrCreate).ToList();
_genericTypeArguments[i] = GetOrCreate(args[i]);
}
}

View File

@ -12,5 +12,5 @@ public interface ICilAttribute
{
ICilType Type { get; }
IList<object?> ConstructorArguments { get; }
IList<object?> ConstructorArgumentValues { get; }
}

View File

@ -14,9 +14,9 @@ public interface ICilField
object? ConstantValue { get; }
IList<ICilAttribute> CustomAttributes { get; }
bool IsLiteral { get; }
bool IsPublic { get; }
bool IsStatic { get; }
IEnumerable<ICilAttribute> GetCustomAttributes();
}

View File

@ -12,14 +12,15 @@ public interface ICilMethod
{
string Name { get; }
bool IsInherited { get; }
bool IsPublic { get; }
bool IsStatic { get; }
bool IsVirtual { get; }
bool IsConstructor { get; }
bool IsInherited { get; }
bool IsPublic { get; }
bool IsStatic { get; }
bool IsVirtual { get; }
ICilType ReturnType { get; }
IEnumerable<ICilAttribute> GetCustomAttributes();
IList<ICilAttribute> CustomAttributes { get; }
IEnumerable<ICilType> GetParameterTypes();
}

View File

@ -20,5 +20,5 @@ public interface ICilProperty
ICilMethod? Getter { get; }
ICilMethod? Setter { get; }
IEnumerable<ICilAttribute> GetCustomAttributes();
IList<ICilAttribute> CustomAttributes { get; }
}

View File

@ -26,7 +26,7 @@ public interface ICilType
IList<ICilType> GenericTypeArguments { get; }
IEnumerable<ICilAttribute> GetCustomAttributes();
IList<ICilAttribute> CustomAttributes { get; }
IEnumerable<ICilField> GetFields();

View File

@ -0,0 +1,22 @@
// 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 CommunityToolkit.Diagnostics;
using LibCpp2IL.Metadata;
namespace LibProtodec.Models.Cil.Il2Cpp;
public sealed class Il2CppAttribute(Il2CppTypeDefinition il2CppAttrType, object?[]? ctorArgValues) : ICilAttribute
{
public ICilType Type =>
Il2CppType.GetOrCreate(il2CppAttrType);
public IList<object?> ConstructorArgumentValues =>
ctorArgValues
?? ThrowHelper.ThrowNotSupportedException<IList<object?>>(
"Attribute constructor argument parsing is only available on metadata version 29 or greater.");
}

View File

@ -0,0 +1,40 @@
// 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;
using LibCpp2IL.Metadata;
namespace LibProtodec.Models.Cil.Il2Cpp;
public sealed class Il2CppField(Il2CppFieldDefinition il2CppField) : Il2CppMember, ICilField
{
private readonly FieldAttributes _attributes =
(FieldAttributes)il2CppField.RawFieldType!.Attrs;
public string Name =>
il2CppField.Name!;
public object? ConstantValue =>
il2CppField.DefaultValue!.Value;
public bool IsLiteral =>
(_attributes & FieldAttributes.Literal) != 0;
public bool IsPublic =>
(_attributes & FieldAttributes.Public) != 0;
public bool IsStatic =>
(_attributes & FieldAttributes.Static) != 0;
protected override Il2CppImageDefinition DeclaringAssembly =>
il2CppField.FieldType!.baseType!.DeclaringAssembly!;
protected override int CustomAttributeIndex =>
il2CppField.customAttributeIndex;
protected override uint Token =>
il2CppField.token;
}

View File

@ -0,0 +1,306 @@
// 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.Text;
using SystemEx.Memory;
using CommunityToolkit.Diagnostics;
using LibCpp2IL;
using LibCpp2IL.BinaryStructures;
using LibCpp2IL.Metadata;
namespace LibProtodec.Models.Cil.Il2Cpp;
public abstract class Il2CppMember
{
private ICilAttribute[]? _customAttributes;
public IList<ICilAttribute> CustomAttributes
{
get
{
if (_customAttributes is null)
{
if (LibCpp2IlMain.MetadataVersion < 29f)
{
int attrTypeRngIdx = LibCpp2IlMain.MetadataVersion <= 24f
? CustomAttributeIndex
: BinarySearchToken(
LibCpp2IlMain.TheMetadata!.attributeTypeRanges,
Token,
DeclaringAssembly.customAttributeStart,
(int)DeclaringAssembly.customAttributeCount);
if (attrTypeRngIdx < 0)
return _customAttributes = Array.Empty<ICilAttribute>();
Il2CppCustomAttributeTypeRange attrTypeRng = LibCpp2IlMain.TheMetadata!.attributeTypeRanges[attrTypeRngIdx];
_customAttributes = new ICilAttribute[attrTypeRng.count];
for (int attrTypeIdx = 0; attrTypeIdx < attrTypeRng.count; attrTypeIdx++)
{
int typeIndex = LibCpp2IlMain.TheMetadata.attributeTypes[attrTypeRng.start + attrTypeIdx];
var type = LibCpp2IlMain.Binary!.GetType(typeIndex);
var typeDef = LibCpp2IlMain.TheMetadata.typeDefs[type.Data.ClassIndex];
_customAttributes[attrTypeIdx] = new Il2CppAttribute(typeDef, null);
}
}
else
{
int attrDataRngIdx = BinarySearchToken(
LibCpp2IlMain.TheMetadata!.AttributeDataRanges,
Token,
DeclaringAssembly.customAttributeStart,
(int)DeclaringAssembly.customAttributeCount);
if (attrDataRngIdx < 0)
return _customAttributes = Array.Empty<ICilAttribute>();
Il2CppCustomAttributeDataRange attrDataRange = LibCpp2IlMain.TheMetadata.AttributeDataRanges[attrDataRngIdx];
Il2CppCustomAttributeDataRange attrDataRngNext = LibCpp2IlMain.TheMetadata.AttributeDataRanges[attrDataRngIdx + 1];
long attrDataStart = LibCpp2IlMain.TheMetadata.metadataHeader.attributeDataOffset + attrDataRange.startOffset;
long attrDataEnd = LibCpp2IlMain.TheMetadata.metadataHeader.attributeDataOffset + attrDataRngNext.startOffset;
byte[] attrData = LibCpp2IlMain.TheMetadata.ReadByteArrayAtRawAddress(attrDataStart, (int)(attrDataEnd - attrDataStart));
MemoryReader reader = new(attrData);
int attributeCount = (int)ReadUnityCompressedUInt32(ref reader);
Span<uint> ctorIndices = stackalloc uint[attributeCount];
for (int i = 0; i < attributeCount; i++)
ctorIndices[i] = reader.ReadUInt32LittleEndian();
_customAttributes = new ICilAttribute[attributeCount];
for (int i = 0; i < attributeCount; i++)
{
uint ctorArgCount = ReadUnityCompressedUInt32(ref reader);
uint fieldCount = ReadUnityCompressedUInt32(ref reader);
uint propCount = ReadUnityCompressedUInt32(ref reader);
object?[] ctorArgValues = ctorArgCount > 0
? new object[ctorArgCount]
: Array.Empty<object?>();
for (int j = 0; j < ctorArgCount; j++)
{
ctorArgValues[j] = ReadValue(ref reader);
}
for (uint j = 0; j < fieldCount; j++)
{
ReadValue(ref reader);
ResolveMember(ref reader);
}
for (uint j = 0; j < propCount; j++)
{
ReadValue(ref reader);
ResolveMember(ref reader);
}
Il2CppMethodDefinition attrCtor = LibCpp2IlMain.TheMetadata.methodDefs[ctorIndices[i]];
Il2CppTypeDefinition attrType = LibCpp2IlMain.TheMetadata.typeDefs[attrCtor.declaringTypeIdx];
_customAttributes[i] = new Il2CppAttribute(attrType, ctorArgValues);
}
}
}
return _customAttributes;
}
}
protected abstract Il2CppImageDefinition DeclaringAssembly { get; }
protected abstract int CustomAttributeIndex { get; }
protected abstract uint Token { get; }
private static int BinarySearchToken(IReadOnlyList<IIl2CppTokenProvider> source, uint token, int start, int count)
{
int lo = start;
int hi = start + count - 1;
while (lo <= hi)
{
int i = lo + ((hi - lo) >> 1);
switch (source[i].Token.CompareTo(token))
{
case 0:
return i;
case < 0:
lo = i + 1;
break;
default:
hi = i - 1;
break;
}
}
return ~lo;
}
private static uint ReadUnityCompressedUInt32(ref MemoryReader reader)
{
byte byt = reader.ReadByte();
switch (byt)
{
case < 128:
return byt;
case 240:
return reader.ReadUInt32LittleEndian();
case 254:
return uint.MaxValue - 1;
case byte.MaxValue:
return uint.MaxValue;
}
if ((byt & 192) == 192)
{
return (byt & ~192U) << 24
| ((uint)reader.ReadByte() << 16)
| ((uint)reader.ReadByte() << 8)
| reader.ReadByte();
}
if ((byt & 128) == 128)
{
return (byt & ~128U) << 8
| reader.ReadByte();
}
return ThrowHelper.ThrowInvalidDataException<uint>();
}
private static int ReadUnityCompressedInt32(ref MemoryReader reader)
{
uint unsigned = ReadUnityCompressedUInt32(ref reader);
if (unsigned == uint.MaxValue)
return int.MinValue;
bool isNegative = (unsigned & 1) == 1;
unsigned >>= 1;
return isNegative
? -(int)(unsigned + 1)
: (int)unsigned;
}
private static object? ReadValue(ref MemoryReader reader)
{
Il2CppTypeEnum type = (Il2CppTypeEnum)reader.ReadByte();
return ReadValue(ref reader, type);
}
private static object? ReadValue(ref MemoryReader reader, Il2CppTypeEnum type)
{
switch (type)
{
case Il2CppTypeEnum.IL2CPP_TYPE_ENUM:
Il2CppTypeEnum underlyingType = ReadEnumUnderlyingType(ref reader);
return ReadValue(ref reader, underlyingType);
case Il2CppTypeEnum.IL2CPP_TYPE_SZARRAY:
return ReadSzArray(ref reader);
case Il2CppTypeEnum.IL2CPP_TYPE_IL2CPP_TYPE_INDEX:
int typeIndex = ReadUnityCompressedInt32(ref reader);
return LibCpp2IlMain.Binary!.GetType(typeIndex);
case Il2CppTypeEnum.IL2CPP_TYPE_BOOLEAN:
return reader.ReadBoolean();
case Il2CppTypeEnum.IL2CPP_TYPE_CHAR:
return (char)reader.ReadInt16LittleEndian();
case Il2CppTypeEnum.IL2CPP_TYPE_I1:
return reader.ReadSByte();
case Il2CppTypeEnum.IL2CPP_TYPE_U1:
return reader.ReadByte();
case Il2CppTypeEnum.IL2CPP_TYPE_I2:
return reader.ReadInt16LittleEndian();
case Il2CppTypeEnum.IL2CPP_TYPE_U2:
return reader.ReadUInt16LittleEndian();
case Il2CppTypeEnum.IL2CPP_TYPE_I4:
return ReadUnityCompressedInt32(ref reader);
case Il2CppTypeEnum.IL2CPP_TYPE_U4:
return ReadUnityCompressedUInt32(ref reader);
case Il2CppTypeEnum.IL2CPP_TYPE_I8:
return reader.ReadInt64LittleEndian();
case Il2CppTypeEnum.IL2CPP_TYPE_U8:
return reader.ReadUInt64LittleEndian();
case Il2CppTypeEnum.IL2CPP_TYPE_R4:
return reader.ReadSingleLittleEndian();
case Il2CppTypeEnum.IL2CPP_TYPE_R8:
return reader.ReadDoubleLittleEndian();
case Il2CppTypeEnum.IL2CPP_TYPE_STRING:
return ReadString(ref reader);
case Il2CppTypeEnum.IL2CPP_TYPE_CLASS:
case Il2CppTypeEnum.IL2CPP_TYPE_OBJECT:
case Il2CppTypeEnum.IL2CPP_TYPE_GENERICINST:
default:
return ThrowHelper.ThrowNotSupportedException<object>();
}
}
private static Il2CppTypeEnum ReadEnumUnderlyingType(ref MemoryReader reader)
{
int typeIdx = ReadUnityCompressedInt32(ref reader);
var enumType = LibCpp2IlMain.Binary!.GetType(typeIdx);
var underlyingType = LibCpp2IlMain.Binary.GetType(
enumType.AsClass().ElementTypeIndex);
return underlyingType.Type;
}
private static object?[]? ReadSzArray(ref MemoryReader reader)
{
int arrayLength = ReadUnityCompressedInt32(ref reader);
if (arrayLength == -1)
return null;
Il2CppTypeEnum arrayType = (Il2CppTypeEnum)reader.ReadByte();
if (arrayType == Il2CppTypeEnum.IL2CPP_TYPE_ENUM)
arrayType = ReadEnumUnderlyingType(ref reader);
bool typePrefixed = reader.ReadBoolean();
if (typePrefixed && arrayType != Il2CppTypeEnum.IL2CPP_TYPE_OBJECT)
ThrowHelper.ThrowInvalidDataException("Array elements are type-prefixed, but the array type is not object");
object?[] array = new object?[arrayLength];
for (int i = 0; i < arrayLength; i++)
{
Il2CppTypeEnum elementType = typePrefixed
? (Il2CppTypeEnum)reader.ReadByte()
: arrayType;
array[i] = ReadValue(ref reader, elementType);
}
return array;
}
private static string? ReadString(ref MemoryReader reader)
{
int length = ReadUnityCompressedInt32(ref reader);
if (length == -1)
return null;
ReadOnlySpan<byte> bytes = reader.ReadBytes(length);
return Encoding.UTF8.GetString(bytes);
}
private static void ResolveMember(ref MemoryReader reader)
{
// We don't care about attribute properties or fields,
// so we just read enough to exhaust the stream
int memberIndex = ReadUnityCompressedInt32(ref reader);
if (memberIndex < 0)
{
uint typeIndex = ReadUnityCompressedUInt32(ref reader);
memberIndex = -(memberIndex + 1);
}
}
}

View File

@ -0,0 +1,57 @@
// 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 LibCpp2IL;
using LibCpp2IL.Metadata;
using LibCpp2IL.Reflection;
namespace LibProtodec.Models.Cil.Il2Cpp;
public sealed class Il2CppMethod(Il2CppMethodDefinition il2CppMethod) : Il2CppMember, ICilMethod
{
public string Name =>
il2CppMethod.Name!;
public bool IsInherited =>
false;
public bool IsConstructor =>
false;
public bool IsPublic =>
(il2CppMethod.Attributes & MethodAttributes.Public) != 0;
public bool IsStatic =>
(il2CppMethod.Attributes & MethodAttributes.Static) != 0;
public bool IsVirtual =>
(il2CppMethod.Attributes & MethodAttributes.Virtual) != 0;
public ICilType ReturnType =>
Il2CppType.GetOrCreate(
LibCpp2ILUtils.GetTypeReflectionData(
LibCpp2IlMain.Binary!.GetType(
il2CppMethod.returnTypeIdx)));
public IEnumerable<ICilType> GetParameterTypes()
{
foreach (Il2CppParameterReflectionData parameter in il2CppMethod.Parameters!)
{
yield return Il2CppType.GetOrCreate(parameter.Type);
}
}
protected override Il2CppImageDefinition DeclaringAssembly =>
il2CppMethod.DeclaringType!.DeclaringAssembly!;
protected override int CustomAttributeIndex =>
il2CppMethod.customAttributeIndex;
protected override uint Token =>
il2CppMethod.token;
}

View File

@ -0,0 +1,54 @@
// 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.Linq;
using LibCpp2IL;
using LibCpp2IL.Metadata;
namespace LibProtodec.Models.Cil.Il2Cpp;
// We take declaring type as a ctor arg because of the nonsensically inefficient way libcpp2il calculates Il2CppPropertyDefinition's DeclaringType property
public sealed class Il2CppProperty(Il2CppPropertyDefinition il2CppProperty, Il2CppTypeDefinition declaringType) : Il2CppMember, ICilProperty
{
public string Name =>
il2CppProperty.Name!;
public bool IsInherited =>
false;
public bool CanRead =>
il2CppProperty.get >= 0;
public bool CanWrite =>
il2CppProperty.set >= 0;
public ICilMethod? Getter =>
CanRead
? new Il2CppMethod(
LibCpp2IlMain.TheMetadata!.methodDefs[
declaringType.FirstMethodIdx + il2CppProperty.get])
: null;
public ICilMethod? Setter =>
CanWrite
? new Il2CppMethod(
LibCpp2IlMain.TheMetadata!.methodDefs[
declaringType.FirstMethodIdx + il2CppProperty.set])
: null;
public ICilType Type =>
Getter?.ReturnType
?? Setter!.GetParameterTypes().First();
protected override Il2CppImageDefinition DeclaringAssembly =>
declaringType.DeclaringAssembly!;
protected override int CustomAttributeIndex =>
il2CppProperty.customAttributeIndex;
protected override uint Token =>
il2CppProperty.token;
}

View File

@ -0,0 +1,197 @@
// 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;
using LibCpp2IL;
using LibCpp2IL.Metadata;
using LibCpp2IL.Reflection;
namespace LibProtodec.Models.Cil.Il2Cpp;
public sealed class Il2CppType : Il2CppMember, ICilType
{
private readonly Il2CppTypeDefinition _il2CppType;
private readonly Il2CppTypeReflectionData[] _genericArgs;
private ICilType[]? _genericTypeArguments;
private Il2CppType(Il2CppTypeDefinition il2CppType, Il2CppTypeReflectionData[] genericArgs) =>
(_il2CppType, _genericArgs) = (il2CppType, genericArgs);
public string Name =>
_il2CppType.Name!;
public string FullName =>
_il2CppType.FullName!;
public string? Namespace =>
_il2CppType.Namespace;
public string DeclaringAssemblyName =>
LibCpp2IlMain.TheMetadata!.GetStringFromIndex(
DeclaringAssembly.nameIndex);
public ICilType? DeclaringType =>
IsNested
? GetOrCreate(
LibCpp2ILUtils.GetTypeReflectionData(
LibCpp2IlMain.Binary!.GetType(
_il2CppType.DeclaringTypeIndex)))
: null;
public ICilType? BaseType =>
_il2CppType.ParentIndex == -1
? null
: GetOrCreate(
LibCpp2ILUtils.GetTypeReflectionData(
LibCpp2IlMain.Binary!.GetType(
_il2CppType.ParentIndex)));
public bool IsAbstract =>
_il2CppType.IsAbstract;
public bool IsClass =>
(_il2CppType.Attributes & TypeAttributes.ClassSemanticsMask) == TypeAttributes.Class
&& !_il2CppType.IsValueType;
public bool IsEnum =>
_il2CppType.IsEnumType;
public bool IsNested =>
_il2CppType.DeclaringTypeIndex >= 0;
public bool IsSealed =>
(_il2CppType.Attributes & TypeAttributes.Sealed) != 0;
public IList<ICilType> GenericTypeArguments
{
get
{
if (_genericTypeArguments is null)
{
_genericTypeArguments = _genericArgs.Length < 1
? Array.Empty<ICilType>()
: new ICilType[_genericArgs.Length];
for (int i = 0; i < _genericArgs.Length; i++)
{
_genericTypeArguments[i] = GetOrCreate(_genericArgs[i]);
}
}
return _genericTypeArguments;
}
}
public IEnumerable<ICilField> GetFields()
{
for (int i = 0; i < _il2CppType.FieldCount; i++)
{
yield return new Il2CppField(
LibCpp2IlMain.TheMetadata!.fieldDefs[
_il2CppType.FirstFieldIdx + i]);
}
}
public IEnumerable<ICilMethod> GetMethods()
{
for (int i = 0; i < _il2CppType.MethodCount; i++)
{
yield return new Il2CppMethod(
LibCpp2IlMain.TheMetadata!.methodDefs[
_il2CppType.FirstMethodIdx + i]);
}
}
public IEnumerable<ICilType> GetNestedTypes()
{
for (int i = 0; i < _il2CppType.NestedTypeCount; i++)
{
yield return GetOrCreate(
LibCpp2IlMain.TheMetadata!.typeDefs[
LibCpp2IlMain.TheMetadata.nestedTypeIndices[
_il2CppType.NestedTypesStart + i]]);
}
}
public IEnumerable<ICilProperty> GetProperties()
{
for (int i = 0; i < _il2CppType.PropertyCount; i++)
{
yield return new Il2CppProperty(
LibCpp2IlMain.TheMetadata!.propertyDefs[
_il2CppType.FirstPropertyId + i],
_il2CppType);
}
}
public bool IsAssignableTo(ICilType type)
{
if (type is Il2CppType il2CppType)
{
return IsAssignableTo(_il2CppType, il2CppType._il2CppType);
}
return ThrowHelper.ThrowNotSupportedException<bool>();
}
protected override Il2CppImageDefinition DeclaringAssembly =>
_il2CppType.DeclaringAssembly!;
protected override int CustomAttributeIndex =>
_il2CppType.CustomAttributeIndex;
protected override uint Token =>
_il2CppType.Token;
private static readonly ConcurrentDictionary<string, Il2CppType> TypeLookup = [];
public static ICilType GetOrCreate(Il2CppTypeDefinition il2CppType) =>
TypeLookup.GetOrAdd(
il2CppType.FullName!,
static (_, il2CppType) =>
new Il2CppType(il2CppType, Array.Empty<Il2CppTypeReflectionData>()),
il2CppType);
public static ICilType GetOrCreate(Il2CppTypeReflectionData il2CppTypeData)
{
Guard.IsTrue(il2CppTypeData.isType);
return TypeLookup.GetOrAdd(
il2CppTypeData.ToString(),
static (_, il2CppTypeData) =>
new Il2CppType(il2CppTypeData.baseType!, il2CppTypeData.genericParams),
il2CppTypeData);
}
private static bool IsAssignableTo(Il2CppTypeDefinition thisType, Il2CppTypeDefinition baseType)
{
if (baseType.IsInterface)
{
foreach (Il2CppTypeReflectionData @interface in thisType.Interfaces!)
{
if (@interface.baseType == baseType)
{
return true;
}
}
}
if (thisType == baseType)
{
return true;
}
Il2CppTypeDefinition? thisTypeBaseType = thisType.BaseType?.baseType;
return thisTypeBaseType is not null
&& IsAssignableTo(thisTypeBaseType, baseType);
}
}

View File

@ -10,7 +10,7 @@ using LibProtodec.Models.Protobuf.Types;
namespace LibProtodec.Models.Protobuf.Fields;
public sealed class MessageField
public sealed class MessageField(Message declaringMessage)
{
public required IProtobufType Type { get; init; }
public required string Name { get; init; }
@ -19,14 +19,15 @@ public sealed class MessageField
public bool IsObsolete { get; init; }
public bool HasHasProp { get; init; }
public void WriteTo(TextWriter writer, TopLevel topLevel, bool isOneOf)
public void WriteTo(TextWriter writer, bool isOneOf)
{
if (HasHasProp && !isOneOf && Type is not Repeated)
{
writer.Write("optional ");
}
Protobuf.WriteTypeNameTo(writer, Type, topLevel);
writer.Write(
declaringMessage.QualifyTypeName(Type));
writer.Write(' ');
writer.Write(Name);
writer.Write(" = ");

View File

@ -10,7 +10,7 @@ using LibProtodec.Models.Protobuf.Types;
namespace LibProtodec.Models.Protobuf.Fields;
public sealed class ServiceMethod
public sealed class ServiceMethod(Service declaringService)
{
public required string Name { get; init; }
public required IProtobufType RequestType { get; init; }
@ -20,7 +20,7 @@ public sealed class ServiceMethod
public bool IsResponseStreamed { get; init; }
public bool IsObsolete { get; init; }
public void WriteTo(IndentedTextWriter writer, TopLevel topLevel)
public void WriteTo(IndentedTextWriter writer)
{
writer.Write("rpc ");
writer.Write(Name);
@ -31,7 +31,8 @@ public sealed class ServiceMethod
writer.Write("stream ");
}
Protobuf.WriteTypeNameTo(writer, RequestType, topLevel);
writer.Write(
declaringService.QualifyTypeName(RequestType));
writer.Write(") returns (");
if (IsResponseStreamed)
@ -39,7 +40,8 @@ public sealed class ServiceMethod
writer.Write("stream ");
}
Protobuf.WriteTypeNameTo(writer, ResponseType, topLevel);
writer.Write(
declaringService.QualifyTypeName(ResponseType));
writer.Write(')');
if (IsObsolete)

View File

@ -9,15 +9,13 @@ using System.Collections.Generic;
using System.IO;
using System.Linq;
using LibProtodec.Models.Protobuf.TopLevels;
using LibProtodec.Models.Protobuf.Types;
namespace LibProtodec.Models.Protobuf;
public sealed class Protobuf
{
private string? _fileName;
private HashSet<string>? _imports;
public HashSet<string> Imports =>
_imports ??= [];
public readonly List<TopLevel> TopLevels = [];
@ -26,7 +24,10 @@ public sealed class Protobuf
public string? Namespace { get; init; }
public string FileName =>
$"{TopLevels.FirstOrDefault()?.Name}.proto";
_fileName ??= $"{string.Join('_', TopLevels.Select(static topLevel => topLevel.Name))}.proto";
public HashSet<string> Imports =>
_imports ??= [];
public void WriteTo(IndentedTextWriter writer)
{
@ -89,17 +90,4 @@ public sealed class Protobuf
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.Indent++;
if (ContainsDuplicateField)
if (ContainsDuplicateFieldId)
{
Protobuf.WriteOptionTo(writer, "allow_alias", "true");
}
@ -49,7 +49,7 @@ public sealed class Enum : TopLevel, INestableType
public bool IsClosed { get; set; }
private bool ContainsDuplicateField
private bool ContainsDuplicateFieldId
{
get
{

View File

@ -37,7 +37,7 @@ public sealed class Message : TopLevel, INestableType
if (oneOfs.Contains(field.Id))
continue;
field.WriteTo(writer, this, isOneOf: false);
field.WriteTo(writer, isOneOf: false);
}
foreach ((string name, List<int> fieldIds) in OneOfs)
@ -50,7 +50,7 @@ public sealed class Message : TopLevel, INestableType
foreach (int fieldId in fieldIds)
{
Fields[fieldId].WriteTo(writer, this, isOneOf: true);
Fields[fieldId].WriteTo(writer, isOneOf: true);
}
writer.Indent--;

View File

@ -28,7 +28,7 @@ public sealed class Service : TopLevel
foreach (ServiceMethod method in Methods)
{
method.WriteTo(writer, this);
method.WriteTo(writer);
}
writer.Indent--;

View File

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

View File

@ -10,9 +10,3 @@ public interface IProtobufType
{
string Name { get; }
}
public sealed class External(string typeName) : IProtobufType
{
public string Name =>
typeName;
}

View File

@ -7,21 +7,24 @@
namespace LibProtodec.Models.Protobuf.Types;
// ReSharper disable StringLiteralTypo
public static class Scalar
public sealed class Scalar(string typeName) : IProtobufType
{
public static readonly IProtobufType Bool = new External("bool");
public static readonly IProtobufType Bytes = new External("bytes");
public static readonly IProtobufType Double = new External("double");
public static readonly IProtobufType Fixed32 = new External("fixed32");
public static readonly IProtobufType Fixed64 = new External("fixed64");
public static readonly IProtobufType Float = new External("float");
public static readonly IProtobufType Int32 = new External("int32");
public static readonly IProtobufType Int64 = new External("int64");
public static readonly IProtobufType SFixed32 = new External("sfixed32");
public static readonly IProtobufType SFixed64 = new External("sfixed64");
public static readonly IProtobufType SInt32 = new External("sint32");
public static readonly IProtobufType SInt64 = new External("sint64");
public static readonly IProtobufType String = new External("string");
public static readonly IProtobufType UInt32 = new External("uint32");
public static readonly IProtobufType UInt64 = new External("uint64");
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

@ -6,34 +6,40 @@
namespace LibProtodec.Models.Protobuf.Types;
public static class WellKnown
public sealed class WellKnown(string typeName, string fileName) : IProtobufType
{
public static readonly IProtobufType Any = new External("google.protobuf.Any");
public static readonly IProtobufType Api = new External("google.protobuf.Api");
public static readonly IProtobufType BoolValue = new External("google.protobuf.BoolValue");
public static readonly IProtobufType BytesValue = new External("google.protobuf.BytesValue");
public static readonly IProtobufType DoubleValue = new External("google.protobuf.DoubleValue");
public static readonly IProtobufType Duration = new External("google.protobuf.Duration");
public static readonly IProtobufType Empty = new External("google.protobuf.Empty");
public static readonly IProtobufType Enum = new External("google.protobuf.Enum");
public static readonly IProtobufType EnumValue = new External("google.protobuf.EnumValue");
public static readonly IProtobufType Field = new External("google.protobuf.Field");
public static readonly IProtobufType FieldMask = new External("google.protobuf.FieldMask");
public static readonly IProtobufType FloatValue = new External("google.protobuf.FloatValue");
public static readonly IProtobufType Int32Value = new External("google.protobuf.Int32Value");
public static readonly IProtobufType Int64Value = new External("google.protobuf.Int64Value");
public static readonly IProtobufType ListValue = new External("google.protobuf.ListValue");
public static readonly IProtobufType Method = new External("google.protobuf.Method");
public static readonly IProtobufType Mixin = new External("google.protobuf.Mixin");
public static readonly IProtobufType NullValue = new External("google.protobuf.NullValue");
public static readonly IProtobufType Option = new External("google.protobuf.Option");
public static readonly IProtobufType SourceContext = new External("google.protobuf.SourceContext");
public static readonly IProtobufType StringValue = new External("google.protobuf.StringValue");
public static readonly IProtobufType Struct = new External("google.protobuf.Struct");
public static readonly IProtobufType Syntax = new External("google.protobuf.Syntax");
public static readonly IProtobufType Timestamp = new External("google.protobuf.Timestamp");
public static readonly IProtobufType Type = new External("google.protobuf.Type");
public static readonly IProtobufType UInt32Value = new External("google.protobuf.UInt32Value");
public static readonly IProtobufType UInt64Value = new External("google.protobuf.UInt64Value");
public static readonly IProtobufType Value = new External("google.protobuf.Value");
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

@ -12,27 +12,28 @@ using System.Diagnostics.CodeAnalysis;
using System.Linq;
using SystemEx;
using CommunityToolkit.Diagnostics;
using LibCpp2IL;
using LibProtodec.Models.Cil;
using LibProtodec.Models.Protobuf;
using LibProtodec.Models.Protobuf.Fields;
using LibProtodec.Models.Protobuf.TopLevels;
using LibProtodec.Models.Protobuf.Types;
using Microsoft.Extensions.Logging;
namespace LibProtodec;
public delegate bool TypeLookupFunc(ICilType cilType, [NotNullWhen(true)] out IProtobufType? protobufType, out string? import);
public delegate bool NameLookupFunc(string name, [MaybeNullWhen(false)] out string translatedName);
public sealed class ProtodecContext
// ReSharper disable ClassWithVirtualMembersNeverInherited.Global, MemberCanBePrivate.Global, MemberCanBeProtected.Global
public class ProtodecContext
{
private readonly Dictionary<string, TopLevel> _parsed = [];
public readonly List<Protobuf> Protobufs = [];
public NameLookupFunc? NameLookup { get; set; }
public ILogger? Logger { get; set; }
public TypeLookupFunc TypeLookup { get; set; } =
LookupScalarAndWellKnownTypes;
public NameLookupFunc? NameLookup { get; set; }
public void WriteAllTo(IndentedTextWriter writer)
{
@ -49,19 +50,21 @@ public sealed class ProtodecContext
}
}
public Message ParseMessage(ICilType messageClass, ParserOptions options = ParserOptions.None)
public virtual Message ParseMessage(ICilType messageClass, ParserOptions options = ParserOptions.None)
{
Guard.IsTrue(messageClass is { IsClass: true, IsSealed: true });
using IDisposable? _ = Logger?.BeginScopeParsingMessage(messageClass.FullName);
if (_parsed.TryGetValue(messageClass.FullName, out TopLevel? parsedMessage))
{
Logger?.LogParsedMessage(parsedMessage.Name);
return (Message)parsedMessage;
}
Message message = new()
{
Name = TranslateTypeName(messageClass),
IsObsolete = HasObsoleteAttribute(messageClass.GetCustomAttributes())
IsObsolete = HasObsoleteAttribute(messageClass.CustomAttributes)
};
_parsed.Add(messageClass.FullName, message);
@ -77,20 +80,23 @@ public sealed class ProtodecContext
for (int pi = 0, fi = 0; pi < properties.Count; pi++)
{
ICilProperty property = properties[pi];
List<ICilAttribute> attributes = property.GetCustomAttributes().ToList();
ICilProperty property = properties[pi];
ICilType propertyType = property.Type;
if (((options & ParserOptions.IncludePropertiesWithoutNonUserCodeAttribute) == 0 && !HasNonUserCodeAttribute(attributes)))
using IDisposable? __ = Logger?.BeginScopeParsingProperty(property.Name, propertyType.FullName);
if (((options & ParserOptions.IncludePropertiesWithoutNonUserCodeAttribute) == 0 && !HasNonUserCodeAttribute(property.CustomAttributes)))
{
Logger?.LogSkippingPropertyWithoutNonUserCodeAttribute();
continue;
}
ICilType propertyType = property.Type;
// only OneOf enums are defined nested directly in the message class
if (propertyType.IsEnum && propertyType.DeclaringType?.Name == messageClass.Name)
{
string oneOfName = TranslateOneOfPropName(property.Name);
Logger?.LogParsedOneOfField(oneOfName);
List<int> oneOfProtoFieldIds = propertyType.GetFields()
.Where(static field => field.IsLiteral)
.Select(static field => (int)field.ConstantValue!)
@ -108,51 +114,63 @@ public sealed class ProtodecContext
pi++;
}
MessageField field = new()
if (idFields.Count <= fi)
{
Logger?.LogFailedToLocateIdField();
continue;
}
MessageField field = new(message)
{
Type = ParseFieldType(propertyType, options, protobuf),
Name = TranslateMessageFieldName(property.Name),
Id = (int)idFields[fi].ConstantValue!,
IsObsolete = HasObsoleteAttribute(attributes),
IsObsolete = HasObsoleteAttribute(property.CustomAttributes),
HasHasProp = msgFieldHasHasProp
};
Logger?.LogParsedField(field.Name, field.Id, field.Type.Name);
message.Fields.Add(field.Id, field);
fi++;
}
Logger?.LogParsedMessage(message.Name);
return message;
}
public Enum ParseEnum(ICilType enumEnum, ParserOptions options = ParserOptions.None)
public virtual Enum ParseEnum(ICilType enumEnum, ParserOptions options = ParserOptions.None)
{
Guard.IsTrue(enumEnum.IsEnum);
using IDisposable? _ = Logger?.BeginScopeParsingEnum(enumEnum.FullName);
if (_parsed.TryGetValue(enumEnum.FullName, out TopLevel? parsedEnum))
{
Logger?.LogParsedEnum(parsedEnum.Name);
return (Enum)parsedEnum;
}
Enum @enum = new()
{
Name = TranslateTypeName(enumEnum),
IsObsolete = HasObsoleteAttribute(enumEnum.GetCustomAttributes())
IsObsolete = HasObsoleteAttribute(enumEnum.CustomAttributes)
};
_parsed.Add(enumEnum.FullName, @enum);
Protobuf protobuf = GetProtobuf(enumEnum, @enum, options);
foreach (ICilField field in enumEnum.GetFields().Where(static field => field.IsLiteral))
foreach (ICilField enumField in enumEnum.GetFields().Where(static field => field.IsLiteral))
{
List<ICilAttribute> attributes = field.GetCustomAttributes().ToList();
using IDisposable? __ = Logger?.BeginScopeParsingField(enumField.Name);
@enum.Fields.Add(
new EnumField
{
Id = (int)field.ConstantValue!,
Name = TranslateEnumFieldName(attributes, field.Name, @enum.Name),
IsObsolete = HasObsoleteAttribute(attributes)
});
EnumField field = new()
{
Id = (int)enumField.ConstantValue!,
Name = TranslateEnumFieldName(enumField.CustomAttributes, enumField.Name, @enum.Name),
IsObsolete = HasObsoleteAttribute(enumField.CustomAttributes)
};
Logger?.LogParsedField(field.Name, field.Id);
@enum.Fields.Add(field);
}
if (@enum.Fields.All(static field => field.Id != 0))
@ -161,10 +179,11 @@ public sealed class ProtodecContext
@enum.IsClosed = true;
}
Logger?.LogParsedEnum(@enum.Name);
return @enum;
}
public Service ParseService(ICilType serviceClass, ParserOptions options = ParserOptions.None)
public virtual Service ParseService(ICilType serviceClass, ParserOptions options = ParserOptions.None)
{
Guard.IsTrue(serviceClass.IsClass);
@ -190,31 +209,35 @@ public sealed class ProtodecContext
}
Guard.IsNotNull(isClientClass);
using IDisposable? _ = Logger?.BeginScopeParsingService(serviceClass.DeclaringType!.FullName);
if (_parsed.TryGetValue(serviceClass.DeclaringType!.FullName, out TopLevel? parsedService))
{
Logger?.LogParsedService(parsedService.Name);
return (Service)parsedService;
}
Service service = new()
{
Name = TranslateTypeName(serviceClass.DeclaringType),
IsObsolete = HasObsoleteAttribute(serviceClass.GetCustomAttributes())
IsObsolete = HasObsoleteAttribute(serviceClass.CustomAttributes)
};
_parsed.Add(serviceClass.DeclaringType!.FullName, service);
Protobuf protobuf = NewProtobuf(serviceClass, service);
foreach (ICilMethod method in serviceClass.GetMethods().Where(static method => method is { IsInherited: false, IsPublic: true, IsStatic: false }))
foreach (ICilMethod cilMethod in serviceClass.GetMethods().Where(static method => method is { IsInherited: false, IsPublic: true, IsStatic: false, IsConstructor: false }))
{
List<ICilAttribute> attributes = method.GetCustomAttributes().ToList();
using IDisposable? __ = Logger?.BeginScopeParsingMethod(cilMethod.Name);
if ((options & ParserOptions.IncludeServiceMethodsWithoutGeneratedCodeAttribute) == 0
&& !HasGeneratedCodeAttribute(attributes, "grpc_csharp_plugin"))
&& !HasGeneratedCodeAttribute(cilMethod.CustomAttributes, "grpc_csharp_plugin"))
{
Logger?.LogSkippingMethodWithoutGeneratedCodeAttribute();
continue;
}
ICilType requestType, responseType, returnType = method.ReturnType;
ICilType requestType, responseType, returnType = cilMethod.ReturnType;
bool streamReq, streamRes;
if (isClientClass.Value)
@ -222,12 +245,14 @@ public sealed class ProtodecContext
string returnTypeName = TranslateTypeName(returnType);
if (returnTypeName == "AsyncUnaryCall`1")
{
Logger?.LogSkippingDuplicateMethod();
continue;
}
List<ICilType> parameters = method.GetParameterTypes().ToList();
List<ICilType> parameters = cilMethod.GetParameterTypes().ToList();
if (parameters.Count > 2)
{
Logger?.LogSkippingDuplicateMethod();
continue;
}
@ -255,7 +280,7 @@ public sealed class ProtodecContext
}
else
{
List<ICilType> parameters = method.GetParameterTypes().ToList();
List<ICilType> parameters = cilMethod.GetParameterTypes().ToList();
if (parameters[0].GenericTypeArguments.Count == 1)
{
@ -280,22 +305,25 @@ public sealed class ProtodecContext
}
}
service.Methods.Add(
new ServiceMethod
{
Name = TranslateMethodName(method.Name),
IsObsolete = HasObsoleteAttribute(attributes),
RequestType = ParseFieldType(requestType, options, protobuf),
ResponseType = ParseFieldType(responseType, options, protobuf),
IsRequestStreamed = streamReq,
IsResponseStreamed = streamRes
});
ServiceMethod method = new(service)
{
Name = TranslateMethodName(cilMethod.Name),
IsObsolete = HasObsoleteAttribute(cilMethod.CustomAttributes),
RequestType = ParseFieldType(requestType, options, protobuf),
ResponseType = ParseFieldType(responseType, options, protobuf),
IsRequestStreamed = streamReq,
IsResponseStreamed = streamRes
};
Logger?.LogParsedMethod(method.Name, method.RequestType.Name, method.ResponseType.Name);
service.Methods.Add(method);
}
Logger?.LogParsedService(service.Name);
return service;
}
private IProtobufType ParseFieldType(ICilType type, ParserOptions options, Protobuf referencingProtobuf)
protected IProtobufType ParseFieldType(ICilType type, ParserOptions options, Protobuf referencingProtobuf)
{
switch (type.GenericTypeArguments.Count)
{
@ -308,40 +336,166 @@ public sealed class ProtodecContext
ParseFieldType(type.GenericTypeArguments[1], options, referencingProtobuf));
}
if (TypeLookup(type, out IProtobufType? fieldType, out string? import))
if (!LookupType(type, out IProtobufType? fieldType))
{
if (import is not null)
if (type.IsEnum)
{
referencingProtobuf.Imports.Add(import);
if ((options & ParserOptions.SkipEnums) > 0)
{
return Scalar.Int32;
}
fieldType = ParseEnum(type, options);
}
return fieldType;
}
if (type.IsEnum)
{
if ((options & ParserOptions.SkipEnums) > 0)
else
{
return Scalar.Int32;
fieldType = ParseMessage(type, options);
}
fieldType = ParseEnum(type, options);
}
else
{
fieldType = ParseMessage(type, options);
}
Protobuf protobuf = ((INestableType)fieldType).Protobuf!;
if (referencingProtobuf != protobuf)
switch (fieldType)
{
referencingProtobuf.Imports.Add(protobuf.FileName);
case WellKnown wellKnown:
referencingProtobuf.Imports.Add(
wellKnown.FileName);
break;
case INestableType nestableType:
Protobuf protobuf = nestableType.Protobuf!;
if (referencingProtobuf != protobuf)
referencingProtobuf.Imports.Add(
protobuf.FileName);
break;
}
return fieldType;
}
private Protobuf NewProtobuf(ICilType topLevelType, TopLevel topLevel)
protected virtual bool LookupType(ICilType cilType, [NotNullWhen(true)] out IProtobufType? protobufType)
{
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()
{
@ -356,7 +510,7 @@ public sealed class ProtodecContext
return protobuf;
}
private Protobuf GetProtobuf<T>(ICilType topLevelType, T topLevel, ParserOptions options)
protected Protobuf GetProtobuf<T>(ICilType topLevelType, T topLevel, ParserOptions options)
where T : TopLevel, INestableType
{
Protobuf protobuf;
@ -382,12 +536,12 @@ public sealed class ProtodecContext
return protobuf;
}
private string TranslateMethodName(string methodName) =>
protected string TranslateMethodName(string methodName) =>
NameLookup?.Invoke(methodName, out string? translatedName) == true
? translatedName
: methodName;
private string TranslateOneOfPropName(string oneOfPropName)
protected string TranslateOneOfPropName(string oneOfPropName)
{
if (NameLookup?.Invoke(oneOfPropName, out string? translatedName) != true)
{
@ -402,7 +556,7 @@ public sealed class ProtodecContext
return translatedName!.TrimEnd("Case").ToSnakeCaseLower();
}
private string TranslateMessageFieldName(string fieldName)
protected string TranslateMessageFieldName(string fieldName)
{
if (NameLookup?.Invoke(fieldName, out string? translatedName) != true)
{
@ -417,10 +571,11 @@ public sealed class ProtodecContext
return translatedName!.ToSnakeCaseLower();
}
private string TranslateEnumFieldName(IEnumerable<ICilAttribute> attributes, string fieldName, string enumName)
protected string TranslateEnumFieldName(IEnumerable<ICilAttribute> attributes, string fieldName, string enumName)
{
if (attributes.SingleOrDefault(static attr => attr.Type.Name == "OriginalNameAttribute")
?.ConstructorArguments[0] is string originalName)
if (LibCpp2IlMain.MetadataVersion >= 29f //TODO: do not merge into master until il2cpp-specific global is removed
&& attributes.SingleOrDefault(static attr => attr.Type.Name == "OriginalNameAttribute")
?.ConstructorArgumentValues[0] is string originalName)
{
return originalName;
}
@ -443,7 +598,7 @@ public sealed class ProtodecContext
return enumName + '_' + fieldName;
}
private string TranslateTypeName(ICilType type)
protected string TranslateTypeName(ICilType type)
{
if (NameLookup is null)
return type.Name;
@ -469,177 +624,18 @@ public sealed class ProtodecContext
return translatedName;
}
public static bool LookupScalarAndWellKnownTypes(ICilType cilType, [NotNullWhen(true)] out IProtobufType? protobufType, out string? import)
{
switch (cilType.FullName)
{
case "System.String":
import = null;
protobufType = Scalar.String;
return true;
case "System.Boolean":
import = null;
protobufType = Scalar.Bool;
return true;
case "System.Double":
import = null;
protobufType = Scalar.Double;
return true;
case "System.UInt32":
import = null;
protobufType = Scalar.UInt32;
return true;
case "System.UInt64":
import = null;
protobufType = Scalar.UInt64;
return true;
case "System.Int32":
import = null;
protobufType = Scalar.Int32;
return true;
case "System.Int64":
import = null;
protobufType = Scalar.Int64;
return true;
case "System.Single":
import = null;
protobufType = Scalar.Float;
return true;
case "Google.Protobuf.ByteString":
import = null;
protobufType = Scalar.Bytes;
return true;
case "Google.Protobuf.WellKnownTypes.Any":
import = "google/protobuf/any.proto";
protobufType = WellKnown.Any;
return true;
case "Google.Protobuf.WellKnownTypes.Api":
import = "google/protobuf/api.proto";
protobufType = WellKnown.Api;
return true;
case "Google.Protobuf.WellKnownTypes.BoolValue":
import = "google/protobuf/wrappers.proto";
protobufType = WellKnown.BoolValue;
return true;
case "Google.Protobuf.WellKnownTypes.BytesValue":
import = "google/protobuf/wrappers.proto";
protobufType = WellKnown.BytesValue;
return true;
case "Google.Protobuf.WellKnownTypes.DoubleValue":
import = "google/protobuf/wrappers.proto";
protobufType = WellKnown.DoubleValue;
return true;
case "Google.Protobuf.WellKnownTypes.Duration":
import = "google/protobuf/duration.proto";
protobufType = WellKnown.Duration;
return true;
case "Google.Protobuf.WellKnownTypes.Empty":
import = "google/protobuf/empty.proto";
protobufType = WellKnown.Empty;
return true;
case "Google.Protobuf.WellKnownTypes.Enum":
import = "google/protobuf/type.proto";
protobufType = WellKnown.Enum;
return true;
case "Google.Protobuf.WellKnownTypes.EnumValue":
import = "google/protobuf/type.proto";
protobufType = WellKnown.EnumValue;
return true;
case "Google.Protobuf.WellKnownTypes.Field":
import = "google/protobuf/type.proto";
protobufType = WellKnown.Field;
return true;
case "Google.Protobuf.WellKnownTypes.FieldMask":
import = "google/protobuf/field_mask.proto";
protobufType = WellKnown.FieldMask;
return true;
case "Google.Protobuf.WellKnownTypes.FloatValue":
import = "google/protobuf/wrappers.proto";
protobufType = WellKnown.FloatValue;
return true;
case "Google.Protobuf.WellKnownTypes.Int32Value":
import = "google/protobuf/wrappers.proto";
protobufType = WellKnown.Int32Value;
return true;
case "Google.Protobuf.WellKnownTypes.Int64Value":
import = "google/protobuf/wrappers.proto";
protobufType = WellKnown.Int64Value;
return true;
case "Google.Protobuf.WellKnownTypes.ListValue":
import = "google/protobuf/struct.proto";
protobufType = WellKnown.ListValue;
return true;
case "Google.Protobuf.WellKnownTypes.Method":
import = "google/protobuf/api.proto";
protobufType = WellKnown.Method;
return true;
case "Google.Protobuf.WellKnownTypes.Mixin":
import = "google/protobuf/api.proto";
protobufType = WellKnown.Mixin;
return true;
case "Google.Protobuf.WellKnownTypes.NullValue":
import = "google/protobuf/struct.proto";
protobufType = WellKnown.NullValue;
return true;
case "Google.Protobuf.WellKnownTypes.Option":
import = "google/protobuf/type.proto";
protobufType = WellKnown.Option;
return true;
case "Google.Protobuf.WellKnownTypes.SourceContext":
import = "google/protobuf/source_context.proto";
protobufType = WellKnown.SourceContext;
return true;
case "Google.Protobuf.WellKnownTypes.StringValue":
import = "google/protobuf/wrappers.proto";
protobufType = WellKnown.StringValue;
return true;
case "Google.Protobuf.WellKnownTypes.Struct":
import = "google/protobuf/struct.proto";
protobufType = WellKnown.Struct;
return true;
case "Google.Protobuf.WellKnownTypes.Syntax":
import = "google/protobuf/type.proto";
protobufType = WellKnown.Syntax;
return true;
case "Google.Protobuf.WellKnownTypes.Timestamp":
import = "google/protobuf/timestamp.proto";
protobufType = WellKnown.Timestamp;
return true;
case "Google.Protobuf.WellKnownTypes.Type":
import = "google/protobuf/type.proto";
protobufType = WellKnown.Type;
return true;
case "Google.Protobuf.WellKnownTypes.UInt32Value":
import = "google/protobuf/wrappers.proto";
protobufType = WellKnown.UInt32Value;
return true;
case "Google.Protobuf.WellKnownTypes.UInt64Value":
import = "google/protobuf/wrappers.proto";
protobufType = WellKnown.UInt64Value;
return true;
case "Google.Protobuf.WellKnownTypes.Value":
import = "google/protobuf/struct.proto";
protobufType = WellKnown.Value;
return true;
default:
import = null;
protobufType = null;
return false;
}
}
// ReSharper disable once IdentifierTypo
private static bool IsBeebyted(string name) =>
protected static bool IsBeebyted(string name) =>
name.Length == 11 && name.CountUpper() == 11;
private static bool HasGeneratedCodeAttribute(IEnumerable<ICilAttribute> attributes, string tool) =>
attributes.Any(attr => attr.Type.Name == nameof(GeneratedCodeAttribute)
&& attr.ConstructorArguments[0] as string == tool);
protected static bool HasGeneratedCodeAttribute(IEnumerable<ICilAttribute> attributes, string tool) =>
attributes.Any(attr => attr.Type.Name == nameof(GeneratedCodeAttribute)
&& (LibCpp2IlMain.MetadataVersion < 29f //TODO: do not merge into master until il2cpp-specific global is removed
|| attr.ConstructorArgumentValues[0] as string == tool));
private static bool HasNonUserCodeAttribute(IEnumerable<ICilAttribute> attributes) =>
protected static bool HasNonUserCodeAttribute(IEnumerable<ICilAttribute> attributes) =>
attributes.Any(static attr => attr.Type.Name == nameof(DebuggerNonUserCodeAttribute));
private static bool HasObsoleteAttribute(IEnumerable<ICilAttribute> attributes) =>
protected static bool HasObsoleteAttribute(IEnumerable<ICilAttribute> attributes) =>
attributes.Any(static attr => attr.Type.Name == nameof(ObsoleteAttribute));
}

View File

@ -9,18 +9,24 @@ using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using AssetRipper.Primitives;
using LibCpp2IL;
using LibProtodec;
using LibProtodec.Loaders;
using LibProtodec.Models.Cil;
using LibProtodec.Models.Protobuf;
using Microsoft.Extensions.Logging;
const string indent = " ";
const string help = """
Usage: protodec(.exe) <target_assembly_path> <out_path> [options]
Usage: protodec(.exe) <game_assembly_path> <global_metadata_path> <unity_version> <out_path> [options]
Arguments:
target_assembly_path Either the path to the target assembly or a directory of assemblies, all of which be parsed.
game_assembly_path The path to the game assembly DLL.
global_metadata_path The path to the global-metadata.dat 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.
Options:
--debug Drops the minimum log level to Debug.
--parse_service_servers Parses gRPC service definitions from server classes.
--parse_service_clients Parses gRPC service definitions from client classes.
--skip_enums Skip parsing enums and replace references to them with int32.
@ -28,15 +34,20 @@ const string help = """
--include_service_methods_without_generated_code_attribute Includes methods that aren't decorated with `GeneratedCode("grpc_csharp_plugin")` when parsing gRPC services.
""";
if (args.Length < 2)
if (args.Length < 4)
{
Console.WriteLine(help);
return;
}
string assembly = args[0];
string outPath = Path.GetFullPath(args[1]);
string metadata = args[1];
string uVersion = args[2];
string outPath = Path.GetFullPath(args[3]);
ParserOptions options = ParserOptions.None;
LogLevel logLevel = args.Contains("--debug")
? LogLevel.Debug
: LogLevel.Information;
if (args.Contains("--skip_enums"))
options |= ParserOptions.SkipEnums;
@ -47,9 +58,30 @@ if (args.Contains("--include_properties_without_non_user_code_attribute"))
if (args.Contains("--include_service_methods_without_generated_code_attribute"))
options |= ParserOptions.IncludeServiceMethodsWithoutGeneratedCodeAttribute;
using ICilAssemblyLoader loader = new ClrAssemblyLoader(assembly);
ProtodecContext ctx = new();
if (!UnityVersion.TryParse(uVersion, out UnityVersion unityVersion, out _))
{
unityVersion = uVersion.EndsWith("globalgamemanagers")
? LibCpp2IlMain.GetVersionFromGlobalGameManagers(
File.ReadAllBytes(uVersion))
: LibCpp2IlMain.GetVersionFromDataUnity3D(
File.OpenRead(uVersion));
}
using ILoggerFactory loggerFactory = LoggerFactory.Create(
builder => builder.AddSimpleConsole(static console => console.IncludeScopes = true)
.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())
{
ctx.ParseMessage(message, options);
@ -57,6 +89,7 @@ foreach (ICilType message in GetProtobufMessageTypes())
if (args.Contains("--parse_service_servers"))
{
logger.LogInformation("Parsing Protobuf service server types...");
foreach (ICilType service in GetProtobufServiceServerTypes())
{
ctx.ParseService(service, options);
@ -65,6 +98,7 @@ if (args.Contains("--parse_service_servers"))
if (args.Contains("--parse_service_clients"))
{
logger.LogInformation("Parsing Protobuf service client types...");
foreach (ICilType service in GetProtobufServiceClientTypes())
{
ctx.ParseService(service, options);
@ -73,8 +107,9 @@ if (args.Contains("--parse_service_clients"))
if (Directory.Exists(outPath))
{
HashSet<string> writtenFiles = [];
logger.LogInformation("Writing {count} Protobuf files to \"{path}\"...", ctx.Protobufs.Count, outPath);
HashSet<string> writtenFiles = [];
foreach (Protobuf protobuf in ctx.Protobufs)
{
// This workaround stops files from being overwritten in the case of a naming conflict,
@ -95,6 +130,8 @@ if (Directory.Exists(outPath))
}
else
{
logger.LogInformation("Writing Protobufs as a single file to \"{path}\"...", outPath);
using StreamWriter streamWriter = new(outPath);
using IndentedTextWriter indentWriter = new(streamWriter, indent);
@ -115,4 +152,4 @@ IEnumerable<ICilType> GetProtobufServiceClientTypes() =>
IEnumerable<ICilType> GetProtobufServiceServerTypes() =>
loader.LoadedTypes.Where(
type => type is { IsNested: true, IsAbstract: true, DeclaringType: { IsNested: false, IsSealed: true, IsAbstract: true } }
&& type.GetCustomAttributes().Any(attribute => attribute.Type == loader.BindServiceMethodAttribute));
&& type.CustomAttributes.Any(attribute => attribute.Type == loader.BindServiceMethodAttribute));

View File

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