Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 49 additions & 26 deletions src/Components/Modules/ModelCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,41 +35,64 @@ namespace Components

ModelCache::ModelCache()
{
static constexpr std::array<std::string_view, 9> fieldsToUpdate
{
// clientState
"modelindex",
"attachModelIndex[0]",
"attachModelIndex[1]",
"attachModelIndex[2]",
"attachModelIndex[3]",
"attachModelIndex[4]",
"attachModelIndex[5]",

// playerState
"viewmodelIndex",

// entityState
"lerp.u.scriptMover.xModelIndex"
};

// To push the model limit we need to update the network protocol because it uses custom integer size
// (currently 9 bits per model, not enough)
const auto oldBitLength = static_cast<size_t>(std::floor(std::log2(BASE_GMODEL_COUNT - 1)) + 1);
const auto newBitLength = static_cast<size_t>(std::floor(std::log2(G_MODELINDEX_LIMIT - 1)) + 1);

assert(oldBitLength == 9);

if (oldBitLength != newBitLength)
static constexpr auto oldBitLength = static_cast<size_t>(std::bit_width(static_cast<size_t>(BASE_GMODEL_COUNT - 1)));
static constexpr auto newBitLength = static_cast<size_t>(std::bit_width(static_cast<size_t>(G_MODELINDEX_LIMIT - 1)));
static_assert(oldBitLength == 9 && newBitLength == 12);

std::array<bool, fieldsToUpdate.size()> found{};

for (auto& netfield : Game::netfields)
{
static const std::unordered_set<std::string> fieldsToUpdate = {
"modelindex",
"attachModelIndex[0]",
"attachModelIndex[1]",
"attachModelIndex[2]",
"attachModelIndex[3]",
"attachModelIndex[4]",
"attachModelIndex[5]",
};

size_t currentBitOffset = 0;

for (size_t i = 0; i < Game::clientStateFieldsCount; i++)
for (size_t i = 0; i < fieldsToUpdate.size(); ++i)
{
auto field = & Game::clientStateFields[i];

if (fieldsToUpdate.contains(field->name))
if (!found[i] && netfield.name == fieldsToUpdate[i])
{
assert(static_cast<size_t>(field->bits) == oldBitLength);

Utils::Hook::Set(&field->bits, newBitLength);
currentBitOffset++;
assert(static_cast<size_t>(netfield.bits) == oldBitLength);

Utils::Hook::Set(&netfield.bits, newBitLength);
found[i] = true;

assert(static_cast<size_t>(netfield.bits) == newBitLength);
break;
}
}
}

assert(std::accumulate(found.begin(), found.end(), 0u) == found.size());

for (auto& netfield : Game::netfields)
{
static_assert(offsetof(Game::entityState_s, index) == 144);

if (netfield.offset == offsetof(Game::entityState_s, index) && (netfield.name == "index"sv || netfield.name == "index.item"sv))
{
// Some of the bit lengths for these index fields are already 15
assert(netfield.bits <= 15);

Utils::Hook::Set(&netfield.bits, std::max(15u, newBitLength));
}
}

// Reallocate G_ModelIndex
Utils::Hook::Set(0x420654 + 3, ModelCache::cached_models_reallocated);
Utils::Hook::Set(0x43BCE4 + 3, ModelCache::cached_models_reallocated);
Expand Down
3 changes: 1 addition & 2 deletions src/Game/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ namespace Game

char(*g_cmdlineCopy)[1024] = reinterpret_cast<char(*)[1024]>(0x1AD7AB0);

NetField* clientStateFields = reinterpret_cast<Game::NetField*>(0x741E40);
size_t clientStateFieldsCount = Utils::Hook::Get<size_t>(0x7433C8);
std::span<NetField> netfields(reinterpret_cast<Game::NetField*>(0x73E3A0), reinterpret_cast<Game::NetField*>(0x742FA0));

MssLocal* milesGlobal = reinterpret_cast<MssLocal*>(0x649A1A0);

Expand Down
3 changes: 1 addition & 2 deletions src/Game/Game.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ namespace Game
extern char(*g_cmdlineCopy)[1024];

// This does not belong anywhere else
extern NetField* clientStateFields;
extern size_t clientStateFieldsCount;
extern std::span<NetField> netfields;
extern MssLocal* milesGlobal;
extern WinVars_t* g_wv;

Expand Down