Updating the serializing features to save/load everything correctly when loading objects back into the world.

This commit is contained in:
Joshua Navarro 2017-02-06 12:32:16 -08:00
parent 3148eef264
commit 7e676f8862
11 changed files with 84 additions and 48 deletions

View File

@ -58,7 +58,7 @@ namespace Revitalize
StardewModdingAPI.Events.GameEvents.GameLoaded += GameEvents_GameLoaded;
StardewModdingAPI.Events.GameEvents.OneSecondTick += MapWipe;
StardewModdingAPI.Events.TimeEvents.DayOfMonthChanged += Util.ResetAllDailyBooleans;
StardewModdingAPI.Events.SaveEvents.AfterLoad += SaveEvents_AfterLoad;
StardewModdingAPI.Events.SaveEvents.BeforeSave += SaveEvents_BeforeSave;
StardewModdingAPI.Events.SaveEvents.AfterSave += SaveEvents_AfterSave;
@ -72,10 +72,15 @@ namespace Revitalize
newLoc = new List<GameLoc>();
}
private void SaveEvents_AfterLoad(object sender, EventArgs e)
{
}
private void SaveEvents_AfterSave(object sender, EventArgs e)
{
Serialize.createDirectories();
Serialize.restoreInventory();
Serialize.restoreAllModObjects();
}
private void SaveEvents_BeforeSave(object sender, EventArgs e)
@ -85,7 +90,7 @@ namespace Revitalize
{
Log.AsyncC(v.name);
}
Serialize.cleanUpWorld();
Serialize.cleanUpWorld(); //grabs all of the items that im tracking and serializes them
}
private void GameEvents_UpdateTick(object sender, EventArgs e)

View File

