Skip to content

Commit

Permalink
refactor: move application class code to assembly code
Browse files Browse the repository at this point in the history
  • Loading branch information
CalvinWilkinson committed Jan 27, 2025
1 parent 6e6ce0d commit 15d1b6b
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 62 deletions.
24 changes: 0 additions & 24 deletions CASL/Application.cs

This file was deleted.

5 changes: 5 additions & 0 deletions CASL/DotnetWrappers/Assembly.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace CASL.DotnetWrappers;

using System;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Runtime.Loader;

/// <inheritdoc cref="IAssembly"/>
Expand All @@ -20,6 +21,7 @@ internal sealed class Assembly : IAssembly, IDisposable
/// </summary>
public Assembly()
{
this.Location = AppContext.BaseDirectory.TrimEnd(Path.DirectorySeparatorChar);
var assembly = System.Reflection.Assembly.GetExecutingAssembly();

ArgumentNullException.ThrowIfNull(assembly);
Expand All @@ -32,6 +34,9 @@ public Assembly()
/// <inheritdoc/>
public event Action? Unloading;

/// <inheritdoc/>
public string Location { get; }

/// <inheritdoc cref="IDisposable.Dispose"/>
public void Dispose() => Dispose(true);

Expand Down
5 changes: 5 additions & 0 deletions CASL/DotnetWrappers/IAssembly.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,9 @@ internal interface IAssembly
/// Occurs when the assembly is unloading.
/// </summary>
event Action Unloading;

/// <summary>
/// Gets the file path of the current assembly.
/// </summary>
string Location { get; }
}
16 changes: 0 additions & 16 deletions CASL/IApplication.cs

This file was deleted.

1 change: 0 additions & 1 deletion CASL/IoC.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ private static void SetupContainer()
IoCContainer.Register<IAssembly, Assembly>(Lifestyle.Singleton);
IoCContainer.Register<ITaskService, TaskService>(true);
IoCContainer.Register<IThreadService, ThreadService>(Lifestyle.Singleton);
IoCContainer.Register<IApplication, Application>(Lifestyle.Singleton);
IoCContainer.Register<IPlatform, Platform>(Lifestyle.Singleton);
IoCContainer.Register<ILibrary, OpenALLibrary>(Lifestyle.Singleton);
IoCContainer.Register<IDelegateFactory, DelegateFactory>(Lifestyle.Singleton);
Expand Down
15 changes: 7 additions & 8 deletions CASL/NativeInterop/NativeLibraryLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,45 +14,44 @@ namespace CASL.NativeInterop;
using System;
using System.IO;
using System.IO.Abstractions;
using DotnetWrappers;
using Exceptions;

