AzerothCore 3.3.5a
OpenSource WoW Emulator
Loading...
Searching...
No Matches
ScriptRegistry< TScript > Class Template Reference

#include "ScriptMgr.h"

Public Types

typedef std::map< uint32, TScript * > ScriptMap
 
typedef ScriptMap::iterator ScriptMapIterator
 
typedef std::vector< std::pair< TScript *, std::vector< uint16 > > > ScriptVector
 
typedef ScriptVector::iterator ScriptVectorIterator
 
typedef std::vector< std::vector< TScript * > > EnabledHooksVector
 
typedef EnabledHooksVector::iterator EnabledHooksVectorIterator
 

Static Public Member Functions

static void InitEnabledHooksIfNeeded (uint16 totalAvailableHooks)
 
static void AddScript (TScript *const script, std::vector< uint16 > enabledHooks={})
 
static void AddALScripts ()
 
static TScript * GetScriptById (uint32 id)
 

Static Public Attributes

static ScriptMap ScriptPointerList
 
static ScriptVector ALScripts
 
static EnabledHooksVector EnabledHooks
 

Static Private Member Functions

static bool _checkMemory (TScript *const script)
 

Static Private Attributes

static uint32 _scriptIdCounter = 0
 

Detailed Description

template<class TScript>
class ScriptRegistry< TScript >

Member Typedef Documentation

◆ EnabledHooksVector

template<class TScript >
typedef std::vector<std::vector<TScript*> > ScriptRegistry< TScript >::EnabledHooksVector

◆ EnabledHooksVectorIterator

template<class TScript >
typedef EnabledHooksVector::iterator ScriptRegistry< TScript >::EnabledHooksVectorIterator

◆ ScriptMap

template<class TScript >
typedef std::map<uint32, TScript*> ScriptRegistry< TScript >::ScriptMap

◆ ScriptMapIterator

template<class TScript >
typedef ScriptMap::iterator ScriptRegistry< TScript >::ScriptMapIterator

◆ ScriptVector

template<class TScript >
typedef std::vector<std::pair<TScript*,std::vector<uint16> > > ScriptRegistry< TScript >::ScriptVector

◆ ScriptVectorIterator

template<class TScript >
typedef ScriptVector::iterator ScriptRegistry< TScript >::ScriptVectorIterator

Member Function Documentation

◆ _checkMemory()

template<class TScript >
static bool ScriptRegistry< TScript >::_checkMemory ( TScript *const  script)
inlinestaticprivate
854 {
855 // See if the script is using the same memory as another script. If this happens, it means that
856 // someone forgot to allocate new memory for a script.
857 for (ScriptMapIterator it = ScriptPointerList.begin(); it != ScriptPointerList.end(); ++it)
858 {
859 if (it->second == script)
860 {
861 LOG_ERROR("scripts", "Script '{}' has same memory pointer as '{}'.",
862 script->GetName(), it->second->GetName());
863
864 return false;
865 }
866 }
867
868 return true;
869 }
#define LOG_ERROR(filterType__,...)
Definition: Log.h:156
static ScriptMap ScriptPointerList
Definition: ScriptMgr.h:725
ScriptMap::iterator ScriptMapIterator
Definition: ScriptMgr.h:715

References LOG_ERROR, and ScriptRegistry< TScript >::ScriptPointerList.

Referenced by ScriptRegistry< TScript >::AddALScripts().

◆ AddALScripts()

template<class TScript >
static void ScriptRegistry< TScript >::AddALScripts ( )
inlinestatic
765 {
766 for (ScriptVectorIterator it = ALScripts.begin(); it != ALScripts.end(); ++it)
767 {
768 TScript* const script = (*it).first;
769
770 script->checkValidity();
771
772 if (script->IsDatabaseBound())
773 {
774 if (!_checkMemory(script))
775 {
776 return;
777 }
778
779 // Get an ID for the script. An ID only exists if it's a script that is assigned in the database
780 // through a script name (or similar).
781 uint32 id = sObjectMgr->GetScriptId(script->GetName().c_str());
782 if (id)
783 {
784 // Try to find an existing script.
785 TScript const* oldScript = nullptr;
786 for (auto iterator = ScriptPointerList.begin(); iterator != ScriptPointerList.end(); ++iterator)
787 {
788 // If the script names match...
789 if (iterator->second->GetName() == script->GetName())
790 {
791 // ... It exists.
792 oldScript = iterator->second;
793 break;
794 }
795 }
796
797 // If the script is already assigned -> delete it!
798 if (oldScript)
799 {
800 for (auto& vIt : EnabledHooks)
801 for (std::size_t i = 0; i < vIt.size(); ++i)
802 if (vIt[i] == oldScript)
803 {
804 vIt.erase(vIt.begin() + i);
805 break;
806 }
807
808 delete oldScript;
809 }
810
811 // Assign new script!
812 ScriptPointerList[id] = script;
813
814 // Increment script count only with new scripts
815 if (!oldScript)
816 {
817 sScriptMgr->IncreaseScriptCount();
818 }
819 }
820 else
821 {
822 // The script uses a script name from database, but isn't assigned to anything.
823 if (script->GetName().find("Smart") == std::string::npos)
824 LOG_ERROR("sql.sql", "Script named '{}' is not assigned in the database.",
825 script->GetName());
826 }
827 }
828 else
829 {
830 for (uint16 v : (*it).second)
831 EnabledHooks[v].emplace_back(script);
832
833 // We're dealing with a code-only script; just add it.
835 sScriptMgr->IncreaseScriptCount();
836 }
837 }
838 }
std::uint32_t uint32
Definition: Define.h:107
std::uint16_t uint16
Definition: Define.h:108
#define sScriptMgr
Definition: ScriptMgr.h:708
#define sObjectMgr
Definition: ObjectMgr.h:1623
static EnabledHooksVector EnabledHooks
Definition: ScriptMgr.h:730
static bool _checkMemory(TScript *const script)
Definition: ScriptMgr.h:853
static uint32 _scriptIdCounter
Definition: ScriptMgr.h:872
ScriptVector::iterator ScriptVectorIterator
Definition: ScriptMgr.h:718
static ScriptVector ALScripts
Definition: ScriptMgr.h:727

