GBSU/Addons/GBSUServer.cs
anavoi 34d9e9261e feat: added "menu" panic button
This avoids restarting the game when encountering an unknown issue.
2025-07-14 00:14:11 +02:00

74 lines
2.3 KiB
C#

using CoreNet.Config;
using GB.Config;
using GB.Core;
using GB.Game;
using HarmonyLib;
using UnityEngine;
#pragma warning disable IDE0051 // Private member is unused
namespace GBSU.Addons;
public class GBSUServer : MonoBehaviour
{
//public MethodInfo _gmInit;
private Traverse localSingleGang;
private Traverse gameTimer;
void FixedUpdate()
{
SetLocalGangToOff(); // make sure correct multiplayer logic applies
}
void Awake()
{
Plugin.Log.LogInfo("GBSU Server awaken");
SetLocalGangToOff();
//_gmInit = AccessTools.Method(typeof(GameMode), "Init");
localSingleGang = Traverse.Create(nameof(GameMode)).Field("localSingleGang");
gameTimer = Traverse.Create(nameof(GameMode)).Field("timer");
}
void SetLocalGangToOff()
{
localSingleGang?.SetValue(false);
}
public static void StartServer()
{
if (!Helper.hosting)
{
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
Helper.SwitchToMenu();
}
/*
public string GetRemainingRoundTime()
{
float t = (float)gameTimer.GetValue();
return t.ToString("0.00"); // 2 decimal places https://stackoverflow.com/a/164932
}
*/
}