Compare commits

..

1 Commits

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

View File

@ -13,7 +13,7 @@ Usage: protodec(.exe) <game_assembly_path> <global_metadata_path> <unity_version
Arguments:
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.
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.

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

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

View File

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

View File

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

@ -1,66 +0,0 @@
// Copyright © 2024 Xpl0itR
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
using System.Collections.Generic;
using System.Linq;
using AssetRipper.Primitives;
using CommunityToolkit.Diagnostics;
using LibCpp2IL;
using LibCpp2IL.Metadata;
using LibCpp2IL.Reflection;
using LibProtodec.Models.Cil;
using LibProtodec.Models.Cil.Il2Cpp;
namespace LibProtodec.Loaders;
public sealed class Il2CppAssemblyLoader : ICilAssemblyLoader
{
public Il2CppAssemblyLoader(string assemblyPath, string metadataPath, UnityVersion unityVersion)
{
if (!LibCpp2IlMain.LoadFromFile(assemblyPath, metadataPath, unityVersion))
ThrowHelper.ThrowInvalidDataException("Failed to load il2cpp assembly!");
LoadedTypes = LibCpp2IlMain.TheMetadata!.typeDefs.Select(Il2CppType.GetOrCreate).ToList();
}
public IReadOnlyList<ICilType> LoadedTypes { get; }
public ICilType IMessage
{
get
{
Il2CppTypeDefinition? iMessage = LibCpp2IlReflection.GetTypeByFullName("Google.Protobuf.IMessage");
Guard.IsNotNull(iMessage);
return Il2CppType.GetOrCreate(iMessage);
}
}
public ICilType ClientBase
{
get
{
Il2CppTypeDefinition? clientBase = LibCpp2IlReflection.GetTypeByFullName("Grpc.Core.ClientBase");
Guard.IsNotNull(clientBase);
return Il2CppType.GetOrCreate(clientBase);
}
}
public ICilType BindServiceMethodAttribute
{
get
{
Il2CppTypeDefinition? attribute = LibCpp2IlReflection.GetTypeByFullName("Grpc.Core.BindServiceMethodAttribute");
Guard.IsNotNull(attribute);
return Il2CppType.GetOrCreate(attribute);
}
}
public void Dispose() =>
LibCpp2IlMain.Reset();
}

View File

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

View File

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

View File

@ -1,33 +0,0 @@
// Copyright © 2024 Xpl0itR
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
using System.Collections.Generic;
using System.Reflection;
namespace LibProtodec.Models.Cil.Clr;
public abstract class ClrMember(MemberInfo clrMember)
{
public string Name =>
clrMember.Name;
public bool IsInherited =>
clrMember.DeclaringType != clrMember.ReflectedType;
public ICilType? DeclaringType =>
clrMember.DeclaringType is null
? null
: ClrType.GetOrCreate(
clrMember.DeclaringType);
public IEnumerable<ICilAttribute> GetCustomAttributes()
{
foreach (CustomAttributeData attribute in clrMember.GetCustomAttributesData())
{
yield return new ClrAttribute(attribute);
}
}
}

View File

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

View File

