Skip to content

Commit 7b85b68

Browse files
ImAciidzThisAMJ
authored andcommitted
fixup: clean up some trolling from the previous PR
This was meant to be a force push on the previous PR, but then the PR was merged 8 minutes before I went to go message AMJ to tell him not to merge yet (oops lol). Anyways, made the thing in LPHud early return, demystified some memes with the MsgFunc_SayText(2) hook, and mostly undid/cleaned up the changes to Interface::GetPtr in favor of a simpler solution. (Related to that, the console module is now initialized first before any other modules, so that it is available immediately within the plugin.)
1 parent 55bbef2 commit 7b85b68

File tree

8 files changed

+55
-49
lines changed

8 files changed

+55
-49
lines changed

src/Features/Hud/LPHud.cpp

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,21 @@ static int getCurrentCount() {
3939
// Get the current total portal count as reported by the the players'
4040
// m_StatsThisLevel values
4141
static int getStatsCount() {
42-
if (!sar.game->Is(SourceGame_Portal2_2011)) {
43-
int total = 0;
44-
45-
int slots = engine->GetMaxClients() >= 2 ? 2 : 1;
46-
for (int slot = 0; slot < slots; ++slot) {
47-
ClientEnt *player = client->GetPlayer(slot + 1);
48-
if (!player) continue;
49-
total += player->field<int>("iNumPortalsPlaced");
50-
}
51-
52-
return total;
53-
} else {
42+
// this field doesn't seem to exist in 2011 Portal 2
43+
if (sar.game->Is(SourceGame_Portal2_2011)) {
5444
return 0;
5545
}
46+
47+
int total = 0;
48+
49+
int slots = engine->GetMaxClients() >= 2 ? 2 : 1;
50+
for (int slot = 0; slot < slots; ++slot) {
51+
ClientEnt *player = client->GetPlayer(slot + 1);
52+
if (!player) continue;
53+
total += player->field<int>("iNumPortalsPlaced");
54+
}
55+
56+
return total;
5657
}
5758

5859
// Calculate an up-to-date total portal count using the player stats and

src/Game.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ Game *Game::CreateNew() {
7979

8080
if (Utils::ICompare(modDir, Portal2::ModDir())) {
8181

82-
void *engineClient = Interface::GetPtr(MODULE("engine"), "VEngineClient015", false);
82+
void *engineClient = Interface::GetPtr(MODULE("engine"), "VEngineClient015");
8383
if (engineClient) {
8484
return new Portal2();
8585
} else {

src/Interface.cpp

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ void Interface::Delete(Interface *ptr) {
7373
ptr = nullptr;
7474
}
7575
}
76-
void *Interface::GetPtr(const char *filename, const char *interfaceSymbol, bool shouldTryLowerVersion) {
76+
void *Interface::GetPtr(const char *filename, const char *interfaceSymbol) {
7777
auto handle = Memory::GetModuleHandleByName(filename);
7878
if (!handle) {
7979
console->DevWarning("SAR: Failed to open module %s!\n", filename);
@@ -92,16 +92,12 @@ void *Interface::GetPtr(const char *filename, const char *interfaceSymbol, bool
9292
void *fn = CreateInterface(interfaceSymbol, &ret);
9393

9494
if (ret) {
95-
uintptr_t CreateInterfaceInternal;
96-
InterfaceReg *s_pInterfaceRegs;
97-
// hacky: if offsets aren't initialized then just hardcode the offsets
98-
// (this is currently only for differentiating 2011/latest p2)
95+
uintptr_t CreateInterfaceInternal = 0;
96+
InterfaceReg *s_pInterfaceRegs = nullptr;
97+
// make sure offsets are initialized so the GetPtr call for detecting 2011 p2 doesn't explode
9998
if (Offsets::CreateInterfaceInternal && Offsets::s_pInterfaceRegs) {
10099
CreateInterfaceInternal = Memory::Read((uintptr_t)CreateInterface + Offsets::CreateInterfaceInternal);
101100
s_pInterfaceRegs = Memory::DerefDeref<InterfaceReg *>(CreateInterfaceInternal + Offsets::s_pInterfaceRegs);
102-
} else {
103-
CreateInterfaceInternal = Memory::Read((uintptr_t)CreateInterface + 5);
104-
s_pInterfaceRegs = Memory::DerefDeref<InterfaceReg *>(CreateInterfaceInternal + 6);
105101
}
106102

107103
if (!CreateInterfaceInternal || !s_pInterfaceRegs) {
@@ -117,7 +113,7 @@ void *Interface::GetPtr(const char *filename, const char *interfaceSymbol, bool
117113
}
118114
}
119115

120-
if (!fn && shouldTryLowerVersion) {
116+
if (!fn) {
121117
// Interface012 -> Interface0
122118
// Try to get any version of the interface
123119
if (interfaceSymbol[std::strlen(interfaceSymbol) - 3] >= '0' && interfaceSymbol[std::strlen(interfaceSymbol) - 3] <= '9') {

src/Interface.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class Interface {
6161
static Interface *Create(void *ptr, bool copyVtable = true, bool autoHook = true);
6262
static Interface *Create(const char *filename, const char *interfaceSymbol, bool copyVtable = true, bool autoHook = true);
6363
static void Delete(Interface *ptr);
64-
static void *GetPtr(const char *filename, const char *interfaceSymbol, bool shouldTryLowerVersion = true);
64+
static void *GetPtr(const char *filename, const char *interfaceSymbol);
6565

6666
template <typename T = void *>
6767
static T Get(const char *filename, const char *interfaceSymbol) {

src/Modules/Client.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ DETOUR(Client::MsgFunc_SayText2, bf_read &msg) {
500500
if (!c) break;
501501
}
502502
} else if (sar.game->Is(SourceGame_PortalReloaded | SourceGame_Portal2_2011)) {
503-
// Reloaded uses the legacy format where it's just one string
503+
// Reloaded and 2011 Portal 2 use the legacy format where it's just one string
504504
while (true) {
505505
char c = (char)(uint8_t)msg.ReadUnsigned(8);
506506
if (!c) break;
@@ -1009,7 +1009,8 @@ bool Client::Init() {
10091009
this->g_HudChat->Hook(Client::MsgFunc_SayText2_Hook, Client::MsgFunc_SayText2, Offsets::MsgFunc_SayText2);
10101010
this->g_HudChat->Hook(Client::GetTextColorForClient_Hook, Client::GetTextColorForClient, Offsets::GetTextColorForClient);
10111011
} else if (sar.game->Is(SourceGame_PortalReloaded | SourceGame_Portal2_2011)) {
1012-
this->g_HudChat->Hook(Client::MsgFunc_SayText2_Hook, Client::MsgFunc_SayText2, Offsets::MsgFunc_SayTextReloaded);
1012+
// This hooks SayText, not SayText2, but the function signature is compatible
1013+
this->g_HudChat->Hook(Client::MsgFunc_SayText2_Hook, Client::MsgFunc_SayText2, Offsets::MsgFunc_SayText);
10131014
}
10141015
} else {
10151016
console->DevWarning("Failed to hook CHudChat\n");

src/Offsets/Portal 2 4554.hpp

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ SIGSCAN_WINDOWS(MatrixBuildRotationAboutAxis, "55 8B EC 51 F3 0F 10 45 ? 0F 5A C
99
// stolen from Infra
1010
SIGSCAN_WINDOWS(DrawTranslucentRenderables, "55 8B EC 81 EC 80 00 00 00 53 56 8B F1")
1111

12-
// same as something else but I found myself
12+
// same as something else
1313
OFFSET_WINDOWS(HostState_OnClientConnected, 695)
1414

1515
OFFSET_WINDOWS(gamerules, 5)
@@ -39,14 +39,18 @@ OFFSET_WINDOWS(GetSteamAPIContext, 176)
3939
//OFFSET_WINDOWS(HudUpdate, 11) // found, if needed
4040
//OFFSET_DEFAULT(IN_ActivateMouse, 15, 15) // verified
4141
//OFFSET_DEFAULT(IN_DeactivateMouse, 16, 16) // verified
42-
OFFSET_WINDOWS(ApplyMouse, 53) // found myself
42+
OFFSET_WINDOWS(ApplyMouse, 53) // found
4343
OFFSET_EMPTY(SteamControllerMove) // THIS DOES NOT EXIST IN 2011!!!! (because the Steam Controller didn't exist in 2011)
4444
//OFFSET_WINDOWS(JoyStickApplyMovement, 60) // found, if needed
4545
//OFFSET_DEFAULT(LevelInitPreEntity, 5, 5) // verified
4646

4747
// CVEngineServer
4848
// all good
4949

50+
// CHudChat/CBaseHudChat
51+
//OFFSET_WINDOWS(MsgFunc_SayText2, 27) // unused, see comment in latest P2 offsets file
52+
//OFFSET_WINDOWS(GetTextColorForClient, 32) // unused but ye
53+
5054
// vgui stuff, pretty sure I just stole these from another version, no idea if they are correct,
5155
// but the game doesn't crash.
5256
OFFSET_WINDOWS(DrawColoredCircle, 158)
@@ -58,8 +62,8 @@ OFFSET_WINDOWS(UncacheUnusedMaterials, 76) // confirmed
5862
OFFSET_WINDOWS(CreateMaterial, 80) // confirmed
5963
OFFSET_WINDOWS(FindMaterial, 81) // confirmed
6064
OFFSET_WINDOWS(CreateProceduralTexture, 89) // confirmed
61-
OFFSET_WINDOWS(GetRenderContext, 111) // found myself
62-
OFFSET_WINDOWS(RemoveMaterial, 153) // found myself
65+
OFFSET_WINDOWS(GetRenderContext, 111) // found
66+
OFFSET_WINDOWS(RemoveMaterial, 153) // found
6367

6468
// CEngineTool
6569
//OFFSET_DEFAULT(GetCurrentMap, 25, 26) // good
@@ -77,22 +81,22 @@ OFFSET_WINDOWS(PrecacheModel, 62)
7781
SIGSCAN_WINDOWS(ViewPunch, "55 8B EC A1 ? ? ? ? 83 EC 0C 83 78 ? 00 56 8B F1") // this works for 9568 too, maybe shorten that pattern/adjust to match this?
7882

7983
// CBasePlayer
80-
OFFSET_WINDOWS(m_pShadowStand, 3156) // found myself
81-
OFFSET_WINDOWS(m_pShadowCrouch, 3160) // found myself
84+
OFFSET_WINDOWS(m_pShadowStand, 3156) // found
85+
OFFSET_WINDOWS(m_pShadowCrouch, 3160) // found
8286

83-
OFFSET_WINDOWS(viewangles, 19112) // found myself
87+
OFFSET_WINDOWS(viewangles, 19112) // found
8488

8589
// CBaseEntity
86-
OFFSET_WINDOWS(AcceptInput, 39) // found myself
87-
OFFSET_WINDOWS(IsPlayer, 84) // found myself
90+
OFFSET_WINDOWS(AcceptInput, 39) // found
91+
OFFSET_WINDOWS(IsPlayer, 84) // found
8892

8993
// CPortal_Player
9094
//OFFSET_WINDOWS(GetPaintPower, 2) // this seems to be fine as is
91-
OFFSET_WINDOWS(PlayerRunCommand, 452) // found myself
92-
OFFSET_WINDOWS(UseSpeedPower, 508) // found myself
95+
OFFSET_WINDOWS(PlayerRunCommand, 452) // found
96+
OFFSET_WINDOWS(UseSpeedPower, 508) // found
9397

9498
// CSteam3Client
95-
OFFSET_WINDOWS(OnGameOverlayActivated, 92)
99+
OFFSET_WINDOWS(OnGameOverlayActivated, 92) // found
96100

97101
// IPhysicsObject
98102
// everything (should) be fine.
@@ -105,7 +109,7 @@ OFFSET_EMPTY(interfaceMgrOff)
105109
// all same as current P2
106110

107111
// CDemoPlayer
108-
OFFSET_WINDOWS(SkipToTick, 14) // found myself
112+
OFFSET_WINDOWS(SkipToTick, 14) // found
109113
// rest are good
110114

111115
// stolen
@@ -117,13 +121,13 @@ OFFSET_WINDOWS(m_bLoadGame, 440)
117121

118122
SIGSCAN_WINDOWS(readConsoleCommandInjectSig, "8B 45 F4 50 68 ? 04 00 00 68 ? ? ? ? 8D 4D 90 E8 ? ? ? ? 8D 4F 04 E8") // needed to mask one byte
119123

120-
OFFSET_WINDOWS(net_time, 21) // found myself
124+
OFFSET_WINDOWS(net_time, 21) // found
121125

122-
OFFSET_WINDOWS(PerUserInput_tSize, 212) // found myself
123-
OFFSET_WINDOWS(m_pCommands, 224) // found myself
126+
OFFSET_WINDOWS(PerUserInput_tSize, 212) // found
127+
OFFSET_WINDOWS(m_pCommands, 224) // found
124128
// multiplayer_backup is good
125129

126130
// check/refind these
127-
OFFSET_WINDOWS(m_pSurfaceData, 4088) // found myself but unsure
128-
OFFSET_WINDOWS(S_m_surfaceFriction, 4092) // found myself
129-
OFFSET_WINDOWS(C_m_surfaceFriction, 5532) // found myself
131+
OFFSET_WINDOWS(m_pSurfaceData, 4088) // found but unsure
132+
OFFSET_WINDOWS(S_m_surfaceFriction, 4092) // found
133+
OFFSET_WINDOWS(C_m_surfaceFriction, 5532) // found

src/Offsets/Portal 2 9568.hpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,9 +246,13 @@ OFFSET_DEFAULT(ClientCommand, 39, 39)
246246
OFFSET_DEFAULT(ClientCommandKeyValues, 135, 135)
247247
OFFSET_DEFAULT(IsServerPaused, 81, 81)
248248
OFFSET_DEFAULT(ServerPause, 121, 121)
249+
250+
// CHudChat/CBaseHudChat
249251
OFFSET_DEFAULT(ChatPrintf, 22, 25)
250252
OFFSET_DEFAULT(MsgFunc_SayText2, 28, 35)
251-
OFFSET_DEFAULT(MsgFunc_SayTextReloaded, 26, 32)
253+
// Portal 2 4554 and Reloaded need this offset and use the same value for it (latest P2's offset is 27 instead of 26).
254+
// Reloaded doesn't have its own offset file, so this is defined here
255+
OFFSET_DEFAULT(MsgFunc_SayText, 26, 32)
252256
OFFSET_DEFAULT(GetTextColorForClient, 33, 41)
253257

254258
// CSteam3Client

src/SAR.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,16 @@ EXPOSE_SINGLE_INTERFACE_GLOBALVAR(SAR, IServerPluginCallbacks, INTERFACEVERSION_
2929

3030

3131
bool SAR::Load(CreateInterfaceFn interfaceFactory, CreateInterfaceFn gameServerFactory) {
32+
console = new Console();
33+
if (!console->Init())
34+
return false;
35+
3236
modules = new Modules();
3337
features = new Features();
3438
cheats = new Cheats();
3539
plugin = new Plugin();
3640
game = Game::CreateNew();
3741

38-
console = new Console();
39-
if (!console->Init())
40-
return false;
41-
4242
#ifdef _WIN32
4343
// The auto-updater can create this file on Windows; we should try
4444
// to delete it.

0 commit comments

Comments
 (0)