big clean up in progress 1

This commit is contained in:
2025-11-27 00:29:56 +09:00
parent 1cdc0a90f9
commit 8cc83bce79
36 changed files with 389 additions and 522 deletions

View File

@@ -12,16 +12,14 @@ using LightlessSync.Services;
using LightlessSync.Services.Mediator;
using LightlessSync.Services.TextureCompression;
using LightlessSync.Utils;
using Penumbra.Api.Enums;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using OtterTex;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Threading;
using System.Threading.Tasks;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
using ImageSharpImage = SixLabors.ImageSharp.Image;
namespace LightlessSync.UI;
@@ -810,11 +808,11 @@ public class DataAnalysisUi : WindowMediatorSubscriberBase
var primaryGamePath = entry.GamePaths.FirstOrDefault() ?? string.Empty;
var classificationPath = string.IsNullOrEmpty(primaryGamePath) ? primaryFile : primaryGamePath;
var mapKind = _textureMetadataHelper.DetermineMapKind(primaryGamePath, primaryFile);
var category = _textureMetadataHelper.DetermineCategory(classificationPath);
var slot = _textureMetadataHelper.DetermineSlot(category, classificationPath);
var category = TextureMetadataHelper.DetermineCategory(classificationPath);
var slot = TextureMetadataHelper.DetermineSlot(category, classificationPath);
var format = entry.Format.Value;
var suggestion = _textureMetadataHelper.GetSuggestedTarget(format, mapKind);
TextureCompressionTarget? currentTarget = _textureMetadataHelper.TryMapFormatToTarget(format, out var mappedTarget)
var suggestion = TextureMetadataHelper.GetSuggestedTarget(format, mapKind);
TextureCompressionTarget? currentTarget = TextureMetadataHelper.TryMapFormatToTarget(format, out var mappedTarget)
? mappedTarget
: null;
var displayName = Path.GetFileName(primaryFile);
@@ -2014,23 +2012,43 @@ public class DataAnalysisUi : WindowMediatorSubscriberBase
private async Task<IDalamudTextureWrap?> BuildPreviewAsync(TextureRow row, CancellationToken token)
{
if (!_ipcManager.Penumbra.APIAvailable)
const int PreviewMaxDimension = 1024;
token.ThrowIfCancellationRequested();
if (!File.Exists(row.PrimaryFilePath))
{
return null;
}
var tempFile = Path.Combine(Path.GetTempPath(), $"lightless_preview_{Guid.NewGuid():N}.png");
try
{
var job = new TextureConversionJob(row.PrimaryFilePath, tempFile, TextureType.Png, IncludeMipMaps: false);
await _ipcManager.Penumbra.ConvertTextureFiles(_logger, new[] { job }, null, token).ConfigureAwait(false);
if (!File.Exists(tempFile))
using var scratch = TexFileHelper.Load(row.PrimaryFilePath);
using var rgbaScratch = scratch.GetRGBA(out var rgbaInfo).ThrowIfError(rgbaInfo);
var meta = rgbaInfo.Meta;
var width = meta.Width;
var height = meta.Height;
var bytesPerPixel = meta.Format.BitsPerPixel() / 8;
var requiredLength = width * height * bytesPerPixel;
token.ThrowIfCancellationRequested();
var rgbaPixels = rgbaScratch.Pixels[..requiredLength].ToArray();
using var image = ImageSharpImage.LoadPixelData<Rgba32>(rgbaPixels, width, height);
if (Math.Max(width, height) > PreviewMaxDimension)
{
return null;
var dominant = Math.Max(width, height);
var scale = PreviewMaxDimension / (float)dominant;
var targetWidth = Math.Max(1, (int)MathF.Round(width * scale));
var targetHeight = Math.Max(1, (int)MathF.Round(height * scale));
image.Mutate(ctx => ctx.Resize(targetWidth, targetHeight, KnownResamplers.Lanczos3));
}
var data = await File.ReadAllBytesAsync(tempFile, token).ConfigureAwait(false);
return _uiSharedService.LoadImage(data);
using var ms = new MemoryStream();
await image.SaveAsPngAsync(ms, cancellationToken: token).ConfigureAwait(false);
return _uiSharedService.LoadImage(ms.ToArray());
}
catch (OperationCanceledException)
{
@@ -2041,20 +2059,6 @@ public class DataAnalysisUi : WindowMediatorSubscriberBase
_logger.LogDebug(ex, "Preview generation failed for {File}", row.PrimaryFilePath);
return null;
}
finally
{
try
{
if (File.Exists(tempFile))
{
File.Delete(tempFile);
}
}
catch (Exception ex)
{
_logger.LogTrace(ex, "Failed to clean up preview temp file {File}", tempFile);
}
}
}
private void ResetPreview(string key)
@@ -2291,7 +2295,7 @@ public class DataAnalysisUi : WindowMediatorSubscriberBase
{
_textureSelections[row.Key] = selectedTarget;
}
var hasSelectedInfo = _textureMetadataHelper.TryGetRecommendationInfo(selectedTarget, out var selectedInfo);
var hasSelectedInfo = TextureMetadataHelper.TryGetRecommendationInfo(selectedTarget, out var selectedInfo);
using (ImRaii.Child("textureDetailInfo", new Vector2(-1, 0), true, ImGuiWindowFlags.AlwaysVerticalScrollbar))
{
@@ -2425,7 +2429,7 @@ public class DataAnalysisUi : WindowMediatorSubscriberBase
if (row.SuggestedTarget.HasValue)
{
var recommendedTarget = row.SuggestedTarget.Value;
var hasRecommendationInfo = _textureMetadataHelper.TryGetRecommendationInfo(recommendedTarget, out var recommendedInfo);
var hasRecommendationInfo = TextureMetadataHelper.TryGetRecommendationInfo(recommendedTarget, out var recommendedInfo);
var recommendedTitle = hasRecommendationInfo ? recommendedInfo!.Title : recommendedTarget.ToString();
var recommendedDescription = hasRecommendationInfo
? recommendedInfo!.Description
@@ -2634,4 +2638,4 @@ public class DataAnalysisUi : WindowMediatorSubscriberBase
}
}
}
}
}