Skip to content

Commit

Permalink
fix(Core/Scripts): Fix build and crash when calling uninitialized scr…
Browse files Browse the repository at this point in the history
…ipts (azerothcore#18718)

fix(Core/Scripts): Fix build and crash when calling uninitialized scripts.
  • Loading branch information
walkline authored Apr 12, 2024
1 parent f05200f commit 3a6231c
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/server/game/Scripting/ScriptDefines/AccountScript.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#define SCRIPT_OBJECT_ACCOUNT_SCRIPT_H_

#include "ScriptObject.h"
#include <vector>

enum AccountHook
{
Expand Down
1 change: 1 addition & 0 deletions src/server/game/Scripting/ScriptDefines/ArenaScript.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#include "ObjectGuid.h"
#include "ScriptObject.h"
#include <vector>

enum ArenaHook
{
Expand Down
1 change: 1 addition & 0 deletions src/server/game/Scripting/ScriptDefines/ArenaTeamScript.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#include "Battleground.h"
#include "ScriptObject.h"
#include <vector>

enum ArenaTeamHook
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#define SCRIPT_OBJECT_AUCTION_HOUSE_SCRIPT_H_

#include "ScriptObject.h"
#include <vector>

enum AuctionHouseHook
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#include "Object.h"
#include "ScriptObject.h"
#include <vector>

enum MovementHook
{
Expand Down
6 changes: 6 additions & 0 deletions src/server/game/Scripting/ScriptMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ void ScriptMgr::Initialize()

_script_loader_callback();
_modules_loader_callback();

ScriptRegistry<PlayerScript>::InitEnabledHooksIfNeeded(PLAYERHOOK_END);
ScriptRegistry<AuctionHouseScript>::InitEnabledHooksIfNeeded(AUCTIONHOUSEHOOK_END);
ScriptRegistry<ArenaTeamScript>::InitEnabledHooksIfNeeded(ARENATEAMHOOK_END);
ScriptRegistry<ArenaScript>::InitEnabledHooksIfNeeded(ARENAHOOK_END);
ScriptRegistry<MovementHandlerScript>::InitEnabledHooksIfNeeded(MOVEMENTHOOK_END);
}

void ScriptMgr::Unload()
Expand Down
7 changes: 6 additions & 1 deletion src/server/game/Scripting/ScriptMgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,11 @@ class ScriptRegistry
// With this approach, we wouldn't call all available hooks in case if we override just one hook.
static EnabledHooksVector EnabledHooks;

static void InitEnabledHooksIfNeeded(uint16 totalAvailableHooks)
{
EnabledHooks.resize(totalAvailableHooks);
}

static void AddScript(TScript* const script, std::vector<uint16> enabledHooks = {})
{
ASSERT(script);
Expand All @@ -733,7 +738,7 @@ class ScriptRegistry
return;

if (EnabledHooks.empty())
EnabledHooks.resize(script->GetTotalAvailableHooks());
InitEnabledHooksIfNeeded(script->GetTotalAvailableHooks());

if (script->isAfterLoadScript())
{
Expand Down

0 comments on commit 3a6231c

Please sign in to comment.