Code commit
This commit is contained in:
107
CustomizePlus/Configuration/Services/ConfigurationMigrator.cs
Normal file
107
CustomizePlus/Configuration/Services/ConfigurationMigrator.cs
Normal file
@@ -0,0 +1,107 @@
|
||||
using Dalamud.Interface.Internal.Notifications;
|
||||
using Newtonsoft.Json;
|
||||
using OtterGui.Classes;
|
||||
using OtterGui.Log;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using CustomizePlus.Core.Data;
|
||||
using CustomizePlus.Core.Services;
|
||||
using CustomizePlus.Configuration.Helpers;
|
||||
using CustomizePlus.Configuration.Data;
|
||||
using CustomizePlus.Core.Events;
|
||||
using CustomizePlus.Configuration.Data.Version3;
|
||||
|
||||
namespace CustomizePlus.Configuration.Services;
|
||||
|
||||
public class ConfigurationMigrator
|
||||
{
|
||||
private readonly SaveService _saveService;
|
||||
private readonly BackupService _backupService;
|
||||
private readonly MessageService _messageService;
|
||||
private readonly Logger _logger;
|
||||
private readonly ReloadEvent _reloadEvent;
|
||||
|
||||
public ConfigurationMigrator(
|
||||
SaveService saveService,
|
||||
BackupService backupService,
|
||||
MessageService messageService,
|
||||
Logger logger,
|
||||
ReloadEvent reloadEvent
|
||||
)
|
||||
{
|
||||
_saveService = saveService;
|
||||
_backupService = backupService;
|
||||
_messageService = messageService;
|
||||
_logger = logger;
|
||||
_reloadEvent = reloadEvent;
|
||||
}
|
||||
|
||||
public void Migrate(PluginConfiguration config)
|
||||
{
|
||||
var configVersion = config.Version;
|
||||
|
||||
if (configVersion >= Constants.ConfigurationVersion)
|
||||
return;
|
||||
|
||||
//V3 migration code
|
||||
if (configVersion < 3)
|
||||
{
|
||||
_messageService.NotificationMessage($"Unable to migrate your Customize+ configuration because it is too old. Manually install latest version of Customize+ 1.x to migrate your configuration to supported version first.", NotificationType.Error);
|
||||
return;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
private void MigrateV3ToV4()
|
||||
{
|
||||
_backupService.CreateV3Backup();
|
||||
|
||||
//I'm sorry, I'm too lazy so v3's enable root position setting is not getting migrated
|
||||
|
||||
var usedGuids = new HashSet<Guid>();
|
||||
foreach (var file in Directory.EnumerateFiles(_saveService.FileNames.ConfigDirectory, "*.profile", SearchOption.TopDirectoryOnly))
|
||||
{
|
||||
_logger.Debug($"Migrating v3 profile {file}");
|
||||
|
||||
var legacyProfile = JsonConvert.DeserializeObject<Version3Profile>(File.ReadAllText(file));
|
||||
if (legacyProfile == null)
|
||||
continue;
|
||||
|
||||
_logger.Debug($"v3 profile {file} loaded as {legacyProfile.ProfileName}");
|
||||
|
||||
(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);
|
||||
|
||||
do
|
||||
{
|
||||
template.UniqueId = Guid.NewGuid();
|
||||
}
|
||||
while (template.UniqueId == Guid.Empty || usedGuids.Contains(template.UniqueId));
|
||||
usedGuids.Add(template.UniqueId);
|
||||
|
||||
_saveService.ImmediateSave(template);
|
||||
_saveService.ImmediateSave(profile);
|
||||
|
||||
_logger.Debug($"Migrated v3 profile {legacyProfile.ProfileName} to profile {profile.UniqueId} and template {template.UniqueId}");
|
||||
File.Delete(file);
|
||||
}
|
||||
|
||||
_reloadEvent.Invoke(ReloadEvent.Type.ReloadAll);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
using CustomizePlus.Core.Services;
|
||||
using OtterGui.Log;
|
||||
using System.IO;
|
||||
|
||||
namespace CustomizePlus.Configuration.Services.Temporary;
|
||||
|
||||
internal class FantasiaPlusConfigMover
|
||||
{
|
||||
private readonly Logger _logger;
|
||||
private readonly FilenameService _filenameService;
|
||||
private readonly BackupService _backupService;
|
||||
|
||||
public FantasiaPlusConfigMover(
|
||||
BackupService backupService,
|
||||
Logger logger,
|
||||
FilenameService filenameService
|
||||
)
|
||||
{
|
||||
_backupService = backupService;
|
||||
_logger = logger;
|
||||
_filenameService = filenameService;
|
||||
}
|
||||
|
||||
public void MoveConfigsIfNeeded()
|
||||
{
|
||||
string fantasiaPlusConfig = _filenameService.ConfigFile.Replace("CustomizePlus.json", "FantasiaPlus.json");
|
||||
if (!File.Exists(_filenameService.ConfigFile.Replace("CustomizePlus.json", "FantasiaPlus.json")))
|
||||
return;
|
||||
|
||||
_logger.Information("Found FantasiaPlus configuration, moving it to CustomizePlus folders");
|
||||
if(File.Exists(_filenameService.ConfigFile) || Directory.Exists(_filenameService.ConfigDirectory))
|
||||
{
|
||||
_logger.Debug("Creating a backup of current c+ config");
|
||||
_backupService.CreateV3Backup("fantasia_plus_migration");
|
||||
}
|
||||
|
||||
_logger.Debug("Removing current c+ data");
|
||||
File.Delete(_filenameService.ConfigFile);
|
||||
Directory.Delete(_filenameService.ConfigDirectory, true);
|
||||
|
||||
_logger.Debug("Copying fantasia+ data");
|
||||
File.Copy(fantasiaPlusConfig, _filenameService.ConfigFile);
|
||||
|
||||
string fantasiaPlusDirectory = _filenameService.ConfigDirectory.Replace("CustomizePlus", "FantasiaPlus");
|
||||
CopyDirectory(fantasiaPlusDirectory, _filenameService.ConfigDirectory, true);
|
||||
|
||||
_logger.Debug("Deleting fantasia+ data");
|
||||
File.Delete(fantasiaPlusConfig);
|
||||
Directory.Delete(fantasiaPlusDirectory, true);
|
||||
|
||||
_logger.Information("Done moving fantasia+ configuration");
|
||||
}
|
||||
|
||||
static void CopyDirectory(string sourceDir, string destinationDir, bool recursive)
|
||||
{
|
||||
// Get information about the source directory
|
||||
var dir = new DirectoryInfo(sourceDir);
|
||||
|
||||
// Check if the source directory exists
|
||||
if (!dir.Exists)
|
||||
throw new DirectoryNotFoundException($"Source directory not found: {dir.FullName}");
|
||||
|
||||
// Cache directories before we start copying
|
||||
DirectoryInfo[] dirs = dir.GetDirectories();
|
||||
|
||||
// Create the destination directory
|
||||
Directory.CreateDirectory(destinationDir);
|
||||
|
||||
// Get the files in the source directory and copy to the destination directory
|
||||
foreach (FileInfo file in dir.GetFiles())
|
||||
{
|
||||
string targetFilePath = Path.Combine(destinationDir, file.Name);
|
||||
file.CopyTo(targetFilePath);
|
||||
}
|
||||
|
||||
// If recursive and copying subdirectories, recursively call this method
|
||||
if (recursive)
|
||||
{
|
||||
foreach (DirectoryInfo subDir in dirs)
|
||||
{
|
||||
string newDestinationDir = Path.Combine(destinationDir, subDir.Name);
|
||||
CopyDirectory(subDir.FullName, newDestinationDir, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user