@@ -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 . 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