From ea89e9a2d2d93786130922fc7b1467b6ce4c81be Mon Sep 17 00:00:00 2001 From: Joshua Navarro Date: Mon, 17 Dec 2018 00:02:43 -0800 Subject: [PATCH] Better working objects for syncing. Just need to fix NetCoreObject issues. --- GeneralMods/StardustCore/ModCore.cs | 42 +++++++++--- .../NetCode/Objects/NetCoreObject.cs | 9 +++ .../StardustCore/Objects/CoreObject.cs | 1 - .../Serialization/Serialization.cs | 64 +++++++++++++++++++ 4 files changed, 105 insertions(+), 11 deletions(-) diff --git a/GeneralMods/StardustCore/ModCore.cs b/GeneralMods/StardustCore/ModCore.cs index b82b9e47..88de6e5d 100644 --- a/GeneralMods/StardustCore/ModCore.cs +++ b/GeneralMods/StardustCore/ModCore.cs @@ -27,7 +27,8 @@ namespace StardustCore /* *Known issues: * Clients have a error on Serialization that says they run across unknown XML elements such as core objects. However, inventories for farmhands and modded objects still get serialized properly. - */ + * Inventories get wiped after being returned home from a festival. + */ public class ModCore : Mod @@ -87,6 +88,8 @@ namespace StardustCore TextureManagers.Add(ModManifest.UniqueID, TextureManager); StardewModdingAPI.Events.ControlEvents.KeyPressed += ControlEvents_KeyPressed; + ModHelper.Events.World.ObjectListChanged += World_ObjectListChanged; + config = ModHelper.ReadConfig(); StardewModdingAPI.Events.GameEvents.UpdateTick += GameEvents_UpdateTick; @@ -97,9 +100,21 @@ namespace StardustCore } + private void World_ObjectListChanged(object sender, StardewModdingAPI.Events.ObjectListChangedEventArgs e) + { + if (e.Added != null) + { + + } + else if (e.Removed != null) + { + + } + } + private void Player_Warped(object sender, StardewModdingAPI.Events.WarpedEventArgs e) { - + if (justWarped) return; SerializationManager.cleanUpInventory(); //SerializationManager.cleanUpWorld(); //SerializationManager.cleanUpStorageContainers(); @@ -112,8 +127,8 @@ namespace StardustCore if (Game1.player != null) { SerializationManager.cleanUpInventory(); - SerializationManager.cleanUpWorld(); - SerializationManager.cleanUpStorageContainers(); + //SerializationManager.cleanUpWorld(); + //SerializationManager.cleanUpStorageContainers(); Monitor.Log("Saved the player data after returning to title!"); } } @@ -125,8 +140,8 @@ namespace StardustCore ModMonitor.Log("Peer disconnected! Serializing custom objects"); SerializationManager.cleanUpInventory(); - SerializationManager.cleanUpWorld(); - SerializationManager.cleanUpStorageContainers(); + //SerializationManager.cleanUpWorld(); + //SerializationManager.cleanUpStorageContainers(); } @@ -145,6 +160,7 @@ namespace StardustCore } else if (e.Type == MultiplayerSupport.RestoreModObjects) { + if (Game1.eventUp) return; //Prevent item duplication. ModMonitor.Log("Restoring custom objects."); SerializationManager.restoreAllModObjects(SerializationManager.trackedObjectList); } @@ -156,11 +172,17 @@ namespace StardustCore if (SerializationManager == null) return; + + if (Game1.eventUp) + { + SerializationManager.restoreAllModObjects(SerializationManager.trackedObjectList); //Force a restore and then a serialize save to prevent deletions. + } ModMonitor.Log("Got peer context. Serialize/remove all custom objects really quick to prevent loading errors."); + SerializationManager.cleanUpInventory(); - SerializationManager.cleanUpWorld(); - SerializationManager.cleanUpStorageContainers(); + //SerializationManager.cleanUpWorld(); + //SerializationManager.cleanUpStorageContainers(); } @@ -336,8 +358,8 @@ namespace StardustCore { ModMonitor.Log("Serialize all objects on disposing!"); SerializationManager.cleanUpInventory(); - SerializationManager.cleanUpWorld(); - SerializationManager.cleanUpStorageContainers(); + //SerializationManager.cleanUpWorld(); + //SerializationManager.cleanUpStorageContainers(); } } diff --git a/GeneralMods/StardustCore/NetCode/Objects/NetCoreObject.cs b/GeneralMods/StardustCore/NetCode/Objects/NetCoreObject.cs index 624ea0fa..251e2322 100644 --- a/GeneralMods/StardustCore/NetCode/Objects/NetCoreObject.cs +++ b/GeneralMods/StardustCore/NetCode/Objects/NetCoreObject.cs @@ -49,6 +49,7 @@ namespace StardustCore.NetCode public NetBool useXML; public NetString serializationName; + //Animation Manager..... public NetAnimationManager animationManager; @@ -101,6 +102,11 @@ namespace StardustCore.NetCode drawPosition.Read(reader, version); Value.drawPosition = drawPosition.Value; + locationName = new NetString(); + locationName.Read(reader, version); + Value.locationsName = locationName.Value; + Value.thisLocation = Game1.getLocationFromName(locationName.Value); + /* animationManager = new NetAnimationManager(); animationManager.Read(reader, version); @@ -132,6 +138,9 @@ namespace StardustCore.NetCode drawPosition = new NetVector2(Value.drawPosition); drawPosition.Write(writer); + locationName = new NetString(Value.locationsName); + locationName.Write(writer); + /* if (Value.animationManager == null) { diff --git a/GeneralMods/StardustCore/Objects/CoreObject.cs b/GeneralMods/StardustCore/Objects/CoreObject.cs index 2252bb35..a92bedea 100644 --- a/GeneralMods/StardustCore/Objects/CoreObject.cs +++ b/GeneralMods/StardustCore/Objects/CoreObject.cs @@ -576,7 +576,6 @@ namespace StardustCore StardustCore.ModCore.SerializationManager.trackedObjectList.Remove(this); this.thisLocation.removeObject(this.TileLocation, false); - this.thisLocation = null; this.locationsName = ""; diff --git a/GeneralMods/StardustCore/Serialization/Serialization.cs b/GeneralMods/StardustCore/Serialization/Serialization.cs index 0ec8d373..155defbe 100644 --- a/GeneralMods/StardustCore/Serialization/Serialization.cs +++ b/GeneralMods/StardustCore/Serialization/Serialization.cs @@ -133,6 +133,8 @@ namespace StardustCore.Serialization /// public void cleanUpWorld() { + + try { ProcessDirectoryForDeletion(objectsInWorldPath); @@ -144,8 +146,70 @@ namespace StardustCore.Serialization List removalList = new List(); int countProcessed = 0; List idk = new List(); + + List allLocations = new List(); + foreach (GameLocation location in Game1.locations) + { + allLocations.Add(location); + } + foreach(Building b in Game1.getFarm().buildings) + { + allLocations.Add(b.indoors); + } + + foreach(GameLocation loc in allLocations) + { + foreach(var layer in loc.objects) + { + foreach(var pair in layer) + { + if (removalList.Contains((pair.Value as CoreObject))) continue; + try + { + if (pair.Value == null) + { + //Log.AsyncG("WTF"); + continue; + } + // Log.AsyncC(d.GetType()); + } + catch (Exception e) + { + //ModCore.ModMonitor.Log(e.ToString()); + } + string s = Convert.ToString((pair.Value.GetType())); + + if (acceptedTypes.ContainsKey(s)) + { + // Log.AsyncM("Object is of accepted type: " + s); + SerializerDataNode t; + + bool works = acceptedTypes.TryGetValue(s, out t); + if (works == true) + { + countProcessed++; + if ((pair.Value as CoreObject).useXML == false) + { + // Log.AsyncY("Saving the object"); + //Removes the object from the world and saves it to a file. + t.worldObj.Invoke((pair.Value as CoreObject)); + } + else + { + idk.Add((pair.Value as CoreObject)); + } + // Log.AsyncC("Progress on saving objects: " + countProcessed + "/" + Lists.trackedObjectList.Count); + removalList.Add((pair.Value as CoreObject)); + } + } + } + } + } + foreach (CoreObject d in trackedObjectList) { + + if (removalList.Contains(d)) continue; try { if (d == null)