@ -1,32 +0,0 @@
// Copyright © 2024 Xpl0itR
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
using System.Reflection;
namespace LibProtodec.Models.Cil.Clr;
public sealed class ClrProperty(PropertyInfo clrProperty) : ClrMember(clrProperty), ICilProperty
{
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

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,20 +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 LibCpp2IL.Reflection;
namespace LibProtodec.Models.Cil.Il2Cpp;
public sealed class Il2CppAttribute(Il2CppTypeReflectionData il2CppAttrType) : ICilAttribute
{
public ICilType Type =>
Il2CppType.GetOrCreate(il2CppAttrType);
public IList<object?> ConstructorArguments =>
throw new NotImplementedException();
}

View File

@ -1,40 +0,0 @@
// Copyright © 2024 Xpl0itR
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
using System.Reflection;
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

@ -1,47 +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 LibCpp2IL;
using LibCpp2IL.Metadata;
namespace LibProtodec.Models.Cil.Il2Cpp;
public abstract class Il2CppMember
{
public IEnumerable<ICilAttribute> GetCustomAttributes()
{
if (LibCpp2IlMain.MetadataVersion >= 29)
{
throw new NotImplementedException();
}
Il2CppCustomAttributeTypeRange? attributeTypeRange = LibCpp2IlMain.TheMetadata!.GetCustomAttributeData(
DeclaringAssembly, CustomAttributeIndex, Token, out _);
if (attributeTypeRange is null || attributeTypeRange.count == 0)
yield break;
for (int attrIndex = attributeTypeRange.start,
end = attributeTypeRange.start + attributeTypeRange.count;
attrIndex < end;
attrIndex++)
{
int typeIndex = LibCpp2IlMain.TheMetadata.attributeTypes[attrIndex];
yield return new Il2CppAttribute(
LibCpp2ILUtils.GetTypeReflectionData(
LibCpp2IlMain.Binary!.GetType(typeIndex)));
}
}
protected abstract Il2CppImageDefinition DeclaringAssembly { get; }
protected abstract int CustomAttributeIndex { get; }
protected abstract uint Token { get; }
}

View File

@ -1,54 +0,0 @@
// Copyright © 2024 Xpl0itR
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
using System.Collections.Generic;
using System.Reflection;
using 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 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

@ -1,54 +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.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

@ -1,194 +0,0 @@
// Copyright © 2024 Xpl0itR
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
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 IList<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)
{
if (_genericArgs.Length < 1)
{
_genericTypeArguments = Array.Empty<ICilType>();
}
else
{
_genericTypeArguments = _genericArgs.Select(GetOrCreate).ToList();
}
}
return _genericTypeArguments;
}
}
public IEnumerable<ICilField> GetFields()
{
for (int idx = _il2CppType.FirstFieldIdx, end = _il2CppType.FirstFieldIdx + _il2CppType.FieldCount; idx < end; idx++)
{
yield return new Il2CppField(
LibCpp2IlMain.TheMetadata!.fieldDefs[idx]);
}
}
public IEnumerable<ICilMethod> GetMethods()
{
for (int idx = _il2CppType.FirstMethodIdx, end = _il2CppType.FirstMethodIdx + _il2CppType.MethodCount; idx < end; idx++)
{
yield return new Il2CppMethod(
LibCpp2IlMain.TheMetadata!.methodDefs[idx]);
}
}
public IEnumerable<ICilType> GetNestedTypes()
{
for (int idx = _il2CppType.NestedTypesStart, end = _il2CppType.NestedTypesStart + _il2CppType.NestedTypeCount; idx < end; idx++)
{
yield return GetOrCreate(
LibCpp2IlMain.TheMetadata!.typeDefs[
LibCpp2IlMain.TheMetadata.nestedTypeIndices[idx]]);
}
}
public IEnumerable<ICilProperty> GetProperties()
{
for (int idx = _il2CppType.FirstPropertyId, end = _il2CppType.FirstPropertyId + _il2CppType.PropertyCount; idx < end; idx++)
{
yield return new Il2CppProperty(
LibCpp2IlMain.TheMetadata!.propertyDefs[idx],
_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

@ -4,7 +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/.
namespace LibProtodec.Models.Protobuf.Fields;
namespace LibProtodec.Models.Fields;
public sealed class EnumField
{

View File

@ -5,16 +5,16 @@
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
using System.IO;
using LibProtodec.Models.Protobuf.TopLevels;
using LibProtodec.Models.Protobuf.Types;
using LibProtodec.Models.TopLevels;
using LibProtodec.Models.Types;
namespace LibProtodec.Models.Protobuf.Fields;
namespace LibProtodec.Models.Fields;
public sealed class MessageField
{
public required IProtobufType Type { get; init; }
public required string Name { get; init; }
public required int Id { get; init; }
public required IType Type { get; init; }
public required string Name { get; init; }
public required int Id { get; init; }
public bool IsObsolete { get; init; }
public bool HasHasProp { get; init; }

View File

@ -5,16 +5,16 @@
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
using System.CodeDom.Compiler;
using LibProtodec.Models.Protobuf.TopLevels;
using LibProtodec.Models.Protobuf.Types;
using LibProtodec.Models.TopLevels;
using LibProtodec.Models.Types;
namespace LibProtodec.Models.Protobuf.Fields;
namespace LibProtodec.Models.Fields;
public sealed class ServiceMethod
{
public required string Name { get; init; }
public required IProtobufType RequestType { get; init; }
public required IProtobufType ResponseType { get; init; }
public required string Name { get; init; }
public required IType RequestType { get; init; }
public required IType ResponseType { get; init; }
public bool IsRequestStreamed { get; init; }
public bool IsResponseStreamed { get; init; }

View File

@ -8,10 +8,10 @@ using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using LibProtodec.Models.Protobuf.TopLevels;
using LibProtodec.Models.Protobuf.Types;
using LibProtodec.Models.TopLevels;
using LibProtodec.Models.Types;
namespace LibProtodec.Models.Protobuf;
namespace LibProtodec.Models;
public sealed class Protobuf
{
@ -90,7 +90,7 @@ public sealed class Protobuf
writer.WriteLine(';');
}
public static void WriteTypeNameTo(TextWriter writer, IProtobufType type, TopLevel topLevel)
public static void WriteTypeNameTo(TextWriter writer, IType type, TopLevel topLevel)
{
if (type is TopLevel { Parent: not null } typeTopLevel && typeTopLevel.Parent != topLevel)
{

View File

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

View File

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

View File

@ -4,13 +4,13 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
global using Enum = LibProtodec.Models.Protobuf.TopLevels.Enum;
global using Enum = LibProtodec.Models.TopLevels.Enum;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using LibProtodec.Models.Protobuf.Fields;
using LibProtodec.Models.Protobuf.Types;
using LibProtodec.Models.Fields;
using LibProtodec.Models.Types;
namespace LibProtodec.Models.Protobuf.TopLevels;
namespace LibProtodec.Models.TopLevels;
public sealed class Enum : TopLevel, INestableType
{

View File

@ -7,10 +7,10 @@
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Linq;
using LibProtodec.Models.Protobuf.Fields;
using LibProtodec.Models.Protobuf.Types;
using LibProtodec.Models.Fields;
using LibProtodec.Models.Types;
namespace LibProtodec.Models.Protobuf.TopLevels;
namespace LibProtodec.Models.TopLevels;
public sealed class Message : TopLevel, INestableType
{
@ -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))

View File

@ -6,9 +6,9 @@
using System.CodeDom.Compiler;
using System.Collections.Generic;
using LibProtodec.Models.Protobuf.Fields;
using LibProtodec.Models.Fields;
namespace LibProtodec.Models.Protobuf.TopLevels;
namespace LibProtodec.Models.TopLevels;
public sealed class Service : TopLevel
{

View File

@ -7,7 +7,7 @@
using System.CodeDom.Compiler;
using System.Collections.Generic;
namespace LibProtodec.Models.Protobuf.TopLevels;
namespace LibProtodec.Models.TopLevels;
public abstract class TopLevel
{

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -10,21 +10,25 @@ using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection;
using SystemEx;
using CommunityToolkit.Diagnostics;
using LibProtodec.Models.Cil;
using LibProtodec.Models.Protobuf;
using LibProtodec.Models.Protobuf.Fields;
using LibProtodec.Models.Protobuf.TopLevels;
using LibProtodec.Models.Protobuf.Types;
using LibCpp2IL.Metadata;
using LibCpp2IL.Reflection;
using LibProtodec.Models;
using LibProtodec.Models.Fields;
using LibProtodec.Models.TopLevels;
using LibProtodec.Models.Types;
namespace LibProtodec;
public delegate bool TypeLookupFunc(ICilType cilType, [NotNullWhen(true)] out IProtobufType? protobufType);
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 FieldAttributes PublicStaticLiteral = FieldAttributes.Public | FieldAttributes.Static | FieldAttributes.Literal;
private readonly Dictionary<string, TopLevel> _parsed = [];
public readonly List<Protobuf> Protobufs = [];
@ -49,11 +53,11 @@ public sealed class ProtodecContext
}
}
public Message ParseMessage(ICilType 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, out TopLevel? parsedMessage))
if (_parsed.TryGetValue(messageClass.FullName!, out TopLevel? parsedMessage))
{
return (Message)parsedMessage;
}
@ -61,39 +65,40 @@ public sealed class ProtodecContext
Message message = new()
{
Name = TranslateTypeName(messageClass),
IsObsolete = HasObsoleteAttribute(messageClass.GetCustomAttributes())
IsObsolete = HasObsoleteAttribute(messageClass.GetCustomAttributeTypes())
};
_parsed.Add(messageClass.FullName, message);
_parsed.Add(messageClass.FullName!, message);
Protobuf protobuf = GetProtobuf(messageClass, message, options);
List<ICilField> idFields = messageClass.GetFields()
.Where(static field => field is { IsPublic: true, IsStatic: true, IsLiteral: true })
.ToList();
List<Il2CppFieldDefinition> idFields = messageClass.GetFields(PublicStaticLiteral).ToList();
Il2CppPropertyDefinition[] properties = messageClass.Properties!;
List<ICilProperty> properties = messageClass.GetProperties()
.Where(static property => property is { IsInherited: false, CanRead: true, Getter: { IsPublic: true, IsStatic: false, IsVirtual: false } })
.ToList();
for (int pi = 0, fi = 0; pi < properties.Count; pi++)
for (int pi = 0, fi = 0; pi < properties.Length; pi++)
{
ICilProperty property = properties[pi];
List<ICilAttribute> attributes = property.GetCustomAttributes().ToList();
Il2CppPropertyDefinition property = properties[pi];
if (!property.CanRead()
|| property.Getter!.IsStatic
|| property.Getter!.IsVirtual()
|| property.Getter!.IsNonPublic())
{
continue;
}
List<Il2CppTypeDefinition> attributes = property.GetCustomAttributeTypes().ToList();
if (((options & ParserOptions.IncludePropertiesWithoutNonUserCodeAttribute) == 0 && !HasNonUserCodeAttribute(attributes)))
{
continue;
}
ICilType propertyType = property.Type;
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);
List<int> oneOfProtoFieldIds = propertyType.GetFields()
.Where(static field => field.IsLiteral)
.Select(static field => (int)field.ConstantValue!)
string oneOfName = TranslateOneOfPropName(property.Name!);
List<int> oneOfProtoFieldIds = propertyType.GetFields(PublicStaticLiteral)
.Select(static field => (int)field.DefaultValue!.Value!)
.Where(static id => id > 0)
.ToList();
@ -101,8 +106,10 @@ public sealed class ProtodecContext
continue;
}
Il2CppFieldDefinition idField = idFields[fi];
bool msgFieldHasHasProp = false; // some field properties are immediately followed by an additional "Has" get-only boolean property
if (properties.Count > pi + 1 && properties[pi + 1].Type.Name == nameof(Boolean) && !properties[pi + 1].CanWrite)
if (properties.Length > pi + 1 && properties[pi + 1].PropertyType!.baseType!.Name == nameof(Boolean) && !properties[pi + 1].CanWrite())
{
msgFieldHasHasProp = true;
pi++;
@ -110,9 +117,9 @@ public sealed class ProtodecContext
MessageField field = new()
{
Type = ParseFieldType(propertyType, options, protobuf),
Name = TranslateMessageFieldName(property.Name),
Id = (int)idFields[fi].ConstantValue!,
Type = ParseFieldType(property.PropertyType!, options, protobuf),
Name = TranslateMessageFieldName(property.Name!),
Id = (int)idField.DefaultValue!.Value!,
IsObsolete = HasObsoleteAttribute(attributes),
HasHasProp = msgFieldHasHasProp
};
@ -124,34 +131,32 @@ public sealed class ProtodecContext
return message;
}
public Enum ParseEnum(ICilType 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, out TopLevel? parsedEnum))
if (_parsed.TryGetValue(enumEnum.FullName!, out TopLevel? parsedEnum))
{
return (Enum)parsedEnum;
}
Enum @enum = new()
{
Name = TranslateTypeName(enumEnum),
IsObsolete = HasObsoleteAttribute(enumEnum.GetCustomAttributes())
Name = TranslateTypeName(enumEnum),
IsObsolete = HasObsoleteAttribute(enumEnum.GetCustomAttributeTypes())
};
_parsed.Add(enumEnum.FullName, @enum);
_parsed.Add(enumEnum.FullName!, @enum);
Protobuf protobuf = GetProtobuf(enumEnum, @enum, options);
foreach (ICilField field in enumEnum.GetFields().Where(static field => field.IsLiteral))
foreach (Il2CppFieldDefinition field in enumEnum.GetFields(PublicStaticLiteral))
{
List<ICilAttribute> attributes = field.GetCustomAttributes().ToList();
@enum.Fields.Add(
new EnumField
{
Id = (int)field.ConstantValue!,
Name = TranslateEnumFieldName(attributes, field.Name, @enum.Name),
IsObsolete = HasObsoleteAttribute(attributes)
Id = (int)field.DefaultValue!.Value!,
Name = TranslateEnumFieldName(field.Name!, @enum.Name),
IsObsolete = HasObsoleteAttribute(field.GetCustomAttributeTypes())
});
}
@ -164,34 +169,34 @@ public sealed class ProtodecContext
return @enum;
}
public Service ParseService(ICilType 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())
{
List<ICilType> nested = serviceClass.GetNestedTypes().ToList();
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, out TopLevel? parsedService))
if (_parsed.TryGetValue(serviceClass.DeclaringType!.FullName!, out TopLevel? parsedService))
{
return (Service)parsedService;
}
@ -199,54 +204,59 @@ public sealed class ProtodecContext
Service service = new()
{
Name = TranslateTypeName(serviceClass.DeclaringType),
IsObsolete = HasObsoleteAttribute(serviceClass.GetCustomAttributes())
IsObsolete = HasObsoleteAttribute(serviceClass.GetCustomAttributeTypes())
};
_parsed.Add(serviceClass.DeclaringType!.FullName, service);
_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 (Il2CppMethodDefinition method in serviceClass.Methods!)
{
List<ICilAttribute> attributes = method.GetCustomAttributes().ToList();
if (method.IsNonPublic() || method.IsStatic)
{
continue;
}
List<Il2CppTypeDefinition> attributes = method.GetCustomAttributeTypes().ToList();
if ((options & ParserOptions.IncludeServiceMethodsWithoutGeneratedCodeAttribute) == 0
&& !HasGeneratedCodeAttribute(attributes, "grpc_csharp_plugin"))
{
continue;
}
ICilType 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;
}
List<ICilType> parameters = method.GetParameterTypes().ToList();
if (parameters.Count > 2)
if (method.Parameters!.Length > 2)
{
continue;
}
switch (returnType.GenericTypeArguments.Count)
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 = parameters[0];
responseType = returnType.GenericTypeArguments[0];
requestType = firstParamType;
responseType = returnType.genericParams[0];
streamReq = false;
streamRes = true;
break;
default:
requestType = parameters[0];
requestType = firstParamType;
responseType = returnType;
streamReq = false;
streamRes = false;
@ -255,35 +265,34 @@ public sealed class ProtodecContext
}
else
{
List<ICilType> parameters = method.GetParameterTypes().ToList();
if (parameters[0].GenericTypeArguments.Count == 1)
Il2CppTypeReflectionData firstParamType = method.Parameters![0].Type;
if (firstParamType.genericParams.Length == 1)
{
streamReq = true;
requestType = parameters[0].GenericTypeArguments[0];
requestType = firstParamType.genericParams[0];
}
else
{
streamReq = false;
requestType = parameters[0];
requestType = firstParamType;
}
if (returnType.GenericTypeArguments.Count == 1)
if (returnType.genericParams.Length == 1)
{
streamRes = false;
responseType = returnType.GenericTypeArguments[0];
responseType = returnType.genericParams[0];
}
else
{
streamRes = true;
responseType = parameters[1].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),
@ -295,58 +304,59 @@ public sealed class ProtodecContext
return service;
}
private IProtobufType ParseFieldType(ICilType type, ParserOptions options, Protobuf referencingProtobuf)
private IType ParseFieldType(Il2CppTypeReflectionData reflectionData, ParserOptions options, Protobuf referencingProtobuf)
{
switch (type.GenericTypeArguments.Count)
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));
}
if (!TypeLookup(type, out IProtobufType? fieldType))
{
if (type.IsEnum)
{
if ((options & ParserOptions.SkipEnums) > 0)
{
return Scalar.Int32;
}
Il2CppTypeDefinition type = reflectionData.baseType!;
fieldType = ParseEnum(type, options);
}
else
if (TypeLookup(type, out IType? fieldType, out string? import))
{
if (import is not null)
{
fieldType = ParseMessage(type, options);
referencingProtobuf.Imports.Add(import);
}
return fieldType;
}
switch (fieldType)
if (type.IsEnumType)
{
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;
if ((options & ParserOptions.SkipEnums) > 0)
{
return Scalar.Int32;
}
fieldType = ParseEnum(type, options);
}
else
{
fieldType = ParseMessage(type, options);
}
Protobuf protobuf = ((INestableType)fieldType).Protobuf!;
if (referencingProtobuf != protobuf)
{
referencingProtobuf.Imports.Add(protobuf.FileName);
}
return fieldType;
}
private Protobuf NewProtobuf(ICilType topLevelType, TopLevel topLevel)
private Protobuf NewProtobuf(Il2CppTypeDefinition topLevelType, TopLevel topLevel)
{
Protobuf protobuf = new()
{
AssemblyName = topLevelType.DeclaringAssemblyName,
AssemblyName = topLevelType.DeclaringAssembly!.Name!,
Namespace = topLevelType.Namespace
};
@ -357,14 +367,14 @@ public sealed class ProtodecContext
return protobuf;
}
private Protobuf GetProtobuf<T>(ICilType 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())
{
ICilType parent = topLevelType.DeclaringType!.DeclaringType!;
if (!_parsed.TryGetValue(parent.FullName, out TopLevel? parentTopLevel))
Il2CppTypeDefinition parent = topLevelType.DeclaringType!.DeclaringType!;
if (!_parsed.TryGetValue(parent.FullName!, out TopLevel? parentTopLevel))
{
parentTopLevel = ParseMessage(parent, options);
}
@ -373,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
{
@ -418,7 +428,7 @@ public sealed class ProtodecContext
return translatedName!.ToSnakeCaseLower();
}
private string TranslateEnumFieldName(IEnumerable<ICilAttribute> attributes, string fieldName, string enumName)
private string TranslateEnumFieldName(string fieldName, string enumName)
{
//TODO: parse original name from first parameter of OriginalNameAttribute constructor
@ -440,19 +450,19 @@ public sealed class ProtodecContext
return enumName + '_' + fieldName;
}
private string TranslateTypeName(ICilType type)
private string TranslateTypeName(Il2CppTypeDefinition type)
{
if (NameLookup is null)
return type.Name;
return type.Name!;
string fullName = type.FullName;
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('/');
@ -466,123 +476,162 @@ public sealed class ProtodecContext
return translatedName;
}
public static bool LookupScalarAndWellKnownTypes(ICilType cilType, [NotNullWhen(true)] out IProtobufType? protobufType)
public static bool LookupScalarAndWellKnownTypes(Il2CppTypeDefinition type, [NotNullWhen(true)] out IType? fieldType, out string? import)
{
switch (cilType.FullName)
switch (type.FullName)
{
case "System.String":
protobufType = Scalar.String;
import = null;
fieldType = Scalar.String;
return true;
case "System.Boolean":
protobufType = Scalar.Bool;
import = null;
fieldType = Scalar.Bool;
return true;
case "System.Double":
protobufType = Scalar.Double;
import = null;
fieldType = Scalar.Double;
return true;
case "System.UInt32":
protobufType = Scalar.UInt32;
import = null;
fieldType = Scalar.UInt32;
return true;
case "System.UInt64":
protobufType = Scalar.UInt64;
import = null;
fieldType = Scalar.UInt64;
return true;
case "System.Int32":
protobufType = Scalar.Int32;
import = null;
fieldType = Scalar.Int32;
return true;
case "System.Int64":
protobufType = Scalar.Int64;
import = null;
fieldType = Scalar.Int64;
return true;
case "System.Single":
protobufType = Scalar.Float;
import = null;
fieldType = Scalar.Float;
return true;
case "Google.Protobuf.ByteString":
protobufType = Scalar.Bytes;
import = null;
fieldType = Scalar.Bytes;
return true;
case "Google.Protobuf.WellKnownTypes.Any":
protobufType = WellKnown.Any;
import = "google/protobuf/any.proto";
fieldType = WellKnown.Any;
return true;
case "Google.Protobuf.WellKnownTypes.Api":
protobufType = WellKnown.Api;
import = "google/protobuf/api.proto";
fieldType = WellKnown.Api;
return true;
case "Google.Protobuf.WellKnownTypes.BoolValue":
protobufType = WellKnown.BoolValue;
import = "google/protobuf/wrappers.proto";
fieldType = WellKnown.BoolValue;
return true;
case "Google.Protobuf.WellKnownTypes.BytesValue":
protobufType = WellKnown.BytesValue;
import = "google/protobuf/wrappers.proto";
fieldType = WellKnown.BytesValue;
return true;
case "Google.Protobuf.WellKnownTypes.DoubleValue":
protobufType = WellKnown.DoubleValue;
import = "google/protobuf/wrappers.proto";
fieldType = WellKnown.DoubleValue;
return true;
case "Google.Protobuf.WellKnownTypes.Duration":
protobufType = WellKnown.Duration;
import = "google/protobuf/duration.proto";
fieldType = WellKnown.Duration;
return true;
case "Google.Protobuf.WellKnownTypes.Empty":
protobufType = WellKnown.Empty;
import = "google/protobuf/empty.proto";
fieldType = WellKnown.Empty;
return true;
case "Google.Protobuf.WellKnownTypes.Enum":
protobufType = WellKnown.Enum;
import = "google/protobuf/type.proto";
fieldType = WellKnown.Enum;
return true;
case "Google.Protobuf.WellKnownTypes.EnumValue":
protobufType = WellKnown.EnumValue;
import = "google/protobuf/type.proto";
fieldType = WellKnown.EnumValue;
return true;
case "Google.Protobuf.WellKnownTypes.Field":
protobufType = WellKnown.Field;
import = "google/protobuf/type.proto";
fieldType = WellKnown.Field;
return true;
case "Google.Protobuf.WellKnownTypes.FieldMask":
protobufType = WellKnown.FieldMask;
import = "google/protobuf/field_mask.proto";
fieldType = WellKnown.FieldMask;
return true;
case "Google.Protobuf.WellKnownTypes.FloatValue":
protobufType = WellKnown.FloatValue;
import = "google/protobuf/wrappers.proto";
fieldType = WellKnown.FloatValue;
return true;
case "Google.Protobuf.WellKnownTypes.Int32Value":
protobufType = WellKnown.Int32Value;
import = "google/protobuf/wrappers.proto";
fieldType = WellKnown.Int32Value;
return true;
case "Google.Protobuf.WellKnownTypes.Int64Value":
protobufType = WellKnown.Int64Value;
import = "google/protobuf/wrappers.proto";
fieldType = WellKnown.Int64Value;
return true;
case "Google.Protobuf.WellKnownTypes.ListValue":
protobufType = WellKnown.ListValue;
import = "google/protobuf/struct.proto";
fieldType = WellKnown.ListValue;
return true;
case "Google.Protobuf.WellKnownTypes.Method":
protobufType = WellKnown.Method;
import = "google/protobuf/api.proto";
fieldType = WellKnown.Method;
return true;
case "Google.Protobuf.WellKnownTypes.Mixin":
protobufType = WellKnown.Mixin;
import = "google/protobuf/api.proto";
fieldType = WellKnown.Mixin;
return true;
case "Google.Protobuf.WellKnownTypes.NullValue":
protobufType = WellKnown.NullValue;
import = "google/protobuf/struct.proto";
fieldType = WellKnown.NullValue;
return true;
case "Google.Protobuf.WellKnownTypes.Option":
protobufType = WellKnown.Option;
import = "google/protobuf/type.proto";
fieldType = WellKnown.Option;
return true;
case "Google.Protobuf.WellKnownTypes.SourceContext":
protobufType = WellKnown.SourceContext;
import = "google/protobuf/source_context.proto";
fieldType = WellKnown.SourceContext;
return true;
case "Google.Protobuf.WellKnownTypes.StringValue":
protobufType = WellKnown.StringValue;
import = "google/protobuf/wrappers.proto";
fieldType = WellKnown.StringValue;
return true;
case "Google.Protobuf.WellKnownTypes.Struct":
protobufType = WellKnown.Struct;
import = "google/protobuf/struct.proto";
fieldType = WellKnown.Struct;
return true;
case "Google.Protobuf.WellKnownTypes.Syntax":
protobufType = WellKnown.Syntax;
import = "google/protobuf/type.proto";
fieldType = WellKnown.Syntax;
return true;
case "Google.Protobuf.WellKnownTypes.Timestamp":
protobufType = WellKnown.Timestamp;
import = "google/protobuf/timestamp.proto";
fieldType = WellKnown.Timestamp;
return true;
case "Google.Protobuf.WellKnownTypes.Type":
protobufType = WellKnown.Type;
import = "google/protobuf/type.proto";
fieldType = WellKnown.Type;
return true;
case "Google.Protobuf.WellKnownTypes.UInt32Value":
protobufType = WellKnown.UInt32Value;
import = "google/protobuf/wrappers.proto";
fieldType = WellKnown.UInt32Value;
return true;
case "Google.Protobuf.WellKnownTypes.UInt64Value":
protobufType = WellKnown.UInt64Value;
import = "google/protobuf/wrappers.proto";
fieldType = WellKnown.UInt64Value;
return true;
case "Google.Protobuf.WellKnownTypes.Value":
protobufType = WellKnown.Value;
import = "google/protobuf/struct.proto";
fieldType = WellKnown.Value;
return true;
default:
protobufType = null;
import = null;
fieldType = null;
return false;
}
}
@ -591,15 +640,15 @@ public sealed class ProtodecContext
private static bool IsBeebyted(string name) =>
name.Length == 11 && name.CountUpper() == 11;
private static bool HasGeneratedCodeAttribute(IEnumerable<ICilAttribute> attributes, string tool)
private static bool HasGeneratedCodeAttribute(IEnumerable<Il2CppTypeDefinition> attributeTypes, string tool)
{
return attributes.Any(attr => attr.Type.Name == nameof(GeneratedCodeAttribute));
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<ICilAttribute> attributes) =>
attributes.Any(static attr => attr.Type.Name == nameof(DebuggerNonUserCodeAttribute));
private static bool HasNonUserCodeAttribute(IEnumerable<Il2CppTypeDefinition> attributeTypes) =>
attributeTypes.Any(static attrType => attrType.Name == nameof(DebuggerNonUserCodeAttribute));
private static bool HasObsoleteAttribute(IEnumerable<ICilAttribute> attributes) =>
attributes.Any(static attr => attr.Type.Name == nameof(ObsoleteAttribute));
private static bool HasObsoleteAttribute(IEnumerable<Il2CppTypeDefinition> attributeTypes) =>
attributeTypes.Any(static attrType => attrType.Name == nameof(ObsoleteAttribute));
}

