first commit
This commit is contained in:
26
Packets/Game/Dragon/SpawnBabyDragonPacket.cs
Normal file
26
Packets/Game/Dragon/SpawnBabyDragonPacket.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
|
||||
namespace KCM.Packets.Game.Dragon
|
||||
{
|
||||
public class SpawnBabyDragonPacket : Packet
|
||||
{
|
||||
public override ushort packetId => (int)Enums.Packets.SpawnBabyDragon;
|
||||
|
||||
public Vector3 start { get; set; }
|
||||
|
||||
public override void HandlePacketClient()
|
||||
{
|
||||
DragonSpawn.inst.SpawnBabyDragon(start);
|
||||
}
|
||||
|
||||
public override void HandlePacketServer()
|
||||
{
|
||||
//throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
26
Packets/Game/Dragon/SpawnMamaDragonPacket.cs
Normal file
26
Packets/Game/Dragon/SpawnMamaDragonPacket.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
|
||||
namespace KCM.Packets.Game.Dragon
|
||||
{
|
||||
public class SpawnMamaDragonPacket : Packet
|
||||
{
|
||||
public override ushort packetId => (int)Enums.Packets.SpawnMamaDragon;
|
||||
|
||||
public Vector3 start { get; set; }
|
||||
|
||||
public override void HandlePacketClient()
|
||||
{
|
||||
DragonSpawn.inst.SpawnMamaDragon(start);
|
||||
}
|
||||
|
||||
public override void HandlePacketServer()
|
||||
{
|
||||
//throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
26
Packets/Game/Dragon/SpawnSiegeDragonPacket.cs
Normal file
26
Packets/Game/Dragon/SpawnSiegeDragonPacket.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
|
||||
namespace KCM.Packets.Game.Dragon
|
||||
{
|
||||
public class SpawnSiegeDragonPacket : Packet
|
||||
{
|
||||
public override ushort packetId => (int)Enums.Packets.SpawnSiegeDragon;
|
||||
|
||||
public Vector3 start { get; set; }
|
||||
|
||||
public override void HandlePacketClient()
|
||||
{
|
||||
DragonSpawn.inst.SpawnSiegeDragon(start);
|
||||
}
|
||||
|
||||
public override void HandlePacketServer()
|
||||
{
|
||||
//throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
27
Packets/Game/GameBuilding/CompleteBuild.cs
Normal file
27
Packets/Game/GameBuilding/CompleteBuild.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace KCM.Packets.Game.GameBuilding
|
||||
{
|
||||
public class CompleteBuild : Packet
|
||||
{
|
||||
public override ushort packetId => (int)Enums.Packets.CompleteBuild;
|
||||
|
||||
public Guid buildingId { get; set; }
|
||||
|
||||
public override void HandlePacketClient()
|
||||
{
|
||||
if (KCClient.client.Id == clientId) return;
|
||||
|
||||
Player.inst.GetBuilding(buildingId).CompleteBuild();
|
||||
}
|
||||
|
||||
public override void HandlePacketServer()
|
||||
{
|
||||
//throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
29
Packets/Game/GameBuilding/UpdateConstruction.cs
Normal file
29
Packets/Game/GameBuilding/UpdateConstruction.cs
Normal file
@@ -0,0 +1,29 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace KCM.Packets.Game.GameBuilding
|
||||
{
|
||||
public class UpdateConstruction : Packet
|
||||
{
|
||||
public override ushort packetId => (int)Enums.Packets.UpdateConstruction;
|
||||
|
||||
public Guid buildingId { get; set; }
|
||||
public float constructionProgress { get; set; }
|
||||
|
||||
public override void HandlePacketClient()
|
||||
{
|
||||
if (KCClient.client.Id == clientId) return;
|
||||
|
||||
//Main.helper.Log($"Received packet from: {clientId} receiving client is {KCClient.client.Id}");
|
||||
Player.inst.GetBuilding(buildingId).constructionProgress = constructionProgress;
|
||||
}
|
||||
|
||||
public override void HandlePacketServer()
|
||||
{
|
||||
//throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
42
Packets/Game/GamePlayer/AddVillagerPacket.cs
Normal file
42
Packets/Game/GamePlayer/AddVillagerPacket.cs
Normal file
@@ -0,0 +1,42 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
|
||||
namespace KCM.Packets.Game.GamePlayer
|
||||
{
|
||||
public class AddVillagerPacket : Packet
|
||||
{
|
||||
public override ushort packetId => (ushort)Enums.Packets.AddVillager;
|
||||
|
||||
public Guid guid { get; set; }
|
||||
|
||||
public override void HandlePacketClient()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (KCClient.client.Id == clientId) return;
|
||||
|
||||
Main.helper.Log("Received add villager packet from " + player.name + $"({player.id})");
|
||||
|
||||
Villager v = Villager.CreateVillager();
|
||||
v.guid = guid;
|
||||
|
||||
player.inst.Workers.Add(v);
|
||||
player.inst.Homeless.Add(v);
|
||||
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Main.helper.Log("Error handling add villager packet: " + e.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public override void HandlePacketServer()
|
||||
{
|
||||
//throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
35
Packets/Game/GamePlayer/SetupInitialWorkersPacket.cs
Normal file
35
Packets/Game/GamePlayer/SetupInitialWorkersPacket.cs
Normal file
@@ -0,0 +1,35 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace KCM.Packets.Game.GamePlayer
|
||||
{
|
||||
public class SetupInitialWorkersPacket : Packet
|
||||
{
|
||||
public override ushort packetId => (ushort)Enums.Packets.SetupInitialWorkers;
|
||||
|
||||
public Guid keepGuid { get; set; }
|
||||
|
||||
|
||||
public override void HandlePacketClient()
|
||||
{
|
||||
if (KCClient.client.Id == clientId) return;
|
||||
|
||||
/*Keep keep = player.inst.GetBuilding(keepGuid).GetComponent<Keep>();
|
||||
if (keep == null)
|
||||
{
|
||||
Main.helper.Log("Keep not found.");
|
||||
return;
|
||||
}
|
||||
|
||||
player.inst.SetupInitialWorkers(keep);*/
|
||||
}
|
||||
|
||||
public override void HandlePacketServer()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
30
Packets/Game/GameTrees/FellTree.cs
Normal file
30
Packets/Game/GameTrees/FellTree.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace KCM.Packets.Game.GameTrees
|
||||
{
|
||||
public class FellTree : Packet
|
||||
{
|
||||
public override ushort packetId => (int)Enums.Packets.FellTree;
|
||||
|
||||
public int idx { get; set; }
|
||||
|
||||
public int x { get; set; }
|
||||
public int z { get; set; }
|
||||
|
||||
public override void HandlePacketClient()
|
||||
{
|
||||
Cell cell = World.inst.GetCellData(x, z);
|
||||
|
||||
TreeSystem.inst.FellTree(cell, idx);
|
||||
}
|
||||
|
||||
public override void HandlePacketServer()
|
||||
{
|
||||
//throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
28
Packets/Game/GameTrees/GrowTree.cs
Normal file
28
Packets/Game/GameTrees/GrowTree.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace KCM.Packets.Game.GameTrees
|
||||
{
|
||||
public class GrowTree : Packet
|
||||
{
|
||||
public override ushort packetId => (int)Enums.Packets.GrowTree;
|
||||
|
||||
public int X { get; set; }
|
||||
public int Z { get; set; }
|
||||
|
||||
public override void HandlePacketClient()
|
||||
{
|
||||
Cell cell = World.inst.GetCellData(X, Z);
|
||||
|
||||
TreeSystem.inst.GrowTree(cell);
|
||||
}
|
||||
|
||||
public override void HandlePacketServer()
|
||||
{
|
||||
//throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
25
Packets/Game/GameTrees/ShakeTree.cs
Normal file
25
Packets/Game/GameTrees/ShakeTree.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace KCM.Packets.Game.GameTrees
|
||||
{
|
||||
public class ShakeTree : Packet
|
||||
{
|
||||
public override ushort packetId => (int)Enums.Packets.ShakeTree;
|
||||
|
||||
public int idx { get; set; }
|
||||
|
||||
public override void HandlePacketClient()
|
||||
{
|
||||
TreeSystem.inst.ShakeTree(idx);
|
||||
}
|
||||
|
||||
public override void HandlePacketServer()
|
||||
{
|
||||
//throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
38
Packets/Game/GameVillager/VillagerTeleportTo.cs
Normal file
38
Packets/Game/GameVillager/VillagerTeleportTo.cs
Normal file
@@ -0,0 +1,38 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
|
||||
namespace KCM.Packets.Game.GameVillager
|
||||
{
|
||||
public class VillagerTeleportTo : Packet
|
||||
{
|
||||
public override ushort packetId => (ushort)Enums.Packets.VillagerTeleportTo;
|
||||
|
||||
public Guid guid { get; set; }
|
||||
public Vector3 pos { get; set; }
|
||||
|
||||
public override void HandlePacketClient()
|
||||
{
|
||||
if (KCClient.client.Id == clientId) return;
|
||||
|
||||
try
|
||||
{
|
||||
Villager.villagers.data.Where(x => x.guid == guid).FirstOrDefault().TeleportTo(pos);
|
||||
|
||||
Main.helper.Log($"Teleporting villager to {pos.ToString()}");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Main.helper.Log("Error handling villager teleport packet: " + e.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public override void HandlePacketServer()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
25
Packets/Game/GameWeather/ChangeWeather.cs
Normal file
25
Packets/Game/GameWeather/ChangeWeather.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace KCM.Packets.Game.GameWeather
|
||||
{
|
||||
public class ChangeWeather : Packet
|
||||
{
|
||||
public override ushort packetId => (int)Enums.Packets.ChangeWeather;
|
||||
|
||||
public int weatherType { get; set; }
|
||||
|
||||
public override void HandlePacketClient()
|
||||
{
|
||||
Weather.CurrentWeather = ((Weather.WeatherType)weatherType);
|
||||
}
|
||||
|
||||
public override void HandlePacketServer()
|
||||
{
|
||||
//throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
145
Packets/Game/GameWorld/WorldPlace.cs
Normal file
145
Packets/Game/GameWorld/WorldPlace.cs
Normal file
@@ -0,0 +1,145 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.Serialization.Formatters.Binary;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
|
||||
namespace KCM.Packets.Game.GameWorld
|
||||
{
|
||||
public class WorldPlace : Packet
|
||||
{
|
||||
public override ushort packetId => (int)Enums.Packets.WorldPlace;
|
||||
|
||||
public string customName { get; set; }
|
||||
public Guid guid { get; set; }
|
||||
public string uniqueName { get; set; }
|
||||
public Quaternion rotation { get; set; }
|
||||
public Vector3 globalPosition { get; set; }
|
||||
public Vector3 localPosition { get; set; }
|
||||
public bool built { get; set; }
|
||||
public bool placed { get; set; }
|
||||
public bool open { get; set; }
|
||||
public bool doBuildAnimation { get; set; }
|
||||
public bool constructionPaused { get; set; }
|
||||
public float constructionProgress { get; set; }
|
||||
public float life { get; set; }
|
||||
public float ModifiedMaxLife { get; set; }
|
||||
public int yearBuilt { get; set; }
|
||||
public float decayProtection { get; set; }
|
||||
public bool seenByPlayer { get; set; }
|
||||
|
||||
public override void HandlePacketClient()
|
||||
{
|
||||
if (clientId == KCClient.client.Id) return; //prevent double placing on same client
|
||||
|
||||
PlaceBuilding();
|
||||
}
|
||||
|
||||
public override void HandlePacketServer()
|
||||
{
|
||||
//PlaceBuilding();
|
||||
|
||||
//SendToAll(clientId);
|
||||
}
|
||||
|
||||
public void PlaceBuilding()
|
||||
{
|
||||
Main.helper.Log("Received place building packet for " + uniqueName + " from " + player.name + $"({player.id})");
|
||||
|
||||
//var originalPlayer = Player.inst;
|
||||
//Player.inst = player.inst;
|
||||
|
||||
Building.BuildingSaveData structureData = new Building.BuildingSaveData()
|
||||
{
|
||||
uniqueName = uniqueName,
|
||||
customName = customName,
|
||||
guid = guid,
|
||||
rotation = rotation,
|
||||
globalPosition = globalPosition,
|
||||
localPosition = localPosition,
|
||||
built = built,
|
||||
placed = placed,
|
||||
open = open,
|
||||
doBuildAnimation = doBuildAnimation,
|
||||
constructionPaused = constructionPaused,
|
||||
constructionProgress = constructionProgress,
|
||||
life = life,
|
||||
ModifiedMaxLife = ModifiedMaxLife,
|
||||
//CollectForBuild = CollectForBuild,
|
||||
yearBuilt = yearBuilt,
|
||||
decayProtection = decayProtection,
|
||||
seenByPlayer = seenByPlayer
|
||||
};
|
||||
|
||||
|
||||
//Player originalInst = Player.inst;
|
||||
//Player.inst = player.inst;
|
||||
|
||||
Building Building = GameState.inst.GetPlaceableByUniqueName(structureData.uniqueName);
|
||||
bool flag = Building;
|
||||
if (flag)
|
||||
{
|
||||
Building building = UnityEngine.Object.Instantiate<Building>(Building);
|
||||
building.transform.position = structureData.globalPosition;
|
||||
Main.helper.Log("Building init");
|
||||
building.Init();
|
||||
building.transform.SetParent(player.inst.buildingContainer.transform, true);
|
||||
Main.helper.Log("Building unpack");
|
||||
structureData.Unpack(building);
|
||||
|
||||
Main.helper.Log(player.inst.ToString());
|
||||
Main.helper.Log((player.inst.PlayerLandmassOwner == null).ToString());
|
||||
Main.helper.Log(building.LandMass().ToString());
|
||||
Main.helper.Log("Player add Building unpacked");
|
||||
player.inst.AddBuilding(building);
|
||||
|
||||
try
|
||||
{
|
||||
|
||||
player.inst.PlayerLandmassOwner.TakeOwnership(building.LandMass());
|
||||
bool flag2 = building.GetComponent<Keep>() != null && building.TeamID() == player.inst.PlayerLandmassOwner.teamId;
|
||||
Main.helper.Log("Set keep " + flag2);
|
||||
if (flag2)
|
||||
{
|
||||
player.inst.keep = building.GetComponent<Keep>();
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Main.helper.Log(e.Message);
|
||||
}
|
||||
|
||||
Main.helper.Log("Place from load");
|
||||
Cell cell = World.inst.PlaceFromLoad(building);
|
||||
Main.helper.Log("unpack stage 2");
|
||||
structureData.UnpackStage2(building);
|
||||
|
||||
building.SetVisibleForFog(false);
|
||||
|
||||
Main.helper.Log("Landmass owner take ownership");
|
||||
|
||||
Main.helper.Log($"{player.id} (team {player.inst.PlayerLandmassOwner.teamId}) banner: {player.inst.PlayerLandmassOwner.bannerIdx} Placed building {building.name} at {building.transform.position}");
|
||||
|
||||
|
||||
//Player.inst = originalInst; // Reset player back to normal // Might not be needed anymore with player ref patches?
|
||||
|
||||
|
||||
Main.helper.Log($"Host player Landmass Names Count: {Player.inst.LandMassNames.Count}, Contents: {string.Join(", ", Player.inst.LandMassNames)}");
|
||||
Main.helper.Log($"Client player ({player.name}) Landmass Names Count: {player.inst.LandMassNames.Count}, Contents: {string.Join(", ", player.inst.LandMassNames)}");
|
||||
|
||||
player.inst.LandMassNames[building.LandMass()] = player.kingdomName;
|
||||
Player.inst.LandMassNames[building.LandMass()] = player.kingdomName;
|
||||
|
||||
//Player.inst = originalPlayer;
|
||||
}
|
||||
else
|
||||
{
|
||||
Main.helper.Log(structureData.uniqueName + " failed to load correctly");
|
||||
}
|
||||
//building.Init();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
102
Packets/Game/PlaceKeepRandomly.cs
Normal file
102
Packets/Game/PlaceKeepRandomly.cs
Normal file
@@ -0,0 +1,102 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Analytics;
|
||||
|
||||
namespace KCM.Packets.Game
|
||||
{
|
||||
public class PlaceKeepRandomly : Packet
|
||||
{
|
||||
public override ushort packetId => (ushort)Enums.Packets.PlaceKeepRandomly;
|
||||
|
||||
public int landmassIdx { get; set; }
|
||||
|
||||
public override void HandlePacketClient()
|
||||
{
|
||||
try
|
||||
{
|
||||
Building keep = UnityEngine.Object.Instantiate<Building>(GameState.inst.GetPlaceableByUniqueName(World.keepName));
|
||||
|
||||
keep.Init();
|
||||
|
||||
|
||||
Cell[] cells = World.inst.GetCellsData().Where(x => x.landMassIdx == landmassIdx).ToArray();
|
||||
Cell keepCell = null;
|
||||
|
||||
|
||||
foreach (Cell cell in cells)
|
||||
{
|
||||
Cell nearbyStoneCell = FindNearbyStoneCell(cells, cell.x, cell.z, landmassIdx, 15); // Place keep within 15 tiles of stone
|
||||
Cell nearbyWaterCell = FindNearbyWaterCell(cells, cell.x, cell.z, landmassIdx, 6); // Do not place keep within 6 tiles of water
|
||||
|
||||
|
||||
Cell clearCell = FindClearCell(cells, cell.x, cell.z, landmassIdx, 4); // cells in 4 by 4 radius are clear?
|
||||
|
||||
if (clearCell != null & nearbyStoneCell != null && nearbyWaterCell == null && cell.Type == ResourceType.None)
|
||||
{
|
||||
Console.WriteLine($"Nearby stone cell found at ({nearbyStoneCell.x}, {nearbyStoneCell.z})");
|
||||
keepCell = cell;
|
||||
|
||||
break;
|
||||
}
|
||||
else
|
||||
continue;
|
||||
|
||||
}
|
||||
|
||||
keep.transform.position = keepCell.Position;
|
||||
|
||||
keep.SendMessage("OnPlayerPlacement", SendMessageOptions.DontRequireReceiver);
|
||||
|
||||
|
||||
Player.inst.PlayerLandmassOwner.TakeOwnership(keep.LandMass());
|
||||
Player.inst.keep = keep.GetComponent<Keep>();
|
||||
Player.inst.RefreshVisibility(true);
|
||||
RandomPlacement(keep);
|
||||
|
||||
} catch (Exception e)
|
||||
{
|
||||
Main.helper.Log($"Error placing keep randomly: {e.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
private void RandomPlacement(Building keep) // This is a hack so I can detect when its being called by this packet
|
||||
{
|
||||
World.inst.Place(keep);
|
||||
|
||||
Cam.inst.SetTrackingPos(keep.GetPosition());
|
||||
}
|
||||
|
||||
private static Cell FindNearbyStoneCell(Cell[] cells, int x, int z, int landmassIdx, int radius)
|
||||
{
|
||||
return cells.FirstOrDefault(cell => IsResourceInRadius(cell, x, z, radius, ResourceType.Stone));
|
||||
}
|
||||
|
||||
private static Cell FindNearbyWaterCell(Cell[] cells, int x, int z, int landmassIdx, int radius)
|
||||
{
|
||||
return cells.FirstOrDefault(cell => IsResourceInRadius(cell, x, z, radius, ResourceType.Water));
|
||||
}
|
||||
private static Cell FindClearCell(Cell[] cells, int x, int z, int landmassIdx, int radius)
|
||||
{
|
||||
return cells.FirstOrDefault(cell => IsResourceInRadius(cell, x, z, radius, ResourceType.None));
|
||||
}
|
||||
|
||||
private static bool IsResourceInRadius(Cell cell, int x, int z, int radius, ResourceType desiredResource)
|
||||
{
|
||||
bool isWithinRadius = Math.Sqrt((cell.x - x) * (cell.x - x) + (cell.z - z) * (cell.z - z)) <= radius;
|
||||
bool isNotCentralCell = cell.x != x || cell.z != z;
|
||||
bool isStoneType = cell.Type == desiredResource;
|
||||
|
||||
bool isWater = desiredResource == ResourceType.Water ? false : cell.deepWater || cell.Type == ResourceType.Water;
|
||||
|
||||
return isWithinRadius && isNotCentralCell && isStoneType && !isWater;
|
||||
}
|
||||
|
||||
public override void HandlePacketServer()
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
28
Packets/Game/SetSpeed.cs
Normal file
28
Packets/Game/SetSpeed.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace KCM.Packets.Game
|
||||
{
|
||||
public class SetSpeed : Packet
|
||||
{
|
||||
public override ushort packetId => (int)Enums.Packets.SetSpeed;
|
||||
|
||||
public int speed { get; set; }
|
||||
|
||||
public override void HandlePacketClient()
|
||||
{
|
||||
if (clientId == KCClient.client.Id) // Prevent speed softlock
|
||||
return;
|
||||
|
||||
SpeedControlUI.inst.SetSpeed(speed);
|
||||
}
|
||||
|
||||
public override void HandlePacketServer()
|
||||
{
|
||||
//throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
203
Packets/Handlers/LobbyHandler.cs
Normal file
203
Packets/Handlers/LobbyHandler.cs
Normal file
@@ -0,0 +1,203 @@
|
||||
using Assets.Code;
|
||||
using Assets;
|
||||
using KCM.Attributes;
|
||||
using KCM.Packets.Lobby;
|
||||
using KCM.Packets.Network;
|
||||
using KCM.ServerLobby;
|
||||
using KCM.ServerLobby.LobbyChat;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using System.Reflection;
|
||||
|
||||
namespace KCM.Packets.Handlers
|
||||
{
|
||||
public class LobbyHandler
|
||||
{
|
||||
public static ServerSettings ServerSettings = new ServerSettings();
|
||||
|
||||
public static List<GameObject> playerEntries = new List<GameObject>();
|
||||
|
||||
|
||||
public static void ClearPlayerList()
|
||||
{
|
||||
try
|
||||
{
|
||||
foreach (GameObject entry in playerEntries)
|
||||
GameObject.Destroy(entry);
|
||||
|
||||
playerEntries.Clear();
|
||||
|
||||
if (!KCServer.IsRunning)
|
||||
{
|
||||
Main.kCPlayers.Clear();
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Main.helper.Log("----------------------- Main exception -----------------------");
|
||||
Main.helper.Log(ex.ToString());
|
||||
Main.helper.Log("----------------------- Main message -----------------------");
|
||||
Main.helper.Log(ex.Message);
|
||||
Main.helper.Log("----------------------- Main stacktrace -----------------------");
|
||||
Main.helper.Log(ex.StackTrace);
|
||||
if (ex.InnerException != null)
|
||||
{
|
||||
Main.helper.Log("----------------------- Inner exception -----------------------");
|
||||
Main.helper.Log(ex.InnerException.ToString());
|
||||
Main.helper.Log("----------------------- Inner message -----------------------");
|
||||
Main.helper.Log(ex.InnerException.Message);
|
||||
Main.helper.Log("----------------------- Inner stacktrace -----------------------");
|
||||
Main.helper.Log(ex.InnerException.StackTrace);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void AddPlayerEntry(ushort client)
|
||||
{
|
||||
try
|
||||
{
|
||||
GameObject entry = GameObject.Instantiate(PrefabManager.serverLobbyPlayerEntryPrefab, ServerLobbyScript.PlayerListContent);
|
||||
entry.SetActive(true);
|
||||
Main.helper.Log(entry.ToString());
|
||||
var s = entry.AddComponent<PlayerEntryScript>();
|
||||
|
||||
s.Client = client;
|
||||
|
||||
playerEntries.Add(entry);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Main.helper.Log("----------------------- Main exception -----------------------");
|
||||
Main.helper.Log(ex.ToString());
|
||||
Main.helper.Log("----------------------- Main message -----------------------");
|
||||
Main.helper.Log(ex.Message);
|
||||
Main.helper.Log("----------------------- Main stacktrace -----------------------");
|
||||
Main.helper.Log(ex.StackTrace);
|
||||
if (ex.InnerException != null)
|
||||
{
|
||||
Main.helper.Log("----------------------- Inner exception -----------------------");
|
||||
Main.helper.Log(ex.InnerException.ToString());
|
||||
Main.helper.Log("----------------------- Inner message -----------------------");
|
||||
Main.helper.Log(ex.InnerException.Message);
|
||||
Main.helper.Log("----------------------- Inner stacktrace -----------------------");
|
||||
Main.helper.Log(ex.InnerException.StackTrace);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void AddSystemMessage(string message)
|
||||
{
|
||||
try
|
||||
{
|
||||
GameObject entry = GameObject.Instantiate(PrefabManager.serverChatSystemEntryPrefab, ServerLobbyScript.PlayerChatContent);
|
||||
entry.SetActive(true);
|
||||
chatEntries.Add(entry);
|
||||
var s = entry.AddComponent<SystemEntryScript>();
|
||||
|
||||
|
||||
SnapTo(entry.GetComponent<RectTransform>());
|
||||
|
||||
s.Message = message;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Main.helper.Log("----------------------- Main exception -----------------------");
|
||||
Main.helper.Log(ex.ToString());
|
||||
Main.helper.Log("----------------------- Main message -----------------------");
|
||||
Main.helper.Log(ex.Message);
|
||||
Main.helper.Log("----------------------- Main stacktrace -----------------------");
|
||||
Main.helper.Log(ex.StackTrace);
|
||||
if (ex.InnerException != null)
|
||||
{
|
||||
Main.helper.Log("----------------------- Inner exception -----------------------");
|
||||
Main.helper.Log(ex.InnerException.ToString());
|
||||
Main.helper.Log("----------------------- Inner message -----------------------");
|
||||
Main.helper.Log(ex.InnerException.Message);
|
||||
Main.helper.Log("----------------------- Inner stacktrace -----------------------");
|
||||
Main.helper.Log(ex.InnerException.StackTrace);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static List<GameObject> chatEntries = new List<GameObject>();
|
||||
|
||||
public static void AddChatMessage(ushort client, string player, string message)
|
||||
{
|
||||
try
|
||||
{
|
||||
GameObject entry = GameObject.Instantiate(PrefabManager.serverChatEntryPrefab, ServerLobbyScript.PlayerChatContent);
|
||||
entry.SetActive(true);
|
||||
|
||||
chatEntries.Add(entry);
|
||||
|
||||
var s = entry.AddComponent<ChatEntryScript>();
|
||||
|
||||
SnapTo(entry.GetComponent<RectTransform>());
|
||||
|
||||
s.Client = client;
|
||||
s.PlayerName = player;
|
||||
s.Message = message;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Main.helper.Log("----------------------- Main exception -----------------------");
|
||||
Main.helper.Log(ex.ToString());
|
||||
Main.helper.Log("----------------------- Main message -----------------------");
|
||||
Main.helper.Log(ex.Message);
|
||||
Main.helper.Log("----------------------- Main stacktrace -----------------------");
|
||||
Main.helper.Log(ex.StackTrace);
|
||||
if (ex.InnerException != null)
|
||||
{
|
||||
Main.helper.Log("----------------------- Inner exception -----------------------");
|
||||
Main.helper.Log(ex.InnerException.ToString());
|
||||
Main.helper.Log("----------------------- Inner message -----------------------");
|
||||
Main.helper.Log(ex.InnerException.Message);
|
||||
Main.helper.Log("----------------------- Inner stacktrace -----------------------");
|
||||
Main.helper.Log(ex.InnerException.StackTrace);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void ClearChatEntries()
|
||||
{
|
||||
try
|
||||
{
|
||||
foreach (GameObject entry in chatEntries)
|
||||
GameObject.Destroy(entry);
|
||||
|
||||
chatEntries.Clear();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Main.helper.Log("----------------------- Main exception -----------------------");
|
||||
Main.helper.Log(ex.ToString());
|
||||
Main.helper.Log("----------------------- Main message -----------------------");
|
||||
Main.helper.Log(ex.Message);
|
||||
Main.helper.Log("----------------------- Main stacktrace -----------------------");
|
||||
Main.helper.Log(ex.StackTrace);
|
||||
if (ex.InnerException != null)
|
||||
{
|
||||
Main.helper.Log("----------------------- Inner exception -----------------------");
|
||||
Main.helper.Log(ex.InnerException.ToString());
|
||||
Main.helper.Log("----------------------- Inner message -----------------------");
|
||||
Main.helper.Log(ex.InnerException.Message);
|
||||
Main.helper.Log("----------------------- Inner stacktrace -----------------------");
|
||||
Main.helper.Log(ex.InnerException.StackTrace);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static void SnapTo(RectTransform target)
|
||||
{
|
||||
Canvas.ForceUpdateCanvases();
|
||||
|
||||
target.parent.parent.parent.GetComponent<ScrollRect>().normalizedPosition = new Vector2(0, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
604
Packets/Handlers/PacketHandler.cs
Normal file
604
Packets/Handlers/PacketHandler.cs
Normal file
@@ -0,0 +1,604 @@
|
||||
using KCM.Attributes;
|
||||
using KCM.Packets.Lobby;
|
||||
using KCM.Packets.Network;
|
||||
using Riptide;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
|
||||
namespace KCM.Packets.Handlers
|
||||
{
|
||||
public class PacketHandler
|
||||
{
|
||||
public static Dictionary<ushort, PacketRef> Packets = new Dictionary<ushort, PacketRef>();
|
||||
public class PacketRef
|
||||
{
|
||||
public IPacket packet;
|
||||
public PropertyInfo[] properties;
|
||||
|
||||
public PacketRef(IPacket packet, PropertyInfo[] properties)
|
||||
{
|
||||
this.packet = packet;
|
||||
this.properties = properties;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static Dictionary<ushort, PacketHandlerDelegate> PacketHandlers = new Dictionary<ushort, PacketHandlerDelegate>();
|
||||
public delegate void PacketHandlerDelegate(IPacket packet);
|
||||
|
||||
public static void Initialise()
|
||||
{
|
||||
try
|
||||
{
|
||||
Main.helper.Log("Loading Packet Handlers...");
|
||||
|
||||
//TO-DO Remove this. Packets now have "handle packet" method
|
||||
#region "Register server packet handlers"
|
||||
|
||||
var serverPacketHandlers = Assembly.GetExecutingAssembly().GetTypes().SelectMany(t => t.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static))
|
||||
.Where(m => m.GetCustomAttributes(typeof(PacketHandlerAttribute), false).Length > 0)
|
||||
.ToArray();
|
||||
|
||||
foreach (MethodInfo method in serverPacketHandlers)
|
||||
{
|
||||
PacketHandlerAttribute attribute = method.GetCustomAttribute<PacketHandlerAttribute>();
|
||||
|
||||
|
||||
if (!method.IsStatic)
|
||||
throw new NonStaticHandlerException(method.DeclaringType, method.Name);
|
||||
|
||||
Delegate packetHandler = Delegate.CreateDelegate(typeof(PacketHandlerDelegate), method, false);
|
||||
if (packetHandler != null)
|
||||
{
|
||||
// It's a message handler for Client instances
|
||||
if (PacketHandlers.ContainsKey(attribute.packetId))
|
||||
{
|
||||
MethodInfo otherMethodWithId = PacketHandlers[attribute.packetId].GetMethodInfo();
|
||||
throw new DuplicateHandlerException(attribute.packetId, method, otherMethodWithId);
|
||||
}
|
||||
else
|
||||
PacketHandlers.Add(attribute.packetId, (PacketHandlerDelegate)packetHandler);
|
||||
}
|
||||
else
|
||||
{
|
||||
Main.helper.Log($"Failed to register handler: {method.Name}");
|
||||
}
|
||||
}
|
||||
|
||||
Main.helper.Log($"Loaded {PacketHandlers.Count} server handlers");
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
Main.helper.Log("Loading packets...");
|
||||
|
||||
var packets = Assembly.GetExecutingAssembly().GetTypes().Where(t => t != null && t.Namespace != null && t.Namespace.StartsWith("KCM.Packets") && !t.IsAbstract && !t.IsInterface && typeof(IPacket).IsAssignableFrom(t)).ToList();
|
||||
|
||||
foreach (var packet in packets)
|
||||
{
|
||||
|
||||
IPacket p = (IPacket)Activator.CreateInstance(packet);
|
||||
var properties = packet.GetProperties(BindingFlags.Instance | BindingFlags.Public).Where(prop => prop.Name != "packetId").ToArray();
|
||||
Array.Sort(properties, (x, y) => String.Compare(x.Name, y.Name));
|
||||
ushort id = (ushort)p.GetType().GetProperty("packetId").GetValue(p, null);
|
||||
|
||||
if (p.GetType() == typeof(SaveTransferPacket))
|
||||
{
|
||||
Main.helper.Log("SaveTransferPacket");
|
||||
Main.helper.Log(string.Join("\n", properties.Select(x => x.Name).ToArray()));
|
||||
}
|
||||
|
||||
Packets.Add(id, new PacketRef(p, properties));
|
||||
Main.helper.Log($"- Registered Packet: {id} {packet.FullName}");
|
||||
|
||||
}
|
||||
|
||||
Main.helper.Log($"Loaded {Packets.Count} packets");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Main.helper.Log("----------------------- Main exception -----------------------");
|
||||
Main.helper.Log(ex.ToString());
|
||||
Main.helper.Log("----------------------- Main message -----------------------");
|
||||
Main.helper.Log(ex.Message);
|
||||
Main.helper.Log("----------------------- Main stacktrace -----------------------");
|
||||
Main.helper.Log(ex.StackTrace);
|
||||
if (ex.InnerException != null)
|
||||
{
|
||||
Main.helper.Log("----------------------- Inner exception -----------------------");
|
||||
Main.helper.Log(ex.InnerException.ToString());
|
||||
Main.helper.Log("----------------------- Inner message -----------------------");
|
||||
Main.helper.Log(ex.InnerException.Message);
|
||||
Main.helper.Log("----------------------- Inner stacktrace -----------------------");
|
||||
Main.helper.Log(ex.InnerException.StackTrace);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void HandlePacketServer(object sender, MessageReceivedEventArgs messageReceived)
|
||||
{
|
||||
var id = messageReceived.MessageId;
|
||||
|
||||
|
||||
IPacket packet = DeserialisePacket(messageReceived);
|
||||
|
||||
//Main.helper.Log($"Server Received packet {Packets[id].packet.GetType().Name} from {messageReceived.FromConnection.Id}");
|
||||
|
||||
|
||||
if (KCServer.IsRunning)
|
||||
{
|
||||
try
|
||||
{
|
||||
packet.HandlePacketServer();
|
||||
|
||||
((Packet)packet).SendToAll();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Main.helper.Log($"Error handling packet {id} {packet.GetType().Name} from {packet.clientId}");
|
||||
|
||||
Main.helper.Log("----------------------- Main exception -----------------------");
|
||||
Main.helper.Log(ex.ToString());
|
||||
Main.helper.Log("----------------------- Main message -----------------------");
|
||||
Main.helper.Log(ex.Message);
|
||||
Main.helper.Log("----------------------- Main stacktrace -----------------------");
|
||||
Main.helper.Log(ex.StackTrace);
|
||||
if (ex.InnerException != null)
|
||||
{
|
||||
Main.helper.Log("----------------------- Inner exception -----------------------");
|
||||
Main.helper.Log(ex.InnerException.ToString());
|
||||
Main.helper.Log("----------------------- Inner message -----------------------");
|
||||
Main.helper.Log(ex.InnerException.Message);
|
||||
Main.helper.Log("----------------------- Inner stacktrace -----------------------");
|
||||
Main.helper.Log(ex.InnerException.StackTrace);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void HandlePacket(object sender, MessageReceivedEventArgs messageReceived)
|
||||
{
|
||||
try
|
||||
{
|
||||
var id = messageReceived.MessageId;
|
||||
|
||||
|
||||
//Main.helper.Log($"Client Received packet {Packets[id].packet.GetType().Name} from {messageReceived.FromConnection.Id}");
|
||||
|
||||
IPacket packet = DeserialisePacket(messageReceived);
|
||||
|
||||
//Main.helper.Log($"Client Received packet {Packets[id].packet.GetType().Name} from {packet.clientId}");
|
||||
|
||||
if (KCClient.client.IsConnected)
|
||||
{
|
||||
try
|
||||
{
|
||||
packet.HandlePacketClient();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Main.helper.Log($"Error handling packet {id} {packet.GetType().Name} from {packet.clientId}");
|
||||
|
||||
Main.helper.Log("----------------------- Main exception -----------------------");
|
||||
Main.helper.Log(ex.ToString());
|
||||
Main.helper.Log("----------------------- Main message -----------------------");
|
||||
Main.helper.Log(ex.Message);
|
||||
Main.helper.Log("----------------------- Main stacktrace -----------------------");
|
||||
Main.helper.Log(ex.StackTrace);
|
||||
if (ex.InnerException != null)
|
||||
{
|
||||
Main.helper.Log("----------------------- Inner exception -----------------------");
|
||||
Main.helper.Log(ex.InnerException.ToString());
|
||||
Main.helper.Log("----------------------- Inner message -----------------------");
|
||||
Main.helper.Log(ex.InnerException.Message);
|
||||
Main.helper.Log("----------------------- Inner stacktrace -----------------------");
|
||||
Main.helper.Log(ex.InnerException.StackTrace);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* if (PacketHandlers.TryGetValue(id, out PacketHandlerDelegate handler))
|
||||
handler(packet);*/
|
||||
|
||||
// Main.helper.Log($"{(KCServer.IsRunning ? "Server" : "Client")} Received packet {id} {packet.GetType().kingdomName}");
|
||||
//Main.helper.Log($"Found handler: {(handler != null).ToString()}");
|
||||
}
|
||||
catch
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public static Message SerialisePacket(IPacket packet)
|
||||
{
|
||||
|
||||
var currentPropName = "";
|
||||
try
|
||||
{
|
||||
var packetRef = Packets[packet.packetId];
|
||||
Message message = Message.Create(MessageSendMode.Reliable, packet.packetId);
|
||||
|
||||
foreach (var prop in packetRef.properties)
|
||||
{
|
||||
if (prop.PropertyType.IsEnum)
|
||||
{
|
||||
currentPropName = prop.Name;
|
||||
message.AddInt((int)prop.GetValue(packet, null));
|
||||
}
|
||||
else if (prop.PropertyType == typeof(ushort))
|
||||
{
|
||||
currentPropName = prop.Name;
|
||||
message.AddUShort((ushort)prop.GetValue(packet, null));
|
||||
}
|
||||
else if (prop.PropertyType == typeof(bool))
|
||||
{
|
||||
currentPropName = prop.Name;
|
||||
message.AddBool((bool)prop.GetValue(packet, null));
|
||||
}
|
||||
else if (prop.PropertyType == typeof(int))
|
||||
{
|
||||
currentPropName = prop.Name;
|
||||
message.AddInt((int)prop.GetValue(packet, null));
|
||||
}
|
||||
else if (prop.PropertyType == typeof(string))
|
||||
{
|
||||
currentPropName = prop.Name;
|
||||
message.AddString((string)prop.GetValue(packet, null));
|
||||
}
|
||||
else if (prop.PropertyType == typeof(float))
|
||||
{
|
||||
currentPropName = prop.Name;
|
||||
message.AddFloat((float)prop.GetValue(packet, null));
|
||||
}
|
||||
else if (prop.PropertyType == typeof(double))
|
||||
{
|
||||
currentPropName = prop.Name;
|
||||
message.AddDouble((double)prop.GetValue(packet, null));
|
||||
}
|
||||
else if (prop.PropertyType == typeof(byte[]))
|
||||
{
|
||||
currentPropName = prop.Name;
|
||||
byte[] bytes = (byte[])prop.GetValue(packet, null);
|
||||
message.AddBytes(bytes, true);
|
||||
}
|
||||
else if (prop.PropertyType == typeof(List<string>))
|
||||
{
|
||||
currentPropName = prop.Name;
|
||||
List<string> list = (List<string>)prop.GetValue(packet, null);
|
||||
message.AddInt(list.Count);
|
||||
foreach (var item in list)
|
||||
message.AddString(item);
|
||||
}
|
||||
else if (prop.PropertyType == typeof(List<bool>))
|
||||
{
|
||||
currentPropName = prop.Name;
|
||||
List<bool> list = (List<bool>)prop.GetValue(packet, null);
|
||||
message.AddInt(list.Count);
|
||||
foreach (var item in list)
|
||||
message.AddBool(item);
|
||||
}
|
||||
else if (prop.PropertyType == typeof(List<ushort>))
|
||||
{
|
||||
currentPropName = prop.Name;
|
||||
List<ushort> list = (List<ushort>)prop.GetValue(packet, null);
|
||||
message.AddInt(list.Count);
|
||||
foreach (var item in list)
|
||||
message.AddUShort(item);
|
||||
}
|
||||
else if (prop.PropertyType == typeof(List<int>))
|
||||
{
|
||||
currentPropName = prop.Name;
|
||||
List<int> list = (List<int>)prop.GetValue(packet, null);
|
||||
message.AddInt(list.Count);
|
||||
foreach (var item in list)
|
||||
message.AddInt(item);
|
||||
}
|
||||
|
||||
else if (prop.PropertyType.IsGenericType && prop.PropertyType.GetGenericTypeDefinition() == typeof(Dictionary<,>))
|
||||
{
|
||||
currentPropName = prop.Name;
|
||||
Type[] argumentTypes = prop.PropertyType.GetGenericArguments();
|
||||
Type keyType = argumentTypes[0];
|
||||
Type valueType = argumentTypes[1];
|
||||
|
||||
object dictionary = prop.GetValue(packet, null);
|
||||
|
||||
int count = (int)dictionary.GetType().GetProperty("Count").GetValue(dictionary, null);
|
||||
|
||||
var enumerator = ((IEnumerable)dictionary).GetEnumerator();
|
||||
while (enumerator.MoveNext())
|
||||
{
|
||||
object key = enumerator.Current.GetType().GetProperty("Key").GetValue(enumerator.Current, null);
|
||||
object value = enumerator.Current.GetType().GetProperty("Value").GetValue(enumerator.Current, null);
|
||||
|
||||
Main.helper.Log($"Key: {key.GetType()}, Value: {value.GetType()}");
|
||||
}
|
||||
}
|
||||
else if (prop.PropertyType == typeof(Vector3))
|
||||
{
|
||||
currentPropName = prop.Name;
|
||||
Vector3 vector = (Vector3)prop.GetValue(packet, null);
|
||||
message.AddFloat(vector.x);
|
||||
message.AddFloat(vector.y);
|
||||
message.AddFloat(vector.z);
|
||||
}
|
||||
else if (prop.PropertyType == typeof(Quaternion))
|
||||
{
|
||||
currentPropName = prop.Name;
|
||||
Quaternion quaternion = (Quaternion)prop.GetValue(packet, null);
|
||||
message.AddFloat(quaternion.x);
|
||||
message.AddFloat(quaternion.y);
|
||||
message.AddFloat(quaternion.z);
|
||||
message.AddFloat(quaternion.w);
|
||||
}
|
||||
else if (prop.PropertyType == typeof(Guid))
|
||||
{
|
||||
currentPropName = prop.Name;
|
||||
Guid guid = (Guid)prop.GetValue(packet, null);
|
||||
message.AddBytes(guid.ToByteArray());
|
||||
}
|
||||
else if (prop.PropertyType.IsGenericType && prop.PropertyType.GetGenericTypeDefinition() == typeof(List<>))
|
||||
{
|
||||
currentPropName = prop.Name;
|
||||
|
||||
Type itemType = prop.PropertyType.GetGenericArguments()[0];
|
||||
|
||||
var list = prop.GetValue(packet, null) as System.Collections.IList;
|
||||
if (list != null)
|
||||
{
|
||||
message.AddInt(list.Count);
|
||||
|
||||
foreach (var item in list)
|
||||
{
|
||||
if (itemType.IsClass && itemType != typeof(string) || itemType.IsValueType && !itemType.IsPrimitive)
|
||||
{
|
||||
var fields = itemType.GetFields(); // Get fields
|
||||
Array.Sort(fields, (x, y) => String.Compare(x.Name, y.Name));
|
||||
var properties = itemType.GetProperties(); // Get properties
|
||||
Array.Sort(properties, (x, y) => String.Compare(x.Name, y.Name));
|
||||
|
||||
|
||||
// Serialize fields
|
||||
foreach (var field in fields)
|
||||
{
|
||||
var fieldValue = field.GetValue(item);
|
||||
AddDynamic(message, fieldValue);
|
||||
}
|
||||
|
||||
// Serialize properties
|
||||
foreach (var property in properties)
|
||||
{
|
||||
var propertyValue = property.GetValue(item);
|
||||
AddDynamic(message, propertyValue);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
AddDynamic(message, item);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// You can add more types as needed
|
||||
}
|
||||
|
||||
|
||||
return message;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Main.helper.Log($"Failed to serialise packet {packet.packetId} {packet.GetType().Name} at {currentPropName}");
|
||||
|
||||
Main.helper.Log("----------------------- Main exception -----------------------");
|
||||
Main.helper.Log(ex.ToString());
|
||||
Main.helper.Log("----------------------- Main message -----------------------");
|
||||
Main.helper.Log(ex.Message);
|
||||
Main.helper.Log("----------------------- Main stacktrace -----------------------");
|
||||
Main.helper.Log(ex.StackTrace);
|
||||
if (ex.InnerException != null)
|
||||
{
|
||||
Main.helper.Log("----------------------- Inner exception -----------------------");
|
||||
Main.helper.Log(ex.InnerException.ToString());
|
||||
Main.helper.Log("----------------------- Inner message -----------------------");
|
||||
Main.helper.Log(ex.InnerException.Message);
|
||||
Main.helper.Log("----------------------- Inner stacktrace -----------------------");
|
||||
Main.helper.Log(ex.InnerException.StackTrace);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
static void AddDynamic(Message message, object value)
|
||||
{
|
||||
if (value is int intValue)
|
||||
message.AddInt(intValue);
|
||||
else if (value is string stringValue)
|
||||
message.AddString(stringValue);
|
||||
else if (value is bool boolValue)
|
||||
message.AddBool(boolValue);
|
||||
else if (value is float floatValue)
|
||||
message.AddFloat(floatValue);
|
||||
else if (value is double doubleValue)
|
||||
message.AddDouble(doubleValue);
|
||||
else if (value is Vector3 vector)
|
||||
{
|
||||
message.AddFloat(vector.x);
|
||||
message.AddFloat(vector.y);
|
||||
message.AddFloat(vector.z);
|
||||
}
|
||||
else if (value is Quaternion quaternion)
|
||||
{
|
||||
message.AddFloat(quaternion.x);
|
||||
message.AddFloat(quaternion.y);
|
||||
message.AddFloat(quaternion.z);
|
||||
message.AddFloat(quaternion.w);
|
||||
}
|
||||
else if (value is Guid guid)
|
||||
message.AddBytes(guid.ToByteArray());
|
||||
// Add more type checks as necessary
|
||||
else
|
||||
throw new NotImplementedException($"Type {value.GetType()} serialization not implemented.");
|
||||
}
|
||||
|
||||
|
||||
public static IPacket DeserialisePacket(MessageReceivedEventArgs messageReceived)
|
||||
{
|
||||
try
|
||||
{
|
||||
var message = messageReceived.Message;
|
||||
var packetRef = Packets[messageReceived.MessageId];
|
||||
IPacket p = (IPacket)Activator.CreateInstance(packetRef.packet.GetType());
|
||||
|
||||
|
||||
foreach (var prop in packetRef.properties)
|
||||
{
|
||||
if (prop.PropertyType.IsEnum)
|
||||
{
|
||||
int enumValue = message.GetInt();
|
||||
string enumName = Enum.GetName(prop.PropertyType, enumValue);
|
||||
|
||||
prop.SetValue(p, Enum.Parse(prop.PropertyType, enumName));
|
||||
}
|
||||
else if (prop.PropertyType == typeof(ushort))
|
||||
{
|
||||
prop.SetValue(p, message.GetUShort());
|
||||
}
|
||||
else if (prop.PropertyType == typeof(bool))
|
||||
{
|
||||
prop.SetValue(p, message.GetBool());
|
||||
}
|
||||
else if (prop.PropertyType == typeof(int))
|
||||
{
|
||||
prop.SetValue(p, message.GetInt());
|
||||
}
|
||||
else if (prop.PropertyType == typeof(string))
|
||||
{
|
||||
prop.SetValue(p, message.GetString());
|
||||
}
|
||||
else if (prop.PropertyType == typeof(float))
|
||||
{
|
||||
prop.SetValue(p, message.GetFloat());
|
||||
}
|
||||
else if (prop.PropertyType == typeof(double))
|
||||
{
|
||||
prop.SetValue(p, message.GetDouble());
|
||||
}
|
||||
else if (prop.PropertyType == typeof(byte[]))
|
||||
{
|
||||
byte[] bytes = message.GetBytes();
|
||||
|
||||
prop.SetValue(p, bytes);
|
||||
}
|
||||
else if (prop.PropertyType == typeof(List<string>))
|
||||
{
|
||||
int count = message.GetInt();
|
||||
List<string> list = new List<string>();
|
||||
|
||||
for (int i = 0; i < count; i++)
|
||||
list.Add(message.GetString());
|
||||
|
||||
prop.SetValue(p, list);
|
||||
}
|
||||
else if (prop.PropertyType == typeof(List<bool>))
|
||||
{
|
||||
int count = message.GetInt();
|
||||
List<bool> list = new List<bool>();
|
||||
|
||||
for (int i = 0; i < count; i++)
|
||||
list.Add(message.GetBool());
|
||||
|
||||
prop.SetValue(p, list);
|
||||
}
|
||||
else if (prop.PropertyType == typeof(List<ushort>))
|
||||
{
|
||||
int count = message.GetInt();
|
||||
List<ushort> list = new List<ushort>();
|
||||
|
||||
for (int i = 0; i < count; i++)
|
||||
list.Add(message.GetUShort());
|
||||
|
||||
prop.SetValue(p, list);
|
||||
}
|
||||
else if (prop.PropertyType == typeof(List<int>))
|
||||
{
|
||||
int count = message.GetInt();
|
||||
List<int> list = new List<int>();
|
||||
|
||||
for (int i = 0; i < count; i++)
|
||||
list.Add(message.GetInt());
|
||||
|
||||
prop.SetValue(p, list);
|
||||
}
|
||||
else if (prop.PropertyType.IsGenericType && prop.PropertyType.GetGenericTypeDefinition() == typeof(Dictionary<,>))
|
||||
{
|
||||
IDictionary dictionary = (IDictionary)prop.GetValue(p, null);
|
||||
Type[] argumentTypes = prop.PropertyType.GetGenericArguments();
|
||||
Type keyType = argumentTypes[0];
|
||||
Type valueType = argumentTypes[1];
|
||||
|
||||
|
||||
message.AddInt(dictionary.Count);
|
||||
|
||||
foreach (DictionaryEntry entry in dictionary)
|
||||
{
|
||||
|
||||
//Serialize(entry.Key, message); // Implement this method based on the type of 'Key'
|
||||
//Serialize(entry.Value, message); // Implement this method based on the type of 'Value'
|
||||
}
|
||||
}
|
||||
else if (prop.PropertyType == typeof(Vector3))
|
||||
{
|
||||
Vector3 vector = new Vector3(message.GetFloat(), message.GetFloat(), message.GetFloat());
|
||||
prop.SetValue(p, vector);
|
||||
}
|
||||
else if (prop.PropertyType == typeof(Quaternion))
|
||||
{
|
||||
Quaternion quaternion = new Quaternion(message.GetFloat(), message.GetFloat(), message.GetFloat(), message.GetFloat());
|
||||
prop.SetValue(p, quaternion);
|
||||
}
|
||||
else if (prop.PropertyType == typeof(Guid))
|
||||
{
|
||||
Guid guid = new Guid(message.GetBytes());
|
||||
prop.SetValue(p, guid);
|
||||
}
|
||||
// You can add more types as needed
|
||||
}
|
||||
|
||||
if (KCServer.IsRunning)
|
||||
{
|
||||
//if (!p.GetType().Name.Contains("Update"))
|
||||
//Main.helper.Log($"Received packet {messageReceived.MessageId} {p.GetType().Name} from {messageReceived.FromConnection.Id}");
|
||||
//Main.helper.Log("Setting packet client id to: " + messageReceived.FromConnection.Id + " for packet: " + p.GetType().Name);
|
||||
//p.clientId = messageReceived.FromConnection.Id;
|
||||
}
|
||||
|
||||
return p;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Main.helper.Log("----------------------- Main exception -----------------------");
|
||||
Main.helper.Log(ex.ToString());
|
||||
Main.helper.Log("----------------------- Main message -----------------------");
|
||||
Main.helper.Log(ex.Message);
|
||||
Main.helper.Log("----------------------- Main stacktrace -----------------------");
|
||||
Main.helper.Log(ex.StackTrace);
|
||||
if (ex.InnerException != null)
|
||||
{
|
||||
Main.helper.Log("----------------------- Inner exception -----------------------");
|
||||
Main.helper.Log(ex.InnerException.ToString());
|
||||
Main.helper.Log("----------------------- Inner message -----------------------");
|
||||
Main.helper.Log(ex.InnerException.Message);
|
||||
Main.helper.Log("----------------------- Inner stacktrace -----------------------");
|
||||
Main.helper.Log(ex.InnerException.StackTrace);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
17
Packets/IPacket.cs
Normal file
17
Packets/IPacket.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace KCM.Packets
|
||||
{
|
||||
public interface IPacket
|
||||
{
|
||||
ushort packetId { get; }
|
||||
ushort clientId { get; set; }
|
||||
|
||||
void HandlePacketServer();
|
||||
void HandlePacketClient();
|
||||
}
|
||||
}
|
||||
32
Packets/Lobby/ChatMessage.cs
Normal file
32
Packets/Lobby/ChatMessage.cs
Normal file
@@ -0,0 +1,32 @@
|
||||
using KCM.Packets.Handlers;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace KCM.Packets.Lobby
|
||||
{
|
||||
public class ChatMessage : Packet
|
||||
{
|
||||
public override ushort packetId => (int)Enums.Packets.ChatMessage;
|
||||
|
||||
public string PlayerName { get; set; }
|
||||
public string Message { get; set; }
|
||||
|
||||
public override void HandlePacketServer()
|
||||
{
|
||||
//Main.helper.Log("Received chat packet: " + Message);
|
||||
|
||||
//SendToAll(KCClient.client.Id);
|
||||
//LobbyHandler.AddChatMessage(clientId, PlayerName, Message);
|
||||
}
|
||||
|
||||
public override void HandlePacketClient()
|
||||
{
|
||||
Main.helper.Log("Received chat packet: " + Message);
|
||||
|
||||
LobbyHandler.AddChatMessage(clientId, PlayerName, Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
26
Packets/Lobby/ChatSystemMessage.cs
Normal file
26
Packets/Lobby/ChatSystemMessage.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
using KCM.Packets.Handlers;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace KCM.Packets.Lobby
|
||||
{
|
||||
public class ChatSystemMessage : Packet
|
||||
{
|
||||
public override ushort packetId => (int)Enums.Packets.ChatSystemMessage;
|
||||
|
||||
public string Message { get; set; }
|
||||
|
||||
public override void HandlePacketServer()
|
||||
{
|
||||
//LobbyHandler.AddSystemMessage(Message);
|
||||
}
|
||||
|
||||
public override void HandlePacketClient()
|
||||
{
|
||||
LobbyHandler.AddSystemMessage(Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
38
Packets/Lobby/KingdomName.cs
Normal file
38
Packets/Lobby/KingdomName.cs
Normal file
@@ -0,0 +1,38 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace KCM.Packets.Lobby
|
||||
{
|
||||
public class KingdomName : Packet
|
||||
{
|
||||
public override ushort packetId => (int)Enums.Packets.KingdomName;
|
||||
|
||||
public string kingdomName { get; set; }
|
||||
|
||||
public override void HandlePacketServer()
|
||||
{
|
||||
if (player == null)
|
||||
return;
|
||||
Main.helper.Log("Received kingdom name packet");
|
||||
|
||||
//SendToAll(KCClient.client.Id);
|
||||
|
||||
player.kingdomName = kingdomName;
|
||||
}
|
||||
|
||||
public override void HandlePacketClient()
|
||||
{
|
||||
if (player == null)
|
||||
return;
|
||||
|
||||
Main.helper.Log("Received kingdom name packet");
|
||||
|
||||
player.kingdomName = kingdomName;
|
||||
|
||||
Main.helper.Log($"Player {player.name} has joined with their kingdom {player.kingdomName}");
|
||||
}
|
||||
}
|
||||
}
|
||||
31
Packets/Lobby/PlayerBanner.cs
Normal file
31
Packets/Lobby/PlayerBanner.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace KCM.Packets.Lobby
|
||||
{
|
||||
public class PlayerBanner : Packet
|
||||
{
|
||||
public override ushort packetId => (int)Enums.Packets.PlayerBanner;
|
||||
|
||||
public int banner { get; set; }
|
||||
|
||||
public override void HandlePacketServer()
|
||||
{
|
||||
//SendToAll(KCClient.client.Id);
|
||||
|
||||
//player.banner = banner;
|
||||
//player.inst.PlayerLandmassOwner.SetBannerIdx(banner);
|
||||
}
|
||||
|
||||
public override void HandlePacketClient()
|
||||
{
|
||||
player.banner = banner;
|
||||
player.inst.PlayerLandmassOwner.SetBannerIdx(banner);
|
||||
|
||||
Main.helper.Log($"Player {clientId} ({player.id}) has set banner to {player.banner}");
|
||||
}
|
||||
}
|
||||
}
|
||||
58
Packets/Lobby/PlayerList.cs
Normal file
58
Packets/Lobby/PlayerList.cs
Normal file
@@ -0,0 +1,58 @@
|
||||
using KCM.Packets.Handlers;
|
||||
using Steamworks;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace KCM.Packets.Lobby
|
||||
{
|
||||
public class PlayerList : Packet
|
||||
{
|
||||
public override ushort packetId => (int)Enums.Packets.PlayerList;
|
||||
|
||||
public List<bool> playersReady { get; set; }
|
||||
public List<string> playersName { get; set; }
|
||||
public List<int> playersBanner { get; set; }
|
||||
public List<ushort> playersId { get; set; }
|
||||
public List<string> playersKingdomName { get; set; }
|
||||
|
||||
public List<string> steamIds { get; set; }
|
||||
|
||||
public override void HandlePacketServer()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public override void HandlePacketClient()
|
||||
{
|
||||
LobbyHandler.ClearPlayerList();
|
||||
|
||||
for (int i = 0; i < playersId.Count; i++)
|
||||
{
|
||||
|
||||
Main.helper.Log("PlayerList: " + playersName[i] + " " + playersId[i] + " " + steamIds[i]);
|
||||
|
||||
Main.kCPlayers.Add(steamIds[i], new KCPlayer(playersName[i], playersId[i], steamIds[i])
|
||||
{
|
||||
name = playersName[i],
|
||||
ready = playersReady[i],
|
||||
banner = playersBanner[i],
|
||||
kingdomName = playersKingdomName[i]
|
||||
});
|
||||
|
||||
|
||||
if (Main.clientSteamIds.ContainsKey(playersId[i]))
|
||||
Main.clientSteamIds[playersId[i]] = steamIds[i];
|
||||
else
|
||||
Main.clientSteamIds.Add(playersId[i], steamIds[i]);
|
||||
|
||||
Main.kCPlayers[steamIds[i]].inst.PlayerLandmassOwner.SetBannerIdx(playersBanner[i]);
|
||||
|
||||
LobbyHandler.AddPlayerEntry(playersId[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
28
Packets/Lobby/PlayerReady.cs
Normal file
28
Packets/Lobby/PlayerReady.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace KCM.Packets.Lobby
|
||||
{
|
||||
public class PlayerReady : Packet
|
||||
{
|
||||
public override ushort packetId => (int)Enums.Packets.PlayerReady;
|
||||
|
||||
public bool IsReady { get; set; }
|
||||
|
||||
public override void HandlePacketServer()
|
||||
{
|
||||
IsReady = !player.ready;
|
||||
//SendToAll(KCClient.client.Id);
|
||||
|
||||
player.ready = IsReady;
|
||||
}
|
||||
|
||||
public override void HandlePacketClient()
|
||||
{
|
||||
player.ready = IsReady;
|
||||
}
|
||||
}
|
||||
}
|
||||
114
Packets/Lobby/SaveTransferPacket.cs
Normal file
114
Packets/Lobby/SaveTransferPacket.cs
Normal file
@@ -0,0 +1,114 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using static KCM.Main;
|
||||
|
||||
namespace KCM.Packets.Lobby
|
||||
{
|
||||
public class SaveTransferPacket : Packet
|
||||
{
|
||||
public override ushort packetId => (ushort)Enums.Packets.SaveTransferPacket;
|
||||
|
||||
public static byte[] saveData = new byte[1];
|
||||
public static bool[] chunksReceived = new bool[1];
|
||||
public static bool loadingSave = false;
|
||||
public static int received = 0;
|
||||
|
||||
|
||||
public int chunkId { get; set; }
|
||||
public int chunkSize { get; set; }
|
||||
|
||||
public int saveSize { get; set; }
|
||||
public int saveDataIndex { get; set; }
|
||||
public int totalChunks { get; set; }
|
||||
|
||||
public byte[] saveDataChunk { get; set; }
|
||||
|
||||
public override void HandlePacketClient()
|
||||
{
|
||||
float savePercent = (float)received / (float)saveSize;
|
||||
|
||||
// Initialize saveData and chunksReceived on the first packet received
|
||||
if (saveData.Length == 1)
|
||||
{
|
||||
|
||||
Main.helper.Log("Save Transfer started!");
|
||||
loadingSave = true;
|
||||
|
||||
ServerLobbyScript.LoadingSave.SetActive(true);
|
||||
|
||||
// save percentage
|
||||
|
||||
|
||||
saveData = new byte[saveSize];
|
||||
chunksReceived = new bool[totalChunks];
|
||||
}
|
||||
|
||||
|
||||
// Copy the chunk data into the correct position in saveData
|
||||
Array.Copy(saveDataChunk, 0, saveData, saveDataIndex, saveDataChunk.Length);
|
||||
|
||||
// Mark this chunk as received
|
||||
chunksReceived[chunkId] = true;
|
||||
|
||||
// Seek to the next position to write to
|
||||
received += chunkSize;
|
||||
|
||||
|
||||
ServerLobbyScript.ProgressBar.fillAmount = savePercent;
|
||||
ServerLobbyScript.ProgressBarText.text = (savePercent * 100).ToString("0.00") + "%";
|
||||
ServerLobbyScript.ProgressText.text = $"{((float)(received / 1000)).ToString("0.00")} KB / {((float)(saveSize / 1000)).ToString("0.00")} KB";
|
||||
|
||||
|
||||
if (chunkId + 1 == totalChunks)
|
||||
{
|
||||
Main.helper.Log($"Received last save transfer packet.");
|
||||
|
||||
Main.helper.Log(WhichIsNotComplete());
|
||||
}
|
||||
|
||||
// Check if all chunks have been received
|
||||
if (IsTransferComplete())
|
||||
{
|
||||
// Handle completed transfer here
|
||||
Main.helper.Log("Save Transfer complete!");
|
||||
|
||||
LoadSaveLoadHook.saveBytes = saveData;
|
||||
LoadSaveLoadHook.memoryStreamHook = true;
|
||||
|
||||
LoadSave.Load();
|
||||
|
||||
|
||||
LoadSaveLoadHook.saveContainer.Unpack(null);
|
||||
Broadcast.OnLoadedEvent.Broadcast(new OnLoadedEvent());
|
||||
|
||||
ServerLobbyScript.LoadingSave.SetActive(false);
|
||||
}
|
||||
}
|
||||
|
||||
public static bool IsTransferComplete()
|
||||
{
|
||||
return chunksReceived.All(x => x == true);
|
||||
}
|
||||
|
||||
public static string WhichIsNotComplete()
|
||||
{
|
||||
string notComplete = "";
|
||||
for (int i = 0; i < chunksReceived.Length; i++)
|
||||
{
|
||||
if (!chunksReceived[i])
|
||||
{
|
||||
notComplete += i + ", ";
|
||||
}
|
||||
}
|
||||
return notComplete;
|
||||
}
|
||||
|
||||
public override void HandlePacketServer()
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
48
Packets/Lobby/ServerSettings.cs
Normal file
48
Packets/Lobby/ServerSettings.cs
Normal file
@@ -0,0 +1,48 @@
|
||||
using KCM.Packets.Handlers;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace KCM.Packets.Lobby
|
||||
{
|
||||
public class ServerSettings : Packet
|
||||
{
|
||||
public override ushort packetId => (int)Enums.Packets.ServerSettings;
|
||||
|
||||
public string ServerName { get; set; }
|
||||
public int MaxPlayers { get; set; }
|
||||
public bool Locked { get; set; }
|
||||
public string Password { get; set; }
|
||||
public int Difficulty { get; set; }
|
||||
public string WorldSeed { get; set; }
|
||||
public World.MapSize WorldSize { get; set; }
|
||||
public World.MapBias WorldType { get; set; }
|
||||
public World.MapRiverLakes WorldRivers { get; set; }
|
||||
public int PlacementType { get; set; }
|
||||
public bool FogOfWar { get; set; }
|
||||
|
||||
public ServerSettings() { this.MaxPlayers = 2; this.Password = " "; this.WorldRivers = World.MapRiverLakes.Some; }
|
||||
|
||||
public override void HandlePacketServer()
|
||||
{
|
||||
//SetServerSettings();
|
||||
}
|
||||
|
||||
public override void HandlePacketClient()
|
||||
{
|
||||
SetServerSettings();
|
||||
}
|
||||
|
||||
public void SetServerSettings()
|
||||
{
|
||||
|
||||
LobbyHandler.ServerSettings = this;
|
||||
|
||||
World.inst.mapSize = WorldSize;
|
||||
World.inst.mapBias = WorldType;
|
||||
World.inst.mapRiverLakes = WorldRivers;
|
||||
}
|
||||
}
|
||||
}
|
||||
103
Packets/Lobby/StartGame.cs
Normal file
103
Packets/Lobby/StartGame.cs
Normal file
@@ -0,0 +1,103 @@
|
||||
using KCM.Enums;
|
||||
using Riptide.Demos.Steam.PlayerHosted;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
|
||||
namespace KCM.Packets.Lobby
|
||||
{
|
||||
public class StartGame : Packet
|
||||
{
|
||||
public override ushort packetId => (int)Enums.Packets.StartGame;
|
||||
|
||||
public void Start()
|
||||
{
|
||||
Main.helper.Log(GameState.inst.mainMenuMode.ToString());
|
||||
|
||||
// Hide server lobby
|
||||
Main.TransitionTo((MenuState)200);
|
||||
|
||||
// This is run when user clicks "accept" on choose your map screeen
|
||||
|
||||
try
|
||||
{
|
||||
if (!LobbyManager.loadingSave)
|
||||
{
|
||||
SpeedControlUI.inst.SetSpeed(0);
|
||||
|
||||
try
|
||||
{
|
||||
typeof(MainMenuMode).GetMethod("StartGame", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(GameState.inst.mainMenuMode, null);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Main.helper.Log(ex.Message.ToString());
|
||||
Main.helper.Log(ex.ToString());
|
||||
}
|
||||
|
||||
SpeedControlUI.inst.SetSpeed(0);
|
||||
}
|
||||
else
|
||||
{
|
||||
LobbyManager.loadingSave = false;
|
||||
GameState.inst.SetNewMode(GameState.inst.playingMode);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// Handle exception here
|
||||
Main.helper.Log(ex.Message.ToString());
|
||||
Main.helper.Log(ex.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
public override void HandlePacketClient()
|
||||
{
|
||||
Start();
|
||||
}
|
||||
|
||||
public override void HandlePacketServer()
|
||||
{
|
||||
//Start();
|
||||
|
||||
|
||||
/*AIBrainsContainer.PreStartAIConfig aiConfig = new AIBrainsContainer.PreStartAIConfig();
|
||||
int count = 0;
|
||||
for (int i = 0; i < RivalKingdomSettingsUI.inst.rivalItems.Length; i++)
|
||||
{
|
||||
RivalItemUI r = RivalKingdomSettingsUI.inst.rivalItems[i];
|
||||
bool flag = r.Enabled && !r.Locked;
|
||||
if (flag)
|
||||
{
|
||||
count++;
|
||||
}
|
||||
}
|
||||
int idx = 0;
|
||||
aiConfig.startData = new AIBrainsContainer.PreStartAIConfig.AIStartData[count];
|
||||
for (int j = 0; j < RivalKingdomSettingsUI.inst.rivalItems.Length; j++)
|
||||
{
|
||||
RivalItemUI item = RivalKingdomSettingsUI.inst.rivalItems[j];
|
||||
bool flag2 = item.Enabled && !item.Locked;
|
||||
if (flag2)
|
||||
{
|
||||
aiConfig.startData[idx] = new AIBrainsContainer.PreStartAIConfig.AIStartData();
|
||||
aiConfig.startData[idx].landmass = item.flag.landmass;
|
||||
aiConfig.startData[idx].bioCode = item.bannerIdx;
|
||||
aiConfig.startData[idx].personalityKey = PersonalityCollection.aiPersonalityKeys[0];
|
||||
aiConfig.startData[idx].skillLevel = item.GetSkillLevel();
|
||||
idx++;
|
||||
}
|
||||
}
|
||||
AIBrainsContainer.inst.aiStartInfo = aiConfig;
|
||||
bool isControllerActive = GamepadControl.inst.isControllerActive;
|
||||
if (isControllerActive)
|
||||
{
|
||||
ConsoleCursorMenu.inst.PrepForGamepad();
|
||||
}*/
|
||||
}
|
||||
}
|
||||
}
|
||||
46
Packets/Lobby/WorldSeed.cs
Normal file
46
Packets/Lobby/WorldSeed.cs
Normal file
@@ -0,0 +1,46 @@
|
||||
using KCM.Packets.Handlers;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
|
||||
namespace KCM.Packets.Lobby
|
||||
{
|
||||
public class WorldSeed : Packet
|
||||
{
|
||||
public override ushort packetId => (int)Enums.Packets.WorldSeed;
|
||||
public int Seed { get; set; }
|
||||
|
||||
public override void HandlePacketServer()
|
||||
{
|
||||
//SetWorldSeed();
|
||||
}
|
||||
|
||||
public override void HandlePacketClient()
|
||||
{
|
||||
SetWorldSeed();
|
||||
}
|
||||
|
||||
public void SetWorldSeed()
|
||||
{
|
||||
try
|
||||
{
|
||||
foreach (var player in Main.kCPlayers.Values)
|
||||
player.inst.Reset();
|
||||
|
||||
World.inst.Generate(Seed);
|
||||
Vector3 center = World.inst.GetCellData(World.inst.GridWidth / 2, World.inst.GridHeight / 2).Center;
|
||||
Cam.inst.SetTrackingPos(center);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Main.helper.Log("Set world seed packet error");
|
||||
Main.helper.Log(e.Message);
|
||||
Main.helper.Log(e.StackTrace);
|
||||
Main.helper.Log(e.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
145
Packets/Network/ClientConnected.cs
Normal file
145
Packets/Network/ClientConnected.cs
Normal file
@@ -0,0 +1,145 @@
|
||||
using KCM.Packets.Handlers;
|
||||
using KCM.Packets.Lobby;
|
||||
using Riptide.Demos.Steam.PlayerHosted;
|
||||
using Steamworks;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using static KCM.Main;
|
||||
|
||||
namespace KCM.Packets.Network
|
||||
{
|
||||
public class ClientConnected : Packet
|
||||
{
|
||||
public override ushort packetId => (int)Enums.Packets.ClientConnected;
|
||||
|
||||
public string Name { get; set; }
|
||||
|
||||
public string SteamId { get; set; }
|
||||
|
||||
public override void HandlePacketClient()
|
||||
{
|
||||
|
||||
Main.helper.Log("Client Player Connected: " + Name + " Id: " + clientId + " SteamID: " + SteamId);
|
||||
|
||||
KCPlayer player;
|
||||
if (Main.kCPlayers.TryGetValue(SteamId, out player))
|
||||
{
|
||||
player.id = clientId;
|
||||
player.name = Name;
|
||||
player.steamId = SteamId;
|
||||
}
|
||||
else
|
||||
Main.kCPlayers.Add(SteamId, new KCPlayer(Name, clientId, SteamId));
|
||||
|
||||
|
||||
if (Main.clientSteamIds.ContainsKey(clientId))
|
||||
Main.clientSteamIds[clientId] = SteamId;
|
||||
else
|
||||
Main.clientSteamIds.Add(clientId, SteamId);
|
||||
|
||||
|
||||
if (!SaveTransferPacket.loadingSave)
|
||||
LobbyHandler.AddPlayerEntry(clientId);
|
||||
}
|
||||
|
||||
public override void HandlePacketServer()
|
||||
{
|
||||
Main.helper.Log("Server Player Connected: " + Name + " Id: " + clientId + " SteamID: " + SteamId);
|
||||
|
||||
List<KCPlayer> list = Main.kCPlayers.Select(x => x.Value).OrderBy(x => x.id).ToList();
|
||||
|
||||
if (list.Count > 0)
|
||||
new PlayerList()
|
||||
{
|
||||
playersBanner = list.Select(x => x.banner).ToList(),
|
||||
playersReady = list.Select(x => x.ready).ToList(),
|
||||
playersName = list.Select(x => x.name).ToList(),
|
||||
playersId = list.Select(x => x.id).ToList(),
|
||||
playersKingdomName = list.Select(x => x.kingdomName).ToList(),
|
||||
steamIds = list.Select(x => x.steamId).ToList()
|
||||
}.SendToAll(KCClient.client.Id);
|
||||
|
||||
new ChatSystemMessage()
|
||||
{
|
||||
Message = $"{Name} has joined the server."
|
||||
}.SendToAll();
|
||||
|
||||
LobbyHandler.ServerSettings.SendToAll(KCClient.client.Id);
|
||||
|
||||
|
||||
if (LobbyManager.loadingSave)
|
||||
{
|
||||
if (clientId == KCClient.client.Id)
|
||||
return;
|
||||
|
||||
byte[] bytes = LoadSaveLoadAtPathHook.saveData;
|
||||
int chunkSize = 900; // 900 bytes per chunk to fit within packet size limit
|
||||
|
||||
List<byte[]> chunks = SplitByteArrayIntoChunks(bytes, chunkSize);
|
||||
Main.helper.Log("Save Transfer started!");
|
||||
|
||||
int sent = 0;
|
||||
int packetsSent = 0;
|
||||
|
||||
for (int i = 0; i < chunks.Count; i++)
|
||||
{
|
||||
var chunk = chunks[i];
|
||||
|
||||
|
||||
new SaveTransferPacket()
|
||||
{
|
||||
saveSize = bytes.Length,
|
||||
saveDataChunk = chunk,
|
||||
chunkId = i,
|
||||
chunkSize = chunk.Length,
|
||||
saveDataIndex = sent,
|
||||
totalChunks = chunks.Count
|
||||
}.Send(clientId);
|
||||
|
||||
Main.helper.Log(" ");
|
||||
|
||||
packetsSent++;
|
||||
sent += chunk.Length;
|
||||
}
|
||||
|
||||
Main.helper.Log($"Sent {packetsSent} save data chunks to client");
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
new WorldSeed()
|
||||
{
|
||||
Seed = World.inst.seed
|
||||
}.SendToAll(KCClient.client.Id);
|
||||
}
|
||||
}
|
||||
|
||||
public static List<byte[]> SplitByteArrayIntoChunks(byte[] source, int chunkSize)
|
||||
{
|
||||
var chunks = new List<byte[]>();
|
||||
int sourceLength = source.Length;
|
||||
|
||||
for (int i = 0; i < sourceLength; i += chunkSize)
|
||||
{
|
||||
// Calculate the length of the current chunk, as the last chunk may be smaller than chunkSize
|
||||
int currentChunkSize = Math.Min(chunkSize, sourceLength - i);
|
||||
|
||||
// Create a chunk array of the correct size
|
||||
byte[] chunk = new byte[currentChunkSize];
|
||||
|
||||
// Copy a segment of the source array into the chunk array
|
||||
Array.Copy(source, i, chunk, 0, currentChunkSize);
|
||||
|
||||
// Add the chunk to the list of chunks
|
||||
chunks.Add(chunk);
|
||||
}
|
||||
|
||||
return chunks;
|
||||
}
|
||||
}
|
||||
}
|
||||
69
Packets/Network/ServerHandshake.cs
Normal file
69
Packets/Network/ServerHandshake.cs
Normal file
@@ -0,0 +1,69 @@
|
||||
using KCM.Enums;
|
||||
using KCM.Packets.Lobby;
|
||||
using Riptide;
|
||||
using Riptide.Demos.Steam.PlayerHosted;
|
||||
using Steamworks;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Xml.Linq;
|
||||
|
||||
namespace KCM.Packets.Network
|
||||
{
|
||||
public class ServerHandshake : Packet
|
||||
{
|
||||
public override ushort packetId => (int)Enums.Packets.ServerHandshake;
|
||||
|
||||
public bool loadingSave { get; set; }
|
||||
|
||||
public override void HandlePacketClient()
|
||||
{
|
||||
ModalManager.HideModal();
|
||||
|
||||
Main.TransitionTo(Enums.MenuState.ServerLobby);
|
||||
|
||||
SfxSystem.PlayUiSelect();
|
||||
|
||||
Cam.inst.desiredDist = 80f;
|
||||
Cam.inst.desiredPhi = 45f;
|
||||
CloudSystem.inst.threshold1 = 0.6f;
|
||||
CloudSystem.inst.threshold2 = 0.8f;
|
||||
CloudSystem.inst.BaseFreq = 4.5f;
|
||||
Weather.inst.SetSeason(Weather.Season.Summer);
|
||||
|
||||
//inst = new KCClient(KCServer.IsRunning ? "Ryan" : "Orion");
|
||||
KCClient.inst = new KCClient(SteamFriends.GetPersonaName());
|
||||
|
||||
Main.helper.Log("Sending client connected. Client ID is: " + clientId);
|
||||
|
||||
Main.kCPlayers.Add(Main.PlayerSteamID, new KCPlayer(KCClient.inst.Name, clientId, Main.PlayerSteamID));
|
||||
|
||||
Player.inst.PlayerLandmassOwner.teamId = clientId * 10 + 2;
|
||||
|
||||
if (loadingSave && KCServer.IsRunning)
|
||||
Main.TransitionTo(MenuState.Load);
|
||||
else if (!loadingSave)
|
||||
{
|
||||
Main.TransitionTo(MenuState.NameAndBanner);
|
||||
|
||||
}
|
||||
|
||||
|
||||
new KingdomName() { kingdomName = TownNameUI.inst.townName, clientId = clientId }.Send();
|
||||
|
||||
new ClientConnected()
|
||||
{
|
||||
clientId = clientId,
|
||||
Name = KCClient.inst.Name,
|
||||
SteamId = Main.PlayerSteamID
|
||||
}.Send();
|
||||
}
|
||||
|
||||
public override void HandlePacketServer()
|
||||
{
|
||||
//throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
113
Packets/Packet.cs
Normal file
113
Packets/Packet.cs
Normal file
@@ -0,0 +1,113 @@
|
||||
using KCM.Packets.Handlers;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace KCM.Packets
|
||||
{
|
||||
public abstract class Packet : IPacket
|
||||
{
|
||||
public abstract ushort packetId { get; }
|
||||
public ushort clientId { get; set; }
|
||||
|
||||
public KCPlayer player
|
||||
{
|
||||
get
|
||||
{
|
||||
KCPlayer p = null;
|
||||
|
||||
if (!Main.clientSteamIds.ContainsKey(clientId))
|
||||
return null;
|
||||
|
||||
//Main.helper.Log($"SteamID: {Main.GetPlayerByClientID(clientId).steamId} for {clientId} ({Main.GetPlayerByClientID(clientId).id})");
|
||||
|
||||
if (Main.kCPlayers.TryGetValue(Main.GetPlayerByClientID(clientId).steamId, out p))
|
||||
return p;
|
||||
else
|
||||
{
|
||||
Main.helper.Log($"Error getting player from packet {packetId} {this.GetType().Name} from {clientId}");
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public void SendToAll(ushort exceptToClient = 0)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (exceptToClient == 0)
|
||||
{
|
||||
if (KCServer.IsRunning)
|
||||
KCServer.server.SendToAll(PacketHandler.SerialisePacket(this));
|
||||
}
|
||||
else
|
||||
{
|
||||
if (KCServer.IsRunning && exceptToClient != 0)
|
||||
KCServer.server.SendToAll(PacketHandler.SerialisePacket(this), exceptToClient);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Main.helper.Log($"Error sending packet to all {packetId} {this.GetType().Name} from {clientId}");
|
||||
|
||||
Main.helper.Log("----------------------- Main exception -----------------------");
|
||||
Main.helper.Log(ex.ToString());
|
||||
Main.helper.Log("----------------------- Main message -----------------------");
|
||||
Main.helper.Log(ex.Message);
|
||||
Main.helper.Log("----------------------- Main stacktrace -----------------------");
|
||||
Main.helper.Log(ex.StackTrace);
|
||||
if (ex.InnerException != null)
|
||||
{
|
||||
Main.helper.Log("----------------------- Inner exception -----------------------");
|
||||
Main.helper.Log(ex.InnerException.ToString());
|
||||
Main.helper.Log("----------------------- Inner message -----------------------");
|
||||
Main.helper.Log(ex.InnerException.Message);
|
||||
Main.helper.Log("----------------------- Inner stacktrace -----------------------");
|
||||
Main.helper.Log(ex.InnerException.StackTrace);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Send(ushort toClient = 0)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (KCClient.client.IsConnected && toClient == 0)
|
||||
{
|
||||
this.clientId = KCClient.client.Id;
|
||||
KCClient.client.Send(PacketHandler.SerialisePacket(this));
|
||||
}
|
||||
else if (KCServer.IsRunning && toClient != 0)
|
||||
{
|
||||
KCServer.server.Send(PacketHandler.SerialisePacket(this), toClient);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Main.helper.Log($"Error sending packet {packetId} {this.GetType().Name} from {clientId}");
|
||||
|
||||
Main.helper.Log("----------------------- Main exception -----------------------");
|
||||
Main.helper.Log(ex.ToString());
|
||||
Main.helper.Log("----------------------- Main message -----------------------");
|
||||
Main.helper.Log(ex.Message);
|
||||
Main.helper.Log("----------------------- Main stacktrace -----------------------");
|
||||
Main.helper.Log(ex.StackTrace);
|
||||
if (ex.InnerException != null)
|
||||
{
|
||||
Main.helper.Log("----------------------- Inner exception -----------------------");
|
||||
Main.helper.Log(ex.InnerException.ToString());
|
||||
Main.helper.Log("----------------------- Inner message -----------------------");
|
||||
Main.helper.Log(ex.InnerException.Message);
|
||||
Main.helper.Log("----------------------- Inner stacktrace -----------------------");
|
||||
Main.helper.Log(ex.InnerException.StackTrace);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public abstract void HandlePacketServer();
|
||||
public abstract void HandlePacketClient();
|
||||
}
|
||||
}
|
||||
30
Packets/ShowModal.cs
Normal file
30
Packets/ShowModal.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace KCM.Packets
|
||||
{
|
||||
public class ShowModal : Packet
|
||||
{
|
||||
public override ushort packetId => (int)Enums.Packets.ShowModal;
|
||||
|
||||
public string title { get; set; }
|
||||
public string message { get; set; }
|
||||
|
||||
public override void HandlePacketClient()
|
||||
{
|
||||
Main.helper.Log("Opening Modal");
|
||||
Main.helper.Log("Title: " + title);
|
||||
Main.helper.Log("Message: " + message);
|
||||
|
||||
ModalManager.ShowModal(title, message);
|
||||
}
|
||||
|
||||
public override void HandlePacketServer()
|
||||
{
|
||||
//throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
115
Packets/State/BuildingStatePacket.cs
Normal file
115
Packets/State/BuildingStatePacket.cs
Normal file
@@ -0,0 +1,115 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
|
||||
namespace KCM.Packets.State
|
||||
{
|
||||
public class BuildingStatePacket : Packet
|
||||
{
|
||||
public override ushort packetId => (ushort)Enums.Packets.BuildingStatePacket;
|
||||
|
||||
public string customName { get; set; }
|
||||
public Guid guid { get; set; }
|
||||
public string uniqueName { get; set; }
|
||||
public Quaternion rotation { get; set; }
|
||||
public Vector3 globalPosition { get; set; }
|
||||
public Vector3 localPosition { get; set; }
|
||||
public bool built { get; set; }
|
||||
public bool placed { get; set; }
|
||||
public bool open { get; set; }
|
||||
public bool doBuildAnimation { get; set; }
|
||||
public bool constructionPaused { get; set; }
|
||||
public float constructionProgress { get; set; }
|
||||
public float resourceProgress { get; set; }
|
||||
public float life { get; set; }
|
||||
public float ModifiedMaxLife { get; set; }
|
||||
public int yearBuilt { get; set; }
|
||||
public float decayProtection { get; set; }
|
||||
public bool seenByPlayer { get; set; }
|
||||
|
||||
|
||||
public override void HandlePacketClient()
|
||||
{
|
||||
if (clientId == KCClient.client.Id) return; //prevent double placing on same client
|
||||
|
||||
//Main.helper.Log("Received building state packet for: " + uniqueName + " from " + Main.kCPlayers[Main.GetPlayerByClientID(clientId).steamId].name + $"({clientId})");
|
||||
|
||||
|
||||
Building building = player.inst.GetBuilding(guid);
|
||||
|
||||
if (building == null)
|
||||
{
|
||||
Main.helper.Log("Building not found.");
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
//PrintProperties();
|
||||
|
||||
building.UniqueName = uniqueName;
|
||||
building.customName = customName;
|
||||
|
||||
|
||||
building.transform.position = this.globalPosition;
|
||||
building.transform.GetChild(0).rotation = this.rotation;
|
||||
building.transform.GetChild(0).localPosition = this.localPosition;
|
||||
|
||||
SetPrivateFieldValue(building, "built", built);
|
||||
SetPrivateFieldValue(building, "placed", placed);
|
||||
SetPrivateFieldValue(building, "resourceProgress", resourceProgress);
|
||||
|
||||
|
||||
building.Open = open;
|
||||
building.doBuildAnimation = doBuildAnimation;
|
||||
building.constructionPaused = constructionPaused;
|
||||
building.constructionProgress = constructionProgress;
|
||||
building.Life = life;
|
||||
building.ModifiedMaxLife = ModifiedMaxLife;
|
||||
|
||||
|
||||
//building.yearBuilt = yearBuilt;
|
||||
SetPrivateFieldValue(building, "yearBuilt", yearBuilt);
|
||||
|
||||
building.decayProtection = decayProtection;
|
||||
//building.seenByPlayer = seenByPlayer;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Main.helper.Log("Error setting building state");
|
||||
Main.helper.Log(e.Message);
|
||||
Main.helper.Log(e.StackTrace);
|
||||
}
|
||||
}
|
||||
|
||||
public override void HandlePacketServer()
|
||||
{
|
||||
//throw new NotImplementedException();
|
||||
}
|
||||
|
||||
private void SetPrivateFieldValue(object obj, string fieldName, object value)
|
||||
{
|
||||
Type type = obj.GetType();
|
||||
FieldInfo field = type.GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Instance);
|
||||
field.SetValue(obj, value);
|
||||
}
|
||||
|
||||
public void PrintProperties()
|
||||
{
|
||||
Type type = typeof(BuildingStatePacket);
|
||||
|
||||
foreach (PropertyInfo property in type.GetProperties())
|
||||
{
|
||||
object value = property.GetValue(this);
|
||||
string propertyName = property.Name;
|
||||
|
||||
Main.helper.Log($"{propertyName}: {value}");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user