-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathUtils.h
137 lines (108 loc) · 4.13 KB
/
Utils.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#pragma once
#include <Windows.h>
#include <fstream>
#include <algorithm>
#include <vector>
#include "D2Structs.h"
#include "D2Tables.h"
#include <filesystem>
#include <cwctype>
#include <unordered_map>
#ifdef _DEBUG
#define DEBUG_LOG(f) std::wprintf(f.c_str()); //PrintGameString(f, TextColor::YELLOW);
#else
#define DEBUG_LOG(f) ;
#endif
#define INFO_LOG(f) PrintGameString(f, TextColor::YELLOW);
#define WARN_LOG(f) PrintGameString(f, TextColor::ORANGE);
#define ERROR_LOG(f) PrintGameString(f, TextColor::RED);
//Hooking
uint32_t __fastcall GetDllOffset(uint32_t baseAddress, int offset);
D2Version GetGameVersion();
void PrintGameString(const std::wstring& wStr, TextColor color);
Unit* FindUnitFromTable(uint32_t unitId, UnitType type);
Unit* FindUnit(uint32_t unitId, UnitType type);
//String funcs
std::wstring_view ltrim(std::wstring_view s);
std::wstring_view rtrim(std::wstring_view s);
std::wstring_view trim(std::wstring_view s);
std::wstring ltrim_copy(std::wstring_view s);
std::wstring rtrim_copy(std::wstring_view s);
std::wstring trim_copy(std::wstring_view s);
void replace(std::wstring& subject, std::wstring_view search, std::wstring_view replace);
std::vector<std::wstring> split(std::wstring_view stringToSplit, std::wstring_view regexPattern);
//Utility D2 Methods
ItemsTxt* GetItemsTxt(Unit* pUnit);
std::wstring GetItemCode(Unit* pUnit);
int32_t GetQualityLevel(Unit* pItem);
// Heterogeneous lookup for std::unordered_map
namespace utility {
namespace detail {
template<typename T>
struct char_type_helper {
using type = std::remove_cv_t<std::remove_pointer_t<T>>;
};
template<typename CharT>
struct char_type_helper<std::basic_string<CharT>> {
using type = CharT;
};
template<typename CharT>
struct char_type_helper<std::basic_string_view<CharT>> {
using type = CharT;
};
template<typename T>
using char_type_helper_t = char_type_helper<T>::type;
template<typename T>
struct string_equal {
using is_transparent = void;
bool operator()(std::basic_string_view<char_type_helper_t<T>> lhs,
std::basic_string_view<char_type_helper_t<T>> rhs) const noexcept {
return lhs == rhs;
}
};
template<typename T>
struct string_hash {
using is_transparent = void;
auto operator()(std::basic_string_view<char_type_helper_t<T>> str) const noexcept {
return std::hash<std::basic_string_view<char_type_helper_t<T>>>{}(str);
}
};
template<typename T = void>
struct string_iequal {
using is_transparent = void;
auto operator()(std::wstring_view lhs, std::wstring_view rhs) const noexcept {
return std::ranges::equal(lhs, rhs, [](wchar_t a, wchar_t b) {
return std::towlower(a) == std::towlower(b);
});
}
};
constexpr size_t FNV_Offset_Basis = 2166136261u;
constexpr size_t FNV_Prime = 16777619u;
template<typename T = void>
struct string_ihash {
using is_transparent = void;
size_t operator()(std::wstring_view str) const noexcept {
static_assert(sizeof(decltype(str)::traits_type::char_type) == 2);
size_t hash{ FNV_Offset_Basis };
for (auto ch : str) {
ch = std::towlower(ch);
hash ^= static_cast<size_t>(ch & 0xFF);
hash *= FNV_Prime;
hash ^= static_cast<size_t>(ch >> 8);
hash *= FNV_Prime;
}
return hash;
}
};
} // namespace detail
template<typename Key, typename T, typename Hash, typename KeyEqual, typename Allocator>
struct unordered_string_map_helper;
template<typename CharT, typename T, typename Hash, typename KeyEqual, typename Allocator>
struct unordered_string_map_helper<std::basic_string<CharT>, T, Hash, KeyEqual, Allocator> {
using type = std::unordered_map<std::basic_string<CharT>, T, Hash, KeyEqual, Allocator>;
};
template<typename Key, typename T, typename Allocator = std::allocator<std::pair<const Key, T>>>
using string_umap = unordered_string_map_helper<Key, T, detail::string_hash<Key>, detail::string_equal<Key>, Allocator>::type;
template<typename Key, typename T, typename Allocator = std::allocator<std::pair<const Key, T>>>
using string_umap_icase = unordered_string_map_helper<Key, T, detail::string_ihash<Key>, detail::string_iequal<Key>, Allocator>::type;
} // namespace utility