feat: in-game error display

This commit is contained in:
anavoi 2025-07-12 23:39:20 +02:00
commit b4ffbc0a0d
2 changed files with 103 additions and 13 deletions

View file

@ -1,3 +1,5 @@
using System;
using System.IO;
using BepInEx;
using CoreNet.Config;
using GB.Config;
@ -13,8 +15,11 @@ namespace GBSU.Addons;
public class GBSUGui : MonoBehaviour
{
private bool menu_shown;
private bool error_shown;
//private string _currentMap;
public Rect gmenu = new(Screen.width / 2, 0, 385f, 690f);
public Rect error_dialog = new(Screen.width / 2, 0, 520f, 175f);
private string error_msg = "Unknown error!";
readonly IInputSystem inputSystem = UnityInput.Current;
string serverip = null;
@ -84,23 +89,43 @@ Set CLI arguments: -ip, -port, -servername, -serverpassword
{
if (LobbyManager.Instance.LobbyStates.SelfState == LobbyState.Game.Menu)
{
hosting = true;
AudioListener.volume = 0; // mute game audio
if (File.Exists(Helper.RotationFolderPath + "config.json"))
{
hosting = true;
Plugin.AddServerComp(); // add custom GBSU server component
AudioListener.volume = 0; // mute game audio
RotationConfig gameConfig = GBConfigLoader.LoadRotationConfig("config.json", true); // load rotation config from Config/Rotation/config.json
ServerConfig serverConfig = NetConfigLoader.LoadServerConfig(); // load default server config, because it can be overridden by args like -ip and -port
MonoSingleton<Global>.Instance.UNetManager.LaunchServer(serverConfig); // launch the server with the server config
MonoSingleton<Global>.Instance.UNetManager.GetComponent<GameManagerNew>().ChangeRotationConfig(gameConfig, 0); // set server's rotationconfig
Plugin.AddServerComp(); // add custom GBSU server component
try
{
RotationConfig gameConfig = GBConfigLoader.LoadRotationConfig("config.json", true); // load rotation config from Config/Rotation/config.json
ServerConfig serverConfig = NetConfigLoader.LoadServerConfig(); // load default server config, because it can be overridden by args like -ip and -port
MonoSingleton<Global>.Instance.UNetManager.LaunchServer(serverConfig); // launch the server with the server config
MonoSingleton<Global>.Instance.UNetManager.GetComponent<GameManagerNew>().ChangeRotationConfig(gameConfig, 0); // set server's rotationconfig
}
catch (Exception e)
{
PushError("Looks like you've caught a bug! Please send your log file to us :)\n" + e);
}
}
else
{
PushError(@$"No config.json could be found in {Helper.RotationFolderPath}
Make sure to download a file from the examples given and rename it to config.json.
{Helper.FilesInRotationDir()}");
}
}
else
{
PushError("Please stay on the main menu to begin hosting. Tip: You might need to exit your lobby.");
}
}
if (GUI.Button(new Rect(195f, 260f, 170f, 30f), "Join"))
{
if (LobbyManager.Instance.LobbyStates.SelfState == LobbyState.Game.Online)
{
if (!hosting && serverip != null && serverport != 0)
if (!hosting)
{
LobbyManager.Instance.LobbyStates.IP = serverip;
LobbyManager.Instance.LobbyStates.Port = serverport;
@ -108,10 +133,22 @@ Set CLI arguments: -ip, -port, -servername, -serverpassword
LobbyManager.Instance.LocalBeasts.SetupNetMemberContext(true);
MonoSingleton<Global>.Instance.UNetManager.LaunchClient(serverip, serverport);
}
else
{
PushError("You are currently hosting a match! Please restart the game before attempting to join.");
}
}
else
{
PushError("Failed to join lobby! Please select the Online option in-game before joining.");
}
}
}
else
{
PushError("Couldn't find the -ip CLI argument. Please refer to the documentation.");
}
if (GUI.Button(new Rect(20f, 295f, 170f, 30f), "Cap FPS to 60"))
{
Application.targetFrameRate = 60;
@ -133,7 +170,7 @@ Set CLI arguments: -ip, -port, -servername, -serverpassword
QualitySettings.vSyncCount = vsync_switch;
}
GUI.Label(new Rect(20f, 365f, 365f, 400f), $@"
Current map: {currentmap}
@ -151,6 +188,19 @@ Please refer to the documentation for more information.");
GUI.DragWindow(new Rect(0, 0, 10000, 10000));
}
private void ShowError(int window)
{
GUI.skin.label.alignment = TextAnchor.MiddleCenter;
GUILayout.Label(error_msg);
if (GUI.Button(new Rect(420f, 135f, 85f, 30f), "Close"))
{
error_shown = false;
}
GUI.DragWindow(new Rect(0, 0, 10000, 10000));
}
private string UpdateScoreDisplay()
{
if (hosting)
@ -165,11 +215,25 @@ Please refer to the documentation for more information.");
return null;
}
private void PushError(string message)
{
Plugin.Log.LogError(message);
// Push this error to the UI
error_msg = message;
error_shown = true;
}
public void OnGUI()
{
if (menu_shown)
{
gmenu = GUILayout.Window(121, gmenu, ShowOurWindow, $"{Plugin.PluginName} [{Plugin.PluginVersion}]");
}
if (error_shown)
{
error_dialog = GUILayout.Window(122, error_dialog, ShowError, "An error occurred!");
}
}
}

View file

@ -1,4 +1,6 @@
using System;
using System.IO;
using UnityEngine;
using UnityEngine.Analytics;
namespace GBSU.Addons;
@ -31,4 +33,28 @@ public class Helper
Plugin.Log.LogError("Could not create rotation folder path: " + e.Message);
}
}
public static string FilesInRotationDir()
{
// https://stackoverflow.com/a/14877330
DirectoryInfo d = new(RotationFolderPath);
FileInfo[] files = d.GetFiles();
int number = files.Length;
// no files?
if (number == 0)
{
return @"No files were found in the directory.";
}
else
{
string names = "";
foreach(FileInfo file in files)
{
names = file.Name + " " + names;
}
return $"There are {number} files: {names}";
}
}
}