Skip to content

Commit

Permalink
Set AssemblyDefinition::PublicKey when generating AsmResolver assemblies
Browse files Browse the repository at this point in the history
  • Loading branch information
ds5678 committed Dec 11, 2024
1 parent edbb994 commit 158ed40
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 4 deletions.
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!.GetBytesFromIndex(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 @@ -426,6 +426,18 @@ public string GetStringFromIndex(int 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[] GetBytesFromIndex(int index)
{
var offset = metadataHeader.stringOffset + index;
var count = ReadUnityCompressedUIntAtRawAddr(offset, out var bytesRead);
return ReadByteArrayAtRawAddress(offset + bytesRead, (int)count);
}

internal string ReadStringFromIndexNoReadLock(int index)
{
if (!_cachedStrings.ContainsKey(index))
Expand Down

0 comments on commit 158ed40

Please sign in to comment.