Code commit

This commit is contained in:
RisaDev
2024-01-06 01:21:41 +03:00
parent a7d7297c59
commit a486dd2c96
90 changed files with 11576 additions and 0 deletions

View File

@@ -0,0 +1,150 @@
using System;
using System.Collections.Generic;
using System.IO;
using Dalamud.Configuration;
using Dalamud.Interface.Internal.Notifications;
using Newtonsoft.Json;
using OtterGui.Classes;
using OtterGui.Log;
using OtterGui.Widgets;
using ErrorEventArgs = Newtonsoft.Json.Serialization.ErrorEventArgs;
using CustomizePlus.Core.Services;
using CustomizePlus.Core.Data;
using CustomizePlus.Configuration.Services;
using CustomizePlus.Game.Services;
using CustomizePlus.UI.Windows;
namespace CustomizePlus.Configuration.Data;
[Serializable]
public class PluginConfiguration : IPluginConfiguration, ISavable
{
public const int CurrentVersion = Constants.ConfigurationVersion;
public int Version { get; set; } = CurrentVersion;
public bool PluginEnabled { get; set; } = true;
public bool DebuggingModeEnabled { get; set; }
/// <summary>
/// Id of the default profile applied to all characters without any profile. Can be set to Empty to disable this feature.
/// </summary>
public Guid DefaultProfile { get; set; } = Guid.Empty;
[Serializable]
public class ChangelogSettingsEntries
{
public int LastSeenVersion { get; set; } = CPlusChangeLog.LastChangelogVersion;
public ChangeLogDisplayType ChangeLogDisplayType { get; set; } = ChangeLogDisplayType.New;
}
public ChangelogSettingsEntries ChangelogSettings { get; set; } = new();
[Serializable]
public class UISettingsEntries
{
public DoubleModifier DeleteTemplateModifier { get; set; } = new(ModifierHotkey.Control, ModifierHotkey.Shift);
public bool FoldersDefaultOpen { get; set; } = true;
public bool HideWindowInCutscene { get; set; } = true;
public bool IncognitoMode { get; set; } = false;
public List<string> ViewedMessageWindows { get; set; } = new();
}
public UISettingsEntries UISettings { get; set; } = new();
[Serializable]
public class EditorConfigurationEntries
{
/// <summary>
/// Hides root position from the UI. DOES NOT DISABLE LOADING IT FROM THE CONFIG!
/// </summary>
public bool RootPositionEditingEnabled { get; set; } = false;
public bool ShowLiveBones { get; set; } = true;
public bool BoneMirroringEnabled { get; set; } = false;
public bool LimitLookupToOwnedObjects { get; set; } = false;
public string? PreviewCharacterName { get; set; } = null;
public int EditorValuesPrecision { get; set; } = 3;
public BoneAttribute EditorMode { get; set; } = BoneAttribute.Position;
}
public EditorConfigurationEntries EditorConfiguration { get; set; } = new();
[JsonIgnore]
private readonly SaveService _saveService;
[JsonIgnore]
private readonly Logger _logger;
[JsonIgnore]
private readonly ChatService _chatService;
[JsonIgnore]
private readonly MessageService _messageService;
public PluginConfiguration(
SaveService saveService,
Logger logger,
ChatService chatService,
MessageService messageService,
ConfigurationMigrator migrator)
{
_saveService = saveService;
_logger = logger;
_chatService = chatService;
_messageService = messageService;
Load(migrator);
}
public void Load(ConfigurationMigrator migrator)
{
static void HandleDeserializationError(object? sender, ErrorEventArgs errorArgs)
{
Plugin.Logger.Error(
$"Error parsing configuration at {errorArgs.ErrorContext.Path}, using default or migrating:\n{errorArgs.ErrorContext.Error}");
errorArgs.ErrorContext.Handled = true;
}
if (!File.Exists(_saveService.FileNames.ConfigFile))
return;
try
{
var text = File.ReadAllText(_saveService.FileNames.ConfigFile);
JsonConvert.PopulateObject(text, this, new JsonSerializerSettings
{
Error = HandleDeserializationError,
});
}
catch (Exception ex)
{
_messageService.NotificationMessage(ex,
"Error reading configuration, reverting to default.\nYou may be able to restore your configuration using the rolling backups in the XIVLauncher/backups/CustomizePlus directory.",
"Error reading configuration", NotificationType.Error);
}
migrator.Migrate(this);
}
public string ToFilename(FilenameService fileNames)
=> fileNames.ConfigFile;
public void Save(StreamWriter writer)
{
using var jWriter = new JsonTextWriter(writer) { Formatting = Formatting.Indented };
var serializer = new JsonSerializer { Formatting = Formatting.Indented };
serializer.Serialize(jWriter, this);
}
public void Save()
=> _saveService.DelaySave(this);
}

View File

