summaryrefslogtreecommitdiff
path: root/src/StardewModdingAPI/Program.cs
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <github@jplamondonw.com>2016-10-27 17:10:43 -0400
committerJesse Plamondon-Willard <github@jplamondonw.com>2016-10-27 17:10:43 -0400
commita68f41c39603c29abf9a67e5ba8e655e50330a0c (patch)
tree05fa91da4bef69c7d31fbf49a3fe59c11524fd36 /src/StardewModdingAPI/Program.cs
parent6cc8c6d7c19e849aeac10e6399c4a48a5d954755 (diff)
downloadSMAPI-a68f41c39603c29abf9a67e5ba8e655e50330a0c.tar.gz
SMAPI-a68f41c39603c29abf9a67e5ba8e655e50330a0c.tar.bz2
SMAPI-a68f41c39603c29abf9a67e5ba8e655e50330a0c.zip
fix crossthread access violation when debugger is attached (#126)
Diffstat (limited to 'src/StardewModdingAPI/Program.cs')
-rw-r--r--src/StardewModdingAPI/Program.cs82
1 files changed, 47 insertions, 35 deletions
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);
+ }
}
/// <summary>
@@ -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
+}