From 6cc8c6d7c19e849aeac10e6399c4a48a5d954755 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Thu, 27 Oct 2016 15:17:17 -0400 Subject: run game on main thread for Mac compatibility (#126) --- src/StardewModdingAPI/Program.cs | 113 +++++++++++++++++---------------------- 1 file changed, 49 insertions(+), 64 deletions(-) (limited to 'src/StardewModdingAPI/Program.cs') diff --git a/src/StardewModdingAPI/Program.cs b/src/StardewModdingAPI/Program.cs index b8a8b856..ef273763 100644 --- a/src/StardewModdingAPI/Program.cs +++ b/src/StardewModdingAPI/Program.cs @@ -57,9 +57,7 @@ namespace StardewModdingAPI Log.AsyncY("SMAPI Version: " + Constants.Version.VersionString); ConfigureUI(); ConfigurePaths(); - ConfigureSDV(); - - GameRunInvoker(); + StartGame(); } catch (Exception e) { @@ -112,13 +110,12 @@ namespace StardewModdingAPI } /// - /// Load Stardev Valley and control features + /// Load Stardev Valley and control features, and launch the game. /// - private static void ConfigureSDV() + private static void StartGame() { - Log.AsyncY("Initializing SDV Assembly..."); - // Load in the assembly - ignores security + Log.AsyncY("Initializing SDV Assembly..."); StardewAssembly = Assembly.UnsafeLoadFrom(GameExecutablePath); StardewProgramType = StardewAssembly.GetType("StardewValley.Program", true); StardewGameInfo = StardewProgramType.GetField("gamePtr"); @@ -127,64 +124,52 @@ namespace StardewModdingAPI Log.AsyncY("Injecting New SDV Version..."); Game1.version += $"-Z_MODDED | SMAPI {Constants.Version.VersionString}"; - // Create the thread for the game to run in. - gameThread = new Thread(RunGame); - Log.AsyncY("Starting SDV..."); - gameThread.Start(); - - // Wait for the game to load up - while (!ready) + // initialise after game launches + new Thread(() => { - } - - //SDV is running - Log.AsyncY("SDV Loaded Into Memory"); - - //Create definition to listen for input - Log.AsyncY("Initializing Console Input Thread..."); - consoleInputThread = new Thread(ConsoleInputThread); - - // The only command in the API (at least it should be, for now) - Command.RegisterCommand("help", "Lists all commands | 'help ' returns command description").CommandFired += help_CommandFired; - - //Subscribe to events - ControlEvents.KeyPressed += Events_KeyPressed; - GameEvents.LoadContent += Events_LoadContent; - - Log.AsyncY("Applying Final SDV Tweaks..."); - gamePtr.IsMouseVisible = false; - gamePtr.Window.Title = "Stardew Valley - Version " + Game1.version; - gamePtr.Window.ClientSizeChanged += GraphicsEvents.InvokeResize; - } - - /// - /// Wrap the 'RunGame' method for console output - /// - private static void GameRunInvoker() - { - //Game's in memory now, send the event - Log.AsyncY("Game Loaded"); - GameEvents.InvokeGameLoaded(); - - Log.AsyncY("Type 'help' for help, or 'help ' for a command's usage"); - //Begin listening to input - consoleInputThread.Start(); - - - while (ready) - { - //Check if the game is still running 10 times a second - Thread.Sleep(1000 / 10); - } - - //abort the thread, we're closing - if (consoleInputThread != null && consoleInputThread.ThreadState == ThreadState.Running) - consoleInputThread.Abort(); - - Log.AsyncY("Game Execution Finished"); - Log.AsyncY("Shutting Down..."); - Thread.Sleep(100); - Environment.Exit(0); + // Wait for the game to load up + while (!ready) Thread.Sleep(1000); + + // Apply final tweaks + Log.AsyncY("Applying Final SDV Tweaks..."); + gamePtr.IsMouseVisible = false; + gamePtr.Window.Title = "Stardew Valley - Version " + Game1.version; + gamePtr.Window.ClientSizeChanged += GraphicsEvents.InvokeResize; + + // Create definition to listen for input + Log.AsyncY("Initializing Console Input Thread..."); + consoleInputThread = new Thread(ConsoleInputThread); + + // The only command in the API (at least it should be, for now) + Command.RegisterCommand("help", "Lists all commands | 'help ' returns command description").CommandFired += help_CommandFired; + + // Subscribe to events + ControlEvents.KeyPressed += Events_KeyPressed; + GameEvents.LoadContent += Events_LoadContent; + + // Game's in memory now, send the event + Log.AsyncY("Game Loaded"); + GameEvents.InvokeGameLoaded(); + + // Listen for command line input + Log.AsyncY("Type 'help' for help, or 'help ' for a command's usage"); + consoleInputThread.Start(); + while (ready) + Thread.Sleep(1000 / 10); // Check if the game is still running 10 times a second + + // Abort the thread, we're closing + if (consoleInputThread != null && consoleInputThread.ThreadState == ThreadState.Running) + consoleInputThread.Abort(); + + Log.AsyncY("Game Execution Finished"); + Log.AsyncY("Shutting Down..."); + Thread.Sleep(100); + Environment.Exit(0); + }).Start(); + + // Start game loop + Log.AsyncY("Starting SDV..."); + RunGame(); } /// -- cgit