From a64fa9b3dd13828852da42be637306d1f0e596ab Mon Sep 17 00:00:00 2001 From: Akash Mozumdar Date: Mon, 18 Feb 2019 23:12:12 -0500 Subject: [PATCH] reorganize includes --- GUI/CMakeLists.txt | 12 ++++---- GUI/extenwindow.cpp | 1 - GUI/extenwindow.h | 2 ++ GUI/host/host.cpp | 1 - GUI/host/textthread.cpp | 1 - GUI/host/util.cpp | 1 - GUI/mainwindow.h | 3 ++ extensions/bingtranslate.cpp | 1 - extensions/googletranslate.cpp | 1 - extensions/lua.cpp | 3 +- extensions/removerepeat.cpp | 1 - extensions/replacer.cpp | 3 +- extensions/util.h | 1 - include/common.h | 51 ++++++++++++++++++++++++++++++++ include/defs.h | 31 -------------------- include/types.h | 53 +++++----------------------------- vnrhook/texthook.cc | 2 -- 17 files changed, 73 insertions(+), 95 deletions(-) diff --git a/GUI/CMakeLists.txt b/GUI/CMakeLists.txt index 5969a92..32746e2 100644 --- a/GUI/CMakeLists.txt +++ b/GUI/CMakeLists.txt @@ -1,10 +1,8 @@ include(QtUtils) msvc_registry_search() find_qt5(Core Widgets) -set(AUTOMOC OFF) -set(RESOURCE_FILES Textractor.rc Textractor.ico) -set(gui_src +add_executable(${PROJECT_NAME} WIN32 main.cpp mainwindow.cpp extenwindow.cpp @@ -12,9 +10,9 @@ set(gui_src host/host.cpp host/textthread.cpp host/util.cpp - ${RESOURCE_FILES} + Textractor.rc + Textractor.ico ) -add_executable(${PROJECT_NAME} WIN32 ${gui_src}) -target_link_libraries(${PROJECT_NAME} Qt5::Widgets winhttp) +target_link_libraries(${PROJECT_NAME} Qt5::Widgets shell32 winhttp) -install_qt5_libs(${PROJECT_NAME}) # can be commented out for consecutive builds +#install_qt5_libs(${PROJECT_NAME}) # can be commented out for consecutive builds diff --git a/GUI/extenwindow.cpp b/GUI/extenwindow.cpp index 9ab7c31..ca845da 100644 --- a/GUI/extenwindow.cpp +++ b/GUI/extenwindow.cpp @@ -2,7 +2,6 @@ #include "ui_extenwindow.h" #include "defs.h" #include "text.h" -#include "types.h" #include "misc.h" #include #include diff --git a/GUI/extenwindow.h b/GUI/extenwindow.h index 1391623..cc2d1eb 100644 --- a/GUI/extenwindow.h +++ b/GUI/extenwindow.h @@ -22,6 +22,8 @@ public: ~ExtenWindow(); private: + inline static constexpr auto EXTEN_SAVE_FILE = u8"SavedExtensions.txt"; + void Add(QFileInfo extenFile); void Sync(); bool eventFilter(QObject* target, QEvent* event) override; diff --git a/GUI/host/host.cpp b/GUI/host/host.cpp index a7b7798..deb8748 100644 --- a/GUI/host/host.cpp +++ b/GUI/host/host.cpp @@ -1,5 +1,4 @@ #include "host.h" -#include "const.h" #include "text.h" #include "defs.h" #include "util.h" diff --git a/GUI/host/textthread.cpp b/GUI/host/textthread.cpp index a8f4733..63b8848 100644 --- a/GUI/host/textthread.cpp +++ b/GUI/host/textthread.cpp @@ -1,5 +1,4 @@ #include "textthread.h" -#include "const.h" #include "text.h" #include "host.h" #include "util.h" diff --git a/GUI/host/util.cpp b/GUI/host/util.cpp index 7df766c..7b1be64 100644 --- a/GUI/host/util.cpp +++ b/GUI/host/util.cpp @@ -1,5 +1,4 @@ #include "util.h" -#include "defs.h" #include "host.h" #include #include diff --git a/GUI/mainwindow.h b/GUI/mainwindow.h index 8112901..9962de7 100644 --- a/GUI/mainwindow.h +++ b/GUI/mainwindow.h @@ -16,6 +16,9 @@ public: ~MainWindow(); private: + inline static constexpr auto HOOK_SAVE_FILE = u8"SavedHooks.txt"; + inline static constexpr auto GAME_SAVE_FILE = u8"SavedGames.txt"; + inline static thread_local bool ok = false; void closeEvent(QCloseEvent*) override; diff --git a/extensions/bingtranslate.cpp b/extensions/bingtranslate.cpp index f7cfa39..63acd78 100644 --- a/extensions/bingtranslate.cpp +++ b/extensions/bingtranslate.cpp @@ -1,5 +1,4 @@ #include "extension.h" -#include "defs.h" #include "text.h" #include "network.h" #include diff --git a/extensions/googletranslate.cpp b/extensions/googletranslate.cpp index 5fda786..e334fee 100644 --- a/extensions/googletranslate.cpp +++ b/extensions/googletranslate.cpp @@ -1,5 +1,4 @@ #include "extension.h" -#include "defs.h" #include "text.h" #include "util.h" #include "network.h" diff --git a/extensions/lua.cpp b/extensions/lua.cpp index 8791d0f..9b2f07b 100644 --- a/extensions/lua.cpp +++ b/extensions/lua.cpp @@ -1,7 +1,6 @@ #include "extension.h" #include "util.h" #include "text.h" -#include "defs.h" #include #include #include @@ -9,6 +8,8 @@ #include #include +constexpr auto LUA_SAVE_FILE = u8"Textractor.lua"; + extern "C" // Lua library { enum LuaType { LUA_TNIL, LUA_TBOOLEAN, LUA_TLIGHTUSERDATA, LUA_TNUMBER, LUA_TSTRING, LUA_TTABLE, LUA_TFUNCTION, LUA_TUSERDATA, LUA_TTHREAD }; diff --git a/extensions/removerepeat.cpp b/extensions/removerepeat.cpp index ff73b87..dfa8cbc 100644 --- a/extensions/removerepeat.cpp +++ b/extensions/removerepeat.cpp @@ -1,5 +1,4 @@ #include "extension.h" -#include "defs.h" void RemoveRepeatedChars(std::wstring& sentence) { diff --git a/extensions/replacer.cpp b/extensions/replacer.cpp index 6dab462..1ffb15e 100644 --- a/extensions/replacer.cpp +++ b/extensions/replacer.cpp @@ -1,5 +1,4 @@ #include "extension.h" -#include "defs.h" #include "text.h" #include #include @@ -7,6 +6,8 @@ #include #include +constexpr auto REPLACE_SAVE_FILE = u8"SavedReplacements.txt"; + std::atomic replaceFileLastWrite; struct diff --git a/extensions/util.h b/extensions/util.h index 0d72277..3fc3470 100644 --- a/extensions/util.h +++ b/extensions/util.h @@ -1,7 +1,6 @@ #pragma once #include "common.h" -#include "types.h" class RateLimiter { diff --git a/include/common.h b/include/common.h index 5e92080..ae7e70b 100644 --- a/include/common.h +++ b/include/common.h @@ -18,3 +18,54 @@ #include #include #include + +template using Array = T[]; + +template +class ThreadSafe +{ +public: + template + ThreadSafe(Args&&... args) : contents(std::forward(args)...) {} + auto operator->() + { + struct + { + E* operator->() { return ptr; } + std::unique_lock lock; + E* ptr; + } lockedProxy{ std::unique_lock(mtx), &contents }; + return lockedProxy; + } + +private: + E contents; + M mtx; +}; + +template +struct Functor +{ + template + auto operator()(Args&&... args) const { return std::invoke(F, std::forward(args)...); } +}; + +template > +class AutoHandle +{ +public: + AutoHandle(HANDLE h) : h(h) {} + operator HANDLE() { return h.get(); } + PHANDLE operator&() { static_assert(sizeof(*this) == sizeof(HANDLE)); return (PHANDLE)this; } + operator bool() { return h.get() != NULL && h.get() != INVALID_HANDLE_VALUE; } + +private: + struct HandleCleaner { void operator()(void* h) { if (h != INVALID_HANDLE_VALUE) HandleCloser()(h); } }; + std::unique_ptr h; +}; + +#ifdef _DEBUG +#define TEST(...) inline AutoHandle<> TEST__RUNNER__DUMMY = CreateThread(nullptr, 0, [](auto) { __VA_ARGS__; return 0UL; }, NULL, 0, nullptr); +#else +#define TEST(...) +#endif diff --git a/include/defs.h b/include/defs.h index de2f537..f9a7f00 100644 --- a/include/defs.h +++ b/include/defs.h @@ -3,8 +3,6 @@ // vnrhook/defs.h // 8/23/2013 jichi -#include "common.h" - // Pipes constexpr auto HOOK_PIPE = L"\\\\.\\pipe\\TEXTRACTOR_HOOK"; @@ -27,38 +25,9 @@ constexpr auto PIPE_AVAILABLE_EVENT = L"TEXTRACTOR_PIPE_AVAILABLE"; constexpr auto ITH_DLL = L"vnrhook"; // .dll but LoadLibrary automatically adds that constexpr auto CONFIG_FILE = u8"Textractor.ini"; -constexpr auto HOOK_SAVE_FILE = u8"SavedHooks.txt"; -constexpr auto GAME_SAVE_FILE = u8"SavedGames.txt"; -constexpr auto EXTEN_SAVE_FILE = u8"SavedExtensions.txt"; -constexpr auto REPLACE_SAVE_FILE = u8"SavedReplacements.txt"; -constexpr auto LUA_SAVE_FILE = u8"Textractor.lua"; // Misc constexpr auto DEFAULT_EXTENSIONS = u8"Remove Repetition>Lua>Copy to Clipboard>Bing Translate>Extra Window>Extra Newlines"; -inline SECURITY_ATTRIBUTES allAccess = std::invoke([] // allows non-admin processes to access kernel objects made by admin processes -{ - static SECURITY_DESCRIPTOR sd = {}; - InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION); - SetSecurityDescriptorDacl(&sd, TRUE, NULL, FALSE); - return SECURITY_ATTRIBUTES{ sizeof(SECURITY_ATTRIBUTES), &sd, FALSE }; -}); - -// Functions - -template -inline void FORMAT_MESSAGE(const char* format, Args... args) -{ - char buffer[250] = {}; - sprintf_s<250>(buffer, format, args...); - MessageBoxA(NULL, buffer, "Textractor Message", MB_OK); -} - -#ifdef _DEBUG -#define TEST(...) inline auto TEST__RUNNER__DUMMY = (CloseHandle(CreateThread(nullptr, 0, [](auto) { __VA_ARGS__; return 0UL; }, NULL, 0, nullptr)), 0); -#else -#define TEST(...) -#endif - // EOF diff --git a/include/types.h b/include/types.h index 15819bc..e17420a 100644 --- a/include/types.h +++ b/include/types.h @@ -3,51 +3,6 @@ #include "common.h" #include "const.h" -template using Array = T[]; - -template -struct Functor -{ - template - auto operator()(Args&&... args) const { return std::invoke(F, std::forward(args)...); } -}; - -template -class ThreadSafe -{ -public: - template - ThreadSafe(Args&&... args) : contents(std::forward(args)...) {} - auto operator->() - { - struct - { - E* operator->() { return ptr; } - std::unique_lock lock; - E* ptr; - } lockedProxy{ std::unique_lock(mtx), &contents }; - return lockedProxy; - } - -private: - E contents; - M mtx; -}; - -template > -class AutoHandle -{ -public: - AutoHandle(HANDLE h) : h(h) {} - operator HANDLE() { return h.get(); } - PHANDLE operator&() { static_assert(sizeof(*this) == sizeof(HANDLE)); return (PHANDLE)this; } - operator bool() { return h.get() != NULL && h.get() != INVALID_HANDLE_VALUE; } - -private: - struct HandleCleaner { void operator()(void* h) { if (h != INVALID_HANDLE_VALUE) HandleCloser()(h); } }; - std::unique_ptr h; -}; - class WinMutex // Like CMutex but works with scoped_lock { public: @@ -59,6 +14,14 @@ private: AutoHandle<> m; }; +inline SECURITY_ATTRIBUTES allAccess = std::invoke([] // allows non-admin processes to access kernel objects made by admin processes +{ + static SECURITY_DESCRIPTOR sd = {}; + InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION); + SetSecurityDescriptorDacl(&sd, TRUE, NULL, FALSE); + return SECURITY_ATTRIBUTES{ sizeof(SECURITY_ATTRIBUTES), &sd, FALSE }; +}); + // jichi 3/7/2014: Add guessed comment struct HookParam { diff --git a/vnrhook/texthook.cc b/vnrhook/texthook.cc index 6f6a487..10363b1 100644 --- a/vnrhook/texthook.cc +++ b/vnrhook/texthook.cc @@ -6,8 +6,6 @@ #include "texthook.h" #include "engine/match.h" #include "main.h" -#include "const.h" -#include "defs.h" #include "text.h" #include "ithsys/ithsys.h"