diff --git a/KCClient.cs b/KCClient.cs index 9e530c6..8de586b 100644 --- a/KCClient.cs +++ b/KCClient.cs @@ -19,7 +19,7 @@ namespace KCM { public class KCClient : MonoBehaviour { - public static Client client = new Client(Main.steamClient); + public static Client client = new Client(); public string Name { get; set; } @@ -101,7 +101,8 @@ namespace KCM public static void Connect(string ip) { - Main.helper.Log("Trying to connect to: " + ip); + Main.EnsureNetworking(); + Main.Log("Trying to connect to: " + ip); client.Connect(ip, useMessageHandlers: false); } @@ -112,8 +113,9 @@ namespace KCM private void Preload(KCModHelper helper) { - - helper.Log("Preload run in client"); + Main.helper = helper; + Main.EnsureNetworking(); + Main.Log("Preload run in client"); } private void SceneLoaded(KCModHelper helper) diff --git a/KCServer.cs b/KCServer.cs index 685f347..2ebfb98 100644 --- a/KCServer.cs +++ b/KCServer.cs @@ -18,7 +18,7 @@ namespace KCM { public class KCServer : MonoBehaviour { - public static Server server = new Server(Main.steamServer); + public static Server server = new Server(); public static bool started = false; private static readonly Dictionary> saveTransferQueues = new Dictionary>(); @@ -33,6 +33,7 @@ namespace KCM public static void StartServer() { + Main.EnsureNetworking(); server = new Server(Main.steamServer); server.MessageReceived += PacketHandler.HandlePacketServer; @@ -203,9 +204,10 @@ namespace KCM private void Preload(KCModHelper helper) { - helper.Log("server?"); - - helper.Log("Preload run in server"); + Main.helper = helper; + Main.EnsureNetworking(); + Main.Log("server?"); + Main.Log("Preload run in server"); } private void SceneLoaded(KCModHelper helper) diff --git a/Main.cs b/Main.cs index 4e3c59a..5664c52 100644 --- a/Main.cs +++ b/Main.cs @@ -1,5 +1,5 @@ using Assets.Code; -using HarmonyLib; +using Harmony; using KCM.Packets; using KCM.Packets.Game; using KCM.Packets.Game.GamePlayer; @@ -10,7 +10,6 @@ using KCM.StateManagement.Observers; using KCM.StateManagement.Sync; using KCM.StateManagement.BuildingState; using Riptide; -using Riptide.Utils; using Steamworks; using System; using System.Collections.Generic; @@ -19,31 +18,68 @@ using System.Linq; using System.Reflection; using System.Text; using UnityEngine; +using SteamTransportClient = Riptide.Transports.Steam.SteamClient; +using SteamTransportServer = Riptide.Transports.Steam.SteamServer; namespace KCM { public class Main : MonoBehaviour { - public static Harmony harmony = new Harmony("com.kcm.mod"); - public static Helper helper; + public static HarmonyInstance harmony; + public static KCModHelper helper; public static Dictionary kCPlayers = new Dictionary(); public static Dictionary clientSteamIds = new Dictionary(); - public static SteamClient steamClient; - public static SteamServer steamServer; + public static SteamTransportClient steamClient; + public static SteamTransportServer steamServer; public static string PlayerSteamID => SteamUser.GetSteamID().ToString(); void Awake() { - helper = new Helper("KCM", true, true, true); - helper.Log("KCM Awake"); + Log("KCM Awake"); } void Start() { - helper.Log("KCM Start"); - harmony.PatchAll(); + Log("KCM Start"); + + EnsureNetworking(); + + if (harmony == null) + harmony = HarmonyInstance.Create("com.kcm.mod"); + + harmony.PatchAll(Assembly.GetExecutingAssembly()); + } + + public static void EnsureNetworking() + { + if (steamServer == null) + steamServer = new SteamTransportServer(); + + if (steamClient == null) + steamClient = new SteamTransportClient(steamServer); + else + steamClient.ChangeLocalServer(steamServer); + + if (KCClient.client != null) + KCClient.client.ChangeTransport(steamClient); + + if (KCServer.server != null) + KCServer.server.ChangeTransport(steamServer); + } + + public static void Log(string message) + { + if (helper != null) + helper.Log(message ?? string.Empty); + else + Debug.Log(message ?? string.Empty); + } + + public static void Log(Exception ex) + { + Log(ex != null ? ex.ToString() : string.Empty); } public static void ServerUpdate() @@ -75,6 +111,14 @@ namespace KCM clientSteamIds.Clear(); } + public static void ResetMultiplayerState(string reason) + { + if (!string.IsNullOrEmpty(reason)) + Log("ResetMultiplayerState: " + reason); + + ResetMultiplayerState(); + } + public static void SetMultiplayerSaveLoadInProgress(bool inProgress) { // Set save/load progress flag @@ -178,7 +222,7 @@ namespace KCM } catch (Exception e) { - helper?.Log("Error in ForceMultiplayerAIUpdate: " + e.Message); + Log("Error in ForceMultiplayerAIUpdate: " + e.Message); } } @@ -202,8 +246,8 @@ namespace KCM { try { - try { Player.inst.irrigation.UpdateIrrigation(); } catch (Exception e) { helper?.Log(e.ToString()); } - try { Player.inst.CalcMaxResources(null, -1); } catch (Exception e) { helper?.Log(e.ToString()); } + try { Player.inst.irrigation.UpdateIrrigation(); } catch (Exception e) { Log(e); } + try { Player.inst.CalcMaxResources(null, -1); } catch (Exception e) { Log(e); } SetLoadTickDelay(Player.inst, 1); SetLoadTickDelay(UnitSystem.inst, 1); @@ -212,8 +256,8 @@ namespace KCM } catch (Exception e) { - helper?.Log("Post-load rebuild failed"); - helper?.Log(e.ToString()); + Log("Post-load rebuild failed"); + Log(e); } } @@ -238,9 +282,9 @@ namespace KCM ? Player.inst.PlayerLandmassOwner.teamId.ToString() : "unknown"; - helper.Log("Failed finding player by teamID: " + teamId + " My teamID is: " + myTeamId); - helper.Log(kCPlayers.Count.ToString()); - helper.Log(string.Join(", ", kCPlayers.Values.Where(p => p != null && p.inst != null && p.inst.PlayerLandmassOwner != null).Select(p => p.inst.PlayerLandmassOwner.teamId.ToString()))); + Log("Failed finding player by teamID: " + teamId + " My teamID is: " + myTeamId); + Log(kCPlayers.Count.ToString()); + Log(string.Join(", ", kCPlayers.Values.Where(p => p != null && p.inst != null && p.inst.PlayerLandmassOwner != null).Select(p => p.inst.PlayerLandmassOwner.teamId.ToString()))); } } @@ -271,7 +315,7 @@ namespace KCM { if (__instance.TeamID() == Player.inst.PlayerLandmassOwner.teamId) { - helper.Log("Overridden complete build"); + Log("Overridden complete build"); Player player = GetPlayerByTeamID(__instance.TeamID()); typeof(Building).GetField("built", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(__instance, true); @@ -294,9 +338,9 @@ namespace KCM } catch (Exception e) { - helper.Log(e.ToString()); - helper.Log(e.Message); - helper.Log(e.StackTrace); + Log(e.ToString()); + Log(e.Message); + Log(e.StackTrace); } return true; @@ -321,9 +365,9 @@ namespace KCM } catch (Exception e) { - helper.Log(e.ToString()); - helper.Log(e.Message); - helper.Log(e.StackTrace); + Log(e.ToString()); + Log(e.Message); + Log(e.StackTrace); } } } @@ -338,7 +382,7 @@ namespace KCM if (!KCClient.client.IsConnected) return; - helper.Log("SpeedControlUI.SetSpeed (local): " + idx); + Log("SpeedControlUI.SetSpeed (local): " + idx); // Force AI restart when speed changes from paused to playing if (idx > 0) @@ -367,7 +411,7 @@ namespace KCM if (!paused) { // Game is unpaused, force AI restart - helper.Log("Game unpaused, forcing AI restart"); + Log("Game unpaused, forcing AI restart"); ForceMultiplayerAIUpdate(); } } @@ -391,4 +435,4 @@ namespace KCM } #endregion } -} \ No newline at end of file +} diff --git a/PrefabManager.cs b/PrefabManager.cs index 083a765..00ce432 100644 --- a/PrefabManager.cs +++ b/PrefabManager.cs @@ -24,11 +24,12 @@ namespace KCM { try { - //Main.helper = _helper; + Main.helper = _helper; + Main.EnsureNetworking(); assetBundle = KCModHelper.LoadAssetBundle(_helper.modPath, "serverbrowserpkg"); - Main.helper.Log(String.Join(", ", assetBundle.GetAllAssetNames())); + Main.Log(String.Join(", ", assetBundle.GetAllAssetNames())); serverBrowserPrefab = assetBundle.LoadAsset("assets/workspace/serverbrowser.prefab") as GameObject; serverEntryItemPrefab = assetBundle.LoadAsset("assets/workspace/serverentryitem.prefab") as GameObject; @@ -41,13 +42,13 @@ namespace KCM modalUIPrefab = assetBundle.LoadAsset("assets/workspace/modalui.prefab") as GameObject; - Main.helper.Log("Loaded assets"); + Main.Log("Loaded assets"); } catch (Exception ex) { - Main.helper.Log(ex.ToString()); - Main.helper.Log(ex.Message); - Main.helper.Log(ex.StackTrace); + Main.Log(ex); + Main.Log(ex.Message); + Main.Log(ex.StackTrace); } } } diff --git a/RiptideSteamTransport/KCMSteamManager.cs b/RiptideSteamTransport/KCMSteamManager.cs index b7e0f70..cff8f3c 100644 --- a/RiptideSteamTransport/KCMSteamManager.cs +++ b/RiptideSteamTransport/KCMSteamManager.cs @@ -49,7 +49,7 @@ public class KCMSteamManager : MonoBehaviour { [AOT.MonoPInvokeCallback(typeof(SteamAPIWarningMessageHook_t))] protected static void SteamAPIDebugTextHook(int nSeverity, System.Text.StringBuilder pchDebugText) { - Main.helper.Log(pchDebugText.ToString()); + Main.Log(pchDebugText.ToString()); } #if UNITY_2019_3_OR_NEWER @@ -64,7 +64,7 @@ public class KCMSteamManager : MonoBehaviour { protected virtual void Awake() { // Only one instance of SteamManager at a time! - Main.helper.Log("Steam awake"); + Main.Log("Steam awake"); if (s_instance != null) { Destroy(gameObject); return; @@ -76,7 +76,7 @@ public class KCMSteamManager : MonoBehaviour { // The most common case where this happens is when SteamManager gets destroyed because of Application.Quit(), // and then some Steamworks code in some other OnDestroy gets called afterwards, creating a new SteamManager. // You should never call Steamworks functions in OnDestroy, always prefer OnDisable if possible. - Main.helper.Log("Tried to Initialize the SteamAPI twice in one session!"); + Main.Log("Tried to Initialize the SteamAPI twice in one session!"); return; } @@ -85,11 +85,11 @@ public class KCMSteamManager : MonoBehaviour { DontDestroyOnLoad(gameObject); if (!Packsize.Test()) { - Main.helper.Log("[Steamworks.NET] Packsize Test returned false, the wrong version of Steamworks.NET is being run in this platform."); + Main.Log("[Steamworks.NET] Packsize Test returned false, the wrong version of Steamworks.NET is being run in this platform."); } if (!DllCheck.Test()) { - Main.helper.Log("[Steamworks.NET] DllCheck Test returned false, One or more of the Steamworks binaries seems to be the wrong version."); + Main.Log("[Steamworks.NET] DllCheck Test returned false, One or more of the Steamworks binaries seems to be the wrong version."); } try { @@ -101,12 +101,12 @@ public class KCMSteamManager : MonoBehaviour { // See the Valve documentation for more information: https://partner.steamgames.com/doc/sdk/api#initialization_and_shutdown if (SteamAPI.RestartAppIfNecessary((AppId_t)569480)) { //Application.Quit(); - Main.helper.Log("Attempted to restart app"); + Main.Log("Attempted to restart app"); return; } } catch (System.DllNotFoundException e) { // We catch this exception here, as it will be the first occurrence of it. - Main.helper.Log("[Steamworks.NET] Could not load [lib]steam_api.dll/so/dylib. It's likely not in the correct location. Refer to the README for more details.\n" + e); + Main.Log("[Steamworks.NET] Could not load [lib]steam_api.dll/so/dylib. It's likely not in the correct location. Refer to the README for more details.\n" + e); //Application.Quit(); return; @@ -123,7 +123,7 @@ public class KCMSteamManager : MonoBehaviour { // https://partner.steamgames.com/doc/sdk/api#initialization_and_shutdown m_bInitialized = SteamAPI.Init(); if (!m_bInitialized) { - Main.helper.Log("[Steamworks.NET] SteamAPI_Init() failed. Refer to Valve's documentation or the comment above this line for more information."); + Main.Log("[Steamworks.NET] SteamAPI_Init() failed. Refer to Valve's documentation or the comment above this line for more information."); return; }