Skip to content

Commit c617b2c

Browse files
authored
Merge pull request #162 from AlamoEngine-Tools/develop
Improve Logging
2 parents 859a726 + 980646b commit c617b2c

19 files changed

Lines changed: 65 additions & 82 deletions

File tree

src/AET.SteamAbstraction/Library/SteamLibrary.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public SteamLibrary(IDirectoryInfo libraryLocation, IServiceProvider serviceProv
5555
public IEnumerable<SteamAppManifest> GetApps()
5656
{
5757
if (!SteamAppsLocation.Exists)
58-
return Array.Empty<SteamAppManifest>();
58+
return [];
5959
var apps = new HashSet<SteamAppManifest>();
6060
foreach (var manifestFile in SteamAppsLocation.EnumerateFiles("*.acf", SearchOption.TopDirectoryOnly))
6161
{

src/AET.SteamAbstraction/Library/SteamLibraryFinder.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ internal sealed class SteamLibraryFinder(IServiceProvider serviceProvider) : ISt
2323

2424
public IEnumerable<ISteamLibrary> FindLibraries(IDirectoryInfo steamInstallDir)
2525
{
26-
_logger?.LogTrace("Searching for Steam libraries on system");
26+
_logger?.LogTrace("Searching for Steam libraries on system...");
2727
var libraryLocationsFile = GetLibraryLocationsFile(steamInstallDir);
2828

2929
if (!libraryLocationsFile.Exists)
3030
{
31-
_logger?.LogWarning("Config file with Steam libraries not found. No Steam libraries are created.");
32-
return Array.Empty<ISteamLibrary>();
31+
_logger?.LogTrace("Config file that should contain Steam libraries information is not found.");
32+
return [];
3333
}
3434

3535
var libraryLocations = SteamVdfReader.ReadLibraryLocationsFromConfig(libraryLocationsFile);
@@ -75,8 +75,7 @@ private bool TryCreateLibraryFromLocation(IDirectoryInfo libraryLocation, bool i
7575
var libraryVdf = _fileSystem.Path.Combine(libraryLocation.FullName, libraryVdfSubPath, libraryVdfName);
7676
if (!_fileSystem.File.Exists(libraryVdf))
7777
{
78-
_logger?.LogTrace(
79-
$"Steam library VDF file '{libraryVdf}' was not found. Library not created.");
78+
_logger?.LogTrace($"Steam library VDF file '{libraryVdf}' was not found. Library not created.");
8079
return false;
8180
}
8281

src/PG.StarWarsGame.Infrastructure.Clients.Steam/SteamPetroglyphStarWarsGameDetector.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ protected override GameLocationData FindGameLocation(GameType gameType)
6666
Debug.Assert(registry.Type == gameType);
6767
if (registry.Version is null)
6868
{
69-
Logger?.LogDebug("Registry-Key found, but games are not initialized.");
69+
Logger?.LogTrace("Registry-Key found, but games are not initialized.");
7070
return GameLocationData.RequiresInitialization;
7171
}
7272

src/PG.StarWarsGame.Infrastructure/Clients/Processes/GameProcessLauncher.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ public IGameProcess StartGameProcess(IFileInfo executable, GameProcessInfo proce
1818
{
1919
var arguments = ArgumentCommandLineBuilder.BuildCommandLine(processInfo.Arguments);
2020

21-
_logger?.LogInformation($"Starting game '{processInfo.Game}' in '{processInfo.BuildType}' configuration and with launch arguments '{arguments}'");
21+
_logger?.LogDebug(
22+
$"Starting game '{processInfo.Game}' with '{processInfo.BuildType}' configuration and with launch arguments '{arguments}'");
2223

2324
var processStartInfo = new ProcessStartInfo(executable.FullName)
2425
{

src/PG.StarWarsGame.Infrastructure/PG.StarWarsGame.Infrastructure.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
3636
</PackageReference>
3737
<PackageReference Include="semver" Version="3.0.0" />
38-
<PackageReference Include="HtmlAgilityPack" Version="1.11.72" />
38+
<PackageReference Include="HtmlAgilityPack" Version="1.11.74" />
3939
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="9.0.2" />
4040
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.2" />
4141
<PackageReference Include="QuikGraph" Version="2.5.0" PrivateAssets="compile" />

src/PG.StarWarsGame.Infrastructure/Services/Detection/Games/CompositeGameDetector.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Linq;
43
using Microsoft.Extensions.DependencyInjection;
54
using Microsoft.Extensions.Logging;
65
using PG.StarWarsGame.Infrastructure.Games;
@@ -38,7 +37,7 @@ public CompositeGameDetector(IList<IGameDetector> sortedDetectors, IServiceProvi
3837
if (serviceProvider == null)
3938
throw new ArgumentNullException(nameof(serviceProvider));
4039
ThrowHelper.ThrowIfCollectionNullOrEmptyOrContainsNull(sortedDetectors);
41-
SortedDetectors = sortedDetectors.ToList();
40+
SortedDetectors = [.. sortedDetectors];
4241
_logger = serviceProvider.GetService<ILoggerFactory>()?.CreateLogger(GetType());
4342
_disposeDetectors = disposeDetectors;
4443
}
@@ -57,19 +56,21 @@ public GameDetectionResult Detect(GameType gameType, params ICollection<GamePlat
5756
GameDetectionResult? lastResult = null;
5857
foreach (var detector in SortedDetectors)
5958
{
60-
_logger?.LogDebug($"Searching for game {gameType} with detector: {detector}");
59+
_logger?.LogTrace($"Searching for game '{gameType}' with detector: '{detector}'");
6160
detector.InitializationRequested += PassThroughInitializationRequest;
6261

6362
try
6463
{
6564
var result = detector.Detect(gameType, platforms);
65+
// ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
66+
// because this is public API, and we cannot guarantee that the result is not null
6667
if (result is not null && result.Installed)
6768
return result;
6869
lastResult = result;
6970
}
7071
catch (Exception e)
7172
{
72-
_logger?.LogTrace($"Failed detecting game using detector {detector}. {e}");
73+
_logger?.LogDebug($"Failed detecting game using detector {detector}. {e}");
7374
errors.Add(e);
7475

7576
if (detector.Equals(SortedDetectors[^1]))
@@ -108,7 +109,7 @@ public bool TryDetect(GameType gameType, ICollection<GamePlatform> platforms, ou
108109
}
109110
catch (Exception e)
110111
{
111-
_logger?.LogWarning(e, "Unable to find any games, due to error in detection.");
112+
_logger?.LogDebug(e, "Unable to find any games, due to error in detection.");
112113
result = GameDetectionResult.NotInstalled(gameType);
113114
return false;
114115
}

src/PG.StarWarsGame.Infrastructure/Services/Detection/Games/DirectoryGameDetector.cs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,19 @@ namespace PG.StarWarsGame.Infrastructure.Services.Detection;
1111
/// <remarks>
1212
/// This detector does not support game initialization requests.
1313
/// </remarks>
14-
public sealed class DirectoryGameDetector : GameDetectorBase
14+
/// <remarks>
15+
/// Creates a new instance of the <see cref="DirectoryGameDetector"/> class.
16+
/// </remarks>
17+
/// <param name="directory">The directory to search for an installation.</param>
18+
/// <param name="serviceProvider">The service provider.</param>
19+
public sealed class DirectoryGameDetector(IDirectoryInfo directory, IServiceProvider serviceProvider) : GameDetectorBase(serviceProvider, false)
1520
{
16-
private readonly IDirectoryInfo _directory;
17-
18-
/// <summary>
19-
/// Creates a new instance of the <see cref="DirectoryGameDetector"/> class.
20-
/// </summary>
21-
/// <param name="directory">The directory to search for an installation.</param>
22-
/// <param name="serviceProvider">The service provider.</param>
23-
public DirectoryGameDetector(IDirectoryInfo directory, IServiceProvider serviceProvider) : base(serviceProvider, false)
24-
{
25-
_directory = directory ?? throw new ArgumentNullException(nameof(directory));
26-
}
21+
private readonly IDirectoryInfo _directory = directory ?? throw new ArgumentNullException(nameof(directory));
2722

2823
/// <inheritdoc/>
2924
protected override GameLocationData FindGameLocation(GameType gameType)
3025
{
31-
Logger?.LogDebug($"Searching for game {gameType} at directory: {_directory}");
26+
Logger?.LogTrace($"Searching for game {gameType} at directory: {_directory}");
3227
return !MinimumGameFilesExist(gameType, _directory) ? GameLocationData.NotInstalled : new GameLocationData(_directory);
3328
}
3429
}

src/PG.StarWarsGame.Infrastructure/Services/Detection/Games/GameDetectorBase.cs

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using System.Collections.Generic;
33
using System.Diagnostics.CodeAnalysis;
44
using System.IO.Abstractions;
5-
using System.Linq;
65
using Microsoft.Extensions.DependencyInjection;
76
using Microsoft.Extensions.Logging;
87
using PG.StarWarsGame.Infrastructure.Games;
@@ -62,7 +61,7 @@ public bool TryDetect(GameType gameType, ICollection<GamePlatform> platforms, ou
6261
}
6362
catch (Exception e)
6463
{
65-
Logger?.LogWarning(e, "Unable to find any games, due to error in detection.");
64+
Logger?.LogDebug(e, "Unable to find any games, due to error in detection.");
6665
result = GameDetectionResult.NotInstalled(gameType);
6766
return false;
6867
}
@@ -77,7 +76,7 @@ public GameDetectionResult Detect(GameType gameType, params ICollection<GamePlat
7776

7877
if (!locationData.IsInstalled)
7978
{
80-
Logger?.LogInformation($"Unable to find an installed game of type {gameType}.");
79+
Logger?.LogTrace($"Unable to find an installed game of type {gameType}.");
8180
return GameDetectionResult.NotInstalled(gameType);
8281
}
8382

@@ -92,20 +91,20 @@ public GameDetectionResult Detect(GameType gameType, params ICollection<GamePlat
9291
// the detector returned a false result.
9392
if (!GameExeExists(location, gameType) || !DataAndMegaFilesXmlExists(location))
9493
{
95-
Logger?.LogDebug($"Unable to find the game's executable or megafiles.xml at the given location: {location.FullName}");
94+
Logger?.LogTrace($"Unable to find the game's executable or megafiles.xml at the given location: {location.FullName}");
9695
return GameDetectionResult.NotInstalled(gameType);
9796
}
9897

9998
if (!MatchesOptionsPlatform(platforms, actualPlatform))
10099
{
101100
var wrongGameFound = GameDetectionResult.NotInstalled(gameType);
102-
Logger?.LogInformation($"Game detected at location: {wrongGameFound.GameLocation?.FullName} " +
101+
Logger?.LogTrace($"Game detected at location: {wrongGameFound.GameLocation?.FullName} " +
103102
$"but Platform {actualPlatform} was not requested.");
104103
return wrongGameFound;
105104
}
106105

107106
var detectedResult = GameDetectionResult.FromInstalled(new GameIdentity(gameType, actualPlatform), location);
108-
Logger?.LogInformation($"Game detected: {detectedResult.GameIdentity} at location: {location.FullName}");
107+
Logger?.LogDebug($"Game detected: {detectedResult.GameIdentity} at location: '{location.FullName}'");
109108
return detectedResult;
110109
}
111110

@@ -167,7 +166,7 @@ private bool HandleInitialization(GameType gameType, ref GameLocationData locati
167166
if (!locationData.InitializationRequired)
168167
return true;
169168

170-
Logger?.LogDebug($"It appears that the game '{locationData.ToString()}' exists but it is not initialized. Game type '{gameType}'.");
169+
Logger?.LogDebug($"It appears that the game '{locationData}' exists but it is not initialized. Game type '{gameType}'.");
171170
if (!_tryHandleInitialization)
172171
return false;
173172

@@ -189,17 +188,21 @@ private bool RequestInitialization(GameType gameType)
189188
return request.Handled;
190189
}
191190

192-
private static IList<GamePlatform> NormalizePlatforms(ICollection<GamePlatform> platforms)
191+
private static HashSet<GamePlatform> NormalizePlatforms(ICollection<GamePlatform> platforms)
193192
{
194193
if (platforms.Count == 0 || platforms.Contains(GamePlatform.Undefined))
195194
return [GamePlatform.Undefined];
196-
return platforms.Distinct().ToList();
195+
return [..platforms];
197196
}
198197

199198
/// <summary>
200199
/// Represents location and initialization state of a game.
201200
/// </summary>
202-
public readonly struct GameLocationData
201+
/// <remarks>
202+
/// Initializes a new instance of the <see cref="GameLocationData"/> struct of the specified game location.
203+
/// </remarks>
204+
/// <param name="location">The detected game location or <see langword="null"/> if no game was detected.</param>
205+
public readonly struct GameLocationData(IDirectoryInfo? location)
203206
{
204207
/// <summary>
205208
/// Gets a <see cref="GameLocationData"/> representing a not installed location.
@@ -211,19 +214,10 @@ public readonly struct GameLocationData
211214
/// </summary>
212215
public static readonly GameLocationData RequiresInitialization = new() { InitializationRequired = true };
213216

214-
/// <summary>
215-
/// Initializes a new instance of the <see cref="GameLocationData"/> struct of the specified game location.
216-
/// </summary>
217-
/// <param name="location">The detected game location or <see langword="null"/> if no game was detected.</param>
218-
public GameLocationData(IDirectoryInfo? location)
219-
{
220-
Location = location;
221-
}
222-
223217
/// <summary>
224218
/// Nullable location entry.
225219
/// </summary>
226-
public IDirectoryInfo? Location { get; }
220+
public IDirectoryInfo? Location { get; } = location;
227221

228222
/// <summary>
229223
/// Indicates whether an initialization is required.

src/PG.StarWarsGame.Infrastructure/Services/Detection/Games/Platform/GamePlatformIdentifier.cs

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,14 @@ namespace PG.StarWarsGame.Infrastructure.Services.Detection.Platform;
1010
/// <summary>
1111
/// Default implementation of the <see cref="IGamePlatformIdentifier"/> service.
1212
/// </summary>
13-
internal sealed class GamePlatformIdentifier : IGamePlatformIdentifier
13+
/// <remarks>
14+
/// Creates a new instance.
15+
/// </remarks>
16+
/// <param name="serviceProvider">Service Provider</param>
17+
internal sealed class GamePlatformIdentifier(IServiceProvider serviceProvider) : IGamePlatformIdentifier
1418
{
15-
private readonly IServiceProvider _serviceProvider;
16-
private readonly ILogger? _logger;
19+
private readonly IServiceProvider _serviceProvider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider));
20+
private readonly ILogger? _logger = serviceProvider.GetService<ILoggerFactory>()?.CreateLogger(typeof(GamePlatformIdentifier));
1721

1822
/// <summary>
1923
/// Default ordering of <see cref="GamePlatform"/>s for identification.
@@ -31,16 +35,6 @@ internal sealed class GamePlatformIdentifier : IGamePlatformIdentifier
3135
GamePlatform.Disk
3236
];
3337

34-
/// <summary>
35-
/// Creates a new instance.
36-
/// </summary>
37-
/// <param name="serviceProvider">Service Provider</param>
38-
public GamePlatformIdentifier(IServiceProvider serviceProvider)
39-
{
40-
_serviceProvider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider));
41-
_logger = serviceProvider.GetService<ILoggerFactory>()?.CreateLogger(typeof(GamePlatformIdentifier));
42-
}
43-
4438
/// <inheritdoc/>
4539
public GamePlatform GetGamePlatform(GameType type, ref IDirectoryInfo location)
4640
{
@@ -49,15 +43,15 @@ public GamePlatform GetGamePlatform(GameType type, ref IDirectoryInfo location)
4943
foreach (var platform in DefaultGamePlatformOrdering)
5044
{
5145
var validator = GamePlatformIdentifierFactory.Create(platform, _serviceProvider);
52-
_logger?.LogDebug($"Validating location for {platform}...");
46+
_logger?.LogTrace($"Validating location for {platform}...");
5347
if (!validator.IsPlatform(type, ref location))
5448
continue;
5549

56-
_logger?.LogDebug($"Game location was identified as {platform}.");
50+
_logger?.LogTrace($"Game location was identified as {platform}.");
5751
return platform;
5852
}
5953

60-
_logger?.LogDebug("Unable to determine which which platform the game has.");
54+
_logger?.LogTrace("Unable to determine which which platform the game has.");
6155
return GamePlatform.Undefined;
6256
}
6357
}

src/PG.StarWarsGame.Infrastructure/Services/Detection/Games/Platform/OriginIdentifier.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ public override bool IsPlatformFoc(ref IDirectoryInfo location)
1818
{
1919
if (!GameDetectorBase.GameExeExists(location, GameType.Foc))
2020
{
21-
Logger?.LogWarning("Unable to find FoC Origin at first location. Trying to fix broken registry path");
21+
Logger?.LogTrace("Unable to find FoC Origin at first location. Trying to fix broken registry path");
2222
TryFixBrokenFocLocation(ref location);
2323
if (!GameDetectorBase.GameExeExists(location, GameType.Foc))
2424
{
25-
Logger?.LogWarning("Origin location fix was unsuccessful. This is not a Origin installation.");
25+
Logger?.LogTrace("Origin location fix was unsuccessful. This is not a Origin installation.");
2626
return false;
2727
}
2828
}
@@ -40,7 +40,7 @@ public override bool IsPlatformEaw(ref IDirectoryInfo location)
4040
{
4141
if (!GameDetectorBase.MinimumGameFilesExist(GameType.Eaw, location))
4242
{
43-
Logger?.LogWarning("Unable to find EaW Origin at first location.");
43+
Logger?.LogTrace("Unable to find EaW Origin at first location.");
4444
return false;
4545
}
4646

@@ -66,7 +66,7 @@ private void TryFixBrokenFocLocation(ref IDirectoryInfo location)
6666
var correctedPath = location.FileSystem.Path.Combine(parentDir.FullName, "EAWX");
6767
if (!location.FileSystem.Directory.Exists(correctedPath))
6868
{
69-
Logger?.LogDebug($"Unable to sanitize origin path: Corrected path '{correctedPath}' does not exists.");
69+
Logger?.LogTrace($"Unable to sanitize origin path: Corrected path '{correctedPath}' does not exists.");
7070
return;
7171
}
7272
location = location.FileSystem.DirectoryInfo.New(correctedPath);

0 commit comments

Comments
 (0)