412 lines
14 KiB
C#
412 lines
14 KiB
C#
using FFXIVClientStructs.FFXIV.Client.Game.Character;
|
|
using FFXIVClientStructs.FFXIV.Client.Graphics.Scene;
|
|
using FFXIVClientStructs.Havok.Animation;
|
|
using FFXIVClientStructs.Havok.Common.Base.Types;
|
|
using FFXIVClientStructs.Havok.Common.Serialize.Util;
|
|
using LightlessSync.FileCache;
|
|
using LightlessSync.Interop.GameModel;
|
|
using LightlessSync.LightlessConfiguration;
|
|
using LightlessSync.PlayerData.Handlers;
|
|
using Microsoft.Extensions.Logging;
|
|
using System.Runtime.InteropServices;
|
|
using System.Text.RegularExpressions;
|
|
|
|
namespace LightlessSync.Services;
|
|
|
|
public sealed class XivDataAnalyzer
|
|
{
|
|
private readonly ILogger<XivDataAnalyzer> _logger;
|
|
private readonly FileCacheManager _fileCacheManager;
|
|
private readonly XivDataStorageService _configService;
|
|
private readonly List<string> _failedCalculatedTris = [];
|
|
|
|
public XivDataAnalyzer(ILogger<XivDataAnalyzer> logger, FileCacheManager fileCacheManager,
|
|
XivDataStorageService configService)
|
|
{
|
|
_logger = logger;
|
|
_fileCacheManager = fileCacheManager;
|
|
_configService = configService;
|
|
}
|
|
|
|
public unsafe Dictionary<string, List<ushort>>? GetSkeletonBoneIndices(GameObjectHandler handler)
|
|
{
|
|
if (handler is null || handler.Address == nint.Zero)
|
|
return null;
|
|
|
|
Dictionary<string, HashSet<ushort>> sets = new(StringComparer.OrdinalIgnoreCase);
|
|
|
|
try
|
|
{
|
|
var drawObject = ((Character*)handler.Address)->GameObject.DrawObject;
|
|
if (drawObject == null)
|
|
return null;
|
|
|
|
var chara = (CharacterBase*)drawObject;
|
|
if (chara->GetModelType() != CharacterBase.ModelType.Human)
|
|
return null;
|
|
|
|
var skeleton = chara->Skeleton;
|
|
if (skeleton == null)
|
|
return null;
|
|
|
|
var resHandles = skeleton->SkeletonResourceHandles;
|
|
var partialCount = skeleton->PartialSkeletonCount;
|
|
if (partialCount <= 0)
|
|
return null;
|
|
|
|
for (int i = 0; i < partialCount; i++)
|
|
{
|
|
var handle = *(resHandles + i);
|
|
if ((nint)handle == nint.Zero)
|
|
continue;
|
|
|
|
if (handle->FileName.Length > 1024)
|
|
continue;
|
|
|
|
var rawName = handle->FileName.ToString();
|
|
if (string.IsNullOrWhiteSpace(rawName))
|
|
continue;
|
|
|
|
var skeletonKey = CanonicalizeSkeletonKey(rawName);
|
|
if (string.IsNullOrEmpty(skeletonKey))
|
|
continue;
|
|
|
|
var boneCount = handle->BoneCount;
|
|
if (boneCount == 0)
|
|
continue;
|
|
|
|
var havokSkel = handle->HavokSkeleton;
|
|
if ((nint)havokSkel == nint.Zero)
|
|
continue;
|
|
|
|
if (!sets.TryGetValue(skeletonKey, out var set))
|
|
{
|
|
set = [];
|
|
sets[skeletonKey] = set;
|
|
}
|
|
|
|
uint maxExclusive = boneCount;
|
|
uint ushortExclusive = (uint)ushort.MaxValue + 1u;
|
|
if (maxExclusive > ushortExclusive)
|
|
maxExclusive = ushortExclusive;
|
|
|
|
for (uint boneIdx = 0; boneIdx < maxExclusive; boneIdx++)
|
|
{
|
|
var name = havokSkel->Bones[boneIdx].Name.String;
|
|
if (name == null)
|
|
continue;
|
|
|
|
set.Add((ushort)boneIdx);
|
|
}
|
|
|
|
_logger.LogTrace("Local skeleton raw file='{raw}', key='{key}', boneCount={count}",
|
|
rawName, skeletonKey, boneCount);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogWarning(ex, "Could not process skeleton data");
|
|
return null;
|
|
}
|
|
|
|
if (sets.Count == 0)
|
|
return null;
|
|
|
|
var output = new Dictionary<string, List<ushort>>(sets.Count, StringComparer.OrdinalIgnoreCase);
|
|
foreach (var (key, set) in sets)
|
|
{
|
|
if (set.Count == 0)
|
|
continue;
|
|
|
|
var list = set.ToList();
|
|
list.Sort();
|
|
output[key] = list;
|
|
}
|
|
|
|
return (output.Count != 0 && output.Values.All(v => v.Count > 0)) ? output : null;
|
|
}
|
|
|
|
public unsafe Dictionary<string, List<ushort>>? GetBoneIndicesFromPap(string hash)
|
|
{
|
|
if (_configService.Current.BonesDictionary.TryGetValue(hash, out var cached))
|
|
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);
|
|
|
|
// most of this is from vfxeditor
|
|
reader.ReadInt32(); // ignore
|
|
reader.ReadInt32(); // ignore
|
|
reader.ReadInt16(); // num animations
|
|
reader.ReadInt16(); // modelid
|
|
var type = reader.ReadByte(); // type
|
|
if (type != 0)
|
|
return null; // not human
|
|
|
|
reader.ReadByte(); // variant
|
|
reader.ReadInt32(); // ignore
|
|
var havokPosition = reader.ReadInt32();
|
|
var footerPosition = reader.ReadInt32();
|
|
|
|
if (havokPosition <= 0 || footerPosition <= havokPosition || footerPosition > fs.Length)
|
|
return null;
|
|
|
|
var havokDataSize = footerPosition - havokPosition;
|
|
reader.BaseStream.Position = havokPosition;
|
|
|
|
var havokData = reader.ReadBytes(havokDataSize);
|
|
if (havokData.Length <= 8)
|
|
return null;
|
|
|
|
var output = new Dictionary<string, List<ushort>>(StringComparer.OrdinalIgnoreCase);
|
|
|
|
// write to temp file
|
|
var tempHavokDataPath = Path.Combine(Path.GetTempPath(), $"lightless_{Guid.NewGuid():N}.hkx");
|
|
var tempHavokDataPathAnsi = IntPtr.Zero;
|
|
|
|
try
|
|
{
|
|
using (var tempFs = new FileStream(tempHavokDataPath, FileMode.CreateNew, FileAccess.Write, FileShare.None, 4096, FileOptions.DeleteOnClose))
|
|
{
|
|
tempFs.Write(havokData, 0, havokData.Length);
|
|
tempFs.Flush(true);
|
|
}
|
|
|
|
if (!File.Exists(tempHavokDataPath))
|
|
{
|
|
_logger.LogTrace("Temporary havok file was deleted before it could be loaded: {path}", 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
|
|
};
|
|
|
|
var resource = hkSerializeUtil.LoadFromFile((byte*)tempHavokDataPathAnsi, null, loadoptions);
|
|
if (resource == null)
|
|
{
|
|
_logger.LogWarning("Havok resource was null after loading from {path}", tempHavokDataPath);
|
|
return null;
|
|
}
|
|
|
|
var rootLevelName = @"hkRootLevelContainer"u8;
|
|
fixed (byte* n1 = rootLevelName)
|
|
{
|
|
var container = (hkRootLevelContainer*)resource->GetContentsPointer(n1, hkBuiltinTypeRegistry.Instance()->GetTypeInfoRegistry());
|
|
if (container == null)
|
|
return null;
|
|
|
|
var animationName = @"hkaAnimationContainer"u8;
|
|
fixed (byte* n2 = animationName)
|
|
{
|
|
var animContainer = (hkaAnimationContainer*)container->findObjectByName(n2, null);
|
|
if (animContainer == null)
|
|
return null;
|
|
|
|
for (int i = 0; i < animContainer->Bindings.Length; i++)
|
|
{
|
|
var binding = animContainer->Bindings[i].ptr;
|
|
if (binding == null)
|
|
continue;
|
|
|
|
var rawSkel = binding->OriginalSkeletonName.String;
|
|
var skeletonKey = CanonicalizeSkeletonKey(rawSkel);
|
|
if (string.IsNullOrEmpty(skeletonKey))
|
|
continue;
|
|
|
|
var boneTransform = binding->TransformTrackToBoneIndices;
|
|
if (boneTransform.Length <= 0)
|
|
continue;
|
|
|
|
if (!output.TryGetValue(skeletonKey, out var list))
|
|
{
|
|
list = new List<ushort>(boneTransform.Length);
|
|
output[skeletonKey] = list;
|
|
}
|
|
|
|
for (int boneIdx = 0; boneIdx < boneTransform.Length; boneIdx++)
|
|
{
|
|
list.Add((ushort)boneTransform[boneIdx]);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
foreach (var key in output.Keys.ToList())
|
|
{
|
|
output[key] = [.. output[key]
|
|
.Distinct()
|
|
.Order()];
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogWarning(ex, "Could not load havok file in {path}", tempHavokDataPath);
|
|
return null;
|
|
}
|
|
finally
|
|
{
|
|
if (tempHavokDataPathAnsi != IntPtr.Zero)
|
|
Marshal.FreeHGlobal(tempHavokDataPathAnsi);
|
|
|
|
try
|
|
{
|
|
if (File.Exists(tempHavokDataPath))
|
|
File.Delete(tempHavokDataPath);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogTrace(ex, "Could not delete temporary havok file: {path}", tempHavokDataPath);
|
|
}
|
|
}
|
|
|
|
_configService.Current.BonesDictionary[hash] = output;
|
|
_configService.Save();
|
|
|
|
return output;
|
|
}
|
|
|
|
private static readonly Regex _bucketPathRegex =
|
|
new(@"(?i)(?:^|/)(?<bucket>c\d{4})(?:/|$)", RegexOptions.Compiled);
|
|
|
|
private static readonly Regex _bucketSklRegex =
|
|
new(@"(?i)\bskl_(?<bucket>c\d{4})[a-z]\d{4}\b", RegexOptions.Compiled);
|
|
|
|
private static readonly Regex _bucketLooseRegex =
|
|
new(@"(?i)(?<![a-z0-9])(?<bucket>c\d{4})(?!\d)", RegexOptions.Compiled);
|
|
|
|
public static string CanonicalizeSkeletonKey(string? raw)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(raw))
|
|
return string.Empty;
|
|
|
|
var s = raw.Replace('\\', '/').Trim();
|
|
|
|
var underscore = s.LastIndexOf('_');
|
|
if (underscore > 0 && underscore + 1 < s.Length && char.IsDigit(s[underscore + 1]))
|
|
s = s[..underscore];
|
|
|
|
if (s.StartsWith("skeleton", StringComparison.OrdinalIgnoreCase))
|
|
return "skeleton";
|
|
|
|
var m = _bucketPathRegex.Match(s);
|
|
if (m.Success)
|
|
return m.Groups["bucket"].Value.ToLowerInvariant();
|
|
|
|
m = _bucketSklRegex.Match(s);
|
|
if (m.Success)
|
|
return m.Groups["bucket"].Value.ToLowerInvariant();
|
|
|
|
m = _bucketLooseRegex.Match(s);
|
|
if (m.Success)
|
|
return m.Groups["bucket"].Value.ToLowerInvariant();
|
|
|
|
return string.Empty;
|
|
}
|
|
|
|
public void DumpLocalSkeletonIndices(GameObjectHandler handler, string? filter = null)
|
|
{
|
|
var skels = GetSkeletonBoneIndices(handler);
|
|
if (skels == null)
|
|
{
|
|
_logger.LogTrace("DumpLocalSkeletonIndices: local skeleton indices are null or not found");
|
|
return;
|
|
}
|
|
|
|
var keys = skels.Keys
|
|
.Order(StringComparer.OrdinalIgnoreCase)
|
|
.ToArray();
|
|
|
|
_logger.LogTrace("Local skeleton indices found ({count}): {keys}",
|
|
keys.Length,
|
|
string.Join(", ", keys));
|
|
|
|
if (!string.IsNullOrWhiteSpace(filter))
|
|
{
|
|
var hits = keys.Where(k =>
|
|
k.Equals(filter, StringComparison.OrdinalIgnoreCase) ||
|
|
k.StartsWith(filter + "_", StringComparison.OrdinalIgnoreCase) ||
|
|
filter.StartsWith(k + "_", StringComparison.OrdinalIgnoreCase) ||
|
|
k.Contains(filter, StringComparison.OrdinalIgnoreCase))
|
|
.ToArray();
|
|
|
|
_logger.LogTrace("Matches found for '{filter}': {hits}",
|
|
filter,
|
|
hits.Length == 0 ? "<none>" : string.Join(", ", hits));
|
|
}
|
|
}
|
|
|
|
public async Task<long> GetTrianglesByHash(string hash)
|
|
{
|
|
if (_configService.Current.TriangleDictionary.TryGetValue(hash, out var cachedTris) && cachedTris > 0)
|
|
return cachedTris;
|
|
|
|
if (_failedCalculatedTris.Contains(hash, StringComparer.Ordinal))
|
|
return 0;
|
|
|
|
var path = _fileCacheManager.GetFileCacheByHash(hash);
|
|
if (path == null || !path.ResolvedFilepath.EndsWith(".mdl", StringComparison.OrdinalIgnoreCase))
|
|
return 0;
|
|
|
|
var filePath = path.ResolvedFilepath;
|
|
|
|
try
|
|
{
|
|
_logger.LogDebug("Detected Model File {path}, calculating Tris", filePath);
|
|
var file = new MdlFile(filePath);
|
|
if (file.LodCount <= 0)
|
|
{
|
|
_failedCalculatedTris.Add(hash);
|
|
_configService.Current.TriangleDictionary[hash] = 0;
|
|
_configService.Save();
|
|
return 0;
|
|
}
|
|
|
|
long tris = 0;
|
|
foreach (var lod in file.Lods)
|
|
{
|
|
try
|
|
{
|
|
var meshIdx = lod.MeshIndex;
|
|
var meshCnt = lod.MeshCount;
|
|
|
|
tris = file.Meshes.Skip(meshIdx).Take(meshCnt).Sum(p => p.IndexCount) / 3;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogDebug(ex, "Could not load lod mesh {mesh} from path {path}", lod.MeshIndex, filePath);
|
|
continue;
|
|
}
|
|
|
|
if (tris > 0)
|
|
{
|
|
_logger.LogDebug("TriAnalysis: {filePath} => {tris} triangles", filePath, tris);
|
|
_configService.Current.TriangleDictionary[hash] = tris;
|
|
_configService.Save();
|
|
break;
|
|
}
|
|
}
|
|
|
|
return tris;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
_failedCalculatedTris.Add(hash);
|
|
_configService.Current.TriangleDictionary[hash] = 0;
|
|
_configService.Save();
|
|
_logger.LogWarning(e, "Could not parse file {file}", filePath);
|
|
return 0;
|
|
}
|
|
}
|
|
}
|