forked from praydog/REFramework
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'praydog:master' into master
- Loading branch information
Showing
3 changed files
with
105 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
#include "WindowFilter.hpp" | ||
|
||
// To prevent usage of statics (TLS breaks the present thread...?) | ||
WindowFilter g_window_filter{}; | ||
|
||
WindowFilter& WindowFilter::get() { | ||
return g_window_filter; | ||
} | ||
|
||
WindowFilter::WindowFilter() { | ||
// We create a job thread because GetWindowTextA can actually deadlock inside | ||
// the present thread... | ||
m_job_thread = std::make_unique<std::jthread>([this](std::stop_token s){ | ||
while (!s.stop_requested()) { | ||
std::this_thread::sleep_for(std::chrono::milliseconds{100}); | ||
|
||
if (m_window_jobs.empty()) { | ||
return; | ||
} | ||
|
||
std::vector<HWND> window_jobs{}; | ||
|
||
{ | ||
std::scoped_lock _{m_mutex}; | ||
window_jobs = std::move(m_window_jobs); | ||
} | ||
|
||
for (const auto hwnd : window_jobs) { | ||
if (is_filtered_nocache(hwnd)) { | ||
filter_window(hwnd); | ||
} | ||
} | ||
} | ||
}); | ||
} | ||
|
||
WindowFilter::~WindowFilter() { | ||
m_job_thread->request_stop(); | ||
m_job_thread->join(); | ||
} | ||
|
||
bool WindowFilter::is_filtered(HWND hwnd) { | ||
if (hwnd == nullptr) { | ||
return true; | ||
} | ||
|
||
std::scoped_lock _{m_mutex}; | ||
|
||
if (m_filtered_windows.find(hwnd) != m_filtered_windows.end()) { | ||
return true; | ||
} | ||
|
||
// if we havent even seen this window yet, add it to the job queue | ||
// and return true; | ||
if (m_seen_windows.find(hwnd) == m_seen_windows.end()) { | ||
m_seen_windows.insert(hwnd); | ||
m_window_jobs.push_back(hwnd); | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
bool WindowFilter::is_filtered_nocache(HWND hwnd) { | ||
// get window name | ||
char window_name[256]{}; | ||
GetWindowTextA(hwnd, window_name, sizeof(window_name)); | ||
|
||
const auto sv = std::string_view{window_name}; | ||
|
||
if (sv.find("UE4SS") != std::string_view::npos) { | ||
return true; | ||
} | ||
|
||
if (sv.find("PimaxXR") != std::string_view::npos) { | ||
return true; | ||
} | ||
|
||
// TODO: more problematic windows | ||
return false; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,35 @@ | ||
#pragma once | ||
|
||
#include <mutex> | ||
#include <Windows.h> | ||
|
||
#include <string_view> | ||
#include <unordered_set> | ||
#include <thread> | ||
#include <vector> | ||
#include <mutex> | ||
|
||
class WindowFilter { | ||
public: | ||
static WindowFilter& get() { | ||
static WindowFilter instance{}; | ||
return instance; | ||
} | ||
static WindowFilter& get(); | ||
|
||
public: | ||
bool is_filtered(HWND hwnd) { | ||
if (hwnd == nullptr) { | ||
return true; | ||
} | ||
|
||
std::scoped_lock _{m_mutex}; | ||
|
||
if (m_filtered_windows.find(hwnd) != m_filtered_windows.end()) { | ||
return true; | ||
} | ||
|
||
if (m_seen_windows.find(hwnd) != m_seen_windows.end()) { | ||
return false; | ||
} | ||
|
||
m_seen_windows.insert(hwnd); | ||
WindowFilter(); | ||
virtual ~WindowFilter(); | ||
|
||
if (is_filtered_nocache(hwnd)) { | ||
filter_window(hwnd); | ||
return true; | ||
} | ||
bool is_filtered(HWND hwnd); | ||
|
||
return false; | ||
} | ||
|
||
private: | ||
void filter_window(HWND hwnd) { | ||
std::scoped_lock _{m_mutex}; | ||
m_filtered_windows.insert(hwnd); | ||
} | ||
|
||
bool is_filtered_nocache(HWND hwnd) { | ||
// get window name | ||
char window_name[256]{}; | ||
GetWindowTextA(hwnd, window_name, sizeof(window_name)); | ||
|
||
const auto sv = std::string_view{window_name}; | ||
|
||
if (sv.find("PimaxXR") != std::string_view::npos) { | ||
return true; | ||
} | ||
private: | ||
bool is_filtered_nocache(HWND hwnd); | ||
|
||
// TODO: more problematic windows | ||
return false; | ||
} | ||
std::recursive_mutex m_mutex{}; | ||
std::vector<HWND> m_window_jobs{}; | ||
std::unique_ptr<std::jthread> m_job_thread{}; | ||
|
||
std::mutex m_mutex{}; | ||
std::unordered_set<HWND> m_seen_windows{}; | ||
std::unordered_set<HWND> m_filtered_windows{}; | ||
}; |