From db88738666ee28b11468ae986af5c0418bb4ee5a Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sat, 27 Oct 2018 13:08:15 -0400 Subject: fix friendly error no longer shown when SMAPI isn't run from the game folder --- src/SMAPI/Program.cs | 53 +++++++++++++++++++++++++++++----------------------- 1 file changed, 30 insertions(+), 23 deletions(-) (limited to 'src') diff --git a/src/SMAPI/Program.cs b/src/SMAPI/Program.cs index b46ecff4..2efcfecb 100644 --- a/src/SMAPI/Program.cs +++ b/src/SMAPI/Program.cs @@ -7,6 +7,7 @@ using System.Threading; #if SMAPI_FOR_WINDOWS #endif using StardewModdingAPI.Framework; +using StardewModdingAPI.Internal; namespace StardewModdingAPI { @@ -30,7 +31,8 @@ namespace StardewModdingAPI public static void Main(string[] args) { AppDomain.CurrentDomain.AssemblyResolve += Program.CurrentDomain_AssemblyResolve; - Program.AssertMinimumCompatibility(); + Program.AssertGamePresent(); + Program.AssertGameVersion(); Program.Start(args); } @@ -60,22 +62,15 @@ namespace StardewModdingAPI } } - /// Assert that the minimum conditions are present to initialise SMAPI without type load exceptions. - private static void AssertMinimumCompatibility() + /// Assert that the game is available. + /// This must be checked *before* any references to , and this method should not reference itself to avoid errors in Mono. + private static void AssertGamePresent() { - void PrintErrorAndExit(string message) - { - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(message); - Console.ResetColor(); - Program.PressAnyKeyToExit(showMessage: true); - } - string gameAssemblyName = Constants.GameAssemblyName; - - // game not present + Platform platform = EnvironmentUtility.DetectPlatform(); + string gameAssemblyName = platform == Platform.Windows ? "Stardew Valley" : "StardewValley"; if (Type.GetType($"StardewValley.Game1, {gameAssemblyName}", throwOnError: false) == null) { - PrintErrorAndExit( + Program.PrintErrorAndExit( "Oops! SMAPI can't find the game. " + (Assembly.GetCallingAssembly().Location.Contains(Path.Combine("internal", "Windows")) || Assembly.GetCallingAssembly().Location.Contains(Path.Combine("internal", "Mono")) ? "It looks like you're running SMAPI from the download package, but you need to run the installed version instead. " @@ -83,24 +78,26 @@ namespace StardewModdingAPI ) + "See the readme.txt file for details." ); - return; } + } - // validate game version + /// Assert that the game version is within and . + private static void AssertGameVersion() + { + // min version if (Constants.GameVersion.IsOlderThan(Constants.MinimumGameVersion)) { ISemanticVersion suggestedApiVersion = Constants.GetCompatibleApiVersion(Constants.GameVersion); - PrintErrorAndExit(suggestedApiVersion != null + Program.PrintErrorAndExit(suggestedApiVersion != null ? $"Oops! You're running Stardew Valley {Constants.GameVersion}, but the oldest supported version is {Constants.MinimumGameVersion}. You can install SMAPI {suggestedApiVersion} instead to fix this error, or update your game to the latest version." : $"Oops! You're running Stardew Valley {Constants.GameVersion}, but the oldest supported version is {Constants.MinimumGameVersion}. Please update your game before using SMAPI." ); - return; - } - if (Constants.MaximumGameVersion != null && Constants.GameVersion.IsNewerThan(Constants.MaximumGameVersion)) - { - PrintErrorAndExit($"Oops! You're running Stardew Valley {Constants.GameVersion}, but this version of SMAPI is only compatible up to Stardew Valley {Constants.MaximumGameVersion}. Please check for a newer version of SMAPI: https://smapi.io."); - return; } + + // max version + else if (Constants.MaximumGameVersion != null && Constants.GameVersion.IsNewerThan(Constants.MaximumGameVersion)) + Program.PrintErrorAndExit($"Oops! You're running Stardew Valley {Constants.GameVersion}, but this version of SMAPI is only compatible up to Stardew Valley {Constants.MaximumGameVersion}. Please check for a newer version of SMAPI: https://smapi.io."); + } /// Initialise SMAPI and launch the game. @@ -130,6 +127,16 @@ namespace StardewModdingAPI core.RunInteractively(); } + /// Write an error directly to the console and exit. + /// The error message to display. + private static void PrintErrorAndExit(string message) + { + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine(message); + Console.ResetColor(); + Program.PressAnyKeyToExit(showMessage: true); + } + /// Show a 'press any key to exit' message, and exit when they press a key. /// Whether to print a 'press any key to exit' message to the console. private static void PressAnyKeyToExit(bool showMessage) -- cgit