refactor(Core/Tools): remove ACE from tools (#3351)

This commit is contained in:
Viste
2020-09-07 15:01:28 +03:00
committed by GitHub
parent 7694c99edb
commit 4dd9b92759
35 changed files with 465 additions and 229 deletions

View File

@@ -0,0 +1,101 @@
/*
* Copyright (C) 2016+ AzerothCore <www.azerothcore.org>, released under GNU GPL v2 license: https://github.com/azerothcore/azerothcore-wotlk/blob/master/LICENSE-GPL2
* Copyright (C) 2008-2016 TrinityCore <http://www.trinitycore.org/>
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
*/
#ifndef _PCQ_H
#define _PCQ_H
#include <condition_variable>
#include <mutex>
#include <queue>
#include <atomic>
#include <type_traits>
template <typename T>
class ProducerConsumerQueue
{
private:
std::mutex _queueLock;
std::queue<T> _queue;
std::condition_variable _condition;
std::atomic<bool> _shutdown;
public:
ProducerConsumerQueue<T>() : _shutdown(false) { }
void Push(const T& value)
{
std::lock_guard<std::mutex> lock(_queueLock);
_queue.push(std::move(value));
_condition.notify_one();
}
bool Empty()
{
std::lock_guard<std::mutex> lock(_queueLock);
return _queue.empty();
}
bool Pop(T& value)
{
std::lock_guard<std::mutex> lock(_queueLock);
if (_queue.empty() || _shutdown)
return false;
value = _queue.front();
_queue.pop();
return true;
}
void WaitAndPop(T& value)
{
std::unique_lock<std::mutex> lock(_queueLock);
// we could be using .wait(lock, predicate) overload here but it is broken
// https://connect.microsoft.com/VisualStudio/feedback/details/1098841
while (_queue.empty() && !_shutdown)
_condition.wait(lock);
if (_queue.empty() || _shutdown)
return;
value = _queue.front();
_queue.pop();
}
void Cancel()
{
std::unique_lock<std::mutex> lock(_queueLock);
while (!_queue.empty())
{
T& value = _queue.front();
DeleteQueuedObject(value);
_queue.pop();
}
_shutdown = true;
_condition.notify_all();
}
private:
template<typename E = T>
typename std::enable_if<std::is_pointer<E>::value>::type DeleteQueuedObject(E& obj) { delete obj; }
template<typename E = T>
typename std::enable_if<!std::is_pointer<E>::value>::type DeleteQueuedObject(E const& /*packet*/) { }
};
#endif

View File

@@ -0,0 +1,16 @@
/*
* Copyright (C) 2016+ AzerothCore <www.azerothcore.org>, released under GNU GPL v2 license: https://github.com/azerothcore/azerothcore-wotlk/blob/master/LICENSE-GPL2
* Copyright (C) 2008-2016 TrinityCore <http://www.trinitycore.org/>
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
*/
#ifndef POLICYLOCK_H
#define POLICYLOCK_H
#include <mutex>
#define RETURN_GUARD(mutex, retval) if (!mutex.try_lock()) \
return retval; \
std::lock_guard<decltype(mutex)> guard(mutex, std::adopt_lock)
#endif

View File

@@ -0,0 +1,144 @@
/*
* Copyright (C) 2016+ AzerothCore <www.azerothcore.org>, released under GNU GPL v2 license: https://github.com/azerothcore/azerothcore-wotlk/blob/master/LICENSE-GPL2
* Copyright (C) 2008-2016 TrinityCore <http://www.trinitycore.org/>
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
*/
#ifndef ACORE_THREADINGMODEL_H
#define ACORE_THREADINGMODEL_H
/**
* @class ThreadingModel<T>
*
*/
#include "Define.h"
namespace acore
{
template<typename MUTEX>
class GeneralLock
{
public:
GeneralLock(MUTEX& m)
: i_mutex(m)
{
i_mutex.lock();
}
~GeneralLock()
{
i_mutex.unlock();
}
private:
GeneralLock(const GeneralLock&);
GeneralLock& operator=(const GeneralLock&);
MUTEX& i_mutex;
};
template<class T>
class SingleThreaded
{
public:
struct Lock // empty object
{
Lock()
{
}
Lock(const T&)
{
}
Lock(const SingleThreaded<T>&) // for single threaded we ignore this
{
}
};
};
template<class T, class MUTEX>
class ObjectLevelLockable
{
public:
ObjectLevelLockable()
: i_mtx()
{
}
friend class Lock;
class Lock
{
public:
Lock(ObjectLevelLockable<T, MUTEX>& host)
: i_lock(host.i_mtx)
{
}
private:
GeneralLock<MUTEX> i_lock;
};
private:
// prevent the compiler creating a copy construct
ObjectLevelLockable(const ObjectLevelLockable<T, MUTEX>&);
ObjectLevelLockable<T, MUTEX>& operator=(const ObjectLevelLockable<T, MUTEX>&);
MUTEX i_mtx;
};
template<class T, class MUTEX>
class ClassLevelLockable
{
public:
ClassLevelLockable()
{
}
friend class Lock;
class Lock
{
public:
Lock(const T& /*host*/)
{
ClassLevelLockable<T, MUTEX>::si_mtx.lock();
}
Lock(const ClassLevelLockable<T, MUTEX>&)
{
ClassLevelLockable<T, MUTEX>::si_mtx.lock();
}
Lock()
{
ClassLevelLockable<T, MUTEX>::si_mtx.lock();
}
~Lock()
{
ClassLevelLockable<T, MUTEX>::si_mtx.unlock();
}
};
private:
static MUTEX si_mtx;
};
}
template<class T, class MUTEX> MUTEX acore::ClassLevelLockable<T, MUTEX>::si_mtx;
#define INSTANTIATE_CLASS_MUTEX(CTYPE, MUTEX) \
template class acore::ClassLevelLockable<CTYPE, MUTEX>
#endif

View File

@@ -10,7 +10,7 @@
#include "mpq_libmpq04.h"
DBCFile::DBCFile(const std::string& filename):
filename(filename), recordSize(0), recordCount(0), fieldCount(0), stringSize(0), data(NULL), stringTable(NULL)
filename(filename), recordSize(0), recordCount(0), fieldCount(0), stringSize(0), data(nullptr), stringTable(nullptr)
{
}

View File

@@ -43,13 +43,13 @@ public:
token = strtok( buffer, seps );
uint32 counter = 0;
while ((token != NULL) && (counter < size)) {
while ((token != nullptr) && (counter < size)) {
//cout << token << endl;
token[strlen(token) - 1] = 0;
string s = token;
filelist.push_back(s);
counter += strlen(token) + 2;
token = strtok(NULL, seps);
token = strtok(nullptr, seps);
}
delete[] buffer;

View File

@@ -9,15 +9,15 @@
#include "LiquidHandler.h"
#include "WorldModelHandler.h"
ADT::ADT( std::string file, int x, int y ) : ObjectData(NULL), Data(NULL), HasObjectData(false),
_DoodadHandler(NULL), _WorldModelHandler(NULL), _LiquidHandler(NULL), X(x), Y(y)
ADT::ADT( std::string file, int x, int y ) : ObjectData(nullptr), Data(nullptr), HasObjectData(false),
_DoodadHandler(nullptr), _WorldModelHandler(nullptr), _LiquidHandler(nullptr), X(x), Y(y)
{
Data = new ChunkedData(file);
ObjectData = new ChunkedData(file);
if (ObjectData->Stream)
HasObjectData = true;
else
ObjectData = NULL;
ObjectData = nullptr;
}
ADT::~ADT()

View File

@@ -9,8 +9,8 @@
#include <string>
#include <map>
#include "Define.h"
#include <ace/Guard_T.h>
#include <ace/Synch.h>
#include "PolicyLock.h"
#include <mutex>
#include "WorldModelRoot.h"
#include "Model.h"
@@ -24,7 +24,7 @@ public:
void Insert(K key, T* val)
{
ACE_GUARD(ACE_Thread_Mutex, g, mutex);
std::lock_guard<std::mutex> guard(_mutex);
if (_items.size() > FlushLimit)
Clear();
@@ -33,11 +33,11 @@ public:
T* Get(K key)
{
ACE_GUARD_RETURN(ACE_Thread_Mutex, g, mutex, NULL);
RETURN_GUAD(mutex, false);
typename std::map<K, T*>::iterator itr = _items.find(key);
if (itr != _items.end())
return itr->second;
return NULL;
return nullptr;
}
void Clear()
@@ -48,7 +48,7 @@ public:
}
private:
std::map<K, T*> _items;
ACE_Thread_Mutex mutex;
std::mutex mutex;
};
class CacheClass

View File

@@ -66,7 +66,7 @@ Chunk* ChunkedData::GetChunkByName( const std::string& name )
for (uint32 i = 0; i < Chunks.size(); ++i)
if (Chunks[i]->Name == name)
return Chunks[i];
return NULL;
return nullptr;
}
ChunkedData::~ChunkedData()

