Skip to content

Commit

Permalink
Prevent UnityEngine.Transform::SetAsLastSibling Error Spam
Browse files Browse the repository at this point in the history
  • Loading branch information
HerpDerpinstine committed Sep 3, 2024
1 parent feeb742 commit 0c1b8ac
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
27 changes: 23 additions & 4 deletions Dependencies/SupportModules/Component.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace MelonLoader.Support
internal class SM_Component : MonoBehaviour
{
private bool isQuitting;
private static bool hadError;

#if SM_Il2Cpp
private delegate bool SetAsLastSiblingDelegate(IntPtr transformptr);
Expand All @@ -26,13 +27,19 @@ static SM_Component()
{
#if SM_Il2Cpp
SetAsLastSiblingDelegateField = IL2CPP.ResolveICall<SetAsLastSiblingDelegate>("UnityEngine.Transform::SetAsLastSibling");
if (SetAsLastSiblingDelegateField == null)
throw new Exception("Unable to find Internal Call for UnityEngine.Transform::SetAsLastSibling");
#else
SetAsLastSiblingMethod = typeof(Transform).GetMethod("SetAsLastSibling", BindingFlags.Public | BindingFlags.Instance);
if (SetAsLastSiblingMethod == null)
throw new Exception("Unable to find Internal Call for UnityEngine.Transform::SetAsLastSibling");
#endif
}
catch (Exception ex)
{
hadError = true;
MelonLogger.Warning($"Exception while Getting Transform.SetAsLastSibling: {ex}");
MelonLogger.Warning("Melon Events might run before some MonoBehaviour Events");
}
}

Expand All @@ -54,13 +61,25 @@ internal static void Create()

private void SiblingFix()
{
if (hadError)
return;

try
{
#if SM_Il2Cpp
SetAsLastSiblingDelegateField(IL2CPP.Il2CppObjectBaseToPtrNotNull(gameObject.transform));
SetAsLastSiblingDelegateField(IL2CPP.Il2CppObjectBaseToPtrNotNull(transform));
SetAsLastSiblingDelegateField(IL2CPP.Il2CppObjectBaseToPtrNotNull(gameObject.transform));
SetAsLastSiblingDelegateField(IL2CPP.Il2CppObjectBaseToPtrNotNull(transform));
#else
SetAsLastSiblingMethod?.Invoke(gameObject.transform, new object[0]);
SetAsLastSiblingMethod?.Invoke(transform, new object[0]);
SetAsLastSiblingMethod?.Invoke(gameObject.transform, new object[0]);
SetAsLastSiblingMethod?.Invoke(transform, new object[0]);
#endif
}
catch (Exception ex)
{
hadError = true;
MelonLogger.Warning($"Exception while Invoking Transform.SetAsLastSibling: {ex}");
MelonLogger.Warning("Melon Events might run before some MonoBehaviour Events");
}
}

internal void Destroy()
Expand Down
6 changes: 3 additions & 3 deletions Dependencies/SupportModules/Il2Cpp/MonoEnumeratorWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

namespace MelonLoader.Support
{
internal class MonoEnumeratorWrapper : Il2CppSystem.Object /*, IEnumerator */
public class MonoEnumeratorWrapper : Il2CppSystem.Object /*, IEnumerator */
{
public unsafe static void Register()
internal unsafe static void Register()
=> ClassInjector.RegisterTypeInIl2Cpp<MonoEnumeratorWrapper>(new()
{
LogSuccess = true,
Expand All @@ -18,7 +18,7 @@ public MonoEnumeratorWrapper(IntPtr ptr) : base(ptr) { }
public MonoEnumeratorWrapper(IEnumerator _enumerator) : base(ClassInjector.DerivedConstructorPointer<MonoEnumeratorWrapper>())
{
ClassInjector.DerivedConstructorBody(this);
enumerator = _enumerator ?? throw new NullReferenceException("routine is null");;
enumerator = _enumerator ?? throw new NullReferenceException("routine is null");
}

public Il2CppSystem.Object /*IEnumerator.*/Current
Expand Down

0 comments on commit 0c1b8ac

Please sign in to comment.