Merge pull request #108 from H0zen/develop21

Warden checks refactoring
This commit is contained in:
Antz 2016-03-28 23:56:26 +01:00
commit 9f31d12914
2 changed files with 79 additions and 141 deletions

View File

@ -36,11 +36,19 @@ WardenCheckMgr::WardenCheckMgr() { }
WardenCheckMgr::~WardenCheckMgr()
{
for (uint16 i = 0; i < CheckStore.size(); ++i)
delete CheckStore[i];
for (CheckMap::iterator it = CheckStore.begin(); it != CheckStore.end(); ++it)
{
for (CheckContainer::iterator itr = it->second->begin(); itr != it->second->end(); ++itr)
delete itr->second;
delete it->second;
}
for (CheckResultContainer::iterator itr = CheckResultStore.begin(); itr != CheckResultStore.end(); ++itr)
delete itr->second;
for (CheckResultMap::iterator it = CheckResultStore.begin(); it != CheckResultStore.end(); ++it)
{
for (CheckResultContainer::iterator itr = it->second->begin(); itr != it->second->end(); ++itr)
delete itr->second;
delete it->second;
}
}
void WardenCheckMgr::LoadWardenChecks()
@ -52,44 +60,48 @@ void WardenCheckMgr::LoadWardenChecks()
return;
}
QueryResult *result = WorldDatabase.Query("SELECT MAX(id) FROM warden_checks");
QueryResult *result = WorldDatabase.Query("SELECT DISTINCT build FROM warden");
if (!result)
{
sLog.outWarden(">> Loaded 0 Warden checks. DB table `warden_checks` is empty!");
sLog.outString("[Warden]: >> Loaded 0 warden data and results");
return;
}
Field* fields = result->Fetch();
uint16 maxCheckId = fields[0].GetUInt16();
CheckStore.resize(maxCheckId + 1);
Field* fields;
do
{
fields = result->Fetch();
uint16 build = fields[0].GetUInt16();
CheckContainer* ck = new CheckContainer();
CheckResultContainer* ckr = new CheckResultContainer();
CheckStore[build] = ck;
CheckResultStore[build] = ckr;
} while (result->NextRow());
delete result;
// 0 1 2 3 4 5 6 7
result = WorldDatabase.Query("SELECT id, type, data, result, address, length, str, comment FROM warden_checks ORDER BY id ASC");
// 0 1 2 3 4 5 6 7 8
result = WorldDatabase.Query("SELECT id, build, type, data, result, address, length, str, comment FROM warden ORDER BY build ASC, id ASC");
// no need to check. done in SELECT DISTINCT before
uint32 count = 0;
if (!result)
{
sLog.outString("[Warden]: >> Loaded %u warden data and results", count);
return;
}
do
{
fields = result->Fetch();
uint16 id = fields[0].GetUInt16();
uint8 checkType = fields[1].GetUInt8();
std::string data = fields[2].GetString();
std::string checkResult = fields[3].GetString();
uint32 address = fields[4].GetUInt32();
uint8 length = fields[5].GetUInt8();
std::string str = fields[6].GetString();
std::string comment = fields[7].GetString();
uint16 id = fields[0].GetUInt16();
uint16 build = fields[1].GetUInt16();
uint8 checkType = fields[2].GetUInt8();
std::string data = fields[3].GetString();
std::string checkResult = fields[4].GetString();
uint32 address = fields[5].GetUInt32();
uint8 length = fields[6].GetUInt8();
std::string str = fields[7].GetString();
std::string comment = fields[8].GetString();
WardenCheck* wardenCheck = new WardenCheck();
wardenCheck->Type = checkType;
@ -128,7 +140,7 @@ void WardenCheckMgr::LoadWardenChecks()
if (checkType == MEM_CHECK || checkType == MPQ_CHECK || checkType == LUA_STR_CHECK || checkType == DRIVER_CHECK || checkType == MODULE_CHECK)
wardenCheck->Str = str;
CheckStore[id] = wardenCheck;
CheckStore[build]->insert(std::pair<uint16,WardenCheck*>(id, wardenCheck));
if (checkType == MPQ_CHECK || checkType == MEM_CHECK)
{
@ -144,7 +156,7 @@ void WardenCheckMgr::LoadWardenChecks()
wr->Result.SetBinary((uint8*)temp, len);
delete[] temp;
}
CheckResultStore[id] = wr;
CheckResultStore[build]->insert(std::pair<uint16, WardenCheckResult*>(id,wr));
}
if (comment.empty())
@ -158,79 +170,6 @@ void WardenCheckMgr::LoadWardenChecks()
sLog.outWarden(">> Loaded %u warden checks.", count);
delete result;
// 0 1 2 3 4 5 6
result = WorldDatabase.Query("SELECT id, build, data, result, address, length, str FROM warden_build_specific ORDER BY id ASC");
if (!result)
{
sLog.outString("[Warden]: >> Loaded 0 warden client build-specific data.");
return;
}
count = 0;
do
{
fields = result->Fetch();
uint16 id = fields[0].GetUInt16();
if (id >= CheckStore.size())
{
sLog.outWarden("ERROR: Build-specific, check is missing in warden_checks, skipping id %u.", id);
continue;
}
uint16 build = fields[1].GetUInt16();
if (build == DEFAULT_CLIENT_BUILD)
{
sLog.outWarden("ERROR: Build-specific table may not contain checks for default %u build, skipping id %u.", DEFAULT_CLIENT_BUILD, id);
continue;
}
//std::string data = fields[2].GetString(); //unused for now
std::string checkResult = fields[3].GetString();
uint32 address = fields[4].GetUInt32();
uint8 length = fields[5].GetUInt8();
std::string str = fields[6].GetString();
WardenCheck* wardenCheck = new WardenCheck();
wardenCheck->CheckId = id;
wardenCheck->Type = CheckStore[id]->Type;
WardenCheckResult* wr = new WardenCheckResult();
switch (wardenCheck->Type)
{
case MEM_CHECK:
wardenCheck->Address = address;
wardenCheck->Length = length;
wardenCheck->Str = str;
wr->Result.SetHexStr(checkResult.c_str());
{
int len = checkResult.size() / 2;
if (wr->Result.GetNumBytes() < len)
{
uint8 *temp = new uint8[len];
memset(temp, 0, len);
memcpy(temp, wr->Result.AsByteArray(), wr->Result.GetNumBytes());
std::reverse(temp, temp + len);
wr->Result.SetBinary((uint8*)temp, len);
delete[] temp;
}
}
break;
default:
sLog.outWarden("The check type %u is considered as build-independent, skipping id %u.", wardenCheck->Type, id);
delete wr;
delete wardenCheck;
continue;
}
MCheckStore[ComposeMultiCheckKey(build, id)] = wardenCheck;
MCheckResultStore[ComposeMultiCheckKey(build, id)] = wr;
++count;
} while (result->NextRow());
sLog.outString(">> Loaded %u warden client build-specific check overrides.", count);
}
void WardenCheckMgr::LoadWardenOverrides()
@ -265,13 +204,21 @@ void WardenCheckMgr::LoadWardenOverrides()
// Check if action value is in range (0-2, see WardenActions enum)
if (action > WARDEN_ACTION_BAN)
sLog.outWarden("Warden check override action out of range (ID: %u, action: %u)", checkId, action);
// Check if check actually exists before accessing the CheckStore vector
else if (checkId > CheckStore.size())
sLog.outWarden("Warden check action override for non-existing check (ID: %u, action: %u), skipped", checkId, action);
else
else
{
CheckStore[checkId]->Action = WardenActions(action);
++count;
for (CheckMap::iterator it = CheckStore.begin(); it != CheckStore.end(); ++it)
{
CheckContainer::iterator ir = it->second->find(checkId);
if (ir == it->second->end())
sLog.outWarden("Warden check action override for non-existing check (ID: %u, action: %u), skipped", checkId, action);
else
{
ir->second->Action = WardenActions(action);
++count;
}
}
}
}
while (result->NextRow());
@ -279,34 +226,30 @@ void WardenCheckMgr::LoadWardenOverrides()
sLog.outWarden(">> Loaded %u warden action overrides.", count);
}
WardenCheck* WardenCheckMgr::GetWardenDataById(uint16 build, uint16 Id)
WardenCheck* WardenCheckMgr::GetWardenDataById(uint16 build, uint16 id)
{
if (Id < CheckStore.size())
WardenCheck* result = NULL;
CheckMap::iterator it = CheckStore.find(build);
if (it != CheckStore.end())
{
if (build != DEFAULT_CLIENT_BUILD)
{
MultiCheckContainer::const_iterator it = MCheckStore.find(ComposeMultiCheckKey(build, Id));
if (it != MCheckStore.end())
return it->second;
}
return CheckStore[Id];
CheckContainer::iterator ir = it->second->find(id);
if (ir != it->second->end())
result = ir->second;
}
return NULL;
return result;
}
WardenCheckResult* WardenCheckMgr::GetWardenResultById(uint16 build, uint16 Id)
WardenCheckResult* WardenCheckMgr::GetWardenResultById(uint16 build, uint16 id)
{
if (build != DEFAULT_CLIENT_BUILD)
WardenCheckResult* result = NULL;
CheckResultMap::iterator it = CheckResultStore.find(build);
if (it != CheckResultStore.end())
{
MultiResultContainer::const_iterator it = MCheckResultStore.find(ComposeMultiCheckKey(build, Id));
if (it != MCheckResultStore.end())
return it->second;
CheckResultContainer::iterator ir = it->second->find(id);
if (ir != it->second->end())
result = ir->second;
}
CheckResultContainer::const_iterator itr = CheckResultStore.find(Id);
if (itr != CheckResultStore.end())
return itr->second;
return NULL;
return result;
}

