From 19732ad2bf1684c2077febda826babd94ac43ee2 Mon Sep 17 00:00:00 2001 From: JoshuaNavarro Date: Tue, 27 Aug 2019 16:47:22 -0700 Subject: [PATCH] Got objects to sync across locations even when not present! --- .../Framework/Illuminate/LightManager.cs | 8 + .../Framework/Objects/BasicItemInformation.cs | 336 ++++++++++++++++-- .../Framework/Objects/CustomObject.cs | 30 ++ .../Framework/Objects/MultiTiledComponent.cs | 20 +- .../Framework/Objects/MultiTiledObject.cs | 15 +- .../Framework/Utilities/InventoryManager.cs | 11 + .../Utilities/MultiplayerUtilities.cs | 19 +- .../Animations/AnimationManager.cs | 12 +- 8 files changed, 408 insertions(+), 43 deletions(-) diff --git a/GeneralMods/Revitalize/Framework/Illuminate/LightManager.cs b/GeneralMods/Revitalize/Framework/Illuminate/LightManager.cs index 6b1f3ef6..7968a389 100644 --- a/GeneralMods/Revitalize/Framework/Illuminate/LightManager.cs +++ b/GeneralMods/Revitalize/Framework/Illuminate/LightManager.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; +using Newtonsoft.Json; using StardewValley; namespace Revitalize.Framework.Illuminate @@ -29,6 +30,9 @@ namespace Revitalize.Framework.Illuminate /// public const int lightBigNumber= 1000000; + [JsonIgnore] + public bool requiresUpdate; + /// /// Constructor. /// @@ -55,6 +59,7 @@ namespace Revitalize.Framework.Illuminate this.lights.Add(IdKey, light); if (this.fakeLights.ContainsKey(IdKey)) return true; this.fakeLights.Add(IdKey, new FakeLightSource(light.Identifier, light.position.Value, light.color.Value.Invert(), light.radius.Value)); + this.requiresUpdate = true; return true; } @@ -80,6 +85,7 @@ namespace Revitalize.Framework.Illuminate this.lights.Add(IdKey, light); if (this.fakeLights.ContainsKey(IdKey)) return true; this.fakeLights.Add(IdKey, new FakeLightSource(light.Identifier, light.position.Value, light.color.Value.Invert(), light.radius.Value)); + this.requiresUpdate = true; return true; } @@ -121,6 +127,7 @@ namespace Revitalize.Framework.Illuminate Game1.currentLightSources.Add(light); location.sharedLights.Add((int)IdKey.X*lightBigNumber+(int)IdKey.Y,light); this.repositionLight(light, IdKey, gameObject); + this.requiresUpdate = true; return true; } @@ -161,6 +168,7 @@ namespace Revitalize.Framework.Illuminate { Vector2 initialPosition = gameObject.TileLocation * Game1.tileSize; light.position.Value = initialPosition + offset; + this.requiresUpdate = true; } /// diff --git a/GeneralMods/Revitalize/Framework/Objects/BasicItemInformation.cs b/GeneralMods/Revitalize/Framework/Objects/BasicItemInformation.cs index ce3405dc..c5e8c3c9 100644 --- a/GeneralMods/Revitalize/Framework/Objects/BasicItemInformation.cs +++ b/GeneralMods/Revitalize/Framework/Objects/BasicItemInformation.cs @@ -7,40 +7,305 @@ using Revitalize.Framework.Illuminate; using Revitalize.Framework.Utilities; using StardewValley; using StardustCore.UIUtilities; +using Newtonsoft.Json; namespace Revitalize.Framework.Objects { public class BasicItemInformation { - public string name; - public string id; - public string description; - public string categoryName; - public Color categoryColor; - public int price; - public int edibility; - public int fragility; - public bool canBeSetIndoors; - public bool canBeSetOutdoors; - public bool isLamp; - public string locationName; + private string _name; + public string name + { + get + { + return this._name; + } + set + { + this._name = value; + this.requiresUpdate = true; + } + } + + private string _id; + public string id + { + get + { + return this._id; + } + set + { + this._id = value; + this.requiresUpdate = true; + } + } - public AnimationManager animationManager; - public Vector2 drawPosition; + private string _description; + public string description + { + get + { + return this._description; + } + set + { + this._description = value; + this.requiresUpdate = true; + } + } - public Color drawColor; + private string _categoryName; + public string categoryName + { + get + { + return this._categoryName; + } + set + { + this._categoryName = value; + this.requiresUpdate = true; + } + } - public bool ignoreBoundingBox; + private Color _categoryColor; + public Color categoryColor + { + get + { + return this._categoryColor; + } + set + { + this._categoryColor = value; + this.requiresUpdate = true; + } + } - public InventoryManager inventory; + private int _price; + public int price + { + get + { + return this._price; + } + set + { + this._price = value; + this.requiresUpdate = true; + } + } - public LightManager lightManager; + private int _edibility; + public int edibility + { + get + { + return this._edibility; + } + set + { + this._edibility = value; + this.requiresUpdate = true; + } + } - public Enums.Direction facingDirection; - public int shakeTimer; + private int _fragility; + public int fragility + { + get + { + return this._fragility; + } + set + { + this._fragility = value; + this.requiresUpdate = true; + } + } + + + private bool _canBeSetIndoors; + public bool canBeSetIndoors + { + get + { + return this._canBeSetIndoors; + } + set + { + this._canBeSetIndoors = value; + this.requiresUpdate = true; + } + } + + + + private bool _canBeSetOutdoors; + public bool canBeSetOutdoors + { + get + { + return this._canBeSetOutdoors; + } + set + { + this._canBeSetOutdoors = value; + this.requiresUpdate = true; + } + } + + private bool _isLamp; + public bool isLamp + { + get + { + return this._isLamp; + } + set + { + this._isLamp = value; + this.requiresUpdate = true; + } + } + + private string _locationName; + public string locationName + { + get + { + return this._locationName; + + } + set + { + this._locationName = value; + this.requiresUpdate = true; + } + } + + private AnimationManager _animationManager; + public AnimationManager animationManager + { + get + { + return this._animationManager; + } + set + { + this._animationManager = value; + this.requiresUpdate = true; + } + } + + private Vector2 _drawPosition; + public Vector2 drawPosition + { + get + { + return this._drawPosition; + } + set + { + this._drawPosition = value; + this.requiresUpdate = true; + } + } + + + private Color _drawColor; + public Color drawColor + { + get + { + return this._drawColor; + } + set + { + this._drawColor = value; + this.requiresUpdate = true; + } + } + + + private bool _ignoreBoundingBox; + public bool ignoreBoundingBox + { + get + { + return this._ignoreBoundingBox; + } + set + { + this._ignoreBoundingBox = value; + this.requiresUpdate = true; + } + } + + private InventoryManager _inventory; + public InventoryManager inventory + { + get + { + return this._inventory; + } + set + { + this._inventory = value; + this.requiresUpdate = true; + } + } + + + private LightManager _lightManager; + public LightManager lightManager + { + get + { + return this._lightManager; + } + set + { + this._lightManager = value; + this.requiresUpdate = true; + } + } + + private Enums.Direction _facingDirection; + public Enums.Direction facingDirection + { + get + { + return this._facingDirection; + } + set + { + this._facingDirection = value; + this.requiresUpdate = true; + } + } + + + private int _shakeTimer; + public int shakeTimer + { + get + { + return this._shakeTimer; + } + set + { + this._shakeTimer = value; + this.requiresUpdate = true; + } + } + + [JsonIgnore] + public bool requiresUpdate; public BasicItemInformation() { this.name = ""; @@ -61,7 +326,6 @@ namespace Revitalize.Framework.Objects this.facingDirection = Enums.Direction.Down; this.id = ""; this.shakeTimer = 0; - } public BasicItemInformation(string name, string id, string description, string categoryName, Color categoryColor,int edibility, int fragility, bool isLamp, int price, bool canBeSetOutdoors, bool canBeSetIndoors, Texture2D texture, AnimationManager animationManager, Color drawColor, bool ignoreBoundingBox, InventoryManager Inventory, LightManager Lights) @@ -116,7 +380,35 @@ namespace Revitalize.Framework.Objects public bool requiresSyncUpdate() { - return true; + return this.requiresUpdate || this.animationManagerRequiresUpdate() || this.inventoryManagerRequiresUpdate() || this.lightManagerRequiresUpdate(); + } + + public void forceUpdate() + { + this.requiresUpdate = true; + } + private bool animationManagerRequiresUpdate() + { + if (this._animationManager == null) return false; + else return this._animationManager.requiresUpdate; + } + private bool inventoryManagerRequiresUpdate() + { + if (this._inventory == null) return false; + else return this._inventory.requiresUpdate; + } + private bool lightManagerRequiresUpdate() + { + if (this._lightManager == null) return false; + else return this._lightManager.requiresUpdate; + } + + public void cleanAfterUpdate() + { + this.requiresUpdate = false; + this._inventory.requiresUpdate = false; + this._animationManager.requiresUpdate = false; + this._lightManager.requiresUpdate = false; } } diff --git a/GeneralMods/Revitalize/Framework/Objects/CustomObject.cs b/GeneralMods/Revitalize/Framework/Objects/CustomObject.cs index 58857e14..5d4bf9e9 100644 --- a/GeneralMods/Revitalize/Framework/Objects/CustomObject.cs +++ b/GeneralMods/Revitalize/Framework/Objects/CustomObject.cs @@ -629,8 +629,38 @@ namespace Revitalize.Framework.Objects return; } + if (this.requiresUpdate()) + { + this.ItemInfo = this.text; + this.text = this.ItemInfo; + this.info.cleanAfterUpdate(); + } + } + + public virtual void forceUpdate() + { + if (this.info == null) + { + this.ItemInfo = this.text; + ModCore.log("Updated item info!"); + return; + } this.ItemInfo = this.text; this.text = this.ItemInfo; + this.info.cleanAfterUpdate(); + this.info.forceUpdate(); + } + + public virtual bool requiresUpdate() + { + if (this.info.requiresSyncUpdate()) + { + return true; + } + else + { + return false; + } } //~~~~~~~~~~~~~~~~~~~~~~~~~// diff --git a/GeneralMods/Revitalize/Framework/Objects/MultiTiledComponent.cs b/GeneralMods/Revitalize/Framework/Objects/MultiTiledComponent.cs index a8628aa2..7f285116 100644 --- a/GeneralMods/Revitalize/Framework/Objects/MultiTiledComponent.cs +++ b/GeneralMods/Revitalize/Framework/Objects/MultiTiledComponent.cs @@ -271,6 +271,8 @@ namespace Revitalize.Framework.Objects { Revitalize.ModCore.Serializer.SerializeGUID(this.containerObject.childrenGuids[this.offsetKey].ToString(), this); } + + this.containerObject.getAdditionalSaveData(); return saveData; @@ -360,11 +362,23 @@ namespace Revitalize.Framework.Objects public override void updateInfo() { - if (this.containerObject != null) + if (this.info == null || this.containerObject==null) { - this.containerObject.updateInfo(); + this.ItemInfo = this.text; + ModCore.log("Updated item info!"); + return; } - base.updateInfo(); + + if (this.requiresUpdate()) + { + this.ItemInfo = this.text; + this.text = this.ItemInfo; + 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 9d86a4bd..0ca4e9ab 100644 --- a/GeneralMods/Revitalize/Framework/Objects/MultiTiledObject.cs +++ b/GeneralMods/Revitalize/Framework/Objects/MultiTiledObject.cs @@ -425,19 +425,12 @@ namespace Revitalize.Framework.Objects return; } - this.ItemInfo = this.text; - this.text = this.ItemInfo; - - if (this.objects == null) + if (this.requiresUpdate()) { - return; + this.ItemInfo = this.text; + this.text = this.ItemInfo; + this.info.cleanAfterUpdate(); } - /* - foreach(CustomObject c in this.objects.Values) - { - c.updateInfo(); - } - */ } } diff --git a/GeneralMods/Revitalize/Framework/Utilities/InventoryManager.cs b/GeneralMods/Revitalize/Framework/Utilities/InventoryManager.cs index ecf938ac..c16409e2 100644 --- a/GeneralMods/Revitalize/Framework/Utilities/InventoryManager.cs +++ b/GeneralMods/Revitalize/Framework/Utilities/InventoryManager.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using System.Linq; +using Newtonsoft.Json; using StardewValley; namespace Revitalize.Framework.Utilities @@ -25,6 +26,8 @@ namespace Revitalize.Framework.Utilities /// Checks to see if this core object actually has a valid inventory. public bool HasInventory => this.capacity > 0; + [JsonIgnore] + public bool requiresUpdate; public InventoryManager() { this.capacity = 0; @@ -77,9 +80,11 @@ namespace Revitalize.Framework.Utilities if (self != null && self.canStackWith(item)) { self.addToStack(item.Stack); + this.requiresUpdate = true; return true; } } + this.requiresUpdate = true; this.items.Add(item); return true; } @@ -108,6 +113,7 @@ namespace Revitalize.Framework.Utilities if (item.Stack == 1) return item; + this.requiresUpdate = true; item.Stack = item.Stack - 1; return item.getOne(); } @@ -115,6 +121,7 @@ namespace Revitalize.Framework.Utilities /// Empty the inventory. public void clear() { + this.requiresUpdate = true; this.items.Clear(); } @@ -128,13 +135,17 @@ namespace Revitalize.Framework.Utilities public void resizeCapacity(int Amount) { if (this.capacity + Amount < this.MaxCapacity) + { this.capacity += Amount; + this.requiresUpdate = true; + } } /// Sets the upper limity of the capacity size for the inventory. public void setMaxLimit(int amount) { this.MaxCapacity = amount; + this.requiresUpdate = true; } /// diff --git a/GeneralMods/Revitalize/Framework/Utilities/MultiplayerUtilities.cs b/GeneralMods/Revitalize/Framework/Utilities/MultiplayerUtilities.cs index de13a589..4c244035 100644 --- a/GeneralMods/Revitalize/Framework/Utilities/MultiplayerUtilities.cs +++ b/GeneralMods/Revitalize/Framework/Utilities/MultiplayerUtilities.cs @@ -28,14 +28,16 @@ namespace Revitalize.Framework.Utilities { ModCore.log("Receieve GUID Request"); string objStr = e.ReadAs (); - var v=ModCore.Serializer.DeserializeFromJSONString(objStr); + CustomObject v=(CustomObject)ModCore.Serializer.DeserializeFromJSONString(objStr); if (ModCore.CustomObjects.ContainsKey((v as CustomObject).guid) == false) { - ModCore.CustomObjects.Add((v as CustomObject).guid, (v as CustomObject)); + ModCore.CustomObjects.Add((v as CustomObject).guid, v); + //v.forceUpdate(); } else { - ModCore.CustomObjects[(v as CustomObject).guid] = (v as CustomObject); + ModCore.CustomObjects[(v as CustomObject).guid] = v; + //v.forceUpdate(); } } @@ -49,14 +51,16 @@ namespace Revitalize.Framework.Utilities { ModCore.log("Receieve GUID Request FOR TILE"); string objStr = e.ReadAs(); - var v = ModCore.Serializer.DeserializeFromJSONString(objStr); + CustomObject v =(CustomObject)ModCore.Serializer.DeserializeFromJSONString(objStr); if (ModCore.CustomObjects.ContainsKey((v as CustomObject).guid) == false) { - ModCore.CustomObjects.Add((v as CustomObject).guid, (v as CustomObject)); + ModCore.CustomObjects.Add((v as CustomObject).guid, v); + //v.forceUpdate(); } else { - ModCore.CustomObjects[(v as CustomObject).guid] = (v as CustomObject); + ModCore.CustomObjects[(v as CustomObject).guid] = v; + //v.forceUpdate(); } } } @@ -66,6 +70,7 @@ namespace Revitalize.Framework.Utilities if (ModCore.CustomObjects.ContainsKey(request)) { ModCore.log("Send guid request!"); + //ModCore.CustomObjects[request].forceUpdate(); ModCore.ModHelper.Multiplayer.SendMessage(ModCore.Serializer.ToJSONString(ModCore.CustomObjects[request]), ReceieveGUIDMessage, new string[] { Revitalize.ModCore.Manifest.UniqueID.ToString() }); } else @@ -79,6 +84,8 @@ namespace Revitalize.Framework.Utilities if (ModCore.CustomObjects.ContainsKey(request)) { ModCore.log("Send guid tile request!"); + //(ModCore.CustomObjects[request] as MultiTiledComponent).forceUpdate(); + //(ModCore.CustomObjects[request] as MultiTiledComponent).containerObject.forceUpdate(); 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/StardustCore/Animations/AnimationManager.cs b/GeneralMods/StardustCore/Animations/AnimationManager.cs index b0222568..4e53cfec 100644 --- a/GeneralMods/StardustCore/Animations/AnimationManager.cs +++ b/GeneralMods/StardustCore/Animations/AnimationManager.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; +using Newtonsoft.Json; using StardewValley; using StardustCore.UIUtilities; @@ -22,6 +23,8 @@ namespace StardustCore.Animations public string animationDataString; + [JsonIgnore] + public bool requiresUpdate; public bool IsNull => this.defaultDrawFrame == null && this.objectTexture == null; /// @@ -99,6 +102,7 @@ namespace StardustCore.Animations if (this.currentAnimation.frameCountUntilNextAnimation == 0) this.getNextAnimation(); this.currentAnimation.tickAnimationFrame(); + this.requiresUpdate = true; } catch (Exception err) { @@ -108,7 +112,7 @@ namespace StardustCore.Animations } /// Get the next animation frame in the list of animations. - public void getNextAnimation() + private void getNextAnimation() { this.currentAnimationListIndex++; if (this.currentAnimationListIndex == this.currentAnimationList.Count) //If the animation frame I'm tryting to get is 1 outside my list length, reset the list. @@ -118,6 +122,7 @@ namespace StardustCore.Animations } else { + this.requiresUpdate = true; this.playDefaultAnimation(); return; } @@ -125,6 +130,7 @@ namespace StardustCore.Animations //Get the next animation from the list and reset it's counter to the starting frame value. this.currentAnimation = this.currentAnimationList[this.currentAnimationListIndex]; this.currentAnimation.startAnimation(); + this.requiresUpdate = true; } /// Gets the animation from the dictionary of all animations available. @@ -139,6 +145,7 @@ namespace StardustCore.Animations this.currentAnimationList = dummyList; this.currentAnimation = this.currentAnimationList[StartingFrame]; this.currentAnimationName = AnimationName; + this.requiresUpdate = true; return true; } else @@ -179,6 +186,7 @@ namespace StardustCore.Animations this.currentAnimationName = AnimationName; this.currentAnimation.startAnimation(); this.loopAnimation = true; + this.requiresUpdate = true; return true; } else @@ -219,6 +227,7 @@ namespace StardustCore.Animations this.currentAnimationName = AnimationName; this.currentAnimation.startAnimation(); this.loopAnimation = false; + this.requiresUpdate = true; return true; } else @@ -245,6 +254,7 @@ namespace StardustCore.Animations this.currentAnimation = this.defaultDrawFrame; this.currentAnimationName = ""; this.currentAnimationListIndex = 0; + this.requiresUpdate = true; } /// Sets the animation manager to an on state, meaning that this animation will update on the draw frame.