diff options
author | Zoryn Aaron <zoryn4163@gmail.com> | 2016-03-02 12:24:15 -0500 |
---|---|---|
committer | Zoryn Aaron <zoryn4163@gmail.com> | 2016-03-02 12:24:15 -0500 |
commit | 900c804c29b6dcedef158d3b36fcebb7846e8a16 (patch) | |
tree | 84d1bab1309429dc4edf6e084abed17704c42d80 /StardewModdingAPI | |
parent | 38bce3315841c66409e02844c86eb7a5e9295565 (diff) | |
download | SMAPI-900c804c29b6dcedef158d3b36fcebb7846e8a16.tar.gz SMAPI-900c804c29b6dcedef158d3b36fcebb7846e8a16.tar.bz2 SMAPI-900c804c29b6dcedef158d3b36fcebb7846e8a16.zip |
update for new event and other crap
Diffstat (limited to 'StardewModdingAPI')
-rw-r--r-- | StardewModdingAPI/Events.cs | 8 | ||||
-rw-r--r-- | StardewModdingAPI/Inheritance/SGame.cs | 39 | ||||
-rw-r--r-- | StardewModdingAPI/Inheritance/SGameLocation.cs | 88 | ||||
-rw-r--r-- | StardewModdingAPI/Inheritance/SObject.cs | 19 | ||||
-rw-r--r-- | StardewModdingAPI/Program.cs | 74 |
5 files changed, 144 insertions, 84 deletions
diff --git a/StardewModdingAPI/Events.cs b/StardewModdingAPI/Events.cs index 05cabc4b..9a92b705 100644 --- a/StardewModdingAPI/Events.cs +++ b/StardewModdingAPI/Events.cs @@ -39,6 +39,9 @@ namespace StardewModdingAPI public static event EventHandler Resize = delegate { }; + public delegate void FarmerChangedD(Farmer newFarmer); + public static event FarmerChangedD FarmerChanged = delegate { }; + public static void InvokeGameLoaded() { GameLoaded.Invoke(); @@ -126,5 +129,10 @@ namespace StardewModdingAPI { Resize.Invoke(sender, e); } + + public static void InvokeFarmerChanged(Farmer newFarmer) + { + FarmerChanged.Invoke(newFarmer); + } } } diff --git a/StardewModdingAPI/Inheritance/SGame.cs b/StardewModdingAPI/Inheritance/SGame.cs index fdc6f3f7..fbea47bd 100644 --- a/StardewModdingAPI/Inheritance/SGame.cs +++ b/StardewModdingAPI/Inheritance/SGame.cs @@ -49,6 +49,8 @@ namespace StardewModdingAPI.Inheritance public GameLocation PreviousGameLocation { get; private set; } public IClickableMenu PreviousActiveMenu { get; private set; } + public Farmer PreviousFarmer { get; private set; } + protected override void Initialize() { Program.Log("XNA Initialize"); @@ -104,6 +106,12 @@ namespace StardewModdingAPI.Inheritance PreviousGameLocation = Game1.currentLocation; } + if (Game1.player != null && Game1.player != PreviousFarmer) + { + Events.InvokeFarmerChanged(Game1.player); + PreviousFarmer = Game1.player; + } + if (CurrentLocation != null) CurrentLocation.update(gameTime); @@ -184,5 +192,36 @@ namespace StardewModdingAPI.Inheritance } return null; } + + public static SGameLocation LoadOrCreateSGameLocationFromName(String name) + { + if (GetLocationFromName(name) != null) + return GetLocationFromName(name); + else + { + GameLocation gl = Game1.locations.FirstOrDefault(x => x.name == name); + if (gl != null) + { + Program.LogDebug("A custom location was created for the new name: " + name); + SGameLocation s = SGameLocation.ConstructFromBaseClass(gl); + ModLocations.Add(s); + return s; + } + else + { + if (Game1.currentLocation != null && Game1.currentLocation.name == name) + { + gl = Game1.currentLocation; + Program.LogDebug("A custom location was created from the current location for the new name: " + name); + SGameLocation s = SGameLocation.ConstructFromBaseClass(gl); + ModLocations.Add(s); + return s; + } + + Program.LogDebug("A custom location could not be created for: " + name); + return null; + } + } + } } }
\ No newline at end of file diff --git a/StardewModdingAPI/Inheritance/SGameLocation.cs b/StardewModdingAPI/Inheritance/SGameLocation.cs index 14d9afa6..50058bff 100644 --- a/StardewModdingAPI/Inheritance/SGameLocation.cs +++ b/StardewModdingAPI/Inheritance/SGameLocation.cs @@ -18,85 +18,40 @@ namespace StardewModdingAPI.Inheritance public SerializableDictionary<Vector2, SObject> ModObjects { get; set; } - public static SGameLocation ConstructFromBaseClass(GameLocation baseClass) + public static SGameLocation ConstructFromBaseClass(GameLocation baseClass, bool copyAllData = false) { SGameLocation s = new SGameLocation(); s.BaseGameLocation = baseClass; - s.ModObjects = new SerializableDictionary<Vector2, SObject>(); + s.name = baseClass.name; + + Program.LogDebug("CONSTRUCTED: " + s.name); - foreach (var v in baseClass.GetType().GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)) + if (copyAllData) { - try + foreach (var v in baseClass.GetType().GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)) { - var fi = s.GetType().GetField(v.Name, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); - if (fi != null && !fi.IsStatic) + try { - fi.SetValue(s, v.GetValue(baseClass)); - //Console.WriteLine("SET {0} ON {1} TO {2}", fi.Name, s.name, v.GetValue(baseClass)); + var fi = s.GetType().GetField(v.Name, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); + if (fi != null && !fi.IsStatic) + { + fi.SetValue(s, v.GetValue(baseClass)); + //Console.WriteLine("SET {0} ON {1} TO {2}", fi.Name, s.name, v.GetValue(baseClass)); + } + } + catch (Exception ex) + { + Program.LogError(ex); } - } - catch (Exception ex) - { - Program.LogError(ex); } } - //s.IsFarm = baseClass.IsFarm; - //s.IsOutdoors = baseClass.IsOutdoors; - //s.LightLevel = baseClass.LightLevel; - //s.Map = baseClass.Map; - //s.objects = baseClass.objects; - //s.temporarySprites = baseClass.temporarySprites; - - /* - s.actionObjectForQuestionDialogue = baseClass.actionObjectForQuestionDialogue; - s.characters = baseClass.characters; - s.critters = (List<Critter>)typeof(GameLocation).GetField("critters", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(baseClass); - s.currentEvent = baseClass.currentEvent; - s.debris = baseClass.debris; - s.doorSprites = baseClass.doorSprites; - s.doors = baseClass.doors; - s.farmers = baseClass.farmers; - s.fishSplashAnimation = baseClass.fishSplashAnimation; - s.fishSplashPoint = baseClass.fishSplashPoint; - s.forceViewportPlayerFollow = baseClass.forceViewportPlayerFollow; - s.ignoreDebrisWeather = baseClass.ignoreDebrisWeather; - s.ignoreLights = baseClass.ignoreLights; - s.ignoreOutdoorLighting = baseClass.ignoreOutdoorLighting; - s.isFarm = baseClass.isFarm; - s.isOutdoors = baseClass.isOutdoors; - s.isStructure = baseClass.isStructure; - s.largeTerrainFeatures = baseClass.largeTerrainFeatures; - s.lastQuestionKey = baseClass.lastQuestionKey; - s.lastTouchActionLocation = baseClass.lastTouchActionLocation; - s.lightGlows = baseClass.lightGlows; - s.map = baseClass.map; - s.name = baseClass.name; - s.numberOfSpawnedObjectsOnMap = baseClass.numberOfSpawnedObjectsOnMap; - s.objects = baseClass.objects; - s.orePanAnimation = baseClass.orePanAnimation; - s.orePanPoint = baseClass.orePanPoint; - s.projectiles = baseClass.projectiles; - s.temporarySprites = baseClass.temporarySprites; - s.terrainFeatures = baseClass.terrainFeatures; - s.uniqueName = baseClass.uniqueName; - s.warps = baseClass.warps; - s.wasUpdated = (bool)typeof(GameLocation).GetField("wasUpdated", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(baseClass); - s.waterAnimationIndex = baseClass.waterAnimationIndex; - s.waterAnimationTimer = baseClass.waterAnimationTimer; - s.waterColor = baseClass.waterColor; - s.waterTileFlip = baseClass.waterTileFlip; - s.waterTiles = baseClass.waterTiles; - */ - - - return s; } - public static List<SGameLocation> ConvertGameLocations(List<GameLocation> baseGameLocations) + public static List<SGameLocation> ConstructFromBaseClasses(List<GameLocation> baseGameLocations, bool copyAllData = false) { - return baseGameLocations.Select(ConstructFromBaseClass).ToList(); + return baseGameLocations.Select(gl => ConstructFromBaseClass(gl, copyAllData)).ToList(); } public virtual void update(GameTime gameTime) @@ -110,5 +65,10 @@ namespace StardewModdingAPI.Inheritance v.Value.draw(b, (int)v.Key.X, (int)v.Key.Y, 0.999f, 1); } } + + public SGameLocation() + { + ModObjects = new SerializableDictionary<Vector2, SObject>(); + } } } diff --git a/StardewModdingAPI/Inheritance/SObject.cs b/StardewModdingAPI/Inheritance/SObject.cs index 97d2fa41..5e36b5b7 100644 --- a/StardewModdingAPI/Inheritance/SObject.cs +++ b/StardewModdingAPI/Inheritance/SObject.cs @@ -48,13 +48,23 @@ namespace StardewModdingAPI.Inheritance public override void draw(SpriteBatch spriteBatch, int x, int y, float alpha = 1) { - if (Texture != null) - spriteBatch.Draw(Texture, new Vector2(x, y), new Color(255, 255, 255, 255f * alpha)); + { + int targSize = Game1.tileSize; + int midX = (int) ((x) + 32); + int midY = (int) ((y) + 32); + + int targX = midX - targSize / 2; + int targY = midY - targSize / 2; + + Rectangle targ = new Rectangle(targX, targY, targSize, targSize); + spriteBatch.Draw(Texture, targ, null, new Color(255, 255, 255, 255f * alpha), 0, Vector2.Zero, SpriteEffects.None, 0.999f); + } } public override void draw(SpriteBatch spriteBatch, int xNonTile, int yNonTile, float layerDepth, float alpha = 1) { + Program.LogInfo("DRAW ME2"); return; try { @@ -183,6 +193,11 @@ namespace StardewModdingAPI.Inheritance public override bool placementAction(GameLocation location, int x, int y, Farmer who = null) { + Vector2 key = new Vector2(x, y); + if (!location.objects.ContainsKey(key)) + location.objects.Add(key, this); + return false; + SGameLocation s = SGame.GetLocationFromName(location.name); if (s.GetHashCode() != SGame.CurrentLocation.GetHashCode()) diff --git a/StardewModdingAPI/Program.cs b/StardewModdingAPI/Program.cs index 4277bdce..53a1a2c7 100644 --- a/StardewModdingAPI/Program.cs +++ b/StardewModdingAPI/Program.cs @@ -49,7 +49,7 @@ namespace StardewModdingAPI public static Thread gameThread; public static Thread consoleInputThread; - public const string Version = "0.33 Alpha"; + public const string Version = "0.34 Alpha"; public const bool debug = false; public static bool disableLogging { get; private set; } @@ -63,16 +63,16 @@ namespace StardewModdingAPI if (debug) Console.Title += " - DEBUG IS NOT FALSE, AUTHOUR FORGOT TO INCREMENT VERSION VARS"; - Application.ThreadException += Application_ThreadException; - Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); - AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; - + + + //TODO: Have an app.config and put the paths inside it so users can define locations to load mods from ExecutionPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); ModPaths.Add(Path.Combine(Path.Combine(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "StardewValley")), "Mods")); ModPaths.Add(Path.Combine(ExecutionPath, "Mods")); ModPaths.Add(Path.Combine(Path.Combine(ExecutionPath, "Mods"), "Content")); ModContentPaths.Add(Path.Combine(Path.Combine(Path.Combine(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "StardewValley")), "Mods"), "Content")); + //Checks that all defined modpaths exist as directories foreach (string ModPath in ModPaths) { try @@ -87,6 +87,7 @@ namespace StardewModdingAPI LogError("Could not create a missing ModPath: " + ModPath + "\n\n" + ex); } } + //Same for content foreach (string ModContentPath in ModContentPaths) { try @@ -99,7 +100,7 @@ namespace StardewModdingAPI LogError("Could not create a missing ModContentPath: " + ModContentPath + "\n\n" + ex); } } - + //And then make sure we have an errorlog dir try { if (!Directory.Exists(LogPath)) @@ -110,10 +111,12 @@ namespace StardewModdingAPI LogError("Could not create the missing ErrorLogs path: " + LogPath + "\n\n" + ex); } - CurrentLog = LogPath + "\\MODDED_ProgramLog_" + System.DateTime.Now.Ticks + ".txt"; + //Define the path to the current log file + CurrentLog = LogPath + "\\MODDED_ProgramLog_LATEST"/* + System.DateTime.Now.Ticks + */ + ".txt"; Log(ExecutionPath, false); + //Create a writer to the log file try { LogStream = new StreamWriter(CurrentLog, false); @@ -124,48 +127,62 @@ namespace StardewModdingAPI LogError("Could not initialize LogStream - Logging is disabled"); } + LogInfo("Initializing SDV Assembly..."); if (!File.Exists(ExecutionPath + "\\Stardew Valley.exe")) { + //If the api isn't next to SDV.exe then terminate. Though it'll crash before we even get here w/o sdv.exe. Perplexing. LogError("Could not find: " + ExecutionPath + "\\Stardew Valley.exe"); LogError("The API will now terminate."); Console.ReadKey(); Environment.Exit(-4); } - StardewAssembly = Assembly.LoadFile(ExecutionPath + "\\Stardew Valley.exe"); + //Load in that assembly. Also, ignore security :D + StardewAssembly = Assembly.UnsafeLoadFrom(ExecutionPath + "\\Stardew Valley.exe"); StardewProgramType = StardewAssembly.GetType("StardewValley.Program", true); StardewGameInfo = StardewProgramType.GetField("gamePtr"); - - + //Change the game's version LogInfo("Injecting New SDV Version..."); Game1.version += "-Z_MODDED | SMAPI " + Version; + //Create the thread for the game to run in. gameThread = new Thread(RunGame); LogInfo("Starting SDV..."); gameThread.Start(); + //I forget. SGame.GetStaticFields(); while (!ready) { - + //Wait for the game to load up } + //SDV is running Log("SDV Loaded Into Memory"); - consoleInputThread = new Thread(ConsoleInputThread); + //Create definition to listen for input LogInfo("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 <cmd>' returns command description").CommandFired += help_CommandFired; + //Command.RegisterCommand("crash", "crashes sdv").CommandFired += delegate { Game1.player.draw(null); }; + //Subscribe to events Events.KeyPressed += Events_KeyPressed; Events.LoadContent += Events_LoadContent; - //Events.MenuChanged += Events_MenuChanged; - Events.LocationsChanged += Events_LocationsChanged; - Events.CurrentLocationChanged += Events_CurrentLocationChanged; + //Events.MenuChanged += Events_MenuChanged; //Idk right now + if (debug) + { + //Experimental + Events.LocationsChanged += Events_LocationsChanged; + Events.CurrentLocationChanged += Events_CurrentLocationChanged; + } + //Do tweaks using winforms invoke because I'm lazy LogInfo("Applying Final SDV Tweaks..."); StardewInvoke(() => { @@ -174,11 +191,13 @@ namespace StardewModdingAPI StardewForm.Resize += Events.InvokeResize; }); + //Game's in memory now, send the event LogInfo("Game Loaded"); Events.InvokeGameLoaded(); - consoleInputThread.Start(); LogColour(ConsoleColor.Cyan, "Type 'help' for help, or 'help <cmd>' for a command's usage"); + //Begin listening to input + consoleInputThread.Start(); while (ready) @@ -187,11 +206,14 @@ namespace StardewModdingAPI Thread.Sleep(1000 / 10); } + //abort the thread, we're closing if (consoleInputThread != null && consoleInputThread.ThreadState == ThreadState.Running) consoleInputThread.Abort(); LogInfo("Game Execution Finished"); LogInfo("Shutting Down..."); + Thread.Sleep(100); + /* int time = 0; int step = 100; int target = 1000; @@ -205,6 +227,7 @@ namespace StardewModdingAPI if (time >= target) break; } + */ Environment.Exit(0); } @@ -216,6 +239,12 @@ namespace StardewModdingAPI public static void RunGame() { + //Does this even do anything??? + Application.ThreadException += Application_ThreadException; + Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); + AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; + //I've yet to see it called :| + try { gamePtr = new SGame(); @@ -340,7 +369,7 @@ namespace StardewModdingAPI { if (debug) { - SGame.ModLocations = SGameLocation.ConvertGameLocations(Game1.locations); + SGame.ModLocations = SGameLocation.ConstructFromBaseClasses(Game1.locations); } } @@ -351,7 +380,7 @@ namespace StardewModdingAPI if (debug) { Console.WriteLine(newLocation.name); - SGame.CurrentLocation = SGame.ModLocations.FirstOrDefault(x => x.name == newLocation.name); + SGame.CurrentLocation = SGame.LoadOrCreateSGameLocationFromName(newLocation.name); } //Game1.currentLocation = SGame.CurrentLocation; //LogInfo(((SGameLocation) newLocation).name); @@ -450,6 +479,15 @@ namespace StardewModdingAPI Console.ForegroundColor = ConsoleColor.Gray; } + public static void LogDebug(object o, params object[] format) + { + if (!debug) + return; + Console.ForegroundColor = ConsoleColor.DarkYellow; + Log(o.ToString(), format); + Console.ForegroundColor = ConsoleColor.Gray; + } + public static void LogValueNotSpecified() { LogError("<value> must be specified"); |