Skip to content

Commit

Permalink
Improve handshake popup (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
js6pak authored Mar 2, 2024
1 parent 787b54b commit 014b6e3
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 13 deletions.
35 changes: 35 additions & 0 deletions Reactor/Networking/HandshakePopup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using Reactor.Utilities.UI;
using UnityEngine;

namespace Reactor.Networking;

internal static class HandshakePopup
{
public const string Message =
"""
This server doesn't support Reactor's modded handshake.
The lobbies shown could be incompatible with your current mods.
For more info see <link=https://reactor.gg/handshake>reactor.gg/handshake</link>
""";

private static ReactorPopup? _popup;

public static void Show()
{
if (_popup == null)
{
_popup = ReactorPopup.Create(nameof(HandshakePopup));
_popup.Background.transform.localPosition = new Vector3(0, 0.20f, 0);
_popup.Background.size = new Vector2(6.5f, 1.7f);
_popup.BackButton.transform.SetLocalY(-0.2f);
}

var message = ReactorConfig.HandshakePopupMessage.Value;
if (string.IsNullOrEmpty(message))
{
message = Message;
}

_popup.Show(message);
}
}
34 changes: 34 additions & 0 deletions Reactor/Networking/MakePublicDisallowedPopup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using Reactor.Utilities.UI;
using UnityEngine;

namespace Reactor.Networking;

internal static class MakePublicDisallowedPopup
{
public const string Message =
"""
You can't make public lobbies on servers that don't support modded handshake.
For more info see <link=https://reactor.gg/handshake>reactor.gg/handshake</link>
""";

private static ReactorPopup? _popup;

public static void Show()
{
if (_popup == null)
{
_popup = ReactorPopup.Create(nameof(MakePublicDisallowedPopup));
_popup.Background.transform.localPosition = new Vector3(0, 0.25f, 0);
_popup.Background.size = new Vector2(7.5f, 1.5f);
_popup.BackButton.transform.SetLocalY(-0.1f);
}

var message = ReactorConfig.MakePublicDisallowedPopupMessage.Value;
if (string.IsNullOrEmpty(message))
{
message = Message;
}

_popup.Show(message);
}
}
16 changes: 3 additions & 13 deletions Reactor/Networking/Patches/HttpPatches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
using Object = UnityEngine.Object;

namespace Reactor.Networking.Patches;

Expand Down Expand Up @@ -85,9 +84,9 @@ public static void Postfix(UnityWebRequest __instance, UnityWebRequestAsyncOpera
if (__instance.GetMethod() == UnityWebRequest.UnityWebRequestMethod.Get)
{
if (responseHeader == null)
if (responseHeader == null && ModList.IsAnyModRequiredOnAllClients)
{
DisconnectPopup.Instance.ShowCustom("This region doesn't support modded handshake.\nThe lobbies shown may not be compatible with your current mods.\nFor more info see https://reactor.gg/handshake");
HandshakePopup.Show();
}
}
Expand Down Expand Up @@ -118,16 +117,7 @@ public static void Postfix(GameStartManager __instance)
__instance.MakePublicButton.sprite = __instance.PrivateGameImage;

var onClick = __instance.MakePublicButton.GetComponent<PassiveButton>().OnClick = new Button.ButtonClickedEvent();
onClick.AddListener((Action) (() =>
{
var popup = Object.Instantiate(DiscordManager.Instance.discordPopup, Camera.main!.transform);
var background = popup.transform.Find("Background").GetComponent<SpriteRenderer>();
var size = background.size;
size.x *= 2.5f;
background.size = size;
popup.TextAreaTMP.fontSizeMin = 2;
popup.Show("You can't make public lobbies on regions that don't support modded handshake.\nFor more info see https://reactor.gg/handshake");
}));
onClick.AddListener((Action) MakePublicDisallowedPopup.Show);

if (AmongUsClient.Instance.AmHost && AmongUsClient.Instance.IsGamePublic)
{
Expand Down
7 changes: 7 additions & 0 deletions Reactor/ReactorConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,18 @@ namespace Reactor;
internal static class ReactorConfig
{
public const string FeaturesSection = "Features";
public const string MessagesSection = "Messages";

public static ConfigEntry<bool> ForceDisableServerAuthority { get; private set; } = null!;

public static ConfigEntry<string> HandshakePopupMessage { get; private set; } = null!;
public static ConfigEntry<string> MakePublicDisallowedPopupMessage { get; private set; } = null!;

public static void Bind(ConfigFile config)
{
ForceDisableServerAuthority = config.Bind(FeaturesSection, nameof(ForceDisableServerAuthority), false, "Enables the DisableServerAuthority flag even when no mods declare it");

HandshakePopupMessage = config.Bind(MessagesSection, nameof(HandshakePopupMessage), string.Empty);
MakePublicDisallowedPopupMessage = config.Bind(MessagesSection, nameof(MakePublicDisallowedPopupMessage), string.Empty);
}
}
58 changes: 58 additions & 0 deletions Reactor/Utilities/UI/ReactorPopup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using Il2CppInterop.Runtime.Attributes;
using Reactor.Utilities.Attributes;
using TMPro;
using UnityEngine;

namespace Reactor.Utilities.UI;

/// <summary>
/// Wrapper over <see cref="GenericPopup"/> that adds hyperlink and controller support.
/// </summary>
[RegisterInIl2Cpp]
internal sealed class ReactorPopup : MonoBehaviour
{
private readonly Il2CppSystem.Collections.Generic.List<SelectableHyperLink> _selectableHyperLinks = new();

public GenericPopup Popup { get; private set; } = null!;
public TextMeshPro TextArea { get; private set; } = null!;
public PassiveButton BackButton { get; private set; } = null!;
public SpriteRenderer Background { get; private set; } = null!;

[HideFromIl2Cpp]
public void Show(string text)
{
Popup.Show(text);

ControllerManager.Instance.OpenOverlayMenu(name, BackButton);

SelectableHyperLinkHelper.AddSelectableUiForHyperlinks(_selectableHyperLinks, name);
TextArea.text = SelectableHyperLinkHelper.DecomposeAnnouncementText(TextArea, _selectableHyperLinks, name, TextArea.text);
SelectableHyperLinkHelper.UpdateHyperlinkPositions(TextArea, _selectableHyperLinks, name);

ControllerManager.Instance.AddSelectableUiElement(BackButton, true);
}

public void OnDisable()
{
ControllerManager.Instance.CloseOverlayMenu(name);
}

[HideFromIl2Cpp]
public static ReactorPopup Create(string name)
{
var genericPopup = Instantiate(DiscordManager.Instance.discordPopup, Camera.main!.transform);
var gameObject = genericPopup.gameObject;
var reactorPopup = gameObject.AddComponent<ReactorPopup>();

reactorPopup.Popup = genericPopup;
reactorPopup.TextArea = genericPopup.TextAreaTMP;
reactorPopup.BackButton = gameObject.transform.Find("ExitGame").GetComponent<PassiveButton>();
reactorPopup.Background = gameObject.transform.Find("Background").GetComponent<SpriteRenderer>();

gameObject.name = name;
genericPopup.destroyOnClose = true;
reactorPopup.TextArea.fontSizeMin = 2;

return reactorPopup;
}
}

0 comments on commit 014b6e3

Please sign in to comment.