From 01c612bc4aca5a1d8921432c94956f4d170d4d4b Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Mon, 4 Nov 2019 13:59:34 -0500 Subject: [PATCH] add friendly error for BadImageFormatException on launch --- docs/release-notes.md | 1 + src/SMAPI/Program.cs | 15 +++++++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/docs/release-notes.md b/docs/release-notes.md index 47e12f83..7cd7b52b 100644 --- a/docs/release-notes.md +++ b/docs/release-notes.md @@ -50,6 +50,7 @@ For modders: * Added support for configuring console colors via `smapi-internal/config.json` (intended for players with unusual consoles). * Added support for specifying SMAPI command-line arguments as environment variables for Linux/Mac compatibility. * Improved launch script compatibility on Linux (thanks to kurumushi and toastal!). + * Made error messages more user-friendly in some cases. * Save Backup now works in the background, to avoid affecting startup time for players with a large number of saves. * The installer now recognises custom game paths stored in `stardewvalley.targets`. * Duplicate-mod errors now show the mod version in each folder. diff --git a/src/SMAPI/Program.cs b/src/SMAPI/Program.cs index d6e0888b..6bacf564 100644 --- a/src/SMAPI/Program.cs +++ b/src/SMAPI/Program.cs @@ -40,6 +40,11 @@ namespace StardewModdingAPI Program.AssertGameVersion(); Program.Start(args); } + catch (BadImageFormatException ex) when (ex.FileName == "StardewValley") + { + string executableName = Program.GetExecutableAssemblyName(); + Console.WriteLine($"SMAPI failed to initialize because your game's {executableName}.exe seems to be invalid.\nThis may be a pirated version which modified the executable in an incompatible way; if so, you can try a different download or buy a legitimate version.\n\nTechnical details:\n{ex}"); + } catch (Exception ex) { Console.WriteLine($"SMAPI failed to initialize: {ex}"); @@ -77,8 +82,7 @@ namespace StardewModdingAPI /// This must be checked *before* any references to , and this method should not reference itself to avoid errors in Mono. private static void AssertGamePresent() { - Platform platform = EnvironmentUtility.DetectPlatform(); - string gameAssemblyName = platform == Platform.Windows ? "Stardew Valley" : "StardewValley"; + string gameAssemblyName = Program.GetExecutableAssemblyName(); if (Type.GetType($"StardewValley.Game1, {gameAssemblyName}", throwOnError: false) == null) Program.PrintErrorAndExit("Oops! SMAPI can't find the game. Make sure you're running StardewModdingAPI.exe in your game folder. See the readme.txt file for details."); } @@ -102,6 +106,13 @@ namespace StardewModdingAPI } + /// Get the game's executable assembly name. + private static string GetExecutableAssemblyName() + { + Platform platform = EnvironmentUtility.DetectPlatform(); + return platform == Platform.Windows ? "Stardew Valley" : "StardewValley"; + } + /// Initialize SMAPI and launch the game. /// The command-line arguments. /// This method is separate from because that can't contain any references to assemblies loaded by (e.g. via ), or Mono will incorrectly show an assembly resolution error before assembly resolution is set up.