Add MCDO (#80)
* update api * mcd online editor impl * most of chara data hub impl * some state of things * some refactoring * random bullshit go * more nearby impl * add uid to peformance msg * cleanup/homogenization * some split, update nuget packages * migrate to latest packages where possible, remove lz4net, do some split, idk * some polish and cleanup * more cleanup, beautification, etc. * fixes and cleanups --------- Co-authored-by: Stanley Dimant <root.darkarchon@outlook.com>
This commit is contained in:
@@ -1,70 +0,0 @@
|
||||
using MareSynchronos.API.Data;
|
||||
using MareSynchronos.API.Data.Enum;
|
||||
using MareSynchronos.FileCache;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace MareSynchronos.PlayerData.Export;
|
||||
|
||||
public record MareCharaFileData
|
||||
{
|
||||
public string Description { get; set; } = string.Empty;
|
||||
public string GlamourerData { get; set; } = string.Empty;
|
||||
public string CustomizePlusData { get; set; } = string.Empty;
|
||||
public string ManipulationData { get; set; } = string.Empty;
|
||||
public List<FileData> Files { get; set; } = [];
|
||||
public List<FileSwap> FileSwaps { get; set; } = [];
|
||||
|
||||
public MareCharaFileData() { }
|
||||
public MareCharaFileData(FileCacheManager manager, string description, CharacterData dto)
|
||||
{
|
||||
Description = description;
|
||||
|
||||
if (dto.GlamourerData.TryGetValue(ObjectKind.Player, out var glamourerData))
|
||||
{
|
||||
GlamourerData = glamourerData;
|
||||
}
|
||||
|
||||
dto.CustomizePlusData.TryGetValue(ObjectKind.Player, out var customizePlusData);
|
||||
CustomizePlusData = customizePlusData ?? string.Empty;
|
||||
ManipulationData = dto.ManipulationData;
|
||||
|
||||
if (dto.FileReplacements.TryGetValue(ObjectKind.Player, out var fileReplacements))
|
||||
{
|
||||
var grouped = fileReplacements.GroupBy(f => f.Hash, StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
foreach (var file in grouped)
|
||||
{
|
||||
if (string.IsNullOrEmpty(file.Key))
|
||||
{
|
||||
foreach (var item in file)
|
||||
{
|
||||
FileSwaps.Add(new FileSwap(item.GamePaths, item.FileSwapPath));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
var filePath = manager.GetFileCacheByHash(file.First().Hash)?.ResolvedFilepath;
|
||||
if (filePath != null)
|
||||
{
|
||||
Files.Add(new FileData(file.SelectMany(f => f.GamePaths), (int)new FileInfo(filePath).Length, file.First().Hash));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public byte[] ToByteArray()
|
||||
{
|
||||
return Encoding.UTF8.GetBytes(JsonSerializer.Serialize(this));
|
||||
}
|
||||
|
||||
public static MareCharaFileData FromByteArray(byte[] data)
|
||||
{
|
||||
return JsonSerializer.Deserialize<MareCharaFileData>(Encoding.UTF8.GetString(data))!;
|
||||
}
|
||||
|
||||
public record FileSwap(IEnumerable<string> GamePaths, string FileSwapPath);
|
||||
|
||||
public record FileData(IEnumerable<string> GamePaths, int Length, string Hash);
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
using MareSynchronos.API.Data;
|
||||
using MareSynchronos.FileCache;
|
||||
|
||||
namespace MareSynchronos.PlayerData.Export;
|
||||
|
||||
internal sealed class MareCharaFileDataFactory
|
||||
{
|
||||
private readonly FileCacheManager _fileCacheManager;
|
||||
|
||||
public MareCharaFileDataFactory(FileCacheManager fileCacheManager)
|
||||
{
|
||||
_fileCacheManager = fileCacheManager;
|
||||
}
|
||||
|
||||
public MareCharaFileData Create(string description, CharacterData characterCacheDto)
|
||||
{
|
||||
return new MareCharaFileData(_fileCacheManager, description, characterCacheDto);
|
||||
}
|
||||
}
|
||||
@@ -1,54 +0,0 @@
|
||||
namespace MareSynchronos.PlayerData.Export;
|
||||
|
||||
public record MareCharaFileHeader(byte Version, MareCharaFileData CharaFileData)
|
||||
{
|
||||
public static readonly byte CurrentVersion = 1;
|
||||
|
||||
public byte Version { get; set; } = Version;
|
||||
public MareCharaFileData CharaFileData { get; set; } = CharaFileData;
|
||||
public string FilePath { get; private set; } = string.Empty;
|
||||
|
||||
public void WriteToStream(BinaryWriter writer)
|
||||
{
|
||||
writer.Write('M');
|
||||
writer.Write('C');
|
||||
writer.Write('D');
|
||||
writer.Write('F');
|
||||
writer.Write(Version);
|
||||
var charaFileDataArray = CharaFileData.ToByteArray();
|
||||
writer.Write(charaFileDataArray.Length);
|
||||
writer.Write(charaFileDataArray);
|
||||
}
|
||||
|
||||
public static MareCharaFileHeader? FromBinaryReader(string path, BinaryReader reader)
|
||||
{
|
||||
var chars = new string(reader.ReadChars(4));
|
||||
if (!string.Equals(chars, "MCDF", StringComparison.Ordinal)) throw new InvalidDataException("Not a Mare Chara File");
|
||||
|
||||
MareCharaFileHeader? decoded = null;
|
||||
|
||||
var version = reader.ReadByte();
|
||||
if (version == 1)
|
||||
{
|
||||
var dataLength = reader.ReadInt32();
|
||||
|
||||
decoded = new(version, MareCharaFileData.FromByteArray(reader.ReadBytes(dataLength)))
|
||||
{
|
||||
FilePath = path,
|
||||
};
|
||||
}
|
||||
|
||||
return decoded;
|
||||
}
|
||||
|
||||
public static void AdvanceReaderToData(BinaryReader reader)
|
||||
{
|
||||
reader.ReadChars(4);
|
||||
var version = reader.ReadByte();
|
||||
if (version == 1)
|
||||
{
|
||||
var length = reader.ReadInt32();
|
||||
_ = reader.ReadBytes(length);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,267 +0,0 @@
|
||||
using Dalamud.Game.ClientState.Objects.Types;
|
||||
using K4os.Compression.LZ4.Legacy;
|
||||
using MareSynchronos.API.Data.Enum;
|
||||
using MareSynchronos.FileCache;
|
||||
using MareSynchronos.Interop.Ipc;
|
||||
using MareSynchronos.MareConfiguration;
|
||||
using MareSynchronos.PlayerData.Factories;
|
||||
using MareSynchronos.PlayerData.Handlers;
|
||||
using MareSynchronos.Services;
|
||||
using MareSynchronos.Services.Mediator;
|
||||
using MareSynchronos.Utils;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System.Text;
|
||||
using CharacterData = MareSynchronos.API.Data.CharacterData;
|
||||
|
||||
namespace MareSynchronos.PlayerData.Export;
|
||||
|
||||
public class MareCharaFileManager : DisposableMediatorSubscriberBase
|
||||
{
|
||||
private readonly MareConfigService _configService;
|
||||
private readonly DalamudUtilService _dalamudUtil;
|
||||
private readonly MareCharaFileDataFactory _factory;
|
||||
private readonly GameObjectHandlerFactory _gameObjectHandlerFactory;
|
||||
private readonly Dictionary<string, GameObjectHandler> _gposeGameObjects;
|
||||
private readonly List<Guid?> _gposeCustomizeObjects;
|
||||
private readonly IpcManager _ipcManager;
|
||||
private readonly ILogger<MareCharaFileManager> _logger;
|
||||
private readonly FileCacheManager _manager;
|
||||
private int _globalFileCounter = 0;
|
||||
private bool _isInGpose = false;
|
||||
|
||||
public MareCharaFileManager(ILogger<MareCharaFileManager> logger, GameObjectHandlerFactory gameObjectHandlerFactory,
|
||||
FileCacheManager manager, IpcManager ipcManager, MareConfigService configService, DalamudUtilService dalamudUtil,
|
||||
MareMediator mediator) : base(logger, mediator)
|
||||
{
|
||||
_factory = new(manager);
|
||||
_logger = logger;
|
||||
_gameObjectHandlerFactory = gameObjectHandlerFactory;
|
||||
_manager = manager;
|
||||
_ipcManager = ipcManager;
|
||||
_configService = configService;
|
||||
_dalamudUtil = dalamudUtil;
|
||||
_gposeGameObjects = [];
|
||||
_gposeCustomizeObjects = [];
|
||||
Mediator.Subscribe<GposeStartMessage>(this, _ => _isInGpose = true);
|
||||
Mediator.Subscribe<GposeEndMessage>(this, async _ =>
|
||||
{
|
||||
_isInGpose = false;
|
||||
CancellationTokenSource cts = new();
|
||||
foreach (var item in _gposeGameObjects)
|
||||
{
|
||||
if ((await dalamudUtil.RunOnFrameworkThread(() => item.Value.CurrentAddress()).ConfigureAwait(false)) != nint.Zero)
|
||||
{
|
||||
await _ipcManager.Glamourer.RevertAsync(logger, item.Value, Guid.NewGuid(), cts.Token).ConfigureAwait(false);
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogDebug("Reverting by name: {name}", item.Key);
|
||||
_ipcManager.Glamourer.RevertByName(logger, item.Key, Guid.NewGuid());
|
||||
}
|
||||
|
||||
|
||||
item.Value.Dispose();
|
||||
}
|
||||
foreach (var id in _gposeCustomizeObjects.Where(d => d != null))
|
||||
{
|
||||
await _ipcManager.CustomizePlus.RevertByIdAsync(id.Value);
|
||||
}
|
||||
_gposeGameObjects.Clear();
|
||||
});
|
||||
}
|
||||
|
||||
public bool CurrentlyWorking { get; private set; } = false;
|
||||
public MareCharaFileHeader? LoadedCharaFile { get; private set; }
|
||||
|
||||
public async Task ApplyMareCharaFile(IGameObject? charaTarget, long expectedLength)
|
||||
{
|
||||
if (charaTarget == null) return;
|
||||
Dictionary<string, string> extractedFiles = new(StringComparer.Ordinal);
|
||||
CurrentlyWorking = true;
|
||||
try
|
||||
{
|
||||
if (LoadedCharaFile == null || !File.Exists(LoadedCharaFile.FilePath)) return;
|
||||
var unwrapped = File.OpenRead(LoadedCharaFile.FilePath);
|
||||
await using (unwrapped.ConfigureAwait(false))
|
||||
{
|
||||
CancellationTokenSource disposeCts = new();
|
||||
using var lz4Stream = new LZ4Stream(unwrapped, LZ4StreamMode.Decompress, LZ4StreamFlags.HighCompression);
|
||||
using var reader = new BinaryReader(lz4Stream);
|
||||
MareCharaFileHeader.AdvanceReaderToData(reader);
|
||||
_logger.LogDebug("Applying to {chara}, expected length of contents: {exp}, stream length: {len}", charaTarget.Name.TextValue, expectedLength, reader.BaseStream.Length);
|
||||
extractedFiles = ExtractFilesFromCharaFile(LoadedCharaFile, reader, expectedLength);
|
||||
Dictionary<string, string> fileSwaps = new(StringComparer.Ordinal);
|
||||
foreach (var fileSwap in LoadedCharaFile.CharaFileData.FileSwaps)
|
||||
{
|
||||
foreach (var path in fileSwap.GamePaths)
|
||||
{
|
||||
fileSwaps.Add(path, fileSwap.FileSwapPath);
|
||||
}
|
||||
}
|
||||
var applicationId = Guid.NewGuid();
|
||||
var coll = await _ipcManager.Penumbra.CreateTemporaryCollectionAsync(_logger, charaTarget.Name.TextValue).ConfigureAwait(false);
|
||||
await _ipcManager.Penumbra.AssignTemporaryCollectionAsync(_logger, coll, charaTarget.ObjectIndex).ConfigureAwait(false);
|
||||
await _ipcManager.Penumbra.SetTemporaryModsAsync(_logger, applicationId, coll, extractedFiles.Union(fileSwaps).ToDictionary(d => d.Key, d => d.Value, StringComparer.Ordinal)).ConfigureAwait(false);
|
||||
await _ipcManager.Penumbra.SetManipulationDataAsync(_logger, applicationId, coll, LoadedCharaFile.CharaFileData.ManipulationData).ConfigureAwait(false);
|
||||
|
||||
GameObjectHandler tempHandler = await _gameObjectHandlerFactory.Create(ObjectKind.Player,
|
||||
() => _dalamudUtil.GetGposeCharacterFromObjectTableByName(charaTarget.Name.ToString(), _isInGpose)?.Address ?? IntPtr.Zero, isWatched: false).ConfigureAwait(false);
|
||||
|
||||
if (!_gposeGameObjects.ContainsKey(charaTarget.Name.ToString()))
|
||||
_gposeGameObjects[charaTarget.Name.ToString()] = tempHandler;
|
||||
|
||||
await _ipcManager.Glamourer.ApplyAllAsync(_logger, tempHandler, LoadedCharaFile.CharaFileData.GlamourerData, applicationId, disposeCts.Token).ConfigureAwait(false);
|
||||
await _ipcManager.Penumbra.RedrawAsync(_logger, tempHandler, applicationId, disposeCts.Token).ConfigureAwait(false);
|
||||
_dalamudUtil.WaitWhileGposeCharacterIsDrawing(charaTarget.Address, 30000);
|
||||
await _ipcManager.Penumbra.RemoveTemporaryCollectionAsync(_logger, applicationId, coll).ConfigureAwait(false);
|
||||
if (!string.IsNullOrEmpty(LoadedCharaFile.CharaFileData.CustomizePlusData))
|
||||
{
|
||||
var id = await _ipcManager.CustomizePlus.SetBodyScaleAsync(tempHandler.Address, LoadedCharaFile.CharaFileData.CustomizePlusData).ConfigureAwait(false);
|
||||
_gposeCustomizeObjects.Add(id);
|
||||
}
|
||||
else
|
||||
{
|
||||
var id = await _ipcManager.CustomizePlus.SetBodyScaleAsync(tempHandler.Address, Convert.ToBase64String(Encoding.UTF8.GetBytes("{}"))).ConfigureAwait(false);
|
||||
_gposeCustomizeObjects.Add(id);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "Failure to read MCDF");
|
||||
throw;
|
||||
}
|
||||
finally
|
||||
{
|
||||
CurrentlyWorking = false;
|
||||
|
||||
_logger.LogDebug("Clearing local files");
|
||||
foreach (var file in Directory.EnumerateFiles(_configService.Current.CacheFolder, "*.tmp"))
|
||||
{
|
||||
File.Delete(file);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void ClearMareCharaFile()
|
||||
{
|
||||
LoadedCharaFile = null;
|
||||
}
|
||||
|
||||
public long LoadMareCharaFile(string filePath)
|
||||
{
|
||||
CurrentlyWorking = true;
|
||||
try
|
||||
{
|
||||
using var unwrapped = File.OpenRead(filePath);
|
||||
using var lz4Stream = new LZ4Stream(unwrapped, LZ4StreamMode.Decompress, LZ4StreamFlags.HighCompression);
|
||||
using var reader = new BinaryReader(lz4Stream);
|
||||
LoadedCharaFile = MareCharaFileHeader.FromBinaryReader(filePath, reader);
|
||||
|
||||
_logger.LogInformation("Read Mare Chara File");
|
||||
_logger.LogInformation("Version: {ver}", (LoadedCharaFile?.Version ?? -1));
|
||||
long expectedLength = 0;
|
||||
if (LoadedCharaFile != null)
|
||||
{
|
||||
_logger.LogTrace("Data");
|
||||
foreach (var item in LoadedCharaFile.CharaFileData.FileSwaps)
|
||||
{
|
||||
foreach (var gamePath in item.GamePaths)
|
||||
{
|
||||
_logger.LogTrace("Swap: {gamePath} => {fileSwapPath}", gamePath, item.FileSwapPath);
|
||||
}
|
||||
}
|
||||
|
||||
var itemNr = 0;
|
||||
foreach (var item in LoadedCharaFile.CharaFileData.Files)
|
||||
{
|
||||
itemNr++;
|
||||
expectedLength += item.Length;
|
||||
foreach (var gamePath in item.GamePaths)
|
||||
{
|
||||
_logger.LogTrace("File {itemNr}: {gamePath} = {len}", itemNr, gamePath, item.Length.ToByteString());
|
||||
}
|
||||
}
|
||||
|
||||
_logger.LogInformation("Expected length: {expected}", expectedLength.ToByteString());
|
||||
}
|
||||
return expectedLength;
|
||||
}
|
||||
finally { CurrentlyWorking = false; }
|
||||
}
|
||||
|
||||
public void SaveMareCharaFile(CharacterData? dto, string description, string filePath)
|
||||
{
|
||||
CurrentlyWorking = true;
|
||||
var tempFilePath = filePath + ".tmp";
|
||||
|
||||
try
|
||||
{
|
||||
if (dto == null) return;
|
||||
|
||||
var mareCharaFileData = _factory.Create(description, dto);
|
||||
MareCharaFileHeader output = new(MareCharaFileHeader.CurrentVersion, mareCharaFileData);
|
||||
|
||||
using var fs = new FileStream(tempFilePath, FileMode.Create, FileAccess.ReadWrite, FileShare.None);
|
||||
using var lz4 = new LZ4Stream(fs, LZ4StreamMode.Compress, LZ4StreamFlags.HighCompression);
|
||||
using var writer = new BinaryWriter(lz4);
|
||||
output.WriteToStream(writer);
|
||||
|
||||
foreach (var item in output.CharaFileData.Files)
|
||||
{
|
||||
var file = _manager.GetFileCacheByHash(item.Hash)!;
|
||||
_logger.LogDebug("Saving to MCDF: {hash}:{file}", item.Hash, file.ResolvedFilepath);
|
||||
_logger.LogDebug("\tAssociated GamePaths:");
|
||||
foreach (var path in item.GamePaths)
|
||||
{
|
||||
_logger.LogDebug("\t{path}", path);
|
||||
}
|
||||
using var fsRead = File.OpenRead(file.ResolvedFilepath);
|
||||
using var br = new BinaryReader(fsRead);
|
||||
byte[] buffer = new byte[item.Length];
|
||||
br.Read(buffer, 0, item.Length);
|
||||
writer.Write(buffer);
|
||||
}
|
||||
writer.Flush();
|
||||
lz4.Flush();
|
||||
fs.Flush();
|
||||
fs.Close();
|
||||
File.Move(tempFilePath, filePath, true);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Failure Saving Mare Chara File, deleting output");
|
||||
File.Delete(tempFilePath);
|
||||
}
|
||||
finally { CurrentlyWorking = false; }
|
||||
}
|
||||
|
||||
private Dictionary<string, string> ExtractFilesFromCharaFile(MareCharaFileHeader charaFileHeader, BinaryReader reader, long expectedLength)
|
||||
{
|
||||
long totalRead = 0;
|
||||
Dictionary<string, string> gamePathToFilePath = new(StringComparer.Ordinal);
|
||||
foreach (var fileData in charaFileHeader.CharaFileData.Files)
|
||||
{
|
||||
var fileName = Path.Combine(_configService.Current.CacheFolder, "mare_" + _globalFileCounter++ + ".tmp");
|
||||
var length = fileData.Length;
|
||||
var bufferSize = length;
|
||||
using var fs = File.OpenWrite(fileName);
|
||||
using var wr = new BinaryWriter(fs);
|
||||
_logger.LogTrace("Reading {length} of {fileName}", length.ToByteString(), fileName);
|
||||
var buffer = reader.ReadBytes(bufferSize);
|
||||
wr.Write(buffer);
|
||||
wr.Flush();
|
||||
wr.Close();
|
||||
if (buffer.Length == 0) throw new EndOfStreamException("Unexpected EOF");
|
||||
foreach (var path in fileData.GamePaths)
|
||||
{
|
||||
gamePathToFilePath[path] = fileName;
|
||||
_logger.LogTrace("{path} => {fileName} [{hash}]", path, fileName, fileData.Hash);
|
||||
}
|
||||
totalRead += length;
|
||||
_logger.LogTrace("Read {read}/{expected} bytes", totalRead.ToByteString(), expectedLength.ToByteString());
|
||||
}
|
||||
|
||||
return gamePathToFilePath;
|
||||
}
|
||||
}
|
||||
@@ -23,6 +23,7 @@ public sealed class CacheCreationService : DisposableMediatorSubscriberBase
|
||||
private CancellationTokenSource _petNicknamesCts = new();
|
||||
private CancellationTokenSource _moodlesCts = new();
|
||||
private bool _isZoning = false;
|
||||
private bool _haltCharaDataCreation;
|
||||
private readonly Dictionary<ObjectKind, CancellationTokenSource> _glamourerCts = new();
|
||||
|
||||
public CacheCreationService(ILogger<CacheCreationService> logger, MareMediator mediator, GameObjectHandlerFactory gameObjectHandlerFactory,
|
||||
@@ -41,6 +42,11 @@ public sealed class CacheCreationService : DisposableMediatorSubscriberBase
|
||||
Mediator.Subscribe<ZoneSwitchStartMessage>(this, (msg) => _isZoning = true);
|
||||
Mediator.Subscribe<ZoneSwitchEndMessage>(this, (msg) => _isZoning = false);
|
||||
|
||||
Mediator.Subscribe<HaltCharaDataCreation>(this, (msg) =>
|
||||
{
|
||||
_haltCharaDataCreation = !msg.Resume;
|
||||
});
|
||||
|
||||
_playerRelatedObjects[ObjectKind.Player] = gameObjectHandlerFactory.Create(ObjectKind.Player, dalamudUtil.GetPlayerPointer, isWatched: true)
|
||||
.GetAwaiter().GetResult();
|
||||
_playerRelatedObjects[ObjectKind.MinionOrMount] = gameObjectHandlerFactory.Create(ObjectKind.MinionOrMount, () => dalamudUtil.GetMinionOrMount(), isWatched: true)
|
||||
@@ -218,7 +224,7 @@ public sealed class CacheCreationService : DisposableMediatorSubscriberBase
|
||||
|
||||
private void ProcessCacheCreation()
|
||||
{
|
||||
if (_isZoning) return;
|
||||
if (_isZoning || _haltCharaDataCreation) return;
|
||||
|
||||
if (_cachesToCreate.Any() && (_cacheCreationTask?.IsCompleted ?? true))
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user