Skip to content

Commit d8447ad

Browse files
authored
Merge pull request #1095 from PlayEveryWare/fix/client-credential-auto-selection
fix: Client Credential auto-selection
2 parents 67d18ae + 9df2dde commit d8447ad

File tree

4 files changed

+168
-9
lines changed

4 files changed

+168
-9
lines changed

Assets/Plugins/Source/Editor/ConfigEditors/IPlatformConfigEditor.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ public interface IPlatformConfigEditor : IConfigEditor
3636
/// </returns>
3737
bool IsPlatformAvailable();
3838

39+
void SetClientCredentials(EOSClientCredentials credentials);
40+
3941
void SetDeployment(Deployment deployment);
4042

4143
PlatformManager.Platform GetPlatform();

Assets/Plugins/Source/Editor/ConfigEditors/PlatformConfigEditor.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ public void SetDeployment(Deployment deployment)
5959
config.deployment = deployment;
6060
}
6161

62+
public void SetClientCredentials(EOSClientCredentials credentials)
63+
{
64+
config.clientCredentials = credentials;
65+
}
66+
6267
public PlatformManager.Platform GetPlatform()
6368
{
6469
return config.Platform;

Assets/Plugins/Source/Editor/EditorWindows/EOSSettingsWindow.cs

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,16 +96,56 @@ public static void ShowWindow()
9696
protected override void OnEnable()
9797
{
9898
base.OnEnable();
99-
ProductConfig.PlatformConfigsDeploymentUpdatedEvent += ReloadDeploymentSettingsForPlatformConfigEditors;
99+
ProductConfig.DeploymentsUpdatedEvent += ReloadDeploymentSettingsForPlatformConfigEditors;
100+
ProductConfig.ClientCredentialsUpdatedEvent += ReloadClientCredentialsForPlatformConfigEditors;
100101
}
101102

102103
protected override void OnDestroy()
103104
{
104-
ProductConfig.PlatformConfigsDeploymentUpdatedEvent -= ReloadDeploymentSettingsForPlatformConfigEditors;
105+
ProductConfig.DeploymentsUpdatedEvent -= ReloadDeploymentSettingsForPlatformConfigEditors;
106+
ProductConfig.ClientCredentialsUpdatedEvent -= ReloadClientCredentialsForPlatformConfigEditors;
105107
base.OnDestroy();
106108
}
107109

108-
private void ReloadDeploymentSettingsForPlatformConfigEditors(object sender, ProductConfig.PlatformConfigsDeploymentUpdatedEventArgs e)
110+
// TODO: Refactor to reduce massive overlap between this function and
111+
// the older ReloadDeploymentSettingsForPlatformConfigEditors
112+
// function.
113+
// The Observable pattern would be appropriate - but
114+
// such a change would constitute a not insignificant change, and
115+
// should be avoided until there is a time to properly review it.
116+
private void ReloadClientCredentialsForPlatformConfigEditors(object sender,
117+
ProductConfig.PlatformConfigsUpdatedEventArgs e)
118+
{
119+
// For each of the platform config editors
120+
foreach (IPlatformConfigEditor platformConfigEditor in _platformConfigEditors)
121+
{
122+
// If the platform config was not one of the ones updated, then skip it.
123+
if (!e.PlatformConfigsUpdated.Contains(platformConfigEditor.GetPlatform()))
124+
{
125+
continue;
126+
}
127+
128+
// If the platform config could not be read from disk
129+
if (!PlatformManager.TryGetConfig(platformConfigEditor.GetPlatform(),
130+
out PlatformConfig platformConfigFromDisk))
131+
{
132+
// TODO: Log warning?
133+
continue;
134+
}
135+
136+
// Update the client credentials for the cached instance of the
137+
// config within the PlatformConfigEditor.
138+
platformConfigEditor.SetClientCredentials(platformConfigFromDisk.clientCredentials);
139+
}
140+
}
141+
142+
// TODO: Refactor to reduce massive overlap between this function and
143+
// the more recently introduced
144+
// ReloadClientCredentialsForPlatformConfigEditors function.
145+
// The Observable pattern would be appropriate - but
146+
// such a change would constitute a not insignificant change, and
147+
// should be avoided until there is a time to properly review it.
148+
private void ReloadDeploymentSettingsForPlatformConfigEditors(object sender, ProductConfig.PlatformConfigsUpdatedEventArgs e)
109149
{
110150
// For each of the platform config editors
111151
foreach (IPlatformConfigEditor platformConfigEditor in _platformConfigEditors)

com.playeveryware.eos/Runtime/Core/Config/ProductConfig.cs

Lines changed: 118 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,24 +110,37 @@ public class ProductConfig : Config
110110
[JsonIgnore]
111111
private bool _deploymentDefinedWhenLoaded;
112112

113+
/// <summary>
114+
/// This field member is used to determine when client credentials are
115+
/// first defined by the user. This determination is used to trigger the
116+
/// process of setting the default deployment for the platforms.
117+
/// </summary>
118+
[JsonIgnore]
119+
private bool _clientCredentialsDefinedWhenLoaded;
120+
113121
/// <summary>
114122
/// Used to store information about what platform configs have been
115123
/// updated.
124+
/// TODO: Implement via the Observable pattern instead.
116125
/// </summary>
117-
public sealed class PlatformConfigsDeploymentUpdatedEventArgs : EventArgs
126+
public sealed class PlatformConfigsUpdatedEventArgs : EventArgs
118127
{
119128
/// <summary>
120129
/// A list of all the platform configs that have been updated.
121130
/// </summary>
122131
public readonly IEnumerable<PlatformManager.Platform> PlatformConfigsUpdated;
123132

124-
public PlatformConfigsDeploymentUpdatedEventArgs(IEnumerable<PlatformManager.Platform> platformConfigsUpdated)
133+
public PlatformConfigsUpdatedEventArgs(
134+
IEnumerable<PlatformManager.Platform> platformConfigsUpdated)
125135
{
126136
PlatformConfigsUpdated = platformConfigsUpdated;
127137
}
128138
}
129139

130-
public static event EventHandler<PlatformConfigsDeploymentUpdatedEventArgs> PlatformConfigsDeploymentUpdatedEvent;
140+
public static event EventHandler<PlatformConfigsUpdatedEventArgs> DeploymentsUpdatedEvent;
141+
142+
public static event EventHandler<PlatformConfigsUpdatedEventArgs>
143+
ClientCredentialsUpdatedEvent;
131144

132145
static ProductConfig()
133146
{
@@ -147,6 +160,27 @@ protected override void OnReadCompleted()
147160
// out parameter is discarded because it is not needed at this
148161
// juncture.
149162
_deploymentDefinedWhenLoaded = Environments.TryGetFirstDefinedNamedDeployment(out _);
163+
164+
// This tracks whether there is a single client credential
165+
// completely defined when the product config is loaded. The out
166+
// parameter is discarded because it is not needed at this juncture.
167+
_clientCredentialsDefinedWhenLoaded = TryGetFirstCompleteNamedClientCredentials(out _);
168+
}
169+
170+
public bool TryGetFirstCompleteNamedClientCredentials(out Named<EOSClientCredentials> credentials)
171+
{
172+
credentials = null;
173+
174+
foreach (var clientCredentials in Clients)
175+
{
176+
if (clientCredentials.Value.IsComplete)
177+
{
178+
credentials = clientCredentials;
179+
break;
180+
}
181+
}
182+
183+
return (credentials != null);
150184
}
151185

152186
// This compile conditional is here because the OnWriteCompleted method
@@ -165,15 +199,21 @@ protected override void BeforeWrite()
165199
}
166200
}
167201