/// <summary>
/// Loads a native library and returns a pointer for the purpose of interoping with it.
/// </summary>
internal sealed class NativeLibraryLoader : ILibraryLoader
{
private readonly IApplication application;
private readonly IAssembly assembly;
private readonly IPlatform platform;
private readonly IDirectory directory;
private readonly IFile file;
private readonly IPath path;

/// <summary>
/// Initializes a new instance of the <see cref="NativeLibraryLoader"/> class.
/// </summary>
/// <param name="assembly">Provides assembly related services.</param>
/// <param name="platform">Provides platform specific information.</param>
/// <param name="directory">Performs operations with directories.</param>
/// <param name="file">Performs operations with files.</param>
/// <param name="path">Manages file paths.</param>
/// <param name="library">The library to load.</param>
public NativeLibraryLoader(
IApplication application,
IAssembly assembly,
IPlatform platform,
IDirectory directory,

Check warning on line 41 in CASL/NativeInterop/NativeLibraryLoader.cs

View workflow job for this annotation

GitHub Actions / CASL Test Status Check / Run Unit Tests

Parameter 'directory' has no matching param tag in the XML comment for 'NativeLibraryLoader.NativeLibraryLoader(IAssembly, IPlatform, IDirectory, IFile, IPath, ILibrary)' (but other parameters do)

Check warning on line 41 in CASL/NativeInterop/NativeLibraryLoader.cs

View workflow job for this annotation

GitHub Actions / CASL Test Status Check / Run Unit Tests

Check warning on line 41 in CASL/NativeInterop/NativeLibraryLoader.cs

View workflow job for this annotation

GitHub Actions / CASL Test Status Check / Run Unit Tests

Parameter 'directory' has no matching param tag in the XML comment for 'NativeLibraryLoader.NativeLibraryLoader(IAssembly, IPlatform, IDirectory, IFile, IPath, ILibrary)' (but other parameters do)

Check warning on line 41 in CASL/NativeInterop/NativeLibraryLoader.cs

View workflow job for this annotation

GitHub Actions / CASL Test Status Check / Run Unit Tests

Check warning on line 41 in CASL/NativeInterop/NativeLibraryLoader.cs

View workflow job for this annotation

GitHub Actions / CASL Build Status Check / Building CASL Project

Parameter 'directory' has no matching param tag in the XML comment for 'NativeLibraryLoader.NativeLibraryLoader(IAssembly, IPlatform, IDirectory, IFile, IPath, ILibrary)' (but other parameters do)

Check warning on line 41 in CASL/NativeInterop/NativeLibraryLoader.cs

View workflow job for this annotation

GitHub Actions / CASL Build Status Check / Building CASL Project

Check warning on line 41 in CASL/NativeInterop/NativeLibraryLoader.cs

View workflow job for this annotation

GitHub Actions / CASL Build Status Check / Building CASL Project

Parameter 'directory' has no matching param tag in the XML comment for 'NativeLibraryLoader.NativeLibraryLoader(IAssembly, IPlatform, IDirectory, IFile, IPath, ILibrary)' (but other parameters do)

Check warning on line 41 in CASL/NativeInterop/NativeLibraryLoader.cs

View workflow job for this annotation

GitHub Actions / CASL Build Status Check / Building CASL Project

IFile file,
IPath path,
ILibrary library)
{
ArgumentNullException.ThrowIfNull(application);
ArgumentNullException.ThrowIfNull(assembly);
ArgumentNullException.ThrowIfNull(platform);
ArgumentNullException.ThrowIfNull(directory);
ArgumentNullException.ThrowIfNull(file);
ArgumentNullException.ThrowIfNull(path);
ArgumentNullException.ThrowIfNull(library);

this.application = application;
this.assembly = assembly;
this.platform = platform;
this.directory = directory;
this.file = file;
this.path = path;

Expand All @@ -65,7 +64,7 @@ public NativeLibraryLoader(
/// <inheritdoc/>
public nint LoadLibrary()
{
var libDirPath = this.application.Location;
var libDirPath = this.assembly.Location;
var libFilePath = $"{libDirPath}{this.path.DirectorySeparatorChar}{LibraryName}";

var (exists, libPtr) = LoadLibraryIfExists(libFilePath);
Expand Down
14 changes: 9 additions & 5 deletions CASL/NativeInterop/OpenALLibrary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace CASL.NativeInterop;

using System.IO;
using System.IO.Abstractions;
using DotnetWrappers;
using Exceptions;

/// <summary>
Expand All @@ -25,20 +26,23 @@ internal sealed class OpenALLibrary : ILibrary
/// Initializes a new instance of the <see cref="OpenALLibrary"/> class.
/// </summary>
/// <param name="platform">Provides platform specific information.</param>
/// <param name="application">Provides application related services.</param>
/// <param name="directory">Performs operations with directories.</param>
/// <param name="file">Performs operations with files.</param>
/// <param name="path">Manages file paths.</param>
/// <param name="assembly">Provides assembly related services.</param>
public OpenALLibrary(
IPlatform platform,
IDirectory directory,
IFile file,
IPath path,
IApplication application)
IAssembly assembly)
{
// TODO: Add null checks for all params
this.platform = platform;
this.directory = directory;
this.file = file;
this.path = path;
this.appDirPath = application.Location;
this.appDirPath = assembly.Location;

ProcessLibFile();
}
Expand Down Expand Up @@ -69,7 +73,7 @@ private void ProcessLibFile()
var libName = GetLibraryName();
var fullLibPath = $"{this.appDirPath}{this.path.DirectorySeparatorChar}{libName}";

// Check if the library exists in the same location as the application
// Check if the library exists in the same location as the assembly
if (this.file.Exists(fullLibPath))
{
return;
Expand Down Expand Up @@ -103,7 +107,7 @@ private void ProcessLibFile()
throw new FileNotFoundException($"The library '{libName}' does not exist in the platform directory '{platformDirPath}'.");
}

// At this point, the library file exist. Copy the library to the same location as the application.
// At this point, the library file exist. Copy the library to the same location as the assembly.
this.file.Copy(fullPlatLibPath, fullLibPath);
}
}
16 changes: 8 additions & 8 deletions Testing/CASLTests/NativeInterop/OpenALLibraryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace CASLTests.NativeInterop;
#pragma warning disable IDE0001 // The name can be simplified
using System.IO;
using System.IO.Abstractions;
using CASL;
using CASL.DotnetWrappers;
using CASL.Exceptions;
using CASL.NativeInterop;
using Xunit;
Expand All @@ -26,7 +26,7 @@ public class OpenALLibraryTests
private readonly IDirectory mockDirectory;
private readonly IFile mockFile;
private readonly IPath mockPath;
private readonly IApplication mockApplication;
private readonly IAssembly mockAssembly;

/// <summary>
/// Initializes a new instance of the <see cref="OpenALLibraryTests"/> class.
Expand All @@ -37,7 +37,7 @@ public OpenALLibraryTests()
this.mockDirectory = Substitute.For<IDirectory>();
this.mockFile = Substitute.For<IFile>();
this.mockPath = Substitute.For<IPath>();
this.mockApplication = Substitute.For<IApplication>();
this.mockAssembly = Substitute.For<IAssembly>();
}


