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

Commit 8d96f9e

Browse files
author
Paul Balaji
authored
Requireable WorkerId (#1016)
1 parent cef6e14 commit 8d96f9e

File tree

11 files changed

+225
-14
lines changed

11 files changed

+225
-14
lines changed

CHANGELOG.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@
55
### Breaking Changes
66

77
- Renamed the `buildTarget` command line argument to `buildEnvironment`. [#1012](https://github.com/spatialos/gdk-for-unity/pull/1012)
8-
- All GDK packages now have an `io.improbable` prefix instead of `com.improbable`.
8+
- All GDK packages now have an `io.improbable` prefix instead of `com.improbable`. [#894](https://github.com/spatialos/gdk-for-unity/pull/894)
99

1010
### Added
1111

1212
- Added the `LinkedGameObjectMap` class for finding the `GameObject`(s) linked with a specified `EntityId`. [#1013](https://github.com/spatialos/gdk-for-unity/pull/1013)
1313
- This can be used with the `[Require]` annotation to inject it into your `MonoBehaviours` provided you are using the `GameObjectCreation` feature module. For example: `[Require] private LinkedGameObjectMap gameObjectMap;`
1414
- Added the ability for the build system to build specific targets of a given build environment. [#1012](https://github.com/spatialos/gdk-for-unity/pull/1012)
1515
- Use the `buildTargetFilter` command line argument to pass in a comma delimited list of build targets to filter for. For example, `+buildTargetFilter win,macos`.
16-
- Added two new GDK packages: `io.improbable.worker.sdk` and `io.improbable.worker.sdk.mobile` which contain the underlying Worker SDK packages for Windows/MacOS/Linux and Android/iOS respectively.
16+
- Added two new GDK packages: `io.improbable.worker.sdk` and `io.improbable.worker.sdk.mobile` which contain the underlying Worker SDK packages for Windows/MacOS/Linux and Android/iOS respectively. [#894](https://github.com/spatialos/gdk-for-unity/pull/894)
17+
- You may now `[Require]` a `WorkerId` in MonoBehaviours. For example: `[Require] private WorkerId workerId;`. [#1016](https://github.com/spatialos/gdk-for-unity/pull/1016)
1718

1819

1920
### Changed
@@ -26,9 +27,9 @@
2627

2728
### Internal
2829

29-
- Stopped throwing a `Test Exception` in playground.
30-
- The embedded `DownloadCoreSdk` `dotnet` project has been removed from `Improbable.Gdk.Tools`.
31-
- The `PluginPostprocessor` tool has been removed from `Improbable.Gdk.Tools`.
30+
- Stopped throwing a `Test Exception` in playground. [#1011](https://github.com/spatialos/gdk-for-unity/pull/1011)
31+
- The embedded `DownloadCoreSdk` `dotnet` project has been removed from `Improbable.Gdk.Tools`. [#894](https://github.com/spatialos/gdk-for-unity/pull/894)
32+
- The `PluginPostprocessor` tool has been removed from `Improbable.Gdk.Tools`. [#894](https://github.com/spatialos/gdk-for-unity/pull/894)
3233

3334
## `0.2.4` - 2019-06-28
3435

workers/unity/Assets/Playground/Resources/Prefabs/Common/Character.prefab

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ GameObject:
1616
- component: {fileID: 114894172867123548}
1717
- component: {fileID: 114602415406350378}
1818
- component: {fileID: 2643701918418317171}
19+
- component: {fileID: -663445870753195477}
1920
m_Layer: 9
2021
m_Name: Character
2122
m_TagString: Untagged
@@ -136,6 +137,18 @@ MonoBehaviour:
136137
m_Script: {fileID: 11500000, guid: 0d4dfc82aab7b1d43b523a4a5172d6f8, type: 3}
137138
m_Name:
138139
m_EditorClassIdentifier:
140+
--- !u!114 &-663445870753195477
141+
MonoBehaviour:
142+
m_ObjectHideFlags: 0
143+
m_CorrespondingSourceObject: {fileID: 0}
144+
m_PrefabInstance: {fileID: 0}
145+
m_PrefabAsset: {fileID: 0}
146+
m_GameObject: {fileID: 1292271339760838}
147+
m_Enabled: 0
148+
m_EditorHideFlags: 0
149+
m_Script: {fileID: 11500000, guid: 5053c3754e6a48118a61672cedc67dec, type: 3}
150+
m_Name:
151+
m_EditorClassIdentifier:
139152
--- !u!1 &1494660013534148
140153
GameObject:
141154
m_ObjectHideFlags: 0
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using Improbable.Gdk.Subscriptions;
2+
using UnityEngine;
3+
4+
namespace Playground
5+
{
6+
public class WorkerIdTest : MonoBehaviour
7+
{
8+
#pragma warning disable 649
9+
[Require] private WorkerId workerId;
10+
#pragma warning restore 649
11+
12+
private void OnEnable()
13+
{
14+
Debug.Log($"Testing [Require] WorkerId: {workerId}");
15+
}
16+
}
17+
}

workers/unity/Assets/Playground/Scripts/MonoBehaviours/WorkerIdTest.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.

workers/unity/Assets/Plugins.meta

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
using System;
2+
using Improbable.Gdk.Core;
3+
using Unity.Entities;
4+
5+
namespace Improbable.Gdk.Subscriptions
6+
{
7+
[AutoRegisterSubscriptionManager]
8+
public class WorkerIdSubscriptionManager : SubscriptionManager<WorkerId>
9+
{
10+
private readonly World world;
11+
private Subscription<WorkerId> cachedSubscription;
12+
13+
public WorkerIdSubscriptionManager(World world)
14+
{
15+
this.world = world;
16+
}
17+
18+
public override Subscription<WorkerId> Subscribe(EntityId entityId)
19+
{
20+
if (cachedSubscription != null)
21+
{
22+
return cachedSubscription;
23+
}
24+
25+
cachedSubscription = new Subscription<WorkerId>(this, new EntityId(0));
26+
var workerId = world.GetExistingSystem<WorkerSystem>().WorkerId;
27+
cachedSubscription.SetAvailable(new WorkerId(workerId));
28+
29+
return cachedSubscription;
30+
}
31+
32+
public override void Cancel(ISubscription subscription)
33+
{
34+
}
35+
36+
public override void ResetValue(ISubscription subscription)
37+
{
38+
}
39+
}
40+
41+
public readonly struct WorkerId : IEquatable<string>, IEquatable<WorkerId>
42+
{
43+
public readonly string Id;
44+
45+
public WorkerId(string id)
46+
{
47+
Id = id;
48+
}
49+
50+
public bool Equals(WorkerId other)
51+
{
52+
if (Id == null)
53+
{
54+
throw new NullReferenceException();
55+
}
56+
57+
return other.Id != null && Id.Equals(other.Id);
58+
}
59+
60+
public bool Equals(string other)
61+
{
62+
if (Id == null)
63+
{
64+
throw new NullReferenceException();
65+
}
66+
67+
return other != null && Id.Equals(other);
68+
}
69+
70+
public override string ToString()
71+
{
72+
return Id;
73+
}
74+
75+
public static implicit operator string(WorkerId workerId)
76+
{
77+
return workerId.Id;
78+
}
79+
}
80+
}

workers/unity/Packages/io.improbable.gdk.core/Subscriptions/StandardSubscriptionManagers/WorkerIdSubscriptionManager.cs.meta

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

workers/unity/Packages/io.improbable.gdk.core/Tests/Editmode/Subscriptions/StandardSubscriptionTests.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ namespace Improbable.Gdk.Core.EditmodeTests.Subscriptions
1212
/// - EntityId
1313
/// - ECS Entity
1414
/// - Log Dispatcher
15+
/// - WorkerId
1516
/// - World Commands
1617
/// - ECS World
1718
/// </summary>
@@ -54,6 +55,14 @@ public void Subscribe_to_log_dispatcher_should_always_be_available()
5455
Assert.AreEqual(LogDispatcher, logSubscription.Value);
5556
}
5657

58+
[Test]
59+
public void Subscribe_to_worker_id_should_always_be_available()
60+
{
61+
var workerIdSubscription = SubscriptionSystem.Subscribe<WorkerId>(entityId);
62+
Assert.True(workerIdSubscription.HasValue);
63+
Assert.AreEqual("TestWorker", workerIdSubscription.Value);
64+
}
65+
5766
[Test]
5867
public void Subscribe_to_world_commands_should_not_be_available_if_the_entity_does_not_exist()
5968
{
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using Improbable.Gdk.Subscriptions;
2+
using NUnit.Framework;
3+
4+
namespace Improbable.Gdk.Core.EditmodeTests.Subscriptions
5+
{
6+
/// <summary>
7+
/// This tests the interoperability of the WorkerId struct with strings.
8+
/// </summary>
9+
[TestFixture]
10+
public class WorkerIdEquatableTests
11+
{
12+
private readonly string workerIdString = "WorkerName-someR4nd0mch4r4ct3r5";
13+
14+
[Test]
15+
public void WorkerId_can_be_implicitly_cast_to_string()
16+
{
17+
Assert.DoesNotThrow(() =>
18+
{
19+
string id = new WorkerId(workerIdString);
20+
});
21+
}
22+
23+
[Test]
24+
public void WorkerId_implicitly_cast_to_string_has_the_same_string_contents()
25+
{
26+
string id = new WorkerId(workerIdString);
27+
28+
Assert.IsTrue(id.Equals(workerIdString));
29+
}
30+
31+
[Test]
32+
public void Identical_WorkerIds_are_equal()
33+
{
34+
WorkerId firstWorkerId = new WorkerId(workerIdString);
35+
WorkerId secondWorkerId = new WorkerId(workerIdString);
36+
37+
Assert.IsTrue(firstWorkerId == secondWorkerId);
38+
}
39+
40+
[Test]
41+
public void Different_WorkerIds_are_not_equal()
42+
{
43+
WorkerId firstWorkerId = new WorkerId("some-workerId");
44+
WorkerId secondWorkerId = new WorkerId("some-different-workerId");
45+
46+
Assert.IsFalse(firstWorkerId == secondWorkerId);
47+
}
48+
49+
[Test]
50+
public void WorkerId_struct_compared_to_equivalent_string()
51+
{
52+
string stringWorkerId = workerIdString;
53+
WorkerId structWorkerId = new WorkerId(stringWorkerId);
54+
55+
Assert.IsTrue(stringWorkerId == structWorkerId);
56+
}
57+
58+
[Test]
59+
public void WorkerId_struct_compared_to_unequivalent_string()
60+
{
61+
string stringWorkerId = "some-workerId";
62+
WorkerId structWorkerId = new WorkerId("some-different-workerId");
63+
64+
Assert.IsFalse(stringWorkerId == structWorkerId);
65+
}
66+
}
67+
}

workers/unity/Packages/io.improbable.gdk.core/Tests/Editmode/Subscriptions/WorkerIdEquatableTests.cs.meta

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

0 commit comments

Comments
 (0)