-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix. Update indexed components on deserialization:
See: #15
- Loading branch information
Showing
5 changed files
with
170 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using System.IO; | ||
using Friflo.Engine.ECS; | ||
using Friflo.Engine.ECS.Serialize; | ||
using NUnit.Framework; | ||
using Tests.ECS.Index; | ||
using static NUnit.Framework.Assert; | ||
|
||
// ReSharper disable InconsistentNaming | ||
namespace Tests.ECS.Github | ||
{ | ||
/// <summary> | ||
/// Update indexed components on deserialization | ||
/// https://github.com/friflo/Friflo.Engine.ECS/issues/15 | ||
/// </summary> | ||
public static class Test_GitHub_15 | ||
{ | ||
[Test] | ||
public static void Test_GitHub_15_update_indexed_components_on_deserialization() | ||
{ | ||
var store = new EntityStore(); | ||
var entityA = store.CreateEntity(1); | ||
entityA.AddComponent(new IndexedInt { value = 11 }); | ||
var entityB = store.CreateEntity(2); | ||
entityB.AddComponent(new IndexedInt { value = 12 }); | ||
|
||
AreEqual(1, store.GetEntitiesWithComponentValue<IndexedInt,int>(11).Count); | ||
AreEqual(2, store.GetAllIndexedComponentValues<IndexedInt,int>().Count); | ||
|
||
using var stream = new MemoryStream(); | ||
var serializer = new EntitySerializer(); | ||
serializer.WriteStore(store, stream); | ||
|
||
var readStore = new EntityStore(); | ||
serializer.ReadIntoStore(readStore, stream); | ||
AreEqual(2, readStore.Count); | ||
AreEqual(1, readStore.GetEntitiesWithComponentValue<IndexedInt,int>(11).Count); | ||
AreEqual(2, readStore.GetAllIndexedComponentValues<IndexedInt,int>().Count); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
using System.IO; | ||
using System.Text; | ||
using Friflo.Engine.ECS; | ||
using Friflo.Engine.ECS.Serialize; | ||
using NUnit.Framework; | ||
using Tests.ECS.Index; | ||
using static NUnit.Framework.Assert; | ||
|
||
// ReSharper disable MethodHasAsyncOverload | ||
// ReSharper disable HeuristicUnreachableCode | ||
// ReSharper disable InconsistentNaming | ||
namespace Tests.ECS.Serialize { | ||
|
||
public static class Test_SerializeIndexedComponent | ||
{ | ||
private const string JSON_withIndexedComponent = | ||
@"[{ | ||
""id"": 1, | ||
""components"": { | ||
""IndexedInt"": {""value"":42} | ||
} | ||
}"; | ||
|
||
private const string JSON_withoutIndexedComponent = | ||
@"[{ | ||
""id"": 1, | ||
""components"": { | ||
} | ||
}"; | ||
|
||
[Test] | ||
public static void Test_SerializeIndexedComponent_add() | ||
{ | ||
var store = new EntityStore(); | ||
store.CreateEntity(1); | ||
|
||
var serializer = new EntitySerializer(); | ||
var readStream = new MemoryStream(Encoding.UTF8.GetBytes(JSON_withIndexedComponent)); | ||
serializer.ReadIntoStore(store, readStream); | ||
AreEqual(1, store.GetEntitiesWithComponentValue<IndexedInt,int>(42).Count); | ||
AreEqual(1, store.GetAllIndexedComponentValues<IndexedInt,int>().Count); | ||
} | ||
|
||
[Test] | ||
public static void Test_SerializeIndexedComponent_update() | ||
{ | ||
var store = new EntityStore(); | ||
Entity entity = store.CreateEntity(1); | ||
entity.AddComponent(new IndexedInt { value = 10 }); | ||
AreEqual(1, store.GetEntitiesWithComponentValue<IndexedInt,int>(10).Count); | ||
|
||
var serializer = new EntitySerializer(); | ||
var readStream = new MemoryStream(Encoding.UTF8.GetBytes(JSON_withIndexedComponent)); | ||
serializer.ReadIntoStore(store, readStream); | ||
AreEqual(1, store.GetEntitiesWithComponentValue<IndexedInt,int>(42).Count); | ||
AreEqual(0, store.GetEntitiesWithComponentValue<IndexedInt,int>(10).Count); | ||
AreEqual(1, store.GetAllIndexedComponentValues<IndexedInt,int>().Count); | ||
} | ||
|
||
[Test] | ||
public static void Test_SerializeIndexedComponent_remove() | ||
{ | ||
var store = new EntityStore(); | ||
var entity = store.CreateEntity(1); | ||
entity.AddComponent(new IndexedInt { value = 10 }); | ||
AreEqual(1, store.GetEntitiesWithComponentValue<IndexedInt,int>(10).Count); | ||
// entity.AddComponent(new MyComponent1 { a = 10 }); | ||
|
||
var serializer = new EntitySerializer(); | ||
var readStream = new MemoryStream(Encoding.UTF8.GetBytes(JSON_withoutIndexedComponent)); | ||
serializer.ReadIntoStore(store, readStream); | ||
AreEqual(0, store.GetEntitiesWithComponentValue<IndexedInt,int>(10).Count); | ||
AreEqual(0, store.GetAllIndexedComponentValues<IndexedInt,int>().Count); | ||
} | ||
} | ||
|
||
} |