Skip to content

Commit

Permalink
General: Fix compatibility with b2802
Browse files Browse the repository at this point in the history
  • Loading branch information
Parik27 committed Dec 22, 2022
1 parent a4ff668 commit 5eae14d
Show file tree
Hide file tree
Showing 27 changed files with 340 additions and 147 deletions.
4 changes: 0 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ file(GLOB_RECURSE SOURCES CONFIGURE_DEPENDS
lib/Patterns/*.cpp)

add_library(${PROJECT_NAME} SHARED ${SOURCES} )
add_library(Mirrored SHARED lib/Patterns/Patterns.cpp lib/ParserUtils.cc src/misc/mirrored.cc)

if (ENABLE_DEBUG_MENU)
include(cmake/DebugImgui.cmake)
Expand All @@ -42,7 +41,4 @@ endif()
set_target_properties(${PROJECT_NAME} PROPERTIES SUFFIX ".asi")
target_link_libraries(${PROJECT_NAME} PUBLIC dbghelp minhook)

target_link_libraries(Mirrored PUBLIC minhook)
target_compile_definitions(Mirrored PUBLIC NOMINMAX)

target_compile_definitions(${PROJECT_NAME} PUBLIC NOMINMAX)
2 changes: 1 addition & 1 deletion lib/CCredits.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ void
CCreditArray::InitialisePatterns ()
{
sm_Instance = GetRelativeReference<CCreditArray> (
"? 8b 05 ? ? ? ? 83 3c 03 0e e9 ? ? ? ? 66", 3, 7);
"? 89 ? ? ? ? ? 89 ? ? ? ? ? 74 ? ? 8d ? ? ? ? ? e8 ? ? ? ? 8b ? ? ? ? ? 83", 3, 7);
}
23 changes: 8 additions & 15 deletions lib/CCredits.hh
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "rage.hh"
#include "ParserUtils.hh"
#include <cstdint>

enum class eCreditLineType : uint32_t
Expand All @@ -22,26 +23,18 @@ enum class eCreditLineType : uint32_t
JOB_AND_NAME_MED
};

struct CCreditItem
struct CCreditItem : public ParserWrapper<CCreditItem>
{
eCreditLineType LineType;
uint8_t _pad0x8[4];
atString cTextId1;
uint8_t _pad0x14[4];
atString cTextId2;
uint8_t _pad0x24[4];

CCreditItem () = default;

CCreditItem (eCreditLineType lineType, atString textId1 = "",
atString textId2 = "")
: LineType (lineType), cTextId1 (textId1), cTextId2 (textId2){};
};

class CCreditArray
class CCreditArray : public ParserWrapper<CCreditArray>
{
public:
atArray<CCreditItem> CreditItems;
auto&
GetCreditItems ()
{
return Get<atArrayGetSizeWrapper<CCreditItem>> ("CreditItems"_joaat);
}

inline static CCreditArray *sm_Instance = nullptr;

Expand Down
20 changes: 16 additions & 4 deletions lib/CItemInfo.hh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include <cstdint>
#include "ParserUtils.hh"

#include <common/logger.hh>

class CItemInfo
{
public:
Expand All @@ -12,14 +14,12 @@ public:
uint32_t Audio;
uint32_t Slot;

private:
virtual void Destructor ();
virtual bool GetIsClassId (uint32_t hash);

// virtual uint32_t* GetClassId (); (older versions)
virtual uint32_t &GetClassId (uint32_t &out);

// Do not use the following virtual functions for compatibility reasons.
//*******************************************************
virtual uint32_t *_GetClassId (uint32_t *out);

// Not present in older versions of GTA V
virtual uint32_t *_GetBaseClassId (uint32_t &out);
Expand All @@ -29,7 +29,19 @@ public:

virtual parStructure *_parser_GetStructure ();

public:
//*******************************************************
uint32_t
GetClassId ()
{
uint32_t tmp;

// Later versions return hash directly
if (Rainbomizer::Logger::GetGameBuild () >= 2802)
return static_cast<uint32_t> (uintptr_t (_GetClassId (nullptr)));
else
return *_GetClassId(&tmp);
}
};

class CAmmoInfo : public ParserWrapper<CAmmoInfo>, public CItemInfo
Expand Down
18 changes: 18 additions & 0 deletions lib/ParserUtils.hh
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,18 @@ protected:
public:
using Type = Class;

ParserWrapper () = default;
ParserWrapper (const ParserWrapper &other)
{
memcpy (this, &other, GetSize ());
}

void
operator= (const ParserWrapper &other)
{
memcpy (this, &other, GetSize ());
}

inline static constexpr uint32_t
GetHash ()
{
Expand Down Expand Up @@ -286,4 +298,10 @@ public:
{
return GetStaticData ()->Structure->nSize;
}

void
CopyTo (uint8_t *out)
{
memcpy (out, this, GetSize ());
}
};
35 changes: 20 additions & 15 deletions lib/Patterns/Patterns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,32 +143,37 @@ void pattern::Initialize(std::string_view pattern)
m_hash = fnv_1()(pattern);
#endif

// transform the base pattern from IDA format to canonical format
TransformPattern(pattern, m_bytes, m_mask);
static FILE* file = fopen("patterns.txt", "w");
fprintf (file, "%s\n", pattern.data ());
fflush (file);

// transform the base pattern from IDA format to canonical format
TransformPattern (pattern, m_bytes, m_mask);

#if PATTERNS_USE_HINTS
// if there's hints, try those first
// if there's hints, try those first
#if PATTERNS_CAN_SERIALIZE_HINTS
if (m_rangeStart == reinterpret_cast<uintptr_t>(GetModuleHandle(nullptr)))
if (m_rangeStart
== reinterpret_cast<uintptr_t> (GetModuleHandle (nullptr)))
#endif
{
auto range = getHints().equal_range(m_hash);
{
auto range = getHints ().equal_range (m_hash);

if (range.first != range.second)
{
std::for_each(range.first, range.second, [&] (const auto& hint)
{
ConsiderHint(hint.second);
});
if (range.first != range.second)
{
std::for_each (range.first, range.second,
[&] (const auto &hint) {
ConsiderHint (hint.second);
});

// if the hints succeeded, we don't need to do anything more
// if the hints succeeded, we don't need to do anything more
if (!m_matches.empty())
{
m_matched = true;
return;
}
}
}
}
}
#endif
}

Expand Down
11 changes: 11 additions & 0 deletions lib/Patterns/Patterns.hh
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,17 @@ namespace hook
}

public:

auto& getMask ()
{
return m_mask;
}

auto& getBytes ()
{
return m_bytes;
}

pattern(std::string_view pattern)
: hook::pattern(getRVA(0))
{
Expand Down
22 changes: 22 additions & 0 deletions lib/Utils.hh
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,22 @@ RegisterHook (const std::string &pattern, int offset, F hookedFunc)
RegisterHook<Jmp> (addr, hookedFunc);
}

/*******************************************************/
template<typename F, typename O>
void
RegisterHookOperand (const std::string &pattern, int offset, F hookedFunc, O& originalFunc)
{
hook::pattern p (pattern);
auto result = p.get_one ();

injector::WriteMemory (originalFunc, result.get<char> (offset + 4)
+ *result.get<int32_t> (offset));

*result.get<int32_t> (offset)
= Trampoline::MakeTrampoline (GetModuleHandle (nullptr))
->Jump (hookedFunc) - result.get<char>(offset + 4);
}

/*******************************************************/
void RegisterJmpHook (void *addr, void *dst, void **outOrignal, int size);

Expand Down Expand Up @@ -254,6 +270,12 @@ GetRelativeReference (const std::string &pattern, int dataOffset)
RegisterHook (pattern, offset, F, function<F>); \
}

