refactor: wip
This commit is contained in:
parent
4b61948f68
commit
0cb7131435
5 changed files with 69 additions and 45 deletions
17
Addons/GBSUClient.cs
Normal file
17
Addons/GBSUClient.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,10 +1,7 @@
|
||||||
using System;
|
using System;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using BepInEx;
|
using BepInEx;
|
||||||
using CoreNet.Config;
|
using CoreNet.Utils;
|
||||||
using GB.Config;
|
|
||||||
using GB.Core;
|
|
||||||
using GB.Game;
|
|
||||||
using GB.Platform.Lobby;
|
using GB.Platform.Lobby;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
||||||
|
|
@ -22,20 +19,6 @@ public class GBSUGui : MonoBehaviour
|
||||||
private string error_msg = "Unknown error!";
|
private string error_msg = "Unknown error!";
|
||||||
|
|
||||||
readonly IInputSystem inputSystem = UnityInput.Current;
|
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()
|
private void Update()
|
||||||
{
|
{
|
||||||
if (inputSystem.GetKeyDown(KeyCode.RightShift))
|
if (inputSystem.GetKeyDown(KeyCode.RightShift))
|
||||||
|
|
@ -77,7 +60,7 @@ Set CLI arguments: -ip, -port
|
||||||
{
|
{
|
||||||
Application.Quit(0);
|
Application.Quit(0);
|
||||||
}
|
}
|
||||||
if (serverip != null)
|
if (Helper.serverip != null)
|
||||||
{
|
{
|
||||||
if (GUI.Button(new Rect(20f, 260f, 170f, 30f), "Host"))
|
if (GUI.Button(new Rect(20f, 260f, 170f, 30f), "Host"))
|
||||||
{
|
{
|
||||||
|
|
@ -85,18 +68,14 @@ Set CLI arguments: -ip, -port
|
||||||
{
|
{
|
||||||
if (File.Exists(Helper.RotationFolderPath + "config.json"))
|
if (File.Exists(Helper.RotationFolderPath + "config.json"))
|
||||||
{
|
{
|
||||||
hosting = true;
|
Helper.hosting = true;
|
||||||
|
|
||||||
AudioListener.volume = 0; // mute game audio
|
AudioListener.volume = 0; // mute game audio
|
||||||
|
|
||||||
Plugin.AddServerComp(); // add custom GBSU server component
|
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
RotationConfig gameConfig = GBConfigLoader.LoadRotationConfig("config.json", true); // load rotation config from Config/Rotation/config.json
|
Plugin.AddServerComp(); // add custom GBSU server component
|
||||||
ServerConfig serverConfig = NetConfigLoader.LoadServerConfig(); // load default server config, because it can be overridden by args like -ip and -port
|
GBSUServer.StartServer();
|
||||||
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)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
|
|
@ -119,13 +98,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 (LobbyManager.Instance.LobbyStates.SelfState == LobbyState.Game.Online)
|
||||||
{
|
{
|
||||||
if (!hosting)
|
if (!Helper.hosting)
|
||||||
{
|
{
|
||||||
LobbyManager.Instance.LobbyStates.IP = serverip;
|
GBSUClient.JoinServer(Helper.serverip, Helper.serverport);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
@ -153,14 +128,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 (GUI.Button(new Rect(20f, 330f, 170f, 30f), "Toggle VSync"))
|
||||||
{
|
{
|
||||||
if (QualitySettings.vSyncCount == 0)
|
Helper.FlipVSync();
|
||||||
{
|
|
||||||
QualitySettings.vSyncCount = 1;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
QualitySettings.vSyncCount = 0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
GUI.Label(new Rect(20f, 365f, 365f, 400f), $@"
|
GUI.Label(new Rect(20f, 365f, 365f, 400f), $@"
|
||||||
|
|
@ -195,7 +163,7 @@ Please refer to the documentation for more information.");
|
||||||
}
|
}
|
||||||
private string UpdateScoreDisplay()
|
private string UpdateScoreDisplay()
|
||||||
{
|
{
|
||||||
if (hosting)
|
if (Helper.hosting)
|
||||||
{
|
{
|
||||||
string scoreString = "Score:\n";
|
string scoreString = "Score:\n";
|
||||||
foreach (var pair in Plugin.GameScore)
|
foreach (var pair in Plugin.GameScore)
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,6 @@
|
||||||
|
using CoreNet.Config;
|
||||||
|
using GB.Config;
|
||||||
|
using GB.Core;
|
||||||
using GB.Game;
|
using GB.Game;
|
||||||
using HarmonyLib;
|
using HarmonyLib;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
@ -29,6 +32,13 @@ public class GBSUServer : MonoBehaviour
|
||||||
localSingleGang?.SetValue(false);
|
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()
|
public string GetRemainingRoundTime()
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,9 @@ public class Helper
|
||||||
{
|
{
|
||||||
public static string RotationFolderPath = Application.dataPath + "/Config/Rotation/";
|
public static string RotationFolderPath = Application.dataPath + "/Config/Rotation/";
|
||||||
public static string GameConfigPath = RotationFolderPath + "config.json";
|
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()
|
public static void DisableAnalytics()
|
||||||
{
|
{
|
||||||
// Try disabling analytics https://docs.unity3d.com/ScriptReference/Analytics.Analytics-deviceStatsEnabled.html
|
// 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}";
|
return $"There are {number} files: {names}";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void FlipVSync()
|
||||||
|
{
|
||||||
|
if (QualitySettings.vSyncCount == 0)
|
||||||
|
{
|
||||||
|
QualitySettings.vSyncCount = 1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
QualitySettings.vSyncCount = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
22
Plugin.cs
22
Plugin.cs
|
|
@ -1,4 +1,4 @@
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using BepInEx;
|
using BepInEx;
|
||||||
using BepInEx.Logging;
|
using BepInEx.Logging;
|
||||||
|
|
@ -54,6 +54,16 @@ public class Plugin : BaseUnityPlugin
|
||||||
Helper.CreateRotationFolder();
|
Helper.CreateRotationFolder();
|
||||||
|
|
||||||
Log.LogDebug("Server game config should be found at " + Helper.GameConfigPath);
|
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()
|
private static void GBSUCompInit()
|
||||||
|
|
@ -66,6 +76,13 @@ public class Plugin : BaseUnityPlugin
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void AddServerComp()
|
public static void AddServerComp()
|
||||||
|
{
|
||||||
|
DestroyServerComp();
|
||||||
|
Log.LogDebug("Adding GBSUServer component");
|
||||||
|
GBSUCompContainer.AddComponent<GBSUServer>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void DestroyServerComp()
|
||||||
{
|
{
|
||||||
Component[] comps = GBSUCompContainer.GetComponents(typeof(Component));
|
Component[] comps = GBSUCompContainer.GetComponents(typeof(Component));
|
||||||
foreach (var comp in comps)
|
foreach (var comp in comps)
|
||||||
|
|
@ -77,8 +94,5 @@ public class Plugin : BaseUnityPlugin
|
||||||
Destroy(comp);
|
Destroy(comp);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Log.LogDebug("Adding GBSUServer component");
|
|
||||||
GBSUCompContainer.AddComponent<GBSUServer>();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue