This repository has been archived by the owner on Jan 18, 2022. It is now read-only.
/
DefaultWorkerConnector.cs
179 lines (160 loc) · 7.61 KB
/
DefaultWorkerConnector.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
using System;
using Improbable.Worker.CInterop;
using Alpha = Improbable.Worker.CInterop.Alpha;
using UnityEngine;
namespace Improbable.Gdk.Core
{
public abstract class DefaultWorkerConnector : WorkerConnector
{
/// <summary>
/// Denotes whether to connect using an external IP address.
/// </summary>
public bool UseExternalIp;
protected override ConnectionService GetConnectionService()
{
var commandLineArguments = Environment.GetCommandLineArgs();
var commandLineArgs = CommandLineUtility.ParseCommandLineArgs(commandLineArguments);
if (commandLineArgs.ContainsKey(RuntimeConfigNames.SteamDeploymentTag) ||
commandLineArgs.ContainsKey(RuntimeConfigNames.SteamTicket))
{
return ConnectionService.Locator;
}
if (commandLineArgs.ContainsKey(RuntimeConfigNames.LoginToken))
{
return commandLineArgs.ContainsKey(RuntimeConfigNames.PlayerIdentityToken)
? ConnectionService.AlphaLocator
: ConnectionService.Locator;
}
return ConnectionService.Receptionist;
}
protected override ConnectionParameters GetConnectionParameters(string workerType, ConnectionService service)
{
// UseExternalIp needs to be true when using the locator
var useExternalIp = service == ConnectionService.Locator
|| service == ConnectionService.AlphaLocator
|| UseExternalIp;
if (Application.isEditor)
{
return new ConnectionParameters
{
WorkerType = workerType,
Network =
{
ConnectionType = RuntimeConfigDefaults.LinkProtocol,
UseExternalIp = useExternalIp,
},
EnableProtocolLoggingAtStartup = false,
DefaultComponentVtable = new ComponentVtable(),
};
}
var commandLineArguments = Environment.GetCommandLineArgs();
var commandLineArgs = CommandLineUtility.ParseCommandLineArgs(commandLineArguments);
var linkProtocol = CommandLineUtility.GetCommandLineValue(
commandLineArgs, RuntimeConfigNames.LinkProtocol, RuntimeConfigDefaults.LinkProtocol);
return new ConnectionParameters
{
WorkerType = workerType,
Network =
{
ConnectionType = linkProtocol,
UseExternalIp = useExternalIp,
},
EnableProtocolLoggingAtStartup = false,
DefaultComponentVtable = new ComponentVtable()
};
}
protected override ReceptionistConfig GetReceptionistConfig(string workerType)
{
if (Application.isEditor)
{
return new ReceptionistConfig
{
ReceptionistHost = RuntimeConfigDefaults.ReceptionistHost,
ReceptionistPort = RuntimeConfigDefaults.ReceptionistPort,
WorkerId = CreateNewWorkerId(workerType)
};
}
var commandLineArguments = Environment.GetCommandLineArgs();
var commandLineArgs = CommandLineUtility.ParseCommandLineArgs(commandLineArguments);
return new ReceptionistConfig
{
ReceptionistHost = CommandLineUtility.GetCommandLineValue(
commandLineArgs, RuntimeConfigNames.ReceptionistHost, RuntimeConfigDefaults.ReceptionistHost),
ReceptionistPort = CommandLineUtility.GetCommandLineValue(
commandLineArgs, RuntimeConfigNames.ReceptionistPort, RuntimeConfigDefaults.ReceptionistPort),
WorkerId = CommandLineUtility.GetCommandLineValue(
commandLineArgs, RuntimeConfigNames.WorkerId, CreateNewWorkerId(workerType)),
};
}
protected override LocatorConfig GetLocatorConfig()
{
var commandLineArguments = Environment.GetCommandLineArgs();
var commandLineArgs = CommandLineUtility.ParseCommandLineArgs(commandLineArguments);
var projectName = CommandLineUtility.GetCommandLineValue(
commandLineArgs, RuntimeConfigNames.ProjectName, string.Empty);
if (string.IsNullOrEmpty(projectName))
{
throw new ConnectionFailedException("Project name is not set. Can't connect via the Locator.", ConnectionErrorReason.InvalidConfig);
}
var loginToken = CommandLineUtility.GetCommandLineValue(
commandLineArgs, RuntimeConfigNames.LoginToken, string.Empty);
var steamDeploymentTag = CommandLineUtility.GetCommandLineValue(
commandLineArgs, RuntimeConfigNames.SteamDeploymentTag, string.Empty);
var steamTicket = CommandLineUtility.GetCommandLineValue(
commandLineArgs, RuntimeConfigNames.SteamTicket, string.Empty);
LocatorCredentialsType credentialType;
if (!string.IsNullOrEmpty(loginToken))
{
credentialType = LocatorCredentialsType.LoginToken;
}
else if (!string.IsNullOrEmpty(steamDeploymentTag) && !string.IsNullOrEmpty(steamTicket))
{
credentialType = LocatorCredentialsType.Steam;
}
else
{
throw new ConnectionFailedException("Neither steam credentials nor login token is set. Can't connect via the Locator.", ConnectionErrorReason.InvalidConfig);
}
return new LocatorConfig
{
DeploymentListCallback = SelectDeploymentName,
LocatorHost = CommandLineUtility.GetCommandLineValue(
commandLineArgs, RuntimeConfigNames.LocatorHost, RuntimeConfigDefaults.LocatorHost),
LocatorParameters = new LocatorParameters
{
CredentialsType = credentialType,
LoginToken = new LoginTokenCredentials
{
Token = loginToken,
},
Steam = new SteamCredentials
{
DeploymentTag = steamDeploymentTag,
Ticket = steamTicket,
},
ProjectName = projectName,
}
};
}
protected override AlphaLocatorConfig GetAlphaLocatorConfig(string workerType)
{
var commandLineArguments = Environment.GetCommandLineArgs();
var commandLineArgs = CommandLineUtility.ParseCommandLineArgs(commandLineArguments);
return new AlphaLocatorConfig
{
LocatorHost = CommandLineUtility.GetCommandLineValue(
commandLineArgs, RuntimeConfigNames.LocatorHost, RuntimeConfigDefaults.LocatorHost),
LocatorParameters = new Alpha.LocatorParameters
{
PlayerIdentity = new Alpha.PlayerIdentityCredentials
{
PlayerIdentityToken = CommandLineUtility.GetCommandLineValue(
commandLineArgs, RuntimeConfigNames.PlayerIdentityToken, string.Empty),
LoginToken = CommandLineUtility.GetCommandLineValue(
commandLineArgs, RuntimeConfigNames.LoginToken, string.Empty)
},
},
};
}
}
}