Skip to content

Commit 702fe93

Browse files
committed
Merge branch 'Galnet_403' into develop
2 parents 8a49a97 + c550c53 commit 702fe93

2 files changed

Lines changed: 32 additions & 21 deletions

File tree

ChangeLog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ Full details of the variables available for each noted event, and VoiceAttack in
88
* Fixed localized default personality selection so custom personalities are upgraded using the selected EDDI UI language rather than the Windows system language. (#2801)
99
* Fixed `Scan organic` recording a bonus of 0 credits at the `Analyze` scan stage. (#2800)
1010
* Revised installer to allow installation when the VoiceAttack 1 and VoiceAttack 2 registry hives point to the same location (e.g. when VoiceAttack 2 is installed over an existing VoiceAttack 1 installation). (#2803)
11+
* Galnet Monitor
12+
* Fixed an error preventing Galnet RSS from being read correctly. (#2804) (thanks to @rodan123)
1113
* Speech Responder
1214
* Fixed vertical whitespace characters being removed from speechresponder.out. (#2797)
1315
* Custom Functions

GalnetMonitor/GalnetMonitor.cs

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
using System.Threading.Tasks;
2020
using System.Windows.Controls;
2121
using System.Xml;
22+
using System.Net.Http;
2223
using Utilities;
2324

2425
namespace EddiGalnetMonitor
@@ -30,14 +31,15 @@ namespace EddiGalnetMonitor
3031
[UsedImplicitly]
3132
public class GalnetMonitor : IEddiMonitor
3233
{
33-
private static Dictionary<string, string> locales = [ ];
34+
private static Dictionary<string, string> locales;
3435
private static string locale;
3536
private GalnetConfiguration configuration = ConfigService.Instance.galnetConfiguration;
3637
private static readonly ResourceManager resourceManager = Properties.GalnetMonitor.ResourceManager;
3738

3839
private CancellationTokenSource cancellationTokenSource;
3940
private bool running;
4041
private DateTime journalTimeStamp;
42+
private static HttpClient httpClient;
4143

4244
/// <summary>
4345
/// The name of the monitor; shows up in EDDI's configuration window
@@ -77,7 +79,14 @@ public void Start ()
7779
{
7880
cancellationTokenSource = new CancellationTokenSource();
7981
running = true;
80-
locales = GetGalnetLocales();
82+
locales ??= GetGalnetLocales();
83+
if ( httpClient is null )
84+
{
85+
httpClient = new HttpClient();
86+
// Add a User-Agent header (required by many servers)
87+
httpClient.DefaultRequestHeaders.Add( "User-Agent", $"EDDI/{Constants.EDDI_VERSION}" );
88+
}
89+
8190
MonitorAsync().GetAwaiter().GetResult();
8291
}
8392

@@ -118,7 +127,7 @@ private async Task MonitorAsync ()
118127
{
119128
if ( configuration.galnetAlwaysOn )
120129
{
121-
FetchGalnet();
130+
await FetchGalnetAsync().ConfigureAwait(false);
122131
await Task.Delay( passiveIntervalMilliSecs , cancellationTokenSource.Token ).ConfigureAwait(false);
123132
}
124133
else
@@ -133,7 +142,7 @@ private async Task MonitorAsync ()
133142
await Task.Delay( inGameOnlyStartDelayMilliSecs, cancellationTokenSource.Token ).ConfigureAwait( false );
134143
}
135144

136-
FetchGalnet();
145+
await FetchGalnetAsync().ConfigureAwait(false);
137146
await Task.Delay( activeIntervalMilliSecs , cancellationTokenSource.Token ).ConfigureAwait(false);
138147
}
139148
else
@@ -150,7 +159,7 @@ private async Task MonitorAsync ()
150159
}
151160
}
152161

153-
private void FetchGalnet ()
162+
private async Task FetchGalnetAsync ()
154163
{
155164
var newsItems = new List<News>();
156165
string firstUid = null;
@@ -159,7 +168,7 @@ private void FetchGalnet ()
159168
var url = GetGalnetResource("sourceURL");
160169

161170
Logging.Debug( "Fetching Galnet articles from " + url );
162-
var items = GetFeedItems( url );
171+
var items = await GetFeedItemsAsync( url ).ConfigureAwait(false);
163172
if ( items != null )
164173
{
165174
try
@@ -218,34 +227,34 @@ private void FetchGalnet ()
218227
}
219228
}
220229
}
221-
private static List<FeedItem> GetFeedItems ( string url, bool fromAltUrl = false )
230+
231+
private static async Task<List<FeedItem>> GetFeedItemsAsync ( string url, bool fromAltUrl = false )
222232
{
223233
var items = new List<FeedItem>();
224234
try
225235
{
226-
using ( var reader = XmlReader.Create( url ) )
236+
await using ( var stream = await httpClient.GetStreamAsync( url ).ConfigureAwait( false ) )
227237
{
228-
var feed = SyndicationFeed.Load( reader );
229-
var normalizer = new GalnetFeedItemNormalizer( fromAltUrl );
230-
foreach ( var syndicationItem in feed.Items )
238+
using ( var reader = XmlReader.Create( stream ) )
231239
{
232-
var feedItem = normalizer.Normalize( feed, syndicationItem );
233-
if ( feedItem != null )
240+
var feed = SyndicationFeed.Load( reader );
241+
var normalizer = new GalnetFeedItemNormalizer( fromAltUrl );
242+
foreach ( var syndicationItem in feed.Items )
234243
{
235-
items.Add( feedItem );
244+
var feedItem = normalizer.Normalize( feed, syndicationItem );
245+
if ( feedItem != null )
246+
{
247+
items.Add( feedItem );
248+
}
236249
}
237250
}
238251
}
239252
}
240-
catch ( WebException wex )
241-
{
242-
Logging.Warn( $"The remote server at {url} has rejected the request: ", wex );
243-
}
244-
catch ( XmlException xex )
253+
catch ( Exception ex ) when ( ex is HttpRequestException or WebException )
245254
{
246-
Logging.Error( "Exception attempting to obtain galnet feed: ", xex );
255+
Logging.Warn( $"The remote server at {url} has rejected the request: ", ex );
247256
}
248-
catch ( FileNotFoundException ex )
257+
catch ( Exception ex ) when ( ex is XmlException or FileNotFoundException )
249258
{
250259
Logging.Error( "Exception attempting to obtain galnet feed: ", ex );
251260
}

0 commit comments

Comments
 (0)