Skip to content
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
2 changes: 1 addition & 1 deletion docs/cvars.md
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,7 @@
|sar_twitch_chat_channel||The Twitch channel to connect to.|
|sar_twitch_chat_color|255 255 255|The color of the Twitch chat messages.|
|sar_twitch_chat_enabled|0|Enables Twitch chat integration. 2 enables spectator command !spec|
|sar_unlocked_chapters|-1|Max unlocked chapter.|
|sar_unlocked_chapters|-1|Max unlocked chapter. Also unlocks the maps of coop courses up to this number in the challenge mode list.|
|sar_update|cmd|sar_update [release\|pre\|canary] [exit\|restart] [force] - update SAR to the latest version. If exit is given, exit the game upon successful update; if force is given, always re-install, even if it may be a downgrade|
|sar_velocitygraph|0|Shows velocity graph.|
|sar_velocitygraph_background|0|Background of velocity graph.|
Expand Down
116 changes: 115 additions & 1 deletion src/Modules/Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ Variable sar_patch_minor_angle_decay("sar_patch_minor_angle_decay", "0", "Patche
#endif
);

Variable sar_unlocked_chapters("sar_unlocked_chapters", "-1", "Max unlocked chapter.\n");
Variable sar_unlocked_chapters("sar_unlocked_chapters", "-1", "Max unlocked chapter. Also unlocks the maps of coop courses up to this number in the challenge mode list.\n");

Variable sar_portalcolor_enable("sar_portalcolor_enable", "0", "Enable custom portal colors.\n");
Variable sar_portalcolor_sp_1("sar_portalcolor_sp_1", "64 160 255", "Portal color for Chell's left portal. r_portal_fastpath 0 required.\n");
Expand Down Expand Up @@ -130,6 +130,10 @@ REDECL(Client::ApplyMouse_Mid_Continue);
#endif
REDECL(Client::DrawPortal);
REDECL(Client::GetChapterProgress);
#ifdef _WIN32
REDECL(Client::IsLevelComplete);
REDECL(Client::SetChapterAndMapInfo);
#endif


CMDECL(Client::GetAbsOrigin, Vector, m_vecAbsOrigin);
Expand Down Expand Up @@ -1014,6 +1018,91 @@ DETOUR(Client::GetChapterProgress) {
}
Hook g_GetChapterProgressHook(&Client::GetChapterProgress_Hook);

// The coop challenge mode list locks every map neither player has completed. Treat the maps of
// courses 1 to N as completed there; the real completion data is left alone.
#ifdef _WIN32
// SetChapterAndMapInfo checks completion through IsLevelComplete in game and through a title
// data helper (its only caller) outside one. Both answer true while it runs for an unlocked course.
static bool g_unlockLeaderboardMapItem = false;

extern Hook g_IsLevelCompleteHook;
DETOUR_T(bool, Client::IsLevelComplete, int branch, int level) {
if (g_unlockLeaderboardMapItem)
return true;

g_IsLevelCompleteHook.Disable();
auto ret = Client::IsLevelComplete(thisptr, branch, level);
g_IsLevelCompleteHook.Enable();
return ret;
}
Hook g_IsLevelCompleteHook(&Client::IsLevelComplete_Hook);

using _TitleDataGetBool = bool(__cdecl *)(void *field, void *titleData);
static _TitleDataGetBool TitleDataGetBool;

extern Hook g_TitleDataGetBoolHook;
static bool __cdecl TitleDataGetBool_Hook(void *field, void *titleData) {
if (g_unlockLeaderboardMapItem)
return true;

g_TitleDataGetBoolHook.Disable();
auto ret = TitleDataGetBool(field, titleData);
g_TitleDataGetBoolHook.Enable();
return ret;
}
Hook g_TitleDataGetBoolHook(&TitleDataGetBool_Hook);

