From 4adf8611131a5d86b15f017a42a0366837d14528 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Wed, 13 Apr 2022 21:07:43 -0400 Subject: enable nullable annotations in the rest of SMAPI core (#837) --- src/SMAPI/Framework/Models/SConfig.cs | 78 +++++++++++++++++++++++++---------- 1 file changed, 56 insertions(+), 22 deletions(-) (limited to 'src/SMAPI/Framework/Models') diff --git a/src/SMAPI/Framework/Models/SConfig.cs b/src/SMAPI/Framework/Models/SConfig.cs index e74d73b5..d626ab4d 100644 --- a/src/SMAPI/Framework/Models/SConfig.cs +++ b/src/SMAPI/Framework/Models/SConfig.cs @@ -1,5 +1,3 @@ -#nullable disable - using System; using System.Collections.Generic; using System.Linq; @@ -40,60 +38,96 @@ namespace StardewModdingAPI.Framework.Models ** Accessors ********/ /// Whether to enable development features. - public bool DeveloperMode { get; set; } + public bool DeveloperMode { get; private set; } /// Whether to check for newer versions of SMAPI and mods on startup. - public bool CheckForUpdates { get; set; } + public bool CheckForUpdates { get; } /// Whether to add a section to the 'mod issues' list for mods which which directly use potentially sensitive .NET APIs like file or shell access. - public bool ParanoidWarnings { get; set; } = (bool)SConfig.DefaultValues[nameof(SConfig.ParanoidWarnings)]; + public bool ParanoidWarnings { get; } /// Whether to show beta versions as valid updates. - public bool UseBetaChannel { get; set; } = (bool)SConfig.DefaultValues[nameof(SConfig.UseBetaChannel)]; + public bool UseBetaChannel { get; } /// SMAPI's GitHub project name, used to perform update checks. - public string GitHubProjectName { get; set; } + public string GitHubProjectName { get; } /// The base URL for SMAPI's web API, used to perform update checks. - public string WebApiBaseUrl { get; set; } + public string WebApiBaseUrl { get; } /// Whether SMAPI should log more information about the game context. - public bool VerboseLogging { get; set; } + public bool VerboseLogging { get; } /// Whether SMAPI should rewrite mods for compatibility. - public bool RewriteMods { get; set; } = (bool)SConfig.DefaultValues[nameof(SConfig.RewriteMods)]; + public bool RewriteMods { get; } /// Whether to enable more aggressive memory optimizations. - public bool AggressiveMemoryOptimizations { get; set; } = (bool)SConfig.DefaultValues[nameof(SConfig.AggressiveMemoryOptimizations)]; + public bool AggressiveMemoryOptimizations { get; } /// Whether SMAPI should log network traffic. Best combined with , which includes network metadata. - public bool LogNetworkTraffic { get; set; } + public bool LogNetworkTraffic { get; } /// The colors to use for text written to the SMAPI console. - public ColorSchemeConfig ConsoleColors { get; set; } + public ColorSchemeConfig ConsoleColors { get; } /// The mod IDs SMAPI should ignore when performing update checks or validating update keys. - public string[] SuppressUpdateChecks { get; set; } + public string[] SuppressUpdateChecks { get; } /******** ** Public methods ********/ + /// Construct an instance. + /// Whether to enable development features. + /// Whether to check for newer versions of SMAPI and mods on startup. + /// Whether to add a section to the 'mod issues' list for mods which which directly use potentially sensitive .NET APIs like file or shell access. + /// Whether to show beta versions as valid updates. + /// SMAPI's GitHub project name, used to perform update checks. + /// The base URL for SMAPI's web API, used to perform update checks. + /// Whether SMAPI should log more information about the game context. + /// Whether SMAPI should rewrite mods for compatibility. + /// Whether to enable more aggressive memory optimizations. + /// Whether SMAPI should log network traffic. + /// The colors to use for text written to the SMAPI console. + /// The mod IDs SMAPI should ignore when performing update checks or validating update keys. + public SConfig(bool developerMode, bool checkForUpdates, bool? paranoidWarnings, bool? useBetaChannel, string gitHubProjectName, string webApiBaseUrl, bool verboseLogging, bool? rewriteMods, bool? aggressiveMemoryOptimizations, bool logNetworkTraffic, ColorSchemeConfig consoleColors, string[]? suppressUpdateChecks) + { + this.DeveloperMode = developerMode; + this.CheckForUpdates = checkForUpdates; + this.ParanoidWarnings = paranoidWarnings ?? (bool)SConfig.DefaultValues[nameof(SConfig.ParanoidWarnings)]; + this.UseBetaChannel = useBetaChannel ?? (bool)SConfig.DefaultValues[nameof(SConfig.UseBetaChannel)]; + this.GitHubProjectName = gitHubProjectName; + this.WebApiBaseUrl = webApiBaseUrl; + this.VerboseLogging = verboseLogging; + this.RewriteMods = rewriteMods ?? (bool)SConfig.DefaultValues[nameof(SConfig.RewriteMods)]; + this.AggressiveMemoryOptimizations = aggressiveMemoryOptimizations ?? (bool)SConfig.DefaultValues[nameof(SConfig.AggressiveMemoryOptimizations)]; + this.LogNetworkTraffic = logNetworkTraffic; + this.ConsoleColors = consoleColors; + this.SuppressUpdateChecks = suppressUpdateChecks ?? Array.Empty(); + } + + /// Override the value of . + /// The value to set. + public void OverrideDeveloperMode(bool value) + { + this.DeveloperMode = value; + } + /// Get the settings which have been customized by the player. - public IDictionary GetCustomSettings() + public IDictionary GetCustomSettings() { - IDictionary custom = new Dictionary(); + Dictionary custom = new(); - foreach (var pair in SConfig.DefaultValues) + foreach ((string? name, object defaultValue) in SConfig.DefaultValues) { - object value = typeof(SConfig).GetProperty(pair.Key)?.GetValue(this); - if (!pair.Value.Equals(value)) - custom[pair.Key] = value; + object? value = typeof(SConfig).GetProperty(name)?.GetValue(this); + if (!defaultValue.Equals(value)) + custom[name] = value; } - HashSet curSuppressUpdateChecks = new HashSet(this.SuppressUpdateChecks ?? Array.Empty(), StringComparer.OrdinalIgnoreCase); + HashSet curSuppressUpdateChecks = new(this.SuppressUpdateChecks, StringComparer.OrdinalIgnoreCase); if (SConfig.DefaultSuppressUpdateChecks.Count != curSuppressUpdateChecks.Count || SConfig.DefaultSuppressUpdateChecks.Any(p => !curSuppressUpdateChecks.Contains(p))) - custom[nameof(this.SuppressUpdateChecks)] = "[" + string.Join(", ", this.SuppressUpdateChecks ?? Array.Empty()) + "]"; + custom[nameof(this.SuppressUpdateChecks)] = "[" + string.Join(", ", this.SuppressUpdateChecks) + "]"; return custom; } -- cgit