diff --git a/src/Components/Modules/AssetInterfaces/IFxEffectDef.cpp b/src/Components/Modules/AssetInterfaces/IFxEffectDef.cpp index ddd0d7f0..8470546b 100644 --- a/src/Components/Modules/AssetInterfaces/IFxEffectDef.cpp +++ b/src/Components/Modules/AssetInterfaces/IFxEffectDef.cpp @@ -1,388 +1,388 @@ -#include - -namespace Assets -{ - void IFxEffectDef::load(Game::XAssetHeader* /*header*/, std::string name, Components::ZoneBuilder::Zone* /*builder*/) - { -#if 0 - Components::FileSystem::File rawFx(fmt::sprintf("fx/%s.efx", name.data())); - if (rawFx.exists()) - { - const char* session = rawFx.getBuffer().data(); - Game::Com_BeginParseSession("fx"); - Game::Com_SetSpaceDelimited(0); - Game::Com_SetParseNegativeNumbers(1); - - const char* format = Game::Com_Parse(&session); - if (format != "iwfx"s) - { - Game::Com_EndParseSession(); - Components::Logger::Error("Effect needs to be updated from the legacy format.\n"); - } - - int version = atoi(Game::Com_Parse(&session)); +#include + +namespace Assets +{ + void IFxEffectDef::load(Game::XAssetHeader* /*header*/, std::string name, Components::ZoneBuilder::Zone* /*builder*/) + { +#if 0 + Components::FileSystem::File rawFx(fmt::sprintf("fx/%s.efx", name.data())); + if (rawFx.exists()) + { + const char* session = rawFx.getBuffer().data(); + Game::Com_BeginParseSession("fx"); + Game::Com_SetSpaceDelimited(0); + Game::Com_SetParseNegativeNumbers(1); + + const char* format = Game::Com_Parse(&session); + if (format != "iwfx"s) + { + Game::Com_EndParseSession(); + Components::Logger::Error("Effect needs to be updated from the legacy format.\n"); + } + + int version = atoi(Game::Com_Parse(&session)); if (version > 2) { Game::Com_EndParseSession(); Components::Logger::Error("Version %i is too high. I can only handle up to %i.\n", version, 2); - } - - Game::FxEditorEffectDef efx; - ZeroMemory(&efx, sizeof(efx)); - -// for (int i = 0; i < FX_ELEM_FIELD_COUNT; ++i) -// { -// Game::s_elemFields[i].handler(&session, efx.elems); -// } - - Game::Com_EndParseSession(); - } -#endif - } - - void IFxEffectDef::markFxElemVisuals(Game::FxElemVisuals* visuals, char elemType, Components::ZoneBuilder::Zone* builder) - { - switch (elemType) - { - case 7: - { - if (visuals->xmodel) - { - builder->loadAsset(Game::XAssetType::ASSET_TYPE_XMODEL, visuals->xmodel); - } - - break; - } - - case 8: - case 9: - break; - - case 0xA: - { - builder->loadAsset(Game::XAssetType::ASSET_TYPE_SOUND, std::string(visuals->soundName), false); - break; - } - - case 0xC: - { - if (visuals->effectDef) - { - builder->loadAsset(Game::XAssetType::ASSET_TYPE_FX, visuals->effectDef, false); - } - - break; - } - - default: - { - if (visuals->material) - { - builder->loadAsset(Game::XAssetType::ASSET_TYPE_MATERIAL, visuals->material); - } - - break; - } - } - } - - void IFxEffectDef::mark(Game::XAssetHeader header, Components::ZoneBuilder::Zone* builder) - { - Game::FxEffectDef* asset = header.fx; - - for (int i = 0; i < (asset->elemDefCountEmission + asset->elemDefCountLooping + asset->elemDefCountOneShot); ++i) - { - Game::FxElemDef* elemDef = &asset->elemDefs[i]; - - { - if (elemDef->elemType == 11) - { - if (elemDef->visuals.markArray) - { - for (char j = 0; j < elemDef->visualCount; ++j) - { - if (elemDef->visuals.markArray[j].data[0]) - { - builder->loadAsset(Game::XAssetType::ASSET_TYPE_MATERIAL, elemDef->visuals.markArray[j].data[0]); - } - - if (elemDef->visuals.markArray[j].data[1]) - { - builder->loadAsset(Game::XAssetType::ASSET_TYPE_MATERIAL, elemDef->visuals.markArray[j].data[1]); - } - } - } - } - else if (elemDef->visualCount > 1) - { - if (elemDef->visuals.array) - { - for (char j = 0; j < elemDef->visualCount; ++j) - { - this->markFxElemVisuals(&elemDef->visuals.array[j], elemDef->elemType, builder); - } - } - } - else - { - this->markFxElemVisuals(&elemDef->visuals.instance, elemDef->elemType, builder); - } - } - - if (elemDef->effectOnImpact) - { - builder->loadAsset(Game::XAssetType::ASSET_TYPE_FX, elemDef->effectOnImpact, false); - } - - if (elemDef->effectOnDeath) - { - builder->loadAsset(Game::XAssetType::ASSET_TYPE_FX, elemDef->effectOnDeath, false); - } - - if (elemDef->effectEmitted) - { - builder->loadAsset(Game::XAssetType::ASSET_TYPE_FX, elemDef->effectEmitted, false); - } - } - } - - void IFxEffectDef::saveFxElemVisuals(Game::FxElemVisuals* visuals, Game::FxElemVisuals* destVisuals, char elemType, Components::ZoneBuilder::Zone* builder) - { - Utils::Stream* buffer = builder->getBuffer(); - - switch (elemType) - { - case 7: - { - if (visuals->xmodel) - { - destVisuals->xmodel = builder->saveSubAsset(Game::XAssetType::ASSET_TYPE_XMODEL, visuals->xmodel).model; - } - - break; - } - - case 8: - case 9: - break; - - case 0xA: - { - if (visuals->soundName) - { - buffer->saveString(visuals->soundName); - Utils::Stream::ClearPointer(&destVisuals->soundName); - } - - break; - } - - case 0xC: - { - if (visuals->effectDef) - { - buffer->saveString(visuals->effectDef->name); - Utils::Stream::ClearPointer(&destVisuals->effectDef); - } - - break; - } - - default: - { - if (visuals->material) - { - destVisuals->material = builder->saveSubAsset(Game::XAssetType::ASSET_TYPE_MATERIAL, visuals->material).material; - } - - break; - } - } - } - - void IFxEffectDef::save(Game::XAssetHeader header, Components::ZoneBuilder::Zone* builder) - { - AssertSize(Game::FxEffectDef, 32); - - Utils::Stream* buffer = builder->getBuffer(); - Game::FxEffectDef* asset = header.fx; - Game::FxEffectDef* dest = buffer->dest(); - buffer->save(asset); - - buffer->pushBlock(Game::XFILE_BLOCK_VIRTUAL); - - if (asset->name) - { - buffer->saveString(builder->getAssetName(this->getType(), asset->name)); - Utils::Stream::ClearPointer(&dest->name); - } - - if (asset->elemDefs) - { - AssertSize(Game::FxElemDef, 252); - buffer->align(Utils::Stream::ALIGN_4); - - Game::FxElemDef* destElemDefs = buffer->dest(); - buffer->saveArray(asset->elemDefs, asset->elemDefCountEmission + asset->elemDefCountLooping + asset->elemDefCountOneShot); - - for (int i = 0; i < (asset->elemDefCountEmission + asset->elemDefCountLooping + asset->elemDefCountOneShot); ++i) - { - Game::FxElemDef* destElemDef = &destElemDefs[i]; - Game::FxElemDef* elemDef = &asset->elemDefs[i]; - - if (elemDef->velSamples) - { - AssertSize(Game::FxElemVelStateSample, 96); - buffer->align(Utils::Stream::ALIGN_4); - - buffer->saveArray(elemDef->velSamples, elemDef->velIntervalCount + 1); - - Utils::Stream::ClearPointer(&destElemDef->velSamples); - } - - if (elemDef->visSamples) - { - AssertSize(Game::FxElemVisStateSample, 48); - buffer->align(Utils::Stream::ALIGN_4); - - buffer->saveArray(elemDef->visSamples, elemDef->visStateIntervalCount + 1); - - Utils::Stream::ClearPointer(&destElemDef->visSamples); - } - - // Save_FxElemDefVisuals - { - if (elemDef->elemType == 11) - { - if (elemDef->visuals.markArray) - { - AssertSize(Game::FxElemMarkVisuals, 8); - buffer->align(Utils::Stream::ALIGN_4); - - Game::FxElemMarkVisuals* destMarkArray = buffer->dest(); - buffer->saveArray(elemDef->visuals.markArray, elemDef->visualCount); - - for (char j = 0; j < elemDef->visualCount; ++j) - { - if (elemDef->visuals.markArray[j].data[0]) - { - destMarkArray[j].data[0] = builder->saveSubAsset(Game::XAssetType::ASSET_TYPE_MATERIAL, elemDef->visuals.markArray[j].data[0]).material; - } - - if (elemDef->visuals.markArray[j].data[1]) - { - destMarkArray[j].data[1] = builder->saveSubAsset(Game::XAssetType::ASSET_TYPE_MATERIAL, elemDef->visuals.markArray[j].data[1]).material; - } - } - - Utils::Stream::ClearPointer(&destElemDef->visuals.markArray); - } - } - else if(elemDef->visualCount > 1) - { - if (elemDef->visuals.array) - { - AssertSize(Game::FxElemVisuals, 4); - buffer->align(Utils::Stream::ALIGN_4); - - Game::FxElemVisuals* destVisuals = buffer->dest(); - buffer->saveArray(elemDef->visuals.array, elemDef->visualCount); - - for (char j = 0; j < elemDef->visualCount; ++j) - { - this->saveFxElemVisuals(&elemDef->visuals.array[j], &destVisuals[j], elemDef->elemType, builder); - } - - Utils::Stream::ClearPointer(&destElemDef->visuals.array); - } - } - else - { - this->saveFxElemVisuals(&elemDef->visuals.instance, &destElemDef->visuals.instance, elemDef->elemType, builder); - } - } - - if (elemDef->effectOnImpact) - { - buffer->saveString(elemDef->effectOnImpact->name); - Utils::Stream::ClearPointer(&destElemDef->effectOnImpact); - } - - if (elemDef->effectOnDeath) - { - buffer->saveString(elemDef->effectOnDeath->name); - Utils::Stream::ClearPointer(&destElemDef->effectOnDeath); - } - - if (elemDef->effectEmitted) - { - buffer->saveString(elemDef->effectEmitted->name); - Utils::Stream::ClearPointer(&destElemDef->effectEmitted); - } - - // Save_FxElemExtendedDefPtr - { - AssertSize(Game::FxElemExtendedDef, 4); - - if (elemDef->elemType == 3) - { - // Save_FxTrailDef - { - if (elemDef->extendedDef.trailDef) - { - AssertSize(Game::FxTrailDef, 36); - buffer->align(Utils::Stream::ALIGN_4); - - Game::FxTrailDef* trailDef = elemDef->extendedDef.trailDef; - Game::FxTrailDef* destTrailDef = buffer->dest(); - buffer->save(trailDef); - - if (trailDef->verts) - { - AssertSize(Game::FxTrailVertex, 20); - buffer->align(Utils::Stream::ALIGN_4); - - buffer->saveArray(trailDef->verts, trailDef->vertCount); - Utils::Stream::ClearPointer(&destTrailDef->verts); - } - - if (trailDef->inds) - { - buffer->align(Utils::Stream::ALIGN_2); - - buffer->saveArray(trailDef->inds, trailDef->indCount); - Utils::Stream::ClearPointer(&destTrailDef->inds); - } - - Utils::Stream::ClearPointer(&destElemDef->extendedDef.trailDef); - } - } - } - else if (elemDef->elemType == 6) - { - if (elemDef->extendedDef.sparkFountain) - { - AssertSize(Game::FxSparkFountain, 52); - buffer->align(Utils::Stream::ALIGN_4); - - buffer->save(elemDef->extendedDef.sparkFountain); - Utils::Stream::ClearPointer(&destElemDef->extendedDef.sparkFountain); - } - } - else - { - if (elemDef->extendedDef.unknownBytes) - { - buffer->save(elemDef->extendedDef.unknownBytes); - Utils::Stream::ClearPointer(&destElemDef->extendedDef.unknownBytes); - } - } - } - } - - Utils::Stream::ClearPointer(&dest->elemDefs); - } - - buffer->popBlock(); - } -} + } + + Game::FxEditorEffectDef efx; + ZeroMemory(&efx, sizeof(efx)); + +// for (int i = 0; i < FX_ELEM_FIELD_COUNT; ++i) +// { +// Game::s_elemFields[i].handler(&session, efx.elems); +// } + + Game::Com_EndParseSession(); + } +#endif + } + + void IFxEffectDef::markFxElemVisuals(Game::FxElemVisuals* visuals, char elemType, Components::ZoneBuilder::Zone* builder) + { + switch (elemType) + { + case 7: + { + if (visuals->xmodel) + { + builder->loadAsset(Game::XAssetType::ASSET_TYPE_XMODEL, visuals->xmodel); + } + + break; + } + + case 8: + case 9: + break; + + case 0xA: + { + builder->loadAsset(Game::XAssetType::ASSET_TYPE_SOUND, std::string(visuals->soundName), false); + break; + } + + case 0xC: + { + if (visuals->effectDef) + { + builder->loadAsset(Game::XAssetType::ASSET_TYPE_FX, visuals->effectDef, false); + } + + break; + } + + default: + { + if (visuals->material) + { + builder->loadAsset(Game::XAssetType::ASSET_TYPE_MATERIAL, visuals->material); + } + + break; + } + } + } + + void IFxEffectDef::mark(Game::XAssetHeader header, Components::ZoneBuilder::Zone* builder) + { + Game::FxEffectDef* asset = header.fx; + + for (int i = 0; i < (asset->elemDefCountEmission + asset->elemDefCountLooping + asset->elemDefCountOneShot); ++i) + { + Game::FxElemDef* elemDef = &asset->elemDefs[i]; + + { + if (elemDef->elemType == 11) + { + if (elemDef->visuals.markArray) + { + for (char j = 0; j < elemDef->visualCount; ++j) + { + if (elemDef->visuals.markArray[j].data[0]) + { + builder->loadAsset(Game::XAssetType::ASSET_TYPE_MATERIAL, elemDef->visuals.markArray[j].data[0]); + } + + if (elemDef->visuals.markArray[j].data[1]) + { + builder->loadAsset(Game::XAssetType::ASSET_TYPE_MATERIAL, elemDef->visuals.markArray[j].data[1]); + } + } + } + } + else if (elemDef->visualCount > 1) + { + if (elemDef->visuals.array) + { + for (char j = 0; j < elemDef->visualCount; ++j) + { + this->markFxElemVisuals(&elemDef->visuals.array[j], elemDef->elemType, builder); + } + } + } + else + { + this->markFxElemVisuals(&elemDef->visuals.instance, elemDef->elemType, builder); + } + } + + if (elemDef->effectOnImpact) + { + builder->loadAsset(Game::XAssetType::ASSET_TYPE_FX, elemDef->effectOnImpact, false); + } + + if (elemDef->effectOnDeath) + { + builder->loadAsset(Game::XAssetType::ASSET_TYPE_FX, elemDef->effectOnDeath, false); + } + + if (elemDef->effectEmitted) + { + builder->loadAsset(Game::XAssetType::ASSET_TYPE_FX, elemDef->effectEmitted, false); + } + } + } + + void IFxEffectDef::saveFxElemVisuals(Game::FxElemVisuals* visuals, Game::FxElemVisuals* destVisuals, char elemType, Components::ZoneBuilder::Zone* builder) + { + Utils::Stream* buffer = builder->getBuffer(); + + switch (elemType) + { + case 7: + { + if (visuals->xmodel) + { + destVisuals->xmodel = builder->saveSubAsset(Game::XAssetType::ASSET_TYPE_XMODEL, visuals->xmodel).model; + } + + break; + } + + case 8: + case 9: + break; + + case 0xA: + { + if (visuals->soundName) + { + buffer->saveString(visuals->soundName); + Utils::Stream::ClearPointer(&destVisuals->soundName); + } + + break; + } + + case 0xC: + { + if (visuals->effectDef) + { + buffer->saveString(visuals->effectDef->name); + Utils::Stream::ClearPointer(&destVisuals->effectDef); + } + + break; + } + + default: + { + if (visuals->material) + { + destVisuals->material = builder->saveSubAsset(Game::XAssetType::ASSET_TYPE_MATERIAL, visuals->material).material; + } + + break; + } + } + } + + void IFxEffectDef::save(Game::XAssetHeader header, Components::ZoneBuilder::Zone* builder) + { + AssertSize(Game::FxEffectDef, 32); + + Utils::Stream* buffer = builder->getBuffer(); + Game::FxEffectDef* asset = header.fx; + Game::FxEffectDef* dest = buffer->dest(); + buffer->save(asset); + + buffer->pushBlock(Game::XFILE_BLOCK_VIRTUAL); + + if (asset->name) + { + buffer->saveString(builder->getAssetName(this->getType(), asset->name)); + Utils::Stream::ClearPointer(&dest->name); + } + + if (asset->elemDefs) + { + AssertSize(Game::FxElemDef, 252); + buffer->align(Utils::Stream::ALIGN_4); + + Game::FxElemDef* destElemDefs = buffer->dest(); + buffer->saveArray(asset->elemDefs, asset->elemDefCountEmission + asset->elemDefCountLooping + asset->elemDefCountOneShot); + + for (int i = 0; i < (asset->elemDefCountEmission + asset->elemDefCountLooping + asset->elemDefCountOneShot); ++i) + { + Game::FxElemDef* destElemDef = &destElemDefs[i]; + Game::FxElemDef* elemDef = &asset->elemDefs[i]; + + if (elemDef->velSamples) + { + AssertSize(Game::FxElemVelStateSample, 96); + buffer->align(Utils::Stream::ALIGN_4); + + buffer->saveArray(elemDef->velSamples, elemDef->velIntervalCount + 1); + + Utils::Stream::ClearPointer(&destElemDef->velSamples); + } + + if (elemDef->visSamples) + { + AssertSize(Game::FxElemVisStateSample, 48); + buffer->align(Utils::Stream::ALIGN_4); + + buffer->saveArray(elemDef->visSamples, elemDef->visStateIntervalCount + 1); + + Utils::Stream::ClearPointer(&destElemDef->visSamples); + } + + // Save_FxElemDefVisuals + { + if (elemDef->elemType == 11) + { + if (elemDef->visuals.markArray) + { + AssertSize(Game::FxElemMarkVisuals, 8); + buffer->align(Utils::Stream::ALIGN_4); + + Game::FxElemMarkVisuals* destMarkArray = buffer->dest(); + buffer->saveArray(elemDef->visuals.markArray, elemDef->visualCount); + + for (char j = 0; j < elemDef->visualCount; ++j) + { + if (elemDef->visuals.markArray[j].data[0]) + { + destMarkArray[j].data[0] = builder->saveSubAsset(Game::XAssetType::ASSET_TYPE_MATERIAL, elemDef->visuals.markArray[j].data[0]).material; + } + + if (elemDef->visuals.markArray[j].data[1]) + { + destMarkArray[j].data[1] = builder->saveSubAsset(Game::XAssetType::ASSET_TYPE_MATERIAL, elemDef->visuals.markArray[j].data[1]).material; + } + } + + Utils::Stream::ClearPointer(&destElemDef->visuals.markArray); + } + } + else if(elemDef->visualCount > 1) + { + if (elemDef->visuals.array) + { + AssertSize(Game::FxElemVisuals, 4); + buffer->align(Utils::Stream::ALIGN_4); + + Game::FxElemVisuals* destVisuals = buffer->dest(); + buffer->saveArray(elemDef->visuals.array, elemDef->visualCount); + + for (char j = 0; j < elemDef->visualCount; ++j) + { + this->saveFxElemVisuals(&elemDef->visuals.array[j], &destVisuals[j], elemDef->elemType, builder); + } + + Utils::Stream::ClearPointer(&destElemDef->visuals.array); + } + } + else + { + this->saveFxElemVisuals(&elemDef->visuals.instance, &destElemDef->visuals.instance, elemDef->elemType, builder); + } + } + + if (elemDef->effectOnImpact) + { + buffer->saveString(elemDef->effectOnImpact->name); + Utils::Stream::ClearPointer(&destElemDef->effectOnImpact); + } + + if (elemDef->effectOnDeath) + { + buffer->saveString(elemDef->effectOnDeath->name); + Utils::Stream::ClearPointer(&destElemDef->effectOnDeath); + } + + if (elemDef->effectEmitted) + { + buffer->saveString(elemDef->effectEmitted->name); + Utils::Stream::ClearPointer(&destElemDef->effectEmitted); + } + + // Save_FxElemExtendedDefPtr + { + AssertSize(Game::FxElemExtendedDef, 4); + + if (elemDef->elemType == 3) + { + // Save_FxTrailDef + { + if (elemDef->extendedDef.trailDef) + { + AssertSize(Game::FxTrailDef, 36); + buffer->align(Utils::Stream::ALIGN_4); + + Game::FxTrailDef* trailDef = elemDef->extendedDef.trailDef; + Game::FxTrailDef* destTrailDef = buffer->dest(); + buffer->save(trailDef); + + if (trailDef->verts) + { + AssertSize(Game::FxTrailVertex, 20); + buffer->align(Utils::Stream::ALIGN_4); + + buffer->saveArray(trailDef->verts, trailDef->vertCount); + Utils::Stream::ClearPointer(&destTrailDef->verts); + } + + if (trailDef->inds) + { + buffer->align(Utils::Stream::ALIGN_2); + + buffer->saveArray(trailDef->inds, trailDef->indCount); + Utils::Stream::ClearPointer(&destTrailDef->inds); + } + + Utils::Stream::ClearPointer(&destElemDef->extendedDef.trailDef); + } + } + } + else if (elemDef->elemType == 6) + { + if (elemDef->extendedDef.sparkFountain) + { + AssertSize(Game::FxSparkFountain, 52); + buffer->align(Utils::Stream::ALIGN_4); + + buffer->save(elemDef->extendedDef.sparkFountain); + Utils::Stream::ClearPointer(&destElemDef->extendedDef.sparkFountain); + } + } + else + { + if (elemDef->extendedDef.unknownBytes) + { + buffer->save(elemDef->extendedDef.unknownBytes); + Utils::Stream::ClearPointer(&destElemDef->extendedDef.unknownBytes); + } + } + } + } + + Utils::Stream::ClearPointer(&dest->elemDefs); + } + + buffer->popBlock(); + } +} diff --git a/src/Components/Modules/AssetInterfaces/IGameWorldMp.cpp b/src/Components/Modules/AssetInterfaces/IGameWorldMp.cpp index f436b9eb..f6ff1344 100644 --- a/src/Components/Modules/AssetInterfaces/IGameWorldMp.cpp +++ b/src/Components/Modules/AssetInterfaces/IGameWorldMp.cpp @@ -27,7 +27,7 @@ namespace Assets { AssertSize(Game::G_GlassData, 128); - Game::G_GlassData* destGlass = buffer->dest(); + Game::G_GlassData* destGlass = buffer->dest(); buffer->save(asset->data); if (asset->data->glassPieces) diff --git a/src/Components/Modules/AssetInterfaces/IGameWorldSp.cpp b/src/Components/Modules/AssetInterfaces/IGameWorldSp.cpp index f9866c59..1afb09cd 100644 --- a/src/Components/Modules/AssetInterfaces/IGameWorldSp.cpp +++ b/src/Components/Modules/AssetInterfaces/IGameWorldSp.cpp @@ -54,10 +54,10 @@ namespace Assets buffer->align(Utils::Stream::ALIGN_4); builder->storePointer(*childNodeTreePtr); - Game::pathnode_tree_t* destChildNodeTree = buffer->dest(); - buffer->save(*childNodeTreePtr); - - this->savepathnode_tree_info_t(*childNodeTreePtr, destChildNodeTree, builder); + Game::pathnode_tree_t* destChildNodeTree = buffer->dest(); + buffer->save(*childNodeTreePtr); + + this->savepathnode_tree_info_t(*childNodeTreePtr, destChildNodeTree, builder); Utils::Stream::ClearPointer(destChildNodeTreePtr); } } @@ -88,12 +88,12 @@ namespace Assets { buffer->align(Utils::Stream::ALIGN_4); builder->storePointer(*trackSegmentPtr); - + Game::VehicleTrackSegment* destTrackSegment = buffer->dest(); - buffer->save(*trackSegmentPtr); - - this->saveVehicleTrackSegment(*trackSegmentPtr, destTrackSegment, builder); - + buffer->save(*trackSegmentPtr); + + this->saveVehicleTrackSegment(*trackSegmentPtr, destTrackSegment, builder); + Utils::Stream::ClearPointer(destTrackSegmentPtr); } } @@ -108,15 +108,15 @@ namespace Assets { buffer->saveString(trackSegment->name); Utils::Stream::ClearPointer(&destTrackSegment->name); - } - - if (trackSegment->trackSectors) - { + } + + if (trackSegment->trackSectors) + { AssertSize(Game::VehicleTrackSector, 60); buffer->align(Utils::Stream::ALIGN_4); Game::VehicleTrackSector* destTrackSectors = buffer->dest(); - buffer->saveArray(trackSegment->trackSectors, trackSegment->trackSectorCount); + buffer->saveArray(trackSegment->trackSectors, trackSegment->trackSectorCount); for (int i = 0; i < trackSegment->trackSectorCount; ++i) { @@ -130,21 +130,21 @@ namespace Assets buffer->saveArray(trackSector->trackObstacles, trackSector->trackObstacleCount); Utils::Stream::ClearPointer(&destTrackSector->trackObstacles); } - } + } + } + + if (trackSegment->trackSegments1) + { + buffer->align(Utils::Stream::ALIGN_4); + this->saveVehicleTrackSegment_ptrArray(trackSegment->trackSegments1, trackSegment->trackSegmentCount1, builder); + Utils::Stream::ClearPointer(&destTrackSegment->trackSegments1); } - if (trackSegment->trackSegments1) - { - buffer->align(Utils::Stream::ALIGN_4); - this->saveVehicleTrackSegment_ptrArray(trackSegment->trackSegments1, trackSegment->trackSegmentCount1, builder); - Utils::Stream::ClearPointer(&destTrackSegment->trackSegments1); - } - - if (trackSegment->trackSegments2) - { - buffer->align(Utils::Stream::ALIGN_4); - this->saveVehicleTrackSegment_ptrArray(trackSegment->trackSegments2, trackSegment->trackSegmentCount2, builder); - Utils::Stream::ClearPointer(&destTrackSegment->trackSegments2); + if (trackSegment->trackSegments2) + { + buffer->align(Utils::Stream::ALIGN_4); + this->saveVehicleTrackSegment_ptrArray(trackSegment->trackSegments2, trackSegment->trackSegmentCount2, builder); + Utils::Stream::ClearPointer(&destTrackSegment->trackSegments2); } } @@ -273,18 +273,18 @@ namespace Assets for (int i = 0; i < asset->vehicleTrack.trackSegmentCount; ++i) { - builder->storePointer(&asset->vehicleTrack.trackSegments[i]); - buffer->save(&asset->vehicleTrack.trackSegments[i]); - } - + builder->storePointer(&asset->vehicleTrack.trackSegments[i]); + buffer->save(&asset->vehicleTrack.trackSegments[i]); + } + for (int i = 0; i < asset->vehicleTrack.trackSegmentCount; ++i) { - Game::VehicleTrackSegment* destTrackSegment = &destTrackSegments[i]; - Game::VehicleTrackSegment* trackSegment = &asset->vehicleTrack.trackSegments[i]; - - this->saveVehicleTrackSegment(trackSegment, destTrackSegment, builder); - } + Game::VehicleTrackSegment* destTrackSegment = &destTrackSegments[i]; + Game::VehicleTrackSegment* trackSegment = &asset->vehicleTrack.trackSegments[i]; + this->saveVehicleTrackSegment(trackSegment, destTrackSegment, builder); + } + Utils::Stream::ClearPointer(&dest->vehicleTrack.trackSegments); } } @@ -297,7 +297,7 @@ namespace Assets AssertSize(Game::G_GlassData, 128); buffer->align(Utils::Stream::ALIGN_4); - Game::G_GlassData* destGlass = buffer->dest(); + Game::G_GlassData* destGlass = buffer->dest(); buffer->save(asset->data); if (asset->data->glassPieces) diff --git a/src/Components/Modules/AssetInterfaces/IXModel.cpp b/src/Components/Modules/AssetInterfaces/IXModel.cpp index be80f5c7..2c135049 100644 --- a/src/Components/Modules/AssetInterfaces/IXModel.cpp +++ b/src/Components/Modules/AssetInterfaces/IXModel.cpp @@ -4,71 +4,71 @@ namespace Assets { - void IXModel::loadXSurfaceCollisionTree(Game::XSurfaceCollisionTree* entry, Utils::Stream::Reader* reader) - { - if (entry->nodes) - { - entry->nodes = reader->readArray(entry->nodeCount); - } - - if (entry->leafs) - { - entry->leafs = reader->readArray(entry->leafCount); - } + void IXModel::loadXSurfaceCollisionTree(Game::XSurfaceCollisionTree* entry, Utils::Stream::Reader* reader) + { + if (entry->nodes) + { + entry->nodes = reader->readArray(entry->nodeCount); + } + + if (entry->leafs) + { + entry->leafs = reader->readArray(entry->leafCount); + } } void IXModel::loadXSurface(Game::XSurface* surf, Utils::Stream::Reader* reader) { - if (surf->vertInfo.vertsBlend) - { - surf->vertInfo.vertsBlend = reader->readArray(surf->vertInfo.vertCount[0] + (surf->vertInfo.vertCount[1] * 3) + (surf->vertInfo.vertCount[2] * 5) + (surf->vertInfo.vertCount[3] * 7)); - } - - // Access vertex block - if (surf->verts0) - { - surf->verts0 = reader->readArray(surf->vertCount); - } - - // Save_XRigidVertListArray - if (surf->vertList) - { - surf->vertList = reader->readArray(surf->vertListCount); - - for (unsigned int i = 0; i < surf->vertListCount; ++i) - { - Game::XRigidVertList* rigidVertList = &surf->vertList[i]; - - if (rigidVertList->collisionTree) - { - rigidVertList->collisionTree = reader->readObject(); - this->loadXSurfaceCollisionTree(rigidVertList->collisionTree, reader); - } - } - } - - // Access index block - if (surf->triIndices) - { - surf->triIndices = reader->readArray(surf->triCount * 3); + if (surf->vertInfo.vertsBlend) + { + surf->vertInfo.vertsBlend = reader->readArray(surf->vertInfo.vertCount[0] + (surf->vertInfo.vertCount[1] * 3) + (surf->vertInfo.vertCount[2] * 5) + (surf->vertInfo.vertCount[3] * 7)); + } + + // Access vertex block + if (surf->verts0) + { + surf->verts0 = reader->readArray(surf->vertCount); + } + + // Save_XRigidVertListArray + if (surf->vertList) + { + surf->vertList = reader->readArray(surf->vertListCount); + + for (unsigned int i = 0; i < surf->vertListCount; ++i) + { + Game::XRigidVertList* rigidVertList = &surf->vertList[i]; + + if (rigidVertList->collisionTree) + { + rigidVertList->collisionTree = reader->readObject(); + this->loadXSurfaceCollisionTree(rigidVertList->collisionTree, reader); + } + } + } + + // Access index block + if (surf->triIndices) + { + surf->triIndices = reader->readArray(surf->triCount * 3); } } void IXModel::loadXModelSurfs(Game::XModelSurfs* asset, Utils::Stream::Reader* reader) { - if (asset->name) - { - asset->name = reader->readCString(); - } - - if (asset->surfaces) - { - asset->surfaces = reader->readArray(asset->numSurfaces); - - for (int i = 0; i < asset->numSurfaces; ++i) - { - this->loadXSurface(&asset->surfaces[i], reader); - } + if (asset->name) + { + asset->name = reader->readCString(); + } + + if (asset->surfaces) + { + asset->surfaces = reader->readArray(asset->numSurfaces); + + for (int i = 0; i < asset->numSurfaces; ++i) + { + this->loadXSurface(&asset->surfaces[i], reader); + } } } @@ -192,14 +192,14 @@ namespace Assets { asset->physPreset = reader.readObject(); - if (asset->physPreset->name) - { - asset->physPreset->name = reader.readCString(); - } - - if (asset->physPreset->sndAliasPrefix) - { - asset->physPreset->sndAliasPrefix = reader.readCString(); + if (asset->physPreset->name) + { + asset->physPreset->name = reader.readCString(); + } + + if (asset->physPreset->sndAliasPrefix) + { + asset->physPreset->sndAliasPrefix = reader.readCString(); } // This is an experiment, ak74 fails though diff --git a/src/Components/Modules/AssetInterfaces/IclipMap_t.cpp b/src/Components/Modules/AssetInterfaces/IclipMap_t.cpp index fd5c57ef..fed92f6f 100644 --- a/src/Components/Modules/AssetInterfaces/IclipMap_t.cpp +++ b/src/Components/Modules/AssetInterfaces/IclipMap_t.cpp @@ -601,24 +601,24 @@ namespace Assets clipMap->name = reader.readCString(); - clipMap->numCPlanes = reader.read(); - clipMap->numStaticModels = reader.read(); - clipMap->numMaterials = reader.read(); - clipMap->numCBrushSides = reader.read(); - clipMap->numCBrushEdges = reader.read(); - clipMap->numCNodes = reader.read(); - clipMap->numCLeaf = reader.read(); - clipMap->numCLeafBrushNodes = reader.read(); - clipMap->numLeafBrushes = reader.read(); - clipMap->numLeafSurfaces = reader.read(); - clipMap->numVerts = reader.read(); - clipMap->numTriIndices = reader.read(); - clipMap->numCollisionBorders = reader.read(); - clipMap->numCollisionPartitions = reader.read(); - clipMap->numCollisionAABBTrees = reader.read(); - clipMap->numCModels = reader.read(); - clipMap->numCBrushes = reader.read(); - clipMap->dynEntCount[0] = reader.read(); + clipMap->numCPlanes = reader.read(); + clipMap->numStaticModels = reader.read(); + clipMap->numMaterials = reader.read(); + clipMap->numCBrushSides = reader.read(); + clipMap->numCBrushEdges = reader.read(); + clipMap->numCNodes = reader.read(); + clipMap->numCLeaf = reader.read(); + clipMap->numCLeafBrushNodes = reader.read(); + clipMap->numLeafBrushes = reader.read(); + clipMap->numLeafSurfaces = reader.read(); + clipMap->numVerts = reader.read(); + clipMap->numTriIndices = reader.read(); + clipMap->numCollisionBorders = reader.read(); + clipMap->numCollisionPartitions = reader.read(); + clipMap->numCollisionAABBTrees = reader.read(); + clipMap->numCModels = reader.read(); + clipMap->numCBrushes = reader.read(); + clipMap->dynEntCount[0] = reader.read(); clipMap->dynEntCount[1] = reader.read(); if (clipMap->numCPlanes) diff --git a/src/Components/Modules/AssetInterfaces/Isnd_alias_list_t.cpp b/src/Components/Modules/AssetInterfaces/Isnd_alias_list_t.cpp index 47e50306..6c9db6f5 100644 --- a/src/Components/Modules/AssetInterfaces/Isnd_alias_list_t.cpp +++ b/src/Components/Modules/AssetInterfaces/Isnd_alias_list_t.cpp @@ -2,12 +2,12 @@ namespace Assets { - void Isnd_alias_list_t::mark(Game::XAssetHeader header, Components::ZoneBuilder::Zone* builder) - { - Game::snd_alias_list_t* asset = header.sound; + void Isnd_alias_list_t::mark(Game::XAssetHeader header, Components::ZoneBuilder::Zone* builder) + { + Game::snd_alias_list_t* asset = header.sound; for (int i = 0; i < asset->count; ++i) - { + { Game::snd_alias_t* alias = &asset->head[i]; if (alias->soundFile && alias->soundFile->type == Game::snd_alias_type_t::SAT_LOADED) @@ -52,12 +52,12 @@ namespace Assets buffer->align(Utils::Stream::ALIGN_4); builder->storePointer(asset->head); - Game::snd_alias_t* destHead = buffer->dest(); - buffer->saveArray(asset->head, asset->count); + Game::snd_alias_t* destHead = buffer->dest(); + buffer->saveArray(asset->head, asset->count); for (int i = 0; i < asset->count; ++i) { - Game::snd_alias_t* destAlias = &destHead[i]; + Game::snd_alias_t* destAlias = &destHead[i]; Game::snd_alias_t* alias = &asset->head[i]; if (alias->name) diff --git a/src/Components/Modules/Dedicated.cpp b/src/Components/Modules/Dedicated.cpp index 2e28bd99..5ab2f06d 100644 --- a/src/Components/Modules/Dedicated.cpp +++ b/src/Components/Modules/Dedicated.cpp @@ -37,7 +37,7 @@ namespace Components if (Dvar::Var("com_logFilter").get()) { - Utils::Hook::Nop(0x647466, 5); // 'dvar set' lines + Utils::Hook::Nop(0x647466, 5); // 'dvar set' lines Utils::Hook::Nop(0x5DF4F2, 5); // 'sending splash open' lines } diff --git a/src/Components/Modules/FastFiles.cpp b/src/Components/Modules/FastFiles.cpp index a51e637f..d331539d 100644 --- a/src/Components/Modules/FastFiles.cpp +++ b/src/Components/Modules/FastFiles.cpp @@ -413,18 +413,18 @@ namespace Components Utils::Hook::Call(0x5BBAC0)(zoneInfo, zoneCount); } -#ifdef DEBUG - void FastFiles::LogStreamRead(int len) - { - *Game::g_streamPos += len; - - if (FastFiles::StreamRead) - { - std::string data = Utils::String::VA("%d\n", len); - if (*Game::g_streamPosIndex == 2) data = Utils::String::VA("(%d)\n", len); - Utils::IO::WriteFile("userraw/logs/iw4_reads.log", data, true); - } - } +#ifdef DEBUG + void FastFiles::LogStreamRead(int len) + { + *Game::g_streamPos += len; + + if (FastFiles::StreamRead) + { + std::string data = Utils::String::VA("%d\n", len); + if (*Game::g_streamPosIndex == 2) data = Utils::String::VA("(%d)\n", len); + Utils::IO::WriteFile("userraw/logs/iw4_reads.log", data, true); + } + } #endif FastFiles::FastFiles() @@ -518,15 +518,15 @@ namespace Components Game::DB_LoadXAssets(&info, 1, true); }); -#ifdef DEBUG - // ZoneBuilder debugging - Utils::IO::WriteFile("userraw/logs/iw4_reads.log", "", false); +#ifdef DEBUG + // ZoneBuilder debugging + Utils::IO::WriteFile("userraw/logs/iw4_reads.log", "", false); Utils::Hook(0x4A8FA0, FastFiles::LogStreamRead, HOOK_JUMP).install()->quick(); - Utils::Hook(0x4BCB62, []() - { - FastFiles::StreamRead = true; - Utils::Hook::Call(0x4B8DB0)(true); // currently set to Load_GfxWorld - FastFiles::StreamRead = false; + Utils::Hook(0x4BCB62, []() + { + FastFiles::StreamRead = true; + Utils::Hook::Call(0x4B8DB0)(true); // currently set to Load_GfxWorld + FastFiles::StreamRead = false; }, HOOK_CALL).install()->quick(); #endif } diff --git a/src/Components/Modules/ModelSurfs.cpp b/src/Components/Modules/ModelSurfs.cpp index 5a3f47bf..fe8b9f4f 100644 --- a/src/Components/Modules/ModelSurfs.cpp +++ b/src/Components/Modules/ModelSurfs.cpp @@ -189,23 +189,26 @@ namespace Components { hasCustomSurface = true; - auto buffer = ModelSurfs::BufferMap.find(surface->triIndices); - if (buffer != ModelSurfs::BufferMap.end()) + if (!ModelSurfs::BufferMap.empty()) { - buffer->second->Release(); - ModelSurfs::BufferMap.erase(buffer); - } + auto buffer = ModelSurfs::BufferMap.find(surface->triIndices); + if (buffer != ModelSurfs::BufferMap.end()) + { + buffer->second->Release(); + ModelSurfs::BufferMap.erase(buffer); + } - buffer = ModelSurfs::BufferMap.find(surface->verts0); - if (buffer != ModelSurfs::BufferMap.end()) - { - buffer->second->Release(); - ModelSurfs::BufferMap.erase(buffer); + buffer = ModelSurfs::BufferMap.find(surface->verts0); + if (buffer != ModelSurfs::BufferMap.end()) + { + buffer->second->Release(); + ModelSurfs::BufferMap.erase(buffer); + } } } } - if (hasCustomSurface) + if (hasCustomSurface && !ModelSurfs::AllocMap.empty()) { auto allocData = ModelSurfs::AllocMap.find(header.surfaces->name); if (allocData != ModelSurfs::AllocMap.end()) diff --git a/src/Utils/Stream.cpp b/src/Utils/Stream.cpp index 67d1fbe7..0c2a0435 100644 --- a/src/Utils/Stream.cpp +++ b/src/Utils/Stream.cpp @@ -59,16 +59,16 @@ namespace Utils } } - void* Stream::Reader::readPointer() - { - void* pointer = this->read(); - if (!this->hasPointer(pointer)) - { - this->pointerMap[pointer] = 0; - } - return pointer; - } - + void* Stream::Reader::readPointer() + { + void* pointer = this->read(); + if (!this->hasPointer(pointer)) + { + this->pointerMap[pointer] = 0; + } + return pointer; + } + void Stream::Reader::mapPointer(void* oldPointer, void* newPointer) { if (this->hasPointer(oldPointer)) @@ -77,9 +77,9 @@ namespace Utils } } - bool Stream::Reader::hasPointer(void* pointer) - { - return this->pointerMap.find(pointer) != this->pointerMap.end(); + bool Stream::Reader::hasPointer(void* pointer) + { + return this->pointerMap.find(pointer) != this->pointerMap.end(); } Stream::Stream() : criticalSectionState(0) diff --git a/src/Utils/Stream.hpp b/src/Utils/Stream.hpp index c33574d1..5a508c52 100644 --- a/src/Utils/Stream.hpp +++ b/src/Utils/Stream.hpp @@ -6,186 +6,186 @@ #else #define SaveLogEnter(x) builder->getBuffer()->enterStruct(x) #define SaveLogExit() builder->getBuffer()->leaveStruct() -#endif - -namespace Utils -{ - class Stream - { - private: - int criticalSectionState; - unsigned int blockSize[Game::MAX_XFILE_COUNT]; - std::vector streamStack; - std::string buffer; - - public: - class Reader - { - public: - Reader(Utils::Memory::Allocator* _allocator, std::string _buffer) : buffer(_buffer), allocator(_allocator), position(0) {} - - std::string readString(); - const char* readCString(); - - char readByte(); - - void* read(size_t size, size_t count = 1); - template inline T* readObject() - { - return readArray(1); - } - template inline T* readArray(size_t count = 1) - { - return reinterpret_cast(this->read(sizeof(T), count)); - } - template T read() - { - T obj; - - for (unsigned int i = 0; i < sizeof(T); ++i) - { - reinterpret_cast(&obj)[i] = this->readByte(); - } - - return obj; - } - - bool end(); - void seek(unsigned int position); - - void* readPointer(); - void mapPointer(void* oldPointer, void* newPointer); - bool hasPointer(void* pointer); - - private: - unsigned int position; - std::string buffer; - std::map pointerMap; - Utils::Memory::Allocator* allocator; - }; - - enum Alignment - { - ALIGN_2, - ALIGN_4, - ALIGN_8, - ALIGN_16, - ALIGN_32, - ALIGN_64, - ALIGN_128, - ALIGN_256, - ALIGN_512, - ALIGN_1024, - ALIGN_2048, - }; - - Stream(); - Stream(size_t size); - ~Stream(); - - size_t length(); - size_t capacity(); - - char* save(const void * _str, size_t size, size_t count = 1); - char* save(Game::XFILE_BLOCK_TYPES stream, const void * _str, size_t size, size_t count); - char* save(Game::XFILE_BLOCK_TYPES stream, int value, size_t count); - template inline char* save(T* object) - { - return saveArray(object, 1); - } - template inline char* saveArray(T* array, size_t count) - { - return save(array, sizeof(T), count); - } - - char* saveString(std::string string); - char* saveString(const char* string); - char* saveString(const char* string, size_t len); - char* saveByte(unsigned char byte, size_t count = 1); - char* saveNull(size_t count = 1); - char* saveMax(size_t count = 1); - - char* saveText(std::string string); - - void align(Alignment align); - bool pushBlock(Game::XFILE_BLOCK_TYPES stream); - bool popBlock(); - bool isValidBlock(Game::XFILE_BLOCK_TYPES stream); - void increaseBlockSize(Game::XFILE_BLOCK_TYPES stream, unsigned int size); - void increaseBlockSize(unsigned int size); - Game::XFILE_BLOCK_TYPES getCurrentBlock(); - unsigned int getBlockSize(Game::XFILE_BLOCK_TYPES stream); - - DWORD getPackedOffset(); - - char* data(); - char* at(); - template T* dest() - { - return reinterpret_cast(this->at()); - } - template static inline void ClearPointer(T** object) - { - *object = reinterpret_cast(-1); - } - - void toBuffer(std::string& outBuffer); - std::string toBuffer(); - - // Enter/Leave critical sections in which reallocations are not allowed. - // If buffer reallocation is detected, the operation has to be terminated - // and more memory has to be allocated next time. This will have to be done - // by editing the code though. - void enterCriticalSection(); - void leaveCriticalSection(); - bool isCriticalSection(); - - // for recording zb writes -#ifdef WRITE_LOGS - int structLevel; - void enterStruct(const char* structName); - void leaveStruct(); -#endif - - // This represents packed offset in streams: - // - lowest 28 bits store the value/offset - // - highest 4 bits store the stream block - class Offset - { - public: - union - { - struct - { - uint32_t offset : 28; - Game::XFILE_BLOCK_TYPES block : 4; - }; - uint32_t packed; - void* pointer; - }; - - Offset() : packed(0) {}; - Offset(Game::XFILE_BLOCK_TYPES _block, uint32_t _offset) : offset(_offset), block(_block) {}; - - // The game needs it to be incremented - uint32_t getPackedOffset() - { - return this->packed + 1; - }; - - uint32_t getUnpackedOffset() - { - Offset lOffset = *this; - lOffset.packed--; - return lOffset.offset; - }; - - int getUnpackedBlock() - { - Offset lOffset = *this; - lOffset.packed--; - return lOffset.block; - }; - }; - }; -} +#endif + +namespace Utils +{ + class Stream + { + private: + int criticalSectionState; + unsigned int blockSize[Game::MAX_XFILE_COUNT]; + std::vector streamStack; + std::string buffer; + + public: + class Reader + { + public: + Reader(Utils::Memory::Allocator* _allocator, std::string _buffer) : buffer(_buffer), allocator(_allocator), position(0) {} + + std::string readString(); + const char* readCString(); + + char readByte(); + + void* read(size_t size, size_t count = 1); + template inline T* readObject() + { + return readArray(1); + } + template inline T* readArray(size_t count = 1) + { + return reinterpret_cast(this->read(sizeof(T), count)); + } + template T read() + { + T obj; + + for (unsigned int i = 0; i < sizeof(T); ++i) + { + reinterpret_cast(&obj)[i] = this->readByte(); + } + + return obj; + } + + bool end(); + void seek(unsigned int position); + + void* readPointer(); + void mapPointer(void* oldPointer, void* newPointer); + bool hasPointer(void* pointer); + + private: + unsigned int position; + std::string buffer; + std::map pointerMap; + Utils::Memory::Allocator* allocator; + }; + + enum Alignment + { + ALIGN_2, + ALIGN_4, + ALIGN_8, + ALIGN_16, + ALIGN_32, + ALIGN_64, + ALIGN_128, + ALIGN_256, + ALIGN_512, + ALIGN_1024, + ALIGN_2048, + }; + + Stream(); + Stream(size_t size); + ~Stream(); + + size_t length(); + size_t capacity(); + + char* save(const void * _str, size_t size, size_t count = 1); + char* save(Game::XFILE_BLOCK_TYPES stream, const void * _str, size_t size, size_t count); + char* save(Game::XFILE_BLOCK_TYPES stream, int value, size_t count); + template inline char* save(T* object) + { + return saveArray(object, 1); + } + template inline char* saveArray(T* array, size_t count) + { + return save(array, sizeof(T), count); + } + + char* saveString(std::string string); + char* saveString(const char* string); + char* saveString(const char* string, size_t len); + char* saveByte(unsigned char byte, size_t count = 1); + char* saveNull(size_t count = 1); + char* saveMax(size_t count = 1); + + char* saveText(std::string string); + + void align(Alignment align); + bool pushBlock(Game::XFILE_BLOCK_TYPES stream); + bool popBlock(); + bool isValidBlock(Game::XFILE_BLOCK_TYPES stream); + void increaseBlockSize(Game::XFILE_BLOCK_TYPES stream, unsigned int size); + void increaseBlockSize(unsigned int size); + Game::XFILE_BLOCK_TYPES getCurrentBlock(); + unsigned int getBlockSize(Game::XFILE_BLOCK_TYPES stream); + + DWORD getPackedOffset(); + + char* data(); + char* at(); + template T* dest() + { + return reinterpret_cast(this->at()); + } + template static inline void ClearPointer(T** object) + { + *object = reinterpret_cast(-1); + } + + void toBuffer(std::string& outBuffer); + std::string toBuffer(); + + // Enter/Leave critical sections in which reallocations are not allowed. + // If buffer reallocation is detected, the operation has to be terminated + // and more memory has to be allocated next time. This will have to be done + // by editing the code though. + void enterCriticalSection(); + void leaveCriticalSection(); + bool isCriticalSection(); + + // for recording zb writes +#ifdef WRITE_LOGS + int structLevel; + void enterStruct(const char* structName); + void leaveStruct(); +#endif + + // This represents packed offset in streams: + // - lowest 28 bits store the value/offset + // - highest 4 bits store the stream block + class Offset + { + public: + union + { + struct + { + uint32_t offset : 28; + Game::XFILE_BLOCK_TYPES block : 4; + }; + uint32_t packed; + void* pointer; + }; + + Offset() : packed(0) {}; + Offset(Game::XFILE_BLOCK_TYPES _block, uint32_t _offset) : offset(_offset), block(_block) {}; + + // The game needs it to be incremented + uint32_t getPackedOffset() + { + return this->packed + 1; + }; + + uint32_t getUnpackedOffset() + { + Offset lOffset = *this; + lOffset.packed--; + return lOffset.offset; + }; + + int getUnpackedBlock() + { + Offset lOffset = *this; + lOffset.packed--; + return lOffset.block; + }; + }; + }; +} diff --git a/src/Utils/String.cpp b/src/Utils/String.cpp index 3b432eae..a6269586 100644 --- a/src/Utils/String.cpp +++ b/src/Utils/String.cpp @@ -3,25 +3,25 @@ #include "base128.h" #endif -#define VA_BUFFER_COUNT 32 +#define VA_BUFFER_COUNT 32 #define VA_BUFFER_SIZE 65536 namespace Utils { namespace String { - const char *VA(const char *fmt, ...) - { - static char g_vaBuffer[VA_BUFFER_COUNT][VA_BUFFER_SIZE]; - static int g_vaNextBufferIndex = 0; - - va_list ap; - va_start(ap, fmt); - char* dest = g_vaBuffer[g_vaNextBufferIndex]; - vsnprintf_s(g_vaBuffer[g_vaNextBufferIndex], VA_BUFFER_SIZE, fmt, ap); - g_vaNextBufferIndex = (g_vaNextBufferIndex + 1) % VA_BUFFER_COUNT; - va_end(ap); - return dest; + const char *VA(const char *fmt, ...) + { + static char g_vaBuffer[VA_BUFFER_COUNT][VA_BUFFER_SIZE]; + static int g_vaNextBufferIndex = 0; + + va_list ap; + va_start(ap, fmt); + char* dest = g_vaBuffer[g_vaNextBufferIndex]; + vsnprintf_s(g_vaBuffer[g_vaNextBufferIndex], VA_BUFFER_SIZE, fmt, ap); + g_vaNextBufferIndex = (g_vaNextBufferIndex + 1) % VA_BUFFER_COUNT; + va_end(ap); + return dest; } std::string ToLower(std::string input)