Skip to content

Commit ff1cfbf

Browse files
committed
feat: enable toggling promo coop items
Linux support is needed, as well as adding multiple arguments for the command autofill.
1 parent 91ab8fc commit ff1cfbf

File tree

4 files changed

+61
-1
lines changed

4 files changed

+61
-1
lines changed

src/Cheats.cpp

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ Variable sar_patch_cfg("sar_patch_cfg", "0", 0, 1, "Patches Crouch Flying Glitch
5050
Variable sar_prevent_ehm("sar_prevent_ehm", "0", 0, 1, "Prevents Entity Handle Misinterpretation (EHM) from happening.\n");
5151
Variable sar_disable_weapon_sway("sar_disable_weapon_sway", "0", 0, 1, "Disables the viewmodel lagging behind.\n");
5252
Variable sar_disable_viewmodel_shadows("sar_disable_viewmodel_shadows", "0", 0, 1, "Disables the shadows on the viewmodel.\n");
53-
Variable sar_floor_reportals("sar_floor_reportals", "0", "Toggles floor reportals. Requires cheats.\n", FCVAR_CHEAT);
53+
Variable sar_floor_reportals("sar_floor_reportals", "0", "Toggles floor reportals.\n");
5454
Variable sar_loads_coop_dots("sar_loads_coop_dots", "0", "Toggles the loading screen dots during map transitions in coop.\n");
5555
Variable sar_disable_autograb("sar_disable_autograb", "0", 0, 1, "Disables the auto-grab in coop. Requires host to enable it for everyone that also enables it.\n");
5656

@@ -65,6 +65,9 @@ Variable mm_session_sys_delay_create_host;
6565
Variable hide_gun_when_holding;
6666
Variable r_flashlightbrightness;
6767

68+
int origPortal2PromoFlagsValue = 0; // By default, nothing is loaded.
69+
int *g_nPortal2PromoFlags = nullptr;
70+
6871
// TSP only
6972
void IN_BhopDown(const CCommand& args) {
7073
if (!client->KeyDown || !client->in_jump) return;
@@ -399,6 +402,8 @@ void Cheats::Init() {
399402
g_autoGrabPatchClient->Restore();
400403
}
401404

405+
g_nPortal2PromoFlags = *reinterpret_cast<int **>(Memory::Scan(MODULE("server"), Offsets::Portal2PromoFlags_ADDR, 2)); // Note: Has to be active before map loads.
406+
402407
Variable::RegisterAll();
403408
Command::RegisterAll();
404409
}
@@ -589,3 +594,48 @@ void Cheats::CheckAutoGrab() {
589594
g_autoGrabPatchClient->Restore();
590595
}
591596
}
597+
598+
DECL_AUTO_COMMAND_COMPLETION(sar_set_promo_items_state, ({"skins", "helmet", "antenna"})) // TODO: Add support for autofilling multiple args.
599+
CON_COMMAND_F_COMPLETION(sar_set_promo_items_state, "Enables coop promotional items on spawn.", FCVAR_CHEAT, AUTOCOMPLETION_FUNCTION(sar_set_promo_items_state)) {
600+
if (!g_nPortal2PromoFlags) {
601+
console->Print("Could not find PromoFlags global!\n");
602+
return;
603+
}
604+
static bool hasCheckedOriginalValue = false;
605+
if (!hasCheckedOriginalValue) {
606+
origPortal2PromoFlagsValue = *g_nPortal2PromoFlags;
607+
hasCheckedOriginalValue = true;
608+
}
609+
bool bSkin = (_stricmp(args[1], "skins") == 0);
610+
bool bHelmet = (_stricmp(args[1], "helmet") == 0);
611+
bool bAntenna = (_stricmp(args[1], "antenna") == 0);
612+
if (!bSkin && !bHelmet && !bAntenna) {
613+
console->Print("Invalid first argument.\n");
614+
return;
615+
}
616+
bool bAdding = (_stricmp(args[2], "add") == 0);
617+
bool bRemoving = (_stricmp(args[2], "remove") == 0);
618+
if (!bAdding && !bRemoving) {
619+
console->Print("Invalid second argument.\n");
620+
return;
621+
}
622+
if (bSkin) {
623+
if (bAdding) {
624+
*g_nPortal2PromoFlags |= (1 << 0);
625+
} else if (bRemoving) {
626+
*g_nPortal2PromoFlags &= ~(1 << 0);
627+
}
628+
} else if (bHelmet) {
629+
if (bAdding) {
630+
*g_nPortal2PromoFlags |= (1 << 1);
631+
} else if (bRemoving) {
632+
*g_nPortal2PromoFlags &= ~(1 << 1);
633+
}
634+
} else if (bAntenna) {
635+
if (bAdding) {
636+
*g_nPortal2PromoFlags |= (1 << 2);
637+
} else if (bRemoving) {
638+
*g_nPortal2PromoFlags &= ~(1 << 2);
639+
}
640+
}
641+
}

src/Cheats.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,6 @@ extern Variable hide_gun_when_holding;
5050
extern Variable r_flashlightbrightness;
5151

5252
extern Command sar_togglewait;
53+
54+
extern int origPortal2PromoFlagsValue;
55+
extern int *g_nPortal2PromoFlags;

src/Modules/Server.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,9 @@ DETOUR(Server::PlayerRunCommand, CUserCmd *cmd, void *moveHelper) {
285285

286286
Cheats::AutoStrafe(slot, thisptr, cmd);
287287
Cheats::CheckFloorReportals();
288+
if (!sv_cheats.GetBool()) {
289+
*g_nPortal2PromoFlags = origPortal2PromoFlagsValue; // We only want to check this once per map load, to preserve the intended behavior.
290+
}
288291

289292
inputHud.SetInputInfo(slot, cmd->buttons, {cmd->sidemove, cmd->forwardmove, cmd->upmove});
290293

src/Offsets/Portal 2 9568.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,10 @@ SIGSCAN_DEFAULT(FloorReportalBranch, "75 7D 8B 8E C0 04 00 00",
523523
SIGSCAN_DEFAULT(CPortal_Player__PollForUseEntity_CheckMP, "74 ? ? ? 8B 82 ? ? ? ? FF D0 84 C0 74 ? 8B CE",
524524
"74 ? 8B 10 83 EC 0C 50 FF 92 88 00 00 00 83 C4 10 84 C0 ? ? ? ? ? ? ? ? ? ? ? ? ? 00 00") // "OnJump" xref -> CPortal_Player:PreThink -> CBasePlayer::PreThink -> CBasePlayer::ItemPreFrame -> CBasePlayer::PlayerUse -> CPortal_Player vtable offset -> CPortal_Player::PlayerUse -> Second function call from disassembly -> CPortal_Player::PollForUseEntity -> jz instruction
525525

526+
// TODO:: Linux support.
527+
SIGSCAN_DEFAULT(Portal2PromoFlags_ADDR, "F6 05 ? ? ? ? ? 74 6D",
528+
"") // CPortal_Player::GiveDefaultItems -> portal2 promo flag check
529+
526530
// VPhysics
527531
OFFSET_EMPTY(DestroyEnvironment)
528532
OFFSET_EMPTY(GetActiveEnvironmentByIndex)

0 commit comments

Comments
 (0)