Skip to content

Commit

Permalink
Make singleton services not disposable
Browse files Browse the repository at this point in the history
  • Loading branch information
AnakinRaW committed Aug 3, 2024
1 parent 3e084ac commit 5b5ba5b
Show file tree
Hide file tree
Showing 23 changed files with 46 additions and 49 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.IO.Abstractions;
using AET.SteamAbstraction.Games;
using AET.SteamAbstraction.Registry;
using AET.SteamAbstraction.Utilities;
using Microsoft.Extensions.DependencyInjection;
using Moq;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.IO.Abstractions;
using System.Linq;
using AET.SteamAbstraction.Library;
using AET.SteamAbstraction.Registry;
using Microsoft.Extensions.DependencyInjection;
using Moq;
using Testably.Abstractions.Testing;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.IO.Abstractions;
using System.Runtime.InteropServices;
using AET.SteamAbstraction.Registry;
using AET.SteamAbstraction.Utilities;
using AnakinRaW.CommonUtilities.Registry;
using Microsoft.Extensions.DependencyInjection;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.IO.Abstractions;
using AET.SteamAbstraction.Registry;
using AET.SteamAbstraction.Utilities;
using Microsoft.Extensions.DependencyInjection;
using Moq;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Threading.Tasks;
using AET.SteamAbstraction.Games;
using AET.SteamAbstraction.Library;
using AET.SteamAbstraction.Registry;
using AET.SteamAbstraction.Utilities;
using Microsoft.Extensions.DependencyInjection;
using Moq;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Diagnostics;
using System.IO.Abstractions;
using AET.SteamAbstraction.Games;
using AET.SteamAbstraction.Registry;
using AET.SteamAbstraction.Utilities;
using Microsoft.Extensions.DependencyInjection;
using Moq;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System;
namespace AET.SteamAbstraction.Games;

namespace AET.SteamAbstraction.Games;