@@ -0,0 +1,60 @@
using System;
using System.Numerics;
namespace CustomizePlus.Configuration.Data.Version3;
public class V3BoneTransform
{
private Vector3 _translation;
public Vector3 Translation
{
get => _translation;
set => _translation = ClampVector(value);
}
private Vector3 _rotation;
public Vector3 Rotation
{
get => _rotation;
set => _rotation = ClampAngles(value);
}
private Vector3 _scaling;
public Vector3 Scaling
{
get => _scaling;
set => _scaling = ClampVector(value);
}
/// <summary>
/// Clamp all vector values to be within allowed limits.
/// </summary>
private Vector3 ClampVector(Vector3 vector)
{
return new Vector3
{
X = Math.Clamp(vector.X, -512, 512),
Y = Math.Clamp(vector.Y, -512, 512),
Z = Math.Clamp(vector.Z, -512, 512)
};
}
private static Vector3 ClampAngles(Vector3 rotVec)
{
static float Clamp(float angle)
{
if (angle > 180)
angle -= 360;
else if (angle < -180)
angle += 360;
return angle;
}
rotVec.X = Clamp(rotVec.X);
rotVec.Y = Clamp(rotVec.Y);
rotVec.Z = Clamp(rotVec.Z);
return rotVec;
}
}

View File

@@ -0,0 +1,24 @@
using CustomizePlus.Core.Data;
using System;
using System.Collections.Generic;
namespace CustomizePlus.Configuration.Data.Version3;
/// <summary>
/// Encapsulates the user-controlled aspects of a character profile, ie all of
/// the information that gets saved to disk by the plugin.
/// </summary>
[Serializable]
public sealed class Version3Profile
{
public string CharacterName { get; set; } = "Default";
public string ProfileName { get; set; } = "Profile";
public nint? Address { get; set; } = null;
public bool OwnedOnly { get; set; } = false;
public int ConfigVersion { get; set; } = Constants.ConfigurationVersion;
public bool Enabled { get; set; }
public DateTime CreationDate { get; set; } = DateTime.Now;
public DateTime ModifiedDate { get; set; } = DateTime.Now;
public Dictionary<string, V3BoneTransform> Bones { get; init; } = new();
}

View File

@@ -0,0 +1,43 @@
using CustomizePlus.Configuration.Data.Version3;
using CustomizePlus.Core.Data;
using CustomizePlus.Profiles.Data;
using CustomizePlus.Templates.Data;
using System;
using System.Collections.Generic;
namespace CustomizePlus.Configuration.Helpers;
internal static class V3ProfileToV4Converter
{
public static (Profile, Template) Convert(Version3Profile v3Profile)
{
var profile = new Profile
{
Name = $"{v3Profile.ProfileName} - {v3Profile.CharacterName}",
CharacterName = v3Profile.CharacterName,
CreationDate = v3Profile.CreationDate,
ModifiedDate = DateTimeOffset.UtcNow,
Enabled = v3Profile.Enabled,
LimitLookupToOwnedObjects = v3Profile.OwnedOnly,
UniqueId = Guid.NewGuid(),
Templates = new List<Template>(1)
};
var template = new Template
{
Name = $"{profile.Name}'s template",
CreationDate = profile.CreationDate,
ModifiedDate = profile.ModifiedDate,
UniqueId = Guid.NewGuid(),
Bones = new Dictionary<string, BoneTransform>(v3Profile.Bones.Count)
};
foreach (var kvPair in v3Profile.Bones)
template.Bones.Add(kvPair.Key,
new BoneTransform { Translation = kvPair.Value.Translation, Rotation = kvPair.Value.Rotation, Scaling = kvPair.Value.Scaling });
profile.Templates.Add(template);
return (profile, template);
}
}

View File

@@ -0,0 +1,39 @@
using CustomizePlus.Configuration.Data.Version3;
using CustomizePlus.Profiles.Data;
using System;
using System.Collections.Generic;
namespace CustomizePlus.Configuration.Helpers;
internal static class V4ProfileToV3Converter
{
public static Version3Profile Convert(Profile v4Profile)
{
var profile = new Version3Profile
{
ProfileName = v4Profile.Name,
CharacterName = v4Profile.CharacterName,
CreationDate = v4Profile.CreationDate.DateTime,
ModifiedDate = DateTime.UtcNow,
Enabled = v4Profile.Enabled,
OwnedOnly = v4Profile.LimitLookupToOwnedObjects,
ConfigVersion = 3,
Bones = new Dictionary<string, V3BoneTransform>()
};
foreach (var template in v4Profile.Templates)
{
foreach (var kvPair in template.Bones) //not super optimal but whatever
{
profile.Bones[kvPair.Key] = new V3BoneTransform
{
Translation = kvPair.Value.Translation,
Rotation = kvPair.Value.Rotation,
Scaling = kvPair.Value.Scaling
};
}
}
return profile;
}
}

View 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);
}
}

View File

@@ -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);
}
}
}
}