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

Commit 1e4a762

Browse files
authored
Add worker flag reader (#898)
1 parent d1a62d6 commit 1e4a762

File tree

17 files changed

+316
-0
lines changed

17 files changed

+316
-0
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
- Added a `Improbable.Gdk.Core.Collections.Result<T, E>` struct to represent a result which can either contain a value `T` or an error `E`.
1919
- Added Scripting Define Symbol `DISABLE_REACTIVE_COMPONENTS`. Using this symbol will disable all reactive componts and systems.
2020
- Currently not compatible with the FPS Starter Project.
21+
- Added a `WorkerFlagReader` which you can subscribe and `Require`. This allows you to:
22+
- Add callbacks for changes to worker flags.
23+
- Read the value of worker flags.
24+
- Expose `GetWorkerFlag(string name)` on the `View`.
2125

2226
### Changed
2327

@@ -35,6 +39,7 @@
3539
- Tools package now uses PackageManager API instead of parsing manifest.json.
3640
- Updated default snapshot to have more than one PlayerCreator entity.
3741
- Fixed package dependencies.
42+
- Worker flag changes are propagated to the `ViewDiff`.
3843

3944
## `0.2.0` - 2019-03-18
4045

default_launch.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@
4242
{
4343
"all": {}
4444
}
45+
],
46+
"flags": [
47+
{
48+
"name": "my_flag",
49+
"value": "has_a_value"
50+
}
4551
]
4652
},
4753
{

test-project/Assets/EditmodeTests/Subscriptions/ReaderWriter/InjectionCriteriaTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public void Setup()
3333
world.CreateManager<WorkerSystem>(connectionHandler, null,
3434
new LoggingDispatcher(), "TestWorkerType", Vector3.zero);
3535
receiveSystem = world.CreateManager<SpatialOSReceiveSystem>();
36+
world.GetOrCreateManager<WorkerFlagCallbackSystem>();
3637
world.GetOrCreateManager<ComponentUpdateSystem>();
3738
world.GetOrCreateManager<ComponentConstraintsCallbackSystem>();
3839
world.CreateManager<SubscriptionSystem>();

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ GameObject:
1515
- component: {fileID: 114079189423237002}
1616
- component: {fileID: 114894172867123548}
1717
- component: {fileID: 114602415406350378}
18+
- component: {fileID: 2643701918418317171}
1819
m_Layer: 9
1920
m_Name: Character
2021
m_TagString: Untagged
@@ -125,6 +126,18 @@ MonoBehaviour:
125126
- {fileID: 11400000, guid: f744e75b6283f6d48ad23018d67547ed, type: 2}
126127
- {fileID: 11400000, guid: ea6888e42dd86884891ba6f89a8df6be, type: 2}
127128
SetKinematicWhenNotAuthoritative: 1
129+
--- !u!114 &2643701918418317171
130+
MonoBehaviour:
131+
m_ObjectHideFlags: 0
132+
m_CorrespondingSourceObject: {fileID: 0}
133+
m_PrefabInstance: {fileID: 0}
134+
m_PrefabAsset: {fileID: 0}
135+
m_GameObject: {fileID: 1292271339760838}
136+
m_Enabled: 0
137+
m_EditorHideFlags: 0
138+
m_Script: {fileID: 11500000, guid: 0d4dfc82aab7b1d43b523a4a5172d6f8, type: 3}
139+
m_Name:
140+
m_EditorClassIdentifier:
128141
--- !u!1 &1494660013534148
129142
GameObject:
130143
m_ObjectHideFlags: 0
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using Improbable.Gdk.Core;
2+
using Improbable.Gdk.Subscriptions;
3+
using UnityEngine;
4+
5+
namespace Playground
6+
{
7+
[WorkerType(WorkerUtils.UnityGameLogic)]
8+
public class WorkerFlagTest : MonoBehaviour
9+
{
10+
[Require] private WorkerFlagReader workerFlagReader;
11+
12+
private void OnEnable()
13+
{
14+
workerFlagReader.OnWorkerFlagChange += (name, value) =>
15+
{
16+
Debug.Log($"Worker flag change detected: {name} with value {value}");
17+
};
18+
}
19+
}
20+
}

workers/unity/Assets/Playground/Scripts/MonoBehaviours/WorkerFlagTest.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.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System;
2+
using Improbable.Gdk.Core;
3+
using Unity.Entities;
4+
5+
namespace Improbable.Gdk.Subscriptions
6+
{
7+
public class WorkerFlagCallbackManager : ICallbackManager
8+
{
9+
private readonly Callbacks<(string, string)> callbacks = new Callbacks<(string, string)>();
10+
private readonly WorkerSystem workerSystem;
11+
12+
private ulong nextCallbackId = 1;
13+
14+
public WorkerFlagCallbackManager(World world)
15+
{
16+
workerSystem = world.GetExistingManager<WorkerSystem>();
17+
}
18+
19+
public ulong RegisterCallback(Action<(string, string)> callback)
20+
{
21+
callbacks.Add(nextCallbackId, callback);
22+
return nextCallbackId++;
23+
}
24+
25+
public bool UnregisterCallback(ulong callbackKey)
26+
{
27+
return callbacks.Remove(callbackKey);
28+
}
29+
30+
public void InvokeCallbacks()
31+
{
32+
var workerFlagChanges = workerSystem.Diff.GetWorkerFlagChanges();
33+
for (int i = 0; i < workerFlagChanges.Count; ++i)
34+
{
35+
var pair = workerFlagChanges[i];
36+
callbacks.InvokeAll(pair);
37+
}
38+
}
39+
}
40+
}

workers/unity/Packages/com.improbable.gdk.core/Subscriptions/CalllbackManagers/WorkerFlagCallbackManager.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.
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Improbable.Gdk.Subscriptions;
4+
using Unity.Entities;
5+
6+
namespace Improbable.Gdk.Core
7+
{
8+
[AutoRegisterSubscriptionManager]
9+
public class WorkerFlagSubscriptionManager : SubscriptionManager<WorkerFlagReader>
10+
{
11+
private World world;
12+
13+
private HashSet<Subscription<WorkerFlagReader>> readerSubscriptions = new HashSet<Subscription<WorkerFlagReader>>();
14+
15+
public WorkerFlagSubscriptionManager(World world)
16+
{
17+
this.world = world;
18+
}
19+
20+
public override void Cancel(ISubscription subscription)
21+
{
22+
var readerSubscription = (Subscription<WorkerFlagReader>) subscription;
23+
24+
readerSubscription.Value.IsValid = false;
25+
readerSubscription.Value.RemoveAllCallbacks();
26+
readerSubscriptions.Remove(readerSubscription);
27+
}
28+
29+
public override void ResetValue(ISubscription subscription)
30+
{
31+
var readerSubscription = (Subscription<WorkerFlagReader>) subscription;
32+
33+
readerSubscription.Value.RemoveAllCallbacks();
34+
}
35+
36+
public override Subscription<WorkerFlagReader> Subscribe(EntityId entityId)
37+
{
38+
var subscription = new Subscription<WorkerFlagReader>(this, new EntityId(0));
39+
readerSubscriptions.Add(subscription);
40+
41+
subscription.SetAvailable(new WorkerFlagReader(world));
42+
43+
return subscription;
44+
}
45+
}
46+
47+
public class WorkerFlagReader
48+
{
49+
public bool IsValid;
50+
51+
private readonly WorkerFlagCallbackSystem callbackSystem;
52+
private readonly View view;
53+
54+
private Dictionary<Action<string, string>, ulong> callbackToKey;
55+
56+
public WorkerFlagReader(World world)
57+
{
58+
IsValid = true;
59+
callbackSystem = world.GetExistingManager<WorkerFlagCallbackSystem>();
60+
view = world.GetExistingManager<WorkerSystem>().View;
61+
}
62+
63+
public event Action<string, string> OnWorkerFlagChange
64+
{
65+
add
66+
{
67+
if (callbackToKey == null)
68+
{
69+
callbackToKey = new Dictionary<Action<string, string>, ulong>();
70+
}
71+
72+
var key = callbackSystem.RegisterWorkerFlagChangeCallback(pair =>
73+
{
74+
if (!IsValid)
75+
{
76+
return;
77+
}
78+
79+
value(pair.Item1, pair.Item2);
80+
});
81+
82+
callbackToKey.Add(value, key);
83+
}
84+
remove
85+
{
86+
if (callbackToKey == null)
87+
{
88+
return;
89+
}
90+
91+
if (!callbackToKey.TryGetValue(value, out var key))
92+
{
93+
return;
94+
}
95+
96+
callbackSystem.UnregisterWorkerFlagChangeCallback(key);
97+
callbackToKey.Remove(value);
98+
}
99+
}
100+
101+
public string GetFlag(string name)
102+
{
103+
return view.GetWorkerFlag(name);
104+
}
105+
106+
internal void RemoveAllCallbacks()
107+
{
108+
if (callbackToKey == null)
109+
{
110+
return;
111+
}
112+
113+
foreach (var valuePair in callbackToKey)
114+
{
115+
callbackSystem.UnregisterWorkerFlagChangeCallback(valuePair.Value);
116+
}
117+
118+
callbackToKey.Clear();
119+
}
120+
}
121+
}

workers/unity/Packages/com.improbable.gdk.core/Subscriptions/StandardSubscriptionManagers/WorkerFlagSubscriptionManager.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)