Partial roll-up to reduce code divergence
Co-authored-by: rootdarkarchon <root.darkarchon@outlook.com>
This commit is contained in:
@@ -1,12 +1,10 @@
|
||||
using Dalamud.Plugin.Services;
|
||||
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 Lumina;
|
||||
using Lumina.Data.Files;
|
||||
using MareSynchronos.FileCache;
|
||||
using MareSynchronos.Interop.GameModel;
|
||||
using MareSynchronos.MareConfiguration;
|
||||
using MareSynchronos.PlayerData.Handlers;
|
||||
using Microsoft.Extensions.Logging;
|
||||
@@ -19,15 +17,14 @@ public sealed class XivDataAnalyzer
|
||||
private readonly ILogger<XivDataAnalyzer> _logger;
|
||||
private readonly FileCacheManager _fileCacheManager;
|
||||
private readonly XivDataStorageService _configService;
|
||||
private readonly GameData _luminaGameData;
|
||||
private readonly List<string> _failedCalculatedTris = [];
|
||||
|
||||
public XivDataAnalyzer(ILogger<XivDataAnalyzer> logger, FileCacheManager fileCacheManager,
|
||||
XivDataStorageService configService, IDataManager gameData)
|
||||
XivDataStorageService configService)
|
||||
{
|
||||
_logger = logger;
|
||||
_fileCacheManager = fileCacheManager;
|
||||
_configService = configService;
|
||||
_luminaGameData = new GameData(gameData.GameData.DataPath.FullName);
|
||||
}
|
||||
|
||||
public unsafe Dictionary<string, List<ushort>>? GetSkeletonBoneIndices(GameObjectHandler handler)
|
||||
@@ -153,50 +150,65 @@ public sealed class XivDataAnalyzer
|
||||
return output;
|
||||
}
|
||||
|
||||
public Task<long> GetTrianglesFromGamePath(string gamePath)
|
||||
public async Task<long> GetTrianglesByHash(string hash)
|
||||
{
|
||||
if (_configService.Current.TriangleDictionary.TryGetValue(gamePath, out var cachedTris))
|
||||
return Task.FromResult(cachedTris);
|
||||
if (_configService.Current.TriangleDictionary.TryGetValue(hash, out var cachedTris) && cachedTris > 0)
|
||||
return cachedTris;
|
||||
|
||||
_logger.LogDebug("Detected Model File {path}, calculating Tris", gamePath);
|
||||
var file = _luminaGameData.GetFile<MdlFile>(gamePath);
|
||||
if (file == null)
|
||||
return Task.FromResult((long)0);
|
||||
|
||||
if (file.FileHeader.LodCount <= 0)
|
||||
return Task.FromResult((long)0);
|
||||
var meshIdx = file.Lods[0].MeshIndex;
|
||||
var meshCnt = file.Lods[0].MeshCount;
|
||||
var tris = file.Meshes.Skip(meshIdx).Take(meshCnt).Sum(p => p.IndexCount) / 3;
|
||||
|
||||
_logger.LogDebug("{filePath} => {tris} triangles", gamePath, tris);
|
||||
_configService.Current.TriangleDictionary[gamePath] = tris;
|
||||
_configService.Save();
|
||||
return Task.FromResult(tris);
|
||||
}
|
||||
|
||||
public Task<long> GetTrianglesByHash(string hash)
|
||||
{
|
||||
if (_configService.Current.TriangleDictionary.TryGetValue(hash, out var cachedTris))
|
||||
return Task.FromResult(cachedTris);
|
||||
if (_failedCalculatedTris.Contains(hash, StringComparer.Ordinal))
|
||||
return 0;
|
||||
|
||||
var path = _fileCacheManager.GetFileCacheByHash(hash);
|
||||
if (path == null || !path.ResolvedFilepath.EndsWith(".mdl", StringComparison.OrdinalIgnoreCase))
|
||||
return Task.FromResult((long)0);
|
||||
return 0;
|
||||
|
||||
var filePath = path.ResolvedFilepath;
|
||||
|
||||
_logger.LogDebug("Detected Model File {path}, calculating Tris", filePath);
|
||||
var file = _luminaGameData.GetFileFromDisk<MdlFile>(filePath);
|
||||
if (file.FileHeader.LodCount <= 0)
|
||||
return Task.FromResult((long)0);
|
||||
var meshIdx = file.Lods[0].MeshIndex;
|
||||
var meshCnt = file.Lods[0].MeshCount;
|
||||
var tris = file.Meshes.Skip(meshIdx).Take(meshCnt).Sum(p => p.IndexCount) / 3;
|
||||
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;
|
||||
}
|
||||
|
||||
_logger.LogDebug("{filePath} => {tris} triangles", filePath, tris);
|
||||
_configService.Current.TriangleDictionary[hash] = tris;
|
||||
_configService.Save();
|
||||
return Task.FromResult(tris);
|
||||
long tris = 0;
|
||||
for (int i = 0; i < file.LodCount; i++)
|
||||
{
|
||||
try
|
||||
{
|
||||
var meshIdx = file.Lods[i].MeshIndex;
|
||||
var meshCnt = file.Lods[i].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}", i, 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user