Skip to content

Commit ae3c587

Browse files
committed
Revert double initialization fix for Speech responder (this fix broke personality change event handling)
1 parent 1b0cfd8 commit ae3c587

2 files changed

Lines changed: 43 additions & 58 deletions

File tree

SpeechResponder/ConfigurationWindow.xaml.cs

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
using System.Collections.Generic;
99
using System.Collections.Specialized;
1010
using System.ComponentModel;
11-
using System.Globalization;
1211
using System.Linq;
1312
using System.Runtime.CompilerServices;
1413
using System.Threading;
@@ -84,28 +83,27 @@ private static AvalonEdit.CottleHighlighting GetHighlighting ( IEnumerable<MetaV
8483
);
8584
}
8685

87-
public ConfigurationWindow()
86+
public ConfigurationWindow(SpeechResponder speechResponder)
8887
{
89-
var speechResponder = EDDI.Instance.ObtainResponder( Properties.SpeechResponder.ResourceManager.GetString( "name", CultureInfo.InvariantCulture ) );
9088
if (speechResponder is null) { return; }
91-
SpeechResponder = (SpeechResponder)speechResponder;
89+
this.SpeechResponder = speechResponder;
9290
customFunctionNames = ScriptResolver.GetCustomFunctions().Select( f => f.name );
93-
DataContext = speechResponder;
9491
Task.Run( GetStandardVariables );
9592

9693
InitializeComponent();
94+
DataContext = speechResponder;
9795

9896
// Set up the scripts view
99-
InitializeView( SpeechResponder.CurrentPersonality.Scripts.Values);
97+
InitializeView(speechResponder.CurrentPersonality.Scripts.Values);
10098

10199
// Set up other preferences
102-
subtitlesCheckbox.IsChecked = SpeechResponder.Configuration?.Subtitles ?? false;
103-
subtitlesOnlyCheckbox.IsChecked = SpeechResponder.Configuration?.SubtitlesOnly ?? false;
100+
subtitlesCheckbox.IsChecked = speechResponder.Configuration?.Subtitles ?? false;
101+
subtitlesOnlyCheckbox.IsChecked = speechResponder.Configuration?.SubtitlesOnly ?? false;
104102

105103
SpeechResponder.PersonalityChanged += PersonalityChanged;
106-
SpeechResponder.Personalities.CollectionChanged += PersonalitiesCollectionChanged;
104+
speechResponder.Personalities.CollectionChanged += PersonalitiesCollectionChanged;
107105

108-
CheckForScriptRecovery( SpeechResponder );
106+
CheckForScriptRecovery(speechResponder);
109107
}
110108

111109
private void CheckForScriptRecovery (SpeechResponder speechResponder)
@@ -327,27 +325,26 @@ private void deleteScript(object sender, RoutedEventArgs e)
327325
var messageBoxText = string.Format(Properties.SpeechResponder.delete_script_message, script.Name);
328326
var caption = Properties.SpeechResponder.delete_script_caption;
329327
var result = MessageBox.Show(messageBoxText, caption, MessageBoxButton.YesNo, MessageBoxImage.Warning);
330-
if ( result == MessageBoxResult.Yes )
328+
switch (result)
331329
{
332-
// Remove the script from the list
333-
SpeechResponder.CurrentPersonality.Scripts.Remove( script.Name );
330+
case MessageBoxResult.Yes:
331+
// Remove the script from the list
332+
SpeechResponder.CurrentPersonality.Scripts.Remove( script.Name );
334333

335-
// Remove any references to the removed script in the `includes` scring of other scripts
336-
SpeechResponder.CurrentPersonality.Scripts.AsParallel().ForAll( kv =>
337-
kv.Value.includes = kv.Value.includes is null
338-
? string.Empty
339-
: string.Join( "; ",
340-
kv.Value.includes.Split( ';' ).Select( s => s.Trim() )
341-
.Except( [ script.Name ] ) ) );
334+
// Remove any references to the removed script in the `includes` scring of other scripts
335+
SpeechResponder.CurrentPersonality.Scripts.AsParallel().ForAll( kv =>
336+
kv.Value.includes = kv.Value.includes is null
337+
? string.Empty
338+
: string.Join( "; ",
339+
kv.Value.includes.Split( ';' ).Select( s => s.Trim() )
340+
.Except( [ script.Name ] ) ) );
342341

343-
SpeechResponder.SavePersonality();
344-
scriptsView.Refresh();
345-
e.Handled = true;
342+
SpeechResponder.SavePersonality();
343+
scriptsView.Refresh();
344+
break;
346345
}
347-
348346
EDDI.Instance.SpeechResponderModalWait = false;
349347
}
350-
351348
private void resetScript(object sender, RoutedEventArgs e)
352349
{
353350
if (SpeechResponder?.CurrentPersonality?.Scripts is null) { return; }
@@ -359,13 +356,14 @@ private void resetScript(object sender, RoutedEventArgs e)
359356
var messageBoxText = string.Format(Properties.SpeechResponder.reset_script_message, script.Name);
360357
var caption = Properties.SpeechResponder.reset_script_button;
361358
var result = MessageBox.Show(messageBoxText, caption, MessageBoxButton.YesNo, MessageBoxImage.Warning);
362-
if ( result == MessageBoxResult.Yes )
359+
switch (result)
363360
{
364-
script.Value = script.defaultValue;
365-
SpeechResponder.CurrentPersonality.Scripts[ script.Name ] = script;
366-
SpeechResponder.SavePersonality();
367-
scriptsData.Items.Refresh();
368-
e.Handled = true;
361+
case MessageBoxResult.Yes:
362+
script.Value = script.defaultValue;
363+
SpeechResponder.CurrentPersonality.Scripts[script.Name] = script;
364+
SpeechResponder.SavePersonality();
365+
scriptsData.Items.Refresh();
366+
break;
369367
}
370368
}
371369
}
@@ -396,7 +394,7 @@ private void newScriptClicked(object sender, RoutedEventArgs e)
396394

