Skip to content

Commit

Permalink
Sync name and colors for vehicles, rocket and cyclops
Browse files Browse the repository at this point in the history
  • Loading branch information
tornac1234 committed Dec 18, 2023
1 parent 7e89ce6 commit 9efe596
Show file tree
Hide file tree
Showing 27 changed files with 401 additions and 144 deletions.
3 changes: 2 additions & 1 deletion NitroxClient/Debuggers/NetworkDebugger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public class NetworkDebugger : BaseDebugger, INetworkDebugger
private readonly List<string> filter = new()
{
nameof(PlayerMovement), nameof(EntityTransformUpdates), nameof(PlayerStats), nameof(SpawnEntities), nameof(VehicleMovement), nameof(PlayerCinematicControllerCall),
nameof(PlayFMODAsset), nameof(PlayFMODCustomEmitter), nameof(PlayFMODStudioEmitter), nameof(PlayFMODCustomLoopingEmitter), nameof(SimulationOwnershipChange)
nameof(PlayFMODAsset), nameof(PlayFMODCustomEmitter), nameof(PlayFMODStudioEmitter), nameof(PlayFMODCustomLoopingEmitter), nameof(SimulationOwnershipChange),
nameof(CellVisibilityChanged), nameof(BatchVisibilityChanged)
};
private readonly List<PacketDebugWrapper> packets = new List<PacketDebugWrapper>(PACKET_STORED_COUNT);

Expand Down
8 changes: 4 additions & 4 deletions NitroxClient/GameLogic/Entities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,13 @@ public void EntityMetadataChanged(object o, NitroxId id)
}
}

public void EntityMetadataChangedThrottled(object o, NitroxId id)
public void EntityMetadataChangedThrottled(object o, NitroxId id, float throttleTime = 0.2f)
{
Optional<EntityMetadata> metadata = EntityMetadataExtractor.Extract(o);

if (metadata.HasValue)
{
BroadcastMetadataUpdateThrottled(id, metadata.Value);
BroadcastMetadataUpdateThrottled(id, metadata.Value, throttleTime);
}
}

Expand All @@ -89,9 +89,9 @@ public void BroadcastMetadataUpdate(NitroxId id, EntityMetadata metadata)
packetSender.Send(new EntityMetadataUpdate(id, metadata));
}

public void BroadcastMetadataUpdateThrottled(NitroxId id, EntityMetadata metadata)
public void BroadcastMetadataUpdateThrottled(NitroxId id, EntityMetadata metadata, float throttleTime = 0.2f)
{
throttledPacketSender.SendThrottled(new EntityMetadataUpdate(id, metadata), (packet) => ((EntityMetadataUpdate)packet).Id);
throttledPacketSender.SendThrottled(new EntityMetadataUpdate(id, metadata), packet => packet.Id, throttleTime);
}

public void BroadcastEntitySpawnedByClient(WorldEntity entity)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System.Linq;
using NitroxClient.Communication;
using NitroxClient.Unity.Helper;
using NitroxModel.DataStructures.GameLogic.Entities.Metadata;
using NitroxModel.DataStructures.Unity;
using NitroxModel.Packets;
using NitroxModel_Subnautica.DataStructures;
using UnityEngine;

namespace NitroxClient.GameLogic.Spawning.Metadata.Abstract;

