From acca21e511028cfcbb875b958c40ac0c462e856a Mon Sep 17 00:00:00 2001 From: JoshuaNavarro Date: Wed, 28 Aug 2019 00:31:14 -0700 Subject: [PATCH] Got multi tiled objects to sync its components so that it displays properly in the world and syncs 100 times better. --- .../Framework/Objects/CustomObject.cs | 14 +- .../Framework/Objects/MultiTiledComponent.cs | 24 +-- .../Framework/Objects/MultiTiledObject.cs | 142 +++++++++++++++++- .../Utilities/MultiplayerUtilities.cs | 20 ++- .../Utilities/Serialization/Serialization.cs | 9 +- GeneralMods/Revitalize/ModCore.cs | 4 +- 6 files changed, 180 insertions(+), 33 deletions(-) diff --git a/GeneralMods/Revitalize/Framework/Objects/CustomObject.cs b/GeneralMods/Revitalize/Framework/Objects/CustomObject.cs index 2487ac83..a78639b0 100644 --- a/GeneralMods/Revitalize/Framework/Objects/CustomObject.cs +++ b/GeneralMods/Revitalize/Framework/Objects/CustomObject.cs @@ -155,8 +155,16 @@ namespace Revitalize.Framework.Objects public Guid guid; + [JsonIgnore] /// The animation manager. - public AnimationManager animationManager => this.info.animationManager; + public AnimationManager animationManager + { + get + { + this.updateInfo(); + return this.info.animationManager; + } + } /// The display texture for this object. [JsonIgnore] @@ -167,7 +175,7 @@ namespace Revitalize.Framework.Objects { get { - return Revitalize.ModCore.Serializer.ToJSONString(this.info)+"<"+this.guid; + return Revitalize.ModCore.Serializer.ToJSONString(this.info)+"<"+this.guid+"<"+ModCore.Serializer.ToJSONString(this.data); } set { @@ -175,8 +183,10 @@ namespace Revitalize.Framework.Objects string[] data = value.Split('<'); string infoString = data[0]; string guidString = data[1]; + string pytkData = data[2]; this.info = (BasicItemInformation)Revitalize.ModCore.Serializer.DeserializeFromJSONString(infoString, typeof(BasicItemInformation)); + this.data = ModCore.Serializer.DeserializeFromJSONString(pytkData); Guid oldGuid = this.guid; this.guid = Guid.Parse(guidString); if (ModCore.CustomObjects.ContainsKey(this.guid)) diff --git a/GeneralMods/Revitalize/Framework/Objects/MultiTiledComponent.cs b/GeneralMods/Revitalize/Framework/Objects/MultiTiledComponent.cs index b3a0d24d..ece39866 100644 --- a/GeneralMods/Revitalize/Framework/Objects/MultiTiledComponent.cs +++ b/GeneralMods/Revitalize/Framework/Objects/MultiTiledComponent.cs @@ -20,9 +20,10 @@ namespace Revitalize.Framework.Objects { string info = Revitalize.ModCore.Serializer.ToJSONString(this.info); string guidStr = this.guid.ToString(); + string pyTkData = ModCore.Serializer.ToJSONString(this.data); string offsetKey = this.offsetKey != null ? ModCore.Serializer.ToJSONString(this.offsetKey) : ""; string container=this.containerObject!=null? this.containerObject.guid.ToString():""; - return info+ "<" +guidStr+"<"+offsetKey+"<"+container; + return info+ "<" +guidStr+"<"+pyTkData+"<"+offsetKey+"<"+container; } set { @@ -30,10 +31,11 @@ namespace Revitalize.Framework.Objects string[] data = value.Split('<'); string infoString = data[0]; string guidString = data[1]; - string offsetVec = data[2]; - string containerObject = data[3]; + string pyTKData = data[2]; + string offsetVec = data[3]; + string containerObject = data[4]; this.info = (BasicItemInformation)Revitalize.ModCore.Serializer.DeserializeFromJSONString(infoString, typeof(BasicItemInformation)); - + this.data = Revitalize.ModCore.Serializer.DeserializeFromJSONString(pyTKData); if (string.IsNullOrEmpty(offsetVec)) return; if (string.IsNullOrEmpty(containerObject)) return; this.offsetKey = ModCore.Serializer.DeserializeFromJSONString(offsetVec); @@ -350,16 +352,6 @@ namespace Revitalize.Framework.Objects } - public override void drawWhenHeld(SpriteBatch spriteBatch, Vector2 objectPosition, Farmer f) - { - base.drawWhenHeld(spriteBatch, objectPosition, f); - } - - public override void draw(SpriteBatch spriteBatch, int xNonTile, int yNonTile, float layerDepth, float alpha = 1) - { - base.draw(spriteBatch, xNonTile, yNonTile, layerDepth, alpha); - } - public override void updateInfo() { if (this.info == null || this.containerObject==null) @@ -376,9 +368,5 @@ namespace Revitalize.Framework.Objects this.info.cleanAfterUpdate(); } } - public override bool requiresUpdate() - { - return base.requiresUpdate(); - } } } diff --git a/GeneralMods/Revitalize/Framework/Objects/MultiTiledObject.cs b/GeneralMods/Revitalize/Framework/Objects/MultiTiledObject.cs index 0ca4e9ab..3a4fe5a2 100644 --- a/GeneralMods/Revitalize/Framework/Objects/MultiTiledObject.cs +++ b/GeneralMods/Revitalize/Framework/Objects/MultiTiledObject.cs @@ -6,6 +6,7 @@ using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Newtonsoft.Json; using PyTK.CustomElementHandler; +using Revitalize.Framework.Utilities; using StardewValley; using StardewValley.Objects; @@ -20,7 +21,28 @@ namespace Revitalize.Framework.Objects { string infoStr = Revitalize.ModCore.Serializer.ToJSONString(this.info); string guidStr = this.guid.ToString(); - return infoStr+ "<" + guidStr; + string pytkData = ModCore.Serializer.ToJSONString(this.data); + string dictStr = ModCore.Serializer.ToJSONString(this.childrenGuids); + if (this.objects != null) + { + foreach (KeyValuePair pair in this.objects) + { + if (pair.Value == null) continue; + if ((pair.Value as CustomObject) == null) continue; + if((pair.Value as CustomObject).guid==null) continue; + if(ModCore.CustomObjects.ContainsKey( (pair.Value as CustomObject).guid)) + { + ModCore.CustomObjects[(pair.Value as CustomObject).guid] = (CustomObject)pair.Value; + } + else + { + ModCore.CustomObjects.Add((pair.Value as CustomObject).guid, (pair.Value as CustomObject)); + } + } + } + + //ModCore.log("Serializing dict:" + dictStr); + return infoStr + "<" + guidStr + "<" + pytkData+"<"+dictStr; } set { @@ -28,10 +50,16 @@ namespace Revitalize.Framework.Objects string[] data = value.Split('<'); string infoString = data[0]; string guidString = data[1]; + string pytkData = data[2]; + string dictStr = data[3]; + //ModCore.log("Getting dictStr:" + dictStr); this.info = (BasicItemInformation)Revitalize.ModCore.Serializer.DeserializeFromJSONString(infoString, typeof(BasicItemInformation)); + this.data = Revitalize.ModCore.Serializer.DeserializeFromJSONString(pytkData); Guid oldGuid = this.guid; this.guid = Guid.Parse(guidString); + + //this.childrenGuids = ModCore.Serializer.DeserializeFromJSONString>(dictStr); if (ModCore.CustomObjects.ContainsKey(this.guid)) { ModCore.CustomObjects[this.guid] = this; @@ -48,6 +76,27 @@ namespace Revitalize.Framework.Objects //ModCore.CustomObjects.Remove(oldGuid); } } + + Dictionary dict = ModCore.Serializer.DeserializeFromJSONString>(dictStr); + if (dict == null) return; + if (dict.Count == 0) return; + else + { + //ModCore.log("Children dicts are updated!"); + this.childrenGuids = dict; + foreach(KeyValuePair pair in dict) + { + if (ModCore.CustomObjects.ContainsKey(pair.Value)) + { + this.removeComponent(pair.Key); + this.addComponent(pair.Key, (MultiTiledComponent)ModCore.CustomObjects[pair.Value]); + } + else + { + MultiplayerUtilities.RequestGuidObject(pair.Value); + } + } + } } } @@ -80,8 +129,8 @@ namespace Revitalize.Framework.Objects } - public MultiTiledObject(CustomObjectData PyTKData,BasicItemInformation info) - : base(PyTKData,info) + public MultiTiledObject(CustomObjectData PyTKData, BasicItemInformation info) + : base(PyTKData, info) { this.objects = new Dictionary(); this.childrenGuids = new Dictionary(); @@ -89,7 +138,7 @@ namespace Revitalize.Framework.Objects } public MultiTiledObject(CustomObjectData PyTKData, BasicItemInformation info, Vector2 TileLocation) - : base(PyTKData,info, TileLocation) + : base(PyTKData, info, TileLocation) { this.objects = new Dictionary(); this.childrenGuids = new Dictionary(); @@ -97,7 +146,7 @@ namespace Revitalize.Framework.Objects } public MultiTiledObject(CustomObjectData PyTKData, BasicItemInformation info, Vector2 TileLocation, Dictionary ObjectsList) - : base(PyTKData,info, TileLocation) + : base(PyTKData, info, TileLocation) { this.objects = new Dictionary(); this.childrenGuids = new Dictionary(); @@ -119,7 +168,7 @@ namespace Revitalize.Framework.Objects } this.objects.Add(key, obj); - if (this.childrenGuids.ContainsKey(key)==false) + if (this.childrenGuids.ContainsKey(key) == false) { this.childrenGuids.Add(key, obj.guid); } @@ -129,9 +178,9 @@ namespace Revitalize.Framework.Objects if (key.Y > this.height) this.height = (int)key.Y; (obj as MultiTiledComponent).containerObject = this; (obj as MultiTiledComponent).offsetKey = key; + this.info.forceUpdate(); return true; } - public bool removeComponent(Vector2 key) { if (!this.objects.ContainsKey(key)) @@ -313,7 +362,7 @@ namespace Revitalize.Framework.Objects { objs.Add(pair.Key, (MultiTiledComponent)pair.Value); } - return new MultiTiledObject(this.data,this.info.Copy(), this.TileLocation, objs); + return new MultiTiledObject(this.data, this.info.Copy(), this.TileLocation, objs); } public override ICustomObject recreate(Dictionary additionalSaveData, object replacement) @@ -418,12 +467,35 @@ namespace Revitalize.Framework.Objects public override void updateInfo() { + //this.recreateComponents(); if (this.info == null) { this.ItemInfo = this.text; ModCore.log("Updated item info for container!"); return; } + if (this.objects != null) + { + if (this.childrenGuids != null) + { + if (this.objects.Count == 0 || this.objects.Count != this.childrenGuids.Count) + { + this.ItemInfo = this.text; + //ModCore.log("Updated item info for container!"); + return; + } + } + else + { + if (this.objects.Count == 0) + { + this.ItemInfo = this.text; + //ModCore.log("Updated item info for container!"); + return; + } + + } + } if (this.requiresUpdate()) { @@ -433,5 +505,59 @@ namespace Revitalize.Framework.Objects } } + public virtual void recreateComponents() + { + + if (ModCore.CustomObjects.ContainsKey(this.guid)) + { + if ((ModCore.CustomObjects[this.guid] as MultiTiledObject).objects.Count > 0) + { + this.objects = (ModCore.CustomObjects[this.guid] as MultiTiledObject).objects; + } + else + { + + } + if((ModCore.CustomObjects[this.guid] as MultiTiledObject).childrenGuids.Count > 0) + { + this.childrenGuids = (ModCore.CustomObjects[this.guid] as MultiTiledObject).childrenGuids; + } + } + else + { + MultiplayerUtilities.RequestGuidObject(this.guid); + MultiplayerUtilities.RequestGuidObject_Tile(this.guid); + } + if (this.objects == null || this.childrenGuids == null) + { + ModCore.log("Either objects or children guids are null"); + return; + } + + + ModCore.log("Recreate children components"); + if (this.objects.Count < this.childrenGuids.Count) + { + foreach (KeyValuePair pair in this.childrenGuids) + { + if (ModCore.CustomObjects.ContainsKey(pair.Value)) + { + this.removeComponent(pair.Key); + this.addComponent(pair.Key,(MultiTiledComponent)ModCore.CustomObjects[pair.Value]); + } + else + { + MultiplayerUtilities.RequestGuidObject(pair.Value); + MultiplayerUtilities.RequestGuidObject_Tile(pair.Value); + } + } + } + else + { + ModCore.log("Count is exactly the same!"); + ModCore.log("Count is: " + this.objects.Count+" : and " + this.childrenGuids.Count); + } + } + } } diff --git a/GeneralMods/Revitalize/Framework/Utilities/MultiplayerUtilities.cs b/GeneralMods/Revitalize/Framework/Utilities/MultiplayerUtilities.cs index 5e2e1c2d..12dcbf9d 100644 --- a/GeneralMods/Revitalize/Framework/Utilities/MultiplayerUtilities.cs +++ b/GeneralMods/Revitalize/Framework/Utilities/MultiplayerUtilities.cs @@ -34,11 +34,13 @@ namespace Revitalize.Framework.Utilities if (ModCore.CustomObjects.ContainsKey((v as CustomObject).guid) == false) { ModCore.CustomObjects.Add((v as CustomObject).guid, v); + v.info.forceUpdate(); v.updateInfo(); } else { ModCore.CustomObjects[(v as CustomObject).guid] = v; + v.info.forceUpdate(); v.updateInfo(); } } @@ -57,11 +59,13 @@ namespace Revitalize.Framework.Utilities if (ModCore.CustomObjects.ContainsKey((v as CustomObject).guid) == false) { ModCore.CustomObjects.Add((v as CustomObject).guid, v); + v.info.forceUpdate(); v.updateInfo(); } else { ModCore.CustomObjects[(v as CustomObject).guid] = v; + v.info.forceUpdate(); v.updateInfo(); } } @@ -81,13 +85,16 @@ namespace Revitalize.Framework.Utilities { if (ModCore.CustomObjects.ContainsKey(request)) { - //ModCore.log("Send guid request!"); + //ModCore.log("Send guid request: "+request.ToString()); //ModCore.CustomObjects[request].forceUpdate(); + ModCore.CustomObjects[request].info.forceUpdate(); + ModCore.CustomObjects[request].updateInfo(); + ModCore.ModHelper.Multiplayer.SendMessage(ModCore.Serializer.ToJSONString(ModCore.CustomObjects[request]), ReceieveGUIDMessage, new string[] { Revitalize.ModCore.Manifest.UniqueID.ToString() }); } else { - //ModCore.log("This mod doesn't have the guid object"); + ModCore.log("This mod doesn't have the guid object"); } } @@ -98,7 +105,14 @@ namespace Revitalize.Framework.Utilities //ModCore.log("Send guid tile request!"); //(ModCore.CustomObjects[request] as MultiTiledComponent).forceUpdate(); //(ModCore.CustomObjects[request] as MultiTiledComponent).containerObject.forceUpdate(); - (ModCore.CustomObjects[request] as MultiTiledComponent).containerObject.updateInfo(); + try + { + (ModCore.CustomObjects[request] as MultiTiledComponent).containerObject.updateInfo(); + } + catch(Exception err) + { + + } ModCore.ModHelper.Multiplayer.SendMessage(ModCore.Serializer.ToJSONString( (ModCore.CustomObjects[request] as MultiTiledComponent).containerObject), ReceieveGUIDMessage_Tile , new string[] { Revitalize.ModCore.Manifest.UniqueID.ToString() }); } else diff --git a/GeneralMods/Revitalize/Framework/Utilities/Serialization/Serialization.cs b/GeneralMods/Revitalize/Framework/Utilities/Serialization/Serialization.cs index 72ea284b..faa54ebf 100644 --- a/GeneralMods/Revitalize/Framework/Utilities/Serialization/Serialization.cs +++ b/GeneralMods/Revitalize/Framework/Utilities/Serialization/Serialization.cs @@ -144,13 +144,20 @@ namespace Revitalize.Framework.Utilities public void restoreModObjects() { //Replace all items in the world. + List objsToRestore = new List(); foreach (var v in ModCore.ObjectGroups) { foreach (var obj in v.Value.objects.Values) { - (obj as CustomObject).replaceAfterLoad(); + //(obj as CustomObject).replaceAfterLoad(); + objsToRestore.Add(obj as CustomObject); } } + foreach(CustomObject o in objsToRestore) + { + (o as CustomObject).replaceAfterLoad(); + } + //Replace all held items or items in inventories. foreach (GameLocation loc in LocationUtilities.GetAllLocations()) { diff --git a/GeneralMods/Revitalize/ModCore.cs b/GeneralMods/Revitalize/ModCore.cs index 868d2156..5c5b9593 100644 --- a/GeneralMods/Revitalize/ModCore.cs +++ b/GeneralMods/Revitalize/ModCore.cs @@ -208,6 +208,7 @@ namespace Revitalize this.initailizeComponents(); Serializer = new Serializer(); playerInfo = new PlayerInfo(); + CustomObjects = new Dictionary(); //Loads in textures to be used by the mod. this.loadInTextures(); @@ -234,7 +235,7 @@ namespace Revitalize ModHelper.Events.Multiplayer.ModMessageReceived += MultiplayerUtilities.GetModMessage; ModHelper.Events.GameLoop.DayEnding += this.GameLoop_DayEnding; ModHelper.Events.GameLoop.Saving += this.GameLoop_Saving; - CustomObjects = new Dictionary(); + //Adds in recipes to the mod. VanillaRecipeBook = new VanillaRecipeBook(); @@ -433,6 +434,7 @@ namespace Revitalize //Game1.player.addItemToInventory(ObjectManager.resources.getOre("Tin", 19)); //Ore tin = ObjectManager.resources.getOre("Tin", 19); Game1.player.addItemToInventory(ObjectManager.GetItem("TinIngot", 1)); + Game1.player.addItemToInventory(new StardewValley.Object(388, 100)); //ModCore.log("Tin sells for: " + tin.sellToStorePrice());