diff options
| author | Sebastian Valle <s.v.h21@hotmail.com> | 2013-10-05 22:18:38 -0500 |
|---|---|---|
| committer | Sebastian Valle <s.v.h21@hotmail.com> | 2013-10-05 22:18:38 -0500 |
| commit | 79ad827895acc0439336a48055164c4242a89a90 (patch) | |
| tree | eea59c5118334a0c937588cf7b03b1d37ca7f0ad /src | |
| parent | 64ab895f1e36470bc0623111d77bf7b815984ed1 (diff) | |
Tools/MeshExtractor: Rewrote the MeshExtractor to use a custom Stream class instead of FILE pointers, this way we avoid the errors that were caused by reaching the maximum limit of the tmpfile function, and the maximum number of open file streams at any given time.
P.S: This was no easy task, damnit.
Diffstat (limited to 'src')
30 files changed, 468 insertions, 512 deletions
diff --git a/src/tools/mesh_extractor/ADT.cpp b/src/tools/mesh_extractor/ADT.cpp index 79ece1213d4..3d3fbdd6365 100644 --- a/src/tools/mesh_extractor/ADT.cpp +++ b/src/tools/mesh_extractor/ADT.cpp @@ -8,7 +8,7 @@ ADT::ADT( std::string file, int x, int y ) : ObjectData(NULL), Data(NULL), HasOb { Data = new ChunkedData(file); ObjectData = new ChunkedData(file); - if (ObjectData->Stream) + if (ObjectData->_Stream) HasObjectData = true; else ObjectData = NULL; diff --git a/src/tools/mesh_extractor/CMakeLists.txt b/src/tools/mesh_extractor/CMakeLists.txt index 9ed8472051d..f5dbb0fd7ff 100644 --- a/src/tools/mesh_extractor/CMakeLists.txt +++ b/src/tools/mesh_extractor/CMakeLists.txt @@ -47,4 +47,4 @@ if( UNIX ) install(TARGETS MeshExtractor DESTINATION bin) elseif( WIN32 ) install(TARGETS MeshExtractor DESTINATION "${CMAKE_INSTALL_PREFIX}") -endif() +endif()
\ No newline at end of file diff --git a/src/tools/mesh_extractor/Chunk.cpp b/src/tools/mesh_extractor/Chunk.cpp index 4605ae0f0dd..a89bb5cadce 100644 --- a/src/tools/mesh_extractor/Chunk.cpp +++ b/src/tools/mesh_extractor/Chunk.cpp @@ -8,24 +8,24 @@ int32 Chunk::FindSubChunkOffset(std::string name) if (name.size() != 4) return -1; - FILE* stream = GetStream(); + Stream* stream = GetStream(); uint32 matched = 0; - while (uint32(ftell(stream)) < Utils::Size(stream)) + while (stream->GetPos() < stream->GetSize()) { - char b = 0; - if (fread(&b, sizeof(char), 1, stream) != 1 || b != name[matched]) + char b = stream->Read<char>(); + if (b != name[matched]) matched = 0; else ++matched; if (matched == 4) - return ftell(stream) - 4; + return stream->GetPos() - 4; } return -1; } -FILE* Chunk::GetStream() +Stream* Chunk::GetStream() { - fseek(Stream, Offset, SEEK_SET); - return Stream; + _Stream->Seek(Offset, SEEK_SET); + return _Stream; } diff --git a/src/tools/mesh_extractor/Chunk.h b/src/tools/mesh_extractor/Chunk.h index f3681a9f576..917a85531da 100644 --- a/src/tools/mesh_extractor/Chunk.h +++ b/src/tools/mesh_extractor/Chunk.h @@ -2,19 +2,21 @@ #define CHUNK_H #include "Define.h" #include <string> +#include "Stream.h" + class ChunkedData; class Chunk { public: - Chunk(const char* name, uint32 length, uint32 offset, FILE* stream) : Name(name), Length(length), Offset(offset), Stream(stream) {} + Chunk(const char* name, uint32 length, uint32 offset, Stream* stream) : Name(name), Length(length), Offset(offset), _Stream(stream) {} int32 FindSubChunkOffset(std::string name); - FILE* GetStream(); + Stream* GetStream(); std::string Name; uint32 Length; uint32 Offset; - FILE* Stream; + Stream* _Stream; }; #endif
\ No newline at end of file diff --git a/src/tools/mesh_extractor/ChunkedData.cpp b/src/tools/mesh_extractor/ChunkedData.cpp index f273ef946dd..cec6dae7fbe 100644 --- a/src/tools/mesh_extractor/ChunkedData.cpp +++ b/src/tools/mesh_extractor/ChunkedData.cpp @@ -4,18 +4,18 @@ #include <string> -ChunkedData::ChunkedData( FILE* stream, uint32 maxLength, uint32 chunksHint /*= 300*/ ) : -Stream(stream) +ChunkedData::ChunkedData(Stream* stream, uint32 maxLength, uint32 chunksHint /*= 300*/ ) : +_Stream(stream) { - if (!Stream) + if (!_Stream) return; Load(maxLength, chunksHint); } ChunkedData::ChunkedData( const std::string& file, uint32 chunksHint /*= 300*/ ) { - Stream = MPQHandler->GetFile(file); - if (!Stream) + _Stream = MPQHandler->GetFile(file); + if (!_Stream) return; Load(0, chunksHint); } @@ -23,27 +23,26 @@ ChunkedData::ChunkedData( const std::string& file, uint32 chunksHint /*= 300*/ ) void ChunkedData::Load( uint32 maxLength, uint32 chunksHint ) { if (!maxLength) - maxLength = Utils::Size(Stream); + maxLength = _Stream->GetSize(); Chunks.reserve(chunksHint); - uint32 baseOffset = ftell(Stream); + uint32 baseOffset = _Stream->GetPos(); uint32 calcOffset = 0; - while ((calcOffset + baseOffset) < Utils::Size(Stream) && (calcOffset < maxLength)) + while ((calcOffset + baseOffset) < _Stream->GetSize() && (calcOffset < maxLength)) { char nameBytes[5]; - uint32 read = fread(&nameBytes, sizeof(char), 4, Stream); - nameBytes[read] = '\0'; + _Stream->Read(nameBytes, sizeof(char) * 4); + nameBytes[4] = '\0'; std::string name = std::string(nameBytes); - // Utils::Reverse(nameBytes); name = std::string(name.rbegin(), name.rend()); - uint32 length; - if (fread(&length, sizeof(uint32), 1, Stream) != 1) - continue; + + uint32 length = _Stream->Read<uint32>(); calcOffset += 8; - Chunks.push_back(new Chunk(name.c_str(), length, calcOffset + baseOffset, Stream)); + + Chunks.push_back(new Chunk(name.c_str(), length, calcOffset + baseOffset, _Stream)); calcOffset += length; // save an extra seek at the end - if ((calcOffset + baseOffset) < Utils::Size(Stream) && calcOffset < maxLength) - fseek(Stream, length, SEEK_CUR); + if ((calcOffset + baseOffset) < _Stream->GetSize() && calcOffset < maxLength) + _Stream->Seek(length, SEEK_CUR); } } @@ -69,6 +68,6 @@ ChunkedData::~ChunkedData() delete *itr; Chunks.clear(); - if (Stream) - fclose(Stream); + if (_Stream) + delete _Stream; } diff --git a/src/tools/mesh_extractor/ChunkedData.h b/src/tools/mesh_extractor/ChunkedData.h index 1e1cb17749e..1b47b54f272 100644 --- a/src/tools/mesh_extractor/ChunkedData.h +++ b/src/tools/mesh_extractor/ChunkedData.h @@ -3,11 +3,12 @@ #include <vector> #include "Chunk.h" +#include "Stream.h" class ChunkedData { public: - ChunkedData(FILE* stream, uint32 maxLength, uint32 chunksHint = 300); + ChunkedData(Stream* stream, uint32 maxLength, uint32 chunksHint = 300); ChunkedData(const std::string &file, uint32 chunksHint = 300); ~ChunkedData(); @@ -16,6 +17,6 @@ public: void Load(uint32 maxLength, uint32 chunksHint); std::vector<Chunk*> Chunks; - FILE* Stream; + Stream* _Stream; }; #endif
\ No newline at end of file diff --git a/src/tools/mesh_extractor/DBC.cpp b/src/tools/mesh_extractor/DBC.cpp index 281a11d10dc..52e4d8c3760 100644 --- a/src/tools/mesh_extractor/DBC.cpp +++ b/src/tools/mesh_extractor/DBC.cpp @@ -2,19 +2,15 @@ #include "DBC.h" #include "Define.h" -DBC::DBC( FILE* stream ) : StringBlock(NULL), StringBlockSize(0), IsFaulty(true) +DBC::DBC(Stream* stream) : StringBlock(NULL), StringBlockSize(0), IsFaulty(true) { - char magic[5]; - uint32 count = 0; - count += fread(&magic, sizeof(char), 4, stream); - magic[4] = '\0'; - count += fread(&RecordCount, sizeof(uint32), 1, stream); + stream->Read(4); // Read the magic "WDBC" + + RecordCount = stream->Read<int>(); Records.reserve(RecordCount); - count += fread(&Fields, sizeof(uint32), 1, stream); - count += fread(&RecordSize, sizeof(uint32), 1, stream); - count += fread(&StringBlockSize, sizeof(uint32), 1, stream); - if (count != 8) - printf("DBC::DBC: Failed to read some data expected 8, read %u\n", count); + Fields = stream->Read<int>(); + RecordSize = stream->Read<int>(); + StringBlockSize = stream->Read<uint32>(); for (int i = 0; i < RecordCount; i++) { @@ -28,17 +24,18 @@ DBC::DBC( FILE* stream ) : StringBlock(NULL), StringBlockSize(0), IsFaulty(true) IsFaulty = true; break; } - uint32 tmp; - if (fread(&tmp, sizeof(uint32), 1, stream) != 1) - printf("DBC::DBC: Failed to read some data expected 1, read 0\n"); - rec->Values.push_back(tmp); + rec->Values.push_back(stream->Read<uint32>()); size += 4; } } - StringBlock = new uint8[StringBlockSize]; - count = fread(StringBlock, sizeof(uint8), StringBlockSize, stream); - if (count != StringBlockSize) - printf("DBC::DBC: Failed to read some data expected %u, read %u\n", StringBlockSize, count); + StringBlock = (uint8*)stream->Read(StringBlockSize); +} + +DBC::~DBC() +{ + delete[] StringBlock; + for (std::vector<Record*>::iterator itr = Records.begin(); itr != Records.end(); ++itr) + delete *itr; } std::string DBC::GetStringByOffset( int offset ) @@ -56,7 +53,7 @@ std::string DBC::GetStringByOffset( int offset ) strcpy(d, (const char*)(StringBlock + offset)); d[len] = '\0'; std::string val = std::string(d); - delete [] d; + delete[] d; return val; } diff --git a/src/tools/mesh_extractor/DBC.h b/src/tools/mesh_extractor/DBC.h index 5ed57754e73..339e896949d 100644 --- a/src/tools/mesh_extractor/DBC.h +++ b/src/tools/mesh_extractor/DBC.h @@ -3,13 +3,15 @@ #include <vector> #include <string> #include "Define.h" +#include "Stream.h" class Record; class DBC { public: - DBC(FILE* stream); + DBC(Stream* stream); + ~DBC(); std::string GetStringByOffset(int offset); diff --git a/src/tools/mesh_extractor/DoodadHandler.cpp b/src/tools/mesh_extractor/DoodadHandler.cpp index 72c051500e1..4619f418e55 100644 --- a/src/tools/mesh_extractor/DoodadHandler.cpp +++ b/src/tools/mesh_extractor/DoodadHandler.cpp @@ -17,20 +17,18 @@ DoodadHandler::DoodadHandler( ADT* adt ) : ReadDoodadPaths(mmid, mmdx); } -void DoodadHandler::ProcessInternal( MapChunk* mcnk ) +void DoodadHandler::ProcessInternal(MapChunk* mcnk) { if (!IsSane()) return; uint32 refCount = mcnk->Header.DoodadRefs; - FILE* stream = mcnk->Source->GetStream(); - fseek(stream, mcnk->Source->Offset + mcnk->Header.OffsetMCRF, SEEK_SET); + Stream* stream = mcnk->Source->GetStream(); + stream->Seek(mcnk->Source->Offset + mcnk->Header.OffsetMCRF, SEEK_SET); + for (uint32 i = 0; i < refCount; i++) { - int32 index; - int32 count; - if ((count = fread(&index, sizeof(int32), 1, stream)) != 1) - printf("DoodadHandler::ProcessInternal: Failed to read some data expected 1, read %d\n", count); + int32 index = stream->Read<int32>(); if (index < 0 || uint32(index) >= _definitions->size()) continue; DoodadDefinition doodad = (*_definitions)[index]; @@ -56,7 +54,7 @@ void DoodadHandler::ProcessInternal( MapChunk* mcnk ) InsertModelGeometry(doodad, model); } // Restore the stream position - fseek(stream, mcnk->Source->Offset, SEEK_SET); + stream->Seek(mcnk->Source->Offset, SEEK_SET); } void DoodadHandler::ReadDoodadDefinitions( Chunk* chunk ) @@ -64,7 +62,7 @@ void DoodadHandler::ReadDoodadDefinitions( Chunk* chunk ) int32 count = chunk->Length / 36; _definitions = new std::vector<DoodadDefinition>; _definitions->reserve(count); - FILE* stream = chunk->GetStream(); + Stream* stream = chunk->GetStream(); for (int i = 0; i < count; i++) { DoodadDefinition def; @@ -80,14 +78,13 @@ void DoodadHandler::ReadDoodadPaths( Chunk* id, Chunk* data ) _paths->reserve(paths); for (int i = 0; i < paths; i++) { - FILE* idStream = id->GetStream(); - fseek(idStream, i * 4, SEEK_CUR); - uint32 offset; - if (fread(&offset, sizeof(uint32), 1, idStream) != 1) - printf("DoodadHandler::ReadDoodadPaths: Failed to read some data expected 1, read 0\n"); - FILE* dataStream = data->GetStream(); - fseek(dataStream, offset + data->Offset, SEEK_SET); - _paths->push_back(Utils::ReadString(dataStream)); + Stream* idStream = id->GetStream(); + idStream->Seek(i * 4, SEEK_CUR); + uint32 offset = idStream->Read<uint32>(); + + Stream* dataStream = data->GetStream(); + dataStream->Seek(offset + data->Offset, SEEK_SET); + _paths->push_back(dataStream->ReadString()); } } diff --git a/src/tools/mesh_extractor/DoodadHandler.h b/src/tools/mesh_extractor/DoodadHandler.h index 51377f32d01..fcf8c2ede28 100644 --- a/src/tools/mesh_extractor/DoodadHandler.h +++ b/src/tools/mesh_extractor/DoodadHandler.h @@ -4,6 +4,7 @@ #include "Utils.h" #include "Chunk.h" #include "Model.h" +#include "Stream.h" #include <set> #include <vector> @@ -22,18 +23,14 @@ public: return Vector3(vec.z, vec.x, vec.y); } - void Read(FILE* stream) + void Read(Stream* stream) { - int count = 0; - - count += fread(&MmidIndex, sizeof(uint32), 1, stream); - count += fread(&UniqueId, sizeof(uint32), 1, stream); - Position = (Vector3::Read(stream)); + MmidIndex = stream->Read<uint32>(); + UniqueId = stream->Read<uint32>(); + Position = Vector3::Read(stream); Rotation = Vector3::Read(stream); - count += fread(&DecimalScale, sizeof(uint16), 1, stream); - count += fread(&Flags, sizeof(uint16), 1, stream); - if (count != 4) - printf("DoodadDefinition::Read: Failed to read some data expected 4, read %d\n", count); + DecimalScale = stream->Read<uint16>(); + Flags = stream->Read<uint16>(); } }; diff --git a/src/tools/mesh_extractor/LiquidHandler.cpp b/src/tools/mesh_extractor/LiquidHandler.cpp index 33a661a9adf..6317748ea23 100644 --- a/src/tools/mesh_extractor/LiquidHandler.cpp +++ b/src/tools/mesh_extractor/LiquidHandler.cpp @@ -15,7 +15,7 @@ void LiquidHandler::HandleNewLiquid() Vertices.reserve(1000); Triangles.reserve(1000); - FILE* stream = chunk->GetStream(); + Stream* stream = chunk->GetStream(); H2OHeader header[256]; MCNKData.reserve(256); for (int i = 0; i < 256; i++) @@ -30,7 +30,7 @@ void LiquidHandler::HandleNewLiquid() MCNKData.push_back(MCNKLiquidData(NULL, H2ORenderMask())); continue; } - fseek(stream, chunk->Offset + h.OffsetInformation, SEEK_SET); + stream->Seek(chunk->Offset + h.OffsetInformation, SEEK_SET); H2OInformation information = H2OInformation::Read(stream); float** heights = new float*[9]; @@ -43,24 +43,22 @@ void LiquidHandler::HandleNewLiquid() H2ORenderMask renderMask; if (information.LiquidType != 2) { - fseek(stream, chunk->Offset + h.OffsetRender, SEEK_SET); + stream->Seek(chunk->Offset + h.OffsetRender, SEEK_SET); renderMask = H2ORenderMask::Read(stream); if ((Utils::IsAllZero(renderMask.Mask, 8) || (information.Width == 8 && information.Height == 8)) && information.OffsetMask2) { - fseek(stream, chunk->Offset + information.OffsetMask2, SEEK_SET); + stream->Seek(chunk->Offset + information.OffsetMask2, SEEK_SET); uint32 size = ceil(information.Width * information.Height / 8.0f); - uint8* altMask = new uint8[size]; - if (fread(altMask, sizeof(uint8), size, stream) == size) - for (uint32 mi = 0; mi < size; mi++) - renderMask.Mask[mi + information.OffsetY] |= altMask[mi]; + uint8* altMask = (uint8*)stream->Read(size); + for (uint32 mi = 0; mi < size; mi++) + renderMask.Mask[mi + information.OffsetY] |= altMask[mi]; delete[] altMask; } - fseek(stream, chunk->Offset + information.OffsetHeightmap, SEEK_SET); + stream->Seek(chunk->Offset + information.OffsetHeightmap, SEEK_SET); for (int y = information.OffsetY; y < (information.OffsetY + information.Height); y++) for (int x = information.OffsetX; x < (information.OffsetX + information.Width); x++) - if (fread(&heights[x][y], sizeof(float), 1, stream) != 1) - return; + heights[x][y] = stream->Read<float>(); } else { diff --git a/src/tools/mesh_extractor/MPQ.cpp b/src/tools/mesh_extractor/MPQ.cpp index 896d7bc32ac..5e822a6835c 100644 --- a/src/tools/mesh_extractor/MPQ.cpp +++ b/src/tools/mesh_extractor/MPQ.cpp @@ -1,5 +1,6 @@ #include "MPQ.h" #include "MPQManager.h" +#include "Stream.h" #include <deque> #include <cstdio> @@ -108,15 +109,8 @@ void MPQFile::close() eof = true; } -FILE* MPQFile::GetFileStream() +Stream* MPQFile::GetFileStream() { - FILE* file = tmpfile(); - if (!file) - { - printf("Could not create temporary file. Please run as Administrator or root\n"); - exit(1); - } - fwrite(buffer, sizeof(char), size, file); - fseek(file, 0, SEEK_SET); - return file; + Stream* stream = new Stream(buffer, size); + return stream; } diff --git a/src/tools/mesh_extractor/MPQ.h b/src/tools/mesh_extractor/MPQ.h index 30e11741550..d548f16adc1 100644 --- a/src/tools/mesh_extractor/MPQ.h +++ b/src/tools/mesh_extractor/MPQ.h @@ -3,6 +3,7 @@ #include "libmpq/mpq.h" #include "Define.h" +#include "Stream.h" #include <string> #include <ctype.h> #include <vector> @@ -13,7 +14,7 @@ class MPQArchive { public: - mpq_archive_s *mpq_a; + mpq_archive_s* mpq_a; std::vector<std::string> Files; @@ -53,8 +54,8 @@ class MPQFile { //MPQHANDLE handle; bool eof; - char *buffer; - libmpq__off_t pointer,size; + char* buffer; + libmpq__off_t pointer, size; // disable copying MPQFile(const MPQFile& /*f*/) {} @@ -64,7 +65,7 @@ public: MPQFile(const char* filename); // filenames are not case sensitive ~MPQFile() { close(); } size_t Read(void* dest, size_t bytes); - FILE* GetFileStream(); + Stream* GetFileStream(); size_t getSize() { return size; } size_t getPos() { return pointer; } char* getBuffer() { return buffer; } diff --git a/src/tools/mesh_extractor/MPQManager.cpp b/src/tools/mesh_extractor/MPQManager.cpp index 950e284f92e..cd98a5e017b 100644 --- a/src/tools/mesh_extractor/MPQManager.cpp +++ b/src/tools/mesh_extractor/MPQManager.cpp @@ -2,6 +2,7 @@ #include "MPQ.h" #include "DBC.h" #include "Utils.h" +#include "Stream.h" #include <ace/Guard_T.h> char const* MPQManager::Files[] = { @@ -83,7 +84,7 @@ void MPQManager::InitializeDBC() printf("Using default locale: %s\n", Languages[BaseLocale]); } -FILE* MPQManager::GetFile(const std::string& path ) +Stream* MPQManager::GetFile(const std::string& path ) { ACE_GUARD_RETURN(ACE_Thread_Mutex, g, mutex, NULL); MPQFile file(path.c_str()); @@ -98,11 +99,11 @@ DBC* MPQManager::GetDBC(const std::string& name ) return new DBC(GetFile(path)); } -FILE* MPQManager::GetFileFromLocale( const std::string& path, uint32 locale ) +Stream* MPQManager::GetFileFromLocale( const std::string& path, uint32 locale ) { ACE_GUARD_RETURN(ACE_Thread_Mutex, g, mutex, NULL); std::deque<MPQArchive*> files = LocaleFiles[locale]; - FILE* ret = NULL; + Stream* ret = NULL; for (std::deque<MPQArchive*>::iterator itr = files.begin(); itr != files.end(); ++itr) { mpq_archive* mpq_a = (*itr)->mpq_a; @@ -123,22 +124,15 @@ FILE* MPQManager::GetFileFromLocale( const std::string& path, uint32 locale ) //libmpq_file_getdata libmpq__file_read(mpq_a, filenum, (unsigned char*)buffer, size, &transferred); - // Pack the return into a FILE stream - ret = tmpfile(); - if (!ret) - { - printf("Could not create temporary file. Please run as Administrator or root\n"); - exit(1); - } - fwrite(buffer, sizeof(uint8), size, ret); - fseek(ret, 0, SEEK_SET); + ret = new Stream(buffer, size); + delete[] buffer; break; } return ret; } -FILE* MPQManager::GetFileFrom(const std::string& path, MPQArchive* file ) +Stream* MPQManager::GetFileFrom(const std::string& path, MPQArchive* file ) { ACE_GUARD_RETURN(ACE_Thread_Mutex, g, mutex, NULL); mpq_archive* mpq_a = file->mpq_a; @@ -161,14 +155,7 @@ FILE* MPQManager::GetFileFrom(const std::string& path, MPQArchive* file ) libmpq__file_read(mpq_a, filenum, (unsigned char*)buffer, size, &transferred); // Pack the return into a FILE stream - FILE* ret = tmpfile(); - if (!ret) - { - printf("Could not create temporary file. Please run as Administrator or root\n"); - exit(1); - } - fwrite(buffer, sizeof(uint8), size, ret); - fseek(ret, 0, SEEK_SET); + Stream* ret = new Stream((char*)buffer, size); delete[] buffer; return ret; } diff --git a/src/tools/mesh_extractor/MPQManager.h b/src/tools/mesh_extractor/MPQManager.h index 3514674b9bf..3fd27b6bffd 100644 --- a/src/tools/mesh_extractor/MPQManager.h +++ b/src/tools/mesh_extractor/MPQManager.h @@ -2,6 +2,7 @@ #define MPQ_MANAGER_H #include "MPQ.h" +#include "Stream.h" #include <ace/Synch.h> #include <set> #include <map> @@ -14,9 +15,9 @@ public: ~MPQManager() {} void Initialize(); - FILE* GetFile(const std::string& path); - FILE* GetFileFrom(const std::string& path, MPQArchive* file); - FILE* GetFileFromLocale(const std::string& path, uint32 locale); + Stream* GetFile(const std::string& path); + Stream* GetFileFrom(const std::string& path, MPQArchive* file); + Stream* GetFileFromLocale(const std::string& path, uint32 locale); DBC* GetDBC(const std::string& name); std::vector<std::string> GetAllFiles(std::string extension); diff --git a/src/tools/mesh_extractor/MapChunk.cpp b/src/tools/mesh_extractor/MapChunk.cpp index 789166d5c9b..ffed64b13af 100644 --- a/src/tools/mesh_extractor/MapChunk.cpp +++ b/src/tools/mesh_extractor/MapChunk.cpp @@ -4,9 +4,9 @@ MapChunk::MapChunk( ADT* _adt, Chunk* chunk ) : Adt(_adt), Source(chunk) { - FILE* stream = chunk->GetStream(); + Stream* stream = chunk->GetStream(); Header.Read(stream); - fseek(stream, chunk->Offset, SEEK_SET); + stream->Seek(chunk->Offset, SEEK_SET); Index = Header.IndexX + Header.IndexY * 16; GenerateVertices(stream); } @@ -47,9 +47,9 @@ void MapChunk::GenerateTriangles() } } -void MapChunk::GenerateVertices( FILE* stream ) +void MapChunk::GenerateVertices(Stream* stream) { - fseek(stream, Header.OffsetMCVT, SEEK_CUR); + stream->Seek(Header.OffsetMCVT, SEEK_CUR); Vertices.reserve(125); for (int j = 0; j < 17; j++) @@ -57,9 +57,7 @@ void MapChunk::GenerateVertices( FILE* stream ) int values = j % 2 ? 8 : 9; for (int i = 0; i < values; i++) { - float tmp; - if (fread(&tmp, sizeof(float), 1, stream) != 1) - printf("MapChunk::GenerateVertices: Failed to read some data expected 1, read 0\n"); + float tmp = stream->Read<float>(); Vector3 vert(Header.Position.x - (j * (Constants::UnitSize * 0.5f)), Header.Position.y - (i * Constants::UnitSize), Header.Position.z + tmp); if (values == 8) vert.y -= Constants::UnitSize * 0.5f; @@ -67,7 +65,7 @@ void MapChunk::GenerateVertices( FILE* stream ) } } // Restore stream position. - fseek(stream, Source->Offset, SEEK_SET); + stream->Seek(Source->Offset, SEEK_SET); } bool MapChunk::HasHole( uint32 map, int x, int y ) diff --git a/src/tools/mesh_extractor/MapChunk.h b/src/tools/mesh_extractor/MapChunk.h index e7d835ae0e3..a317c3a371b 100644 --- a/src/tools/mesh_extractor/MapChunk.h +++ b/src/tools/mesh_extractor/MapChunk.h @@ -12,7 +12,7 @@ public: MapChunk(ADT* _adt, Chunk* chunk); void GenerateTriangles(); - void GenerateVertices(FILE* stream); + void GenerateVertices(Stream* stream); static bool HasHole(uint32 map, int x, int y); ADT* Adt; Chunk* Source; diff --git a/src/tools/mesh_extractor/MeshExtractor.cpp b/src/tools/mesh_extractor/MeshExtractor.cpp index 1ed09e5d9dc..22afa763099 100644 --- a/src/tools/mesh_extractor/MeshExtractor.cpp +++ b/src/tools/mesh_extractor/MeshExtractor.cpp @@ -352,7 +352,6 @@ void LoadTile(dtNavMesh*& navMesh, const char* tile) int main(int argc, char* argv[]) { - _setmaxstdio(2048); uint32 threads = 4, extractFlags = 0; std::set<uint32> mapIds; diff --git a/src/tools/mesh_extractor/Model.cpp b/src/tools/mesh_extractor/Model.cpp index 5fb521d5a36..0b21ad1027d 100644 --- a/src/tools/mesh_extractor/Model.cpp +++ b/src/tools/mesh_extractor/Model.cpp @@ -2,15 +2,15 @@ #include "MPQManager.h" #include "Utils.h" -Model::Model( std::string path ) : IsCollidable(false), IsBad(false) +Model::Model(std::string path) : IsCollidable(false), IsBad(false) { - Stream = MPQHandler->GetFile(Utils::FixModelPath(path)); - if (!Stream) + _Stream = MPQHandler->GetFile(Utils::FixModelPath(path)); + if (!_Stream) { IsBad = true; return; } - Header.Read(Stream); + Header.Read(_Stream); if (Header.OffsetBoundingNormals > 0 && Header.OffsetBoundingVertices > 0 && Header.OffsetBoundingTriangles > 0 && Header.BoundingRadius > 0.0f) { @@ -23,17 +23,17 @@ Model::Model( std::string path ) : IsCollidable(false), IsBad(false) Model::~Model() { - if (Stream) - fclose(Stream); + if (_Stream) + delete _Stream; } void Model::ReadVertices() { - fseek(Stream, Header.OffsetBoundingVertices, SEEK_SET); + _Stream->Seek(Header.OffsetBoundingVertices, SEEK_SET); Vertices.reserve(Header.CountBoundingVertices); for (uint32 i = 0; i < Header.CountBoundingVertices; ++i) { - Vertices.push_back(Vector3::Read(Stream)); + Vertices.push_back(Vector3::Read(_Stream)); if (Constants::ToWoWCoords) Vertices[i] = Utils::ToWoWCoords(Vertices[i]); } @@ -41,27 +41,24 @@ void Model::ReadVertices() void Model::ReadBoundingTriangles() { - fseek(Stream, Header.OffsetBoundingTriangles, SEEK_SET); + _Stream->Seek(Header.OffsetBoundingTriangles, SEEK_SET); Triangles.reserve(Header.CountBoundingTriangles / 3); for (uint32 i = 0; i < Header.CountBoundingTriangles / 3; i++) { Triangle<uint16> tri; tri.Type = Constants::TRIANGLE_TYPE_DOODAD; - int count = 0; - count += fread(&tri.V0, sizeof(uint16), 1, Stream); - count += fread(&tri.V1, sizeof(uint16), 1, Stream); - count += fread(&tri.V2, sizeof(uint16), 1, Stream); - if (count != 3) - printf("Model::ReadBoundingTriangles: Error reading data, expected 3, read %d\n", count); + tri.V0 = _Stream->Read<uint16>(); + tri.V1 = _Stream->Read<uint16>(); + tri.V2 = _Stream->Read<uint16>(); Triangles.push_back(tri); } } void Model::ReadBoundingNormals() { - fseek(Stream, Header.OffsetBoundingNormals, SEEK_SET); + _Stream->Seek(Header.OffsetBoundingNormals, SEEK_SET); Normals.reserve(Header.CountBoundingNormals); for (uint32 i = 0; i < Header.CountBoundingNormals; i++) - Normals.push_back(Vector3::Read(Stream)); + Normals.push_back(Vector3::Read(_Stream)); } diff --git a/src/tools/mesh_extractor/Model.h b/src/tools/mesh_extractor/Model.h index ed8627dad6f..fcbdd81145c 100644 --- a/src/tools/mesh_extractor/Model.h +++ b/src/tools/mesh_extractor/Model.h @@ -2,6 +2,7 @@ #define MODEL_H #include <vector> #include "Utils.h" +#include "Stream.h" class Model { @@ -17,7 +18,7 @@ public: std::vector<Vector3> Normals; std::vector<Triangle<uint16> > Triangles; bool IsCollidable; - FILE* Stream; + Stream* _Stream; bool IsBad; }; #endif
\ No newline at end of file diff --git a/src/tools/mesh_extractor/Stream.cpp b/src/tools/mesh_extractor/Stream.cpp new file mode 100644 index 00000000000..f775d97d114 --- /dev/null +++ b/src/tools/mesh_extractor/Stream.cpp @@ -0,0 +1,47 @@ +#include "Stream.h" +#include <iostream> + +Stream::Stream(char* buffer, uint32 size) : _size(size), _position(0) +{ + _buffer = new char[size]; + memcpy(_buffer, buffer, size); // Initialize the buffer +} + +Stream::~Stream() +{ + delete[] _buffer; +} + +char* Stream::Read(uint32 size) +{ + char* buff = new char[size]; + memcpy(buff, &_buffer[_position], size); + _position += size; + return buff; +} + +void Stream::Seek(uint32 position, uint32 type) +{ + switch (type) + { + case SEEK_SET: + _position = position; + break; + case SEEK_CUR: + _position += position; + break; + } +} + +std::string Stream::ReadString() +{ + std::string str; + while (true) + { + char b = Read<char>(); + if (b == 0) + break; + str.push_back(b); + } + return str; +}
\ No newline at end of file diff --git a/src/tools/mesh_extractor/Stream.h b/src/tools/mesh_extractor/Stream.h new file mode 100644 index 00000000000..76d9511bbc4 --- /dev/null +++ b/src/tools/mesh_extractor/Stream.h @@ -0,0 +1,53 @@ +#ifndef STREAM_H +#define STREAM_H + +#include "Define.h" +#include <iostream> + +class Stream +{ +public: + Stream(char* buffer, uint32 size); + ~Stream(); + + template<typename T> + T Read() + { + T ret = *((T*)(&_buffer[_position])); + _position += sizeof(T); + return ret; + } + + template<typename T> + void Read(T* dest, uint32 size) + { + memcpy(dest, &_buffer[_position], size); + _position += size; + } + + char* Read(uint32 size); + std::string ReadString(); + + void Reset() + { + _position = 0; + } + + void Seek(uint32 position, uint32 type); + + uint32 GetSize() + { + return _size; + } + + uint32 GetPos() + { + return _position; + } + +private: + char* _buffer; + uint32 _size; + uint32 _position; +}; +#endif
\ No newline at end of file diff --git a/src/tools/mesh_extractor/TileBuilder.cpp b/src/tools/mesh_extractor/TileBuilder.cpp index cdc3131b3db..5d82fb17e25 100644 --- a/src/tools/mesh_extractor/TileBuilder.cpp +++ b/src/tools/mesh_extractor/TileBuilder.cpp @@ -228,7 +228,7 @@ uint8* TileBuilder::BuildTiled(dtNavMeshParams& navMeshParams) ADT* _adt = new ADT(Utils::GetAdtPath(World, tx, ty), tx, ty); // If this condition is met, it means that this WDT does not contain the ADT - if (!_adt->Data->Stream) + if (!_adt->Data->_Stream) { delete _adt; continue; diff --git a/src/tools/mesh_extractor/Utils.cpp b/src/tools/mesh_extractor/Utils.cpp index 24cfb5cd1db..834303d164f 100644 --- a/src/tools/mesh_extractor/Utils.cpp +++ b/src/tools/mesh_extractor/Utils.cpp @@ -1,6 +1,7 @@ #include "Utils.h" #include "WorldModelHandler.h" #include "Constants.h" +#include "Stream.h" #include <cstring> #include "G3D/Matrix4.h" #include "G3D/Quat.h" @@ -47,31 +48,6 @@ void Utils::Reverse(char word[]) } } -std::string Utils::ReadString( FILE* file ) -{ - std::string ret; - while (true) - { - char b; - if (fread(&b, sizeof(char), 1, file) != 1 || b == 0) - break; - ret.push_back(b); - } - return ret; -} - -uint32 Utils::Size( FILE* file ) -{ - // store the old position - uint32 offset = ftell(file); - // Get file size - fseek(file, 0, SEEK_END); - uint32 size = ftell(file); - // reset back to the old position - fseek(file, offset, SEEK_SET); - return size; -} - Vector3 Utils::ToRecast(const Vector3& val ) { return Vector3(-val.y, val.z, -val.x); @@ -136,12 +112,9 @@ std::string Utils::GetPathBase(const std::string& path ) return path; } -Vector3 Vector3::Read( FILE* file ) +Vector3 Vector3::Read(Stream* file) { - Vector3 ret; - if (fread(&ret, sizeof(Vector3), 1, file) != 1) - printf("Vector3::Read: Failed to read some data expected 1, read 0\n"); - return ret; + return file->Read<Vector3>(); } Vector3 Utils::GetLiquidVert(const IDefinition& def, Vector3 basePosition, float height, int x, int y, bool translate) @@ -167,41 +140,36 @@ std::string Utils::Replace( std::string str, const std::string& oldStr, const st return str; } -void Utils::SaveToDisk( FILE* stream, const std::string& path ) +void Utils::SaveToDisk(Stream* stream, const std::string& path) { FILE* disk = fopen(path.c_str(), "wb"); if (!disk) { printf("SaveToDisk: Could not save file %s to disk, please verify that you have write permissions on that directory\n", path.c_str()); - fclose(stream); + delete stream; return; } - uint32 size = Utils::Size(stream); - uint8* data = new uint8[size]; - // Read the data to an array - size_t read = fread(data, size, 1, stream); - if (read != 1) - { - printf("SaveToDisk: Error reading from Stream while trying to save file %s to disk.\n", path.c_str()); - fclose(disk); - fclose(stream); - return; - } + uint32 size = stream->GetSize(); + stream->Reset(); // Reset the stream just in case + // Read the data to an array + char* data = stream->Read(size); + // And write it in the file size_t wrote = fwrite(data, size, 1, disk); if (wrote != 1) { printf("SaveToDisk: Error writing to the file while trying to save %s to disk.\n", path.c_str()); - fclose(stream); + delete[] data; + delete stream; fclose(disk); return; } // Close the filestream fclose(disk); - fclose(stream); + delete stream; // Free the used memory delete[] data; @@ -222,206 +190,172 @@ std::string Utils::GetExtension( std::string path ) return extension; } -void MapChunkHeader::Read(FILE* stream) -{ - int count = 0; - - count += fread(&Flags, sizeof(uint32), 1, stream); - count += fread(&IndexX, sizeof(uint32), 1, stream); - count += fread(&IndexY, sizeof(uint32), 1, stream); - count += fread(&Layers, sizeof(uint32), 1, stream); - count += fread(&DoodadRefs, sizeof(uint32), 1, stream); - count += fread(&OffsetMCVT, sizeof(uint32), 1, stream); - count += fread(&OffsetMCNR, sizeof(uint32), 1, stream); - count += fread(&OffsetMCLY, sizeof(uint32), 1, stream); - count += fread(&OffsetMCRF, sizeof(uint32), 1, stream); - count += fread(&OffsetMCAL, sizeof(uint32), 1, stream); - count += fread(&SizeMCAL, sizeof(uint32), 1, stream); - count += fread(&OffsetMCSH, sizeof(uint32), 1, stream); - count += fread(&SizeMCSH, sizeof(uint32), 1, stream); - count += fread(&AreaId, sizeof(uint32), 1, stream); - count += fread(&MapObjectRefs, sizeof(uint32), 1, stream); - count += fread(&Holes, sizeof(uint32), 1, stream); +void MapChunkHeader::Read(Stream* stream) +{ + Flags = stream->Read<uint32>(); + IndexX = stream->Read<uint32>(); + IndexY = stream->Read<uint32>(); + Layers = stream->Read<uint32>(); + DoodadRefs = stream->Read<uint32>(); + OffsetMCVT = stream->Read<uint32>(); + OffsetMCNR = stream->Read<uint32>(); + OffsetMCLY = stream->Read<uint32>(); + OffsetMCRF = stream->Read<uint32>(); + OffsetMCAL = stream->Read<uint32>(); + SizeMCAL = stream->Read<uint32>(); + OffsetMCSH = stream->Read<uint32>(); + SizeMCSH = stream->Read<uint32>(); + AreaId = stream->Read<uint32>(); + MapObjectRefs = stream->Read<uint32>(); + Holes = stream->Read<uint32>(); LowQualityTextureMap = new uint32[4]; - count += fread(LowQualityTextureMap, sizeof(uint32), 4, stream); - count += fread(&PredTex, sizeof(uint32), 1, stream); - count += fread(&NumberEffectDoodad, sizeof(uint32), 1, stream); - count += fread(&OffsetMCSE, sizeof(uint32), 1, stream); - count += fread(&SoundEmitters, sizeof(uint32), 1, stream); - count += fread(&OffsetMCLQ, sizeof(uint32), 1, stream); - count += fread(&SizeMCLQ, sizeof(uint32), 1, stream); + stream->Read(LowQualityTextureMap, sizeof(uint32) * 4); + PredTex = stream->Read<uint32>(); + NumberEffectDoodad = stream->Read<uint32>(); + OffsetMCSE = stream->Read<uint32>(); + SoundEmitters = stream->Read<uint32>(); + OffsetMCLQ = stream->Read<uint32>(); + SizeMCLQ = stream->Read<uint32>(); Position = Vector3::Read(stream); - count += fread(&OffsetMCCV, sizeof(uint32), 1, stream); - - if (count != 27) - printf("MapChunkHeader::Read: Failed to read some data expected 27, read %d\n", count); + OffsetMCCV = stream->Read<uint32>(); } -void MHDR::Read(FILE* stream) +void MHDR::Read(Stream* stream) { int count = 0; - count += fread(&Flags, sizeof(uint32), 1, stream); - count += fread(&OffsetMCIN, sizeof(uint32), 1, stream); - count += fread(&OffsetMTEX, sizeof(uint32), 1, stream); - count += fread(&OffsetMMDX, sizeof(uint32), 1, stream); - count += fread(&OffsetMMID, sizeof(uint32), 1, stream); - count += fread(&OffsetMWMO, sizeof(uint32), 1, stream); - count += fread(&OffsetMWID, sizeof(uint32), 1, stream); - count += fread(&OffsetMDDF, sizeof(uint32), 1, stream); - count += fread(&OffsetMODF, sizeof(uint32), 1, stream); - count += fread(&OffsetMFBO, sizeof(uint32), 1, stream); - count += fread(&OffsetMH2O, sizeof(uint32), 1, stream); - count += fread(&OffsetMTFX, sizeof(uint32), 1, stream); - - if (count != 12) - printf("MHDR::Read: Failed to read some data expected 12, read %d\n", count); + Flags = stream->Read<uint32>(); + OffsetMCIN = stream->Read<uint32>(); + OffsetMTEX = stream->Read<uint32>(); + OffsetMMDX = stream->Read<uint32>(); + OffsetMMID = stream->Read<uint32>(); + OffsetMWMO = stream->Read<uint32>(); + OffsetMWID = stream->Read<uint32>(); + OffsetMDDF = stream->Read<uint32>(); + OffsetMODF = stream->Read<uint32>(); + OffsetMFBO = stream->Read<uint32>(); + OffsetMH2O = stream->Read<uint32>(); + OffsetMTFX = stream->Read<uint32>(); } -void ModelHeader::Read(FILE* stream) +void ModelHeader::Read(Stream* stream) { - int count = 0; - - count += fread(&Magic, sizeof(char), 4, stream); + stream->Read(Magic, 4); Magic[4] = '\0'; // null-terminate it. - count += fread(&Version, sizeof(uint32), 1, stream); - count += fread(&LengthModelName, sizeof(uint32), 1, stream); - count += fread(&OffsetName, sizeof(uint32), 1, stream); - count += fread(&ModelFlags, sizeof(uint32), 1, stream); - count += fread(&CountGlobalSequences, sizeof(uint32), 1, stream); - count += fread(&OffsetGlobalSequences, sizeof(uint32), 1, stream); - count += fread(&CountAnimations, sizeof(uint32), 1, stream); - count += fread(&OffsetAnimations, sizeof(uint32), 1, stream); - count += fread(&CountAnimationLookup, sizeof(uint32), 1, stream); - count += fread(&OffsetAnimationLookup, sizeof(uint32), 1, stream); - count += fread(&CountBones, sizeof(uint32), 1, stream); - count += fread(&OffsetBones, sizeof(uint32), 1, stream); - count += fread(&CountKeyBoneLookup, sizeof(uint32), 1, stream); - count += fread(&OffsetKeyBoneLookup, sizeof(uint32), 1, stream); - count += fread(&CountVertices, sizeof(uint32), 1, stream); - count += fread(&OffsetVertices, sizeof(uint32), 1, stream); - count += fread(&CountViews, sizeof(uint32), 1, stream); - count += fread(&CountColors, sizeof(uint32), 1, stream); - count += fread(&OffsetColors, sizeof(uint32), 1, stream); - count += fread(&CountTextures, sizeof(uint32), 1, stream); - count += fread(&OffsetTextures, sizeof(uint32), 1, stream); - count += fread(&CountTransparency, sizeof(uint32), 1, stream); - count += fread(&OffsetTransparency, sizeof(uint32), 1, stream); - count += fread(&CountUvAnimation, sizeof(uint32), 1, stream); - count += fread(&OffsetUvAnimation, sizeof(uint32), 1, stream); - count += fread(&CountTexReplace, sizeof(uint32), 1, stream); - count += fread(&OffsetTexReplace, sizeof(uint32), 1, stream); - count += fread(&CountRenderFlags, sizeof(uint32), 1, stream); - count += fread(&OffsetRenderFlags, sizeof(uint32), 1, stream); - count += fread(&CountBoneLookup, sizeof(uint32), 1, stream); - count += fread(&OffsetBoneLookup, sizeof(uint32), 1, stream); - count += fread(&CountTexLookup, sizeof(uint32), 1, stream); - count += fread(&OffsetTexLookup, sizeof(uint32), 1, stream); - count += fread(&CountTexUnits, sizeof(uint32), 1, stream); - count += fread(&OffsetTexUnits, sizeof(uint32), 1, stream); - count += fread(&CountTransLookup, sizeof(uint32), 1, stream); - count += fread(&OffsetTransLookup, sizeof(uint32), 1, stream); - count += fread(&CountUvAnimLookup, sizeof(uint32), 1, stream); - count += fread(&OffsetUvAnimLookup, sizeof(uint32), 1, stream); + Version = stream->Read<uint32>(); + LengthModelName = stream->Read<uint32>(); + OffsetName = stream->Read<uint32>(); + ModelFlags = stream->Read<uint32>(); + CountGlobalSequences = stream->Read<uint32>(); + OffsetGlobalSequences = stream->Read<uint32>(); + CountAnimations = stream->Read<uint32>(); + OffsetAnimations = stream->Read<uint32>(); + CountAnimationLookup = stream->Read<uint32>(); + OffsetAnimationLookup = stream->Read<uint32>(); + CountBones = stream->Read<uint32>(); + OffsetBones = stream->Read<uint32>(); + CountKeyBoneLookup = stream->Read<uint32>(); + OffsetKeyBoneLookup = stream->Read<uint32>(); + CountVertices = stream->Read<uint32>(); + OffsetVertices = stream->Read<uint32>(); + CountViews = stream->Read<uint32>(); + CountColors = stream->Read<uint32>(); + OffsetColors = stream->Read<uint32>(); + CountTextures = stream->Read<uint32>(); + OffsetTextures = stream->Read<uint32>(); + CountTransparency = stream->Read<uint32>(); + OffsetTransparency = stream->Read<uint32>(); + CountUvAnimation = stream->Read<uint32>(); + OffsetUvAnimation = stream->Read<uint32>(); + CountTexReplace = stream->Read<uint32>(); + OffsetTexReplace = stream->Read<uint32>(); + CountRenderFlags = stream->Read<uint32>(); + OffsetRenderFlags = stream->Read<uint32>(); + CountBoneLookup = stream->Read<uint32>(); + OffsetBoneLookup = stream->Read<uint32>(); + CountTexLookup = stream->Read<uint32>(); + OffsetTexLookup = stream->Read<uint32>(); + CountTexUnits = stream->Read<uint32>(); + OffsetTexUnits = stream->Read<uint32>(); + CountTransLookup = stream->Read<uint32>(); + OffsetTransLookup = stream->Read<uint32>(); + CountUvAnimLookup = stream->Read<uint32>(); + OffsetUvAnimLookup = stream->Read<uint32>(); VertexBox[0] = Vector3::Read(stream); VertexBox[1] = Vector3::Read(stream); - count += fread(&VertexRadius, sizeof(float), 1, stream); + VertexRadius = stream->Read<float>(); BoundingBox[0] = Vector3::Read(stream); BoundingBox[1] = Vector3::Read(stream); - count += fread(&BoundingRadius, sizeof(float), 1, stream); - count += fread(&CountBoundingTriangles, sizeof(uint32), 1, stream); - count += fread(&OffsetBoundingTriangles, sizeof(uint32), 1, stream); - count += fread(&CountBoundingVertices, sizeof(uint32), 1, stream); - count += fread(&OffsetBoundingVertices, sizeof(uint32), 1, stream); - count += fread(&CountBoundingNormals, sizeof(uint32), 1, stream); - count += fread(&OffsetBoundingNormals, sizeof(uint32), 1, stream); - - if (count != 51) - printf("ModelHeader::Read: Failed to read some data expected 51, read %d\n", count); - + BoundingRadius = stream->Read<float>(); + CountBoundingTriangles = stream->Read<uint32>(); + OffsetBoundingTriangles = stream->Read<uint32>(); + CountBoundingVertices = stream->Read<uint32>(); + OffsetBoundingVertices = stream->Read<uint32>(); + CountBoundingNormals = stream->Read<uint32>(); + OffsetBoundingNormals = stream->Read<uint32>(); } -WorldModelHeader WorldModelHeader::Read(FILE* stream) +WorldModelHeader WorldModelHeader::Read(Stream* stream) { WorldModelHeader ret; int count = 0; - - count += fread(&ret.CountMaterials, sizeof(uint32), 1, stream); - count += fread(&ret.CountGroups, sizeof(uint32), 1, stream); - count += fread(&ret.CountPortals, sizeof(uint32), 1, stream); - count += fread(&ret.CountLights, sizeof(uint32), 1, stream); - count += fread(&ret.CountModels, sizeof(uint32), 1, stream); - count += fread(&ret.CountDoodads, sizeof(uint32), 1, stream); - count += fread(&ret.CountSets, sizeof(uint32), 1, stream); - count += fread(&ret.AmbientColorUnk, sizeof(uint32), 1, stream); - count += fread(&ret.WmoId, sizeof(uint32), 1, stream); + ret.CountMaterials = stream->Read<uint32>(); + ret.CountGroups = stream->Read<uint32>(); + ret.CountPortals = stream->Read<uint32>(); + ret.CountLights = stream->Read<uint32>(); + ret.CountModels = stream->Read<uint32>(); + ret.CountDoodads = stream->Read<uint32>(); + ret.CountSets = stream->Read<uint32>(); + ret.AmbientColorUnk = stream->Read<uint32>(); + ret.WmoId = stream->Read<uint32>(); ret.BoundingBox[0] = Vector3::Read(stream); ret.BoundingBox[1] = Vector3::Read(stream); - count += fread(&ret.LiquidTypeRelated, sizeof(uint32), 1, stream); - - if (count != 10) - printf("WorldModelHeader::Read: Failed to read some data expected 10, read %d\n", count); + ret.LiquidTypeRelated = stream->Read<uint32>(); return ret; } -DoodadInstance DoodadInstance::Read(FILE* stream) +DoodadInstance DoodadInstance::Read(Stream* stream) { DoodadInstance ret; - int count = 0; - count += fread(&ret.FileOffset, sizeof(uint32), 1, stream); + ret.FileOffset = stream->Read<uint32>(); ret.Position = Vector3::Read(stream); - count += fread(&ret.QuatW, sizeof(float), 1, stream); - count += fread(&ret.QuatX, sizeof(float), 1, stream); - count += fread(&ret.QuatY, sizeof(float), 1, stream); - count += fread(&ret.QuatZ, sizeof(float), 1, stream); - count += fread(&ret.Scale, sizeof(float), 1, stream); - count += fread(&ret.LightColor, sizeof(uint32), 1, stream); - - if (count != 7) - printf("DoodadInstance::Read: Failed to read some data expected 7, read %d\n", count); - + ret.QuatW = stream->Read<float>(); + ret.QuatX = stream->Read<float>(); + ret.QuatY = stream->Read<float>(); + ret.QuatZ = stream->Read<float>(); + ret.Scale = stream->Read<float>(); + ret.LightColor = stream->Read<uint32>(); return ret; } -DoodadSet DoodadSet::Read(FILE* stream) +DoodadSet DoodadSet::Read(Stream* stream) { DoodadSet ret; - char name[21]; - int count = 0; - - count += fread(&name, sizeof(char), 20, stream); - name[20] = '\0'; - ret.Name = name; - count += fread(&ret.FirstInstanceIndex, sizeof(uint32), 1, stream); - count += fread(&ret.CountInstances, sizeof(uint32), 1, stream); - count += fread(&ret.UnknownZero, sizeof(uint32), 1, stream); - - if (count != 23) - printf("DoodadSet::Read: Failed to read some data expected 23, read %d\n", count); + ret.Name = std::string(stream->Read(20), 20); + ret.FirstInstanceIndex = stream->Read<uint32>(); + ret.CountInstances = stream->Read<uint32>(); + ret.UnknownZero = stream->Read<uint32>(); + return ret; } -LiquidHeader LiquidHeader::Read(FILE* stream) +LiquidHeader LiquidHeader::Read(Stream* stream) { LiquidHeader ret; - int count = 0; - count += fread(&ret.CountXVertices, sizeof(uint32), 1, stream); - count += fread(&ret.CountYVertices, sizeof(uint32), 1, stream); - count += fread(&ret.Width, sizeof(uint32), 1, stream); - count += fread(&ret.Height, sizeof(uint32), 1, stream); + ret.CountXVertices = stream->Read<uint32>(); + ret.CountYVertices = stream->Read<uint32>(); + ret.Width = stream->Read<uint32>(); + ret.Height = stream->Read<uint32>(); ret.BaseLocation = Vector3::Read(stream); - count += fread(&ret.MaterialId, sizeof(uint16), 1, stream); - - if (count != 5) - printf("LiquidHeader::Read: Failed to read some data expected 5, read %d\n", count); + ret.MaterialId = stream->Read<uint16>(); return ret; } -LiquidData LiquidData::Read(FILE* stream, LiquidHeader& header) +LiquidData LiquidData::Read(Stream* stream, LiquidHeader& header) { LiquidData ret; ret.HeightMap = new float*[header.CountXVertices]; @@ -436,35 +370,22 @@ LiquidData LiquidData::Read(FILE* stream, LiquidHeader& header) { for (uint32 x = 0; x < header.CountXVertices; x++) { - uint32 discard; - float tmp; - if (fread(&discard, sizeof(uint32), 1, stream) == 1 && - fread(&tmp, sizeof(float), 1, stream) == 1) - { - ret.HeightMap[x][y] = tmp; - } + stream->Read<uint32>(); // Dummy value + ret.HeightMap[x][y] = stream->Read<float>(); } } for (uint32 y = 0; y < header.Height; y++) - { for (uint32 x = 0; x < header.Width; x++) - { - uint8 tmp = 0; - if (fread(&tmp, sizeof(uint8), 1, stream) == 1) - ret.RenderFlags[x][y] = tmp; - } - } + ret.RenderFlags[x][y] = stream->Read<uint8>(); return ret; } -H2ORenderMask H2ORenderMask::Read(FILE* stream) +H2ORenderMask H2ORenderMask::Read(Stream* stream) { H2ORenderMask ret; - int32 count; - if ((count = fread(&ret.Mask, sizeof(uint8), 8, stream)) != 8) - printf("H2OHeader::Read: Failed to read some data expected 8, read %d\n", count); + stream->Read(ret.Mask, sizeof(uint8) * 8); return ret; } @@ -480,38 +401,31 @@ bool MCNKLiquidData::IsWater(int x, int y, float height) return false; } -H2OHeader H2OHeader::Read(FILE* stream) +H2OHeader H2OHeader::Read(Stream* stream) { H2OHeader ret; - int count = 0; - count += fread(&ret.OffsetInformation, sizeof(uint32), 1, stream); - count += fread(&ret.LayerCount, sizeof(uint32), 1, stream); - count += fread(&ret.OffsetRender, sizeof(uint32), 1, stream); - - if (count != 3) - printf("H2OHeader::Read: Failed to read some data expected 3, read %d\n", count); - + + ret.OffsetInformation = stream->Read<uint32>(); + ret.LayerCount = stream->Read<uint32>(); + ret.OffsetRender = stream->Read<uint32>(); + return ret; } -H2OInformation H2OInformation::Read(FILE* stream) +H2OInformation H2OInformation::Read(Stream* stream) { H2OInformation ret; - int count = 0; - count += fread(&ret.LiquidType, sizeof(uint16), 1, stream); - count += fread(&ret.Flags, sizeof(uint16), 1, stream); - count += fread(&ret.HeightLevel1, sizeof(float), 1, stream); - count += fread(&ret.HeightLevel2, sizeof(float), 1, stream); - count += fread(&ret.OffsetX, sizeof(uint8), 1, stream); - count += fread(&ret.OffsetY, sizeof(uint8), 1, stream); - count += fread(&ret.Width, sizeof(uint8), 1, stream); - count += fread(&ret.Height, sizeof(uint8), 1, stream); - count += fread(&ret.OffsetMask2, sizeof(uint32), 1, stream); - count += fread(&ret.OffsetHeightmap, sizeof(uint32), 1, stream); - - if (count != 10) - printf("H2OInformation::Read: Failed to read some data expected 10, read %d\n", count); - + ret.LiquidType = stream->Read<uint16>(); + ret.Flags = stream->Read<uint16>(); + ret.HeightLevel1 = stream->Read<float>(); + ret.HeightLevel2 = stream->Read<float>(); + ret.OffsetX = stream->Read<uint8>(); + ret.OffsetY = stream->Read<uint8>(); + ret.Width = stream->Read<uint8>(); + ret.Height = stream->Read<uint8>(); + ret.OffsetMask2 = stream->Read<uint32>(); + ret.OffsetHeightmap = stream->Read<uint32>(); + return ret; } @@ -524,24 +438,20 @@ char* Utils::GetPlainName(const char* FileName) return (char*)FileName; } -WMOGroupHeader WMOGroupHeader::Read( FILE* stream ) +WMOGroupHeader WMOGroupHeader::Read(Stream* stream) { WMOGroupHeader ret; - int count = 0; - count += fread(&ret.OffsetGroupName, sizeof(uint32), 1, stream); - count += fread(&ret.OffsetDescriptiveName, sizeof(uint32), 1, stream); - count += fread(&ret.Flags, sizeof(uint32), 1, stream); + ret.OffsetGroupName = stream->Read<uint32>(); + ret.OffsetDescriptiveName = stream->Read<uint32>(); + ret.Flags = stream->Read<uint32>(); ret.BoundingBox[0] = Vector3::Read(stream); ret.BoundingBox[1] = Vector3::Read(stream); - count += fread(&ret.OffsetPortals, sizeof(uint32), 1, stream); - count += fread(&ret.CountPortals, sizeof(uint32), 1, stream); - count += fread(&ret.CountBatches, sizeof(uint16), 4, stream); - count += fread(&ret.Fogs, sizeof(uint8), 4, stream); - count += fread(&ret.LiquidTypeRelated, sizeof(uint32), 1, stream); - count += fread(&ret.WmoId, sizeof(uint32), 1, stream); - - if (count != 15) - printf("WMOGroupHeader::Read: Failed to read some data expected 15, read %d\n", count); - + ret.OffsetPortals = stream->Read<uint32>(); + ret.CountPortals = stream->Read<uint32>(); + stream->Read(ret.CountBatches, sizeof(uint16) * 4); + stream->Read(ret.Fogs, sizeof(uint8) * 4); + ret.LiquidTypeRelated = stream->Read<uint32>(); + ret.WmoId = stream->Read<uint32>(); + return ret; } diff --git a/src/tools/mesh_extractor/Utils.h b/src/tools/mesh_extractor/Utils.h index 472cf6dbd1b..617da2b2244 100644 --- a/src/tools/mesh_extractor/Utils.h +++ b/src/tools/mesh_extractor/Utils.h @@ -10,6 +10,8 @@ #include "Define.h" #include "Constants.h" +#include "Stream.h" + #include <ace/Stack_Trace.h> struct WorldModelDefinition; @@ -42,7 +44,7 @@ struct Vector3 return Vector3(x * s, y * s, z * s); } - static Vector3 Read(FILE* file); + static Vector3 Read(Stream* file); }; struct TilePos @@ -93,7 +95,7 @@ public: Vector3 Position; uint32 OffsetMCCV; - void Read(FILE* stream); + void Read(Stream* stream); }; class MHDR @@ -113,7 +115,7 @@ public: uint32 OffsetMH2O; uint32 OffsetMTFX; - void Read(FILE* stream); + void Read(Stream* stream); }; class ModelHeader @@ -170,7 +172,7 @@ public: uint32 CountBoundingNormals; uint32 OffsetBoundingNormals; - void Read(FILE* stream); + void Read(Stream* stream); }; class WorldModelHeader @@ -189,7 +191,7 @@ public: Vector3 BoundingBox[2]; uint32 LiquidTypeRelated; - static WorldModelHeader Read(FILE* stream); + static WorldModelHeader Read(Stream* stream); }; class DoodadInstance @@ -206,7 +208,7 @@ public: float Scale; uint32 LightColor; - static DoodadInstance Read(FILE* stream); + static DoodadInstance Read(Stream* stream); }; class DoodadSet @@ -218,7 +220,7 @@ public: uint32 CountInstances; uint32 UnknownZero; - static DoodadSet Read(FILE* stream); + static DoodadSet Read(Stream* stream); }; class LiquidHeader @@ -232,7 +234,7 @@ public: Vector3 BaseLocation; uint16 MaterialId; - static LiquidHeader Read(FILE* stream); + static LiquidHeader Read(Stream* stream); }; class LiquidData @@ -247,7 +249,7 @@ public: return RenderFlags[x][y] != 0x0F; } - static LiquidData Read(FILE* stream, LiquidHeader& header); + static LiquidData Read(Stream* stream, LiquidHeader& header); }; class H2ORenderMask @@ -261,7 +263,7 @@ public: return (Mask[y] >> x & 1) != 0; } - static H2ORenderMask Read(FILE* stream); + static H2ORenderMask Read(Stream* stream); }; class MCNKLiquidData @@ -284,7 +286,7 @@ public: uint32 LayerCount; uint32 OffsetRender; - static H2OHeader Read(FILE* stream); + static H2OHeader Read(Stream* stream); }; class H2OInformation @@ -302,7 +304,7 @@ public: uint32 OffsetMask2; uint32 OffsetHeightmap; - static H2OInformation Read(FILE* stream); + static H2OInformation Read(Stream* stream); }; class WMOGroupHeader @@ -321,7 +323,7 @@ public: uint32 LiquidTypeRelated; uint32 WmoId; - static WMOGroupHeader Read(FILE* stream); + static WMOGroupHeader Read(Stream* stream); }; // Dummy class to act as an interface. @@ -352,8 +354,6 @@ class Utils { public: static void Reverse(char word[]); - static std::string ReadString(FILE* file); - static uint32 Size(FILE* file); static Vector3 ToRecast(const Vector3& val ); static std::string GetAdtPath(const std::string& world, int x, int y); static std::string FixModelPath(const std::string& path); @@ -377,14 +377,14 @@ public: return false; return true; } - static std::string Replace( std::string str, const std::string& oldStr, const std::string& newStr ); - static void CreateDir( const std::string& Path ); - static void SaveToDisk(FILE* stream, const std::string& path); - static Vector3 ToWoWCoords(const Vector3& vec ); - static std::string GetExtension( std::string path ); + static std::string Replace(std::string str, const std::string& oldStr, const std::string& newStr); + static void CreateDir(const std::string& Path); + static void SaveToDisk(Stream* stream, const std::string& path); + static Vector3 ToWoWCoords(const Vector3& vec); + static std::string GetExtension( std::string path); static char* GetPlainName(const char* FileName); static Vector3 TransformDoodadVertex(const IDefinition& def, Vector3& vec, bool translate = true); - static Vector3 VectorTransform(const Vector3& vec, const G3D::Matrix4& matrix, bool normal = false ); - static Vector3 TransformWmoDoodad(const DoodadInstance& inst, const WorldModelDefinition& root, Vector3& vec, bool translate = true ); + static Vector3 VectorTransform(const Vector3& vec, const G3D::Matrix4& matrix, bool normal = false); + static Vector3 TransformWmoDoodad(const DoodadInstance& inst, const WorldModelDefinition& root, Vector3& vec, bool translate = true); }; #endif diff --git a/src/tools/mesh_extractor/WDT.cpp b/src/tools/mesh_extractor/WDT.cpp index 2a5a18c9848..4a1d9c45b58 100644 --- a/src/tools/mesh_extractor/WDT.cpp +++ b/src/tools/mesh_extractor/WDT.cpp @@ -20,7 +20,7 @@ void WDT::ReadGlobalModel() IsGlobalModel = true; ModelDefinition = WorldModelDefinition::Read(defChunk->GetStream()); - ModelFile = Utils::ReadString(fileChunk->GetStream()); + ModelFile = fileChunk->GetStream()->ReadString(); Model = new WorldModelRoot(ModelFile); } @@ -30,20 +30,14 @@ void WDT::ReadTileTable() if (!chunk) return; IsValid = true; - FILE* stream = chunk->GetStream(); + Stream* stream = chunk->GetStream(); for (int y = 0; y < 64; ++y) { for (int x = 0; x < 64; ++x) { const uint32 hasTileFlag = 0x1; - uint32 flags; - uint32 discard; - int count = 0; - count += fread(&flags, sizeof(uint32), 1, stream); - count += fread(&discard, sizeof(uint32), 1, stream); - - if (count != 2) - printf("WDT::ReadTileTable: Failed to read some data expected 2, read %d\n", count); + uint32 flags = stream->Read<uint32>(); + uint32 discard = stream->Read<uint32>(); if (flags & hasTileFlag) TileTable.push_back(TilePos(x, y)); diff --git a/src/tools/mesh_extractor/WorldModelGroup.cpp b/src/tools/mesh_extractor/WorldModelGroup.cpp index f76df73aaa6..08b315b9db3 100644 --- a/src/tools/mesh_extractor/WorldModelGroup.cpp +++ b/src/tools/mesh_extractor/WorldModelGroup.cpp @@ -3,10 +3,10 @@ #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(NULL), IsBad(false), HasLiquidData(false) { Data = new ChunkedData(path); - if (!Data->Stream) + if (!Data->_Stream) { IsBad = true; return; @@ -18,8 +18,8 @@ WorldModelGroup::WorldModelGroup( std::string path, int groupIndex ) : GroupInde Name = Utils::GetPlainName(path.c_str()); - FILE* stream = mainChunk->GetStream(); - fseek(stream, firstSub, SEEK_SET); + Stream* stream = mainChunk->GetStream(); + stream->Seek(firstSub, SEEK_SET); SubData = new ChunkedData(stream, mainChunk->Length - firstSub); ReadHeader(); @@ -40,7 +40,7 @@ void WorldModelGroup::ReadNormals() uint32 normalCount = chunk->Length / 12; ASSERT(normalCount == Vertices.size() && "normalCount is different than the Vertices count"); Normals.reserve(normalCount); - FILE* stream = chunk->GetStream(); + Stream* stream = chunk->GetStream(); for (uint32 i = 0; i < normalCount; i++) Normals.push_back(Vector3::Read(stream)); } @@ -52,7 +52,7 @@ void WorldModelGroup::ReadLiquid() return; HasLiquidData = true; - FILE* stream = chunk->GetStream(); + Stream* stream = chunk->GetStream(); LiquidDataHeader = LiquidHeader::Read(stream); LiquidDataGeometry = LiquidData::Read(stream, LiquidDataHeader); } @@ -65,7 +65,7 @@ void WorldModelGroup::ReadVertices() uint32 verticeCount = chunk->Length / 12; Vertices.reserve(verticeCount); - FILE* stream = chunk->GetStream(); + Stream* stream = chunk->GetStream(); for (uint32 i = 0; i < verticeCount; i++) Vertices.push_back(Vector3::Read(stream)); } @@ -78,19 +78,13 @@ void WorldModelGroup::ReadTriangles() uint32 triangleCount = chunk->Length / 6; ASSERT(triangleCount == TriangleFlags.size() && "triangleCount != TriangleFlags.size()"); - FILE* stream = chunk->GetStream(); + Stream* stream = chunk->GetStream(); Triangles.reserve(triangleCount); for (uint32 i = 0; i < triangleCount; i++) { - uint16 v0; - uint16 v1; - uint16 v2; - int count = 0; - count += fread(&v0, sizeof(uint16), 1, stream); - count += fread(&v1, sizeof(uint16), 1, stream); - count += fread(&v2, sizeof(uint16), 1, stream); - if (count != 3) - printf("WorldModelGroup::ReadMaterials: Error reading data, expected 3, read %d\n", count); + uint16 v0 = stream->Read<uint16>(); + uint16 v1 = stream->Read<uint16>(); + uint16 v2 = stream->Read<uint16>(); Triangles.push_back(Triangle<uint16>(Constants::TRIANGLE_TYPE_WMO, v0, v1, v2)); } @@ -102,20 +96,15 @@ void WorldModelGroup::ReadMaterials() if (!chunk) return; - FILE* stream = chunk->GetStream(); + Stream* stream = chunk->GetStream(); uint32 triangleCount = chunk->Length / 2; TriangleFlags.reserve(triangleCount); TriangleMaterials.reserve(triangleCount); for (uint32 i = 0; i < triangleCount; i++) { - uint8 tmp; - if (fread(&tmp, sizeof(uint8), 1, stream) != 1) - printf("WorldModelGroup::ReadMaterials: Error reading data, expected 1, read 0\n"); - TriangleFlags.push_back(tmp); + TriangleFlags.push_back(stream->Read<uint8>()); // Read again for material. - if (fread(&tmp, sizeof(uint8), 1, stream) != 1) - printf("WorldModelGroup::ReadMaterials: Error reading data, expected 1, read 0\n"); - TriangleMaterials.push_back(tmp); + TriangleMaterials.push_back(stream->Read<uint8>()); } } @@ -125,7 +114,7 @@ void WorldModelGroup::ReadHeader() if (!chunk) return; - FILE* stream = chunk->GetStream(); + Stream* stream = chunk->GetStream(); Header = WMOGroupHeader::Read(stream); } @@ -137,7 +126,5 @@ void WorldModelGroup::ReadBatches() MOBALength = chunk->Length / 2; MOBA = new uint16[MOBALength]; - uint32 count = (uint32)fread(MOBA, sizeof(uint16), MOBALength, chunk->GetStream()); - if (count != MOBALength) - printf("WorldModelGroup::ReadBatches: Error reading data, expected %u, read %u\n", MOBALength, count); + chunk->GetStream()->Read(MOBA, sizeof(uint16) * MOBALength); } diff --git a/src/tools/mesh_extractor/WorldModelHandler.cpp b/src/tools/mesh_extractor/WorldModelHandler.cpp index 3565a1d6af0..d9f9cef2b96 100644 --- a/src/tools/mesh_extractor/WorldModelHandler.cpp +++ b/src/tools/mesh_extractor/WorldModelHandler.cpp @@ -4,27 +4,24 @@ #include "Cache.h" #include "Model.h" #include "Define.h" +#include "Stream.h" #include "G3D/Matrix4.h" #include "G3D/Quat.h" #include <cstdio> -WorldModelDefinition WorldModelDefinition::Read( FILE* file ) +WorldModelDefinition WorldModelDefinition::Read(Stream* file) { WorldModelDefinition ret; - int count = 0; - count += fread(&ret.MwidIndex, sizeof(uint32), 1, file); - count += fread(&ret.UniqueId, sizeof(uint32), 1, file); + ret.MwidIndex = file->Read<uint32>(); + ret.UniqueId = file->Read<uint32>(); ret.Position = Vector3::Read(file); ret.Rotation = Vector3::Read(file); ret.UpperExtents = Vector3::Read(file); ret.LowerExtents = Vector3::Read(file); - count += fread(&ret.Flags, sizeof(uint16), 1, file); - count += fread(&ret.DoodadSet, sizeof(uint16), 1, file); - uint32 discard; - count += fread(&discard, sizeof(uint32), 1, file); - - if (count != 5) - printf("WorldModelDefinition::Read: Error reading data, expected 5, read %d\n", count); + ret.Flags = file->Read<uint16>(); + ret.DoodadSet = file->Read<uint16>(); + file->Read<uint32>(); // Discarded + return ret; } @@ -41,14 +38,12 @@ void WorldModelHandler::ProcessInternal( MapChunk* mcnk ) return; uint32 refCount = mcnk->Header.MapObjectRefs; - FILE* stream = mcnk->Source->GetStream(); - fseek(stream, mcnk->Source->Offset + mcnk->Header.OffsetMCRF, SEEK_SET); + Stream* stream = mcnk->Source->GetStream(); + stream->Seek(mcnk->Source->Offset + mcnk->Header.OffsetMCRF, SEEK_SET); // Start looping at the last Doodad Ref index for (uint32 i = mcnk->Header.DoodadRefs; i < refCount; i++) { - int32 index; - if (fread(&index, sizeof(int32), 1, stream) != 1) - printf("WorldModelDefinition::Read: Error reading data, expected 1, read 0\n"); + int32 index = stream->Read<int32>(); if (index < 0 || uint32(index) >= _definitions->size()) continue; @@ -76,7 +71,7 @@ void WorldModelHandler::ProcessInternal( MapChunk* mcnk ) InsertModelGeometry(Vertices, Triangles, wmo, model); } // Restore the stream position - fseek(stream, mcnk->Source->Offset, SEEK_SET); + stream->Seek(mcnk->Source->Offset, SEEK_SET); } void WorldModelHandler::InsertModelGeometry( std::vector<Vector3>& verts, std::vector<Triangle<uint32> >& tris, const WorldModelDefinition& def, WorldModelRoot* root, bool translate ) @@ -185,7 +180,7 @@ void WorldModelHandler::ReadDefinitions() uint32 definitionCount = chunk->Length / definitionSize; _definitions = new std::vector<WorldModelDefinition>; _definitions->reserve(definitionCount); - FILE* stream = chunk->GetStream(); + Stream* stream = chunk->GetStream(); for (uint32 i = 0; i < definitionCount; i++) _definitions->push_back(WorldModelDefinition::Read(stream)); } @@ -202,14 +197,13 @@ void WorldModelHandler::ReadModelPaths() _paths->reserve(paths); for (uint32 i = 0; i < paths; i++) { - FILE* stream = mwid->GetStream(); - fseek(stream, i * 4, SEEK_CUR); - uint32 offset; - if (fread(&offset, sizeof(uint32), 1, stream) != 1) - printf("WorldModelDefinition::Read: Error reading data, expected 1, read 0\n"); - FILE* dataStream = mwmo->GetStream(); - fseek(dataStream, offset + mwmo->Offset, SEEK_SET); - _paths->push_back(Utils::ReadString(dataStream)); + Stream* stream = mwid->GetStream(); + stream->Seek(i * 4, SEEK_CUR); + uint32 offset = stream->Read<uint32>(); + + Stream* dataStream = mwmo->GetStream(); + dataStream->Seek(offset + mwmo->Offset, SEEK_SET); + _paths->push_back(dataStream->ReadString()); } } diff --git a/src/tools/mesh_extractor/WorldModelHandler.h b/src/tools/mesh_extractor/WorldModelHandler.h index 87a5ad62938..7924eb3f1a9 100644 --- a/src/tools/mesh_extractor/WorldModelHandler.h +++ b/src/tools/mesh_extractor/WorldModelHandler.h @@ -22,7 +22,7 @@ public: uint16 Flags; uint16 DoodadSet; - static WorldModelDefinition Read(FILE* file); + static WorldModelDefinition Read(Stream* file); }; class WorldModelHandler : public ObjectDataHandler diff --git a/src/tools/mesh_extractor/WorldModelRoot.cpp b/src/tools/mesh_extractor/WorldModelRoot.cpp index a00506f3b16..db909ad919c 100644 --- a/src/tools/mesh_extractor/WorldModelRoot.cpp +++ b/src/tools/mesh_extractor/WorldModelRoot.cpp @@ -37,7 +37,7 @@ void WorldModelRoot::ReadDoodadSets() if (!chunk) return; - FILE* stream = chunk->GetStream(); + Stream* stream = chunk->GetStream(); ASSERT(chunk->Length / 32 == Header.CountSets && "chunk.Length / 32 == Header.CountSets"); DoodadSets.reserve(Header.CountSets); for (uint32 i = 0; i < Header.CountSets; i++) @@ -56,14 +56,14 @@ void WorldModelRoot::ReadDoodadInstances() DoodadInstances.reserve(countInstances); for (uint32 i = 0; i < countInstances; i++) { - FILE* stream = chunk->GetStream(); - fseek(stream, instanceSize * i, SEEK_CUR); + Stream* stream = chunk->GetStream(); + stream->Seek(instanceSize * i, SEEK_CUR); DoodadInstance instance = DoodadInstance::Read(stream); - FILE* nameStream = nameChunk->GetStream(); + Stream* nameStream = nameChunk->GetStream(); if (instance.FileOffset >= nameChunk->Length) continue; - fseek(nameStream, instance.FileOffset, SEEK_CUR); - instance.File = Utils::ReadString(nameStream); + nameStream->Seek(instance.FileOffset, SEEK_CUR); + instance.File = nameStream->ReadString(); DoodadInstances.push_back(instance); } } @@ -74,6 +74,6 @@ void WorldModelRoot::ReadHeader() if (!chunk) return; - FILE* stream = chunk->GetStream(); + Stream* stream = chunk->GetStream(); Header = WorldModelHeader::Read(stream); } |
