refactor SaveAnywhere utilities & simplify save/load logic

This commit formats/documents/simplifies code, standardises naming conventions, removes unused code, decouples classes, etc.
This commit is contained in:
Jesse Plamondon-Willard 2017-07-30 21:58:48 -04:00
parent e88bc4615b
commit c14f7c8b24
3 changed files with 331 additions and 752 deletions

View File

@ -3,70 +3,59 @@ using System.IO;
namespace Omegasis.SaveAnywhere
{
/// <summary>Provides methods for reading and writing the config file.</summary>
internal class ConfigUtilities
{
public string key_binding = "K";
public bool warp_character;
/*********
** Properties
*********/
/// <summary>The full path to the mod folder.</summary>
private readonly string ModPath;
public void DataLoader_Settings()
/*********
** Accessors
*********/
/// <summary>The key which saves the game.</summary>
public string KeyBinding { get; private set; } = "K";
/*********
** Public methods
*********/
/// <summary>Construct an instance.</summary>
/// <param name="modPath">The full path to the mod folder.</param>
public ConfigUtilities(string modPath)
{
//loads the data to the variables upon loading the game.
string myname = StardewValley.Game1.player.name;
string mylocation = Path.Combine(SaveAnywhere.mod_path, "Save_Anywhere_Config");
string mylocation2 = mylocation;
string mylocation3 = mylocation2 + ".txt";
if (!File.Exists(mylocation3)) //if not data.json exists, initialize the data variables to the ModConfig data. I.E. starting out.
{
key_binding = "K";
warp_character = true;
}
this.ModPath = modPath;
}
/// <summary>Load the configuration settings.</summary>
public void LoadConfig()
{
string path = Path.Combine(this.ModPath, "Save_Anywhere_Config.txt");
if (!File.Exists(path))
this.KeyBinding = "K";
else
{
string[] readtext = File.ReadAllLines(mylocation3);
key_binding = Convert.ToString(readtext[3]);
string[] text = File.ReadAllLines(path);
this.KeyBinding = Convert.ToString(text[3]);
}
}
public void MyWritter_Settings()
/// <summary>Save the configuration settings.</summary>
public void WriteConfig()
{
string path = Path.Combine(this.ModPath, "Save_Anywhere_Config.txt");
//write all of my info to a text file.
string myname = StardewValley.Game1.player.name;
string[] text = new string[20];
text[0] = "Config: Save_Anywhere Info. Feel free to mess with these settings.";
text[1] = "====================================================================================";
string mylocation = Path.Combine(SaveAnywhere.mod_path, "Save_Anywhere_Config");
string mylocation2 = mylocation;
string mylocation3 = mylocation2 + ".txt";
text[2] = "Key binding for saving anywhere. Press this key to save anywhere!";
text[3] = this.KeyBinding;
string[] mystring3 = new string[20];
if (!File.Exists(mylocation3))
{
Console.WriteLine("The custom character save info doesn't exist. It will be created when the custom saving method is run. Which is now.");
mystring3[0] = "Config: Save_Anywhere Info. Feel free to mess with these settings.";
mystring3[1] = "====================================================================================";
mystring3[2] = "Key binding for saving anywhere. Press this key to save anywhere!";
mystring3[3] = key_binding.ToString();
File.WriteAllLines(mylocation3, mystring3);
}
else
{
//write out the info to a text file at the end of a day. This will run if it doesnt exist.
mystring3[0] = "Config: Save_Anywhere Info. Feel free to mess with these settings.";
mystring3[1] = "====================================================================================";
mystring3[2] = "Key binding for saving anywhere. Press this key to save anywhere!";
mystring3[3] = key_binding.ToString();
File.WriteAllLines(mylocation3, mystring3);
}
File.WriteAllLines(path, text);
}
}
}

View File

