Skip to content

Commit 28a08b5

Browse files
committed
Do not fail catastrophically if we can't migrate a profile
1 parent f2456ef commit 28a08b5

File tree

1 file changed

+38
-27
lines changed

1 file changed

+38
-27
lines changed

CustomizePlus/Configuration/Services/ConfigurationMigrator.cs

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@
1111
using CustomizePlus.Configuration.Data;
1212
using CustomizePlus.Core.Events;
1313
using CustomizePlus.Configuration.Data.Version3;
14+
using CustomizePlus.UI.Windows;
1415

1516
namespace CustomizePlus.Configuration.Services;
1617

1718
public class ConfigurationMigrator
1819
{
1920
private readonly SaveService _saveService;
2021
private readonly BackupService _backupService;
21-
private readonly MessageService _messageService;
22+
private readonly MessageService _messageService; //we can't use popups here since they rely on PluginConfiguration and using them here hangs plugin loading
2223
private readonly Logger _logger;
2324
private readonly ReloadEvent _reloadEvent;
2425

@@ -54,9 +55,6 @@ public void Migrate(PluginConfiguration config)
5455
MigrateV3ToV4();
5556
// /V3 migration code
5657

57-
//I'm sorry, I'm too lazy so v3's enable root position setting is not getting migrated for now
58-
//MigrateV3ToV4(configVersion);
59-
6058
config.Version = Constants.ConfigurationVersion;
6159
_saveService.ImmediateSave(config);
6260
}
@@ -67,41 +65,54 @@ private void MigrateV3ToV4()
6765

6866
//I'm sorry, I'm too lazy so v3's enable root position setting is not getting migrated
6967

68+
bool anyMigrationFailures = false;
69+
7070
var usedGuids = new HashSet<Guid>();
7171
foreach (var file in Directory.EnumerateFiles(_saveService.FileNames.ConfigDirectory, "*.profile", SearchOption.TopDirectoryOnly))
7272
{
73-
_logger.Debug($"Migrating v3 profile {file}");
73+
try
74+
{
75+
_logger.Debug($"Migrating v3 profile {file}");
7476

75-
var legacyProfile = JsonConvert.DeserializeObject<Version3Profile>(File.ReadAllText(file));
76-
if (legacyProfile == null)
77-
continue;
77+
var legacyProfile = JsonConvert.DeserializeObject<Version3Profile>(File.ReadAllText(file));
78+
if (legacyProfile == null)
79+
continue;
7880

79-
_logger.Debug($"v3 profile {file} loaded as {legacyProfile.ProfileName}");
81+
_logger.Debug($"v3 profile {file} loaded as {legacyProfile.ProfileName}");
8082

81-
(var profile, var template) = V3ProfileToV4Converter.Convert(legacyProfile);
83+
(var profile, var template) = V3ProfileToV4Converter.Convert(legacyProfile);
8284

83-
//regenerate guids just to be safe
84-
do
85-
{
86-
profile.UniqueId = Guid.NewGuid();
87-
}
88-
while (profile.UniqueId == Guid.Empty || usedGuids.Contains(profile.UniqueId));
89-
usedGuids.Add(profile.UniqueId);
85+
//regenerate guids just to be safe
86+
do
87+
{
88+
profile.UniqueId = Guid.NewGuid();
89+
}
90+
while (profile.UniqueId == Guid.Empty || usedGuids.Contains(profile.UniqueId));
91+
usedGuids.Add(profile.UniqueId);
9092

91-
do
92-
{
93-
template.UniqueId = Guid.NewGuid();
94-
}
95-
while (template.UniqueId == Guid.Empty || usedGuids.Contains(template.UniqueId));
96-
usedGuids.Add(template.UniqueId);
93+
do
94+
{
95+
template.UniqueId = Guid.NewGuid();
96+
}
97+
while (template.UniqueId == Guid.Empty || usedGuids.Contains(template.UniqueId));
98+
usedGuids.Add(template.UniqueId);
9799

98-
_saveService.ImmediateSaveSync(template);
99-
_saveService.ImmediateSaveSync(profile);
100+
_saveService.ImmediateSaveSync(template);
101+
_saveService.ImmediateSaveSync(profile);
100102

101-
_logger.Debug($"Migrated v3 profile {legacyProfile.ProfileName} to profile {profile.UniqueId} and template {template.UniqueId}");
102-
File.Delete(file);
103+
_logger.Debug($"Migrated v3 profile {legacyProfile.ProfileName} to profile {profile.UniqueId} and template {template.UniqueId}");
104+
File.Delete(file);
105+
}
106+
catch(Exception ex)
107+
{
108+
anyMigrationFailures = true;
109+
_logger.Error($"Error while migrating {file}: {ex}");
110+
}
103111
}
104112

113+
if (anyMigrationFailures)
114+
_messageService.NotificationMessage($"Some of your Customize+ profiles failed to migrate correctly.\nDetails have been printed to Dalamud log (/xllog in chat).", NotificationType.Error);
115+
105116
_reloadEvent.Invoke(ReloadEvent.Type.ReloadAll);
106117
}
107118
}

0 commit comments

Comments
 (0)