using System; using System.IO; using System.Linq; using System.Reflection; using StardewModdingAPI.Enums; using StardewModdingAPI.Framework; using StardewModdingAPI.Framework.ModLoading; using StardewModdingAPI.Toolkit.Framework; using StardewModdingAPI.Toolkit.Utilities; using StardewValley; namespace StardewModdingAPI { /// Contains constants that are accessed before the game itself has been loaded. /// Most code should use instead of this class directly. internal static class EarlyConstants { // // Note: this class *must not* depend on any external DLL beyond .NET Framework itself. // That includes the game or SMAPI toolkit, since it's accessed before those are loaded. // // Adding an external dependency may seem to work in some cases, but will prevent SMAPI // from showing a human-readable error if the game isn't available. To test this, just // rename "Stardew Valley.exe" in the game folder; you should see an error like "Oops! // SMAPI can't find the game", not a technical exception. // /********* ** Accessors *********/ /// The path to the game folder. public static string ExecutionPath { get; } = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); /// The absolute path to the folder containing SMAPI's internal files. public static readonly string InternalFilesPath = Path.Combine(EarlyConstants.ExecutionPath, "smapi-internal"); /// The target game platform. internal static GamePlatform Platform { get; } = (GamePlatform)Enum.Parse(typeof(GamePlatform), LowLevelEnvironmentUtility.DetectPlatform()); /// The game's assembly name. internal static string GameAssemblyName => EarlyConstants.Platform == GamePlatform.Windows ? "Stardew Valley" : "StardewValley"; /// The value which should appear in the SMAPI log, if any. internal static int? LogScreenId { get; set; } } /// Contains SMAPI's constants and assumptions. public static class Constants { /********* ** Accessors *********/ /**** ** Public ****/ /// SMAPI's current semantic version. public static ISemanticVersion ApiVersion { get; } = new Toolkit.SemanticVersion("3.8.3"); /// The minimum supported version of Stardew Valley. public static ISemanticVersion MinimumGameVersion { get; } = new GameVersion("1.5.2"); /// The maximum supported version of Stardew Valley. public static ISemanticVersion MaximumGameVersion { get; } = null; /// The target game platform. public static GamePlatform TargetPlatform { get; } = EarlyConstants.Platform; /// The path to the game folder. public static string ExecutionPath { get; } = EarlyConstants.ExecutionPath; /// The directory path containing Stardew Valley's app data. public static string DataPath { get; } = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "StardewValley"); /// The directory path in which error logs should be stored. public static string LogDir { get; } = Path.Combine(Constants.DataPath, "ErrorLogs"); /// The directory path where all saves are stored. public static string SavesPath { get; } = Path.Combine(Constants.DataPath, "Saves"); /// The name of the current save folder (if save info is available, regardless of whether the save file exists yet). public static string SaveFolderName => Constants.GetSaveFolderName(); /// The absolute path to the current save folder (if save info is available and the save file exists). public static string CurrentSavePath => Constants.GetSaveFolderPathIfExists(); /**** ** Internal ****/ /// Whether SMAPI was compiled in debug mode. internal const bool IsDebugBuild = #if DEBUG true; #else false; #endif /// The URL of the SMAPI home page. internal const string HomePageUrl = "https://smapi.io"; /// The absolute path to the folder containing SMAPI's internal files. internal static readonly string InternalFilesPath = EarlyConstants.InternalFilesPath; /// The file path for the SMAPI configuration file. internal static string ApiConfigPath => Path.Combine(Constants.InternalFilesPath, "config.json"); /// The file path for the overrides file for , which is applied over it. internal static string ApiUserConfigPath => Path.Combine(Constants.InternalFilesPath, "config.user.json"); /// The file path for the SMAPI metadata file. internal static string ApiMetadataPath => Path.Combine(Constants.InternalFilesPath, "metadata.json"); /// The filename prefix used for all SMAPI logs. internal static string LogNamePrefix { get; } = "SMAPI-"; /// The filename for SMAPI's main log, excluding the . internal static string LogFilename { get; } = $"{Constants.LogNamePrefix}latest"; /// The filename extension for SMAPI log files. internal static string LogExtension { get; } = "txt"; /// The file path for the log containing the previous fatal crash, if any. internal static string FatalCrashLog => Path.Combine(Constants.LogDir, "SMAPI-crash.txt"); /// The file path which stores a fatal crash message for the next run. internal static string FatalCrashMarker => Path.Combine(Constants.InternalFilesPath, "StardewModdingAPI.crash.marker"); /// The file path which stores the detected update version for the next run. internal static string UpdateMarker => Path.Combine(Constants.InternalFilesPath, "StardewModdingAPI.update.marker"); /// The default full path to search for mods. internal static string DefaultModsPath { get; } = Path.Combine(Constants.ExecutionPath, "Mods"); /// The actual full path to search for mods. internal static string ModsPath { get; set; } /// The game's current semantic version. internal static ISemanticVersion GameVersion { get; } = new GameVersion(Game1.version); /// The target game platform as a SMAPI toolkit constant. internal static Platform Platform { get; } = (Platform)Constants.TargetPlatform; /// The language code for non-translated mod assets. internal static LocalizedContentManager.LanguageCode DefaultLanguage { get; } = LocalizedContentManager.LanguageCode.en; /********* ** Internal methods *********/ /// Get the SMAPI version to recommend for an older game version, if any. /// The game version to search. /// Returns the compatible SMAPI version, or null if none was found. internal static ISemanticVersion GetCompatibleApiVersion(ISemanticVersion version) { // This covers all officially supported public game updates. It might seem like version // ranges would be better, but the given SMAPI versions may not be compatible with // intermediate unlisted versions (e.g. private beta updates). // // Nonstandard versions are normalized by GameVersion (e.g. 1.07 => 1.0.7). switch (version.ToString()) { case "1.4.1": case "1.4.0": return new SemanticVersion("3.0.1"); case "1.3.36": return new SemanticVersion("2.11.2"); case "1.3.33": case "1.3.32": return new SemanticVersion("2.10.2"); case "1.3.28": return new SemanticVersion("2.7.0"); case "1.2.33": case "1.2.32": case "1.2.31": case "1.2.30": return new SemanticVersion("2.5.5"); case "1.2.29": case "1.2.28": case "1.2.27": case "1.2.26": return new SemanticVersion("1.13.1"); case "1.1.1": case "1.1.0": return new SemanticVersion("1.9.0"); case "1.0.7.1": case "1.0.7": case "1.0.6": case "1.0.5.2": case "1.0.5.1": case "1.0.5": case "1.0.4": case "1.0.3": case "1.0.2": case "1.0.1": case "1.0.0": return new SemanticVersion("0.40.0"); default: return null; } } /// Get metadata for mapping assemblies to the current platform. /// The target game platform. internal static PlatformAssemblyMap GetAssemblyMap(Platform targetPlatform) { // get assembly changes needed for platform string[] removeAssemblyReferences; Assembly[] targetAssemblies; switch (targetPlatform) { case Platform.Linux: case Platform.Mac: removeAssemblyReferences = new[] { "Netcode", "Stardew Valley", "Microsoft.Xna.Framework", "Microsoft.Xna.Framework.Game", "Microsoft.Xna.Framework.Graphics", "Microsoft.Xna.Framework.Xact", "StardewModdingAPI.Toolkit.CoreInterfaces" // renamed in SMAPI 3.0 }; targetAssemblies = new[] { typeof(StardewValley.Game1).Assembly, // note: includes Netcode types on Linux/Mac typeof(Microsoft.Xna.Framework.Vector2).Assembly, typeof(StardewModdingAPI.IManifest).Assembly }; break; case Platform.Windows: removeAssemblyReferences = new[] { "StardewValley", "MonoGame.Framework", "StardewModdingAPI.Toolkit.CoreInterfaces" // renamed in SMAPI 3.0 }; targetAssemblies = new[] { typeof(Netcode.NetBool).Assembly, typeof(StardewValley.Game1).Assembly, typeof(Microsoft.Xna.Framework.Vector2).Assembly, typeof(Microsoft.Xna.Framework.Game).Assembly, typeof(Microsoft.Xna.Framework.Graphics.SpriteBatch).Assembly, typeof(StardewModdingAPI.IManifest).Assembly }; break; default: throw new InvalidOperationException($"Unknown target platform '{targetPlatform}'."); } return new PlatformAssemblyMap(targetPlatform, removeAssemblyReferences, targetAssemblies); } /********* ** Private methods *********/ /// Get the name of the save folder, if any. private static string GetSaveFolderName() { // save not available if (Context.LoadStage == LoadStage.None) return null; // get basic info string saveName = Game1.GetSaveGameName(set_value: false); ulong saveID = Context.LoadStage == LoadStage.SaveParsed ? SaveGame.loaded.uniqueIDForThisGame : Game1.uniqueIDForThisGame; // build folder name return $"{new string(saveName.Where(char.IsLetterOrDigit).ToArray())}_{saveID}"; } /// Get the path to the current save folder, if any. private static string GetSaveFolderPathIfExists() { string folderName = Constants.GetSaveFolderName(); if (folderName == null) return null; string path = Path.Combine(Constants.SavesPath, folderName); return Directory.Exists(path) ? path : null; } } }