diff --git a/GeneralMods/StardustCore/ModCore.cs b/GeneralMods/StardustCore/ModCore.cs index 35e34ec9..b82b9e47 100644 --- a/GeneralMods/StardustCore/ModCore.cs +++ b/GeneralMods/StardustCore/ModCore.cs @@ -19,6 +19,8 @@ using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; +using System.Xml; +using System.Xml.Serialization; namespace StardustCore { @@ -44,6 +46,8 @@ namespace StardustCore public bool playerJustDisconnected; + public bool justWarped; + public static string ContentDirectory; public override void Entry(IModHelper helper) { @@ -87,10 +91,22 @@ namespace StardustCore StardewModdingAPI.Events.GameEvents.UpdateTick += GameEvents_UpdateTick; + Helper.Events.Player.Warped += Player_Warped; + ModHelper.Events.GameLoop.ReturnedToTitle += GameLoop_ReturnedToTitle; } + private void Player_Warped(object sender, StardewModdingAPI.Events.WarpedEventArgs e) + { + + SerializationManager.cleanUpInventory(); + //SerializationManager.cleanUpWorld(); + //SerializationManager.cleanUpStorageContainers(); + justWarped = true; + } + + private void GameLoop_ReturnedToTitle(object sender, StardewModdingAPI.Events.ReturnedToTitleEventArgs e) { if (Game1.player != null) @@ -247,6 +263,12 @@ namespace StardustCore ModMonitor.Log("Restore objects after peer disconnect!"); SerializationManager.restoreAllModObjects(SerializationManager.trackedObjectList); } + else if (justWarped && Game1.eventUp==false && Game1.activeClickableMenu==null) + { + justWarped = false; + ModMonitor.Log("Restore objects after player warping!"); + SerializationManager.restoreAllModObjects(SerializationManager.trackedObjectList,true); + } } private void ControlEvents_KeyPressed(object sender, StardewModdingAPI.Events.EventArgsKeyPressed e) @@ -261,10 +283,12 @@ namespace StardustCore { Game1.game1.Disposed += Game1_Disposed; - string invPath = Path.Combine(ModCore.ModHelper.DirectoryPath, "PlayerData", Game1.player.Name+"_"+Game1.player.uniqueMultiplayerID, "PlayerInventory"); - string worldPath = Path.Combine(ModCore.ModHelper.DirectoryPath, Game1.player.Name+"_"+Game1.player.uniqueMultiplayerID, "ObjectsInWorld"); ; - string trashPath = Path.Combine(ModCore.ModHelper.DirectoryPath, "ModTrashFolder"); - string chestPath = Path.Combine(ModCore.ModHelper.DirectoryPath, "StorageContainers"); + string basePath=Path.Combine( ModCore.ModHelper.DirectoryPath, "PlayerData", Game1.player.Name + "_" + Game1.player.uniqueMultiplayerID); + + string invPath = Path.Combine(basePath,"PlayerInventory"); + string worldPath = Path.Combine(basePath, "ObjectsInWorld"); + string trashPath = Path.Combine(basePath,"ModTrashFolder"); + string chestPath = Path.Combine(basePath, "StorageContainers"); SerializationManager = new SerializationManager(invPath, trashPath, worldPath, chestPath); SerializationManager.initializeDefaultSuportedTypes(); diff --git a/GeneralMods/StardustCore/Objects/CoreObject.cs b/GeneralMods/StardustCore/Objects/CoreObject.cs index 0e6173a2..70c3f450 100644 --- a/GeneralMods/StardustCore/Objects/CoreObject.cs +++ b/GeneralMods/StardustCore/Objects/CoreObject.cs @@ -281,6 +281,7 @@ namespace StardustCore { performRemoveAction(this.TileLocation, this.thisLocation); who.addItemToInventory(this); + return true; } @@ -571,6 +572,8 @@ namespace StardustCore { } + + this.thisLocation = null; base.performRemoveAction(tileLocation, environment); } @@ -1254,6 +1257,13 @@ namespace StardustCore /// public static void Serialize(Item I) { + + if((I as CoreObject).thisLocation != null) + { + SerializeToWorldPath(I); + return; + } + String savePath = ModCore.SerializationManager.playerInventoryPath; String fileName = I.Name + ".json"; String resultPath = Path.Combine(savePath, fileName); @@ -1271,6 +1281,26 @@ namespace StardustCore //StardustCore.ModCore.ModHelper.WriteJsonFile(resultPath, (CoreObject)I); } + public static void SerializeToWorldPath(Item I) + { + String savePath = ModCore.SerializationManager.objectsInWorldPath; + String fileName = I.Name + ".json"; + String resultPath = Path.Combine(savePath, fileName); + int count = 0; + while (File.Exists(resultPath)) + { + resultPath = Serialization.SerializationManager.getValidSavePathIfDuplicatesExist(I, savePath, count); + count++; + } + JsonSerializerSettings settings = new JsonSerializerSettings(); + settings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; + (I as CoreObject).textureName = (I as CoreObject).TextureSheet.Name; + string json = JsonConvert.SerializeObject(I, Formatting.Indented, settings); + System.IO.File.WriteAllText(resultPath, json); + } + + + /// /// Serializes the said item to a chest. /// diff --git a/GeneralMods/StardustCore/Serialization/Serialization.cs b/GeneralMods/StardustCore/Serialization/Serialization.cs index 2f8139e4..0ec8d373 100644 --- a/GeneralMods/StardustCore/Serialization/Serialization.cs +++ b/GeneralMods/StardustCore/Serialization/Serialization.cs @@ -318,10 +318,11 @@ namespace StardustCore.Serialization /// Reloads all modded objects added by this mod back to the game in proper locations. /// /// - public void restoreAllModObjects(List thingsToAddBackIn) + public void restoreAllModObjects(List thingsToAddBackIn, bool onlyInventory=false) { + processDirectoryForDeserialization(playerInventoryPath,thingsToAddBackIn); - + if (onlyInventory) return; // Log.AsyncG("Done deserializing player inventory."); try @@ -472,7 +473,7 @@ namespace StardustCore.Serialization } catch(Exception err) { - throw new Exception(err.ToString()); + //throw new Exception(err.ToString()); return; } //Util.placementAction(cObj, cObj.thisLocation,(int)cObj.tileLocation.X,(int) cObj.tileLocation.Y,null,false);