Expand Down Expand Up @@ -65,7 +65,7 @@ public void Ctor_WhenPlatformDirPathDoesNotExist_ThrowsException()
{
// Arrange
MockWindowsPlatform();
this.mockApplication.Location.Returns(@"C:\app-dir");
this.mockAssembly.Location.Returns(@"C:\app-dir");
this.mockFile.Exists(Arg.Any<string>()).Returns(false);
this.mockDirectory.Exists(Arg.Any<string>()).Returns(false);

Expand All @@ -84,7 +84,7 @@ public void Ctor_WhenFullPlatformLibFilePathDoesNotExist_ThrowsException()
const string expected = @"The library 'soft_oal.dll' does not exist" +
@" in the platform directory 'C:\app-dir\runtimes\win-x64\native'.";
MockWindowsPlatform();
this.mockApplication.Location.Returns(@"C:\app-dir");
this.mockAssembly.Location.Returns(@"C:\app-dir");
this.mockFile.Exists(Arg.Any<string>()).Returns(false);
this.mockDirectory.Exists(Arg.Any<string>()).Returns(true);

Expand Down Expand Up @@ -172,7 +172,7 @@ private OpenALLibrary CreateSystemUnderTest()
this.mockDirectory,
this.mockFile,
this.mockPath,
this.mockApplication);
this.mockAssembly);

/// <summary>
/// Mocks a windows platform.
Expand All @@ -182,7 +182,7 @@ private void MockWindowsPlatform()
this.mockPlatform.IsWinPlatform().Returns(true);
this.mockPlatform.IsPosixPlatform().Returns(false);
this.mockPath.DirectorySeparatorChar.Returns('\\');
this.mockApplication.Location.Returns(@"C:\app-dir");
this.mockAssembly.Location.Returns(@"C:\app-dir");
}

/// <summary>
Expand All @@ -193,6 +193,6 @@ private void MockPosixPlatform()
this.mockPlatform.IsWinPlatform().Returns(false);
this.mockPlatform.IsPosixPlatform().Returns(true);
this.mockPath.DirectorySeparatorChar.Returns('/');
this.mockApplication.Location.Returns("/app-dir");
this.mockAssembly.Location.Returns("/app-dir");
}
}

0 comments on commit 15d1b6b

Please sign in to comment.