From 524dc9e002d86e81c1e38827a2303cc83b71eba7 Mon Sep 17 00:00:00 2001 From: Zoryn Aaron Date: Sun, 28 Feb 2016 22:16:32 -0500 Subject: expose some private fields for modding --- StardewModdingAPI/Extensions.cs | 4 +- StardewModdingAPI/Inheritance/Menus/SBobberBar.cs | 33 ++++++++++ .../Inheritance/Minigames/SMinigameBase.cs | 38 +++++++++++ StardewModdingAPI/Inheritance/SGame.cs | 75 ++++++++++++++++++++++ StardewModdingAPI/Program.cs | 18 ++++++ StardewModdingAPI/SGame.cs | 66 ------------------- StardewModdingAPI/StardewModdingAPI.csproj | 5 +- 7 files changed, 170 insertions(+), 69 deletions(-) create mode 100644 StardewModdingAPI/Inheritance/Menus/SBobberBar.cs create mode 100644 StardewModdingAPI/Inheritance/Minigames/SMinigameBase.cs create mode 100644 StardewModdingAPI/Inheritance/SGame.cs delete mode 100644 StardewModdingAPI/SGame.cs (limited to 'StardewModdingAPI') diff --git a/StardewModdingAPI/Extensions.cs b/StardewModdingAPI/Extensions.cs index 0078ebe9..b960f027 100644 --- a/StardewModdingAPI/Extensions.cs +++ b/StardewModdingAPI/Extensions.cs @@ -22,9 +22,9 @@ namespace StardewModdingAPI return new Color(Random.Next(0, 255), Random.Next(0, 255), Random.Next(0, 255)); } - public static string ToSingular(this IEnumerable enumerable) + public static string ToSingular(this IEnumerable enumerable, string split = ", ") { - string result = string.Join(", ", enumerable); + string result = string.Join(split, enumerable); return result; } diff --git a/StardewModdingAPI/Inheritance/Menus/SBobberBar.cs b/StardewModdingAPI/Inheritance/Menus/SBobberBar.cs new file mode 100644 index 00000000..9e5f6865 --- /dev/null +++ b/StardewModdingAPI/Inheritance/Menus/SBobberBar.cs @@ -0,0 +1,33 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; +using StardewValley.Menus; + +namespace StardewModdingAPI.Inheritance.Menus +{ + public class SBobberBar : BobberBar + { + public static FieldInfo[] PrivateFields { get { return GetPrivateFields(); } } + + /// + /// DO NOT CONSTRUCT THIS CLASS + /// This class ONLY provides functionality to access the base BobberBar class fields. + /// + /// + /// + /// + /// + public SBobberBar(int whichFish, float fishSize, bool treasure, int bobber) : base(whichFish, fishSize, treasure, bobber) + { + + } + + public static FieldInfo[] GetPrivateFields() + { + return typeof (BobberBar).GetFields(BindingFlags.Instance | BindingFlags.NonPublic); + } + } +} diff --git a/StardewModdingAPI/Inheritance/Minigames/SMinigameBase.cs b/StardewModdingAPI/Inheritance/Minigames/SMinigameBase.cs new file mode 100644 index 00000000..5ce29d8d --- /dev/null +++ b/StardewModdingAPI/Inheritance/Minigames/SMinigameBase.cs @@ -0,0 +1,38 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; +using Microsoft.Xna.Framework.Input; + +namespace StardewModdingAPI.Inheritance.Minigames +{ + abstract class SMinigameBase : StardewValley.Minigames.IMinigame + { + public abstract bool tick(GameTime time); + + public abstract void receiveLeftClick(int x, int y, bool playSound = true); + + public abstract void leftClickHeld(int x, int y); + + public abstract void receiveRightClick(int x, int y, bool playSound = true); + + public abstract void releaseLeftClick(int x, int y); + + public abstract void releaseRightClick(int x, int y); + + public abstract void receiveKeyPress(Keys k); + + public abstract void receiveKeyRelease(Keys k); + + public abstract void draw(SpriteBatch b); + + public abstract void changeScreenSize(); + + public abstract void unload(); + + public abstract void receiveEventPoke(int data); + } +} diff --git a/StardewModdingAPI/Inheritance/SGame.cs b/StardewModdingAPI/Inheritance/SGame.cs new file mode 100644 index 00000000..61a0f390 --- /dev/null +++ b/StardewModdingAPI/Inheritance/SGame.cs @@ -0,0 +1,75 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Text; +using System.Threading.Tasks; +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Input; +using StardewValley; +using StardewValley.Minigames; + +namespace StardewModdingAPI.Inheritance +{ + public class SGame : Game1 + { + public static FieldInfo[] StaticFields { get { return Thing(); } } + + public static FieldInfo[] Thing() + { + return typeof(Game1).GetFields(); + } + + public KeyboardState KStateNow { get; private set; } + public KeyboardState KStatePrior { get; private set; } + + public Keys[] CurrentlyPressedKeys { get; private set; } + public Keys[] PreviouslyPressedKeys { get; private set; } + + public Keys[] FramePressedKeys + { + get { return CurrentlyPressedKeys.Where(x => !PreviouslyPressedKeys.Contains(x)).ToArray(); } + } + + protected override void Initialize() + { + Program.Log("XNA Initialize"); + Events.InvokeInitialize(); + base.Initialize(); + } + + protected override void LoadContent() + { + Program.Log("XNA LoadContent"); + Events.InvokeLoadContent(); + base.LoadContent(); + } + + protected override void Update(GameTime gameTime) + { + KStateNow = Keyboard.GetState(); + CurrentlyPressedKeys = KStateNow.GetPressedKeys(); + + foreach (Keys k in FramePressedKeys) + Events.InvokeKeyPressed(k); + + if (KStateNow != KStatePrior) + { + Events.InvokeKeyboardChanged(KStateNow); + } + + Events.InvokeUpdateTick(); + base.Update(gameTime); + + KStatePrior = KStateNow; + PreviouslyPressedKeys = CurrentlyPressedKeys; + } + + protected override void Draw(GameTime gameTime) + { + Events.InvokeDrawTick(); + base.Draw(gameTime); + } + } +} \ No newline at end of file diff --git a/StardewModdingAPI/Program.cs b/StardewModdingAPI/Program.cs index 52ea76e8..846050ed 100644 --- a/StardewModdingAPI/Program.cs +++ b/StardewModdingAPI/Program.cs @@ -11,6 +11,7 @@ using System.Windows.Forms; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; +using StardewModdingAPI.Inheritance; using StardewValley; using StardewValley.Menus; using StardewValley.Minigames; @@ -44,6 +45,8 @@ namespace StardewModdingAPI { Console.Title = "Stardew Modding API Console"; + AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve); + if (File.Exists(ModPath)) File.Delete(ModPath); if (!Directory.Exists(ModPath)) @@ -62,6 +65,8 @@ namespace StardewModdingAPI LogInfo("Starting SDV..."); gameThread.Start(); + SGame.Thing(); + while (!ready) { @@ -184,7 +189,20 @@ namespace StardewModdingAPI StardewForm.Invoke(a); } + public static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args) + { + string dllName = args.Name.Contains(',') ? args.Name.Substring(0, args.Name.IndexOf(',')) : args.Name.Replace(".dll", ""); + + dllName = dllName.Replace(".", "_"); + if (dllName.EndsWith("_resources")) return null; + + System.Resources.ResourceManager rm = new System.Resources.ResourceManager(typeof(Program).Namespace + ".Properties.Resources", System.Reflection.Assembly.GetExecutingAssembly()); + + byte[] bytes = (byte[])rm.GetObject(dllName); + + return System.Reflection.Assembly.Load(bytes); + } ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/StardewModdingAPI/SGame.cs b/StardewModdingAPI/SGame.cs deleted file mode 100644 index 6af7689a..00000000 --- a/StardewModdingAPI/SGame.cs +++ /dev/null @@ -1,66 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Runtime.CompilerServices; -using System.Text; -using System.Threading.Tasks; -using Microsoft.Xna.Framework; -using Microsoft.Xna.Framework.Input; -using StardewValley; - -namespace StardewModdingAPI -{ - public class SGame : Game1 - { - public KeyboardState KStateNow { get; private set; } - public KeyboardState KStatePrior { get; private set; } - - public Keys[] CurrentlyPressedKeys { get; private set; } - public Keys[] PreviouslyPressedKeys { get; private set; } - - public Keys[] FramePressedKeys - { - get { return CurrentlyPressedKeys.Where(x => !PreviouslyPressedKeys.Contains(x)).ToArray(); } - } - - protected override void Initialize() - { - Program.Log("XNA Initialize"); - Events.InvokeInitialize(); - base.Initialize(); - } - - protected override void LoadContent() - { - Program.Log("XNA LoadContent"); - Events.InvokeLoadContent(); - base.LoadContent(); - } - - protected override void Update(GameTime gameTime) - { - KStateNow = Keyboard.GetState(); - CurrentlyPressedKeys = KStateNow.GetPressedKeys(); - - foreach (Keys k in FramePressedKeys) - Events.InvokeKeyPressed(k); - - if (KStateNow != KStatePrior) - { - Events.InvokeKeyboardChanged(KStateNow); - } - - Events.InvokeUpdateTick(); - base.Update(gameTime); - - KStatePrior = KStateNow; - PreviouslyPressedKeys = CurrentlyPressedKeys; - } - - protected override void Draw(GameTime gameTime) - { - Events.InvokeDrawTick(); - base.Draw(gameTime); - } - } -} \ No newline at end of file diff --git a/StardewModdingAPI/StardewModdingAPI.csproj b/StardewModdingAPI/StardewModdingAPI.csproj index 39e659bf..a6815f26 100644 --- a/StardewModdingAPI/StardewModdingAPI.csproj +++ b/StardewModdingAPI/StardewModdingAPI.csproj @@ -54,6 +54,7 @@ False ..\..\..\..\..\..\Program Files (x86)\Steam\steamapps\common\Stardew Valley\Stardew Valley.exe + False @@ -68,11 +69,13 @@ + + - + -- cgit