Fixed syncronization issues with events and warping.

This commit is contained in:
Joshua Navarro 2018-12-16 21:27:07 -08:00
parent c393752f05
commit e503fb210c
3 changed files with 62 additions and 7 deletions

View File

@ -19,6 +19,8 @@ using System.Linq;
using System.Reflection; using System.Reflection;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization;
namespace StardustCore namespace StardustCore
{ {
@ -44,6 +46,8 @@ namespace StardustCore
public bool playerJustDisconnected; public bool playerJustDisconnected;
public bool justWarped;
public static string ContentDirectory; public static string ContentDirectory;
public override void Entry(IModHelper helper) public override void Entry(IModHelper helper)
{ {
@ -87,10 +91,22 @@ namespace StardustCore
StardewModdingAPI.Events.GameEvents.UpdateTick += GameEvents_UpdateTick; StardewModdingAPI.Events.GameEvents.UpdateTick += GameEvents_UpdateTick;
Helper.Events.Player.Warped += Player_Warped;
ModHelper.Events.GameLoop.ReturnedToTitle += GameLoop_ReturnedToTitle; 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) private void GameLoop_ReturnedToTitle(object sender, StardewModdingAPI.Events.ReturnedToTitleEventArgs e)
{ {
if (Game1.player != null) if (Game1.player != null)
@ -247,6 +263,12 @@ namespace StardustCore
ModMonitor.Log("Restore objects after peer disconnect!"); ModMonitor.Log("Restore objects after peer disconnect!");
SerializationManager.restoreAllModObjects(SerializationManager.trackedObjectList); 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) private void ControlEvents_KeyPressed(object sender, StardewModdingAPI.Events.EventArgsKeyPressed e)
@ -261,10 +283,12 @@ namespace StardustCore
{ {
Game1.game1.Disposed += Game1_Disposed; Game1.game1.Disposed += Game1_Disposed;
string invPath = Path.Combine(ModCore.ModHelper.DirectoryPath, "PlayerData", Game1.player.Name+"_"+Game1.player.uniqueMultiplayerID, "PlayerInventory"); string basePath=Path.Combine( ModCore.ModHelper.DirectoryPath, "PlayerData", Game1.player.Name + "_" + Game1.player.uniqueMultiplayerID);
string worldPath = Path.Combine(ModCore.ModHelper.DirectoryPath, Game1.player.Name+"_"+Game1.player.uniqueMultiplayerID, "ObjectsInWorld"); ;
string trashPath = Path.Combine(ModCore.ModHelper.DirectoryPath, "ModTrashFolder"); string invPath = Path.Combine(basePath,"PlayerInventory");
string chestPath = Path.Combine(ModCore.ModHelper.DirectoryPath, "StorageContainers"); 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 = new SerializationManager(invPath, trashPath, worldPath, chestPath);
SerializationManager.initializeDefaultSuportedTypes(); SerializationManager.initializeDefaultSuportedTypes();

View File

@ -281,6 +281,7 @@ namespace StardustCore
{ {
performRemoveAction(this.TileLocation, this.thisLocation); performRemoveAction(this.TileLocation, this.thisLocation);
who.addItemToInventory(this); who.addItemToInventory(this);
return true; return true;
} }
@ -571,6 +572,8 @@ namespace StardustCore
{ {
} }
this.thisLocation = null;
base.performRemoveAction(tileLocation, environment); base.performRemoveAction(tileLocation, environment);
} }
@ -1254,6 +1257,13 @@ namespace StardustCore
/// <param name="I"></param> /// <param name="I"></param>
public static void Serialize(Item I) public static void Serialize(Item I)
{ {
if((I as CoreObject).thisLocation != null)
{
SerializeToWorldPath(I);
return;
}
String savePath = ModCore.SerializationManager.playerInventoryPath; String savePath = ModCore.SerializationManager.playerInventoryPath;
String fileName = I.Name + ".json"; String fileName = I.Name + ".json";
String resultPath = Path.Combine(savePath, fileName); String resultPath = Path.Combine(savePath, fileName);
@ -1271,6 +1281,26 @@ namespace StardustCore
//StardustCore.ModCore.ModHelper.WriteJsonFile<CoreObject>(resultPath, (CoreObject)I); //StardustCore.ModCore.ModHelper.WriteJsonFile<CoreObject>(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);
}
/// <summary> /// <summary>
/// Serializes the said item to a chest. /// Serializes the said item to a chest.
/// </summary> /// </summary>

View File

@ -318,10 +318,11 @@ namespace StardustCore.Serialization
/// Reloads all modded objects added by this mod back to the game in proper locations. /// Reloads all modded objects added by this mod back to the game in proper locations.
/// </summary> /// </summary>
/// <param name="thingsToAddBackIn"></param> /// <param name="thingsToAddBackIn"></param>
public void restoreAllModObjects(List<IItemSerializeable> thingsToAddBackIn) public void restoreAllModObjects(List<IItemSerializeable> thingsToAddBackIn, bool onlyInventory=false)
{ {
processDirectoryForDeserialization(playerInventoryPath,thingsToAddBackIn);
processDirectoryForDeserialization(playerInventoryPath,thingsToAddBackIn);
if (onlyInventory) return;
// Log.AsyncG("Done deserializing player inventory."); // Log.AsyncG("Done deserializing player inventory.");
try try
@ -472,7 +473,7 @@ namespace StardustCore.Serialization
} }
catch(Exception err) catch(Exception err)
{ {
throw new Exception(err.ToString()); //throw new Exception(err.ToString());
return; return;
} }
//Util.placementAction(cObj, cObj.thisLocation,(int)cObj.tileLocation.X,(int) cObj.tileLocation.Y,null,false); //Util.placementAction(cObj, cObj.thisLocation,(int)cObj.tileLocation.X,(int) cObj.tileLocation.Y,null,false);