Skip to content
This repository was archived by the owner on Oct 20, 2021. It is now read-only.

Commit 689a835

Browse files
author
Jamie Brynes
authored
Worker Abstraction Update (#203)
1 parent 1da5392 commit 689a835

17 files changed

+521
-422
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
## Unreleased
44

5+
### Changed
6+
7+
- Updated the worker connectors following the [refactor in the GDK](https://github.com/spatialos/gdk-for-unity/pull/981). [#203](https://github.com/spatialos/gdk-for-unity-fps-starter-project/pull/203).
8+
- Refactored the simulated player flow to closer align with the worker connector changes mentioned above. [#203](https://github.com/spatialos/gdk-for-unity-fps-starter-project/pull/203)
9+
510
## `0.2.3` - 2019-06-12
611

712
### Changed

gdk.pinned

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
eae6cf5dcdb72655bde2121614001cdf1dde51bf
1+
84243525d98aff511e7aa1f7703c37347017e386

workers/unity/Assets/Fps/Prefabs/SimulatedPlayerCoordinatorWorker.prefab

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,10 @@ MonoBehaviour:
4444
m_Name:
4545
m_EditorClassIdentifier:
4646
MaxConnectionAttempts: 3
47-
DevelopmentAuthToken:
48-
UseExternalIp: 0
4947
TargetFrameRate: 30
5048
mapTemplate: {fileID: 11400000, guid: f62fdf28a64923b4caf60e8249f24ed7, type: 2}
5149
SimulatedPlayerWorkerConnector: {fileID: 1673159791827972, guid: 416b79ec2ab48a14b94f5d11fe45403f,
5250
type: 3}
53-
DefaultSimulatedPlayerCount: 1
54-
DefaultSimulatedPlayerCreationInterval: 5
55-
SimulatedPlayerDevAuthTokenId:
56-
SimulatedPlayerTargetDeployment:
51+
MaxSimulatedPlayerCount: 1
52+
SimulatedPlayerCreationInterval: 5
53+
UseSessionFlow: 0

workers/unity/Assets/Fps/Scripts/GameLogic/StateMachine/Default/DefaultConnectState.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public override void Tick()
3232
return;
3333
}
3434

35-
if (!Blackboard.ClientConnector.HasConnected())
35+
if (!Blackboard.ClientConnector.HasConnected)
3636
{
3737
return;
3838
}

workers/unity/Assets/Fps/Scripts/GameLogic/StateMachine/Session/SessionConnectState.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public override void StartState()
4141

4242
public override void Tick()
4343
{
44-
if (!Blackboard.ClientConnector.HasConnected())
44+
if (!Blackboard.ClientConnector.HasConnected)
4545
{
4646
return;
4747
}

workers/unity/Assets/Fps/Scripts/MetricSendSystem.cs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace Fps
88
{
99
public class MetricSendSystem : ComponentSystem
1010
{
11-
private Connection connection;
11+
private WorkerSystem worker;
1212

1313
private DateTime timeOfNextUpdate;
1414
private DateTime timeOfLastUpdate;
@@ -31,7 +31,7 @@ public class MetricSendSystem : ComponentSystem
3131
protected override void OnCreate()
3232
{
3333
base.OnCreate();
34-
connection = World.GetExistingSystem<WorkerSystem>().Connection;
34+
worker = World.GetExistingSystem<WorkerSystem>();
3535

3636
targetFps = Application.targetFrameRate == -1
3737
? DefaultTargetFrameRate
@@ -46,19 +46,14 @@ protected override void OnCreate()
4646

4747
protected override void OnUpdate()
4848
{
49-
if (connection == null)
50-
{
51-
return;
52-
}
53-
5449
if (DateTime.Now >= timeOfNextUpdate)
5550
{
5651
CalculateFps();
5752
WorkerMetrics.GaugeMetrics["Dynamic.FPS"] = calculatedFps;
5853
WorkerMetrics.GaugeMetrics["Unity used heap size"] = GC.GetTotalMemory(false);
5954
WorkerMetrics.Load = CalculateLoad();
6055

61-
connection.SendMetrics(WorkerMetrics);
56+
worker.SendMetrics(WorkerMetrics);
6257

6358
timeOfLastUpdate = DateTime.Now;
6459
timeOfNextUpdate = timeOfLastUpdate.AddSeconds(TimeBetweenMetricUpdatesSecs);

workers/unity/Assets/Fps/Scripts/SetupLogic/AdvancedEntityPipeline.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public class AdvancedEntityPipeline : IEntityGameObjectCreator
2222

2323
private readonly IEntityGameObjectCreator fallback;
2424
private readonly string workerIdAttribute;
25-
private readonly Worker worker;
25+
private readonly WorkerInWorld worker;
2626

2727
private readonly Dictionary<EntityId, GameObject> gameObjectsCreated = new Dictionary<EntityId, GameObject>();
2828

@@ -34,7 +34,7 @@ public class AdvancedEntityPipeline : IEntityGameObjectCreator
3434
typeof(Rigidbody)
3535
};
3636

37-
public AdvancedEntityPipeline(Worker worker, string authPlayer, string nonAuthPlayer,
37+
public AdvancedEntityPipeline(WorkerInWorld worker, string authPlayer, string nonAuthPlayer,
3838
IEntityGameObjectCreator fallback)
3939
{
4040
this.worker = worker;

workers/unity/Assets/Fps/Scripts/SetupLogic/ClientWorkerConnector.cs

Lines changed: 95 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,28 @@
11
using System;
22
using System.Collections;
33
using System.Collections.Generic;
4+
using System.Linq;
45
using System.Text;
56
using Improbable.Gdk.Core;
6-
using UnityEngine;
77
using Improbable.Gdk.GameObjectCreation;
88
using Improbable.Gdk.PlayerLifecycle;
99
using Improbable.Worker.CInterop;
1010
using Improbable.Worker.CInterop.Alpha;
11-
using Unity.Entities;
11+
using UnityEngine;
1212

1313
namespace Fps
1414
{
1515
public class ClientWorkerConnector : WorkerConnectorBase
1616
{
17-
private string deployment;
17+
protected string deployment;
18+
1819
private string playerName;
1920
private bool isReadyToSpawn;
2021
private bool wantsSpawn;
2122
private Action<PlayerCreator.CreatePlayer.ReceivedResponse> onPlayerResponse;
2223
private AdvancedEntityPipeline entityPipeline;
2324

25+
public bool HasConnected => Worker != null;
2426
protected bool UseSessionFlow => !string.IsNullOrEmpty(deployment);
2527

2628
public event Action OnLostPlayerEntity;
@@ -38,21 +40,11 @@ public void SpawnPlayer(string playerName, Action<PlayerCreator.CreatePlayer.Rec
3840
wantsSpawn = true;
3941
}
4042

41-
public bool HasConnected()
42-
{
43-
return Worker != null;
44-
}
45-
4643
public void DisconnectPlayer()
4744
{
4845
StartCoroutine(PrepareDestroy());
4946
}
5047

51-
protected override string GetWorkerType()
52-
{
53-
return WorkerUtils.UnityClient;
54-
}
55-
5648
protected virtual string GetAuthPlayerPrefabPath()
5749
{
5850
return "Prefabs/UnityClient/Authoritative/Player";
@@ -63,36 +55,51 @@ protected virtual string GetNonAuthPlayerPrefabPath()
6355
return "Prefabs/UnityClient/NonAuthoritative/Player";
6456
}
6557

66-
protected override AlphaLocatorConfig GetAlphaLocatorConfig(string workerType)
58+
protected override IConnectionHandlerBuilder GetConnectionHandlerBuilder()
6759
{
68-
return UseSessionFlow
69-
? GetAlphaLocatorConfigViaDevAuthFlow(workerType)
70-
: base.GetAlphaLocatorConfig(workerType);
71-
}
60+
var connectionParams = new ConnectionParameters
61+
{
62+
DefaultComponentVtable = new ComponentVtable(),
63+
WorkerType = WorkerUtils.UnityClient
64+
};
65+
66+
var builder = new SpatialOSConnectionHandlerBuilder()
67+
.SetConnectionParameters(connectionParams);
7268

73-
protected override string SelectLoginToken(List<LoginTokenDetails> loginTokens)
74-
{
7569
if (UseSessionFlow)
7670
{
77-
foreach (var loginToken in loginTokens)
78-
{
79-
if (loginToken.DeploymentName == deployment)
80-
{
81-
return loginToken.LoginToken;
82-
}
83-
}
71+
connectionParams.Network.UseExternalIp = true;
72+
builder.SetConnectionFlow(new ChosenDeploymentAlphaLocatorFlow(deployment,
73+
new SessionConnectionFlowInitializer(new CommandLineConnectionFlowInitializer())));
8474
}
85-
else
75+
else if (Application.isEditor)
8676
{
87-
return base.SelectLoginToken(loginTokens);
77+
builder.SetConnectionFlow(new ReceptionistFlow(CreateNewWorkerId(WorkerUtils.UnityClient)));
8878
}
79+
else
80+
{
81+
var initializer = new CommandLineConnectionFlowInitializer();
8982

90-
throw new ArgumentException("Was not able to connect to deployment.");
91-
}
83+
switch (initializer.GetConnectionService())
84+
{
85+
case ConnectionService.Receptionist:
86+
builder.SetConnectionFlow(new ReceptionistFlow(CreateNewWorkerId(WorkerUtils.UnityClient),
87+
initializer));
88+
break;
89+
case ConnectionService.Locator:
90+
connectionParams.Network.UseExternalIp = true;
91+
builder.SetConnectionFlow(new LocatorFlow(initializer));
92+
break;
93+
case ConnectionService.AlphaLocator:
94+
connectionParams.Network.UseExternalIp = true;
95+
builder.SetConnectionFlow(new AlphaLocatorFlow(initializer));
96+
break;
97+
default:
98+
throw new ArgumentOutOfRangeException();
99+
}
100+
}
92101

93-
protected override ConnectionService GetConnectionService()
94-
{
95-
return UseSessionFlow ? ConnectionService.AlphaLocator : base.GetConnectionService();
102+
return builder;
96103
}
97104

98105
protected override void HandleWorkerConnectionEstablished()
@@ -169,4 +176,58 @@ private void SendRequest()
169176
.RequestPlayerCreation(serializedArgs, onPlayerResponse);
170177
}
171178
}
179+
180+
internal class ChosenDeploymentAlphaLocatorFlow : AlphaLocatorFlow
181+
{
182+
private readonly string targetDeployment;
183+
184+
public ChosenDeploymentAlphaLocatorFlow(string targetDeployment,
185+
IConnectionFlowInitializer<AlphaLocatorFlow> initializer = null) : base(initializer)
186+
{
187+
this.targetDeployment = targetDeployment;
188+
}
189+
190+
protected override string SelectLoginToken(List<LoginTokenDetails> loginTokens)
191+
{
192+
var token = loginTokens.FirstOrDefault(loginToken => loginToken.DeploymentName == targetDeployment);
193+
194+
return token.LoginToken ?? throw new ArgumentException("Was not able to connect to deployment");
195+
}
196+
}
197+
198+
internal class SessionConnectionFlowInitializer : IConnectionFlowInitializer<AlphaLocatorFlow>
199+
{
200+
private IConnectionFlowInitializer<AlphaLocatorFlow> initializer;
201+
202+
public SessionConnectionFlowInitializer(IConnectionFlowInitializer<AlphaLocatorFlow> standaloneInitializer)
203+
{
204+
initializer = standaloneInitializer;
205+
}
206+
207+
public void Initialize(AlphaLocatorFlow flow)
208+
{
209+
if (Application.isEditor)
210+
{
211+
if (PlayerPrefs.HasKey(RuntimeConfigNames.DevAuthTokenKey))
212+
{
213+
flow.DevAuthToken = PlayerPrefs.GetString(RuntimeConfigNames.DevAuthTokenKey);
214+
return;
215+
}
216+
217+
var textAsset = Resources.Load<TextAsset>("DevAuthToken");
218+
219+
if (textAsset == null)
220+
{
221+
throw new MissingReferenceException("Unable to find DevAuthToken.txt in the Resources folder. " +
222+
"You can generate one via SpatialOS > Generate Dev Authentication Token.");
223+
}
224+
225+
flow.DevAuthToken = textAsset.text;
226+
}
227+
else
228+
{
229+
initializer.Initialize(flow);
230+
}
231+
}
232+
}
172233
}

workers/unity/Assets/Fps/Scripts/SetupLogic/GameLogicWorkerConnector.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
using System.Collections;
2-
using UnityEngine;
2+
using Improbable.Gdk.Core;
33
using Improbable.Gdk.GameObjectCreation;
44
using Improbable.Gdk.Guns;
55
using Improbable.Gdk.Health;
66
using Improbable.Gdk.PlayerLifecycle;
7+
using UnityEngine;
78

89
namespace Fps
910
{
@@ -17,9 +18,22 @@ protected override async void Start()
1718
await AttemptConnect();
1819
}
1920

20-
protected override string GetWorkerType()
21+
protected override IConnectionHandlerBuilder GetConnectionHandlerBuilder()
2122
{
22-
return WorkerUtils.UnityGameLogic;
23+
var builder = new SpatialOSConnectionHandlerBuilder()
24+
.SetConnectionParameters(CreateConnectionParameters(WorkerUtils.UnityGameLogic));
25+
26+
if (Application.isEditor)
27+
{
28+
builder.SetConnectionFlow(new ReceptionistFlow(CreateNewWorkerId(WorkerUtils.UnityGameLogic)));
29+
}
30+
else
31+
{
32+
builder.SetConnectionFlow(new ReceptionistFlow(CreateNewWorkerId(WorkerUtils.UnityGameLogic),
33+
new CommandLineConnectionFlowInitializer()));
34+
}
35+
36+
return builder;
2337
}
2438

2539
protected override void HandleWorkerConnectionEstablished()

0 commit comments

Comments
 (0)