Fixed a ton of serialization issues. Now objects can be safely loaded. Still have issues with clients disconnecting from the game though.

This commit is contained in:
Joshua Navarro 2018-12-16 00:54:57 -08:00
parent f7749117e5
commit 97f353743c
3 changed files with 122 additions and 33 deletions

View File

@ -40,6 +40,8 @@ namespace StardustCore
public ModConfig config; public ModConfig config;
public bool playerJustDisconnected;
public static string ContentDirectory; public static string ContentDirectory;
public override void Entry(IModHelper helper) public override void Entry(IModHelper helper)
{ {
@ -50,11 +52,7 @@ namespace StardustCore
// StardewModdingAPI.Events.GraphicsEvents.OnPostRenderGuiEvent += Metadata.GameEvents_UpdateTick; // StardewModdingAPI.Events.GraphicsEvents.OnPostRenderGuiEvent += Metadata.GameEvents_UpdateTick;
//StardewModdingAPI.Events.ControlEvents.MouseChanged += ControlEvents_MouseChanged; //StardewModdingAPI.Events.ControlEvents.MouseChanged += ControlEvents_MouseChanged;
string invPath = Path.Combine(ModCore.ModHelper.DirectoryPath, "PlayerData", Game1.player.Name, "PlayerInventory");
string worldPath = Path.Combine(ModCore.ModHelper.DirectoryPath, Game1.player.Name, "ObjectsInWorld"); ;
string trashPath = Path.Combine(ModCore.ModHelper.DirectoryPath, "ModTrashFolder");
string chestPath = Path.Combine(ModCore.ModHelper.DirectoryPath, "StorageContainers");
SerializationManager = new SerializationManager(invPath, trashPath, worldPath,chestPath);
StardewModdingAPI.Events.SaveEvents.AfterSave += SaveEvents_AfterSave; StardewModdingAPI.Events.SaveEvents.AfterSave += SaveEvents_AfterSave;
StardewModdingAPI.Events.SaveEvents.BeforeSave += SaveEvents_BeforeSave; StardewModdingAPI.Events.SaveEvents.BeforeSave += SaveEvents_BeforeSave;
@ -64,13 +62,19 @@ namespace StardustCore
ModHelper.Events.Multiplayer.PeerContextReceived += Multiplayer_PeerContextReceived; ModHelper.Events.Multiplayer.PeerContextReceived += Multiplayer_PeerContextReceived;
ModHelper.Events.Multiplayer.ModMessageReceived += Multiplayer_ModMessageReceived; ModHelper.Events.Multiplayer.ModMessageReceived += Multiplayer_ModMessageReceived;
ModHelper.Events.Multiplayer.PeerDisconnected += Multiplayer_PeerDisconnected;
StardewModdingAPI.Events.TimeEvents.AfterDayStarted += TimeEvents_AfterDayStarted;
playerJustDisconnected = false;
IlluminateFramework.Colors.initializeColors(); IlluminateFramework.Colors.initializeColors();
ContentDirectory = "Content"; ContentDirectory = "Content";
if (!Directory.Exists(ContentDirectory)) Directory.CreateDirectory(Path.Combine(ModHelper.DirectoryPath, "Content")); if (!Directory.Exists(ContentDirectory)) Directory.CreateDirectory(Path.Combine(ModHelper.DirectoryPath, "Content"));
SpriteFonts.initialize(); SpriteFonts.initialize();
SerializationManager.initializeDefaultSuportedTypes();
TextureManagers = new Dictionary<string, TextureManager>(); TextureManagers = new Dictionary<string, TextureManager>();
TextureManager = new TextureManager(); TextureManager = new TextureManager();
TextureManager.addTexture("Test1", new Texture2DExtended(ModCore.ModHelper, Manifest,Path.Combine("Content", "Graphics", "MultiTest", "Test1.png"))); TextureManager.addTexture("Test1", new Texture2DExtended(ModCore.ModHelper, Manifest,Path.Combine("Content", "Graphics", "MultiTest", "Test1.png")));
@ -86,6 +90,37 @@ namespace StardustCore
} }
private void TimeEvents_AfterDayStarted(object sender, EventArgs e)
{
return;
if (lastMenuType == null) return;
if (lastMenuType == typeof(StardewValley.Menus.TitleMenu)) return;
if(lastMenuType== typeof(StardewValley.Menus.SaveGameMenu) || lastMenuType== typeof(StardewValley.Menus.ShippingMenu))
{
ModMonitor.Log("Start a new day clean!");
SerializationManager.cleanUpInventory();
SerializationManager.cleanUpWorld();
SerializationManager.cleanUpStorageContainers();
List<long> playerIds = new List<long>();
foreach (Farmer f in Game1.getAllFarmers())
{
if (f == Game1.player) continue;
playerIds.Add(f.uniqueMultiplayerID);
}
ModHelper.Multiplayer.SendMessage<string>(MultiplayerSupport.CleanUpModObjects, MultiplayerSupport.CleanUpModObjects, new string[] { ModManifest.UniqueID }, playerIds.ToArray());
}
}
private void Multiplayer_PeerDisconnected(object sender, StardewModdingAPI.Events.PeerDisconnectedEventArgs e)
{
this.playerJustDisconnected = true;
SerializationManager.cleanUpInventory();
SerializationManager.cleanUpWorld();
SerializationManager.cleanUpStorageContainers();
}
private void Multiplayer_ModMessageReceived(object sender, StardewModdingAPI.Events.ModMessageReceivedEventArgs e) private void Multiplayer_ModMessageReceived(object sender, StardewModdingAPI.Events.ModMessageReceivedEventArgs e)
{ {
if (e.FromModID == this.ModManifest.UniqueID) if (e.FromModID == this.ModManifest.UniqueID)
@ -106,14 +141,14 @@ namespace StardustCore
private void Multiplayer_PeerContextReceived(object sender, StardewModdingAPI.Events.PeerContextReceivedEventArgs e) private void Multiplayer_PeerContextReceived(object sender, StardewModdingAPI.Events.PeerContextReceivedEventArgs e)
{ {
//ModMonitor.Log("TRY TO CLEAN UP THE MESS!!!!"); //ModMonitor.Log("TRY TO CLEAN UP THE MESS!!!!");
if (SerializationManager == null) return;
SerializationManager.cleanUpInventory(); SerializationManager.cleanUpInventory();
SerializationManager.cleanUpWorld(); SerializationManager.cleanUpWorld();
SerializationManager.cleanUpStorageContainers(); SerializationManager.cleanUpStorageContainers();
} }
private void MenuEvents_MenuClosed(object sender, StardewModdingAPI.Events.EventArgsClickableMenuClosed e) private void MenuEvents_MenuClosed(object sender, StardewModdingAPI.Events.EventArgsClickableMenuClosed e)
{
if (Game1.IsMasterGame == false && config.enableMultiplayerHack)
{ {
if (this.lastMenuType == null) if (this.lastMenuType == null)
{ {
@ -121,25 +156,47 @@ namespace StardustCore
} }
else else
{ {
if (lastMenuType == typeof(StardewValley.Menus.SaveGameMenu) ||lastMenuType==typeof(StardewValley.Menus.ReadyCheckDialog)) /*
if (lastMenuType == typeof(StardewValley.Menus.SaveGameMenu) ||lastMenuType==typeof(StardewValley.Menus.ShippingMenu))
{ {
SerializationManager.restoreAllModObjects(SerializationManager.trackedObjectList); SerializationManager.restoreAllModObjects(SerializationManager.trackedObjectList);
List<long> playerIds = new List<long>();
foreach (Farmer f in Game1.getAllFarmers())
{
if (f == Game1.player) continue;
playerIds.Add(f.uniqueMultiplayerID);
} }
ModHelper.Multiplayer.SendMessage<string>(MultiplayerSupport.RestoreModObjects, MultiplayerSupport.RestoreModObjects, new string[] { ModManifest.UniqueID }, playerIds.ToArray());
}
*/
//Only fires in multiplayer since ReadyCheckDialogue only appears in multiplayer
if (lastMenuType == typeof(StardewValley.Menus.ReadyCheckDialog) && Game1.player.canMove==false && Game1.player.isInBed)
{
ModMonitor.Log("Sleepy Time!");
SerializationManager.cleanUpInventory();
SerializationManager.cleanUpWorld();
SerializationManager.cleanUpStorageContainers();
/*
List<long> playerIds = new List<long>();
foreach (Farmer f in Game1.getAllFarmers())
{
if (f == null) continue;
if (f == Game1.player) continue;
playerIds.Add(f.uniqueMultiplayerID);
}
*/
//ModHelper.Multiplayer.SendMessage<string>(MultiplayerSupport.CleanUpModObjects, MultiplayerSupport.CleanUpModObjects, new string[] { ModManifest.UniqueID }, playerIds.ToArray());
} }
} }
} }
private void MenuEvents_MenuChanged(object sender, StardewModdingAPI.Events.EventArgsClickableMenuChanged e) private void MenuEvents_MenuChanged(object sender, StardewModdingAPI.Events.EventArgsClickableMenuChanged e)
{ {
if (Game1.IsMasterGame == false && config.enableMultiplayerHack)
{
if (Game1.activeClickableMenu.GetType() == typeof(StardewValley.Menus.ReadyCheckDialog))
{
SerializationManager.cleanUpInventory();
SerializationManager.cleanUpWorld();
SerializationManager.cleanUpStorageContainers();
}
}
lastMenuType = Game1.activeClickableMenu.GetType(); lastMenuType = Game1.activeClickableMenu.GetType();
} }
@ -179,11 +236,10 @@ namespace StardustCore
private void GameEvents_UpdateTick(object sender, EventArgs e) private void GameEvents_UpdateTick(object sender, EventArgs e)
{ {
if (playerJustDisconnected)
if (Game1.client !=null && serverHack == false && config.enableMultiplayerHack)
{ {
playerJustDisconnected = false;
SerializationManager.restoreAllModObjects(SerializationManager.trackedObjectList);
} }
} }
@ -198,10 +254,23 @@ namespace StardustCore
private void SaveEvents_AfterLoad(object sender, EventArgs e) private void SaveEvents_AfterLoad(object sender, EventArgs e)
{ {
string invPath = Path.Combine(ModCore.ModHelper.DirectoryPath, "PlayerData", Game1.player.Name, "PlayerInventory");
string worldPath = Path.Combine(ModCore.ModHelper.DirectoryPath, Game1.player.Name, "ObjectsInWorld"); ;
string trashPath = Path.Combine(ModCore.ModHelper.DirectoryPath, "ModTrashFolder");
string chestPath = Path.Combine(ModCore.ModHelper.DirectoryPath, "StorageContainers");
SerializationManager = new SerializationManager(invPath, trashPath, worldPath, chestPath);
SerializationManager.initializeDefaultSuportedTypes();
SerializationManager.restoreAllModObjects(SerializationManager.trackedObjectList); SerializationManager.restoreAllModObjects(SerializationManager.trackedObjectList);
ModHelper.Multiplayer.SendMessage<string>(MultiplayerSupport.RestoreModObjects, MultiplayerSupport.RestoreModObjects,new string[] { ModManifest.UniqueID }, null); List<long> playerIds = new List<long>();
foreach (Farmer f in Game1.getAllFarmers())
{
if (f == Game1.player) continue;
playerIds.Add(f.uniqueMultiplayerID);
}
ModHelper.Multiplayer.SendMessage<string>(MultiplayerSupport.RestoreModObjects, MultiplayerSupport.RestoreModObjects, new string[] { ModManifest.UniqueID },playerIds.ToArray());
/* /*
List<KeyValuePair<Vector2, MultiTileComponent>> objs = new List<KeyValuePair<Vector2, MultiTileComponent>>(); List<KeyValuePair<Vector2, MultiTileComponent>> objs = new List<KeyValuePair<Vector2, MultiTileComponent>>();
@ -231,17 +300,24 @@ namespace StardustCore
private void SaveEvents_AfterSave(object sender, EventArgs e) private void SaveEvents_AfterSave(object sender, EventArgs e)
{ {
SerializationManager.restoreAllModObjects(SerializationManager.trackedObjectList); SerializationManager.restoreAllModObjects(SerializationManager.trackedObjectList);
} }
private void SaveEvents_BeforeSave(object sender, EventArgs e) private void SaveEvents_BeforeSave(object sender, EventArgs e)
{
//Call the serialization if alone since the ReadyCheckDialogue menu never shows with just 1 player online.
if (Game1.IsMultiplayer == false || (Game1.IsMultiplayer && Game1.getOnlineFarmers().Count==1))
{ {
SerializationManager.cleanUpInventory(); SerializationManager.cleanUpInventory();
SerializationManager.cleanUpWorld(); SerializationManager.cleanUpWorld();
SerializationManager.cleanUpStorageContainers(); SerializationManager.cleanUpStorageContainers();
} }
}
private void ControlEvents_MouseChanged(object sender, StardewModdingAPI.Events.EventArgsMouseStateChanged e) private void ControlEvents_MouseChanged(object sender, StardewModdingAPI.Events.EventArgsMouseStateChanged e)
{ {
//??? //???

View File

@ -708,6 +708,12 @@ namespace StardustCore
return this.boundingBox.Height / Game1.tileSize; return this.boundingBox.Height / Game1.tileSize;
} }
public override bool canStackWith(Item other)
{
if (other.Name == this.name && (other as CoreObject).textureName == this.textureName) return true;
else return false;
}
public override bool placementAction(GameLocation location, int x, int y, StardewValley.Farmer who = null) public override bool placementAction(GameLocation location, int x, int y, StardewValley.Farmer who = null)
{ {

View File

@ -434,9 +434,16 @@ namespace StardustCore.Serialization
return; return;
} }
else else
{
try
{ {
(cObj as CoreObject).thisLocation.objects.Add((cObj as CoreObject).TileLocation, (StardewValley.Object)cObj); (cObj as CoreObject).thisLocation.objects.Add((cObj as CoreObject).TileLocation, (StardewValley.Object)cObj);
thingsToAddBackIn.Add(cObj as CoreObject); thingsToAddBackIn.Add(cObj as CoreObject);
}
catch(Exception err)
{
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);
} }
} }