From 7799834f4600964bed2df4e144fc866a3ff5bdcf Mon Sep 17 00:00:00 2001 From: Joshua Navarro Date: Thu, 10 Jan 2019 12:44:10 -0800 Subject: [PATCH] It compiles in single player but does not like multiplayer. --- .../Framework/Objects/CustomObject.cs | 10 ++- .../Furniture/ChairMultiTiledObject.cs | 4 +- .../Objects/Furniture/ChairTileComponent.cs | 2 +- .../Framework/Objects/MultiTiledComponent.cs | 33 +++++----- .../Framework/Objects/MultiTiledObject.cs | 64 ++++++++++++++----- GeneralMods/Revitalize/ModCore.cs | 30 --------- 6 files changed, 77 insertions(+), 66 deletions(-) diff --git a/GeneralMods/Revitalize/Framework/Objects/CustomObject.cs b/GeneralMods/Revitalize/Framework/Objects/CustomObject.cs index 19de257d..ffae4a48 100644 --- a/GeneralMods/Revitalize/Framework/Objects/CustomObject.cs +++ b/GeneralMods/Revitalize/Framework/Objects/CustomObject.cs @@ -25,6 +25,8 @@ namespace Revitalize.Framework.Objects public BasicItemInformation info; public GameLocation location; + + public Guid guid; /// The animation manager. public AnimationManager animationManager => this.info.animationManager; @@ -36,7 +38,9 @@ namespace Revitalize.Framework.Objects /// Empty constructor. - public CustomObject() { } + public CustomObject() { + this.guid = Guid.NewGuid(); + } /// Construct an instance. public CustomObject(BasicItemInformation info) @@ -44,6 +48,7 @@ namespace Revitalize.Framework.Objects { this.info = info; this.initializeBasics(); + this.guid = Guid.NewGuid(); } /// Construct an instance. @@ -52,6 +57,7 @@ namespace Revitalize.Framework.Objects { this.info = info; this.initializeBasics(); + this.guid = Guid.NewGuid(); } /// Sets some basic information up. @@ -334,6 +340,8 @@ namespace Revitalize.Framework.Objects /// What happens when the object is drawn when held by a player. public override void drawWhenHeld(SpriteBatch spriteBatch, Vector2 objectPosition, StardewValley.Farmer f) { + if (this.animationManager == null) Revitalize.ModCore.log("Animation Manager Null"); + if (this.displayTexture == null) Revitalize.ModCore.log("Display texture is null"); if (f.ActiveObject.bigCraftable.Value) { spriteBatch.Draw(this.displayTexture, objectPosition, this.animationManager.currentAnimation.sourceRectangle, this.info.drawColor, 0f, Vector2.Zero, (float)Game1.pixelZoom, SpriteEffects.None, Math.Max(0f, (float)(f.getStandingY() + 2) / 10000f)); diff --git a/GeneralMods/Revitalize/Framework/Objects/Furniture/ChairMultiTiledObject.cs b/GeneralMods/Revitalize/Framework/Objects/Furniture/ChairMultiTiledObject.cs index 9519d1df..72664884 100644 --- a/GeneralMods/Revitalize/Framework/Objects/Furniture/ChairMultiTiledObject.cs +++ b/GeneralMods/Revitalize/Framework/Objects/Furniture/ChairMultiTiledObject.cs @@ -42,11 +42,11 @@ namespace Revitalize.Framework.Objects.Furniture public override void rotate() { Revitalize.ModCore.log("Rotate!"); - foreach(KeyValuePair pair in this.objects) + foreach(KeyValuePair pair in this.objects) { (pair.Value as ChairTileComponent).rotate(); } - foreach (KeyValuePair pair in this.objects) + foreach (KeyValuePair pair in this.objects) { (pair.Value as ChairTileComponent).checkForSpecialUpSittingAnimation(); } diff --git a/GeneralMods/Revitalize/Framework/Objects/Furniture/ChairTileComponent.cs b/GeneralMods/Revitalize/Framework/Objects/Furniture/ChairTileComponent.cs index 11fb9914..fa39aaac 100644 --- a/GeneralMods/Revitalize/Framework/Objects/Furniture/ChairTileComponent.cs +++ b/GeneralMods/Revitalize/Framework/Objects/Furniture/ChairTileComponent.cs @@ -69,7 +69,7 @@ namespace Revitalize.Framework.Objects.Furniture if (this.CanSitHere) { Revitalize.ModCore.playerInfo.sittingInfo.sit(this.containerObject, this.TileLocation*Game1.tileSize); - foreach(KeyValuePair pair in this.containerObject.objects) + foreach(KeyValuePair pair in this.containerObject.objects) { (pair.Value as ChairTileComponent).checkForSpecialUpSittingAnimation(); } diff --git a/GeneralMods/Revitalize/Framework/Objects/MultiTiledComponent.cs b/GeneralMods/Revitalize/Framework/Objects/MultiTiledComponent.cs index 310d48f6..b2601011 100644 --- a/GeneralMods/Revitalize/Framework/Objects/MultiTiledComponent.cs +++ b/GeneralMods/Revitalize/Framework/Objects/MultiTiledComponent.cs @@ -116,26 +116,28 @@ namespace Revitalize.Framework.Objects Vector2 offsetKey = new Vector2(Convert.ToInt32(additionalSaveData["offsetKeyX"]), Convert.ToInt32(additionalSaveData["offsetKeyY"])); - - + + Revitalize.ModCore.log("HELLO WORLD!"); //do same container creation logic in multitiled object - MultiTiledComponent self = null; + MultiTiledComponent self = Revitalize.ModCore.Serializer.Deserialize(Path.Combine(Revitalize.ModCore.ModHelper.DirectoryPath, additionalSaveData["GUID"] + ".json")); - if (!Revitalize.ModCore.ObjectGroups.ContainsKey(additionalSaveData["GUID"])) + if (!Revitalize.ModCore.ObjectGroups.ContainsKey(additionalSaveData["ParentGUID"])) { //Get new container - MultiTiledObject obj = (MultiTiledObject)Revitalize.ModCore.Serializer.Deserialize(Path.Combine(Revitalize.ModCore.ModHelper.DirectoryPath, additionalSaveData["GUID"] + ".json")); - self = (MultiTiledComponent)(obj as MultiTiledObject).objects[offsetKey]; - self.containerObject = obj; - Revitalize.ModCore.ObjectGroups.Add(additionalSaveData["GUID"], (MultiTiledObject)obj); + MultiTiledObject obj = (MultiTiledObject)Revitalize.ModCore.Serializer.Deserialize(Path.Combine(Revitalize.ModCore.ModHelper.DirectoryPath, additionalSaveData["ParentGUID"] + ".json")); + self.containerObject = obj; + obj.addComponent(offsetKey, self); + Revitalize.ModCore.log("ADD IN AN OBJECT!!!!"); + Revitalize.ModCore.ObjectGroups.Add(additionalSaveData["GUID"], (MultiTiledObject)obj); } else { - self =(MultiTiledComponent)Revitalize.ModCore.ObjectGroups[additionalSaveData["GUID"]].objects[offsetKey]; self.containerObject = Revitalize.ModCore.ObjectGroups[additionalSaveData["GUID"]]; + Revitalize.ModCore.ObjectGroups[additionalSaveData["GUID"]].addComponent(offsetKey, self); + Revitalize.ModCore.log("READD AN OBJECT!!!!"); } return (ICustomObject)self; @@ -174,7 +176,9 @@ namespace Revitalize.Framework.Objects saveData.Add("GameLocationName", saveLocation); saveData.Add("Rotation", ((int)this.info.facingDirection).ToString()); - saveData.Add("GUID", this.containerObject.guid.ToString()); + saveData.Add("ParentGUID", this.containerObject.guid.ToString()); + saveData.Add("GUID", this.guid.ToString()); + Revitalize.ModCore.Serializer.Serialize(Path.Combine(Revitalize.ModCore.ModHelper.DirectoryPath, this.containerObject.childrenGuids[this.offsetKey].ToString() + ".json"),this); return saveData; @@ -197,7 +201,9 @@ namespace Revitalize.Framework.Objects public override void draw(SpriteBatch spriteBatch, int x, int y, float alpha = 1f) { Revitalize.ModCore.log("DRAW THE THING!!!"); - + if (this.info == null) Revitalize.ModCore.log("info is null"); + if (this.animationManager == null) Revitalize.ModCore.log("Animation Manager Null"); + if (this.displayTexture == null) Revitalize.ModCore.log("Display texture is null"); if (x <= -1) { spriteBatch.Draw(this.info.animationManager.getTexture(), Game1.GlobalToLocal(Game1.viewport, this.info.drawPosition), new Rectangle?(this.animationManager.currentAnimation.sourceRectangle), this.info.drawColor * alpha, 0f, Vector2.Zero, (float)Game1.pixelZoom, this.flipped ? SpriteEffects.FlipHorizontally : SpriteEffects.None, Math.Max(0f, (float)(this.TileLocation.Y * Game1.tileSize) / 10000f)); @@ -241,10 +247,7 @@ namespace Revitalize.Framework.Objects } } - public static implicit operator MultiTiledComponent(Chest chest) - { - return new MultiTiledComponent(new BasicItemInformation(),chest.TileLocation); - } + } } diff --git a/GeneralMods/Revitalize/Framework/Objects/MultiTiledObject.cs b/GeneralMods/Revitalize/Framework/Objects/MultiTiledObject.cs index 4d240496..ff245bcc 100644 --- a/GeneralMods/Revitalize/Framework/Objects/MultiTiledObject.cs +++ b/GeneralMods/Revitalize/Framework/Objects/MultiTiledObject.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.IO; +using System.Text; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Newtonsoft.Json; @@ -11,9 +12,10 @@ namespace Revitalize.Framework.Objects { public class MultiTiledObject : CustomObject { - public Dictionary objects; + [JsonIgnore] + public Dictionary objects; - public Guid guid; + public Dictionary childrenGuids; private int width; private int height; @@ -34,28 +36,32 @@ namespace Revitalize.Framework.Objects public MultiTiledObject() { - this.objects = new Dictionary(); + this.objects = new Dictionary(); + this.childrenGuids = new Dictionary(); this.guid = Guid.NewGuid(); } public MultiTiledObject(BasicItemInformation info) : base(info) { - this.objects = new Dictionary(); + this.objects = new Dictionary(); + this.childrenGuids = new Dictionary(); this.guid = Guid.NewGuid(); } public MultiTiledObject(BasicItemInformation info, Vector2 TileLocation) : base(info, TileLocation) { - this.objects = new Dictionary(); + this.objects = new Dictionary(); + this.childrenGuids = new Dictionary(); this.guid = Guid.NewGuid(); } public MultiTiledObject(BasicItemInformation info, Vector2 TileLocation, Dictionary ObjectsList) : base(info, TileLocation) { - this.objects = new Dictionary(); + this.objects = new Dictionary(); + this.childrenGuids = new Dictionary(); foreach (var v in ObjectsList) { MultiTiledComponent component =(MultiTiledComponent) v.Value.getOne(); @@ -71,6 +77,8 @@ namespace Revitalize.Framework.Objects return false; this.objects.Add(key, obj); + this.childrenGuids.Add(key, Guid.NewGuid()); + if (key.X > this.width) this.width = (int)key.X; if (key.Y > this.height) this.height = (int)key.Y; (obj as MultiTiledComponent).containerObject = this; @@ -89,13 +97,13 @@ namespace Revitalize.Framework.Objects public override void draw(SpriteBatch spriteBatch, int x, int y, float alpha = 1) { - foreach (KeyValuePair pair in this.objects) + foreach (KeyValuePair pair in this.objects) pair.Value.draw(spriteBatch, x + (int)pair.Key.X * Game1.tileSize, y + (int)pair.Key.Y * Game1.tileSize, alpha); } public override void draw(SpriteBatch spriteBatch, int xNonTile, int yNonTile, float layerDepth, float alpha = 1) { - foreach (KeyValuePair pair in this.objects) + foreach (KeyValuePair pair in this.objects) pair.Value.draw(spriteBatch, xNonTile + (int)pair.Key.X * Game1.tileSize, yNonTile + (int)pair.Key.Y * Game1.tileSize, layerDepth, alpha); //base.draw(spriteBatch, xNonTile, yNonTile, layerDepth, alpha); @@ -103,14 +111,14 @@ namespace Revitalize.Framework.Objects public override void drawInMenu(SpriteBatch spriteBatch, Vector2 location, float scaleSize, float transparency, float layerDepth, bool drawStackNumber, Color c, bool drawShadow) { - foreach (KeyValuePair pair in this.objects) + foreach (KeyValuePair pair in this.objects) pair.Value.drawInMenu(spriteBatch, location + (pair.Key * 16), 1.0f, transparency, layerDepth, drawStackNumber, c, drawShadow); //base.drawInMenu(spriteBatch, location, scaleSize, transparency, layerDepth, drawStackNumber, c, drawShadow); } public override void drawWhenHeld(SpriteBatch spriteBatch, Vector2 objectPosition, Farmer f) { - foreach (KeyValuePair pair in this.objects) + foreach (KeyValuePair pair in this.objects) pair.Value.drawWhenHeld(spriteBatch, objectPosition + (pair.Key * Game1.tileSize), f); //base.drawWhenHeld(spriteBatch, objectPosition, f); } @@ -123,7 +131,7 @@ namespace Revitalize.Framework.Objects bool canPickUp = this.removeAndAddToPlayersInventory(); if (canPickUp) { - foreach (KeyValuePair pair in this.objects) + foreach (KeyValuePair pair in this.objects) (pair.Value as MultiTiledComponent).removeFromLocation((pair.Value as MultiTiledComponent).location, pair.Key); this.location = null; } @@ -144,7 +152,7 @@ namespace Revitalize.Framework.Objects public override bool placementAction(GameLocation location, int x, int y, Farmer who = null) { - foreach (KeyValuePair pair in this.objects) + foreach (KeyValuePair pair in this.objects) { pair.Value.placementAction(location, x + (int)pair.Key.X * Game1.tileSize, y + (int)pair.Key.Y * Game1.tileSize, who); //ModCore.log(pair.Value.TileLocation); @@ -156,7 +164,7 @@ namespace Revitalize.Framework.Objects public override bool canBePlacedHere(GameLocation l, Vector2 tile) { - foreach (KeyValuePair pair in this.objects) + foreach (KeyValuePair pair in this.objects) { if (!pair.Value.canBePlacedHere(l, tile + pair.Key)) return false; @@ -191,7 +199,12 @@ namespace Revitalize.Framework.Objects public override Item getOne() { - return new MultiTiledObject(this.info, this.TileLocation, this.objects); + Dictionary objs = new Dictionary(); + foreach (var pair in this.objects) + { + objs.Add(pair.Key, (MultiTiledComponent)pair.Value); + } + return new MultiTiledObject(this.info, this.TileLocation, objs); } public override ICustomObject recreate(Dictionary additionalSaveData, object replacement) @@ -201,11 +214,26 @@ namespace Revitalize.Framework.Objects MultiTiledObject obj = (MultiTiledObject)Revitalize.ModCore.Serializer.Deserialize(Path.Combine(Revitalize.ModCore.ModHelper.DirectoryPath, additionalSaveData["GUID"] + ".json")); - foreach(KeyValuePair pair in this.objects) + Revitalize.ModCore.log("OK I SUPPOSE"); + + Dictionary guids = new Dictionary(); + + foreach(KeyValuePair pair in obj.childrenGuids) { - pair.Value.containerObject = obj; + guids.Add(pair.Key, pair.Value); } + foreach(KeyValuePair pair in guids) + { + obj.childrenGuids.Remove(pair.Key); + Revitalize.ModCore.log("DESERIALIZE: " + pair.Value.ToString()); + MultiTiledComponent component= (MultiTiledComponent)Revitalize.ModCore.Serializer.Deserialize(Path.Combine(Revitalize.ModCore.ModHelper.DirectoryPath, pair.Value + ".json")); + obj.addComponent(pair.Key, component); + + + } + + if (!Revitalize.ModCore.ObjectGroups.ContainsKey(additionalSaveData["GUID"])) { Revitalize.ModCore.ObjectGroups.Add(additionalSaveData["GUID"], obj); @@ -223,12 +251,14 @@ namespace Revitalize.Framework.Objects { Dictionary saveData= base.getAdditionalSaveData(); saveData.Add("GUID", this.guid.ToString()); + + Revitalize.ModCore.Serializer.Serialize(Path.Combine(Revitalize.ModCore.ModHelper.DirectoryPath, this.guid.ToString() + ".json"), this); return saveData; } public void setAllAnimationsToDefault() { - foreach(KeyValuePair pair in this.objects) + foreach(KeyValuePair pair in this.objects) { string animationKey = (pair.Value as MultiTiledComponent) .generateDefaultRotationalAnimationKey(); if ((pair.Value as MultiTiledComponent).animationManager.animations.ContainsKey(animationKey)) diff --git a/GeneralMods/Revitalize/ModCore.cs b/GeneralMods/Revitalize/ModCore.cs index 469bc4b7..4964d35d 100644 --- a/GeneralMods/Revitalize/ModCore.cs +++ b/GeneralMods/Revitalize/ModCore.cs @@ -277,39 +277,9 @@ namespace Revitalize { Game1.player.addItemToInventory(customObjects["Omegasis.BigTiledTest"]); - var obj = customObjects["Omegasis.BigTiledTest"]; - Serializer.Serialize(Path.Combine(this.Helper.DirectoryPath, (obj as MultiTiledObject).guid + ".json"), obj); - - MultiTiledObject hello=Serializer.Deserialize(Path.Combine(this.Helper.DirectoryPath, (obj as MultiTiledObject).guid + ".json")); - - //(hello as MultiTiledObject).info.drawColor = Color.Blue; - customObjects["Omegasis.BigTiledTest"].info.drawColor = hello.info.drawColor; - if (hello == null) log("WTF"); - else - { - log("AHHHH" + hello.name); - } - hello.info.drawColor = Color.Blue; - if (hello.objects == null) - { - log("NEVER MIND"); - } - foreach(KeyValuePair pair in hello.objects){ - pair.Value.containerObject = hello; - //log((pair.Value as CustomObject).name); - } - - Game1.player.items.Add(hello); - - - - Game1.activeClickableMenu = new StardewValley.Menus.ItemGrabMenu(new List() - { - hello - }); }