Skip to content

Commit 5c81b00

Browse files
committed
Migration logic!
Fixes: Fix for macOS which makes it crash on startup (Title bar) Changes: Added a bit of spacing for install theme buttons Added Equals to RpcProfile and RichPresence (Needed for migration to only import what hasn't already been added by the user manually)
1 parent d039bec commit 5c81b00

File tree

12 files changed

+427
-6
lines changed

12 files changed

+427
-6
lines changed

src/MultiRPC/Assets/Language/en-gb.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,5 +252,8 @@
252252
"Secondary": "Secondary",
253253
"Buttons": "Buttons",
254254
"Pages": "Pages",
255-
"Other": "Other"
255+
"Other": "Other",
256+
"FailedToGetOldConfig": "We failed to get your configuration from V5!",
257+
"FailedToGetOldProfiles": "We failed to get your profiles from V5!",
258+
"FailedToGetOldData": "We failed to get your configuration and profiles from V5!"
256259
}

src/MultiRPC/Legacy/Changelog.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System;
2+
using System.IO;
3+
using TinyUpdate.Core;
4+
5+
namespace MultiRPC.Legacy;
6+
7+
public class Changelog
8+
{
9+
public static ReleaseNote[]? MakeReleaseNoteFromChangelog()
10+
{
11+
if (File.Exists(FileLocations.ChangelogFileLocalLocation))
12+
{
13+
var releaseNotes = new ReleaseNote[43];
14+
var counter = -1;
15+
var content = string.Empty;
16+
foreach (var line in File.ReadLines(FileLocations.ChangelogFileLocalLocation))
17+
{
18+
if (line.StartsWith("---") && content != string.Empty)
19+
{
20+
counter++;
21+
releaseNotes[counter] = new ReleaseNote(content.TrimEnd(), NoteType.Plain);
22+
content = string.Empty;
23+
}
24+
content += line + "\r\n";
25+
}
26+
counter++;
27+
releaseNotes[counter] = new ReleaseNote(content.TrimEnd(), NoteType.Plain);
28+
if (counter != 42)
29+
{
30+
Array.Resize(ref releaseNotes, counter + 1);
31+
}
32+
Array.Reverse(releaseNotes);
33+
34+
return releaseNotes;
35+
}
36+
return null;
37+
}
38+
}

