Skip to content

Commit c63be3d

Browse files
author
Alex Cole
committed
Revert OnPlayerConnect vandalism.
1 parent 59c5b09 commit c63be3d

File tree

2 files changed

+40
-32
lines changed

2 files changed

+40
-32
lines changed

Server/Components/Pawn/Manager/Manager.cpp

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ void PawnManager::ProcessTick(Microseconds elapsed, TimePoint now)
365365
if (nextRestart_ != TimePoint::min() && nextRestart_ <= now)
366366
{
367367
// Reloading a script. Restart is in the past, load the next GM.
368-
Load(mainName_, true, true);
368+
Load(mainName_, true);
369369
nextRestart_ = TimePoint::min();
370370
}
371371
if (mainScript_ && nextSleep_ != TimePoint::min() && nextSleep_ <= now)
@@ -441,7 +441,7 @@ bool PawnManager::Load(DynamicArray<StringView> const& mainScripts)
441441
return Load("gamemodes/" + gamemodes_[0], true);
442442
}
443443

444-
void PawnManager::openAMX(PawnScript& script, bool isEntryScript, bool restarting)
444+
void PawnManager::openAMX(PawnScript& script, bool isEntryScript)
445445
{
446446
script.Register("CallLocalFunction", &utils::pawn_Script_Call);
447447
script.Register("Script_CallByIndex", &utils::pawn_Script_CallByIndex);
@@ -529,25 +529,17 @@ void PawnManager::openAMX(PawnScript& script, bool isEntryScript, bool restartin
529529
script.cache_.inited = true;
530530
}
531531

532-
for (auto const p : players->entries())
533-
{
534-
// Call OnScriptUnloadPlayer for any pawn script that is being loaded, this way people can
535-
// Make use of this callback for resetting variables, or initializing anything player related.
536-
// First paramter is obviously player ID, and second parameter is a boolean determining whether it's
537-
// An entry script (main script) or a side script
538-
script.Call("OnScriptLoadPlayer", DefaultReturnValue_True, p->getID(), isEntryScript);
532+
// Assume that all initialisation and header mangling is now complete, and that it is safe to
533+
// cache public pointers.
539534

540-
// If it's entry script and it's restarting, after loading we call OnPlayerConnect in all scripts
541-
// Regardless of their types, as if players have rejoined the server. This is also what SA-MP does.
542-
if (isEntryScript && restarting)
543-
{
544-
script.Call("OnPlayerConnect", DefaultReturnValue_True, p->getID());
545-
CallInSides("OnPlayerConnect", DefaultReturnValue_True, p->getID());
546-
}
535+
// Call `OnPlayerConnect` (can be after caching).
536+
for (auto p : players->entries())
537+
{
538+
script.Call("OnPlayerConnect", DefaultReturnValue_True, p->getID());
547539
}
548540
}
549541

550-
bool PawnManager::Load(std::string const& name, bool isEntryScript, bool restarting)
542+
bool PawnManager::Load(std::string const& name, bool isEntryScript)
551543
{
552544
std::string normal_script_name;
553545
utils::NormaliseScriptName(name, normal_script_name);
@@ -601,24 +593,40 @@ bool PawnManager::Load(std::string const& name, bool isEntryScript, bool restart
601593

602594
void PawnManager::closeAMX(PawnScript& script, bool isEntryScript)
603595
{
604-
// Call OnPlayerDisconnect on entry script close first, then we proceed to do unload player callback
605-
if (isEntryScript)
596+
AMX* amx = script.GetAMX();
597+
int idx;
598+
bool once = true;
599+
// Reason 4, to match fixes.inc. Why was it not 3? I don't know.
600+
if (amx_FindPublic(amx, "OnPlayerDisconnect", &idx) == AMX_ERR_NONE)
606601
{
607602
for (auto const p : players->entries())
608603
{
609-
PawnManager::Get()->CallInEntry("OnPlayerDisconnect", DefaultReturnValue_True, p->getID(), PeerDisconnectReason_Quit);
604+
cell ret = 1;
605+
int err = script.CallChecked(idx, ret, p->getID(), PeerDisconnectReason_ModeEnd);
606+
switch (err)
607+
{
608+
case AMX_ERR_NONE:
609+
break;
610+
case AMX_ERR_BOUNDS:
611+
// Test the `OP_BOUNDS` parameter and the current index.
612+
if (once && (*(cell*)((uintptr_t)amx->base + (((AMX_HEADER*)amx->base)->cod + amx->cip - sizeof(cell)))) == 2) // && amx->pri == 4)
613+
{
614+
core->printLn(R"(
615+
Array out-of-bounds encountered during `OnPlayerDisconnect` with reason `4`
616+
(script exit). This may be due to old code assuming the highest possible reason
617+
is `2`.
618+
)");
619+
// Only show the error once, don't spam it.
620+
once = false;
621+
break;
622+
}
623+
// Fallthrough
624+
default:
625+
core->logLn(LogLevel::Error, "%s", aux_StrError(err));
626+
break;
627+
}
610628
}
611629
}
612-
613-
// Call OnScriptUnloadPlayer for any pawn script that is being unloaded, this way people can
614-
// Make use of this callback for resetting variables, or uninitializing anything player related.
615-
// First paramter is obviously player ID, and second parameter is a boolean determining whether it's
616-
// An entry script (main script) or a side script
617-
for (auto const p : players->entries())
618-
{
619-
script.Call("OnScriptUnloadPlayer", DefaultReturnValue_True, p->getID(), isEntryScript);
620-
}
621-
622630
if (isEntryScript)
623631
{
624632
script.Call("OnGameModeExit", DefaultReturnValue_False);

Server/Components/Pawn/Manager/Manager.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ class PawnManager : public Singleton<PawnManager>, public PawnLookup
8989
});
9090
}
9191

92-
void openAMX(PawnScript& script, bool isEntryScript, bool restarting = false);
92+
void openAMX(PawnScript& script, bool isEntryScript);
9393
void closeAMX(PawnScript& script, bool isEntryScript);
9494

9595
public:
@@ -104,7 +104,7 @@ class PawnManager : public Singleton<PawnManager>, public PawnLookup
104104
void SetBasePath(std::string const& path);
105105
void SetScriptPath(std::string const& path);
106106

107-
bool Load(std::string const& name, bool primary = false, bool restarting = false);
107+
bool Load(std::string const& name, bool primary = false);
108108
bool Load(DynamicArray<StringView> const& mainScripts);
109109
bool Reload(std::string const& name);
110110
bool Unload(std::string const& name);

0 commit comments

Comments
 (0)