This commit is contained in:
2025-12-14 00:42:03 +01:00
parent 77f4d4fed0
commit 634a5f7983
4 changed files with 156 additions and 1 deletions

View File

@@ -47,6 +47,7 @@ namespace KCM.Enums
PlaceKeepRandomly = 91,
ResyncRequest = 92,
ResourceSnapshot = 93,
BuildingSnapshot = 94
BuildingSnapshot = 94,
VillagerSnapshot = 95
}
}

View File

@@ -0,0 +1,56 @@
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace KCM.Packets.Game.GameVillager
{
public class VillagerSnapshotPacket : Packet
{
public override ushort packetId => (ushort)Enums.Packets.VillagerSnapshot;
public List<Guid> guids { get; set; } = new List<Guid>();
public List<Vector3> positions { get; set; } = new List<Vector3>();
public override void HandlePacketClient()
{
if (KCClient.client != null && clientId == KCClient.client.Id)
return;
try
{
int count = Math.Min(guids?.Count ?? 0, positions?.Count ?? 0);
if (count == 0)
return;
for (int i = 0; i < count; i++)
{
Guid guid = guids[i];
Vector3 position = positions[i];
Villager villager = null;
try
{
villager = Villager.villagers?.data.FirstOrDefault(v => v != null && v.guid == guid);
}
catch
{
}
if (villager == null)
continue;
villager.TeleportTo(position);
}
}
catch (Exception e)
{
Main.helper.Log("Error handling villager snapshot packet: " + e.Message);
}
}
public override void HandlePacketServer()
{
}
}
}

View File

@@ -310,6 +310,27 @@ namespace KCM.Packets.Handlers
message.AddInt(item);
}
else if (prop.PropertyType == typeof(List<Guid>))
{
currentPropName = prop.Name;
List<Guid> list = (List<Guid>)prop.GetValue(packet, null);
message.AddInt(list.Count);
foreach (var item in list)
message.AddBytes(item.ToByteArray(), true);
}
else if (prop.PropertyType == typeof(List<Vector3>))
{
currentPropName = prop.Name;
List<Vector3> list = (List<Vector3>)prop.GetValue(packet, null);
message.AddInt(list.Count);
foreach (var item in list)
{
message.AddFloat(item.x);
message.AddFloat(item.y);
message.AddFloat(item.z);
}
}
else if (prop.PropertyType.IsGenericType && prop.PropertyType.GetGenericTypeDefinition() == typeof(Dictionary<,>))
{
currentPropName = prop.Name;
@@ -543,6 +564,29 @@ namespace KCM.Packets.Handlers
prop.SetValue(p, list);
}
else if (prop.PropertyType == typeof(List<Guid>))
{
int count = message.GetInt();
List<Guid> list = new List<Guid>();
for (int i = 0; i < count; i++)
list.Add(new Guid(message.GetBytes()));
prop.SetValue(p, list);
}
else if (prop.PropertyType == typeof(List<Vector3>))
{
int count = message.GetInt();
List<Vector3> list = new List<Vector3>();
for (int i = 0; i < count; i++)
{
Vector3 vector = new Vector3(message.GetFloat(), message.GetFloat(), message.GetFloat());
list.Add(vector);
}
prop.SetValue(p, list);
}
else if (prop.PropertyType.IsGenericType && prop.PropertyType.GetGenericTypeDefinition() == typeof(Dictionary<,>))
{
IDictionary dictionary = (IDictionary)prop.GetValue(p, null);

View File

@@ -16,9 +16,11 @@ namespace KCM.StateManagement.Sync
private const int MaxBuildingSnapshotBytes = 30000;
private const int MaxVillagerTeleportsPerResync = 400;
private const int VillagerValidationIntervalMs = 10000; // 10 seconds
private const int VillagerSnapshotIntervalMs = 1000;
private static long lastResourceBroadcastMs;
private static long lastVillagerValidationMs;
private static long lastVillagerSnapshotMs;
private static FieldInfo freeResourceAmountField;
private static MethodInfo resourceAmountGetMethod;
@@ -63,6 +65,20 @@ namespace KCM.StateManagement.Sync
lastVillagerValidationMs = now;
ValidateAndCorrectVillagerStates();
}
if ((now - lastVillagerSnapshotMs) >= VillagerSnapshotIntervalMs)
{
lastVillagerSnapshotMs = now;
try
{
BroadcastVillagerSnapshot();
}
catch (Exception ex)
{
Main.helper.Log("Error broadcasting villager snapshot");
Main.helper.Log(ex.ToString());
}
}
}
public static void SendResyncToClient(ushort toClient, string reason)
@@ -559,6 +575,44 @@ namespace KCM.StateManagement.Sync
}
}
private static void BroadcastVillagerSnapshot()
{
if (!KCServer.IsRunning)
return;
if (KCServer.server.ClientCount == 0)
return;
if (Villager.villagers == null || Villager.villagers.Count == 0)
return;
List<Guid> guids = new List<Guid>();
List<Vector3> positions = new List<Vector3>();
const int maxVillagersPerSnapshot = 200;
for (int i = 0; i < Villager.villagers.Count && guids.Count < maxVillagersPerSnapshot; i++)
{
Villager villager = Villager.villagers.data[i];
if (villager == null)
continue;
guids.Add(villager.guid);
positions.Add(villager.Pos);
}
if (guids.Count == 0)
return;
VillagerSnapshotPacket snapshot = new VillagerSnapshotPacket
{
guids = guids,
positions = positions
};
ushort exceptId = KCClient.client != null ? KCClient.client.Id : (ushort)0;
snapshot.SendToAll(exceptId);
}
public static void ApplyResourceSnapshot(List<int> resourceTypes, List<int> amounts)
{
if (resourceTypes == null || amounts == null)