na vissza geci chatgptre mert nem működött... xdd

This commit is contained in:
2025-12-13 23:46:32 +01:00
parent d7718c1dff
commit 5abd025860
5 changed files with 99 additions and 50 deletions

View File

@@ -19,7 +19,7 @@ namespace KCM
{ {
public class KCClient : MonoBehaviour public class KCClient : MonoBehaviour
{ {
public static Client client = new Client(Main.steamClient); public static Client client = new Client();
public string Name { get; set; } public string Name { get; set; }
@@ -101,7 +101,8 @@ namespace KCM
public static void Connect(string ip) 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); client.Connect(ip, useMessageHandlers: false);
} }
@@ -112,8 +113,9 @@ namespace KCM
private void Preload(KCModHelper helper) private void Preload(KCModHelper helper)
{ {
Main.helper = helper;
helper.Log("Preload run in client"); Main.EnsureNetworking();
Main.Log("Preload run in client");
} }
private void SceneLoaded(KCModHelper helper) private void SceneLoaded(KCModHelper helper)

View File

@@ -18,7 +18,7 @@ namespace KCM
{ {
public class KCServer : MonoBehaviour public class KCServer : MonoBehaviour
{ {
public static Server server = new Server(Main.steamServer); public static Server server = new Server();
public static bool started = false; public static bool started = false;
private static readonly Dictionary<ushort, Queue<SaveTransferPacket>> saveTransferQueues = new Dictionary<ushort, Queue<SaveTransferPacket>>(); private static readonly Dictionary<ushort, Queue<SaveTransferPacket>> saveTransferQueues = new Dictionary<ushort, Queue<SaveTransferPacket>>();
@@ -33,6 +33,7 @@ namespace KCM
public static void StartServer() public static void StartServer()
{ {
Main.EnsureNetworking();
server = new Server(Main.steamServer); server = new Server(Main.steamServer);
server.MessageReceived += PacketHandler.HandlePacketServer; server.MessageReceived += PacketHandler.HandlePacketServer;
@@ -203,9 +204,10 @@ namespace KCM
private void Preload(KCModHelper helper) private void Preload(KCModHelper helper)
{ {
helper.Log("server?"); Main.helper = helper;
Main.EnsureNetworking();
helper.Log("Preload run in server"); Main.Log("server?");
Main.Log("Preload run in server");
} }
private void SceneLoaded(KCModHelper helper) private void SceneLoaded(KCModHelper helper)

98
Main.cs
View File

@@ -1,5 +1,5 @@
using Assets.Code; using Assets.Code;
using HarmonyLib; using Harmony;
using KCM.Packets; using KCM.Packets;
using KCM.Packets.Game; using KCM.Packets.Game;
using KCM.Packets.Game.GamePlayer; using KCM.Packets.Game.GamePlayer;
@@ -10,7 +10,6 @@ using KCM.StateManagement.Observers;
using KCM.StateManagement.Sync; using KCM.StateManagement.Sync;
using KCM.StateManagement.BuildingState; using KCM.StateManagement.BuildingState;
using Riptide; using Riptide;
using Riptide.Utils;
using Steamworks; using Steamworks;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@@ -19,31 +18,68 @@ using System.Linq;
using System.Reflection; using System.Reflection;
using System.Text; using System.Text;
using UnityEngine; using UnityEngine;
using SteamTransportClient = Riptide.Transports.Steam.SteamClient;
using SteamTransportServer = Riptide.Transports.Steam.SteamServer;
namespace KCM namespace KCM
{ {
public class Main : MonoBehaviour public class Main : MonoBehaviour
{ {
public static Harmony harmony = new Harmony("com.kcm.mod"); public static HarmonyInstance harmony;
public static Helper helper; public static KCModHelper helper;
public static Dictionary<string, KCPlayer> kCPlayers = new Dictionary<string, KCPlayer>(); public static Dictionary<string, KCPlayer> kCPlayers = new Dictionary<string, KCPlayer>();
public static Dictionary<ushort, string> clientSteamIds = new Dictionary<ushort, string>(); public static Dictionary<ushort, string> clientSteamIds = new Dictionary<ushort, string>();
public static SteamClient steamClient; public static SteamTransportClient steamClient;
public static SteamServer steamServer; public static SteamTransportServer steamServer;
public static string PlayerSteamID => SteamUser.GetSteamID().ToString(); public static string PlayerSteamID => SteamUser.GetSteamID().ToString();
void Awake() void Awake()
{ {
helper = new Helper("KCM", true, true, true); Log("KCM Awake");
helper.Log("KCM Awake");
} }
void Start() void Start()
{ {
helper.Log("KCM Start"); Log("KCM Start");
harmony.PatchAll();
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() public static void ServerUpdate()
@@ -75,6 +111,14 @@ namespace KCM
clientSteamIds.Clear(); clientSteamIds.Clear();
} }
public static void ResetMultiplayerState(string reason)
{
if (!string.IsNullOrEmpty(reason))
Log("ResetMultiplayerState: " + reason);
ResetMultiplayerState();
}
public static void SetMultiplayerSaveLoadInProgress(bool inProgress) public static void SetMultiplayerSaveLoadInProgress(bool inProgress)
{ {
// Set save/load progress flag // Set save/load progress flag
@@ -178,7 +222,7 @@ namespace KCM
} }
catch (Exception e) catch (Exception e)
{ {
helper?.Log("Error in ForceMultiplayerAIUpdate: " + e.Message); Log("Error in ForceMultiplayerAIUpdate: " + e.Message);
} }
} }
@@ -202,8 +246,8 @@ namespace KCM
{ {
try try
{ {
try { Player.inst.irrigation.UpdateIrrigation(); } 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) { helper?.Log(e.ToString()); } try { Player.inst.CalcMaxResources(null, -1); } catch (Exception e) { Log(e); }
SetLoadTickDelay(Player.inst, 1); SetLoadTickDelay(Player.inst, 1);
SetLoadTickDelay(UnitSystem.inst, 1); SetLoadTickDelay(UnitSystem.inst, 1);
@@ -212,8 +256,8 @@ namespace KCM
} }
catch (Exception e) catch (Exception e)
{ {
helper?.Log("Post-load rebuild failed"); Log("Post-load rebuild failed");
helper?.Log(e.ToString()); Log(e);
} }
} }
@@ -238,9 +282,9 @@ namespace KCM
? Player.inst.PlayerLandmassOwner.teamId.ToString() ? Player.inst.PlayerLandmassOwner.teamId.ToString()
: "unknown"; : "unknown";
helper.Log("Failed finding player by teamID: " + teamId + " My teamID is: " + myTeamId); Log("Failed finding player by teamID: " + teamId + " My teamID is: " + myTeamId);
helper.Log(kCPlayers.Count.ToString()); 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(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) if (__instance.TeamID() == Player.inst.PlayerLandmassOwner.teamId)
{ {
helper.Log("Overridden complete build"); Log("Overridden complete build");
Player player = GetPlayerByTeamID(__instance.TeamID()); Player player = GetPlayerByTeamID(__instance.TeamID());
typeof(Building).GetField("built", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(__instance, true); typeof(Building).GetField("built", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(__instance, true);
@@ -294,9 +338,9 @@ namespace KCM
} }
catch (Exception e) catch (Exception e)
{ {
helper.Log(e.ToString()); Log(e.ToString());
helper.Log(e.Message); Log(e.Message);
helper.Log(e.StackTrace); Log(e.StackTrace);
} }
return true; return true;
@@ -321,9 +365,9 @@ namespace KCM
} }
catch (Exception e) catch (Exception e)
{ {
helper.Log(e.ToString()); Log(e.ToString());
helper.Log(e.Message); Log(e.Message);
helper.Log(e.StackTrace); Log(e.StackTrace);
} }
} }
} }
@@ -338,7 +382,7 @@ namespace KCM
if (!KCClient.client.IsConnected) if (!KCClient.client.IsConnected)
return; return;
helper.Log("SpeedControlUI.SetSpeed (local): " + idx); Log("SpeedControlUI.SetSpeed (local): " + idx);
// Force AI restart when speed changes from paused to playing // Force AI restart when speed changes from paused to playing
if (idx > 0) if (idx > 0)
@@ -367,7 +411,7 @@ namespace KCM
if (!paused) if (!paused)
{ {
// Game is unpaused, force AI restart // Game is unpaused, force AI restart
helper.Log("Game unpaused, forcing AI restart"); Log("Game unpaused, forcing AI restart");
ForceMultiplayerAIUpdate(); ForceMultiplayerAIUpdate();
} }
} }

View File

@@ -24,11 +24,12 @@ namespace KCM
{ {
try try
{ {
//Main.helper = _helper; Main.helper = _helper;
Main.EnsureNetworking();
assetBundle = KCModHelper.LoadAssetBundle(_helper.modPath, "serverbrowserpkg"); 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; serverBrowserPrefab = assetBundle.LoadAsset("assets/workspace/serverbrowser.prefab") as GameObject;
serverEntryItemPrefab = assetBundle.LoadAsset("assets/workspace/serverentryitem.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; modalUIPrefab = assetBundle.LoadAsset("assets/workspace/modalui.prefab") as GameObject;
Main.helper.Log("Loaded assets"); Main.Log("Loaded assets");
} }
catch (Exception ex) catch (Exception ex)
{ {
Main.helper.Log(ex.ToString()); Main.Log(ex);
Main.helper.Log(ex.Message); Main.Log(ex.Message);
Main.helper.Log(ex.StackTrace); Main.Log(ex.StackTrace);
} }
} }
} }

View File

@@ -49,7 +49,7 @@ public class KCMSteamManager : MonoBehaviour {
[AOT.MonoPInvokeCallback(typeof(SteamAPIWarningMessageHook_t))] [AOT.MonoPInvokeCallback(typeof(SteamAPIWarningMessageHook_t))]
protected static void SteamAPIDebugTextHook(int nSeverity, System.Text.StringBuilder pchDebugText) { 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 #if UNITY_2019_3_OR_NEWER
@@ -64,7 +64,7 @@ public class KCMSteamManager : MonoBehaviour {
protected virtual void Awake() { protected virtual void Awake() {
// Only one instance of SteamManager at a time! // Only one instance of SteamManager at a time!
Main.helper.Log("Steam awake"); Main.Log("Steam awake");
if (s_instance != null) { if (s_instance != null) {
Destroy(gameObject); Destroy(gameObject);
return; 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(), // 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. // 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. // 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; return;
} }
@@ -85,11 +85,11 @@ public class KCMSteamManager : MonoBehaviour {
DontDestroyOnLoad(gameObject); DontDestroyOnLoad(gameObject);
if (!Packsize.Test()) { 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()) { 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 { 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 // See the Valve documentation for more information: https://partner.steamgames.com/doc/sdk/api#initialization_and_shutdown
if (SteamAPI.RestartAppIfNecessary((AppId_t)569480)) { if (SteamAPI.RestartAppIfNecessary((AppId_t)569480)) {
//Application.Quit(); //Application.Quit();
Main.helper.Log("Attempted to restart app"); Main.Log("Attempted to restart app");
return; return;
} }
} }
catch (System.DllNotFoundException e) { // We catch this exception here, as it will be the first occurrence of it. 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(); //Application.Quit();
return; return;
@@ -123,7 +123,7 @@ public class KCMSteamManager : MonoBehaviour {
// https://partner.steamgames.com/doc/sdk/api#initialization_and_shutdown // https://partner.steamgames.com/doc/sdk/api#initialization_and_shutdown
m_bInitialized = SteamAPI.Init(); m_bInitialized = SteamAPI.Init();
if (!m_bInitialized) { 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; return;
} }