Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🚧Simple texture loader improvement #1076

Merged
merged 2 commits into from
Jan 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 5 additions & 29 deletions Testing/VelaptorTests/Content/TextureLoaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace VelaptorTests.Content;

using System;
using System.IO;
using System.IO.Abstractions;
using System.Runtime.InteropServices;
using FluentAssertions;
Expand All @@ -28,7 +27,6 @@ public class TextureLoaderTests
private readonly IItemCache<string, ITexture> mockTextureCache;
private readonly IContentPathResolver mockTexturePathResolver;
private readonly IDirectory mockDirectory;
private readonly IPath mockPath;

/// <summary>
/// Initializes a new instance of the <see cref="TextureLoaderTests"/> class.
Expand All @@ -38,7 +36,6 @@ public TextureLoaderTests()
this.mockTexturePathResolver = Substitute.For<IContentPathResolver>();
this.mockTextureCache = Substitute.For<IItemCache<string, ITexture>>();
this.mockDirectory = Substitute.For<IDirectory>();
this.mockPath = Substitute.For<IPath>();
}

#region Constructor Tests
Expand All @@ -49,8 +46,7 @@ public void Ctor_WithNullTextureCacheParam_ThrowsException()
var act = () => new TextureLoader(
null,
this.mockTexturePathResolver,
this.mockDirectory,
this.mockPath);
this.mockDirectory);

// Assert
act.Should().Throw<ArgumentNullException>()
Expand All @@ -64,8 +60,7 @@ public void Ctor_WithNullTexturePathResolverParam_ThrowsException()
var act = () => new TextureLoader(
this.mockTextureCache,
null,
this.mockDirectory,
this.mockPath);
this.mockDirectory);

// Assert
act.Should().Throw<ArgumentNullException>()
Expand All @@ -79,29 +74,12 @@ public void Ctor_WithNullDirectoryParam_ThrowsException()
var act = () => new TextureLoader(
this.mockTextureCache,
this.mockTexturePathResolver,
null,
this.mockPath);

// Assert
act.Should()
.Throw<ArgumentNullException>()
.WithMessage("Value cannot be null. (Parameter 'directory')");
}

[Fact]
public void Ctor_WithNullPathParam_ThrowsException()
{
// Arrange & Act
var act = () => new TextureLoader(
this.mockTextureCache,
this.mockTexturePathResolver,
this.mockDirectory,
null);

// Assert
act.Should()
.Throw<ArgumentNullException>()
.WithMessage("Value cannot be null. (Parameter 'path')");
.WithMessage("Value cannot be null. (Parameter 'directory')");
}
#endregion

Expand Down Expand Up @@ -160,9 +138,7 @@ public void Load_WhenContentDirPathDoesNotExist_CreateDirectory()
{
// Arrange
this.mockDirectory.Exists(Arg.Any<string>()).Returns(false);
this.mockPath.AltDirectorySeparatorChar.Returns(Path.AltDirectorySeparatorChar);
this.mockTexturePathResolver.RootDirectoryPath.Returns(ContentDirPath);
this.mockTexturePathResolver.ContentDirectoryName.Returns("Graphics");
this.mockTexturePathResolver.ResolveDirPath().Returns($"{ContentDirPath}/Graphics");

var sut = CreateSystemUnderTest();

Expand Down Expand Up @@ -192,5 +168,5 @@ public void Unload_WhenInvoked_UnloadsCachedTextures()
/// Creates a new instance of <see cref="TextureLoader"/> for the purpose of testing.
/// </summary>
/// <returns>The instance to test.</returns>
private TextureLoader CreateSystemUnderTest() => new (this.mockTextureCache, this.mockTexturePathResolver, this.mockDirectory, this.mockPath);
private TextureLoader CreateSystemUnderTest() => new (this.mockTextureCache, this.mockTexturePathResolver, this.mockDirectory);
}
10 changes: 3 additions & 7 deletions Velaptor/Content/TextureLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,28 @@ internal sealed class TextureLoader : ILoader<ITexture>
private readonly IItemCache<string, ITexture> textureCache;
private readonly IContentPathResolver texturePathResolver;
private readonly IDirectory directory;
private readonly IPath path;

/// <summary>
/// Initializes a new instance of the <see cref="TextureLoader"/> class.
/// </summary>
/// <param name="textureCache">Caches textures for later use to improve performance.</param>
/// <param name="texturePathResolver">Resolves paths to texture content.</param>
/// <param name="directory">Performs operations with directories.</param>
/// <param name="path">Processes directory and file paths.</param>
/// <exception cref="ArgumentNullException">
/// Invoked when any of the parameters are null.
/// </exception>
public TextureLoader(
IItemCache<string, ITexture> textureCache,
IContentPathResolver texturePathResolver,
IDirectory directory,
IPath path)
IDirectory directory)
{
ArgumentNullException.ThrowIfNull(textureCache);
ArgumentNullException.ThrowIfNull(texturePathResolver);
ArgumentNullException.ThrowIfNull(directory);
ArgumentNullException.ThrowIfNull(path);

this.textureCache = textureCache;
this.texturePathResolver = texturePathResolver;
this.directory = directory;
this.path = path;
}

/// <summary>
Expand All @@ -66,7 +61,8 @@ public ITexture Load(string contentPathOrName)
{
ArgumentException.ThrowIfNullOrEmpty(contentPathOrName);

var contentDirPath = $"{this.texturePathResolver.RootDirectoryPath}{this.path.AltDirectorySeparatorChar}{this.texturePathResolver.ContentDirectoryName}";
var contentDirPath = this.texturePathResolver.ResolveDirPath();

if (!this.directory.Exists(contentDirPath))
{
this.directory.CreateDirectory(contentDirPath);
Expand Down
4 changes: 1 addition & 3 deletions Velaptor/Factories/ContentLoaderFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,12 @@ public static ILoader<ITexture> CreateTextureLoader(IContentPathResolver pathRes
}

var cache = IoC.Container.GetInstance<IItemCache<string, ITexture>>();
var path = IoC.Container.GetInstance<IPath>();

var directory = IoC.Container.GetInstance<IDirectory>();
textureLoader = new TextureLoader(
cache,
pathResolver,
directory,
path);
directory);

return textureLoader;
}
Expand Down
Loading