src/MultiRPC/Legacy/Config.cs

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
using System;
2+
using System.Globalization;
3+
using System.IO;
4+
using System.Text.Json;
5+
using TinyUpdate.Core.Logging;
6+
7+
namespace MultiRPC.Legacy;
8+
9+
public class Config
10+
{
11+
/// <summary> Debug test </summary>
12+
public bool Debug { get; set; }
13+
14+
/// <summary> What language is to be shown </summary>
15+
public string ActiveLanguage { get; set; } = CultureInfo.CurrentUICulture.Name;
16+
17+
/// <summary> What theme to use </summary>
18+
public string ActiveTheme { get; set; } = Path.Combine("Assets", "Themes", "DarkTheme" + Constants.LegacyThemeFileExtension);
19+
20+
/// <summary> Enable showing afk time </summary>
21+
public bool AFKTime { get; set; }
22+
23+
/// <summary> Enable starting rich presence when app loads </summary>
24+
public string AutoStart { get; set; } = Language.GetText(LanguageText.No);
25+
26+
/// <summary> If to auto update the app </summary>
27+
public bool AutoUpdate { get; set; }
28+
29+
/// <summary> If to check the token </summary>
30+
public bool CheckToken { get; set; } = true;
31+
32+
/// <summary> What client to connect to </summary>
33+
public int ClientToUse { get; set; }
34+
35+
/// <summary> Disabled settings config </summary>
36+
public DisableConfig Disabled { get; set; } = new DisableConfig();
37+
38+
/// <summary> Check if discord is running </summary>
39+
public bool DiscordCheck { get; set; } = true;
40+
41+
/// <summary> If to hide the taskbar when minimized </summary>
42+
public bool HideTaskbarIconWhenMin { get; set; } = true;
43+
44+
/// <summary> Has the user been warned for invites in rich presence text </summary>
45+
public bool InviteWarn { get; set; }
46+
47+
/// <summary> What the user name and 4 digit number was last time this app was ran </summary>
48+
public string LastUser { get; set; } = string.Empty;
49+
50+
/// <summary> Default rich presence config </summary>
51+
public DefaultConfig MultiRPC { get; set; } = new DefaultConfig();
52+
53+
/// <summary> Tells the app what custom button to press in code </summary>
54+
public int SelectedCustom { get; set; }
55+
56+
public bool ShowPageTooltips { get; set; } = true;
57+
58+
public bool HadTriggerWarning { get; set; }
59+
60+
private static readonly ILogging Logger = LoggingCreator.CreateLogger(nameof(Config));
61+
/// <summary> Get the settings stored on disk </summary>
62+
public static Config? Load()
63+
{
64+
if (File.Exists(FileLocations.ConfigFileLocalLocation))
65+
{
66+
try
67+
{
68+
using var file = File.OpenRead(FileLocations.ConfigFileLocalLocation);
69+
return JsonSerializer.Deserialize<Config>(file, new JsonSerializerOptions());
70+
}
71+
catch (Exception ex)
72+
{
73+
Logger.Error(ex);
74+
return null;
75+
}
76+
}
77+
78+
return null;
79+
}
80+
}
81+
82+
/// <summary> Default rich presence config </summary>
83+
public class DefaultConfig
84+
{
85+
public int LargeKey { get; set; } = 2;
86+
public string LargeText { get; set; }
87+
public bool ShowTime { get; set; }
88+
public int SmallKey { get; set; }
89+
public string SmallText { get; set; }
90+
public string Text1 { get; set; } = Language.GetText(LanguageText.Hello);
91+
public string Text2 { get; set; } = Language.GetText(LanguageText.World);
92+
public string Button1Name { get; set; } = string.Empty;
93+
public string Button1Url { get; set; } = string.Empty;
94+
public string Button2Name { get; set; } = string.Empty;
95+
public string Button2Url { get; set; } = string.Empty;
96+
}
97+
98+
/// <summary> Disabled settings config </summary>
99+
public class DisableConfig
100+
{
101+
/// <summary> Disable the custom tab help icons </summary>
102+
public bool HelpIcons { get; set; }
103+
}

src/MultiRPC/Legacy/CustomProfile.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using MultiRPC.Rpc;
2+
3+
namespace MultiRPC.Legacy;
4+
5+
public class CustomProfile
6+
{
7+
public string ClientID { get; set; } = string.Empty;
8+
public string LargeKey { get; set; } = string.Empty;
9+
public string LargeText { get; set; } = string.Empty;
10+
public string Name { get; set; } = string.Empty;
11+
public bool ShowTime { get; set; }
12+
public string SmallKey { get; set; } = string.Empty;
13+
public string SmallText { get; set; } = string.Empty;
14+
public string Text1 { get; set; } = string.Empty;
15+
public string Text2 { get; set; } = string.Empty;
16+
public string Button1Name { get; set; } = string.Empty;
17+
public string Button1Url { get; set; } = string.Empty;
18+
public string Button2Name { get; set; } = string.Empty;
19+
public string Button2Url { get; set; } = string.Empty;
20+
21+
public RichPresence? ToRichPresence()
22+
{
23+
if (!long.TryParse(ClientID, out var id))
24+
{
25+
return null;
26+
}
27+
28+
return new RichPresence(Name, id)
29+
{
30+
Profile = new RpcProfile()
31+
{
32+
LargeKey = LargeKey,
33+
LargeText = LargeText,
34+
ShowTime = ShowTime,
35+
SmallKey = SmallKey,
36+
SmallText = SmallText,
37+
Details = Text1,
38+
State = Text2,
39+
Button1Text = Button1Name,
40+
Button1Url = Button1Url,
41+
Button2Text = Button2Name,
42+
Button2Url = Button2Url,
43+
}
44+
};
45+
}
46+
}

