|
6 | 6 | using System.IO; |
7 | 7 | using System.Linq; |
8 | 8 | using System.Reflection; |
| 9 | +using Newtonsoft.Json.Linq; |
9 | 10 | using StardewModdingApi.Installer.Enums; |
10 | 11 | using StardewModdingAPI.Installer.Framework; |
11 | 12 | using StardewModdingAPI.Internal.ConsoleWriting; |
12 | 13 | using StardewModdingAPI.Toolkit; |
13 | 14 | using StardewModdingAPI.Toolkit.Framework; |
14 | 15 | using StardewModdingAPI.Toolkit.Framework.GameScanning; |
15 | 16 | using StardewModdingAPI.Toolkit.Framework.ModScanning; |
| 17 | +using StardewModdingAPI.Toolkit.Serialization; |
16 | 18 | using StardewModdingAPI.Toolkit.Utilities; |
17 | 19 |
|
18 | 20 | namespace StardewModdingApi.Installer; |
@@ -501,10 +503,10 @@ public void Run(string[] args) |
501 | 503 | // set SMAPI's color scheme if defined |
502 | 504 | if (scheme != MonitorColorScheme.AutoDetect) |
503 | 505 | { |
504 | | - string text = File |
505 | | - .ReadAllText(paths.ApiConfigPath) |
506 | | - .Replace(@"""UseScheme"": ""AutoDetect""", $@"""UseScheme"": ""{scheme}"""); |
507 | | - File.WriteAllText(paths.ApiConfigPath, text); |
| 506 | + this.SaveUserSettings(paths.ApiUserConfigPath, new Dictionary<string, object> |
| 507 | + { |
| 508 | + ["ConsoleColorScheme"] = scheme.ToString() |
| 509 | + }); |
508 | 510 | } |
509 | 511 | } |
510 | 512 | } |
@@ -559,6 +561,33 @@ private string GetDisplayText(MonitorColorScheme scheme) |
559 | 561 | } |
560 | 562 | } |
561 | 563 |
|
| 564 | + /// <summary>Save the given options to a SMAPI internal config file.</summary> |
| 565 | + /// <param name="filePath">The file path to edit.</param> |
| 566 | + /// <param name="settings">The settings to add or overwrite.</param> |
| 567 | + [SuppressMessage("Trimming", "IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code", Justification = "This isn't an issue here, since we're only using JToken and object.")] |
| 568 | + private void SaveUserSettings(string filePath, Dictionary<string, object> settings) |
| 569 | + { |
| 570 | + JsonHelper jsonHelper = new(); |
| 571 | + |
| 572 | + // merge into existing settings |
| 573 | + Dictionary<string, JToken> saveSettings = settings.ToDictionary(p => p.Key, p => JToken.FromObject(p.Value)); |
| 574 | + try |
| 575 | + { |
| 576 | + if (jsonHelper.ReadJsonFileIfExists(filePath, out Dictionary<string, JToken>? fileSettings)) |
| 577 | + { |
| 578 | + foreach ((string key, JToken value) in fileSettings) |
| 579 | + saveSettings.TryAdd(key, value); |
| 580 | + } |
| 581 | + } |
| 582 | + catch (Exception ex) |
| 583 | + { |
| 584 | + this.PrintWarning($"Couldn't parse your SMAPI settings file. Replacing it with a default version.\n\nTechnical details:\n{ex}"); |
| 585 | + } |
| 586 | + |
| 587 | + // save file |
| 588 | + jsonHelper.WriteJsonFile(filePath, saveSettings); |
| 589 | + } |
| 590 | + |
562 | 591 | /// <summary>Print a message without formatting.</summary> |
563 | 592 | /// <param name="text">The text to print.</param> |
564 | 593 | private void PrintPlain(string text) |
|
0 commit comments