Skip to content

Commit 9379f85

Browse files
committed
Tidy up variable initialization
1 parent 9a8e7e3 commit 9379f85

3 files changed

Lines changed: 58 additions & 13 deletions

File tree

Tests/VoiceAttackPluginTests.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,5 +614,29 @@ public async Task RuntimeReceiver_PascalCaseRuntimeMetadata_AppliesCommandAction
614614
Assert.AreEqual( "Ship", mockVAProxy.GetText( "Status vehicle" ) );
615615
Assert.IsFalse( mockVAProxy.GetBoolean( "Status being interdicted" ) );
616616
}
617+
618+
[TestMethod]
619+
public void TestInitializeStandardValues_WithoutData_CompletesSuccessfully()
620+
{
621+
// Arrange: Clear cache to simulate fresh initialization
622+
VoiceAttackVariables.ClearDispatchCache();
623+
624+
// Act: Call initializeStandardValues when EDDI data may not be fully loaded
625+
// (simulates the early startup scenario where monitors haven't populated data yet)
626+
try
627+
{
628+
VoiceAttackVariables.initializeStandardValues();
629+
}
630+
catch (Exception ex)
631+
{
632+
Assert.Fail($"initializeStandardValues should not throw exceptions during early startup. Exception: {ex.Message}");
633+
}
634+
635+
// Assert: Verify that initialization completed successfully
636+
// EDDI version should always be set (independent of EDDI data availability)
637+
var eddiVersion = mockVAProxy.GetText("EDDI version");
638+
Assert.IsFalse(string.IsNullOrEmpty(eddiVersion),
639+
"EDDI version should be set after initialization");
640+
}
617641
}
618642
}

VoiceAttackResponder/VoiceAttackInvokationHandler.cs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,6 @@ private static void InvokeInaraSystem ()
366366
var systemUri =
367367
$"https://inara.cz/elite/starsystem/?search={EDDI.Instance.CurrentStarSystem.systemAddress}";
368368
OpenOrStoreURI( systemUri );
369-
VoiceAttackVariables.setStatus( "Operational" );
370369
}
371370
catch ( Exception e )
372371
{
@@ -396,7 +395,6 @@ private static void InvokeInaraStation ()
396395

397396
var stationUri = $"https://inara.cz/elite/station/?search={EDDI.Instance.CurrentStation.marketId}";
398397
OpenOrStoreURI( stationUri );
399-
VoiceAttackVariables.setStatus( "Operational" );
400398
}
401399
catch ( Exception e )
402400
{
@@ -420,7 +418,6 @@ private static void InvokeInaraFleetCarrier ()
420418
var carrierUri =
421419
$"https://inara.cz/elite/cmdr-fleetcarrier/?search={EDDI.Instance.FleetCarrier.callsign}";
422420
OpenOrStoreURI( carrierUri );
423-
VoiceAttackVariables.setStatus( "Operational" );
424421
}
425422
catch ( Exception e )
426423
{
@@ -444,7 +441,6 @@ private static void InvokeInaraProfile ()
444441

445442
var cmdrUri = $"https://inara.cz/elite/cmdr/{inaraID}/";
446443
OpenOrStoreURI( cmdrUri );
447-
VoiceAttackVariables.setStatus( "Operational" );
448444
}
449445
catch ( Exception e )
450446
{
@@ -467,7 +463,6 @@ private static void InvokeCoriolis ( bool beta = false )
467463

468464
var shipUri = EDDI.Instance.CurrentShip.CoriolisUri( beta );
469465
OpenOrStoreURI( shipUri );
470-
VoiceAttackVariables.setStatus( "Operational" );
471466
}
472467
catch ( Exception e )
473468
{
@@ -490,7 +485,6 @@ private static void InvokeEDShipyard ()
490485

491486
var shipUri = EDDI.Instance.CurrentShip.EDShipyardUri();
492487
OpenOrStoreURI( shipUri );
493-
VoiceAttackVariables.setStatus( "Operational" );
494488
}
495489
catch ( Exception e )
496490
{
@@ -1032,5 +1026,4 @@ private static async Task InvokeRouteDetailsAsync ()
10321026
}
10331027
}
10341028
}
1035-
}
1036-
1029+
}

VoiceAttackResponder/VoiceAttackVariables.cs

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,17 +109,49 @@ protected internal static void initializeStandardValues()
109109
{
110110
RunWithRuntimeActionBatch( () =>
111111
{
112+
setStatus( "Initializing" );
113+
112114
foreach (var standardValue in StandardValues)
113115
{
114116
try
115117
{
116-
LockManager.GetLock(standardValue.Key, standardValue.Value);
118+
// Wrap the standard value action to only execute if the underlying EDDI property has data
119+
// This prevents caching of null/empty values during early startup, allowing proper
120+
// initialization when monitors populate their data and PropertyChanged events fire
121+
LockManager.GetLock(standardValue.Key, () =>
122+
{
123+
// Determine if the property has data before executing the action
124+
var hasData = standardValue.Key switch
125+
{
126+
var k when k.Contains(nameof(EDDI.Instance.CurrentStarSystem)) => EDDI.Instance.CurrentStarSystem != null,
127+
var k when k.Contains(nameof(EDDI.Instance.LastStarSystem)) => EDDI.Instance.LastStarSystem != null,
128+
var k when k.Contains(nameof(EDDI.Instance.NextStarSystem)) => EDDI.Instance.NextStarSystem != null,
129+
var k when k.Contains(nameof(EDDI.Instance.DestinationStarSystem)) => EDDI.Instance.DestinationStarSystem != null,
130+
var k when k.Contains(nameof(EDDI.Instance.CurrentStellarBody)) => EDDI.Instance.CurrentStellarBody != null,
131+
var k when k.Contains(nameof(EDDI.Instance.CurrentStation)) => EDDI.Instance.CurrentStation != null,
132+
var k when k.Contains(nameof(EDDI.Instance.CurrentShip)) => EDDI.Instance.CurrentShip != null,
133+
_ => true, // Other properties are assumed to always have data
134+
};
135+
136+
if (hasData)
137+
{
138+
standardValue.Value();
139+
}
140+
});
117141
}
118142
catch (Exception ex)
119143
{
120144
Logging.Error($"Failed to initialize {standardValue.Key}", ex);
121145
}
122146
}
147+
148+
// Initialize shipyard information
149+
var shipConfig = ConfigService.Instance.shipMonitorConfiguration;
150+
if ( shipConfig?.shipyard != null )
151+
{
152+
setShipyardValues( shipConfig.shipyard.ToList() );
153+
}
154+
123155
RuntimeSetText("EDDI version", Constants.EDDI_VERSION.ToString());
124156
} );
125157
}
@@ -327,8 +359,6 @@ protected static void setCommanderValues(Commander cmdr)
327359

328360
// Backwards-compatibility with 1.x
329361
RuntimeSetText("System rank", cmdr?.title);
330-
331-
setStatus( "Operational");
332362
}
333363
catch (Exception e)
334364
{
@@ -422,8 +452,6 @@ public static void setShipValues(Ship ship, string prefix)
422452
RuntimeSetText( $"{prefix} station", ship.station);
423453
RuntimeSetDecimal( $"{prefix} distance", ship.distance);
424454
}
425-
426-
setStatus( "Operational");
427455
}
428456
catch (Exception e)
429457
{

0 commit comments

Comments
 (0)