Compare commits

...

3 commits

Author SHA1 Message Date
4771a41fa7 refactor: unused import 2025-07-13 21:49:21 +02:00
20ca020d02 fix: remove hosting status if it fails 2025-07-13 16:17:37 +02:00
0cb7131435 refactor: wip 2025-07-13 16:16:41 +02:00
5 changed files with 70 additions and 45 deletions

17
Addons/GBSUClient.cs Normal file
View file

@ -0,0 +1,17 @@
using GB.Core;
using GB.Platform.Lobby;
using UnityEngine;
namespace GBSU.Addons;
public class GBSUClient : MonoBehaviour
{
public static void JoinServer(string serverip, int serverport)
{
LobbyManager.Instance.LobbyStates.IP = serverip;
LobbyManager.Instance.LobbyStates.Port = serverport;
LobbyManager.Instance.LobbyStates.CurrentState = LobbyState.State.Ready | LobbyState.State.InGame;
LobbyManager.Instance.LocalBeasts.SetupNetMemberContext(true);
MonoSingleton<Global>.Instance.UNetManager.LaunchClient(serverip, serverport);
}
}

View file

@ -1,10 +1,6 @@
using System;
using System.IO;
using BepInEx;
using CoreNet.Config;
using GB.Config;
using GB.Core;
using GB.Game;
using GB.Platform.Lobby;
using UnityEngine;
@ -22,20 +18,6 @@ public class GBSUGui : MonoBehaviour
private string error_msg = "Unknown error!";
readonly IInputSystem inputSystem = UnityInput.Current;
string serverip = null;
int serverport = 5999;
bool hosting = false;
private void Start()
{
if (CommandLineParser.Instance.KeyExists("-ip"))
{
serverip = CommandLineParser.Instance.GetValueForKey("-ip", true);
}
if (CommandLineParser.Instance.KeyExists("-port"))
{
int.TryParse(CommandLineParser.Instance.GetValueForKey("-port", true), out serverport);
}
}
private void Update()
{
if (inputSystem.GetKeyDown(KeyCode.RightShift))
@ -77,7 +59,7 @@ Set CLI arguments: -ip, -port
{
Application.Quit(0);
}
if (serverip != null)
if (Helper.serverip != null)
{
if (GUI.Button(new Rect(20f, 260f, 170f, 30f), "Host"))
{
@ -85,22 +67,20 @@ Set CLI arguments: -ip, -port
{
if (File.Exists(Helper.RotationFolderPath + "config.json"))
{
hosting = true;
Helper.hosting = true;
AudioListener.volume = 0; // mute game audio
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
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();
}
}
else
@ -119,13 +99,9 @@ Make sure to download a file from the examples given and rename it to config.jso
{
if (LobbyManager.Instance.LobbyStates.SelfState == LobbyState.Game.Online)
{
if (!hosting)
if (!Helper.hosting)
{
LobbyManager.Instance.LobbyStates.IP = serverip;
LobbyManager.Instance.LobbyStates.Port = serverport;
LobbyManager.Instance.LobbyStates.CurrentState = LobbyState.State.Ready | LobbyState.State.InGame;
LobbyManager.Instance.LocalBeasts.SetupNetMemberContext(true);
MonoSingleton<Global>.Instance.UNetManager.LaunchClient(serverip, serverport);
GBSUClient.JoinServer(Helper.serverip, Helper.serverport);
}
else
{
@ -153,14 +129,7 @@ Make sure to download a file from the examples given and rename it to config.jso
}
if (GUI.Button(new Rect(20f, 330f, 170f, 30f), "Toggle VSync"))
{
if (QualitySettings.vSyncCount == 0)
{
QualitySettings.vSyncCount = 1;
}
else
{
QualitySettings.vSyncCount = 0;
}
Helper.FlipVSync();
}
GUI.Label(new Rect(20f, 365f, 365f, 400f), $@"
@ -195,7 +164,7 @@ Please refer to the documentation for more information.");
}
private string UpdateScoreDisplay()
{
if (hosting)
if (Helper.hosting)
{
string scoreString = "Score:\n";
foreach (var pair in Plugin.GameScore)

View file

@ -1,3 +1,6 @@
using CoreNet.Config;
using GB.Config;
using GB.Core;
using GB.Game;
using HarmonyLib;
using UnityEngine;
@ -29,6 +32,13 @@ public class GBSUServer : MonoBehaviour
localSingleGang?.SetValue(false);
}
public static void StartServer()
{
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
}
/*
public string GetRemainingRoundTime()
{

View file

@ -8,6 +8,9 @@ public class Helper
{
public static string RotationFolderPath = Application.dataPath + "/Config/Rotation/";
public static string GameConfigPath = RotationFolderPath + "config.json";
public static string serverip = null;
public static int serverport = 5999;
public static bool hosting = false;
public static void DisableAnalytics()
{
// Try disabling analytics https://docs.unity3d.com/ScriptReference/Analytics.Analytics-deviceStatsEnabled.html
@ -57,4 +60,16 @@ public class Helper
return $"There are {number} files: {names}";
}
}
public static void FlipVSync()
{
if (QualitySettings.vSyncCount == 0)
{
QualitySettings.vSyncCount = 1;
}
else
{
QualitySettings.vSyncCount = 0;
}
}
}

View file

@ -1,4 +1,4 @@
using System.Collections.Generic;
using System.Collections.Generic;
using System.Reflection;
using BepInEx;
using BepInEx.Logging;
@ -54,6 +54,16 @@ public class Plugin : BaseUnityPlugin
Helper.CreateRotationFolder();
Log.LogDebug("Server game config should be found at " + Helper.GameConfigPath);
// Parse CLI arguments
if (CommandLineParser.Instance.KeyExists("-ip"))
{
Helper.serverip = CommandLineParser.Instance.GetValueForKey("-ip", true);
}
if (CommandLineParser.Instance.KeyExists("-port"))
{
int.TryParse(CommandLineParser.Instance.GetValueForKey("-port", true), out Helper.serverport);
}
}
private static void GBSUCompInit()
@ -66,6 +76,13 @@ public class Plugin : BaseUnityPlugin
}
public static void AddServerComp()
{
DestroyServerComp();
Log.LogDebug("Adding GBSUServer component");
GBSUCompContainer.AddComponent<GBSUServer>();
}
public static void DestroyServerComp()
{
Component[] comps = GBSUCompContainer.GetComponents(typeof(Component));
foreach (var comp in comps)
@ -77,8 +94,5 @@ public class Plugin : BaseUnityPlugin
Destroy(comp);
}
}
Log.LogDebug("Adding GBSUServer component");
GBSUCompContainer.AddComponent<GBSUServer>();
}
}