Skip to content
This repository has been archived by the owner on Jul 1, 2024. It is now read-only.

Commit

Permalink
Fix crashes on Unity 2020.1+, update changelog
Browse files Browse the repository at this point in the history
  • Loading branch information
knah committed Jun 13, 2021
1 parent 56362da commit 120cd1a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 18 deletions.
14 changes: 3 additions & 11 deletions ReleaseChangelog.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
This is a small bugfix update. Generated assemblies should be mostly compatible with those generated by 0.4.14.0.
Some external tools that relied on native struct definitions might break though - some structs were moved to used the wrapper model.
This is a small bugfix update. Generated assemblies should be mostly compatible with those generated by 0.4.15.1.
Changes:
* Generic structs are now always (correctly) generated as non-blittable
* Rewrote native struct handling to enable easy support of different Unity versions.
* This release should, in theory, support all Unity versions from 2017.1.0 to 2020.2.x and perhaps even above. However, bugs are still a thing.
* The base of this was contributed by @ghorsington in #35
* Some fixes and improvements here were contributed by @ds5678 (#39)
* Added `Il2CppObjectBase.WasCollected` (contributed by @ds5678 in #37)
* Added basic support for loading custom components (of injected types) from assetbundles (contributed by @ds5678 in #38)
* Added an overload of `Il2CppType.Of` method that doesn't throw exceptions for non-found types (contributed by @ds5678 in #36)
* Field writes now use `il2cpp_gc_wbarrier_set_field` if it exists in GameAssembly
* Added `ClassInjectionAssemblyTargetAttribute` (contributed by @ds5678 in #49)
* Fixed crashes on Unity 2020.1.x and later

7 changes: 4 additions & 3 deletions UnhollowerBaseLib/ClassInjector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ namespace UnhollowerRuntimeLib
{
public unsafe static class ClassInjector
{
private static readonly Il2CppAssembly* FakeAssembly;
private static readonly INativeImageStruct FakeImage;
private static Il2CppAssembly* FakeAssembly;
private static INativeImageStruct FakeImage;

/// <summary> type.FullName </summary>
private static readonly HashSet<string> InjectedTypes = new HashSet<string>();
/// <summary> namespace, class, image, pointer </summary>
private static readonly Dictionary<(string, string, IntPtr), IntPtr> ClassFromNameDictionary = new Dictionary<(string, string, IntPtr), IntPtr>();

static ClassInjector()
static void CreateFakeAssembly()
{
FakeAssembly = (Il2CppAssembly*) Marshal.AllocHGlobal(Marshal.SizeOf<Il2CppAssembly>());
FakeImage = UnityVersionHandler.NewImage(); //(Il2CppImage*) Marshal.AllocHGlobal(Marshal.SizeOf<Il2CppImage>());
Expand Down Expand Up @@ -104,6 +104,7 @@ public static void RegisterTypeInIl2Cpp(Type type, bool logSuccess)

if (ourOriginalTypeToClassMethod == null) HookClassFromType();
if (originalClassFromNameMethod == null) HookClassFromName();
if (FakeAssembly == null) CreateFakeAssembly();

var classPointer = UnityVersionHandler.NewClass(baseClassPointer.VtableCount);

Expand Down
22 changes: 18 additions & 4 deletions UnhollowerBaseLib/Runtime/VersionSpecific/Image/Images_27.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@ public unsafe class NativeImageStructHandler_27 : INativeImageStructHandler
{
public INativeImageStruct CreateNewImageStruct()
{
var pointer = Marshal.AllocHGlobal(Marshal.SizeOf<Il2CppImageU2019>());
var pointer = (Il2CppImageU2019*) Marshal.AllocHGlobal(Marshal.SizeOf<Il2CppImageU2019>());
var metadataPointer = (Il2CppImageGlobalMetadata*) Marshal.AllocHGlobal(Marshal.SizeOf<Il2CppImageGlobalMetadata>());

*(Il2CppImageU2019*)pointer = default;
*pointer = default;
*metadataPointer = default;
pointer->metadataHandle = metadataPointer;
metadataPointer->image = pointer;

return new NativeImageStruct(pointer);
return new NativeImageStruct((IntPtr) pointer);
}

public INativeImageStruct Wrap(Il2CppImage* imagePointer)
Expand All @@ -32,7 +36,7 @@ private struct Il2CppImageU2019
public uint exportedTypeCount;
public uint customAttributeCount;

public IntPtr metadataHandle;
public Il2CppImageGlobalMetadata* metadataHandle;

public /*Il2CppNameToTypeDefinitionIndexHashTable **/ IntPtr nameToClassHashTable;
public IntPtr codeGenModule;
Expand All @@ -41,6 +45,16 @@ private struct Il2CppImageU2019
public byte dynamic;
}

[StructLayout(LayoutKind.Sequential)]
private struct Il2CppImageGlobalMetadata
{
public int typeStart;
public int exportedTypeStart;
public int customAttributeStart;
public int entryPointIndex;
public Il2CppImageU2019* image;
}

private class NativeImageStruct : INativeImageStruct
{
public NativeImageStruct(IntPtr pointer)
Expand Down

0 comments on commit 120cd1a

Please sign in to comment.