Skip to content
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

Support PVS visibility changes for entities #507

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

makindotcc
Copy link
Contributor

@makindotcc makindotcc commented Feb 10, 2024

In lately introduced update two extra bits that represents:

  1. is "preserve" or "update" event - we already know that
  2. is entity visible in PVS (valve's exact name: m_bVisibleinPVS) - a new thing i discovered now

Part of an decompiled CClientState::ReadPacketEntities:

        *(_DWORD *)updateType = 2;              // delta update
        (*(void (__fastcall **)(_DWORD *, CEntityReadInfo *))(*(_QWORD *)clientState + 1040i64))(
          clientState,
          entityReadInfo);
        *(_DWORD *)(entityReadInfo->qword18 + 12i64) = *(_DWORD *)pNewEntity;
        v95 = (__int64 *)qword_5742A0;
        *(_DWORD *)(entityReadInfo->qword18 + 4 * ((__int64)*(int *)pNewEntity >> 5) + 32) |= 1 << (*(_DWORD *)pNewEntity & 0x1F);
        hasPvsBitSet_1 = (*(_DWORD *)&entityReadInfo->m_UpdateFlags & 0x10) != 0; // m_UpdateFlags is an our "cmd"
        has_vis_bits = *p_has_vis_bits; // has_vis_bits from packet
        if ( v95 )
        {
          v98 = *v95;
          v99 = *(unsigned int *)&entityReadInfo->m_nNewEntity;
          v147 = 0i64;
          entityInstance = 0i64;
          v149 = 0i64;
          v150 = 0i64;
          v151 = 0i64;
          v152 = 0;
          v153 = -1i64;
          if ( (*(unsigned __int8 (__fastcall **)(__int64 *, __int64, __int128 *))(v98 + 208))(v95, v99, &v147) ) // get entity or smth
          {
            visibleInPVS = 1;
            if ( has_vis_bits )
              visibleInPVS = hasPvsBitSet_1;
            *(_BYTE *)(entityInstance + 0x30) = visibleInPVS;// m_bVisibleinPVS
          }
        }
        v101 = entityReadInfo->qword10;

Similar code is in PreserveEnt handler.

Looks like server does not use PVS for entities yet.

Im requesting this as a draft because I dont know what to do with m_bVisibleinPVS yet - there is active field in Entity parser struct, but should we use it as representation of m_bVisibleinPVS? I don't understand why there is additional bit for it when there already is updateFlagLeave and updateFlagDelete.

Copy link

codecov bot commented Feb 10, 2024

Codecov Report

Attention: 14 lines in your changes are missing coverage. Please review.

Comparison is base (359934e) 80.49% compared to head (cba32f0) 80.50%.

Files Patch % Lines
pkg/demoinfocs/sendtables2/entity.go 70.21% 9 Missing and 5 partials ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##           master     #507   +/-   ##
=======================================
  Coverage   80.49%   80.50%           
=======================================
  Files          47       47           
  Lines        6367     6375    +8     
=======================================
+ Hits         5125     5132    +7     
- Misses       1029     1030    +1     
  Partials      213      213           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

if cmd&0x01 == 0 {
if cmd&0x02 != 0 {

if cmd&updateFlagLeave == 0 {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

to reviewdog: it was already "complex" 😭

I can "flatten" it or extract handlers to other functions, that was on my mind first, but I thought people in go don't like too many functions.

Copy link
Owner

Choose a reason for hiding this comment

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

I will take a look when I'm on my PC 🙂

Personally I like having lots of functions in Go (I know this project doesn't look like it as a lot of it is REd and kinda done 1:1) but let's leave it for now until I had a chance to take a proper look 😄

Copy link
Owner

Choose a reason for hiding this comment

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

PS: reviewdog is more of a guideline than hard rules, so we don't always have to listen to it 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, I already made this way - inspired by game code.
ezgif com-video-to-gif_2

@makindotcc
Copy link
Contributor Author

makindotcc commented Feb 10, 2024

More about m_bVisibleinPVS:

  1. it is set to 1 when new entity is created
  2. it is set to packet_has_pvs_vis_bits == 0 when entity left (not deleted)

Still no idea why they introduced these extra bits, since there already were soft "entity left".

@spheenik
Copy link

spheenik commented Feb 15, 2024

@makindotcc

Hmm. So it is set to 1 on creation. Fine.
In case of update, the value from the PVS bits is taken. Also fine.
But in case the entity leaves, what happens exactly?
In case the packet has PVS bits, we'll set it to 1, and 0 otherwise?
Why not on delete? It is a leave and delete after all.

@makindotcc
Copy link
Contributor Author

makindotcc commented Feb 15, 2024

In case the packet has PVS bits, we'll set it to 1, and 0 otherwise?

In case the packet has PVS bits, we'll set it to 0 and 1 otherwise. I guess this is done for backwards compatibility.

really raw decompilation of cs2 engine2.dll
ProcessPacketEntities:

// [...]
no_pvs_vis_bits = packetEntities->_impl_.has_pvs_vis_bits_ == 0;
entityReadInfo->has_pvs_vis_bits1 = !no_pvs_vis_bits;
// [...]
ReadPacketEntities(clientStateIGuess, entityReadInfo);
// [...]

ReadPacketEntities:

    // [...]
    p_has_pvs_vis_bits1 = &entityReadInfo->has_pvs_vis_bits1
    // [...]
    p_UpdateFlags = &entityReadInfo->m_UpdateFlags;
    *(_DWORD *)updateType = 3;
    pNewEntity = &entityReadInfo->m_nNewEntity;
    while ( 1 )
    {
      packet_has_pvs_vis_bits = *p_has_pvs_vis_bits1;
      // [...]
      cmd = *(_DWORD *)&entityReadInfo->m_UpdateFlags;
      if ( (cmd & 4) == 0 )
      {
        if ( (cmd & 1) != 0 )
        {
          isNotDelete = (*p_UpdateFlags & 2) == 0;// https://github.com/markus-wa/demoinfocs-golang/blob/cba32f0669f0b9f055d711547ece1ea4f703d9f6/pkg/demoinfocs/sendtables2/entity.go#L578
          *(_DWORD *)updateType = 1;            // leave entity
          if ( isNotDelete )
          {
            if ( qword_5742A0 )
            {
              v71 = (unsigned int)*v9;
              v169 = 0;
              v164 = 0i64;
              entity = 0i64;
              v166 = 0i64;
              v167 = 0i64;
              v168 = 0i64;
              v170 = -1i64;
              if ( (*(unsigned __int8 (__fastcall **)(__int64, __int64, __int128 *))(*(_QWORD *)qword_5742A0 + 208i64))(
                     qword_5742A0,
                     v71,
                     &v164) )                   // get entity or smth
              {
                entity->m_bVisibleinPVS = packet_has_pvs_vis_bits == 0;
              }
            }
          }
          else
          {
            v70 = qword_5742A0;
            *(&entityReadInfo->unsigned_intE0 + ((__int64)*v9 >> 5)) |= 1 << (*v9 & 0x1F);
            memset(v161, 0, sizeof(v161));
            v162 = 0;
            v163 = -1i64;
            if ( v70
              && (*(unsigned __int8 (__fastcall **)(__int64, _QWORD, __int128 *))(*(_QWORD *)v70 + 208i64))(
                   v70,
                   (unsigned int)*v9,
                   v161) )
            {
              *(_DWORD *)(*((_QWORD *)&v161[0] + 1) + 0x30i64) |= 0x8000u; // I didn't reverse this flag, but it hasn't changed in the "arms" update, so I assume this marks entity as deleted
            }
          }
      // [...]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants