From a68f41c39603c29abf9a67e5ba8e655e50330a0c Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Thu, 27 Oct 2016 17:10:43 -0400 Subject: fix crossthread access violation when debugger is attached (#126) --- src/StardewModdingAPI/Program.cs | 82 +++++++++++++++++++++++----------------- 1 file changed, 47 insertions(+), 35 deletions(-) (limited to 'src') diff --git a/src/StardewModdingAPI/Program.cs b/src/StardewModdingAPI/Program.cs index ef273763..cd062f8b 100644 --- a/src/StardewModdingAPI/Program.cs +++ b/src/StardewModdingAPI/Program.cs @@ -124,17 +124,47 @@ namespace StardewModdingAPI Log.AsyncY("Injecting New SDV Version..."); Game1.version += $"-Z_MODDED | SMAPI {Constants.Version.VersionString}"; - // initialise after game launches - new Thread(() => + // add error interceptors +#if SMAPI_FOR_WINDOWS + Application.ThreadException += Log.Application_ThreadException; + Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); +#endif + AppDomain.CurrentDomain.UnhandledException += Log.CurrentDomain_UnhandledException; + + // initialise game + try { - // Wait for the game to load up - while (!ready) Thread.Sleep(1000); + Log.AsyncY("Initializing SDV..."); + gamePtr = new SGame(); + + // hook events + gamePtr.Exiting += (sender, e) => ready = false; + gamePtr.Window.ClientSizeChanged += GraphicsEvents.InvokeResize; + + // patch graphics + Log.AsyncY("Patching SDV Graphics Profile..."); + Game1.graphics.GraphicsProfile = GraphicsProfile.HiDef; + + // load mods + LoadMods(); - // Apply final tweaks + // initialise + StardewGameInfo.SetValue(StardewProgramType, gamePtr); Log.AsyncY("Applying Final SDV Tweaks..."); gamePtr.IsMouseVisible = false; gamePtr.Window.Title = "Stardew Valley - Version " + Game1.version; - gamePtr.Window.ClientSizeChanged += GraphicsEvents.InvokeResize; + } + catch (Exception ex) + { + Log.AsyncR("Game failed to initialise: " + ex); + return; + } + + // initialise after game launches + new Thread(() => + { + // Wait for the game to load up + while (!ready) Thread.Sleep(1000); // Create definition to listen for input Log.AsyncY("Initializing Console Input Thread..."); @@ -169,7 +199,16 @@ namespace StardewModdingAPI // Start game loop Log.AsyncY("Starting SDV..."); - RunGame(); + try + { + ready = true; + gamePtr.Run(); + } + catch (Exception ex) + { + ready = false; + Log.AsyncR("Game failed to start: " + ex); + } } /// @@ -193,33 +232,6 @@ namespace StardewModdingAPI ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - public static void RunGame() - { -#if SMAPI_FOR_WINDOWS - Application.ThreadException += Log.Application_ThreadException; - Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); -#endif - AppDomain.CurrentDomain.UnhandledException += Log.CurrentDomain_UnhandledException; - - try - { - gamePtr = new SGame(); - Log.AsyncY("Patching SDV Graphics Profile..."); - Game1.graphics.GraphicsProfile = GraphicsProfile.HiDef; - LoadMods(); - - ready = true; - gamePtr.Exiting += (sender, e) => ready = false; - - StardewGameInfo.SetValue(StardewProgramType, gamePtr); - gamePtr.Run(); - } - catch (Exception ex) - { - Log.AsyncR("Game failed to start: " + ex); - } - } - public static void LoadMods() { Log.AsyncY("LOADING MODS"); @@ -357,4 +369,4 @@ namespace StardewModdingAPI Log.AsyncY("Commands: " + Command.RegisteredCommands.Select(x => x.CommandName).ToSingular()); } } -} \ No newline at end of file +} -- cgit