References ScriptRegistry< TScript >::_checkMemory(), ScriptRegistry< TScript >::_scriptIdCounter, ScriptRegistry< TScript >::ALScripts, ScriptRegistry< TScript >::EnabledHooks, LOG_ERROR, ScriptRegistry< TScript >::ScriptPointerList, sObjectMgr, and sScriptMgr.

Referenced by ScriptMgr::LoadDatabase().

◆ AddScript()

template<class TScript >
static void ScriptRegistry< TScript >::AddScript ( TScript *const  script,
std::vector< uint16 enabledHooks = {} 
)
inlinestatic
737 {})
738 {
739 ASSERT(script);
740
741 if (!_checkMemory(script))
742 return;
743
744 if (EnabledHooks.empty())
745 InitEnabledHooksIfNeeded(script->GetTotalAvailableHooks());
746
747 if (script->isAfterLoadScript())
748 {
749 ALScripts.emplace_back(script, std::move(enabledHooks));
750 }
751 else
752 {
753 script->checkValidity();
754
755 for (uint16 v : enabledHooks)
756 EnabledHooks[v].emplace_back(script);
757
758 // We're dealing with a code-only script; just add it.
760 sScriptMgr->IncreaseScriptCount();
761 }
762 }
#define ASSERT
Definition: Errors.h:68
static void InitEnabledHooksIfNeeded(uint16 totalAvailableHooks)
Definition: ScriptMgr.h:732

Referenced by AccountScript::AccountScript(), AchievementCriteriaScript::AchievementCriteriaScript(), AchievementScript::AchievementScript(), AllBattlegroundScript::AllBattlegroundScript(), AllCommandScript::AllCommandScript(), AllCreatureScript::AllCreatureScript(), AllGameObjectScript::AllGameObjectScript(), AllItemScript::AllItemScript(), AllMapScript::AllMapScript(), AllSpellScript::AllSpellScript(), AreaTriggerScript::AreaTriggerScript(), ArenaScript::ArenaScript(), ArenaTeamScript::ArenaTeamScript(), AuctionHouseScript::AuctionHouseScript(), BattlegroundMapScript::BattlegroundMapScript(), BattlegroundScript::BattlegroundScript(), CommandScript::CommandScript(), ConditionScript::ConditionScript(), CreatureScript::CreatureScript(), DatabaseScript::DatabaseScript(), DynamicObjectScript::DynamicObjectScript(), ElunaScript::ElunaScript(), FormulaScript::FormulaScript(), GameEventScript::GameEventScript(), GameObjectScript::GameObjectScript(), GlobalScript::GlobalScript(), GroupScript::GroupScript(), GuildScript::GuildScript(), InstanceMapScript::InstanceMapScript(), ItemScript::ItemScript(), LootScript::LootScript(), MailScript::MailScript(), MiscScript::MiscScript(), ModuleScript::ModuleScript(), MovementHandlerScript::MovementHandlerScript(), OutdoorPvPScript::OutdoorPvPScript(), PetScript::PetScript(), PlayerScript::PlayerScript(), ServerScript::ServerScript(), SpellScriptLoader::SpellScriptLoader(), TransportScript::TransportScript(), UnitScript::UnitScript(), VehicleScript::VehicleScript(), WeatherScript::WeatherScript(), WorldMapScript::WorldMapScript(), WorldObjectScript::WorldObjectScript(), and WorldScript::WorldScript().

◆ GetScriptById()

◆ InitEnabledHooksIfNeeded()

template<class TScript >
static void ScriptRegistry< TScript >::InitEnabledHooksIfNeeded ( uint16  totalAvailableHooks)
inlinestatic
733 {
734 EnabledHooks.resize(totalAvailableHooks);
735 }

References ScriptRegistry< TScript >::EnabledHooks.

Referenced by ScriptMgr::Initialize().

Member Data Documentation

◆ _scriptIdCounter

template<class TScript >
uint32 ScriptRegistry< TScript >::_scriptIdCounter = 0
staticprivate

◆ ALScripts

template<class TScript >
std::vector< std::pair< TScript *, std::vector< uint16 > > > ScriptRegistry< TScript >::ALScripts
static

◆ EnabledHooks

template<class TScript >
std::vector< std::vector< TScript * > > ScriptRegistry< TScript >::EnabledHooks
static

◆ ScriptPointerList

template<class TScript >
std::map< uint32, TScript * > ScriptRegistry< TScript >::ScriptPointerList
static