using System;
using System.Collections;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using Photon.Pun;
using TMPro;
using UnboundLib;
using UnboundLib.GameModes;
using UnboundLib.Utils.UI;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.SceneManagement;
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: CompilationRelaxations(8)]
[assembly: AssemblyVersion("0.0.0.0")]
namespace DKyzaButtons_RestartButton;
[BepInProcess("Rounds.exe")]
[BepInPlugin("com.dkyza.rounds.buttons.restartbutton", "DKyzaButtons_RestartButton", "1.0.5")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
[BepInDependency(/*Could not decode attribute arguments.*/)]
public sealed class HostMenuPlugin : BaseUnityPlugin
{
public const string ModId = "com.dkyza.rounds.buttons.restartbutton";
public const string ModName = "DKyzaButtons_RestartButton";
public const string ModVersion = "1.0.5";
private static HostMenuPlugin instance;
private GameObject restartButton;
private GameObject lobbyButton;
private TextMeshProUGUI restartText;
private TextMeshProUGUI lobbyText;
private bool restartArmed;
private bool lobbyArmed;
private float restartArmUntil;
private float lobbyArmUntil;
private void Awake()
{
instance = this;
}
private void Start()
{
SceneManager.sceneLoaded += OnSceneLoaded;
((MonoBehaviour)this).StartCoroutine(ButtonWatcher());
((BaseUnityPlugin)this).Logger.LogInfo((object)"DKyzaButtons_RestartButton loaded.");
}
private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
restartButton = null;
lobbyButton = null;
restartText = null;
lobbyText = null;
restartArmed = false;
lobbyArmed = false;
}
private IEnumerator ButtonWatcher()
{
while (true)
{
TryCreateButtons();
UpdateButtonVisibility();
UpdateConfirmationTimers();
yield return (object)new WaitForSecondsRealtime(0.2f);
}
}
private bool IsCurrentClientHost()
{
try
{
return PhotonNetwork.InRoom && PhotonNetwork.IsMasterClient && GameModeManager.CurrentHandler != null;
}
catch
{
return false;
}
}
private void TryCreateButtons()
{
//IL_00a7: Unknown result type (might be due to invalid IL or missing references)
//IL_00ca: Expected O, but got Unknown
//IL_0138: Unknown result type (might be due to invalid IL or missing references)
//IL_015b: Expected O, but got Unknown
if (((Object)(object)restartButton != (Object)null && (Object)(object)lobbyButton != (Object)null) || (Object)(object)UIHandler.instance == (Object)null)
{
return;
}
Transform val = ((Component)UIHandler.instance).transform.Find("Canvas/EscapeMenu/Main");
if ((Object)(object)val == (Object)null)
{
return;
}
Transform val2 = val.Find("Group");
if ((Object)(object)val2 == (Object)null)
{
return;
}
if ((Object)(object)restartButton == (Object)null)
{
Transform val3 = val2.Find("DKyzaButtons_Restart");
if ((Object)(object)val3 != (Object)null)
{
restartButton = ((Component)val3).gameObject;
}
else
{
restartButton = MenuHandler.CreateButton("RESTART", ((Component)val2).gameObject, new UnityAction(OnRestartPressed), 60, true, (Color?)null, (TMP_FontAsset)null, (Material)null, (TextAlignmentOptions?)null);
((Object)restartButton).name = "DKyzaButtons_Restart";
}
restartText = restartButton.GetComponentInChildren<TextMeshProUGUI>(true);
}
if ((Object)(object)lobbyButton == (Object)null)
{
Transform val4 = val2.Find("DKyzaButtons_BackToLobby");
if ((Object)(object)val4 != (Object)null)
{
lobbyButton = ((Component)val4).gameObject;
}
else
{
lobbyButton = MenuHandler.CreateButton("BACK TO LOBBY", ((Component)val2).gameObject, new UnityAction(OnLobbyPressed), 60, true, (Color?)null, (TMP_FontAsset)null, (Material)null, (TextAlignmentOptions?)null);
((Object)lobbyButton).name = "DKyzaButtons_BackToLobby";
}
lobbyText = lobbyButton.GetComponentInChildren<TextMeshProUGUI>(true);
}
if ((Object)(object)restartButton != (Object)null)
{
restartButton.transform.SetAsLastSibling();
}
if ((Object)(object)lobbyButton != (Object)null)
{
lobbyButton.transform.SetAsLastSibling();
}
ResetButtonLabels();
}
private void UpdateButtonVisibility()
{
bool flag = IsCurrentClientHost();
if ((Object)(object)restartButton != (Object)null && restartButton.activeSelf != flag)
{
restartButton.SetActive(flag);
}
if ((Object)(object)lobbyButton != (Object)null && lobbyButton.activeSelf != flag)
{
lobbyButton.SetActive(flag);
}
if (!flag)
{
restartArmed = false;
lobbyArmed = false;
ResetButtonLabels();
}
}
private void UpdateConfirmationTimers()
{
float unscaledTime = Time.unscaledTime;
if (restartArmed && unscaledTime > restartArmUntil)
{
restartArmed = false;
SetText(restartText, "RESTART");
}
if (lobbyArmed && unscaledTime > lobbyArmUntil)
{
lobbyArmed = false;
SetText(lobbyText, "BACK TO LOBBY");
}
}
private void OnRestartPressed()
{
if (IsCurrentClientHost())
{
if (!restartArmed)
{
restartArmed = true;
lobbyArmed = false;
restartArmUntil = Time.unscaledTime + 4f;
SetText(restartText, "CLICK AGAIN: RESTART");
SetText(lobbyText, "BACK TO LOBBY");
}
else
{
restartArmed = false;
((MonoBehaviour)this).StartCoroutine(SendRoundsWithFriendsAction("Rematch"));
}
}
}
private void OnLobbyPressed()
{
if (IsCurrentClientHost())
{
if (!lobbyArmed)
{
lobbyArmed = true;
restartArmed = false;
lobbyArmUntil = Time.unscaledTime + 4f;
SetText(lobbyText, "CLICK AGAIN: LOBBY");
SetText(restartText, "RESTART");
}
else
{
lobbyArmed = false;
((MonoBehaviour)this).StartCoroutine(SendRoundsWithFriendsAction("Exit"));
}
}
}
private static void SetText(TextMeshProUGUI text, string value)
{
if ((Object)(object)text != (Object)null)
{
((TMP_Text)text).text = value;
}
}
private void ResetButtonLabels()
{
SetText(restartText, "RESTART");
SetText(lobbyText, "BACK TO LOBBY");
}
private static void CloseEscapeMenu()
{
Time.timeScale = 1f;
GameManager.lockInput = false;
if ((Object)(object)UIHandler.instance == (Object)null)
{
return;
}
Transform val = ((Component)UIHandler.instance).transform.Find("Canvas/EscapeMenu");
if ((Object)(object)val != (Object)null)
{
((Component)val).gameObject.SetActive(false);
return;
}
Transform val2 = ((Component)UIHandler.instance).transform.Find("Canvas/EscapeMenu/Main");
if ((Object)(object)val2 != (Object)null)
{
((Component)val2).gameObject.SetActive(false);
}
}
private IEnumerator SendRoundsWithFriendsAction(string methodName)
{
if (!IsCurrentClientHost())
{
yield break;
}
CloseEscapeMenu();
yield return null;
yield return (object)new WaitForSecondsRealtime(0.1f);
Type roundEndHandler = FindLoadedType("RWF.RoundEndHandler");
if (roundEndHandler == null)
{
((BaseUnityPlugin)this).Logger.LogError((object)"RoundsWithFriends RoundEndHandler was not found.");
yield break;
}
try
{
NetworkingManager.RPC(roundEndHandler, methodName, new object[0]);
((BaseUnityPlugin)this).Logger.LogInfo((object)("Sent RoundsWithFriends RPC: " + methodName));
}
catch (Exception ex)
{
((BaseUnityPlugin)this).Logger.LogError((object)("Failed to send RoundsWithFriends RPC " + methodName + ": " + ex));
}
}
private static Type FindLoadedType(string fullTypeName)
{
Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
for (int i = 0; i < assemblies.Length; i++)
{
Type type = assemblies[i].GetType(fullTypeName, throwOnError: false);
if (type != null)
{
return type;
}
}
return null;
}
private void OnDestroy()
{
SceneManager.sceneLoaded -= OnSceneLoaded;
if ((Object)(object)restartButton != (Object)null)
{
Object.Destroy((Object)(object)restartButton);
}
if ((Object)(object)lobbyButton != (Object)null)
{
Object.Destroy((Object)(object)lobbyButton);
}
if ((Object)(object)instance == (Object)(object)this)
{
instance = null;
}
}
}