internal interface ISteamGameFinder : IDisposable
internal interface ISteamGameFinder
{
SteamAppManifest? FindGame(uint gameId);
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
using System;
using System.Linq;
using AET.SteamAbstraction.Library;
using AnakinRaW.CommonUtilities;
using Microsoft.Extensions.DependencyInjection;

namespace AET.SteamAbstraction.Games;

internal class SteamGameFinder : DisposableObject, ISteamGameFinder
internal class SteamGameFinder : ISteamGameFinder
{
private readonly ISteamLibraryFinder _libraryFinder;

Expand All @@ -26,10 +25,4 @@ public SteamGameFinder(IServiceProvider serviceProvider)
.FirstOrDefault(matching => matching is not null);
return game;
}

protected override void DisposeManagedResources()
{
base.DisposeManagedResources();
_libraryFinder.Dispose();
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;

namespace AET.SteamAbstraction.Library;

internal interface ISteamLibraryFinder : IDisposable
internal interface ISteamLibraryFinder
{
IEnumerable<ISteamLibrary> FindLibraries();
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
using System;
using System.Collections.Generic;
using System.IO.Abstractions;
using AnakinRaW.CommonUtilities;
using AET.SteamAbstraction.Registry;
using Microsoft.Extensions.DependencyInjection;

namespace AET.SteamAbstraction.Library;

internal class SteamLibraryFinder(IServiceProvider serviceProvider) : DisposableObject, ISteamLibraryFinder
internal class SteamLibraryFinder(IServiceProvider serviceProvider) : ISteamLibraryFinder
{
private readonly IServiceProvider _serviceProvider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider));
private readonly ISteamRegistry _registry = serviceProvider.GetRequiredService<ISteamRegistryFactory>().CreateRegistry();
private readonly ISteamRegistryFactory _registryFactory = serviceProvider.GetRequiredService<ISteamRegistryFactory>();
private readonly IFileSystem _fileSystem = serviceProvider.GetRequiredService<IFileSystem>();
private readonly ISteamVdfReader _configReader = serviceProvider.GetRequiredService<ISteamVdfReader>();

Expand Down Expand Up @@ -41,7 +41,8 @@ private bool TryCreateLibraryFromLocation(IDirectoryInfo libraryLocation, out St

private IFileInfo? GetLibraryLocationsFile()
{
var steamInstallLocation = _registry.InstallationDirectory;
using var registry = _registryFactory.CreateRegistry();
var steamInstallLocation = registry.InstallationDirectory;
if (steamInstallLocation is null)
throw new SteamException("Unable to find an installation of Steam.");

Expand All @@ -56,10 +57,4 @@ private bool TryCreateLibraryFromLocation(IDirectoryInfo libraryLocation, out St

return libraryLocationsFile.Exists ? libraryLocationsFile : null;
}

protected override void DisposeManagedResources()
{
base.DisposeManagedResources();
_registry.Dispose();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.IO.Abstractions;
using AET.SteamAbstraction.Registry;
using AnakinRaW.CommonUtilities;

namespace AET.SteamAbstraction;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using AET.SteamAbstraction.Registry;

namespace AET.SteamAbstraction;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO.Abstractions;
using AnakinRaW.CommonUtilities.Registry;

namespace AET.SteamAbstraction;
namespace AET.SteamAbstraction.Registry;

/// <summary>
/// Registry representation for the Steam Client.
Expand All @@ -24,13 +22,4 @@ internal interface ISteamRegistry : IDisposable
/// The installation directory of the Steam client
/// </summary>
IDirectoryInfo? InstallationDirectory { get; }
}

internal interface IWindowsSteamRegistry : ISteamRegistry
{
IRegistryKey? ActiveProcessKey { get; }

int? ProcessId { get; }

ISet<uint>? InstalledApps { get; }
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace AET.SteamAbstraction;
namespace AET.SteamAbstraction.Registry;

internal interface ISteamRegistryFactory
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System.Collections.Generic;
using AnakinRaW.CommonUtilities.Registry;

namespace AET.SteamAbstraction.Registry;

internal interface IWindowsSteamRegistry : ISteamRegistry
{
IRegistryKey? ActiveProcessKey { get; }

int? ProcessId { get; }

ISet<uint>? InstalledApps { get; }
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Runtime.InteropServices;

namespace AET.SteamAbstraction;
namespace AET.SteamAbstraction.Registry;

internal class SteamRegistryFactory(IServiceProvider serviceProvider) : ISteamRegistryFactory
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using AET.SteamAbstraction.Games;
using AET.SteamAbstraction.Library;
using AET.SteamAbstraction.Registry;
using AET.SteamAbstraction.Utilities;
using Microsoft.Extensions.DependencyInjection;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,4 @@ public SteamException(string message) : base(message)
public SteamException(string message, Exception exception) : base(message, exception)
{
}
}


/// <summary>
///
/// </summary>
public sealed class SteamNotFoundException : SteamException
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace AET.SteamAbstraction;

/// <summary>
/// Exception which gets thrown if an installation of Steam was not found.
/// </summary>
public sealed class SteamNotFoundException : SteamException;
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Threading;
using System.Threading.Tasks;
using AET.SteamAbstraction.Games;
using AET.SteamAbstraction.Registry;
using AnakinRaW.CommonUtilities;
using Microsoft.Extensions.DependencyInjection;

Expand Down Expand Up @@ -63,7 +64,7 @@ public bool IsUserLoggedIn
public virtual bool IsGameInstalled(uint gameId, [NotNullWhen(true)] out SteamAppManifest? game)
{
ThrowIfSteamNotInstalled();
using var gameFinder = ServiceProvider.GetRequiredService<ISteamGameFinder>();
var gameFinder = ServiceProvider.GetRequiredService<ISteamGameFinder>();
game = gameFinder.FindGame(gameId);
return game is not null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Runtime.InteropServices;
using AET.SteamAbstraction.Registry;
using Microsoft.Extensions.DependencyInjection;

namespace AET.SteamAbstraction;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.IO.Abstractions;
using System;
using System.Linq;
using AET.SteamAbstraction.Registry;
using AnakinRaW.CommonUtilities;
using Microsoft.Extensions.DependencyInjection;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Threading.Tasks;
using AET.SteamAbstraction.Games;
using AET.SteamAbstraction.NativeMethods;
using AET.SteamAbstraction.Registry;
using AET.SteamAbstraction.Utilities;
using AnakinRaW.CommonUtilities.Registry.Windows;
using Microsoft.Extensions.DependencyInjection;
Expand Down

0 comments on commit 5b5ba5b

Please sign in to comment.