PalacePal/Pal.Client/Configuration/ConfigurationManager.cs

156 lines
5.4 KiB
C#
Raw Normal View History

2023-02-15 22:17:19 +00:00
using System;
using System.IO;
2023-02-15 01:38:04 +00:00
using System.Linq;
using System.Text;
using System.Text.Encodings.Web;
2023-02-15 01:38:04 +00:00
using System.Text.Json;
using Dalamud.Logging;
using Dalamud.Plugin;
using ImGuiNET;
2023-02-16 18:51:54 +00:00
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Pal.Client.Configuration.Legacy;
2023-02-16 18:51:54 +00:00
using Pal.Client.Database;
using NJson = Newtonsoft.Json;
2023-02-15 01:38:04 +00:00
2023-03-30 20:01:43 +00:00
namespace Pal.Client.Configuration;
internal sealed class ConfigurationManager
2023-02-15 01:38:04 +00:00
{
2023-03-30 20:01:43 +00:00
private readonly ILogger<ConfigurationManager> _logger;
private readonly DalamudPluginInterface _pluginInterface;
private readonly IServiceProvider _serviceProvider;
2023-02-15 01:38:04 +00:00
2023-03-30 20:01:43 +00:00
public event EventHandler<IPalacePalConfiguration>? Saved;
2023-02-15 22:17:19 +00:00
2023-03-30 20:01:43 +00:00
public ConfigurationManager(ILogger<ConfigurationManager> logger, DalamudPluginInterface pluginInterface,
IServiceProvider serviceProvider)
{
_logger = logger;
_pluginInterface = pluginInterface;
_serviceProvider = serviceProvider;
}
2023-02-15 01:38:04 +00:00
2023-03-30 20:01:43 +00:00
private string ConfigPath =>
Path.Join(_pluginInterface.GetPluginConfigDirectory(), ConfigurationData.ConfigFileName);
2023-02-15 01:38:04 +00:00
2023-03-30 20:01:43 +00:00
public IPalacePalConfiguration Load()
{
if (!File.Exists(ConfigPath))
2023-02-15 12:00:00 +00:00
{
2023-03-30 20:01:43 +00:00
_logger.LogInformation("No config file exists, creating one");
Save(new ConfigurationV7(), false);
2023-02-15 12:00:00 +00:00
}
2023-03-30 20:01:43 +00:00
return JsonSerializer.Deserialize<ConfigurationV7>(File.ReadAllText(ConfigPath, Encoding.UTF8)) ??
new ConfigurationV7();
}
public void Save(IConfigurationInConfigDirectory config, bool queue = true)
{
File.WriteAllText(ConfigPath,
JsonSerializer.Serialize(config, config.GetType(),
new JsonSerializerOptions
{ WriteIndented = true, Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping }),
Encoding.UTF8);
if (queue && config is ConfigurationV7 v7)
Saved?.Invoke(this, v7);
}
2023-02-15 12:00:00 +00:00
2023-02-15 01:38:04 +00:00
#pragma warning disable CS0612
#pragma warning disable CS0618
2023-03-30 20:01:43 +00:00
public void Migrate()
{
if (_pluginInterface.ConfigFile.Exists)
2023-02-15 01:38:04 +00:00
{
2023-03-30 20:01:43 +00:00
_logger.LogInformation("Migrating config file from v1-v6 format");
2023-02-15 01:38:04 +00:00
2023-03-30 20:01:43 +00:00
ConfigurationV1 configurationV1 =
NJson.JsonConvert.DeserializeObject<ConfigurationV1>(
File.ReadAllText(_pluginInterface.ConfigFile.FullName)) ?? new ConfigurationV1();
configurationV1.Migrate(_pluginInterface,
_serviceProvider.GetRequiredService<ILogger<ConfigurationV1>>());
configurationV1.Save(_pluginInterface);
2023-02-15 01:38:04 +00:00
2023-03-30 20:01:43 +00:00
var v7 = MigrateToV7(configurationV1);
Save(v7, queue: false);
2023-02-15 01:38:04 +00:00
2023-03-30 20:01:43 +00:00
using (var scope = _serviceProvider.CreateScope())
{
using var dbContext = scope.ServiceProvider.GetRequiredService<PalClientContext>();
dbContext.Imports.RemoveRange(dbContext.Imports);
2023-02-16 18:51:54 +00:00
2023-03-30 20:01:43 +00:00
foreach (var importHistory in configurationV1.ImportHistory)
{
_logger.LogInformation("Migrating import {Id}", importHistory.Id);
dbContext.Imports.Add(new ImportHistory
2023-02-16 18:51:54 +00:00
{
2023-03-30 20:01:43 +00:00
Id = importHistory.Id,
RemoteUrl = importHistory.RemoteUrl?.Replace(".μ.tv", ".liza.sh"),
ExportedAt = importHistory.ExportedAt,
ImportedAt = importHistory.ImportedAt
});
2023-02-16 18:51:54 +00:00
}
2023-03-30 20:01:43 +00:00
dbContext.SaveChanges();
2023-02-15 01:38:04 +00:00
}
2023-03-30 20:01:43 +00:00
File.Move(_pluginInterface.ConfigFile.FullName, _pluginInterface.ConfigFile.FullName + ".old", true);
2023-02-15 01:38:04 +00:00
}
2023-03-30 20:01:43 +00:00
}
2023-02-15 01:38:04 +00:00
2023-03-30 20:01:43 +00:00
private ConfigurationV7 MigrateToV7(ConfigurationV1 v1)
{
ConfigurationV7 v7 = new()
2023-02-15 01:38:04 +00:00
{
2023-03-30 20:01:43 +00:00
Version = 7,
FirstUse = v1.FirstUse,
Mode = v1.Mode,
BetaKey = v1.BetaKey,
2023-02-15 01:38:04 +00:00
2023-03-30 20:01:43 +00:00
DeepDungeons = new DeepDungeonConfiguration
{
Traps = new MarkerConfiguration
2023-02-15 01:38:04 +00:00
{
2023-03-30 20:01:43 +00:00
Show = v1.ShowTraps,
Color = ImGui.ColorConvertFloat4ToU32(v1.TrapColor),
OnlyVisibleAfterPomander = v1.OnlyVisibleTrapsAfterPomander,
Fill = false
},
HoardCoffers = new MarkerConfiguration
{
Show = v1.ShowHoard,
Color = ImGui.ColorConvertFloat4ToU32(v1.HoardColor),
OnlyVisibleAfterPomander = v1.OnlyVisibleHoardAfterPomander,
Fill = false
},
SilverCoffers = new MarkerConfiguration
{
Show = v1.ShowSilverCoffers,
Color = ImGui.ColorConvertFloat4ToU32(v1.SilverCofferColor),
OnlyVisibleAfterPomander = false,
Fill = v1.FillSilverCoffers
2023-02-15 01:38:04 +00:00
}
}
2023-03-30 20:01:43 +00:00
};
2023-02-15 01:38:04 +00:00
2023-03-30 20:01:43 +00:00
foreach (var (server, oldAccount) in v1.Accounts)
{
string? accountId = oldAccount.Id;
if (string.IsNullOrEmpty(accountId))
continue;
2023-02-15 01:38:04 +00:00
2023-03-30 20:01:43 +00:00
string serverName = server.Replace(".μ.tv", ".liza.sh");
IAccountConfiguration newAccount = v7.CreateAccount(serverName, accountId);
newAccount.CachedRoles = oldAccount.CachedRoles.ToList();
2023-02-15 01:38:04 +00:00
}
2023-03-30 20:01:43 +00:00
// TODO Migrate ImportHistory
return v7;
}
2023-02-15 01:38:04 +00:00
#pragma warning restore CS0618
#pragma warning restore CS0612
}