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+ }
0 commit comments