Compare commits

...

1 Commits

Author SHA1 Message Date
Xpl0itR ba35d37138
PoC using dev branch of LibCpp2Il 2024-06-14 05:35:56 +01:00
9 changed files with 332 additions and 226 deletions

View File

@ -1,13 +1,19 @@
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.
out_path An existing directory to output into individual files, otherwise output to a single file.
Options:
--parse_service_servers Parses gRPC service definitions from server classes.
@ -21,8 +27,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
- 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

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

View File

@ -0,0 +1,68 @@
// 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.Linq;
using AssetRipper.Primitives;
using CommunityToolkit.Diagnostics;
using LibCpp2IL;
using LibCpp2IL.Metadata;
using LibCpp2IL.Reflection;
namespace LibProtodec;
public sealed class Il2CppLoader : IDisposable
{
public Il2CppLoader(string assemblyPath, string metadataPath, UnityVersion unityVersion)
{
if (!LibCpp2IlMain.LoadFromFile(assemblyPath, metadataPath, unityVersion))
ThrowHelper.ThrowInvalidDataException("Failed to load il2cpp assembly!");
}
public IReadOnlyList<Il2CppTypeDefinition> LoadedTypes =>
LibCpp2IlMain.TheMetadata!.typeDefs;
public IEnumerable<Il2CppTypeDefinition> GetProtobufMessageTypes()
{
Il2CppTypeDefinition? iMessage = LibCpp2IlReflection.GetTypeByFullName("Google.Protobuf.IMessage");
Guard.IsNotNull(iMessage);
return LoadedTypes.Where(
type => !type.IsNested()
&& type.IsSealed()
&& type.Namespace?.StartsWith("Google.Protobuf", StringComparison.Ordinal) != true
&& type.IsAssignableTo(iMessage));
}
public IEnumerable<Il2CppTypeDefinition> GetProtobufServiceClientTypes()
{
Il2CppTypeDefinition? clientBase = LibCpp2IlReflection.GetTypeByFullName("Grpc.Core.ClientBase");
Guard.IsNotNull(clientBase);
return LoadedTypes.Where(
type => type.IsNested()
&& !type.IsAbstract
&& type.IsAssignableTo(clientBase));
}
public IEnumerable<Il2CppTypeDefinition> GetProtobufServiceServerTypes()
{
Il2CppTypeDefinition? bindServiceMethodAttribute = LibCpp2IlReflection.GetTypeByFullName("Grpc.Core.BindServiceMethodAttribute");
Guard.IsNotNull(bindServiceMethodAttribute);
return LoadedTypes.Where(
type => type.IsNested()
&& type is { IsAbstract: true, DeclaringType: not null }
&& !type.DeclaringType.IsNested()
&& type.DeclaringType.IsSealed()
&& type.DeclaringType.IsAbstract
&& type.GetCustomAttributeTypes().Any(attributeType => attributeType == bindServiceMethodAttribute));
}
public void Dispose() =>
LibCpp2IlMain.Reset();
}

View File

