From c94b51cd2284db8a254c6fa81ae96c9a5adc67d5 Mon Sep 17 00:00:00 2001 From: Zoryn Aaron Date: Mon, 28 Mar 2016 22:57:05 -0400 Subject: optimize reflection a little --- StardewModdingAPI/Constants.cs | 2 +- StardewModdingAPI/Inheritance/SGame.cs | 83 +++++++++++++++++++++++++++++----- 2 files changed, 73 insertions(+), 12 deletions(-) diff --git a/StardewModdingAPI/Constants.cs b/StardewModdingAPI/Constants.cs index 4a01c38a..83789b85 100644 --- a/StardewModdingAPI/Constants.cs +++ b/StardewModdingAPI/Constants.cs @@ -10,7 +10,7 @@ namespace StardewModdingAPI /// public static class Constants { - public static readonly Version Version = new Version(0, 39, 3, "Alpha"); + public static readonly Version Version = new Version(0, 39, 4, "Alpha"); /// /// Not quite "constant", but it makes more sense for it to be here, at least for now diff --git a/StardewModdingAPI/Inheritance/SGame.cs b/StardewModdingAPI/Inheritance/SGame.cs index 42de205d..8eb7ad62 100644 --- a/StardewModdingAPI/Inheritance/SGame.cs +++ b/StardewModdingAPI/Inheritance/SGame.cs @@ -391,6 +391,34 @@ namespace StardewModdingAPI.Inheritance return buttons.ToArray(); } + /// + /// + /// + public static MethodInfo DrawFarmBuildings = typeof (Game1).GetMethod("drawFarmBuildings", BindingFlags.NonPublic | BindingFlags.Instance); + + /// + /// + /// + public static MethodInfo DrawHUD = typeof (Game1).GetMethod("drawHUD", BindingFlags.NonPublic | BindingFlags.Instance); + + /// + /// + /// + public static MethodInfo DrawDialogueBox = typeof (Game1).GetMethod("drawDialogueBox", BindingFlags.NonPublic | BindingFlags.Instance); + + public static MethodInfo CheckForEscapeKeys = typeof (Game1).GetMethod("checkForEscapeKeys", BindingFlags.NonPublic | BindingFlags.Instance); + + public static MethodInfo UpdateControlInput = typeof(Game1).GetMethod("UpdateControlInput", BindingFlags.NonPublic | BindingFlags.Instance); + + public static MethodInfo UpdateCharacters = typeof(Game1).GetMethod("UpdateCharacters", BindingFlags.NonPublic | BindingFlags.Instance); + + public static MethodInfo UpdateLocations = typeof(Game1).GetMethod("UpdateLocations", BindingFlags.NonPublic | BindingFlags.Instance); + + public static MethodInfo getViewportCenter = typeof(Game1).GetMethod("getViewportCenter", BindingFlags.NonPublic | BindingFlags.Instance); + + public static MethodInfo UpdateTitleScreen = typeof(Game1).GetMethod("UpdateTitleScreen", BindingFlags.NonPublic | BindingFlags.Instance); + + /// /// Whether or not the game's zoom level is 1.0f /// @@ -468,7 +496,9 @@ namespace StardewModdingAPI.Inheritance client.receiveMessages(); } if (IsActive) - InvokeBasePrivateInstancedMethod("checkForEscapeKeys"); + InvokeMethodInfo(CheckForEscapeKeys); + //InvokeBasePrivateInstancedMethod("checkForEscapeKeys"); + //this.checkForEscapeKeys(); updateMusic(); updateRaindropPosition(); @@ -514,7 +544,8 @@ namespace StardewModdingAPI.Inheritance } } else - InvokeBasePrivateInstancedMethod("UpdateControlInput", gameTime); + InvokeMethodInfo(UpdateControlInput, gameTime); + //InvokeBasePrivateInstancedMethod("UpdateControlInput", gameTime); //this.UpdateControlInput(gameTime); } else if (pauseThenDoFunctionTimer > 0) @@ -693,7 +724,8 @@ namespace StardewModdingAPI.Inheritance if (pauseTime > 0.0) updatePause(gameTime); if (!globalFade && !freezeControls && (activeClickableMenu == null && IsActive)) - InvokeBasePrivateInstancedMethod("UpdateControlInput", gameTime); + InvokeMethodInfo(UpdateControlInput, gameTime); + //InvokeBasePrivateInstancedMethod("UpdateControlInput", gameTime); //this.UpdateControlInput(gameTime); } if (showingEndOfNightStuff && endOfNightMenus.Count() == 0 && activeClickableMenu == null) @@ -707,9 +739,13 @@ namespace StardewModdingAPI.Inheritance UpdateGameClock(gameTime); //this.UpdateCharacters(gameTime); //this.UpdateLocations(gameTime); - InvokeBasePrivateInstancedMethod("UpdateCharacters", gameTime); - InvokeBasePrivateInstancedMethod("UpdateLocations", gameTime); - UpdateViewPort(false, (Point)InvokeBasePrivateInstancedMethod("getViewportCenter")); + //InvokeBasePrivateInstancedMethod("UpdateCharacters", gameTime); + //InvokeBasePrivateInstancedMethod("UpdateLocations", gameTime); + //UpdateViewPort(false, (Point)InvokeBasePrivateInstancedMethod("getViewportCenter")); + + InvokeMethodInfo(UpdateCharacters, gameTime); + InvokeMethodInfo(UpdateLocations, gameTime); + UpdateViewPort(false, (Point)InvokeMethodInfo(getViewportCenter)); } UpdateOther(gameTime); if (messagePause) @@ -726,7 +762,8 @@ namespace StardewModdingAPI.Inheritance } else { - InvokeBasePrivateInstancedMethod("UpdateTitleScreen", gameTime); + //InvokeBasePrivateInstancedMethod("UpdateTitleScreen", gameTime); + InvokeMethodInfo(UpdateTitleScreen, gameTime); //this.UpdateTitleScreen(gameTime); if (activeClickableMenu != null) updateActiveMenu(gameTime); @@ -1008,7 +1045,8 @@ namespace StardewModdingAPI.Inheritance } if (currentLocation.Name.Equals("Farm")) { - typeof (Game1).GetMethod("drawFarmBuildings", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(Program.gamePtr, null); + //typeof (Game1).GetMethod("drawFarmBuildings", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(Program.gamePtr, null); + DrawFarmBuildings.Invoke(Program.gamePtr, null); //this.drawFarmBuildings(); } if (tvStation >= 0) @@ -1166,7 +1204,8 @@ namespace StardewModdingAPI.Inheritance if ((displayHUD || eventUp) && currentBillboard == 0 && gameMode == 3 && !freezeControls && !panMode) { GraphicsEvents.InvokeOnPreRenderHudEvent(null, EventArgs.Empty); - typeof (Game1).GetMethod("drawHUD", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(Program.gamePtr, null); + //typeof (Game1).GetMethod("drawHUD", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(Program.gamePtr, null); + DrawHUD.Invoke(Program.gamePtr, null); GraphicsEvents.InvokeOnPostRenderHudEvent(null, EventArgs.Empty); //this.drawHUD(); } @@ -1185,7 +1224,8 @@ namespace StardewModdingAPI.Inheritance farmEvent?.draw(spriteBatch); if (dialogueUp && !nameSelectUp && !messagePause && !(activeClickableMenu is DialogueBox)) { - typeof (Game1).GetMethod("drawDialogueBox", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(Program.gamePtr, null); + //typeof (Game1).GetMethod("drawDialogueBox", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(Program.gamePtr, null); + DrawDialogueBox.Invoke(Program.gamePtr, null); //this.drawDialogueBox(); } if (progressBar) @@ -1215,7 +1255,8 @@ namespace StardewModdingAPI.Inheritance } if ((messagePause || globalFade) && dialogueUp) { - typeof (Game1).GetMethod("drawDialogueBox", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(Program.gamePtr, null); + //typeof (Game1).GetMethod("drawDialogueBox", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(Program.gamePtr, null); + DrawDialogueBox.Invoke(Program.gamePtr, null); //this.drawDialogueBox(); } foreach (TemporaryAnimatedSprite current8 in screenOverlayTempSprites) @@ -1607,6 +1648,7 @@ namespace StardewModdingAPI.Inheritance /// The name of the method /// Any parameters needed /// Whatever the method normally returns. Null if void. + [Obsolete("This is very slow. Cache the method info and then invoke it with InvokeMethodInfo().")] public static object InvokeBasePrivateInstancedMethod(string name, params object[] parameters) { try @@ -1620,6 +1662,25 @@ namespace StardewModdingAPI.Inheritance } } + /// + /// Invokes a given method info with the supplied parameters + /// + /// + /// + /// + public static object InvokeMethodInfo(MethodInfo mi, params object[] parameters) + { + try + { + return mi.Invoke(Program.gamePtr, parameters); + } + catch + { + Log.AsyncR("Failed to call base method: " + mi.Name); + return null; + } + } + /// /// Queue's a message to be drawn in Debug mode (F3) /// -- cgit From d952f836430933ca7b7e378405f51015007804f9 Mon Sep 17 00:00:00 2001 From: Zoryn Aaron Date: Mon, 28 Mar 2016 23:23:09 -0400 Subject: Some fixes I guess --- StardewModdingAPI/Inheritance/SGame.cs | 1282 ++++++++++++++++---------------- 1 file changed, 656 insertions(+), 626 deletions(-) diff --git a/StardewModdingAPI/Inheritance/SGame.cs b/StardewModdingAPI/Inheritance/SGame.cs index 8eb7ad62..c346157c 100644 --- a/StardewModdingAPI/Inheritance/SGame.cs +++ b/StardewModdingAPI/Inheritance/SGame.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Reflection; +using System.Runtime.CompilerServices; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; @@ -418,6 +419,7 @@ namespace StardewModdingAPI.Inheritance public static MethodInfo UpdateTitleScreen = typeof(Game1).GetMethod("UpdateTitleScreen", BindingFlags.NonPublic | BindingFlags.Instance); + public delegate void BaseBaseDraw(); /// /// Whether or not the game's zoom level is 1.0f @@ -458,11 +460,11 @@ namespace StardewModdingAPI.Inheritance QueueDebugMessage("FPS: " + FramesPerSecond); UpdateEventCalls(); - /*if (ZoomLevelIsOne) + if (ZoomLevelIsOne) { options.zoomLevel = 0.99f; InvokeBasePrivateInstancedMethod("Window_ClientSizeChanged", null, null); - }*/ + } if (FramePressedKeys.Contains(Keys.F3)) { @@ -479,163 +481,185 @@ namespace StardewModdingAPI.Inheritance { #region Overridden Update Call - if (Program.BuildType == 0) - SteamHelper.update(); - if ((paused /*|| !this.IsActive*/) && (options == null || options.pauseWhenOutOfFocus || paused)) - return; - if (quit) - Exit(); - currentGameTime = gameTime; - if (gameMode != 11) + try { - if (IsMultiplayer && gameMode == 3) + if (Program.BuildType == 0) + SteamHelper.update(); + if ((paused /*|| !this.IsActive*/) && (options == null || options.pauseWhenOutOfFocus || paused)) + return; + if (quit) + Exit(); + currentGameTime = gameTime; + if (gameMode != 11) { - if (multiplayerMode == 2) - server.receiveMessages(); - else - client.receiveMessages(); - } - if (IsActive) - InvokeMethodInfo(CheckForEscapeKeys); + if (IsMultiplayer && gameMode == 3) + { + if (multiplayerMode == 2) + server.receiveMessages(); + else + client.receiveMessages(); + } + if (IsActive) + InvokeMethodInfo(CheckForEscapeKeys); //InvokeBasePrivateInstancedMethod("checkForEscapeKeys"); - + //this.checkForEscapeKeys(); - updateMusic(); - updateRaindropPosition(); - if (bloom != null) - bloom.tick(gameTime); - if (globalFade) - { - if (!dialogueUp) + updateMusic(); + updateRaindropPosition(); + if (bloom != null) + bloom.tick(gameTime); + if (globalFade) { - if (fadeIn) + if (!dialogueUp) { - fadeToBlackAlpha = Math.Max(0.0f, fadeToBlackAlpha - globalFadeSpeed); - if (fadeToBlackAlpha <= 0.0) + if (fadeIn) { - globalFade = false; - if (afterFade != null) + fadeToBlackAlpha = Math.Max(0.0f, fadeToBlackAlpha - globalFadeSpeed); + if (fadeToBlackAlpha <= 0.0) { - afterFadeFunction afterFadeFunction = afterFade; - afterFade(); - if (afterFade != null && afterFade.Equals(afterFadeFunction)) - afterFade = null; - if (nonWarpFade) - fadeToBlack = false; + globalFade = false; + if (afterFade != null) + { + afterFadeFunction afterFadeFunction = afterFade; + afterFade(); + if (afterFade != null && afterFade.Equals(afterFadeFunction)) + afterFade = null; + if (nonWarpFade) + fadeToBlack = false; + } } } - } - else - { - fadeToBlackAlpha = Math.Min(1f, fadeToBlackAlpha + globalFadeSpeed); - if (fadeToBlackAlpha >= 1.0) + else { - globalFade = false; - if (afterFade != null) + fadeToBlackAlpha = Math.Min(1f, fadeToBlackAlpha + globalFadeSpeed); + if (fadeToBlackAlpha >= 1.0) { - afterFadeFunction afterFadeFunction = afterFade; - afterFade(); - if (afterFade != null && afterFade.Equals(afterFadeFunction)) - afterFade = null; - if (nonWarpFade) - fadeToBlack = false; + globalFade = false; + if (afterFade != null) + { + afterFadeFunction afterFadeFunction = afterFade; + afterFade(); + if (afterFade != null && afterFade.Equals(afterFadeFunction)) + afterFade = null; + if (nonWarpFade) + fadeToBlack = false; + } } } } + else + InvokeMethodInfo(UpdateControlInput, gameTime); + //InvokeBasePrivateInstancedMethod("UpdateControlInput", gameTime); + //this.UpdateControlInput(gameTime); } - else - InvokeMethodInfo(UpdateControlInput, gameTime); - //InvokeBasePrivateInstancedMethod("UpdateControlInput", gameTime); - //this.UpdateControlInput(gameTime); - } - else if (pauseThenDoFunctionTimer > 0) - { - freezeControls = true; - pauseThenDoFunctionTimer -= gameTime.ElapsedGameTime.Milliseconds; - if (pauseThenDoFunctionTimer <= 0) - { - freezeControls = false; - if (afterPause != null) - afterPause(); - } - } - if (gameMode == 3 || gameMode == 2) - { - player.millisecondsPlayed += (uint)gameTime.ElapsedGameTime.Milliseconds; - bool flag = true; - if (currentMinigame != null) + else if (pauseThenDoFunctionTimer > 0) { - if (pauseTime > 0.0) - updatePause(gameTime); - if (fadeToBlack) + freezeControls = true; + pauseThenDoFunctionTimer -= gameTime.ElapsedGameTime.Milliseconds; + if (pauseThenDoFunctionTimer <= 0) { - updateScreenFade(gameTime); - if (fadeToBlackAlpha >= 1.0) - fadeToBlack = false; + freezeControls = false; + if (afterPause != null) + afterPause(); } - else + } + if (gameMode == 3 || gameMode == 2) + { + player.millisecondsPlayed += (uint) gameTime.ElapsedGameTime.Milliseconds; + bool flag = true; + if (currentMinigame != null) { - if (ThumbstickMotionMargin > 0) - ThumbstickMotionMargin -= gameTime.ElapsedGameTime.Milliseconds; - if (IsActive) + if (pauseTime > 0.0) + updatePause(gameTime); + if (fadeToBlack) { - KeyboardState state1 = Keyboard.GetState(); - MouseState state2 = Mouse.GetState(); - GamePadState state3 = GamePad.GetState(PlayerIndex.One); - foreach (Keys keys in state1.GetPressedKeys()) - { - if (!oldKBState.IsKeyDown(keys)) - currentMinigame.receiveKeyPress(keys); - } - if (options.gamepadControls) + updateScreenFade(gameTime); + if (fadeToBlackAlpha >= 1.0) + fadeToBlack = false; + } + else + { + if (ThumbstickMotionMargin > 0) + ThumbstickMotionMargin -= gameTime.ElapsedGameTime.Milliseconds; + if (IsActive) { - if (currentMinigame == null) + KeyboardState state1 = Keyboard.GetState(); + MouseState state2 = Mouse.GetState(); + GamePadState state3 = GamePad.GetState(PlayerIndex.One); + foreach (Keys keys in state1.GetPressedKeys()) { - oldMouseState = state2; - oldKBState = state1; - oldPadState = state3; - return; + if (!oldKBState.IsKeyDown(keys)) + currentMinigame.receiveKeyPress(keys); } - foreach (Buttons b in Utility.getPressedButtons(state3, oldPadState)) - currentMinigame.receiveKeyPress(Utility.mapGamePadButtonToKey(b)); - if (currentMinigame == null) + if (options.gamepadControls) { - oldMouseState = state2; - oldKBState = state1; - oldPadState = state3; - return; + if (currentMinigame == null) + { + oldMouseState = state2; + oldKBState = state1; + oldPadState = state3; + return; + } + foreach (Buttons b in Utility.getPressedButtons(state3, oldPadState)) + currentMinigame.receiveKeyPress(Utility.mapGamePadButtonToKey(b)); + if (currentMinigame == null) + { + oldMouseState = state2; + oldKBState = state1; + oldPadState = state3; + return; + } + if (state3.ThumbSticks.Right.Y < -0.200000002980232 && oldPadState.ThumbSticks.Right.Y >= -0.200000002980232) + currentMinigame.receiveKeyPress(Keys.Down); + if (state3.ThumbSticks.Right.Y > 0.200000002980232 && oldPadState.ThumbSticks.Right.Y <= 0.200000002980232) + currentMinigame.receiveKeyPress(Keys.Up); + if (state3.ThumbSticks.Right.X < -0.200000002980232 && oldPadState.ThumbSticks.Right.X >= -0.200000002980232) + currentMinigame.receiveKeyPress(Keys.Left); + if (state3.ThumbSticks.Right.X > 0.200000002980232 && oldPadState.ThumbSticks.Right.X <= 0.200000002980232) + currentMinigame.receiveKeyPress(Keys.Right); + if (oldPadState.ThumbSticks.Right.Y < -0.200000002980232 && state3.ThumbSticks.Right.Y >= -0.200000002980232) + currentMinigame.receiveKeyRelease(Keys.Down); + if (oldPadState.ThumbSticks.Right.Y > 0.200000002980232 && state3.ThumbSticks.Right.Y <= 0.200000002980232) + currentMinigame.receiveKeyRelease(Keys.Up); + if (oldPadState.ThumbSticks.Right.X < -0.200000002980232 && state3.ThumbSticks.Right.X >= -0.200000002980232) + currentMinigame.receiveKeyRelease(Keys.Left); + if (oldPadState.ThumbSticks.Right.X > 0.200000002980232 && state3.ThumbSticks.Right.X <= 0.200000002980232) + currentMinigame.receiveKeyRelease(Keys.Right); + if (isGamePadThumbstickInMotion()) + { + setMousePosition(getMouseX() + (int) (state3.ThumbSticks.Left.X * 16.0), getMouseY() - (int) (state3.ThumbSticks.Left.Y * 16.0)); + lastCursorMotionWasMouse = false; + } + else if (getMousePosition().X != getOldMouseX() || getMousePosition().Y != getOldMouseY()) + lastCursorMotionWasMouse = true; } - if (state3.ThumbSticks.Right.Y < -0.200000002980232 && oldPadState.ThumbSticks.Right.Y >= -0.200000002980232) - currentMinigame.receiveKeyPress(Keys.Down); - if (state3.ThumbSticks.Right.Y > 0.200000002980232 && oldPadState.ThumbSticks.Right.Y <= 0.200000002980232) - currentMinigame.receiveKeyPress(Keys.Up); - if (state3.ThumbSticks.Right.X < -0.200000002980232 && oldPadState.ThumbSticks.Right.X >= -0.200000002980232) - currentMinigame.receiveKeyPress(Keys.Left); - if (state3.ThumbSticks.Right.X > 0.200000002980232 && oldPadState.ThumbSticks.Right.X <= 0.200000002980232) - currentMinigame.receiveKeyPress(Keys.Right); - if (oldPadState.ThumbSticks.Right.Y < -0.200000002980232 && state3.ThumbSticks.Right.Y >= -0.200000002980232) - currentMinigame.receiveKeyRelease(Keys.Down); - if (oldPadState.ThumbSticks.Right.Y > 0.200000002980232 && state3.ThumbSticks.Right.Y <= 0.200000002980232) - currentMinigame.receiveKeyRelease(Keys.Up); - if (oldPadState.ThumbSticks.Right.X < -0.200000002980232 && state3.ThumbSticks.Right.X >= -0.200000002980232) - currentMinigame.receiveKeyRelease(Keys.Left); - if (oldPadState.ThumbSticks.Right.X > 0.200000002980232 && state3.ThumbSticks.Right.X <= 0.200000002980232) - currentMinigame.receiveKeyRelease(Keys.Right); - if (isGamePadThumbstickInMotion()) + foreach (Keys keys in oldKBState.GetPressedKeys()) { - setMousePosition(getMouseX() + (int)(state3.ThumbSticks.Left.X * 16.0), getMouseY() - (int)(state3.ThumbSticks.Left.Y * 16.0)); - lastCursorMotionWasMouse = false; + if (!state1.IsKeyDown(keys)) + currentMinigame.receiveKeyRelease(keys); + } + if (options.gamepadControls) + { + if (currentMinigame == null) + { + oldMouseState = state2; + oldKBState = state1; + oldPadState = state3; + return; + } + if (state3.IsConnected && state3.IsButtonDown(Buttons.X) && !oldPadState.IsButtonDown(Buttons.X)) + currentMinigame.receiveRightClick(getMouseX(), getMouseY(), true); + else if (state3.IsConnected && state3.IsButtonDown(Buttons.A) && !oldPadState.IsButtonDown(Buttons.A)) + currentMinigame.receiveLeftClick(getMouseX(), getMouseY(), true); + else if (state3.IsConnected && !state3.IsButtonDown(Buttons.X) && oldPadState.IsButtonDown(Buttons.X)) + currentMinigame.releaseRightClick(getMouseX(), getMouseY()); + else if (state3.IsConnected && !state3.IsButtonDown(Buttons.A) && oldPadState.IsButtonDown(Buttons.A)) + currentMinigame.releaseLeftClick(getMouseX(), getMouseY()); + foreach (Buttons b in Utility.getPressedButtons(oldPadState, state3)) + currentMinigame.receiveKeyRelease(Utility.mapGamePadButtonToKey(b)); + if (state3.IsConnected && state3.IsButtonDown(Buttons.A) && currentMinigame != null) + currentMinigame.leftClickHeld(0, 0); } - else if (getMousePosition().X != getOldMouseX() || getMousePosition().Y != getOldMouseY()) - lastCursorMotionWasMouse = true; - } - foreach (Keys keys in oldKBState.GetPressedKeys()) - { - if (!state1.IsKeyDown(keys)) - currentMinigame.receiveKeyRelease(keys); - } - if (options.gamepadControls) - { if (currentMinigame == null) { oldMouseState = state2; @@ -643,137 +667,122 @@ namespace StardewModdingAPI.Inheritance oldPadState = state3; return; } - if (state3.IsConnected && state3.IsButtonDown(Buttons.X) && !oldPadState.IsButtonDown(Buttons.X)) - currentMinigame.receiveRightClick(getMouseX(), getMouseY(), true); - else if (state3.IsConnected && state3.IsButtonDown(Buttons.A) && !oldPadState.IsButtonDown(Buttons.A)) + if (state2.LeftButton == ButtonState.Pressed && oldMouseState.LeftButton != ButtonState.Pressed) currentMinigame.receiveLeftClick(getMouseX(), getMouseY(), true); - else if (state3.IsConnected && !state3.IsButtonDown(Buttons.X) && oldPadState.IsButtonDown(Buttons.X)) - currentMinigame.releaseRightClick(getMouseX(), getMouseY()); - else if (state3.IsConnected && !state3.IsButtonDown(Buttons.A) && oldPadState.IsButtonDown(Buttons.A)) + if (state2.RightButton == ButtonState.Pressed && oldMouseState.RightButton != ButtonState.Pressed) + currentMinigame.receiveRightClick(getMouseX(), getMouseY(), true); + if (state2.LeftButton == ButtonState.Released && oldMouseState.LeftButton == ButtonState.Pressed) currentMinigame.releaseLeftClick(getMouseX(), getMouseY()); - foreach (Buttons b in Utility.getPressedButtons(oldPadState, state3)) - currentMinigame.receiveKeyRelease(Utility.mapGamePadButtonToKey(b)); - if (state3.IsConnected && state3.IsButtonDown(Buttons.A) && currentMinigame != null) - currentMinigame.leftClickHeld(0, 0); - } - if (currentMinigame == null) - { + if (state2.RightButton == ButtonState.Released && oldMouseState.RightButton == ButtonState.Pressed) + currentMinigame.releaseLeftClick(getMouseX(), getMouseY()); + if (state2.LeftButton == ButtonState.Pressed && oldMouseState.LeftButton == ButtonState.Pressed) + currentMinigame.leftClickHeld(getMouseX(), getMouseY()); oldMouseState = state2; oldKBState = state1; oldPadState = state3; + } + if (currentMinigame != null && currentMinigame.tick(gameTime)) + { + currentMinigame.unload(); + currentMinigame = null; + fadeIn = true; + fadeToBlackAlpha = 1f; return; } - if (state2.LeftButton == ButtonState.Pressed && oldMouseState.LeftButton != ButtonState.Pressed) - currentMinigame.receiveLeftClick(getMouseX(), getMouseY(), true); - if (state2.RightButton == ButtonState.Pressed && oldMouseState.RightButton != ButtonState.Pressed) - currentMinigame.receiveRightClick(getMouseX(), getMouseY(), true); - if (state2.LeftButton == ButtonState.Released && oldMouseState.LeftButton == ButtonState.Pressed) - currentMinigame.releaseLeftClick(getMouseX(), getMouseY()); - if (state2.RightButton == ButtonState.Released && oldMouseState.RightButton == ButtonState.Pressed) - currentMinigame.releaseLeftClick(getMouseX(), getMouseY()); - if (state2.LeftButton == ButtonState.Pressed && oldMouseState.LeftButton == ButtonState.Pressed) - currentMinigame.leftClickHeld(getMouseX(), getMouseY()); - oldMouseState = state2; - oldKBState = state1; - oldPadState = state3; } - if (currentMinigame != null && currentMinigame.tick(gameTime)) + flag = IsMultiplayer; + } + else if (farmEvent != null && farmEvent.tickUpdate(gameTime)) + { + farmEvent.makeChangesToLocation(); + timeOfDay = 600; + UpdateOther(gameTime); + displayHUD = true; + farmEvent = null; + currentLocation = getLocationFromName("FarmHouse"); + player.position = Utility.PointToVector2(Utility.getHomeOfFarmer(player).getBedSpot()) * tileSize; + player.position.X -= tileSize; + changeMusicTrack("none"); + currentLocation.resetForPlayerEntry(); + player.forceCanMove(); + freezeControls = false; + displayFarmer = true; + outdoorLight = Color.White; + viewportFreeze = false; + fadeToBlackAlpha = 0.0f; + fadeToBlack = false; + globalFadeToClear(null, 0.02f); + player.mailForTomorrow.Clear(); + showEndOfNightStuff(); + } + if (flag) + { + if (endOfNightMenus.Count() > 0 && activeClickableMenu == null) + activeClickableMenu = endOfNightMenus.Pop(); + if (activeClickableMenu != null) + { + updateActiveMenu(gameTime); + } + else + { + if (pauseTime > 0.0) + updatePause(gameTime); + if (!globalFade && !freezeControls && (activeClickableMenu == null && IsActive)) + InvokeMethodInfo(UpdateControlInput, gameTime); + //InvokeBasePrivateInstancedMethod("UpdateControlInput", gameTime); + //this.UpdateControlInput(gameTime); + } + if (showingEndOfNightStuff && endOfNightMenus.Count() == 0 && activeClickableMenu == null) + { + showingEndOfNightStuff = false; + globalFadeToClear(playMorningSong, 0.02f); + } + if (!showingEndOfNightStuff) + { + if (IsMultiplayer || activeClickableMenu == null && currentMinigame == null) + UpdateGameClock(gameTime); + //this.UpdateCharacters(gameTime); + //this.UpdateLocations(gameTime); + //InvokeBasePrivateInstancedMethod("UpdateCharacters", gameTime); + //InvokeBasePrivateInstancedMethod("UpdateLocations", gameTime); + //UpdateViewPort(false, (Point)InvokeBasePrivateInstancedMethod("getViewportCenter")); + + InvokeMethodInfo(UpdateCharacters, gameTime); + InvokeMethodInfo(UpdateLocations, gameTime); + UpdateViewPort(false, (Point) InvokeMethodInfo(getViewportCenter)); + } + UpdateOther(gameTime); + if (messagePause) { - currentMinigame.unload(); - currentMinigame = null; - fadeIn = true; - fadeToBlackAlpha = 1f; - return; + KeyboardState state1 = Keyboard.GetState(); + MouseState state2 = Mouse.GetState(); + GamePadState state3 = GamePad.GetState(PlayerIndex.One); + if (isOneOfTheseKeysDown(state1, options.actionButton) && !isOneOfTheseKeysDown(oldKBState, options.actionButton)) + pressActionButton(state1, state2, state3); + oldKBState = state1; + oldPadState = state3; } } - flag = IsMultiplayer; } - else if (farmEvent != null && farmEvent.tickUpdate(gameTime)) - { - farmEvent.makeChangesToLocation(); - timeOfDay = 600; - UpdateOther(gameTime); - displayHUD = true; - farmEvent = null; - currentLocation = getLocationFromName("FarmHouse"); - player.position = Utility.PointToVector2(Utility.getHomeOfFarmer(player).getBedSpot()) * tileSize; - player.position.X -= tileSize; - changeMusicTrack("none"); - currentLocation.resetForPlayerEntry(); - player.forceCanMove(); - freezeControls = false; - displayFarmer = true; - outdoorLight = Color.White; - viewportFreeze = false; - fadeToBlackAlpha = 0.0f; - fadeToBlack = false; - globalFadeToClear(null, 0.02f); - player.mailForTomorrow.Clear(); - showEndOfNightStuff(); - } - if (flag) + else { - if (endOfNightMenus.Count() > 0 && activeClickableMenu == null) - activeClickableMenu = endOfNightMenus.Pop(); + //InvokeBasePrivateInstancedMethod("UpdateTitleScreen", gameTime); + InvokeMethodInfo(UpdateTitleScreen, gameTime); + //this.UpdateTitleScreen(gameTime); if (activeClickableMenu != null) - { updateActiveMenu(gameTime); - } - else - { - if (pauseTime > 0.0) - updatePause(gameTime); - if (!globalFade && !freezeControls && (activeClickableMenu == null && IsActive)) - InvokeMethodInfo(UpdateControlInput, gameTime); - //InvokeBasePrivateInstancedMethod("UpdateControlInput", gameTime); - //this.UpdateControlInput(gameTime); - } - if (showingEndOfNightStuff && endOfNightMenus.Count() == 0 && activeClickableMenu == null) - { - showingEndOfNightStuff = false; - globalFadeToClear(playMorningSong, 0.02f); - } - if (!showingEndOfNightStuff) - { - if (IsMultiplayer || activeClickableMenu == null && currentMinigame == null) - UpdateGameClock(gameTime); - //this.UpdateCharacters(gameTime); - //this.UpdateLocations(gameTime); - //InvokeBasePrivateInstancedMethod("UpdateCharacters", gameTime); - //InvokeBasePrivateInstancedMethod("UpdateLocations", gameTime); - //UpdateViewPort(false, (Point)InvokeBasePrivateInstancedMethod("getViewportCenter")); - - InvokeMethodInfo(UpdateCharacters, gameTime); - InvokeMethodInfo(UpdateLocations, gameTime); - UpdateViewPort(false, (Point)InvokeMethodInfo(getViewportCenter)); - } - UpdateOther(gameTime); - if (messagePause) - { - KeyboardState state1 = Keyboard.GetState(); - MouseState state2 = Mouse.GetState(); - GamePadState state3 = GamePad.GetState(PlayerIndex.One); - if (isOneOfTheseKeysDown(state1, options.actionButton) && !isOneOfTheseKeysDown(oldKBState, options.actionButton)) - pressActionButton(state1, state2, state3); - oldKBState = state1; - oldPadState = state3; - } + if (gameMode == 10) + UpdateOther(gameTime); } + if (audioEngine != null) + audioEngine.Update(); + if (multiplayerMode == 2 && gameMode == 3) + server.sendMessages(gameTime); } - else - { - //InvokeBasePrivateInstancedMethod("UpdateTitleScreen", gameTime); - InvokeMethodInfo(UpdateTitleScreen, gameTime); - //this.UpdateTitleScreen(gameTime); - if (activeClickableMenu != null) - updateActiveMenu(gameTime); - if (gameMode == 10) - UpdateOther(gameTime); - } - if (audioEngine != null) - audioEngine.Update(); - if (multiplayerMode == 2 && gameMode == 3) - server.sendMessages(gameTime); + } + catch (Exception ex) + { + Log.Error("An error occurred in the overridden update loop: " + ex); } //typeof (Game).GetMethod("Update", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(this, new object[] {gameTime}); @@ -844,467 +853,488 @@ namespace StardewModdingAPI.Inheritance { #region Overridden Draw - if (!ZoomLevelIsOne) - { - GraphicsDevice.SetRenderTarget(Screen); - } - - GraphicsDevice.Clear(BgColour); - if (options.showMenuBackground && activeClickableMenu != null && activeClickableMenu.showWithoutTransparencyIfOptionIsSet()) + try { - spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointClamp, null, null); - activeClickableMenu.drawBackground(spriteBatch); - activeClickableMenu.draw(spriteBatch); - spriteBatch.End(); if (!ZoomLevelIsOne) { - GraphicsDevice.SetRenderTarget(null); - GraphicsDevice.Clear(BgColour); - spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, SamplerState.LinearClamp, DepthStencilState.Default, RasterizerState.CullNone); - spriteBatch.Draw(Screen, Vector2.Zero, Screen.Bounds, Color.White, 0f, Vector2.Zero, options.zoomLevel, SpriteEffects.None, 1f); - spriteBatch.End(); + GraphicsDevice.SetRenderTarget(Screen); } - return; - } - if (gameMode == 11) - { - spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, SamplerState.PointClamp, null, null); - spriteBatch.DrawString(smoothFont, "Stardew Valley has crashed...", new Vector2(16f, 16f), Color.HotPink); - spriteBatch.DrawString(smoothFont, "Please send the error report or a screenshot of this message to @ConcernedApe. (http://stardewvalley.net/contact/)", new Vector2(16f, 32f), new Color(0, 255, 0)); - spriteBatch.DrawString(smoothFont, parseText(errorMessage, smoothFont, graphics.GraphicsDevice.Viewport.Width), new Vector2(16f, 48f), Color.White); - spriteBatch.End(); - return; - } - if (currentMinigame != null) - { - currentMinigame.draw(spriteBatch); - if (globalFade && !menuUp && (!nameSelectUp || messagePause)) + + GraphicsDevice.Clear(BgColour); + if (options.showMenuBackground && activeClickableMenu != null && activeClickableMenu.showWithoutTransparencyIfOptionIsSet()) { spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointClamp, null, null); - spriteBatch.Draw(fadeToBlackRect, graphics.GraphicsDevice.Viewport.Bounds, Color.Black * ((gameMode == 0) ? (1f - fadeToBlackAlpha) : fadeToBlackAlpha)); - spriteBatch.End(); - } - if (!ZoomLevelIsOne) - { - GraphicsDevice.SetRenderTarget(null); - GraphicsDevice.Clear(BgColour); - spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, SamplerState.LinearClamp, DepthStencilState.Default, RasterizerState.CullNone); - spriteBatch.Draw(Screen, Vector2.Zero, Screen.Bounds, Color.White, 0f, Vector2.Zero, options.zoomLevel, SpriteEffects.None, 1f); + activeClickableMenu.drawBackground(spriteBatch); + activeClickableMenu.draw(spriteBatch); spriteBatch.End(); + if (!ZoomLevelIsOne) + { + GraphicsDevice.SetRenderTarget(null); + GraphicsDevice.Clear(BgColour); + spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, SamplerState.LinearClamp, DepthStencilState.Default, RasterizerState.CullNone); + spriteBatch.Draw(Screen, Vector2.Zero, Screen.Bounds, Color.White, 0f, Vector2.Zero, options.zoomLevel, SpriteEffects.None, 1f); + spriteBatch.End(); + } + return; } - return; - } - if (showingEndOfNightStuff) - { - spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointClamp, null, null); - activeClickableMenu?.draw(spriteBatch); - spriteBatch.End(); - if (!ZoomLevelIsOne) + if (gameMode == 11) { - GraphicsDevice.SetRenderTarget(null); - GraphicsDevice.Clear(BgColour); - spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, SamplerState.LinearClamp, DepthStencilState.Default, RasterizerState.CullNone); - spriteBatch.Draw(Screen, Vector2.Zero, Screen.Bounds, Color.White, 0f, Vector2.Zero, options.zoomLevel, SpriteEffects.None, 1f); + spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, SamplerState.PointClamp, null, null); + spriteBatch.DrawString(smoothFont, "Stardew Valley has crashed...", new Vector2(16f, 16f), Color.HotPink); + spriteBatch.DrawString(smoothFont, "Please send the error report or a screenshot of this message to @ConcernedApe. (http://stardewvalley.net/contact/)", new Vector2(16f, 32f), new Color(0, 255, 0)); + spriteBatch.DrawString(smoothFont, parseText(errorMessage, smoothFont, graphics.GraphicsDevice.Viewport.Width), new Vector2(16f, 48f), Color.White); spriteBatch.End(); + return; } - return; - } - if (gameMode == 6) - { - spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointClamp, null, null); - string text = ""; - int num = 0; - while (num < gameTime.TotalGameTime.TotalMilliseconds % 999.0 / 333.0) + if (currentMinigame != null) { - text += "."; - num++; + currentMinigame.draw(spriteBatch); + if (globalFade && !menuUp && (!nameSelectUp || messagePause)) + { + spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointClamp, null, null); + spriteBatch.Draw(fadeToBlackRect, graphics.GraphicsDevice.Viewport.Bounds, Color.Black * ((gameMode == 0) ? (1f - fadeToBlackAlpha) : fadeToBlackAlpha)); + spriteBatch.End(); + } + if (!ZoomLevelIsOne) + { + GraphicsDevice.SetRenderTarget(null); + GraphicsDevice.Clear(BgColour); + spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, SamplerState.LinearClamp, DepthStencilState.Default, RasterizerState.CullNone); + spriteBatch.Draw(Screen, Vector2.Zero, Screen.Bounds, Color.White, 0f, Vector2.Zero, options.zoomLevel, SpriteEffects.None, 1f); + spriteBatch.End(); + } + return; } - SpriteText.drawString(spriteBatch, "Loading" + text, 64, graphics.GraphicsDevice.Viewport.Height - 64, 999, -1, 999, 1f, 1f, false, 0, "Loading..."); - spriteBatch.End(); - if (!ZoomLevelIsOne) + if (showingEndOfNightStuff) { - GraphicsDevice.SetRenderTarget(null); - GraphicsDevice.Clear(BgColour); - spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, SamplerState.LinearClamp, DepthStencilState.Default, RasterizerState.CullNone); - spriteBatch.Draw(Screen, Vector2.Zero, Screen.Bounds, Color.White, 0f, Vector2.Zero, options.zoomLevel, SpriteEffects.None, 1f); + spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointClamp, null, null); + activeClickableMenu?.draw(spriteBatch); spriteBatch.End(); + if (!ZoomLevelIsOne) + { + GraphicsDevice.SetRenderTarget(null); + GraphicsDevice.Clear(BgColour); + spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, SamplerState.LinearClamp, DepthStencilState.Default, RasterizerState.CullNone); + spriteBatch.Draw(Screen, Vector2.Zero, Screen.Bounds, Color.White, 0f, Vector2.Zero, options.zoomLevel, SpriteEffects.None, 1f); + spriteBatch.End(); + } + return; } - return; - } - if (gameMode == 0) - { - spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointClamp, null, null); - } - else - { - if (drawLighting) + if (gameMode == 6) { - GraphicsDevice.SetRenderTarget(lightmap); - GraphicsDevice.Clear(Color.White * 0f); - spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.NonPremultiplied, SamplerState.PointClamp, null, null); - spriteBatch.Draw(staminaRect, lightmap.Bounds, currentLocation.name.Equals("UndergroundMine") ? mine.getLightingColor(gameTime) : ((!ambientLight.Equals(Color.White) && (!isRaining || !currentLocation.isOutdoors)) ? ambientLight : outdoorLight)); - for (int i = 0; i < currentLightSources.Count; i++) + spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointClamp, null, null); + string text = ""; + int num = 0; + while (num < gameTime.TotalGameTime.TotalMilliseconds % 999.0 / 333.0) { - if (Utility.isOnScreen(currentLightSources.ElementAt(i).position, (int) (currentLightSources.ElementAt(i).radius * tileSize * 4f))) - { - spriteBatch.Draw(currentLightSources.ElementAt(i).lightTexture, GlobalToLocal(viewport, currentLightSources.ElementAt(i).position) / options.lightingQuality, currentLightSources.ElementAt(i).lightTexture.Bounds, currentLightSources.ElementAt(i).color, 0f, new Vector2(currentLightSources.ElementAt(i).lightTexture.Bounds.Center.X, currentLightSources.ElementAt(i).lightTexture.Bounds.Center.Y), currentLightSources.ElementAt(i).radius / options.lightingQuality, SpriteEffects.None, 0.9f); - } + text += "."; + num++; } + SpriteText.drawString(spriteBatch, "Loading" + text, 64, graphics.GraphicsDevice.Viewport.Height - 64, 999, -1, 999, 1f, 1f, false, 0, "Loading..."); spriteBatch.End(); - GraphicsDevice.SetRenderTarget(!ZoomLevelIsOne ? null : Screen); + if (!ZoomLevelIsOne) + { + GraphicsDevice.SetRenderTarget(null); + GraphicsDevice.Clear(BgColour); + spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, SamplerState.LinearClamp, DepthStencilState.Default, RasterizerState.CullNone); + spriteBatch.Draw(Screen, Vector2.Zero, Screen.Bounds, Color.White, 0f, Vector2.Zero, options.zoomLevel, SpriteEffects.None, 1f); + spriteBatch.End(); + } + return; } - if (bloomDay) + if (gameMode == 0) { - bloom?.BeginDraw(); + spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointClamp, null, null); } - GraphicsDevice.Clear(BgColour); - spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointClamp, null, null); - GraphicsEvents.InvokeOnPreRenderEvent(null, EventArgs.Empty); - background?.draw(spriteBatch); - mapDisplayDevice.BeginScene(spriteBatch); - currentLocation.Map.GetLayer("Back").Draw(mapDisplayDevice, viewport, Location.Origin, false, pixelZoom); - currentLocation.drawWater(spriteBatch); - if (CurrentEvent == null) + else { - using (List.Enumerator enumerator = currentLocation.characters.GetEnumerator()) + if (drawLighting) { - while (enumerator.MoveNext()) + GraphicsDevice.SetRenderTarget(lightmap); + GraphicsDevice.Clear(Color.White * 0f); + spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.NonPremultiplied, SamplerState.PointClamp, null, null); + spriteBatch.Draw(staminaRect, lightmap.Bounds, currentLo