From 12bf4fd843be26f89b5fe3415aeec3055c54d786 Mon Sep 17 00:00:00 2001 From: Zoryn Aaron Date: Sun, 27 Mar 2016 01:09:09 -0400 Subject: someone needs to generate xml doc info im not fuck that shit --- StardewModdingAPI/Inheritance/SGame.cs | 397 ++++++++++++++++++++++++--------- 1 file changed, 291 insertions(+), 106 deletions(-) (limited to 'StardewModdingAPI/Inheritance/SGame.cs') diff --git a/StardewModdingAPI/Inheritance/SGame.cs b/StardewModdingAPI/Inheritance/SGame.cs index ce2488d5..f1887253 100644 --- a/StardewModdingAPI/Inheritance/SGame.cs +++ b/StardewModdingAPI/Inheritance/SGame.cs @@ -11,60 +11,264 @@ using StardewValley.Menus; namespace StardewModdingAPI.Inheritance { + /// + /// The 'SGame' class. + /// This summary, and many others, only exists because XML doc tags. + /// public class SGame : Game1 { - public static Dictionary ModItems { get; private set; } - public const Int32 LowestModItemID = 1000; + /// + /// Useless right now. + /// + public const int LowestModItemID = 1000; - public static FieldInfo[] StaticFields { get { return GetStaticFields(); } } + private bool FireLoadedGameEvent; + + /// + /// Gets a jagged array of all buttons pressed on the gamepad the prior frame. + /// + public Buttons[][] PreviouslyPressedButtons; - public static FieldInfo[] GetStaticFields() + internal SGame() { - return typeof(Game1).GetFields(); + Instance = this; + FirstUpdate = true; } + /// + /// Useless at this time. + /// + [Obsolete] + public static Dictionary ModItems { get; private set; } + + /// + /// The current KeyboardState + /// public KeyboardState KStateNow { get; private set; } + /// + /// The prior KeyboardState + /// public KeyboardState KStatePrior { get; private set; } + /// + /// The current MouseState + /// public MouseState MStateNow { get; private set; } + + /// + /// The prior MouseState + /// public MouseState MStatePrior { get; private set; } + /// + /// All keys pressed on the current frame + /// public Keys[] CurrentlyPressedKeys => KStateNow.GetPressedKeys(); + + /// + /// All keys pressed on the prior frame + /// public Keys[] PreviouslyPressedKeys => KStatePrior.GetPressedKeys(); + /// + /// All keys pressed on this frame except for the ones pressed on the prior frame + /// public Keys[] FramePressedKeys => CurrentlyPressedKeys.Except(PreviouslyPressedKeys).ToArray(); + /// + /// All keys pressed on the prior frame except for the ones pressed on the current frame + /// public Keys[] FrameReleasedKeys => PreviouslyPressedKeys.Except(CurrentlyPressedKeys).ToArray(); - public Buttons[][] PreviouslyPressedButtons; + /// + /// Whether or not a save was tagged as 'Loaded' the prior frame. + /// + public bool PreviouslyLoadedGame { get; private set; } + + /// + /// The list of GameLocations on the prior frame + /// + public int PreviousGameLocations { get; private set; } + + /// + /// The list of GameObjects on the prior frame + /// + public int PreviousLocationObjects { get; private set; } + /// + /// The list of Items in the player's inventory on the prior frame + /// + public Dictionary PreviousItems { get; private set; } + + /// + /// The player's Combat level on the prior frame + /// + public int PreviousCombatLevel { get; private set; } + /// + /// The player's Farming level on the prior frame + /// + public int PreviousFarmingLevel { get; private set; } + /// + /// The player's Fishing level on the prior frame + /// + public int PreviousFishingLevel { get; private set; } + /// + /// The player's Foraging level on the prior frame + /// + public int PreviousForagingLevel { get; private set; } + /// + /// The player's Mining level on the prior frame + /// + public int PreviousMiningLevel { get; private set; } + /// + /// The player's Luck level on the prior frame + /// + public int PreviousLuckLevel { get; private set; } + + //Kill me now comments are so boring + + /// + /// The player's previous game location + /// + public GameLocation PreviousGameLocation { get; private set; } + + /// + /// The previous ActiveGameMenu in Game1 + /// + public IClickableMenu PreviousActiveMenu { get; private set; } + + /// + /// The previous mine level + /// + public int PreviousMineLevel { get; private set; } + + /// + /// The previous TimeOfDay (Int32 between 600 and 2400?) + /// + public int PreviousTimeOfDay { get; private set; } + + /// + /// The previous DayOfMonth (Int32 between 1 and 28?) + /// + public int PreviousDayOfMonth { get; private set; } + + /// + /// The previous Season (String as follows: "winter", "spring", "summer", "fall") + /// + public string PreviousSeasonOfYear { get; private set; } + + /// + /// The previous Year + /// + public int PreviousYearOfGame { get; private set; } + + /// + /// The previous 'Farmer' (Player) + /// + public Farmer PreviousFarmer { get; private set; } + + /// + /// The current index of the update tick. Recycles every 60th tick to 0. (Int32 between 0 and 59) + /// + public int CurrentUpdateTick { get; private set; } + + /// + /// Whether or not this update frame is the very first of the entire game + /// + public bool FirstUpdate { get; private set; } + + /// + /// The current RenderTarget in Game1 (Private field, uses reflection) + /// + public RenderTarget2D Screen + { + get { return typeof (Game1).GetBaseFieldValue(Program.gamePtr, "screen"); } + set { typeof (Game1).SetBaseFieldValue(this, "screen", value); } + } + + /// + /// Static accessor for an Instance of the class SGame + /// + public static SGame Instance { get; private set; } + + /// + /// The game's FPS. Re-determined every Draw update. + /// + public static float FramesPerSecond { get; private set; } + + /// + /// Whether or not we're in a pseudo 'debug' mode. Mostly for displaying information like FPS. + /// + public static bool Debug { get; private set; } + + /// + /// The current player (equal to Farmer.Player) + /// + [Obsolete("Use Farmer.Player instead")] + public Farmer CurrentFarmer => player; + + /// + /// Gets ALL static fields that belong to 'Game1' + /// + public static FieldInfo[] GetStaticFields => typeof (Game1).GetFields(); + + /// + /// Whether or not a button was just pressed on the controller + /// + /// + /// + /// + /// private bool WasButtonJustPressed(Buttons button, ButtonState buttonState, PlayerIndex stateIndex) { - return buttonState == ButtonState.Pressed && !PreviouslyPressedButtons[(int)stateIndex].Contains(button); + return buttonState == ButtonState.Pressed && !PreviouslyPressedButtons[(int) stateIndex].Contains(button); } + /// + /// Whether or not a button was just released on the controller + /// + /// + /// + /// + /// private bool WasButtonJustReleased(Buttons button, ButtonState buttonState, PlayerIndex stateIndex) { - return buttonState == ButtonState.Released && PreviouslyPressedButtons[(int)stateIndex].Contains(button); + return buttonState == ButtonState.Released && PreviouslyPressedButtons[(int) stateIndex].Contains(button); } + /// + /// Whether or not an analog button was just pressed on the controller + /// + /// + /// + /// + /// private bool WasButtonJustPressed(Buttons button, float value, PlayerIndex stateIndex) { return WasButtonJustPressed(button, value > 0.2f ? ButtonState.Pressed : ButtonState.Released, stateIndex); } - + + /// + /// Whether or not an analog button was just released on the controller + /// + /// + /// + /// + /// private bool WasButtonJustReleased(Buttons button, float value, PlayerIndex stateIndex) { return WasButtonJustReleased(button, value > 0.2f ? ButtonState.Pressed : ButtonState.Released, stateIndex); } - public bool PreviouslyLoadedGame { get; private set; } - private bool FireLoadedGameEvent; - + /// + /// Gets an array of all Buttons pressed on a joystick + /// + /// + /// public Buttons[] GetButtonsDown(PlayerIndex index) { - GamePadState state = GamePad.GetState(index); - List buttons = new List(); + var state = GamePad.GetState(index); + var buttons = new List(); if (state.IsConnected) { if (state.Buttons.A == ButtonState.Pressed) buttons.Add(Buttons.A); @@ -88,15 +292,20 @@ namespace StardewModdingAPI.Inheritance return buttons.ToArray(); } + /// + /// Gets all buttons that were pressed on the current frame of a joystick + /// + /// + /// public Buttons[] GetFramePressedButtons(PlayerIndex index) - { - GamePadState state = GamePad.GetState(index); - List buttons = new List(); + { + var state = GamePad.GetState(index); + var buttons = new List(); if (state.IsConnected) { - if (WasButtonJustPressed(Buttons.A, state.Buttons.A, index)) buttons.Add(Buttons.A); - if (WasButtonJustPressed(Buttons.B, state.Buttons.B, index)) buttons.Add(Buttons.B); - if (WasButtonJustPressed(Buttons.Back, state.Buttons.Back, index)) buttons.Add(Buttons.Back); + if (WasButtonJustPressed(Buttons.A, state.Buttons.A, index)) buttons.Add(Buttons.A); + if (WasButtonJustPressed(Buttons.B, state.Buttons.B, index)) buttons.Add(Buttons.B); + if (WasButtonJustPressed(Buttons.Back, state.Buttons.Back, index)) buttons.Add(Buttons.Back); if (WasButtonJustPressed(Buttons.BigButton, state.Buttons.BigButton, index)) buttons.Add(Buttons.BigButton); if (WasButtonJustPressed(Buttons.LeftShoulder, state.Buttons.LeftShoulder, index)) buttons.Add(Buttons.LeftShoulder); if (WasButtonJustPressed(Buttons.LeftStick, state.Buttons.LeftStick, index)) buttons.Add(Buttons.LeftStick); @@ -112,13 +321,18 @@ namespace StardewModdingAPI.Inheritance if (WasButtonJustPressed(Buttons.LeftTrigger, state.Triggers.Left, index)) buttons.Add(Buttons.LeftTrigger); if (WasButtonJustPressed(Buttons.RightTrigger, state.Triggers.Right, index)) buttons.Add(Buttons.RightTrigger); } - return buttons.ToArray(); + return buttons.ToArray(); } + /// + /// Gets all buttons that were released on the current frame of a joystick + /// + /// + /// public Buttons[] GetFrameReleasedButtons(PlayerIndex index) { - GamePadState state = GamePad.GetState(index); - List buttons = new List(); + var state = GamePad.GetState(index); + var buttons = new List(); if (state.IsConnected) { if (WasButtonJustReleased(Buttons.A, state.Buttons.A, index)) buttons.Add(Buttons.A); @@ -142,70 +356,34 @@ namespace StardewModdingAPI.Inheritance return buttons.ToArray(); } - public int PreviousGameLocations { get; private set; } - public int PreviousLocationObjects { get; private set; } - public Dictionary PreviousItems { get; private set; } - - public int PreviousCombatLevel { get; private set; } - public int PreviousFarmingLevel { get; private set; } - public int PreviousFishingLevel { get; private set; } - public int PreviousForagingLevel { get; private set; } - public int PreviousMiningLevel { get; private set; } - public int PreviousLuckLevel { get; private set; } - - public GameLocation PreviousGameLocation { get; private set; } - public IClickableMenu PreviousActiveMenu { get; private set; } - - public int PreviousMineLevel { get; private set; } - - public Int32 PreviousTimeOfDay { get; private set; } - public Int32 PreviousDayOfMonth { get; private set; } - public String PreviousSeasonOfYear { get; private set; } - public Int32 PreviousYearOfGame { get; private set; } - - public Farmer PreviousFarmer { get; private set; } - - public Int32 CurrentUpdateTick { get; private set; } - public bool FirstUpdate { get; private set; } - - public RenderTarget2D Screen - { - get { return typeof (Game1).GetBaseFieldValue(Program.gamePtr, "screen"); } - set { typeof (Game1).SetBaseFieldValue(this, "screen", value); } - } - - private static SGame instance; - public static SGame Instance => instance; - - public static float FramesPerSecond { get; private set; } - public static bool Debug { get; private set; } - - public Farmer CurrentFarmer => player; - - public SGame() - { - instance = this; - FirstUpdate = true; - } - + /// + /// XNA Init Method + /// protected override void Initialize() { - Log.Verbose("XNA Initialize"); - ModItems = new Dictionary(); + Log.AsyncY("XNA Initialize"); + //ModItems = new Dictionary(); PreviouslyPressedButtons = new Buttons[4][]; - for (int i = 0; i < 4; ++i) PreviouslyPressedButtons[i] = new Buttons[0]; + for (var i = 0; i < 4; ++i) PreviouslyPressedButtons[i] = new Buttons[0]; base.Initialize(); GameEvents.InvokeInitialize(); } + /// + /// XNA LC Method + /// protected override void LoadContent() { - Log.Verbose("XNA LoadContent"); + Log.AsyncY("XNA LoadContent"); base.LoadContent(); GameEvents.InvokeLoadContent(); } + /// + /// XNA Update Method + /// + /// protected override void Update(GameTime gameTime) { UpdateEventCalls(); @@ -221,7 +399,7 @@ namespace StardewModdingAPI.Inheritance } catch (Exception ex) { - Log.Error("An error occured in the base update loop: " + ex); + Log.AsyncR("An error occured in the base update loop: " + ex); Console.ReadKey(); } @@ -257,15 +435,19 @@ namespace StardewModdingAPI.Inheritance if (KStatePrior != KStateNow) KStatePrior = KStateNow; - for (PlayerIndex i = PlayerIndex.One; i <= PlayerIndex.Four; i++) + for (var i = PlayerIndex.One; i <= PlayerIndex.Four; i++) { - PreviouslyPressedButtons[(int)i] = GetButtonsDown(i); + PreviouslyPressedButtons[(int) i] = GetButtonsDown(i); } } + /// + /// XNA Draw Method + /// + /// protected override void Draw(GameTime gameTime) { - FramesPerSecond = 1 / (float)gameTime.ElapsedGameTime.TotalSeconds; + FramesPerSecond = 1 / (float) gameTime.ElapsedGameTime.TotalSeconds; try { @@ -273,7 +455,7 @@ namespace StardewModdingAPI.Inheritance } catch (Exception ex) { - Log.Error("An error occured in the base draw loop: " + ex); + Log.AsyncR("An error occured in the base draw loop: " + ex); Console.ReadKey(); } @@ -299,6 +481,12 @@ namespace StardewModdingAPI.Inheritance //spriteBatch.End(); + //Re-draw the HUD + spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, SamplerState.PointClamp, null, null); + if ((displayHUD || eventUp) && currentBillboard == 0 && gameMode == 3 && !freezeControls && !panMode) + typeof (Game1).GetMethod("drawHUD", BindingFlags.NonPublic | BindingFlags.Instance)?.Invoke(Program.gamePtr, null); + spriteBatch.End(); + if (!options.zoomLevel.Equals(1.0f)) { GraphicsDevice.SetRenderTarget(null); @@ -306,11 +494,6 @@ namespace StardewModdingAPI.Inheritance spriteBatch.Draw(Screen, Vector2.Zero, Screen.Bounds, Color.White, 0.0f, Vector2.Zero, options.zoomLevel, SpriteEffects.None, 1f); spriteBatch.End(); } - - //Re-draw the mouse - spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, SamplerState.PointClamp, null, null); - spriteBatch.Draw(mouseCursors, new Vector2(getMouseX(), getMouseY()), getSourceRectForStandardTileSheet(mouseCursors, mouseCursor, 16, 16), Color.White * mouseCursorTransparency, 0.0f, Vector2.Zero, pixelZoom + dialogueButtonScale / 150f, SpriteEffects.None, 1f); - spriteBatch.End(); } if (Debug) @@ -321,14 +504,15 @@ namespace StardewModdingAPI.Inheritance } } - public static Int32 RegisterModItem(SObject modItem) + [Obsolete("Do not use at this time.")] + private static int RegisterModItem(SObject modItem) { if (modItem.HasBeenRegistered) { - Log.Error("The item {0} has already been registered with ID {1}", modItem.Name, modItem.RegisteredId); + Log.AsyncR($"The item {modItem.Name} has already been registered with ID {modItem.RegisteredId}"); return modItem.RegisteredId; } - Int32 newId = LowestModItemID; + var newId = LowestModItemID; if (ModItems.Count > 0) newId = Math.Max(LowestModItemID, ModItems.OrderBy(x => x.Key).First().Key + 1); ModItems.Add(newId, modItem); @@ -337,7 +521,8 @@ namespace StardewModdingAPI.Inheritance return newId; } - public static SObject PullModItemFromDict(Int32 id, bool isIndex) + [Obsolete("Do not use at this time.")] + private static SObject PullModItemFromDict(int id, bool isIndex) { if (isIndex) { @@ -345,35 +530,35 @@ namespace StardewModdingAPI.Inheritance { return ModItems.ElementAt(id).Value.Clone(); } - Log.Error("ModItem Dictionary does not contain index: " + id); + Log.AsyncR("ModItem Dictionary does not contain index: " + id); return null; } if (ModItems.ContainsKey(id)) { return ModItems[id].Clone(); } - Log.Error("ModItem Dictionary does not contain ID: " + id); + Log.AsyncR("ModItem Dictionary does not contain ID: " + id); return null; } - - public void UpdateEventCalls() + + private void UpdateEventCalls() { KStateNow = Keyboard.GetState(); MStateNow = Mouse.GetState(); - foreach (Keys k in FramePressedKeys) + foreach (var k in FramePressedKeys) ControlEvents.InvokeKeyPressed(k); - foreach (Keys k in FrameReleasedKeys) + foreach (var k in FrameReleasedKeys) ControlEvents.InvokeKeyReleased(k); - for (PlayerIndex i = PlayerIndex.One; i <= PlayerIndex.Four; i++) + for (var i = PlayerIndex.One; i <= PlayerIndex.Four; i++) { - Buttons[] buttons = GetFramePressedButtons(i); - foreach (Buttons b in buttons) + var buttons = GetFramePressedButtons(i); + foreach (var b in buttons) { - if(b == Buttons.LeftTrigger || b == Buttons.RightTrigger) + if (b == Buttons.LeftTrigger || b == Buttons.RightTrigger) { ControlEvents.InvokeTriggerPressed(i, b, b == Buttons.LeftTrigger ? GamePad.GetState(i).Triggers.Left : GamePad.GetState(i).Triggers.Right); } @@ -384,9 +569,9 @@ namespace StardewModdingAPI.Inheritance } } - for (PlayerIndex i = PlayerIndex.One; i <= PlayerIndex.Four; i++) + for (var i = PlayerIndex.One; i <= PlayerIndex.Four; i++) { - foreach (Buttons b in GetFrameReleasedButtons(i)) + foreach (var b in GetFrameReleasedButtons(i)) { if (b == Buttons.LeftTrigger || b == Buttons.RightTrigger) { @@ -471,7 +656,7 @@ namespace StardewModdingAPI.Inheritance PreviousLuckLevel = player.luckLevel; } - List changedItems; + List changedItems; if (player != null && HasInventoryChanged(player.items, out changedItems)) { PlayerEvents.InvokeInventoryChanged(player.items, changedItems); @@ -479,7 +664,7 @@ namespace StardewModdingAPI.Inheritance } var objectHash = currentLocation?.objects?.GetHash(); - if(objectHash != null && PreviousLocationObjects != objectHash) + if (objectHash != null && PreviousLocationObjects != objectHash) { LocationEvents.InvokeOnNewLocationObject(currentLocation.objects); PreviousLocationObjects = objectHash ?? -1; @@ -537,24 +722,24 @@ namespace StardewModdingAPI.Inheritance { if (PreviousItems != null && PreviousItems.ContainsKey(item)) { - if(PreviousItems[item] != item.Stack) + if (PreviousItems[item] != item.Stack) { - changedItems.Add(new ItemStackChange { Item = item, StackChange = item.Stack - PreviousItems[item], ChangeType = ChangeType.StackChange }); + changedItems.Add(new ItemStackChange {Item = item, StackChange = item.Stack - PreviousItems[item], ChangeType = ChangeType.StackChange}); } } else { - changedItems.Add(new ItemStackChange { Item = item, StackChange = item.Stack, ChangeType = ChangeType.Added }); + changedItems.Add(new ItemStackChange {Item = item, StackChange = item.Stack, ChangeType = ChangeType.Added}); } } if (PreviousItems != null) { changedItems.AddRange(PreviousItems.Where(n => actualItems.All(i => i != n.Key)).Select(n => - new ItemStackChange { Item = n.Key, StackChange = -n.Key.Stack, ChangeType = ChangeType.Removed })); + new ItemStackChange {Item = n.Key, StackChange = -n.Key.Stack, ChangeType = ChangeType.Removed})); } - return (changedItems.Any()); + return changedItems.Any(); } } } \ No newline at end of file -- cgit