Better working objects for syncing. Just need to fix NetCoreObject issues.

This commit is contained in:
Joshua Navarro 2018-12-17 00:02:43 -08:00
parent 835f21fb22
commit ea89e9a2d2
4 changed files with 105 additions and 11 deletions

View File

@ -27,7 +27,8 @@ namespace StardustCore
/* /*
*Known issues: *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. * 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 public class ModCore : Mod
@ -87,6 +88,8 @@ namespace StardustCore
TextureManagers.Add(ModManifest.UniqueID, TextureManager); TextureManagers.Add(ModManifest.UniqueID, TextureManager);
StardewModdingAPI.Events.ControlEvents.KeyPressed += ControlEvents_KeyPressed; StardewModdingAPI.Events.ControlEvents.KeyPressed += ControlEvents_KeyPressed;
ModHelper.Events.World.ObjectListChanged += World_ObjectListChanged;
config = ModHelper.ReadConfig<ModConfig>(); config = ModHelper.ReadConfig<ModConfig>();
StardewModdingAPI.Events.GameEvents.UpdateTick += GameEvents_UpdateTick; 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) private void Player_Warped(object sender, StardewModdingAPI.Events.WarpedEventArgs e)
{ {
if (justWarped) return;
SerializationManager.cleanUpInventory(); SerializationManager.cleanUpInventory();
//SerializationManager.cleanUpWorld(); //SerializationManager.cleanUpWorld();
//SerializationManager.cleanUpStorageContainers(); //SerializationManager.cleanUpStorageContainers();
@ -112,8 +127,8 @@ namespace StardustCore
if (Game1.player != null) if (Game1.player != null)
{ {
SerializationManager.cleanUpInventory(); SerializationManager.cleanUpInventory();
SerializationManager.cleanUpWorld(); //SerializationManager.cleanUpWorld();
SerializationManager.cleanUpStorageContainers(); //SerializationManager.cleanUpStorageContainers();
Monitor.Log("Saved the player data after returning to title!"); Monitor.Log("Saved the player data after returning to title!");
} }
} }
@ -125,8 +140,8 @@ namespace StardustCore
ModMonitor.Log("Peer disconnected! Serializing custom objects"); ModMonitor.Log("Peer disconnected! Serializing custom objects");
SerializationManager.cleanUpInventory(); SerializationManager.cleanUpInventory();
SerializationManager.cleanUpWorld(); //SerializationManager.cleanUpWorld();
SerializationManager.cleanUpStorageContainers(); //SerializationManager.cleanUpStorageContainers();
} }
@ -145,6 +160,7 @@ namespace StardustCore
} }
else if (e.Type == MultiplayerSupport.RestoreModObjects) else if (e.Type == MultiplayerSupport.RestoreModObjects)
{ {
if (Game1.eventUp) return; //Prevent item duplication.
ModMonitor.Log("Restoring custom objects."); ModMonitor.Log("Restoring custom objects.");
SerializationManager.restoreAllModObjects(SerializationManager.trackedObjectList); SerializationManager.restoreAllModObjects(SerializationManager.trackedObjectList);
} }
@ -156,11 +172,17 @@ namespace StardustCore
if (SerializationManager == null) return; 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."); ModMonitor.Log("Got peer context. Serialize/remove all custom objects really quick to prevent loading errors.");
SerializationManager.cleanUpInventory(); SerializationManager.cleanUpInventory();
SerializationManager.cleanUpWorld(); //SerializationManager.cleanUpWorld();
SerializationManager.cleanUpStorageContainers(); //SerializationManager.cleanUpStorageContainers();
} }
@ -336,8 +358,8 @@ namespace StardustCore
{ {
ModMonitor.Log("Serialize all objects on disposing!"); ModMonitor.Log("Serialize all objects on disposing!");
SerializationManager.cleanUpInventory(); SerializationManager.cleanUpInventory();
SerializationManager.cleanUpWorld(); //SerializationManager.cleanUpWorld();
SerializationManager.cleanUpStorageContainers(); //SerializationManager.cleanUpStorageContainers();
} }
} }

View File

@ -49,6 +49,7 @@ namespace StardustCore.NetCode
public NetBool useXML; public NetBool useXML;
public NetString serializationName; public NetString serializationName;
//Animation Manager..... //Animation Manager.....
public NetAnimationManager animationManager; public NetAnimationManager animationManager;
@ -101,6 +102,11 @@ namespace StardustCore.NetCode
drawPosition.Read(reader, version); drawPosition.Read(reader, version);
Value.drawPosition = drawPosition.Value; 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 = new NetAnimationManager();
animationManager.Read(reader, version); animationManager.Read(reader, version);
@ -132,6 +138,9 @@ namespace StardustCore.NetCode
drawPosition = new NetVector2(Value.drawPosition); drawPosition = new NetVector2(Value.drawPosition);
drawPosition.Write(writer); drawPosition.Write(writer);
locationName = new NetString(Value.locationsName);
locationName.Write(writer);
/* /*
if (Value.animationManager == null) if (Value.animationManager == null)
{ {

View File

@ -576,7 +576,6 @@ namespace StardustCore
StardustCore.ModCore.SerializationManager.trackedObjectList.Remove(this); StardustCore.ModCore.SerializationManager.trackedObjectList.Remove(this);
this.thisLocation.removeObject(this.TileLocation, false); this.thisLocation.removeObject(this.TileLocation, false);
this.thisLocation = null; this.thisLocation = null;
this.locationsName = ""; this.locationsName = "";

View File

@ -133,6 +133,8 @@ namespace StardustCore.Serialization
/// </summary> /// </summary>
public void cleanUpWorld() public void cleanUpWorld()
{ {
try try
{ {
ProcessDirectoryForDeletion(objectsInWorldPath); ProcessDirectoryForDeletion(objectsInWorldPath);
@ -144,8 +146,70 @@ namespace StardustCore.Serialization
List<IItemSerializeable> removalList = new List<IItemSerializeable>(); List<IItemSerializeable> removalList = new List<IItemSerializeable>();
int countProcessed = 0; int countProcessed = 0;
List<Item> idk = new List<Item>(); List<Item> idk = new List<Item>();
List<GameLocation> allLocations = new List<GameLocation>();
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) foreach (CoreObject d in trackedObjectList)
{ {
if (removalList.Contains(d)) continue;
try try
{ {
if (d == null) if (d == null)