11using log4net ;
2- using NETworkManager . Models . Network ;
32using NETworkManager . Settings ;
43using NETworkManager . Utilities ;
54using System ;
@@ -135,6 +134,36 @@ private static void LoadedProfileFileChanged(ProfileFileInfo profileFileInfo, bo
135134 OnLoadedProfileFileChangedEvent ? . Invoke ( null , new ProfileFileInfoArgs ( profileFileInfo , profileFileUpdating ) ) ;
136135 }
137136
137+ /// <summary>
138+ /// Occurs when the profile migration process begins.
139+ /// </summary>
140+ [ Obsolete ( "Will be removed after some time, as profile migration from legacy XML files is a one-time process." ) ]
141+ public static event EventHandler OnProfileMigrationStarted ;
142+
143+ /// <summary>
144+ /// Raises the event indicating that the profile migration process from legacy XML files has started.
145+ /// </summary>
146+ [ Obsolete ( "Will be removed after some time, as profile migration from legacy XML files is a one-time process." ) ]
147+ private static void ProfileMigrationStarted ( )
148+ {
149+ OnProfileMigrationStarted ? . Invoke ( null , EventArgs . Empty ) ;
150+ }
151+
152+ /// <summary>
153+ /// Occurs when the profile migration from legacy XML files has completed.
154+ /// </summary>
155+ [ Obsolete ( "Will be removed after some time, as profile migration from legacy XML files is a one-time process." ) ]
156+ public static event EventHandler OnProfileMigrationCompleted ;
157+
158+ /// <summary>
159+ /// Raises the event indicating that the profile migration from legacy XML files has completed.
160+ /// </summary>
161+ [ Obsolete ( "Will be removed after some time, as profile migration from legacy XML files is a one-time process." ) ]
162+ private static void ProfileMigrationCompleted ( )
163+ {
164+ OnProfileMigrationCompleted ? . Invoke ( null , EventArgs . Empty ) ;
165+ }
166+
138167 /// <summary>
139168 /// Event is fired if the profiles have changed.
140169 /// </summary>
@@ -507,7 +536,30 @@ private static void Load(ProfileFileInfo profileFileInfo)
507536
508537 if ( IsXmlContent ( decryptedBytes ) )
509538 {
510- groups = new List < GroupInfo > ( ) ; // ToDo
539+ //
540+ // MIGRATION FROM LEGACY XML PROFILE FILE
541+ //
542+
543+ Log . Info ( $ "Legacy XML profile file detected inside encrypted profile: { profileFileInfo . Path } . Migration in progress...") ;
544+
545+ // Load from legacy XML byte array
546+ groups = DeserializeFromXmlByteArray ( decryptedBytes ) ;
547+
548+ // Create a backup of the legacy XML file
549+ Backup ( profileFileInfo . Path ,
550+ GetSettingsBackupFolderLocation ( ) ,
551+ TimestampHelper . GetTimestampFilename ( Path . GetFileName ( profileFileInfo . Path ) ) ) ;
552+
553+ // Save encrypted profile file with new JSON format
554+ var newDecryptedBytes = SerializeToByteArray ( [ .. groups ] ) ;
555+ var newEncryptedBytes = CryptoHelper . Encrypt ( newDecryptedBytes ,
556+ SecureStringHelper . ConvertToString ( profileFileInfo . Password ) ,
557+ GlobalStaticConfiguration . Profile_EncryptionKeySize ,
558+ GlobalStaticConfiguration . Profile_EncryptionIterations ) ;
559+
560+ File . WriteAllBytes ( profileFileInfo . Path , newEncryptedBytes ) ;
561+
562+ Log . Info ( $ "Legacy XML profile file migration completed inside encrypted profile: { profileFileInfo . Path } .") ;
511563 }
512564 else
513565 {
@@ -528,6 +580,9 @@ private static void Load(ProfileFileInfo profileFileInfo)
528580
529581 if ( Path . GetExtension ( profileFileInfo . Path ) == LegacyProfileFileExtension )
530582 {
583+ //
584+ // MIGRATION FROM LEGACY XML PROFILE FILE
585+ //
531586 Log . Info ( $ "Legacy XML profile file detected: { profileFileInfo . Path } . Migration in progress...") ;
532587
533588 // Load from legacy XML file
@@ -537,22 +592,23 @@ private static void Load(ProfileFileInfo profileFileInfo)
537592
538593 LoadedProfileFile = profileFileInfo ;
539594
595+ // Create a backup of the legacy XML file and delete the original
596+ Backup ( profileFileInfo . Path ,
597+ GetSettingsBackupFolderLocation ( ) ,
598+ TimestampHelper . GetTimestampFilename ( Path . GetFileName ( profileFileInfo . Path ) ) ) ;
599+
540600 // Create new profile file info with JSON extension
541601 var newProfileFileInfo = new ProfileFileInfo ( profileFileInfo . Name ,
542602 Path . ChangeExtension ( profileFileInfo . Path , ProfileFileExtension ) ) ;
543603
544604 // Save new JSON file
545605 SerializeToFile ( newProfileFileInfo . Path , groups ) ;
546606
547- // Create a backup of the legacy XML file and delete the original
548- Backup ( profileFileInfo . Path ,
549- GetSettingsBackupFolderLocation ( ) ,
550- TimestampHelper . GetTimestampFilename ( Path . GetFileName ( profileFileInfo . Path ) ) ) ;
551-
607+ // Notify migration started
608+ ProfileMigrationStarted ( ) ;
609+
552610 // Add the new profile
553- Log . Info ( "Adding migrated profile file to the profile files list." ) ;
554611 ProfileFiles . Add ( newProfileFileInfo ) ;
555- Log . Info ( "Migrated profile file added to the profile files list." ) ;
556612
557613 // Switch profile
558614 Log . Info ( $ "Switching to migrated profile file: { newProfileFileInfo . Path } .") ;
@@ -563,6 +619,9 @@ private static void Load(ProfileFileInfo profileFileInfo)
563619 File . Delete ( profileFileInfo . Path ) ;
564620 ProfileFiles . Remove ( profileFileInfo ) ;
565621
622+ // Notify migration completed
623+ ProfileMigrationCompleted ( ) ;
624+
566625 Log . Info ( $ "Legacy XML profile file migration completed: { profileFileInfo . Path } .") ;
567626 return ;
568627 }
0 commit comments