feat: start/stop server handling

The original app volume is also restored when the server is stopped.
This commit is contained in:
anavoi 2025-07-13 23:47:40 +02:00
commit 5eedbf2f1e
4 changed files with 35 additions and 7 deletions

View file

@ -67,20 +67,14 @@ Set CLI arguments: -ip, -port (optionally -maplist)
{
if (File.Exists(Helper.GameConfigPath))
{
Helper.hosting = true;
AudioListener.volume = 0; // mute game audio
try
{
Plugin.AddServerComp(); // add custom GBSU server component
GBSUServer.StartServer();
}
catch (Exception e)
{
PushError("Looks like you've caught a bug! Please send your log file to us :)\n" + e);
Helper.hosting = false;
Plugin.DestroyServerComp();
GBSUServer.StopServer();
}
}
else
@ -131,6 +125,13 @@ Make sure to download a file from the examples given and rename it to config.jso
{
Helper.FlipVSync();
}
if (Helper.hosting)
{
if (GUI.Button(new Rect(195f, 330f, 170f, 30f), "Stop server"))
{
GBSUServer.StopServer();
}
}
GUI.Label(new Rect(20f, 365f, 365f, 400f), $@"

View file

@ -4,6 +4,7 @@ using GB.Core;
using GB.Game;
using HarmonyLib;
using UnityEngine;
using UnityEngine.SceneManagement;
#pragma warning disable IDE0051 // Private member is unused
@ -34,11 +35,33 @@ public class GBSUServer : MonoBehaviour
public static void StartServer()
{
Helper.hosting = true;
AudioListener.volume = 0; // mute game audio
Plugin.AddServerComp(); // add custom GBSU server component
RotationConfig gameConfig = GBConfigLoader.LoadRotationConfig(null); // the argument doesn't matter as we do stuff in the method
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
}
public static void StopServer()
{
// WIP: At the moment the only way to stop the server is to exit/kill the game
Helper.hosting = false;
AudioListener.volume = Helper.saved_volume; // restore volume
// stop network listener
MonoSingleton<Global>.Instance.UNetManager.StopServer();
// destroy GBSU server comp
Plugin.DestroyServerComp();
// go back to main menu
SceneManager.LoadScene("Menu");
}
/*
public string GetRemainingRoundTime()
{

View file

@ -11,6 +11,7 @@ public class Helper
public static string serverip = null;
public static int serverport = 5999;
public static bool hosting = false;
public static float saved_volume;
public static void DisableAnalytics()
{
// Try disabling analytics https://docs.unity3d.com/ScriptReference/Analytics.Analytics-deviceStatsEnabled.html