TrinityCore
Loading...
Searching...
No Matches
AddonMgr.cpp
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include "AddonMgr.h"
19#include "CryptoHash.h"
20#include "DatabaseEnv.h"
21#include "DBCStores.h"
22#include "Log.h"
23#include "Timer.h"
24
25namespace AddonMgr
26{
27
28// Anonymous namespace ensures file scope of all the stuff inside it, even
29// if you add something more to this namespace somewhere else.
30namespace
31{
32 // List of saved addons (in DB).
33 typedef std::list<SavedAddon> SavedAddonsList;
34
35 SavedAddonsList m_knownAddons;
36
37 BannedAddonList m_bannedAddons;
38}
39
41{
42 uint32 oldMSTime = getMSTime();
43
44 QueryResult result = CharacterDatabase.Query("SELECT name, crc FROM addons");
45 if (result)
46 {
47 uint32 count = 0;
48
49 do
50 {
51 Field* fields = result->Fetch();
52
53 std::string name = fields[0].GetString();
54 uint32 crc = fields[1].GetUInt32();
55
56 m_knownAddons.push_back(SavedAddon(name, crc));
57
58 ++count;
59 }
60 while (result->NextRow());
61
62 TC_LOG_INFO("server.loading", ">> Loaded {} known addons in {} ms", count, GetMSTimeDiffToNow(oldMSTime));
63 }
64 else
65 TC_LOG_INFO("server.loading", ">> Loaded 0 known addons. DB table `addons` is empty!");
66
67 oldMSTime = getMSTime();
68 result = CharacterDatabase.Query("SELECT id, name, version, UNIX_TIMESTAMP(timestamp) FROM banned_addons ORDER BY timestamp");
69 if (result)
70 {
71 uint32 count = 0;
72 uint32 dbcMaxBannedAddon = sBannedAddOnsStore.GetNumRows();
73
74 do
75 {
76 Field* fields = result->Fetch();
77
78 BannedAddon addon;
79 addon.Id = fields[0].GetUInt32() + dbcMaxBannedAddon;
80 addon.Timestamp = uint32(fields[3].GetUInt64());
81
82 std::string name = fields[1].GetString();
83 std::string version = fields[2].GetString();
84
87
88 m_bannedAddons.push_back(addon);
89
90 ++count;
91 }
92 while (result->NextRow());
93
94 TC_LOG_INFO("server.loading", ">> Loaded {} banned addons in {} ms", count, GetMSTimeDiffToNow(oldMSTime));
95 }
96}
97
98void SaveAddon(std::string const& name, uint32 publicKeyCrc)
99{
101
102 stmt->setString(0, name);
103 stmt->setUInt32(1, publicKeyCrc);
104
105 CharacterDatabase.Execute(stmt);
106
107 m_knownAddons.emplace_back(name, publicKeyCrc);
108}
109
110SavedAddon const* GetAddonInfo(const std::string& name)
111{
112 for (SavedAddonsList::const_iterator it = m_knownAddons.begin(); it != m_knownAddons.end(); ++it)
113 {
114 SavedAddon const& addon = (*it);
115 if (addon.Name == name)
116 return &addon;
117 }
118
119 return nullptr;
120}
121
123{
124 return &m_bannedAddons;
125}
126
127} // Namespace
@ CHAR_INS_ADDON
DBCStorage< BannedAddOnsEntry > sBannedAddOnsStore(BannedAddOnsfmt)
std::shared_ptr< ResultSet > QueryResult
DatabaseWorkerPool< CharacterDatabaseConnection > CharacterDatabase
Accessor to the character database.
uint32_t uint32
Definition Define.h:133
#define TC_LOG_INFO(filterType__,...)
Definition Log.h:159
uint32 GetMSTimeDiffToNow(uint32 oldMSTime)
Definition Timer.h:57
uint32 getMSTime()
Definition Timer.h:33
Class used to access individual fields of database query result.
Definition Field.h:92
std::string GetString() const
Definition Field.cpp:125
uint32 GetUInt32() const
Definition Field.cpp:61
void setUInt32(uint8 index, uint32 value)
void setString(uint8 index, std::string const &value)
static Digest GetDigestOf(uint8 const *data, size_t len)
Definition CryptoHash.h:49
void SaveAddon(std::string const &name, uint32 publicKeyCrc)
Definition AddonMgr.cpp:98
std::vector< BannedAddon > BannedAddonList
Definition AddonMgr.h:53
SavedAddon const * GetAddonInfo(const std::string &name)
Definition AddonMgr.cpp:110
void LoadFromDB()
Definition AddonMgr.cpp:40
BannedAddonList const * GetBannedAddons()
Definition AddonMgr.cpp:122
std::array< uint8, 16 > NameMD5
Definition AddonMgr.h:40
uint32 Id
Definition AddonMgr.h:39
std::array< uint8, 16 > VersionMD5
Definition AddonMgr.h:41
uint32 Timestamp
Definition AddonMgr.h:42
std::string Name
Definition AddonMgr.h:33