From 9b97a89ef8e1dd1694985d1fc8e88808da1550cd Mon Sep 17 00:00:00 2001 From: Subv Date: Fri, 28 Sep 2012 17:09:44 -0500 Subject: [PATCH] Tools/MeshExtractor: Fixed a crash and some other mistakes. First glances of multithreading ( need to figure out a way around StormLib ) --- src/tools/mesh_extractor/ContinentBuilder.cpp | 92 ++++++++++++++----- src/tools/mesh_extractor/MapChunk.cpp | 1 + src/tools/mesh_extractor/MeshExtractor.cpp | 2 +- src/tools/mesh_extractor/TileBuilder.cpp | 2 +- src/tools/mesh_extractor/TileBuilder.h | 3 +- 5 files changed, 75 insertions(+), 25 deletions(-) diff --git a/src/tools/mesh_extractor/ContinentBuilder.cpp b/src/tools/mesh_extractor/ContinentBuilder.cpp index 7391ebb8767..02a4d3626b7 100644 --- a/src/tools/mesh_extractor/ContinentBuilder.cpp +++ b/src/tools/mesh_extractor/ContinentBuilder.cpp @@ -3,6 +3,49 @@ #include "WDT.h" #include "Utils.h" #include "DetourNavMesh.h" +#include "ace/Task.h" + +class BuilderThread : public ACE_Task +{ +private: + int X, Y, MapId; + std::string Continent; +public: + BuilderThread() : Free(true) {} + void SetData(int x, int y, int map, std::string cont) { X = x; Y = y; MapId = map; Continent = cont; } + + int svc() + { + Free = false; + TileBuilder builder(Continent, X, Y, MapId); + char buff[100]; + sprintf(buff, "%03u%02u%02u.mmtile", MapId, X, Y); + FILE* f = fopen(buff, "r"); + if (f) // Check if file already exists. + { + fclose(f); + Free = true; + return 0; + } + uint8* nav = builder.Build(); + if (nav) + { + fclose(f); + f = fopen(buff, "wb"); + MmapTileHeader header; + header.size = builder.DataSize; + fwrite(&header, sizeof(MmapTileHeader), 1, f); + fwrite(nav, sizeof(unsigned char), builder.DataSize, f); + fclose(f); + } + dtFree(nav); + printf("[%02u,%02u] Tile Built!\n", X, Y); + Free = true; + return 0; + } + + bool Free; +}; void ContinentBuilder::Build() { @@ -19,29 +62,36 @@ void ContinentBuilder::Build() params.tileWidth = 533.33333f; fwrite(¶ms, sizeof(dtNavMeshParams), 1, mmap); fclose(mmap); + std::vector Threads; + /*for (uint32 i = 0; i < 1; ++i) + Threads.push_back(new BuilderThread());*/ for (std::vector::iterator itr = TileMap->TileTable.begin(); itr != TileMap->TileTable.end(); ++itr) { - TileBuilder builder(Continent, itr->X, itr->Y, TileMap, MapId); - char buff[100]; - sprintf(buff, "%03u%02u%02u.mmtile", MapId, itr->X, itr->Y); - FILE* f = fopen(buff, "r"); - if (f) // Check if file already exists. + BuilderThread th; + th.SetData(itr->X, itr->Y, MapId, Continent); + th.svc(); + /*bool next = false; + while (!next) { - fclose(f); - continue; - } - uint8* nav = builder.Build(); - if (nav) - { - fclose(f); - f = fopen(buff, "wb"); - MmapTileHeader header; - header.size = builder.DataSize; - fwrite(&header, sizeof(MmapTileHeader), 1, f); - fwrite(nav, sizeof(unsigned char), builder.DataSize, f); - fclose(f); - } - dtFree(nav); - printf("[%02u,%02u] Tile Built!\n", itr->X, itr->Y); + for (std::vector::iterator _th = Threads.begin(); _th != Threads.end(); ++_th) + { + if ((*_th)->Free) + { + (*_th)->SetData(itr->X, itr->Y, MapId, Continent); + (*_th)->activate(); + next = true; + break; + } + } + // Wait for 20 seconds + ACE_OS::sleep(ACE_Time_Value (0, 20000)); + }*/ } + + /*// Free memory + for (std::vector::iterator _th = Threads.begin(); _th != Threads.end(); ++_th) + { + (*_th)->wait(); + delete *_th; + }*/ } diff --git a/src/tools/mesh_extractor/MapChunk.cpp b/src/tools/mesh_extractor/MapChunk.cpp index 61c106f9766..debde08cda8 100644 --- a/src/tools/mesh_extractor/MapChunk.cpp +++ b/src/tools/mesh_extractor/MapChunk.cpp @@ -7,6 +7,7 @@ MapChunk::MapChunk( ADT* _adt, Chunk* chunk ) : Adt(_adt), Source(chunk) FILE* stream = chunk->GetStream(); Header.Read(stream); fseek(stream, chunk->Offset, SEEK_SET); + Index = Header.IndexX + Header.IndexY * 16; GenerateVertices(stream); } diff --git a/src/tools/mesh_extractor/MeshExtractor.cpp b/src/tools/mesh_extractor/MeshExtractor.cpp index a99cc4475a0..7ae6afadb84 100644 --- a/src/tools/mesh_extractor/MeshExtractor.cpp +++ b/src/tools/mesh_extractor/MeshExtractor.cpp @@ -23,7 +23,7 @@ void ExtractAllMaps(uint32 onlyMap) WDT wdt("World\\maps\\" + name + "\\" + name + ".wdt"); if (!wdt.IsValid || wdt.IsGlobalModel) continue; - printf("Building %s MapId %u\n", name, mapId); + printf("Building %s MapId %u\n", name.c_str(), mapId); ContinentBuilder builder(name, mapId, &wdt); builder.Build(); } diff --git a/src/tools/mesh_extractor/TileBuilder.cpp b/src/tools/mesh_extractor/TileBuilder.cpp index 8bbfd5afb69..baf1fa0375c 100644 --- a/src/tools/mesh_extractor/TileBuilder.cpp +++ b/src/tools/mesh_extractor/TileBuilder.cpp @@ -9,7 +9,7 @@ #include "RecastAlloc.h" #include "DetourNavMeshBuilder.h" -TileBuilder::TileBuilder(std::string world, int x, int y, WDT* wdt, uint32 mapId) : _Geometry(NULL), World(world), X(x), Y(y), MapId(mapId), DataSize(0), Wdt(wdt) +TileBuilder::TileBuilder(std::string world, int x, int y, uint32 mapId) : _Geometry(NULL), World(world), X(x), Y(y), MapId(mapId), DataSize(0) { // Cell Size = TileSize / TileVoxelSize // 1800 = TileVoxelSize diff --git a/src/tools/mesh_extractor/TileBuilder.h b/src/tools/mesh_extractor/TileBuilder.h index 261675af7e2..f91a732f70c 100644 --- a/src/tools/mesh_extractor/TileBuilder.h +++ b/src/tools/mesh_extractor/TileBuilder.h @@ -10,7 +10,7 @@ class WDT; class TileBuilder { public: - TileBuilder(std::string world, int x, int y, WDT* wdt, uint32 mapId); + TileBuilder(std::string world, int x, int y, uint32 mapId); void CalculateTileBounds(float*& bmin, float*& bmax); uint8* Build(); @@ -22,6 +22,5 @@ public: rcContext* Context; Geometry* _Geometry; uint32 DataSize; - WDT* Wdt; }; #endif \ No newline at end of file