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:
* 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
@ -87,6 +88,8 @@ namespace StardustCore
TextureManagers.Add(ModManifest.UniqueID, TextureManager);
StardewModdingAPI.Events.ControlEvents.KeyPressed += ControlEvents_KeyPressed;
ModHelper.Events.World.ObjectListChanged += World_ObjectListChanged;
config = ModHelper.ReadConfig<ModConfig>();
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)
{
if (justWarped) return;
SerializationManager.cleanUpInventory();
//SerializationManager.cleanUpWorld();
//SerializationManager.cleanUpStorageContainers();
@ -112,8 +127,8 @@ namespace StardustCore
if (Game1.player != null)
{
SerializationManager.cleanUpInventory();
SerializationManager.cleanUpWorld();
SerializationManager.cleanUpStorageContainers();
//SerializationManager.cleanUpWorld();
//SerializationManager.cleanUpStorageContainers();
Monitor.Log("Saved the player data after returning to title!");
}
}
@ -125,8 +140,8 @@ namespace StardustCore
ModMonitor.Log("Peer disconnected! Serializing custom objects");
SerializationManager.cleanUpInventory();
SerializationManager.cleanUpWorld();
SerializationManager.cleanUpStorageContainers();
//SerializationManager.cleanUpWorld();
//SerializationManager.cleanUpStorageContainers();
}
@ -145,6 +160,7 @@ namespace StardustCore
}
else if (e.Type == MultiplayerSupport.RestoreModObjects)
{
if (Game1.eventUp) return; //Prevent item duplication.
ModMonitor.Log("Restoring custom objects.");
SerializationManager.restoreAllModObjects(SerializationManager.trackedObjectList);
}
@ -156,11 +172,17 @@ namespace StardustCore
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.");
SerializationManager.cleanUpInventory();
SerializationManager.cleanUpWorld();
SerializationManager.cleanUpStorageContainers();
//SerializationManager.cleanUpWorld();
//SerializationManager.cleanUpStorageContainers();
}
@ -336,8 +358,8 @@ namespace StardustCore
{
ModMonitor.Log("Serialize all objects on disposing!");
SerializationManager.cleanUpInventory();
SerializationManager.cleanUpWorld();
SerializationManager.cleanUpStorageContainers();
//SerializationManager.cleanUpWorld();
//SerializationManager.cleanUpStorageContainers();
}
}

View File

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

View File

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

View File

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