走査を専用スレッドで行うようにした

このコミットが含まれているのは:
routersys committed 2026-09-09 14:55:58 +09:00
1 親 f10c747423
コミット dca152181d
8ファイル変更
+69 -82

検索条件に一致するファイルはありません

+43 -26
ファイルの表示
@@ -2,8 +2,12 @@
#include <algorithm>
#include <atomic>
#include <condition_variable>
#include <map>
#include <mutex>
#include <chrono>
#include <string>
#include <thread>
#include "HostContext.h"
#include "Log.h"
@@ -18,10 +22,13 @@ namespace {
const wchar_t* kEffect = L"動画ファイル";
const wchar_t* kItem = L"ファイル";
const UINT kAutomaticScan = WM_APP + 71;
HWND g_message_window = nullptr;
std::atomic<bool> g_scanning{false};
std::atomic<unsigned long long> g_last_scan{0};
std::atomic<bool> g_requested{false};
std::atomic<bool> g_stop{false};
std::thread g_worker;
std::mutex g_wake_lock;
std::condition_variable g_wake;
std::string ToUtf8(const std::wstring& text) {
if (text.empty()) return std::string();
@@ -43,16 +50,14 @@ std::wstring FromUtf8(const char* text) {
}
struct Collected {
int layers = 1;
std::vector<OBJECT_HANDLE> objects;
std::vector<std::wstring> files;
};
void CollectObjects(void* param, EDIT_SECTION* edit) {
Collected* collected = (Collected*)param;
EDIT_INFO info{};
if (edit->info) info = *edit->info;
const int layers = std::max(info.layer_max + 1, 1);
for (int layer = 0; layer < layers; layer++) {
for (int layer = 0; layer < collected->layers; layer++) {
int frame = 0;
for (int guard = 0; guard < 20000; guard++) {
OBJECT_HANDLE object = edit->find_object(layer, frame);
@@ -114,12 +119,25 @@ bool Eligible(const std::wstring& path, const MEDIA_INFO& info) {
return false;
}
LRESULT CALLBACK MessageProc(HWND window, UINT message, WPARAM first, LPARAM second) {
if (message == kAutomaticScan) {
if (CurrentSettings().enabled) ApplyProxies();
return 0;
void ScanWorker() {
while (!g_stop.load()) {
{
std::unique_lock<std::mutex> lock(g_wake_lock);
g_wake.wait_for(lock, std::chrono::milliseconds(400));
}
if (g_stop.load()) return;
if (!g_requested.exchange(false)) continue;
if (!CurrentSettings().enabled) continue;
ScanResult result = ApplyProxies();
g_last_scan.store(GetTickCount64());
if (result.swapped > 0) {
Say(L"自動でプロキシへ差し替えました: %d 件", result.swapped);
}
}
return DefWindowProcW(window, message, first, second);
}
void OnHostEvent(void*) {
RequestAutomaticScan();
}
void OnEditMenuApply(EDIT_SECTION*) {
@@ -151,6 +169,7 @@ 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);
SwapRequest request;
@@ -196,6 +215,7 @@ ScanResult ApplyProxies() {
ScanResult RestoreOriginals() {
ScanResult result;
Collected collected;
collected.layers = std::max(EditInfo().layer_max + 1, 1);
CallReadSection(&collected, CollectObjects);
SwapRequest request;
@@ -220,31 +240,28 @@ ScanResult RestoreOriginals() {
}
void RequestAutomaticScan() {
if (g_message_window) PostMessageW(g_message_window, kAutomaticScan, 0, 0);
if (GetTickCount64() - g_last_scan.load() < 1500) return;
g_requested.store(true);
g_wake.notify_all();
}
void StartScanController() {
if (g_message_window) return;
WNDCLASSEXW description{};
description.cbSize = sizeof(description);
description.lpfnWndProc = MessageProc;
description.hInstance = ModuleInstance();
description.lpszClassName = L"ProxyEditScanWindow";
RegisterClassExW(&description);
g_message_window = CreateWindowExW(0, L"ProxyEditScanWindow", L"", 0, 0, 0, 0, 0, HWND_MESSAGE,
nullptr, description.hInstance, nullptr);
if (g_worker.joinable()) return;
g_stop.store(false);
g_worker = std::thread(ScanWorker);
}
void StopScanController() {
if (g_message_window) {
DestroyWindow(g_message_window);
g_message_window = nullptr;
}
g_stop.store(true);
g_wake.notify_all();
if (g_worker.joinable()) g_worker.join();
}
void RegisterScanMenus(HOST_APP_TABLE* host) {
host->register_edit_menu(L"プロキシへ差し替え", OnEditMenuApply);
host->register_edit_menu(L"プロキシから元素材へ戻す", OnEditMenuRestore);
host->register_event_listener(EVENT_TYPE::UPDATE_OBJECT, nullptr, OnHostEvent);
host->register_event_listener(EVENT_TYPE::CHANGE_EDIT_SCENE, nullptr, OnHostEvent);
}
}