From 9257605595c8e6a172a9db3087ca0ba8b3e4822d Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sat, 5 Aug 2017 15:49:27 -0400 Subject: [PATCH] replace manual JSON parsing with SMAPI's config API --- GeneralMods/BuildEndurance/BuildEndurance.cs | 34 ++----------------- .../BuildEndurance/BuildEndurance.csproj | 4 --- .../BuildEndurance/Framework/ModConfig.cs | 18 +++++----- GeneralMods/BuildEndurance/packages.config | 1 - GeneralMods/BuildHealth/BuildHealth.cs | 30 +--------------- GeneralMods/BuildHealth/BuildHealth.csproj | 4 --- .../BuildHealth/Framework/ModConfig.cs | 14 ++++---- GeneralMods/BuildHealth/packages.config | 1 - 8 files changed, 19 insertions(+), 87 deletions(-) diff --git a/GeneralMods/BuildEndurance/BuildEndurance.cs b/GeneralMods/BuildEndurance/BuildEndurance.cs index 0a87f1f6..03fbbb42 100644 --- a/GeneralMods/BuildEndurance/BuildEndurance.cs +++ b/GeneralMods/BuildEndurance/BuildEndurance.cs @@ -1,7 +1,5 @@ using System; using System.IO; -using System.Text; -using Newtonsoft.Json; using Omegasis.BuildEndurance.Framework; using StardewModdingAPI; using StardewModdingAPI.Events; @@ -67,35 +65,7 @@ namespace Omegasis.BuildEndurance SaveEvents.AfterLoad += this.SaveEvents_AfterLoad; TimeEvents.AfterDayStarted += this.TimeEvents_AfterDayStarted; - string configPath = Path.Combine(helper.DirectoryPath, "BuildEnduranceConfig.json"); - if (!File.Exists(configPath)) - { - this.Monitor.Log("Initial configuration file setup."); - this.Config = new ModConfig - { - CurrentLevel = 0, - MaxLevel = 100, - StaminaIncreasePerLevel = 1, - CurrentExp = 0, - ExpToNextLevel = 20, - ExpCurve = 1.15, - ExpForEating = 2, - ExpForSleeping = 10, - ExpForToolUse = 1, - BaseStaminaBonus = 0, - CurrentLevelStaminaBonus = 0, - ExpForExhaustion = 25, - ExpForCollapsing = 50 - }; - File.WriteAllBytes(configPath, Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(this.Config))); - } - else - { - this.Config = JsonConvert.DeserializeObject(Encoding.UTF8.GetString(File.ReadAllBytes(configPath))); - this.Monitor.Log("Found BuildEndurance config file."); - } - - this.Monitor.Log("BuildEndurance Initialization Completed"); + this.Config = helper.ReadConfig(); } /// The method invoked once per second during a game update. @@ -234,7 +204,7 @@ namespace Omegasis.BuildEndurance string path = Path.Combine(Helper.DirectoryPath, "PlayerData", $"BuildEndurance_data_{Game1.player.name}.txt"); if (!File.Exists(path)) { - this.Monitor.Log("Clear Data Loaded could not find the correct file.")); + this.Monitor.Log("Clear Data Loaded could not find the correct file."); this.ClearModEffects = false; this.OriginalStamina = 0; diff --git a/GeneralMods/BuildEndurance/BuildEndurance.csproj b/GeneralMods/BuildEndurance/BuildEndurance.csproj index 16ff6c1a..3dd19942 100644 --- a/GeneralMods/BuildEndurance/BuildEndurance.csproj +++ b/GeneralMods/BuildEndurance/BuildEndurance.csproj @@ -30,10 +30,6 @@ 4 - - ..\packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll - False - diff --git a/GeneralMods/BuildEndurance/Framework/ModConfig.cs b/GeneralMods/BuildEndurance/Framework/ModConfig.cs index f2a12060..477ca87f 100644 --- a/GeneralMods/BuildEndurance/Framework/ModConfig.cs +++ b/GeneralMods/BuildEndurance/Framework/ModConfig.cs @@ -4,7 +4,7 @@ namespace Omegasis.BuildEndurance.Framework internal class ModConfig { /// The XP points needed to reach the next endurance level. - public double ExpToNextLevel { get; set; } + public double ExpToNextLevel { get; set; } = 20; /// The player's current endurance XP points. public double CurrentExp { get; set; } @@ -19,27 +19,27 @@ namespace Omegasis.BuildEndurance.Framework public int CurrentLevelStaminaBonus { get; set; } /// The multiplier for the experience points to need to reach an endurance level relative to the previous one. - public double ExpCurve { get; set; } + public double ExpCurve { get; set; } = 1.15; /// The maximum endurance level the player can reach. - public int MaxLevel { get; set; } + public int MaxLevel { get; set; } = 100; /// The amount of stamina the player should gain for each endurance level. - public int StaminaIncreasePerLevel { get; set; } + public int StaminaIncreasePerLevel { get; set; } = 1; /// The experience points to gain for using a tool. - public int ExpForToolUse { get; set; } + public int ExpForToolUse { get; set; } = 1; /// The experience points to gain for eating or drinking. - public int ExpForEating { get; set; } + public int ExpForEating { get; set; } = 2; /// The experience points to gain for sleeping. - public int ExpForSleeping { get; set; } + public int ExpForSleeping { get; set; } = 10; /// The experience points to gain for reaching a state of exhaustion for the day. - public int ExpForExhaustion { get; set; } + public int ExpForExhaustion { get; set; } = 25; /// The experience points to gain for collapsing for the day. - public int ExpForCollapsing { get; set; } + public int ExpForCollapsing { get; set; } = 50; } } diff --git a/GeneralMods/BuildEndurance/packages.config b/GeneralMods/BuildEndurance/packages.config index 31bc8030..058d0838 100644 --- a/GeneralMods/BuildEndurance/packages.config +++ b/GeneralMods/BuildEndurance/packages.config @@ -1,5 +1,4 @@  - \ No newline at end of file diff --git a/GeneralMods/BuildHealth/BuildHealth.cs b/GeneralMods/BuildHealth/BuildHealth.cs index ea234699..4e29c848 100644 --- a/GeneralMods/BuildHealth/BuildHealth.cs +++ b/GeneralMods/BuildHealth/BuildHealth.cs @@ -1,7 +1,5 @@ using System; using System.IO; -using System.Text; -using Newtonsoft.Json; using Omegasis.BuildHealth.Framework; using StardewModdingAPI; using StardewModdingAPI.Events; @@ -61,36 +59,10 @@ namespace Omegasis.BuildHealth { GameEvents.UpdateTick += this.GameEvents_UpdateTick; GameEvents.OneSecondTick += this.GameEvents_OneSecondTick; - TimeEvents.AfterDayStarted += this.TimeEvents_AfterDayStarted; SaveEvents.AfterLoad += this.SaveEvents_AfterLoaded; - var configPath = Path.Combine(helper.DirectoryPath, "BuildHealthConfig.json"); - if (!File.Exists(configPath)) - { - this.Config = new ModConfig - { - CurrentLevel = 0, - MaxLevel = 100, - HealthIncreasePerLevel = 1, - CurrentExp = 0, - ExpToNextLevel = 20, - ExpCurve = 1.15, - ExpForEating = 2, - ExpForSleeping = 10, - ExpForToolUse = 1, - BaseHealthBonus = 0, - CurrentLevelHealthBonus = 0 - }; - File.WriteAllBytes(configPath, Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(this.Config))); - } - else - { - this.Config = JsonConvert.DeserializeObject(Encoding.UTF8.GetString(File.ReadAllBytes(configPath))); - this.Monitor.Log("Found BuildHealth config file."); - } - - this.Monitor.Log("BuildHealth Initialization Completed"); + this.Config = helper.ReadConfig(); } /// The method invoked once per second during a game update. diff --git a/GeneralMods/BuildHealth/BuildHealth.csproj b/GeneralMods/BuildHealth/BuildHealth.csproj index 40c8afd8..77d7a1f3 100644 --- a/GeneralMods/BuildHealth/BuildHealth.csproj +++ b/GeneralMods/BuildHealth/BuildHealth.csproj @@ -30,10 +30,6 @@ 4 - - ..\packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll - False - diff --git a/GeneralMods/BuildHealth/Framework/ModConfig.cs b/GeneralMods/BuildHealth/Framework/ModConfig.cs index 7fb1cf14..e0849f74 100644 --- a/GeneralMods/BuildHealth/Framework/ModConfig.cs +++ b/GeneralMods/BuildHealth/Framework/ModConfig.cs @@ -4,7 +4,7 @@ internal class ModConfig { /// The XP points needed to reach the next level. - public double ExpToNextLevel { get; set; } + public double ExpToNextLevel { get; set; } = 20; /// The player's current XP points. public double CurrentExp { get; set; } @@ -19,22 +19,22 @@ public int CurrentLevelHealthBonus { get; set; } /// The multiplier for the experience points to need to reach an endurance level relative to the previous one. - public double ExpCurve { get; set; } + public double ExpCurve { get; set; } = 1.15; /// The maximum endurance level the player can reach. - public int MaxLevel { get; set; } + public int MaxLevel { get; set; } = 100; /// The amount of stamina the player should gain for each endurance level. - public int HealthIncreasePerLevel { get; set; } + public int HealthIncreasePerLevel { get; set; } = 1; /// The experience points to gain for using a tool. - public int ExpForToolUse { get; set; } + public int ExpForToolUse { get; set; } = 1; /// The experience points to gain for eating or drinking. - public int ExpForEating { get; set; } + public int ExpForEating { get; set; } = 2; /// The experience points to gain for sleeping. - public int ExpForSleeping { get; set; } + public int ExpForSleeping { get; set; } = 10; /// The experience points to gain for collapsing for the day. public int ExpForCollapsing { get; set; } diff --git a/GeneralMods/BuildHealth/packages.config b/GeneralMods/BuildHealth/packages.config index 31bc8030..058d0838 100644 --- a/GeneralMods/BuildHealth/packages.config +++ b/GeneralMods/BuildHealth/packages.config @@ -1,5 +1,4 @@  - \ No newline at end of file