From 6a07a1cbaf62f7bf7ccdf89c341e6b761bd2d6af Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Fri, 24 Feb 2017 14:46:24 -0500 Subject: clean up constants, avoid regenerating values unnecessarily --- src/StardewModdingAPI/Constants.cs | 61 ++++++++++++++++++-------------------- src/StardewModdingAPI/Mod.cs | 2 +- 2 files changed, 30 insertions(+), 33 deletions(-) diff --git a/src/StardewModdingAPI/Constants.cs b/src/StardewModdingAPI/Constants.cs index 69dd1fa8..3326e43f 100644 --- a/src/StardewModdingAPI/Constants.cs +++ b/src/StardewModdingAPI/Constants.cs @@ -16,11 +16,11 @@ namespace StardewModdingAPI /********* ** Properties *********/ - /// The directory name containing the current save's data (if a save is loaded). - private static string RawSaveFolderName => Constants.PlayerNull ? string.Empty : Constants.GetSaveFolderName(); - /// The directory path containing the current save's data (if a save is loaded). - private static string RawSavePath => Constants.PlayerNull ? string.Empty : Path.Combine(Constants.SavesPath, Constants.RawSaveFolderName); + private static string RawSavePath => Constants.IsSaveLoaded ? Path.Combine(Constants.SavesPath, Constants.GetSaveFolderName()) : null; + + /// Whether the directory containing the current save's data exists on disk. + private static bool SavePathReady => Constants.IsSaveLoaded && Directory.Exists(Constants.RawSavePath); /********* @@ -30,28 +30,28 @@ namespace StardewModdingAPI ** Public ****/ /// SMAPI's current semantic version. - public static ISemanticVersion ApiVersion => new SemanticVersion(1, 8, 0, null); + public static ISemanticVersion ApiVersion { get; } = new SemanticVersion(1, 8, 0); /// The minimum supported version of Stardew Valley. - public const string MinimumGameVersion = "1.2.9"; + public static string MinimumGameVersion { get; } = "1.2.9"; + + /// The path to the game folder. + public static string ExecutionPath { get; } = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); /// The directory path containing Stardew Valley's app data. - public static string DataPath => Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "StardewValley"); + public static string DataPath { get; } = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "StardewValley"); + + /// The directory path in which error logs should be stored. + public static string LogDir { get; } = Path.Combine(Constants.DataPath, "ErrorLogs"); /// The directory path where all saves are stored. - public static string SavesPath => Path.Combine(Constants.DataPath, "Saves"); + public static string SavesPath { get; } = Path.Combine(Constants.DataPath, "Saves"); /// The directory name containing the current save's data (if a save is loaded and the directory exists). - public static string SaveFolderName => Constants.CurrentSavePathExists ? Constants.RawSaveFolderName : ""; + public static string SaveFolderName => Constants.SavePathReady ? Constants.GetSaveFolderName() : ""; /// The directory path containing the current save's data (if a save is loaded and the directory exists). - public static string CurrentSavePath => Constants.CurrentSavePathExists ? Constants.RawSavePath : ""; - - /// The path to the current assembly being executing. - public static string ExecutionPath => Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); - - /// The directory path in which error logs should be stored. - public static string LogDir => Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "StardewValley", "ErrorLogs"); + public static string CurrentSavePath => Constants.SavePathReady ? Path.Combine(Constants.SavesPath, Constants.GetSaveFolderName()) : ""; /**** ** Internal @@ -65,24 +65,11 @@ namespace StardewModdingAPI /// The file path to the log where the latest output should be saved. internal static string LogPath => Path.Combine(Constants.LogDir, "SMAPI-latest.txt"); - /// Whether the directory containing the current save's data exists on disk. - internal static bool CurrentSavePathExists => Directory.Exists(Constants.RawSavePath); - /// Whether a player save has been loaded. - internal static bool PlayerNull => !Game1.hasLoadedGame || Game1.player == null || string.IsNullOrEmpty(Game1.player.name); + internal static bool IsSaveLoaded => Game1.hasLoadedGame && !string.IsNullOrEmpty(Game1.player.name); - /// The actual game version. - /// This is necessary because is a constant, so SMAPI's references to it are inlined at compile-time. - internal static string GameVersion - { - get - { - FieldInfo field = typeof(Game1).GetField(nameof(Game1.version), BindingFlags.Public | BindingFlags.Static); - if (field == null) - throw new InvalidOperationException($"The {nameof(Game1)}.{nameof(Game1.version)} field could not be found."); - return (string)field.GetValue(null); - } - } + /// The current game version. + internal static string GameVersion { get; } = Constants.GetGameVersion(); /********* ** Protected methods @@ -154,5 +141,15 @@ namespace StardewModdingAPI string prefix = new string(Game1.player.name.Where(char.IsLetterOrDigit).ToArray()); return $"{prefix}_{Game1.uniqueIDForThisGame}"; } + + /// Get the actual game version. + /// This uses reflection because is a constant, so SMAPI's references to it are inlined at compile-time. + private static string GetGameVersion() + { + FieldInfo field = typeof(Game1).GetField(nameof(Game1.version), BindingFlags.Public | BindingFlags.Static); + if (field == null) + throw new InvalidOperationException($"The {nameof(Game1)}.{nameof(Game1.version)} field could not be found."); + return (string)field.GetValue(null); + } } } diff --git a/src/StardewModdingAPI/Mod.cs b/src/StardewModdingAPI/Mod.cs index 44cfd4b3..caa20774 100644 --- a/src/StardewModdingAPI/Mod.cs +++ b/src/StardewModdingAPI/Mod.cs @@ -65,7 +65,7 @@ namespace StardewModdingAPI { Mod.DeprecationManager.Warn($"{nameof(Mod)}.{nameof(Mod.PerSaveConfigPath)}", "1.0", DeprecationLevel.Info); Mod.DeprecationManager.MarkWarned($"{nameof(Mod)}.{nameof(Mod.PerSaveConfigFolder)}", "1.0"); // avoid redundant warnings - return Constants.CurrentSavePathExists ? Path.Combine(this.PerSaveConfigFolder, Constants.SaveFolderName + ".json") : ""; + return Constants.IsSaveLoaded ? Path.Combine(this.PerSaveConfigFolder, $"{Constants.SaveFolderName}.json") : ""; } } -- cgit