ファイル
YMM4-ProxyForge/ZeroDiskProxy/ViewModels/SettingsViewModel.cs
T
routersys c1400be515 スレッド安全性・ゼロアロケーション・設計品質の全面的な改善
ProxyCacheManagerのStartProxyGenerationにおいてContainsKeyとTryAddの間に存在したTOCTOU競合を除去し、TryAddの戻り値のみで重複判断するよう修正しました。
RunGenerationAsyncで3箇所に重複していたActiveGenerations.Remove呼び出しをループ後の単一パスに集約し、delayMs変数で各ケースの遅延時間を管理するよう統一しました。
FallbackStreamのWriteメソッドで_inner.Writeの呼び出しを_switchLockの内側に移動し、SwitchToDiskCoreをロック内から呼ぶ内部メソッドとして分離することでWriteとSwitchToDiskの競合を解消しました。
ProxyCacheEntryのSetMemoryDataおよびSetDiskPathにおいて旧フィールドをクリアしてから新フィールドを設定する順序に変更し、IsValidがどちらもnullという一貫性のない状態を観測しないよう保証しました。
MemoryBudgetのTryAllocateにおいてGetAvailablePhysicalMemoryの呼び出しをCASループ外からループ内に移動し、ループを複数回回る間に物理メモリの実態が変化しても常に最新の値で判断するよう修正しました。
ProxyCacheManagerのMakeCacheKeyを文字列補間からstring.Create+stackallocベースに書き換え、中間文字列アロケーションを排除しました。
SettingsViewModelとCacheEntryViewModelに重複していたFormatBytesをUtilities/ByteFormatterとして一元化し、stackallocとstring.Createを組み合わせることでアロケーションをゼロに抑えました。
IDialogServiceインターフェースとWpfDialogService実装を新規追加し、SettingsViewModelが直接呼び出していたSystem.Windows.MessageBox.Showをインターフェース経由に変更しました。
SettingsViewModelのコンストラクタをProxyCacheManagerとMemoryBudgetを直接受け取る形に変更し、PluginHost.Instanceへの直接依存を除去しました。
PluginHost.Instanceの解決はZeroDiskProxySettingsView.xaml.csのコンポジションルートで行うよう責務を整理しました。
GenerationPopupViewのOnItemsChangedからShow・Hideの呼び出しを削除し、ウィンドウ可視性の管理をPluginHost.OnGenerationsChangedに一元化しました。
MfProxyEncoderにValidateInputPathとValidateTempPathを追加し、パストラバーサル攻撃を防止しました。
ValidateTempPathはstackallocとSpan<char>を用いてゼロアロケーションで_fallbackDirectory配下であることを検証します。
ExportDetectorのウィンドウタイトルバッファを256文字から512文字に拡大し、長いウィンドウタイトルによる切り捨てを防止しました。
MfProxyEncoder.AlignTo16をprivate staticからinternal staticに変更しテスト可能にしました。
WinRT APIへの移行により不要になったMfGuids内の全GUIDを削除し、実際に参照されているMF_MT_MAX_KEYFRAME_SPACINGのみを残しました。
2026-03-24 15:42:37 +09:00

163 行
5.5 KiB
C#
Raw Blame 履歴