168-
protected override void OnWriteCompleted()
202+
// TODO: Refactor to reduce massive overlap between this function and
203+
// the more recently introduced
204+
// UpdatePlatformConfigClientCredentials function.
205+
// The Observable pattern would be appropriate - but such a change
206+
// would constitute a not insignificant refactor and should be
207+
// avoided until there is a time to properly review it.
208+
private void UpdatePlatformConfigDeployments()
169209
{
170210
bool definedDeploymentExists =
171211
Environments.TryGetFirstDefinedNamedDeployment(out Named<Deployment> deploymentToSetPlatformsTo);
172212

173213
// If when the config was last read there was a deployment defined,
174214
// or there is not now one defined - then there is no need to try
175215
// and set the deployment values for each platform config.
176-
if (_deploymentDefinedWhenLoaded ||
216+
if (_deploymentDefinedWhenLoaded ||
177217
!definedDeploymentExists)
178218
{
179219
return;
@@ -217,8 +257,80 @@ protected override void OnWriteCompleted()
217257
// the event that indicates as much
218258
if (platformConfigsUpdated.Count > 0)
219259
{
220-
PlatformConfigsDeploymentUpdatedEvent?.Invoke(this, new PlatformConfigsDeploymentUpdatedEventArgs(platformConfigsUpdated));
260+
DeploymentsUpdatedEvent?.Invoke(this, new PlatformConfigsUpdatedEventArgs(platformConfigsUpdated));
261+
}
262+
}
263+
264+
// TODO: Refactor to reduce massive overlap between this function and
265+
// the older UpdatePlatformConfigDeployments function.
266+
// The Observable pattern would be appropriate - but such a change
267+
// would constitute a not insignificant refactor and should be
268+
// avoided until there is a time to properly review it.
269+
private void UpdatePlatformConfigClientCredentials()
270+
{
271+
bool completeClientCredentialsExist =
272+
TryGetFirstCompleteNamedClientCredentials(out Named<EOSClientCredentials> credentialsToSetPlatformsTo);
273+
274+
// If when the config was last read there was a set of client
275+
// credentials already defined, or there is not now one defined -
276+
// then there is no need to try and set the client credential values
277+
// for each platform config.
278+
if (_deploymentDefinedWhenLoaded ||
279+
!completeClientCredentialsExist)
280+
{
281+
return;
221282
}
283+
284+
List<PlatformManager.Platform> platformConfigsUpdated = new();
285+
286+
// For each platform for which configuration can be done
287+
foreach (var platform in PlatformManager.ConfigurablePlatforms)
288+
{
289+
// If the PlatformConfig could not be retrieved, continue to the
290+
// next.
291+
if (!PlatformManager.TryGetConfig(platform, out PlatformConfig config))
292+
{
293+
continue;
294+
}
295+
296+
// If the config already has a completely defined set of client
297+
// credentials, then do not override, and move to the next
298+
// platform config.
299+
if (config.clientCredentials != null && config.clientCredentials.IsComplete)
300+
{
301+
continue;
302+
}
303+
304+
// Add to the list of platform configs that have been updated
305+
platformConfigsUpdated.Add(platform);
306+
307+
// Set the client credentials.
308+
config.clientCredentials = credentialsToSetPlatformsTo.Value;
309+
310+
// Tell the user
311+
Debug.Log($"Client credentials for platform " +
312+
$"\"{config.Platform}\" has been defaulted to " +
313+
$"{credentialsToSetPlatformsTo}.");
314+
315+
// Save the config
316+
config.Write();
317+
}
318+
319+
// If at least one platform config was updated as a result, trigger
320+
// the event that indicates as much
321+
if (platformConfigsUpdated.Count > 0)
322+
{
323+
ClientCredentialsUpdatedEvent?.Invoke(this, new PlatformConfigsUpdatedEventArgs(platformConfigsUpdated));
324+
}
325+
}
326+
327+
protected override void OnWriteCompleted()
328+
{
329+
// Update the platform config deployments if needed.
330+
UpdatePlatformConfigDeployments();
331+
332+
// Update the platform config client credentials if needed.
333+
UpdatePlatformConfigClientCredentials();
222334
}
223335

224336
#endif

0 commit comments

Comments
 (0)