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
855 {
856 // See if the script is using the same memory as another script. If this happens, it means that
857 // someone forgot to allocate new memory for a script.
858 for (ScriptMapIterator it = ScriptPointerList.begin(); it != ScriptPointerList.end(); ++it)
859 {
860 if (it->second == script)
861 {
862 LOG_ERROR("scripts", "Script '{}' has same memory pointer as '{}'.",
863 script->GetName(), it->second->GetName());
864
865 return false;
866 }
867 }
868
869 return true;
870 }
#define LOG_ERROR(filterType__,...)
Definition: Log.h:157
static ScriptMap ScriptPointerList
Definition: ScriptMgr.h:726
ScriptMap::iterator ScriptMapIterator
Definition: ScriptMgr.h:716

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

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

◆ AddALScripts()

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

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
738 {})
739 {
740 ASSERT(script);
741
742 if (!_checkMemory(script))
743 return;
744
745 if (EnabledHooks.empty())
746 InitEnabledHooksIfNeeded(script->GetTotalAvailableHooks());
747
748 if (script->isAfterLoadScript())
749 {
750 ALScripts.emplace_back(script, std::move(enabledHooks));
751 }
752 else
753 {
754 script->checkValidity();
755
756 for (uint16 v : enabledHooks)
757 EnabledHooks[v].emplace_back(script);
758
759 // We're dealing with a code-only script; just add it.
761 sScriptMgr->IncreaseScriptCount();
762 }
763 }
#define ASSERT
Definition: Errors.h:68
static void InitEnabledHooksIfNeeded(uint16 totalAvailableHooks)
Definition: ScriptMgr.h:733

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
734 {
735 EnabledHooks.resize(totalAvailableHooks);
736 }

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