extern Hook g_SetChapterAndMapInfoHook;
DETOUR_T(void, Client::SetChapterAndMapInfo, int chapter, int map, int a4, int chapterProgress) {
g_unlockLeaderboardMapItem = chapter <= sar_unlocked_chapters.GetInt();

g_SetChapterAndMapInfoHook.Disable();
Client::SetChapterAndMapInfo(thisptr, chapter, map, a4, chapterProgress);
g_SetChapterAndMapInfoHook.Enable();

g_unlockLeaderboardMapItem = false;
}
Hook g_SetChapterAndMapInfoHook(&Client::SetChapterAndMapInfo_Hook);
#else
// Linux inlines that helper into SetChapterAndMapInfo, which also takes its arguments in registers.
// Both of its completion checks meet at "mov eax, [ebp]; mov ebx, [eax + 0x28]; test dl, dl" with
// the result in dl and the map item in esi, so those first 6 bytes become a call to this stub. It
// redoes them, then sets dl for maps of unlocked courses (the item's chapter is at +0x1A0).
Memory::Patch *g_leaderboardMapItemPatch;

extern "C" {
__attribute__((used)) static bool LeaderboardMapItemUnlocked(int chapter) {
return chapter <= sar_unlocked_chapters.GetInt();
}
__attribute__((visibility("hidden"))) void LeaderboardMapItemComplete_Stub();
}

asm(R"(
.text
.globl LeaderboardMapItemComplete_Stub
.hidden LeaderboardMapItemComplete_Stub
.p2align 4
LeaderboardMapItemComplete_Stub:
.intel_syntax noprefix
mov eax, [ebp]
mov ebx, [eax + 0x28]
test dl, dl
jnz 1f
push ebp
mov ebp, esp
and esp, -16
sub esp, 12
push dword ptr [esi + 0x1A0]
call LeaderboardMapItemUnlocked
mov esp, ebp
pop ebp
mov edx, eax
1:
ret
.att_syntax prefix
)");
#endif