src/MultiRPC/Legacy/FileLocations.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System;
2+
using System.IO;
3+
4+
namespace MultiRPC.Legacy;
5+
6+
public static class FileLocations
7+
{
8+
public static readonly string ConfigFolder =
9+
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "MultiRPC");
10+
11+
public static readonly string ThemesFolder = Path.Combine(ConfigFolder, "Themes");
12+
13+
public const string ConfigFileName = "Config.json";
14+
public static readonly string ConfigFileLocalLocation = Path.Combine(ConfigFolder, ConfigFileName);
15+
16+
private const string ProfilesFileName = "Profiles.json";
17+
public static readonly string ProfilesFileLocalLocation = Path.Combine(ConfigFolder, ProfilesFileName);
18+
19+
private const string ChangelogFileName = "Changelog.txt";
20+
public static readonly string ChangelogFileLocalLocation = Path.Combine(ConfigFolder, ChangelogFileName);
21+
}

src/MultiRPC/Legacy/MigrateData.cs

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Text.Json;
6+
using MultiRPC.Rpc;
7+
using MultiRPC.Setting;
8+
using MultiRPC.Setting.Settings;
9+
using MultiRPC.Theming;
10+
using TinyUpdate.Core.Logging;
11+
12+
namespace MultiRPC.Legacy;
13+
14+
/// <summary>
15+
/// Migrates all data from V5+ into the current version
16+
/// </summary>
17+
public static class MigrateData
18+
{
19+
public enum MigrateStatus
20+
{
21+
Success,
22+
Failed,
23+
PartialSuccess,
24+
NoMigrationNeeded
25+
}
26+
27+
private static readonly ILogging Logging = LoggingCreator.CreateLogger(nameof(MigrateData));
28+
public static (MigrateStatus status, string? failedReason) Migrate()
29+
{
30+
if (!Directory.Exists(FileLocations.ConfigFolder))
31+
{
32+
return (MigrateStatus.NoMigrationNeeded, null);
33+
}
34+
35+
// Get old config
36+
var oldConfig = Config.Load();
37+
38+
// Get old profiles
39+
var oldProfilesExist = File.Exists(FileLocations.ProfilesFileLocalLocation);
40+
var oldProfilesStream = oldProfilesExist ? File.OpenRead(FileLocations.ProfilesFileLocalLocation) : Stream.Null;
41+
Dictionary<string, CustomProfile>? oldProfiles = null;
42+
try
43+
{
44+
oldProfiles = oldProfilesExist
45+
? JsonSerializer.Deserialize<Dictionary<string, CustomProfile>>(oldProfilesStream)
46+
: null;
47+
oldProfilesStream.Dispose();
48+
}
49+
catch (Exception e)
50+
{
51+
oldProfilesStream.Dispose();
52+
Logging.Error(e);
53+
}
54+
55+
//Fail if we wasn't able to successful get anything
56+
var ableToGetConfig = oldConfig != null || !File.Exists(FileLocations.ConfigFileName);
57+
var ableToGetOldProfiles = oldProfiles != null || !oldProfilesExist;
58+
if (!ableToGetConfig && !ableToGetOldProfiles)
59+
{
60+
return (MigrateStatus.Failed, Language.GetText(LanguageText.FailedToGetOldData));
61+
}
62+
63+
// Process old config
64+
var profileSettings = SettingManager<ProfilesSettings>.Setting;
65+
if (oldConfig != null)
66+
{
67+
// Process normal settings
68+
var settings = SettingManager<GeneralSettings>.Setting;
69+
settings.LastUser = oldConfig.LastUser;
70+
settings.LogLevel = oldConfig.Debug ? LogLevel.Trace : LogLevel.Info;
71+
settings.ShowAfkTime = oldConfig.AFKTime;
72+
settings.ThemeFile = Path.Combine(Constants.ThemeFolder, Path.GetFileName(oldConfig.ActiveTheme));
73+
settings.Language = oldConfig.ActiveLanguage;
74+
settings.AutoStart = oldConfig.AutoStart;
75+
settings.Client = (DiscordClients)oldConfig.ClientToUse;
76+
77+
// Process disabled settings
78+
var disabledSettings = SettingManager<DisableSettings>.Setting;
79+
disabledSettings.AutoUpdate = !oldConfig.AutoUpdate;
80+
disabledSettings.TokenCheck = !oldConfig.CheckToken;
81+
disabledSettings.DiscordCheck = !oldConfig.DiscordCheck;
82+
disabledSettings.HideTaskbarIcon = !oldConfig.HideTaskbarIconWhenMin;
83+
disabledSettings.InviteWarn = oldConfig.InviteWarn;
84+
disabledSettings.ShowPageTooltips = !oldConfig.ShowPageTooltips;
85+
disabledSettings.HelpIcons = oldConfig.Disabled.HelpIcons;
86+
87+
// Process MultiRPC profile
88+
var multiRPC = SettingManager<MultiRPCSettings>.Setting;
89+
//multiRPC.Presence.Profile.LargeKey = oldConfig.MultiRPC.LargeKey;
90+
multiRPC.Presence.Profile.LargeText = oldConfig.MultiRPC.LargeText;
91+
multiRPC.Presence.Profile.ShowTime = oldConfig.MultiRPC.ShowTime;
92+
//multiRPC.Presence.Profile.SmallKey = oldConfig.MultiRPC.SmallKey;
93+
multiRPC.Presence.Profile.SmallText = oldConfig.MultiRPC.SmallText;
94+
multiRPC.Presence.Profile.Details = oldConfig.MultiRPC.Text1;
95+
multiRPC.Presence.Profile.State = oldConfig.MultiRPC.Text2;
96+
multiRPC.Presence.Profile.Button1Text = oldConfig.MultiRPC.Button1Name;
97+
multiRPC.Presence.Profile.Button1Url = oldConfig.MultiRPC.Button1Url;
98+
multiRPC.Presence.Profile.Button2Text = oldConfig.MultiRPC.Button2Name;
99+
multiRPC.Presence.Profile.Button2Url = oldConfig.MultiRPC.Button2Url;
100+
}
101+
102+
// Process old profiles
103+
var counter = 0;
104+
var oldProfileCounter = oldConfig?.SelectedCustom ?? -1;
105+
if (oldProfiles != null)
106+
{
107+
foreach (var (_, profile) in oldProfiles)
108+
{
109+
var presence = profile.ToRichPresence();
110+
if (presence != null && profileSettings.Profiles.All(x => !x.Equals(presence)))
111+
{
112+
profileSettings.Profiles.Add(presence);
113+
}
114+
115+
if (counter == oldProfileCounter && presence != null)
116+
{
117+
profileSettings.LastSelectedProfileIndex = profileSettings.Profiles.IndexOf(presence);
118+
}
119+
counter++;
120+
}
121+
}
122+
123+
// Copy old theme's
124+
if (Directory.Exists(FileLocations.ThemesFolder))
125+
{
126+
foreach (var file in Directory.EnumerateFiles(FileLocations.ThemesFolder))
127+
{
128+
var newFileLoc = Path.Combine(Constants.ThemeFolder, Path.GetFileName(file));
129+
if (!File.Exists(newFileLoc) && Theme.Load(file) != null)
130+
{
131+
File.Copy(file, newFileLoc);
132+
}
133+
}
134+
}
135+
136+
if (!ableToGetConfig)
137+
{
138+
return (MigrateStatus.PartialSuccess, Language.GetText(LanguageText.FailedToGetOldConfig));
139+
}
140+
141+
if (!ableToGetOldProfiles)
142+
{
143+
return (MigrateStatus.PartialSuccess, Language.GetText(LanguageText.FailedToGetOldProfiles));
144+
}
145+
return (MigrateStatus.Success, null);
146+
}
147+
}

src/MultiRPC/MultiRPC.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<BuiltInComInteropSupport>true</BuiltInComInteropSupport>
99
<LangVersion>preview</LangVersion>
1010
<EnablePreviewFeatures>true</EnablePreviewFeatures>
11+
<Version>7.0.0</Version>
1112
</PropertyGroup>
1213

1314
<ItemGroup Label="Packages">

src/MultiRPC/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
using TinyUpdate.Core.Logging;
1414
using TinyUpdate.Core.Logging.Loggers;
1515

16-
[assembly: SemanticVersion("7.0.0-beta6")]
16+
[assembly: SemanticVersion("7.0.0-beta6+1")]
1717
namespace MultiRPC;
1818

1919
internal static class Program

0 commit comments

Comments
 (0)