migrate Save Anywhere to SMAPI's config API

This commit is contained in:
Jesse Plamondon-Willard 2017-08-05 23:20:48 -04:00
parent c980d587a8
commit fad849f800
5 changed files with 16 additions and 72 deletions

View File

@ -1,61 +0,0 @@
using System;
using System.IO;
namespace Omegasis.SaveAnywhere.Framework
{
/// <summary>Provides methods for reading and writing the config file.</summary>
internal class ConfigUtilities
{
/*********
** Properties
*********/
/// <summary>The full path to the mod folder.</summary>
private readonly string ModPath;
/*********
** 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)
{
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[] text = File.ReadAllLines(path);
this.KeyBinding = Convert.ToString(text[3]);
}
}
/// <summary>Save the configuration settings.</summary>
public void WriteConfig()
{
string path = Path.Combine(this.ModPath, "Save_Anywhere_Config.txt");
string[] text = new string[20];
text[0] = "Config: Save_Anywhere Info. Feel free to mess with these settings.";
text[1] = "====================================================================================";
text[2] = "Key binding for saving anywhere. Press this key to save anywhere!";
text[3] = this.KeyBinding;
File.WriteAllLines(path, text);
}
}
}

View File

@ -0,0 +1,9 @@
namespace Omegasis.SaveAnywhere.Framework
{
/// <summary>The mod configuration.</summary>
internal class ModConfig
{
/// <summary>The key which initiates a save.</summary>
public string SaveKey { get; set; } = "K";
}
}

View File

@ -38,6 +38,6 @@ Press `K` to save anywhere. Edit `Save_Anywhere_Config.txt` to configure the key
2.5:
* Updated for SMAPI 2.0.
* Overhauled save format.
* Fixed player/NPC facing direction not being restored.
* Switched to standard JSON config file.
* Fixed issue where reloading after a sleep save restored some data from your last non-sleep save.
* Internal refactoring.

View File

@ -15,12 +15,12 @@ namespace Omegasis.SaveAnywhere
/*********
** Properties
*********/
/// <summary>The mod configuration.</summary>
private ModConfig Config;
/// <summary>Provides methods for saving and loading game data.</summary>
private SaveManager SaveManager;
/// <summary>Provides methods for reading and writing the config file.</summary>
private ConfigUtilities ConfigUtilities;
/// <summary>The parsed schedules by NPC name.</summary>
private readonly IDictionary<string, string> NpcSchedules = new Dictionary<string, string>();
@ -38,7 +38,7 @@ namespace Omegasis.SaveAnywhere
/// <param name="helper">Provides simplified APIs for writing mods.</param>
public override void Entry(IModHelper helper)
{
this.ConfigUtilities = new ConfigUtilities(this.Helper.DirectoryPath);
this.Config = helper.ReadConfig<ModConfig>();
SaveEvents.AfterLoad += this.SaveEvents_AfterLoad;
SaveEvents.AfterSave += this.SaveEvents_AfterSave;
@ -61,10 +61,6 @@ namespace Omegasis.SaveAnywhere
this.IsCustomSaving = false;
this.ShouldResetSchedules = false;
// load config
this.ConfigUtilities.LoadConfig();
this.ConfigUtilities.WriteConfig();
// load positions
this.SaveManager = new SaveManager(this.Helper, this.Helper.Reflection, onLoaded: () => this.ShouldResetSchedules = true);
this.SaveManager.LoadData();
@ -130,7 +126,7 @@ namespace Omegasis.SaveAnywhere
if (!Context.IsPlayerFree)
return;
if (e.KeyPressed.ToString() == this.ConfigUtilities.KeyBinding)
if (e.KeyPressed.ToString() == this.Config.SaveKey)
this.SaveManager.BeginSaveData();
}

View File

@ -37,12 +37,12 @@
<Compile Include="..\GlobalAssemblyInfo.cs">
<Link>Properties\GlobalAssemblyInfo.cs</Link>
</Compile>
<Compile Include="Framework\ModConfig.cs" />
<Compile Include="Framework\Models\CharacterType.cs" />
<Compile Include="Framework\Models\PositionData.cs" />
<Compile Include="Framework\NewSaveMenu.cs" />
<Compile Include="Framework\Models\PlayerData.cs" />
<Compile Include="Framework\SaveManager.cs" />
<Compile Include="Framework\ConfigUtilities.cs" />
<Compile Include="SaveAnywhere.cs" />
<Compile Include="Framework\NewShippingMenu.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />