Skip to content

Commit

Permalink
Build 4.1
Browse files Browse the repository at this point in the history
  • Loading branch information
CyberMor committed Nov 4, 2023
1 parent c9a649f commit d29e24a
Show file tree
Hide file tree
Showing 23 changed files with 210 additions and 117 deletions.
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Makefile text
*.cpp text
*.hlsl text
*.def text
*.cfg text
*.cfg text eol=lf
*.inc text
*.iss text
*.md text
Expand Down
4 changes: 2 additions & 2 deletions client/client.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalLibraryDirectories>files/libraries</AdditionalLibraryDirectories>
<MinimumRequiredVersion />
<Version>4.0</Version>
<Version>4.1</Version>
<ImportLibrary />
<ProgramDatabaseFile />
</Link>
Expand Down Expand Up @@ -114,7 +114,7 @@
<GenerateDebugInformation>false</GenerateDebugInformation>
<MinimumRequiredVersion />
<SetChecksum>true</SetChecksum>
<Version>4.0</Version>
<Version>4.1</Version>
<ProgramDatabaseFile />
<ImportLibrary />
<ImageHasSafeExceptionHandlers />
Expand Down
2 changes: 1 addition & 1 deletion client/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -971,7 +971,7 @@ static DWORD WINAPI LibraryWaitingThread(const LPVOID) noexcept
HMODULE samp_module;

while ((samp_module = GetModuleHandle(gLibraryName)) == nullptr)
utils::thread::sleep(100); // 100 milliseconds
utils::thread::sleep(100);

OnSampLoad(samp_module);

Expand Down
2 changes: 1 addition & 1 deletion client/main.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include <system/types.hpp>
#include <other/version.hpp>

constexpr Version kCurrentVersion = MakeVersion(4, 0, 0);
constexpr Version kCurrentVersion = MakeVersion(4, 1, 0);

