add friendly error for BadImageFormatException on launch

This commit is contained in:
Jesse Plamondon-Willard 2019-11-04 13:59:34 -05:00
parent e454de1e11
commit 01c612bc4a
No known key found for this signature in database
GPG Key ID: CF8B1456B3E29F49
2 changed files with 14 additions and 2 deletions

View File

@ -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.

View File

@ -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
/// <remarks>This must be checked *before* any references to <see cref="Constants"/>, and this method should not reference <see cref="Constants"/> itself to avoid errors in Mono.</remarks>
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
}
/// <summary>Get the game's executable assembly name.</summary>
private static string GetExecutableAssemblyName()
{
Platform platform = EnvironmentUtility.DetectPlatform();
return platform == Platform.Windows ? "Stardew Valley" : "StardewValley";
}
/// <summary>Initialize SMAPI and launch the game.</summary>
/// <param name="args">The command-line arguments.</param>
/// <remarks>This method is separate from <see cref="Main"/> because that can't contain any references to assemblies loaded by <see cref="CurrentDomain_AssemblyResolve"/> (e.g. via <see cref="Constants"/>), or Mono will incorrectly show an assembly resolution error before assembly resolution is set up.</remarks>