Skip to content

Commit

Permalink
Added tests for SerializedWorldEntity in WorldPersistenceTest
Browse files Browse the repository at this point in the history
  • Loading branch information
tornac1234 committed Dec 27, 2023
1 parent 7b10ab9 commit 2af9c87
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
14 changes: 11 additions & 3 deletions Nitrox.Test/Server/Serialization/WorldPersistenceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using NitroxModel.DataStructures.GameLogic.Entities.Bases;
using NitroxModel.DataStructures.GameLogic.Entities.Metadata;
using NitroxModel.DataStructures.GameLogic.Entities.Metadata.Bases;
using NitroxModel.DataStructures;

namespace NitroxServer.Serialization;

Expand Down Expand Up @@ -355,6 +356,16 @@ private static void EntityTest(Entity entity, Entity entityAfter)
Assert.AreEqual(oxygenPipeEntity.RootPipeId, oxygenPipeEntityAfter.RootPipeId);
Assert.AreEqual(oxygenPipeEntity.ParentPosition, oxygenPipeEntityAfter.ParentPosition);
break;
case PrefabPlaceholderEntity prefabPlaceholderEntity when entityAfter is PrefabPlaceholderEntity prefabPlaceholderEntityAfter:
Assert.AreEqual(prefabPlaceholderEntity.ComponentIndex, prefabPlaceholderEntityAfter.ComponentIndex);
break;
case SerializedWorldEntity serializedWorldEntity when entityAfter is SerializedWorldEntity serializedWorldEntityAfter:
Assert.AreEqual(serializedWorldEntity.AbsoluteEntityCell, serializedWorldEntityAfter.AbsoluteEntityCell);
AssertHelper.IsListEqual(serializedWorldEntity.Components.OrderBy(c => c.GetHashCode()), serializedWorldEntityAfter.Components.OrderBy(c => c.GetHashCode()), (SerializedComponent c1, SerializedComponent c2) => c1.Equals(c2));
Assert.AreEqual(serializedWorldEntity.Layer, serializedWorldEntityAfter.Layer);
Assert.AreEqual(serializedWorldEntity.BatchId, serializedWorldEntityAfter.BatchId);
Assert.AreEqual(serializedWorldEntity.CellId, serializedWorldEntityAfter.CellId);
break;
case GlobalRootEntity globalRootEntity when worldEntityAfter is GlobalRootEntity globalRootEntityAfter:
if (globalRootEntity.GetType() != typeof(GlobalRootEntity))
{
Expand Down Expand Up @@ -420,9 +431,6 @@ private static void EntityTest(Entity entity, Entity entityAfter)
Assert.AreEqual(prefabChildEntity.ComponentIndex, prefabChildEntityAfter.ComponentIndex);
Assert.AreEqual(prefabChildEntity.ClassId, prefabChildEntityAfter.ClassId);
break;
case PrefabPlaceholderEntity prefabPlaceholderEntity when entityAfter is PrefabPlaceholderEntity prefabPlaceholderEntityAfter:
Assert.AreEqual(prefabPlaceholderEntity.ComponentIndex, prefabPlaceholderEntityAfter.ComponentIndex);
break;
case InventoryEntity inventoryEntity when entityAfter is InventoryEntity inventoryEntityAfter:
Assert.AreEqual(inventoryEntity.ComponentIndex, inventoryEntityAfter.ComponentIndex);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ private bool VerifyCanSpawnOrError(WorldEntity entity, Optional<GameObject> pare
return true;
}

Log.Error($"[{nameof(PrefabPlaceholderEntity)}] Can't find a {nameof(PrefabPlaceholdersGroup)} on parent for {entity.Id}");
Log.Error($"[{nameof(PrefabPlaceholderEntitySpawner)}] Can't find a {nameof(PrefabPlaceholdersGroup)} on parent for {entity.Id}");
placeholder = null;
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using NitroxModel.DataStructures.GameLogic.Entities;
using NitroxModel.DataStructures.Unity;
using NitroxModel.DataStructures.Util;
using NitroxModel.Helper;
using NitroxModel_Subnautica.DataStructures;
using UnityEngine;
using UWE;
Expand All @@ -25,7 +26,7 @@ public class SerializedWorldEntitySpawner : IWorldEntitySpawner, IWorldEntitySyn
public SerializedWorldEntitySpawner()
{
// Preloading a useful asset
if (!ProtobufSerializer.emptyGameObjectPrefab)
if (!NitroxEnvironment.IsTesting && !ProtobufSerializer.emptyGameObjectPrefab)
{
ProtobufSerializer.emptyGameObjectPrefab = Resources.Load<GameObject>("SerializerEmptyGameObject");
}
Expand Down
19 changes: 19 additions & 0 deletions NitroxModel/DataStructures/SerializedComponent.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;

namespace NitroxModel.DataStructures;
Expand Down Expand Up @@ -29,4 +30,22 @@ public SerializedComponent(string typeName, bool isEnabled, byte[] data)
IsEnabled = isEnabled;
Data = data;
}

// Generated by Visual Studio
public override bool Equals(object obj)
{
return obj is SerializedComponent component &&
TypeName == component.TypeName &&
IsEnabled == component.IsEnabled &&
EqualityComparer<byte[]>.Default.Equals(Data, component.Data);
}

public override int GetHashCode()
{
int hashCode = -1120560399;
hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(TypeName);
hashCode = hashCode * -1521134295 + IsEnabled.GetHashCode();
hashCode = hashCode * -1521134295 + EqualityComparer<byte[]>.Default.GetHashCode(Data);
return hashCode;
}
}

0 comments on commit 2af9c87

Please sign in to comment.