From eb39f3d5ea28f004ab352113d869ae6d7772bcd5 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Wed, 16 Nov 2016 17:52:49 -0500 Subject: use interface for IModHelper --- src/StardewModdingAPI/Advanced/ConfigFile.cs | 2 +- src/StardewModdingAPI/Advanced/IConfigFile.cs | 2 +- .../Framework/DeprecationManager.cs | 5 ++- src/StardewModdingAPI/IModHelper.cs | 43 ++++++++++++++++++++++ src/StardewModdingAPI/Mod.cs | 15 +++++--- src/StardewModdingAPI/ModHelper.cs | 3 +- src/StardewModdingAPI/Program.cs | 9 +++-- src/StardewModdingAPI/StardewModdingAPI.csproj | 1 + 8 files changed, 67 insertions(+), 13 deletions(-) create mode 100644 src/StardewModdingAPI/IModHelper.cs (limited to 'src') diff --git a/src/StardewModdingAPI/Advanced/ConfigFile.cs b/src/StardewModdingAPI/Advanced/ConfigFile.cs index 000eaf4c..1aba2f2c 100644 --- a/src/StardewModdingAPI/Advanced/ConfigFile.cs +++ b/src/StardewModdingAPI/Advanced/ConfigFile.cs @@ -10,7 +10,7 @@ namespace StardewModdingAPI.Advanced ** Accessors *********/ /// Provides methods for interacting with the mod directory, including read/writing the config file. - public ModHelper ModHelper { get; set; } + public IModHelper ModHelper { get; set; } /// The file path from which the model was loaded, relative to the mod directory. public string FilePath { get; set; } diff --git a/src/StardewModdingAPI/Advanced/IConfigFile.cs b/src/StardewModdingAPI/Advanced/IConfigFile.cs index 78fd44a6..841f4c58 100644 --- a/src/StardewModdingAPI/Advanced/IConfigFile.cs +++ b/src/StardewModdingAPI/Advanced/IConfigFile.cs @@ -7,7 +7,7 @@ ** Accessors *********/ /// Provides methods for interacting with the mod directory, including read/writing the config file. - ModHelper ModHelper { get; set; } + IModHelper ModHelper { get; set; } /// The file path from which the model was loaded, relative to the mod directory. string FilePath { get; set; } diff --git a/src/StardewModdingAPI/Framework/DeprecationManager.cs b/src/StardewModdingAPI/Framework/DeprecationManager.cs index 4de92bca..044c2a40 100644 --- a/src/StardewModdingAPI/Framework/DeprecationManager.cs +++ b/src/StardewModdingAPI/Framework/DeprecationManager.cs @@ -112,9 +112,10 @@ namespace StardewModdingAPI.Framework /// The type to check. /// The base type which declares the virtual method. /// The method name. - public bool IsVirtualMethodImplemented(Type subtype, Type baseType, string name) + /// The expected argument types. + internal bool IsVirtualMethodImplemented(Type subtype, Type baseType, string name, Type[] argumentTypes) { - MethodInfo method = subtype.GetMethod(nameof(Mod.Entry), new[] { typeof(object[]) }); + MethodInfo method = subtype.GetMethod(nameof(Mod.Entry), argumentTypes); return method.DeclaringType != baseType; } } diff --git a/src/StardewModdingAPI/IModHelper.cs b/src/StardewModdingAPI/IModHelper.cs new file mode 100644 index 00000000..1af7df6b --- /dev/null +++ b/src/StardewModdingAPI/IModHelper.cs @@ -0,0 +1,43 @@ +namespace StardewModdingAPI +{ + /// Provides methods for interacting with a mod directory. + public interface IModHelper + { + /********* + ** Accessors + *********/ + /// The mod directory path. + string DirectoryPath { get; } + + + /********* + ** Public methods + *********/ + /**** + ** Mod config file + ****/ + /// Read the mod's configuration file (and create it if needed). + /// The config class type. This should be a plain class that has public properties for the settings you want. These can be complex types. + TConfig ReadConfig() where TConfig : class, new(); + + /// Save to the mod's configuration file. + /// The config class type. + /// The config settings to save. + void WriteConfig(TConfig config) where TConfig : class, new(); + + /**** + ** Generic JSON files + ****/ + /// Read a JSON file. + /// The model type. + /// The file path relative to the mod directory. + /// Returns the deserialised model, or null if the file doesn't exist or is empty. + TModel ReadJsonFile(string path) where TModel : class; + + /// Save to a JSON file. + /// The model type. + /// The file path relative to the mod directory. + /// The model to save. + void WriteJsonFile(string path, TModel model) where TModel : class; + } +} \ No newline at end of file diff --git a/src/StardewModdingAPI/Mod.cs b/src/StardewModdingAPI/Mod.cs index e3011a19..c2a755b0 100644 --- a/src/StardewModdingAPI/Mod.cs +++ b/src/StardewModdingAPI/Mod.cs @@ -17,7 +17,7 @@ namespace StardewModdingAPI ** Accessors *********/ /// Provides methods for interacting with the mod directory, such as read/writing a config file or custom JSON files. - public ModHelper Helper { get; internal set; } + public IModHelper Helper { get; internal set; } /// Writes messages to the console and log file. public IMonitor Monitor { get; internal set; } @@ -26,7 +26,7 @@ namespace StardewModdingAPI public Manifest Manifest { get; internal set; } /// The full path to the mod's directory on the disk. - [Obsolete("Use " + nameof(Mod.Helper) + "." + nameof(ModHelper.DirectoryPath) + " instead")] + [Obsolete("Use " + nameof(Mod.Helper) + "." + nameof(IModHelper.DirectoryPath) + " instead")] public string PathOnDisk { get @@ -38,7 +38,7 @@ namespace StardewModdingAPI } /// The full path to the mod's config.json file on the disk. - [Obsolete("Use " + nameof(Mod.Helper) + "." + nameof(ModHelper.ReadConfig) + " instead")] + [Obsolete("Use " + nameof(Mod.Helper) + "." + nameof(IModHelper.ReadConfig) + " instead")] public string BaseConfigPath { get @@ -50,11 +50,11 @@ namespace StardewModdingAPI } /// The full path to the per-save configs folder (if is true). - [Obsolete("Use " + nameof(Mod.Helper) + "." + nameof(ModHelper.ReadJsonFile) + " instead")] + [Obsolete("Use " + nameof(Mod.Helper) + "." + nameof(IModHelper.ReadJsonFile) + " instead")] public string PerSaveConfigFolder => this.GetPerSaveConfigFolder(); /// The full path to the per-save configuration file for the current save (if is true). - [Obsolete("Use " + nameof(Mod.Helper) + "." + nameof(ModHelper.ReadJsonFile) + " instead")] + [Obsolete("Use " + nameof(Mod.Helper) + "." + nameof(IModHelper.ReadJsonFile) + " instead")] public string PerSaveConfigPath { get @@ -75,8 +75,13 @@ namespace StardewModdingAPI /// The entry point for your mod. It will always be called once when the mod loads. /// Provides methods for interacting with the mod directory, such as read/writing a config file or custom JSON files. + [Obsolete("This overload is obsolete since SMAPI 1.1.")] public virtual void Entry(ModHelper helper) { } + /// The entry point for your mod. It will always be called once when the mod loads. + /// Provides methods for interacting with the mod directory, such as read/writing a config file or custom JSON files. + public virtual void Entry(IModHelper helper) { } + /********* ** Private methods diff --git a/src/StardewModdingAPI/ModHelper.cs b/src/StardewModdingAPI/ModHelper.cs index 52cabff0..29ae1280 100644 --- a/src/StardewModdingAPI/ModHelper.cs +++ b/src/StardewModdingAPI/ModHelper.cs @@ -6,7 +6,8 @@ using StardewModdingAPI.Advanced; namespace StardewModdingAPI { /// Provides methods for interacting with a mod directory. - public class ModHelper + [Obsolete("Use " + nameof(IModHelper) + "instead.")] + public class ModHelper : IModHelper { /********* ** Accessors diff --git a/src/StardewModdingAPI/Program.cs b/src/StardewModdingAPI/Program.cs index 962a30f8..e60fb6b2 100644 --- a/src/StardewModdingAPI/Program.cs +++ b/src/StardewModdingAPI/Program.cs @@ -404,12 +404,15 @@ namespace StardewModdingAPI modEntry.PathOnDisk = directory; Program.Monitor.Log($"Loaded mod: {modEntry.Manifest.Name} by {modEntry.Manifest.Author}, v{modEntry.Manifest.Version} | {modEntry.Manifest.Description}", LogLevel.Info); Program.ModsLoaded += 1; - modEntry.Entry(); // deprecated - modEntry.Entry(modEntry.Helper); + modEntry.Entry(); // deprecated since 1.0 + modEntry.Entry((ModHelper)modEntry.Helper); // deprecated since 1.1 + modEntry.Entry(modEntry.Helper); // deprecated since 1.1 // raise deprecation warning for old Entry() method - if (Program.DeprecationManager.IsVirtualMethodImplemented(modEntryType, typeof(Mod), nameof(Mod.Entry))) + if (Program.DeprecationManager.IsVirtualMethodImplemented(modEntryType, typeof(Mod), nameof(Mod.Entry), new[] { typeof(object[]) })) Program.DeprecationManager.Warn(manifest.Name, $"an old version of {nameof(Mod)}.{nameof(Mod.Entry)}", "1.0", DeprecationLevel.Notice); + if (Program.DeprecationManager.IsVirtualMethodImplemented(modEntryType, typeof(Mod), nameof(Mod.Entry), new[] { typeof(ModHelper) })) + Program.DeprecationManager.Warn(manifest.Name, $"an old version of {nameof(Mod)}.{nameof(Mod.Entry)}", "1.1", DeprecationLevel.Notice); } } else diff --git a/src/StardewModdingAPI/StardewModdingAPI.csproj b/src/StardewModdingAPI/StardewModdingAPI.csproj index 2d6a7c7f..475d8893 100644 --- a/src/StardewModdingAPI/StardewModdingAPI.csproj +++ b/src/StardewModdingAPI/StardewModdingAPI.csproj @@ -195,6 +195,7 @@ + -- cgit