Compare commits
18 Commits
i18n
...
2.0.2.76-D
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3654365f2a | ||
|
|
9b256dd185 | ||
|
|
d8b9e9cf19 | ||
|
|
ad34d88336 | ||
| 9167bb1afd | |||
|
|
5161c6bad3 | ||
|
|
223ade39cb | ||
|
|
5aca9e70b2 | ||
|
|
ce28799db3 | ||
|
|
92772cf334 | ||
|
|
0395e81a9f | ||
|
|
7734a7bf7e | ||
|
|
db2d19bb1e | ||
|
|
ab305a249c | ||
|
|
9d104a9dd8 | ||
|
|
bcd3bd5ca2 | ||
|
|
c1829a9837 | ||
|
|
cca23f6e05 |
Submodule LightlessAPI updated: c3caa7e25c...4ecd5375e6
@@ -3,7 +3,7 @@
|
|||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<Authors></Authors>
|
<Authors></Authors>
|
||||||
<Company></Company>
|
<Company></Company>
|
||||||
<Version>2.0.3</Version>
|
<Version>2.0.2.76</Version>
|
||||||
<Description></Description>
|
<Description></Description>
|
||||||
<Copyright></Copyright>
|
<Copyright></Copyright>
|
||||||
<PackageProjectUrl>https://github.com/Light-Public-Syncshells/LightlessClient</PackageProjectUrl>
|
<PackageProjectUrl>https://github.com/Light-Public-Syncshells/LightlessClient</PackageProjectUrl>
|
||||||
|
|||||||
@@ -566,20 +566,20 @@ public class PlayerDataFactory
|
|||||||
await _papParseLimiter.WaitAsync(ct).ConfigureAwait(false);
|
await _papParseLimiter.WaitAsync(ct).ConfigureAwait(false);
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
try
|
var cacheEntity = _fileCacheManager.GetFileCacheByHash(hash);
|
||||||
|
var papPath = cacheEntity?.ResolvedFilepath;
|
||||||
|
|
||||||
|
if (!string.IsNullOrEmpty(papPath) && File.Exists(papPath))
|
||||||
{
|
{
|
||||||
papIndices = await Task.Run(() => _modelAnalyzer.GetBoneIndicesFromPap(hash, persistToConfig: false), ct)
|
var havokBytes = await Task.Run(() => XivDataAnalyzer.ReadHavokBytesFromPap(papPath), ct)
|
||||||
|
.ConfigureAwait(false);
|
||||||
|
|
||||||
|
if (havokBytes is { Length: > 8 })
|
||||||
|
{
|
||||||
|
papIndices = await _dalamudUtil.RunOnFrameworkThread(
|
||||||
|
() => _modelAnalyzer.ParseHavokBytesOnFrameworkThread(havokBytes, hash, persistToConfig: false))
|
||||||
.ConfigureAwait(false);
|
.ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
catch (SEHException ex)
|
|
||||||
{
|
|
||||||
_logger.LogError(ex, "SEH exception while parsing PAP file (hash={hash}, path={path}). Error code: 0x{code:X}. Skipping this animation.", hash, papPathSummary, ex.ErrorCode);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
_logger.LogError(ex, "Unexpected error parsing PAP file (hash={hash}, path={path}). Skipping this animation.", hash, papPathSummary);
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
@@ -590,20 +590,6 @@ public class PlayerDataFactory
|
|||||||
if (papIndices == null || papIndices.Count == 0)
|
if (papIndices == null || papIndices.Count == 0)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
bool hasValidIndices = false;
|
|
||||||
try
|
|
||||||
{
|
|
||||||
hasValidIndices = papIndices.All(k => k.Value != null && k.Value.DefaultIfEmpty().Max() <= 105);
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
_logger.LogWarning(ex, "Error validating bone indices for PAP (hash={hash}, path={path}). Skipping.", hash, papPathSummary);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (hasValidIndices)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
if (_logger.IsEnabled(LogLevel.Debug))
|
if (_logger.IsEnabled(LogLevel.Debug))
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
@@ -703,8 +689,8 @@ public class PlayerDataFactory
|
|||||||
return new Dictionary<string, string[]>(StringComparer.OrdinalIgnoreCase).AsReadOnly();
|
return new Dictionary<string, string[]>(StringComparer.OrdinalIgnoreCase).AsReadOnly();
|
||||||
}
|
}
|
||||||
|
|
||||||
var forwardPathsLower = forwardPaths.Length == 0 ? Array.Empty<string>() : forwardPaths.Select(p => p.ToLowerInvariant()).ToArray();
|
var forwardPathsLower = forwardPaths.Length == 0 ? [] : forwardPaths.Select(p => p.ToLowerInvariant()).ToArray();
|
||||||
var reversePathsLower = reversePaths.Length == 0 ? Array.Empty<string>() : reversePaths.Select(p => p.ToLowerInvariant()).ToArray();
|
var reversePathsLower = reversePaths.Length == 0 ? [] : reversePaths.Select(p => p.ToLowerInvariant()).ToArray();
|
||||||
|
|
||||||
Dictionary<string, List<string>> resolvedPaths = new(forwardPaths.Length + reversePaths.Length, StringComparer.Ordinal);
|
Dictionary<string, List<string>> resolvedPaths = new(forwardPaths.Length + reversePaths.Length, StringComparer.Ordinal);
|
||||||
if (handler.ObjectKind != ObjectKind.Player)
|
if (handler.ObjectKind != ObjectKind.Player)
|
||||||
|
|||||||
@@ -121,6 +121,7 @@ internal sealed class PairHandlerAdapter : DisposableMediatorSubscriberBase, IPa
|
|||||||
private DateTime _nextActorLookupUtc = DateTime.MinValue;
|
private DateTime _nextActorLookupUtc = DateTime.MinValue;
|
||||||
private static readonly TimeSpan ActorLookupInterval = TimeSpan.FromSeconds(1);
|
private static readonly TimeSpan ActorLookupInterval = TimeSpan.FromSeconds(1);
|
||||||
private static readonly SemaphoreSlim ActorInitializationLimiter = new(1, 1);
|
private static readonly SemaphoreSlim ActorInitializationLimiter = new(1, 1);
|
||||||
|
private static readonly SemaphoreSlim _papParseLimiter = new(1, 1);
|
||||||
private const int FullyLoadedTimeoutMsPlayer = 30000;
|
private const int FullyLoadedTimeoutMsPlayer = 30000;
|
||||||
private const int FullyLoadedTimeoutMsOther = 5000;
|
private const int FullyLoadedTimeoutMsOther = 5000;
|
||||||
private readonly object _actorInitializationGate = new();
|
private readonly object _actorInitializationGate = new();
|
||||||
@@ -2910,7 +2911,7 @@ internal sealed class PairHandlerAdapter : DisposableMediatorSubscriberBase, IPa
|
|||||||
|
|
||||||
var mode = _configService.Current.AnimationValidationMode;
|
var mode = _configService.Current.AnimationValidationMode;
|
||||||
var allowBasedShift = _configService.Current.AnimationAllowOneBasedShift;
|
var allowBasedShift = _configService.Current.AnimationAllowOneBasedShift;
|
||||||
var allownNightIndex = _configService.Current.AnimationAllowNeighborIndexTolerance;
|
var allowNeighborIndex = _configService.Current.AnimationAllowNeighborIndexTolerance;
|
||||||
|
|
||||||
if (mode == AnimationValidationMode.Unsafe || papOnly.Count == 0)
|
if (mode == AnimationValidationMode.Unsafe || papOnly.Count == 0)
|
||||||
return 0;
|
return 0;
|
||||||
@@ -2930,47 +2931,86 @@ internal sealed class PairHandlerAdapter : DisposableMediatorSubscriberBase, IPa
|
|||||||
foreach (var (rawKey, list) in boneIndices)
|
foreach (var (rawKey, list) in boneIndices)
|
||||||
{
|
{
|
||||||
var key = XivDataAnalyzer.CanonicalizeSkeletonKey(rawKey);
|
var key = XivDataAnalyzer.CanonicalizeSkeletonKey(rawKey);
|
||||||
if (string.IsNullOrEmpty(key)) continue;
|
if (string.IsNullOrEmpty(key) || list == null || list.Count == 0)
|
||||||
|
continue;
|
||||||
|
|
||||||
if (!localBoneSets.TryGetValue(key, out var set))
|
if (!localBoneSets.TryGetValue(key, out var set))
|
||||||
localBoneSets[key] = set = [];
|
localBoneSets[key] = set = new HashSet<ushort>();
|
||||||
|
|
||||||
foreach (var v in list)
|
foreach (var v in list)
|
||||||
set.Add(v);
|
set.Add(v);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (localBoneSets.Count == 0)
|
||||||
|
{
|
||||||
|
var removedCount = papOnly.Count;
|
||||||
|
papOnly.Clear();
|
||||||
|
return removedCount;
|
||||||
|
}
|
||||||
|
|
||||||
int removed = 0;
|
int removed = 0;
|
||||||
|
|
||||||
foreach (var hash in papOnly.Keys.Select(k => k.Hash).Where(h => !string.IsNullOrEmpty(h)).Distinct(StringComparer.OrdinalIgnoreCase).ToList())
|
var groups = papOnly
|
||||||
|
.Where(kvp => !string.IsNullOrEmpty(kvp.Key.Hash))
|
||||||
|
.GroupBy(kvp => kvp.Key.Hash!, StringComparer.OrdinalIgnoreCase)
|
||||||
|
.ToList();
|
||||||
|
|
||||||
|
foreach (var grp in groups)
|
||||||
{
|
{
|
||||||
token.ThrowIfCancellationRequested();
|
token.ThrowIfCancellationRequested();
|
||||||
|
|
||||||
var papIndices = await _dalamudUtil.RunOnFrameworkThread(
|
var hash = grp.Key;
|
||||||
() => _modelAnalyzer.GetBoneIndicesFromPap(hash!))
|
|
||||||
|
var papPath = grp.Select(x => x.Value)
|
||||||
|
.FirstOrDefault(p => !string.IsNullOrEmpty(p) && File.Exists(p));
|
||||||
|
|
||||||
|
if (string.IsNullOrEmpty(papPath))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
var havokBytes = await Task.Run(() => XivDataAnalyzer.ReadHavokBytesFromPap(papPath), token)
|
||||||
.ConfigureAwait(false);
|
.ConfigureAwait(false);
|
||||||
|
|
||||||
|
if (havokBytes is not { Length: > 8 })
|
||||||
|
continue;
|
||||||
|
|
||||||
|
Dictionary<string, List<ushort>>? papIndices;
|
||||||
|
|
||||||
|
await _papParseLimiter.WaitAsync(token).ConfigureAwait(false);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
papIndices = await _dalamudUtil.RunOnFrameworkThread(
|
||||||
|
() => _modelAnalyzer.ParseHavokBytesOnFrameworkThread(havokBytes, hash, persistToConfig: false))
|
||||||
|
.ConfigureAwait(false);
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
_papParseLimiter.Release();
|
||||||
|
}
|
||||||
|
|
||||||
if (papIndices == null || papIndices.Count == 0)
|
if (papIndices == null || papIndices.Count == 0)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (papIndices.All(k => k.Value.DefaultIfEmpty().Max() <= 105))
|
if (papIndices.All(k => k.Value == null || k.Value.Count == 0 || k.Value.Max() <= 105))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (XivDataAnalyzer.IsPapCompatible(localBoneSets, papIndices, mode, allowBasedShift, allownNightIndex, out var reason))
|
if (XivDataAnalyzer.IsPapCompatible(localBoneSets, papIndices, mode, allowBasedShift, allowNeighborIndex, out var reason))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
var keysToRemove = papOnly.Keys.Where(k => string.Equals(k.Hash, hash, StringComparison.OrdinalIgnoreCase)).ToList();
|
var keysToRemove = grp.Select(x => x.Key).ToList();
|
||||||
foreach (var k in keysToRemove)
|
foreach (var k in keysToRemove)
|
||||||
papOnly.Remove(k);
|
papOnly.Remove(k);
|
||||||
|
|
||||||
removed += keysToRemove.Count;
|
removed += keysToRemove.Count;
|
||||||
|
|
||||||
if (_blockedPapHashes.TryAdd(hash!, 0))
|
if (_blockedPapHashes.TryAdd(hash, 0))
|
||||||
Logger.LogWarning("Blocked remote object PAP (hash {hash}) for {handler}: {reason}", hash, GetLogIdentifier(), reason);
|
Logger.LogWarning("Blocked remote object PAP {papPath} (hash {hash}) for {handler}: {reason}",
|
||||||
|
papPath, hash, GetLogIdentifier(), reason);
|
||||||
|
|
||||||
if (charaData.FileReplacements.TryGetValue(ObjectKind.Player, out var list))
|
if (charaData.FileReplacements.TryGetValue(ObjectKind.Player, out var list))
|
||||||
{
|
{
|
||||||
list.RemoveAll(r => string.Equals(r.Hash, hash, StringComparison.OrdinalIgnoreCase)
|
list.RemoveAll(r =>
|
||||||
&& r.GamePaths.Any(p => p.EndsWith(".pap", StringComparison.OrdinalIgnoreCase)));
|
string.Equals(r.Hash, hash, StringComparison.OrdinalIgnoreCase) &&
|
||||||
|
r.GamePaths.Any(p => p.EndsWith(".pap", StringComparison.OrdinalIgnoreCase)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2984,6 +3024,7 @@ internal sealed class PairHandlerAdapter : DisposableMediatorSubscriberBase, IPa
|
|||||||
return removed;
|
return removed;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private async Task ApplyCustomizeAsync(nint address, string customizeData, ObjectKind kind)
|
private async Task ApplyCustomizeAsync(nint address, string customizeData, ObjectKind kind)
|
||||||
{
|
{
|
||||||
_customizeIds[kind] = await _ipcManager.CustomizePlus.SetBodyScaleAsync(address, customizeData).ConfigureAwait(false);
|
_customizeIds[kind] = await _ipcManager.CustomizePlus.SetBodyScaleAsync(address, customizeData).ConfigureAwait(false);
|
||||||
|
|||||||
3
LightlessSync/Resources/Resources.Designer.cs
generated
3
LightlessSync/Resources/Resources.Designer.cs
generated
@@ -1,6 +1,7 @@
|
|||||||
//------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------
|
||||||
// <auto-generated>
|
// <auto-generated>
|
||||||
// This code was generated by a tool.
|
// This code was generated by a tool.
|
||||||
|
// Runtime Version:4.0.30319.42000
|
||||||
//
|
//
|
||||||
// Changes to this file may cause incorrect behavior and will be lost if
|
// Changes to this file may cause incorrect behavior and will be lost if
|
||||||
// the code is regenerated.
|
// the code is regenerated.
|
||||||
@@ -18,7 +19,7 @@ namespace LightlessSync.Resources {
|
|||||||
// class via a tool like ResGen or Visual Studio.
|
// class via a tool like ResGen or Visual Studio.
|
||||||
// To add or remove a member, edit your .ResX file then rerun ResGen
|
// To add or remove a member, edit your .ResX file then rerun ResGen
|
||||||
// with the /str option, or rebuild your VS project.
|
// with the /str option, or rebuild your VS project.
|
||||||
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")]
|
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "18.0.0.0")]
|
||||||
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
|
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
|
||||||
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
|
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
|
||||||
public class Resources {
|
public class Resources {
|
||||||
|
|||||||
@@ -93,7 +93,7 @@ public class DalamudUtilService : IHostedService, IMediatorSubscriber
|
|||||||
{
|
{
|
||||||
return gameData.GetExcelSheet<Lumina.Excel.Sheets.World>(clientLanguage)!
|
return gameData.GetExcelSheet<Lumina.Excel.Sheets.World>(clientLanguage)!
|
||||||
.Where(w => !w.Name.IsEmpty && w.DataCenter.RowId != 0 && (w.IsPublic || char.IsUpper(w.Name.ToString()[0])
|
.Where(w => !w.Name.IsEmpty && w.DataCenter.RowId != 0 && (w.IsPublic || char.IsUpper(w.Name.ToString()[0])
|
||||||
|| w is { RowId: > 1000, UserType: 101 or 201 }))
|
|| w is { RowId: > 1000, Region: 101 or 201 }))
|
||||||
.ToDictionary(w => (ushort)w.RowId, w => w.Name.ToString());
|
.ToDictionary(w => (ushort)w.RowId, w => w.Name.ToString());
|
||||||
});
|
});
|
||||||
JobData = new(() =>
|
JobData = new(() =>
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ using FFXIVClientStructs.FFXIV.Client.Graphics.Scene;
|
|||||||
using FFXIVClientStructs.Havok.Common.Serialize.Resource;
|
using FFXIVClientStructs.Havok.Common.Serialize.Resource;
|
||||||
using FFXIVClientStructs.Havok.Animation;
|
using FFXIVClientStructs.Havok.Animation;
|
||||||
using FFXIVClientStructs.Havok.Common.Base.Types;
|
using FFXIVClientStructs.Havok.Common.Base.Types;
|
||||||
|
using FFXIVClientStructs.Havok.Common.Serialize.Resource;
|
||||||
using FFXIVClientStructs.Havok.Common.Serialize.Util;
|
using FFXIVClientStructs.Havok.Common.Serialize.Util;
|
||||||
using LightlessSync.FileCache;
|
using LightlessSync.FileCache;
|
||||||
using LightlessSync.Interop.GameModel;
|
using LightlessSync.Interop.GameModel;
|
||||||
@@ -10,6 +11,7 @@ using LightlessSync.LightlessConfiguration;
|
|||||||
using LightlessSync.PlayerData.Factories;
|
using LightlessSync.PlayerData.Factories;
|
||||||
using LightlessSync.PlayerData.Handlers;
|
using LightlessSync.PlayerData.Handlers;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
|
using OtterGui.Text.EndObjects;
|
||||||
using System.Collections.Concurrent;
|
using System.Collections.Concurrent;
|
||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
@@ -130,315 +132,135 @@ public sealed partial class XivDataAnalyzer
|
|||||||
return (output.Count != 0 && output.Values.All(v => v.Count > 0)) ? output : null;
|
return (output.Count != 0 && output.Values.All(v => v.Count > 0)) ? output : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public unsafe Dictionary<string, List<ushort>>? GetBoneIndicesFromPap(string hash, bool persistToConfig = true)
|
public static byte[]? ReadHavokBytesFromPap(string papPath)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrWhiteSpace(hash))
|
using var fs = File.Open(papPath, FileMode.Open, FileAccess.Read, FileShare.Read);
|
||||||
return null;
|
|
||||||
|
|
||||||
if (_configService.Current.BonesDictionary.TryGetValue(hash, out var cached) && cached is not null)
|
|
||||||
return cached;
|
|
||||||
|
|
||||||
var cacheEntity = _fileCacheManager.GetFileCacheByHash(hash);
|
|
||||||
if (cacheEntity == null || string.IsNullOrEmpty(cacheEntity.ResolvedFilepath) || !File.Exists(cacheEntity.ResolvedFilepath))
|
|
||||||
return null;
|
|
||||||
|
|
||||||
using var fs = File.Open(cacheEntity.ResolvedFilepath, FileMode.Open, FileAccess.Read, FileShare.Read);
|
|
||||||
using var reader = new BinaryReader(fs);
|
using var reader = new BinaryReader(fs);
|
||||||
|
|
||||||
// PAP header (mostly from vfxeditor)
|
_ = reader.ReadInt32();
|
||||||
try
|
_ = reader.ReadInt32();
|
||||||
{
|
_ = reader.ReadInt16();
|
||||||
_ = reader.ReadInt32(); // ignore
|
_ = reader.ReadInt16();
|
||||||
_ = reader.ReadInt32(); // ignore
|
|
||||||
var numAnimations = reader.ReadInt16(); // num animations
|
|
||||||
var modelId = reader.ReadInt16(); // modelid
|
|
||||||
|
|
||||||
if (numAnimations < 0 || numAnimations > 1000)
|
var type = reader.ReadByte();
|
||||||
{
|
if (type != 0) return null;
|
||||||
_logger.LogWarning("PAP file {hash} has invalid animation count {count}, skipping", hash, numAnimations);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
var type = reader.ReadByte(); // type
|
_ = reader.ReadByte();
|
||||||
if (type != 0)
|
_ = reader.ReadInt32();
|
||||||
return null; // not human
|
|
||||||
|
|
||||||
_ = reader.ReadByte(); // variant
|
|
||||||
_ = reader.ReadInt32(); // ignore
|
|
||||||
|
|
||||||
var havokPosition = reader.ReadInt32();
|
var havokPosition = reader.ReadInt32();
|
||||||
var footerPosition = reader.ReadInt32();
|
var footerPosition = reader.ReadInt32();
|
||||||
|
|
||||||
if (havokPosition <= 0 || footerPosition <= havokPosition ||
|
if (havokPosition <= 0 || footerPosition <= havokPosition || footerPosition > fs.Length)
|
||||||
footerPosition > fs.Length || havokPosition >= fs.Length)
|
|
||||||
{
|
|
||||||
_logger.LogWarning("PAP file {hash} has invalid offsets (havok={havok}, footer={footer}, length={length})",
|
|
||||||
hash, havokPosition, footerPosition, fs.Length);
|
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
|
var sizeLong = (long)footerPosition - havokPosition;
|
||||||
|
if (sizeLong <= 8 || sizeLong > int.MaxValue)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
var size = (int)sizeLong;
|
||||||
|
|
||||||
|
fs.Position = havokPosition;
|
||||||
|
var bytes = reader.ReadBytes(size);
|
||||||
|
return bytes.Length > 8 ? bytes : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
var havokDataSizeLong = (long)footerPosition - havokPosition;
|
public unsafe Dictionary<string, List<ushort>>? ParseHavokBytesOnFrameworkThread(
|
||||||
if (havokDataSizeLong <= 8 || havokDataSizeLong > int.MaxValue)
|
byte[] havokData,
|
||||||
|
string hash,
|
||||||
|
bool persistToConfig)
|
||||||
{
|
{
|
||||||
_logger.LogWarning("PAP file {hash} has invalid Havok data size {size}", hash, havokDataSizeLong);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
var havokDataSize = (int)havokDataSizeLong;
|
|
||||||
|
|
||||||
reader.BaseStream.Position = havokPosition;
|
|
||||||
|
|
||||||
var havokData = new byte[havokDataSize];
|
|
||||||
var bytesRead = reader.Read(havokData, 0, havokDataSize);
|
|
||||||
if (bytesRead != havokDataSize)
|
|
||||||
{
|
|
||||||
_logger.LogWarning("PAP file {hash}: Expected to read {expected} bytes but got {actual}",
|
|
||||||
hash, havokDataSize, bytesRead);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (havokData.Length < 8)
|
|
||||||
return null;
|
|
||||||
|
|
||||||
var tempSets = new Dictionary<string, HashSet<ushort>>(StringComparer.OrdinalIgnoreCase);
|
var tempSets = new Dictionary<string, HashSet<ushort>>(StringComparer.OrdinalIgnoreCase);
|
||||||
|
|
||||||
var tempFileName = $"lightless_pap_{Guid.NewGuid():N}_{hash.Substring(0, Math.Min(8, hash.Length))}.hkx";
|
var tempHkxPath = Path.Combine(Path.GetTempPath(), $"lightless_{Guid.NewGuid():N}.hkx");
|
||||||
var tempHavokDataPath = Path.Combine(Path.GetTempPath(), tempFileName);
|
IntPtr pathAnsi = IntPtr.Zero;
|
||||||
IntPtr tempHavokDataPathAnsi = IntPtr.Zero;
|
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var tempDir = Path.GetDirectoryName(tempHavokDataPath);
|
File.WriteAllBytes(tempHkxPath, havokData);
|
||||||
if (!Directory.Exists(tempDir))
|
|
||||||
{
|
|
||||||
_logger.LogWarning("Temp directory {dir} doesn't exist", tempDir);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
File.WriteAllBytes(tempHavokDataPath, havokData);
|
pathAnsi = Marshal.StringToHGlobalAnsi(tempHkxPath);
|
||||||
|
|
||||||
if (!File.Exists(tempHavokDataPath))
|
hkSerializeUtil.LoadOptions loadOptions = default;
|
||||||
{
|
loadOptions.TypeInfoRegistry = hkBuiltinTypeRegistry.Instance()->GetTypeInfoRegistry();
|
||||||
_logger.LogWarning("Temporary havok file was not created at {path}", tempHavokDataPath);
|
loadOptions.ClassNameRegistry = hkBuiltinTypeRegistry.Instance()->GetClassNameRegistry();
|
||||||
return null;
|
loadOptions.Flags = new hkFlags<hkSerializeUtil.LoadOptionBits, int>
|
||||||
}
|
|
||||||
|
|
||||||
var writtenFileInfo = new FileInfo(tempHavokDataPath);
|
|
||||||
if (writtenFileInfo.Length != havokData.Length)
|
|
||||||
{
|
|
||||||
_logger.LogWarning("Written temp file size mismatch: expected {expected}, got {actual}",
|
|
||||||
havokData.Length, writtenFileInfo.Length);
|
|
||||||
File.Delete(tempHavokDataPath);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
tempHavokDataPathAnsi = Marshal.StringToHGlobalAnsi(tempHavokDataPath);
|
|
||||||
|
|
||||||
var loadoptions = stackalloc hkSerializeUtil.LoadOptions[1];
|
|
||||||
loadoptions->TypeInfoRegistry = hkBuiltinTypeRegistry.Instance()->GetTypeInfoRegistry();
|
|
||||||
loadoptions->ClassNameRegistry = hkBuiltinTypeRegistry.Instance()->GetClassNameRegistry();
|
|
||||||
loadoptions->Flags = new hkFlags<hkSerializeUtil.LoadOptionBits, int>
|
|
||||||
{
|
{
|
||||||
Storage = (int)hkSerializeUtil.LoadOptionBits.Default
|
Storage = (int)hkSerializeUtil.LoadOptionBits.Default
|
||||||
};
|
};
|
||||||
|
|
||||||
hkResource* resource = null;
|
hkSerializeUtil.LoadOptions* pOpts = &loadOptions;
|
||||||
try
|
|
||||||
{
|
|
||||||
resource = hkSerializeUtil.LoadFromFile((byte*)tempHavokDataPathAnsi, null, loadoptions);
|
|
||||||
}
|
|
||||||
catch (SEHException ex)
|
|
||||||
{
|
|
||||||
_logger.LogError(ex, "SEH exception loading Havok file from {path} (hash={hash}). Native error code: 0x{code:X}",
|
|
||||||
tempHavokDataPath, hash, ex.ErrorCode);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
var resource = hkSerializeUtil.LoadFromFile((byte*)pathAnsi, errorResult: null, pOpts);
|
||||||
if (resource == null)
|
if (resource == null)
|
||||||
{
|
|
||||||
_logger.LogDebug("Havok resource was null after loading from {path} (hash={hash})", tempHavokDataPath, hash);
|
|
||||||
return null;
|
return null;
|
||||||
}
|
|
||||||
|
|
||||||
if ((nint)resource == nint.Zero || !IsValidPointer((IntPtr)resource))
|
|
||||||
{
|
|
||||||
_logger.LogDebug("Havok resource pointer is invalid (hash={hash})", hash);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
var rootLevelName = @"hkRootLevelContainer"u8;
|
var rootLevelName = @"hkRootLevelContainer"u8;
|
||||||
fixed (byte* n1 = rootLevelName)
|
fixed (byte* n1 = rootLevelName)
|
||||||
{
|
{
|
||||||
var container = (hkRootLevelContainer*)resource->GetContentsPointer(n1, hkBuiltinTypeRegistry.Instance()->GetTypeInfoRegistry());
|
var container = (hkRootLevelContainer*)resource->GetContentsPointer(
|
||||||
if (container == null)
|
n1, hkBuiltinTypeRegistry.Instance()->GetTypeInfoRegistry());
|
||||||
{
|
|
||||||
_logger.LogDebug("hkRootLevelContainer is null (hash={hash})", hash);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ((nint)container == nint.Zero || !IsValidPointer((IntPtr)container))
|
if (container == null) return null;
|
||||||
{
|
|
||||||
_logger.LogDebug("hkRootLevelContainer pointer is invalid (hash={hash})", hash);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
var animationName = @"hkaAnimationContainer"u8;
|
var animationName = @"hkaAnimationContainer"u8;
|
||||||
fixed (byte* n2 = animationName)
|
fixed (byte* n2 = animationName)
|
||||||
{
|
{
|
||||||
var animContainer = (hkaAnimationContainer*)container->findObjectByName(n2, null);
|
var animContainer = (hkaAnimationContainer*)container->findObjectByName(n2, null);
|
||||||
if (animContainer == null)
|
if (animContainer == null) return null;
|
||||||
{
|
|
||||||
_logger.LogDebug("hkaAnimationContainer is null (hash={hash})", hash);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ((nint)animContainer == nint.Zero || !IsValidPointer((IntPtr)animContainer))
|
|
||||||
{
|
|
||||||
_logger.LogDebug("hkaAnimationContainer pointer is invalid (hash={hash})", hash);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (animContainer->Bindings.Length < 0 || animContainer->Bindings.Length > 10000)
|
|
||||||
{
|
|
||||||
_logger.LogDebug("Invalid bindings count {count} (hash={hash})", animContainer->Bindings.Length, hash);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int i = 0; i < animContainer->Bindings.Length; i++)
|
for (int i = 0; i < animContainer->Bindings.Length; i++)
|
||||||
{
|
{
|
||||||
var binding = animContainer->Bindings[i].ptr;
|
var binding = animContainer->Bindings[i].ptr;
|
||||||
if (binding == null)
|
if (binding == null) continue;
|
||||||
continue;
|
|
||||||
|
|
||||||
if ((nint)binding == nint.Zero || !IsValidPointer((IntPtr)binding))
|
|
||||||
{
|
|
||||||
_logger.LogDebug("Skipping invalid binding at index {index} (hash={hash})", i, hash);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
var rawSkel = binding->OriginalSkeletonName.String;
|
var rawSkel = binding->OriginalSkeletonName.String;
|
||||||
var skeletonKey = CanonicalizeSkeletonKey(rawSkel);
|
var skeletonKey = CanonicalizeSkeletonKey(rawSkel);
|
||||||
if (string.IsNullOrEmpty(skeletonKey))
|
if (string.IsNullOrEmpty(skeletonKey)) continue;
|
||||||
continue;
|
|
||||||
|
|
||||||
var boneTransform = binding->TransformTrackToBoneIndices;
|
var boneTransform = binding->TransformTrackToBoneIndices;
|
||||||
if (boneTransform.Length <= 0 || boneTransform.Length > 10000)
|
if (boneTransform.Length <= 0) continue;
|
||||||
{
|
|
||||||
_logger.LogDebug("Invalid bone transform length {length} for skeleton {skel} (hash={hash})",
|
|
||||||
boneTransform.Length, skeletonKey, hash);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!tempSets.TryGetValue(skeletonKey, out var set))
|
if (!tempSets.TryGetValue(skeletonKey, out var set))
|
||||||
{
|
tempSets[skeletonKey] = set = [];
|
||||||
set = [];
|
|
||||||
tempSets[skeletonKey] = set;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int boneIdx = 0; boneIdx < boneTransform.Length; boneIdx++)
|
for (int boneIdx = 0; boneIdx < boneTransform.Length; boneIdx++)
|
||||||
{
|
{
|
||||||
var v = boneTransform[boneIdx];
|
var v = boneTransform[boneIdx];
|
||||||
if (v < 0 || v > ushort.MaxValue)
|
if (v < 0) continue;
|
||||||
continue;
|
|
||||||
set.Add((ushort)v);
|
set.Add((ushort)v);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (SEHException ex)
|
|
||||||
{
|
|
||||||
_logger.LogError(ex, "SEH exception processing PAP file {hash} from {path}. Error code: 0x{code:X}",
|
|
||||||
hash, tempHavokDataPath, ex.ErrorCode);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
_logger.LogError(ex, "Managed exception loading havok file {hash} from {path}", hash, tempHavokDataPath);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
if (tempHavokDataPathAnsi != IntPtr.Zero)
|
if (pathAnsi != IntPtr.Zero)
|
||||||
Marshal.FreeHGlobal(tempHavokDataPathAnsi);
|
Marshal.FreeHGlobal(pathAnsi);
|
||||||
|
|
||||||
int retryCount = 3;
|
try { if (File.Exists(tempHkxPath)) File.Delete(tempHkxPath); }
|
||||||
while (retryCount > 0 && File.Exists(tempHavokDataPath))
|
catch { /* ignore */ }
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
File.Delete(tempHavokDataPath);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
catch (IOException ex)
|
|
||||||
{
|
|
||||||
retryCount--;
|
|
||||||
if (retryCount == 0)
|
|
||||||
{
|
|
||||||
_logger.LogDebug(ex, "Failed to delete temporary havok file after retries: {path}", tempHavokDataPath);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Thread.Sleep(50);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
_logger.LogDebug(ex, "Unexpected error deleting temporary havok file: {path}", tempHavokDataPath);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (tempSets.Count == 0)
|
if (tempSets.Count == 0) return null;
|
||||||
{
|
|
||||||
_logger.LogDebug("No bone sets found in PAP file (hash={hash})", hash);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
var output = new Dictionary<string, List<ushort>>(tempSets.Count, StringComparer.OrdinalIgnoreCase);
|
var output = new Dictionary<string, List<ushort>>(tempSets.Count, StringComparer.OrdinalIgnoreCase);
|
||||||
foreach (var (key, set) in tempSets)
|
foreach (var (key, set) in tempSets)
|
||||||
{
|
{
|
||||||
if (set.Count == 0) continue;
|
if (set.Count == 0) continue;
|
||||||
|
|
||||||
var list = set.ToList();
|
var list = set.ToList();
|
||||||
list.Sort();
|
list.Sort();
|
||||||
output[key] = list;
|
output[key] = list;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (output.Count == 0)
|
if (output.Count == 0) return null;
|
||||||
return null;
|
|
||||||
|
|
||||||
_configService.Current.BonesDictionary[hash] = output;
|
_configService.Current.BonesDictionary[hash] = output;
|
||||||
|
if (persistToConfig) _configService.Save();
|
||||||
if (persistToConfig)
|
|
||||||
_configService.Save();
|
|
||||||
|
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
_logger.LogError(ex, "Outer exception reading PAP file (hash={hash})", hash);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static bool IsValidPointer(IntPtr ptr)
|
|
||||||
{
|
|
||||||
if (ptr == IntPtr.Zero)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
_ = Marshal.ReadByte(ptr);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
catch
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public static string CanonicalizeSkeletonKey(string? raw)
|
public static string CanonicalizeSkeletonKey(string? raw)
|
||||||
{
|
{
|
||||||
@@ -517,41 +339,56 @@ public sealed partial class XivDataAnalyzer
|
|||||||
if (mode == AnimationValidationMode.Unsafe)
|
if (mode == AnimationValidationMode.Unsafe)
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
var papBuckets = papBoneIndices.Keys
|
var papByBucket = new Dictionary<string, List<ushort>>(StringComparer.OrdinalIgnoreCase);
|
||||||
.Select(CanonicalizeSkeletonKey)
|
|
||||||
.Where(k => !string.IsNullOrEmpty(k))
|
|
||||||
.Distinct(StringComparer.OrdinalIgnoreCase)
|
|
||||||
.ToList();
|
|
||||||
|
|
||||||
if (papBuckets.Count == 0)
|
foreach (var (rawKey, list) in papBoneIndices)
|
||||||
|
{
|
||||||
|
var key = CanonicalizeSkeletonKey(rawKey);
|
||||||
|
if (string.IsNullOrEmpty(key))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (string.Equals(key, "skeleton", StringComparison.OrdinalIgnoreCase))
|
||||||
|
key = "__any__";
|
||||||
|
|
||||||
|
if (!papByBucket.TryGetValue(key, out var acc))
|
||||||
|
papByBucket[key] = acc = [];
|
||||||
|
|
||||||
|
if (list is { Count: > 0 })
|
||||||
|
acc.AddRange(list);
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var k in papByBucket.Keys.ToList())
|
||||||
|
papByBucket[k] = papByBucket[k].Distinct().ToList();
|
||||||
|
|
||||||
|
if (papByBucket.Count == 0)
|
||||||
{
|
{
|
||||||
reason = "No skeleton bucket bindings found in the PAP";
|
reason = "No skeleton bucket bindings found in the PAP";
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mode == AnimationValidationMode.Safe)
|
static bool AllIndicesOk(
|
||||||
|
HashSet<ushort> available,
|
||||||
|
List<ushort> indices,
|
||||||
|
bool papLikelyOneBased,
|
||||||
|
bool allowOneBasedShift,
|
||||||
|
bool allowNeighborTolerance,
|
||||||
|
out ushort missing)
|
||||||
{
|
{
|
||||||
if (papBuckets.Any(b => localBoneSets.ContainsKey(b)))
|
foreach (var idx in indices)
|
||||||
|
{
|
||||||
|
if (!ContainsIndexCompat(available, idx, papLikelyOneBased, allowOneBasedShift, allowNeighborTolerance))
|
||||||
|
{
|
||||||
|
missing = idx;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
missing = 0;
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
reason = $"No matching skeleton bucket between PAP [{string.Join(", ", papBuckets)}] and local [{string.Join(", ", localBoneSets.Keys.Order())}].";
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (var bucket in papBuckets)
|
foreach (var (bucket, indices) in papByBucket)
|
||||||
{
|
{
|
||||||
if (!localBoneSets.TryGetValue(bucket, out var available))
|
|
||||||
{
|
|
||||||
reason = $"Missing skeleton bucket '{bucket}' on local actor.";
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
var indices = papBoneIndices
|
|
||||||
.Where(kvp => string.Equals(CanonicalizeSkeletonKey(kvp.Key), bucket, StringComparison.OrdinalIgnoreCase))
|
|
||||||
.SelectMany(kvp => kvp.Value ?? Enumerable.Empty<ushort>())
|
|
||||||
.Distinct()
|
|
||||||
.ToList();
|
|
||||||
|
|
||||||
if (indices.Count == 0)
|
if (indices.Count == 0)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
@@ -565,14 +402,32 @@ public sealed partial class XivDataAnalyzer
|
|||||||
}
|
}
|
||||||
bool papLikelyOneBased = allowOneBasedShift && (min == 1) && has1 && !has0;
|
bool papLikelyOneBased = allowOneBasedShift && (min == 1) && has1 && !has0;
|
||||||
|
|
||||||
foreach (var idx in indices)
|
if (string.Equals(bucket, "__any__", StringComparison.OrdinalIgnoreCase))
|
||||||
{
|
{
|
||||||
if (!ContainsIndexCompat(available, idx, papLikelyOneBased, allowOneBasedShift, allowNeighborTolerance))
|
foreach (var (lk, ls) in localBoneSets)
|
||||||
{
|
{
|
||||||
reason = $"No compatible local skeleton for PAP '{bucket}': missing bone index {idx}.";
|
if (AllIndicesOk(ls, indices, papLikelyOneBased, allowOneBasedShift, allowNeighborTolerance, out _))
|
||||||
|
goto nextBucket;
|
||||||
|
}
|
||||||
|
|
||||||
|
reason = $"No compatible local skeleton bucket for generic PAP skeleton '{bucket}'. Local buckets: {string.Join(", ", localBoneSets.Keys)}";
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!localBoneSets.TryGetValue(bucket, out var available))
|
||||||
|
{
|
||||||
|
reason = $"Missing skeleton bucket '{bucket}' on local actor.";
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!AllIndicesOk(available, indices, papLikelyOneBased, allowOneBasedShift, allowNeighborTolerance, out var missing))
|
||||||
|
{
|
||||||
|
reason = $"No compatible local skeleton for PAP '{bucket}': missing bone index {missing}.";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
nextBucket:
|
||||||
|
;
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
Reference in New Issue
Block a user