public abstract class NamedColoredMetadataProcessor<T> : GenericEntityMetadataProcessor<T> where T : NamedColoredMetadata
{
public override void ProcessMetadata(GameObject gameObject, T metadata)
{
using (PacketSuppressor<EntityMetadataUpdate>.Suppress())
{
SetNameAndColors(gameObject, metadata.Name, metadata.Colors);
}
}

protected void SetNameAndColors(GameObject gameObject, string text, NitroxVector3[] nitroxColor)
{
SubName subName = gameObject.RequireComponent<SubName>();
if (!string.IsNullOrEmpty(text))
{
subName.DeserializeName(text);
}
if (nitroxColor != null)
{
Vector3[] colors = nitroxColor.Select(c => c.ToUnity()).ToArray();
subName.DeserializeColors(colors);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using NitroxClient.Communication;
using NitroxClient.Unity.Helper;
using NitroxModel.DataStructures.GameLogic.Entities.Metadata;
using NitroxModel.Packets;
using UnityEngine;

namespace NitroxClient.GameLogic.Spawning.Metadata.Abstract;

public abstract class VehicleMetadataProcessor<T> : NamedColoredMetadataProcessor<T> where T : VehicleMetadata
{
private readonly LiveMixinManager liveMixinManager;

public VehicleMetadataProcessor(LiveMixinManager liveMixinManager)
{
this.liveMixinManager = liveMixinManager;
}

public override void ProcessMetadata(GameObject gameObject, T metadata)
{
using (PacketSuppressor<EntityMetadataUpdate>.Suppress())
{
SetHealth(gameObject, metadata.Health);
}

base.ProcessMetadata(gameObject, metadata);
}

protected void SetHealth(GameObject gameObject, float health)
{
LiveMixin liveMixin = gameObject.RequireComponentInChildren<LiveMixin>(true);
liveMixinManager.SyncRemoteHealth(liveMixin, health);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using NitroxClient.GameLogic.Spawning.Metadata.Abstract;
using NitroxModel.DataStructures.GameLogic.Entities.Metadata;
using UnityEngine;

namespace NitroxClient.GameLogic.Spawning.Metadata;

public class ExosuitMetadataProcessor : VehicleMetadataProcessor<ExosuitMetadata>
{
public ExosuitMetadataProcessor(LiveMixinManager liveMixinManager) : base(liveMixinManager)
{ }

public override void ProcessMetadata(GameObject gameObject, ExosuitMetadata metadata)
{
if (!gameObject.TryGetComponent(out Exosuit exosuit))
{
Log.ErrorOnce($"[{nameof(ExosuitMetadataProcessor)}] Could not find {nameof(Exosuit)} on {gameObject}");
return;
}

base.ProcessMetadata(gameObject, metadata);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Linq;
using NitroxClient.Unity.Helper;
using NitroxModel.DataStructures.GameLogic.Entities.Metadata;
using NitroxModel.DataStructures.Unity;
using NitroxModel_Subnautica.DataStructures;

namespace NitroxClient.GameLogic.Spawning.Metadata.Extractor.Abstract;

public abstract class NamedColoredMetadataExtractor<I, O> : GenericEntityMetadataExtractor<I, O> where O : NamedColoredMetadata
{
public string GetName(SubName subName)
{
return subName.AliveOrNull()?.hullName.AliveOrNull()?.text;
}

public NitroxVector3[] GetColors(SubName subName)
{
return subName.AliveOrNull()?.GetColors().Select(color => color.ToDto()).ToArray();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using NitroxModel.DataStructures.GameLogic.Entities.Metadata;

namespace NitroxClient.GameLogic.Spawning.Metadata.Extractor.Abstract;

public abstract class VehicleMetadataExtractor<I, O> : NamedColoredMetadataExtractor<I, O> where O : VehicleMetadata
{
public float GetHealth(LiveMixin liveMixin)
{
return liveMixin.health;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using NitroxClient.GameLogic.Spawning.Metadata.Extractor.Abstract;
using NitroxModel.DataStructures.GameLogic.Entities.Metadata;

namespace NitroxClient.GameLogic.Spawning.Metadata.Extractor;

public class ExosuitMetadataExtractor : VehicleMetadataExtractor<Exosuit, ExosuitMetadata>
{
public override ExosuitMetadata Extract(Exosuit exosuit)
{
LiveMixin liveMixin = exosuit.liveMixin;
SubName subName = exosuit.subName;

return new(GetHealth(liveMixin), GetName(subName), GetColors(subName));
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
using NitroxClient.Unity.Helper;
using NitroxClient.GameLogic.Spawning.Metadata.Extractor.Abstract;
using NitroxModel.DataStructures.GameLogic.Entities.Metadata;

namespace NitroxClient.GameLogic.Spawning.Metadata.Extractor;

public class SeamothMetadataExtractor : GenericEntityMetadataExtractor<SeaMoth, SeamothMetadata>
public class SeamothMetadataExtractor : VehicleMetadataExtractor<SeaMoth, SeamothMetadata>
{
public override SeamothMetadata Extract(SeaMoth seamoth)
{
bool lightsOn = (seamoth.toggleLights) ? seamoth.toggleLights.GetLightsActive() : true;
float health = seamoth.RequireComponentInChildren<LiveMixin>().health;
LiveMixin liveMixin = seamoth.liveMixin;
SubName subName = seamoth.subName;

return new(lightsOn, health);
return new(lightsOn, GetHealth(liveMixin), GetName(subName), GetColors(subName));
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
using System.Linq;
using NitroxClient.GameLogic.Spawning.Metadata.Extractor.Abstract;
using NitroxModel.DataStructures.GameLogic.Entities.Metadata;
using NitroxModel.DataStructures.Unity;
using NitroxModel_Subnautica.DataStructures;

namespace NitroxClient.GameLogic.Spawning.Metadata.Extractor;

public class SubNameInputMetadataExtractor : GenericEntityMetadataExtractor<SubNameInput, SubNameInputMetadata>
public class SubNameInputMetadataExtractor : NamedColoredMetadataExtractor<SubNameInput, SubNameInputMetadata>
{
public override SubNameInputMetadata Extract(SubNameInput subNameInput)
{
NitroxVector3[] colors = subNameInput.target.GetColors().Select(color => color.ToDto()).ToArray();

return new(subNameInput.inputField.text, colors);
SubName subName = subNameInput.target;
return new(subNameInput.selectedColorIndex, GetName(subName), GetColors(subName));
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System.Collections.Generic;
using System.Linq;
using NitroxClient.Communication;
using NitroxClient.Communication.Abstract;
using NitroxClient.Helpers;
using NitroxClient.Unity.Helper;
using NitroxModel.DataStructures.GameLogic.Entities.Metadata;
using NitroxModel.Packets;
Expand All @@ -16,13 +14,6 @@ public class RocketMetadataProcessor : GenericEntityMetadataProcessor<RocketMeta
// For newly connected players, we will only build the previous stage with construction bots for a certain time period.
private const float MAX_ALLOWABLE_TIME_FOR_CONSTRUCTOR_BOTS = 10;

private IPacketSender packetSender;

public RocketMetadataProcessor(IPacketSender packetSender)
{
this.packetSender = packetSender;
}

/** Rocket states :
* 0 : Launch Platform
* 1 : Gantry
Expand Down
Original file line number Diff line number Diff line change
@@ -1,37 +1,31 @@
using NitroxClient.Communication;
using NitroxClient.Communication.Abstract;
using NitroxClient.GameLogic.FMOD;
using NitroxClient.Unity.Helper;
using NitroxClient.GameLogic.Spawning.Metadata.Abstract;
using NitroxModel.DataStructures.GameLogic.Entities.Metadata;
using NitroxModel.Packets;
using UnityEngine;

namespace NitroxClient.GameLogic.Spawning.Metadata;

public class SeamothMetadataProcessor : GenericEntityMetadataProcessor<SeamothMetadata>
public class SeamothMetadataProcessor : VehicleMetadataProcessor<SeamothMetadata>
{
private readonly IPacketSender packetSender;
private readonly LiveMixinManager liveMixinManager;

public SeamothMetadataProcessor(IPacketSender packetSender, LiveMixinManager liveMixinManager)
{
this.packetSender = packetSender;
this.liveMixinManager = liveMixinManager;
}
public SeamothMetadataProcessor(LiveMixinManager liveMixinManager) : base(liveMixinManager)
{ }

public override void ProcessMetadata(GameObject gameObject, SeamothMetadata metadata)
{
SeaMoth seamoth = gameObject.GetComponent<SeaMoth>();
if (!seamoth)
if (!gameObject.TryGetComponent(out SeaMoth seamoth))
{
Log.Error($"Could not find seamoth on {gameObject.name}");
Log.ErrorOnce($"[{nameof(SeamothMetadataProcessor)}] Could not find {nameof(SeaMoth)} on {gameObject}");
return;
}

using (PacketSuppressor<EntityMetadataUpdate>.Suppress())
{
SetLights(seamoth, metadata.LightsOn);
SetHealth(seamoth, metadata.Health);
}

base.ProcessMetadata(gameObject, metadata);
}

private void SetLights(SeaMoth seamoth, bool lightsOn)
Expand All @@ -41,10 +35,4 @@ private void SetLights(SeaMoth seamoth, bool lightsOn)
seamoth.toggleLights.SetLightsActive(lightsOn);
}
}

private void SetHealth(SeaMoth seamoth, float health)
{
LiveMixin liveMixin = seamoth.RequireComponentInChildren<LiveMixin>(true);
liveMixinManager.SyncRemoteHealth(liveMixin, health);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using NitroxClient.Communication;
using NitroxClient.GameLogic.Spawning.Metadata.Abstract;
using NitroxModel.DataStructures.GameLogic.Entities.Metadata;
using NitroxModel.Packets;
using UnityEngine;

namespace NitroxClient.GameLogic.Spawning.Metadata;

public class SubNameInputMetadataProcessor : NamedColoredMetadataProcessor<SubNameInputMetadata>
{
public override void ProcessMetadata(GameObject gameObject, SubNameInputMetadata metadata)
{
if (!gameObject.TryGetComponent(out SubNameInput subNameInput))
{
Log.ErrorOnce($"[{nameof(SubNameInputMetadataProcessor)}] Could not find {nameof(SubNameInput)} on {gameObject}");
return;
}

SubName subName = subNameInput.target;
if (!subName && !subNameInput.TryGetComponent(out subName))
{
Log.ErrorOnce($"[{nameof(SubNameInputMetadataProcessor)}] {gameObject}'s {nameof(subNameInput)} doesn't have a target.");
return;
}

// Name and color applying must be applied before SelectedColorIndex
base.ProcessMetadata(subName.gameObject, metadata);
using (PacketSuppressor<EntityMetadataUpdate>.Suppress())
{
subNameInput.SetSelected(metadata.SelectedColorIndex);
}
}
}
36 changes: 0 additions & 36 deletions NitroxClient/GameLogic/Spawning/Metadata/SubNameInputProcessor.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ namespace NitroxModel.DataStructures.GameLogic.Entities.Metadata
[ProtoInclude(290, typeof(WaterParkCreatureMetadata))]
[ProtoInclude(300, typeof(BeaconMetadata))]
[ProtoInclude(310, typeof(FlareMetadata))]
[ProtoInclude(320, typeof(ExosuitMetadata))]
[ProtoInclude(330, typeof(VehicleMetadata))]
[ProtoInclude(340, typeof(NamedColoredMetadata))]
public abstract class EntityMetadata
{
}
Expand Down
Loading

0 comments on commit 9efe596

Please sign in to comment.