@ -0,0 +1,105 @@
// 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.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using LibCpp2IL;
using LibCpp2IL.Metadata;
namespace LibProtodec;
// ReSharper disable LoopCanBeConvertedToQuery
public static class Il2CppReflectionExtensions
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsClass(this Il2CppTypeDefinition type) =>
(type.Attributes & TypeAttributes.ClassSemanticsMask) == TypeAttributes.Class;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsNested(this Il2CppTypeDefinition type) =>
type.DeclaringType is not null;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsSealed(this Il2CppTypeDefinition type) =>
(type.Attributes & TypeAttributes.Sealed) != 0;
public static IEnumerable<Il2CppTypeDefinition> GetCustomAttributeTypes(this Il2CppTypeDefinition type) =>
GetAttributeTypes(
LibCpp2IlMain.TheMetadata!.GetCustomAttributeData(
type.DeclaringAssembly!, type.CustomAttributeIndex, type.Token, out _));
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsVirtual(this Il2CppMethodDefinition method) =>
(method.Attributes & MethodAttributes.Virtual) != 0;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsNonPublic(this Il2CppMethodDefinition method) =>
(method.Attributes & MethodAttributes.Public) == 0;
public static IEnumerable<Il2CppTypeDefinition> GetCustomAttributeTypes(this Il2CppMethodDefinition method) =>
GetAttributeTypes(
LibCpp2IlMain.TheMetadata!.GetCustomAttributeData(
method.DeclaringType!.DeclaringAssembly!, method.customAttributeIndex, method.token, out _));
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool CanRead(this Il2CppPropertyDefinition property) =>
property.get >= 0;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool CanWrite(this Il2CppPropertyDefinition property) =>
property.set >= 0;
public static IEnumerable<Il2CppTypeDefinition> GetCustomAttributeTypes(this Il2CppPropertyDefinition property) =>
GetAttributeTypes(
LibCpp2IlMain.TheMetadata!.GetCustomAttributeData(
property.DeclaringType!.DeclaringAssembly!, property.customAttributeIndex, property.token, out _));
public static IEnumerable<Il2CppTypeDefinition> GetCustomAttributeTypes(this Il2CppFieldDefinition field) =>
GetAttributeTypes(
LibCpp2IlMain.TheMetadata!.GetCustomAttributeData(
field.FieldType!.baseType!.DeclaringAssembly!, field.customAttributeIndex, field.token, out _));
public static IEnumerable<Il2CppFieldDefinition> GetFields(this Il2CppTypeDefinition type, FieldAttributes fieldAttributes)
{
foreach (Il2CppFieldDefinition field in type.Fields!)
{
if (((FieldAttributes)field.RawFieldType!.Attrs & fieldAttributes) == fieldAttributes)
yield return field;
}
}
public static bool IsAssignableTo(this Il2CppTypeDefinition thisType, Il2CppTypeDefinition baseType)
{
if ((baseType.IsInterface && thisType.Interfaces!.Any(reflectionData => reflectionData.baseType == baseType))
|| thisType == baseType)
{
return true;
}
Il2CppTypeDefinition? thisTypeBaseType = thisType.BaseType?.baseType;
return thisTypeBaseType is not null
&& IsAssignableTo(thisTypeBaseType, baseType);
}
private static IEnumerable<Il2CppTypeDefinition> GetAttributeTypes(Il2CppCustomAttributeTypeRange? attributeTypeRange)
{
if (attributeTypeRange is null)
yield break;
for (int i = attributeTypeRange.start, end = attributeTypeRange.start + attributeTypeRange.count; i < end; i++)
{
int j = LibCpp2IlMain.TheMetadata!.attributeTypes[i];
yield return LibCpp2IlMain.Binary!.GetType(j).AsClass();
}
}
}

View File

@ -19,8 +19,8 @@
<ItemGroup>
<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" />
<ProjectReference Include="..\..\..\Cpp2IL\LibCpp2IL\LibCpp2IL.csproj" />
<PackageReference Include="Xpl0itR.SystemEx" Version="1.2.0" />
</ItemGroup>
</Project>

View File