@ -226,8 +226,8 @@ namespace Revitalize.Objects
if (Game1.player.currentLocation is FarmHouse)
{
// Game1.showRedMessage("Why2?");
this.heldObject = new Decoration(parentSheetIndex, Vector2.Zero);
Game1.player.addItemByMenuIfNecessary((Item)this.heldObject);
// this.heldObject = new Decoration(parentSheetIndex, Vector2.Zero);
Util.addItemToInventoryAndCleanTrackedList(this);
this.flaggedForPickUp = true;
this.thisLocation = null;
return true;
@ -242,8 +242,8 @@ namespace Revitalize.Objects
this.heldObject = new TV(parentSheetIndex, Vector2.Zero);
}
else {
this.heldObject = new Decoration(parentSheetIndex, Vector2.Zero);
Game1.player.addItemByMenuIfNecessary((Item)this.heldObject);
// this.heldObject = new Decoration(parentSheetIndex, Vector2.Zero);
Util.addItemToInventoryAndCleanTrackedList(this);
// this.heldObject.performRemoveAction(this.tileLocation, who.currentLocation);
// this.heldObject = null;
Game1.playSound("coin");
@ -258,7 +258,7 @@ namespace Revitalize.Objects
// Game1.showRedMessage("Why3?");
this.heldObject.performRemoveAction(this.tileLocation, who.currentLocation);
this.heldObject = null;
Game1.player.addItemByMenuIfNecessary((Item)this);
Util.addItemToInventoryAndCleanTrackedList(this);
Game1.playSound("coin");
this.thisLocation = null;
return true;

View File

@ -197,7 +197,7 @@ namespace Revitalize.Objects
if (Game1.player.currentLocation is FarmHouse)
{
//
Game1.player.addItemByMenuIfNecessary(this);
Util.addItemToInventoryAndCleanTrackedList(this);
removeLights(this.thisLocation);
this.lightsOn = false;
Game1.playSound("coin");
@ -209,7 +209,7 @@ namespace Revitalize.Objects
{
// return true;
// this.heldObject = new ExtraSeeds(parentSheetIndex, Vector2.Zero, this.lightColor, this.inventoryMaxSize);
Game1.player.addItemByMenuIfNecessary(this);
Util.addItemToInventoryAndCleanTrackedList(this);
removeLights(this.thisLocation);
this.lightsOn = false;
Game1.playSound("coin");
@ -222,7 +222,7 @@ namespace Revitalize.Objects
{
// Game1.showRedMessage("Why3?");
// if(this.heldObject!=null) Game1.player.addItemByMenuIfNecessary((Item)this.heldObject);
Util.addItemToInventoryElseDrop(this);
Util.addItemToInventoryAndCleanTrackedList(this);
this.heldObject.performRemoveAction(this.tileLocation, who.currentLocation);
this.heldObject = null;
Game1.playSound("coin");

View File

@ -194,7 +194,7 @@ namespace Revitalize.Objects
if (Game1.player.currentLocation is FarmHouse)
{
//
Game1.player.addItemByMenuIfNecessary(this);
Util.addItemToInventoryAndCleanTrackedList(this);
removeLights(this.thisLocation);
this.lightsOn = false;
Game1.playSound("coin");
@ -206,7 +206,7 @@ namespace Revitalize.Objects
{
// return true;
// this.heldObject = new GiftPackage(parentSheetIndex, Vector2.Zero, this.lightColor, this.inventoryMaxSize);
Game1.player.addItemByMenuIfNecessary(this);
Util.addItemToInventoryAndCleanTrackedList(this);
removeLights(this.thisLocation);
this.lightsOn = false;
Game1.playSound("coin");
@ -219,7 +219,7 @@ namespace Revitalize.Objects
{
// Game1.showRedMessage("Why3?");
// if(this.heldObject!=null) Game1.player.addItemByMenuIfNecessary((Item)this.heldObject);
Util.addItemToInventoryElseDrop(this);
Util.addItemToInventoryAndCleanTrackedList(this);
this.heldObject.performRemoveAction(this.tileLocation, who.currentLocation);
this.heldObject = null;
Game1.playSound("coin");

View File

@ -188,7 +188,7 @@ namespace Revitalize.Objects
if (Game1.player.currentLocation is FarmHouse)
{
//
Game1.player.addItemByMenuIfNecessary(this);
Util.addItemToInventoryAndCleanTrackedList(this);
removeLights(this.thisLocation);
this.lightsOn = false;
Game1.playSound("coin");
@ -200,7 +200,7 @@ namespace Revitalize.Objects
{
// return true;
// this.heldObject = new Light(parentSheetIndex, Vector2.Zero, this.lightColor, this.inventoryMaxSize);
Game1.player.addItemByMenuIfNecessary(this);
Util.addItemToInventoryAndCleanTrackedList(this);
removeLights(this.thisLocation);
this.lightsOn = false;
Game1.playSound("coin");
@ -213,7 +213,7 @@ namespace Revitalize.Objects
{
// Game1.showRedMessage("Why3?");
// if(this.heldObject!=null) Game1.player.addItemByMenuIfNecessary((Item)this.heldObject);
Util.addItemToInventoryElseDrop(this);
Util.addItemToInventoryAndCleanTrackedList(this);
this.heldObject.performRemoveAction(this.tileLocation, who.currentLocation);
this.heldObject = null;
Game1.playSound("coin");

View File

@ -271,7 +271,7 @@ namespace Revitalize.Objects.Machines
if (this.heldObject != null) Util.addItemToInventoryElseDrop(this.heldObject.getOne());
this.heldObject = new Machine(parentSheetIndex, Vector2.Zero, this.inventoryMaxSize);
Util.addItemToInventoryElseDrop(this.heldObject.getOne());
Util.addItemToInventoryAndCleanTrackedList(this);
this.heldObject = null;
this.flaggedForPickUp = true;
this.thisLocation = null;
@ -291,7 +291,7 @@ namespace Revitalize.Objects.Machines
// Util.addItemToInventoryElseDrop(this.heldObject);
var obj = new Machine(parentSheetIndex, Vector2.Zero, this.inventoryMaxSize);
Util.addItemToInventoryElseDrop(obj);
Util.addItemToInventoryAndCleanTrackedList(this);
// this.spillInventoryEverywhere();
if (this.heldObject != null) this.heldObject.performRemoveAction(this.tileLocation, who.currentLocation);
@ -309,7 +309,7 @@ namespace Revitalize.Objects.Machines
// if(this.heldObject!=null) Game1.player.addItemByMenuIfNecessary((Item)this.heldObject);
// this.spillInventoryEverywhere();
var obj = new Machine(parentSheetIndex, Vector2.Zero, this.inventoryMaxSize);
Util.addItemToInventoryElseDrop(obj);
Util.addItemToInventoryAndCleanTrackedList(this);
if (this.heldObject != null) this.heldObject.performRemoveAction(this.tileLocation, who.currentLocation);
this.heldObject = null;
Game1.playSound("coin");

View File

@ -338,7 +338,7 @@ namespace Revitalize.Objects.Machines
inventory.Clear();
// if (this.output != null) Util.addItemToInventoryElseDrop(this.output.getOne());
//this.output = new Quarry(parentSheetIndex, Vector2.Zero, this.inventoryMaxSize);
Util.addItemToInventoryElseDrop(this);
Util.addItemToInventoryAndCleanTrackedList(this);
this.output = null;
this.flaggedForPickUp = true;
this.thisLocation = null;
@ -356,7 +356,7 @@ namespace Revitalize.Objects.Machines
//if (this.output != null) Util.addItemToInventoryElseDrop(this.output.getOne());
this.output = this;
Util.addItemToInventoryElseDrop(this);
Util.addItemToInventoryAndCleanTrackedList(this);
this.output = null;
this.flaggedForPickUp = true;
this.thisLocation = null;
@ -376,7 +376,7 @@ namespace Revitalize.Objects.Machines
// Util.addItemToInventoryElseDrop(this.output);
var obj = this;
Util.addItemToInventoryElseDrop(obj);
Util.addItemToInventoryAndCleanTrackedList(this);
// this.showUI();
// if (this.output != null) this.output.performRemoveAction(this.tileLocation, who.currentLocation);
@ -394,7 +394,7 @@ namespace Revitalize.Objects.Machines
// if(this.output!=null) Game1.player.addItemByMenuIfNecessary((Item)this.output);
// this.showUI();
var obj = this;
Util.addItemToInventoryElseDrop(obj);
Util.addItemToInventoryAndCleanTrackedList(this);
// if (this.output != null) this.output.performRemoveAction(this.tileLocation, who.currentLocation);
this.output = null;
Game1.playSound("coin");

View File

@ -288,7 +288,7 @@ namespace Revitalize.Objects.Machines
if(this.heldObject!=null) Util.addItemToInventoryElseDrop(this.heldObject.getOne());
this.heldObject = new Spawner(parentSheetIndex, Vector2.Zero, this.inventoryMaxSize);
Util.addItemToInventoryElseDrop(this.heldObject.getOne());
Util.addItemToInventoryAndCleanTrackedList(this);
this.heldObject = null;
this.flaggedForPickUp = true;
this.thisLocation = null;
@ -308,9 +308,9 @@ namespace Revitalize.Objects.Machines
// Util.addItemToInventoryElseDrop(this.heldObject);
var obj = new Spawner(parentSheetIndex, Vector2.Zero,this.inventoryMaxSize);
Util.addItemToInventoryElseDrop(obj);
Util.addItemToInventoryAndCleanTrackedList(this);
// this.spillInventoryEverywhere();
if(this.heldObject!=null) this.heldObject.performRemoveAction(this.tileLocation, who.currentLocation);
if (this.heldObject!=null) this.heldObject.performRemoveAction(this.tileLocation, who.currentLocation);
this.heldObject = null;
Game1.playSound("coin");
@ -326,8 +326,8 @@ namespace Revitalize.Objects.Machines
// if(this.heldObject!=null) Game1.player.addItemByMenuIfNecessary((Item)this.heldObject);
// this.spillInventoryEverywhere();
var obj = new Spawner(parentSheetIndex, Vector2.Zero, this.inventoryMaxSize);
Util.addItemToInventoryElseDrop(obj);
if(this.heldObject!=null) this.heldObject.performRemoveAction(this.tileLocation, who.currentLocation);
Util.addItemToInventoryAndCleanTrackedList(this);
if (this.heldObject!=null) this.heldObject.performRemoveAction(this.tileLocation, who.currentLocation);
this.heldObject = null;
Game1.playSound("coin");
this.thisLocation = null;

View File

@ -180,7 +180,7 @@ namespace Revitalize.Objects
{
// Game1.showRedMessage("Why2?");
//this.heldObject = new shopObject(parentSheetIndex, Vector2.Zero);
Game1.player.addItemByMenuIfNecessary(this);
Util.addItemToInventoryAndCleanTrackedList(this);
this.flaggedForPickUp = true;
this.thisLocation = null;
return true;
@ -196,7 +196,7 @@ namespace Revitalize.Objects
}
else {
// this.heldObject = new shopObject(parentSheetIndex, Vector2.Zero);
Game1.player.addItemByMenuIfNecessary(this);
Util.addItemToInventoryAndCleanTrackedList(this);
// this.heldObject.performRemoveAction(this.tileLocation, who.currentLocation);
// this.heldObject = null;
Game1.playSound("coin");
@ -211,7 +211,7 @@ namespace Revitalize.Objects
// Game1.showRedMessage("Why3?");
this.heldObject.performRemoveAction(this.tileLocation, who.currentLocation);
this.heldObject = null;
Game1.player.addItemByMenuIfNecessary((Item)this);
Util.addItemToInventoryAndCleanTrackedList(this);
Game1.playSound("coin");
this.thisLocation = null;
return true;

View File

@ -160,14 +160,18 @@ namespace Revitalize
Log.AsyncM("Done cleaning inventory!");
}
public static void restoreInventory()
/// <summary>
/// Restore all of the serialized mod objects from the mod's player's data folders to either the player's inventory or the game world depending where the object was located when it was serialized.
/// </summary>
public static void restoreAllModObjects()
{
// Log.AsyncG(InvPath);
ProcessDirectoryForCleanUp(InvPath);
try
{
ProcessDirectoryForCleanUp(objectsInWorldPath);
Lists.trackedObjectList.Clear(); //clear whatever mod objects I'm tracking
ProcessDirectoryForCleanUp(objectsInWorldPath); //restore whatever I'm tracking here when I replace the object back into the world. This also works when loading up the game, not just when saving/loading
}
catch(Exception e)
{
@ -175,6 +179,10 @@ namespace Revitalize
}
}
/// <summary>
/// Cleans up the mod files by deleting possible old files for objects that may/may not exist. Aka delete everything before serializing everything again.
/// </summary>
/// <param name="targetDirectory"></param>
public static void ProcessDirectoryForDeletion(string targetDirectory)
{
// Process the list of files found in the directory.
@ -188,7 +196,7 @@ namespace Revitalize
// Recurse into subdirectories of this directory.
string[] subdirectoryEntries = Directory.GetDirectories(targetDirectory);
foreach (string subdirectory in subdirectoryEntries)
ProcessDirectoryForCleanUp(subdirectory);
ProcessDirectoryForDeletion(subdirectory);
}
@ -246,14 +254,14 @@ namespace Revitalize
else
{
cObj.thisLocation.objects.Add(cObj.tileLocation, cObj);
Lists.trackedObjectList.Add(cObj);
//Util.placementAction(cObj, cObj.thisLocation,(int)cObj.tileLocation.X,(int) cObj.tileLocation.Y,null,false);
}
}
}
}
}

View File

@ -66,6 +66,25 @@ namespace Revitalize
}
}
public static bool addItemToInventoryAndCleanTrackedList(CoreObject I)
{
if (Game1.player.isInventoryFull() == false)
{
Game1.player.addItemToInventoryBool(I, false);
Lists.trackedObjectList.Remove(I);
return true;
}
else
{
Random random = new Random(129);
int i = random.Next();
i = i % 4;
Vector2 v2 = new Vector2(Game1.player.getTileX() * Game1.tileSize, Game1.player.getTileY() * Game1.tileSize);
Game1.createItemDebris(I, v2, i);
return false;
}
}
public static bool addItemToInventoryElseUseMenu (List<Item> I)
{
@ -91,7 +110,7 @@ namespace Revitalize
}
public static bool placementAction(CoreObject cObj, GameLocation location, int x, int y, Farmer who = null)
public static bool placementAction(CoreObject cObj, GameLocation location, int x, int y, Farmer who = null, bool playSound=true)
{
Vector2 vector = new Vector2((float)(x / Game1.tileSize), (float)(y / Game1.tileSize));
// cObj.health = 10;
@ -659,7 +678,11 @@ namespace Revitalize
}
@object.initializeLightSource(vector);
}
Game1.playSound("woodyStep");
if(playSound==true) Game1.playSound("woodyStep");
else
{
Log.AsyncG("restoring item from file");
}
//Log.AsyncM("Placed and object");
cObj.locationsName = location.name;
Lists.trackedObjectList.Add(cObj);