Skip to content

Commit 81d6214

Browse files
committed
update Linux/macOS installer color scheme logic for 4.5.0+ changes
The selected color scheme should now be set in `config.user.json` instead of `config.json`, and the field was renamed.
1 parent 8b0936c commit 81d6214

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

docs/release-notes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# Release notes
44
## Upcoming release
55
* For players:
6+
* Fixed the Linux/macOS installer not saving the color scheme correctly in 4.5.0+.
67
* Fixed typo in config UI text (thanks to QuentiumYT!).
78
* Improved translations. Thanks to dewanggatrustha (updated Indonesian), QuentiumYT (updated French), and Timur13240
89
(updated Russian)!

src/SMAPI.Installer/InteractiveInstaller.cs

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@
66
using System.IO;
77
using System.Linq;
88
using System.Reflection;
9+
using Newtonsoft.Json.Linq;
910
using StardewModdingApi.Installer.Enums;
1011
using StardewModdingAPI.Installer.Framework;
1112
using StardewModdingAPI.Internal.ConsoleWriting;
1213
using StardewModdingAPI.Toolkit;
1314
using StardewModdingAPI.Toolkit.Framework;
1415
using StardewModdingAPI.Toolkit.Framework.GameScanning;
1516
using StardewModdingAPI.Toolkit.Framework.ModScanning;
17+
using StardewModdingAPI.Toolkit.Serialization;
1618
using StardewModdingAPI.Toolkit.Utilities;
1719

1820
namespace StardewModdingApi.Installer;
@@ -501,10 +503,10 @@ public void Run(string[] args)
501503
// set SMAPI's color scheme if defined
502504
if (scheme != MonitorColorScheme.AutoDetect)
503505
{
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+
});
508510
}
509511
}
510512
}
@@ -559,6 +561,33 @@ private string GetDisplayText(MonitorColorScheme scheme)
559561
}
560562
}
561563

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+
562591
/// <summary>Print a message without formatting.</summary>
563592
/// <param name="text">The text to print.</param>
564593
private void PrintPlain(string text)

0 commit comments

Comments
 (0)