summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--docs/release-notes.md4
-rw-r--r--src/SMAPI.ModBuildConfig.Analyzer.Tests/NetFieldAnalyzerTests.cs2
-rw-r--r--src/SMAPI.Tests.ModApiConsumer/Interfaces/ISimpleApi.cs4
-rw-r--r--src/SMAPI.Tests.ModApiProvider/Framework/SimpleApi.cs15
-rw-r--r--src/SMAPI.Tests.ModApiProvider/SMAPI.Tests.ModApiProvider.csproj4
-rw-r--r--src/SMAPI.Tests/Core/AssetNameTests.cs16
-rw-r--r--src/SMAPI.Tests/Core/InterfaceProxyTests.cs103
-rw-r--r--src/SMAPI.Tests/Core/ModResolverTests.cs60
-rw-r--r--src/SMAPI.Toolkit/Framework/Clients/Wiki/ChangeDescriptor.cs7
-rw-r--r--src/SMAPI.Toolkit/Framework/ModScanning/ModScanner.cs33
-rw-r--r--src/SMAPI.Toolkit/Utilities/CaseInsensitivePathLookup.cs (renamed from src/SMAPI/Utilities/CaseInsensitivePathCache.cs)37
-rw-r--r--src/SMAPI.sln.DotSettings2
-rw-r--r--src/SMAPI/Framework/Content/AssetName.cs16
-rw-r--r--src/SMAPI/Framework/ContentCoordinator.cs19
-rw-r--r--src/SMAPI/Framework/ContentManagers/ModContentManager.cs6
-rw-r--r--src/SMAPI/Framework/ContentPack.cs6
-rw-r--r--src/SMAPI/Framework/ModHelpers/ModContentHelper.cs6
-rw-r--r--src/SMAPI/Framework/ModHelpers/ModRegistryHelper.cs4
-rw-r--r--src/SMAPI/Framework/ModLoading/ModResolver.cs23
-rw-r--r--src/SMAPI/Framework/Models/SConfig.cs10
-rw-r--r--src/SMAPI/Framework/Reflection/IInterfaceProxyFactory.cs17
-rw-r--r--src/SMAPI/Framework/Reflection/InterfaceProxyFactory.cs10
-rw-r--r--src/SMAPI/Framework/Reflection/OriginalInterfaceProxyBuilder.cs118
-rw-r--r--src/SMAPI/Framework/Reflection/OriginalInterfaceProxyFactory.cs57
-rw-r--r--src/SMAPI/Framework/SCore.cs17
-rw-r--r--src/SMAPI/Framework/Translator.cs3
-rw-r--r--src/SMAPI/SMAPI.config.json6
27 files changed, 451 insertions, 154 deletions
diff --git a/docs/release-notes.md b/docs/release-notes.md
index 75c1941e..0c14360b 100644
--- a/docs/release-notes.md
+++ b/docs/release-notes.md
@@ -44,8 +44,8 @@ the C# mod that loads them is updated.
_These include new features not supported by the old API like load conflict resolution, edit priority, and content pack labels. They also support new cases like easily detecting when an asset has changed, and avoid data corruption issues in some edge cases._
* Added [nullable reference type annotations](https://stardewvalleywiki.com/Modding:Migrate_to_SMAPI_4.0#Nullable_reference_type_annotations) for all APIs.
* Added `helper.GameContent` and `helper.ModContent`, which will replace `helper.Content` in SMAPI 4.0.0.
- * Overhauled [mod-provided API](https://stardewvalleywiki.com/Modding:Modder_Guide/APIs/Integrations#Mod-provided_APIs) proxying (thanks to Shockah!).
- _This adds support for many previously unsupported cases: proxied interfaces in return values or input arguments, proxied enums if their values match, generic methods, and more. Existing mod APIs should work fine as-is._
+ * Added optional overhaul of [mod-provided API](https://stardewvalleywiki.com/Modding:Modder_Guide/APIs/Integrations#Mod-provided_APIs) proxying (thanks to Shockah!).
+ _This is experimental (you can enable it via `UsePintail` in `smapi-internal/config.json`). This adds support for many previously unsupported cases: proxied interfaces in return values or input arguments, proxied enums if their values match, generic methods, and more. Existing mod APIs should work fine as-is._
* Mod files loaded through SMAPI APIs (including `helper.Content.Load`) are now case-insensitive, even on Linux.
* Enabled deprecation notices for all deprecated APIs. These will only be shown in `TRACE` logs for at least a month after SMAPI 3.14.0 releases.
* Other improvements:
diff --git a/src/SMAPI.ModBuildConfig.Analyzer.Tests/NetFieldAnalyzerTests.cs b/src/SMAPI.ModBuildConfig.Analyzer.Tests/NetFieldAnalyzerTests.cs
index f11a59d3..a6fa5633 100644
--- a/src/SMAPI.ModBuildConfig.Analyzer.Tests/NetFieldAnalyzerTests.cs
+++ b/src/SMAPI.ModBuildConfig.Analyzer.Tests/NetFieldAnalyzerTests.cs
@@ -87,7 +87,7 @@ namespace SMAPI.ModBuildConfig.Analyzer.Tests
[TestCase("SObject obj = null; if (obj.netRefProperty != null);", 24, "obj.netRefProperty", "NetRef", "object")]
[TestCase("Item item = new Item(); object list = item.netList;", 38, "item.netList", "NetList", "object")] // ↓ NetList field converted to a non-interface type
[TestCase("Item item = new Item(); object list = item.netCollection;", 38, "item.netCollection", "NetCollection", "object")]
- [TestCase("Item item = new Item(); int x = (int)item.netIntField;", 32, "item.netIntField", "NetInt", "int")] // ↓ explicit conversion to invalid type
+ [TestCase("Item item = new Item(); int x = (int)item.netIntField;", 32, "item.netIntField", "NetFieldBase", "int")] // ↓ explicit conversion to invalid type
[TestCase("Item item = new Item(); int x = item.netRefField as object;", 32, "item.netRefField", "NetRef", "object")]
public void AvoidImplicitNetFieldComparisons_RaisesDiagnostic(string codeText, int column, string expression, string fromType, string toType)
{
diff --git a/src/SMAPI.Tests.ModApiConsumer/Interfaces/ISimpleApi.cs b/src/SMAPI.Tests.ModApiConsumer/Interfaces/ISimpleApi.cs
index 7f94e137..c99605e4 100644
--- a/src/SMAPI.Tests.ModApiConsumer/Interfaces/ISimpleApi.cs
+++ b/src/SMAPI.Tests.ModApiConsumer/Interfaces/ISimpleApi.cs
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Reflection;
+using StardewModdingAPI.Utilities;
namespace SMAPI.Tests.ModApiConsumer.Interfaces
{
@@ -69,6 +70,9 @@ namespace SMAPI.Tests.ModApiConsumer.Interfaces
/// <summary>A simple method which returns a lambda.</summary>
Func<string, string> GetLambda(Func<string, string> value);
+ /// <summary>A simple method which returns out parameters.</summary>
+ bool TryGetOutParameter(int inputNumber, out int outNumber, out string outString, out PerScreen<int> outReference, out IDictionary<int, PerScreen<int>> outComplexType);
+
/****
** Inherited members
diff --git a/src/SMAPI.Tests.ModApiProvider/Framework/SimpleApi.cs b/src/SMAPI.Tests.ModApiProvider/Framework/SimpleApi.cs
index e7e1ccef..c8781da5 100644
--- a/src/SMAPI.Tests.ModApiProvider/Framework/SimpleApi.cs
+++ b/src/SMAPI.Tests.ModApiProvider/Framework/SimpleApi.cs
@@ -3,6 +3,7 @@
using System;
using System.Collections.Generic;
using System.Reflection;
+using StardewModdingAPI.Utilities;
namespace SMAPI.Tests.ModApiProvider.Framework
{
@@ -96,6 +97,20 @@ namespace SMAPI.Tests.ModApiProvider.Framework
return value;
}
+ /// <summary>A simple method which returns out parameters.</summary>
+ public bool TryGetOutParameter(int inputNumber, out int outNumber, out string outString, out PerScreen<int> outReference, out IDictionary<int, PerScreen<int>> outComplexType)
+ {
+ outNumber = inputNumber;
+ outString = inputNumber.ToString();
+ outReference = new PerScreen<int>(() => inputNumber);
+ outComplexType = new Dictionary<int, PerScreen<int>>
+ {
+ [inputNumber] = new PerScreen<int>(() => inputNumber)
+ };
+
+ return true;
+ }
+
/*********
** Helper methods
diff --git a/src/SMAPI.Tests.ModApiProvider/SMAPI.Tests.ModApiProvider.csproj b/src/SMAPI.Tests.ModApiProvider/SMAPI.Tests.ModApiProvider.csproj
index 70d5a0ce..7fef4ebd 100644
--- a/src/SMAPI.Tests.ModApiProvider/SMAPI.Tests.ModApiProvider.csproj
+++ b/src/SMAPI.Tests.ModApiProvider/SMAPI.Tests.ModApiProvider.csproj
@@ -4,4 +4,8 @@
</PropertyGroup>
<Import Project="..\..\build\common.targets" />
+
+ <ItemGroup>
+ <ProjectReference Include="..\SMAPI\SMAPI.csproj" />
+ </ItemGroup>
</Project>
diff --git a/src/SMAPI.Tests/Core/AssetNameTests.cs b/src/SMAPI.Tests/Core/AssetNameTests.cs
index a1712726..655e9bae 100644
--- a/src/SMAPI.Tests/Core/AssetNameTests.cs
+++ b/src/SMAPI.Tests/Core/AssetNameTests.cs
@@ -78,9 +78,9 @@ namespace SMAPI.Tests.Core
[TestCase("DATA\\achievements", "data/ACHIEVEMENTS", ExpectedResult = true)]
[TestCase("DATA\\\\achievements", "data////ACHIEVEMENTS", ExpectedResult = true)]
- // whitespace-sensitive
- [TestCase("Data/Achievements", " Data/Achievements ", ExpectedResult = false)]
- [TestCase(" Data/Achievements ", "Data/Achievements", ExpectedResult = false)]
+ // whitespace-insensitive
+ [TestCase("Data/Achievements", " Data/Achievements ", ExpectedResult = true)]
+ [TestCase(" Data/Achievements ", "Data/Achievements", ExpectedResult = true)]
// other is null or whitespace
[TestCase("Data/Achievements", null, ExpectedResult = false)]
@@ -109,7 +109,7 @@ namespace SMAPI.Tests.Core
[TestCase("Data/Achievements", "Data/Achievements", ExpectedResult = true)]
[TestCase("DATA/achievements", "data/ACHIEVEMENTS", ExpectedResult = true)]
[TestCase("DATA\\\\achievements", "data////ACHIEVEMENTS", ExpectedResult = true)]
- [TestCase(" Data/Achievements ", "Data/Achievements", ExpectedResult = false)]
+ [TestCase(" Data/Achievements ", "Data/Achievements", ExpectedResult = true)]
[TestCase("Data/Achievements", " ", ExpectedResult = false)]
// with locale codes
@@ -141,13 +141,13 @@ namespace SMAPI.Tests.Core
[TestCase("DATA\\achievements", "data/ACHIEVEMENTS", ExpectedResult = true)]
[TestCase("DATA\\\\achievements", "data////ACHIEVEMENTS", ExpectedResult = true)]
- // leading-whitespace-sensitive
- [TestCase("Data/Achievements", " Data/Achievements", ExpectedResult = false)]
- [TestCase(" Data/Achievements ", "Data/Achievements", ExpectedResult = false)]
+ // whitespace-insensitive
+ [TestCase("Data/Achievements", " Data/Achievements", ExpectedResult = true)]
+ [TestCase(" Data/Achievements ", "Data/Achievements", ExpectedResult = true)]
+ [TestCase("Data/Achievements", " ", ExpectedResult = true)]
// invalid prefixes
[TestCase("Data/Achievements", null, ExpectedResult = false)]
- [TestCase("Data/Achievements", " ", ExpectedResult = false)]
// with locale codes
[TestCase("Data/Achievements.fr-FR", "Data/Achievements", ExpectedResult = true)]
diff --git a/src/SMAPI.Tests/Core/InterfaceProxyTests.cs b/src/SMAPI.Tests/Core/InterfaceProxyTests.cs
index 0b4919ed..6be97526 100644
--- a/src/SMAPI.Tests/Core/InterfaceProxyTests.cs
+++ b/src/SMAPI.Tests/Core/InterfaceProxyTests.cs
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
+using System.Linq;
using System.Reflection;
using FluentAssertions;
using NUnit.Framework;
@@ -8,6 +9,7 @@ using SMAPI.Tests.ModApiConsumer;
using SMAPI.Tests.ModApiConsumer.Interfaces;
using SMAPI.Tests.ModApiProvider;
using StardewModdingAPI.Framework.Reflection;
+using StardewModdingAPI.Utilities;
namespace SMAPI.Tests.Core
{
@@ -27,6 +29,12 @@ namespace SMAPI.Tests.Core
/// <summary>The random number generator with which to create sample values.</summary>
private readonly Random Random = new();
+ /// <summary>Sample user inputs for season names.</summary>
+ private static readonly IInterfaceProxyFactory[] ProxyFactories = {
+ new InterfaceProxyFactory(),
+ new OriginalInterfaceProxyFactory()
+ };
+
/*********
** Unit tests
@@ -35,8 +43,9 @@ namespace SMAPI.Tests.Core
** Events
****/
/// <summary>Assert that an event field can be proxied correctly.</summary>
+ /// <param name="proxyFactory">The proxy factory to test.</param>
[Test]
- public void CanProxy_EventField()
+ public void CanProxy_EventField([ValueSource(nameof(InterfaceProxyTests.ProxyFactories))] IInterfaceProxyFactory proxyFactory)
{
// arrange
ProviderMod providerMod = new();
@@ -44,7 +53,7 @@ namespace SMAPI.Tests.Core
int expectedValue = this.Random.Next();
// act
- ISimpleApi proxy = this.GetProxy(implementation);
+ ISimpleApi proxy = this.GetProxy(proxyFactory, implementation);
new ApiConsumer().UseEventField(proxy, out Func<(int timesCalled, int lastValue)> getValues);
providerMod.RaiseEvent(expectedValue);
(int timesCalled, int lastValue) = getValues();
@@ -55,8 +64,9 @@ namespace SMAPI.Tests.Core
}
/// <summary>Assert that an event property can be proxied correctly.</summary>
+ /// <param name="proxyFactory">The proxy factory to test.</param>
[Test]
- public void CanProxy_EventProperty()
+ public void CanProxy_EventProperty([ValueSource(nameof(InterfaceProxyTests.ProxyFactories))] IInterfaceProxyFactory proxyFactory)
{
// arrange
ProviderMod providerMod = new();
@@ -64,7 +74,7 @@ namespace SMAPI.Tests.Core
int expectedValue = this.Random.Next();
// act
- ISimpleApi proxy = this.GetProxy(implementation);
+ ISimpleApi proxy = this.GetProxy(proxyFactory, implementation);
new ApiConsumer().UseEventProperty(proxy, out Func<(int timesCalled, int lastValue)> getValues);
providerMod.RaiseEvent(expectedValue);
(int timesCalled, int lastValue) = getValues();
@@ -78,10 +88,10 @@ namespace SMAPI.Tests.Core
** Properties
****/
/// <summary>Assert that properties can be proxied correctly.</summary>
+ /// <param name="proxyFactory">The proxy factory to test.</param>
/// <param name="setVia">Whether to set the properties through the <c>provider mod</c> or <c>proxy interface</c>.</param>
- [TestCase("set via provider mod")]
- [TestCase("set via proxy interface")]
- public void CanProxy_Properties(string setVia)
+ [Test]
+ public void CanProxy_Properties([ValueSource(nameof(InterfaceProxyTests.ProxyFactories))] IInterfaceProxyFactory proxyFactory, [Values("set via provider mod", "set via proxy interface")] string setVia)
{
// arrange
ProviderMod providerMod = new();
@@ -96,7 +106,7 @@ namespace SMAPI.Tests.Core
BindingFlags expectedEnum = BindingFlags.Instance | BindingFlags.Public;
// act
- ISimpleApi proxy = this.GetProxy(implementation);
+ ISimpleApi proxy = this.GetProxy(proxyFactory, implementation);
switch (setVia)
{
case "set via provider mod":
@@ -196,27 +206,29 @@ namespace SMAPI.Tests.Core
}
/// <summary>Assert that a simple method with no return value can be proxied correctly.</summary>
+ /// <param name="proxyFactory">The proxy factory to test.</param>
[Test]
- public void CanProxy_SimpleMethod_Void()
+ public void CanProxy_SimpleMethod_Void([ValueSource(nameof(InterfaceProxyTests.ProxyFactories))] IInterfaceProxyFactory proxyFactory)
{
// arrange
object implementation = new ProviderMod().GetModApi();
// act
- ISimpleApi proxy = this.GetProxy(implementation);
+ ISimpleApi proxy = this.GetProxy(proxyFactory, implementation);
proxy.GetNothing();
}
/// <summary>Assert that a simple int method can be proxied correctly.</summary>
+ /// <param name="proxyFactory">The proxy factory to test.</param>
[Test]
- public void CanProxy_SimpleMethod_Int()
+ public void CanProxy_SimpleMethod_Int([ValueSource(nameof(InterfaceProxyTests.ProxyFactories))] IInterfaceProxyFactory proxyFactory)
{
// arrange
object implementation = new ProviderMod().GetModApi();
int expectedValue = this.Random.Next();
// act
- ISimpleApi proxy = this.GetProxy(implementation);
+ ISimpleApi proxy = this.GetProxy(proxyFactory, implementation);
int actualValue = proxy.GetInt(expectedValue);
// assert
@@ -224,15 +236,16 @@ namespace SMAPI.Tests.Core
}
/// <summary>Assert that a simple object method can be proxied correctly.</summary>
+ /// <param name="proxyFactory">The proxy factory to test.</param>
[Test]
- public void CanProxy_SimpleMethod_Object()
+ public void CanProxy_SimpleMethod_Object([ValueSource(nameof(InterfaceProxyTests.ProxyFactories))] IInterfaceProxyFactory proxyFactory)
{
// arrange
object implementation = new ProviderMod().GetModApi();
object expectedValue = new();
// act
- ISimpleApi proxy = this.GetProxy(implementation);
+ ISimpleApi proxy = this.GetProxy(proxyFactory, implementation);
object actualValue = proxy.GetObject(expectedValue);
// assert
@@ -240,15 +253,16 @@ namespace SMAPI.Tests.Core
}
/// <summary>Assert that a simple list method can be proxied correctly.</summary>
+ /// <param name="proxyFactory">The proxy factory to test.</param>
[Test]
- public void CanProxy_SimpleMethod_List()
+ public void CanProxy_SimpleMethod_List([ValueSource(nameof(InterfaceProxyTests.ProxyFactories))] IInterfaceProxyFactory proxyFactory)
{
// arrange
object implementation = new ProviderMod().GetModApi();
string expectedValue = this.GetRandomString();
// act
- ISimpleApi proxy = this.GetProxy(implementation);
+ ISimpleApi proxy = this.GetProxy(proxyFactory, implementation);
IList<string> actualValue = proxy.GetList(expectedValue);
// assert
@@ -256,15 +270,16 @@ namespace SMAPI.Tests.Core
}
/// <summary>Assert that a simple list with interface method can be proxied correctly.</summary>
+ /// <param name="proxyFactory">The proxy factory to test.</param>
[Test]
- public void CanProxy_SimpleMethod_ListWithInterface()
+ public void CanProxy_SimpleMethod_ListWithInterface([ValueSource(nameof(InterfaceProxyTests.ProxyFactories))] IInterfaceProxyFactory proxyFactory)
{
// arrange
object implementation = new ProviderMod().GetModApi();
string expectedValue = this.GetRandomString();
// act
- ISimpleApi proxy = this.GetProxy(implementation);
+ ISimpleApi proxy = this.GetProxy(proxyFactory, implementation);
IList<string> actualValue = proxy.GetListWithInterface(expectedValue);
// assert
@@ -272,8 +287,9 @@ namespace SMAPI.Tests.Core
}
/// <summary>Assert that a simple method which returns generic types can be proxied correctly.</summary>
+ /// <param name="proxyFactory">The proxy factory to test.</param>
[Test]
- public void CanProxy_SimpleMethod_GenericTypes()
+ public void CanProxy_SimpleMethod_GenericTypes([ValueSource(nameof(InterfaceProxyTests.ProxyFactories))] IInterfaceProxyFactory proxyFactory)
{
// arrange
object implementation = new ProviderMod().GetModApi();
@@ -281,7 +297,7 @@ namespace SMAPI.Tests.Core
string expectedValue = this.GetRandomString();
// act
- ISimpleApi proxy = this.GetProxy(implementation);
+ ISimpleApi proxy = this.GetProxy(proxyFactory, implementation);
IDictionary<string, IList<string>> actualValue = proxy.GetGenerics(expectedKey, expectedValue);
// assert
@@ -292,22 +308,61 @@ namespace SMAPI.Tests.Core
}
/// <summary>Assert that a simple lambda method can be proxied correctly.</summary>
+ /// <param name="proxyFactory">The proxy factory to test.</param>
[Test]
[SuppressMessage("ReSharper", "ConvertToLocalFunction")]
- public void CanProxy_SimpleMethod_Lambda()
+ public void CanProxy_SimpleMethod_Lambda([ValueSource(nameof(InterfaceProxyTests.ProxyFactories))] IInterfaceProxyFactory proxyFactory)
{
// arrange
object implementation = new ProviderMod().GetModApi();
Func<string, string> expectedValue = _ => "test";
// act
- ISimpleApi proxy = this.GetProxy(implementation);
+ ISimpleApi proxy = this.GetProxy(proxyFactory, implementation);
object actualValue = proxy.GetObject(expectedValue);
// assert
actualValue.Should().BeSameAs(expectedValue);
}
+ /// <summary>Assert that a method with out parameters can be proxied correctly.</summary>
+ /// <param name="proxyFactory">The proxy factory to test.</param>
+ [Test]
+ [SuppressMessage("ReSharper", "ConvertToLocalFunction")]
+ public void CanProxy_Method_OutParameters([ValueSource(nameof(InterfaceProxyTests.ProxyFactories))] IInterfaceProxyFactory proxyFactory)
+ {
+ // arrange
+ object implementation = new ProviderMod().GetModApi();
+ const int expectedNumber = 42;
+
+ // act
+ ISimpleApi proxy = this.GetProxy(proxyFactory, implementation);
+ bool result = proxy.TryGetOutParameter(
+ inputNumber: expectedNumber,
+
+ out int outNumber,
+ out string outString,
+ out PerScreen<int> outReference,
+ out IDictionary<int, PerScreen<int>> outComplexType
+ );
+
+ // assert
+ result.Should().BeTrue();
+
+ outNumber.Should().Be(expectedNumber);
+
+ outString.Should().Be(expectedNumber.ToString());
+
+ outReference.Should().NotBeNull();
+ outReference.Value.Should().Be(expectedNumber);
+
+ outComplexType.Should().NotBeNull();
+ outComplexType.Count.Should().Be(1);
+ outComplexType.Keys.First().Should().Be(expectedNumber);
+ outComplexType.Values.First().Should().NotBeNull();
+ outComplexType.Values.First().Value.Should().Be(expectedNumber);
+ }
+
/*********
** Private methods
@@ -335,10 +390,10 @@ namespace SMAPI.Tests.Core
}
/// <summary>Get a proxy API instance.</summary>
+ /// <param name="proxyFactory">The proxy factory to use.</param>
/// <param name="implementation">The underlying API instance.</param>
- private ISimpleApi GetProxy(object implementation)
+ private ISimpleApi GetProxy(IInterfaceProxyFactory proxyFactory, object implementation)
{
- var proxyFactory = new InterfaceProxyFactory();
return proxyFactory.CreateProxy<ISimpleApi>(implementation, this.FromModId, this.ToModId);
}
}
diff --git a/src/SMAPI.Tests/Core/ModResolverTests.cs b/src/SMAPI.Tests/Core/ModResolverTests.cs
index bd621bbf..6b2746f5 100644
--- a/src/SMAPI.Tests/Core/ModResolverTests.cs
+++ b/src/SMAPI.Tests/Core/ModResolverTests.cs
@@ -38,6 +38,9 @@ namespace SMAPI.Tests.Core
// assert
Assert.AreEqual(0, mods.Length, 0, $"Expected to find zero manifests, found {mods.Length} instead.");
+
+ // cleanup
+ Directory.Delete(rootFolder, recursive: true);
}
[Test(Description = "Assert that the resolver correctly returns a failed metadata if there's an empty mod folder.")]
@@ -56,6 +59,9 @@ namespace SMAPI.Tests.Core
Assert.AreEqual(1, mods.Length, 0, $"Expected to find one manifest, found {mods.Length} instead.");
Assert.AreEqual(ModMetadataStatus.Failed, mod!.Status, "The mod metadata was not marked failed.");
Assert.IsNotNull(mod.Error, "The mod metadata did not have an error message set.");
+
+ // cleanup
+ Directory.Delete(rootFolder, recursive: true);
}
[Test(Description = "Assert that the resolver correctly reads manifest data from a randomized file.")]
@@ -115,6 +121,9 @@ namespace SMAPI.Tests.Core
Assert.IsNotNull(mod.Manifest.Dependencies, "The dependencies field should not be null.");
Assert.AreEqual(1, mod.Manifest.Dependencies.Length, "The dependencies field should contain one value.");
Assert.AreEqual(originalDependency[nameof(IManifestDependency.UniqueID)], mod.Manifest.Dependencies[0].UniqueID, "The first dependency's unique ID doesn't match.");
+
+ // cleanup
+ Directory.Delete(rootFolder, recursive: true);
}
/****
@@ -123,7 +132,7 @@ namespace SMAPI.Tests.Core
[Test(Description = "Assert that validation doesn't fail if there are no mods installed.")]
public void ValidateManifests_NoMods_DoesNothing()
{
- new ModResolver().ValidateManifests(Array.Empty<ModMetadata>(), apiVersion: new SemanticVersion("1.0"), getUpdateUrl: _ => null);
+ new ModResolver().ValidateManifests(Array.Empty<ModMetadata>(), apiVersion: new SemanticVersion("1.0"), getUpdateUrl: _ => null, validateFilesExist: false);
}
[Test(Description = "Assert that validation skips manifests that have already failed without calling any other properties.")]
@@ -134,7 +143,7 @@ namespace SMAPI.Tests.Core
mock.Setup(p => p.Status).Returns(ModMetadataStatus.Failed);
// act
- new ModResolver().ValidateManifests(new[] { mock.Object }, apiVersion: new SemanticVersion("1.0"), getUpdateUrl: _ => null);
+ new ModResolver().ValidateManifests(new[] { mock.Object }, apiVersion: new SemanticVersion("1.0"), getUpdateUrl: _ => null, validateFilesExist: false);
// assert
mock.VerifyGet(p => p.Status, Times.Once, "The validation did not check the manifest status.");
@@ -145,13 +154,13 @@ namespace SMAPI.Tests.Core
{
// arrange
Mock<IModMetadata> mock = this.GetMetadata("Mod A", Array.Empty<string>(), allowStatusChange: true);
- this.SetupMetadataForValidation(mock, new ModDataRecordVersionedFields(this.GetModDataRecord())
+ mock.Setup(p => p.DataRecord).Returns(() => new ModDataRecordVersionedFields(this.GetModDataRecord())
{
Status = ModStatus.AssumeBroken
});
// act
- new ModResolver().ValidateManifests(new[] { mock.Object }, apiVersion: new SemanticVersion("1.0"), getUpdateUrl: _ => null);
+ new ModResolver().ValidateManifests(new[] { mock.Object }, apiVersion: new SemanticVersion("1.0"), getUpdateUrl: _ => null, validateFilesExist: false);
// assert
mock.Verify(p => p.SetStatus(ModMetadataStatus.Failed, It.IsAny<ModFailReason>(), It.IsAny<string>(), It.IsAny<string>()), Times.Once, "The validation did not fail the metadata.");
@@ -163,10 +172,9 @@ namespace SMAPI.Tests.Core
// arrange
Mock<IModMetadata> mock = this.GetMetadata("Mod A", Array.Empty<string>(), allowStatusChange: true);
mock.Setup(p => p.Manifest).Returns(this.GetManifest(minimumApiVersion: "1.1"));
- this.SetupMetadataForValidation(mock);
// act
- new ModResolver().ValidateManifests(new[] { mock.Object }, apiVersion: new SemanticVersion("1.0"), getUpdateUrl: _ => null);
+ new ModResolver().ValidateManifests(new[] { mock.Object }, apiVersion: new SemanticVersion("1.0"), getUpdateUrl: _ => null, validateFilesExist: false);
// assert
mock.Verify(p => p.SetStatus(ModMetadataStatus.Failed, It.IsAny<ModFailReason>(), It.IsAny<string>(), It.IsAny<string>()), Times.Once, "The validation did not fail the metadata.");
@@ -176,14 +184,18 @@ namespace SMAPI.Tests.Core
public void ValidateManifests_MissingEntryDLL_Fails()
{
// arrange
- Mock<IModMetadata> mock = this.GetMetadata(this.GetManifest(id: "Mod A", version: "1.0", entryDll: "Missing.dll"), allowStatusChange: true);
- this.SetupMetadataForValidation(mock);
+ string directoryPath = this.GetTempFolderPath();
+ Mock<IModMetadata> mock = this.GetMetadata(this.GetManifest(id: "Mod A", version: "1.0", entryDll: "Missing.dll"), allowStatusChange: true, directoryPath: directoryPath);
+ Directory.CreateDirectory(directoryPath);
// act
new ModResolver().ValidateManifests(new[] { mock.Object }, apiVersion: new SemanticVersion("1.0"), getUpdateUrl: _ => null);
// assert
mock.Verify(p => p.SetStatus(ModMetadataStatus.Failed, It.IsAny<ModFailReason>(), It.IsAny<string>(), It.IsAny<string>()), Times.Once, "The validation did not fail the metadata.");
+
+ // cleanup
+ Directory.Delete(directoryPath);
}
[Test(Description = "Assert that validation fails when multiple mods have the same unique ID.")]
@@ -192,16 +204,13 @@ namespace SMAPI.Tests.Core
// arrange
Mock<IModMetadata> modA = this.GetMetadata("Mod A", Array.Empty<string>(), allowStatusChange: true);
Mock<IModMetadata> modB = this.GetMetadata(this.GetManifest(id: "Mod A", name: "Mod B", version: "1.0"), allowStatusChange: true);
- Mock<IModMetadata> modC = this.GetMetadata("Mod C", Array.Empty<string>(), allowStatusChange: false);
- foreach (Mock<IModMetadata> mod in new[] { modA, modB, modC })
- this.SetupMetadataForValidation(mod);
// act
- new ModResolver().ValidateManifests(new[] { modA.Object, modB.Object }, apiVersion: new SemanticVersion("1.0"), getUpdateUrl: _ => null);
+ new ModResolver().ValidateManifests(new[] { modA.Object, modB.Object }, apiVersion: new SemanticVersion("1.0"), getUpdateUrl: _ => null, validateFilesExist: false);
// assert
- modA.Verify(p => p.SetStatus(ModMetadataStatus.Failed, It.IsAny<ModFailReason>(), It.IsAny<string>(), It.IsAny<string>()), Times.Once, "The validation did not fail the first mod with a unique ID.");
- modB.Verify(p => p.SetStatus(ModMetadataStatus.Failed, It.IsAny<ModFailReason>(), It.IsAny<string>(), It.IsAny<string>()), Times.Once, "The validation did not fail the second mod with a unique ID.");
+ modA.Verify(p => p.SetStatus(ModMetadataStatus.Failed, ModFailReason.Duplicate, It.IsAny<string>(), It.IsAny<string>()), Times.AtLeastOnce, "The validation did not fail the first mod with a unique ID.");
+ modB.Verify(p => p.SetStatus(ModMetadataStatus.Failed, ModFailReason.Duplicate, It.IsAny<string>(), It.IsAny<string>()), Times.AtLeastOnce, "The validation did not fail the second mod with a unique ID.");
}
[Test(Description = "Assert that validation fails when the manifest references a DLL that does not exist.")]
@@ -227,6 +236,9 @@ namespace SMAPI.Tests.Core
// assert
// if Moq doesn't throw a method-not-setup exception, the validation didn't override the status.
+
+ // cleanup
+ Directory.Delete(modFolder, recursive: true);
}
/****
@@ -514,14 +526,20 @@ namespace SMAPI.Tests.Core
/// <summary>Get a randomized basic manifest.</summary>
/// <param name="manifest">The mod manifest.</param>
/// <param name="allowStatusChange">Whether the code being tested is allowed to change the mod status.</param>
- private Mock<IModMetadata> GetMetadata(IManifest manifest, bool allowStatusChange = false)
+ /// <param name="directoryPath">The directory path the mod metadata should be pointed at, or <c>null</c> to generate a fake path.</param>
+ private Mock<IModMetadata> GetMetadata(IManifest manifest, bool allowStatusChange = false, string? directoryPath = null)
{
+ directoryPath ??= this.GetTempFolderPath();
+
Mock<IModMetadata> mod = new(MockBehavior.Strict);
mod.Setup(p => p.DataRecord).Returns(this.GetModDataRecordVersionedFields());
mod.Setup(p => p.Status).Returns(ModMetadataStatus.Found);
mod.Setup(p => p.DisplayName).Returns(manifest.UniqueID);
+ mod.Setup(p => p.DirectoryPath).Returns(directoryPath);
mod.Setup(p => p.Manifest).Returns(manifest);
mod.Setup(p => p.HasID(It.IsAny<string>())).Returns((string id) => manifest.UniqueID == id);
+ mod.Setup(p => p.GetUpdateKeys(It.IsAny<bool>())).Returns(Enumerable.Empty<UpdateKey>());
+ mod.Setup(p => p.GetRelativePathWithRoot()).Returns(directoryPath);
if (allowStatusChange)
{
mod
@@ -532,18 +550,6 @@ namespace SMAPI.Tests.Core
return mod;
}
- /// <summary>Set up a mock mod metadata for <see cref="ModResolver.ValidateManifests"/>.</summary>
- /// <param name="mod">The mock mod metadata.</param>
- /// <param name="modRecord">The extra metadata about the mod from SMAPI's internal data (if any).</param>