Skip to content
This repository has been archived by the owner on May 20, 2024. It is now read-only.

Commit

Permalink
more refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
shevernitskiy committed Jul 20, 2023
1 parent 4fc4e8f commit cfdfd5f
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 21 deletions.
13 changes: 9 additions & 4 deletions src/hook/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@

namespace Config {

constexpr const auto PATH_OFFSETS = "./dfint_data/offsets/";
constexpr const auto PATH_LOG = "./dfint_data/dfint_log.log";
constexpr const auto PATH_DICTIONARY = "./dfint_data/dfint_dictionary.csv";
constexpr const auto PATH_REPORTS = "./dfint_data/crash_reports/";

inline time_t PETimestamp(const std::string filename) {
std::ifstream file(filename);

Expand All @@ -20,7 +25,7 @@ namespace Config {
}

inline toml::v3::ex::parse_result GetOffsetsFile(time_t target_checksum) {
for (const auto& filepath : std::filesystem::recursive_directory_iterator("./dfint_data/offsets/")) {
for (const auto& filepath : std::filesystem::recursive_directory_iterator(PATH_OFFSETS)) {
auto file = toml::parse_file(filepath.path().c_str());
if (target_checksum == file["metadata"]["checksum"].value_or<time_t>(0)) {
return file;
Expand Down Expand Up @@ -67,13 +72,13 @@ namespace Config {
namespace Setting {

inline auto log_level = Config::config["settings"]["log_level"].value_or<int>(0);
inline auto log_file = Config::config["settings"]["log_file"].value_or<std::string>("./dfint_data/dfint_log.log");
inline auto log_file = Config::config["settings"]["log_file"].value_or<std::string>(PATH_LOG);
inline auto crash_report = Config::config["settings"]["crash_report"].value_or<bool>(true);
inline auto enable_search = Config::config["settings"]["enable_search"].value_or<bool>(true);
inline auto enable_translation = Config::config["settings"]["enable_translation"].value_or<bool>(true);
inline auto enable_patches = Config::config["settings"]["enable_patches"].value_or<bool>(true);
inline auto dictionary = Config::config["settings"]["dictionary"].value_or<std::string>("./dfint_data/dfint_dictionary.csv");
inline auto crash_report_dir = Config::config["settings"]["crash_report_dir"].value_or<std::string>("./dfint_data/crash_reports/");
inline auto dictionary = Config::config["settings"]["dictionary"].value_or<std::string>(PATH_DICTIONARY);
inline auto crash_report_dir = Config::config["settings"]["crash_report_dir"].value_or<std::string>(PATH_REPORTS);
inline auto watchdog = Config::config["settings"]["watchdog"].value_or<bool>(true);

} // namespace Setting
Expand Down
2 changes: 1 addition & 1 deletion src/hook/crash_report.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace CrashReport {

constinit const auto PATH_REPORTS = "./dfint_data/crash_reports/";
constexpr const auto PATH_REPORTS = "./dfint_data/crash_reports/";

std::ofstream GetCrashReportLogHandle(const std::string& filename) {
if (!std::filesystem::is_directory(PATH_REPORTS) || !std::filesystem::exists(PATH_REPORTS)) {
Expand Down
2 changes: 1 addition & 1 deletion src/hook/pch.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
#include <chrono>
#include <codecvt> // for std::codecvt_utf8 - myabe not needed cause we can render utf8
#include <cstddef>
#include <experimental/generator>
#include <filesystem>
#include <format>
#include <fstream>
#include <list>
#include <locale> // for std::wstring_convert - myabe not needed cause we can render utf8
// #include <regex> // remove if regex dont needed
#include <set>
#include <stacktrace>
#include <stdexcept>
Expand Down
53 changes: 38 additions & 15 deletions src/hook/watchdog.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,47 @@
namespace Watchdog {
namespace Handler {

void Keypress() {
enum Action {
TRANSLATE_OFF,
TRANSLATE_ON,
DICTIONARY_RELOAD
};

std::experimental::generator<Action> WaitForAction() {
while (true) {
Sleep(10);
std::this_thread::sleep_for(std::chrono::milliseconds(50));
if ((GetAsyncKeyState(VK_CONTROL) & GetAsyncKeyState(VK_F3)) && Config::Setting::enable_translation == true) {
Config::Setting::enable_translation = false;
logger::info("translation switched off");
MessageBoxA(nullptr, "translation switched off", "dfint hook info", MB_ICONWARNING);
co_yield Action::TRANSLATE_OFF;
} else if ((GetAsyncKeyState(VK_CONTROL) & GetAsyncKeyState(VK_F4)) && Config::Setting::enable_translation == false) {
co_yield Action::TRANSLATE_ON;
} else if (GetAsyncKeyState(VK_CONTROL) & GetAsyncKeyState(VK_F2)) {
co_yield Action::DICTIONARY_RELOAD;
}
if ((GetAsyncKeyState(VK_CONTROL) & GetAsyncKeyState(VK_F4)) && Config::Setting::enable_translation == false) {
Config::Setting::enable_translation = true;
logger::info("translation switched on");
MessageBoxA(nullptr, "translation switched on", "dfint hook info", MB_ICONWARNING);
}
if ((GetAsyncKeyState(VK_CONTROL) & GetAsyncKeyState(VK_F2))) {
logger::info("reload dictionary");
Dictionary::GetSingleton()->Clear();
Dictionary::GetSingleton()->LoadCsv(Config::Setting::dictionary);
MessageBoxA(nullptr, "dictionary reloaded", "dfint hook info", MB_ICONWARNING);
}
}

void Keypress() {
for (auto& action : WaitForAction()) {
switch (action) {
case TRANSLATE_OFF: {
Config::Setting::enable_translation = false;
logger::info("translation switched off");
MessageBoxA(nullptr, "translation switched off", "dfint hook info", MB_ICONWARNING);
break;
}
case TRANSLATE_ON: {
Config::Setting::enable_translation = true;
logger::info("translation switched on");
MessageBoxA(nullptr, "translation switched on", "dfint hook info", MB_ICONWARNING);
break;
}
case DICTIONARY_RELOAD: {
logger::info("reload dictionary");
Dictionary::GetSingleton()->Clear();
Dictionary::GetSingleton()->LoadCsv(Config::Setting::dictionary);
MessageBoxA(nullptr, "dictionary reloaded", "dfint hook info", MB_ICONWARNING);
break;
}
}
}
}
Expand Down

0 comments on commit cfdfd5f

Please sign in to comment.