2018-05-01 09:21:31 +08:00
using Microsoft.Xna.Framework ;
using Newtonsoft.Json ;
2017-09-05 19:13:42 +08:00
using Newtonsoft.Json.Linq ;
using StardewModdingAPI ;
using StardewValley ;
2017-09-14 04:53:12 +08:00
using StardewValley.Buildings ;
2017-09-05 19:13:42 +08:00
using System ;
using System.Collections.Generic ;
using System.IO ;
using System.Linq ;
using System.Text ;
using System.Threading.Tasks ;
using System.Xml ;
namespace StardustCore.Serialization
{
/// <summary>
/// TODO: Find a way to serialize objects and tools.
/// </summary>
public class SerializationManager
{
public string objectsInWorldPath ;
public string playerInventoryPath ;
2017-09-14 04:53:12 +08:00
public string serializerTrashPath ;
public string storageContainerPath ;
2017-09-05 19:13:42 +08:00
2017-09-14 04:53:12 +08:00
public Dictionary < string , SerializerDataNode > acceptedTypes = new Dictionary < string , SerializerDataNode > ( ) ;
public List < CoreObject > trackedObjectList = new List < CoreObject > ( ) ;
2017-09-05 19:13:42 +08:00
2017-09-14 04:53:12 +08:00
public SerializationManager ( string PlayerInventoryPath , string SerializerTrashPath , string ObjectsInWorldPath , string StorageContainerPath )
{
objectsInWorldPath = ObjectsInWorldPath ;
playerInventoryPath = PlayerInventoryPath ;
serializerTrashPath = SerializerTrashPath ;
storageContainerPath = StorageContainerPath ;
2017-09-05 19:13:42 +08:00
verifyAllDirectoriesExist ( ) ;
}
private void verifyAllDirectoriesExist ( )
{
2017-09-14 04:53:12 +08:00
if ( ! Directory . Exists ( playerInventoryPath ) ) Directory . CreateDirectory ( playerInventoryPath ) ;
if ( ! Directory . Exists ( serializerTrashPath ) ) Directory . CreateDirectory ( serializerTrashPath ) ;
if ( ! Directory . Exists ( objectsInWorldPath ) ) Directory . CreateDirectory ( objectsInWorldPath ) ;
if ( ! Directory . Exists ( storageContainerPath ) ) Directory . CreateDirectory ( storageContainerPath ) ;
2017-09-05 19:13:42 +08:00
}
public void cleanUpInventory ( )
{
ProcessDirectoryForDeletion ( playerInventoryPath ) ;
//ProcessDirectoryForDeletion(SerializerTrashPath);
List < Item > removalList = new List < Item > ( ) ;
2018-05-01 09:21:31 +08:00
foreach ( Item d in Game1 . player . Items )
2017-09-05 19:13:42 +08:00
{
try
{
if ( d = = null )
{
//Log.AsyncG("WTF");
continue ;
}
// Log.AsyncC(d.GetType());
}
2018-05-01 09:21:31 +08:00
catch ( Exception err )
2017-09-05 19:13:42 +08:00
{
2018-05-01 09:21:31 +08:00
ModCore . ModMonitor . Log ( err . ToString ( ) ) ;
2017-09-05 19:13:42 +08:00
}
2018-05-01 09:21:31 +08:00
string s = Convert . ToString ( ( d . GetType ( ) ) ) ;
2017-09-05 19:13:42 +08:00
if ( acceptedTypes . ContainsKey ( s ) )
{
SerializerDataNode t ;
bool works = acceptedTypes . TryGetValue ( s , out t ) ;
if ( works = = true )
{
t . serialize . Invoke ( d ) ;
removalList . Add ( d ) ;
}
}
}
foreach ( var i in removalList )
{
Game1 . player . removeItemFromInventory ( i ) ;
}
removalList . Clear ( ) ;
}
public void cleanUpWorld ( )
{
2017-09-12 03:58:46 +08:00
try
{
ProcessDirectoryForDeletion ( objectsInWorldPath ) ;
}
catch ( Exception e )
{
2018-05-01 09:21:31 +08:00
ModCore . ModMonitor . Log ( e . ToString ( ) ) ;
2017-09-12 03:58:46 +08:00
}
2017-09-05 19:13:42 +08:00
List < CoreObject > removalList = new List < CoreObject > ( ) ;
int countProcessed = 0 ;
List < Item > idk = new List < Item > ( ) ;
foreach ( CoreObject d in trackedObjectList )
{
try
{
if ( d = = null )
{
//Log.AsyncG("WTF");
continue ;
}
// Log.AsyncC(d.GetType());
}
catch ( Exception e )
{
2018-05-01 09:21:31 +08:00
ModCore . ModMonitor . Log ( e . ToString ( ) ) ;
2017-09-05 19:13:42 +08:00
}
string s = Convert . ToString ( ( d . GetType ( ) ) ) ;
if ( acceptedTypes . ContainsKey ( s ) )
{
2017-09-14 04:53:12 +08:00
// Log.AsyncM("Object is of accepted type: " + s);
2017-09-05 19:13:42 +08:00
SerializerDataNode t ;
bool works = acceptedTypes . TryGetValue ( s , out t ) ;
if ( works = = true )
{
countProcessed + + ;
if ( d . useXML = = false )
{
2017-09-14 04:53:12 +08:00
// Log.AsyncY("Saving the object");
2017-09-05 19:13:42 +08:00
t . worldObj . Invoke ( d ) ;
}
else
{
idk . Add ( d ) ;
}
// Log.AsyncC("Progress on saving objects: " + countProcessed + "/" + Lists.trackedObjectList.Count);
removalList . Add ( d ) ;
}
}
}
foreach ( var i in removalList )
{
2018-05-01 09:21:31 +08:00
i . thisLocation . removeObject ( i . TileLocation , false ) ;
2017-09-05 19:13:42 +08:00
}
foreach ( var v in idk )
{
string s = Convert . ToString ( ( v . GetType ( ) ) ) ;
if ( acceptedTypes . ContainsKey ( s ) )
{
SerializerDataNode t ;
bool works = acceptedTypes . TryGetValue ( s , out t ) ;
if ( works = = true )
{
countProcessed + + ;
if ( ( v as CoreObject ) . useXML = = true )
{
t . worldObj . Invoke ( v as CoreObject ) ;
}
//Log.AsyncG("Progress on saving objects: " + countProcessed + "/" + Lists.trackedObjectList.Count);
removalList . Add ( v as CoreObject ) ;
}
}
}
removalList . Clear ( ) ;
// Log.AsyncM("Revitalize: Done cleaning world for saving.");
}
2017-09-14 08:11:05 +08:00
public void cleanUpStorageContainers ( )
2017-09-14 04:53:12 +08:00
{
2017-09-14 08:11:05 +08:00
ProcessDirectoryForDeletion ( storageContainerPath ) ;
List < Item > removalList = new List < Item > ( ) ;
2017-09-14 04:53:12 +08:00
foreach ( GameLocation loc in Game1 . locations )
{
2018-05-01 09:21:31 +08:00
int i = loc . objects . Pairs . Count ( ) ;
2017-09-14 08:11:05 +08:00
int j = 0 ;
2018-05-01 09:21:31 +08:00
foreach ( KeyValuePair < Vector2 , StardewValley . Object > obj in loc . objects . Pairs )
2017-09-14 04:53:12 +08:00
{
2017-09-14 08:11:05 +08:00
j + + ;
2018-05-01 09:21:31 +08:00
ModCore . ModMonitor . Log ( "Parsing location " + loc . Name + " : object number" + j + "/" + i + " : object name: " + obj . Value . name ) ;
2017-09-14 04:53:12 +08:00
if ( obj . Value is StardewValley . Objects . Chest ) {
2017-09-14 08:11:05 +08:00
int k = ( obj . Value as StardewValley . Objects . Chest ) . items . Count ;
int l = 0 ;
foreach ( var item in ( obj . Value as StardewValley . Objects . Chest ) . items )
2017-09-14 04:53:12 +08:00
{
2017-09-14 08:11:05 +08:00
l + + ;
2018-05-01 09:21:31 +08:00
ModCore . ModMonitor . Log ( "Parsing Chest at : " + loc . Name + " X: " + obj . Key . X + " Y: " + obj . Key . Y + " : object number: " + l + "/" + k + "object name: " + item . Name ) ;
2017-09-14 08:11:05 +08:00
if ( item is CoreObject ) removalList . Add ( item ) ;
2017-09-14 04:53:12 +08:00
}
2017-09-14 08:11:05 +08:00
foreach ( var v in removalList )
{
( obj . Value as StardewValley . Objects . Chest ) . items . Remove ( v ) ;
SerializerDataNode t ;
if ( acceptedTypes . ContainsKey ( ( v as CoreObject ) . serializationName ) )
{
acceptedTypes . TryGetValue ( ( v as CoreObject ) . serializationName , out t ) ;
2018-05-01 09:21:31 +08:00
string s = Path . Combine ( loc . Name , "Chest," + Convert . ToString ( ( int ) obj . Key . X ) + "," + Convert . ToString ( ( int ) obj . Key . Y ) ) ;
2017-09-14 08:11:05 +08:00
string s2 = Path . Combine ( ModCore . SerializationManager . storageContainerPath , s ) ;
if ( ! Directory . Exists ( s ) ) Directory . CreateDirectory ( s2 ) ;
t . serializeToContainer . Invoke ( v , s2 ) ;
}
}
removalList . Clear ( ) ;
2017-09-14 04:53:12 +08:00
}
}
}
foreach ( Building building in Game1 . getFarm ( ) . buildings )
{
2017-09-14 08:11:05 +08:00
2018-05-07 09:21:31 +08:00
GameLocation loc = Game1 . getLocationFromName ( building . nameOfIndoors , true ) ;
2018-05-01 09:21:31 +08:00
ModCore . ModMonitor . Log ( "Cleaning up farm building: " + loc . uniqueName . Value ) ;
int i = loc . objects . Pairs . Count ( ) ;
2017-09-14 08:11:05 +08:00
int j = 0 ;
2018-05-01 09:21:31 +08:00
foreach ( KeyValuePair < Vector2 , StardewValley . Object > obj in loc . objects . Pairs )
2017-09-14 04:53:12 +08:00
{
2017-09-14 08:11:05 +08:00
j + + ;
2018-05-01 09:21:31 +08:00
ModCore . ModMonitor . Log ( "Parsing location " + loc . Name + " : object number" + j + "/" + i + " : object name: " + obj . Value . name ) ;
2017-09-14 04:53:12 +08:00
if ( obj . Value is StardewValley . Objects . Chest )
{
2017-09-14 08:11:05 +08:00
int k = ( obj . Value as StardewValley . Objects . Chest ) . items . Count ;
int l = 0 ;
2017-09-14 04:53:12 +08:00
foreach ( var item in ( obj . Value as StardewValley . Objects . Chest ) . items )
{
2017-09-14 08:11:05 +08:00
l + + ;
2018-05-01 09:21:31 +08:00
ModCore . ModMonitor . Log ( "Parsing Chest at : " + loc . Name + " X: " + obj . Key . X + " Y: " + obj . Key . Y + " : object number: " + l + "/" + k + "object name: " + item . Name ) ;
2017-09-14 08:11:05 +08:00
if ( item is CoreObject ) removalList . Add ( item ) ;
2017-09-14 04:53:12 +08:00
}
2017-09-14 08:11:05 +08:00
foreach ( var v in removalList )
{
( obj . Value as StardewValley . Objects . Chest ) . items . Remove ( v ) ;
SerializerDataNode t ;
if ( acceptedTypes . ContainsKey ( ( v as CoreObject ) . serializationName ) ) {
acceptedTypes . TryGetValue ( ( v as CoreObject ) . serializationName , out t ) ;
2018-05-07 09:21:31 +08:00
string s = Path . Combine ( building . nameOfIndoors , "Chest," + Convert . ToString ( ( int ) obj . Key . X ) + "," + Convert . ToString ( ( int ) obj . Key . Y ) ) ;
2017-09-14 08:11:05 +08:00
string s2 = Path . Combine ( ModCore . SerializationManager . storageContainerPath , s ) ;
if ( ! Directory . Exists ( s ) ) Directory . CreateDirectory ( s2 ) ;
t . serializeToContainer . Invoke ( v , s2 ) ;
}
}
removalList . Clear ( ) ;
2017-09-14 04:53:12 +08:00
}
}
}
}
2017-09-05 19:13:42 +08:00
2018-05-01 09:21:31 +08:00
/// <summary>
/// Reloads all modded objects added by this mod back to the game in proper locations.
/// </summary>
/// <param name="thingsToAddBackIn"></param>
2017-09-05 19:13:42 +08:00
public void restoreAllModObjects ( List < CoreObject > thingsToAddBackIn )
{
2017-09-14 04:53:12 +08:00
processDirectoryForDeserialization ( playerInventoryPath , thingsToAddBackIn ) ;
2017-09-14 08:11:05 +08:00
2017-09-14 04:53:12 +08:00
// Log.AsyncG("Done deserializing player inventory.");
2017-09-05 19:13:42 +08:00
try
{
trackedObjectList . Clear ( ) ; //clear whatever mod objects I'm tracking
processDirectoryForDeserialization ( objectsInWorldPath , thingsToAddBackIn ) ; //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
2017-09-14 08:11:05 +08:00
processDirectoryForDeserializationIntoContainer ( storageContainerPath , thingsToAddBackIn ) ;
2017-09-05 19:13:42 +08:00
}
catch ( Exception e )
{
2018-05-01 09:21:31 +08:00
ModCore . ModMonitor . Log ( e . ToString ( ) ) ;
2017-09-05 19:13:42 +08:00
}
}
public void ProcessDirectoryForDeletion ( string targetDirectory )
{
// Process the list of files found in the directory.
string [ ] fileEntries = Directory . GetFiles ( targetDirectory ) ;
foreach ( string fileName in fileEntries )
{
File . Delete ( fileName ) ;
// File.Delete(fileName);
}
// Recurse into subdirectories of this directory.
string [ ] subdirectoryEntries = Directory . GetDirectories ( targetDirectory ) ;
foreach ( string subdirectory in subdirectoryEntries )
ProcessDirectoryForDeletion ( subdirectory ) ;
}
public void WriteToJsonFile < T > ( string filePath , T objectToWrite , bool append = false ) where T : new ( )
{
TextWriter writer = null ;
try
{
JsonSerializerSettings settings = new JsonSerializerSettings ( ) ;
//settings.TypeNameHandling = TypeNameHandling.Auto;
settings . ReferenceLoopHandling = ReferenceLoopHandling . Ignore ;
settings . TypeNameHandling = TypeNameHandling . Auto ;
// settings.Formatting = Formatting.Indented;
var contentsToWriteToFile = JsonConvert . SerializeObject ( objectToWrite , settings ) ;
int i = 0 ;
string s = filePath ;
while ( File . Exists ( s ) = = true )
{
s = filePath ;
s = ( s + Convert . ToString ( i ) ) ;
i + + ;
}
filePath = s ;
writer = new StreamWriter ( filePath , append ) ;
writer . Write ( contentsToWriteToFile ) ;
}
finally
{
if ( writer ! = null )
writer . Close ( ) ;
}
}
public void serializeXML < T > ( Item I )
{
System . Xml . Serialization . XmlSerializer xmlSerializer = new System . Xml . Serialization . XmlSerializer ( typeof ( T ) ) ;
var newWriter = new StringWriter ( ) ;
using ( var writer = XmlWriter . Create ( newWriter ) )
{
xmlSerializer . Serialize ( writer , I ) ;
}
}
/// <summary>
///
/// </summary>
/// <param name="pathToFile"></param>
/// <param name="thingsToAddBackIn">Typically this would be the trackedObjectList.</param>
public void processDirectoryForDeserialization ( string pathToFile , List < CoreObject > thingsToAddBackIn )
{
string [ ] fileEntries = Directory . GetFiles ( pathToFile ) ;
2017-09-14 04:53:12 +08:00
// Log.AsyncC(pathToFile);
2017-09-05 19:13:42 +08:00
foreach ( var fileName in fileEntries )
{
ProcessFileForCleanUp ( fileName , thingsToAddBackIn ) ;
2017-09-14 04:53:12 +08:00
// Log.AsyncG(fileName);
2017-09-05 19:13:42 +08:00
}
string [ ] subDirectories = Directory . GetDirectories ( pathToFile ) ;
foreach ( var folder in subDirectories )
{
processDirectoryForDeserialization ( folder , thingsToAddBackIn ) ;
}
}
2017-09-14 08:11:05 +08:00
public void processDirectoryForDeserializationIntoContainer ( string pathToFile , List < CoreObject > thingsToAddBackIn )
{
string [ ] fileEntries = Directory . GetFiles ( pathToFile ) ;
// Log.AsyncC(pathToFile);
foreach ( var fileName in fileEntries )
{
ProcessFileForCleanUpIntoContainer ( fileName , thingsToAddBackIn ) ;
// Log.AsyncG(fileName);
}
string [ ] subDirectories = Directory . GetDirectories ( pathToFile ) ;
foreach ( var folder in subDirectories )
{
processDirectoryForDeserializationIntoContainer ( folder , thingsToAddBackIn ) ;
}
}
2017-09-05 19:13:42 +08:00
public void ProcessFileForCleanUp ( string path , List < CoreObject > thingsToAddBackIn )
{
2017-09-14 04:53:12 +08:00
//Log.AsyncC(path);
2017-09-05 19:13:42 +08:00
string [ ] ehh = File . ReadAllLines ( path ) ;
string data = ehh [ 0 ] ;
CoreObject cObj ;
string a ;
string [ ] b ;
string s = "" ;
// Log.AsyncC(path);
// Log.AsyncC(data);
try
{
dynamic obj = JObject . Parse ( data ) ;
// Log.AsyncC(obj.thisType);
a = obj . serializationName ;
b = a . Split ( ',' ) ;
s = b . ElementAt ( 0 ) ;
2017-09-14 04:53:12 +08:00
// Log.AsyncC(s);
2017-09-05 19:13:42 +08:00
}
catch ( Exception e )
{
2018-05-01 09:21:31 +08:00
e . ToString ( ) ; //Get rid of that warning because I'll do other things.
2017-09-05 19:13:42 +08:00
//USE XML STYLE DESERIALIZING
foreach ( KeyValuePair < string , SerializerDataNode > pair in acceptedTypes )
{
var word = ParseXMLType ( path ) ;
if ( pair . Key = = word . ToString ( ) )
{
cObj = ( CoreObject ) pair . Value . parse . Invoke ( path ) ;
( cObj as CoreObject ) . thisLocation = Game1 . getLocationFromName ( ( cObj as CoreObject ) . locationsName ) ;
( cObj as CoreObject ) . resetTexture ( ) ;
if ( ( cObj as CoreObject ) . thisLocation = = null )
{
Game1 . player . addItemToInventory ( cObj ) ;
2017-09-14 04:53:12 +08:00
// Log.AsyncY("ADDED ITEM TO INVENTORY");
2017-09-05 19:13:42 +08:00
return ;
}
else
{
2018-05-01 09:21:31 +08:00
( cObj as CoreObject ) . thisLocation . objects . Add ( ( cObj as CoreObject ) . TileLocation , ( StardewValley . Object ) cObj ) ;
2017-09-05 19:13:42 +08:00
thingsToAddBackIn . Add ( cObj ) ;
//Util.placementAction(cObj, cObj.thisLocation,(int)cObj.tileLocation.X,(int) cObj.tileLocation.Y,null,false);
}
}
}
// Log.AsyncG("attempting to parse from path and value of s is " + s);
}
// var cObj = parseBagOfHolding(path); //pair.Value.parse.Invoke(path);
// cObj.TextureSheet = Game1.content.Load<Texture2D>(Path.Combine("Revitalize", "CropsNSeeds", "Graphics", "seeds"));
/ *
cObj . thisLocation = Game1 . getLocationFromName ( cObj . locationsName ) ;
if ( cObj . thisLocation = = null )
{
Game1 . player . addItemToInventory ( cObj ) ;
return ;
}
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);
}
* /
//USE JSON STYLE DESERIALIZNG
if ( acceptedTypes . ContainsKey ( s ) )
{
2017-09-14 04:53:12 +08:00
//Log.AsyncC("FUUUUU");
2017-09-05 19:13:42 +08:00
foreach ( KeyValuePair < string , SerializerDataNode > pair in acceptedTypes )
{
2017-09-14 04:53:12 +08:00
// Log.AsyncY(pair.Key);
2017-09-05 19:13:42 +08:00
if ( pair . Key = = s )
{
try
{
//parse from Json Style
2017-09-14 04:53:12 +08:00
// Log.AsyncR("1");
2017-09-05 19:13:42 +08:00
cObj = ( CoreObject ) pair . Value . parse . Invoke ( data ) ;
( cObj as CoreObject ) . thisLocation = Game1 . getLocationFromName ( ( cObj as CoreObject ) . locationsName ) ;
if ( ( cObj as CoreObject ) . thisLocation = = null )
{
Game1 . player . addItemToInventory ( cObj ) ;
2017-09-14 04:53:12 +08:00
// Log.AsyncY("ADDED ITEM TO INVENTORY");
2017-09-05 19:13:42 +08:00
return ;
}
else
{
2018-05-01 09:21:31 +08:00
( cObj as CoreObject ) . thisLocation . objects . Add ( ( cObj as CoreObject ) . TileLocation , ( StardewValley . Object ) cObj ) ;
2017-09-05 19:13:42 +08:00
thingsToAddBackIn . Add ( cObj ) ;
//Util.placementAction(cObj, cObj.thisLocation,(int)cObj.tileLocation.X,(int) cObj.tileLocation.Y,null,false);
}
}
catch ( Exception e )
{
2018-05-01 09:21:31 +08:00
ModCore . ModMonitor . Log ( e . ToString ( ) ) ;
2017-09-14 04:53:12 +08:00
// Log.AsyncO(e);
2017-09-05 19:13:42 +08:00
}
}
}
}
else
{
2017-10-12 03:55:59 +08:00
ModCore . ModMonitor . Log ( "Error parsing unknown object type: " + s , LogLevel . Error ) ;
2017-09-05 19:13:42 +08:00
}
}
2017-09-14 08:11:05 +08:00
public void ProcessFileForCleanUpIntoContainer ( string path , List < CoreObject > thingsToAddBackIn )
{
//Log.AsyncC(path);
string newLine = Environment . NewLine ;
string [ ] chestArray = path . Split ( new string [ ] { "/" } , StringSplitOptions . None ) ;
string [ ] chestArray2 = path . Split ( new string [ ] { "\\" } , StringSplitOptions . None ) ;
/ *
foreach ( var v in chestArray )
{
Log . AsyncC ( "PART OF PATH " + v ) ;
}
foreach ( var v in chestArray2 )
{
Log . AsyncC ( "PART OF PATH2 " + v ) ;
}
* /
if ( chestArray2 . Length > chestArray . Length ) chestArray = chestArray2 ;
GameLocation loc = Game1 . getLocationFromName ( chestArray [ chestArray . Length - 3 ] ) ;
string [ ] chest = chestArray [ chestArray . Length - 2 ] . Split ( ',' ) ;
StardewValley . Object chestObject ;
bool f = loc . objects . TryGetValue ( new Microsoft . Xna . Framework . Vector2 ( Convert . ToInt32 ( chest [ 1 ] ) , Convert . ToInt32 ( chest [ 2 ] ) ) , out chestObject ) ;
if ( f = = true )
{
2017-10-12 03:55:59 +08:00
ModCore . ModMonitor . Log ( "YAY" ) ;
2017-09-14 08:11:05 +08:00
}
else
{
2017-10-12 03:55:59 +08:00
ModCore . ModMonitor . Log ( "BOO" ) ;
2017-09-14 08:11:05 +08:00
}
string [ ] ehh = File . ReadAllLines ( path ) ;
string data = ehh [ 0 ] ;
CoreObject cObj ;
string a ;
string [ ] b ;
string s = "" ;
// Log.AsyncC(path);
// Log.AsyncC(data);
try
{
dynamic obj = JObject . Parse ( data ) ;
// Log.AsyncC(obj.thisType);
a = obj . serializationName ;
b = a . Split ( ',' ) ;
s = b . ElementAt ( 0 ) ;
// Log.AsyncC(s);
}
catch ( Exception e )
{
2018-05-01 09:21:31 +08:00
e . ToString ( ) ;
2017-09-14 08:11:05 +08:00
//USE XML STYLE DESERIALIZING
foreach ( KeyValuePair < string , SerializerDataNode > pair in acceptedTypes )
{
var word = ParseXMLType ( path ) ;
if ( pair . Key = = word . ToString ( ) )
{
cObj = ( CoreObject ) pair . Value . parse . Invoke ( path ) ;
( cObj as CoreObject ) . thisLocation = Game1 . getLocationFromName ( ( cObj as CoreObject ) . locationsName ) ;
( cObj as CoreObject ) . resetTexture ( ) ;
if ( ( cObj as CoreObject ) . thisLocation = = null )
{
// Game1.player.addItemToInventory(cObj);
try
{
2018-05-01 09:21:31 +08:00
2017-09-14 08:11:05 +08:00
Utilities . addItemToOtherInventory ( ( chestObject as StardewValley . Objects . Chest ) . items , cObj ) ;
}
catch ( Exception err )
{
2017-10-12 03:55:59 +08:00
ModCore . ModMonitor . Log ( err . ToString ( ) , LogLevel . Error ) ;
2017-09-14 08:11:05 +08:00
}
// Log.AsyncY("ADDED ITEM TO INVENTORY");
return ;
}
else
{
2018-05-01 09:21:31 +08:00
( cObj as CoreObject ) . thisLocation . objects . Add ( ( cObj as CoreObject ) . TileLocation , ( StardewValley . Object ) cObj ) ;
2017-09-14 08:11:05 +08:00
thingsToAddBackIn . Add ( cObj ) ;
//Util.placementAction(cObj, cObj.thisLocation,(int)cObj.tileLocation.X,(int) cObj.tileLocation.Y,null,false);
}
}
}
// Log.AsyncG("attempting to parse from path and value of s is " + s);
}
// var cObj = parseBagOfHolding(path); //pair.Value.parse.Invoke(path);
// cObj.TextureSheet = Game1.content.Load<Texture2D>(Path.Combine("Revitalize", "CropsNSeeds", "Graphics", "seeds"));
/ *
cObj . thisLocation = Game1 . getLocationFromName ( cObj . locationsName ) ;
if ( cObj . thisLocation = = null )
{
Game1 . player . addItemToInventory ( cObj ) ;
return ;
}
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);
}
* /
//USE JSON STYLE DESERIALIZNG
if ( acceptedTypes . ContainsKey ( s ) )
{
2017-10-12 03:55:59 +08:00
ModCore . ModMonitor . Log ( "parse???" ) ;
2017-09-14 08:11:05 +08:00
foreach ( KeyValuePair < string , SerializerDataNode > pair in acceptedTypes )
{
// Log.AsyncY(pair.Key);
if ( pair . Key = = s )
{
try
{
//parse from Json Style
// Log.AsyncR("1");
cObj = ( CoreObject ) pair . Value . parse . Invoke ( data ) ;
( cObj as CoreObject ) . thisLocation = Game1 . getLocationFromName ( ( cObj as CoreObject ) . locationsName ) ;
2017-10-12 03:55:59 +08:00
ModCore . ModMonitor . Log ( "closer" ) ;
2017-09-14 08:11:05 +08:00
if ( ( cObj as CoreObject ) . thisLocation = = null )
{
try
{
2017-10-12 03:55:59 +08:00
ModCore . ModMonitor . Log ( "HERE PRETTY MUCH!!!" ) ;
2017-09-14 08:11:05 +08:00
Utilities . addItemToOtherInventory ( ( chestObject as StardewValley . Objects . Chest ) . items , cObj ) ;
foreach ( var v in ( chestObject as StardewValley . Objects . Chest ) . items )
{
2017-10-12 03:55:59 +08:00
ModCore . ModMonitor . Log ( v . Name ) ;
2017-09-14 08:11:05 +08:00
}
}
catch ( Exception err )
{
2017-10-12 03:55:59 +08:00
ModCore . ModMonitor . Log ( err . ToString ( ) , LogLevel . Error ) ;
2017-09-14 08:11:05 +08:00
}
// Log.AsyncY("ADDED ITEM TO INVENTORY");
return ;
}
else
{
2017-10-12 03:55:59 +08:00
ModCore . ModMonitor . Log ( "WHY HERE????" ) ;
2018-05-01 09:21:31 +08:00
( cObj as CoreObject ) . thisLocation . objects . Add ( ( cObj as CoreObject ) . TileLocation , ( StardewValley . Object ) cObj ) ;
2017-09-14 08:11:05 +08:00
thingsToAddBackIn . Add ( cObj ) ;
//Util.placementAction(cObj, cObj.thisLocation,(int)cObj.tileLocation.X,(int) cObj.tileLocation.Y,null,false);
}
}
catch ( Exception e )
{
2018-05-01 09:21:31 +08:00
ModCore . ModMonitor . Log ( e . ToString ( ) ) ;
2017-09-14 08:11:05 +08:00
// Log.AsyncO(e);
}
}
}
}
else
{
2017-10-12 03:55:59 +08:00
ModCore . ModMonitor . Log ( "Error parsing unknown object type: " + s , LogLevel . Error ) ;
2017-09-14 08:11:05 +08:00
}
}
2017-09-05 19:13:42 +08:00
public string ParseXMLType ( string path )
{
string [ ] s = File . ReadAllLines ( path ) ;
string returnString = "" ;
foreach ( string v in s )
{
// Log.AsyncC(v);
if ( v . Contains ( "serializationName" ) )
{
returnString = v . Remove ( 0 , 12 ) ;
returnString = returnString . Remove ( returnString . Length - 11 , 11 ) ;
}
}
return returnString ;
}
public static Microsoft . Xna . Framework . Rectangle parseRectFromJson ( string s )
{
s = s . Replace ( '{' , ' ' ) ;
s = s . Replace ( '}' , ' ' ) ;
s = s . Replace ( '^' , ' ' ) ;
s = s . Replace ( ':' , ' ' ) ;
string [ ] parsed = s . Split ( ' ' ) ;
foreach ( var v in parsed )
{
//Log.AsyncY(v);
}
return new Microsoft . Xna . Framework . Rectangle ( Convert . ToInt32 ( parsed [ 2 ] ) , Convert . ToInt32 ( parsed [ 4 ] ) , Convert . ToInt32 ( parsed [ 6 ] ) , Convert . ToInt32 ( parsed [ 8 ] ) ) ;
}
2018-05-01 09:21:31 +08:00
/// <summary>
/// Remove all objects that there are a copy of this thing?
/// </summary>
/// <param name="c"></param>
2017-11-25 03:21:03 +08:00
public void removeObjectWithCopy ( CoreObject c )
{
foreach ( var v in StardustCore . ModCore . SerializationManager . trackedObjectList )
{
2018-05-01 09:21:31 +08:00
if ( c . TileLocation = = v . TileLocation & & c . thisLocation = = v . thisLocation )
2017-11-25 03:21:03 +08:00
{
StardustCore . ModCore . SerializationManager . trackedObjectList . Remove ( v ) ;
}
}
}
2017-09-05 19:13:42 +08:00
}
}