Got multi tiled objects to sync its components so that it displays properly in the world and syncs 100 times better.

This commit is contained in:
JoshuaNavarro 2019-08-28 00:31:14 -07:00
parent a752503964
commit acca21e511
6 changed files with 180 additions and 33 deletions

View File

@ -155,8 +155,16 @@ namespace Revitalize.Framework.Objects
public Guid guid;
[JsonIgnore]
/// <summary>The animation manager.</summary>
public AnimationManager animationManager => this.info.animationManager;
public AnimationManager animationManager
{
get
{
this.updateInfo();
return this.info.animationManager;
}
}
/// <summary>The display texture for this object.</summary>
[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<CustomObjectData>(pytkData);
Guid oldGuid = this.guid;
this.guid = Guid.Parse(guidString);
if (ModCore.CustomObjects.ContainsKey(this.guid))

View File

@ -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<CustomObjectData>(pyTKData);
if (string.IsNullOrEmpty(offsetVec)) return;
if (string.IsNullOrEmpty(containerObject)) return;
this.offsetKey = ModCore.Serializer.DeserializeFromJSONString<Vector2>(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();
}
}
}

View File

@ -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<Vector2, StardewValley.Object> 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<CustomObjectData>(pytkData);
Guid oldGuid = this.guid;
this.guid = Guid.Parse(guidString);
//this.childrenGuids = ModCore.Serializer.DeserializeFromJSONString<Dictionary<Vector2, Guid>>(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<Vector2, Guid> dict = ModCore.Serializer.DeserializeFromJSONString<Dictionary<Vector2, Guid>>(dictStr);
if (dict == null) return;
if (dict.Count == 0) return;
else
{
//ModCore.log("Children dicts are updated!");
this.childrenGuids = dict;
foreach(KeyValuePair<Vector2,Guid> 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<Vector2, StardewValley.Object>();
this.childrenGuids = new Dictionary<Vector2, Guid>();
@ -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<Vector2, StardewValley.Object>();
this.childrenGuids = new Dictionary<Vector2, Guid>();
@ -97,7 +146,7 @@ namespace Revitalize.Framework.Objects
}
public MultiTiledObject(CustomObjectData PyTKData, BasicItemInformation info, Vector2 TileLocation, Dictionary<Vector2, MultiTiledComponent> ObjectsList)
: base(PyTKData,info, TileLocation)
: base(PyTKData, info, TileLocation)
{
this.objects = new Dictionary<Vector2, StardewValley.Object>();
this.childrenGuids = new Dictionary<Vector2, Guid>();
@ -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<string, string> 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<Vector2, Guid> 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);
}
}
}
}

View File

@ -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<string>(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<string>(ModCore.Serializer.ToJSONString( (ModCore.CustomObjects[request] as MultiTiledComponent).containerObject), ReceieveGUIDMessage_Tile , new string[] { Revitalize.ModCore.Manifest.UniqueID.ToString() });
}
else

View File

@ -144,13 +144,20 @@ namespace Revitalize.Framework.Utilities
public void restoreModObjects()
{
//Replace all items in the world.
List<CustomObject> objsToRestore = new List<CustomObject>();
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())
{

View File

@ -208,6 +208,7 @@ namespace Revitalize
this.initailizeComponents();
Serializer = new Serializer();
playerInfo = new PlayerInfo();
CustomObjects = new Dictionary<Guid, CustomObject>();
//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<Guid, CustomObject>();
//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());