Skip to content

Commit

Permalink
Merge pull request #135 from AlamoEngine-Tools/develop
Browse files Browse the repository at this point in the history
Fix accidental disposing of singleton services.
  • Loading branch information
AnakinRaW authored Aug 3, 2024
2 parents 2f1a0ba + 0ea1629 commit d8b544e
Show file tree
Hide file tree
Showing 25 changed files with 51 additions and 54 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
Expand Up @@ -16,9 +16,9 @@ internal class SteamLibrary : ISteamLibrary

private readonly Dictionary<KnownLibraryLocations, string[]> _locationsNames = new()
{
{ KnownLibraryLocations.SteamApps, new[] { "steamapps" } },
{ KnownLibraryLocations.Common, new[] { "steamapps", "common" } },
{ KnownLibraryLocations.Workshops, new[] { "steamapps", "workshop" } }
{ KnownLibraryLocations.SteamApps, ["steamapps"] },
{ KnownLibraryLocations.Common, ["steamapps", "common"] },
{ KnownLibraryLocations.Workshops, ["steamapps", "workshop"] }
};

private readonly string _normalizedLocation;
Expand Down
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
Expand Up @@ -8,7 +8,7 @@ internal class WindowHandleInfo(IntPtr handle)
{
public List<IntPtr> GetAllChildHandles()
{
List<IntPtr> childHandles = new();
List<IntPtr> childHandles = [];

var gcChildHandlesList = GCHandle.Alloc(childHandles);
var pointerChildHandlesList = GCHandle.ToIntPtr(gcChildHandlesList);
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 All @@ -8,7 +9,7 @@ namespace AET.SteamAbstraction;
/// <summary>
/// Provides initialization routines for this library.
/// </summary>
public class SteamAbstractionLayer
public static class SteamAbstractionLayer
{
/// <summary>
/// Adds services provided by this library to the given <paramref name="serviceCollection"/>
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 d8b544e

Please sign in to comment.