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

pre/post: reworked changes in AddToFullPack for correctness and portability #551

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
143 changes: 84 additions & 59 deletions BunnymodXT/modules/ServerDLL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2508,73 +2508,96 @@ void ServerDLL::GetTriggerAlpha(const char *classname, bool inactive, bool addit
a = common_alphas[inactive][additive];
}

HOOK_DEF_7(ServerDLL, int, __cdecl, AddToFullPack, struct entity_state_s*, state, int, e, edict_t*, ent, edict_t*, host, int, hostflags, int, player, unsigned char*, pSet)
void ServerDLL::AddToFullPack_Pre(edict_t *ent, unsigned char **pSet, int &status)
{
if (!HwDLL::GetInstance().ppGlobals) {
return ORIG_AddToFullPack(state, e, ent, host, hostflags, player, pSet);
}

if (CVars::bxt_render_far_entities.GetBool()) // https://github.com/ValveSoftware/halflife/blob/c7240b965743a53a29491dd49320c88eecf6257b/dlls/client.cpp#L1114-L1122
pSet = NULL;
*pSet = NULL;

auto oldEffects = ent->v.effects;
auto oldRendermode = ent->v.rendermode;
auto oldRenderColor = ent->v.rendercolor;
auto oldRenderAmount = ent->v.renderamt;
auto oldRenderFx = ent->v.renderfx;
auto oldFlags = ent->v.flags;
auto oldIUser1 = ent->v.iuser1;
auto oldIUser2 = ent->v.iuser2;
if (!CVars::bxt_show_hidden_entities.GetBool() && !CVars::bxt_show_hidden_entities_clientside.GetBool() && !CVars::bxt_show_triggers_legacy.GetBool())
return;

const char *classname = HwDLL::GetInstance().ppGlobals->pStringBase + ent->v.classname;
bool is_trigger = std::strncmp(classname, "trigger_", 8) == 0;
bool is_ladder = std::strncmp(classname, "func_ladder", 11) == 0;
bool is_trigger = !std::strncmp(classname, "trigger_", 8) || !std::strcmp(classname, "func_ladder");

if (!is_trigger && CVars::bxt_show_hidden_entities.GetBool()) {
bool show = ent->v.rendermode != kRenderNormal && ent->v.rendermode != kRenderGlow;
switch (CVars::bxt_show_hidden_entities.GetInt()) {
case 1:
show = show && ent->v.renderamt == 0;
break;
case 2:
break;
default:
show = show || (ent->v.effects & EF_NODRAW) != 0;
if (!is_trigger)
{
if (CVars::bxt_show_hidden_entities.GetBool())
{
bool show = ent->v.rendermode != kRenderNormal && ent->v.rendermode != kRenderGlow;
switch (CVars::bxt_show_hidden_entities.GetInt()) {
case 1:
show = show && ent->v.renderamt == 0;
break;
case 2:
break;
default:
show = show || (ent->v.effects & EF_NODRAW) != 0;
}

if (show) {
ent->v.effects &= ~EF_NODRAW;
ent->v.rendermode = kRenderNormal;

status = BXT_ADDTOFULLPACK_STATE_HIDDEN_ENTITIES;
}
}
else if (CVars::bxt_show_hidden_entities_clientside.GetBool())
{
if (ent->v.effects & EF_NODRAW)
{
ent->v.effects &= ~EF_NODRAW;
ent->v.renderamt = 0;

if (show) {
ent->v.effects &= ~EF_NODRAW;
ent->v.rendermode = kRenderNormal;
// e.g. func_wall_toggle is kRenderNormal when it's EF_NODRAW'd, so that'd make it visible always, fix that
if (ent->v.rendermode == kRenderNormal)
ent->v.rendermode = kRenderTransTexture;

status = BXT_ADDTOFULLPACK_STATE_HIDDEN_ENTITIES_CLIENTSIDE;
}
}
} else if (!is_trigger && CVars::bxt_show_hidden_entities_clientside.GetBool()) {
if (ent->v.effects & EF_NODRAW)
}
else
{
if (CVars::bxt_show_triggers_legacy.GetBool())
{
ent->v.effects &= ~EF_NODRAW;
ent->v.renderamt = 0;
ent->v.rendermode = kRenderTransColor;
ent->v.renderfx = kRenderFxTrigger;
GetTriggerColor(classname, ent->v.rendercolor.x, ent->v.rendercolor.y, ent->v.rendercolor.z);

// e.g. func_wall_toggle is kRenderNormal when it's EF_NODRAW'd, so that'd make it visible always, fix that
if (ent->v.rendermode == kRenderNormal)
ent->v.rendermode = kRenderTransTexture;
status = BXT_ADDTOFULLPACK_STATE_TRIGGERS;
}
}
else if ((is_trigger || is_ladder) && CVars::bxt_show_triggers_legacy.GetBool()) {
ent->v.effects &= ~EF_NODRAW;
ent->v.renderamt = 0;
ent->v.rendermode = kRenderTransColor;
ent->v.renderfx = kRenderFxTrigger;
GetTriggerColor(classname, ent->v.rendercolor.x, ent->v.rendercolor.y, ent->v.rendercolor.z);
}

HOOK_DEF_7(ServerDLL, int, __cdecl, AddToFullPack, struct entity_state_s*, state, int, e, edict_t*, ent, edict_t*, host, int, hostflags, int, player, unsigned char*, pSet)
{
if (!HwDLL::GetInstance().ppGlobals) {
return ORIG_AddToFullPack(state, e, ent, host, hostflags, player, pSet);
}

int status = BXT_ADDTOFULLPACK_STATE_NO;
bool oldEffectsNodraw = ent->v.effects & EF_NODRAW;
auto oldRendermode = ent->v.rendermode;
auto oldRenderAmount = ent->v.renderamt;
auto oldRenderColor = ent->v.rendercolor;
auto oldRenderFx = ent->v.renderfx;

AddToFullPack_Pre(ent, &pSet, status);

auto ret = ORIG_AddToFullPack(state, e, ent, host, hostflags, player, pSet);

ent->v.effects = oldEffects;
ent->v.rendermode = oldRendermode;
ent->v.rendercolor = oldRenderColor;
ent->v.renderamt = oldRenderAmount;
ent->v.renderfx = oldRenderFx;
ent->v.flags = oldFlags;
ent->v.iuser1 = oldIUser1;
ent->v.iuser2 = oldIUser2;
if (status != BXT_ADDTOFULLPACK_STATE_NO) {
ent->v.effects = SET_OR_UNSET_FLAG(oldEffectsNodraw, ent->v.effects, EF_NODRAW);
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Damn, how did I let this happen and the compiler didn't even complain? Will fix it later simply to:

SET_OR_UNSET_FLAG(oldEffectsNodraw, ent->v.effects, EF_NODRAW);

Copy link
Owner

Choose a reason for hiding this comment

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

Not sure I would've caught this. It's difficult to review when two different changes are bundled together in one commit like this, especially when the diff is completely messed up too

ent->v.rendermode = oldRendermode;
}
if (status == BXT_ADDTOFULLPACK_STATE_TRIGGERS) {
ent->v.rendercolor = oldRenderColor;
ent->v.renderfx = oldRenderFx;
}
if (status == BXT_ADDTOFULLPACK_STATE_HIDDEN_ENTITIES_CLIENTSIDE || status == BXT_ADDTOFULLPACK_STATE_TRIGGERS)
ent->v.renderamt = oldRenderAmount;

return ret;
}
Expand Down Expand Up @@ -3085,29 +3108,31 @@ HOOK_DEF_3(ServerDLL, void, __fastcall, CChangeLevel__TouchChangeLevel, void*, t

HOOK_DEF_1(ServerDLL, void, __fastcall, CTriggerCamera__FollowTarget, void*, thisptr)
{
if (!CVars::bxt_cof_allow_skipping_all_cutscenes.GetBool())
{
ORIG_CTriggerCamera__FollowTarget(thisptr);
return;
}

bool changed = false, oldSpawnFlagsCoFUnskippable = false;
auto pev = GET_PEV(thisptr);
if (pev)
{
bool changed = false;
auto oldSpawnFlags = pev->spawnflags;
if (CVars::bxt_cof_allow_skipping_all_cutscenes.GetBool())
{
if (pev->spawnflags & COF_TRIGGER_CAMERA_FLAGS_UNSKIPPABLE) // "Unskippable" flag from .fgd
oldSpawnFlagsCoFUnskippable = pev->spawnflags & COF_TRIGGER_CAMERA_FLAGS_UNSKIPPABLE;
if (oldSpawnFlagsCoFUnskippable) // "Unskippable" flag from .fgd
{
pev->spawnflags &= ~COF_TRIGGER_CAMERA_FLAGS_UNSKIPPABLE;
changed = true;
}
}
}

ORIG_CTriggerCamera__FollowTarget(thisptr);
ORIG_CTriggerCamera__FollowTarget(thisptr);

if (changed)
pev->spawnflags = oldSpawnFlags;
}
else
{
ORIG_CTriggerCamera__FollowTarget(thisptr);
}
if (changed)
SET_OR_UNSET_FLAG(oldSpawnFlagsCoFUnskippable, pev->spawnflags, COF_TRIGGER_CAMERA_FLAGS_UNSKIPPABLE)
}

void ServerDLL::TraceLineWrap(const Vector* vecStart, const Vector* vecEnd, int igmon, edict_t* pentIgnore, TraceResult* ptr)
Expand Down
2 changes: 2 additions & 0 deletions BunnymodXT/modules/ServerDLL.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ class ServerDLL : public IHookableDirFilter

bool IsPlayer(edict_t *ent);

void AddToFullPack_Pre(edict_t *ent, unsigned char **pSet, int &status);

private:
ServerDLL() : IHookableDirFilter({ L"dlls", L"cl_dlls"}) {};
ServerDLL(const ServerDLL&);
Expand Down
12 changes: 11 additions & 1 deletion BunnymodXT/shared.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,14 @@ constexpr int kRenderFxTrigger = 241; // DO NOT CHANGE THIS VALUE OR YOU WILL BR
constexpr steamid_t STEAMID64_CONST = 76561197960265728; // 0x110000100000000

// - Custom macros
#define GET_PEV(thisptr) *reinterpret_cast<entvars_t**>(reinterpret_cast<uintptr_t>(thisptr) + off_pev);
#define GET_PEV(thisptr) *reinterpret_cast<entvars_t**>(reinterpret_cast<uintptr_t>(thisptr) + off_pev);
#define SET_OR_UNSET_FLAG(boolVar, setVar, flag) boolVar ? (setVar |= flag) : (setVar &= ~flag);
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
#define SET_OR_UNSET_FLAG(boolVar, setVar, flag) boolVar ? (setVar |= flag) : (setVar &= ~flag);
#define SET_OR_UNSET_FLAG(boolVar, setVar, flag) (boolVar) ? (setVar |= (flag)) : (setVar &= ~(flag));

Otherwise it's possible to get unintended splitting.


// - Custom enums
typedef enum
{
BXT_ADDTOFULLPACK_STATE_NO = 0,
BXT_ADDTOFULLPACK_STATE_HIDDEN_ENTITIES,
BXT_ADDTOFULLPACK_STATE_HIDDEN_ENTITIES_CLIENTSIDE,
BXT_ADDTOFULLPACK_STATE_TRIGGERS
} BXT_ENUM_ADDTOFULLPACK;