Skip to content

Commit

Permalink
Do not fail catastrophically if we can't migrate a profile
Browse files Browse the repository at this point in the history
  • Loading branch information
RisaDev committed Feb 4, 2024
1 parent f2456ef commit 28a08b5
Showing 1 changed file with 38 additions and 27 deletions.
65 changes: 38 additions & 27 deletions CustomizePlus/Configuration/Services/ConfigurationMigrator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@
using CustomizePlus.Configuration.Data;
using CustomizePlus.Core.Events;
using CustomizePlus.Configuration.Data.Version3;
using CustomizePlus.UI.Windows;

namespace CustomizePlus.Configuration.Services;

public class ConfigurationMigrator
{
private readonly SaveService _saveService;
private readonly BackupService _backupService;
private readonly MessageService _messageService;
private readonly MessageService _messageService; //we can't use popups here since they rely on PluginConfiguration and using them here hangs plugin loading
private readonly Logger _logger;
private readonly ReloadEvent _reloadEvent;

Expand Down Expand Up @@ -54,9 +55,6 @@ public void Migrate(PluginConfiguration config)
MigrateV3ToV4();
// /V3 migration code

//I'm sorry, I'm too lazy so v3's enable root position setting is not getting migrated for now
//MigrateV3ToV4(configVersion);

config.Version = Constants.ConfigurationVersion;
_saveService.ImmediateSave(config);
}
Expand All @@ -67,41 +65,54 @@ private void MigrateV3ToV4()

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

bool anyMigrationFailures = false;

var usedGuids = new HashSet<Guid>();
foreach (var file in Directory.EnumerateFiles(_saveService.FileNames.ConfigDirectory, "*.profile", SearchOption.TopDirectoryOnly))
{
_logger.Debug($"Migrating v3 profile {file}");
try
{
_logger.Debug($"Migrating v3 profile {file}");

var legacyProfile = JsonConvert.DeserializeObject<Version3Profile>(File.ReadAllText(file));
if (legacyProfile == null)
continue;
var legacyProfile = JsonConvert.DeserializeObject<Version3Profile>(File.ReadAllText(file));
if (legacyProfile == null)
continue;

_logger.Debug($"v3 profile {file} loaded as {legacyProfile.ProfileName}");
_logger.Debug($"v3 profile {file} loaded as {legacyProfile.ProfileName}");

(var profile, var template) = V3ProfileToV4Converter.Convert(legacyProfile);
(var profile, var template) = V3ProfileToV4Converter.Convert(legacyProfile);

//regenerate guids just to be safe
do
{
profile.UniqueId = Guid.NewGuid();
}
while (profile.UniqueId == Guid.Empty || usedGuids.Contains(profile.UniqueId));
usedGuids.Add(profile.UniqueId);
//regenerate guids just to be safe
do
{
profile.UniqueId = Guid.NewGuid();
}
while (profile.UniqueId == Guid.Empty || usedGuids.Contains(profile.UniqueId));
usedGuids.Add(profile.UniqueId);

do
{
template.UniqueId = Guid.NewGuid();
}
while (template.UniqueId == Guid.Empty || usedGuids.Contains(template.UniqueId));
usedGuids.Add(template.UniqueId);
do
{
template.UniqueId = Guid.NewGuid();
}
while (template.UniqueId == Guid.Empty || usedGuids.Contains(template.UniqueId));
usedGuids.Add(template.UniqueId);

_saveService.ImmediateSaveSync(template);
_saveService.ImmediateSaveSync(profile);
_saveService.ImmediateSaveSync(template);
_saveService.ImmediateSaveSync(profile);

_logger.Debug($"Migrated v3 profile {legacyProfile.ProfileName} to profile {profile.UniqueId} and template {template.UniqueId}");
File.Delete(file);
_logger.Debug($"Migrated v3 profile {legacyProfile.ProfileName} to profile {profile.UniqueId} and template {template.UniqueId}");
File.Delete(file);
}
catch(Exception ex)
{
anyMigrationFailures = true;
_logger.Error($"Error while migrating {file}: {ex}");
}
}

if (anyMigrationFailures)
_messageService.NotificationMessage($"Some of your Customize+ profiles failed to migrate correctly.\nDetails have been printed to Dalamud log (/xllog in chat).", NotificationType.Error);

_reloadEvent.Invoke(ReloadEvent.Type.ReloadAll);
}
}

0 comments on commit 28a08b5

Please sign in to comment.