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

Commit

Permalink
some refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
shevernitskiy committed Jul 18, 2023
1 parent e08ab51 commit 4fc4e8f
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 106 deletions.
3 changes: 1 addition & 2 deletions src/hook/cache.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ class LRUCache {
}

std::optional<std::reference_wrapper<ValueType>> Get(const KeyType& key) {
auto it = items_map.find(key);
if (it == items_map.end()) {
if (auto it = items_map.find(key); it == items_map.end()) {
return std::nullopt;
} else {
items_list.splice(items_list.begin(), items_list, it->second);
Expand Down
83 changes: 29 additions & 54 deletions src/hook/crash_report.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,89 +2,66 @@

namespace CrashReport {

std::ofstream GetCrashReportLogHandle(std::string filename) {
if (!std::filesystem::is_directory("./dfint_data/crash_reports/") || !std::filesystem::exists("./dfint_data/crash_reports/")) {
std::filesystem::create_directory("./dfint_data/crash_reports/");
constinit 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)) {
std::filesystem::create_directory(PATH_REPORTS);
}

std::ofstream file(filename);
return file;
}

std::string ErrCodeToString(DWORD code) {
std::string errcode;

switch (code) {
case EXCEPTION_ACCESS_VIOLATION:
errcode = "Error: EXCEPTION_ACCESS_VIOLATION";
break;
return "Error: EXCEPTION_ACCESS_VIOLATION";
case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
errcode = "Error: EXCEPTION_ARRAY_BOUNDS_EXCEEDED";
break;
return "Error: EXCEPTION_ARRAY_BOUNDS_EXCEEDED";
case EXCEPTION_BREAKPOINT:
errcode = "Error: EXCEPTION_BREAKPOINT";
break;
return "Error: EXCEPTION_BREAKPOINT";
case EXCEPTION_DATATYPE_MISALIGNMENT:
errcode = "Error: EXCEPTION_DATATYPE_MISALIGNMENT";
break;
return "Error: EXCEPTION_DATATYPE_MISALIGNMENT";
case EXCEPTION_FLT_DENORMAL_OPERAND:
errcode = "Error: EXCEPTION_FLT_DENORMAL_OPERAND";
break;
return "Error: EXCEPTION_FLT_DENORMAL_OPERAND";
case EXCEPTION_FLT_DIVIDE_BY_ZERO:
errcode = "Error: EXCEPTION_FLT_DIVIDE_BY_ZERO";
break;
return "Error: EXCEPTION_FLT_DIVIDE_BY_ZERO";
case EXCEPTION_FLT_INEXACT_RESULT:
errcode = "Error: EXCEPTION_FLT_INEXACT_RESULT";
break;
return "Error: EXCEPTION_FLT_INEXACT_RESULT";
case EXCEPTION_FLT_INVALID_OPERATION:
errcode = "Error: EXCEPTION_FLT_INVALID_OPERATION";
break;
return "Error: EXCEPTION_FLT_INVALID_OPERATION";
case EXCEPTION_FLT_OVERFLOW:
errcode = "Error: EXCEPTION_FLT_OVERFLOW";
break;
return "Error: EXCEPTION_FLT_OVERFLOW";
case EXCEPTION_FLT_STACK_CHECK:
errcode = "Error: EXCEPTION_FLT_STACK_CHECK";
break;
return "Error: EXCEPTION_FLT_STACK_CHECK";
case EXCEPTION_FLT_UNDERFLOW:
errcode = "Error: EXCEPTION_FLT_UNDERFLOW";
break;
return "Error: EXCEPTION_FLT_UNDERFLOW";
case EXCEPTION_ILLEGAL_INSTRUCTION:
errcode = "Error: EXCEPTION_ILLEGAL_INSTRUCTION";
break;
return "Error: EXCEPTION_ILLEGAL_INSTRUCTION";
case EXCEPTION_IN_PAGE_ERROR:
errcode = "Error: EXCEPTION_IN_PAGE_ERROR";
break;
return "Error: EXCEPTION_IN_PAGE_ERROR";
case EXCEPTION_INT_DIVIDE_BY_ZERO:
errcode = "Error: EXCEPTION_INT_DIVIDE_BY_ZERO";
break;
return "Error: EXCEPTION_INT_DIVIDE_BY_ZERO";
case EXCEPTION_INT_OVERFLOW:
errcode = "Error: EXCEPTION_INT_OVERFLOW";
break;
return "Error: EXCEPTION_INT_OVERFLOW";
case EXCEPTION_INVALID_DISPOSITION:
errcode = "Error: EXCEPTION_INVALID_DISPOSITION";
break;
return "Error: EXCEPTION_INVALID_DISPOSITION";
case EXCEPTION_NONCONTINUABLE_EXCEPTION:
errcode = "Error: EXCEPTION_NONCONTINUABLE_EXCEPTION";
break;
return "Error: EXCEPTION_NONCONTINUABLE_EXCEPTION";
case EXCEPTION_PRIV_INSTRUCTION:
errcode = "Error: EXCEPTION_PRIV_INSTRUCTION";
break;
return "Error: EXCEPTION_PRIV_INSTRUCTION";
case EXCEPTION_SINGLE_STEP:
errcode = "Error: EXCEPTION_SINGLE_STEP";
break;
return "Error: EXCEPTION_SINGLE_STEP";
case EXCEPTION_STACK_OVERFLOW:
errcode = "Error: EXCEPTION_STACK_OVERFLOW";
break;
return "Error: EXCEPTION_STACK_OVERFLOW";
default:
errcode = "Error: Unrecognized Exception";
break;
return "Error: Unrecognized Exception";
}

return errcode;
}

LONG WINAPI Handler(EXCEPTION_POINTERS* ExceptionInfo) {
std::string errcode = ErrCodeToString(ExceptionInfo->ExceptionRecord->ExceptionCode);
const std::string& errcode = ErrCodeToString(ExceptionInfo->ExceptionRecord->ExceptionCode);

if (EXCEPTION_STACK_OVERFLOW != ExceptionInfo->ExceptionRecord->ExceptionCode) {
auto cr_filename = Config::Setting::crash_report_dir + "cr_" + Utils::now() + ".txt";
Expand All @@ -97,11 +74,9 @@ namespace CrashReport {
cr_file << "--------------Stack-------------\n";
cr_file << std::stacktrace::current() << "\n";
cr_file << "--------------------------------\n";
cr_file.close();

std::string message("Oops, it's a crash!\n");
message += errcode + "\n";
message += "Crash log: " + cr_filename + "\n";

auto message = std::format("Oops, it's a crash!\n{}\nCrash log: {}\n", errcode, cr_filename);
MessageBoxA(nullptr, message.c_str(), "dfint hook error", MB_ICONERROR);
} else {
MessageBoxA(nullptr, "Stack Overflow!", "dfint hook error", MB_ICONERROR);
Expand Down
38 changes: 16 additions & 22 deletions src/hook/dictionary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,29 +45,25 @@ std::optional<std::string> Dictionary::Get(const std::string& key) {
if (key.empty()) {
return std::nullopt;
}
if (this->dict.find(key) == this->dict.end()) {
return std::nullopt;
}
auto value = this->dict.at(key);
if (value.empty()) {
return std::nullopt;
if (auto it = this->dict.find(key); it != this->dict.end()) {
if (auto value = it->second; !value.empty()) {
return value;
}
}
return value;
return std::nullopt;
}

std::optional<std::string> Dictionary::Get(const char* key) {
auto len = strnlen_s(key, 1000);
if (!key || len <= 0 || len >= 1000) {
return std::nullopt;
}
if (this->dict.find(key) == this->dict.end()) {
return std::nullopt;
if (auto it = this->dict.find(key); it != this->dict.end()) {
if (auto value = it->second; !value.empty()) {
return value;
}
}
auto value = this->dict.at(key);
if (value.empty()) {
return std::nullopt;
}
return value;
return std::nullopt;
}

std::optional<std::string> Dictionary::Get(const char* key, size_t size) {
Expand All @@ -80,18 +76,16 @@ std::optional<std::string> Dictionary::Get(const char* key, size_t size) {
strncpy(buf.get(), key, size);
buf[size] = '\0';
}
if (this->dict.find(buf ? buf.get() : key) == this->dict.end()) {
return std::nullopt;
}
auto value = this->dict.at(buf ? buf.get() : key);
if (value.empty()) {
return std::nullopt;
if (auto it = this->dict.find(buf ? buf.get() : key); it != this->dict.end()) {
if (auto value = it->second; !value.empty()) {
return value;
}
}
return value;
return std::nullopt;
}

bool Dictionary::Exist(std::string& key) {
return this->dict.find(key) != this->dict.end();
return this->dict.contains(key);
}

void Dictionary::Add(std::string& key, std::string& value) {
Expand Down
25 changes: 9 additions & 16 deletions src/hook/hooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -455,13 +455,9 @@ namespace Hooks {
}
// if INTERFACEKEY_SELECT || INTERFACEKEY_LEAVESCREEN
// lost mouse rbut here, cause it is in enabler instance
if (events.contains(1) || events.contains(2)) {
return false;
}
if (events.contains(1) || events.contains(2)) return false;
events.clear();
if (str.size() >= maxlen) {
return false;
}
if (str.size() >= maxlen) return false;

bool any_valid = false;
for (size_t i = 0; i < 32; i++) {
Expand All @@ -471,15 +467,13 @@ namespace Hooks {
} else {
entry = utf[i];
}
if (entry == 0) {
break;
}
if (entry == 0) break;

if (str.length() < maxlen && (entry == 10) || (!(flag & STRINGENTRY_FILENAME) || invalid_filename_chars.contains(entry) == false) ||
if (str.size() < maxlen && (entry == 10) || (!(flag & STRINGENTRY_FILENAME) || !invalid_filename_chars.contains(entry)) ||
(flag & STRINGENTRY_SYMBOLS) ||
((flag & STRINGENTRY_LETTERS) && (entry >= INTERFACEKEY_STRING_A097 && entry <= INTERFACEKEY_STRING_A122) ||
(entry >= INTERFACEKEY_STRING_A065 && entry <= INTERFACEKEY_STRING_A090) ||
(entry >= INTERFACEKEY_STRING_A192 && entry <= INTERFACEKEY_STRING_A255)) ||
((flag & STRINGENTRY_LETTERS) && ((entry >= INTERFACEKEY_STRING_A097 && entry <= INTERFACEKEY_STRING_A122) ||
(entry >= INTERFACEKEY_STRING_A065 && entry <= INTERFACEKEY_STRING_A090) ||
(entry >= INTERFACEKEY_STRING_A192 && entry <= INTERFACEKEY_STRING_A255))) ||
((flag & STRINGENTRY_SPACE) && entry == ' ') ||
((flag & STRINGENTRY_NUMBERS) && (entry >= INTERFACEKEY_STRING_A048 && entry <= INTERFACEKEY_STRING_A057))) {

Expand All @@ -489,11 +483,10 @@ namespace Hooks {

any_valid = true;
str.push_back(entry);
if (entry == 0 || entry == 10 || str.size() >= maxlen) {
break;
}
if (entry == 0 || entry == 10 || str.size() >= maxlen) break;
}
}

return any_valid;
}

Expand Down
4 changes: 2 additions & 2 deletions src/hook/keybindings.h
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ enum InterfaceKeyType {
INTERFACEKEYNUM,
};

static const std::map<int, int> cyrillic_utf8_to_cp1251{
static const std::unordered_map<int, int> cyrillic_utf8_to_cp1251{
{ 37072, 192 }, { 37328, 193 }, { 37584, 194 }, { 37840, 195 }, { 38096, 196 }, { 38352, 197 }, { 38608, 198 }, { 38864, 199 },
{ 39120, 200 }, { 39376, 201 }, { 39632, 202 }, { 39888, 203 }, { 40144, 204 }, { 40400, 205 }, { 40656, 206 }, { 40912, 207 },
{ 41168, 208 }, { 41424, 209 }, { 41680, 210 }, { 41936, 211 }, { 42192, 212 }, { 42448, 213 }, { 42704, 214 }, { 42960, 215 },
Expand All @@ -282,4 +282,4 @@ static const std::map<int, int> cyrillic_utf8_to_cp1251{
{ 37074, 165 }, { 37330, 180 }, { 36560, 161 }, { 40657, 162 }
};

static const std::set<char> invalid_filename_chars{ '<', '>', ':', '"', '/', '\\', '|', '?', '*' };
static const std::unordered_set<char> invalid_filename_chars{ '<', '>', ':', '"', '/', '\\', '|', '?', '*' };
19 changes: 10 additions & 9 deletions src/hook/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

extern "C" __declspec(dllexport) VOID NullExport(VOID) {}

void ProcessAttach() {
BOOL ProcessAttach() {
InitLogger();
if (Config::Setting::watchdog) {
Watchdog::WatchKeyboard();
Expand Down Expand Up @@ -40,12 +40,13 @@ void ProcessAttach() {
if (Config::Setting::enable_patches) {
Patches::Install();
}

DetourTransactionCommit();
logger::info("hooks installed");

return TRUE;
}

void ProcessDetach() {
BOOL ProcessDetach() {
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());

Expand All @@ -58,8 +59,9 @@ void ProcessDetach() {
// Hooks::UninstallTTFInjection();
// Hooks::UninstallStateManager();
logger::info("hooks uninstalled");

DetourTransactionCommit();

return TRUE;
}

BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) {
Expand All @@ -69,11 +71,10 @@ BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserv

switch (ul_reason_for_call) {
case DLL_PROCESS_ATTACH:
ProcessAttach();
break;
return ProcessAttach();
case DLL_PROCESS_DETACH:
ProcessDetach();
break;
return ProcessDetach();
default:
return TRUE;
}
return TRUE;
}
1 change: 1 addition & 0 deletions src/hook/pch.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <stdexcept>
#include <thread>
#include <unordered_map>
#include <unordered_set>

/* disable unused headers from Windows.h */
#define WIN32_LEAN_AND_MEAN
Expand Down
3 changes: 2 additions & 1 deletion src/pe_parser/main.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#include "pe_parser.hpp"
#include <filesystem>
#include <format>
#include <fstream>
#include <iostream>

#include "pe_parser.hpp"

/*
* first argument - path to Dwarf Fortress.exe
* second argument - file with strings to translate
Expand Down

0 comments on commit 4fc4e8f

Please sign in to comment.