Skip to content
This repository was archived by the owner on Jan 18, 2022. It is now read-only.

Commit c77d059

Browse files
authored
Better reference provider (#1358)
* No longer code-gen the reference providers Replace uint handle with typed ReferenceHandle Move API to handle to abstract the reference provider * Fix cleanup logic to dispose of ReferenceHandle's Remove redundant world tracking in ReferenceProviders * Move ReferenceProvider.cs to Collections * Add Unity tests for ReferenceProvider * Changelog
1 parent 9c57d9e commit c77d059

File tree

44 files changed

+1032
-7930
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1032
-7930
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
- GDK Tools Configuration window now autosaves. [#1356](https://github.com/spatialos/gdk-for-unity/pull/1356)
1414

15-
1615
### Fixed
1716

1817
- Invalid JSON is now logged if there is an error parsing the codegen output. [#1353](https://github.com/spatialos/gdk-for-unity/pull/1353)
@@ -22,6 +21,7 @@
2221
### Internal
2322

2423
- Produce code coverage reports in tests [#1359](https://github.com/spatialos/gdk-for-unity/pull/1359)
24+
- Replaced code generated ReferenceProviders with generic version. [#1358](https://github.com/spatialos/gdk-for-unity/pull/1358)
2525

2626
## `0.3.5` - 2020-04-23
2727

test-project/Assets/EditmodeTests/Collections.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using Improbable.Gdk.Core;
2+
using Improbable.Gdk.TestUtils;
3+
using NUnit.Framework;
4+
using Assert = Unity.Assertions.Assert;
5+
6+
namespace Improbable.Gdk.EditmodeTests.Collections
7+
{
8+
[TestFixture]
9+
public class ReferenceProviderTests
10+
{
11+
private readonly long entityId = 100;
12+
13+
[Test]
14+
public void Disposed_world_cleans_provider()
15+
{
16+
using (var mockWorld = MockWorld.Create(new MockWorld.Options()))
17+
{
18+
mockWorld.Step(world =>
19+
{
20+
world.Connection.CreateEntity(entityId, new EntityTemplate());
21+
22+
world.Connection.AddComponent(entityId, Position.ComponentId,
23+
new Position.Update {Coords = Coordinates.Zero});
24+
25+
world.Connection.AddComponent(entityId, Metadata.ComponentId,
26+
new Metadata.Update {EntityType = "EntityWithReferenceComponent"});
27+
});
28+
29+
Assert.AreEqual(1, ReferenceProvider<string>.Count);
30+
}
31+
32+
Assert.AreEqual(0, ReferenceProvider<string>.Count);
33+
}
34+
35+
[Test]
36+
public void Removed_component_disposes_reference()
37+
{
38+
using (var mockWorld = MockWorld.Create(new MockWorld.Options()))
39+
{
40+
mockWorld.Step(world =>
41+
{
42+
world.Connection.CreateEntity(entityId, new EntityTemplate());
43+
44+
world.Connection.AddComponent(entityId, Position.ComponentId,
45+
new Position.Update {Coords = Coordinates.Zero});
46+
47+
world.Connection.AddComponent(entityId, Metadata.ComponentId,
48+
new Metadata.Update {EntityType = "EntityWithReferenceComponent"});
49+
}).Step(world =>
50+
{
51+
Assert.AreEqual(1, ReferenceProvider<string>.Count);
52+
world.Connection.RemoveComponent(entityId, Metadata.ComponentId);
53+
});
54+
55+
Assert.AreEqual(0, ReferenceProvider<string>.Count);
56+
}
57+
}
58+
}
59+
}

test-project/Assets/EditmodeTests/Collections/ReferenceProviderTests.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test-project/Assets/Generated/Source/improbable/dependentschema/DependentComponent.cs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ public unsafe struct Component : IComponentData, ISpatialComponentData, ISnapsho
2020
// Bit masks for tracking which component properties were changed locally and need to be synced.
2121
private fixed UInt32 dirtyBits[1];
2222

23-
internal uint aHandle;
23+
internal global::Improbable.Gdk.Core.ReferenceProvider<global::Improbable.TestSchema.ExhaustiveRepeatedData>.ReferenceHandle aHandle;
2424

2525
public global::Improbable.TestSchema.ExhaustiveRepeatedData A
2626
{
27-
get => global::Improbable.DependentSchema.DependentComponent.ReferenceTypeProviders.AProvider.Get(aHandle);
27+
get => aHandle.Get();
2828
set
2929
{
3030
MarkDataDirty(0);
31-
global::Improbable.DependentSchema.DependentComponent.ReferenceTypeProviders.AProvider.Set(aHandle, value);
31+
aHandle.Set(value);
3232
}
3333
}
3434

@@ -44,39 +44,39 @@ public unsafe struct Component : IComponentData, ISpatialComponentData, ISnapsho
4444
}
4545
}
4646

47-
internal uint cHandle;
47+
internal global::Improbable.Gdk.Core.ReferenceProvider<global::Improbable.TestSchema.SomeEnum?>.ReferenceHandle cHandle;
4848

4949
public global::Improbable.TestSchema.SomeEnum? C
5050
{
51-
get => global::Improbable.DependentSchema.DependentComponent.ReferenceTypeProviders.CProvider.Get(cHandle);
51+
get => cHandle.Get();
5252
set
5353
{
5454
MarkDataDirty(2);
55-
global::Improbable.DependentSchema.DependentComponent.ReferenceTypeProviders.CProvider.Set(cHandle, value);
55+
cHandle.Set(value);
5656
}
5757
}
5858

59-
internal uint dHandle;
59+
internal global::Improbable.Gdk.Core.ReferenceProvider<global::System.Collections.Generic.List<global::Improbable.TestSchema.SomeType>>.ReferenceHandle dHandle;
6060

6161
public global::System.Collections.Generic.List<global::Improbable.TestSchema.SomeType> D
6262
{
63-
get => global::Improbable.DependentSchema.DependentComponent.ReferenceTypeProviders.DProvider.Get(dHandle);
63+
get => dHandle.Get();
6464
set
6565
{
6666
MarkDataDirty(3);
67-
global::Improbable.DependentSchema.DependentComponent.ReferenceTypeProviders.DProvider.Set(dHandle, value);
67+
dHandle.Set(value);
6868
}
6969
}
7070

71-
internal uint eHandle;
71+
internal global::Improbable.Gdk.Core.ReferenceProvider<global::System.Collections.Generic.Dictionary<global::Improbable.TestSchema.SomeEnum, global::Improbable.TestSchema.SomeType>>.ReferenceHandle eHandle;
7272

7373
public global::System.Collections.Generic.Dictionary<global::Improbable.TestSchema.SomeEnum, global::Improbable.TestSchema.SomeType> E
7474
{
75-
get => global::Improbable.DependentSchema.DependentComponent.ReferenceTypeProviders.EProvider.Get(eHandle);
75+
get => eHandle.Get();
7676
set
7777
{
7878
MarkDataDirty(4);
79-
global::Improbable.DependentSchema.DependentComponent.ReferenceTypeProviders.EProvider.Set(eHandle, value);
79+
eHandle.Set(value);
8080
}
8181
}
8282

@@ -370,20 +370,20 @@ public static void SerializeSnapshot(global::Improbable.DependentSchema.Dependen
370370
{
371371
var component = new global::Improbable.DependentSchema.DependentComponent.Component();
372372

373-
component.aHandle = global::Improbable.DependentSchema.DependentComponent.ReferenceTypeProviders.AProvider.Allocate(world);
373+
component.aHandle = global::Improbable.Gdk.Core.ReferenceProvider<global::Improbable.TestSchema.ExhaustiveRepeatedData>.Create();
374374

375375
component.A = global::Improbable.TestSchema.ExhaustiveRepeatedData.Serialization.Deserialize(obj.GetObject(1));
376376

377377
component.B = (global::Improbable.TestSchema.SomeEnum) obj.GetEnum(2);
378378

379-
component.cHandle = global::Improbable.DependentSchema.DependentComponent.ReferenceTypeProviders.CProvider.Allocate(world);
379+
component.cHandle = global::Improbable.Gdk.Core.ReferenceProvider<global::Improbable.TestSchema.SomeEnum?>.Create();
380380

381381
if (obj.GetEnumCount(3) == 1)
382382
{
383383
component.C = new global::Improbable.TestSchema.SomeEnum?((global::Improbable.TestSchema.SomeEnum) obj.GetEnum(3));
384384
}
385385

386-
component.dHandle = global::Improbable.DependentSchema.DependentComponent.ReferenceTypeProviders.DProvider.Allocate(world);
386+
component.dHandle = global::Improbable.Gdk.Core.ReferenceProvider<global::System.Collections.Generic.List<global::Improbable.TestSchema.SomeType>>.Create();
387387

388388
{
389389
component.D = new global::System.Collections.Generic.List<global::Improbable.TestSchema.SomeType>();
@@ -396,7 +396,7 @@ public static void SerializeSnapshot(global::Improbable.DependentSchema.Dependen
396396
}
397397
}
398398

399-
component.eHandle = global::Improbable.DependentSchema.DependentComponent.ReferenceTypeProviders.EProvider.Allocate(world);
399+
component.eHandle = global::Improbable.Gdk.Core.ReferenceProvider<global::System.Collections.Generic.Dictionary<global::Improbable.TestSchema.SomeEnum, global::Improbable.TestSchema.SomeType>>.Create();
400400

401401
{
402402
var map = new global::System.Collections.Generic.Dictionary<global::Improbable.TestSchema.SomeEnum, global::Improbable.TestSchema.SomeType>();

test-project/Assets/Generated/Source/improbable/dependentschema/DependentComponentEcsViewManager.cs

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Unity.Entities;
77
using Improbable.Worker.CInterop;
88
using Improbable.Gdk.Core;
9+
using Unity.Collections;
910

1011
namespace Improbable.DependentSchema
1112
{
@@ -15,7 +16,6 @@ public class EcsViewManager : IEcsViewManager
1516
{
1617
private WorkerSystem workerSystem;
1718
private EntityManager entityManager;
18-
private World world;
1919

2020
private readonly ComponentType[] initialComponents = new ComponentType[]
2121
{
@@ -64,7 +64,6 @@ public void ApplyDiff(ViewDiff diff)
6464

6565
public void Init(World world)
6666
{
67-
this.world = world;
6867
entityManager = world.EntityManager;
6968

7069
workerSystem = world.GetExistingSystem<WorkerSystem>();
@@ -75,29 +74,37 @@ public void Init(World world)
7574
}
7675
}
7776

78-
public void Clean(World world)
77+
public void Clean()
7978
{
80-
global::Improbable.DependentSchema.DependentComponent.ReferenceTypeProviders.AProvider.CleanDataInWorld(world);
79+
var query = entityManager.CreateEntityQuery(typeof(global::Improbable.DependentSchema.DependentComponent.Component));
80+
var componentDataArray = query.ToComponentDataArray<global::Improbable.DependentSchema.DependentComponent.Component>(Allocator.TempJob);
8181

82-
global::Improbable.DependentSchema.DependentComponent.ReferenceTypeProviders.CProvider.CleanDataInWorld(world);
82+
foreach (var component in componentDataArray)
83+
{
84+
component.aHandle.Dispose();
85+
86+
component.cHandle.Dispose();
8387

84-
global::Improbable.DependentSchema.DependentComponent.ReferenceTypeProviders.DProvider.CleanDataInWorld(world);
88+
component.dHandle.Dispose();
89+
90+
component.eHandle.Dispose();
91+
}
8592

86-
global::Improbable.DependentSchema.DependentComponent.ReferenceTypeProviders.EProvider.CleanDataInWorld(world);
93+
componentDataArray.Dispose();
8794
}
8895

8996
private void AddComponent(EntityId entityId)
9097
{
9198
var entity = workerSystem.GetEntity(entityId);
9299
var component = new global::Improbable.DependentSchema.DependentComponent.Component();
93100

94-
component.aHandle = global::Improbable.DependentSchema.DependentComponent.ReferenceTypeProviders.AProvider.Allocate(world);
101+
component.aHandle = global::Improbable.Gdk.Core.ReferenceProvider<global::Improbable.TestSchema.ExhaustiveRepeatedData>.Create();
95102

96-
component.cHandle = global::Improbable.DependentSchema.DependentComponent.ReferenceTypeProviders.CProvider.Allocate(world);
103+
component.cHandle = global::Improbable.Gdk.Core.ReferenceProvider<global::Improbable.TestSchema.SomeEnum?>.Create();
97104

98-
component.dHandle = global::Improbable.DependentSchema.DependentComponent.ReferenceTypeProviders.DProvider.Allocate(world);
105+
component.dHandle = global::Improbable.Gdk.Core.ReferenceProvider<global::System.Collections.Generic.List<global::Improbable.TestSchema.SomeType>>.Create();
99106

100-
component.eHandle = global::Improbable.DependentSchema.DependentComponent.ReferenceTypeProviders.EProvider.Allocate(world);
107+
component.eHandle = global::Improbable.Gdk.Core.ReferenceProvider<global::System.Collections.Generic.Dictionary<global::Improbable.TestSchema.SomeEnum, global::Improbable.TestSchema.SomeType>>.Create();
101108

102109
component.MarkDataClean();
103110
entityManager.AddComponentData(entity, component);
@@ -110,13 +117,13 @@ private void RemoveComponent(EntityId entityId)
110117

111118
var data = entityManager.GetComponentData<global::Improbable.DependentSchema.DependentComponent.Component>(entity);
112119

113-
global::Improbable.DependentSchema.DependentComponent.ReferenceTypeProviders.AProvider.Free(data.aHandle);
120+
data.aHandle.Dispose();
114121

115-
global::Improbable.DependentSchema.DependentComponent.ReferenceTypeProviders.CProvider.Free(data.cHandle);
122+
data.cHandle.Dispose();
116123

117-
global::Improbable.DependentSchema.DependentComponent.ReferenceTypeProviders.DProvider.Free(data.dHandle);
124+
data.dHandle.Dispose();
118125

119-
global::Improbable.DependentSchema.DependentComponent.ReferenceTypeProviders.EProvider.Free(data.eHandle);
126+
data.eHandle.Dispose();
120127

121128
entityManager.RemoveComponent<global::Improbable.DependentSchema.DependentComponent.Component>(entity);
122129
}

0 commit comments

Comments
 (0)