Skip to content

Net code: model index bit count update #246

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: develop
Choose a base branch
from
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));
}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not entirely sure whether this second netfield loop is required. If it is, it's probably easiest to set all bit counts of all 15 index netfields to a bit count of 15 (they are currently set to 10, 11 or 15).

This probably could use some more investigating and testing.


// 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