Addons/AddonMgr: converted from singleton to namespace with free functions

This commit is contained in:
Spp-
2011-09-09 15:22:52 +02:00
parent 45af1238cf
commit 14b3ed9164
6 changed files with 38 additions and 53 deletions
+22 -18
View File
@@ -16,27 +16,31 @@
* with this program. If not, see <http://www.gnu.org/licenses/>. * with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "DatabaseEnv.h"
#include "AddonMgr.h" #include "AddonMgr.h"
#include "ObjectAccessor.h" #include "DatabaseEnv.h"
#include "Player.h" #include "Log.h"
#include "Util.h" #include "Timer.h"
#include "SHA1.h"
AddonMgr::AddonMgr() #include <list>
namespace AddonMgr
{ {
// Anonymous namespace ensures file scope of all the stuff inside it, even
// if you add something more to this namespace somewhere else.
namespace
{
// List of saved addons (in DB).
typedef std::list<SavedAddon> SavedAddonsList;
SavedAddonsList m_knownAddons;
} }
AddonMgr::~AddonMgr() void LoadFromDB()
{
}
void AddonMgr::LoadFromDB()
{ {
uint32 oldMSTime = getMSTime(); uint32 oldMSTime = getMSTime();
QueryResult result = CharacterDatabase.Query("SELECT name, crc FROM addons"); QueryResult result = CharacterDatabase.Query("SELECT name, crc FROM addons");
if (!result) if (!result)
{ {
sLog->outString(">> Loaded 0 known addons. DB table `addons` is empty!"); sLog->outString(">> Loaded 0 known addons. DB table `addons` is empty!");
@@ -53,8 +57,7 @@ void AddonMgr::LoadFromDB()
std::string name = fields[0].GetString(); std::string name = fields[0].GetString();
uint32 crc = fields[1].GetUInt32(); uint32 crc = fields[1].GetUInt32();
SavedAddon addon(name, crc); m_knownAddons.push_back(SavedAddon(name, crc));
m_knownAddons.push_back(addon);
++count; ++count;
} }
@@ -64,17 +67,16 @@ void AddonMgr::LoadFromDB()
sLog->outString(); sLog->outString();
} }
void AddonMgr::SaveAddon(AddonInfo const& addon) void SaveAddon(AddonInfo const& addon)
{ {
std::string name = addon.Name; std::string name = addon.Name;
CharacterDatabase.EscapeString(name); CharacterDatabase.EscapeString(name);
CharacterDatabase.PExecute("INSERT INTO addons (name, crc) VALUES ('%s', %u)", name.c_str(), addon.CRC); CharacterDatabase.PExecute("INSERT INTO addons (name, crc) VALUES ('%s', %u)", name.c_str(), addon.CRC);
SavedAddon newAddon(addon.Name, addon.CRC); m_knownAddons.push_back(SavedAddon(addon.Name, addon.CRC));
m_knownAddons.push_back(newAddon);
} }
SavedAddon const* AddonMgr::GetAddonInfo(const std::string& name) const SavedAddon const* GetAddonInfo(const std::string& name)
{ {
for (SavedAddonsList::const_iterator it = m_knownAddons.begin(); it != m_knownAddons.end(); ++it) for (SavedAddonsList::const_iterator it = m_knownAddons.begin(); it != m_knownAddons.end(); ++it)
{ {
@@ -85,3 +87,5 @@ SavedAddon const* AddonMgr::GetAddonInfo(const std::string& name) const
return NULL; return NULL;
} }
} // Namespace
+4 -27
View File
@@ -19,13 +19,9 @@
#ifndef _ADDONMGR_H #ifndef _ADDONMGR_H
#define _ADDONMGR_H #define _ADDONMGR_H
#include "Common.h" #include "Define.h"
#include <ace/Singleton.h>
#include <string> #include <string>
class WorldSession;
struct AddonInfo struct AddonInfo
{ {
AddonInfo(const std::string& name, uint8 enabled, uint32 crc, uint8 state, bool crcOrPubKey) AddonInfo(const std::string& name, uint8 enabled, uint32 crc, uint8 state, bool crcOrPubKey)
@@ -56,33 +52,14 @@ struct SavedAddon
uint32 CRC; uint32 CRC;
}; };
// List of client addons (for WorldSession).
typedef std::list<AddonInfo> AddonsList;
// List of saved addons (in DB).
typedef std::list<SavedAddon> SavedAddonsList;
#define STANDARD_ADDON_CRC 0x4c1c776d #define STANDARD_ADDON_CRC 0x4c1c776d
class AddonMgr namespace AddonMgr
{ {
friend class ACE_Singleton<AddonMgr, ACE_Null_Mutex>;
AddonMgr();
~AddonMgr();
public:
void LoadFromDB(); void LoadFromDB();
void SaveAddon(AddonInfo const& addon); void SaveAddon(AddonInfo const& addon);
SavedAddon const* GetAddonInfo(const std::string& name);
SavedAddon const* GetAddonInfo(const std::string& name) const; }
private:
SavedAddonsList m_knownAddons; // Known addons.
};
#define sAddonMgr ACE_Singleton<AddonMgr, ACE_Null_Mutex>::instance()
#endif #endif
@@ -124,6 +124,8 @@ class AuctionHouseObject
class AuctionHouseMgr class AuctionHouseMgr
{ {
friend class ACE_Singleton<AuctionHouseMgr, ACE_Null_Mutex>; friend class ACE_Singleton<AuctionHouseMgr, ACE_Null_Mutex>;
private:
AuctionHouseMgr(); AuctionHouseMgr();
~AuctionHouseMgr(); ~AuctionHouseMgr();
+2 -2
View File
@@ -868,7 +868,7 @@ void WorldSession::ReadAddonsInfo(WorldPacket &data)
AddonInfo addon(addonName, enabled, crc, 2, true); AddonInfo addon(addonName, enabled, crc, 2, true);
SavedAddon const* savedAddon = sAddonMgr->GetAddonInfo(addonName); SavedAddon const* savedAddon = AddonMgr::GetAddonInfo(addonName);
if (savedAddon) if (savedAddon)
{ {
bool match = true; bool match = true;
@@ -883,7 +883,7 @@ void WorldSession::ReadAddonsInfo(WorldPacket &data)
} }
else else
{ {
sAddonMgr->SaveAddon(addon); AddonMgr::SaveAddon(addon);
sLog->outDetail("ADDON: %s (0x%x) was not known, saving...", addon.Name.c_str(), addon.CRC); sLog->outDetail("ADDON: %s (0x%x) was not known, saving...", addon.Name.c_str(), addon.CRC);
} }
+2
View File
@@ -939,6 +939,8 @@ class WorldSession
uint32 _accountId; uint32 _accountId;
uint8 m_expansion; uint8 m_expansion;
typedef std::list<AddonInfo> AddonsList;
time_t _logoutTime; time_t _logoutTime;
bool m_inQueue; // session wait in auth.queue bool m_inQueue; // session wait in auth.queue
bool m_playerLoading; // code processed in LoginPlayer bool m_playerLoading; // code processed in LoginPlayer
+1 -1
View File
@@ -1586,7 +1586,7 @@ void World::SetInitialWorldSettings()
sTicketMgr->LoadSurveys(); sTicketMgr->LoadSurveys();
sLog->outString("Loading client addons..."); sLog->outString("Loading client addons...");
sAddonMgr->LoadFromDB(); AddonMgr::LoadFromDB();
///- Handle outdated emails (delete/return) ///- Handle outdated emails (delete/return)
sLog->outString("Returning old mails..."); sLog->outString("Returning old mails...");