Skip to content

Commit

Permalink
Merge branch 'praydog:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
joeyhodge authored Sep 12, 2023
2 parents 728e592 + 236a9b8 commit 9c8334d
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 43 deletions.
11 changes: 11 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1975,6 +1975,7 @@ if(REF_BUILD_FRAMEWORK AND CMAKE_SIZEOF_VOID_P EQUAL 8) # build-framework
"src/Main.cpp"
"src/Mods.cpp"
"src/REFramework.cpp"
"src/WindowFilter.cpp"
"src/WindowsMessageHook.cpp"
"src/mods/APIProxy.cpp"
"src/mods/Camera.cpp"
Expand Down Expand Up @@ -2171,6 +2172,7 @@ if(REF_BUILD_FRAMEWORK AND CMAKE_SIZEOF_VOID_P EQUAL 8) # build-framework
"src/Main.cpp"
"src/Mods.cpp"
"src/REFramework.cpp"
"src/WindowFilter.cpp"
"src/WindowsMessageHook.cpp"
"src/mods/APIProxy.cpp"
"src/mods/Camera.cpp"
Expand Down Expand Up @@ -3804,6 +3806,7 @@ if(REF_BUILD_FRAMEWORK AND CMAKE_SIZEOF_VOID_P EQUAL 8) # build-framework
"src/Main.cpp"
"src/Mods.cpp"
"src/REFramework.cpp"
"src/WindowFilter.cpp"
"src/WindowsMessageHook.cpp"
"src/mods/APIProxy.cpp"
"src/mods/Camera.cpp"
Expand Down Expand Up @@ -4000,6 +4003,7 @@ if(REF_BUILD_FRAMEWORK AND CMAKE_SIZEOF_VOID_P EQUAL 8) # build-framework
"src/Main.cpp"
"src/Mods.cpp"
"src/REFramework.cpp"
"src/WindowFilter.cpp"
"src/WindowsMessageHook.cpp"
"src/mods/APIProxy.cpp"
"src/mods/Camera.cpp"
Expand Down Expand Up @@ -4196,6 +4200,7 @@ if(REF_BUILD_FRAMEWORK AND CMAKE_SIZEOF_VOID_P EQUAL 8) # build-framework
"src/Main.cpp"
"src/Mods.cpp"
"src/REFramework.cpp"
"src/WindowFilter.cpp"
"src/WindowsMessageHook.cpp"
"src/mods/APIProxy.cpp"
"src/mods/Camera.cpp"
Expand Down Expand Up @@ -6549,6 +6554,7 @@ if(REF_BUILD_FRAMEWORK AND CMAKE_SIZEOF_VOID_P EQUAL 8) # build-framework
"src/Main.cpp"
"src/Mods.cpp"
"src/REFramework.cpp"
"src/WindowFilter.cpp"
"src/WindowsMessageHook.cpp"
"src/mods/APIProxy.cpp"
"src/mods/Camera.cpp"
Expand Down Expand Up @@ -6745,6 +6751,7 @@ if(REF_BUILD_FRAMEWORK AND CMAKE_SIZEOF_VOID_P EQUAL 8) # build-framework
"src/Main.cpp"
"src/Mods.cpp"
"src/REFramework.cpp"
"src/WindowFilter.cpp"
"src/WindowsMessageHook.cpp"
"src/mods/APIProxy.cpp"
"src/mods/Camera.cpp"
Expand Down Expand Up @@ -7661,6 +7668,7 @@ if(REF_BUILD_FRAMEWORK AND CMAKE_SIZEOF_VOID_P EQUAL 8) # build-framework
"src/Main.cpp"
"src/Mods.cpp"
"src/REFramework.cpp"
"src/WindowFilter.cpp"
"src/WindowsMessageHook.cpp"
"src/mods/APIProxy.cpp"
"src/mods/Camera.cpp"
Expand Down Expand Up @@ -8575,6 +8583,7 @@ if(REF_BUILD_FRAMEWORK AND CMAKE_SIZEOF_VOID_P EQUAL 8) # build-framework
"src/Main.cpp"
"src/Mods.cpp"
"src/REFramework.cpp"
"src/WindowFilter.cpp"
"src/WindowsMessageHook.cpp"
"src/mods/APIProxy.cpp"
"src/mods/Camera.cpp"
Expand Down Expand Up @@ -9491,6 +9500,7 @@ if(REF_BUILD_FRAMEWORK AND CMAKE_SIZEOF_VOID_P EQUAL 8) # build-framework
"src/Main.cpp"
"src/Mods.cpp"
"src/REFramework.cpp"
"src/WindowFilter.cpp"
"src/WindowsMessageHook.cpp"
"src/mods/APIProxy.cpp"
"src/mods/Camera.cpp"
Expand Down Expand Up @@ -10407,6 +10417,7 @@ if(REF_BUILD_FRAMEWORK AND CMAKE_SIZEOF_VOID_P EQUAL 8) # build-framework
"src/Main.cpp"
"src/Mods.cpp"
"src/REFramework.cpp"
"src/WindowFilter.cpp"
"src/WindowsMessageHook.cpp"
"src/mods/APIProxy.cpp"
"src/mods/Camera.cpp"
Expand Down
81 changes: 81 additions & 0 deletions src/WindowFilter.cpp
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;
}
56 changes: 13 additions & 43 deletions src/WindowFilter.hpp
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{};
};

0 comments on commit 9c8334d

Please sign in to comment.