Skip to content

Commit

Permalink
Core/Scripts: Fixed calling UnitScript hooks
Browse files Browse the repository at this point in the history
Closes #16543

(cherry picked from commit f809e5e)
  • Loading branch information
Shauren committed Feb 11, 2016
1 parent b4b43d0 commit 7f817a4
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 27 deletions.
33 changes: 10 additions & 23 deletions src/server/game/Scripting/ScriptMgr.cpp
Expand Up @@ -37,7 +37,6 @@

// namespace
// {
UnusedScriptContainer UnusedScripts;
UnusedScriptNamesContainer UnusedScriptNames;
// }

Expand Down Expand Up @@ -107,8 +106,9 @@ class ScriptRegistry
// The actual list of scripts. This will be accessed concurrently, so it must not be modified
// after server startup.
static ScriptMap ScriptPointerList;
static std::vector<TScript*> Scripts;

static void AddScript(TScript* const script)
static void AddScript(TScript* const script, bool addToDeleteContainer = true)
{
ASSERT(script);

Expand All @@ -126,6 +126,8 @@ class ScriptRegistry
}

AddScript(is_script_database_bound<TScript>{}, script);
if (addToDeleteContainer)
Scripts.push_back(script);
}

// Gets a script by its ID (assigned by ObjectMgr).
Expand Down Expand Up @@ -186,11 +188,6 @@ class ScriptRegistry
{
// The script uses a script name from database, but isn't assigned to anything.
TC_LOG_ERROR("sql.sql", "Script named '%s' does not have a script name assigned in database.", script->GetName().c_str());

// Avoid calling "delete script;" because we are currently in the script constructor
// In a valid scenario this will not happen because every script has a name assigned in the database
UnusedScripts.push_back(script);
return;
}
}

Expand All @@ -210,6 +207,7 @@ class ScriptRegistry
#define SCR_REG_MAP(T) ScriptRegistry<T>::ScriptMap
#define SCR_REG_ITR(T) ScriptRegistry<T>::ScriptMapIterator
#define SCR_REG_LST(T) ScriptRegistry<T>::ScriptPointerList
#define SCR_REG_VEC(T) ScriptRegistry<T>::Scripts

// Utility macros for looping over scripts.
#define FOR_SCRIPTS(T, C, E) \
Expand Down Expand Up @@ -266,17 +264,15 @@ void ScriptMgr::Initialize()
}
#endif

UnloadUnusedScripts();

TC_LOG_INFO("server.loading", ">> Loaded %u C++ scripts in %u ms", GetScriptCount(), GetMSTimeDiffToNow(oldMSTime));
}

void ScriptMgr::Unload()
{
#define SCR_CLEAR(T) \
for (SCR_REG_ITR(T) itr = SCR_REG_LST(T).begin(); itr != SCR_REG_LST(T).end(); ++itr) \
delete itr->second; \
SCR_REG_LST(T).clear();
for (T* scr : SCR_REG_VEC(T)) \
delete scr; \
SCR_REG_VEC(T).clear();

// Clear scripts for every script type.
SCR_CLEAR(SpellScriptLoader);
Expand Down Expand Up @@ -308,19 +304,10 @@ void ScriptMgr::Unload()

#undef SCR_CLEAR

UnloadUnusedScripts();

delete[] SpellSummary;
delete[] UnitAI::AISpellInfo;
}

void ScriptMgr::UnloadUnusedScripts()
{
for (size_t i = 0; i < UnusedScripts.size(); ++i)
delete UnusedScripts[i];
UnusedScripts.clear();
}

void ScriptMgr::LoadDatabase()
{
sScriptSystemMgr->LoadScriptWaypoints();
Expand Down Expand Up @@ -1555,8 +1542,7 @@ FormulaScript::FormulaScript(const char* name)
UnitScript::UnitScript(const char* name, bool addToScripts)
: ScriptObject(name)
{
if (addToScripts)
ScriptRegistry<UnitScript>::AddScript(this);
ScriptRegistry<UnitScript>::AddScript(this, addToScripts);
}

WorldMapScript::WorldMapScript(const char* name, uint32 mapId)
Expand Down Expand Up @@ -1696,6 +1682,7 @@ GroupScript::GroupScript(const char* name)

// Instantiate static members of ScriptRegistry.
template<class TScript> std::map<uint32, TScript*> ScriptRegistry<TScript>::ScriptPointerList;
template<class TScript> std::vector<TScript*> ScriptRegistry<TScript>::Scripts;
template<class TScript> uint32 ScriptRegistry<TScript>::_scriptIdCounter = 0;

// Specialize for each script type class like so:
Expand Down
4 changes: 0 additions & 4 deletions src/server/game/Scripting/ScriptMgr.h
Expand Up @@ -843,10 +843,7 @@ class GroupScript : public ScriptObject

// namespace
// {
typedef std::vector<ScriptObject*> UnusedScriptContainer;
typedef std::list<std::string> UnusedScriptNamesContainer;

extern UnusedScriptContainer UnusedScripts;
extern UnusedScriptNamesContainer UnusedScriptNames;
// }

Expand Down Expand Up @@ -878,7 +875,6 @@ class ScriptMgr
public: /* Unloading */

void Unload();
void UnloadUnusedScripts();

public: /* SpellScriptLoader */

Expand Down

0 comments on commit 7f817a4

Please sign in to comment.