View File

@@ -10,11 +10,11 @@
#include "Utils.h"
#include "DetourNavMesh.h"
#include "Cache.h"
#include "ace/Task.h"
#include <thread>
#include "Recast.h"
#include "DetourCommon.h"
class BuilderThread : public ACE_Task_Base
class BuilderThread
{
private:
int X, Y, MapId;
@@ -99,7 +99,7 @@ void ContinentBuilder::CalculateTileBounds()
tileYMax = std::max(itr->Y, tileYMax);
tileYMin = std::min(itr->Y, tileYMin);
}
getTileBounds(tileXMax, tileYMax, NULL, 0, bmin, bmax);
getTileBounds(tileXMax, tileYMax, nullptr, 0, bmin, bmax);
}
void ContinentBuilder::Build()
@@ -186,7 +186,7 @@ void ContinentBuilder::Build()
}
}
// Wait for 20 seconds
ACE_OS::sleep(ACE_Time_Value (0, 20000));
std::this_thread::sleep_for(std::chrono::seconds(20));
}
}
}

View File

@@ -8,7 +8,7 @@
#include "DBC.h"
#include "Define.h"
DBC::DBC( FILE* stream ) : StringBlock(NULL), StringBlockSize(0), IsFaulty(true)
DBC::DBC( FILE* stream ) : StringBlock(nullptr), StringBlockSize(0), IsFaulty(true)
{
char magic[5];
uint32 count = 0;
@@ -72,5 +72,5 @@ Record* DBC::GetRecordById( int id )
for (std::vector<Record*>::iterator itr = Records.begin(); itr != Records.end(); ++itr)
if ((*itr)->Values[0] == id)
return *itr;
return NULL;
return nullptr;
}

View File

