Skip to content

Commit

Permalink
tweak(streaming/five): standardize usage of GetArchetypeFromHashKey
Browse files Browse the repository at this point in the history
Reduce code duplication and make it easier to use between different games.
  • Loading branch information
Disquse committed Sep 17, 2023
1 parent 9729577 commit edf77da
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 72 deletions.
13 changes: 4 additions & 9 deletions code/components/devtools-five/src/WorldEditor.cpp
Expand Up @@ -22,15 +22,10 @@

#include <nutsnbolts.h>

static hook::cdecl_stub<fwArchetype*(uint32_t nameHash, uint64_t* archetypeUnk)> getArchetype([]()
{
return hook::get_call(hook::pattern("89 44 24 40 8B 4F 08 80 E3 01 E8").count(1).get(0).get<void>(10));
});

/*static ConsoleCommand consoleCmd("make_entity", [](const std::string& name)
{
uint64_t index;
fwArchetype* archetype = getArchetype(HashString(name.c_str()), &index);
rage::fwModelId index;
fwArchetype* archetype = rage::fwArchetypeManager::GetArchetypeFromHashKey(HashString(name.c_str()), index);
fwEntityDef entityDef;
entityDef.archetypeName = HashString(name.c_str());
Expand Down Expand Up @@ -228,8 +223,8 @@ void RecreateModel()
entityDef.flags = 0;
entityDef.lodLevel = 1;

uint64_t index;
getArchetype(archetypeDef->name, &index);
rage::fwModelId index;
rage::fwArchetypeManager::GetArchetypeFromHashKey(archetypeDef->name, index);

fwEntity* entity = mi->CreateEntity();
entity->SetModelIndex((uint32_t*)&index);
Expand Down
21 changes: 8 additions & 13 deletions code/components/extra-natives-five/src/PedExtraNatives.cpp
Expand Up @@ -12,11 +12,6 @@

#include <GameInit.h>

static hook::cdecl_stub<fwArchetype*(uint32_t nameHash, uint64_t* archetypeUnk)> getArchetype([]()
{
return hook::get_call(hook::pattern("89 44 24 40 8B 4F 08 80 E3 01 E8").count(1).get(0).get<void>(10));
});

class CPedHeadBlendData
{
public:
Expand Down Expand Up @@ -254,8 +249,8 @@ static HookFunction initFunction([]()
{
for (auto& [pedModel, personality] : undoPersonalities)
{
uint64_t index;
auto archetype = getArchetype(pedModel, &index);
rage::fwModelId index;
auto archetype = rage::fwArchetypeManager::GetArchetypeFromHashKey(pedModel, index);

// if is ped
if (archetype && archetype->miType == 6)
Expand All @@ -272,8 +267,8 @@ static HookFunction initFunction([]()
auto pedModel = context.GetArgument<uint32_t>(0);
auto personality = context.GetArgument<uint32_t>(1);

uint64_t index;
auto archetype = getArchetype(pedModel, &index);
rage::fwModelId index;
auto archetype = rage::fwArchetypeManager::GetArchetypeFromHashKey(pedModel, index);

// if is ped
if (archetype && archetype->miType == 6)
Expand All @@ -299,8 +294,8 @@ static HookFunction initFunction([]()
{
auto pedModel = context.GetArgument<uint32_t>(0);

uint64_t index;
auto archetype = getArchetype(pedModel, &index);
rage::fwModelId index;
auto archetype = rage::fwArchetypeManager::GetArchetypeFromHashKey(pedModel, index);

// if is ped
if (archetype && archetype->miType == 6)
Expand All @@ -316,8 +311,8 @@ static HookFunction initFunction([]()
{
auto pedModel = context.GetArgument<uint32_t>(0);

uint64_t index;
auto archetype = getArchetype(pedModel, &index);
rage::fwModelId index;
auto archetype = rage::fwArchetypeManager::GetArchetypeFromHashKey(pedModel, index);

uint32_t result = 0;

Expand Down
21 changes: 2 additions & 19 deletions code/components/extra-natives-five/src/RuntimeAssetNatives.cpp
Expand Up @@ -1072,23 +1072,6 @@ static hook::cdecl_stub<void*(fwEntityDef*, int fileIdx, fwArchetype* archetype,
return hook::get_call(hook::pattern("4C 8D 4C 24 40 4D 8B C6 41 8B D7 48 8B CF").count(1).get(0).get<void>(14));
});

static hook::cdecl_stub<fwArchetype*(uint32_t nameHash, uint64_t* archetypeUnk)> getArchetype([]()
{
return hook::get_call(hook::pattern("89 44 24 40 8B 4F 08 80 E3 01 E8").count(1).get(0).get<void>(10));
});

fwArchetype* GetArchetypeSafe(uint32_t archetypeHash, uint64_t* archetypeUnk)
{
__try
{
return getArchetype(archetypeHash, archetypeUnk);
}
__except (EXCEPTION_EXECUTE_HANDLER)
{
return nullptr;
}
}

static InitFunction initFunction([]()
{
OnMainGameFrame.Connect([]
Expand Down Expand Up @@ -1231,8 +1214,8 @@ static InitFunction initFunction([]()
fwEntityDef* entityDef = (fwEntityDef*)MakeStructFromMsgPack("CEntityDef", entityData);
mapData->entities.Set(i, entityDef);

uint64_t archetypeUnk = 0xFFFFFFF;
fwArchetype* archetype = GetArchetypeSafe(entityDef->archetypeName, &archetypeUnk);
rage::fwModelId modelId{ 0xFFFFFFF };
fwArchetype* archetype = rage::fwArchetypeManager::GetArchetypeFromHashKeySafe(entityDef->archetypeName, modelId);

if (archetype)
{
Expand Down
4 changes: 3 additions & 1 deletion code/components/gta-streaming-five/include/EntitySystem.h
Expand Up @@ -218,7 +218,9 @@ struct fwModelId
class STREAMING_EXPORT fwArchetypeManager
{
public:
static fwArchetype* GetArchetypeFromHashKey(uint32_t hash, fwModelId& id);
static fwArchetype* GetArchetypeFromHashKey(uint32_t hash, fwModelId& id);

static fwArchetype* GetArchetypeFromHashKeySafe(uint32_t hash, fwModelId& id);
};
}

Expand Down
12 changes: 12 additions & 0 deletions code/components/gta-streaming-five/src/EntitySystem.cpp
Expand Up @@ -64,3 +64,15 @@ fwArchetype* rage::fwArchetypeManager::GetArchetypeFromHashKey(uint32_t hash, fw
{
return getArchetype(hash, id);
}

fwArchetype* rage::fwArchetypeManager::GetArchetypeFromHashKeySafe(uint32_t hash, fwModelId& id)
{
__try
{
return getArchetype(hash, id);
}
__except (EXCEPTION_EXECUTE_HANDLER)
{
return nullptr;
}
}
25 changes: 4 additions & 21 deletions code/components/gta-streaming-five/src/PlacementHacks.cpp
Expand Up @@ -66,16 +66,11 @@ CMapData::CMapData()
_mapData_ctor(this);
}

static hook::cdecl_stub<void*(fwEntityDef*, int fileIdx, fwArchetype* archetype, uint64_t* archetypeUnk)> fwEntityDef__instantiate([] ()
static hook::cdecl_stub<void*(fwEntityDef*, int fileIdx, fwArchetype* archetype, rage::fwModelId* archetypeUnk)> fwEntityDef__instantiate([] ()
{
return hook::get_call(hook::pattern("4C 8D 4C 24 40 4D 8B C6 41 8B D7 48 8B CF").count(1).get(0).get<void>(14));
});

static hook::cdecl_stub<fwArchetype*(uint32_t nameHash, uint64_t* archetypeUnk)> getArchetype([] ()
{
return hook::get_call(hook::pattern("89 44 24 40 8B 4F 08 80 E3 01 E8").count(1).get(0).get<void>(10));
});

atArray<fwFactoryBase<fwArchetype>*>* g_archetypeFactories;

struct DataFileEntry
Expand Down Expand Up @@ -124,18 +119,6 @@ static hook::cdecl_stub<void(fwArchetype*)> registerArchetype([]()
return hook::get_pattern("48 8B D9 8A 49 60 80 F9", -11);
});

fwArchetype* GetArchetypeSafe(uint32_t archetypeHash, uint64_t* archetypeUnk)
{
__try
{
return getArchetype(archetypeHash, archetypeUnk);
}
__except (EXCEPTION_EXECUTE_HANDLER)
{
return nullptr;
}
}

static std::vector<CMapDataContents*> g_sceneContentsList;
static uintptr_t sceneNodeThing;

Expand Down Expand Up @@ -417,8 +400,8 @@ void ParseArchetypeFile(char* text, size_t length)
archetypeHash = _atoi64(&(archetypeName.c_str())[5]);
}

uint64_t archetypeUnk = 0xFFFFFFF;
fwArchetype* archetype = GetArchetypeSafe(archetypeHash, &archetypeUnk);
rage::fwModelId modelId{ 0xFFFFFFF };
fwArchetype* archetype = rage::fwArchetypeManager::GetArchetypeFromHashKeySafe(archetypeHash, modelId);

if (archetype)
{
Expand All @@ -440,7 +423,7 @@ void ParseArchetypeFile(char* text, size_t length)

getUInt("flags", &entityDef->flags);

void* entity = fwEntityDef__instantiate(entityDef, 0, archetype, &archetypeUnk);
void* entity = fwEntityDef__instantiate(entityDef, 0, archetype, &modelId);

contents->entities[i] = entity;

Expand Down
Expand Up @@ -3,7 +3,6 @@

#include <EntitySystem.h>

fwArchetype* GetArchetypeSafe(uint32_t archetypeHash, uint64_t* archetypeUnk);

static hook::cdecl_stub<fwArchetype*(uint32_t* archetypeUnk)> getArchetypeFromUnk([]()
{
Expand Down Expand Up @@ -43,16 +42,16 @@ static void* CreatePopulationPedWrap(uint32_t mi, float* position, float a3, voi
// apply changed data
if (creationState.model != modelHash)
{
uint64_t archetypeUnk = mi;
GetArchetypeSafe(creationState.model, &archetypeUnk);
rage::fwModelId archetypeUnk{ mi };
rage::fwArchetypeManager::GetArchetypeFromHashKeySafe(creationState.model, archetypeUnk);

uint32_t at = archetypeUnk;
uint32_t at = archetypeUnk.id;
if (!hasModelLoaded(&at))
{
return nullptr;
}

mi = archetypeUnk & 0xFFFF;
mi = archetypeUnk.id & 0xFFFF;
}

position[0] = creationState.position[0];
Expand Down
Expand Up @@ -9,8 +9,6 @@ extern std::string GetCurrentStreamingName();

static int(*g_origCalculateMipLevel)(uint8_t type, uint16_t width, uint16_t height, uint8_t levels, uint32_t format);

fwArchetype* GetArchetypeSafe(uint32_t archetypeHash, uint64_t* archetypeUnk);

static ConVar<int>* g_maxVehicleTextureRes;
static ConVar<int>* g_maxVehicleTextureResRgba;
static uintptr_t g_vtbl_CVehicleModelInfo;
Expand All @@ -26,8 +24,8 @@ static int CalculateMipLevelHook(uint8_t type, uint16_t width, uint16_t height,
auto baseName = strName.substr(0, strName.find('.'));

// try getting the relevant archetype, and see if it's a vehicle
uint64_t archetypeUnk = 0xFFFFFFF;
auto archetype = GetArchetypeSafe(HashString(baseName.c_str()), &archetypeUnk);
rage::fwModelId archetypeUnk{ 0xFFFFFFF };
auto archetype = rage::fwArchetypeManager::GetArchetypeFromHashKeySafe(HashString(baseName.c_str()), archetypeUnk);

if (archetype)
{
Expand Down

0 comments on commit edf77da

Please sign in to comment.