bool Client::Init() {
bool readJmp = false;
#ifdef _WIN32
Expand Down Expand Up @@ -1288,6 +1377,27 @@ bool Client::Init() {
Client::GetChapterProgress = (decltype(Client::GetChapterProgress))Memory::Scan(this->Name(), Offsets::GetChapterProgress);
g_GetChapterProgressHook.SetFunc(Client::GetChapterProgress);

if (sar.game->Is(SourceGame_Portal2 | SourceGame_Portal2_2011)) {
#ifdef _WIN32
Client::IsLevelComplete = (decltype(Client::IsLevelComplete))Memory::Scan(this->Name(), Offsets::IsLevelComplete);
Client::SetChapterAndMapInfo = (decltype(Client::SetChapterAndMapInfo))Memory::Scan(this->Name(), Offsets::SetChapterAndMapInfo);
TitleDataGetBool = (_TitleDataGetBool)Memory::Scan(this->Name(), Offsets::TitleDataGetBool);
if (Client::IsLevelComplete && Client::SetChapterAndMapInfo && TitleDataGetBool) {
g_IsLevelCompleteHook.SetFunc(Client::IsLevelComplete);
g_SetChapterAndMapInfoHook.SetFunc(Client::SetChapterAndMapInfo);
g_TitleDataGetBoolHook.SetFunc(TitleDataGetBool);
}
#else
g_leaderboardMapItemPatch = new Memory::Patch();
auto completeCheck = Memory::Scan(this->Name(), Offsets::LeaderboardMapItemComplete, Offsets::LeaderboardMapItemCompleteOff);
if (completeCheck) {
unsigned char callStub[6] = {0xE8, 0, 0, 0, 0, 0x90};
*(uint32_t *)(callStub + 1) = (uintptr_t)&LeaderboardMapItemComplete_Stub - (completeCheck + 5);
g_leaderboardMapItemPatch->Execute(completeCheck, callStub);
}
#endif
}

cl_showpos = Variable("cl_showpos");
cl_sidespeed = Variable("cl_sidespeed");
cl_forwardspeed = Variable("cl_forwardspeed");
Expand Down Expand Up @@ -1342,6 +1452,10 @@ void Client::Shutdown() {
g_drawPortalGhostPatch->Restore();
SAFE_DELETE(g_drawPortalPatch);
SAFE_DELETE(g_drawPortalGhostPatch);
#ifndef _WIN32
if (g_leaderboardMapItemPatch) g_leaderboardMapItemPatch->Restore();
SAFE_DELETE(g_leaderboardMapItemPatch);
#endif
}

Client *client;
8 changes: 8 additions & 0 deletions src/Modules/Client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,14 @@ class Client : public Module {
// CBaseModPanel::GetChapterProgress
DECL_DETOUR(GetChapterProgress);

#ifdef _WIN32
// C_PortalMPGameRules::IsLevelComplete
DECL_DETOUR_T(bool, IsLevelComplete, int branch, int level);

// CLeaderboardMapItem::SetChapterAndMapInfo
DECL_DETOUR_T(void, SetChapterAndMapInfo, int chapter, int map, int a4, int chapterProgress);
#endif

// BaseModUI::CPortalLeaderboardPanel::OnCommand
DECL_DETOUR(OnCommand, const char *a2);

Expand Down
8 changes: 8 additions & 0 deletions src/Offsets/Portal 2 10090.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,14 @@ SIGSCAN_EMPTY(FindElementSig)

SIGSCAN_DEFAULT(GetChapterProgress, "56 8B 35 ? ? ? ? 57 8B F9 FF D6 8B 10 8B C8",
"55 89 E5 57 56 53 83 EC 0C E8 ? ? ? ? 83 EC 08 8B 10")
SIGSCAN_DEFAULT(IsLevelComplete, "55 8B EC 8B 55 0C 83 FA 0F 77 ? 8B 45 08 83 F8 05 77 ? C1 E0 04 03 C2 80 BC 08 B5 19 00 00 00",
"") // "#COOP_MAP_NAME_TR%d_MAP%d" xref -> CLeaderboardMapItem::SetChapterAndMapInfo -> in-game branch -> C_PortalMPGameRules::IsLevelComplete (Linux: covered by LeaderboardMapItemComplete)
SIGSCAN_DEFAULT(SetChapterAndMapInfo, "55 8B EC 81 EC 20 02 00 00 53 8B 5D 08 57 8B F9 89 7D F8 85 DB 0F 8E ? ? ? ? 8B 4D 0C 85 C9 0F 8E",
"") // "#COOP_MAP_NAME_TR%d_MAP%d" xref -> CLeaderboardMapItem::SetChapterAndMapInfo (Linux: custom calling convention)
SIGSCAN_DEFAULT(TitleDataGetBool, "55 8B EC 8B 45 08 8B 4D 0C 8B 11 8B 52 14 56 8B 70 0C 8B 40 04 57 50 FF D2",
"") // CLeaderboardMapItem::SetChapterAndMapInfo -> "MP.complete.%s" title data bit read (Linux: inlined)
SIGSCAN_DEFAULT(LeaderboardMapItemComplete, "", "83 E2 01 8B 2D ? ? ? ? 8B 45 00 8B 58 28 84 D2 74 ? FF B6 A8 01 00 00") // CLeaderboardMapItem::SetChapterAndMapInfo -> where its two completion checks meet
OFFSET_DEFAULT(LeaderboardMapItemCompleteOff, 0, 9)

SIGSCAN_DEFAULT(LoadingProgress__SetupControlStatesInstruction, "66 C7 86 ? ? ? ? ? ? EB", "66 89 83 ? ? ? ? E9 ? ? ? ? ? ? ? ? ? ? ? 85 F6") // "vgui/loading_screens/loadingscreen_coop" xref -> LoadingProgress::SetupControlStates -> m_bDrawProgress set to 0
OFFSET_DEFAULT(LoadingProgress__SetupControlStatesBoolOffset, 7, 5)
Expand Down
Loading