summaryrefslogtreecommitdiff
path: root/src/SMAPI/Framework
diff options
context:
space:
mode:
Diffstat (limited to 'src/SMAPI/Framework')
-rw-r--r--src/SMAPI/Framework/ContentManagers/ModContentManager.cs10
-rw-r--r--src/SMAPI/Framework/ModLoading/ModResolver.cs13
-rw-r--r--src/SMAPI/Framework/SCore.cs12
-rw-r--r--src/SMAPI/Framework/SModHooks.cs18
4 files changed, 30 insertions, 23 deletions
diff --git a/src/SMAPI/Framework/ContentManagers/ModContentManager.cs b/src/SMAPI/Framework/ContentManagers/ModContentManager.cs
index badbd766..2c068784 100644
--- a/src/SMAPI/Framework/ContentManagers/ModContentManager.cs
+++ b/src/SMAPI/Framework/ContentManagers/ModContentManager.cs
@@ -130,7 +130,7 @@ namespace StardewModdingAPI.Framework.ContentManagers
".png" => this.LoadImageFile<T>(assetName, file),
".tbin" or ".tmx" => this.LoadMapFile<T>(assetName, file),
".xnb" => this.LoadXnbFile<T>(assetName),
- _ => this.HandleUnknownFileType<T>(assetName, file)
+ _ => (T)this.HandleUnknownFileType(assetName, file, typeof(T))
};
}
catch (Exception ex)
@@ -323,13 +323,15 @@ namespace StardewModdingAPI.Framework.ContentManagers
}
/// <summary>Handle a request to load a file type that isn't supported by SMAPI.</summary>
- /// <typeparam name="T">The expected file type.</typeparam>
/// <param name="assetName">The asset name relative to the loader root directory.</param>
/// <param name="file">The file to load.</param>
- private T HandleUnknownFileType<T>(IAssetName assetName, FileInfo file)
+ /// <param name="assetType">The expected file type.</param>
+ private object HandleUnknownFileType(IAssetName assetName, FileInfo file, Type assetType)
{
this.ThrowLoadError(assetName, ContentLoadErrorType.InvalidName, $"unknown file extension '{file.Extension}'; must be one of '.fnt', '.json', '.png', '.tbin', '.tmx', or '.xnb'.");
- return default;
+ return assetType.IsValueType
+ ? Activator.CreateInstance(assetType)
+ : null;
}
/// <summary>Assert that the asset type is compatible with one of the allowed types.</summary>
diff --git a/src/SMAPI/Framework/ModLoading/ModResolver.cs b/src/SMAPI/Framework/ModLoading/ModResolver.cs
index cb62e16f..607bb70d 100644
--- a/src/SMAPI/Framework/ModLoading/ModResolver.cs
+++ b/src/SMAPI/Framework/ModLoading/ModResolver.cs
@@ -180,13 +180,16 @@ namespace StardewModdingAPI.Framework.ModLoading
return mods
.OrderBy(mod =>
{
- string id = mod.Manifest.UniqueID;
+ string? id = mod.Manifest?.UniqueID;
- if (modIdsToLoadEarly.TryGetValue(id, out string? actualId))
- return -int.MaxValue + Array.IndexOf(earlyArray, actualId);
+ if (id is not null)
+ {
+ if (modIdsToLoadEarly.TryGetValue(id, out string? actualId))
+ return -int.MaxValue + Array.IndexOf(earlyArray, actualId);
- if (modIdsToLoadLate.TryGetValue(id, out actualId))
- return int.MaxValue - Array.IndexOf(lateArray, actualId);
+ if (modIdsToLoadLate.TryGetValue(id, out actualId))
+ return int.MaxValue - Array.IndexOf(lateArray, actualId);
+ }
return 0;
})
diff --git a/src/SMAPI/Framework/SCore.cs b/src/SMAPI/Framework/SCore.cs
index fea0f7d0..3e179ef7 100644
--- a/src/SMAPI/Framework/SCore.cs
+++ b/src/SMAPI/Framework/SCore.cs
@@ -260,7 +260,11 @@ namespace StardewModdingAPI.Framework
monitor: this.Monitor,
reflection: this.Reflection,
eventManager: this.EventManager,
- modHooks: new SModHooks(this.OnNewDayAfterFade, this.Monitor),
+ modHooks: new SModHooks(
+ parent: new ModHooks(),
+ beforeNewDayAfterFade: this.OnNewDayAfterFade,
+ monitor: this.Monitor
+ ),
multiplayer: this.Multiplayer,
exitGameImmediately: this.ExitGameImmediately,
@@ -431,7 +435,7 @@ namespace StardewModdingAPI.Framework
// apply load order customizations
if (this.Settings.ModsToLoadEarly.Any() || this.Settings.ModsToLoadLate.Any())
{
- HashSet<string> installedIds = new HashSet<string>(mods.Select(p => p.Manifest.UniqueID), StringComparer.OrdinalIgnoreCase);
+ HashSet<string> installedIds = new HashSet<string>(mods.Select(p => p.Manifest?.UniqueID).Where(p => p is not null), StringComparer.OrdinalIgnoreCase);
string[] missingEarlyMods = this.Settings.ModsToLoadEarly.Where(id => !installedIds.Contains(id)).OrderBy(p => p, StringComparer.OrdinalIgnoreCase).ToArray();
string[] missingLateMods = this.Settings.ModsToLoadLate.Where(id => !installedIds.Contains(id)).OrderBy(p => p, StringComparer.OrdinalIgnoreCase).ToArray();
@@ -1797,7 +1801,7 @@ namespace StardewModdingAPI.Framework
// call entry method
try
{
- mod.Entry(mod.Helper!);
+ mod.Entry(mod.Helper);
}
catch (Exception ex)
{
@@ -1824,7 +1828,7 @@ namespace StardewModdingAPI.Framework
}
// validate mod doesn't implement both GetApi() and GetApi(mod)
- if (metadata.Api != null && mod.GetType().GetMethod(nameof(Mod.GetApi), new Type[] { typeof(IModInfo) })!.DeclaringType != typeof(Mod))
+ if (metadata.Api != null && mod.GetType().GetMethod(nameof(Mod.GetApi), new[] { typeof(IModInfo) })!.DeclaringType != typeof(Mod))
metadata.LogAsMod($"Mod implements both {nameof(Mod.GetApi)}() and {nameof(Mod.GetApi)}({nameof(IModInfo)}), which isn't allowed. The latter will be ignored.", LogLevel.Error);
}
Context.HeuristicModsRunningCode.TryPop(out _);
diff --git a/src/SMAPI/Framework/SModHooks.cs b/src/SMAPI/Framework/SModHooks.cs
index a7736c8b..ac4f242c 100644
--- a/src/SMAPI/Framework/SModHooks.cs
+++ b/src/SMAPI/Framework/SModHooks.cs
@@ -1,11 +1,12 @@
using System;
using System.Threading.Tasks;
+using StardewModdingAPI.Utilities;
using StardewValley;
namespace StardewModdingAPI.Framework
{
/// <summary>Invokes callbacks for mod hooks provided by the game.</summary>
- internal class SModHooks : ModHooks
+ internal class SModHooks : DelegatingModHooks
{
/*********
** Fields
@@ -21,25 +22,24 @@ namespace StardewModdingAPI.Framework
** Public methods
*********/
/// <summary>Construct an instance.</summary>
+ /// <param name="parent">The underlying hooks to call by default.</param>
/// <param name="beforeNewDayAfterFade">A callback to invoke before <see cref="Game1.newDayAfterFade"/> runs.</param>
/// <param name="monitor">Writes messages to the console.</param>
- public SModHooks(Action beforeNewDayAfterFade, IMonitor monitor)
+ public SModHooks(ModHooks parent, Action beforeNewDayAfterFade, IMonitor monitor)
+ : base(parent)
{
this.BeforeNewDayAfterFade = beforeNewDayAfterFade;
this.Monitor = monitor;
}
- /// <summary>A hook invoked when <see cref="Game1.newDayAfterFade"/> is called.</summary>
- /// <param name="action">The vanilla <see cref="Game1.newDayAfterFade"/> logic.</param>
+ /// <inheritdoc />
public override void OnGame1_NewDayAfterFade(Action action)
{
this.BeforeNewDayAfterFade();
action();
}
- /// <summary>Start an asynchronous task for the game.</summary>
- /// <param name="task">The task to start.</param>
- /// <param name="id">A unique key which identifies the task.</param>
+ /// <inheritdoc />
public override Task StartTask(Task task, string id)
{
this.Monitor.Log($"Synchronizing '{id}' task...");
@@ -48,9 +48,7 @@ namespace StardewModdingAPI.Framework
return task;
}
- /// <summary>Start an asynchronous task for the game.</summary>
- /// <param name="task">The task to start.</param>
- /// <param name="id">A unique key which identifies the task.</param>
+ /// <inheritdoc />
public override Task<T> StartTask<T>(Task<T> task, string id)
{
this.Monitor.Log($"Synchronizing '{id}' task...");