#define REGISTER_HOOK_OPERAND(pattern, offset, function, ret, ...) \
{ \
static ret (*F) (__VA_ARGS__); \
RegisterHookOperand (pattern, offset, F, function<F>); \
}

/*******************************************************/
#define REGISTER_HOOK_JMP(pattern, offset, function, ret, ...) \
{ \
Expand Down
9 changes: 9 additions & 0 deletions lib/atArray.hh
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,15 @@ struct atArrayGetSizeWrapper
uint16_t Size;
uint16_t Capacity;

atArrayGetSizeWrapper () = default;

atArrayGetSizeWrapper (uint8_t *data, uint16_t size)
{
this->Data = *reinterpret_cast<decltype (this->Data) *> (&data);
this->Size = size;
this->Capacity = size;
}

constexpr Iterator
end ()
{
Expand Down
2 changes: 1 addition & 1 deletion lib/audEngine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ audEngine::InitialisePatterns ()
"? 8d 2d ? ? ? ? ? 8b f0 85 d2 74 ? ", 3, 7);

ConvertCall (hook::get_pattern (
"8d 42 01 ? 8b c9 a9 fe ff ff ff 75 ? 33 c0"),
"8d ? ? ? 8b ? a9 fe ff ff ff 75 ? 33 ? c3 "),
audMetadataManager_GetObjectByRef);

