Compare commits
3 Commits
3124f82a2f
...
76f1033bd2
| Author | SHA1 | Date | |
|---|---|---|---|
| 76f1033bd2 | |||
| 26b5f1201e | |||
| 9ee675ac19 |
@@ -36,6 +36,7 @@ namespace KCM
|
||||
|
||||
private static void Client_Disconnected(object sender, DisconnectedEventArgs e)
|
||||
{
|
||||
Main.CleanupMultiplayerSession();
|
||||
Main.helper.Log("Client disconnected event start");
|
||||
try
|
||||
{
|
||||
|
||||
42
Main.cs
42
Main.cs
@@ -106,11 +106,51 @@ namespace KCM
|
||||
public static string PlayerSteamID = SteamUser.GetSteamID().ToString();
|
||||
|
||||
public static KCMSteamManager KCMSteamManager = null;
|
||||
public static LobbyManager lobbyManager = null;
|
||||
public static SteamServer steamServer = new SteamServer();
|
||||
public static Riptide.Transports.Steam.SteamClient steamClient = new Riptide.Transports.Steam.SteamClient(steamServer);
|
||||
|
||||
public static ushort currentClient = 0;
|
||||
|
||||
public static void CleanupMultiplayerSession()
|
||||
{
|
||||
if (helper == null) return; // Avoid running if mod is not fully initialized
|
||||
|
||||
helper.Log("--- Starting Multiplayer Session Cleanup ---");
|
||||
|
||||
// Disconnect client
|
||||
if (KCClient.client != null && KCClient.client.IsConnected)
|
||||
{
|
||||
helper.Log("Disconnecting client...");
|
||||
KCClient.client.Disconnect();
|
||||
}
|
||||
|
||||
// Stop server
|
||||
if (KCServer.server != null && KCServer.IsRunning)
|
||||
{
|
||||
helper.Log("Stopping server...");
|
||||
KCServer.server.Stop();
|
||||
}
|
||||
|
||||
// Clear player lists
|
||||
if (kCPlayers.Count > 0 || clientSteamIds.Count > 0)
|
||||
{
|
||||
helper.Log($"Clearing {kCPlayers.Count} KCPlayer entries and {clientSteamIds.Count} client steam IDs.");
|
||||
kCPlayers.Clear();
|
||||
clientSteamIds.Clear();
|
||||
}
|
||||
|
||||
// Destroy persistent managers
|
||||
if (lobbyManager != null)
|
||||
{
|
||||
helper.Log("Destroying LobbyManager.");
|
||||
Destroy(lobbyManager.gameObject);
|
||||
lobbyManager = null;
|
||||
}
|
||||
|
||||
helper.Log("--- Multiplayer Session Cleanup Finished ---");
|
||||
}
|
||||
|
||||
#region "SceneLoaded"
|
||||
private void SceneLoaded(KCModHelper helper)
|
||||
{
|
||||
@@ -123,7 +163,7 @@ namespace KCM
|
||||
KCMSteamManager = new GameObject("KCMSteamManager").AddComponent<KCMSteamManager>();
|
||||
DontDestroyOnLoad(KCMSteamManager);
|
||||
|
||||
var lobbyManager = new GameObject("LobbyManager").AddComponent<LobbyManager>();
|
||||
lobbyManager = new GameObject("LobbyManager").AddComponent<LobbyManager>();
|
||||
DontDestroyOnLoad(lobbyManager);
|
||||
|
||||
//SteamFriends.InviteUserToGame(new CSteamID(76561198036307537), "test");
|
||||
|
||||
@@ -21,12 +21,12 @@ namespace KCM.ServerLobby
|
||||
|
||||
public void Start()
|
||||
{
|
||||
banner = transform.Find("PlayerBanner").GetComponent<RawImage>();
|
||||
|
||||
SetValues();
|
||||
|
||||
InvokeRepeating("SetValues", 0, 0.25f);
|
||||
|
||||
banner = transform.Find("PlayerBanner").GetComponent<RawImage>();
|
||||
|
||||
transform.Find("PlayerBanner").GetComponent<Button>().onClick.AddListener(() =>
|
||||
{
|
||||
Main.TransitionTo(MenuState.NameAndBanner);//ChooseBannerUI Hooks required, as well as townnameui
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
using KCM.Packets;
|
||||
using KCM.Packets;
|
||||
using KCM.Packets.State;
|
||||
using KCM.StateManagement.Observers;
|
||||
using System;
|
||||
@@ -6,12 +6,15 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
using static KCM.StateManagement.Observers.Observer;
|
||||
|
||||
namespace KCM.StateManagement.BuildingState
|
||||
{
|
||||
public class BuildingStateManager
|
||||
{
|
||||
private static readonly Dictionary<Guid, float> lastUpdateTime = new Dictionary<Guid, float>();
|
||||
private const float UpdateInterval = 0.1f; // 10 times per second
|
||||
|
||||
public static void BuildingStateChanged(object sender, StateUpdateEventArgs args)
|
||||
{
|
||||
@@ -23,9 +26,29 @@ namespace KCM.StateManagement.BuildingState
|
||||
try
|
||||
{
|
||||
Observer observer = (Observer)sender;
|
||||
|
||||
Building building = (Building)observer.state;
|
||||
|
||||
if (building == null)
|
||||
{
|
||||
if(observer != null)
|
||||
{
|
||||
UnityEngine.Object.Destroy(observer.gameObject);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
Guid guid = building.guid;
|
||||
|
||||
if (lastUpdateTime.ContainsKey(guid) && Time.time < lastUpdateTime[guid] + UpdateInterval)
|
||||
{
|
||||
return; // Not time to update yet
|
||||
}
|
||||
|
||||
if (!lastUpdateTime.ContainsKey(guid))
|
||||
lastUpdateTime.Add(guid, Time.time);
|
||||
else
|
||||
lastUpdateTime[guid] = Time.time;
|
||||
|
||||
//Main.helper.Log("Should send building network update for: " + building.UniqueName);
|
||||
|
||||
new BuildingStatePacket()
|
||||
|
||||
Reference in New Issue
Block a user