From 7f7c348a3facc56cc63cb84d57eabddec38f6ed8 Mon Sep 17 00:00:00 2001 From: routersys Date: Sun, 13 Sep 2026 11:16:27 +0900 Subject: [PATCH] =?UTF-8?q?=E7=94=9F=E6=88=90=E3=82=92=E5=BE=85=E3=81=A3?= =?UTF-8?q?=E3=81=A6=E3=83=97=E3=83=AD=E3=82=AD=E3=82=B7=E3=82=92=E8=AA=AD?= =?UTF-8?q?=E3=81=BF=E8=BE=BC=E3=82=80=E5=87=A6=E7=90=86=E3=82=92=E8=BF=BD?= =?UTF-8?q?=E5=8A=A0=E3=81=99=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ProxyForge/Sources/ProxyUpgradeLoader.cs | 91 ++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 ProxyForge/Sources/ProxyUpgradeLoader.cs diff --git a/ProxyForge/Sources/ProxyUpgradeLoader.cs b/ProxyForge/Sources/ProxyUpgradeLoader.cs new file mode 100644 index 0000000..8ff709b --- /dev/null +++ b/ProxyForge/Sources/ProxyUpgradeLoader.cs @@ -0,0 +1,91 @@ +using ProxyForge.Cache; +using ProxyForge.Encoding; +using YukkuriMovieMaker.Commons; + +namespace ProxyForge.Sources; + +internal sealed class ProxyUpgradeLoader : IDisposable +{ + const int Waiting = 0; + const int Loading = 1; + const int Finished = 2; + + readonly ProxyVideoSource owner; + readonly IGraphicsDevices devices; + readonly SourceIdentity source; + readonly int scale; + readonly ProxyCache cache; + readonly ProxyGenerationQueue queue; + readonly VideoSourceFactory factory; + int state; + + public ProxyUpgradeLoader( + ProxyVideoSource owner, + IGraphicsDevices devices, + SourceIdentity source, + int scale, + ProxyCache cache, + ProxyGenerationQueue queue, + VideoSourceFactory factory) + { + this.owner = owner; + this.devices = devices; + this.source = source; + this.scale = scale; + this.cache = cache; + this.queue = queue; + this.factory = factory; + + queue.Completed += OnCompleted; + var existing = cache.Find(source, scale); + if (existing is not null) + Begin(existing); + } + + void OnCompleted(SourceIdentity completedSource, int completedScale, ProxyCacheEntry entry) + { + if (completedScale == scale && completedSource.Matches(source.Path, source.Length, source.WriteTimeTicks)) + Begin(entry); + } + + void Begin(ProxyCacheEntry entry) + { + if (Interlocked.CompareExchange(ref state, Loading, Waiting) != Waiting) + return; + + queue.Completed -= OnCompleted; + Task.Run(() => Load(entry)); + } + + void Load(ProxyCacheEntry entry) + { + IGraphicsDevicesAndContext? context = null; + try + { + context = devices.CreateContext(); + var opened = OpenedProxy.Open(context, cache.GetFilePath(entry), entry, factory); + if (opened is null) + { + context.Dispose(); + cache.Remove(entry.Id); + Log.Default.Write($"ProxyForge: 生成したプロキシを開けなかったため取り除きました。{source.Path}"); + return; + } + + var upgrade = new ProxyUpgrade(context, opened.Value); + if (!owner.TryHandOver(upgrade)) + upgrade.Dispose(); + } + catch (Exception exception) + { + context?.Dispose(); + Log.Default.Write($"ProxyForge: プロキシへの切り替えに失敗しました。{source.Path}", exception); + } + } + + public void Dispose() + { + if (Interlocked.Exchange(ref state, Finished) == Waiting) + queue.Completed -= OnCompleted; + } +}