diff --git a/src/ExportGuard.cpp b/src/ExportGuard.cpp index bf73842..d99860f 100644 --- a/src/ExportGuard.cpp +++ b/src/ExportGuard.cpp @@ -2,6 +2,7 @@ #include +#include #include #include @@ -17,10 +18,12 @@ const UINT_PTR kSubclassId = 0x50450003; HWND g_host = nullptr; bool g_hooked = false; +UINT g_ready_message = 0; std::set g_output_commands; bool g_collected = false; -bool g_restored = false; -bool g_in_command = false; +std::atomic g_waiting{0}; +std::atomic g_passthrough{0}; +std::atomic g_restored{false}; std::wstring WithoutMarker(const wchar_t* text) { std::wstring result; @@ -72,16 +75,30 @@ void CollectOutputCommands() { LRESULT CALLBACK GuardProc(HWND window, UINT message, WPARAM first, LPARAM second, UINT_PTR, DWORD_PTR) { + if (g_ready_message != 0 && message == g_ready_message) { + const int id = g_waiting.exchange(0); + if (id != 0) { + g_passthrough.store(id); + PostMessageW(window, WM_COMMAND, (WPARAM)id, 0); + } + return 0; + } if (message == WM_COMMAND && second == 0 && HIWORD(first) <= 1) { CollectOutputCommands(); - if (g_output_commands.count((int)LOWORD(first)) > 0 && !g_in_command) { - g_in_command = true; + const int id = (int)LOWORD(first); + if (g_output_commands.count(id) > 0) { + if (g_passthrough.load() == id) { + g_passthrough.store(0); + LRESULT result = DefSubclassProc(window, message, first, second); + SuspendAutomaticScan(false); + if (g_restored.exchange(false)) RequestApply(); + return result; + } + if (g_waiting.load() != 0) return 0; + g_waiting.store(id); SuspendAutomaticScan(true); - g_restored = RestoreForExport() > 0; - LRESULT result = DefSubclassProc(window, message, first, second); - g_in_command = false; - NoticeEditActivity(); - return result; + RequestExportRestore(); + return 0; } } return DefSubclassProc(window, message, first, second); @@ -93,6 +110,7 @@ void StartExportGuard() { if (g_hooked) return; g_host = HostWindow(); if (!g_host) return; + g_ready_message = RegisterWindowMessageW(L"ProxyEditExportReady"); g_hooked = SetWindowSubclass(g_host, GuardProc, kSubclassId, 0) != FALSE; } @@ -103,15 +121,17 @@ void StopExportGuard() { g_host = nullptr; } +void ExportRestoreFinished(bool restored) { + g_restored.store(restored); + if (g_host && g_ready_message != 0) PostMessageW(g_host, g_ready_message, 0, 0); +} + void NoticeEditActivity() { - if (g_in_command) return; + if (g_waiting.load() != 0 || g_passthrough.load() != 0) return; if (!AutomaticScanSuspended()) return; if (EditState() != EDIT_HANDLE::EDIT_STATE_EDIT) return; SuspendAutomaticScan(false); - if (g_restored) { - g_restored = false; - ApplyAfterExport(); - } + if (g_restored.exchange(false)) RequestApply(); } } diff --git a/src/ExportGuard.h b/src/ExportGuard.h index 0ba830c..f161dd7 100644 --- a/src/ExportGuard.h +++ b/src/ExportGuard.h @@ -8,5 +8,6 @@ void StartExportGuard(); void StopExportGuard(); void NoticeEditActivity(); +void ExportRestoreFinished(bool restored); } diff --git a/src/ScanController.cpp b/src/ScanController.cpp index bb6b41e..c4dba5f 100644 --- a/src/ScanController.cpp +++ b/src/ScanController.cpp @@ -12,6 +12,7 @@ #include "HostContext.h" #include "Log.h" #include "ExportGuard.h" +#include "ProxyStore.h" #include "Notify.h" #include "ProxyBuilder.h" #include "ProxyFormat.h" @@ -20,15 +21,10 @@ namespace pe { -ScanResult ApplyProxies(); -ScanResult RestoreOriginals(); -ScanResult RestoreFailed(const std::vector& proxies); - namespace { const wchar_t* kEffect = L"動画ファイル"; const wchar_t* kItem = L"ファイル"; -std::atomic g_scanning{false}; std::atomic g_suspended{false}; std::atomic g_pending{0}; std::atomic g_stop{false}; @@ -41,10 +37,13 @@ enum RequestKind { kRequestApply = 2, kRequestRestore = 4, kRequestRestoreFailed = 8, + kRequestExportRestore = 16, + kRequestDelete = 32, }; std::mutex g_report_lock; std::vector g_failed_proxies; +std::vector g_delete_list; std::vector g_unsupported; void Post(int kind) { @@ -151,84 +150,8 @@ bool Eligible(const std::wstring& path, const MEDIA_INFO& info) { return false; } -void ScanWorker() { - while (true) { - int taken = 0; - { - std::unique_lock lock(g_wake_lock); - g_wake.wait(lock, [] { return g_stop.load() || g_pending.load() != 0; }); - if (g_stop.load()) return; - taken = g_pending.exchange(0); - } - if (taken & kRequestRestoreFailed) { - std::vector proxies; - { - std::lock_guard lock(g_report_lock); - proxies.swap(g_failed_proxies); - } - ScanResult result = RestoreFailed(proxies); - if (result.restored > 0) { - Say(L"生成に失敗したので元素材へ戻しました: %d 件", result.restored); - } - PublishStateChange(); - } - if (taken & kRequestRestore) { - ScanResult result = RestoreOriginals(); - Say(L"元素材へ戻しました: %d 件", result.restored); - PublishStateChange(); - } - if (taken & kRequestApply) { - ScanResult result = ApplyProxies(); - Say(L"プロキシへ差し替えました: 対象 %d 件、差し替え %d 件、対象外 %d 件", result.eligible, - result.swapped, result.rejected); - if (result.unsupported > 0) { - Warn(L"プロキシを作れない素材が %d 件あります。本体は読めますがこのプラグインの復号が対応していない形式です", - result.unsupported); - } - PublishStateChange(); - } else if ((taken & kRequestAutomatic) && CurrentSettings().enabled && - !g_suspended.load()) { - ScanResult result = ApplyProxies(); - if (result.swapped > 0) { - Say(L"自動でプロキシへ差し替えました: %d 件", result.swapped); - } - PublishStateChange(); - (void)result; - } - } -} - -void OnHostEvent(void*) { - NoticeEditActivity(); - Post(kRequestAutomatic); -} - -void OnEditMenuApply(EDIT_SECTION*) { - Post(kRequestApply); -} - -void OnEditMenuRestore(EDIT_SECTION*) { - Post(kRequestRestore); -} - -} - -bool IsProxyPath(const std::wstring& path) { - if (path.size() < 4) return false; - return _wcsicmp(path.c_str() + path.size() - 4, L".pxy") == 0; -} - -std::wstring SourceOfProxy(const std::wstring& path) { - ProxyReader reader; - if (!reader.Open(path)) return std::wstring(); - std::wstring source = reader.Header().source_path; - reader.Close(); - return source; -} - ScanResult ApplyProxies() { ScanResult result; - if (g_scanning.exchange(true)) return result; Collected collected; collected.layers = std::max(EditInfo().layer_max + 1, 1); CallReadSection(&collected, CollectObjects); @@ -306,7 +229,6 @@ ScanResult ApplyProxies() { std::lock_guard lock(g_report_lock); g_unsupported = unsupported; } - g_scanning.store(false); return result; } @@ -373,10 +295,121 @@ ScanResult RestoreFailed(const std::vector& proxies) { return result; } +void ScanWorker() { + while (true) { + int taken = 0; + { + std::unique_lock lock(g_wake_lock); + g_wake.wait(lock, [] { return g_stop.load() || g_pending.load() != 0; }); + if (g_stop.load()) return; + taken = g_pending.exchange(0); + } + if (taken & kRequestExportRestore) { + ScanResult result = RestoreOriginals(); + if (result.restored > 0) { + Say(L"出力のため元素材へ戻しました: %d 件", result.restored); + } + PublishStateChange(); + ExportRestoreFinished(result.restored > 0); + } + if (taken & kRequestDelete) { + std::vector proxies; + { + std::lock_guard lock(g_report_lock); + proxies.swap(g_delete_list); + } + RestoreFailed(proxies); + for (const std::wstring& proxy : proxies) { + std::wstring source = SourceOfProxy(proxy); + if (!source.empty()) DiscardSource(source); + RemoveProxy(proxy); + } + if (!proxies.empty()) Say(L"プロキシを削除しました: %d 件", (int)proxies.size()); + PublishStateChange(); + } + if (taken & kRequestRestoreFailed) { + std::vector proxies; + { + std::lock_guard lock(g_report_lock); + proxies.swap(g_failed_proxies); + } + ScanResult result = RestoreFailed(proxies); + if (result.restored > 0) { + Say(L"生成に失敗したので元素材へ戻しました: %d 件", result.restored); + } + PublishStateChange(); + } + if (taken & kRequestRestore) { + ScanResult result = RestoreOriginals(); + Say(L"元素材へ戻しました: %d 件", result.restored); + PublishStateChange(); + } + if (taken & kRequestApply) { + ScanResult result = ApplyProxies(); + Say(L"プロキシへ差し替えました: 対象 %d 件、差し替え %d 件、対象外 %d 件", result.eligible, + result.swapped, result.rejected); + if (result.unsupported > 0) { + Warn(L"プロキシを作れない素材が %d 件あります。本体は読めますがこのプラグインの復号が対応していない形式です", + result.unsupported); + } + PublishStateChange(); + } else if ((taken & kRequestAutomatic) && CurrentSettings().enabled && + !g_suspended.load()) { + ScanResult result = ApplyProxies(); + if (result.swapped > 0) { + Say(L"自動でプロキシへ差し替えました: %d 件", result.swapped); + } + PublishStateChange(); + (void)result; + } + } +} + +void OnHostEvent(void*) { + NoticeEditActivity(); + Post(kRequestAutomatic); +} + +void OnEditMenuApply(EDIT_SECTION*) { + Post(kRequestApply); +} + +void OnEditMenuRestore(EDIT_SECTION*) { + Post(kRequestRestore); +} + +} + +bool IsProxyPath(const std::wstring& path) { + if (path.size() < 4) return false; + return _wcsicmp(path.c_str() + path.size() - 4, L".pxy") == 0; +} + +std::wstring SourceOfProxy(const std::wstring& path) { + ProxyReader reader; + if (!reader.Open(path)) return std::wstring(); + std::wstring source = reader.Header().source_path; + reader.Close(); + return source; +} + void RequestApply() { Post(kRequestApply); } +void RequestExportRestore() { + Post(kRequestExportRestore); +} + +void RequestDeleteProxies(const std::vector& proxies) { + if (proxies.empty()) return; + { + std::lock_guard lock(g_report_lock); + for (const std::wstring& proxy : proxies) g_delete_list.push_back(proxy); + } + Post(kRequestDelete); +} + void RequestRestoreProxy(const std::wstring& proxy) { { std::lock_guard lock(g_report_lock); @@ -388,30 +421,11 @@ void RequestRestoreProxy(const std::wstring& proxy) { Post(kRequestRestoreFailed); } -int RestoreProxiesNow(const std::vector& proxies) { - ScanResult result = RestoreFailed(proxies); - if (result.restored > 0) PublishStateChange(); - return result.restored; -} - std::vector UnsupportedSources() { std::lock_guard lock(g_report_lock); return g_unsupported; } -int RestoreForExport() { - ScanResult result = RestoreOriginals(); - if (result.restored > 0) Say(L"出力のため元素材へ戻しました: %d 件", result.restored); - PublishStateChange(); - return result.restored; -} - -void ApplyAfterExport() { - ScanResult result = ApplyProxies(); - if (result.swapped > 0) Say(L"出力が終わったのでプロキシへ戻しました: %d 件", result.swapped); - PublishStateChange(); -} - void SuspendAutomaticScan(bool suspend) { g_suspended.store(suspend); } diff --git a/src/ScanController.h b/src/ScanController.h index 72d4549..1daef0d 100644 --- a/src/ScanController.h +++ b/src/ScanController.h @@ -26,12 +26,10 @@ struct Unsupported { void RegisterScanMenus(HOST_APP_TABLE* host); void RequestApply(); void RequestRestore(); - -int RestoreForExport(); void RequestRestoreProxy(const std::wstring& proxy); -int RestoreProxiesNow(const std::vector& proxies); +void RequestExportRestore(); +void RequestDeleteProxies(const std::vector& proxies); std::vector UnsupportedSources(); -void ApplyAfterExport(); void SuspendAutomaticScan(bool suspend); bool AutomaticScanSuspended(); bool IsProxyPath(const std::wstring& path); diff --git a/src/StoreDialog.cpp b/src/StoreDialog.cpp index c6bbee5..8151bf8 100644 --- a/src/StoreDialog.cpp +++ b/src/StoreDialog.cpp @@ -9,7 +9,6 @@ #include "FlatButton.h" #include "HostContext.h" -#include "Log.h" #include "Notify.h" #include "ProxyBuilder.h" #include "ProxyStore.h" @@ -188,20 +187,7 @@ void Erase(const std::vector& targets) { std::vector proxies; proxies.reserve(targets.size()); for (const Row* row : targets) proxies.push_back(row->item.proxy); - RestoreProxiesNow(proxies); - - int erased = 0; - for (const Row* row : targets) { - if (row->in_use && !row->item.source.empty()) { - DiscardSource(row->item.source); - } else { - RemoveProxy(row->item.proxy); - } - erased++; - } - Say(L"プロキシを削除しました: %d 件", erased); - PublishStateChange(); - Refresh(); + RequestDeleteProxies(proxies); } std::vector Selected() {