This commit is contained in:
2025-12-13 17:11:00 +01:00
parent 6e16be8646
commit c6c60a7e07
4 changed files with 36 additions and 5 deletions

17
Main.cs
View File

@@ -550,7 +550,12 @@ namespace KCM
{
public static bool Prefix(Player __instance)
{
if (KCClient.client.IsConnected && __instance.gameObject.name.Contains("Client Player") && !LobbyManager.loadingSave)
var localPlayer = Player.inst;
if (KCClient.client.IsConnected &&
__instance != null &&
(localPlayer == null || __instance != localPlayer) &&
__instance.gameObject != null &&
__instance.gameObject.name.Contains("Client Player"))
{
try
{
@@ -1206,6 +1211,7 @@ namespace KCM
if (KCServer.IsRunning)
{
Main.helper.Log("Trying to load multiplayer save");
KCM.StateManagement.Observers.StateObserver.ClearAll();
LoadSave.LastLoadDirectory = path;
path = path + "/" + filename;
@@ -1453,11 +1459,12 @@ namespace KCM
Main.helper.Log(e.Message);
}
bool flag2 = building.GetComponent<Keep>() != null && building.TeamID() == p.PlayerLandmassOwner.teamId;
Main.helper.Log("Set keep? " + flag2);
if (flag2)
var keep = building.GetComponent<Keep>();
bool shouldSetKeep = keep != null && p.keep == null;
Main.helper.Log("Set keep? " + shouldSetKeep);
if (shouldSetKeep)
{
p.keep = building.GetComponent<Keep>();
p.keep = keep;
Main.helper.Log(p.keep.ToString());
}

View File

@@ -4,6 +4,7 @@ using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using KCM.StateManagement.Observers;
using static KCM.Main;
namespace KCM.Packets.Lobby
@@ -41,6 +42,8 @@ namespace KCM.Packets.Lobby
loadingSave = true;
received = 0;
StateObserver.ClearAll();
saveData = new byte[saveSize];
chunksReceived = new bool[totalChunks];

View File

@@ -1,6 +1,7 @@
using KCM;
using KCM.Enums;
using KCM.Packets.Handlers;
using KCM.StateManagement.Observers;
using Steamworks;
using UnityEngine;
@@ -157,6 +158,8 @@ namespace Riptide.Demos.Steam.PlayerHosted
if (KCClient.client.IsConnected)
KCClient.client.Disconnect();
StateObserver.ClearAll();
Main.helper.Log("clear players");
Main.kCPlayers.Clear();
Main.clientSteamIds.Clear();

View File

@@ -12,6 +12,24 @@ namespace KCM.StateManagement.Observers
{
public static Dictionary<int, IObserver> observers = new Dictionary<int, IObserver>();
public static void ClearAll()
{
foreach (var observer in observers.Values)
{
try
{
var component = observer as Component;
if (component != null)
UnityEngine.Object.Destroy(component.gameObject);
}
catch
{
}
}
observers.Clear();
}
public static void RegisterObserver<T>(T instance, string[] monitoredFields, EventHandler<StateUpdateEventArgs> eventHandler = null, EventHandler<StateUpdateEventArgs> sendUpdateHandler = null)
{
if (observers.ContainsKey(instance.GetHashCode()))