このファイルには曖昧(ambiguous)なUnicode文字が含まれています
このファイルには、他の文字と見間違える可能性があるUnicode文字が含まれています。 それが意図的なものと考えられる場合は、この警告を無視して構いません。 それらの文字を表示するにはエスケープボタンを使用します。
using System.Collections.ObjectModel;
using System.Windows.Input;
using YukkuriMovieMaker.Commons;
using ZeroDiskProxy.Core;
using ZeroDiskProxy.Interfaces;
using ZeroDiskProxy.Localization;
using ZeroDiskProxy.Memory;
using ZeroDiskProxy.Utilities;
namespace ZeroDiskProxy.ViewModels;
internal sealed class SettingsViewModel : Bindable
{
private readonly ProxyCacheManager? _cacheManager;
private readonly MemoryBudget? _budget;
private readonly IDialogService _dialogService;
private string _cacheSummaryText = Translate.CacheSummaryLoading;
public string CacheSummaryText
{
get => _cacheSummaryText;
set => Set(ref _cacheSummaryText, value);
}
private string _memoryUsageText = "";
public string MemoryUsageText
{
get => _memoryUsageText;
set => Set(ref _memoryUsageText, value);
}
private bool _hasCacheEntries;
public bool HasCacheEntries
{
get => _hasCacheEntries;
set => Set(ref _hasCacheEntries, value);
}
public ObservableCollection<CacheEntryViewModel> CacheEntries { get; } = [];
public ICommand ClearCacheCommand { get; }
public ICommand RefreshCacheInfoCommand { get; }
public ICommand RemoveCacheEntryCommand { get; }
internal SettingsViewModel(ProxyCacheManager? cacheManager, MemoryBudget? budget, IDialogService dialogService)
{
_cacheManager = cacheManager;
_budget = budget;
_dialogService = dialogService;
ClearCacheCommand = new ActionCommand(ExecuteClearCache);
RefreshCacheInfoCommand = new ActionCommand(ExecuteRefreshCacheInfo);
RemoveCacheEntryCommand = new ActionCommand<CacheEntryViewModel>(ExecuteRemoveEntry);
ExecuteRefreshCacheInfo();
}
private void ExecuteClearCache()
{
if (_cacheManager is null)
return;
var (count, totalSize) = _cacheManager.ClearAll();
if (count == 0)
{
_dialogService.ShowInformation(Translate.CacheNoEntriesToDelete, Translate.AppName);
}
else
{
_dialogService.ShowInformation(
string.Format(Translate.CacheDeleteSummaryFormat, count.ToString("N0"), ByteFormatter.Format(totalSize)),
Translate.AppName);
}
ExecuteRefreshCacheInfo();
}
private void ExecuteRefreshCacheInfo()
{
if (_cacheManager is null)
{
CacheSummaryText = Translate.CacheSummaryPluginNotInitialized;
MemoryUsageText = "";
HasCacheEntries = false;
return;
}
var (count, memSize, diskSize, memCount, diskCount) = _cacheManager.GetCacheInfo();
CacheSummaryText = string.Format(Translate.CacheSummaryCountFormat, count.ToString("N0"));
var usageText = string.Format(
Translate.CacheUsageMemoryAndDiskFormat,
memCount.ToString("N0"),
ByteFormatter.Format(memSize),
diskCount.ToString("N0"),
ByteFormatter.Format(diskSize));
if (_budget is not null)
usageText = string.Concat(
usageText,
string.Format(Translate.CacheUsageAllocatedFormat, ByteFormatter.Format(_budget.AllocatedBytes)));
MemoryUsageText = usageText;
CacheEntries.Clear();
var snapshots = _cacheManager.GetAllSnapshots();
foreach (var snap in snapshots)
CacheEntries.Add(new CacheEntryViewModel(snap));
HasCacheEntries = CacheEntries.Count > 0;
}
private void ExecuteRemoveEntry(CacheEntryViewModel? entry)
{
if (entry is null || _cacheManager is null)
return;
_cacheManager.RemoveProxy(entry.OriginalPath, entry.Scale);
ExecuteRefreshCacheInfo();
}
}
internal sealed class CacheEntryViewModel : Bindable
{
public string FileName { get; }
public string OriginalPath { get; }
public float Scale { get; }
public string Resolution { get; }
public bool IsInMemory { get; }
public string StorageType { get; }
public long DataSize { get; }
public string DataSizeText { get; }
public string CreatedAtText { get; }
public string LastAccessedText { get; }
public string ScaleText { get; }
internal CacheEntryViewModel(CacheEntrySnapshot snapshot)
{
FileName = snapshot.FileName;
OriginalPath = snapshot.OriginalPath;
Scale = snapshot.Scale;
Resolution = string.Concat(snapshot.ProxyWidth.ToString(), "×", snapshot.ProxyHeight.ToString());
IsInMemory = snapshot.IsInMemory;
StorageType = snapshot.IsInMemory ? Translate.StorageMemory : Translate.StorageDisk;
DataSize = snapshot.DataSize;
DataSizeText = ByteFormatter.Format(snapshot.DataSize);
CreatedAtText = snapshot.CreatedAt.ToLocalTime().ToString("HH:mm:ss");
LastAccessedText = snapshot.LastAccessedAt.ToLocalTime().ToString("HH:mm:ss");
ScaleText = string.Concat((snapshot.Scale * 100).ToString("F0"), "%");
}
}
internal sealed class ActionCommand(Action execute) : ICommand
{
public event EventHandler? CanExecuteChanged { add { } remove { } }
public bool CanExecute(object? parameter) => true;
public void Execute(object? parameter) => execute();
}
internal sealed class ActionCommand<T>(Action<T?> execute) : ICommand where T : class
{
public event EventHandler? CanExecuteChanged { add { } remove { } }
public bool CanExecute(object? parameter) => true;
public void Execute(object? parameter) => execute(parameter as T);
}