397395
private void copyPersonalityClicked(object sender, RoutedEventArgs e)
398396
{
399-
if (SpeechResponder.Personalities is null) { return; }
397+
if (SpeechResponder?.Personalities is null) { return; }
400398
EDDI.Instance.SpeechResponderModalWait = true;
401399
var window = new CopyPersonalityWindow(SpeechResponder.Personalities)
402400
{
@@ -418,7 +416,7 @@ private void copyPersonalityClicked(object sender, RoutedEventArgs e)
418416

419417
private void deletePersonalityClicked(object sender, RoutedEventArgs e)
420418
{
421-
if (SpeechResponder.Personalities is null) { return; }
419+
if (SpeechResponder?.Personalities is null) { return; }
422420
EDDI.Instance.SpeechResponderModalWait = true;
423421
var messageBoxText = string.Format(Properties.SpeechResponder.delete_personality_message, SpeechResponder.CurrentPersonality.Name);
424422
var caption = Properties.SpeechResponder.delete_personality_caption;

SpeechResponder/SpeechResponder.cs

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,7 @@ public class SpeechResponder : IEddiResponder
2929
// The file to log speech
3030
private static readonly string LogFile = Constants.DATA_DIR + @"\speechresponder.out";
3131

32-
// Static lazy initialization ensures only one thread initializes personalities
33-
private static readonly Lazy<ObservableCollection<Personality>> PersonalitiesLazy
34-
= new(InitializePersonalities);
35-
36-
public static ObservableCollection<Personality> Personalities => PersonalitiesLazy.Value;
32+
public ObservableCollection<Personality> Personalities { get; private set; }
3733

3834
public Personality CurrentPersonality
3935
{
@@ -101,24 +97,28 @@ public string ResponderDescription()
10197
public SpeechResponder()
10298
{
10399
Configuration = ConfigService.Instance.speechResponderConfiguration;
100+
Personalities = GetPersonalities();
104101
TrySetPersonality(Configuration.Personality);
105102
}
106103

107104
#region Personalities
108105

109-
private static ObservableCollection<Personality> InitializePersonalities()
106+
private ObservableCollection<Personality> GetPersonalities()
110107
{
111-
var personalities = new ObservableCollection<Personality> { Personality.Default() };
108+
if (Personalities is not null) { return Personalities; }
109+
110+
// Initialize our collection and add our default personality
111+
Personalities = [ Personality.Default() ];
112112

113+
// Add our custom personalities
113114
foreach (var customPersonality in Personality.AllFromDirectory())
114115
{
115116
if (customPersonality != null)
116117
{
117-
personalities.Add(customPersonality);
118+
Personalities.Add(customPersonality);
118119
}
119120
}
120-
121-
return personalities;
121+
return Personalities;
122122
}
123123

124124
/// <summary>
@@ -243,20 +243,7 @@ public void Stop()
243243
public void Reload()
244244
{
245245
Configuration = ConfigService.Instance.speechResponderConfiguration;
246-
247-
// Clear and repopulate the existing personalities collection
248-
var personalities = Personalities;
249-
personalities.Clear();
250-
personalities.Add(Personality.Default());
251-
252-
foreach (var customPersonality in Personality.AllFromDirectory())
253-
{
254-
if (customPersonality != null)
255-
{
256-
personalities.Add(customPersonality);
257-
}
258-
}
259-
246+
Personalities = GetPersonalities();
260247
TrySetPersonality(Configuration.Personality);
261248
Logging.Debug($"Reloaded {ResponderName()}");
262249
}
@@ -390,7 +377,7 @@ await SpeechService.Instance.SayAsync( ship, speech,
390377

391378
public UserControl ConfigurationTabItem()
392379
{
393-
return new ConfigurationWindow();
380+
return new ConfigurationWindow(this);
394381
}
395382

396383
public Task HandleStatusAsync ( Status status )

0 commit comments

Comments
 (0)