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

Commit

Permalink
futher format rework
Browse files Browse the repository at this point in the history
  • Loading branch information
shevernitskiy committed Jul 12, 2023
1 parent 238f9e2 commit 5ced860
Show file tree
Hide file tree
Showing 19 changed files with 137 additions and 251 deletions.
11 changes: 3 additions & 8 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,13 @@ BasedOnStyle: Mozilla
IndentWidth: 2
AlignAfterOpenBracket: true
AlignOperands: true
AllowShortFunctionsOnASingleLine: InlineOnly
AllowShortFunctionsOnASingleLine: Empty
AllowShortEnumsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
BinPackArguments: true
BinPackParameters: true
BraceWrapping:
AfterClass: true
AfterEnum: true
AfterFunction: true
AfterStruct: true
AfterUnion: true
BreakBeforeBraces: Custom
BreakBeforeBraces: Attach
ColumnLimit: 140
IndentPPDirectives: AfterHash
SortUsingDeclarations: true
Expand Down
31 changes: 15 additions & 16 deletions src/hook/cache.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,21 @@
#define _LRUCACHE_HPP_INCLUDED_

template <typename KeyType, typename ValueType>
class LRUCache
{
class LRUCache {
public:
typedef typename std::pair<KeyType, ValueType> KeyValuePairType;
typedef typename std::list<KeyValuePairType>::iterator ListIteratorType;

LRUCache(size_t max_size)
: max_size(max_size)
{
}
: max_size(max_size) {}

~LRUCache()
{
~LRUCache() {
this->items_list.clear();
this->items_map.clear();
delete this;
};

void Put(const KeyType& key, ValueType& value)
{
void Put(const KeyType& key, ValueType& value) {
auto it = items_map.find(key);
items_list.push_front(KeyValuePairType(key, value));
if (it != items_map.end()) {
Expand All @@ -44,8 +39,7 @@ class LRUCache
}
}

std::optional<std::reference_wrapper<ValueType>> Get(const KeyType& key)
{
std::optional<std::reference_wrapper<ValueType>> Get(const KeyType& key) {
auto it = items_map.find(key);
if (it == items_map.end()) {
return std::nullopt;
Expand All @@ -55,14 +49,19 @@ class LRUCache
}
}

void SetEraseCallback(std::function<void(const KeyType& key, ValueType& value)> callback) { this->erase_callback = callback; }
void SetEraseCallback(std::function<void(const KeyType& key, ValueType& value)> callback) {
this->erase_callback = callback;
}

bool Exists(const KeyType& key) const { return items_map.find(key) != items_map.end(); }
bool Exists(const KeyType& key) const {
return items_map.find(key) != items_map.end();
}

size_t Size() const { return items_map.size(); }
size_t Size() const {
return items_map.size();
}

void Clear() noexcept
{
void Clear() noexcept {
this->items_map.clear();
this->items_list.clear();
}
Expand Down
9 changes: 3 additions & 6 deletions src/hook/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@

namespace Config {

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

if (!file.is_open()) {
Expand All @@ -20,8 +19,7 @@ namespace Config {
return nt_header->FileHeader.TimeDateStamp;
}

inline toml::v3::ex::parse_result GetOffsetsFile(time_t target_checksum)
{
inline toml::v3::ex::parse_result GetOffsetsFile(time_t target_checksum) {
for (const auto& filepath : std::filesystem::recursive_directory_iterator("./dfint_data/offsets/")) {
auto file = toml::parse_file(filepath.path().c_str());
if (target_checksum == file["metadata"]["checksum"].value_or<time_t>(0)) {
Expand All @@ -41,8 +39,7 @@ namespace Config {
inline auto pe_timestamp = Config::PETimestamp("Dwarf Fortress.exe");
inline auto offsets = Config::GetOffsetsFile(Config::pe_timestamp);

inline std::vector<uintptr_t> GetStringsOffsetArray()
{
inline std::vector<uintptr_t> GetStringsOffsetArray() {
auto strings_patches_nodes =
Config::offsets["offsets"]["string_patches"] ? Config::offsets["offsets"]["string_patches"].as_array() : nullptr;
std::vector<uintptr_t> strings_patches{};
Expand Down
12 changes: 4 additions & 8 deletions src/hook/crash_report.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

namespace CrashReport {

std::ofstream GetCrashReportLogHandle(std::string filename)
{
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/");
}
Expand All @@ -12,8 +11,7 @@ namespace CrashReport {
return file;
}

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

switch (code) {
Expand Down Expand Up @@ -85,8 +83,7 @@ namespace CrashReport {
return errcode;
}

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

if (EXCEPTION_STACK_OVERFLOW != ExceptionInfo->ExceptionRecord->ExceptionCode) {
Expand Down Expand Up @@ -114,8 +111,7 @@ namespace CrashReport {
return EXCEPTION_EXECUTE_HANDLER;
}

void Install()
{
void Install() {
::SetUnhandledExceptionFilter(Handler);
}

Expand Down
36 changes: 12 additions & 24 deletions src/hook/dictionary.cpp
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
#include "dictionary.h"

void Dictionary::ReplaceAll(std::string& subject, const std::string& search, const std::string& replace)
{
void Dictionary::ReplaceAll(std::string& subject, const std::string& search, const std::string& replace) {
size_t pos = 0;
while ((pos = subject.find(search, pos)) != std::string::npos) {
subject.replace(pos, search.length(), replace);
pos += replace.length();
}
}

std::string Dictionary::Sanitize(std::string& str)
{
std::string Dictionary::Sanitize(std::string& str) {
ReplaceAll(str, R"("")", "\"");
return str;
}

std::pair<std::string, std::string> Dictionary::Split(const std::string& str)
{
std::pair<std::string, std::string> Dictionary::Split(const std::string& str) {
std::string delimiter = "\",\"";
auto delimiter_pos = str.find(delimiter);
auto key = str.substr(1, delimiter_pos - 1);
Expand All @@ -25,8 +22,7 @@ std::pair<std::string, std::string> Dictionary::Split(const std::string& str)
}

// C code would be faster here, but we need to load just once
void Dictionary::LoadCsv(const std::string& filename)
{
void Dictionary::LoadCsv(const std::string& filename) {
logger::info("trying to load dictionary from csv file {}", filename);
std::ifstream file(filename);
if (!file.is_open()) {
Expand All @@ -45,8 +41,7 @@ void Dictionary::LoadCsv(const std::string& filename)
logger::info("csv dictionary loaded, total lines {}", this->Size());
}

std::optional<std::string> Dictionary::Get(const std::string& key)
{
std::optional<std::string> Dictionary::Get(const std::string& key) {
if (key.empty()) {
return std::nullopt;
}
Expand All @@ -60,8 +55,7 @@ std::optional<std::string> Dictionary::Get(const std::string& key)
return value;
}

std::optional<std::string> Dictionary::Get(const char* key)
{
std::optional<std::string> Dictionary::Get(const char* key) {
auto len = strnlen_s(key, 1000);
if (!key || len <= 0 || len >= 1000) {
return std::nullopt;
Expand All @@ -76,8 +70,7 @@ std::optional<std::string> Dictionary::Get(const char* key)
return value;
}

std::optional<std::string> Dictionary::Get(const char* key, size_t size)
{
std::optional<std::string> Dictionary::Get(const char* key, size_t size) {
if (!key) {
return std::nullopt;
}
Expand All @@ -97,27 +90,22 @@ std::optional<std::string> Dictionary::Get(const char* key, size_t size)
return value;
}

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

void Dictionary::Add(std::string& key, std::string& value)
{
void Dictionary::Add(std::string& key, std::string& value) {
this->dict.emplace(std::make_pair(key, value));
}

void Dictionary::Add(std::pair<std::string, std::string>& pair)
{
void Dictionary::Add(std::pair<std::string, std::string>& pair) {
this->dict.emplace(pair);
}

size_t Dictionary::Size()
{
size_t Dictionary::Size() {
return this->dict.size();
}

void Dictionary::Clear()
{
void Dictionary::Clear() {
this->dict.clear();
}
6 changes: 2 additions & 4 deletions src/hook/dictionary.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
#pragma once

class Dictionary
{
class Dictionary {
public:
[[nodiscard]] static Dictionary* GetSingleton()
{
[[nodiscard]] static Dictionary* GetSingleton() {
static Dictionary singleton;
return &singleton;
}
Expand Down
Loading

0 comments on commit 5ced860

Please sign in to comment.