View File

@ -1,20 +1,12 @@
// 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;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using AssetRipper.Primitives;
using LibCpp2IL;
using LibCpp2IL.Metadata;
using LibProtodec;
using LibProtodec.Loaders;
using LibProtodec.Models.Cil;
using LibProtodec.Models.Protobuf;
using LibProtodec.Models;
const string indent = " ";
const string help = """
@ -22,7 +14,7 @@ const string help = """
Arguments:
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.
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.
@ -40,7 +32,7 @@ if (args.Length < 4)
string assembly = args[0];
string metadata = args[1];
string uVersion = args[2];
UnityVersion uVersion = UnityVersion.Parse(args[2]);
string outPath = Path.GetFullPath(args[3]);
ParserOptions options = ParserOptions.None;
@ -53,26 +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;
if (!UnityVersion.TryParse(uVersion, out UnityVersion unityVersion, out _))
{
unityVersion = uVersion.EndsWith("globalgamemanagers")
? LibCpp2IlMain.GetVersionFromGlobalGameManagers(
File.ReadAllBytes(uVersion))
: LibCpp2IlMain.GetVersionFromDataUnity3D(
File.OpenRead(uVersion));
}
using ICilAssemblyLoader loader = new Il2CppAssemblyLoader(assembly, metadata, unityVersion);
using Il2CppLoader inspector = new(assembly, metadata, uVersion);
ProtodecContext ctx = new();
foreach (ICilType message in GetProtobufMessageTypes())
foreach (Il2CppTypeDefinition message in inspector.GetProtobufMessageTypes())
{
ctx.ParseMessage(message, options);
}
if (args.Contains("--parse_service_servers"))
{
foreach (ICilType service in GetProtobufServiceServerTypes())
foreach (Il2CppTypeDefinition service in inspector.GetProtobufServiceServerTypes())
{
ctx.ParseService(service, options);
}
@ -80,7 +63,7 @@ if (args.Contains("--parse_service_servers"))
if (args.Contains("--parse_service_clients"))
{
foreach (ICilType service in GetProtobufServiceClientTypes())
foreach (Il2CppTypeDefinition service in inspector.GetProtobufServiceClientTypes())
{
ctx.ParseService(service, options);
}
@ -114,20 +97,4 @@ else
using IndentedTextWriter indentWriter = new(streamWriter, indent);
ctx.WriteAllTo(indentWriter);
}
IEnumerable<ICilType> GetProtobufMessageTypes() =>
loader.LoadedTypes.Where(
type => type is { IsNested: false, IsSealed: true }
&& type.Namespace?.StartsWith("Google.Protobuf", StringComparison.Ordinal) != true
&& type.IsAssignableTo(loader.IMessage));
IEnumerable<ICilType> GetProtobufServiceClientTypes() =>
loader.LoadedTypes.Where(
type => type is { IsNested: true, IsAbstract: false }
&& type.IsAssignableTo(loader.ClientBase));
IEnumerable<ICilType> GetProtobufServiceServerTypes() =>
loader.LoadedTypes.Where(
type => type is { IsNested: true, IsAbstract: true, DeclaringType: { IsNested: false, IsSealed: true, IsAbstract: true } }
&& type.GetCustomAttributes().Any(attribute => attribute.Type == loader.BindServiceMethodAttribute));
}