Skip to content

Commit

Permalink
AssetLink SetPath moved to extension methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Noggog committed Jul 22, 2024
1 parent 1f286dd commit 44eae43
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 35 deletions.
34 changes: 17 additions & 17 deletions Mutagen.Bethesda.Core.UnitTests/Plugins/Assets/AssetLinkTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class AssetLinkTests
[Fact]
public void TrySetPath()
{
var link = new AssetLink<TestAssetType>();
IAssetLink link = new AssetLink<TestAssetType>();
var path = Path.Combine("Meshes", "SomeSubFolder", "SomeModel.nif");
link.TrySetPath(path)
.Should().BeTrue();
Expand All @@ -22,7 +22,7 @@ public void TrySetPath()
[Fact]
public void TrySetPathToNull()
{
var link = new AssetLink<TestAssetType>();
IAssetLink link = new AssetLink<TestAssetType>();
link.TrySetPath(Path.Combine("Meshes", "SomeSubFolder", "SomeModel.nif"))
.Should().BeTrue();
link.TrySetPath(null);
Expand All @@ -33,7 +33,7 @@ public void TrySetPathToNull()
[Fact]
public void TrySetPathFailed()
{
var link = new AssetLink<TestAssetType>();
IAssetLink link = new AssetLink<TestAssetType>();
var path = Path.Combine("Meshes", "SomeSubFolder", "SomeModel.nif");
link.TrySetPath(path)
.Should().BeTrue();
Expand All @@ -46,7 +46,7 @@ public void TrySetPathFailed()
[Fact]
public void SetPathDataRelative()
{
var link = new AssetLink<TestAssetType>();
IAssetLink link = new AssetLink<TestAssetType>();
var path = Path.Combine("Meshes", "SomeSubFolder", "SomeModel.nif");
link.SetPath(path);
link.GivenPath.Should().Be(path);
Expand All @@ -56,7 +56,7 @@ public void SetPathDataRelative()
[Fact]
public void SetPathAbsolute()
{
var link = new AssetLink<TestAssetType>();
IAssetLink link = new AssetLink<TestAssetType>();
var path = Path.Combine("SomeFolder", "Data", "Meshes", "SomeSubFolder", "SomeModel.nif");
link.SetPath(path);
link.GivenPath.Should().Be(path);
Expand All @@ -66,7 +66,7 @@ public void SetPathAbsolute()
[Fact]
public void SetPathToNull()
{
var link = new AssetLink<TestAssetType>();
IAssetLink link = new AssetLink<TestAssetType>();
link.SetPath(Path.Combine("Meshes", "SomeSubFolder", "SomeModel.nif"));
link.TrySetPath(null);
link.GivenPath.Should().Be(string.Empty);
Expand All @@ -76,7 +76,7 @@ public void SetPathToNull()
[Fact]
public void SetPathFailed()
{
var link = new AssetLink<TestAssetType>();
IAssetLink link = new AssetLink<TestAssetType>();
var path = Path.Combine("Meshes", "SomeSubFolder", "SomeModel.nif");
link.SetPath(path);
Assert.Throws<AssetPathMisalignedException>(() =>
Expand All @@ -90,7 +90,7 @@ public void SetPathFailed()
[Fact]
public void TestEqualsDifferentRawPath()
{
var link = new AssetLinkGetter<TestAssetType>(Path.Combine("SomeSubFolder", "SomeModel.nif"));
IAssetLinkGetter link = new AssetLinkGetter<TestAssetType>(Path.Combine("SomeSubFolder", "SomeModel.nif"));
var otherLink = new AssetLinkGetter<TestAssetType>(Path.Combine("Meshes", "SomeSubFolder", "SomeModel.nif"));

link.Equals(otherLink).Should().BeTrue();
Expand All @@ -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<TestAssetType>(filePath.Path);
IAssetLinkGetter link = new AssetLinkGetter<TestAssetType>(filePath.Path);
var otherLink = new AssetLinkGetter<TestAssetType>(Path.Combine("Meshes", "SomeSubFolder", "SomeModel.nif"));

link.Equals(otherLink).Should().BeTrue();
Expand All @@ -109,7 +109,7 @@ public void TestEqualsAbsolutePath()
[Fact]
public void TestObjectEqualsGetterGetter()
{
var link = new AssetLinkGetter<TestAssetType>(Path.Combine("Meshes", "SomeSubFolder", "SomeModel.nif"));
IAssetLinkGetter link = new AssetLinkGetter<TestAssetType>(Path.Combine("Meshes", "SomeSubFolder", "SomeModel.nif"));
var otherLink = new AssetLinkGetter<TestAssetType>(Path.Combine("Meshes", "SomeSubFolder", "SomeModel.nif"));

Equals(link, otherLink).Should().BeTrue();
Expand All @@ -118,7 +118,7 @@ public void TestObjectEqualsGetterGetter()
[Fact]
public void TestObjectEqualsGetterSetter()
{
var link = new AssetLinkGetter<TestAssetType>(Path.Combine("Meshes", "SomeSubFolder", "SomeModel.nif"));
IAssetLinkGetter link = new AssetLinkGetter<TestAssetType>(Path.Combine("Meshes", "SomeSubFolder", "SomeModel.nif"));
var otherLink = new AssetLink<TestAssetType>(Path.Combine("Meshes", "SomeSubFolder", "SomeModel.nif"));

Equals(link, otherLink).Should().BeTrue();
Expand All @@ -127,7 +127,7 @@ public void TestObjectEqualsGetterSetter()
[Fact]
public void TestObjectEqualsSetterGetter()
{
var link = new AssetLink<TestAssetType>(Path.Combine("Meshes", "SomeSubFolder", "SomeModel.nif"));
IAssetLinkGetter link = new AssetLink<TestAssetType>(Path.Combine("Meshes", "SomeSubFolder", "SomeModel.nif"));
var otherLink = new AssetLinkGetter<TestAssetType>(Path.Combine("Meshes", "SomeSubFolder", "SomeModel.nif"));

Equals(link, otherLink).Should().BeTrue();
Expand All @@ -136,7 +136,7 @@ public void TestObjectEqualsSetterGetter()
[Fact]
public void TestObjectEqualsSetterSetter()
{
var link = new AssetLink<TestAssetType>(Path.Combine("Meshes", "SomeSubFolder", "SomeModel.nif"));
IAssetLinkGetter link = new AssetLink<TestAssetType>(Path.Combine("Meshes", "SomeSubFolder", "SomeModel.nif"));
var otherLink = new AssetLink<TestAssetType>(Path.Combine("Meshes", "SomeSubFolder", "SomeModel.nif"));

Equals(link, otherLink).Should().BeTrue();
Expand All @@ -145,7 +145,7 @@ public void TestObjectEqualsSetterSetter()
[Fact]
public void TestEqualsGetterGetter()
{
var link = new AssetLinkGetter<TestAssetType>(Path.Combine("Meshes", "SomeSubFolder", "SomeModel.nif"));
IAssetLinkGetter link = new AssetLinkGetter<TestAssetType>(Path.Combine("Meshes", "SomeSubFolder", "SomeModel.nif"));
var otherLink = new AssetLinkGetter<TestAssetType>(Path.Combine("Meshes", "SomeSubFolder", "SomeModel.nif"));

link.Equals(otherLink).Should().BeTrue();
Expand All @@ -154,7 +154,7 @@ public void TestEqualsGetterGetter()
[Fact]
public void TestEqualsGetterSetter()
{
var link = new AssetLinkGetter<TestAssetType>(Path.Combine("Meshes", "SomeSubFolder", "SomeModel.nif"));
IAssetLinkGetter link = new AssetLinkGetter<TestAssetType>(Path.Combine("Meshes", "SomeSubFolder", "SomeModel.nif"));
var otherLink = new AssetLink<TestAssetType>(Path.Combine("Meshes", "SomeSubFolder", "SomeModel.nif"));

link.Equals(otherLink).Should().BeTrue();
Expand All @@ -163,7 +163,7 @@ public void TestEqualsGetterSetter()
[Fact]
public void TestEqualsSetterGetter()
{
var link = new AssetLink<TestAssetType>(Path.Combine("Meshes", "SomeSubFolder", "SomeModel.nif"));
IAssetLinkGetter link = new AssetLink<TestAssetType>(Path.Combine("Meshes", "SomeSubFolder", "SomeModel.nif"));
var otherLink = new AssetLinkGetter<TestAssetType>(Path.Combine("Meshes", "SomeSubFolder", "SomeModel.nif"));

link.Equals(otherLink).Should().BeTrue();
Expand All @@ -172,7 +172,7 @@ public void TestEqualsSetterGetter()
[Fact]
public void TestEqualsSetterSetter()
{
var link = new AssetLink<TestAssetType>(Path.Combine("Meshes", "SomeSubFolder", "SomeModel.nif"));
IAssetLinkGetter link = new AssetLink<TestAssetType>(Path.Combine("Meshes", "SomeSubFolder", "SomeModel.nif"));
var otherLink = new AssetLink<TestAssetType>(Path.Combine("Meshes", "SomeSubFolder", "SomeModel.nif"));

link.Equals(otherLink).Should().BeTrue();
Expand Down
42 changes: 25 additions & 17 deletions Mutagen.Bethesda.Core/Plugins/Assets/AssetLink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ public class AssetLinkGetter<TAssetType> :
{
protected DataRelativePath _DataRelativePath;
protected string _givenPath;
protected static readonly IAssetType AssetInstance;
protected static readonly TAssetType AssetInstance;
public static readonly AssetLinkGetter<TAssetType> Null = new();

public IAssetType AssetTypeInstance => AssetInstance;

static AssetLinkGetter()
{
AssetInstance = TAssetType.Instance;
AssetInstance = (TAssetType)TAssetType.Instance;
}

public AssetLinkGetter()
Expand Down Expand Up @@ -177,6 +179,8 @@ public class AssetLink<TAssetType> :
IAssetLink<TAssetType>
where TAssetType : class, IAssetType
{
TAssetType IAssetLink<TAssetType>.AssetTypeInstance => AssetInstance;

public AssetLink()
: base(DataRelativePath.NullPath)
{
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -321,3 +310,22 @@ public int CompareTo(AssetLink<TAssetType>? other)
[return: NotNullIfNotNull("path")]
public static implicit operator AssetLink<TAssetType>?(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);
}
}
}
15 changes: 14 additions & 1 deletion Mutagen.Bethesda.Core/Plugins/Assets/IAssetLink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ namespace Mutagen.Bethesda.Plugins.Assets;

public interface IAssetLinkGetter
{
IAssetType AssetTypeInstance { get; }

/// <summary>
/// Original given path to the Asset Link
/// </summary>
Expand Down Expand Up @@ -47,6 +49,8 @@ public interface IAssetLinkGetter<out TAssetType> : IAssetLinkGetter
public interface IAssetLink<out TAssetType> : IAssetLink<IAssetLink<TAssetType>, TAssetType>
where TAssetType : IAssetType
{
new TAssetType AssetTypeInstance { get; }

/// <summary>
/// Raw path pointing to the asset
/// </summary>
Expand All @@ -70,9 +74,18 @@ public interface IAssetLink<out TLinkType, out TAssetType> :
public interface IAssetLink : IAssetLinkGetter
{
/// <summary>
/// Set the path to a path that is relative to the game's Data directory
/// Attempts to set the path of the AssetLink. <br />
/// Will back out and return false if path does not align with expected folder structure.
/// </summary>
/// <returns>True if path matched expected patterns. False if it did not align with expected folder structure</returns>
bool TrySetPath(DataRelativePath? path);

/// <summary>
/// Attempts to set the path of the AssetLink. <br />
/// Will back out and return false if path does not align with expected folder structure.
/// </summary>
/// <returns>True if path matched expected patterns. False if it did not align with expected folder structure</returns>
bool TrySetPath(string? path);

/// <summary>
/// Raw path pointing to the asset
Expand Down

0 comments on commit 44eae43

Please sign in to comment.