@@ -11,7 +11,7 @@
#include "G3D/Matrix4.h"
DoodadHandler::DoodadHandler( ADT* adt ) :
ObjectDataHandler(adt), _definitions(NULL), _paths(NULL)
ObjectDataHandler(adt), _definitions(nullptr), _paths(nullptr)
{
Chunk* mddf = adt->ObjectData->GetChunkByName("MDDF");
if (mddf)

View File

@@ -33,7 +33,7 @@ void LiquidHandler::HandleNewLiquid()
if (h.LayerCount == 0)
{
// Need to fill in missing data with dummies.
MCNKData.push_back(MCNKLiquidData(NULL, H2ORenderMask()));
MCNKData.push_back(MCNKLiquidData(nullptr, H2ORenderMask()));
continue;
}
fseek(stream, chunk->Offset + h.OffsetInformation, SEEK_SET);

View File

@@ -9,6 +9,7 @@
#include "libmpq/mpq.h"
#include "Define.h"
#include "Errors.h"
#include <string>
#include <ctype.h>
#include <vector>
@@ -17,42 +18,44 @@
class MPQArchive
{
public:
mpq_archive_s *mpq_a;
std::vector<std::string> Files;
MPQArchive(const char* filename);
void close();
void GetFileListTo(std::vector<std::string>& filelist) {
uint32_t filenum;
if(libmpq__file_number(mpq_a, "(listfile)", &filenum)) return;
libmpq__off_t size, transferred;
libmpq__file_unpacked_size(mpq_a, filenum, &size);
char* buffer = new char[size + 1];
buffer[size] = '\0';
libmpq__file_read(mpq_a, filenum, (unsigned char*)buffer, size, &transferred);
char seps[] = "\n";
char* token;
token = strtok( buffer, seps );
uint32 counter = 0;
while ((token != NULL) && (counter < size)) {
//cout << token << endl;
token[strlen(token) - 1] = 0;
std::string s = token;
filelist.push_back(s);
counter += strlen(token) + 2;
token = strtok(NULL, seps);
public:
mpq_archive_s *mpq_a;
std::vector<std::string> Files;
MPQArchive(const char* filename);
void close();
void GetFileListTo(std::vector<std::string>& filelist)
{
uint32_t filenum;
if(libmpq__file_number(mpq_a, "(listfile)", &filenum)) return;
libmpq__off_t size, transferred;
libmpq__file_unpacked_size(mpq_a, filenum, &size);
char* buffer = new char[size + 1];
buffer[size] = '\0';
libmpq__file_read(mpq_a, filenum, (unsigned char*)buffer, size, &transferred);
char seps[] = "\n";
char* token;
token = strtok( buffer, seps );
uint32 counter = 0;
while ((token != nullptr) && (counter < size))
{
//cout << token << endl;
token[strlen(token) - 1] = 0;
std::string s = token;
filelist.push_back(s);
counter += strlen(token) + 2;
token = strtok(nullptr, seps);
}
delete[] buffer;
}
delete[] buffer;
}
};
class MPQFile

View File

@@ -8,7 +8,6 @@
#include "MPQ.h"
#include "DBC.h"
#include "Utils.h"
#include <ace/Guard_T.h>
char const* MPQManager::Files[] = {
"common.MPQ",
@@ -39,7 +38,7 @@ void MPQManager::InitializeDBC()
BaseLocale = -1;
std::string fileName;
uint32 size = sizeof(Languages) / sizeof(char*);
MPQArchive* _baseLocale = NULL;
MPQArchive* _baseLocale = nullptr;
for (uint32 i = 0; i < size; ++i)
{
std::string _fileName = "Data/" + std::string(Languages[i]) + "/locale-" + std::string(Languages[i]) + ".MPQ";
@@ -72,10 +71,10 @@ void MPQManager::InitializeDBC()
FILE* MPQManager::GetFile(const std::string& path )
{
ACE_GUARD_RETURN(ACE_Thread_Mutex, g, mutex, NULL);
RETURN_GUAD(mutex, false);
MPQFile file(path.c_str());
if (file.isEof())
return NULL;
return nullptr;
return file.GetFileStream();
}
@@ -87,12 +86,12 @@ DBC* MPQManager::GetDBC(const std::string& name )
FILE* MPQManager::GetFileFrom(const std::string& path, MPQArchive* file )
{
ACE_GUARD_RETURN(ACE_Thread_Mutex, g, mutex, NULL);
RETURN_GUAD(mutex, false);
mpq_archive* mpq_a = file->mpq_a;
uint32_t filenum;
if(libmpq__file_number(mpq_a, path.c_str(), &filenum))
return NULL;
return nullptr;
libmpq__off_t transferred;
libmpq__off_t size = 0;
@@ -100,7 +99,7 @@ FILE* MPQManager::GetFileFrom(const std::string& path, MPQArchive* file )
// HACK: in patch.mpq some files don't want to open and give 1 for filesize
if (size <= 1)
return NULL;
return nullptr;
uint8* buffer = new uint8[size];

View File

@@ -8,7 +8,8 @@
#define MPQ_MANAGER_H
#include "MPQ.h"
#include <ace/Synch.h>
#include "PolicyLock.h"
#include <mutex>
#include <set>
#include <map>
@@ -35,7 +36,7 @@ public:
protected:
void InitializeDBC();
private:
ACE_Thread_Mutex mutex;
std::mutex mutex;
};
extern MPQManager* MPQHandler;

View File

@@ -252,7 +252,7 @@ void ExtractGameobjectModels()
bool HandleArgs(int argc, char** argv, uint32& threads, std::set<uint32>& mapList, bool& debugOutput, uint32& extractFlags)
{
char* param = NULL;
char* param = nullptr;
extractFlags = 0;
for (int i = 1; i < argc; ++i)
@@ -277,7 +277,7 @@ bool HandleArgs(int argc, char** argv, uint32& threads, std::set<uint32>& mapLis
while (token)
{
mapList.insert(atoi(token));
token = strtok(NULL, ",");
token = strtok(nullptr, ",");
}
free(copy);
@@ -345,7 +345,7 @@ void LoadTile(dtNavMesh*& navMesh, const char* tile)
if (fread(nav, header.size, 1, f) != 1)
return;
navMesh->addTile(nav, header.size, DT_TILE_FREE_DATA, 0, NULL);
navMesh->addTile(nav, header.size, DT_TILE_FREE_DATA, 0, nullptr);
fclose(f);
}

View File

@@ -17,10 +17,8 @@
#include "RecastAlloc.h"
#include "DetourNavMeshBuilder.h"
#include <ace/Synch.h>
TileBuilder::TileBuilder(ContinentBuilder* _cBuilder, std::string world, int x, int y, uint32 mapId) :
World(world), X(x), Y(y), MapId(mapId), _Geometry(NULL), DataSize(0), cBuilder(_cBuilder)
World(world), X(x), Y(y), MapId(mapId), _Geometry(nullptr), DataSize(0), cBuilder(_cBuilder)
{
// Config for normal maps
memset(&Config, 0, sizeof(rcConfig));
@@ -82,7 +80,7 @@ void TileBuilder::AddGeometry(WorldModelRoot* root, const WorldModelDefinition&
uint8* TileBuilder::BuildInstance( dtNavMeshParams& navMeshParams )
{
float* bmin = NULL, *bmax = NULL;
float* bmin = nullptr, *bmax = nullptr;
_Geometry->CalculateBoundingBox(bmin, bmax);
@@ -186,7 +184,7 @@ uint8* TileBuilder::BuildInstance( dtNavMeshParams& navMeshParams )
printf("No polygons to build on tile, skipping.\n");
rcFreePolyMesh(pmesh);
rcFreePolyMeshDetail(dmesh);
return NULL;
return nullptr;
}
int navDataSize;
@@ -204,7 +202,7 @@ uint8* TileBuilder::BuildInstance( dtNavMeshParams& navMeshParams )
return navData;
}
return NULL;
return nullptr;
}
uint8* TileBuilder::BuildTiled(dtNavMeshParams& navMeshParams)
@@ -217,9 +215,9 @@ uint8* TileBuilder::BuildTiled(dtNavMeshParams& navMeshParams)
delete adt;
if (_Geometry->Vertices.empty() && _Geometry->Triangles.empty())
return NULL;
return nullptr;
float* bmin = NULL, *bmax = NULL;
float* bmin = nullptr, *bmax = nullptr;
CalculateTileBounds(bmin, bmax, navMeshParams);
_Geometry->CalculateMinMaxHeight(bmin[1], bmax[1]);
@@ -326,7 +324,7 @@ uint8* TileBuilder::BuildTiled(dtNavMeshParams& navMeshParams)
params.buildBvTree = true;
// Recalculate the bounds with the added geometry
float* bmin2 = NULL, *bmax2 = NULL;
float* bmin2 = nullptr, *bmax2 = nullptr;
CalculateTileBounds(bmin2, bmax2, navMeshParams);
bmin2[1] = bmin[1];
bmax2[1] = bmax[1];
@@ -354,7 +352,7 @@ uint8* TileBuilder::BuildTiled(dtNavMeshParams& navMeshParams)
printf("[%02i, %02i] No polygons to build on tile, skipping.\n", X, Y);
rcFreePolyMesh(pmesh);
rcFreePolyMeshDetail(dmesh);
return NULL;
return nullptr;
}
int navDataSize;
@@ -372,7 +370,7 @@ uint8* TileBuilder::BuildTiled(dtNavMeshParams& navMeshParams)
return navData;
}
return NULL;
return nullptr;
}
void TileBuilder::OutputDebugVertices()

View File

@@ -525,7 +525,7 @@ char* Utils::GetPlainName(const char* FileName)
{
char* temp;
if((temp = (char*)strrchr(FileName, '\\')) != NULL)
if((temp = (char*)strrchr(FileName, '\\')) != nullptr)
FileName = temp + 1;
return (char*)FileName;
}

View File

@@ -16,13 +16,11 @@
#include "Define.h"
#include "Constants.h"
#include <ace/Stack_Trace.h>
struct WorldModelDefinition;
class DoodadDefinition;
class DoodadInstance;
#define ASSERT(assertion) { if (!(assertion)) { ACE_Stack_Trace st; fprintf(stderr, "\n%s:%i in %s ASSERTION FAILED:\n %s\n%s\n", __FILE__, __LINE__, __FUNCTION__, #assertion, st.c_str()); *((volatile int*)NULL) = 0; } }
#define ASSERT(assertion) { if (!(assertion)) {fprintf(stderr, "\n%s:%i in %s ASSERTION FAILED:\n %s\n%s\n", __FILE__, __LINE__, __FUNCTION__, #assertion, st.c_str()); *((volatile int*)nullptr) = 0; } }
struct Vector3
{

View File

@@ -10,7 +10,7 @@
#include "Utils.h"
#include "WorldModelHandler.h"
WDT::WDT(std::string file) : IsGlobalModel(false), IsValid(false), Model(NULL)
WDT::WDT(std::string file) : IsGlobalModel(false), IsValid(false), Model(nullptr)
{
Data = new ChunkedData(file, 2);
ReadTileTable();

View File

@@ -9,7 +9,7 @@
#include "Chunk.h"
#include "Utils.h"
WorldModelGroup::WorldModelGroup( std::string path, int groupIndex ) : GroupIndex(groupIndex), MOBA(NULL), IsBad(false), HasLiquidData(false)
WorldModelGroup::WorldModelGroup( std::string path, int groupIndex ) : GroupIndex(groupIndex), MOBA(nullptr), IsBad(false), HasLiquidData(false)
{
Data = new ChunkedData(path);
if (!Data->Stream)

View File

@@ -35,7 +35,7 @@ WorldModelDefinition WorldModelDefinition::Read( FILE* file )
}
WorldModelHandler::WorldModelHandler( ADT* adt ) : ObjectDataHandler(adt), _definitions(NULL), _paths(NULL)
WorldModelHandler::WorldModelHandler( ADT* adt ) : ObjectDataHandler(adt), _definitions(nullptr), _paths(nullptr)
{
ReadModelPaths();
ReadDefinitions();

View File

@@ -23,8 +23,8 @@ namespace MMAP
rcPolyMesh* polyMesh;
rcPolyMeshDetail* polyMeshDetail;
IntermediateValues() : heightfield(NULL), compactHeightfield(NULL),
contours(NULL), polyMesh(NULL), polyMeshDetail(NULL) {}
IntermediateValues() : heightfield(nullptr), compactHeightfield(nullptr),
contours(nullptr), polyMesh(nullptr), polyMeshDetail(nullptr) {}
~IntermediateValues();
void writeIV(uint32 mapID, uint32 tileX, uint32 tileY);

View File

@@ -50,7 +50,7 @@ namespace MMAP
MapBuilder::MapBuilder(float maxWalkableAngle, bool skipLiquid,
bool skipContinents, bool skipJunkMaps, bool skipBattlegrounds,
bool debugOutput, bool bigBaseUnit, const char* offMeshFilePath) :
m_terrainBuilder (NULL),
m_terrainBuilder (nullptr),
m_debugOutput (debugOutput),
m_offMeshFilePath (offMeshFilePath),
m_skipContinents (skipContinents),
@@ -58,7 +58,8 @@ namespace MMAP
m_skipBattlegrounds (skipBattlegrounds),
m_maxWalkableAngle (maxWalkableAngle),
m_bigBaseUnit (bigBaseUnit),
m_rcContext (NULL)
m_rcContext (nullptr),
_cancelationToken (false)
{
m_terrainBuilder = new TerrainBuilder(skipLiquid);
@@ -88,14 +89,14 @@ namespace MMAP
void MapBuilder::discoverTiles()
{
std::vector<std::string> files;
uint32 mapID, tileX, tileY, tileID, count = 0, fsize = 0;
uint32 mapID, tileX, tileY, tileID, count = 0;
char filter[12];
printf("Discovering maps... ");
getDirContents(files, "maps");
for (uint32 i = 0; i < files.size(); ++i)
{
mapID = uint32(atoi(files[i].substr(0, files[i].size() - 8).c_str()));
mapID = uint32(atoi(files[i].substr(0,3).c_str()));
if (std::find(m_tiles.begin(), m_tiles.end(), mapID) == m_tiles.end())
{
m_tiles.emplace_back(MapTiles(mapID, new std::set<uint32>));
@@ -107,7 +108,7 @@ namespace MMAP
getDirContents(files, "vmaps", "*.vmtree");
for (uint32 i = 0; i < files.size(); ++i)
{
mapID = uint32(atoi(files[i].substr(0, files[i].size() - 7).c_str()));
mapID = uint32(atoi(files[i].substr(0,3).c_str()));
if (std::find(m_tiles.begin(), m_tiles.end(), mapID) == m_tiles.end())
{
m_tiles.emplace_back(MapTiles(mapID, new std::set<uint32>));
@@ -128,10 +129,8 @@ namespace MMAP
getDirContents(files, "vmaps", filter);
for (uint32 i = 0; i < files.size(); ++i)
{
fsize = files[i].size();
tileY = uint32(atoi(files[i].substr(fsize - 12, 2).c_str()));
tileX = uint32(atoi(files[i].substr(fsize - 9, 2).c_str()));
tileX = uint32(atoi(files[i].substr(7,2).c_str()));
tileY = uint32(atoi(files[i].substr(4,2).c_str()));
tileID = StaticMapTree::packTileID(tileY, tileX);
tiles->insert(tileID);
@@ -143,20 +142,36 @@ namespace MMAP
getDirContents(files, "maps", filter);
for (uint32 i = 0; i < files.size(); ++i)
{
fsize = files[i].size();
tileY = uint32(atoi(files[i].substr(fsize - 8, 2).c_str()));
tileX = uint32(atoi(files[i].substr(fsize - 6, 2).c_str()));
tileY = uint32(atoi(files[i].substr(3,2).c_str()));
tileX = uint32(atoi(files[i].substr(5,2).c_str()));
tileID = StaticMapTree::packTileID(tileX, tileY);
if (tiles->insert(tileID).second)
count++;
}
// make sure we process maps which don't have tiles
if (tiles->empty())
{
// convert coord bounds to grid bounds
uint32 minX, minY, maxX, maxY;
getGridBounds(mapID, minX, minY, maxX, maxY);
// add all tiles within bounds to tile list.
for (uint32 i = minX; i <= maxX; ++i)
for (uint32 j = minY; j <= maxY; ++j)
if (tiles->insert(StaticMapTree::packTileID(i, j)).second)
count++;
}
}
printf("found %u.\n\n", count);
// percentageDone - total tiles to process
m_totalTiles = count;
// Calculate tiles to process in total
for (TileList::iterator it = m_tiles.begin(); it != m_tiles.end(); ++it)
{
if (!shouldSkipMap(it->m_mapId))
m_totalTiles += it->m_tiles->size();
}
}
/**************************************************************************/
@@ -172,11 +187,14 @@ namespace MMAP
}
/**************************************************************************/
void MapBuilder::buildAllMaps(int threads)
void MapBuilder::buildAllMaps(unsigned int threads)
{
std::vector<BuilderThread*> _threads;
printf("Using %u threads to extract mmaps\n", threads);
BuilderThreadPool* pool = threads > 0 ? new BuilderThreadPool() : NULL;
for (unsigned int i = 0; i < threads; ++i)
{
_workerThreads.push_back(std::thread(&MapBuilder::WorkerThread, this));
}
m_tiles.sort([](MapTiles a, MapTiles b)
{
@@ -185,27 +203,29 @@ namespace MMAP
for (TileList::iterator it = m_tiles.begin(); it != m_tiles.end(); ++it)
{
uint32 mapID = it->m_mapId;
if (!shouldSkipMap(mapID))
uint32 mapId = it->m_mapId;
if (!shouldSkipMap(mapId))
{
if (threads > 0)
pool->Enqueue(new MapBuildRequest(mapID));
_queue.Push(mapId);
else
buildMap(mapID);
buildMap(mapId);
}
}
for (int i = 0; i < threads; ++i)
_threads.push_back(new BuilderThread(this, pool->Queue()));
// Free memory
for (std::vector<BuilderThread*>::iterator _th = _threads.begin(); _th != _threads.end(); ++_th)
while (!_queue.Empty())
{
(*_th)->wait();
delete *_th;
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
delete pool;
_cancelationToken = true;
_queue.Cancel();
for (auto& thread : _workerThreads)
{
thread.join();
}
}
/**************************************************************************/
@@ -277,7 +297,7 @@ namespace MMAP
return;
}
dtNavMesh* navMesh = NULL;
dtNavMesh* navMesh = nullptr;
buildNavMesh(mapId, navMesh);
if (!navMesh)
{
@@ -341,7 +361,7 @@ namespace MMAP
/**************************************************************************/
void MapBuilder::buildSingleTile(uint32 mapID, uint32 tileX, uint32 tileY)
{
dtNavMesh* navMesh = NULL;
dtNavMesh* navMesh = nullptr;
buildNavMesh(mapID, navMesh);
if (!navMesh)
{
@@ -353,13 +373,24 @@ namespace MMAP
dtFreeNavMesh(navMesh);
}
void MapBuilder::WorkerThread()
{
while (1)
{
uint32 mapId = 0;
_queue.WaitAndPop(mapId);
if (_cancelationToken)
return;
buildMap(mapId);
}
}
/**************************************************************************/
void MapBuilder::buildMap(uint32 mapID)
{
#ifndef __APPLE__
//printf("[Thread %u] Building map %03u:\n", uint32(ACE_Thread::self()), mapID);
#endif
std::set<uint32>* tiles = getTileList(mapID);
// make sure we process maps which don't have tiles
@@ -378,7 +409,7 @@ namespace MMAP
if (!tiles->empty())
{
// build navMesh
dtNavMesh* navMesh = NULL;
dtNavMesh* navMesh = nullptr;
buildNavMesh(mapID, navMesh);
if (!navMesh)
{
@@ -485,7 +516,7 @@ namespace MMAP
// use Max because '32 - tileX' is negative for values over 32
float bmin[3], bmax[3];
getTileBounds(tileXMax, tileYMax, NULL, 0, bmin, bmax);
getTileBounds(tileXMax, tileYMax, nullptr, 0, bmin, bmax);
/*** now create the navmesh ***/
@@ -683,11 +714,11 @@ namespace MMAP
// we may want to keep them in the future for debug
// but right now, we don't have the code to merge them
rcFreeHeightField(tile.solid);
tile.solid = NULL;
tile.solid = nullptr;
rcFreeCompactHeightfield(tile.chf);
tile.chf = NULL;
tile.chf = nullptr;
rcFreeContourSet(tile.cset);
tile.cset = NULL;
tile.cset = nullptr;
pmmerge[nmerge] = tile.pmesh;
dmmerge[nmerge] = tile.dmesh;
@@ -764,7 +795,7 @@ namespace MMAP
params.buildBvTree = true;
// will hold final navmesh
unsigned char* navData = NULL;
unsigned char* navData = nullptr;
int navDataSize = 0;
do
@@ -832,7 +863,7 @@ namespace MMAP
char message[1024];
sprintf(message, "[Map %03i] Failed to open %s for writing!\n", mapID, fileName);
perror(message);
navMesh->removeTile(tileRef, NULL, NULL);
navMesh->removeTile(tileRef, nullptr, nullptr);
break;
}
@@ -849,7 +880,7 @@ namespace MMAP
fclose(file);
// now that tile is written to disk, we can unload it
navMesh->removeTile(tileRef, NULL, NULL);
navMesh->removeTile(tileRef, nullptr, nullptr);
}
while (0);

View File

@@ -7,31 +7,27 @@
#ifndef _MAP_BUILDER_H
#define _MAP_BUILDER_H
#include <list>
#include <vector>
#include <set>
#include <atomic>
#include <map>
#include <list>
#include <thread>
#include "TerrainBuilder.h"
#include "IntermediateValues.h"
#include "Recast.h"
#include "DetourNavMesh.h"
#include <ace/Task.h>
#include <ace/Activation_Queue.h>
#include <ace/Method_Request.h>
#include "PCQueue.h"
using namespace VMAP;
// G3D namespace typedefs conflicts with ACE typedefs
namespace MMAP
{
struct MapTiles
{
MapTiles() : m_mapId(uint32(-1)), m_tiles(NULL) {}
MapTiles() : m_mapId(uint32(-1)), m_tiles(nullptr) {}
MapTiles(uint32 id, std::set<uint32>* tiles) : m_mapId(id), m_tiles(tiles) {}
~MapTiles() {}
@@ -49,7 +45,7 @@ namespace MMAP
struct Tile
{
Tile() : chf(NULL), solid(NULL), cset(NULL), pmesh(NULL), dmesh(NULL) {}
Tile() : chf(nullptr), solid(nullptr), cset(nullptr), pmesh(nullptr), dmesh(nullptr) {}
~Tile()
{
rcFreeCompactHeightfield(chf);
@@ -75,7 +71,7 @@ namespace MMAP
bool skipBattlegrounds = false,
bool debugOutput = false,
bool bigBaseUnit = false,
const char* offMeshFilePath = NULL);
const char* offMeshFilePath = nullptr);
~MapBuilder();
@@ -87,7 +83,9 @@ namespace MMAP
void buildSingleTile(uint32 mapID, uint32 tileX, uint32 tileY);
// builds list of maps, then builds all of mmap tiles (based on the skip settings)
void buildAllMaps(int threads);
void buildAllMaps(unsigned int threads);
void WorkerThread();
private:
// detect maps and tiles
@@ -131,68 +129,15 @@ namespace MMAP
float m_maxWalkableAngle;
bool m_bigBaseUnit;
// percentageDone - variables to calculate percentage
uint32 m_totalTiles;
std::atomic<uint32> m_totalTiles;
std::atomic<uint32> m_totalTilesBuilt;
// build performance - not really used for now
rcContext* m_rcContext;
};
class MapBuildRequest : public ACE_Method_Request
{
public:
MapBuildRequest(uint32 mapId) : _mapId(mapId) {}
virtual int call()
{
/// @ Actually a creative way of unabstracting the class and returning a member variable
return (int)_mapId;
}
private:
uint32 _mapId;
};
class BuilderThread : public ACE_Task_Base
{
private:
MapBuilder* _builder;
ACE_Activation_Queue* _queue;
public:
BuilderThread(MapBuilder* builder, ACE_Activation_Queue* queue) : _builder(builder), _queue(queue) { activate(); }
int svc()
{
/// @ Set a timeout for dequeue attempts (only used when the queue is empty) as it will never get populated after thread starts
ACE_Time_Value timeout(5);
ACE_Method_Request* request = NULL;
while ((request = _queue->dequeue(&timeout)) != NULL)
{
_builder->buildMap(request->call());
delete request;
request = NULL;
}
return 0;
}
};
class BuilderThreadPool
{
public:
BuilderThreadPool() : _queue(new ACE_Activation_Queue()) {}
~BuilderThreadPool() { _queue->queue()->close(); delete _queue; }
void Enqueue(MapBuildRequest* request)
{
_queue->enqueue(request);
}
ACE_Activation_Queue* Queue() { return _queue; }
private:
ACE_Activation_Queue* _queue;
std::vector<std::thread> _workerThreads;
ProducerConsumerQueue<uint32> _queue;
std::atomic<bool> _cancelationToken;
};
}

View File

@@ -106,7 +106,7 @@ namespace MMAP
while (dirp)
{
errno = 0;
if ((dp = readdir(dirp)) != NULL)
if ((dp = readdir(dirp)) != nullptr)
{
if (matchWildcardFilter(filter.c_str(), dp->d_name))
fileList.push_back(std::string(dp->d_name));

View File

@@ -61,9 +61,9 @@ bool handleArgs(int argc, char** argv,
bool &bigBaseUnit,
char* &offMeshInputPath,
char* &file,
int& threads)
unsigned int& threads)
{
char* param = NULL;
char* param = nullptr;
for (int i = 1; i < argc; ++i)
{
if (strcmp(argv[i], "--maxAngle") == 0)
@@ -83,8 +83,7 @@ bool handleArgs(int argc, char** argv,
param = argv[++i];
if (!param)
return false;
threads = atoi(param);
printf("Using %i threads to extract mmaps\n", threads);
threads = static_cast<unsigned int>(std::max(0, atoi(param)));
}
else if (strcmp(argv[i], "--file") == 0)
{
@@ -100,7 +99,7 @@ bool handleArgs(int argc, char** argv,
return false;
char* stileX = strtok(param, ",");
char* stileY = strtok(NULL, ",");
char* stileY = strtok(nullptr, ",");
int tilex = atoi(stileX);
int tiley = atoi(stileY);
@@ -230,7 +229,8 @@ int finish(const char* message, int returnValue)
int main(int argc, char** argv)
{
int threads = 3, mapnum = -1;
unsigned int threads = std::thread::hardware_concurrency();
int mapnum = -1;
float maxAngle = 70.0f;
int tileX = -1, tileY = -1;
bool skipLiquid = false,
@@ -240,8 +240,8 @@ int main(int argc, char** argv)
debugOutput = false,
silent = false,
bigBaseUnit = false;
char* offMeshInputPath = NULL;
char* file = NULL;
char* offMeshInputPath = nullptr;
char* file = nullptr;
bool validParam = handleArgs(argc, argv, mapnum,
tileX, tileY, maxAngle,

View File

@@ -270,7 +270,7 @@ namespace MMAP
printf("TerrainBuilder::loadMap: Failed to read some data expected 1, read 0\n");
float* liquid_map = NULL;
float* liquid_map = nullptr;
if (!(lheader.flags & MAP_LIQUID_NO_TYPE))
if (fread(liquid_type, sizeof(liquid_type), 1, mapFile) != 1)
@@ -363,7 +363,7 @@ namespace MMAP
// make a copy of liquid vertices
// used to pad right-bottom frame due to lost vertex data at extraction
float* lverts_copy = NULL;
float* lverts_copy = nullptr;
if (meshData.liquidVerts.size())
{
lverts_copy = new float[meshData.liquidVerts.size()];
@@ -636,7 +636,7 @@ namespace MMAP
if (!instanceTrees[mapID])
break;
ModelInstance* models = NULL;
ModelInstance* models = nullptr;
uint32 count = 0;
instanceTrees[mapID]->getModelInstances(models, count);
@@ -673,7 +673,7 @@ namespace MMAP
std::vector<G3D::Vector3> tempVertices;
std::vector<G3D::Vector3> transformedVertices;
std::vector<MeshTriangle> tempTriangles;
WmoLiquid* liquid = NULL;
WmoLiquid* liquid = nullptr;
it->getMeshData(tempVertices, tempTriangles, liquid);
@@ -884,7 +884,7 @@ namespace MMAP
void TerrainBuilder::loadOffMeshConnections(uint32 mapID, uint32 tileX, uint32 tileY, MeshData &meshData, const char* offMeshFilePath)
{
// no meshfile input given?
if (offMeshFilePath == NULL)
if (offMeshFilePath == nullptr)
return;
FILE* fp = fopen(offMeshFilePath, "rb");

View File

@@ -18,7 +18,7 @@ char const* GetPlainName(char const* FileName)
{
const char * szTemp;
if((szTemp = strrchr(FileName, '\\')) != NULL)
if((szTemp = strrchr(FileName, '\\')) != nullptr)
FileName = szTemp + 1;
return FileName;
}
@@ -27,7 +27,7 @@ char* GetPlainName(char* FileName)
{
char * szTemp;
if((szTemp = strrchr(FileName, '\\')) != NULL)
if((szTemp = strrchr(FileName, '\\')) != nullptr)
FileName = szTemp + 1;
return FileName;
}
@@ -59,10 +59,10 @@ char* GetExtension(char* FileName)
{
if (char* szTemp = strrchr(FileName, '.'))
return szTemp;
return NULL;
return nullptr;
}
ADTFile::ADTFile(char* filename): ADT(filename), nWMO(0), nMDX(0), WmoInstansName(NULL), ModelInstansName(NULL)
ADTFile::ADTFile(char* filename): ADT(filename), nWMO(0), nMDX(0), WmoInstansName(nullptr), ModelInstansName(nullptr)
{
Adtfilename.append(filename);
}

View File

@@ -12,7 +12,7 @@
#include <cstdio>
DBCFile::DBCFile(const std::string& filename):
filename(filename), recordSize(0), recordCount(0), fieldCount(0), stringSize(0), data(NULL), stringTable(NULL)
filename(filename), recordSize(0), recordCount(0), fieldCount(0), stringSize(0), data(nullptr), stringTable(nullptr)
{
}
@@ -34,7 +34,7 @@ bool DBCFile::open()
if (header[0]!='W' || header[1]!='D' || header[2]!='B' || header[3] != 'C')
{
f.close();
data = NULL;
data = nullptr;
printf("Critical Error: An error occured while trying to read the DBCFile %s.", filename.c_str());
return false;
}

View File

@@ -23,8 +23,8 @@ private:
{
delete[] vertices;
delete[] indices;
vertices = NULL;
indices = NULL;
vertices = nullptr;
indices = nullptr;
}
std::string filename;
public:

View File

@@ -42,13 +42,13 @@ public:
token = strtok( buffer, seps );
uint32 counter = 0;
while ((token != NULL) && (counter < size)) {
while ((token != nullptr) && (counter < size)) {
//cout << token << endl;
token[strlen(token) - 1] = 0;
string s = token;
filelist.push_back(s);
counter += strlen(token) + 2;
token = strtok(NULL, seps);
token = strtok(nullptr, seps);
}
delete[] buffer;

View File

@@ -148,7 +148,7 @@ bool ExtractSingleWmo(std::string& fname)
int p = 0;
// Select root wmo files
char const* rchr = strrchr(plain_name, '_');
if (rchr != NULL)
if (rchr != nullptr)
{
char cpy[4];
memcpy(cpy, rchr, 4);

View File

@@ -13,12 +13,12 @@ char * wdtGetPlainName(char * FileName)
{
char * szTemp;
if((szTemp = strrchr(FileName, '\\')) != NULL)
if((szTemp = strrchr(FileName, '\\')) != nullptr)
FileName = szTemp + 1;
return FileName;
}
WDTFile::WDTFile(char* file_name, char* file_name1) : WDT(file_name), gWmoInstansName(NULL), gnWMO(0)
WDTFile::WDTFile(char* file_name, char* file_name1) : WDT(file_name), gWmoInstansName(nullptr), gnWMO(0)
{
filename.append(file_name1,strlen(file_name1));
}
@@ -109,7 +109,7 @@ WDTFile::~WDTFile(void)
ADTFile* WDTFile::GetMap(int x, int z)
{
if(!(x>=0 && z >= 0 && x<64 && z<64))
return NULL;
return nullptr;
char name[512];

View File

@@ -473,7 +473,7 @@ WMOGroup::~WMOGroup()
}
WMOInstance::WMOInstance(MPQFile& f, char const* WmoInstName, uint32 mapID, uint32 tileX, uint32 tileY, FILE* pDirfile)
: currx(0), curry(0), wmo(NULL), doodadset(0), pos(), indx(0), id(0), d2(0), d3(0)
: currx(0), curry(0), wmo(nullptr), doodadset(0), pos(), indx(0), id(0), d2(0), d3(0)
{
float ff[3];
f.read(&id, 4);