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;
|
2023-02-15 09:20:25 +00:00
|
|
|
|
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 Pal.Client.Database;
|
2023-02-15 09:20:25 +00:00
|
|
|
|
using NJson = Newtonsoft.Json;
|
2023-02-15 01:38:04 +00:00
|
|
|
|
|
|
|
|
|
namespace Pal.Client.Configuration
|
|
|
|
|
{
|
2023-02-15 22:51:35 +00:00
|
|
|
|
internal sealed class ConfigurationManager
|
2023-02-15 01:38:04 +00:00
|
|
|
|
{
|
|
|
|
|
private readonly DalamudPluginInterface _pluginInterface;
|
2023-02-16 18:51:54 +00:00
|
|
|
|
private readonly IServiceProvider _serviceProvider;
|
2023-02-15 01:38:04 +00:00
|
|
|
|
|
2023-02-15 22:17:19 +00:00
|
|
|
|
public event EventHandler<IPalacePalConfiguration>? Saved;
|
|
|
|
|
|
2023-02-16 18:51:54 +00:00
|
|
|
|
public ConfigurationManager(DalamudPluginInterface pluginInterface, IServiceProvider serviceProvider)
|
2023-02-15 01:38:04 +00:00
|
|
|
|
{
|
|
|
|
|
_pluginInterface = pluginInterface;
|
2023-02-16 18:51:54 +00:00
|
|
|
|
_serviceProvider = serviceProvider;
|
2023-02-15 22:17:19 +00:00
|
|
|
|
|
|
|
|
|
Migrate();
|
2023-02-15 01:38:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-15 22:17:19 +00:00
|
|
|
|
private string ConfigPath => Path.Join(_pluginInterface.GetPluginConfigDirectory(), "palace-pal.config.json");
|
2023-02-15 01:38:04 +00:00
|
|
|
|
|
2023-02-15 12:00:00 +00:00
|
|
|
|
public IPalacePalConfiguration Load()
|
|
|
|
|
{
|
|
|
|
|
return JsonSerializer.Deserialize<ConfigurationV7>(File.ReadAllText(ConfigPath, Encoding.UTF8)) ??
|
|
|
|
|
new ConfigurationV7();
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-15 22:17:19 +00:00
|
|
|
|
public void Save(IConfigurationInConfigDirectory config, bool queue = true)
|
2023-02-15 12:00:00 +00:00
|
|
|
|
{
|
|
|
|
|
File.WriteAllText(ConfigPath,
|
2023-02-15 22:17:19 +00:00
|
|
|
|
JsonSerializer.Serialize(config, config.GetType(),
|
|
|
|
|
new JsonSerializerOptions
|
|
|
|
|
{ WriteIndented = true, Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping }),
|
2023-02-15 12:00:00 +00:00
|
|
|
|
Encoding.UTF8);
|
2023-02-16 12:17:55 +00:00
|
|
|
|
|
2023-02-15 22:17:19 +00:00
|
|
|
|
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-02-15 22:17:19 +00:00
|
|
|
|
private void Migrate()
|
2023-02-15 01:38:04 +00:00
|
|
|
|
{
|
|
|
|
|
if (_pluginInterface.ConfigFile.Exists)
|
|
|
|
|
{
|
|
|
|
|
PluginLog.Information("Migrating config file from v1-v6 format");
|
|
|
|
|
|
|
|
|
|
ConfigurationV1 configurationV1 =
|
2023-02-15 09:20:25 +00:00
|
|
|
|
NJson.JsonConvert.DeserializeObject<ConfigurationV1>(
|
2023-02-15 01:38:04 +00:00
|
|
|
|
File.ReadAllText(_pluginInterface.ConfigFile.FullName)) ?? new ConfigurationV1();
|
2023-02-15 22:51:35 +00:00
|
|
|
|
configurationV1.Migrate(_pluginInterface);
|
|
|
|
|
configurationV1.Save(_pluginInterface);
|
2023-02-15 01:38:04 +00:00
|
|
|
|
|
|
|
|
|
var v7 = MigrateToV7(configurationV1);
|
2023-02-15 22:17:19 +00:00
|
|
|
|
Save(v7, queue: false);
|
2023-02-15 01:38:04 +00:00
|
|
|
|
|
2023-02-16 18:51:54 +00:00
|
|
|
|
using (var scope = _serviceProvider.CreateScope())
|
|
|
|
|
{
|
|
|
|
|
using var dbContext = scope.ServiceProvider.GetRequiredService<PalClientContext>();
|
|
|
|
|
dbContext.Imports.RemoveRange(dbContext.Imports);
|
|
|
|
|
|
|
|
|
|
foreach (var importHistory in configurationV1.ImportHistory)
|
|
|
|
|
{
|
|
|
|
|
PluginLog.Information($"Migrating import {importHistory.Id}");
|
|
|
|
|
dbContext.Imports.Add(new ImportHistory
|
|
|
|
|
{
|
|
|
|
|
Id = importHistory.Id,
|
|
|
|
|
RemoteUrl = importHistory.RemoteUrl?.Replace(".μ.tv", ".liza.sh"),
|
|
|
|
|
ExportedAt = importHistory.ExportedAt,
|
|
|
|
|
ImportedAt = importHistory.ImportedAt
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dbContext.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-16 12:17:55 +00:00
|
|
|
|
File.Move(_pluginInterface.ConfigFile.FullName, _pluginInterface.ConfigFile.FullName + ".old", true);
|
2023-02-15 01:38:04 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private ConfigurationV7 MigrateToV7(ConfigurationV1 v1)
|
|
|
|
|
{
|
|
|
|
|
ConfigurationV7 v7 = new()
|
|
|
|
|
{
|
|
|
|
|
Version = 7,
|
|
|
|
|
FirstUse = v1.FirstUse,
|
|
|
|
|
Mode = v1.Mode,
|
|
|
|
|
BetaKey = v1.BetaKey,
|
|
|
|
|
|
|
|
|
|
DeepDungeons = new DeepDungeonConfiguration
|
|
|
|
|
{
|
|
|
|
|
Traps = new MarkerConfiguration
|
|
|
|
|
{
|
|
|
|
|
Show = v1.ShowTraps,
|
|
|
|
|
Color = ImGui.ColorConvertFloat4ToU32(v1.TrapColor),
|
2023-02-15 22:51:35 +00:00
|
|
|
|
OnlyVisibleAfterPomander = v1.OnlyVisibleTrapsAfterPomander,
|
2023-02-15 01:38:04 +00:00
|
|
|
|
Fill = false
|
|
|
|
|
},
|
|
|
|
|
HoardCoffers = new MarkerConfiguration
|
|
|
|
|
{
|
|
|
|
|
Show = v1.ShowHoard,
|
|
|
|
|
Color = ImGui.ColorConvertFloat4ToU32(v1.HoardColor),
|
2023-02-15 22:51:35 +00:00
|
|
|
|
OnlyVisibleAfterPomander = v1.OnlyVisibleHoardAfterPomander,
|
2023-02-15 01:38:04 +00:00
|
|
|
|
Fill = false
|
|
|
|
|
},
|
|
|
|
|
SilverCoffers = new MarkerConfiguration
|
|
|
|
|
{
|
|
|
|
|
Show = v1.ShowSilverCoffers,
|
|
|
|
|
Color = ImGui.ColorConvertFloat4ToU32(v1.SilverCofferColor),
|
2023-02-15 22:51:35 +00:00
|
|
|
|
OnlyVisibleAfterPomander = false,
|
2023-02-15 01:38:04 +00:00
|
|
|
|
Fill = v1.FillSilverCoffers
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
foreach (var (server, oldAccount) in v1.Accounts)
|
|
|
|
|
{
|
|
|
|
|
string? accountId = oldAccount.Id;
|
|
|
|
|
if (string.IsNullOrEmpty(accountId))
|
|
|
|
|
continue;
|
|
|
|
|
|
2023-02-15 09:20:25 +00:00
|
|
|
|
string serverName = server.Replace(".μ.tv", ".liza.sh");
|
|
|
|
|
IAccountConfiguration newAccount = v7.CreateAccount(serverName, accountId);
|
2023-02-15 01:38:04 +00:00
|
|
|
|
newAccount.CachedRoles = oldAccount.CachedRoles.ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO Migrate ImportHistory
|
|
|
|
|
|
|
|
|
|
return v7;
|
|
|
|
|
}
|
|
|
|
|
#pragma warning restore CS0618
|
|
|
|
|
#pragma warning restore CS0612
|
|
|
|
|
}
|
|
|
|
|
}
|