From 44eae437505ab548b0d4bdc27581cd92fc24820a Mon Sep 17 00:00:00 2001 From: Justin Swanson Date: Mon, 22 Jul 2024 02:57:03 -0500 Subject: [PATCH] AssetLink SetPath moved to extension methods --- .../Plugins/Assets/AssetLinkTests.cs | 34 +++++++-------- .../Plugins/Assets/AssetLink.cs | 42 +++++++++++-------- .../Plugins/Assets/IAssetLink.cs | 15 ++++++- 3 files changed, 56 insertions(+), 35 deletions(-) diff --git a/Mutagen.Bethesda.Core.UnitTests/Plugins/Assets/AssetLinkTests.cs b/Mutagen.Bethesda.Core.UnitTests/Plugins/Assets/AssetLinkTests.cs index 64ba3cb96..2a31a6001 100644 --- a/Mutagen.Bethesda.Core.UnitTests/Plugins/Assets/AssetLinkTests.cs +++ b/Mutagen.Bethesda.Core.UnitTests/Plugins/Assets/AssetLinkTests.cs @@ -11,7 +11,7 @@ public class AssetLinkTests [Fact] public void TrySetPath() { - var link = new AssetLink(); + IAssetLink link = new AssetLink(); var path = Path.Combine("Meshes", "SomeSubFolder", "SomeModel.nif"); link.TrySetPath(path) .Should().BeTrue(); @@ -22,7 +22,7 @@ public void TrySetPath() [Fact] public void TrySetPathToNull() { - var link = new AssetLink(); + IAssetLink link = new AssetLink(); link.TrySetPath(Path.Combine("Meshes", "SomeSubFolder", "SomeModel.nif")) .Should().BeTrue(); link.TrySetPath(null); @@ -33,7 +33,7 @@ public void TrySetPathToNull() [Fact] public void TrySetPathFailed() { - var link = new AssetLink(); + IAssetLink link = new AssetLink(); var path = Path.Combine("Meshes", "SomeSubFolder", "SomeModel.nif"); link.TrySetPath(path) .Should().BeTrue(); @@ -46,7 +46,7 @@ public void TrySetPathFailed() [Fact] public void SetPathDataRelative() { - var link = new AssetLink(); + IAssetLink link = new AssetLink(); var path = Path.Combine("Meshes", "SomeSubFolder", "SomeModel.nif"); link.SetPath(path); link.GivenPath.Should().Be(path); @@ -56,7 +56,7 @@ public void SetPathDataRelative() [Fact] public void SetPathAbsolute() { - var link = new AssetLink(); + IAssetLink link = new AssetLink(); var path = Path.Combine("SomeFolder", "Data", "Meshes", "SomeSubFolder", "SomeModel.nif"); link.SetPath(path); link.GivenPath.Should().Be(path); @@ -66,7 +66,7 @@ public void SetPathAbsolute() [Fact] public void SetPathToNull() { - var link = new AssetLink(); + IAssetLink link = new AssetLink(); link.SetPath(Path.Combine("Meshes", "SomeSubFolder", "SomeModel.nif")); link.TrySetPath(null); link.GivenPath.Should().Be(string.Empty); @@ -76,7 +76,7 @@ public void SetPathToNull() [Fact] public void SetPathFailed() { - var link = new AssetLink(); + IAssetLink link = new AssetLink(); var path = Path.Combine("Meshes", "SomeSubFolder", "SomeModel.nif"); link.SetPath(path); Assert.Throws(() => @@ -90,7 +90,7 @@ public void SetPathFailed() [Fact] public void TestEqualsDifferentRawPath() { - var link = new AssetLinkGetter(Path.Combine("SomeSubFolder", "SomeModel.nif")); + IAssetLinkGetter link = new AssetLinkGetter(Path.Combine("SomeSubFolder", "SomeModel.nif")); var otherLink = new AssetLinkGetter(Path.Combine("Meshes", "SomeSubFolder", "SomeModel.nif")); link.Equals(otherLink).Should().BeTrue(); @@ -100,7 +100,7 @@ public void TestEqualsDifferentRawPath() public void TestEqualsAbsolutePath() { var filePath = new FilePath(Path.Combine("SomeFolder", "Data", "Meshes", "SomeSubFolder", "SomeModel.nif")); - var link = new AssetLinkGetter(filePath.Path); + IAssetLinkGetter link = new AssetLinkGetter(filePath.Path); var otherLink = new AssetLinkGetter(Path.Combine("Meshes", "SomeSubFolder", "SomeModel.nif")); link.Equals(otherLink).Should().BeTrue(); @@ -109,7 +109,7 @@ public void TestEqualsAbsolutePath() [Fact] public void TestObjectEqualsGetterGetter() { - var link = new AssetLinkGetter(Path.Combine("Meshes", "SomeSubFolder", "SomeModel.nif")); + IAssetLinkGetter link = new AssetLinkGetter(Path.Combine("Meshes", "SomeSubFolder", "SomeModel.nif")); var otherLink = new AssetLinkGetter(Path.Combine("Meshes", "SomeSubFolder", "SomeModel.nif")); Equals(link, otherLink).Should().BeTrue(); @@ -118,7 +118,7 @@ public void TestObjectEqualsGetterGetter() [Fact] public void TestObjectEqualsGetterSetter() { - var link = new AssetLinkGetter(Path.Combine("Meshes", "SomeSubFolder", "SomeModel.nif")); + IAssetLinkGetter link = new AssetLinkGetter(Path.Combine("Meshes", "SomeSubFolder", "SomeModel.nif")); var otherLink = new AssetLink(Path.Combine("Meshes", "SomeSubFolder", "SomeModel.nif")); Equals(link, otherLink).Should().BeTrue(); @@ -127,7 +127,7 @@ public void TestObjectEqualsGetterSetter() [Fact] public void TestObjectEqualsSetterGetter() { - var link = new AssetLink(Path.Combine("Meshes", "SomeSubFolder", "SomeModel.nif")); + IAssetLinkGetter link = new AssetLink(Path.Combine("Meshes", "SomeSubFolder", "SomeModel.nif")); var otherLink = new AssetLinkGetter(Path.Combine("Meshes", "SomeSubFolder", "SomeModel.nif")); Equals(link, otherLink).Should().BeTrue(); @@ -136,7 +136,7 @@ public void TestObjectEqualsSetterGetter() [Fact] public void TestObjectEqualsSetterSetter() { - var link = new AssetLink(Path.Combine("Meshes", "SomeSubFolder", "SomeModel.nif")); + IAssetLinkGetter link = new AssetLink(Path.Combine("Meshes", "SomeSubFolder", "SomeModel.nif")); var otherLink = new AssetLink(Path.Combine("Meshes", "SomeSubFolder", "SomeModel.nif")); Equals(link, otherLink).Should().BeTrue(); @@ -145,7 +145,7 @@ public void TestObjectEqualsSetterSetter() [Fact] public void TestEqualsGetterGetter() { - var link = new AssetLinkGetter(Path.Combine("Meshes", "SomeSubFolder", "SomeModel.nif")); + IAssetLinkGetter link = new AssetLinkGetter(Path.Combine("Meshes", "SomeSubFolder", "SomeModel.nif")); var otherLink = new AssetLinkGetter(Path.Combine("Meshes", "SomeSubFolder", "SomeModel.nif")); link.Equals(otherLink).Should().BeTrue(); @@ -154,7 +154,7 @@ public void TestEqualsGetterGetter() [Fact] public void TestEqualsGetterSetter() { - var link = new AssetLinkGetter(Path.Combine("Meshes", "SomeSubFolder", "SomeModel.nif")); + IAssetLinkGetter link = new AssetLinkGetter(Path.Combine("Meshes", "SomeSubFolder", "SomeModel.nif")); var otherLink = new AssetLink(Path.Combine("Meshes", "SomeSubFolder", "SomeModel.nif")); link.Equals(otherLink).Should().BeTrue(); @@ -163,7 +163,7 @@ public void TestEqualsGetterSetter() [Fact] public void TestEqualsSetterGetter() { - var link = new AssetLink(Path.Combine("Meshes", "SomeSubFolder", "SomeModel.nif")); + IAssetLinkGetter link = new AssetLink(Path.Combine("Meshes", "SomeSubFolder", "SomeModel.nif")); var otherLink = new AssetLinkGetter(Path.Combine("Meshes", "SomeSubFolder", "SomeModel.nif")); link.Equals(otherLink).Should().BeTrue(); @@ -172,7 +172,7 @@ public void TestEqualsSetterGetter() [Fact] public void TestEqualsSetterSetter() { - var link = new AssetLink(Path.Combine("Meshes", "SomeSubFolder", "SomeModel.nif")); + IAssetLinkGetter link = new AssetLink(Path.Combine("Meshes", "SomeSubFolder", "SomeModel.nif")); var otherLink = new AssetLink(Path.Combine("Meshes", "SomeSubFolder", "SomeModel.nif")); link.Equals(otherLink).Should().BeTrue(); diff --git a/Mutagen.Bethesda.Core/Plugins/Assets/AssetLink.cs b/Mutagen.Bethesda.Core/Plugins/Assets/AssetLink.cs index d35725a05..383c8278d 100644 --- a/Mutagen.Bethesda.Core/Plugins/Assets/AssetLink.cs +++ b/Mutagen.Bethesda.Core/Plugins/Assets/AssetLink.cs @@ -17,12 +17,14 @@ public class AssetLinkGetter : { protected DataRelativePath _DataRelativePath; protected string _givenPath; - protected static readonly IAssetType AssetInstance; + protected static readonly TAssetType AssetInstance; public static readonly AssetLinkGetter Null = new(); + + public IAssetType AssetTypeInstance => AssetInstance; static AssetLinkGetter() { - AssetInstance = TAssetType.Instance; + AssetInstance = (TAssetType)TAssetType.Instance; } public AssetLinkGetter() @@ -177,6 +179,8 @@ public class AssetLink : IAssetLink where TAssetType : class, IAssetType { + TAssetType IAssetLink.AssetTypeInstance => AssetInstance; + public AssetLink() : base(DataRelativePath.NullPath) { @@ -235,21 +239,6 @@ public bool TrySetPath(string? path) return false; } - public void SetPath(DataRelativePath? path) - { - if (!TrySetPath(path)) - { - throw new AssetPathMisalignedException(path?.Path!, AssetInstance); - } - } - - public void SetPath(string? path) - { - if (!TrySetPath(path)) - { - throw new AssetPathMisalignedException(path!, AssetInstance); - } - } public new string GivenPath { @@ -321,3 +310,22 @@ public int CompareTo(AssetLink? other) [return: NotNullIfNotNull("path")] public static implicit operator AssetLink?(DataRelativePath? path) => path == null ? null : new(path.Value); } + +public static class AssetLinkExt +{ + public static void SetPath(this IAssetLink assetLink, DataRelativePath? path) + { + if (!assetLink.TrySetPath(path)) + { + throw new AssetPathMisalignedException(path?.Path!, assetLink.AssetTypeInstance); + } + } + + public static void SetPath(this IAssetLink assetLink, string? path) + { + if (!assetLink.TrySetPath(path)) + { + throw new AssetPathMisalignedException(path!, assetLink.AssetTypeInstance); + } + } +} \ No newline at end of file diff --git a/Mutagen.Bethesda.Core/Plugins/Assets/IAssetLink.cs b/Mutagen.Bethesda.Core/Plugins/Assets/IAssetLink.cs index a62c1a2fe..cf0e1ef91 100644 --- a/Mutagen.Bethesda.Core/Plugins/Assets/IAssetLink.cs +++ b/Mutagen.Bethesda.Core/Plugins/Assets/IAssetLink.cs @@ -4,6 +4,8 @@ namespace Mutagen.Bethesda.Plugins.Assets; public interface IAssetLinkGetter { + IAssetType AssetTypeInstance { get; } + /// /// Original given path to the Asset Link /// @@ -47,6 +49,8 @@ public interface IAssetLinkGetter : IAssetLinkGetter public interface IAssetLink : IAssetLink, TAssetType> where TAssetType : IAssetType { + new TAssetType AssetTypeInstance { get; } + /// /// Raw path pointing to the asset /// @@ -70,9 +74,18 @@ public interface IAssetLink : public interface IAssetLink : IAssetLinkGetter { /// - /// Set the path to a path that is relative to the game's Data directory + /// Attempts to set the path of the AssetLink.
+ /// Will back out and return false if path does not align with expected folder structure. ///
+ /// True if path matched expected patterns. False if it did not align with expected folder structure bool TrySetPath(DataRelativePath? path); + + /// + /// Attempts to set the path of the AssetLink.
+ /// Will back out and return false if path does not align with expected folder structure. + ///
+ /// True if path matched expected patterns. False if it did not align with expected folder structure + bool TrySetPath(string? path); /// /// Raw path pointing to the asset