using System;
using System.IO;
using StardewModdingAPI.Events;
#if SMAPI_DEPRECATED
using StardewModdingAPI.Framework.Deprecations;
#endif
using StardewModdingAPI.Framework.Input;
namespace StardewModdingAPI.Framework.ModHelpers
{
/// Provides simplified APIs for writing mods.
internal class ModHelper : BaseHelper, IModHelper, IDisposable
{
#if SMAPI_DEPRECATED
/*********
** Fields
*********/
/// The backing field for .
[Obsolete("This only exists to support legacy code and will be removed in SMAPI 4.0.0.")]
private readonly ContentHelper ContentImpl;
#endif
/*********
** Accessors
*********/
///
public string DirectoryPath { get; }
///
public IModEvents Events { get; }
#if SMAPI_DEPRECATED
///
[Obsolete($"Use {nameof(IGameContentHelper)} or {nameof(IModContentHelper)} instead.")]
public IContentHelper Content
{
get
{
SCore.DeprecationManager.Warn(
source: this.Mod,
nounPhrase: $"{nameof(IModHelper)}.{nameof(IModHelper.Content)}",
version: "3.14.0",
severity: DeprecationLevel.Info
);
return this.ContentImpl;
}
}
#endif
///
public IGameContentHelper GameContent { get; }
///
public IModContentHelper ModContent { get; }
///
public IContentPackHelper ContentPacks { get; }
///
public IDataHelper Data { get; }
///
public IInputHelper Input { get; }
///
public IReflectionHelper Reflection { get; }
///
public IModRegistry ModRegistry { get; }
///
public ICommandHelper ConsoleCommands { get; }
///
public IMultiplayerHelper Multiplayer { get; }
///
public ITranslationHelper Translation { get; }
/*********
** Public methods
*********/
/// Construct an instance.
/// The mod using this instance.
/// The full path to the mod's folder.
/// Manages the game's input state for the current player instance. That may not be the main player in split-screen mode.
/// Manages access to events raised by SMAPI.
/// An API for loading content assets.
/// An API for loading content assets from the game's Content folder or via .
/// An API for loading content assets from your mod's files.
/// An API for managing content packs.
/// An API for managing console commands.
/// An API for reading and writing persistent mod data.
/// an API for fetching metadata about loaded mods.
/// An API for accessing private game code.
/// Provides multiplayer utilities.
/// An API for reading translations stored in the mod's i18n folder.
/// An argument is null or empty.
/// The path does not exist on disk.
public ModHelper(
IModMetadata mod, string modDirectory, Func currentInputState, IModEvents events,
#if SMAPI_DEPRECATED
ContentHelper contentHelper,
#endif
IGameContentHelper gameContentHelper, IModContentHelper modContentHelper, IContentPackHelper contentPackHelper, ICommandHelper commandHelper, IDataHelper dataHelper, IModRegistry modRegistry, IReflectionHelper reflectionHelper, IMultiplayerHelper multiplayer, ITranslationHelper translationHelper
)
: base(mod)
{
// validate directory
if (string.IsNullOrWhiteSpace(modDirectory))
throw new ArgumentNullException(nameof(modDirectory));
if (!Directory.Exists(modDirectory))
throw new InvalidOperationException("The specified mod directory does not exist.");
// initialize
this.DirectoryPath = modDirectory;
#if SMAPI_DEPRECATED
this.ContentImpl = contentHelper ?? throw new ArgumentNullException(nameof(contentHelper));
#endif
this.GameContent = gameContentHelper ?? throw new ArgumentNullException(nameof(gameContentHelper));
this.ModContent = modContentHelper ?? throw new ArgumentNullException(nameof(modContentHelper));
this.ContentPacks = contentPackHelper ?? throw new ArgumentNullException(nameof(contentPackHelper));
this.Data = dataHelper ?? throw new ArgumentNullException(nameof(dataHelper));
this.Input = new InputHelper(mod, currentInputState);
this.ModRegistry = modRegistry ?? throw new ArgumentNullException(nameof(modRegistry));
this.ConsoleCommands = commandHelper ?? throw new ArgumentNullException(nameof(commandHelper));
this.Reflection = reflectionHelper ?? throw new ArgumentNullException(nameof(reflectionHelper));
this.Multiplayer = multiplayer ?? throw new ArgumentNullException(nameof(multiplayer));
this.Translation = translationHelper ?? throw new ArgumentNullException(nameof(translationHelper));
this.Events = events;
}
#if SMAPI_DEPRECATED
/// Get the underlying instance for .
[Obsolete("This only exists to support legacy code and will be removed in SMAPI 4.0.0.")]
public ContentHelper GetLegacyContentHelper()
{
return this.ContentImpl;
}
#endif
/****
** Mod config file
****/
///
public TConfig ReadConfig()
where TConfig : class, new()
{
TConfig config = this.Data.ReadJsonFile("config.json") ?? new TConfig();
this.WriteConfig(config); // create file or fill in missing fields
return config;
}
///
public void WriteConfig(TConfig config)
where TConfig : class, new()
{
this.Data.WriteJsonFile("config.json", config);
}
/****
** Disposal
****/
///
public void Dispose()
{
// nothing to dispose yet
}
}
}