Skip to content

Commit

Permalink
Upgrade module persistence (#667)
Browse files Browse the repository at this point in the history
* upgrade modules come in after vehicles.

* Added base piece check to listener
  • Loading branch information
amar0k authored and Sunrunner37 committed Jan 27, 2019
1 parent 853bb0a commit ca8cafa
Showing 1 changed file with 84 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using NitroxModel.DataStructures.GameLogic.Buildings.Metadata;
using NitroxClient.GameLogic.Bases.Metadata;
using System.Linq;
using NitroxClient.Unity.Helper;

namespace NitroxClient.Communication.Packets.Processors
{
Expand Down Expand Up @@ -49,7 +50,6 @@ public override void Process(InitialPlayerSync packet)
SetEscapePodInfo(packet.EscapePodsData, packet.AssignedEscapePodGuid);
SetPlayerGuid(packet.PlayerGuid);
AddStartingItemsToPlayer(packet.FirstTimeConnecting);
SpawnPlayerEquipment(packet.EquippedItems); //Need to Set Equipment On Vehicles before SpawnItemContainer due to the locker upgrade (VehicleStorageModule Seamoth / Prawn)
SpawnBasePieces(packet.BasePieces);
SpawnGlobalRootEntities(packet.GlobalRootEntities);
SetEncyclopediaEntry(packet.PDAData.EncyclopediaEntries);
Expand All @@ -61,12 +61,14 @@ public override void Process(InitialPlayerSync packet)
SetPlayerGameMode(packet.GameMode);

bool hasBasePiecesToSpawn = packet.BasePieces.Count > 0;
bool hasVehiclesToSpawn = packet.Vehicles.Count > 0;

SpawnInventoryItemsAfterBasePiecesFinish(packet.InventoryItems, hasBasePiecesToSpawn, packet.PlayerGuid);
SpawnRemotePlayersAfterBasePiecesFinish(packet.RemotePlayerData, hasBasePiecesToSpawn);
SpawnVehiclesAfterBasePiecesFinish(packet.Vehicles, hasBasePiecesToSpawn);
SetPlayerLocationAfterBasePiecesFinish(packet.PlayerSpawnData, packet.PlayerSubRootGuid, hasBasePiecesToSpawn);
AssignBasePieceMetadataAfterBuildingsComplete(packet.BasePieces);
SpawnPlayerEquipment(packet.EquippedItems, hasVehiclesToSpawn, hasBasePiecesToSpawn);
SpawnInventoryItemsAfterBasePiecesFinish(packet.InventoryItems, hasBasePiecesToSpawn, packet.PlayerGuid);
}

private void SpawnGlobalRootEntities(List<Entity> globalRootEntities)
Expand Down Expand Up @@ -194,13 +196,19 @@ private void SetPlayerGuid(string playerguid)
Log.Info("Received initial sync Player Guid: " + playerguid);
}

private void SpawnPlayerEquipment(List<EquippedItemData> equippedItems)
private void SpawnPlayerEquipment(List<EquippedItemData> equippedItems, bool vehiclesToSpawn, bool basePiecesToSpawn)
{
Log.Info("Received initial sync packet with " + equippedItems.Count + " equipment items");

using (packetSender.Suppress<EquipmentAddItem>())
EquipmentItemAdder itemAdder = new EquipmentItemAdder(packetSender, equippedItems);

if (vehiclesToSpawn && basePiecesToSpawn)
{
ThrottledBuilder.main.QueueDrained += itemAdder.AddEquipmentToInventories;
}
else
{
equipment.AddItems(equippedItems);
itemAdder.AddEquipmentToInventories(null, null);
}
}

Expand Down Expand Up @@ -322,7 +330,76 @@ public void AddItemsToInventories(object sender, EventArgs eventArgs)
}
}
}


/*
* This class simply encapsulates a callback method that is invoked when the throttled builder
* is completed with the initial sync of vehicles. We keep this in a new class to be able to
* hold the relevant equipment and use them when the time comes. This can be later extended
* to wait on other events if need be.
*/
private class EquipmentItemAdder
{
private IPacketSender packetSender;
private List<EquippedItemData> equippedItems;

public EquipmentItemAdder(IPacketSender packetSender, List<EquippedItemData> equippedItems)
{
this.packetSender = packetSender;
this.equippedItems = equippedItems;
}

public void AddEquipmentToInventories(object sender, EventArgs eventArgs)
{
ThrottledBuilder.main.QueueDrained -= AddEquipmentToInventories;

using (packetSender.Suppress<ItemContainerAdd>())
{

foreach (EquippedItemData equippedItem in equippedItems)
{
GameObject gameObject = SerializationHelper.GetGameObject(equippedItem.SerializedData);

// Mark this entity as spawned by the server
gameObject.AddComponent<NitroxEntity>();

Pickupable pickupable = gameObject.RequireComponent<Pickupable>();

Optional<GameObject> opGameObject = GuidHelper.GetObjectFrom(equippedItem.ContainerGuid);

if (opGameObject.IsPresent())
{
GameObject owner = opGameObject.Get();

Optional<Equipment> opEquipment = EquipmentHelper.GetBasedOnOwnersType(owner);

if (opEquipment.IsPresent())
{
Equipment equipment = opEquipment.Get();
InventoryItem inventoryItem = new InventoryItem(pickupable);
inventoryItem.container = equipment;
inventoryItem.item.Reparent(equipment.tr);

Dictionary<string, InventoryItem> itemsBySlot = (Dictionary<string, InventoryItem>)equipment.ReflectionGet("equipment");
itemsBySlot[equippedItem.Slot] = inventoryItem;

equipment.ReflectionCall("UpdateCount", false, false, new object[] { pickupable.GetTechType(), true });
Equipment.SendEquipmentEvent(pickupable, 0, owner, equippedItem.Slot);
equipment.ReflectionCall("NotifyEquip", false, false, new object[] { equippedItem.Slot, inventoryItem });
}
else
{
Log.Info("Could not find equipment type for " + gameObject.name);
}
}
else
{
Log.Info("Could not find Container for " + gameObject.name);
}
}
}
}
}

private void SetPlayerLocationAfterBasePiecesFinish(Vector3 position, Optional<string> subRootGuid, bool basePiecesToSpawn)
{
Log.Info("Received initial sync packet with location " + position + " and subroot " + subRootGuid);
Expand Down Expand Up @@ -468,7 +545,7 @@ public void CreateVehicles(object sender, EventArgs eventArgs)
foreach(VehicleModel vehicle in vehicleModels)
{
vehicles.CreateVehicle(vehicle);
}
}
}
}

Expand Down

0 comments on commit ca8cafa

Please sign in to comment.