This repository has been archived by the owner on Jan 18, 2022. It is now read-only.
/
ConnectionFlowInitializers.cs
75 lines (66 loc) · 3.08 KB
/
ConnectionFlowInitializers.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
using System.Collections.Generic;
namespace Improbable.Gdk.Core
{
/// <summary>
/// Represents an object which can initialize a connection flow of a certain type.
/// </summary>
/// <typeparam name="TConnectionFlow">The type of connection flow that this object can initialize.</typeparam>
public interface IConnectionFlowInitializer<TConnectionFlow> where TConnectionFlow : IConnectionFlow
{
/// <summary>
/// Initializes the flow. Seeds the flow implementation with the data required to successfully connect.
/// </summary>
/// <param name="flow">The flow object to initialize.</param>
void Initialize(TConnectionFlow flow);
}
/// <summary>
/// Represents an object which can initialize the <see cref="ReceptionistFlow" />, <see cref="LocatorFlow" />,
/// and <see cref="LocatorFlow" /> connection flows from the command line.
/// </summary>
public class CommandLineConnectionFlowInitializer :
IConnectionFlowInitializer<ReceptionistFlow>,
IConnectionFlowInitializer<LocatorFlow>
{
private readonly CommandLineArgs commandLineArgs;
public CommandLineConnectionFlowInitializer()
{
commandLineArgs = CommandLineArgs.FromCommandLine();
}
internal CommandLineConnectionFlowInitializer(Dictionary<string, string> commandLineArgs)
{
this.commandLineArgs = CommandLineArgs.From(commandLineArgs);
}
/// <summary>
/// Gets the connection service to use based on command line arguments.
/// </summary>
/// <returns>The connection service to use.</returns>
public ConnectionService GetConnectionService()
{
if (commandLineArgs.Contains(RuntimeConfigNames.LoginToken) && commandLineArgs.Contains(RuntimeConfigNames.PlayerIdentityToken))
{
return ConnectionService.Locator;
}
return ConnectionService.Receptionist;
}
public void Initialize(ReceptionistFlow receptionist)
{
commandLineArgs.TryGetCommandLineValue(RuntimeConfigNames.ReceptionistHost,
ref receptionist.ReceptionistHost);
commandLineArgs.TryGetCommandLineValue(RuntimeConfigNames.ReceptionistPort,
ref receptionist.ReceptionistPort);
commandLineArgs.TryGetCommandLineValue(RuntimeConfigNames.WorkerId, ref receptionist.WorkerId);
}
public void Initialize(LocatorFlow locator)
{
commandLineArgs.TryGetCommandLineValue(RuntimeConfigNames.LocatorHost, ref locator.LocatorHost);
commandLineArgs.TryGetCommandLineValue(RuntimeConfigNames.PlayerIdentityToken,
ref locator.PlayerIdentityToken);
commandLineArgs.TryGetCommandLineValue(RuntimeConfigNames.LoginToken, ref locator.LoginToken);
if (!string.IsNullOrEmpty(locator.PlayerIdentityToken)
&& !string.IsNullOrEmpty(locator.LoginToken))
{
locator.UseDevAuthFlow = false;
}
}
}
}