add more resilience to MCDF export and loading
This commit is contained in:
@@ -65,7 +65,7 @@ public class MareCharaFileManager : DisposableMediatorSubscriberBase
|
|||||||
public bool CurrentlyWorking { get; private set; } = false;
|
public bool CurrentlyWorking { get; private set; } = false;
|
||||||
public MareCharaFileHeader? LoadedCharaFile { get; private set; }
|
public MareCharaFileHeader? LoadedCharaFile { get; private set; }
|
||||||
|
|
||||||
public async Task ApplyMareCharaFile(GameObject? charaTarget)
|
public async Task ApplyMareCharaFile(GameObject? charaTarget, long expectedLength)
|
||||||
{
|
{
|
||||||
if (charaTarget == null) return;
|
if (charaTarget == null) return;
|
||||||
Dictionary<string, string> extractedFiles = new(StringComparer.Ordinal);
|
Dictionary<string, string> extractedFiles = new(StringComparer.Ordinal);
|
||||||
@@ -80,8 +80,8 @@ public class MareCharaFileManager : DisposableMediatorSubscriberBase
|
|||||||
using var lz4Stream = new LZ4Stream(unwrapped, LZ4StreamMode.Decompress, LZ4StreamFlags.HighCompression);
|
using var lz4Stream = new LZ4Stream(unwrapped, LZ4StreamMode.Decompress, LZ4StreamFlags.HighCompression);
|
||||||
using var reader = new BinaryReader(lz4Stream);
|
using var reader = new BinaryReader(lz4Stream);
|
||||||
MareCharaFileHeader.AdvanceReaderToData(reader);
|
MareCharaFileHeader.AdvanceReaderToData(reader);
|
||||||
_logger.LogDebug("Applying to {chara}", charaTarget.Name.TextValue);
|
_logger.LogDebug("Applying to {chara}, expected length of contents: {exp}", charaTarget.Name.TextValue, expectedLength);
|
||||||
extractedFiles = ExtractFilesFromCharaFile(LoadedCharaFile, reader);
|
extractedFiles = ExtractFilesFromCharaFile(LoadedCharaFile, reader, expectedLength);
|
||||||
Dictionary<string, string> fileSwaps = new(StringComparer.Ordinal);
|
Dictionary<string, string> fileSwaps = new(StringComparer.Ordinal);
|
||||||
foreach (var fileSwap in LoadedCharaFile.CharaFileData.FileSwaps)
|
foreach (var fileSwap in LoadedCharaFile.CharaFileData.FileSwaps)
|
||||||
{
|
{
|
||||||
@@ -125,14 +125,19 @@ public class MareCharaFileManager : DisposableMediatorSubscriberBase
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogWarning(ex, "Failure to read MCDF");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
CurrentlyWorking = false;
|
CurrentlyWorking = false;
|
||||||
|
|
||||||
_logger.LogDebug("Clearing local files");
|
_logger.LogDebug("Clearing local files");
|
||||||
foreach (var file in extractedFiles)
|
foreach (var file in Directory.EnumerateFiles(_configService.Current.CacheFolder, "*.tmp"))
|
||||||
{
|
{
|
||||||
File.Delete(file.Value);
|
File.Delete(file);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -142,7 +147,7 @@ public class MareCharaFileManager : DisposableMediatorSubscriberBase
|
|||||||
LoadedCharaFile = null;
|
LoadedCharaFile = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void LoadMareCharaFile(string filePath)
|
public long LoadMareCharaFile(string filePath)
|
||||||
{
|
{
|
||||||
CurrentlyWorking = true;
|
CurrentlyWorking = true;
|
||||||
try
|
try
|
||||||
@@ -179,6 +184,7 @@ public class MareCharaFileManager : DisposableMediatorSubscriberBase
|
|||||||
|
|
||||||
_logger.LogInformation("Expected length: {expected}", expectedLength);
|
_logger.LogInformation("Expected length: {expected}", expectedLength);
|
||||||
}
|
}
|
||||||
|
return expectedLength;
|
||||||
}
|
}
|
||||||
finally { CurrentlyWorking = false; }
|
finally { CurrentlyWorking = false; }
|
||||||
}
|
}
|
||||||
@@ -214,34 +220,38 @@ public class MareCharaFileManager : DisposableMediatorSubscriberBase
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Failure Saving Mare Chara File, deleting output");
|
||||||
|
File.Delete(filePath);
|
||||||
|
}
|
||||||
finally { CurrentlyWorking = false; }
|
finally { CurrentlyWorking = false; }
|
||||||
}
|
}
|
||||||
|
|
||||||
private Dictionary<string, string> ExtractFilesFromCharaFile(MareCharaFileHeader charaFileHeader, BinaryReader reader)
|
private Dictionary<string, string> ExtractFilesFromCharaFile(MareCharaFileHeader charaFileHeader, BinaryReader reader, long expectedLength)
|
||||||
{
|
{
|
||||||
|
long totalRead = 0;
|
||||||
Dictionary<string, string> gamePathToFilePath = new(StringComparer.Ordinal);
|
Dictionary<string, string> gamePathToFilePath = new(StringComparer.Ordinal);
|
||||||
foreach (var fileData in charaFileHeader.CharaFileData.Files)
|
foreach (var fileData in charaFileHeader.CharaFileData.Files)
|
||||||
{
|
{
|
||||||
var fileName = Path.Combine(_configService.Current.CacheFolder, "mare_" + _globalFileCounter++ + ".tmp");
|
var fileName = Path.Combine(_configService.Current.CacheFolder, "mare_" + _globalFileCounter++ + ".tmp");
|
||||||
var length = fileData.Length;
|
var length = (int)fileData.Length;
|
||||||
var bufferSize = 4 * 1024 * 1024;
|
var bufferSize = length;
|
||||||
using var fs = File.OpenWrite(fileName);
|
using var fs = File.OpenWrite(fileName);
|
||||||
using var wr = new BinaryWriter(fs);
|
using var wr = new BinaryWriter(fs);
|
||||||
int chunk = 0;
|
_logger.LogTrace("Reading {length} of {fileName}", length, fileName);
|
||||||
while (length > 0)
|
var buffer = reader.ReadBytes(bufferSize);
|
||||||
{
|
wr.Write(buffer);
|
||||||
if (length < bufferSize) bufferSize = (int)length;
|
|
||||||
_logger.LogTrace("Reading chunk {chunk} {bufferSize}/{length} of {fileName}", chunk++, bufferSize, length, fileName);
|
|
||||||
var buffer = reader.ReadBytes(bufferSize);
|
|
||||||
wr.Write(length > bufferSize ? buffer : buffer.Take((int)length).ToArray());
|
|
||||||
length -= bufferSize;
|
|
||||||
}
|
|
||||||
wr.Flush();
|
wr.Flush();
|
||||||
|
wr.Close();
|
||||||
|
if (buffer.Length == 0) throw new EndOfStreamException("Unexpected EOF");
|
||||||
foreach (var path in fileData.GamePaths)
|
foreach (var path in fileData.GamePaths)
|
||||||
{
|
{
|
||||||
gamePathToFilePath[path] = fileName;
|
gamePathToFilePath[path] = fileName;
|
||||||
_logger.LogTrace("{path} => {fileName}", path, fileName);
|
_logger.LogTrace("{path} => {fileName}", path, fileName);
|
||||||
}
|
}
|
||||||
|
totalRead += length;
|
||||||
|
_logger.LogTrace("Read {read}/{expected} bytes", totalRead, expectedLength);
|
||||||
}
|
}
|
||||||
|
|
||||||
return gamePathToFilePath;
|
return gamePathToFilePath;
|
||||||
|
|||||||
@@ -15,6 +15,8 @@ public class GposeUi : WindowMediatorSubscriberBase
|
|||||||
private readonly DalamudUtilService _dalamudUtil;
|
private readonly DalamudUtilService _dalamudUtil;
|
||||||
private readonly FileDialogManager _fileDialogManager;
|
private readonly FileDialogManager _fileDialogManager;
|
||||||
private readonly MareCharaFileManager _mareCharaFileManager;
|
private readonly MareCharaFileManager _mareCharaFileManager;
|
||||||
|
private Task<long>? _expectedLength;
|
||||||
|
private Task? _applicationTask;
|
||||||
|
|
||||||
public GposeUi(ILogger<GposeUi> logger, MareCharaFileManager mareCharaFileManager,
|
public GposeUi(ILogger<GposeUi> logger, MareCharaFileManager mareCharaFileManager,
|
||||||
DalamudUtilService dalamudUtil, FileDialogManager fileDialogManager, MareConfigService configService,
|
DalamudUtilService dalamudUtil, FileDialogManager fileDialogManager, MareConfigService configService,
|
||||||
@@ -51,21 +53,28 @@ public class GposeUi : WindowMediatorSubscriberBase
|
|||||||
_configService.Current.ExportFolder = Path.GetDirectoryName(path) ?? string.Empty;
|
_configService.Current.ExportFolder = Path.GetDirectoryName(path) ?? string.Empty;
|
||||||
_configService.Save();
|
_configService.Save();
|
||||||
|
|
||||||
_ = Task.Run(() => _mareCharaFileManager.LoadMareCharaFile(path));
|
_expectedLength = Task.Run(() => _mareCharaFileManager.LoadMareCharaFile(path));
|
||||||
}, 1, Directory.Exists(_configService.Current.ExportFolder) ? _configService.Current.ExportFolder : null);
|
}, 1, Directory.Exists(_configService.Current.ExportFolder) ? _configService.Current.ExportFolder : null);
|
||||||
}
|
}
|
||||||
UiSharedService.AttachToolTip("Applies it to the currently selected GPose actor");
|
UiSharedService.AttachToolTip("Applies it to the currently selected GPose actor");
|
||||||
if (_mareCharaFileManager.LoadedCharaFile != null)
|
if (_mareCharaFileManager.LoadedCharaFile != null && _expectedLength != null)
|
||||||
{
|
{
|
||||||
UiSharedService.TextWrapped("Loaded file: " + _mareCharaFileManager.LoadedCharaFile.FilePath);
|
UiSharedService.TextWrapped("Loaded file: " + _mareCharaFileManager.LoadedCharaFile.FilePath);
|
||||||
UiSharedService.TextWrapped("File Description: " + _mareCharaFileManager.LoadedCharaFile.CharaFileData.Description);
|
UiSharedService.TextWrapped("File Description: " + _mareCharaFileManager.LoadedCharaFile.CharaFileData.Description);
|
||||||
if (UiSharedService.NormalizedIconTextButton(FontAwesomeIcon.Check, "Apply loaded MCDF"))
|
if (UiSharedService.NormalizedIconTextButton(FontAwesomeIcon.Check, "Apply loaded MCDF"))
|
||||||
{
|
{
|
||||||
_ = Task.Run(async () => await _mareCharaFileManager.ApplyMareCharaFile(_dalamudUtil.GposeTargetGameObject).ConfigureAwait(false));
|
_applicationTask = Task.Run(async () => await _mareCharaFileManager.ApplyMareCharaFile(_dalamudUtil.GposeTargetGameObject, _expectedLength!.GetAwaiter().GetResult()).ConfigureAwait(false));
|
||||||
}
|
}
|
||||||
UiSharedService.AttachToolTip("Applies it to the currently selected GPose actor");
|
UiSharedService.AttachToolTip("Applies it to the currently selected GPose actor");
|
||||||
UiSharedService.ColorTextWrapped("Warning: redrawing or changing the character will revert all applied mods.", ImGuiColors.DalamudYellow);
|
UiSharedService.ColorTextWrapped("Warning: redrawing or changing the character will revert all applied mods.", ImGuiColors.DalamudYellow);
|
||||||
}
|
}
|
||||||
|
if (_applicationTask?.IsFaulted ?? false)
|
||||||
|
{
|
||||||
|
UiSharedService.ColorTextWrapped("Failure to read MCDF file. MCDF file is possibly corrupt. Re-export the MCDF file and try again.",
|
||||||
|
ImGuiColors.DalamudRed);
|
||||||
|
UiSharedService.ColorTextWrapped("Note: if this is your MCDF, try redrawing yourself, wait and re-export the file. " +
|
||||||
|
"If you received it from someone else have them do the same.", ImGuiColors.DalamudYellow);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -77,6 +86,8 @@ public class GposeUi : WindowMediatorSubscriberBase
|
|||||||
private void EndGpose()
|
private void EndGpose()
|
||||||
{
|
{
|
||||||
IsOpen = false;
|
IsOpen = false;
|
||||||
|
_applicationTask = null;
|
||||||
|
_expectedLength = null;
|
||||||
_mareCharaFileManager.ClearMareCharaFile();
|
_mareCharaFileManager.ClearMareCharaFile();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -54,6 +54,7 @@ public class SettingsUi : WindowMediatorSubscriberBase
|
|||||||
private Task<List<FileCacheEntity>>? _validationTask;
|
private Task<List<FileCacheEntity>>? _validationTask;
|
||||||
private CancellationTokenSource? _validationCts;
|
private CancellationTokenSource? _validationCts;
|
||||||
private (int, int, FileCacheEntity) _currentProgress;
|
private (int, int, FileCacheEntity) _currentProgress;
|
||||||
|
private Task? _exportTask;
|
||||||
|
|
||||||
public SettingsUi(ILogger<SettingsUi> logger,
|
public SettingsUi(ILogger<SettingsUi> logger,
|
||||||
UiSharedService uiShared, MareConfigService configService,
|
UiSharedService uiShared, MareConfigService configService,
|
||||||
@@ -439,17 +440,11 @@ public class SettingsUi : WindowMediatorSubscriberBase
|
|||||||
_configService.Current.ExportFolder = Path.GetDirectoryName(path) ?? string.Empty;
|
_configService.Current.ExportFolder = Path.GetDirectoryName(path) ?? string.Empty;
|
||||||
_configService.Save();
|
_configService.Save();
|
||||||
|
|
||||||
_ = Task.Run(() =>
|
_exportTask = Task.Run(() =>
|
||||||
{
|
{
|
||||||
try
|
var desc = _exportDescription;
|
||||||
{
|
_exportDescription = string.Empty;
|
||||||
_mareCharaFileManager.SaveMareCharaFile(LastCreatedCharacterData, _exportDescription, path);
|
_mareCharaFileManager.SaveMareCharaFile(LastCreatedCharacterData, desc, path);
|
||||||
_exportDescription = string.Empty;
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
_logger.LogCritical(ex, "Error saving data");
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}, Directory.Exists(_configService.Current.ExportFolder) ? _configService.Current.ExportFolder : null);
|
}, Directory.Exists(_configService.Current.ExportFolder) ? _configService.Current.ExportFolder : null);
|
||||||
}
|
}
|
||||||
@@ -461,6 +456,11 @@ public class SettingsUi : WindowMediatorSubscriberBase
|
|||||||
UiSharedService.ColorTextWrapped("Export in progress", ImGuiColors.DalamudYellow);
|
UiSharedService.ColorTextWrapped("Export in progress", ImGuiColors.DalamudYellow);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (_exportTask?.IsFaulted ?? false)
|
||||||
|
{
|
||||||
|
UiSharedService.ColorTextWrapped("Export failed, check /xllog for more details.", ImGuiColors.DalamudRed);
|
||||||
|
}
|
||||||
|
|
||||||
ImGui.Unindent();
|
ImGui.Unindent();
|
||||||
}
|
}
|
||||||
bool openInGpose = _configService.Current.OpenGposeImportOnGposeStart;
|
bool openInGpose = _configService.Current.OpenGposeImportOnGposeStart;
|
||||||
|
|||||||
Reference in New Issue
Block a user