@ -14,7 +14,7 @@ namespace LibProtodec.Models.TopLevels;
public sealed class Message : TopLevel, INestableType
{
public readonly Dictionary<string, int[]> OneOfs = [];
public readonly Dictionary<string, List<int>> OneOfs = [];
public readonly Dictionary<int, MessageField> Fields = [];
public readonly Dictionary<string, INestableType> Nested = [];
@ -30,8 +30,7 @@ public sealed class Message : TopLevel, INestableType
Protobuf.WriteOptionTo(writer, "deprecated", "true");
}
int[] oneOfs = OneOfs.SelectMany(static oneOf => oneOf.Value).ToArray();
List<int> oneOfs = OneOfs.SelectMany(static oneOf => oneOf.Value).ToList();
foreach (MessageField field in Fields.Values)
{
if (oneOfs.Contains(field.Id))
@ -40,7 +39,7 @@ public sealed class Message : TopLevel, INestableType
field.WriteTo(writer, this, isOneOf: false);
}
foreach ((string name, int[] fieldIds) in OneOfs)
foreach ((string name, List<int> fieldIds) in OneOfs)
{
// ReSharper disable once StringLiteralTypo
writer.Write("oneof ");

View File

@ -13,6 +13,8 @@ using System.Linq;
using System.Reflection;
using SystemEx;
using CommunityToolkit.Diagnostics;
using LibCpp2IL.Metadata;
using LibCpp2IL.Reflection;
using LibProtodec.Models;
using LibProtodec.Models.Fields;
using LibProtodec.Models.TopLevels;
@ -20,13 +22,12 @@ using LibProtodec.Models.Types;
namespace LibProtodec;
public delegate bool TypeLookupFunc(Type type, [NotNullWhen(true)] out IType? fieldType, out string? import);
public delegate bool TypeLookupFunc(Il2CppTypeDefinition type, [NotNullWhen(true)] out IType? fieldType, out string? import);
public delegate bool NameLookupFunc(string name, [MaybeNullWhen(false)] out string translatedName);
public sealed class ProtodecContext
{
private const BindingFlags PublicStatic = BindingFlags.Public | BindingFlags.Static;
private const BindingFlags PublicInstanceDeclared = BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly;
private const FieldAttributes PublicStaticLiteral = FieldAttributes.Public | FieldAttributes.Static | FieldAttributes.Literal;
private readonly Dictionary<string, TopLevel> _parsed = [];
@ -52,11 +53,11 @@ public sealed class ProtodecContext
}
}
public Message ParseMessage(Type messageClass, ParserOptions options = ParserOptions.None)
public Message ParseMessage(Il2CppTypeDefinition messageClass, ParserOptions options = ParserOptions.None)
{
Guard.IsTrue(messageClass is { IsClass: true, IsSealed: true });
Guard.IsTrue(messageClass.IsClass() && messageClass.IsSealed());
if (_parsed.TryGetValue(messageClass.FullName ?? messageClass.Name, out TopLevel? parsedMessage))
if (_parsed.TryGetValue(messageClass.FullName!, out TopLevel? parsedMessage))
{
return (Message)parsedMessage;
}
@ -64,50 +65,51 @@ public sealed class ProtodecContext
Message message = new()
{
Name = TranslateTypeName(messageClass),
IsObsolete = HasObsoleteAttribute(messageClass.GetCustomAttributesData())
IsObsolete = HasObsoleteAttribute(messageClass.GetCustomAttributeTypes())
};
_parsed.Add(messageClass.FullName ?? messageClass.Name, message);
_parsed.Add(messageClass.FullName!, message);
Protobuf protobuf = GetProtobuf(messageClass, message, options);
FieldInfo[] idFields = messageClass.GetFields(PublicStatic);
PropertyInfo[] properties = messageClass.GetProperties(PublicInstanceDeclared);
List<Il2CppFieldDefinition> idFields = messageClass.GetFields(PublicStaticLiteral).ToList();
Il2CppPropertyDefinition[] properties = messageClass.Properties!;
for (int pi = 0, fi = 0; pi < properties.Length; pi++, fi++)
for (int pi = 0, fi = 0; pi < properties.Length; pi++)
{
PropertyInfo property = properties[pi];
IList<CustomAttributeData> attributes = property.GetCustomAttributesData();
if (((options & ParserOptions.IncludePropertiesWithoutNonUserCodeAttribute) == 0 && !HasNonUserCodeAttribute(attributes))
|| property.GetMethod?.IsVirtual != false)
Il2CppPropertyDefinition property = properties[pi];
if (!property.CanRead()
|| property.Getter!.IsStatic
|| property.Getter!.IsVirtual()
|| property.Getter!.IsNonPublic())
{
fi--;
continue;
}
Type propertyType = property.PropertyType;
List<Il2CppTypeDefinition> attributes = property.GetCustomAttributeTypes().ToList();
if (((options & ParserOptions.IncludePropertiesWithoutNonUserCodeAttribute) == 0 && !HasNonUserCodeAttribute(attributes)))
{
continue;
}
Il2CppTypeDefinition propertyType = property.PropertyType!.baseType!;
// only OneOf enums are defined nested directly in the message class
if (propertyType.IsEnum && propertyType.DeclaringType?.Name == messageClass.Name)
if (propertyType.IsEnumType && propertyType.DeclaringType?.Name == messageClass.Name)
{
string oneOfName = TranslateOneOfPropName(property.Name);
int[] oneOfProtoFieldIds = propertyType.GetFields(PublicStatic)
.Select(static field => (int)field.GetRawConstantValue()!)
string oneOfName = TranslateOneOfPropName(property.Name!);
List<int> oneOfProtoFieldIds = propertyType.GetFields(PublicStaticLiteral)
.Select(static field => (int)field.DefaultValue!.Value!)
.Where(static id => id > 0)
.ToArray();
.ToList();
message.OneOfs.Add(oneOfName, oneOfProtoFieldIds);
fi--;
continue;
}
FieldInfo idField = idFields[fi];
Guard.IsTrue(idField.IsLiteral);
Guard.IsEqualTo(idField.FieldType.Name, nameof(Int32));
Il2CppFieldDefinition idField = idFields[fi];
bool msgFieldHasHasProp = false; // some field properties are immediately followed by an additional "Has" get-only boolean property
if (properties.Length > pi + 1 && properties[pi + 1].PropertyType.Name == nameof(Boolean) && !properties[pi + 1].CanWrite)
if (properties.Length > pi + 1 && properties[pi + 1].PropertyType!.baseType!.Name == nameof(Boolean) && !properties[pi + 1].CanWrite())
{
msgFieldHasHasProp = true;
pi++;
@ -115,24 +117,25 @@ public sealed class ProtodecContext
MessageField field = new()
{
Type = ParseFieldType(propertyType, options, protobuf),
Name = TranslateMessageFieldName(property.Name),
Id = (int)idField.GetRawConstantValue()!,
Type = ParseFieldType(property.PropertyType!, options, protobuf),
Name = TranslateMessageFieldName(property.Name!),
Id = (int)idField.DefaultValue!.Value!,
IsObsolete = HasObsoleteAttribute(attributes),
HasHasProp = msgFieldHasHasProp
};
message.Fields.Add(field.Id, field);
fi++;
}
return message;
}
public Enum ParseEnum(Type enumEnum, ParserOptions options = ParserOptions.None)
public Enum ParseEnum(Il2CppTypeDefinition enumEnum, ParserOptions options = ParserOptions.None)
{
Guard.IsTrue(enumEnum.IsEnum);
Guard.IsTrue(enumEnum.IsEnumType);
if (_parsed.TryGetValue(enumEnum.FullName ?? enumEnum.Name, out TopLevel? parsedEnum))
if (_parsed.TryGetValue(enumEnum.FullName!, out TopLevel? parsedEnum))
{
return (Enum)parsedEnum;
}
@ -140,20 +143,20 @@ public sealed class ProtodecContext
Enum @enum = new()
{
Name = TranslateTypeName(enumEnum),
IsObsolete = HasObsoleteAttribute(enumEnum.GetCustomAttributesData())
IsObsolete = HasObsoleteAttribute(enumEnum.GetCustomAttributeTypes())
};
_parsed.Add(enumEnum.FullName ?? enumEnum.Name, @enum);
_parsed.Add(enumEnum.FullName!, @enum);
Protobuf protobuf = GetProtobuf(enumEnum, @enum, options);
foreach (FieldInfo field in enumEnum.GetFields(PublicStatic))
foreach (Il2CppFieldDefinition field in enumEnum.GetFields(PublicStaticLiteral))
{
@enum.Fields.Add(
new EnumField
{
Id = (int)field.GetRawConstantValue()!,
Name = TranslateEnumFieldName(field, @enum.Name),
IsObsolete = HasObsoleteAttribute(field.GetCustomAttributesData())
Id = (int)field.DefaultValue!.Value!,
Name = TranslateEnumFieldName(field.Name!, @enum.Name),
IsObsolete = HasObsoleteAttribute(field.GetCustomAttributeTypes())
});
}
@ -166,34 +169,34 @@ public sealed class ProtodecContext
return @enum;
}
public Service ParseService(Type serviceClass, ParserOptions options = ParserOptions.None)
public Service ParseService(Il2CppTypeDefinition serviceClass, ParserOptions options = ParserOptions.None)
{
Guard.IsTrue(serviceClass.IsClass);
Guard.IsTrue(serviceClass.IsClass());
bool? isClientClass = null;
if (serviceClass.IsAbstract)
{
if (serviceClass is { IsSealed: true, IsNested: false })
if (serviceClass.IsSealed() && !serviceClass.IsNested())
{
Type[] nested = serviceClass.GetNestedTypes();
serviceClass = nested.SingleOrDefault(static nested => nested is { IsAbstract: true, IsSealed: false })
?? nested.Single(static nested => nested is { IsClass: true, IsAbstract: false });
Il2CppTypeDefinition[] nested = serviceClass.NestedTypes!;
serviceClass = nested.SingleOrDefault(static nested => nested.IsAbstract && !nested.IsSealed())
?? nested.Single(static nested => nested.IsClass() && !nested.IsAbstract);
}
if (serviceClass is { IsNested: true, IsAbstract: true, IsSealed: false })
if (serviceClass.IsNested() && serviceClass.IsAbstract && !serviceClass.IsSealed())
{
isClientClass = false;
}
}
if (serviceClass is { IsAbstract: false, IsNested: true, DeclaringType: not null })
if (serviceClass is { IsAbstract: false, DeclaringType: not null })
{
isClientClass = true;
}
Guard.IsNotNull(isClientClass);
if (_parsed.TryGetValue(serviceClass.DeclaringType!.FullName ?? serviceClass.DeclaringType!.Name, out TopLevel? parsedService))
if (_parsed.TryGetValue(serviceClass.DeclaringType!.FullName!, out TopLevel? parsedService))
{
return (Service)parsedService;
}
@ -201,50 +204,54 @@ public sealed class ProtodecContext
Service service = new()
{
Name = TranslateTypeName(serviceClass.DeclaringType),
IsObsolete = HasObsoleteAttribute(serviceClass.GetCustomAttributesData())
IsObsolete = HasObsoleteAttribute(serviceClass.GetCustomAttributeTypes())
};
_parsed.Add(serviceClass.DeclaringType!.FullName ?? serviceClass.DeclaringType.Name, service);
_parsed.Add(serviceClass.DeclaringType!.FullName!, service);
Protobuf protobuf = NewProtobuf(serviceClass, service);
foreach (MethodInfo method in serviceClass.GetMethods(PublicInstanceDeclared))
foreach (Il2CppMethodDefinition method in serviceClass.Methods!)
{
IList<CustomAttributeData> attributes = method.GetCustomAttributesData();
if (method.IsNonPublic() || method.IsStatic)
{
continue;
}
List<Il2CppTypeDefinition> attributes = method.GetCustomAttributeTypes().ToList();
if ((options & ParserOptions.IncludeServiceMethodsWithoutGeneratedCodeAttribute) == 0
&& !HasGeneratedCodeAttribute(attributes, "grpc_csharp_plugin"))
{
continue;
}
Type requestType, responseType, returnType = method.ReturnType;
Il2CppTypeReflectionData requestType, responseType, returnType = method.ReturnType!;
bool streamReq, streamRes;
if (isClientClass.Value)
{
string returnTypeName = TranslateTypeName(returnType);
string returnTypeName = TranslateTypeName(returnType.baseType!);
if (returnTypeName == "AsyncUnaryCall`1")
{
continue;
}
ParameterInfo[] parameters = method.GetParameters();
if (parameters.Length > 2)
if (method.Parameters!.Length > 2)
{
continue;
}
Type firstParamType = parameters[0].ParameterType;
switch (returnType.GenericTypeArguments.Length)
Il2CppTypeReflectionData firstParamType = method.Parameters![0].Type;
switch (returnType.genericParams.Length)
{
case 2:
requestType = returnType.GenericTypeArguments[0];
responseType = returnType.GenericTypeArguments[1];
requestType = returnType.genericParams[0];
responseType = returnType.genericParams[1];
streamReq = true;
streamRes = returnTypeName == "AsyncDuplexStreamingCall`2";
break;
case 1:
requestType = firstParamType;
responseType = returnType.GenericTypeArguments[0];
responseType = returnType.genericParams[0];
streamReq = false;
streamRes = true;
break;
@ -258,13 +265,11 @@ public sealed class ProtodecContext
}
else
{
ParameterInfo[] parameters = method.GetParameters();
Type firstParamType = parameters[0].ParameterType;
if (firstParamType.GenericTypeArguments.Length == 1)
Il2CppTypeReflectionData firstParamType = method.Parameters![0].Type;
if (firstParamType.genericParams.Length == 1)
{
streamReq = true;
requestType = firstParamType.GenericTypeArguments[0];
requestType = firstParamType.genericParams[0];
}
else
{
@ -272,22 +277,22 @@ public sealed class ProtodecContext
requestType = firstParamType;
}
if (returnType.GenericTypeArguments.Length == 1)
if (returnType.genericParams.Length == 1)
{
streamRes = false;
responseType = returnType.GenericTypeArguments[0];
responseType = returnType.genericParams[0];
}
else
{
streamRes = true;
responseType = parameters[1].ParameterType.GenericTypeArguments[0];
responseType = method.Parameters![1].Type.genericParams[0];
}
}
service.Methods.Add(
new ServiceMethod
{
Name = TranslateMethodName(method.Name),
Name = TranslateMethodName(method.Name!),
IsObsolete = HasObsoleteAttribute(attributes),
RequestType = ParseFieldType(requestType, options, protobuf),
ResponseType = ParseFieldType(responseType, options, protobuf),
@ -299,19 +304,21 @@ public sealed class ProtodecContext
return service;
}
private IType ParseFieldType(Type type, ParserOptions options, Protobuf referencingProtobuf)
private IType ParseFieldType(Il2CppTypeReflectionData reflectionData, ParserOptions options, Protobuf referencingProtobuf)
{
switch (type.GenericTypeArguments.Length)
switch (reflectionData.genericParams.Length)
{
case 1:
return new Repeated(
ParseFieldType(type.GenericTypeArguments[0], options, referencingProtobuf));
ParseFieldType(reflectionData.genericParams[0], options, referencingProtobuf));
case 2:
return new Map(
ParseFieldType(type.GenericTypeArguments[0], options, referencingProtobuf),
ParseFieldType(type.GenericTypeArguments[1], options, referencingProtobuf));
ParseFieldType(reflectionData.genericParams[0], options, referencingProtobuf),
ParseFieldType(reflectionData.genericParams[1], options, referencingProtobuf));
}
Il2CppTypeDefinition type = reflectionData.baseType!;
if (TypeLookup(type, out IType? fieldType, out string? import))
{
if (import is not null)
@ -322,7 +329,7 @@ public sealed class ProtodecContext
return fieldType;
}
if (type.IsEnum)
if (type.IsEnumType)
{
if ((options & ParserOptions.SkipEnums) > 0)
{
@ -345,11 +352,11 @@ public sealed class ProtodecContext
return fieldType;
}
private Protobuf NewProtobuf(Type topLevelType, TopLevel topLevel)
private Protobuf NewProtobuf(Il2CppTypeDefinition topLevelType, TopLevel topLevel)
{
Protobuf protobuf = new()
{
AssemblyName = topLevelType.Assembly.FullName,
AssemblyName = topLevelType.DeclaringAssembly!.Name!,
Namespace = topLevelType.Namespace
};
@ -360,14 +367,14 @@ public sealed class ProtodecContext
return protobuf;
}
private Protobuf GetProtobuf<T>(Type topLevelType, T topLevel, ParserOptions options)
private Protobuf GetProtobuf<T>(Il2CppTypeDefinition topLevelType, T topLevel, ParserOptions options)
where T : TopLevel, INestableType
{
Protobuf protobuf;
if (topLevelType.IsNested)
if (topLevelType.IsNested())
{
Type parent = topLevelType.DeclaringType!.DeclaringType!;
if (!_parsed.TryGetValue(parent.FullName ?? parent.Name, out TopLevel? parentTopLevel))
Il2CppTypeDefinition parent = topLevelType.DeclaringType!.DeclaringType!;
if (!_parsed.TryGetValue(parent.FullName!, out TopLevel? parentTopLevel))
{
parentTopLevel = ParseMessage(parent, options);
}
@ -376,7 +383,7 @@ public sealed class ProtodecContext
topLevel.Protobuf = protobuf;
topLevel.Parent = parentTopLevel;
((Message)parentTopLevel).Nested.Add(topLevelType.Name, topLevel);
((Message)parentTopLevel).Nested.Add(topLevelType.Name!, topLevel);
}
else
{
@ -421,25 +428,18 @@ public sealed class ProtodecContext
return translatedName!.ToSnakeCaseLower();
}
private string TranslateEnumFieldName(FieldInfo field, string enumName)
private string TranslateEnumFieldName(string fieldName, string enumName)
{
if (field.GetCustomAttributesData()
.SingleOrDefault(static attr => attr.AttributeType.Name == "OriginalNameAttribute")
?.ConstructorArguments[0]
.Value
is string originalName)
//TODO: parse original name from first parameter of OriginalNameAttribute constructor
if (NameLookup?.Invoke(fieldName, out string? translatedName) == true)
{
return originalName;
fieldName = translatedName;
}
if (NameLookup?.Invoke(field.Name, out string? fieldName) != true)
if (!IsBeebyted(fieldName))
{
fieldName = field.Name;
}
if (!IsBeebyted(fieldName!))
{
fieldName = fieldName!.ToSnakeCaseUpper();
fieldName = fieldName.ToSnakeCaseUpper();
}
if (!IsBeebyted(enumName))
@ -450,21 +450,19 @@ public sealed class ProtodecContext
return enumName + '_' + fieldName;
}
private string TranslateTypeName(Type type)
private string TranslateTypeName(Il2CppTypeDefinition type)
{
if (NameLookup is null)
return type.Name;
string? fullName = type.FullName;
Guard.IsNotNull(fullName);
return type.Name!;
string fullName = type.FullName!;
int genericArgs = fullName.IndexOf('[');
if (genericArgs != -1)
fullName = fullName[..genericArgs];
if (!NameLookup(fullName, out string? translatedName))
{
return type.Name;
return type.Name!;
}
int lastSlash = translatedName.LastIndexOf('/');
@ -478,7 +476,7 @@ public sealed class ProtodecContext
return translatedName;
}
public static bool LookupScalarAndWellKnownTypes(Type type, [NotNullWhen(true)] out IType? fieldType, out string? import)
public static bool LookupScalarAndWellKnownTypes(Il2CppTypeDefinition type, [NotNullWhen(true)] out IType? fieldType, out string? import)
{
switch (type.FullName)
{
@ -642,13 +640,15 @@ public sealed class ProtodecContext
private static bool IsBeebyted(string name) =>
name.Length == 11 && name.CountUpper() == 11;
private static bool HasGeneratedCodeAttribute(IEnumerable<CustomAttributeData> attributes, string tool) =>
attributes.Any(attr => attr.AttributeType.Name == nameof(GeneratedCodeAttribute)
&& attr.ConstructorArguments[0].Value as string == tool);
private static bool HasGeneratedCodeAttribute(IEnumerable<Il2CppTypeDefinition> attributeTypes, string tool)
{
return attributeTypes.Any(static attrType => attrType.Name == nameof(GeneratedCodeAttribute));
//TODO: ensure the first argument of the GeneratedCodeAttribute constructor == tool parameter
}
private static bool HasNonUserCodeAttribute(IEnumerable<CustomAttributeData> attributes) =>
attributes.Any(static attr => attr.AttributeType.Name == nameof(DebuggerNonUserCodeAttribute));
private static bool HasNonUserCodeAttribute(IEnumerable<Il2CppTypeDefinition> attributeTypes) =>
attributeTypes.Any(static attrType => attrType.Name == nameof(DebuggerNonUserCodeAttribute));
private static bool HasObsoleteAttribute(IEnumerable<CustomAttributeData> attributes) =>
attributes.Any(static attr => attr.AttributeType.Name == nameof(ObsoleteAttribute));
private static bool HasObsoleteAttribute(IEnumerable<Il2CppTypeDefinition> attributeTypes) =>
attributeTypes.Any(static attrType => attrType.Name == nameof(ObsoleteAttribute));
}

View File

@ -3,14 +3,18 @@ using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using AssetRipper.Primitives;
using LibCpp2IL.Metadata;
using LibProtodec;
using LibProtodec.Models;
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.
out_path An existing directory to output into individual files, otherwise output to a single file.
Options:
--parse_service_servers Parses gRPC service definitions from server classes.
@ -20,14 +24,16 @@ 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];
UnityVersion uVersion = UnityVersion.Parse(args[2]);
string outPath = Path.GetFullPath(args[3]);
ParserOptions options = ParserOptions.None;
if (args.Contains("--skip_enums"))
@ -39,17 +45,17 @@ if (args.Contains("--include_properties_without_non_user_code_attribute"))
if (args.Contains("--include_service_methods_without_generated_code_attribute"))
options |= ParserOptions.IncludeServiceMethodsWithoutGeneratedCodeAttribute;
using AssemblyInspector inspector = new(assembly);
using Il2CppLoader inspector = new(assembly, metadata, uVersion);
ProtodecContext ctx = new();
foreach (Type message in inspector.GetProtobufMessageTypes())
foreach (Il2CppTypeDefinition message in inspector.GetProtobufMessageTypes())
{
ctx.ParseMessage(message, options);
}
if (args.Contains("--parse_service_servers"))
{
foreach (Type service in inspector.GetProtobufServiceServerTypes())
foreach (Il2CppTypeDefinition service in inspector.GetProtobufServiceServerTypes())
{
ctx.ParseService(service, options);
}
@ -57,7 +63,7 @@ if (args.Contains("--parse_service_servers"))
if (args.Contains("--parse_service_clients"))
{
foreach (Type service in inspector.GetProtobufServiceClientTypes())
foreach (Il2CppTypeDefinition service in inspector.GetProtobufServiceClientTypes())
{
ctx.ParseService(service, options);
}