@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using Microsoft.Xna.Framework;
@ -16,41 +15,40 @@ namespace Omegasis.SaveAnywhere
private SaveManager SaveManager;
private ConfigUtilities ConfigUtilities;
public static string mod_path;
public static string player_path;
public static string animal_path;
public static string npc_path;
public static bool npc_warp;
public static int checking_time;
public static bool once;
public static bool new_day;
Dictionary<string, string> npc_key_value_pair;
public static IMonitor thisMonitor;
public override void Entry(IModHelper helper)
{
this.ConfigUtilities = new ConfigUtilities();
this.SaveManager = new SaveManager(this.Helper.Reflection);
this.ConfigUtilities = new ConfigUtilities(this.Helper.DirectoryPath);
ControlEvents.KeyPressed += KeyPressed_Save_Load_Menu;
SaveEvents.AfterLoad += PlayerEvents_LoadedGame;
GameEvents.UpdateTick += Warp_Check;
GameEvents.UpdateTick += PassiveSaveChecker;
GameEvents.UpdateTick += this.GameEvents_UpdateTick;
TimeEvents.TimeOfDayChanged += NPC_scheduel_update;
TimeEvents.DayOfMonthChanged += TimeEvents_DayOfMonthChanged;
TimeEvents.DayOfMonthChanged += TimeEvents_OnNewDay;
SaveAnywhere.mod_path = Helper.DirectoryPath;
npc_key_value_pair = new Dictionary<string, string>();
SaveAnywhere.thisMonitor = Monitor;
}
private void PassiveSaveChecker(object sender, EventArgs e)
/// <summary>The method invoked when the game updates (roughly 60 times per second).</summary>
/// <param name="sender">The event sender.</param>
/// <param name="e">The event data.</param>
private void GameEvents_UpdateTick(object sender, EventArgs e)
{
if (this.SaveManager.passiveSave && Game1.activeClickableMenu == null)
if (!Context.IsWorldReady)
return;
try
{
Game1.activeClickableMenu = new StardewValley.Menus.SaveGameMenu();
this.SaveManager.passiveSave = false;
this.SaveManager.Update();
}
catch (Exception ex)
{
this.Monitor.Log(ex.ToString(), LogLevel.Error);
}
}
@ -59,11 +57,7 @@ namespace Omegasis.SaveAnywhere
{
try
{
//Log.Info("Day of Month Changed");
SaveAnywhere.new_day = true;
string name = Game1.player.name;
SaveAnywhere.player_path = Path.Combine(SaveAnywhere.mod_path, "Save_Data", name);
}
catch (Exception err)
{
@ -672,61 +666,16 @@ namespace Omegasis.SaveAnywhere
}
//done
private void ShippingCheck(object sender, EventArgs e)
{
try
{
if (Game1.activeClickableMenu == null)
this.SaveManager.shipping_check();
}
catch (Exception err)
{
Monitor.Log(err.ToString());
}
}
//done
private void Warp_Check(object sender, EventArgs e)
{
try
{
string name = Game1.player.name;
SaveAnywhere.player_path = Path.Combine(SaveAnywhere.mod_path, "Save_Data", name);
if (!Directory.Exists(SaveAnywhere.player_path))
{
//Log.AsyncM(Save_Anywhere_V2.Mod_Core.player_path);
//Log.AsyncC("WOOPS");
return;
}
// Log.AsyncY(Player_Utilities.has_player_warped_yet);
if (this.SaveManager.has_player_warped_yet == false && Game1.player.isMoving() == true)
{
//Log.AsyncM("Ok Good");
this.SaveManager.warp_player();
this.SaveManager.load_animal_info();
this.SaveManager.Load_NPC_Info();
this.SaveManager.has_player_warped_yet = true;
}
}
catch (Exception err)
{
//7Log.AsyncO("THIS DOESNT MAKE SENSE");
Monitor.Log(err.ToString());
}
}
//done
private void PlayerEvents_LoadedGame(object sender, EventArgs e)
{
this.SaveManager = new SaveManager(Game1.player, this.Helper.DirectoryPath, this.Monitor, this.Helper.Reflection);
try
{
this.SaveManager.load_player_info();
this.ConfigUtilities.DataLoader_Settings();
this.ConfigUtilities.MyWritter_Settings();
this.ConfigUtilities.LoadConfig();
this.ConfigUtilities.WriteConfig();
this.SaveManager.LoadPositions();
}
catch (Exception err)
{
@ -736,24 +685,20 @@ namespace Omegasis.SaveAnywhere
public void KeyPressed_Save_Load_Menu(object sender, EventArgsKeyPressed e)
{
if (e.KeyPressed.ToString() == this.ConfigUtilities.key_binding) //if the key is pressed, load my cusom save function
if (e.KeyPressed.ToString() == this.ConfigUtilities.KeyBinding) //if the key is pressed, load my cusom save function
{
if (Game1.activeClickableMenu != null) return;
try
{
this.SaveManager.save_game();
if (Game1.activeClickableMenu == null)
this.SaveManager.SaveGameAndPositions();
}
catch (Exception exe)
catch (Exception ex)
{
SaveAnywhere.thisMonitor.Log(exe.ToString(), LogLevel.Error);
this.Monitor.Log(ex.ToString(), LogLevel.Error);
}
}
}
private Dictionary<int, SchedulePathDescription> parseMasterSchedule(NPC npc, string rawData)
{
string[] array = rawData.Split(new char[]

View File

@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.Xna.Framework;
@ -7,6 +6,8 @@ using StardewModdingAPI;
using StardewValley;
using StardewValley.Characters;
using StardewValley.Menus;
using StardewValley.Monsters;
using SFarmer = StardewValley.Farmer;
namespace Omegasis.SaveAnywhere
{
@ -16,696 +17,340 @@ namespace Omegasis.SaveAnywhere
/*********
** Properties
*********/
/// <summary>The player for which to save data.</summary>
private readonly SFarmer Player;
/// <summary>Simplifies access to game code.</summary>
private readonly IReflectionHelper Reflection;
public bool passiveSave;
public bool should_ship;
public string npc_name;
public int npc_tile_x;
public int npc_tile_y;
public string npc_current_map_name;
public List<List<string>> routesFromLocationToLocation = new List<List<string>>();
public Point npc_point;
/// <summary>Writes messages to the console and log file.</summary>
private readonly IMonitor Monitor;
public string pet_name;
public Character my_pet;
public string pet_map_name;
public int pet_tile_x;
public int pet_tile_y;
public bool is_pet_outside;
public Point pet_point;
/// <summary>The full path to the folder in which to store data for this player.</summary>
private readonly string SavePath;
public int player_x_tile;
public int player_y_tile;
public string players_current_map_name;
public int player_game_time;
public int player_facing_direction;
public bool has_player_warped_yet;
/// <summary>The full path to the folder in which to store animal data for this player.</summary>
private readonly string SaveAnimalsPath;
/// <summary>The full path to the folder in which to store villager data for this player.</summary>
private readonly string SaveVillagersPath;
/// <summary>Whether we should save at the next opportunity.</summary>
private bool WaitingToSave;
/*********
** Public methods
*********/
/// <summary>Construct an instance.</summary>
/// <param name="player">The player for which to save data.</param>
/// <param name="modPath">The full path to the mod folder.</param>
/// <param name="monitor">Writes messages to the console and log file.</param>
/// <param name="reflection">Simplifies access to game code.</param>
public SaveManager(IReflectionHelper reflection)
public SaveManager(SFarmer player, string modPath, IMonitor monitor, IReflectionHelper reflection)
{
// save info
this.Player = player;
this.Monitor = monitor;
this.Reflection = reflection;
// generate paths
this.SavePath = Path.Combine(modPath, "Save_Data", player.name);
this.SaveAnimalsPath = Path.Combine(this.SavePath, "Animals");
this.SaveVillagersPath = Path.Combine(this.SavePath, "NPC_Save_Info");
}
public void shipping_check()
/// <summary>Perform any required update logic.</summary>
public void Update()
{
if (Game1.activeClickableMenu != null) return;
if (this.should_ship)
{
Game1.activeClickableMenu = new NewShippingMenu(Game1.getFarm().shippingBin, this.Reflection);
this.should_ship = false;
Game1.getFarm().shippingBin.Clear();
Game1.getFarm().lastItemShipped = null;
this.passiveSave = true;
}
else
// perform passive save
if (this.WaitingToSave && Game1.activeClickableMenu == null)
{
Game1.activeClickableMenu = new SaveGameMenu();
this.WaitingToSave = false;
}
}
public void save_game()
/// <summary>Save all game data.</summary>
public void SaveGameAndPositions()
{
/*
if (Game1.player.currentLocation.name == "Sewer")
// save game data
Farm farm = Game1.getFarm();
if (farm.shippingBin.Any())
{
Log.Error("There is an issue saving in the Sewer. Blame the animals for not being saved to the player's save file.");
Log.Error("Your data has not been saved. Sorry for the issue.");
return;
Game1.activeClickableMenu = new NewShippingMenu(farm.shippingBin, this.Reflection);
farm.shippingBin.Clear();
farm.lastItemShipped = null;
this.WaitingToSave = true;
}
*/
//if a player has shipped an item, run this code.
if (Enumerable.Count<Item>((IEnumerable<Item>)Game1.getFarm().shippingBin) > 0)
{
this.should_ship = true;
// Game1.endOfNightMenus.Push((IClickableMenu)new ShippingMenu(Game1.getFarm().shippingBin));
// Game1.showEndOfNightStuff(); //shows the nightly shipping menu.
// Game1.getFarm().shippingBin.Clear(); //clears out the shipping bin to prevent exploits
}
try
{
this.shipping_check();
// Game1.activeClickableMenu = new StardewValley.Menus.SaveGameMenu();
}
catch (Exception rrr)
{
Game1.showRedMessage("Can't save here. See log for error.");
SaveAnywhere.thisMonitor.Log(rrr.ToString(), LogLevel.Error);
}
// Game1.activeClickableMenu = new StardewValley.Menus.SaveGameMenu(); //This command is what allows the player to save anywhere as it calls the saving function.
this.save_player_info();
this.save_animal_info();
this.Save_NPC_Info();
//grab the player's info
// player_map_name = StardewValley.Game1.player.currentLocation.name;
// player_tile_x = StardewValley.Game1.player.getTileX();
// player_tile_Y = StardewValley.Game1.player.getTileY();
// player_flop = false;
// MyWritter_Player(); //write my info to a text file
// MyWritter_Horse();
// DataLoader_Settings(); //load settings. Prevents acidental overwrite.
// MyWritter_Settings(); //save settings.
//Game1.warpFarmer(player_map_name, player_tile_x, player_tile_Y, player_flop); //refresh the player's location just incase. That will prove that they character's info was valid.
//so this is essentially the basics of the code...
// Log.Error("IS THIS BREAKING?");
}
public void Save_Horse_Info()
{
Horse horse = Utility.findHorse();
if (horse == null)
{
//Game1.getFarm().characters.Add((NPC)new Horse(this.player_tile_x + 1, this.player_tile_Y + 1));
SaveAnywhere.thisMonitor.Log("NEIGH: No horse exists", LogLevel.Debug);
return;
}
// else
// Game1.warpCharacter((NPC)horse, Game1.player.currentLocation.name, StardewValley.Game1.player.getTileLocationPoint(), false, true);
string myname = Game1.player.name;
string mylocation = Path.Combine(SaveAnywhere.animal_path, "Horse_Save_Info_");
string mylocation2 = mylocation + myname;
string mylocation3 = mylocation2 + ".txt";
string[] mystring3 = new string[20];
if (!File.Exists(mylocation3))
{
SaveAnywhere.thisMonitor.Log("The horse save info doesn't exist. It will be created when the custom saving method is run. Which is now.", LogLevel.Debug);
//write out the info to a text file at the end of a day. This will run if it doesnt exist.
mystring3[0] = "Horse: Save_Anywhere Info. Editing this might break some things.";
mystring3[1] = "====================================================================================";
mystring3[2] = "Horse Current Map Name";
mystring3[3] = horse.currentLocation.name.ToString();
mystring3[4] = "Horse X Position";
mystring3[5] = horse.getTileX().ToString();
mystring3[6] = "Horse Y Position";
mystring3[7] = horse.getTileY().ToString();
File.WriteAllLines(mylocation3, mystring3);
}
else
{
// Console.WriteLine("The custom character save info doesn't exist. It will be created when the custom saving method is run. Which is now.");
//write out the info to a text file at the end of a day. This will run if it doesnt exist.
Game1.activeClickableMenu = new SaveGameMenu();
mystring3[0] = "Horse: Save_Anywhere Info. Editing this might break some things.";
mystring3[1] = "====================================================================================";
mystring3[2] = "Horse Current Map Name";
mystring3[3] = horse.currentLocation.name.ToString();
mystring3[4] = "Horse X Position";
mystring3[5] = horse.getTileX().ToString();
mystring3[6] = "Horse Y Position";
mystring3[7] = horse.getTileY().ToString();
File.WriteAllLines(mylocation3, mystring3);
}
// save custom data
Directory.CreateDirectory(this.SaveAnimalsPath);
Directory.CreateDirectory(this.SaveVillagersPath);
this.SavePlayerPosition();
this.SaveHorsePosition();
this.SavePetPosition();
this.SaveVillagerPositions();
}
public void Load_Horse_Info()
/// <summary>Load all game data.</summary>
public void LoadPositions()
{
if (!this.HasSaveData())
return;
this.LoadPlayerPosition();
this.LoadHorsePosition();
this.LoadPetPosition();
this.LoadVillagerPositions();
}
/*********
** Private methods
*********/
/// <summary>Save the horse state to the save file.</summary>
private void SaveHorsePosition()
{
// find horse
Horse horse = Utility.findHorse();
if (horse == null)
{
SaveAnywhere.thisMonitor.Log("NEIGH: No horse exists", LogLevel.Debug);
return;
}
// DataLoader_Settings();
//loads the data to the variables upon loading the game.
string myname = Game1.player.name;
string mylocation = Path.Combine(SaveAnywhere.animal_path, "Horse_Save_Info_");
string mylocation2 = mylocation + myname;
string mylocation3 = mylocation2 + ".txt";
if (!File.Exists(mylocation3)) //if not data.json exists, initialize the data variables to the ModConfig data. I.E. starting out.
{
}
else
// get horse info
string map = horse.currentLocation.name;
Point tile = horse.getTileLocationPoint();
// save data
string path = Path.Combine(this.SaveAnimalsPath, $"Horse_Save_Info_{this.Player.name}.txt");
string[] text = new string[20];
text[0] = "Horse: Save_Anywhere Info. Editing this might break some things.";
text[1] = "====================================================================================";
text[2] = "Horse Current Map Name";
text[3] = map;
text[4] = "Horse X Position";
text[5] = tile.X.ToString();
text[6] = "Horse Y Position";
text[7] = tile.Y.ToString();
File.WriteAllLines(path, text);
}
/// <summary>Reset the horse to the saved state.</summary>
private void LoadHorsePosition()
{
// find horse
Horse horse = Utility.findHorse();
if (horse == null)
return;
// get file path
string path = Path.Combine(this.SaveAnimalsPath, $"Horse_Save_Info_{this.Player.name}.txt");
if (!File.Exists(path))
return;
// read saved data
string[] text = File.ReadAllLines(path);
string map = Convert.ToString(text[3]);
int x = Convert.ToInt32(text[5]);
int y = Convert.ToInt32(text[7]);
// update horse
Game1.warpCharacter(horse, map, new Point(x, y), false, true);
}
/// <summary>Save the villager states to the save file.</summary>
private void SaveVillagerPositions()
{
foreach (NPC npc in Utility.getAllCharacters())
{
string horse_map_name = "";
int horse_x;
int horse_y;
Point horse_point;
string[] readtext = File.ReadAllLines(mylocation3);
horse_map_name = Convert.ToString(readtext[3]);
horse_x = Convert.ToInt32(readtext[5]);
horse_y = Convert.ToInt32(readtext[7]);
horse_point.X = horse_x;
horse_point.Y = horse_y;
Game1.warpCharacter((NPC)horse, horse_map_name, horse_point, false, true);
// ignore non-villagers
if (npc is Pet || npc is Monster)
continue;
// get NPC data
string name = npc.name;
string map = npc.currentLocation.name;
Point tile = npc.getTileLocationPoint();
// save data
string path = Path.Combine(this.SaveVillagersPath, npc.name + ".txt");
string[] text = new string[20];
text[0] = "NPC: Save_Anywhere Info. Editing this might break some things.";
text[1] = "====================================================================================";
text[2] = "NPC Name";
text[3] = name;
text[4] = "NPC Current Map Name";
text[5] = map;
text[6] = "NPC X Position";
text[7] = tile.X.ToString();
text[8] = "NPC Y Position";
text[9] = tile.Y.ToString();
File.WriteAllLines(path, text);
}
}
public void save_animal_info()
/// <summary>Reset the villagers to their saved state.</summary>
private void LoadVillagerPositions()
{
SaveAnywhere.animal_path = Path.Combine(SaveAnywhere.player_path, "Animals");
if (!Directory.Exists(SaveAnywhere.animal_path))
foreach (NPC npc in Utility.getAllCharacters())
{
Directory.CreateDirectory(SaveAnywhere.animal_path);
// ignore non-villagers
if (npc is Pet || npc is Monster)
continue;
}
this.Save_Horse_Info();
this.save_pet_info();
}
public void load_animal_info()
{
SaveAnywhere.animal_path = Path.Combine(SaveAnywhere.player_path, "Animals");
if (!Directory.Exists(SaveAnywhere.animal_path))
{
Directory.CreateDirectory(SaveAnywhere.animal_path);
}
this.Load_Horse_Info();
this.Load_pet_Info();
}
public void Save_NPC_Info()
{
SaveAnywhere.npc_path = Path.Combine(SaveAnywhere.player_path, "NPC_Save_Info");
if (!Directory.Exists(SaveAnywhere.npc_path))
{
Directory.CreateDirectory(SaveAnywhere.npc_path);
}
foreach (var location in Game1.locations)
{
foreach (var npc in location.characters)
// get file path
string path = Path.Combine(this.SaveVillagersPath, npc.name + ".txt");
if (!File.Exists(path))
{
if (npc.IsMonster == true) continue;
if (npc is StardewValley.Monsters.Bat || npc is StardewValley.Monsters.BigSlime || npc is StardewValley.Monsters.Bug || npc is StardewValley.Monsters.Cat || npc is StardewValley.Monsters.Crow || npc is StardewValley.Monsters.Duggy || npc is StardewValley.Monsters.DustSpirit || npc is StardewValley.Monsters.Fireball || npc is StardewValley.Monsters.Fly || npc is StardewValley.Monsters.Ghost || npc is StardewValley.Monsters.GoblinPeasant || npc is StardewValley.Monsters.GoblinWizard || npc is StardewValley.Monsters.GreenSlime || npc is StardewValley.Monsters.Grub || npc is StardewValley.Monsters.LavaCrab || npc is StardewValley.Monsters.MetalHead || npc is StardewValley.Monsters.Monster || npc is StardewValley.Monsters.Mummy || npc is StardewValley.Monsters.RockCrab || npc is StardewValley.Monsters.RockGolem || npc is StardewValley.Monsters.Serpent || npc is StardewValley.Monsters.ShadowBrute || npc is StardewValley.Monsters.ShadowGirl || npc is StardewValley.Monsters.ShadowGuy || npc is StardewValley.Monsters.ShadowShaman || npc is StardewValley.Monsters.Skeleton || npc is StardewValley.Monsters.SkeletonMage || npc is StardewValley.Monsters.SkeletonWarrior || npc is StardewValley.Monsters.Spiker || npc is StardewValley.Monsters.SquidKid) continue;
this.npc_name = npc.name;
this.npc_current_map_name = location.name;
this.npc_tile_x = npc.getTileX();
this.npc_tile_y = npc.getTileY();
string mylocation = Path.Combine(SaveAnywhere.npc_path, npc.name);
string mylocation2 = mylocation;
string mylocation3 = mylocation2 + ".txt";
string[] mystring3 = new string[20];
if (!File.Exists(mylocation3))
{
SaveAnywhere.thisMonitor.Log("Save Anywhere: The NPC save info for " + this.npc_name + " doesn't exist. It will be created when the custom saving method is run. Which is now.");
//write out the info to a text file at the end of a day. This will run if it doesnt exist.
mystring3[0] = "NPC: Save_Anywhere Info. Editing this might break some things.";
mystring3[1] = "====================================================================================";
mystring3[2] = "NPC Name";
mystring3[3] = this.npc_name.ToString();
mystring3[4] = "NPC Current Map Name";
mystring3[5] = this.npc_current_map_name.ToString();
mystring3[6] = "NPC X Position";
mystring3[7] = this.npc_tile_x.ToString();
mystring3[8] = "NPC Y Position";
mystring3[9] = this.npc_tile_y.ToString();
File.WriteAllLines(mylocation3, mystring3);
}
else
{
// Console.WriteLine("The custom character save info doesn't exist. It will be created when the custom saving method is run. Which is now.");
//write out the info to a text file at the end of a day. This will run if it doesnt exist.
mystring3[0] = "NPC: Save_Anywhere Info. Editing this might break some things.";
mystring3[1] = "====================================================================================";
mystring3[2] = "NPC Current Map Name";
mystring3[3] = this.npc_name.ToString();
mystring3[4] = "NPC Current Map Name";
mystring3[5] = this.npc_current_map_name.ToString();
mystring3[6] = "NPC X Position";
mystring3[7] = this.npc_tile_x.ToString();
mystring3[8] = "NPC Y Position";
mystring3[9] = this.npc_tile_y.ToString();
File.WriteAllLines(mylocation3, mystring3);
}
}
}
}
public void Load_NPC_Info()
{
List<NPC> npc_list = new List<NPC>();
foreach (var location in Game1.locations)
{
foreach (var npc in location.characters)
{
if (npc.IsMonster == true) continue;
if (npc is StardewValley.Monsters.Bat || npc is StardewValley.Monsters.BigSlime || npc is StardewValley.Monsters.Bug || npc is StardewValley.Monsters.Cat || npc is StardewValley.Monsters.Crow || npc is StardewValley.Monsters.Duggy || npc is StardewValley.Monsters.DustSpirit || npc is StardewValley.Monsters.Fireball || npc is StardewValley.Monsters.Fly || npc is StardewValley.Monsters.Ghost || npc is StardewValley.Monsters.GoblinPeasant || npc is StardewValley.Monsters.GoblinWizard || npc is StardewValley.Monsters.GreenSlime || npc is StardewValley.Monsters.Grub || npc is StardewValley.Monsters.LavaCrab || npc is StardewValley.Monsters.MetalHead || npc is StardewValley.Monsters.Monster || npc is StardewValley.Monsters.Mummy || npc is StardewValley.Monsters.RockCrab || npc is StardewValley.Monsters.RockGolem || npc is StardewValley.Monsters.Serpent || npc is StardewValley.Monsters.ShadowBrute || npc is StardewValley.Monsters.ShadowGirl || npc is StardewValley.Monsters.ShadowGuy || npc is StardewValley.Monsters.ShadowShaman || npc is StardewValley.Monsters.Skeleton || npc is StardewValley.Monsters.SkeletonMage || npc is StardewValley.Monsters.SkeletonWarrior || npc is StardewValley.Monsters.Spiker || npc is StardewValley.Monsters.SquidKid) continue;
if (npc is NPC || npc is Cat || npc is Dog) npc_list.Add(npc);
}
}
foreach (var npc in npc_list)
{
if (npc.IsMonster == true) continue;
if (npc is StardewValley.Monsters.Bat || npc is StardewValley.Monsters.BigSlime || npc is StardewValley.Monsters.Bug || npc is StardewValley.Monsters.Cat || npc is StardewValley.Monsters.Crow || npc is StardewValley.Monsters.Duggy || npc is StardewValley.Monsters.DustSpirit || npc is StardewValley.Monsters.Fireball || npc is StardewValley.Monsters.Fly || npc is StardewValley.Monsters.Ghost || npc is StardewValley.Monsters.GoblinPeasant || npc is StardewValley.Monsters.GoblinWizard || npc is StardewValley.Monsters.GreenSlime || npc is StardewValley.Monsters.Grub || npc is StardewValley.Monsters.LavaCrab || npc is StardewValley.Monsters.MetalHead || npc is StardewValley.Monsters.Monster || npc is StardewValley.Monsters.Mummy || npc is StardewValley.Monsters.RockCrab || npc is StardewValley.Monsters.RockGolem || npc is StardewValley.Monsters.Serpent || npc is StardewValley.Monsters.ShadowBrute || npc is StardewValley.Monsters.ShadowGirl || npc is StardewValley.Monsters.ShadowGuy || npc is StardewValley.Monsters.ShadowShaman || npc is StardewValley.Monsters.Skeleton || npc is StardewValley.Monsters.SkeletonMage || npc is StardewValley.Monsters.SkeletonWarrior || npc is StardewValley.Monsters.Spiker || npc is StardewValley.Monsters.SquidKid) continue;
SaveAnywhere.npc_path = Path.Combine(SaveAnywhere.player_path, "NPC_Save_Info");
if (!Directory.Exists(SaveAnywhere.npc_path))
{
Directory.CreateDirectory(SaveAnywhere.npc_path);
}
string mylocation = Path.Combine(SaveAnywhere.npc_path, npc.name);
string mylocation2 = mylocation;
string mylocation3 = mylocation2 + ".txt";
string[] mystring3 = new string[20];
if (!File.Exists(mylocation3))
{
SaveAnywhere.thisMonitor.Log("Missing character file?!?", LogLevel.Error);
this.Monitor.Log($"No save data for {npc.name} villager, skipping.", LogLevel.Error);
continue;
}
else
{
string[] readtext = File.ReadAllLines(mylocation3);
this.npc_name = Convert.ToString(readtext[3]);
this.npc_current_map_name = Convert.ToString(readtext[5]);
this.npc_tile_x = Convert.ToInt32(readtext[7]);
this.npc_tile_y = Convert.ToInt32(readtext[9]);
this.npc_point = new Point();
this.npc_point.X = this.npc_tile_x;
this.npc_point.Y = this.npc_tile_y;
if (this.npc_current_map_name == "" || this.npc_current_map_name == null) continue;
//Log.Info("Warped NPC" +npc_name);
Game1.warpCharacter((NPC)npc, this.npc_current_map_name, this.npc_point, false, true);
// read data
string[] text = File.ReadAllLines(path);
string map = Convert.ToString(text[5]);
int x = Convert.ToInt32(text[7]);
int y = Convert.ToInt32(text[9]);
if (string.IsNullOrEmpty(map))
continue;
// npc.updateMovement(Game1.getLocationFromName(npc_current_map_name), Game1.currentGameTime);
//npc.moveCharacterOnSchedulePath();
// npc.dayUpdate(Game1.dayOfMonth);
//npc_update(npc, Game1.dayOfMonth);
// npc.DirectionsToNewLocation = pathfindToNextScheduleLocation(npc, npc.currentLocation.name, npc.getTileX(), npc.getTileY(), npc.currentLocation.name, 52, 99, 3, "", "");
// npc.updateMovement(npc.currentLocation,Game1.currentGameTime);
//npc.Schedule = npc.getSchedule(Game1.dayOfMonth);
//npc.moveCharacterOnSchedulePath();
}
// update NPC
Game1.warpCharacter(npc, map, new Point(x, y), false, true);
}
SaveAnywhere.npc_warp = true;
}
private Stack<Point> addToStackForSchedule(Stack<Point> original, Stack<Point> toAdd)
{
if (toAdd == null)
return original;
original = new Stack<Point>((IEnumerable<Point>)original);
while (original.Count > 0)
toAdd.Push(original.Pop());
return toAdd;
}
private List<string> getLocationRoute(NPC npc, string startingLocation, string endingLocation)
{
foreach (List<string> list in this.routesFromLocationToLocation)
{
if (Enumerable.First<string>((IEnumerable<string>)list).Equals(startingLocation) && Enumerable.Last<string>((IEnumerable<string>)list).Equals(endingLocation) && (npc.gender == 0 || !list.Contains("BathHouse_MensLocker")) && (npc.gender != 0 || !list.Contains("BathHouse_WomensLocker")))
return list;
}
return (List<string>)null;
}
private SchedulePathDescription pathfindToNextScheduleLocation(NPC npc, string startingLocation, int startingX, int startingY, string endingLocation, int endingX, int endingY, int finalFacingDirection, string endBehavior, string endMessage)
{
Stack<Point> stack = new Stack<Point>();
Point startPoint = new Point(startingX, startingY);
List<string> list = startingLocation.Equals(endingLocation) ? (List<string>)null : this.getLocationRoute(npc, startingLocation, endingLocation);
if (list != null)
{
for (int index = 0; index < Enumerable.Count<string>((IEnumerable<string>)list); ++index)
{
GameLocation locationFromName = Game1.getLocationFromName(list[index]);
if (index < Enumerable.Count<string>((IEnumerable<string>)list) - 1)
{
Point warpPointTo = locationFromName.getWarpPointTo(list[index + 1]);
if (warpPointTo.Equals(Point.Zero) || startPoint.Equals(Point.Zero))
throw new Exception("schedule pathing tried to find a warp point that doesn't exist.");
stack = this.addToStackForSchedule(stack, PathFindController.findPathForNPCSchedules(startPoint, warpPointTo, locationFromName, 30000));
startPoint = locationFromName.getWarpPointTarget(warpPointTo);
}
else
stack = this.addToStackForSchedule(stack, PathFindController.findPathForNPCSchedules(startPoint, new Point(endingX, endingY), locationFromName, 30000));
}
}
else if (startingLocation.Equals(endingLocation))
stack = PathFindController.findPathForNPCSchedules(startPoint, new Point(endingX, endingY), Game1.getLocationFromName(startingLocation), 30000);
return new SchedulePathDescription(stack, finalFacingDirection, endBehavior, endMessage);
/// <summary>Save the pet state to the save file.</summary>
private void SavePetPosition()
{
if (!this.Player.hasPet())
return;
// find pet
Pet pet = Utility.getAllCharacters().OfType<Pet>().FirstOrDefault();
if (pet == null)
return;
// get pet info
string map = pet.currentLocation.name;
Point tile = pet.getTileLocationPoint();
// save data
string path = Path.Combine(this.SaveAnimalsPath, $"Pet_Save_Info_{this.Player.name}.txt");
string[] text = new string[20];
text[0] = "Pet: Save_Anywhere Info. Editing this might break some things.";
text[1] = "====================================================================================";
text[2] = "Pet Current Map Name";
text[3] = map;
text[4] = "Pet X Position";
text[5] = tile.X.ToString();
text[6] = "Pet Y Position";
text[7] = tile.Y.ToString();
File.WriteAllLines(path, text);
}
public void save_pet_info()
/// <summary>Reset the pet to the saved state.</summary>
private void LoadPetPosition()
{
if (Game1.player.hasPet() == false) return;
this.pet_name = Game1.player.getPetName();
foreach (var location in Game1.locations)
{
foreach (var npc in location.characters)
{
if (npc is Dog || npc is Cat)
{
this.pet_map_name = location.name;
this.pet_tile_x = npc.getTileX();
this.pet_tile_y = npc.getTileY();
this.is_pet_outside = location.isOutdoors;
if (!this.Player.hasPet())
return;
}
// find pet
Pet pet = Utility.getAllCharacters().OfType<Pet>().FirstOrDefault();
if (pet == null)
return;
}
// get file path
string path = Path.Combine(this.SaveAnimalsPath, $"Pet_Save_Info_{this.Player.name}.txt");
if (!File.Exists(path))
return;
}
string myname = Game1.player.name;
string mylocation = Path.Combine(SaveAnywhere.animal_path, "Pet_Save_Info_");
string mylocation2 = mylocation + myname;
string mylocation3 = mylocation2 + ".txt";
string[] mystring3 = new string[20];
if (!File.Exists(mylocation3))
{
SaveAnywhere.thisMonitor.Log("Save Anywhere: The pet save info doesn't exist. It will be created when the custom saving method is run. Which is now.", LogLevel.Debug);
//write out the info to a text file at the end of a day. This will run if it doesnt exist.
mystring3[0] = "Pet: Save_Anywhere Info. Editing this might break some things.";
mystring3[1] = "====================================================================================";
mystring3[2] = "Pet Current Map Name";
mystring3[3] = this.pet_map_name.ToString();
mystring3[4] = "Pet X Position";
mystring3[5] = this.pet_tile_x.ToString();
mystring3[6] = "Pet Y Position";
mystring3[7] = this.pet_tile_y.ToString();
File.WriteAllLines(mylocation3, mystring3);
}
else
{
// Console.WriteLine("The custom character save info doesn't exist. It will be created when the custom saving method is run. Which is now.");
//write out the info to a text file at the end of a day. This will run if it doesnt exist.
mystring3[0] = "Pet: Save_Anywhere Info. Editing this might break some things.";
mystring3[1] = "====================================================================================";
mystring3[2] = "Pet Current Map Name";
mystring3[3] = this.pet_map_name.ToString();
mystring3[4] = "Pet X Position";
mystring3[5] = this.pet_tile_x.ToString();
mystring3[6] = "Pet Y Position";
mystring3[7] = this.pet_tile_y.ToString();
File.WriteAllLines(mylocation3, mystring3);
}
// read saved data
string[] text = File.ReadAllLines(path);
string map = Convert.ToString(text[3]);
int x = Convert.ToInt32(text[5]);
int y = Convert.ToInt32(text[7]);
// update pet
Game1.warpCharacter(pet, map, new Point(x, y), false, true);
}
public void Load_pet_Info()
/// <summary>Save the player state to the save file.</summary>
private void SavePlayerPosition()
{
if (Game1.player.hasPet() == false) return;
// DataLoader_Settings();
//loads the data to the variables upon loading the game.
string myname = Game1.player.name;
string mylocation = Path.Combine(SaveAnywhere.animal_path, "Pet_Save_Info_");
string mylocation2 = mylocation + myname;
string mylocation3 = mylocation2 + ".txt";
if (!File.Exists(mylocation3)) //if not data.json exists, initialize the data variables to the ModConfig data. I.E. starting out.
{
}
// get player info
string map = this.Player.currentLocation.name;
Point tile = this.Player.getTileLocationPoint();
else
{
string[] readtext = File.ReadAllLines(mylocation3);
this.pet_map_name = Convert.ToString(readtext[3]);
this.pet_tile_x = Convert.ToInt32(readtext[5]);
this.pet_tile_y = Convert.ToInt32(readtext[7]);
this.get_pet();
this.pet_point = new Point();
this.pet_point.X = this.pet_tile_x;
this.pet_point.Y = this.pet_tile_y;
Game1.warpCharacter((NPC)this.my_pet, this.pet_map_name, this.pet_point, false, true);
}
// save data
string path = Path.Combine(this.SavePath, $"Player_Save_Info_{this.Player.name}.txt");
string[] text = new string[20];
text[0] = "Player: Save_Anywhere Info. Editing this might break some things.";
text[1] = "====================================================================================";
text[2] = "Player Current Game Time";
text[3] = Game1.timeOfDay.ToString();
text[4] = "Player Current Map Name";
text[5] = map;
text[6] = "Player X Position";
text[7] = tile.X.ToString();
text[8] = "Player Y Position";
text[9] = tile.Y.ToString();
File.WriteAllLines(path, text);
}
public void get_pet()
/// <summary>Reset the player to the saved state.</summary>
private void LoadPlayerPosition()
{
if (Game1.player.hasPet() == false) return;
foreach (var location in Game1.locations)
{
foreach (var npc in location.characters)
{
if (npc is Dog || npc is Cat)
{
this.pet_map_name = location.name;
this.pet_tile_x = npc.getTileX();
this.pet_tile_y = npc.getTileY();
this.is_pet_outside = location.isOutdoors;
this.my_pet = npc;
}
// get file path
string path = Path.Combine(this.SavePath, $"Player_Save_Info_{this.Player.name}.txt");
if (!File.Exists(path))
return;
}
// read saved data
string[] text = File.ReadAllLines(path);
int time = Convert.ToInt32(text[3]);
string map = Convert.ToString(text[5]);
int x = Convert.ToInt32(text[7]);
int y = Convert.ToInt32(text[9]);
}
// update player
Game1.timeOfDay = time;
}
public void get_player_info()
{
this.get_x();
this.get_y();
this.get_current_map_name();
this.get_facing_direction();
}
public void get_x()
{
this.player_x_tile = Game1.player.getTileX();
}
public void get_y()
{
this.player_y_tile = Game1.player.getTileY();
}
public void get_current_map_name()
{
this.players_current_map_name = Game1.player.currentLocation.name;
}
public void get_facing_direction()
{
this.player_facing_direction = Game1.player.facingDirection;
}
public void save_player_info()
{
this.get_player_info();
string name = Game1.player.name;
SaveAnywhere.player_path = Path.Combine(SaveAnywhere.mod_path, "Save_Data", name);
if (!Directory.Exists(SaveAnywhere.player_path))
{
Directory.CreateDirectory(SaveAnywhere.player_path);
}
string mylocation = Path.Combine(SaveAnywhere.player_path, "Player_Save_Info_");
string mylocation2 = mylocation + name;
string mylocation3 = mylocation2 + ".txt";
string[] mystring3 = new string[20];
if (!File.Exists(mylocation3))
{
SaveAnywhere.thisMonitor.Log("Save Anywhere: The custom character save info doesn't exist. It will be created when the custom saving method is run. Which is now.", LogLevel.Info);
//write out the info to a text file at the end of a day. This will run if it doesnt exist.
mystring3[0] = "Player: Save_Anywhere Info. Editing this might break some things.";
mystring3[1] = "====================================================================================";
mystring3[2] = "Player Current Game Time";
mystring3[3] = Game1.timeOfDay.ToString();
mystring3[4] = "Player Current Map Name";
mystring3[5] = this.players_current_map_name.ToString();
mystring3[6] = "Player X Position";
mystring3[7] = this.player_x_tile.ToString();
mystring3[8] = "Player Y Position";
mystring3[9] = this.player_y_tile.ToString();
File.WriteAllLines(mylocation3, mystring3);
}
else
{
//write out the info to a text file at the end of a day. This will run if it doesnt exist.
mystring3[0] = "Player: Save_Anywhere Info. Editing this might break some things.";
mystring3[1] = "====================================================================================";
mystring3[2] = "Player Current Game Time";
mystring3[3] = Game1.timeOfDay.ToString();
mystring3[4] = "Player Current Map Name";
mystring3[5] = this.players_current_map_name.ToString();
mystring3[6] = "Player X Position";
mystring3[7] = this.player_x_tile.ToString();
mystring3[8] = "Player Y Position";
mystring3[9] = this.player_y_tile.ToString();
File.WriteAllLines(mylocation3, mystring3);
}
}
public void load_player_info()
{
string name = Game1.player.name;
SaveAnywhere.player_path = Path.Combine(SaveAnywhere.mod_path, "Save_Data", name);
if (!Directory.Exists(SaveAnywhere.player_path))
{
Directory.CreateDirectory(SaveAnywhere.player_path);
}
string mylocation = Path.Combine(SaveAnywhere.player_path, "Player_Save_Info_");
string mylocation2 = mylocation + name;
string mylocation3 = mylocation2 + ".txt";
string[] mystring3 = new string[20];
if (!File.Exists(mylocation3)) //if not data.json exists, initialize the data variables to the ModConfig data. I.E. starting out.
{
// Console.WriteLine("Can't load custom save info since the file doesn't exist.");
this.has_player_warped_yet = true;
}
else
{
// Console.WriteLine("HEY THERE IM LOADING DATA");
string[] readtext = File.ReadAllLines(mylocation3);
this.player_game_time = Convert.ToInt32(readtext[3]);
this.players_current_map_name = Convert.ToString(readtext[5]);
this.player_x_tile = Convert.ToInt32(readtext[7]);
this.player_y_tile = Convert.ToInt32(readtext[9]);
Game1.timeOfDay = this.player_game_time;
}
}
public void warp_player()
{
GameLocation new_location = Game1.getLocationFromName(this.players_current_map_name);
Game1.player.previousLocationName = Game1.player.currentLocation.name;
Game1.locationAfterWarp = new_location;
Game1.xLocationAfterWarp = this.player_x_tile;
Game1.yLocationAfterWarp = this.player_y_tile;
Game1.facingDirectionAfterWarp = this.player_facing_direction;
this.Player.previousLocationName = this.Player.currentLocation.name;
Game1.locationAfterWarp = Game1.getLocationFromName(map);
Game1.xLocationAfterWarp = x;
Game1.yLocationAfterWarp = y;
//Game1.facingDirectionAfterWarp = this.player_facing_direction;
Game1.fadeScreenToBlack();
Game1.warpFarmer(map, x, y, false);
//this.Player.faceDirection(this.player_facing_direction);
}
Game1.warpFarmer(this.players_current_map_name, this.player_x_tile, this.player_y_tile, false);
SaveAnywhere.thisMonitor.Log("WARP THE PLAYER");
Game1.player.faceDirection(this.player_facing_direction);
if (Directory.Exists(SaveAnywhere.player_path))
{
// Directory.Delete(player_path, true);
}
/// <summary>Get whether any data has been saved for this player yet.</summary>
private bool HasSaveData()
{
return Directory.Exists(this.SavePath);
}
}
}