forked from danielkrupinski/Osiris
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHelpers.cpp
194 lines (164 loc) · 6.19 KB
/
Helpers.cpp
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include <array>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <cwctype>
#include <fstream>
#include <random>
#include <span>
#include <string_view>
#include <unordered_map>
#include "Platform/IsPlatform.h"
#if IS_WIN32()
#include <Windows.h>
#endif
#include "imgui/imgui.h"
#include "ConfigStructs.h"
#include "GameData.h"
#include "Helpers.h"
#include "Memory.h"
#include "SDK/GlobalVars.h"
#include "SDK/Engine.h"
static auto rainbowColor(float time, float speed, float alpha) noexcept
{
constexpr float pi = std::numbers::pi_v<float>;
return std::array{ std::sin(speed * time) * 0.5f + 0.5f,
std::sin(speed * time + 2 * pi / 3) * 0.5f + 0.5f,
std::sin(speed * time + 4 * pi / 3) * 0.5f + 0.5f,
alpha };
}
static float alphaFactor = 1.0f;
unsigned int Helpers::calculateColor(float time, Color4 color) noexcept
{
color.color[3] *= alphaFactor;
// if (!config->ignoreFlashbang)
color.color[3] *= (255.0f - GameData::local().flashDuration) / 255.0f;
return ImGui::ColorConvertFloat4ToU32(color.rainbow ? rainbowColor(time, color.rainbowSpeed, color.color[3]) : color.color);
}
unsigned int Helpers::calculateColor(float time, Color3 color) noexcept
{
return ImGui::ColorConvertFloat4ToU32(color.rainbow ? rainbowColor(time, color.rainbowSpeed, 1.0f) : ImVec4{ color.color[0], color.color[1], color.color[2], 1.0f});
}
unsigned int Helpers::calculateColor(int r, int g, int b, int a) noexcept
{
a -= static_cast<int>(a * GameData::local().flashDuration / 255.0f);
return IM_COL32(r, g, b, a * alphaFactor);
}
void Helpers::setAlphaFactor(float newAlphaFactor) noexcept
{
alphaFactor = newAlphaFactor;
}
float Helpers::getAlphaFactor() noexcept
{
return alphaFactor;
}
void Helpers::convertHSVtoRGB(float h, float s, float v, float& outR, float& outG, float& outB) noexcept
{
ImGui::ColorConvertHSVtoRGB(h, s, v, outR, outG, outB);
}
void Helpers::healthColor(float fraction, float& outR, float& outG, float& outB) noexcept
{
constexpr auto greenHue = 1.0f / 3.0f;
constexpr auto redHue = 0.0f;
convertHSVtoRGB(std::lerp(redHue, greenHue, fraction), 1.0f, 1.0f, outR, outG, outB);
}
unsigned int Helpers::healthColor(float fraction) noexcept
{
float r, g, b;
healthColor(fraction, r, g, b);
return calculateColor(static_cast<int>(r * 255.0f), static_cast<int>(g * 255.0f), static_cast<int>(b * 255.0f), 255);
}
ImWchar* Helpers::getFontGlyphRanges() noexcept
{
static ImVector<ImWchar> ranges;
if (ranges.empty()) {
ImFontGlyphRangesBuilder builder;
constexpr ImWchar baseRanges[]{
0x0100, 0x024F, // Latin Extended-A + Latin Extended-B
0x0300, 0x03FF, // Combining Diacritical Marks + Greek/Coptic
0x0600, 0x06FF, // Arabic
0x0E00, 0x0E7F, // Thai
0
};
builder.AddRanges(baseRanges);
builder.AddRanges(ImGui::GetIO().Fonts->GetGlyphRangesCyrillic());
builder.AddRanges(ImGui::GetIO().Fonts->GetGlyphRangesChineseSimplifiedCommon());
builder.AddText("\u9F8D\u738B\u2122");
builder.BuildRanges(&ranges);
}
return ranges.Data;
}
std::wstring Helpers::toWideString(const std::string& str) noexcept
{
std::wstring wide(str.length(), L'\0');
if (const auto newLen = std::mbstowcs(wide.data(), str.c_str(), wide.length()); newLen != static_cast<std::size_t>(-1))
wide.resize(newLen);
return wide;
}
bool Helpers::decodeVFONT(std::vector<char>& buffer) noexcept
{
constexpr std::string_view tag = "VFONT1";
unsigned char magic = 0xA7;
if (buffer.size() <= tag.length())
return false;
const auto tagIndex = buffer.size() - tag.length();
if (std::memcmp(tag.data(), &buffer[tagIndex], tag.length()))
return false;
unsigned char saltBytes = buffer[tagIndex - 1];
const auto saltIndex = tagIndex - saltBytes;
--saltBytes;
for (std::size_t i = 0; i < saltBytes; ++i)
magic ^= (buffer[saltIndex + i] + 0xA7) % 0x100;
for (std::size_t i = 0; i < saltIndex; ++i) {
unsigned char xored = buffer[i] ^ magic;
magic = (buffer[i] + 0xA7) % 0x100;
buffer[i] = xored;
}
buffer.resize(saltIndex);
return true;
}
std::vector<char> Helpers::loadBinaryFile(const std::string& path) noexcept
{
std::vector<char> result;
std::ifstream in{ path, std::ios::binary };
if (!in)
return result;
in.seekg(0, std::ios_base::end);
result.resize(static_cast<std::size_t>(in.tellg()));
in.seekg(0, std::ios_base::beg);
in.read(result.data(), result.size());
return result;
}
std::size_t Helpers::calculateVmtLength(const std::uintptr_t* vmt) noexcept
{
std::size_t length = 0;
#if IS_WIN32()
MEMORY_BASIC_INFORMATION memoryInfo;
while (VirtualQuery(LPCVOID(vmt[length]), &memoryInfo, sizeof(memoryInfo)) && memoryInfo.Protect & (PAGE_EXECUTE | PAGE_EXECUTE_READ | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_WRITECOPY))
++length;
#else
while (vmt[length])
++length;
#endif
return length;
}
static bool transformWorldPositionToScreenPosition(const Matrix4x4& matrix, const Vector& worldPosition, ImVec2& screenPosition) noexcept
{
const auto w = matrix._41 * worldPosition.x + matrix._42 * worldPosition.y + matrix._43 * worldPosition.z + matrix._44;
if (w < 0.001f)
return false;
screenPosition = ImGui::GetIO().DisplaySize / 2.0f;
screenPosition.x *= 1.0f + (matrix._11 * worldPosition.x + matrix._12 * worldPosition.y + matrix._13 * worldPosition.z + matrix._14) / w;
screenPosition.y *= 1.0f - (matrix._21 * worldPosition.x + matrix._22 * worldPosition.y + matrix._23 * worldPosition.z + matrix._24) / w;
return true;
}
bool Helpers::worldToScreen(const Vector& worldPosition, ImVec2& screenPosition) noexcept
{
return transformWorldPositionToScreenPosition(GameData::toScreenMatrix(), worldPosition, screenPosition);
}
bool Helpers::worldToScreenPixelAligned(const Vector& worldPosition, ImVec2& screenPosition) noexcept
{
const bool onScreen = transformWorldPositionToScreenPosition(GameData::toScreenMatrix(), worldPosition, screenPosition);
screenPosition = ImFloor(screenPosition);
return onScreen;
}