2023-02-15 01:38:04 +00:00
using Dalamud.Logging ;
2023-02-05 01:07:58 +00:00
using ECommons.Schedulers ;
2022-12-22 00:52:52 +00:00
using Newtonsoft.Json ;
2022-12-22 00:01:09 +00:00
using Pal.Client.Scheduled ;
2022-10-31 15:21:27 +00:00
using System ;
2023-02-05 01:07:58 +00:00
using System.Collections.Concurrent ;
2022-10-31 15:21:27 +00:00
using System.Collections.Generic ;
2023-02-05 01:07:58 +00:00
using System.IO ;
2022-12-11 14:22:41 +00:00
using System.Linq ;
2022-10-23 02:38:58 +00:00
using System.Numerics ;
2023-02-10 19:48:14 +00:00
using Pal.Client.Extensions ;
2022-10-23 02:38:58 +00:00
2023-02-15 01:38:04 +00:00
namespace Pal.Client.Configuration
2022-10-23 02:38:58 +00:00
{
2023-02-15 01:38:04 +00:00
[Obsolete]
public class ConfigurationV1
2022-10-23 02:38:58 +00:00
{
2023-02-06 21:00:38 +00:00
public int Version { get ; set ; } = 6 ;
2022-10-23 02:38:58 +00:00
#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 ;
2022-10-31 15:21:27 +00:00
[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 ( ) ;
2022-10-23 02:38:58 +00:00
2022-12-22 00:01:09 +00:00
public List < ImportHistoryEntry > ImportHistory { get ; set ; } = new ( ) ;
2022-10-23 02:38:58 +00:00
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 ) ;
2022-10-26 21:38:29 +00:00
public bool OnlyVisibleTrapsAfterPomander { get ; set ; } = true ;
2022-10-23 02:38:58 +00:00
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 ) ;
2022-10-26 21:38:29 +00:00
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 ;
2022-10-23 02:38:58 +00:00
2022-12-22 00:01:09 +00:00
/// <summary>
/// Needs to be manually set.
/// </summary>
public string BetaKey { get ; set ; } = "" ;
#endregion
2022-10-23 02:38:58 +00:00
2022-10-31 15:21:27 +00:00
public void Migrate ( )
{
if ( Version = = 1 )
{
2022-12-11 14:22:41 +00:00
PluginLog . Information ( "Updating config to version 2" ) ;
2022-10-31 15:21:27 +00:00
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 ;
Save ( ) ;
}
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 ;
Save ( ) ;
}
2022-12-22 00:52:52 +00:00
if ( Version = = 3 )
{
Version = 4 ;
Save ( ) ;
}
2023-02-05 01:07:58 +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.
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 ( ) ;
changedAnyFile = true ;
}
else
{
// just add version information, nothing else
s . Save ( ) ;
}
} ) ;
// 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-05 01:07:58 +00:00
{
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." ) ;
2023-02-05 01:07:58 +00:00
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 ) ;
}
Version = 5 ;
Save ( ) ;
}
2023-02-06 21:00:38 +00:00
if ( Version = = 5 )
{
LocalState . UpdateAll ( ) ;
Version = 6 ;
Save ( ) ;
}
2022-10-31 15:21:27 +00:00
}
2022-10-23 02:38:58 +00:00
public void Save ( )
{
2023-02-15 01:38:04 +00:00
File . WriteAllText ( Service . PluginInterface . ConfigFile . FullName , JsonConvert . SerializeObject ( this , Formatting . Indented , new JsonSerializerSettings
{
TypeNameAssemblyFormatHandling = TypeNameAssemblyFormatHandling . Simple ,
TypeNameHandling = TypeNameHandling . Objects
} ) ) ;
2022-12-22 00:01:09 +00:00
Service . Plugin . EarlyEventQueue . Enqueue ( new QueuedConfigUpdate ( ) ) ;
2022-10-23 02:38:58 +00:00
}
2022-12-11 14:22:41 +00:00
public class AccountInfo
{
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
}
2022-12-22 00:01:09 +00:00
public 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 ; }
}
2022-10-23 02:38:58 +00:00
}
}