Skip to content

Commit

Permalink
feat(Studio): Add migration for GameInfo settings changes
Browse files Browse the repository at this point in the history
  • Loading branch information
psyGamer committed Sep 29, 2024
1 parent 857fc36 commit 8f4c117
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 3 deletions.
2 changes: 2 additions & 0 deletions Studio/CelesteStudio/Migration/MigrateV3_0_0.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ public static void PreLoad() {
if (oldSettings.themes == "Light") {
newDocument.Put("ThemeName", "Light");
}

Migrator.WriteSettings(newDocument);
} catch (Exception ex) {
Console.Error.WriteLine($"Failed to read legacy settings file from path '{Path.Combine(Studio.CelesteDirectory, "Celeste Studio.toml")}'");
Console.Error.WriteLine(ex);
Expand Down
26 changes: 26 additions & 0 deletions Studio/CelesteStudio/Migration/MigrateV3_2_0.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.IO;
using Tomlet;

namespace CelesteStudio.Migration;

/// v3.2.0 Merged "bool ShowGameInfo" and "bool GameInfoPopoutOpen" into "GameInfoType GameInfo"
public static class MigrateV3_2_0 {
public static void PreLoad() {
var document = TomlParser.ParseFile(Settings.SettingsPath);

bool showGameInfo = document.GetBoolean("ShowGameInfo");
bool popoutOpen = document.GetBoolean("GameInfoPopoutOpen");

if (showGameInfo) {
if (popoutOpen) {
document.Put("GameInfo", "Popout");
} else {
document.Put("GameInfo", "Panel");
}
} else {
document.Put("GameInfo", "Disabled");
}

Migrator.WriteSettings(document);
}
}
21 changes: 19 additions & 2 deletions Studio/CelesteStudio/Migration/Migrator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using Tomlet.Models;

namespace CelesteStudio.Migration;

Expand All @@ -11,12 +12,20 @@ public static class Migrator {
private static string LatestVersionPath => Path.Combine(Settings.BaseConfigPath, ".latest-version");

private static readonly (Version Version, Action? PreLoad, Action? PostLoad)[] migrations = [
(new Version(3, 0, 0), MigrateV3_0_0.PreLoad, null)
(new Version(3, 0, 0), MigrateV3_0_0.PreLoad, null),
(new Version(3, 2, 0), MigrateV3_2_0.PreLoad, null),
];

private static Version oldVersion = null!, newVersion = null!;
private static readonly List<(string versionName, Stream stream)> changelogs = [];

public static void WriteSettings(TomlDocument document) {
// Write to another file and then move that over, to avoid getting interrupted while writing and corrupting the settings
var tmpFile = Settings.SettingsPath + ".tmp";
File.WriteAllText(tmpFile, document.SerializedValue);
File.Move(tmpFile, Settings.SettingsPath, overwrite: true);
}

/// Migrates settings and other configurations from the last used to the current version
/// Also shows changelog dialogs when applicable
public static void ApplyPreLoadMigrations() {
Expand All @@ -30,7 +39,12 @@ public static void ApplyPreLoadMigrations() {
// Need to check .toml since .exe and .pdb were already deleted by CelesteTAS
bool studioV2Present = File.Exists(Path.Combine(Studio.CelesteDirectory ?? string.Empty, "Celeste Studio.toml"));

#if DEBUG
// Always apply the latest migration in debug builds
newVersion = migrations[^1].Version;
#else
newVersion = Assembly.GetExecutingAssembly().GetName().Version!;
#endif
if (firstV3Launch) {
if (studioV2Present) {
oldVersion = new Version(2, 0, 0);
Expand All @@ -44,7 +58,10 @@ public static void ApplyPreLoadMigrations() {

File.WriteAllText(LatestVersionPath, newVersion.ToString(3));

if (oldVersion == newVersion) {
if (oldVersion.Major == newVersion.Major &&
oldVersion.Minor == newVersion.Minor &&
oldVersion.Build == newVersion.Build)
{
return;
}

Expand Down
1 change: 0 additions & 1 deletion Studio/CelesteStudio/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,6 @@ public string ThemeName {
public Point LastLocation { get; set; } = Point.Empty;
public Size LastSize { get; set; } = new(400, 600);

public bool GameInfoPopoutOpen { get; set; } = false;
public bool GameInfoPopoutTopmost { get; set; } = false;
public Point GameInfoPopoutLocation { get; set; } = Point.Empty;
public Size GameInfoPopoutSize { get; set; } = new(400, 250);
Expand Down

0 comments on commit 8f4c117

Please sign in to comment.