View File

@ -66,8 +66,8 @@ class WardenCheckMgr
return &instance;
}
WardenCheck* GetWardenDataById(uint16 build, uint16 Id);
WardenCheckResult* GetWardenResultById(uint16 build, uint16 Id);
WardenCheck* GetWardenDataById(uint16 /*build*/, uint16 /*id*/);
WardenCheckResult* GetWardenResultById(uint16 /*build*/, uint16 /*id*/);
std::vector<uint16> MemChecksIdPool;
std::vector<uint16> OtherChecksIdPool;
@ -78,21 +78,16 @@ class WardenCheckMgr
ACE_RW_Mutex _checkStoreLock;
private:
uint32 inline ComposeMultiCheckKey(uint16 clientBuild, uint16 checkID) { return (uint32(checkID) << 16) | clientBuild; }
// We have a linear key without any gaps, so we use vector for fast access
typedef std::vector<WardenCheck*> CheckContainer;
typedef std::map<uint32, WardenCheckResult*> CheckResultContainer;
typedef std::map< uint16, WardenCheck* > CheckContainer;
typedef std::map< uint16, WardenCheckResult* > CheckResultContainer;
typedef std::map< uint16 /*build*/, CheckContainer* > CheckMap;
typedef std::map< uint16 /*build*/, CheckResultContainer* > CheckResultMap;
CheckContainer CheckStore;
CheckResultContainer CheckResultStore;
CheckMap CheckStore;
CheckResultMap CheckResultStore;
// here we have just few checks, vector is not appropriate; key is from ComposeMultiCheckKey
typedef std::map<uint32 /*MultiCheckKey*/, WardenCheck*> MultiCheckContainer;
typedef std::map<uint32 /*MultiCheckKey*/, WardenCheckResult*> MultiResultContainer;
MultiCheckContainer MCheckStore;
MultiResultContainer MCheckResultStore;
};
#define sWardenCheckMgr WardenCheckMgr::instance()