Skip to content

Commit

Permalink
Ped Randomizer: Player randomizer improvements + lots of crash fixes
Browse files Browse the repository at this point in the history
* Cutscene Randomizer will no longer randomize ped models because it
has a million issues with ped registering. Ped Randomizer does it
pretty well including player randomization. It'll be renamed to
Cutscene Prop Randomizer in the future

* Cops are now random with Ped Randomizer and cops can spawn as normal
peds (and won't bust you).

* Fish/Rat related skeleton crashes have been fixed for the most
part. There might be few edge cases that need to be discovered for it
to be fully functional though. This lays ground for future work on ped
randomizer and allowing animals to be used as normal peds in missions.
  • Loading branch information
Parik27 committed Apr 29, 2021
1 parent 5a0c61c commit 3d25680
Show file tree
Hide file tree
Showing 12 changed files with 395 additions and 96 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ file(GLOB_RECURSE SOURCES CONFIGURE_DEPENDS
src/misc/*.cc
src/mission/*.cc
src/sounds/*.cc
src/peds/*.cc
src/vehicles/*.cc
src/weapons/*.cc
lib/*.cc
Expand Down
2 changes: 1 addition & 1 deletion build-count.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
314
334
7 changes: 4 additions & 3 deletions lib/CModelInfo.hh
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public:
class CPedModelInfo : public CBaseModelInfo
{
public:
uint8_t field_0xb0[54];
uint8_t field_0xb0[64];
uint32_t m_nMovementClipSet;
uint8_t field_0xf4[4];
uint64_t field_0xf8;
Expand All @@ -82,7 +82,7 @@ public:
float m_nMinActivationImpulse;
float m_fStubble;
uint8_t field_0x144[6];
short field_0x14a;
short m_nPersonality; // confirmed
uint8_t field_0x14c[16];
uint8_t Radio1;
uint8_t Radio2;
Expand Down Expand Up @@ -141,9 +141,10 @@ public:
uint32_t m_nPerceptionInfo;
uint32_t m_nDefaultBrawlingStyle;
uint32_t m_nDefaultUnarmedWeapon;
uint32_t m_nSpecialAbility;
} _info;

uint8_t field_0x278[6];
uint8_t field_0x278[2];
uint8_t m_nFlags;
uint8_t field_0x27f;
uint8_t m_nFlags2;
Expand Down
4 changes: 4 additions & 0 deletions lib/ModUtils/Trampoline.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ class Trampoline
return *Pointer<T>( align );
}

LPVOID Space( size_t size, size_t align = 0)
{
return GetNewSpace( size, align );
}

private:
static Trampoline* MakeTrampoline( uintptr_t addr )
Expand Down
6 changes: 3 additions & 3 deletions lib/Utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ MakeJMP64 (injector::memory_pointer_tr at, injector::memory_pointer_raw dest)
void
RegisterJmpHook (void *addr, void *dst, void **outOrignal, int size)
{
LPVOID mem = VirtualAlloc (0, size + 12, MEM_COMMIT | MEM_RESERVE,
PAGE_EXECUTE_READWRITE);
LPVOID mem = Trampoline::MakeTrampoline (GetModuleHandle (nullptr))
->Space (size + 5, 1);
memcpy (mem, addr, size);
MakeJMP64 (uintptr_t (mem) + size, uintptr_t (addr) + size);
injector::MakeJMP (uintptr_t (mem) + size, uintptr_t (addr) + size);
MakeJMP64 (addr, dst);

*outOrignal = mem;
Expand Down
21 changes: 18 additions & 3 deletions lib/Utils.hh
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,22 @@ RegisterHook (void *addr, F hookedFunc)
/*******************************************************/
template <bool Jmp = false, typename F, typename O>
void
RegisterHook (const std::string &pattern, int offset, O &originalFunc,
F hookedFunc)
RegisterHook (void *addr, O &originalFunc, F hookedFunc)
{
void *addr = hook::get_pattern (pattern, offset);
ReadCall (addr, originalFunc);
RegisterHook<Jmp> (addr, hookedFunc);
}

/*******************************************************/
template <bool Jmp = false, typename F, typename O>
void
RegisterHook (const std::string &pattern, int offset, O &originalFunc,
F hookedFunc)
{
RegisterHook<Jmp> (hook::get_pattern (pattern, offset), originalFunc,
hookedFunc);
}

/*******************************************************/
template <bool Jmp = false, typename F>
void
Expand Down Expand Up @@ -222,3 +230,10 @@ GetRelativeReference (const std::string &pattern, int dataOffset)
static ret (*F) (__VA_ARGS__); \
RegisterHook (pattern, offset, F, function<F>); \
}

/*******************************************************/
#define REGISTER_JMP_HOOK(size, pattern, offset, function, ret, ...) \
{ \
static ret (*F) (__VA_ARGS__); \
RegisterJmpHook<size> (pattern, offset, F, function<F>); \
}
64 changes: 6 additions & 58 deletions src/misc/cutscenes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
#include <cstdio>
#include <Utils.hh>
#include <CutSceneManager.hh>
#include "CModelInfo.hh"
#include "CStreaming.hh"
#include "Patterns/Patterns.hh"
#include "common/logger.hh"
#include "common/common.hh"
#include "common/config.hh"
#include "injector/injector.hpp"
#include "CPed.hh"
#include "peds/peds_Compatibility.hh"

class parInstanceVisitor;

Expand All @@ -17,8 +20,6 @@ void (*VisitTopLevelStructure_37027e) (parInstanceVisitor *,

class CutSceneRandomizer
{
inline static std::array<int32_t, 3> m_aPlayerObjects{};

/*******************************************************/
static std::vector<std::vector<uint32_t>> &
GetModelsList ()
Expand All @@ -29,15 +30,15 @@ class CutSceneRandomizer

/*******************************************************/
static uint32_t
GetRandomModel (uint32_t model)
GetRandomModel (uint32_t modelHash)
{
for (const auto &i : GetModelsList ())
{
if (DoesElementExist (i, model))
if (DoesElementExist (i, modelHash))
return GetRandomElement (i);
}

return model;
return modelHash;
}

/*******************************************************/
Expand Down Expand Up @@ -68,36 +69,10 @@ class CutSceneRandomizer
return true;
}

/*******************************************************/
static int32_t
GetPlayerArrayIdx (uint32_t hash)
{
switch (hash)
{
case "player_zero"_joaat: return 0; break;
case "player_one"_joaat: return 1; break;
case "player_two"_joaat: return 2; break;
default: return -1;
}
}

/*******************************************************/
static void
SetPlayerObjectIdFromObject (cutfModelObject *obj)
{
int idx = GetPlayerArrayIdx (obj->StreamingName);

if (idx != -1)
m_aPlayerObjects[idx] = obj->iObjectId;
}

/*******************************************************/
static void
RandomizeCutScene (parInstanceVisitor *visitor, cutfCutsceneFile2 *file)
{
for (auto &obj : m_aPlayerObjects)
obj = -1;

for (int i = 0; i < file->pCutsceneObjects.Size; i++)
{
switch (file->pCutsceneObjects.Data[i]->GetType ())
Expand All @@ -106,8 +81,6 @@ class CutSceneRandomizer
case eCutfObjectType::MODEL: {
auto obj = static_cast<cutfModelObject *> (
file->pCutsceneObjects.Data[i]);

SetPlayerObjectIdFromObject(obj);

obj->StreamingName
= GetRandomModel (obj->StreamingName);
Expand Down Expand Up @@ -135,20 +108,6 @@ class CutSceneRandomizer
VisitTopLevelStructure_37027e (visitor, file);
}

/*******************************************************/
static void
CorrectPlayerObjIdx (CutSceneManager *mgr)
{
int32_t arrIdx = GetPlayerArrayIdx (
CPedFactory::Get ()->pPlayer->m_pModelInfo->m_nHash);

if (m_aPlayerObjects[arrIdx] != -1)
{
mgr->bHasPlayerObjectId = true;
mgr->m_nPlayerObjId = m_aPlayerObjects[arrIdx];
}
}

public:
/*******************************************************/
CutSceneRandomizer ()
Expand All @@ -163,16 +122,5 @@ class CutSceneRandomizer

RegisterHook ("8d ? ? 20 0f ba e8 10 89 44 ? ? e8", 12,
VisitTopLevelStructure_37027e, RandomizeCutScene);

//?? 8b d9 40 38 3d ?? ?? ?? ?? 75 ?? e8 ?? ?? ?? ??
RegisterHook ("8b d9 40 38 3d ? ? ? ? 75 ? e8", 11,
CorrectPlayerObjIdx);

#if (false)
// Disables the check for StreamingName of cutscene registered entities
// pCVar5>m_nStreamingName == *param_3
injector::WriteMemory<uint8_t> (
hook::get_pattern ("41 8b 06 39 83 ac 00 00 00 74", 9), 0xeb);
#endif
}
} _cuts;
Loading

0 comments on commit 3d25680

Please sign in to comment.