ConvertCall (hook::get_pattern (
Expand Down
23 changes: 15 additions & 8 deletions lib/scrThread.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "scrThread.hh"
#include "Patterns/Patterns.hh"
#include "Utils.hh"
#include <cstdint>
#include <utility>
#include <array>
#include <sstream>
Expand Down Expand Up @@ -198,23 +199,22 @@ scrThread::FindCurrentFunctionBounds (scrProgram *program)

/*******************************************************/
uint16_t
scrThread::FindInstSize (scrProgram *program, uint32_t offset)
scrThread::FindInstSize(uint8_t* bytes, int64_t bytesLen)
{
auto getByteAt = [program] (uint32_t offset) -> uint8_t & {
return program->GetCodeByte<uint8_t> (offset);
};

uint8_t opcode = getByteAt (offset);
uint8_t opcode = bytes[0];
uint16_t size = 1;

auto params = mOpcodes[opcode].second;
for (size_t i = 0; i < strlen (params); ++i)
{
if (bytesLen != -1 && size >= bytesLen)
return 0xFFFF;

switch (params[i])
{
case '$': size += getByteAt (offset + size) + 1; break;
case '$': size += bytes[size] + 1; break;
case 'R': size += 2; break;
case 'S': size += getByteAt (offset + size) * 6 + 1; break;
case 'S': size += bytes[size] * 6 + 1; break;
case 'a': size += 3; break;
case 'b': size += 1; break;
case 'd':
Expand All @@ -227,6 +227,13 @@ scrThread::FindInstSize (scrProgram *program, uint32_t offset)
return size;
}

/*******************************************************/
uint16_t
scrThread::FindInstSize (scrProgram *program, uint32_t offset)
{
return FindInstSize(&program->GetCodeByte<uint8_t>(offset));
}

#ifdef ENABLE_DEBUG_SERVER
/*******************************************************/
std::string
Expand Down
1 change: 1 addition & 0 deletions lib/scrThread.hh
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,7 @@ public:

scrProgram *GetProgram ();

static uint16_t FindInstSize (uint8_t* code, int64_t size = -1);
static uint16_t FindInstSize (scrProgram *program, uint32_t offset);
static std::string DisassemblInsn (scrProgram *program, uint32_t offset);

Expand Down
3 changes: 3 additions & 0 deletions src/common/PatchFilterDLC2612.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ static std::unordered_set<std::string> g_badFiles{
"dlc_mpSum2_g9ec:/common/data/effects/peds/first_person_alternates.meta",
"dlc_mpSum2_g9ec:/common/data/effects/peds/first_person.meta",
"dlc_mpSum2_g9ecCRC:/common/data/pedalternatevariations.meta",

"dlc_mpChristmas3_G9EC:/x64/levels/mpChristmas3_G9EC/vehiclemods/entity3hsw_mods.rpf",
"dlc_mpChristmas3_G9EC:/x64/levels/mpChristmas3_G9EC/vehiclemods/issi8hsw_mods.rpf"
};

static void (*_applyChangeSetEntry)(ChangeSetEntry* entry);
Expand Down
15 changes: 13 additions & 2 deletions src/common/logger.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
#ifndef NDEBUG
#define RAINBOMIZER_BUILD "Debug Build: " __DATE__ " " __TIME__
#else
#define RAINBOMIZER_BUILD "Release v3.3.1: " __DATE__ " " __TIME__
#define RAINBOMIZER_BUILD_SHORT "Release v3.3.1"
#define RAINBOMIZER_BUILD "Release v3.3.3: " __DATE__ " " __TIME__
#define RAINBOMIZER_BUILD_SHORT "Release v3.3.3"
#endif

constexpr int RAINBOMIZER_BUILD_NUMBER =
Expand All @@ -26,6 +26,8 @@ char *(*rage__formatf6eb9) (char *, char const *, ...);

namespace Rainbomizer {

static uint32_t sg_GameBuild = 0;

/*******************************************************/
std::string
GetTimeNow ()
Expand Down Expand Up @@ -91,6 +93,12 @@ Logger::LogMessage (const char *format, ...)
fflush (file);
}

uint32_t
Logger::GetGameBuild ()
{
return sg_GameBuild;
}

/*******************************************************/
class DisplayBuildVersion
{
Expand All @@ -99,6 +107,9 @@ class DisplayBuildVersion
char *version)
{
#ifndef NDEBUG
sg_GameBuild = std::stoi (version);
Logger::LogMessage("Set Game Build to %d", sg_GameBuild);

return rage__formatf6eb9 (out, "Rainbomizer Build %d Build %s",
RAINBOMIZER_BUILD_NUMBER + 1, version);
#else
Expand Down
2 changes: 2 additions & 0 deletions src/common/logger.hh
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class Logger

public:
static void LogMessage (const char *format, ...);

static uint32_t GetGameBuild ();
};

} // namespace Rainbomizer
Loading

0 comments on commit 5eae14d

Please sign in to comment.