constexpr size_t kMaxPlayers = 1024;
constexpr size_t kMaxStreams = 4096;
Expand Down
2 changes: 2 additions & 0 deletions client/voice.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ struct VoiceService {

bool Open() noexcept
{
_socket.Deinitialize();

IPv4UdpSocket socket;

if (!socket.Initialize(true)) return false;
Expand Down
37 changes: 23 additions & 14 deletions common/memory/hacks/call_hook.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,16 @@ struct CallHook {
public:

CallHook(const ptr_t inject, const cptr_t hook, const bool enabled = true) noexcept
: _target { reinterpret_cast<ptr_t>((reinterpret_cast<sptrint_t>(inject) +
static_cast<sptrint_t>(sizeof(CallInstruction))) + static_cast<const CallInstruction*>(inject)->Offset()) }
, _patch { inject, &CallInstruction(reinterpret_cast<sptrint_t>(hook) -
(reinterpret_cast<sptrint_t>(inject) + static_cast<sptrint_t>(sizeof(CallInstruction)))), enabled }
{
assert(hook != nullptr);
assert(inject != nullptr);
assert(hook != nullptr);

_target = reinterpret_cast<ptr_t>((reinterpret_cast<sptrint_t>(inject) +
static_cast<sptrint_t>(sizeof(CallInstruction))) + static_cast<const CallInstruction*>(inject)->Offset());

if (_target != nullptr && !_patch.Initialize(inject, &CallInstruction(reinterpret_cast<sptrint_t>(hook) -
(reinterpret_cast<sptrint_t>(inject) + static_cast<sptrint_t>(sizeof(CallInstruction)))), enabled))
_target = nullptr;
}

public:
Expand All @@ -86,12 +90,17 @@ struct CallHook {

bool Initialize(const ptr_t inject, const cptr_t hook, const bool enabled = true) noexcept
{
assert(hook != nullptr);
assert(inject != nullptr);
assert(hook != nullptr);

_target = reinterpret_cast<ptr_t>((reinterpret_cast<sptrint_t>(inject) +
static_cast<sptrint_t>(sizeof(CallInstruction))) + static_cast<const CallInstruction*>(inject)->Offset());

return (_target = reinterpret_cast<ptr_t>((reinterpret_cast<sptrint_t>(inject) +
static_cast<sptrint_t>(sizeof(CallInstruction))) + static_cast<const CallInstruction*>(inject)->Offset())) != nullptr
&& _patch.Initialize(inject, &CallInstruction(reinterpret_cast<sptrint_t>(hook) -
(reinterpret_cast<sptrint_t>(inject) + static_cast<sptrint_t>(sizeof(CallInstruction)))), enabled);
if (_target != nullptr && !_patch.Initialize(inject, &CallInstruction(reinterpret_cast<sptrint_t>(hook) -
(reinterpret_cast<sptrint_t>(inject) + static_cast<sptrint_t>(sizeof(CallInstruction)))), enabled))
_target = nullptr;

return _target != nullptr && _patch.Valid();
}

void Deinitialize() noexcept
Expand All @@ -103,14 +112,14 @@ struct CallHook {

public:

void Enable() noexcept
bool Enable() noexcept
{
_patch.Enable();
return _patch.Enable();
}

void Disable() noexcept
bool Disable() noexcept
{
_patch.Disable();
return _patch.Disable();
}

public:
Expand Down
8 changes: 4 additions & 4 deletions common/memory/hacks/jump_hook.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,14 @@ struct JumpHook {

public:

void Enable() noexcept
bool Enable() noexcept
{
_patch.Enable();
return _patch.Enable();
}

void Disable() noexcept
bool Disable() noexcept
{
_patch.Disable();
return _patch.Disable();
}

public:
Expand Down
41 changes: 26 additions & 15 deletions common/memory/hacks/patch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ struct Patch {
std::memcpy(_patch_bytes, patch, Size);
std::memcpy(_orig_bytes, address, Size);

if (enabled) Enable();
if (_upscope.Valid() && enabled == true && Enable() == false)
_upscope.Deinitialize();
}

public:
Expand All @@ -95,8 +96,10 @@ struct Patch {
std::memcpy(_patch_bytes, patch, Size);
std::memcpy(_orig_bytes, address, Size);

return _upscope.Initialize(address, false) &&
(enabled == false || (Enable(), _enabled == true));
if (_upscope.Initialize(address, false) && enabled == true && Enable() == false)
_upscope.Deinitialize();

return _upscope.Valid();
}

void Deinitialize() noexcept
Expand All @@ -108,28 +111,36 @@ struct Patch {

public:

void Enable() noexcept
bool Enable() noexcept
{
if (_enabled == false)
{
_upscope.Enable();
std::memcpy(_upscope.Address(), _patch_bytes, Size);
_upscope.Disable();
if (_upscope.Enable())
{
std::memcpy(_upscope.Address(), _patch_bytes, Size);
_upscope.Disable();

_enabled = true;
_enabled = true;
}
}

return _enabled == true;
}

void Disable() noexcept
bool Disable() noexcept
{
if (_enabled == true)
{
_upscope.Enable();
std::memcpy(_upscope.Address(), _orig_bytes, Size);
_upscope.Disable();
if (_upscope.Enable())
{
std::memcpy(_upscope.Address(), _orig_bytes, Size);
_upscope.Disable();

_enabled = false;
_enabled = false;
}
}

return _enabled == false;
}

public:
Expand All @@ -148,8 +159,8 @@ struct Patch {

bool _enabled = false;

ubyte_t _patch_bytes[Size];
ubyte_t _orig_bytes[Size];
alignas (alignof(std::max_align_t)) ubyte_t _patch_bytes[Size];
alignas (alignof(std::max_align_t)) ubyte_t _orig_bytes[Size];

UnprotectScope<Size> _upscope;

Expand Down
25 changes: 18 additions & 7 deletions common/memory/hacks/unprotect_scope.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ struct UnprotectScope {
UnprotectScope(const ptr_t address, const bool enabled = true) noexcept
: _address { address }
{
if (enabled) Enable();
if (_address != nullptr && enabled == true && Enable() == false)
_address = nullptr;
}

public:
Expand All @@ -93,8 +94,10 @@ struct UnprotectScope {
_protect = PAGE_EXECUTE_READWRITE;
#endif

return _address != nullptr &&
(enabled == false || (Enable(), _enabled == true));
if (_address != nullptr && enabled == true && Enable() == false)
_address = nullptr;

return _address != nullptr;
}

void Deinitialize() noexcept
Expand All @@ -106,7 +109,7 @@ struct UnprotectScope {

public:

void Enable() noexcept
bool Enable() noexcept
{
if (_enabled == false)
{
Expand All @@ -117,7 +120,7 @@ struct UnprotectScope {
if (VirtualProtect(_address, Size, _protect, &_protect) != 0)
_enabled = true;
#else
if (const long pagesize = sysconf(_SC_PAGESIZE); pagesize != -1)
if (const long pagesize = sysconf(_SC_PAGESIZE); pagesize > 0)
{
const adr_t begin = reinterpret_cast<adr_t>
(reinterpret_cast<uptrint_t>(_address) & -static_cast<uptrint_t>(pagesize));
Expand All @@ -128,9 +131,11 @@ struct UnprotectScope {
}
#endif
}

return _enabled == true;
}

void Disable() noexcept
bool Disable() noexcept
{
if (_enabled == true)
{
Expand All @@ -140,6 +145,12 @@ struct UnprotectScope {
_enabled = false;
#endif
}

#ifdef _WIN32
return _enabled == false;
#else
return true;
#endif
}

public:
Expand Down Expand Up @@ -170,7 +181,7 @@ inline bool UnprotectMemory(const ptr_t address, const size_t length) noexcept
if (DWORD protect; VirtualProtect(address, length, PAGE_EXECUTE_READWRITE, &protect) != 0)
return true;
#else
if (const long pagesize = sysconf(_SC_PAGESIZE); pagesize != -1)
if (const long pagesize = sysconf(_SC_PAGESIZE); pagesize > 0)
{
const adr_t begin = reinterpret_cast<adr_t>
(reinterpret_cast<uptrint_t>(address) & -static_cast<uptrint_t>(pagesize));
Expand Down
22 changes: 13 additions & 9 deletions common/network/socket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ struct Socket {
return GetOption(level, option, static_cast<ptr_t>(buffer), length * sizeof(DataType)) / sizeof(DataType);
}

template <class Buffer, typename = std::enable_if_t<!std::is_const_v<std::remove_all_extents_t<Buffer>>>>
template <class Buffer, typename = std::enable_if_t<!std::is_const_v<Buffer>>>
socklen_t GetOption(const int level, const int option, Buffer& buffer) const noexcept
{
return GetOption(level, option, reinterpret_cast<std::remove_all_extents_t<Buffer>*>(&buffer),
Expand Down Expand Up @@ -369,7 +369,7 @@ struct Socket {
: static_cast<decltype(result)>(SOCKET_ERROR);
}

template <class Buffer, typename = std::enable_if_t<!std::is_const_v<std::remove_all_extents_t<Buffer>>>>
template <class Buffer, typename = std::enable_if_t<!std::is_const_v<Buffer>>>
auto PeekValue(Buffer& buffer, const int flags = 0) const noexcept
{
return Peek(reinterpret_cast<std::remove_all_extents_t<Buffer>*>(&buffer),
Expand All @@ -392,7 +392,7 @@ struct Socket {
: static_cast<decltype(result)>(SOCKET_ERROR);
}

template <class Buffer, typename = std::enable_if_t<!std::is_const_v<std::remove_all_extents_t<Buffer>>>>
template <class Buffer, typename = std::enable_if_t<!std::is_const_v<Buffer>>>
auto ReceiveValue(Buffer& buffer, const int flags = 0) noexcept
{
return Receive(reinterpret_cast<std::remove_all_extents_t<Buffer>*>(&buffer),
Expand Down Expand Up @@ -424,7 +424,8 @@ struct Socket {

public:

template <bool Endian = NetEndian, class... Buffers>
template <bool Endian = NetEndian, class... Buffers,
typename = std::enable_if_t<(... && !std::is_const_v<Buffers>)>>
int PeekPacket(Buffers&... buffers) const noexcept
{
DataPacket<Buffers...> packet;
Expand All @@ -435,7 +436,8 @@ struct Socket {
return 1;
}

template <bool Endian = NetEndian, class... Buffers>
template <bool Endian = NetEndian, class... Buffers,
typename = std::enable_if_t<(... && !std::is_const_v<Buffers>)>>
int ReceivePacket(Buffers&... buffers) noexcept
{
DataPacket<Buffers...> packet;
Expand Down Expand Up @@ -479,7 +481,7 @@ struct Socket {
: static_cast<decltype(result)>(SOCKET_ERROR);
}

template <class Buffer, typename = std::enable_if_t<!std::is_const_v<std::remove_all_extents_t<Buffer>>>>
template <class Buffer, typename = std::enable_if_t<!std::is_const_v<Buffer>>>
auto PeekValueFrom(Buffer& buffer, Address<Domain>& address, const int flags = 0) const noexcept
{
return PeekFrom(reinterpret_cast<std::remove_all_extents_t<Buffer>*>(&buffer),
Expand Down Expand Up @@ -508,7 +510,7 @@ struct Socket {
: static_cast<decltype(result)>(SOCKET_ERROR);
}

template <class Buffer, typename = std::enable_if_t<!std::is_const_v<std::remove_all_extents_t<Buffer>>>>
template <class Buffer, typename = std::enable_if_t<!std::is_const_v<Buffer>>>
auto ReceiveValueFrom(Buffer& buffer, Address<Domain>& address, const int flags = 0) noexcept
{
return ReceiveFrom(reinterpret_cast<std::remove_all_extents_t<Buffer>*>(&buffer),
Expand Down Expand Up @@ -543,7 +545,8 @@ struct Socket {

public:

template <bool Endian = NetEndian, class... Buffers>
template <bool Endian = NetEndian, class... Buffers,
typename = std::enable_if_t<(... && !std::is_const_v<Buffers>)>>
int PeekPacketFrom(Address<Domain>& address, Buffers&... buffers) const noexcept
{
DataPacket<Buffers...> packet;
Expand All @@ -554,7 +557,8 @@ struct Socket {
return 1;
}

template <bool Endian = NetEndian, class... Buffers>
template <bool Endian = NetEndian, class... Buffers,
typename = std::enable_if_t<(... && !std::is_const_v<Buffers>)>>
int ReceivePacketFrom(Address<Domain>& address, Buffers&... buffers) noexcept
{
DataPacket<Buffers...> packet;
Expand Down
Loading

0 comments on commit d29e24a

Please sign in to comment.