mirror of https://github.com/Xpl0itR/protodec.git
PoC using dev branch of LibCpp2Il
This commit is contained in:
parent
d43deef033
commit
ba35d37138
17
README.md
17
README.md
|
@ -1,13 +1,19 @@
|
||||||
protodec
|
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
|
||||||
-----
|
-----
|
||||||
```
|
```
|
||||||
Usage: protodec(.exe) <target_assembly_path> <out_path> [options]
|
Usage: protodec(.exe) <game_assembly_path> <global_metadata_path> <unity_version> <out_path> [options]
|
||||||
Arguments:
|
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.
|
out_path An existing directory to output into individual files, otherwise output to a single file.
|
||||||
Options:
|
Options:
|
||||||
--parse_service_servers Parses gRPC service definitions from server classes.
|
--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.
|
- 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.
|
- 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
|
- Due to the development branch of Cpp2Il not yet recovering method bodies
|
||||||
- The `Name` parameter of `OriginalNameAttribute` is not dumped. In this case, the CIL enum field names are used after conforming them to protobuf conventions
|
- 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
|
License
|
||||||
-------
|
-------
|
||||||
|
|
12
protodec.sln
12
protodec.sln
|
@ -12,6 +12,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
|
||||||
README.md = README.md
|
README.md = README.md
|
||||||
EndProjectSection
|
EndProjectSection
|
||||||
EndProject
|
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
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
Debug|Any CPU = Debug|Any CPU
|
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}.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.ActiveCfg = Release|Any CPU
|
||||||
{5F6DAD82-D9AD-4CE5-86E6-D20C9F059A4D}.Release|Any CPU.Build.0 = 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
|
EndGlobalSection
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
|
|
|
@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -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();
|
||||||
|
}
|
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -19,8 +19,8 @@
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All" />
|
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All" />
|
||||||
<PackageReference Include="System.Reflection.MetadataLoadContext" Version="8.0.0" />
|
<ProjectReference Include="..\..\..\Cpp2IL\LibCpp2IL\LibCpp2IL.csproj" />
|
||||||
<PackageReference Include="Xpl0itR.SystemEx" Version="1.1.0" />
|
<PackageReference Include="Xpl0itR.SystemEx" Version="1.2.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
|
@ -14,7 +14,7 @@ namespace LibProtodec.Models.TopLevels;
|
||||||
|
|
||||||
public sealed class Message : TopLevel, INestableType
|
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<int, MessageField> Fields = [];
|
||||||
public readonly Dictionary<string, INestableType> Nested = [];
|
public readonly Dictionary<string, INestableType> Nested = [];
|
||||||
|
|
||||||
|
@ -30,8 +30,7 @@ public sealed class Message : TopLevel, INestableType
|
||||||
Protobuf.WriteOptionTo(writer, "deprecated", "true");
|
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)
|
foreach (MessageField field in Fields.Values)
|
||||||
{
|
{
|
||||||
if (oneOfs.Contains(field.Id))
|
if (oneOfs.Contains(field.Id))
|
||||||
|
@ -40,7 +39,7 @@ public sealed class Message : TopLevel, INestableType
|
||||||
field.WriteTo(writer, this, isOneOf: false);
|
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
|
// ReSharper disable once StringLiteralTypo
|
||||||
writer.Write("oneof ");
|
writer.Write("oneof ");
|
||||||
|
|
|
@ -13,6 +13,8 @@ using System.Linq;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using SystemEx;
|
using SystemEx;
|
||||||
using CommunityToolkit.Diagnostics;
|
using CommunityToolkit.Diagnostics;
|
||||||
|
using LibCpp2IL.Metadata;
|
||||||
|
using LibCpp2IL.Reflection;
|
||||||
using LibProtodec.Models;
|
using LibProtodec.Models;
|
||||||
using LibProtodec.Models.Fields;
|
using LibProtodec.Models.Fields;
|
||||||
using LibProtodec.Models.TopLevels;
|
using LibProtodec.Models.TopLevels;
|
||||||
|
@ -20,13 +22,12 @@ using LibProtodec.Models.Types;
|
||||||
|
|
||||||
namespace LibProtodec;
|
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 delegate bool NameLookupFunc(string name, [MaybeNullWhen(false)] out string translatedName);
|
||||||
|
|
||||||
public sealed class ProtodecContext
|
public sealed class ProtodecContext
|
||||||
{
|
{
|
||||||
private const BindingFlags PublicStatic = BindingFlags.Public | BindingFlags.Static;
|
private const FieldAttributes PublicStaticLiteral = FieldAttributes.Public | FieldAttributes.Static | FieldAttributes.Literal;
|
||||||
private const BindingFlags PublicInstanceDeclared = BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly;
|
|
||||||
|
|
||||||
private readonly Dictionary<string, TopLevel> _parsed = [];
|
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;
|
return (Message)parsedMessage;
|
||||||
}
|
}
|
||||||
|
@ -64,50 +65,51 @@ public sealed class ProtodecContext
|
||||||
Message message = new()
|
Message message = new()
|
||||||
{
|
{
|
||||||
Name = TranslateTypeName(messageClass),
|
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);
|
Protobuf protobuf = GetProtobuf(messageClass, message, options);
|
||||||
|
|
||||||
FieldInfo[] idFields = messageClass.GetFields(PublicStatic);
|
List<Il2CppFieldDefinition> idFields = messageClass.GetFields(PublicStaticLiteral).ToList();
|
||||||
PropertyInfo[] properties = messageClass.GetProperties(PublicInstanceDeclared);
|
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];
|
Il2CppPropertyDefinition property = properties[pi];
|
||||||
IList<CustomAttributeData> attributes = property.GetCustomAttributesData();
|
if (!property.CanRead()
|
||||||
|
|| property.Getter!.IsStatic
|
||||||
if (((options & ParserOptions.IncludePropertiesWithoutNonUserCodeAttribute) == 0 && !HasNonUserCodeAttribute(attributes))
|
|| property.Getter!.IsVirtual()
|
||||||
|| property.GetMethod?.IsVirtual != false)
|
|| property.Getter!.IsNonPublic())
|
||||||
{
|
{
|
||||||
fi--;
|
|
||||||
continue;
|
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
|
// 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);
|
string oneOfName = TranslateOneOfPropName(property.Name!);
|
||||||
int[] oneOfProtoFieldIds = propertyType.GetFields(PublicStatic)
|
List<int> oneOfProtoFieldIds = propertyType.GetFields(PublicStaticLiteral)
|
||||||
.Select(static field => (int)field.GetRawConstantValue()!)
|
.Select(static field => (int)field.DefaultValue!.Value!)
|
||||||
.Where(static id => id > 0)
|
.Where(static id => id > 0)
|
||||||
.ToArray();
|
.ToList();
|
||||||
|
|
||||||
message.OneOfs.Add(oneOfName, oneOfProtoFieldIds);
|
message.OneOfs.Add(oneOfName, oneOfProtoFieldIds);
|
||||||
|
|
||||||
fi--;
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
FieldInfo idField = idFields[fi];
|
Il2CppFieldDefinition idField = idFields[fi];
|
||||||
Guard.IsTrue(idField.IsLiteral);
|
|
||||||
Guard.IsEqualTo(idField.FieldType.Name, nameof(Int32));
|
|
||||||
|
|
||||||
bool msgFieldHasHasProp = false; // some field properties are immediately followed by an additional "Has" get-only boolean property
|
bool msgFieldHasHasProp = false; // some field properties are immediately followed by an additional "Has" get-only boolean property
|
||||||
if (properties.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;
|
msgFieldHasHasProp = true;
|
||||||
pi++;
|
pi++;
|
||||||
|
@ -115,24 +117,25 @@ public sealed class ProtodecContext
|
||||||
|
|
||||||
MessageField field = new()
|
MessageField field = new()
|
||||||
{
|
{
|
||||||
Type = ParseFieldType(propertyType, options, protobuf),
|
Type = ParseFieldType(property.PropertyType!, options, protobuf),
|
||||||
Name = TranslateMessageFieldName(property.Name),
|
Name = TranslateMessageFieldName(property.Name!),
|
||||||
Id = (int)idField.GetRawConstantValue()!,
|
Id = (int)idField.DefaultValue!.Value!,
|
||||||
IsObsolete = HasObsoleteAttribute(attributes),
|
IsObsolete = HasObsoleteAttribute(attributes),
|
||||||
HasHasProp = msgFieldHasHasProp
|
HasHasProp = msgFieldHasHasProp
|
||||||
};
|
};
|
||||||
|
|
||||||
message.Fields.Add(field.Id, field);
|
message.Fields.Add(field.Id, field);
|
||||||
|
fi++;
|
||||||
}
|
}
|
||||||
|
|
||||||
return message;
|
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;
|
return (Enum)parsedEnum;
|
||||||
}
|
}
|
||||||
|
@ -140,20 +143,20 @@ public sealed class ProtodecContext
|
||||||
Enum @enum = new()
|
Enum @enum = new()
|
||||||
{
|
{
|
||||||
Name = TranslateTypeName(enumEnum),
|
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);
|
Protobuf protobuf = GetProtobuf(enumEnum, @enum, options);
|
||||||
|
|
||||||
foreach (FieldInfo field in enumEnum.GetFields(PublicStatic))
|
foreach (Il2CppFieldDefinition field in enumEnum.GetFields(PublicStaticLiteral))
|
||||||
{
|
{
|
||||||
@enum.Fields.Add(
|
@enum.Fields.Add(
|
||||||
new EnumField
|
new EnumField
|
||||||
{
|
{
|
||||||
Id = (int)field.GetRawConstantValue()!,
|
Id = (int)field.DefaultValue!.Value!,
|
||||||
Name = TranslateEnumFieldName(field, @enum.Name),
|
Name = TranslateEnumFieldName(field.Name!, @enum.Name),
|
||||||
IsObsolete = HasObsoleteAttribute(field.GetCustomAttributesData())
|
IsObsolete = HasObsoleteAttribute(field.GetCustomAttributeTypes())
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -166,34 +169,34 @@ public sealed class ProtodecContext
|
||||||
return @enum;
|
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;
|
bool? isClientClass = null;
|
||||||
if (serviceClass.IsAbstract)
|
if (serviceClass.IsAbstract)
|
||||||
{
|
{
|
||||||
if (serviceClass is { IsSealed: true, IsNested: false })
|
if (serviceClass.IsSealed() && !serviceClass.IsNested())
|
||||||
{
|
{
|
||||||
Type[] nested = serviceClass.GetNestedTypes();
|
Il2CppTypeDefinition[] nested = serviceClass.NestedTypes!;
|
||||||
serviceClass = nested.SingleOrDefault(static nested => nested is { IsAbstract: true, IsSealed: false })
|
serviceClass = nested.SingleOrDefault(static nested => nested.IsAbstract && !nested.IsSealed())
|
||||||
?? nested.Single(static nested => nested is { IsClass: true, IsAbstract: false });
|
?? 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;
|
isClientClass = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (serviceClass is { IsAbstract: false, IsNested: true, DeclaringType: not null })
|
if (serviceClass is { IsAbstract: false, DeclaringType: not null })
|
||||||
{
|
{
|
||||||
isClientClass = true;
|
isClientClass = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
Guard.IsNotNull(isClientClass);
|
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;
|
return (Service)parsedService;
|
||||||
}
|
}
|
||||||
|
@ -201,50 +204,54 @@ public sealed class ProtodecContext
|
||||||
Service service = new()
|
Service service = new()
|
||||||
{
|
{
|
||||||
Name = TranslateTypeName(serviceClass.DeclaringType),
|
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);
|
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
|
if ((options & ParserOptions.IncludeServiceMethodsWithoutGeneratedCodeAttribute) == 0
|
||||||
&& !HasGeneratedCodeAttribute(attributes, "grpc_csharp_plugin"))
|
&& !HasGeneratedCodeAttribute(attributes, "grpc_csharp_plugin"))
|
||||||
{
|
{
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
Type requestType, responseType, returnType = method.ReturnType;
|
Il2CppTypeReflectionData requestType, responseType, returnType = method.ReturnType!;
|
||||||
bool streamReq, streamRes;
|
bool streamReq, streamRes;
|
||||||
|
|
||||||
if (isClientClass.Value)
|
if (isClientClass.Value)
|
||||||
{
|
{
|
||||||
string returnTypeName = TranslateTypeName(returnType);
|
string returnTypeName = TranslateTypeName(returnType.baseType!);
|
||||||
if (returnTypeName == "AsyncUnaryCall`1")
|
if (returnTypeName == "AsyncUnaryCall`1")
|
||||||
{
|
{
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
ParameterInfo[] parameters = method.GetParameters();
|
if (method.Parameters!.Length > 2)
|
||||||
if (parameters.Length > 2)
|
|
||||||
{
|
{
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
Type firstParamType = parameters[0].ParameterType;
|
Il2CppTypeReflectionData firstParamType = method.Parameters![0].Type;
|
||||||
switch (returnType.GenericTypeArguments.Length)
|
switch (returnType.genericParams.Length)
|
||||||
{
|
{
|
||||||
case 2:
|
case 2:
|
||||||
requestType = returnType.GenericTypeArguments[0];
|
requestType = returnType.genericParams[0];
|
||||||
responseType = returnType.GenericTypeArguments[1];
|
responseType = returnType.genericParams[1];
|
||||||
streamReq = true;
|
streamReq = true;
|
||||||
streamRes = returnTypeName == "AsyncDuplexStreamingCall`2";
|
streamRes = returnTypeName == "AsyncDuplexStreamingCall`2";
|
||||||
break;
|
break;
|
||||||
case 1:
|
case 1:
|
||||||
requestType = firstParamType;
|
requestType = firstParamType;
|
||||||
responseType = returnType.GenericTypeArguments[0];
|
responseType = returnType.genericParams[0];
|
||||||
streamReq = false;
|
streamReq = false;
|
||||||
streamRes = true;
|
streamRes = true;
|
||||||
break;
|
break;
|
||||||
|
@ -258,13 +265,11 @@ public sealed class ProtodecContext
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
ParameterInfo[] parameters = method.GetParameters();
|
Il2CppTypeReflectionData firstParamType = method.Parameters![0].Type;
|
||||||
Type firstParamType = parameters[0].ParameterType;
|
if (firstParamType.genericParams.Length == 1)
|
||||||
|
|
||||||
if (firstParamType.GenericTypeArguments.Length == 1)
|
|
||||||
{
|
{
|
||||||
streamReq = true;
|
streamReq = true;
|
||||||
requestType = firstParamType.GenericTypeArguments[0];
|
requestType = firstParamType.genericParams[0];
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -272,22 +277,22 @@ public sealed class ProtodecContext
|
||||||
requestType = firstParamType;
|
requestType = firstParamType;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (returnType.GenericTypeArguments.Length == 1)
|
if (returnType.genericParams.Length == 1)
|
||||||
{
|
{
|
||||||
streamRes = false;
|
streamRes = false;
|
||||||
responseType = returnType.GenericTypeArguments[0];
|
responseType = returnType.genericParams[0];
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
streamRes = true;
|
streamRes = true;
|
||||||
responseType = parameters[1].ParameterType.GenericTypeArguments[0];
|
responseType = method.Parameters![1].Type.genericParams[0];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
service.Methods.Add(
|
service.Methods.Add(
|
||||||
new ServiceMethod
|
new ServiceMethod
|
||||||
{
|
{
|
||||||
Name = TranslateMethodName(method.Name),
|
Name = TranslateMethodName(method.Name!),
|
||||||
IsObsolete = HasObsoleteAttribute(attributes),
|
IsObsolete = HasObsoleteAttribute(attributes),
|
||||||
RequestType = ParseFieldType(requestType, options, protobuf),
|
RequestType = ParseFieldType(requestType, options, protobuf),
|
||||||
ResponseType = ParseFieldType(responseType, options, protobuf),
|
ResponseType = ParseFieldType(responseType, options, protobuf),
|
||||||
|
@ -299,19 +304,21 @@ public sealed class ProtodecContext
|
||||||
return service;
|
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:
|
case 1:
|
||||||
return new Repeated(
|
return new Repeated(
|
||||||
ParseFieldType(type.GenericTypeArguments[0], options, referencingProtobuf));
|
ParseFieldType(reflectionData.genericParams[0], options, referencingProtobuf));
|
||||||
case 2:
|
case 2:
|
||||||
return new Map(
|
return new Map(
|
||||||
ParseFieldType(type.GenericTypeArguments[0], options, referencingProtobuf),
|
ParseFieldType(reflectionData.genericParams[0], options, referencingProtobuf),
|
||||||
ParseFieldType(type.GenericTypeArguments[1], options, referencingProtobuf));
|
ParseFieldType(reflectionData.genericParams[1], options, referencingProtobuf));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Il2CppTypeDefinition type = reflectionData.baseType!;
|
||||||
|
|
||||||
if (TypeLookup(type, out IType? fieldType, out string? import))
|
if (TypeLookup(type, out IType? fieldType, out string? import))
|
||||||
{
|
{
|
||||||
if (import is not null)
|
if (import is not null)
|
||||||
|
@ -322,7 +329,7 @@ public sealed class ProtodecContext
|
||||||
return fieldType;
|
return fieldType;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (type.IsEnum)
|
if (type.IsEnumType)
|
||||||
{
|
{
|
||||||
if ((options & ParserOptions.SkipEnums) > 0)
|
if ((options & ParserOptions.SkipEnums) > 0)
|
||||||
{
|
{
|
||||||
|
@ -345,11 +352,11 @@ public sealed class ProtodecContext
|
||||||
return fieldType;
|
return fieldType;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Protobuf NewProtobuf(Type topLevelType, TopLevel topLevel)
|
private Protobuf NewProtobuf(Il2CppTypeDefinition topLevelType, TopLevel topLevel)
|
||||||
{
|
{
|
||||||
Protobuf protobuf = new()
|
Protobuf protobuf = new()
|
||||||
{
|
{
|
||||||
AssemblyName = topLevelType.Assembly.FullName,
|
AssemblyName = topLevelType.DeclaringAssembly!.Name!,
|
||||||
Namespace = topLevelType.Namespace
|
Namespace = topLevelType.Namespace
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -360,14 +367,14 @@ public sealed class ProtodecContext
|
||||||
return protobuf;
|
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
|
where T : TopLevel, INestableType
|
||||||
{
|
{
|
||||||
Protobuf protobuf;
|
Protobuf protobuf;
|
||||||
if (topLevelType.IsNested)
|
if (topLevelType.IsNested())
|
||||||
{
|
{
|
||||||
Type parent = topLevelType.DeclaringType!.DeclaringType!;
|
Il2CppTypeDefinition parent = topLevelType.DeclaringType!.DeclaringType!;
|
||||||
if (!_parsed.TryGetValue(parent.FullName ?? parent.Name, out TopLevel? parentTopLevel))
|
if (!_parsed.TryGetValue(parent.FullName!, out TopLevel? parentTopLevel))
|
||||||
{
|
{
|
||||||
parentTopLevel = ParseMessage(parent, options);
|
parentTopLevel = ParseMessage(parent, options);
|
||||||
}
|
}
|
||||||
|
@ -376,7 +383,7 @@ public sealed class ProtodecContext
|
||||||
topLevel.Protobuf = protobuf;
|
topLevel.Protobuf = protobuf;
|
||||||
topLevel.Parent = parentTopLevel;
|
topLevel.Parent = parentTopLevel;
|
||||||
|
|
||||||
((Message)parentTopLevel).Nested.Add(topLevelType.Name, topLevel);
|
((Message)parentTopLevel).Nested.Add(topLevelType.Name!, topLevel);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -421,25 +428,18 @@ public sealed class ProtodecContext
|
||||||
return translatedName!.ToSnakeCaseLower();
|
return translatedName!.ToSnakeCaseLower();
|
||||||
}
|
}
|
||||||
|
|
||||||
private string TranslateEnumFieldName(FieldInfo field, string enumName)
|
private string TranslateEnumFieldName(string fieldName, string enumName)
|
||||||
{
|
{
|
||||||
if (field.GetCustomAttributesData()
|
//TODO: parse original name from first parameter of OriginalNameAttribute constructor
|
||||||
.SingleOrDefault(static attr => attr.AttributeType.Name == "OriginalNameAttribute")
|
|
||||||
?.ConstructorArguments[0]
|
if (NameLookup?.Invoke(fieldName, out string? translatedName) == true)
|
||||||
.Value
|
|
||||||
is string originalName)
|
|
||||||
{
|
{
|
||||||
return originalName;
|
fieldName = translatedName;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (NameLookup?.Invoke(field.Name, out string? fieldName) != true)
|
if (!IsBeebyted(fieldName))
|
||||||
{
|
{
|
||||||
fieldName = field.Name;
|
fieldName = fieldName.ToSnakeCaseUpper();
|
||||||
}
|
|
||||||
|
|
||||||
if (!IsBeebyted(fieldName!))
|
|
||||||
{
|
|
||||||
fieldName = fieldName!.ToSnakeCaseUpper();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!IsBeebyted(enumName))
|
if (!IsBeebyted(enumName))
|
||||||
|
@ -450,21 +450,19 @@ public sealed class ProtodecContext
|
||||||
return enumName + '_' + fieldName;
|
return enumName + '_' + fieldName;
|
||||||
}
|
}
|
||||||
|
|
||||||
private string TranslateTypeName(Type type)
|
private string TranslateTypeName(Il2CppTypeDefinition type)
|
||||||
{
|
{
|
||||||
if (NameLookup is null)
|
if (NameLookup is null)
|
||||||
return type.Name;
|
return type.Name!;
|
||||||
|
|
||||||
string? fullName = type.FullName;
|
|
||||||
Guard.IsNotNull(fullName);
|
|
||||||
|
|
||||||
|
string fullName = type.FullName!;
|
||||||
int genericArgs = fullName.IndexOf('[');
|
int genericArgs = fullName.IndexOf('[');
|
||||||
if (genericArgs != -1)
|
if (genericArgs != -1)
|
||||||
fullName = fullName[..genericArgs];
|
fullName = fullName[..genericArgs];
|
||||||
|
|
||||||
if (!NameLookup(fullName, out string? translatedName))
|
if (!NameLookup(fullName, out string? translatedName))
|
||||||
{
|
{
|
||||||
return type.Name;
|
return type.Name!;
|
||||||
}
|
}
|
||||||
|
|
||||||
int lastSlash = translatedName.LastIndexOf('/');
|
int lastSlash = translatedName.LastIndexOf('/');
|
||||||
|
@ -478,7 +476,7 @@ public sealed class ProtodecContext
|
||||||
return translatedName;
|
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)
|
switch (type.FullName)
|
||||||
{
|
{
|
||||||
|
@ -642,13 +640,15 @@ public sealed class ProtodecContext
|
||||||
private static bool IsBeebyted(string name) =>
|
private static bool IsBeebyted(string name) =>
|
||||||
name.Length == 11 && name.CountUpper() == 11;
|
name.Length == 11 && name.CountUpper() == 11;
|
||||||
|
|
||||||
private static bool HasGeneratedCodeAttribute(IEnumerable<CustomAttributeData> attributes, string tool) =>
|
private static bool HasGeneratedCodeAttribute(IEnumerable<Il2CppTypeDefinition> attributeTypes, string tool)
|
||||||
attributes.Any(attr => attr.AttributeType.Name == nameof(GeneratedCodeAttribute)
|
{
|
||||||
&& attr.ConstructorArguments[0].Value as 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) =>
|
||||||
private static bool HasObsoleteAttribute(IEnumerable<CustomAttributeData> attributes) =>
|
attributeTypes.Any(static attrType => attrType.Name == nameof(DebuggerNonUserCodeAttribute));
|
||||||
attributes.Any(static attr => attr.AttributeType.Name == nameof(ObsoleteAttribute));
|
|
||||||
|
private static bool HasObsoleteAttribute(IEnumerable<Il2CppTypeDefinition> attributeTypes) =>
|
||||||
|
attributeTypes.Any(static attrType => attrType.Name == nameof(ObsoleteAttribute));
|
||||||
}
|
}
|
|
@ -3,14 +3,18 @@ using System.CodeDom.Compiler;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using AssetRipper.Primitives;
|
||||||
|
using LibCpp2IL.Metadata;
|
||||||
using LibProtodec;
|
using LibProtodec;
|
||||||
using LibProtodec.Models;
|
using LibProtodec.Models;
|
||||||
|
|
||||||
const string indent = " ";
|
const string indent = " ";
|
||||||
const string help = """
|
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:
|
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.
|
out_path An existing directory to output into individual files, otherwise output to a single file.
|
||||||
Options:
|
Options:
|
||||||
--parse_service_servers Parses gRPC service definitions from server classes.
|
--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.
|
--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);
|
Console.WriteLine(help);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
string assembly = args[0];
|
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;
|
ParserOptions options = ParserOptions.None;
|
||||||
|
|
||||||
if (args.Contains("--skip_enums"))
|
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"))
|
if (args.Contains("--include_service_methods_without_generated_code_attribute"))
|
||||||
options |= ParserOptions.IncludeServiceMethodsWithoutGeneratedCodeAttribute;
|
options |= ParserOptions.IncludeServiceMethodsWithoutGeneratedCodeAttribute;
|
||||||
|
|
||||||
using AssemblyInspector inspector = new(assembly);
|
using Il2CppLoader inspector = new(assembly, metadata, uVersion);
|
||||||
ProtodecContext ctx = new();
|
ProtodecContext ctx = new();
|
||||||
|
|
||||||
foreach (Type message in inspector.GetProtobufMessageTypes())
|
foreach (Il2CppTypeDefinition message in inspector.GetProtobufMessageTypes())
|
||||||
{
|
{
|
||||||
ctx.ParseMessage(message, options);
|
ctx.ParseMessage(message, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (args.Contains("--parse_service_servers"))
|
if (args.Contains("--parse_service_servers"))
|
||||||
{
|
{
|
||||||
foreach (Type service in inspector.GetProtobufServiceServerTypes())
|
foreach (Il2CppTypeDefinition service in inspector.GetProtobufServiceServerTypes())
|
||||||
{
|
{
|
||||||
ctx.ParseService(service, options);
|
ctx.ParseService(service, options);
|
||||||
}
|
}
|
||||||
|
@ -57,7 +63,7 @@ if (args.Contains("--parse_service_servers"))
|
||||||
|
|
||||||
if (args.Contains("--parse_service_clients"))
|
if (args.Contains("--parse_service_clients"))
|
||||||
{
|
{
|
||||||
foreach (Type service in inspector.GetProtobufServiceClientTypes())
|
foreach (Il2CppTypeDefinition service in inspector.GetProtobufServiceClientTypes())
|
||||||
{
|
{
|
||||||
ctx.ParseService(service, options);
|
ctx.ParseService(service, options);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue