Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set AssemblyDefinition::PublicKey when generating AsmResolver assemblies #390

Merged
merged 3 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions Cpp2IL.Core/OutputFormats/AsmResolverDummyDllOutputFormat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,10 @@ private static AssemblyDefinition BuildStubAssembly(AssemblyAnalysisContext asse

var ourAssembly = new AssemblyDefinition(assemblyNameString, version)
{
HashAlgorithm = (AssemblyHashAlgorithm)assemblyDefinition.AssemblyName.hash_alg, Attributes = (AssemblyAttributes)assemblyDefinition.AssemblyName.flags, Culture = assemblyDefinition.AssemblyName.Culture,
//TODO find a way to set hash? or not needed
HashAlgorithm = (AssemblyHashAlgorithm)assemblyDefinition.AssemblyName.hash_alg,
Attributes = (AssemblyAttributes)assemblyDefinition.AssemblyName.flags,
Culture = assemblyDefinition.AssemblyName.Culture,
PublicKey = assemblyDefinition.AssemblyName.PublicKey,
};

//Setting the corlib module allows element types in references to that assembly to be set correctly without us having to manually set them.
Expand Down
3 changes: 2 additions & 1 deletion LibCpp2IL/ClassReadingBinaryReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,8 @@ protected internal byte[] ReadByteArrayAtRawAddressNoLock(long offset, int count
if (offset != -1)
Position = offset;

var initialPos = Position;
if (count == 0)
return [];

try
{
Expand Down
23 changes: 22 additions & 1 deletion LibCpp2IL/Metadata/Il2CppAssemblyNameDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,28 @@ public class Il2CppAssemblyNameDefinition : ReadableClass
public string Name => LibCpp2IlMain.TheMetadata!.GetStringFromIndex(nameIndex);
public string Culture => LibCpp2IlMain.TheMetadata!.GetStringFromIndex(cultureIndex);

public string PublicKey => LibCpp2IlMain.TheMetadata!.GetStringFromIndex(publicKeyIndex);
public byte[]? PublicKey
{
get
{
var result = LibCpp2IlMain.TheMetadata!.GetByteArrayFromIndex(publicKeyIndex);
return result.Length == 0 ? null : result;
}
}

/// <summary>
/// This returns the public key token as a byte array, or null if the token is 0.
/// </summary>
/// <remarks>
/// Returning null is necessary to match the behavior of AsmResolver.
/// </remarks>
public byte[]? PublicKeyToken
{
get
{
return publicKeyToken == default ? null : BitConverter.GetBytes(publicKeyToken);
}
}

public string HashValue => LibCpp2IlMain.MetadataVersion > 24.3f ? "NULL" : LibCpp2IlMain.TheMetadata!.GetStringFromIndex(hashValueIndex);

Expand Down
12 changes: 12 additions & 0 deletions LibCpp2IL/Metadata/Il2CppMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,18 @@ public int GetDefaultValueFromIndex(int index)
return metadataHeader.fieldAndParameterDefaultValueDataOffset + index;
}

/// <summary>
/// Read a byte array from the string data section of the metadata.
/// </summary>
/// <param name="index">The offset relative to the start of the string section.</param>
/// <returns>The </returns>
public byte[] GetByteArrayFromIndex(int index)
{
var offset = metadataHeader.stringOffset + index;
var count = ReadUnityCompressedUIntAtRawAddr(offset, out var bytesRead);
return ReadByteArrayAtRawAddress(offset + bytesRead, (int)count);
}

private ConcurrentDictionary<int, string> _cachedStrings = new ConcurrentDictionary<int, string>();

public string GetStringFromIndex(int index)
Expand Down
Loading