using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Runtime.ExceptionServices;
using System.Security;
using System.Text;
using System.Threading;
using Microsoft.Xna.Framework;
#if SMAPI_FOR_WINDOWS
using Microsoft.Win32;
#endif
using Newtonsoft.Json;
using StardewModdingAPI.Enums;
using StardewModdingAPI.Events;
using StardewModdingAPI.Framework.Content;
using StardewModdingAPI.Framework.ContentManagers;
using StardewModdingAPI.Framework.Deprecations;
using StardewModdingAPI.Framework.Events;
using StardewModdingAPI.Framework.Exceptions;
using StardewModdingAPI.Framework.Input;
using StardewModdingAPI.Framework.Logging;
using StardewModdingAPI.Framework.Models;
using StardewModdingAPI.Framework.ModHelpers;
using StardewModdingAPI.Framework.ModLoading;
using StardewModdingAPI.Framework.Networking;
using StardewModdingAPI.Framework.Reflection;
using StardewModdingAPI.Framework.Rendering;
using StardewModdingAPI.Framework.Serialization;
using StardewModdingAPI.Framework.StateTracking.Comparers;
using StardewModdingAPI.Framework.StateTracking.Snapshots;
using StardewModdingAPI.Framework.Utilities;
using StardewModdingAPI.Internal;
using StardewModdingAPI.Internal.Patching;
using StardewModdingAPI.Patches;
using StardewModdingAPI.Toolkit;
using StardewModdingAPI.Toolkit.Framework.Clients.WebApi;
using StardewModdingAPI.Toolkit.Framework.ModData;
using StardewModdingAPI.Toolkit.Serialization;
using StardewModdingAPI.Toolkit.Utilities;
using StardewModdingAPI.Toolkit.Utilities.PathLookups;
using StardewModdingAPI.Utilities;
using StardewValley;
using StardewValley.Menus;
using StardewValley.Objects;
using xTile.Display;
using LanguageCode = StardewValley.LocalizedContentManager.LanguageCode;
using MiniMonoModHotfix = MonoMod.Utils.MiniMonoModHotfix;
using PathUtilities = StardewModdingAPI.Toolkit.Utilities.PathUtilities;
using SObject = StardewValley.Object;
namespace StardewModdingAPI.Framework
{
/// The core class which initializes and manages SMAPI.
internal class SCore : IDisposable
{
/*********
** Fields
*********/
/****
** Low-level components
****/
/// Tracks whether the game should exit immediately and any pending initialization should be cancelled.
private readonly CancellationTokenSource CancellationToken = new();
/// Manages the SMAPI console window and log file.
private readonly LogManager LogManager;
/// The core logger and monitor for SMAPI.
private Monitor Monitor => this.LogManager.Monitor;
/// Simplifies access to private game code.
private readonly Reflector Reflection = new();
/// Encapsulates access to SMAPI core translations.
private readonly Translator Translator = new();
/// The SMAPI configuration settings.
private readonly SConfig Settings;
/// The mod toolkit used for generic mod interactions.
private readonly ModToolkit Toolkit = new();
/****
** Higher-level components
****/
/// Manages console commands.
private readonly CommandManager CommandManager;
/// The underlying game instance.
private SGameRunner Game = null!; // initialized very early
/// SMAPI's content manager.
private ContentCoordinator ContentCore = null!; // initialized very early
/// The game's core multiplayer utility for the main player.
private SMultiplayer Multiplayer = null!; // initialized very early
/// Tracks the installed mods.
/// This is initialized after the game starts.
private readonly ModRegistry ModRegistry = new();
/// Manages SMAPI events for mods.
private readonly EventManager EventManager;
/****
** State
****/
/// The path to search for mods.
private string ModsPath => Constants.ModsPath;
/// Whether the game is currently running.
private bool IsGameRunning;
/// Whether the program has been disposed.
private bool IsDisposed;
/// Whether the next content manager requested by the game will be for .
private bool NextContentManagerIsMain;
/// Whether post-game-startup initialization has been performed.
private bool IsInitialized;
/// Whether the game has initialized for any custom languages from Data/AdditionalLanguages .
private bool AreCustomLanguagesInitialized;
/// Whether the player just returned to the title screen.
public bool JustReturnedToTitle { get; set; }
/// The last language set by the game.
private (string Locale, LanguageCode Code) LastLanguage { get; set; } = ("", LanguageCode.en);
/// The maximum number of consecutive attempts SMAPI should make to recover from an update error.
private readonly Countdown UpdateCrashTimer = new(60); // 60 ticks = roughly one second
/// Asset interceptors added or removed since the last tick.
private readonly List ReloadAssetInterceptorsQueue = new();
/// A list of queued commands to parse and execute.
/// This property must be thread-safe, since it's accessed from a separate console input thread.
private readonly ConcurrentQueue RawCommandQueue = new();
/// A list of commands to execute on each screen.
private readonly PerScreen> ScreenCommandQueue = new(() => new List());
/*********
** Accessors
*********/
/// Manages deprecation warnings.
/// This is initialized after the game starts. This is accessed directly because it's not part of the normal class model.
internal static DeprecationManager DeprecationManager { get; private set; } = null!; // initialized in constructor, which happens before other code can access it
/// The singleton instance.
/// This is only intended for use by external code like the Error Handler mod.
internal static SCore Instance { get; private set; } = null!; // initialized in constructor, which happens before other code can access it
/// The number of game update ticks which have already executed. This is similar to , but incremented more consistently for every tick.
internal static uint TicksElapsed { get; private set; }
/// A specialized form of which is incremented each time SMAPI performs a processing tick (whether that's a game update, one wait cycle while synchronizing code, etc).
internal static uint ProcessTicksElapsed { get; private set; }
/*********
** Public methods
*********/
/// Construct an instance.
/// The path to search for mods.
/// Whether to output log messages to the console.
/// Whether to enable development features, or null to use the value from the settings file.
public SCore(string modsPath, bool writeToConsole, bool? developerMode)
{
SCore.Instance = this;
// init paths
this.VerifyPath(modsPath);
this.VerifyPath(Constants.LogDir);
Constants.ModsPath = modsPath;
// init log file
this.PurgeNormalLogs();
string logPath = this.GetLogPath();
// init basics
this.Settings = JsonConvert.DeserializeObject(File.ReadAllText(Constants.ApiConfigPath));
if (File.Exists(Constants.ApiUserConfigPath))
JsonConvert.PopulateObject(File.ReadAllText(Constants.ApiUserConfigPath), this.Settings);
if (developerMode.HasValue)
this.Settings.OverrideDeveloperMode(developerMode.Value);
this.LogManager = new LogManager(logPath: logPath, colorConfig: this.Settings.ConsoleColors, writeToConsole: writeToConsole, verboseLogging: this.Settings.VerboseLogging, isDeveloperMode: this.Settings.DeveloperMode, getScreenIdForLog: this.GetScreenIdForLog);
this.CommandManager = new CommandManager(this.Monitor);
this.EventManager = new EventManager(this.ModRegistry);
SCore.DeprecationManager = new DeprecationManager(this.Monitor, this.ModRegistry);
SDate.Translations = this.Translator;
// log SMAPI/OS info
this.LogManager.LogIntro(modsPath, this.Settings.GetCustomSettings());
// validate platform
#if SMAPI_FOR_WINDOWS
if (Constants.Platform != Platform.Windows)
{
this.Monitor.Log("Oops! You're running Windows, but this version of SMAPI is for Linux or macOS. Please reinstall SMAPI to fix this.", LogLevel.Error);
this.LogManager.PressAnyKeyToExit();
}
#else
if (Constants.Platform == Platform.Windows)
{
this.Monitor.Log($"Oops! You're running {Constants.Platform}, but this version of SMAPI is for Windows. Please reinstall SMAPI to fix this.", LogLevel.Error);
this.LogManager.PressAnyKeyToExit();
}
#endif
}
/// Launch SMAPI.
[HandleProcessCorruptedStateExceptions, SecurityCritical] // let try..catch handle corrupted state exceptions
public void RunInteractively()
{
// initialize SMAPI
try
{
JsonConverter[] converters = {
new ColorConverter(),
new KeybindConverter(),
new PointConverter(),
new Vector2Converter(),
new RectangleConverter()
};
foreach (JsonConverter converter in converters)
this.Toolkit.JsonHelper.JsonSettings.Converters.Add(converter);
// add error handlers
AppDomain.CurrentDomain.UnhandledException += (_, e) => this.Monitor.Log($"Critical app domain exception: {e.ExceptionObject}", LogLevel.Error);
// add more lenient assembly resolver
AppDomain.CurrentDomain.AssemblyResolve += (_, e) => AssemblyLoader.ResolveAssembly(e.Name);
// hook locale event
LocalizedContentManager.OnLanguageChange += _ => this.OnLocaleChanged();
// override game
this.Multiplayer = new SMultiplayer(this.Monitor, this.EventManager, this.Toolkit.JsonHelper, this.ModRegistry, this.Reflection, this.OnModMessageReceived, this.Settings.LogNetworkTraffic);
SGame.CreateContentManagerImpl = this.CreateContentManager; // must be static since the game accesses it before the SGame constructor is called
this.Game = new SGameRunner(
monitor: this.Monitor,
reflection: this.Reflection,
eventManager: this.EventManager,
modHooks: new SModHooks(this.OnNewDayAfterFade, this.Monitor),
multiplayer: this.Multiplayer,
exitGameImmediately: this.ExitGameImmediately,
onGameContentLoaded: this.OnInstanceContentLoaded,
onGameUpdating: this.OnGameUpdating,
onPlayerInstanceUpdating: this.OnPlayerInstanceUpdating,
onGameExiting: this.OnGameExiting
);
StardewValley.GameRunner.instance = this.Game;
// apply game patches
MiniMonoModHotfix.Apply();
HarmonyPatcher.Apply("SMAPI", this.Monitor,
new Game1Patcher(this.Reflection, this.OnLoadStageChanged),
new TitleMenuPatcher(this.OnLoadStageChanged)
);
// add exit handler
this.CancellationToken.Token.Register(() =>
{
if (this.IsGameRunning)
{
this.LogManager.WriteCrashLog();
this.Game.Exit();
}
});
// set window titles
this.UpdateWindowTitles();
}
catch (Exception ex)
{
this.Monitor.Log($"SMAPI failed to initialize: {ex.GetLogSummary()}", LogLevel.Error);
this.LogManager.PressAnyKeyToExit();
return;
}
// log basic info
this.LogManager.HandleMarkerFiles();
this.LogManager.LogSettingsHeader(this.Settings);
// set window titles
this.UpdateWindowTitles();
// start game
this.Monitor.Log("Waiting for game to launch...", LogLevel.Debug);
try
{
this.IsGameRunning = true;
StardewValley.Program.releaseBuild = true; // game's debug logic interferes with SMAPI opening the game window
this.Game.Run();
}
catch (Exception ex)
{
this.LogManager.LogFatalLaunchError(ex);
this.LogManager.PressAnyKeyToExit();
}
finally
{
try
{
this.Dispose();
}
catch (Exception ex)
{
this.Monitor.Log($"The game ended, but SMAPI wasn't able to dispose correctly. Technical details: {ex}", LogLevel.Error);
}
}
}
/// Get the core logger and monitor on behalf of the game.
/// This method is called using reflection by the ErrorHandler mod to log game errors.
[SuppressMessage("ReSharper", "UnusedMember.Global", Justification = "Used via reflection")]
public IMonitor GetMonitorForGame()
{
return this.LogManager.MonitorForGame;
}
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
[SuppressMessage("ReSharper", "ConstantConditionalAccessQualifier", Justification = "May be disposed before SMAPI is fully initialized.")]
public void Dispose()
{
// skip if already disposed
if (this.IsDisposed)
return;
this.IsDisposed = true;
this.Monitor.Log("Disposing...");
// dispose mod data
foreach (IModMetadata mod in this.ModRegistry.GetAll())
{
try
{
(mod.Mod as IDisposable)?.Dispose();
}
catch (Exception ex)
{
mod.LogAsMod($"Mod failed during disposal: {ex.GetLogSummary()}.", LogLevel.Warn);
}
}
// dispose core components
this.IsGameRunning = false;
this.ContentCore?.Dispose();
this.CancellationToken.Dispose();
this.Game?.Dispose();
this.LogManager.Dispose(); // dispose last to allow for any last-second log messages
// end game (moved from Game1.OnExiting to let us clean up first)
Process.GetCurrentProcess().Kill();
}
/*********
** Private methods
*********/
/// Initialize mods before the first game asset is loaded. At this point the core content managers are loaded (so mods can load their own assets), but the game is mostly uninitialized.
private void InitializeBeforeFirstAssetLoaded()
{
if (this.CancellationToken.IsCancellationRequested)
{
this.Monitor.Log("SMAPI shutting down: aborting initialization.", LogLevel.Warn);
return;
}
// init TMX support
xTile.Format.FormatManager.Instance.RegisterMapFormat(new TMXTile.TMXFormat(Game1.tileSize / Game1.pixelZoom, Game1.tileSize / Game1.pixelZoom, Game1.pixelZoom, Game1.pixelZoom));
// load mod data
ModToolkit toolkit = new();
ModDatabase modDatabase = toolkit.GetModDatabase(Constants.ApiMetadataPath);
// load mods
{
this.Monitor.Log("Loading mod metadata...", LogLevel.Debug);
ModResolver resolver = new();
// log loose files
{
string[] looseFiles = new DirectoryInfo(this.ModsPath).GetFiles().Select(p => p.Name).ToArray();
if (looseFiles.Any())
this.Monitor.Log($" Ignored loose files: {string.Join(", ", looseFiles.OrderBy(p => p, StringComparer.OrdinalIgnoreCase))}");
}
// load manifests
IModMetadata[] mods = resolver.ReadManifests(toolkit, this.ModsPath, modDatabase, useCaseInsensitiveFilePaths: this.Settings.UseCaseInsensitivePaths).ToArray();
// filter out ignored mods
foreach (IModMetadata mod in mods.Where(p => p.IsIgnored))
this.Monitor.Log($" Skipped {mod.GetRelativePathWithRoot()} (folder name starts with a dot).");
mods = mods.Where(p => !p.IsIgnored).ToArray();
// load mods
resolver.ValidateManifests(mods, Constants.ApiVersion, toolkit.GetUpdateUrl, getFileLookup: this.GetFileLookup);
mods = resolver.ProcessDependencies(mods, modDatabase).ToArray();
this.LoadMods(mods, this.Toolkit.JsonHelper, this.ContentCore, modDatabase);
// check for software likely to cause issues
this.CheckForSoftwareConflicts();
// check for updates
this.CheckForUpdatesAsync(mods);
}
// update window titles
this.UpdateWindowTitles();
}
/// Raised after the game finishes initializing.
private void OnGameInitialized()
{
// validate XNB integrity
if (!this.ValidateContentIntegrity())
this.Monitor.Log("SMAPI found problems in your game's content files which are likely to cause errors or crashes. Consider uninstalling XNB mods or reinstalling the game.", LogLevel.Error);
// start SMAPI console
new Thread(
() => this.LogManager.RunConsoleInputLoop(
commandManager: this.CommandManager,
reloadTranslations: this.ReloadTranslations,
handleInput: input => this.RawCommandQueue.Enqueue(input),
continueWhile: () => this.IsGameRunning && !this.CancellationToken.IsCancellationRequested
)
).Start();
}
/// Raised after an instance finishes loading its initial content.
private void OnInstanceContentLoaded()
{
// override map display device
Game1.mapDisplayDevice = new SDisplayDevice(Game1.content, Game1.game1.GraphicsDevice);
// log GPU info
#if SMAPI_FOR_WINDOWS
this.Monitor.Log($"Running on GPU: {Game1.game1.GraphicsDevice?.Adapter?.Description ?? ""}");
#endif
}
/// Raised when the game is updating its state (roughly 60 times per second).
/// A snapshot of the game timing state.
/// Invoke the game's update logic.
private void OnGameUpdating(GameTime gameTime, Action runGameUpdate)
{
try
{
/*********
** Safe queued work
*********/
// print warnings/alerts
SCore.DeprecationManager.PrintQueued();
/*********
** First-tick initialization
*********/
if (!this.IsInitialized)
{
this.IsInitialized = true;
this.OnGameInitialized();
}
/*********
** Special cases
*********/
// Abort if SMAPI is exiting.
if (this.CancellationToken.IsCancellationRequested)
{
this.Monitor.Log("SMAPI shutting down: aborting update.");
return;
}
/*********
** Reload assets when interceptors are added/removed
*********/
if (this.ReloadAssetInterceptorsQueue.Any())
{
// get unique interceptors
AssetInterceptorChange[] interceptors = this.ReloadAssetInterceptorsQueue
.GroupBy(p => p.Instance, new ObjectReferenceComparer())
.Select(p => p.First())
.ToArray();
this.ReloadAssetInterceptorsQueue.Clear();
// log summary
this.Monitor.Log("Invalidating cached assets for new editors & loaders...");
this.Monitor.Log(
" changed: "
+ string.Join(", ",
interceptors
.GroupBy(p => p.Mod)
.OrderBy(p => p.Key.DisplayName)
.Select(modGroup =>
$"{modGroup.Key.DisplayName} ("
+ string.Join(", ", modGroup.GroupBy(p => p.WasAdded).ToDictionary(p => p.Key, p => p.Count()).Select(p => $"{(p.Key ? "added" : "removed")} {p.Value}"))
+ ")"
)
)
+ "."
);
// reload affected assets
this.ContentCore.InvalidateCache(asset => interceptors.Any(p => p.CanIntercept(asset)));
}
/*********
** Parse commands
*********/
while (this.RawCommandQueue.TryDequeue(out string? rawInput))
{
// parse command
string? name;
string[]? args;
Command? command;
int screenId;
try
{
if (!this.CommandManager.TryParse(rawInput, out name, out args, out command, out screenId))
{
this.Monitor.Log("Unknown command; type 'help' for a list of available commands.", LogLevel.Error);
continue;
}
}
catch (Exception ex)
{
this.Monitor.Log($"Failed parsing that command:\n{ex.GetLogSummary()}", LogLevel.Error);
continue;
}
// queue command for screen
this.ScreenCommandQueue.GetValueForScreen(screenId).Add(new(command, name, args));
}
/*********
** Run game update
*********/
runGameUpdate();
/*********
** Reset crash timer
*********/
this.UpdateCrashTimer.Reset();
}
catch (Exception ex)
{
// log error
this.Monitor.Log($"An error occurred in the overridden update loop: {ex.GetLogSummary()}", LogLevel.Error);
// exit if irrecoverable
if (!this.UpdateCrashTimer.Decrement())
this.ExitGameImmediately("The game crashed when updating, and SMAPI was unable to recover the game.");
}
finally
{
SCore.TicksElapsed++;
SCore.ProcessTicksElapsed++;
}
}
/// Raised when the game instance for a local player is updating (once per per player).
/// The game instance being updated.
/// A snapshot of the game timing state.
/// Invoke the game's update logic.
private void OnPlayerInstanceUpdating(SGame instance, GameTime gameTime, Action runUpdate)
{
EventManager events = this.EventManager;
bool verbose = this.Monitor.IsVerbose;
try
{
/*********
** Reapply overrides
*********/
if (this.JustReturnedToTitle)
{
if (Game1.mapDisplayDevice is not SDisplayDevice)
Game1.mapDisplayDevice = this.GetMapDisplayDevice();
this.JustReturnedToTitle = false;
}
/*********
** Execute commands
*********/
if (this.ScreenCommandQueue.Value.Any())
{
var commandQueue = this.ScreenCommandQueue.Value;
foreach ((Command? command, string? name, string[]? args) in commandQueue)
{
try
{
command.Callback.Invoke(name, args);
}
catch (Exception ex)
{
if (command.Mod != null)
command.Mod.LogAsMod($"Mod failed handling that command:\n{ex.GetLogSummary()}", LogLevel.Error);
else
this.Monitor.Log($"Failed handling that command:\n{ex.GetLogSummary()}", LogLevel.Error);
}
}
commandQueue.Clear();
}
/*********
** Update input
*********/
// This should *always* run, even when suppressing mod events, since the game uses
// this too. For example, doing this after mod event suppression would prevent the
// user from doing anything on the overnight shipping screen.
SInputState inputState = instance.Input;
if (this.Game.IsActive)
inputState.TrueUpdate();
/*********
** Special cases
*********/
// Run async tasks synchronously to avoid issues due to mod events triggering
// concurrently with game code.
bool saveParsed = false;
if (Game1.currentLoader != null)
{
this.Monitor.Log("Game loader synchronizing...");
this.Reflection.GetMethod(Game1.game1, "UpdateTitleScreen").Invoke(Game1.currentGameTime); // run game logic to change music on load, etc
// ReSharper disable once ConstantConditionalAccessQualifier -- may become null within the loop
while (Game1.currentLoader?.MoveNext() == true)
{
SCore.ProcessTicksElapsed++;
// raise load stage changed
switch (Game1.currentLoader.Current)
{
case 20 when (!saveParsed && SaveGame.loaded != null):
saveParsed = true;
this.OnLoadStageChanged(LoadStage.SaveParsed);
break;
case 36:
this.OnLoadStageChanged(LoadStage.SaveLoadedBasicInfo);
break;
case 50:
this.OnLoadStageChanged(LoadStage.SaveLoadedLocations);
break;
default:
if (Game1.gameMode == Game1.playingGameMode)
this.OnLoadStageChanged(LoadStage.Preloaded);
break;
}
}
Game1.currentLoader = null;
this.Monitor.Log("Game loader done.");
}
// While a background task is in progress, the game may make changes to the game
// state while mods are running their code. This is risky, because data changes can
// conflict (e.g. collection changed during enumeration errors) and data may change
// unexpectedly from one mod instruction to the next.
//
// Therefore we can just run Game1.Update here without raising any SMAPI events. There's
// a small chance that the task will finish after we defer but before the game checks,
// which means technically events should be raised, but the effects of missing one
// update tick are negligible and not worth the complications of bypassing Game1.Update.
if (Game1.gameMode == Game1.loadingMode)
{
events.UnvalidatedUpdateTicking.RaiseEmpty();
runUpdate();
events.UnvalidatedUpdateTicked.RaiseEmpty();
return;
}
// Raise minimal events while saving.
// While the game is writing to the save file in the background, mods can unexpectedly
// fail since they don't have exclusive access to resources (e.g. collection changed
// during enumeration errors). To avoid problems, events are not invoked while a save
// is in progress. It's safe to raise SaveEvents.BeforeSave as soon as the menu is
// opened (since the save hasn't started yet), but all other events should be suppressed.
if (Context.IsSaving)
{
// raise before-create
if (!Context.IsWorldReady && !instance.IsBetweenCreateEvents)
{
instance.IsBetweenCreateEvents = true;
this.Monitor.Log("Context: before save creation.");
events.SaveCreating.RaiseEmpty();
}
// raise before-save
if (Context.IsWorldReady && !instance.IsBetweenSaveEvents)
{
instance.IsBetweenSaveEvents = true;
this.Monitor.Log("Context: before save.");
events.Saving.RaiseEmpty();
}
// suppress non-save events
events.UnvalidatedUpdateTicking.RaiseEmpty();
runUpdate();
events.UnvalidatedUpdateTicked.RaiseEmpty();
return;
}
/*********
** Update context
*********/
bool wasWorldReady = Context.IsWorldReady;
if ((Context.IsWorldReady && !Context.IsSaveLoaded) || Game1.exitToTitle)
{
Context.IsWorldReady = false;
instance.AfterLoadTimer.Reset();
}
else if (Context.IsSaveLoaded && instance.AfterLoadTimer.Current > 0 && Game1.currentLocation != null)
{
if (Game1.dayOfMonth != 0) // wait until new-game intro finishes (world not fully initialized yet)
instance.AfterLoadTimer.Decrement();
Context.IsWorldReady = instance.AfterLoadTimer.Current == 0;
}
/*********
** Update watchers
** (Watchers need to be updated, checked, and reset in one go so we can detect any changes mods make in event handlers.)
*********/
instance.Watchers.Update();
instance.WatcherSnapshot.Update(instance.Watchers);
instance.Watchers.Reset();
WatcherSnapshot state = instance.WatcherSnapshot;
/*********
** Pre-update events
*********/
{
/*********
** Save created/loaded events
*********/
if (instance.IsBetweenCreateEvents)
{
// raise after-create
instance.IsBetweenCreateEvents = false;
this.Monitor.Log($"Context: after save creation, starting {Game1.currentSeason} {Game1.dayOfMonth} Y{Game1.year}.");
this.OnLoadStageChanged(LoadStage.CreatedSaveFile);
events.SaveCreated.RaiseEmpty();
}
if (instance.IsBetweenSaveEvents)
{
// raise after-save
instance.IsBetweenSaveEvents = false;
this.Monitor.Log($"Context: after save, starting {Game1.currentSeason} {Game1.dayOfMonth} Y{Game1.year}.");
events.Saved.RaiseEmpty();
events.DayStarted.RaiseEmpty();
}
/*********
** Locale changed events
*********/
if (state.Locale.IsChanged)
this.Monitor.Log($"Context: locale set to {state.Locale.New} ({this.ContentCore.GetLocaleCode(state.Locale.New)}).");
/*********
** Load / return-to-title events
*********/
if (wasWorldReady && !Context.IsWorldReady)
this.OnLoadStageChanged(LoadStage.None);
else if (Context.IsWorldReady && Context.LoadStage != LoadStage.Ready)
{
// print context
string context = $"Context: loaded save '{Constants.SaveFolderName}', starting {Game1.currentSeason} {Game1.dayOfMonth} Y{Game1.year}, locale set to {this.ContentCore.GetLocale()}.";
if (Context.IsMultiplayer)
{
int onlineCount = Game1.getOnlineFarmers().Count();
context += $" {(Context.IsMainPlayer ? "Main player" : "Farmhand")} with {onlineCount} {(onlineCount == 1 ? "player" : "players")} online.";
}
else
context += " Single-player.";
this.Monitor.Log(context);
// raise events
this.OnLoadStageChanged(LoadStage.Ready);
events.SaveLoaded.RaiseEmpty();
events.DayStarted.RaiseEmpty();
}
/*********
** Window events
*********/
// Here we depend on the game's viewport instead of listening to the Window.Resize
// event because we need to notify mods after the game handles the resize, so the
// game's metadata (like Game1.viewport) are updated. That's a bit complicated
// since the game adds & removes its own handler on the fly.
if (state.WindowSize.IsChanged)
{
if (verbose)
this.Monitor.Log($"Events: window size changed to {state.WindowSize.New}.");
if (events.WindowResized.HasListeners)
events.WindowResized.Raise(new WindowResizedEventArgs(state.WindowSize.Old, state.WindowSize.New));
}
/*********
** Input events (if window has focus)
*********/
if (this.Game.IsActive)
{
// raise events
bool isChatInput = Game1.IsChatting || (Context.IsMultiplayer && Context.IsWorldReady && Game1.activeClickableMenu == null && Game1.currentMinigame == null && inputState.IsAnyDown(Game1.options.chatButton));
if (!isChatInput)
{
ICursorPosition cursor = instance.Input.CursorPosition;
// raise cursor moved event
if (state.Cursor.IsChanged && events.CursorMoved.HasListeners)
events.CursorMoved.Raise(new CursorMovedEventArgs(state.Cursor.Old!, state.Cursor.New!));
// raise mouse wheel scrolled
if (state.MouseWheelScroll.IsChanged)
{
if (verbose)
this.Monitor.Log($"Events: mouse wheel scrolled to {state.MouseWheelScroll.New}.");
if (events.MouseWheelScrolled.HasListeners)
events.MouseWheelScrolled.Raise(new MouseWheelScrolledEventArgs(cursor, state.MouseWheelScroll.Old, state.MouseWheelScroll.New));
}
// raise input button events
if (inputState.ButtonStates.Count > 0)
{
if (events.ButtonsChanged.HasListeners)
events.ButtonsChanged.Raise(new ButtonsChangedEventArgs(cursor, inputState));
bool raisePressed = events.ButtonPressed.HasListeners;
bool raiseReleased = events.ButtonReleased.HasListeners;
if (verbose || raisePressed || raiseReleased)
{
foreach ((SButton button, SButtonState status) in inputState.ButtonStates)
{
switch (status)
{
case SButtonState.Pressed:
if (verbose)
this.Monitor.Log($"Events: button {button} pressed.");
if (raisePressed)
events.ButtonPressed.Raise(new ButtonPressedEventArgs(button, cursor, inputState));
break;
case SButtonState.Released:
if (verbose)
this.Monitor.Log($"Events: button {button} released.");
if (raiseReleased)
events.ButtonReleased.Raise(new ButtonReleasedEventArgs(button, cursor, inputState));
break;
}
}
}
}
}
}
/*********
** Menu events
*********/
if (state.ActiveMenu.IsChanged)
{
IClickableMenu? was = state.ActiveMenu.Old;
IClickableMenu? now = state.ActiveMenu.New;
if (verbose)
this.Monitor.Log($"Context: menu changed from {was?.GetType().FullName ?? "none"} to {now?.GetType().FullName ?? "none"}.");
// raise menu events
if (events.MenuChanged.HasListeners)
events.MenuChanged.Raise(new MenuChangedEventArgs(was, now));
}
/*********
** World & player events
*********/
if (Context.IsWorldReady)
{
bool raiseWorldEvents = !state.SaveID.IsChanged; // don't report changes from unloaded => loaded
// location list changes
if (state.Locations.LocationList.IsChanged && (events.LocationListChanged.HasListeners || verbose))
{
var added = state.Locations.LocationList.Added.ToArray();
var removed = state.Locations.LocationList.Removed.ToArray();
if (verbose)
{
string addedText = added.Any() ? string.Join(", ", added.Select(p => p.Name)) : "none";
string removedText = removed.Any() ? string.Join(", ", removed.Select(p => p.Name)) : "none";
this.Monitor.Log($"Context: location list changed (added {addedText}; removed {removedText}).");
}
if (events.LocationListChanged.HasListeners)
events.LocationListChanged.Raise(new LocationListChangedEventArgs(added, removed));
}
// raise location contents changed
if (raiseWorldEvents)
{
foreach (LocationSnapshot locState in state.Locations.Locations)
{
GameLocation location = locState.Location;
// buildings changed
if (locState.Buildings.IsChanged && events.BuildingListChanged.HasListeners)
events.BuildingListChanged.Raise(new BuildingListChangedEventArgs(location, locState.Buildings.Added, locState.Buildings.Removed));
// debris changed
if (locState.Debris.IsChanged && events.DebrisListChanged.HasListeners)
events.DebrisListChanged.Raise(new DebrisListChangedEventArgs(location, locState.Debris.Added, locState.Debris.Removed));
// large terrain features changed
if (locState.LargeTerrainFeatures.IsChanged && events.LargeTerrainFeatureListChanged.HasListeners)
events.LargeTerrainFeatureListChanged.Raise(new LargeTerrainFeatureListChangedEventArgs(location, locState.LargeTerrainFeatures.Added, locState.LargeTerrainFeatures.Removed));
// NPCs changed
if (locState.Npcs.IsChanged && events.NpcListChanged.HasListeners)
events.NpcListChanged.Raise(new NpcListChangedEventArgs(location, locState.Npcs.Added, locState.Npcs.Removed));
// objects changed
if (locState.Objects.IsChanged && events.ObjectListChanged.HasListeners)
events.ObjectListChanged.Raise(new ObjectListChangedEventArgs(location, locState.Objects.Added, locState.Objects.Removed));
// chest items changed
if (events.ChestInventoryChanged.HasListeners)
{
foreach ((Chest chest, SnapshotItemListDiff diff) in locState.ChestItems)
events.ChestInventoryChanged.Raise(new ChestInventoryChangedEventArgs(chest, location, added: diff.Added, removed: diff.Removed, quantityChanged: diff.QuantityChanged));
}
// terrain features changed
if (locState.TerrainFeatures.IsChanged && events.TerrainFeatureListChanged.HasListeners)
events.TerrainFeatureListChanged.Raise(new TerrainFeatureListChangedEventArgs(location, locState.TerrainFeatures.Added, locState.TerrainFeatures.Removed));
// furniture changed
if (locState.Furniture.IsChanged && events.FurnitureListChanged.HasListeners)
events.FurnitureListChanged.Raise(new FurnitureListChangedEventArgs(location, locState.Furniture.Added, locState.Furniture.Removed));
}
}
// raise time changed
if (raiseWorldEvents && state.Time.IsChanged)
{
if (verbose)
this.Monitor.Log($"Context: time changed to {state.Time.New}.");
if (events.TimeChanged.HasListeners)
events.TimeChanged.Raise(new TimeChangedEventArgs(state.Time.Old, state.Time.New));
}
// raise player events
if (raiseWorldEvents)
{
PlayerSnapshot playerState = state.CurrentPlayer!; // not null at this point
Farmer player = playerState.Player;
// raise current location changed
if (playerState.Location.IsChanged)
{
if (verbose)
this.Monitor.Log($"Context: set location to {playerState.Location.New}.");
if (events.Warped.HasListeners)
events.Warped.Raise(new WarpedEventArgs(player, playerState.Location.Old!, playerState.Location.New!));
}
// raise player leveled up a skill
bool raiseLevelChanged = events.LevelChanged.HasListeners;
if (verbose || raiseLevelChanged)
{
foreach ((SkillType skill, var value) in playerState.Skills)
{
if (!value.IsChanged)
continue;
if (verbose)
this.Monitor.Log($"Events: player skill '{skill}' changed from {value.Old} to {value.New}.");
if (raiseLevelChanged)
events.LevelChanged.Raise(new LevelChangedEventArgs(player, skill, value.Old, value.New));
}
}
// raise player inventory changed
if (playerState.Inventory.IsChanged)
{
if (verbose)
this.Monitor.Log("Events: player inventory changed.");
if (events.InventoryChanged.HasListeners)
{
SnapshotItemListDiff inventory = playerState.Inventory;
events.InventoryChanged.Raise(new InventoryChangedEventArgs(player, added: inventory.Added, removed: inventory.Removed, quantityChanged: inventory.QuantityChanged));
}
}
}
}
/*********
** Game update
*********/
// game launched (not raised for secondary players in split-screen mode)
if (instance.IsFirstTick && !Context.IsGameLaunched)
{
Context.IsGameLaunched = true;
if (events.GameLaunched.HasListeners)
events.GameLaunched.Raise(new GameLaunchedEventArgs());
}
// preloaded
if (Context.IsSaveLoaded && Context.LoadStage != LoadStage.Loaded && Context.LoadStage != LoadStage.Ready && Game1.dayOfMonth != 0)
this.OnLoadStageChanged(LoadStage.Loaded);
// additional languages initialized
if (!this.AreCustomLanguagesInitialized && TitleMenu.ticksUntilLanguageLoad < 0)
{
this.AreCustomLanguagesInitialized = true;
this.ContentCore.OnAdditionalLanguagesInitialized();
}
}
/*********
** Game update tick
*********/
{
bool isOneSecond = SCore.TicksElapsed % 60 == 0;
events.UnvalidatedUpdateTicking.RaiseEmpty();
events.UpdateTicking.RaiseEmpty();
if (isOneSecond)
events.OneSecondUpdateTicking.RaiseEmpty();
try
{
instance.Input.ApplyOverrides(); // if mods added any new overrides since the update, process them now
runUpdate();
}
catch (Exception ex)
{
this.LogManager.MonitorForGame.Log($"An error occurred in the base update loop: {ex.GetLogSummary()}", LogLevel.Error);
}
events.UnvalidatedUpdateTicked.RaiseEmpty();
events.UpdateTicked.RaiseEmpty();
if (isOneSecond)
events.OneSecondUpdateTicked.RaiseEmpty();
}
/*********
** Update events
*********/
this.UpdateCrashTimer.Reset();
}
catch (Exception ex)
{
// log error
this.Monitor.Log($"An error occurred in the overridden update loop: {ex.GetLogSummary()}", LogLevel.Error);
// exit if irrecoverable
if (!this.UpdateCrashTimer.Decrement())
this.ExitGameImmediately("The game crashed when updating, and SMAPI was unable to recover the game.");
}
}
/// Handle the game changing locale.
private void OnLocaleChanged()
{
this.ContentCore.OnLocaleChanged();
// get locale
string locale = this.ContentCore.GetLocale();
LanguageCode languageCode = this.ContentCore.Language;
// update core translations
this.Translator.SetLocale(locale, languageCode);
// update mod translation helpers
foreach (IModMetadata mod in this.ModRegistry.GetAll())
{
TranslationHelper translations = mod.Translations!; // not null at this point
translations.SetLocale(locale, languageCode);
foreach (ContentPack contentPack in mod.GetFakeContentPacks())
contentPack.TranslationImpl.SetLocale(locale, languageCode);
}
// raise event
if (this.EventManager.LocaleChanged.HasListeners)
{
this.EventManager.LocaleChanged.Raise(
new LocaleChangedEventArgs(
oldLanguage: this.LastLanguage.Code,
oldLocale: this.LastLanguage.Locale,
newLanguage: languageCode,
newLocale: locale
)
);
}
this.LastLanguage = (locale, languageCode);
}
/// Raised when the low-level stage while loading a save changes.
/// The new load stage.
internal void OnLoadStageChanged(LoadStage newStage)
{
// nothing to do
if (newStage == Context.LoadStage)
return;
// update data
LoadStage oldStage = Context.LoadStage;
Context.LoadStage = newStage;
this.Monitor.VerboseLog($"Context: load stage changed to {newStage}");
// handle stages
switch (newStage)
{
case LoadStage.ReturningToTitle:
this.Monitor.Log("Context: returning to title");
this.OnReturningToTitle();
break;
case LoadStage.None:
this.JustReturnedToTitle = true;
break;
case LoadStage.Loaded:
// override chat box
Game1.onScreenMenus.Remove(Game1.chatBox);
Game1.onScreenMenus.Add(Game1.chatBox = new SChatBox(this.LogManager.MonitorForGame));
break;
}
// raise events
EventManager events = this.EventManager;
if (events.LoadStageChanged.HasListeners)
events.LoadStageChanged.Raise(new LoadStageChangedEventArgs(oldStage, newStage));
if (newStage == LoadStage.None)
events.ReturnedToTitle.RaiseEmpty();
}
/// A callback invoked before runs.
protected void OnNewDayAfterFade()
{
this.EventManager.DayEnding.RaiseEmpty();
this.Reflection.NewCacheInterval();
}
/// A callback invoked after an asset is fully loaded through a content manager.
/// The content manager through which the asset was loaded.
/// The asset name that was loaded.
private void OnAssetLoaded(IContentManager contentManager, IAssetName assetName)
{
if (this.EventManager.AssetReady.HasListeners)
this.EventManager.AssetReady.Raise(new AssetReadyEventArgs(assetName, assetName.GetBaseAssetName()));
}
/// A callback invoked after assets have been invalidated from the content cache.
/// The invalidated asset names.
private void OnAssetsInvalidated(IList assetNames)
{
if (this.EventManager.AssetsInvalidated.HasListeners)
this.EventManager.AssetsInvalidated.Raise(new AssetsInvalidatedEventArgs(assetNames, assetNames.Select(p => p.GetBaseAssetName())));
}
/// Get the load/edit operations to apply to an asset by querying registered event handlers.
/// The asset info being requested.
private AssetOperationGroup? RequestAssetOperations(IAssetInfo asset)
{
// get event
var requestedEvent = this.EventManager.AssetRequested;
if (!requestedEvent.HasListeners)
return null;
// raise event
AssetRequestedEventArgs args = new(asset, this.GetOnBehalfOfContentPack);
requestedEvent.Raise(
invoke: (mod, invoke) =>
{
args.SetMod(mod);
invoke(args);
}
);
// collect operations
return args.LoadOperations.Count != 0 || args.EditOperations.Count != 0
? new AssetOperationGroup(args.LoadOperations, args.EditOperations)
: null;
}
/// Get the mod metadata for a content pack whose ID matches , if it's a valid content pack for the given .
/// The mod requesting to act on the content pack's behalf.
/// The content pack ID.
/// The verb phrase indicating what action will be performed, like 'load assets' or 'edit assets'.
/// Returns the content pack metadata if valid, else null .
private IModMetadata? GetOnBehalfOfContentPack(IModMetadata mod, string? id, string verb)
{
if (id == null)
return null;
string errorPrefix = $"Can't {verb} on behalf of content pack ID '{id}'";
// get target mod
IModMetadata? onBehalfOf = this.ModRegistry.Get(id);
if (onBehalfOf == null)
{
mod.LogAsModOnce($"{errorPrefix}: there's no content pack installed with that ID.", LogLevel.Warn);
return null;
}
// make sure it's a content pack for the requesting mod
if (!onBehalfOf.IsContentPack || !string.Equals(onBehalfOf.Manifest.ContentPackFor?.UniqueID, mod.Manifest.UniqueID))
{
mod.LogAsModOnce($"{errorPrefix}: that isn't a content pack for this mod.", LogLevel.Warn);
return null;
}
return onBehalfOf;
}
/// Raised immediately before the player returns to the title screen.
private void OnReturningToTitle()
{
// perform cleanup
this.Multiplayer.CleanupOnMultiplayerExit();
this.ContentCore.OnReturningToTitleScreen();
}
/// Raised before the game exits.
private void OnGameExiting()
{
this.Multiplayer.Disconnect(StardewValley.Multiplayer.DisconnectType.ClosedGame);
this.Dispose();
}
/// Raised when a mod network message is received.
/// The message to deliver to applicable mods.
private void OnModMessageReceived(ModMessageModel message)
{
if (this.EventManager.ModMessageReceived.HasListeners)
{
// get mod IDs to notify
HashSet modIDs = new(message.ToModIDs ?? this.ModRegistry.GetAll().Select(p => p.Manifest.UniqueID), StringComparer.OrdinalIgnoreCase);
if (message.FromPlayerID == Game1.player?.UniqueMultiplayerID)
modIDs.Remove(message.FromModID); // don't send a broadcast back to the sender
// raise events
ModMessageReceivedEventArgs? args = null;
this.EventManager.ModMessageReceived.Raise(
invoke: (mod, invoke) =>
{
if (modIDs.Contains(mod.Manifest.UniqueID))
{
args ??= new(message, this.Toolkit.JsonHelper);
invoke(args);
}
}
);
}
}
/// Constructor a content manager to read game content files.
/// The service provider to use to locate services.
/// The root directory to search for content.
private LocalizedContentManager CreateContentManager(IServiceProvider serviceProvider, string rootDirectory)
{
// Game1._temporaryContent initializing from SGame constructor
// ReSharper disable once ConditionIsAlwaysTrueOrFalse -- this is the method that initializes it
if (this.ContentCore == null)
{
this.ContentCore = new ContentCoordinator(
serviceProvider: serviceProvider,
rootDirectory: rootDirectory,
currentCulture: Thread.CurrentThread.CurrentUICulture,
monitor: this.Monitor,
reflection: this.Reflection,
jsonHelper: this.Toolkit.JsonHelper,
onLoadingFirstAsset: this.InitializeBeforeFirstAssetLoaded,
onAssetLoaded: this.OnAssetLoaded,
onAssetsInvalidated: this.OnAssetsInvalidated,
getFileLookup: this.GetFileLookup,
requestAssetOperations: this.RequestAssetOperations,
useExperimentalImageLoading: this.Settings.UseExperimentalImageLoading
);
if (this.ContentCore.Language != this.Translator.LocaleEnum)
this.Translator.SetLocale(this.ContentCore.GetLocale(), this.ContentCore.Language);
this.NextContentManagerIsMain = true;
return this.ContentCore.CreateGameContentManager("Game1._temporaryContent");
}
// Game1.content initializing from LoadContent
if (this.NextContentManagerIsMain)
{
this.NextContentManagerIsMain = false;
return this.ContentCore.MainContentManager;
}
// any other content manager
return this.ContentCore.CreateGameContentManager("(generated)");
}
/// Get the current game instance. This may not be the main player if playing in split-screen.
private SGame GetCurrentGameInstance()
{
return Game1.game1 as SGame
?? throw new InvalidOperationException("The current game instance wasn't created by SMAPI.");
}
/// Look for common issues with the game's XNB content, and log warnings if anything looks broken or outdated.
/// Returns whether all integrity checks passed.
private bool ValidateContentIntegrity()
{
this.Monitor.Log("Detecting common issues...");
bool issuesFound = false;
// object format (commonly broken by outdated files)
{
// detect issues
bool hasObjectIssues = false;
void LogIssue(int id, string issue) => this.Monitor.Log($@"Detected issue: item #{id} in Content\Data\ObjectInformation.xnb is invalid ({issue}).");
foreach ((int id, string? fieldsStr) in Game1.objectInformation)
{
// must not be empty
if (string.IsNullOrWhiteSpace(fieldsStr))
{
LogIssue(id, "entry is empty");
hasObjectIssues = true;
continue;
}
// require core fields
string[] fields = fieldsStr.Split('/');
if (fields.Length < SObject.objectInfoDescriptionIndex + 1)
{
LogIssue(id, "too few fields for an object");
hasObjectIssues = true;
continue;
}
// check min length for specific types
switch (fields[SObject.objectInfoTypeIndex].Split(new[] { ' ' }, 2)[0])
{
case "Cooking":
if (fields.Length < SObject.objectInfoBuffDurationIndex + 1)
{
LogIssue(id, "too few fields for a cooking item");
hasObjectIssues = true;
}
break;
}
}
// log error
if (hasObjectIssues)
{
issuesFound = true;
this.Monitor.Log(@"Your Content\Data\ObjectInformation.xnb file seems to be broken or outdated.", LogLevel.Warn);
}
}
return !issuesFound;
}
/// Set the titles for the game and console windows.
private void UpdateWindowTitles()
{
string consoleTitle = $"SMAPI {Constants.ApiVersion} - running Stardew Valley {Constants.GameVersion}";
string gameTitle = $"Stardew Valley {Constants.GameVersion} - running SMAPI {Constants.ApiVersion}";
if (this.ModRegistry.AreAllModsLoaded)
{
int modsLoaded = this.ModRegistry.GetAll().Count();
consoleTitle += $" with {modsLoaded} mods";
gameTitle += $" with {modsLoaded} mods";
}
this.Game.Window.Title = gameTitle;
this.LogManager.SetConsoleTitle(consoleTitle);
}
/// Log a warning if software known to cause issues is installed.
private void CheckForSoftwareConflicts()
{
#if SMAPI_FOR_WINDOWS
this.Monitor.Log("Checking for known software conflicts...");
try
{
string[] registryKeys = { @"SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall", @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" };
string[] installedNames = registryKeys
.SelectMany(registryKey =>
{
using RegistryKey? key = Registry.LocalMachine.OpenSubKey(registryKey);
if (key == null)
return Array.Empty();
return key
.GetSubKeyNames()
.Select(subkeyName =>
{
using RegistryKey? subkey = key.OpenSubKey(subkeyName);
string? displayName = (string?)subkey?.GetValue("DisplayName");
string? displayVersion = (string?)subkey?.GetValue("DisplayVersion");
if (displayName != null && displayVersion != null && displayName.EndsWith($" {displayVersion}"))
displayName = displayName.Substring(0, displayName.Length - displayVersion.Length - 1);
return displayName;
})
.ToArray();
})
.Where(name => name != null && (name.Contains("MSI Afterburner") || name.Contains("RivaTuner")))
.Select(name => name!)
.Distinct()
.OrderBy(name => name)
.ToArray();
if (installedNames.Any())
this.Monitor.Log($"Found {string.Join(" and ", installedNames)} installed, which may conflict with SMAPI. If you experience errors or crashes, try disabling that software or adding an exception for SMAPI and Stardew Valley.", LogLevel.Warn);
else
this.Monitor.Log(" None found!");
}
catch (Exception ex)
{
this.Monitor.Log($"Failed when checking for conflicting software. Technical details:\n{ex}");
}
#endif
}
/// Asynchronously check for a new version of SMAPI and any installed mods, and print alerts to the console if an update is available.
/// The mods to include in the update check (if eligible).
private void CheckForUpdatesAsync(IModMetadata[] mods)
{
if (!this.Settings.CheckForUpdates)
return;
new Thread(() =>
{
// create client
string url = this.Settings.WebApiBaseUrl;
WebApiClient client = new(url, Constants.ApiVersion);
this.Monitor.Log("Checking for updates...");
// check SMAPI version
{
ISemanticVersion? updateFound = null;
string? updateUrl = null;
try
{
// fetch update check
ModEntryModel response = client.GetModInfo(new[] { new ModSearchEntryModel("Pathoschild.SMAPI", Constants.ApiVersion, new[] { $"GitHub:{this.Settings.GitHubProjectName}" }) }, apiVersion: Constants.ApiVersion, gameVersion: Constants.GameVersion, platform: Constants.Platform).Single().Value;
updateFound = response.SuggestedUpdate?.Version;
updateUrl = response.SuggestedUpdate?.Url;
// log message
if (updateFound != null)
this.Monitor.Log($"You can update SMAPI to {updateFound}: {updateUrl}", LogLevel.Alert);
else
this.Monitor.Log(" SMAPI okay.");
// show errors
if (response.Errors.Any())
{
this.Monitor.Log("Couldn't check for a new version of SMAPI. This won't affect your game, but you may not be notified of new versions if this keeps happening.", LogLevel.Warn);
this.Monitor.Log($"Error: {string.Join("\n", response.Errors)}");
}
}
catch (Exception ex)
{
this.Monitor.Log("Couldn't check for a new version of SMAPI. This won't affect your game, but you won't be notified of new versions if this keeps happening.", LogLevel.Warn);
this.Monitor.Log(ex is WebException && ex.InnerException == null
? $"Error: {ex.Message}"
: $"Error: {ex.GetLogSummary()}"
);
}
// show update message on next launch
if (updateFound != null)
this.LogManager.WriteUpdateMarker(updateFound.ToString(), updateUrl ?? Constants.HomePageUrl);
}
// check mod versions
if (mods.Any())
{
try
{
HashSet suppressUpdateChecks = this.Settings.SuppressUpdateChecks;
// prepare search model
List searchMods = new List();
foreach (IModMetadata mod in mods)
{
if (!mod.HasID() || suppressUpdateChecks.Contains(mod.Manifest.UniqueID))
continue;
string[] updateKeys = mod
.GetUpdateKeys(validOnly: true)
.Select(p => p.ToString())
.ToArray();
searchMods.Add(new ModSearchEntryModel(mod.Manifest.UniqueID, mod.Manifest.Version, updateKeys.ToArray(), isBroken: mod.Status == ModMetadataStatus.Failed));
}
// fetch results
this.Monitor.Log($" Checking for updates to {searchMods.Count} mods...");
IDictionary results = client.GetModInfo(searchMods.ToArray(), apiVersion: Constants.ApiVersion, gameVersion: Constants.GameVersion, platform: Constants.Platform);
// extract update alerts & errors
var updates = new List>();
var errors = new StringBuilder();
foreach (IModMetadata mod in mods.OrderBy(p => p.DisplayName))
{
// link to update-check data
if (!mod.HasID() || !results.TryGetValue(mod.Manifest.UniqueID, out ModEntryModel? result))
continue;
mod.SetUpdateData(result);
// handle errors
if (result.Errors.Any())
{
errors.AppendLine(result.Errors.Length == 1
? $" {mod.DisplayName}: {result.Errors[0]}"
: $" {mod.DisplayName}:\n - {string.Join("\n - ", result.Errors)}"
);
}
// handle update
if (result.SuggestedUpdate != null)
updates.Add(Tuple.Create(mod, result.SuggestedUpdate.Version, result.SuggestedUpdate.Url));
}
// show update errors
if (errors.Length != 0)
this.Monitor.Log("Got update-check errors for some mods:\n" + errors.ToString().TrimEnd());
// show update alerts
if (updates.Any())
{
this.Monitor.Newline();
this.Monitor.Log($"You can update {updates.Count} mod{(updates.Count != 1 ? "s" : "")}:", LogLevel.Alert);
foreach ((IModMetadata mod, ISemanticVersion newVersion, string newUrl) in updates)
this.Monitor.Log($" {mod.DisplayName} {newVersion}: {newUrl}", LogLevel.Alert);
}
else
this.Monitor.Log(" All mods up to date.");
}
catch (Exception ex)
{
this.Monitor.Log("Couldn't check for new mod versions. This won't affect your game, but you won't be notified of mod updates if this keeps happening.", LogLevel.Warn);
this.Monitor.Log(ex is WebException && ex.InnerException == null
? ex.Message
: ex.ToString()
);
}
}
}).Start();
}
/// Create a directory path if it doesn't exist.
/// The directory path.
private void VerifyPath(string path)
{
try
{
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
}
catch (Exception ex)
{
// note: this happens before this.Monitor is initialized
Console.WriteLine($"Couldn't create a path: {path}\n\n{ex.GetLogSummary()}");
}
}
/// Load and hook up the given mods.
/// The mods to load.
/// The JSON helper with which to read mods' JSON files.
/// The content manager to use for mod content.
/// Handles access to SMAPI's internal mod metadata list.
private void LoadMods(IModMetadata[] mods, JsonHelper jsonHelper, ContentCoordinator contentCore, ModDatabase modDatabase)
{
this.Monitor.Log("Loading mods...", LogLevel.Debug);
// load mods
IList skippedMods = new List();
using (AssemblyLoader modAssemblyLoader = new(Constants.Platform, this.Monitor, this.Settings.ParanoidWarnings, this.Settings.RewriteMods))
{
// init
HashSet suppressUpdateChecks = this.Settings.SuppressUpdateChecks;
IInterfaceProxyFactory proxyFactory = this.Settings.UsePintail
? new InterfaceProxyFactory()
: new OriginalInterfaceProxyFactory();
// load mods
foreach (IModMetadata mod in mods)
{
if (!this.TryLoadMod(mod, mods, modAssemblyLoader, proxyFactory, jsonHelper, contentCore, modDatabase, suppressUpdateChecks, out ModFailReason? failReason, out string? errorPhrase, out string? errorDetails))
{
mod.SetStatus(ModMetadataStatus.Failed, failReason.Value, errorPhrase, errorDetails);
skippedMods.Add(mod);
}
}
}
IModMetadata[] loaded = this.ModRegistry.GetAll().ToArray();
IModMetadata[] loadedContentPacks = loaded.Where(p => p.IsContentPack).ToArray();
IModMetadata[] loadedMods = loaded.Where(p => !p.IsContentPack).ToArray();
// unlock content packs
this.ModRegistry.AreAllModsLoaded = true;
// log mod info
this.LogManager.LogModInfo(loaded, loadedContentPacks, loadedMods, skippedMods.ToArray(), this.Settings.ParanoidWarnings);
// initialize translations
this.ReloadTranslations(loaded);
// initialize loaded non-content-pack mods
this.Monitor.Log("Launching mods...", LogLevel.Debug);
#pragma warning disable CS0612, CS0618 // deprecated code
foreach (IModMetadata metadata in loadedMods)
{
// add interceptors
if (metadata.Mod?.Helper is ModHelper helper)
{
// ReSharper disable SuspiciousTypeConversion.Global
if (metadata.Mod is IAssetEditor editor)
{
SCore.DeprecationManager.Warn(
source: metadata,
nounPhrase: $"{nameof(IAssetEditor)}",
version: "3.14.0",
severity: DeprecationLevel.Notice,
logStackTrace: false
);
this.ContentCore.Editors.Add(new ModLinked(metadata, editor));
}
if (metadata.Mod is IAssetLoader loader)
{
SCore.DeprecationManager.Warn(
source: metadata,
nounPhrase: $"{nameof(IAssetLoader)}",
version: "3.14.0",
severity: DeprecationLevel.Notice,
logStackTrace: false
);
this.ContentCore.Loaders.Add(new ModLinked(metadata, loader));
}
// ReSharper restore SuspiciousTypeConversion.Global
ContentHelper content = helper.GetLegacyContentHelper();
content.ObservableAssetEditors.CollectionChanged += (_, e) => this.OnAssetInterceptorsChanged(metadata, e.NewItems?.Cast(), e.OldItems?.Cast(), this.ContentCore.Editors);
content.ObservableAssetLoaders.CollectionChanged += (_, e) => this.OnAssetInterceptorsChanged(metadata, e.NewItems?.Cast(), e.OldItems?.Cast(), this.ContentCore.Loaders);
}
#pragma warning restore CS0612, CS0618
// log deprecation warnings
if (metadata.HasWarnings(ModWarning.DetectedLegacyCachingDll, ModWarning.DetectedLegacyConfigurationDll, ModWarning.DetectedLegacyPermissionsDll))
{
string?[] referenced =
new[]
{
metadata.Warnings.HasFlag(ModWarning.DetectedLegacyConfigurationDll) ? "System.Configuration.ConfigurationManager" : null,
metadata.Warnings.HasFlag(ModWarning.DetectedLegacyCachingDll) ? "System.Runtime.Caching" : null,
metadata.Warnings.HasFlag(ModWarning.DetectedLegacyPermissionsDll) ? "System.Security.Permissions" : null
}
.Where(p => p is not null)
.ToArray();
foreach (string? name in referenced)
{
DeprecationManager.Warn(
metadata,
$"using {name} without bundling it",
"3.14.7",
DeprecationLevel.Notice,
logStackTrace: false
);
}
}
// call entry method
Context.HeuristicModsRunningCode.Push(metadata);
try
{
IMod mod = metadata.Mod!;
mod.Entry(mod.Helper!);
}
catch (Exception ex)
{
metadata.LogAsMod($"Mod crashed on entry and might not work correctly. Technical details:\n{ex.GetLogSummary()}", LogLevel.Error);
}
// get mod API
try
{
object? api = metadata.Mod!.GetApi();
if (api != null && !api.GetType().IsPublic)
{
api = null;
this.Monitor.Log($"{metadata.DisplayName} provides an API instance with a non-public type. This isn't currently supported, so the API won't be available to other mods.", LogLevel.Warn);
}
if (api != null)
this.Monitor.Log($" Found mod-provided API ({api.GetType().FullName}).");
metadata.SetApi(api);
}
catch (Exception ex)
{
this.Monitor.Log($"Failed loading mod-provided API for {metadata.DisplayName}. Integrations with other mods may not work. Error: {ex.GetLogSummary()}", LogLevel.Error);
}
Context.HeuristicModsRunningCode.TryPop(out _);
}
// unlock mod integrations
this.ModRegistry.AreAllModsInitialized = true;
this.Monitor.Log("Mods loaded and ready!", LogLevel.Debug);
}
/// Raised after a mod adds or removes asset interceptors.
/// The asset interceptor type (one of or ).
/// The mod metadata.
/// The interceptors that were added.
/// The interceptors that were removed.
/// A list of interceptors to update for the change.
private void OnAssetInterceptorsChanged(IModMetadata mod, IEnumerable? added, IEnumerable? removed, IList> list)
where T : notnull
{
foreach (T interceptor in added ?? Array.Empty())
{
this.ReloadAssetInterceptorsQueue.Add(new AssetInterceptorChange(mod, interceptor, wasAdded: true));
list.Add(new ModLinked(mod, interceptor));
}
foreach (T interceptor in removed ?? Array.Empty())
{
this.ReloadAssetInterceptorsQueue.Add(new AssetInterceptorChange(mod, interceptor, wasAdded: false));
foreach (ModLinked entry in list.Where(p => p.Mod == mod && object.ReferenceEquals(p.Data, interceptor)).ToArray())
list.Remove(entry);
}
}
/// Load a given mod.
/// The mod to load.
/// The mods being loaded.
/// Preprocesses and loads mod assemblies.
/// Generates proxy classes to access mod APIs through an arbitrary interface.
/// The JSON helper with which to read mods' JSON files.
/// The content manager to use for mod content.
/// Handles access to SMAPI's internal mod metadata list.
/// The mod IDs to ignore when validating update keys.
/// The reason the mod couldn't be loaded, if applicable.
/// The user-facing reason phrase explaining why the mod couldn't be loaded (if applicable).
/// More detailed details about the error intended for developers (if any).
/// Returns whether the mod was successfully loaded.
private bool TryLoadMod(IModMetadata mod, IModMetadata[] mods, AssemblyLoader assemblyLoader, IInterfaceProxyFactory proxyFactory, JsonHelper jsonHelper, ContentCoordinator contentCore, ModDatabase modDatabase, HashSet suppressUpdateChecks, [NotNullWhen(false)] out ModFailReason? failReason, out string? errorReasonPhrase, out string? errorDetails)
{
errorDetails = null;
// log entry
{
string relativePath = mod.GetRelativePathWithRoot();
if (mod.IsContentPack)
this.Monitor.Log($" {mod.DisplayName} (from {relativePath}) [content pack]...");
// ReSharper disable once ConstantConditionalAccessQualifier -- mod may be invalid at this point
else if (mod.Manifest?.EntryDll != null)
this.Monitor.Log($" {mod.DisplayName} (from {relativePath}{Path.DirectorySeparatorChar}{mod.Manifest.EntryDll})..."); // don't use Path.Combine here, since EntryDLL might not be valid
else
this.Monitor.Log($" {mod.DisplayName} (from {relativePath})...");
}
// add warning for missing update key
if (mod.HasID() && !suppressUpdateChecks.Contains(mod.Manifest!.UniqueID) && !mod.HasValidUpdateKeys())
mod.SetWarning(ModWarning.NoUpdateKeys);
// validate status
if (mod.Status == ModMetadataStatus.Failed)
{
this.Monitor.Log($" Failed: {mod.ErrorDetails ?? mod.Error}");
failReason = mod.FailReason ?? ModFailReason.LoadFailed;
errorReasonPhrase = mod.Error;
return false;
}
IManifest manifest = mod.Manifest!;
// validate dependencies
// Although dependencies are validated before mods are loaded, a dependency may have failed to load.
foreach (IManifestDependency dependency in manifest.Dependencies.Where(p => p.IsRequired))
{
if (this.ModRegistry.Get(dependency.UniqueID) == null)
{
string dependencyName = mods
.FirstOrDefault(otherMod => otherMod.HasID(dependency.UniqueID))
?.DisplayName ?? dependency.UniqueID;
errorReasonPhrase = $"it needs the '{dependencyName}' mod, which couldn't be loaded.";
failReason = ModFailReason.MissingDependencies;
return false;
}
}
// load as content pack
if (mod.IsContentPack)
{
IMonitor monitor = this.LogManager.GetMonitor(manifest.UniqueID, mod.DisplayName);
IFileLookup fileLookup = this.GetFileLookup(mod.DirectoryPath);
GameContentHelper gameContentHelper = new(this.ContentCore, mod, mod.DisplayName, monitor, this.Reflection);
IModContentHelper modContentHelper = new ModContentHelper(this.ContentCore, mod.DirectoryPath, mod, mod.DisplayName, gameContentHelper.GetUnderlyingContentManager(), this.Reflection);
TranslationHelper translationHelper = new(mod, contentCore.GetLocale(), contentCore.Language);
IContentPack contentPack = new ContentPack(mod.DirectoryPath, manifest, modContentHelper, translationHelper, jsonHelper, fileLookup);
mod.SetMod(contentPack, monitor, translationHelper);
this.ModRegistry.Add(mod);
errorReasonPhrase = null;
failReason = null;
return true;
}
// load as mod
else
{
// get mod info
FileInfo assemblyFile = this.GetFileLookup(mod.DirectoryPath).GetFile(manifest.EntryDll!);
// load mod
Assembly modAssembly;
try
{
modAssembly = assemblyLoader.Load(mod, assemblyFile, assumeCompatible: mod.DataRecord?.Status == ModStatus.AssumeCompatible);
this.ModRegistry.TrackAssemblies(mod, modAssembly);
}
catch (IncompatibleInstructionException) // details already in trace logs
{
string[] updateUrls = new[] { modDatabase.GetModPageUrlFor(manifest.UniqueID), "https://smapi.io/mods" }.Where(p => p != null).ToArray()!;
errorReasonPhrase = $"it's no longer compatible. Please check for a new version at {string.Join(" or ", updateUrls)}";
failReason = ModFailReason.Incompatible;
return false;
}
catch (SAssemblyLoadFailedException ex)
{
errorReasonPhrase = $"its DLL couldn't be loaded: {ex.Message}";
failReason = ModFailReason.LoadFailed;
return false;
}
catch (Exception ex)
{
errorReasonPhrase = "its DLL couldn't be loaded.";
if (ex is BadImageFormatException && !EnvironmentUtility.Is64BitAssembly(assemblyFile.FullName))
errorReasonPhrase = "it needs to be updated for 64-bit mode.";
errorDetails = $"Error: {ex.GetLogSummary()}";
failReason = ModFailReason.LoadFailed;
return false;
}
// initialize mod
try
{
// get mod instance
if (!this.TryLoadModEntry(mod, modAssembly, out Mod? modEntry, out errorReasonPhrase))
{
failReason = ModFailReason.LoadFailed;
return false;
}
// get content packs
IContentPack[] GetContentPacks()
{
if (!this.ModRegistry.AreAllModsLoaded)
throw new InvalidOperationException("Can't access content packs before SMAPI finishes loading mods.");
return this.ModRegistry
.GetAll(assemblyMods: false)
.Where(p => p.IsContentPack && mod.HasID(p.Manifest.ContentPackFor!.UniqueID))
.Select(p => p.ContentPack!)
.ToArray();
}
// init mod helpers
IMonitor monitor = this.LogManager.GetMonitor(manifest.UniqueID, mod.DisplayName);
TranslationHelper translationHelper = new(mod, contentCore.GetLocale(), contentCore.Language);
IModHelper modHelper;
{
IModEvents events = new ModEvents(mod, this.EventManager);
ICommandHelper commandHelper = new CommandHelper(mod, this.CommandManager);
#pragma warning disable CS0612 // deprecated code
ContentHelper contentHelper = new(contentCore, mod.DirectoryPath, mod, monitor, this.Reflection);
#pragma warning restore CS0612
GameContentHelper gameContentHelper = new(contentCore, mod, mod.DisplayName, monitor, this.Reflection);
IModContentHelper modContentHelper = new ModContentHelper(contentCore, mod.DirectoryPath, mod, mod.DisplayName, gameContentHelper.GetUnderlyingContentManager(), this.Reflection);
IContentPackHelper contentPackHelper = new ContentPackHelper(
mod: mod,
contentPacks: new Lazy(GetContentPacks),
createContentPack: (dirPath, fakeManifest) => this.CreateFakeContentPack(dirPath, fakeManifest, contentCore, mod)
);
IDataHelper dataHelper = new DataHelper(mod, mod.DirectoryPath, jsonHelper);
IReflectionHelper reflectionHelper = new ReflectionHelper(mod, mod.DisplayName, this.Reflection);
IModRegistry modRegistryHelper = new ModRegistryHelper(mod, this.ModRegistry, proxyFactory, monitor);
IMultiplayerHelper multiplayerHelper = new MultiplayerHelper(mod, this.Multiplayer);
modHelper = new ModHelper(mod, mod.DirectoryPath, () => this.GetCurrentGameInstance().Input, events, contentHelper, gameContentHelper, modContentHelper, contentPackHelper, commandHelper, dataHelper, modRegistryHelper, reflectionHelper, multiplayerHelper, translationHelper);
}
// init mod
modEntry.ModManifest = manifest;
modEntry.Helper = modHelper;
modEntry.Monitor = monitor;
// track mod
mod.SetMod(modEntry, translationHelper);
this.ModRegistry.Add(mod);
failReason = null;
return true;
}
catch (Exception ex)
{
errorReasonPhrase = $"initialization failed:\n{ex.GetLogSummary()}";
failReason = ModFailReason.LoadFailed;
return false;
}
}
}
/// Create a fake content pack instance for a parent mod.
/// The absolute path to the fake content pack's directory.
/// The fake content pack's manifest.
/// The content manager to use for mod content.
/// The mod for which the content pack is being created.
private IContentPack CreateFakeContentPack(string packDirPath, IManifest packManifest, ContentCoordinator contentCore, IModMetadata parentMod)
{
// create fake mod info
string relativePath = Path.GetRelativePath(Constants.ModsPath, packDirPath);
IModMetadata fakeMod = new ModMetadata(
displayName: packManifest.Name,
directoryPath: packDirPath,
rootPath: Constants.ModsPath,
manifest: packManifest,
dataRecord: null,
isIgnored: false
);
// create mod helpers
IMonitor packMonitor = this.LogManager.GetMonitor(packManifest.UniqueID, packManifest.Name);
GameContentHelper gameContentHelper = new(contentCore, fakeMod, packManifest.Name, packMonitor, this.Reflection);
IModContentHelper packContentHelper = new ModContentHelper(contentCore, packDirPath, fakeMod, packManifest.Name, gameContentHelper.GetUnderlyingContentManager(), this.Reflection);
TranslationHelper packTranslationHelper = new(fakeMod, contentCore.GetLocale(), contentCore.Language);
// add content pack
IFileLookup fileLookup = this.GetFileLookup(packDirPath);
ContentPack contentPack = new(packDirPath, packManifest, packContentHelper, packTranslationHelper, this.Toolkit.JsonHelper, fileLookup);
this.ReloadTranslationsForTemporaryContentPack(parentMod, contentPack);
parentMod.FakeContentPacks.Add(new WeakReference(contentPack));
// log change
string pathLabel = packDirPath.Contains("..") ? packDirPath : relativePath;
this.Monitor.Log($"{parentMod.DisplayName} created dynamic content pack '{packManifest.Name}' (unique ID: {packManifest.UniqueID}{(packManifest.Name.Contains(pathLabel) ? "" : $", path: {pathLabel}")}).");
return contentPack;
}
/// Load a mod's entry class.
/// The mod metadata whose entry class is being loaded.
/// The mod assembly.
/// The loaded instance.
/// The error indicating why loading failed (if applicable).
/// Returns whether the mod entry class was successfully loaded.
private bool TryLoadModEntry(IModMetadata metadata, Assembly modAssembly, [NotNullWhen(true)] out Mod? mod, [NotNullWhen(false)] out string? error)
{
mod = null;
// find type
TypeInfo[] modEntries = modAssembly.DefinedTypes.Where(type => typeof(Mod).IsAssignableFrom(type) && !type.IsAbstract).Take(2).ToArray();
if (modEntries.Length == 0)
{
error = $"its DLL has no '{nameof(Mod)}' subclass.";
return false;
}
if (modEntries.Length > 1)
{
error = $"its DLL contains multiple '{nameof(Mod)}' subclasses.";
return false;
}
// get implementation
Context.HeuristicModsRunningCode.Push(metadata);
try
{
mod = (Mod?)modAssembly.CreateInstance(modEntries[0].ToString());
}
finally
{
Context.HeuristicModsRunningCode.TryPop(out _);
}
if (mod == null)
{
error = "its entry class couldn't be instantiated.";
return false;
}
error = null;
return true;
}
/// Reload translations for all mods.
private void ReloadTranslations()
{
this.ReloadTranslations(this.ModRegistry.GetAll());
}
/// Reload translations for the given mods.
/// The mods for which to reload translations.
private void ReloadTranslations(IEnumerable mods)
{
// core SMAPI translations
{
var translations = this.ReadTranslationFiles(Path.Combine(Constants.InternalFilesPath, "i18n"), out IList errors);
if (errors.Any() || !translations.Any())
{
this.Monitor.Log("SMAPI couldn't load some core translations. You may need to reinstall SMAPI.", LogLevel.Warn);
foreach (string error in errors)
this.Monitor.Log($" - {error}", LogLevel.Warn);
}
this.Translator.SetTranslations(translations);
}
// mod translations
foreach (IModMetadata metadata in mods)
{
// top-level mod
{
var translations = this.ReadTranslationFiles(Path.Combine(metadata.DirectoryPath, "i18n"), out IList errors);
if (errors.Any())
{
metadata.LogAsMod("Mod couldn't load some translation files:", LogLevel.Warn);
foreach (string error in errors)
metadata.LogAsMod($" - {error}", LogLevel.Warn);
}
metadata.Translations!.SetTranslations(translations);
}
// fake content packs
foreach (ContentPack pack in metadata.GetFakeContentPacks())
this.ReloadTranslationsForTemporaryContentPack(metadata, pack);
}
}
/// Load or reload translations for a temporary content pack created by a mod.
/// The parent mod which created the content pack.
/// The content pack instance.
private void ReloadTranslationsForTemporaryContentPack(IModMetadata parentMod, ContentPack contentPack)
{
var translations = this.ReadTranslationFiles(Path.Combine(contentPack.DirectoryPath, "i18n"), out IList errors);
if (errors.Any())
{
parentMod.LogAsMod($"Generated content pack at '{PathUtilities.GetRelativePath(Constants.ModsPath, contentPack.DirectoryPath)}' couldn't load some translation files:", LogLevel.Warn);
foreach (string error in errors)
parentMod.LogAsMod($" - {error}", LogLevel.Warn);
}
contentPack.TranslationImpl.SetTranslations(translations);
}
/// Read translations from a directory containing JSON translation files.
/// The folder path to search.
/// The errors indicating why translation files couldn't be parsed, indexed by translation filename.
private IDictionary> ReadTranslationFiles(string folderPath, out IList errors)
{
JsonHelper jsonHelper = this.Toolkit.JsonHelper;
// read translation files
var translations = new Dictionary>();
errors = new List();
DirectoryInfo translationsDir = new(folderPath);
if (translationsDir.Exists)
{
foreach (FileInfo file in translationsDir.EnumerateFiles("*.json"))
{
string locale = Path.GetFileNameWithoutExtension(file.Name.ToLower().Trim());
try
{
if (!jsonHelper.ReadJsonFileIfExists(file.FullName, out IDictionary? data))
{
errors.Add($"{file.Name} file couldn't be read"); // mainly happens when the file is corrupted or empty
continue;
}
translations[locale] = data;
}
catch (Exception ex)
{
errors.Add($"{file.Name} file couldn't be parsed: {ex.GetLogSummary()}");
}
}
}
// validate translations
foreach (string locale in translations.Keys.ToArray())
{
// handle duplicates
HashSet keys = new(StringComparer.OrdinalIgnoreCase);
HashSet duplicateKeys = new(StringComparer.OrdinalIgnoreCase);
foreach (string key in translations[locale].Keys.ToArray())
{
if (!keys.Add(key))
{
duplicateKeys.Add(key);
translations[locale].Remove(key);
}
}
if (duplicateKeys.Any())
errors.Add($"{locale}.json has duplicate translation keys: [{string.Join(", ", duplicateKeys)}]. Keys are case-insensitive.");
}
return translations;
}
/// Get a file lookup for the given directory.
/// The root path to scan.
private IFileLookup GetFileLookup(string rootDirectory)
{
return this.Settings.UseCaseInsensitivePaths
? CaseInsensitiveFileLookup.GetCachedFor(rootDirectory)
: MinimalFileLookup.GetCachedFor(rootDirectory);
}
/// Get the map display device which applies SMAPI features like tile rotation to loaded maps.
/// This is separate to let mods like PyTK wrap it with their own functionality.
private IDisplayDevice GetMapDisplayDevice()
{
return new SDisplayDevice(Game1.content, Game1.game1.GraphicsDevice);
}
/// Get the absolute path to the next available log file.
private string GetLogPath()
{
// default path
{
FileInfo defaultFile = new(Path.Combine(Constants.LogDir, $"{Constants.LogFilename}.{Constants.LogExtension}"));
if (!defaultFile.Exists)
return defaultFile.FullName;
}
// get first disambiguated path
for (int i = 2; i < int.MaxValue; i++)
{
FileInfo file = new(Path.Combine(Constants.LogDir, $"{Constants.LogFilename}.player-{i}.{Constants.LogExtension}"));
if (!file.Exists)
return file.FullName;
}
// should never happen
throw new InvalidOperationException("Could not find an available log path.");
}
/// Delete normal (non-crash) log files created by SMAPI.
private void PurgeNormalLogs()
{
DirectoryInfo logsDir = new(Constants.LogDir);
if (!logsDir.Exists)
return;
foreach (FileInfo logFile in logsDir.EnumerateFiles())
{
// skip non-SMAPI file
if (!logFile.Name.StartsWith(Constants.LogNamePrefix, StringComparison.OrdinalIgnoreCase))
continue;
// skip crash log
if (logFile.FullName == Constants.FatalCrashLog)
continue;
// delete file
try
{
FileUtilities.ForceDelete(logFile);
}
catch (IOException)
{
// ignore file if it's in use
}
}
}
/// Immediately exit the game without saving. This should only be invoked when an irrecoverable fatal error happens that risks save corruption or game-breaking bugs.
/// The fatal log message.
private void ExitGameImmediately(string message)
{
this.Monitor.LogFatal(message);
this.CancellationToken.Cancel();
}
/// Get the screen ID that should be logged to distinguish between players in split-screen mode, if any.
private int? GetScreenIdForLog()
{
if (Context.ScreenId != 0 || (Context.IsWorldReady && Context.IsSplitScreen))
return Context.ScreenId;
return null;
}
/*********
** Private types
*********/
/// A queued console command to run during the update loop.
/// The command which can handle the input.
/// The parsed command name.
/// The parsed command arguments.
private readonly record struct QueuedCommand(Command Command, string Name, string[] Args);
}
}