Skip to content

Commit

Permalink
Fixed BepInEx Popup crash
Browse files Browse the repository at this point in the history
  • Loading branch information
AuriRex committed Dec 21, 2023
1 parent 681e4e5 commit 7c0613a
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 2 deletions.
148 changes: 148 additions & 0 deletions TheArchive.IL2CPP/Features/Dev/PageRundownPopupManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
using System;
using System.Collections;
using System.Collections.Generic;
using TheArchive.Core.Attributes;
using TheArchive.Core.FeaturesAPI;
using TheArchive.Interfaces;
using TheArchive.Loader;
using UnityEngine;
using static TheArchive.Utilities.Utils;

namespace TheArchive.Features.Dev
{
[HideInModSettings]
[EnableFeatureByDefault]
[DisallowInGameToggle]
[RundownConstraint(RundownFlags.RundownFive, RundownFlags.Latest)]
internal class PageRundownPopupManager : Feature
{
public override string Name => "PopupQueue";

public override string Group => FeatureGroups.Dev;

public override string Description => "Popups, yay!";

public static new IArchiveLogger FeatureLogger { get; set; }

public override void Init()
{
LoaderWrapper.ClassInjector.RegisterTypeInIl2Cpp<ILoveWhenTheThingCrashesBecauseIdkYaaaaay>();
}

public class ILoveWhenTheThingCrashesBecauseIdkYaaaaay : MonoBehaviour
{
protected PopupMessage _0;
protected PopupMessage _1;
protected PopupMessage _2;
protected PopupMessage _3;
protected PopupMessage _4;
protected PopupMessage _5;
protected PopupMessage _6;
protected PopupMessage _7;
protected PopupMessage _8;
protected PopupMessage _9;

public const int MAX_POPUPS = 10;

private int _count = 0;

public ILoveWhenTheThingCrashesBecauseIdkYaaaaay(IntPtr ptr) : base(ptr) { }

// Idk why but it seems to even work above 10 messages lmaoo
public void MagicJankCache(PopupMessage popupMessage)
{
var field = typeof(ILoveWhenTheThingCrashesBecauseIdkYaaaaay).GetField($"_{_count}", AnyBindingFlagss);

if (field == null)
throw new InvalidOperationException("Please fix this, thanks!");

field.SetValue(this, popupMessage);

_count++;
if(_count >= MAX_POPUPS)
{
_count = 0;
}
}
}

private static readonly Queue<PopupMessage> _popupQueue = new Queue<PopupMessage>();
private static ILoveWhenTheThingCrashesBecauseIdkYaaaaay _cache;

public static void ShowPopup(PopupMessage popupMessage)
{
if(MainMenuGuiLayer.Current?.PageRundownNew == null)
{
FeatureLogger.Error("Called too early!");
return;
}

if (!MainMenuGuiLayer.Current.PageRundownNew.isActiveAndEnabled)
{
var pageRD = MainMenuGuiLayer.Current.PageRundownNew.gameObject;

var enabledListener = pageRD.GetComponent<ModSettings.OnEnabledListener>();

if (enabledListener == null)
{
enabledListener = pageRD.AddComponent<ModSettings.OnEnabledListener>();
_cache = pageRD.AddComponent<ILoveWhenTheThingCrashesBecauseIdkYaaaaay>();

enabledListener.OnEnabledSelf += PageRundownEnabled;
}

_cache.MagicJankCache(popupMessage);
_popupQueue.Enqueue(popupMessage);
return;
}

try
{
GlobalPopupMessageManager.ShowPopup(popupMessage);
}
catch (Exception ex)
{
FeatureLogger.Error("Failed to show single popup.");
FeatureLogger.Exception(ex);
}
}

private static bool _runningAllPopups = false;
private static void PageRundownEnabled(GameObject go)
{
if (_popupQueue.Count <= 0)
return;

if (_runningAllPopups)
return;

LoaderWrapper.StartCoroutine(ShowAllPopups());
}

private static IEnumerator ShowAllPopups()
{
if (_runningAllPopups)
yield break;

_runningAllPopups = true;
yield return new WaitForSeconds(0.1f);

while (_popupQueue.Count > 0)
{
try
{
var popupMessage = _popupQueue.Dequeue();

GlobalPopupMessageManager.ShowPopup(popupMessage);
}
catch (Exception ex)
{
FeatureLogger.Error("Failed to show popup.");
FeatureLogger.Exception(ex);
}
}

_runningAllPopups = false;
}
}
}
4 changes: 3 additions & 1 deletion TheArchive.IL2CPP/Features/Dev/UpdateNotifier.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using TheArchive.Core.Attributes;
using TheArchive.Core.Attributes.Feature.Settings;
Expand Down Expand Up @@ -81,6 +82,7 @@ public override void OnButtonPressed(ButtonSetting setting)
{
UpdateChecker.CheckForUpdate((releaseInfo) =>
{
MainMenuGuiLayer.Current.ChangePage(eCM_MenuPage.CMP_RUNDOWN_NEW);
ShowUpdatesPopup(showUpToDate: true);
});
}
Expand Down Expand Up @@ -140,7 +142,7 @@ public static void ShowUpdatesPopup(bool showUpToDate = false)
updateText = $"{updateText}\n\n<size=80%><color=orange>(This can be turned off in mod settings!)</color>\n[Mod Settings] > [{FeatureGroups.ArchiveCore.Name}] > [{nameof(UpdateNotifier)}]</size>";
}

GlobalPopupMessageManager.ShowPopup(new PopupMessage()
PageRundownPopupManager.ShowPopup(new PopupMessage()
{
BlinkInContent = true,
BlinkTimeInterval = 0.2f,
Expand Down
3 changes: 2 additions & 1 deletion TheArchive.IL2CPP/Features/Special/RundownEightReminder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using TheArchive.Core.Attributes;
using TheArchive.Core.Attributes.Feature.Settings;
using TheArchive.Core.FeaturesAPI;
using TheArchive.Features.Dev;
using TheArchive.Features.QoL;
using TheArchive.Loader;
using UnityEngine;
Expand Down Expand Up @@ -89,7 +90,7 @@ public static void ShowReminderPopup()

var updateText = $"Hi, quick reminder that you have <color=orange>{noStoryDialog.Name}</color> <#0f0>enabled</color> and that <color=orange><b>might impact your Rundown 8 experience</b></color>!\n\nConsider turning it <#F00>off</color> in mod settings <color=orange>if you care about the Story</color>!\n\nYou can find it under:\n<color=orange>[Mod Settings] > [{noStoryDialog.Group}] > [{noStoryDialog.Name}]</color>";

GlobalPopupMessageManager.ShowPopup(new PopupMessage()
PageRundownPopupManager.ShowPopup(new PopupMessage()
{
BlinkInContent = true,
BlinkTimeInterval = 0.2f,
Expand Down

0 comments on commit 7c0613a

Please sign in to comment.