From e93831319f8183f28f428b90d222884ad03a3557 Mon Sep 17 00:00:00 2001 From: momo5502 Date: Thu, 22 Dec 2016 00:06:08 +0100 Subject: [PATCH 1/5] [ZoneBuilder] IGfxWorld and IclipMap_t fixes --- src/Components/Modules/AssetHandler.cpp | 5 ---- .../Modules/AssetInterfaces/IGfxWorld.cpp | 26 +++++++--------- .../Modules/AssetInterfaces/IclipMap_t.cpp | 30 +++++++++---------- src/Game/Structs.hpp | 2 +- 4 files changed, 27 insertions(+), 36 deletions(-) diff --git a/src/Components/Modules/AssetHandler.cpp b/src/Components/Modules/AssetHandler.cpp index a6f1e41e..501d4657 100644 --- a/src/Components/Modules/AssetHandler.cpp +++ b/src/Components/Modules/AssetHandler.cpp @@ -240,7 +240,6 @@ namespace Components void AssetHandler::OffsetToAlias(Utils::Stream::Offset* offset) { - // Same here, reinterpret the value, as we're operating inside the game's environment void* pointer = (*Game::g_streamBlocks)[offset->getUnpackedBlock()].data + offset->getUnpackedOffset(); if (AssetHandler::Relocations.find(pointer) != AssetHandler::Relocations.end()) @@ -249,10 +248,6 @@ namespace Components } offset->pointer = *reinterpret_cast(pointer); - -#ifdef DEBUG - Game::XAssetHeader zob{ offset->pointer }; -#endif } void AssetHandler::ZoneSave(Game::XAsset asset, ZoneBuilder::Zone* builder) diff --git a/src/Components/Modules/AssetInterfaces/IGfxWorld.cpp b/src/Components/Modules/AssetInterfaces/IGfxWorld.cpp index e6ad6c38..d8fea78d 100644 --- a/src/Components/Modules/AssetInterfaces/IGfxWorld.cpp +++ b/src/Components/Modules/AssetInterfaces/IGfxWorld.cpp @@ -146,7 +146,7 @@ namespace Assets if (asset->sceneEntCellBits) { buffer->align(Utils::Stream::ALIGN_4); - buffer->save(asset->sceneEntCellBits, 2048, asset->cellCount); + buffer->save(asset->sceneEntCellBits, 1, asset->cellCount << 11); Utils::Stream::ClearPointer(&dest->sceneEntCellBits); } @@ -166,12 +166,12 @@ namespace Assets Game::GfxImage** imageDest = buffer->dest(); buffer->saveArray(asset->reflectionImages, asset->reflectionProbeCount); - for (unsigned int i = 0; i < asset->reflectionProbeCount; i++) + for (unsigned int i = 0; i < asset->reflectionProbeCount; ++i) { imageDest[i] = builder->requireAsset(Game::XAssetType::ASSET_TYPE_IMAGE, asset->reflectionImages[i]->name).image; } - Utils::Stream::ClearPointer(asset->reflectionImages); + Utils::Stream::ClearPointer(&dest->reflectionImages); } if (asset->reflectionProbes) @@ -205,7 +205,7 @@ namespace Assets Game::GfxLightmapArray* lightmapArrayDestTable = buffer->dest(); buffer->saveArray(asset->lightmaps, asset->lightmapCount); - for (int i = 0; i < asset->lightmapCount; i++) + for (int i = 0; i < asset->lightmapCount; ++i) { Game::GfxLightmapArray* lightmapArrayDest = &lightmapArrayDestTable[i]; Game::GfxLightmapArray* lightmapArray = &asset->lightmaps[i]; @@ -221,7 +221,7 @@ namespace Assets } } - Utils::Stream::ClearPointer(asset->reflectionImages); + Utils::Stream::ClearPointer(&dest->lightmaps); } buffer->pushBlock(Game::XFILE_BLOCK_RUNTIME); @@ -256,13 +256,12 @@ namespace Assets { if (asset->vd.vertices) { + AssertSize(Game::GfxWorldVertex, 44); + buffer->align(Utils::Stream::ALIGN_4); buffer->saveArray(asset->vd.vertices, asset->vertexCount); Utils::Stream::ClearPointer(&dest->vd.vertices); } - - // this one has no if statement on purpouse - buffer->save(&asset->vd.worldVb, 1, 4); } // saveGfxWorldVertexLayerData @@ -274,8 +273,6 @@ namespace Assets buffer->saveArray(asset->vld.data, asset->vertexLayerDataSize); Utils::Stream::ClearPointer(&dest->vld.data); } - - buffer->save(&asset->vld.layerVb, 1, 4); } if (asset->indices) @@ -295,7 +292,7 @@ namespace Assets if (asset->rowDataStart) { buffer->align(Utils::Stream::ALIGN_2); - buffer->saveArray(asset->rowDataStart, (asset->maxs[asset->rowAxis] - asset->mins[asset->rowAxis]) + 2); + buffer->saveArray(asset->rowDataStart, (asset->maxs[asset->rowAxis] - asset->mins[asset->rowAxis]) + 1); Utils::Stream::ClearPointer(&dest->rowDataStart); } @@ -553,7 +550,7 @@ namespace Assets this->saveGfxWorldDpvsPlanes(asset, &asset->dpvsPlanes, &dest->dpvsPlanes, builder); - uint32_t cellCount = asset->dpvsPlanes.cellCount; + int cellCount = asset->dpvsPlanes.cellCount; if (asset->aabbTreeCounts) { @@ -572,7 +569,7 @@ namespace Assets Game::GfxCellTree* destCellTreeTable = buffer->dest(); buffer->saveArray(asset->aabbTrees, cellCount); - for (unsigned int i = 0; i < cellCount; ++i) + for (int i = 0; i < cellCount; ++i) { Game::GfxCellTree* destCellTree = &destCellTreeTable[i]; Game::GfxCellTree* cellTree = &asset->aabbTrees[i]; @@ -614,7 +611,6 @@ namespace Assets Utils::Stream::ClearPointer(&destCellTree->aabbTree); } - } Utils::Stream::ClearPointer(&dest->aabbTrees); @@ -628,7 +624,7 @@ namespace Assets Game::GfxCell* destCellTable = buffer->dest(); buffer->saveArray(asset->cells, cellCount); - for (unsigned int i = 0; i < cellCount; ++i) + for (int i = 0; i < cellCount; ++i) { Game::GfxCell* destCell = &destCellTable[i]; Game::GfxCell* cell = &asset->cells[i]; diff --git a/src/Components/Modules/AssetInterfaces/IclipMap_t.cpp b/src/Components/Modules/AssetInterfaces/IclipMap_t.cpp index bb37082a..87ab4e34 100644 --- a/src/Components/Modules/AssetInterfaces/IclipMap_t.cpp +++ b/src/Components/Modules/AssetInterfaces/IclipMap_t.cpp @@ -29,9 +29,9 @@ namespace Assets } else { + buffer->align(Utils::Stream::ALIGN_4); builder->storePointer(asset->cPlanes); - - buffer->align(Utils::Stream::ALIGN_4); + buffer->saveArray(asset->cPlanes, asset->numCPlanes); Utils::Stream::ClearPointer(&dest->cPlanes); } @@ -93,9 +93,9 @@ namespace Assets } else { + buffer->align(Utils::Stream::ALIGN_4); builder->storePointer(sides[i].side); - - buffer->align(Utils::Stream::ALIGN_4); + buffer->save(sides[i].side); Utils::Stream::ClearPointer(&sides[i].side); } @@ -130,9 +130,9 @@ namespace Assets } else { + buffer->align(Utils::Stream::ALIGN_4); builder->storePointer(nodes[i].plane); - - buffer->align(Utils::Stream::ALIGN_4); + buffer->save(nodes[i].plane); Utils::Stream::ClearPointer(&nodes[i].plane); } @@ -178,9 +178,9 @@ namespace Assets } else { + buffer->align(Utils::Stream::ALIGN_2); builder->storePointer(node[i].data.brushes); - - buffer->align(Utils::Stream::ALIGN_2); + buffer->saveArray(node[i].data.brushes, node[i].leafBrushCount); Utils::Stream::ClearPointer(&node[i].data.brushes); } @@ -254,9 +254,9 @@ namespace Assets } else { + buffer->align(Utils::Stream::ALIGN_4); builder->storePointer(border[i].borders); - - buffer->align(Utils::Stream::ALIGN_4); + buffer->save(border[i].borders); Utils::Stream::ClearPointer(&border[i].borders); } @@ -302,11 +302,11 @@ namespace Assets } else { - builder->storePointer(brushes[i].brushSide); - AssertSize(Game::cbrushside_t, 8); - buffer->align(Utils::Stream::ALIGN_4); + buffer->align(Utils::Stream::ALIGN_4); + builder->storePointer(brushes[i].brushSide); + Game::cbrushside_t* side = buffer->dest(); buffer->save(brushes[i].brushSide); @@ -318,9 +318,9 @@ namespace Assets } else { + buffer->align(Utils::Stream::ALIGN_4); builder->storePointer(brushes[i].brushSide->side); - - buffer->align(Utils::Stream::ALIGN_4); + buffer->save(brushes[i].brushSide->side); Utils::Stream::ClearPointer(&side->side); } diff --git a/src/Game/Structs.hpp b/src/Game/Structs.hpp index 0ff944fd..b8611108 100644 --- a/src/Game/Structs.hpp +++ b/src/Game/Structs.hpp @@ -2948,7 +2948,7 @@ namespace Game { GfxPortalWritable writable; DpvsPlane plane; - float(*vertices)[3]; + vec3_t* vertices; char unknown[2]; char vertexCount; float hullAxis[2][3]; From 56ba9d0ed617d0329127c61c85b3d17523ca203e Mon Sep 17 00:00:00 2001 From: momo5502 Date: Thu, 22 Dec 2016 01:23:48 +0100 Subject: [PATCH 2/5] [IGfxWorld] Code styling --- .../Modules/AssetInterfaces/IGfxWorld.cpp | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/Components/Modules/AssetInterfaces/IGfxWorld.cpp b/src/Components/Modules/AssetInterfaces/IGfxWorld.cpp index d8fea78d..37fea8ae 100644 --- a/src/Components/Modules/AssetInterfaces/IGfxWorld.cpp +++ b/src/Components/Modules/AssetInterfaces/IGfxWorld.cpp @@ -85,8 +85,6 @@ namespace Assets builder->loadAsset(Game::XAssetType::ASSET_TYPE_IMAGE, asset->unknownImage->name); } - - if (asset->dpvs.surfaces) { for (int i = 0; i < asset->dpvsSurfaceCount; ++i) @@ -266,7 +264,6 @@ namespace Assets // saveGfxWorldVertexLayerData { - if (asset->vld.data) { // no align for char @@ -389,7 +386,7 @@ namespace Assets Game::GfxSurface* destSurfaceTable = buffer->dest(); buffer->saveArray(asset->surfaces, world->dpvsSurfaceCount); - for (int i = 0; i < world->dpvsSurfaceCount; i++) + for (int i = 0; i < world->dpvsSurfaceCount; ++i) { Game::GfxSurface* surface = &asset->surfaces[i]; Game::GfxSurface* destSurface = &destSurfaceTable[i]; @@ -420,7 +417,7 @@ namespace Assets Game::GfxStaticModelDrawInst* destModelTable = buffer->dest(); buffer->saveArray(asset->smodelDrawInsts, asset->smodelCount); - for (unsigned int i = 0; i < asset->smodelCount; i++) + for (unsigned int i = 0; i < asset->smodelCount; ++i) { Game::GfxStaticModelDrawInst* model = &asset->smodelDrawInsts[i]; Game::GfxStaticModelDrawInst* destModel = &destModelTable[i]; @@ -480,9 +477,9 @@ namespace Assets } // this covers [0][0], [1][0], [0][1], [1][1], [0][2], [1][2] - for (char i = 0; i < 3; i++) + for (char i = 0; i < 3; ++i) { - for (char j = 0; j < 2; j++) + for (char j = 0; j < 2; ++j) { if (asset->dynEntVisData[j][i]) { @@ -498,7 +495,7 @@ namespace Assets void IGfxWorld::save(Game::XAssetHeader header, Components::ZoneBuilder::Zone* builder) { - AssertSize(Game::GfxWorld, 0x274); + AssertSize(Game::GfxWorld, 628); Utils::Stream* buffer = builder->getBuffer(); Game::GfxWorld* asset = header.gfxWorld; From 64e76c905e32d253b8cf31b40dfe11d4a1c3b412 Mon Sep 17 00:00:00 2001 From: momo5502 Date: Thu, 22 Dec 2016 01:32:07 +0100 Subject: [PATCH 3/5] [General] Use pre-incrementation in for loops --- src/Components/Modules/AntiCheat.cpp | 2 +- src/Components/Modules/AssetHandler.cpp | 6 +++--- .../Modules/AssetInterfaces/IclipMap_t.cpp | 8 ++++---- src/Components/Modules/Maps.cpp | 14 +++++++------- 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/Components/Modules/AntiCheat.cpp b/src/Components/Modules/AntiCheat.cpp index 7ff43fd3..4f3fd0b3 100644 --- a/src/Components/Modules/AntiCheat.cpp +++ b/src/Components/Modules/AntiCheat.cpp @@ -399,7 +399,7 @@ namespace Components dwSize += GetLengthSid(psidArray[0]); dwSize += sizeof(ACCESS_DENIED_ACE) - sizeof(DWORD); - for (UINT i = 1; i < _countof(psidArray); i++) + for (UINT i = 1; i < _countof(psidArray); ++i) { // DWORD is the SidStart field, which is not used for absolute format dwSize += GetLengthSid(psidArray[i]); diff --git a/src/Components/Modules/AssetHandler.cpp b/src/Components/Modules/AssetHandler.cpp index 501d4657..e12506aa 100644 --- a/src/Components/Modules/AssetHandler.cpp +++ b/src/Components/Modules/AssetHandler.cpp @@ -134,15 +134,15 @@ namespace Components // Fix shader const stuff if (type == Game::XAssetType::ASSET_TYPE_TECHSET && Zones::Version() >= 359) { - for (int i = 0; i < 48; i++) + for (int i = 0; i < 48; ++i) { if (asset.techniqueSet->techniques[i]) { - for (int j = 0; j < asset.techniqueSet->techniques[i]->numPasses; j++) + for (int j = 0; j < asset.techniqueSet->techniques[i]->numPasses; ++j) { Game::MaterialPass* pass = &asset.techniqueSet->techniques[i]->passes[j]; - for (int k = 0; k < (pass->argCount1 + pass->argCount2 + pass->argCount3); k++) + for (int k = 0; k < (pass->argCount1 + pass->argCount2 + pass->argCount3); ++k) { if (pass->argumentDef[k].type == D3DSHADER_PARAM_REGISTER_TYPE::D3DSPR_CONSTINT) { diff --git a/src/Components/Modules/AssetInterfaces/IclipMap_t.cpp b/src/Components/Modules/AssetInterfaces/IclipMap_t.cpp index 87ab4e34..a5e3a876 100644 --- a/src/Components/Modules/AssetInterfaces/IclipMap_t.cpp +++ b/src/Components/Modules/AssetInterfaces/IclipMap_t.cpp @@ -416,7 +416,7 @@ namespace Assets buffer->pushBlock(Game::XFILE_BLOCK_RUNTIME); - for (int i = 0; i < 2; i++) + for (int i = 0; i < 2; ++i) { if (asset->dynEntPoseList[i]) { @@ -429,7 +429,7 @@ namespace Assets } } - for (int i = 0; i < 2; i++) + for (int i = 0; i < 2; ++i) { if (asset->dynEntClientList[i]) { @@ -441,7 +441,7 @@ namespace Assets } } - for (int i = 0; i < 2; i++) + for (int i = 0; i < 2; ++i) { if (asset->dynEntCollList[i]) { @@ -466,7 +466,7 @@ namespace Assets builder->loadAsset(Game::XAssetType::ASSET_TYPE_XMODEL, m->name); } - for (int j = 0; j < 2; j++) + for (int j = 0; j < 2; ++j) { Game::DynEntityDef* def = asset->dynEntDefList[j]; diff --git a/src/Components/Modules/Maps.cpp b/src/Components/Modules/Maps.cpp index ecd1a6bb..25da9e99 100644 --- a/src/Components/Modules/Maps.cpp +++ b/src/Components/Modules/Maps.cpp @@ -306,7 +306,7 @@ namespace Components map.append(fmt::sprintf("mtllib %s.mtl\n\n", world->baseName)); Logger::Print("Writing vertices...\n"); - for (unsigned int i = 0; i < world->worldDraw.vertexCount; i++) + for (unsigned int i = 0; i < world->worldDraw.vertexCount; ++i) { float x = world->worldDraw.vd.vertices[i].xyz[1]; float y = world->worldDraw.vd.vertices[i].xyz[2]; @@ -318,7 +318,7 @@ namespace Components map.append("\n"); Logger::Print("Writing texture coordinates...\n"); - for (unsigned int i = 0; i < world->worldDraw.vertexCount; i++) + for (unsigned int i = 0; i < world->worldDraw.vertexCount; ++i) { map.append(fmt::sprintf("vt %.6f %.6f\n", world->worldDraw.vd.vertices[i].texCoord[0], -world->worldDraw.vd.vertices[i].texCoord[1])); } @@ -327,11 +327,11 @@ namespace Components int materialCount = 0; Game::Material** materials = allocator.allocateArray(world->dpvs.staticSurfaceCount); - for (unsigned int i = 0; i < world->dpvs.staticSurfaceCount; i++) + for (unsigned int i = 0; i < world->dpvs.staticSurfaceCount; ++i) { bool isNewMat = true; - for (int j = 0; j < materialCount; j++) + for (int j = 0; j < materialCount; ++j) { if (world->dpvs.surfaces[i].material == materials[j]) { @@ -350,7 +350,7 @@ namespace Components mtl.append(fmt::sprintf("# Material Count: %d\n", materialCount)); Logger::Print("Exporting materials and faces...\n"); - for (int m = 0; m < materialCount; m++) + for (int m = 0; m < materialCount; ++m) { std::string name = materials[m]->name; @@ -386,14 +386,14 @@ namespace Components mtl.append(fmt::sprintf("map_Ka textures/%s.png\n", image->name)); mtl.append(fmt::sprintf("map_Kd textures/%s.png\n", image->name)); - for (unsigned int i = 0; i < world->dpvs.staticSurfaceCount; i++) + for (unsigned int i = 0; i < world->dpvs.staticSurfaceCount; ++i) { if (world->dpvs.surfaces[i].material != materials[m]) continue; int vertOffset = world->dpvs.surfaces[i].tris.firstVertex + 1;//+1 cus obj starts at 1 int indexOffset = world->dpvs.surfaces[i].tris.baseIndex; - for (unsigned short j = 0; j < world->dpvs.surfaces[i].tris.triCount; j++) + for (unsigned short j = 0; j < world->dpvs.surfaces[i].tris.triCount; ++j) { int a = world->worldDraw.indices[indexOffset + j * 3 + 0] + vertOffset; int b = world->worldDraw.indices[indexOffset + j * 3 + 1] + vertOffset; From 33754f40aa0ebc5de2663b79a9ecf1a2350112b7 Mon Sep 17 00:00:00 2001 From: momo5502 Date: Thu, 22 Dec 2016 02:00:09 +0100 Subject: [PATCH 4/5] [Stream] Reimplement the correct stream writing (thx @TheApadayo) --- src/Utils/Stream.cpp | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/Utils/Stream.cpp b/src/Utils/Stream.cpp index 9f8bd08b..64edd840 100644 --- a/src/Utils/Stream.cpp +++ b/src/Utils/Stream.cpp @@ -96,9 +96,15 @@ namespace Utils char* Stream::save(Game::XFILE_BLOCK_TYPES stream, const void * _str, size_t size, size_t count) { - //if (stream == XFILE_BLOCK_TEMP || stream == XFILE_BLOCK_VIRTUAL || stream == XFILE_BLOCK_PHYSICAL) // Only those seem to actually write data. - // As I'm not sure though, I'll still write the data - // Use IncreaseBlockSize to fill virtual streams + // Only those seem to actually write data. + // As I'm not sure though, I'll still write the data + // Use IncreaseBlockSize to fill virtual streams + if (stream != Game::XFILE_BLOCK_TEMP && stream != Game::XFILE_BLOCK_VIRTUAL && stream != Game::XFILE_BLOCK_PHYSICAL) + { + this->increaseBlockSize(stream, size * count); + return this->at(); + } + auto data = this->data(); if (this->isCriticalSection() && this->length() + (size * count) > this->capacity()) From 896c3b70688dacd8a192098e227a2f49a059fd53 Mon Sep 17 00:00:00 2001 From: momo5502 Date: Thu, 22 Dec 2016 02:48:00 +0100 Subject: [PATCH 5/5] [ZoneBuilder] Correctly write GfxImages --- .../Modules/AssetInterfaces/IGfxImage.cpp | 18 +++++----- src/Components/Modules/Maps.cpp | 2 +- src/Components/Modules/ZoneBuilder.cpp | 36 +++++++++++++++++-- src/Components/Modules/ZoneBuilder.hpp | 6 ++++ src/Components/Modules/Zones.cpp | 2 +- src/Game/Structs.hpp | 28 +++++++++++---- 6 files changed, 71 insertions(+), 21 deletions(-) diff --git a/src/Components/Modules/AssetInterfaces/IGfxImage.cpp b/src/Components/Modules/AssetInterfaces/IGfxImage.cpp index 6933759a..a6709810 100644 --- a/src/Components/Modules/AssetInterfaces/IGfxImage.cpp +++ b/src/Components/Modules/AssetInterfaces/IGfxImage.cpp @@ -40,7 +40,7 @@ namespace Assets const Game::GfxImageFileHeader* iwiHeader = reinterpret_cast(iwiBuffer.data()); - image->mapType = 3; + image->mapType = Game::MAPTYPE_2D; image->dataLen1 = iwiHeader->fileSizeForPicmip[0] - 32; image->dataLen2 = iwiHeader->fileSizeForPicmip[0] - 32; @@ -52,7 +52,7 @@ namespace Assets std::memcpy(image->loadDef->dimensions, iwiHeader->dimensions, 6); image->loadDef->flags = 0; - image->loadDef->mipLevels = 0; + image->loadDef->levelCount = 0; switch (iwiHeader->format) { @@ -93,6 +93,7 @@ namespace Assets void IGfxImage::save(Game::XAssetHeader header, Components::ZoneBuilder::Zone* builder) { AssertSize(Game::GfxImage, 32); + AssertSize(Game::MapType, 1); Utils::Stream* buffer = builder->getBuffer(); Game::GfxImage* asset = header.image; @@ -109,21 +110,18 @@ namespace Assets buffer->pushBlock(Game::XFILE_BLOCK_TEMP); - if (asset->texture) + if (asset->loadDef) { buffer->align(Utils::Stream::ALIGN_4); Game::GfxImageLoadDef* destTexture = buffer->dest(); - buffer->save(asset->loadDef, 16); + buffer->save(asset->loadDef, 16, 1); - builder->incrementExternalSize(asset->loadDef->dataSize); + builder->incrementExternalSize(asset->loadDef->resourceSize); - // Zero the size! - destTexture->dataSize = 0; - - if (destTexture->dataSize > 0) + if (destTexture->resourceSize > 0) { - buffer->save(asset->loadDef->data, asset->loadDef->dataSize); + buffer->save(asset->loadDef->data, asset->loadDef->resourceSize); } Utils::Stream::ClearPointer(&dest->loadDef); diff --git a/src/Components/Modules/Maps.cpp b/src/Components/Modules/Maps.cpp index 25da9e99..a67f5462 100644 --- a/src/Components/Modules/Maps.cpp +++ b/src/Components/Modules/Maps.cpp @@ -377,7 +377,7 @@ namespace Components } std::string _name = fmt::sprintf("raw/mapdump/%s/textures/%s.png", world->baseName, image->name); - D3DXSaveTextureToFile(std::wstring(_name.begin(), _name.end()).data(), D3DXIFF_PNG, image->texture, NULL); + D3DXSaveTextureToFile(std::wstring(_name.begin(), _name.end()).data(), D3DXIFF_PNG, image->map, NULL); mtl.append(fmt::sprintf("\nnewmtl %s\n", name.data())); mtl.append("Ka 1.0000 1.0000 1.0000\n"); diff --git a/src/Components/Modules/ZoneBuilder.cpp b/src/Components/Modules/ZoneBuilder.cpp index 6795a95f..6355cb1b 100644 --- a/src/Components/Modules/ZoneBuilder.cpp +++ b/src/Components/Modules/ZoneBuilder.cpp @@ -2,6 +2,8 @@ namespace Components { + Utils::Memory::Allocator ZoneBuilder::MemAllocator; + std::string ZoneBuilder::TraceZone; std::vector> ZoneBuilder::TraceAssets; @@ -316,7 +318,7 @@ namespace Components this->buffer.enterCriticalSection(); Game::XFile* header = reinterpret_cast(this->buffer.data()); header->size = this->buffer.length() - sizeof(Game::XFile); // Write correct data size - header->externalSize = 0;//this->externalSize; // This actually stores how much external data has to be loaded. It's used to calculate the loadscreen progress + header->externalSize = this->externalSize; // This actually stores how much external data has to be loaded. It's used to calculate the loadscreen progress // Write stream sizes for (int i = 0; i < Game::MAX_XFILE_COUNT; ++i) @@ -486,6 +488,25 @@ namespace Components return AssetTrace; } + int ZoneBuilder::StoreTexture(Game::GfxImageLoadDef **loadDef, Game::GfxImage *image) + { + size_t size = 16 + (*loadDef)->resourceSize; + void* data = ZoneBuilder::MemAllocator.allocate(size); + std::memcpy(data, *loadDef, size); + + image->loadDef = reinterpret_cast(data); + + return 0; + } + + void ZoneBuilder::ReleaseTexture(Game::XAssetHeader header) + { + if (header.image && header.image->loadDef) + { + ZoneBuilder::MemAllocator.free(header.image->loadDef); + } + } + ZoneBuilder::ZoneBuilder() { AssertSize(Game::XFileHeader, 21); @@ -497,7 +518,13 @@ namespace Components if (ZoneBuilder::IsEnabled()) { // Prevent loading textures (preserves loaddef) - Utils::Hook::Set(0x51F4E0, 0xC3); + //Utils::Hook::Set(Game::Load_Texture, 0xC3); + + // Store the loaddef + Utils::Hook(Game::Load_Texture, StoreTexture, HOOK_JUMP).install()->quick(); + + // Release the loaddef + Game::DB_ReleaseXAssetHandlers[Game::XAssetType::ASSET_TYPE_IMAGE] = ZoneBuilder::ReleaseTexture; //r_loadForrenderer = 0 Utils::Hook::Set(0x519DDF, 0); @@ -620,4 +647,9 @@ namespace Components }); } } + + ZoneBuilder::~ZoneBuilder() + { + assert(ZoneBuilder::MemAllocator.empty()); + } } diff --git a/src/Components/Modules/ZoneBuilder.hpp b/src/Components/Modules/ZoneBuilder.hpp index 207e6c24..7a164bc6 100644 --- a/src/Components/Modules/ZoneBuilder.hpp +++ b/src/Components/Modules/ZoneBuilder.hpp @@ -75,6 +75,7 @@ namespace Components }; ZoneBuilder(); + ~ZoneBuilder(); #if defined(DEBUG) || defined(FORCE_UNIT_TESTS) const char* getName() { return "ZoneBuilder"; }; @@ -87,5 +88,10 @@ namespace Components static void BeginAssetTrace(std::string zone); static std::vector> EndAssetTrace(); + + private: + static Utils::Memory::Allocator MemAllocator; + static int StoreTexture(Game::GfxImageLoadDef **loadDef, Game::GfxImage *image); + static void ReleaseTexture(Game::XAssetHeader header); }; } diff --git a/src/Components/Modules/Zones.cpp b/src/Components/Modules/Zones.cpp index 34493123..ad64f4c6 100644 --- a/src/Components/Modules/Zones.cpp +++ b/src/Components/Modules/Zones.cpp @@ -1167,7 +1167,7 @@ namespace Components struct { Game::GfxImageLoadDef* texture; - char mapType; + Game::MapType mapType; char semantic; char category; char flags; diff --git a/src/Game/Structs.hpp b/src/Game/Structs.hpp index b8611108..f369ed64 100644 --- a/src/Game/Structs.hpp +++ b/src/Game/Structs.hpp @@ -285,27 +285,41 @@ namespace Game int fileSizeForPicmip[4]; }; - struct GfxImageLoadDef // actually a IDirect3DTexture* but this is easier + struct __declspec(align(4)) GfxImageLoadDef { - char mipLevels; + char levelCount; char flags; - short dimensions[3]; - int format; // usually the compression Magic - int dataSize; // set to zero to load from IWD + __int16 dimensions[3]; + int format; + int resourceSize; char data[1]; }; + enum MapType : char + { + MAPTYPE_NONE = 0x0, + MAPTYPE_INVALID1 = 0x1, + MAPTYPE_INVALID2 = 0x2, + MAPTYPE_2D = 0x3, + MAPTYPE_3D = 0x4, + MAPTYPE_CUBE = 0x5, + MAPTYPE_COUNT = 0x6, + }; + struct GfxImage { union { GfxImageLoadDef* loadDef; #ifdef __cplusplus - IDirect3DTexture9* texture; + IDirect3DBaseTexture9 *basemap; + IDirect3DTexture9 *map; + IDirect3DVolumeTexture9 *volmap; + IDirect3DCubeTexture9 *cubemap; #endif }; - char mapType; // 5 is cube, 4 is 3d, 3 is 2d + MapType mapType; char semantic; char category; char flags;