PalacePal/Pal.Client/Configuration/ConfigurationV1.cs

168 lines
6.1 KiB
C#
Raw Normal View History

2023-02-15 01:38:04 +00:00
using Dalamud.Logging;
2022-12-22 00:52:52 +00:00
using Newtonsoft.Json;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
2022-12-11 14:22:41 +00:00
using System.Linq;
using System.Numerics;
2023-02-15 22:51:35 +00:00
using Dalamud.Plugin;
2023-02-15 01:38:04 +00:00
namespace Pal.Client.Configuration
{
2023-02-15 01:38:04 +00:00
[Obsolete]
2023-02-15 22:51:35 +00:00
public sealed class ConfigurationV1
{
2023-02-06 21:00:38 +00:00
public int Version { get; set; } = 6;
#region Saved configuration values
public bool FirstUse { get; set; } = true;
public EMode Mode { get; set; } = EMode.Offline;
2023-02-08 15:06:43 +00:00
public ERenderer Renderer { get; set; } = ERenderer.Splatoon;
[Obsolete]
public string? DebugAccountId { private get; set; }
[Obsolete]
public string? AccountId { private get; set; }
2022-12-11 14:22:41 +00:00
[Obsolete]
public Dictionary<string, Guid> AccountIds { private get; set; } = new();
public Dictionary<string, AccountInfo> Accounts { get; set; } = new();
public List<ImportHistoryEntry> ImportHistory { get; set; } = new();
public bool ShowTraps { get; set; } = true;
2023-02-11 20:10:45 +00:00
public Vector4 TrapColor { get; set; } = new(1, 0, 0, 0.4f);
public bool OnlyVisibleTrapsAfterPomander { get; set; } = true;
public bool ShowHoard { get; set; } = true;
2023-02-11 20:10:45 +00:00
public Vector4 HoardColor { get; set; } = new(0, 1, 1, 0.4f);
public bool OnlyVisibleHoardAfterPomander { get; set; } = true;
2023-02-11 20:10:45 +00:00
public bool ShowSilverCoffers { get; set; }
public Vector4 SilverCofferColor { get; set; } = new(1, 1, 1, 0.4f);
2022-10-25 21:31:35 +00:00
public bool FillSilverCoffers { get; set; } = true;
/// <summary>
/// Needs to be manually set.
/// </summary>
public string BetaKey { get; set; } = "";
#endregion
2023-02-15 22:51:35 +00:00
public void Migrate(DalamudPluginInterface pluginInterface)
{
if (Version == 1)
{
2022-12-11 14:22:41 +00:00
PluginLog.Information("Updating config to version 2");
if (DebugAccountId != null && Guid.TryParse(DebugAccountId, out Guid debugAccountId))
AccountIds["http://localhost:5145"] = debugAccountId;
if (AccountId != null && Guid.TryParse(AccountId, out Guid accountId))
AccountIds["https://pal.μ.tv"] = accountId;
Version = 2;
2023-02-15 22:51:35 +00:00
Save(pluginInterface);
}
2022-12-11 14:22:41 +00:00
if (Version == 2)
{
PluginLog.Information("Updating config to version 3");
Accounts = AccountIds.ToDictionary(x => x.Key, x => new AccountInfo
{
2023-02-15 01:38:04 +00:00
Id = x.Value.ToString() // encryption happens in V7 migration at latest
2022-12-11 14:22:41 +00:00
});
Version = 3;
2023-02-15 22:51:35 +00:00
Save(pluginInterface);
2022-12-11 14:22:41 +00:00
}
2022-12-22 00:52:52 +00:00
if (Version == 3)
{
Version = 4;
2023-02-15 22:51:35 +00:00
Save(pluginInterface);
2022-12-22 00:52:52 +00:00
}
if (Version == 4)
{
// 2.2 had a bug that would mark chests as traps, there's no easy way to detect this -- or clean this up.
// Not a problem for online players, but offline players might be fucked.
2023-02-15 22:17:19 +00:00
//bool changedAnyFile = false;
LocalState.ForEach(s =>
{
foreach (var marker in s.Markers)
marker.SinceVersion = "0.0";
var lastModified = File.GetLastWriteTimeUtc(s.GetSaveLocation());
if (lastModified >= new DateTime(2023, 2, 3, 0, 0, 0, DateTimeKind.Utc))
{
s.Backup(suffix: "bak");
s.Markers = new ConcurrentBag<Marker>(s.Markers.Where(m => m.SinceVersion != "0.0" || m.Type == Marker.EType.Hoard || m.WasImported));
s.Save();
2023-02-15 22:17:19 +00:00
//changedAnyFile = true;
}
else
{
// just add version information, nothing else
s.Save();
}
});
2023-02-15 22:17:19 +00:00
/*
// Only notify offline users - we can just re-download the backup markers from the server seamlessly.
if (Mode == EMode.Offline && changedAnyFile)
{
2023-02-11 20:10:45 +00:00
_ = new TickScheduler(delegate
{
2023-02-10 19:48:14 +00:00
Service.Chat.PalError("Due to a bug, some coffers were accidentally saved as traps. To fix the related display issue, locally cached data was cleaned up.");
Service.Chat.PrintError($"If you have any backup tools installed, please restore the contents of '{Service.PluginInterface.GetPluginConfigDirectory()}' to any backup from February 2, 2023 or before.");
Service.Chat.PrintError("You can also manually restore .json.bak files (by removing the '.bak') if you have not been in any deep dungeon since February 2, 2023.");
}, 2500);
}
2023-02-15 22:17:19 +00:00
*/
Version = 5;
2023-02-15 22:51:35 +00:00
Save(pluginInterface);
}
2023-02-06 21:00:38 +00:00
if (Version == 5)
{
LocalState.UpdateAll();
Version = 6;
2023-02-15 22:51:35 +00:00
Save(pluginInterface);
2023-02-06 21:00:38 +00:00
}
}
2023-02-15 22:51:35 +00:00
public void Save(DalamudPluginInterface pluginInterface)
{
2023-02-15 22:51:35 +00:00
File.WriteAllText(pluginInterface.ConfigFile.FullName, JsonConvert.SerializeObject(this, Formatting.Indented, new JsonSerializerSettings
2023-02-15 01:38:04 +00:00
{
TypeNameAssemblyFormatHandling = TypeNameAssemblyFormatHandling.Simple,
TypeNameHandling = TypeNameHandling.Objects
}));
}
2023-02-15 22:51:35 +00:00
public sealed class AccountInfo
2022-12-11 14:22:41 +00:00
{
2023-02-15 01:38:04 +00:00
public string? Id { get; set; }
2023-02-10 19:48:14 +00:00
public List<string> CachedRoles { get; set; } = new();
2022-12-11 14:22:41 +00:00
}
2023-02-15 22:51:35 +00:00
public sealed class ImportHistoryEntry
{
public Guid Id { get; set; }
public string? RemoteUrl { get; set; }
public DateTime ExportedAt { get; set; }
/// <summary>
/// Set when the file is imported locally.
/// </summary>
public DateTime ImportedAt { get; set; }
}
}
}