From 727beadae1e5e114a026dbd153c7510258a2fec5 Mon Sep 17 00:00:00 2001 From: momo5502 Date: Thu, 3 Nov 2016 20:19:43 +0100 Subject: [PATCH] AntiCheat refactoring part 1 --- src/Components/Modules/Zones.cpp | 858 ++++++++++++++++--------------- src/Components/Modules/Zones.hpp | 30 +- 2 files changed, 441 insertions(+), 447 deletions(-) diff --git a/src/Components/Modules/Zones.cpp b/src/Components/Modules/Zones.cpp index 45f7d16d..a71c7b22 100644 --- a/src/Components/Modules/Zones.cpp +++ b/src/Components/Modules/Zones.cpp @@ -7,64 +7,51 @@ namespace Components int Zones::FxEffectIndex; char* Zones::FxEffectStrings[64]; - Utils::Hook Zones::LoadFxElemDefHook; - Utils::Hook Zones::LoadFxElemDefArrayHook; - Utils::Hook Zones::LoadXModelLodInfoHook; - Utils::Hook Zones::LoadXModelHook; - Utils::Hook Zones::LoadXSurfaceArrayHook; - Utils::Hook Zones::LoadGameWorldSpHook; - Utils::Hook Zones::LoadPathDataHook; - Utils::Hook Zones::LoadVehicleDefHook; - Utils::Hook Zones::Loadsnd_alias_tArrayHook; - Utils::Hook Zones::LoadLoadedSoundHook; - Utils::Hook Zones::LoadmenuDef_tHook; - Utils::Hook Zones::LoadFxEffectDefHook; - Utils::Hook Zones::LoadMaterialShaderArgumentArrayHook; - Utils::Hook Zones::LoadStructuredDataStructPropertyArrayHook; - Utils::Hook Zones::LoadPathDataTailHook; - Utils::Hook Zones::LoadWeaponAttachHook; - Utils::Hook Zones::LoadWeaponCompleteDefHook; - Utils::Hook Zones::LoadGfxImageHook; - Utils::Hook Zones::LoadXAssetHook; - Utils::Hook Zones::LoadMaterialTechniqueHook; - Utils::Hook Zones::LoadMaterialHook; - Utils::Hook Zones::LoadGfxWorldHook; - Utils::Hook Zones::Loadsunflare_tHook; - bool Zones::LoadFxEffectDef(bool atStreamStart, char* buffer, int size) { - size /= 252; - int count = size; - size *= 260; + int count = 0; + + if (Zones::Version() >= VERSION_ALPHA2) + { + size /= 252; + count = size; + size *= 260; + } bool result = Game::Load_Stream(atStreamStart, buffer, size); Zones::FxEffectIndex = 0; - Utils::Memory::Allocator allocator; - Game::FxElemDef* elems = allocator.AllocateArray(count); - - for (int i = 0; i < count; ++i) + if (Zones::Version() >= VERSION_ALPHA2) { - AssetHandler::Relocate(buffer + (260 * i), buffer + (252 * i), 252); - std::memcpy(&elems[i], buffer + (260 * i), 252); - Zones::FxEffectStrings[i] = *reinterpret_cast(buffer + (260 * i) + 256); - } + Utils::Memory::Allocator allocator; + Game::FxElemDef* elems = allocator.AllocateArray(count); - std::memcpy(buffer, elems, sizeof(Game::FxElemDef) * count); + for (int i = 0; i < count; ++i) + { + AssetHandler::Relocate(buffer + (260 * i), buffer + (252 * i), 252); + std::memcpy(&elems[i], buffer + (260 * i), 252); + Zones::FxEffectStrings[i] = *reinterpret_cast(buffer + (260 * i) + 256); + } + + std::memcpy(buffer, elems, sizeof(Game::FxElemDef) * count); + } return result; } bool Zones::LoadFxElemDefStub(bool atStreamStart, Game::FxElemDef* fxElem, int size) { - if (fxElem->elemType == 3) + if (Zones::Version() >= VERSION_ALPHA2) { - fxElem->elemType = 2; - } - else if (fxElem->elemType >= 5) - { - fxElem->elemType -= 2; + if (fxElem->elemType == 3) + { + fxElem->elemType = 2; + } + else if (fxElem->elemType >= 5) + { + fxElem->elemType -= 2; + } } return Game::Load_Stream(atStreamStart, fxElem, size); @@ -73,44 +60,64 @@ namespace Components void Zones::LoadFxElemDefArrayStub(bool atStreamStart) { Game::Load_FxElemDef(atStreamStart); - *Game::varXString = &Zones::FxEffectStrings[Zones::FxEffectIndex++]; - Game::Load_XString(false); + + if (Zones::Version() >= VERSION_ALPHA2) + { + *Game::varXString = &Zones::FxEffectStrings[Zones::FxEffectIndex++]; + Game::Load_XString(false); + } } bool Zones::LoadXModel(bool atStreamStart, char* xmodel, int size) { - bool result = Game::Load_Stream(atStreamStart, xmodel, size); - - int elSize = (Zones::ZoneVersion == VERSION_ALPHA2) ? 364 : 360; - - Game::XModel model[2]; // Allocate 2 models, as we exceed the buffer - - std::memcpy(model, xmodel, 36); - std::memcpy(&model->pad3[0x1C], &xmodel[44], 28); - - for (int i = 0; i < 4; ++i) + if (Zones::Version() >= VERSION_ALPHA2) { - std::memcpy(&model->lods[i], &xmodel[72 + (i * 56)], 12); - std::memcpy(&model->lods[i].pad3, &xmodel[72 + (i * 56) + 16], 32); - - std::memcpy(reinterpret_cast(&model) + (elSize - 4) - (i * 4), &xmodel[72 + (i * 56) + 12], 4); + if (Zones::Version() == VERSION_ALPHA2) + { + size = 0x16C; + } + else + { + size = 0x168; + } } - std::memcpy(&model->lods[3].pad4[0], &xmodel[292], (elSize - 292 - 4)/*68*/); - std::memcpy(&model->physPreset, &xmodel[(elSize - 8)], 8); + bool result = Game::Load_Stream(atStreamStart, xmodel, size); - model[1].name = reinterpret_cast(0xDEADC0DE); + if (Zones::Version() >= VERSION_ALPHA2) + { + Game::XModel model[2]; // Allocate 2 models, as we exceed the buffer - std::memcpy(xmodel, &model, elSize); + std::memcpy(model, xmodel, 36); + std::memcpy(&model->pad3[0x1C], &xmodel[44], 28); + + for (int i = 0; i < 4; ++i) + { + std::memcpy(&model->lods[i], &xmodel[72 + (i * 56)], 12); + std::memcpy(&model->lods[i].pad3, &xmodel[72 + (i * 56) + 16], 32); + + std::memcpy(reinterpret_cast(&model) + (size - 4) - (i * 4), &xmodel[72 + (i * 56) + 12], 4); + } + + std::memcpy(&model->lods[3].pad4[0], &xmodel[292], (size - 292 - 4)/*68*/); + std::memcpy(&model->physPreset, &xmodel[(size - 8)], 8); + + model[1].name = reinterpret_cast(0xDEADC0DE); + + std::memcpy(xmodel, &model, size); + } return result; } void Zones::LoadXModelLodInfo(int i) { - int elSize = (Zones::ZoneVersion == VERSION_ALPHA2) ? 364 : 360; - *Game::varXString = reinterpret_cast(reinterpret_cast(*Game::varXModel) + (elSize - 4) - (4 * (4 - i))); - Game::Load_XString(false); + if (Zones::Version() >= VERSION_ALPHA2) + { + int elSize = (Zones::ZoneVersion == VERSION_ALPHA2) ? 364 : 360; + *Game::varXString = reinterpret_cast(reinterpret_cast(*Game::varXModel) + (elSize - 4) - (4 * (4 - i))); + Game::Load_XString(false); + } } __declspec(naked) void Zones::LoadXModelLodInfoStub() @@ -134,62 +141,75 @@ namespace Components bool Zones::LoadXSurfaceArray(bool atStreamStart, char* buffer, int size) { - size >>= 6; + int count = 0; + + if (Zones::Version() >= VERSION_ALPHA2) + { + size >>= 6; - int count = size; - size *= 84; + count = size; + size *= 84; + } bool result = Game::Load_Stream(atStreamStart, buffer, size); - Utils::Memory::Allocator allocator; - Game::XSurface* tempSurfaces = allocator.AllocateArray(count); + if (Zones::Version() >= VERSION_ALPHA2) + { + Utils::Memory::Allocator allocator; + Game::XSurface* tempSurfaces = allocator.AllocateArray(count); - for (int i = 0; i < count; ++i) - { - char* source = &buffer[i * 84]; + for (int i = 0; i < count; ++i) + { + char* source = &buffer[i * 84]; - std::memcpy(&tempSurfaces[i], source, 12); - std::memcpy(&tempSurfaces[i].indexBuffer, source + 16, 20); - std::memcpy(&tempSurfaces[i].numCT, source + 40, 8); - std::memcpy(&tempSurfaces[i].something, source + 52, 24); + std::memcpy(&tempSurfaces[i], source, 12); + std::memcpy(&tempSurfaces[i].indexBuffer, source + 16, 20); + std::memcpy(&tempSurfaces[i].numCT, source + 40, 8); + std::memcpy(&tempSurfaces[i].something, source + 52, 24); - if (Zones::ZoneVersion >= 332) - { - struct + if (Zones::ZoneVersion >= 332) { - short pad; // +0 - char flag; // +2 - unsigned char streamHandle; // +3 - unsigned short numVertices; // +4 - unsigned short numPrimitives; // +6 - // [...] - } surface332; + struct + { + short pad; // +0 + char flag; // +2 + unsigned char streamHandle; // +3 + unsigned short numVertices; // +4 + unsigned short numPrimitives; // +6 + // [...] + } surface332; - // Copy the data to our new structure - std::memcpy(&surface332, &tempSurfaces[i], sizeof(surface332)); + // Copy the data to our new structure + std::memcpy(&surface332, &tempSurfaces[i], sizeof(surface332)); - // Check if that special flag is set - if (!(surface332.flag & 0x20)) - { - Logger::Error("We're not able to handle XSurface buffer allocation yet!"); - } + // Check if that special flag is set + if (!(surface332.flag & 0x20)) + { + Logger::Error("We're not able to handle XSurface buffer allocation yet!"); + } - // Copy the correct data back to our surface - tempSurfaces[i].streamHandle = surface332.streamHandle; - tempSurfaces[i].numVertices = surface332.numVertices; - tempSurfaces[i].numPrimitives = surface332.numPrimitives; + // Copy the correct data back to our surface + tempSurfaces[i].streamHandle = surface332.streamHandle; + tempSurfaces[i].numVertices = surface332.numVertices; + tempSurfaces[i].numPrimitives = surface332.numPrimitives; - //std::memmove(&tempSurfaces[i].numVertices, &tempSurfaces[i].numPrimitives, 6); - } + //std::memmove(&tempSurfaces[i].numVertices, &tempSurfaces[i].numPrimitives, 6); + } + } + + std::memcpy(buffer, tempSurfaces, sizeof(Game::XSurface) * count); } - std::memcpy(buffer, tempSurfaces, sizeof(Game::XSurface) * count); - return result; } void Zones::LoadWeaponCompleteDef() { + if (Zones::ZoneVersion < VERSION_ALPHA2) + { + return Utils::Hook::Call(0x4AE7B0)(true); + } + // setup structures we use char* varWeaponCompleteDef = *reinterpret_cast(0x112A9F4); @@ -847,16 +867,24 @@ namespace Components // Code-analysis has a bug, the first memcpy makes it believe size of tempVar is 44 instead of 84 #pragma warning(push) #pragma warning(disable: 6385) - bool Zones::LoadGameWorldSp(bool atStreamStart, char* buffer) + bool Zones::LoadGameWorldSp(bool atStreamStart, char* buffer, int size) { - bool result = Game::Load_Stream(atStreamStart, buffer, 84); + if (Zones::Version() >= VERSION_ALPHA2) + { + size = 84; + } - char tempVar[84] = { 0 }; - std::memcpy(&tempVar[0], &buffer[0], 44); - std::memcpy(&tempVar[56], &buffer[44], 28); - std::memcpy(&tempVar[44], &buffer[72], 12); + bool result = Game::Load_Stream(atStreamStart, buffer, size); - std::memcpy(buffer, tempVar, sizeof(tempVar)); + if (Zones::Version() >= VERSION_ALPHA2) + { + char tempVar[84] = { 0 }; + std::memcpy(&tempVar[0], &buffer[0], 44); + std::memcpy(&tempVar[56], &buffer[44], 28); + std::memcpy(&tempVar[44], &buffer[72], 12); + + std::memcpy(buffer, tempVar, sizeof(tempVar)); + } return result; } @@ -864,62 +892,81 @@ namespace Components void Zones::LoadPathDataTail() { - char* varPathData = reinterpret_cast(*Game::varPathData); - - if (*reinterpret_cast(varPathData + 56)) + if (Zones::ZoneVersion >= VERSION_ALPHA2) { - *reinterpret_cast(varPathData + 56) = Game::DB_AllocStreamPos(0); - Game::Load_Stream(true, *reinterpret_cast(varPathData + 56), *reinterpret_cast(varPathData + 52)); - } + char* varPathData = reinterpret_cast(*Game::varPathData); - if (*reinterpret_cast(varPathData + 64)) - { - *reinterpret_cast(varPathData + 64) = Game::DB_AllocStreamPos(0); - Game::Load_Stream(true, *reinterpret_cast(varPathData + 64), *reinterpret_cast(varPathData + 60)); - } + if (*reinterpret_cast(varPathData + 56)) + { + *reinterpret_cast(varPathData + 56) = Game::DB_AllocStreamPos(0); + Game::Load_Stream(true, *reinterpret_cast(varPathData + 56), *reinterpret_cast(varPathData + 52)); + } - if (*reinterpret_cast(varPathData + 76)) - { - *reinterpret_cast(varPathData + 76) = Game::DB_AllocStreamPos(0); - Game::Load_Stream(true, *reinterpret_cast(varPathData + 76), *reinterpret_cast(varPathData + 72)); + if (*reinterpret_cast(varPathData + 64)) + { + *reinterpret_cast(varPathData + 64) = Game::DB_AllocStreamPos(0); + Game::Load_Stream(true, *reinterpret_cast(varPathData + 64), *reinterpret_cast(varPathData + 60)); + } + + if (*reinterpret_cast(varPathData + 76)) + { + *reinterpret_cast(varPathData + 76) = Game::DB_AllocStreamPos(0); + Game::Load_Stream(true, *reinterpret_cast(varPathData + 76), *reinterpret_cast(varPathData + 72)); + } } } bool Zones::Loadsnd_alias_tArray(bool atStreamStart, char* buffer, int len) { - len /= 100; - int count = len; - len *= 108; + int count = 0; + + if (Zones::Version() >= VERSION_ALPHA2) + { + len /= 100; + count = len; + len *= 108; + } bool result = Game::Load_Stream(atStreamStart, buffer, len); - Utils::Memory::Allocator allocator; - Game::snd_alias_t* tempSounds = allocator.AllocateArray(count); + if (Zones::Version() >= VERSION_ALPHA2) + { + Utils::Memory::Allocator allocator; + Game::snd_alias_t* tempSounds = allocator.AllocateArray(count); - for (int i = 0; i < count; ++i) - { - char* src = &buffer[i * 108]; + for (int i = 0; i < count; ++i) + { + char* src = &buffer[i * 108]; - std::memcpy(&tempSounds[i], src + 0, 60); - std::memcpy(&tempSounds[i].pad2[36], src + 68, 20); - std::memcpy(&tempSounds[i].pad2[56], src + 88, 20); + std::memcpy(&tempSounds[i], src + 0, 60); + std::memcpy(&tempSounds[i].pad2[36], src + 68, 20); + std::memcpy(&tempSounds[i].pad2[56], src + 88, 20); - AssetHandler::Relocate(src + 0, buffer + (i * 100) + 0, 60); - AssetHandler::Relocate(src + 68, buffer + (i * 100) + 60, 20); - AssetHandler::Relocate(src + 88, buffer + (i * 100) + 80, 20); - } + AssetHandler::Relocate(src + 0, buffer + (i * 100) + 0, 60); + AssetHandler::Relocate(src + 68, buffer + (i * 100) + 60, 20); + AssetHandler::Relocate(src + 88, buffer + (i * 100) + 80, 20); + } - std::memcpy(buffer, tempSounds, sizeof(Game::snd_alias_t) * count); + std::memcpy(buffer, tempSounds, sizeof(Game::snd_alias_t) * count); + } return result; } - bool Zones::LoadLoadedSound(bool atStreamStart, char* buffer) + bool Zones::LoadLoadedSound(bool atStreamStart, char* buffer, int size) { - bool result = Game::Load_Stream(atStreamStart, buffer, 48); + if (Zones::Version() >= VERSION_ALPHA2) + { + size = 48; + } - std::memmove(buffer + 28, buffer + 32, 16); - AssetHandler::Relocate(buffer + 32, buffer + 28, 16); + bool result = Game::Load_Stream(atStreamStart, buffer, size); + + if (Zones::Version() >= VERSION_ALPHA2) + { + std::memmove(buffer + 28, buffer + 32, 16); + AssetHandler::Relocate(buffer + 32, buffer + 28, 16); + } return result; } @@ -927,17 +974,25 @@ namespace Components // Code-analysis has a bug, the first memcpy makes it believe size of tempVar is 400 instead of 788 #pragma warning(push) #pragma warning(disable: 6385) - bool Zones::LoadVehicleDef(bool atStreamStart, char* buffer) + bool Zones::LoadVehicleDef(bool atStreamStart, char* buffer, int size) { - bool result = Game::Load_Stream(atStreamStart, buffer, 788); + if (Zones::Version() >= VERSION_ALPHA2) + { + size = 788; + } - char tempVar[788] = { 0 }; - std::memcpy(&tempVar[0], &buffer[0], 400); - std::memcpy(&tempVar[408], &buffer[400], 380); + bool result = Game::Load_Stream(atStreamStart, buffer, size); - AssetHandler::Relocate(buffer + 400, buffer + 408, 388); + if (Zones::Version() >= VERSION_ALPHA2) + { + char tempVar[788] = { 0 }; + std::memcpy(&tempVar[0], &buffer[0], 400); + std::memcpy(&tempVar[408], &buffer[400], 380); - std::memmove(buffer, tempVar, sizeof(tempVar)); + AssetHandler::Relocate(buffer + 400, buffer + 408, 388); + + std::memmove(buffer, tempVar, sizeof(tempVar)); + } return result; } @@ -965,19 +1020,28 @@ namespace Components bool Zones::LoadmenuDef_t(bool atStreamStart, char* buffer, int size) { - if (Zones::ZoneVersion != 359) size += 4; + if (Zones::ZoneVersion != 359 && Zones::ZoneVersion >= VERSION_ALPHA2) size += 4; bool result = Game::Load_Stream(atStreamStart, buffer, size); - std::memmove(buffer + 168, buffer + 172, (Zones::ZoneVersion != 359 ? 232 : 228)); - AssetHandler::Relocate(buffer + 172, buffer + 168, (Zones::ZoneVersion != 359 ? 232 : 228)); - reinterpret_cast(buffer)->expressionData = nullptr; + if (Zones::ZoneVersion >= VERSION_ALPHA2) + { + std::memmove(buffer + 168, buffer + 172, (Zones::ZoneVersion != 359 ? 232 : 228)); + AssetHandler::Relocate(buffer + 172, buffer + 168, (Zones::ZoneVersion != 359 ? 232 : 228)); + + reinterpret_cast(buffer)->expressionData = nullptr; + } return result; } void Zones::LoadWeaponAttach() { + if (Zones::ZoneVersion < VERSION_ALPHA2) + { + return Utils::Hook::Call(0x4F4160)(true); + } + // setup structures we use char* varWeaponAttach = *reinterpret_cast(0x112ADE0); // varAddonMapEnts @@ -1002,7 +1066,7 @@ namespace Components Game::MaterialPass* curPass = *Game::varMaterialPass; int count = curPass->argCount1 + curPass->argCount2 + curPass->argCount3; - for (int i = 0; i < count; ++i) + for (int i = 0; i < count && (Zones::ZoneVersion >= VERSION_ALPHA2); ++i) { Game::MaterialShaderArgument* arg = &argument[i]; @@ -1059,16 +1123,24 @@ namespace Components bool Zones::LoadStructuredDataStructPropertyArray(bool atStreamStart, char* data, int size) { - size /= 16; - int count = size; - size *= 24; + int count = 0; + + if (Zones::ZoneVersion >= VERSION_ALPHA2) + { + size /= 16; + count = size; + size *= 24; + } bool result = Game::Load_Stream(atStreamStart, data, size); - for (int i = 0; i < count; ++i) + if (Zones::ZoneVersion >= VERSION_ALPHA2) { - std::memmove(data + (i * 16), data + (i * 24), 16); - AssetHandler::Relocate(data + (i * 24), data + (i * 16), 16); + for (int i = 0; i < count; ++i) + { + std::memmove(data + (i * 16), data + (i * 24), 16); + AssetHandler::Relocate(data + (i * 24), data + (i * 16), 16); + } } return result; @@ -1076,60 +1148,67 @@ namespace Components bool Zones::LoadGfxImage(bool atStreamStart, char* buffer, int size) { - size = (Zones::ZoneVersion >= 359) ? 52 : 36; + if (Zones::ZoneVersion >= 332) + { + size = (Zones::ZoneVersion >= 359) ? 52 : 36; + } + bool result = Game::Load_Stream(atStreamStart, buffer, size); - AssetHandler::Relocate(buffer + (size - 4), buffer + 28, 4); - - if (Zones::Version() >= 359) + if (Zones::ZoneVersion >= 332) { - struct - { - Game::GfxImageLoadDef* texture; - char mapType; - char semantic; - char category; - char flags; - int cardMemory; - char pad[8]; // ? - int dataLen1; - int dataLen2; - char pad2[4]; // ? - short height; - short width; - short depth; - char loaded; - char pad3[5]; - Game::GfxImageLoadDef* storedTexture; - char* name; - } image359; + AssetHandler::Relocate(buffer + (size - 4), buffer + 28, 4); - Assert_Size(image359, 52); + if (Zones::Version() >= 359) + { + struct + { + Game::GfxImageLoadDef* texture; + char mapType; + char semantic; + char category; + char flags; + int cardMemory; + char pad[8]; // ? + int dataLen1; + int dataLen2; + char pad2[4]; // ? + short height; + short width; + short depth; + char loaded; + char pad3[5]; + Game::GfxImageLoadDef* storedTexture; + char* name; + } image359; - // Copy to new struct - memcpy(&image359, buffer, sizeof(image359)); + Assert_Size(image359, 52); - // Convert to old struct - Game::GfxImage* image = reinterpret_cast(buffer); - image->mapType = image359.mapType; - image->semantic = image359.semantic; - image->category = image359.category; - image->flags = image359.flags; - image->cardMemory = image359.cardMemory; - image->dataLen1 = image359.dataLen1; - image->dataLen2 = image359.dataLen2; - image->height = image359.height; - image->width = image359.width; - image->depth = image359.depth; - image->loaded = image359.loaded; - image->name = image359.name; + // Copy to new struct + memcpy(&image359, buffer, sizeof(image359)); - // Used for later stuff - image->pad = image359.pad3[1]; - } - else - { - memcpy(buffer + 28, buffer + (size - 4), 4); + // Convert to old struct + Game::GfxImage* image = reinterpret_cast(buffer); + image->mapType = image359.mapType; + image->semantic = image359.semantic; + image->category = image359.category; + image->flags = image359.flags; + image->cardMemory = image359.cardMemory; + image->dataLen1 = image359.dataLen1; + image->dataLen2 = image359.dataLen2; + image->height = image359.height; + image->width = image359.width; + image->depth = image359.depth; + image->loaded = image359.loaded; + image->name = image359.name; + + // Used for later stuff + image->pad = image359.pad3[1]; + } + else + { + memcpy(buffer + 28, buffer + (size - 4), 4); + } } return result; @@ -1137,43 +1216,59 @@ namespace Components bool Zones::LoadXAsset(bool atStreamStart, char* buffer, int size) { - size /= 8; - int count = size; - size *= 16; + int count = 0; + + if (Zones::ZoneVersion >= 359) + { + size /= 8; + count = size; + size *= 16; + } bool result = Game::Load_Stream(atStreamStart, buffer, size); - Utils::Memory::Allocator allocator; - Game::XAsset* tempAssets = allocator.AllocateArray(count); + if (Zones::ZoneVersion >= 359) + { + Utils::Memory::Allocator allocator; + Game::XAsset* tempAssets = allocator.AllocateArray(count); - for (int i = 0; i < count; ++i) - { - char* src = &buffer[i * 16]; + for (int i = 0; i < count; ++i) + { + char* src = &buffer[i * 16]; - std::memcpy(&tempAssets[i].type, src + 0, 4); - std::memcpy(&tempAssets[i].header, src + 8, 4); + std::memcpy(&tempAssets[i].type, src + 0, 4); + std::memcpy(&tempAssets[i].header, src + 8, 4); - AssetHandler::Relocate(src + 0, buffer + (i * 8) + 0, 4); - AssetHandler::Relocate(src + 8, buffer + (i * 8) + 4, 4); + AssetHandler::Relocate(src + 0, buffer + (i * 8) + 0, 4); + AssetHandler::Relocate(src + 8, buffer + (i * 8) + 4, 4); + } + + std::memcpy(buffer, tempAssets, sizeof(Game::XAsset) * count); } - std::memcpy(buffer, tempAssets, sizeof(Game::XAsset) * count); - return result; } bool Zones::LoadMaterialTechnique(bool atStreamStart, char* buffer, int size) { - bool result = Game::Load_Stream(atStreamStart, buffer, size + 4); + if (Zones::ZoneVersion >= 359) + { + size += 4; + } + + bool result = Game::Load_Stream(atStreamStart, buffer, size); - // This shouldn't make any difference. - // The new entry is an additional remapped techset which is linked at runtime. - // It's used when the 0x100 gameFlag in a material is set. - // As MW2 flags are only 1 byte large, this won't be possible anyways - int shiftTest = 4; + if (Zones::ZoneVersion >= 359) + { + // This shouldn't make any difference. + // The new entry is an additional remapped techset which is linked at runtime. + // It's used when the 0x100 gameFlag in a material is set. + // As MW2 flags are only 1 byte large, this won't be possible anyways + int shiftTest = 4; - std::memmove(buffer + 8 + shiftTest, buffer + 12 + shiftTest, 196 - shiftTest); - AssetHandler::Relocate(buffer + 12 + shiftTest, buffer + 8 + shiftTest, 196 - shiftTest); + std::memmove(buffer + 8 + shiftTest, buffer + 12 + shiftTest, 196 - shiftTest); + AssetHandler::Relocate(buffer + 12 + shiftTest, buffer + 8 + shiftTest, 196 - shiftTest); + } return result; } @@ -1182,53 +1277,56 @@ namespace Components { bool result = Game::Load_Stream(atStreamStart, buffer, size); - struct material339_s - { - char drawSurfBegin[8]; // 4 - //int surfaceTypeBits; - const char *name; - char drawSurf[6]; - - union - { - char gameFlags; - short sGameFlags; - }; - char sortKey; - char textureAtlasRowCount; - char textureAtlasColumnCount; - } material359; - - static_assert(offsetof(material339_s, gameFlags) == 18, ""); - static_assert(offsetof(material339_s, sortKey) == 20, ""); - static_assert(offsetof(material339_s, textureAtlasColumnCount) == 22, ""); - - static_assert(offsetof(Game::Material, stateBitsEntry) == 24, ""); - - Game::Material* material = (Game::Material*)buffer; - memcpy(&material359, material, sizeof(material359)); - - material->name = material359.name; - material->sortKey = material359.sortKey; - material->textureAtlasRowCount = material359.textureAtlasRowCount; - material->textureAtlasColumnCount = material359.textureAtlasColumnCount; - material->gameFlags = material359.gameFlags; - - // Probably wrong - material->surfaceTypeBits = 0;//material359.surfaceTypeBits; - - // Pretty sure that's wrong - // Actually, it's not - memcpy(material->drawSurf, material359.drawSurfBegin, 8); - - material->drawSurf[8] = material359.drawSurf[0]; - material->drawSurf[9] = material359.drawSurf[1]; - material->drawSurf[10] = material359.drawSurf[2]; - material->drawSurf[11] = material359.drawSurf[3]; - - if (material359.sGameFlags & 0x100) + if (Zones::ZoneVersion >= 359) { - //OutputDebugStringA(""); + struct material339_s + { + char drawSurfBegin[8]; // 4 + //int surfaceTypeBits; + const char *name; + char drawSurf[6]; + + union + { + char gameFlags; + short sGameFlags; + }; + char sortKey; + char textureAtlasRowCount; + char textureAtlasColumnCount; + } material359; + + static_assert(offsetof(material339_s, gameFlags) == 18, ""); + static_assert(offsetof(material339_s, sortKey) == 20, ""); + static_assert(offsetof(material339_s, textureAtlasColumnCount) == 22, ""); + + static_assert(offsetof(Game::Material, stateBitsEntry) == 24, ""); + + Game::Material* material = (Game::Material*)buffer; + memcpy(&material359, material, sizeof(material359)); + + material->name = material359.name; + material->sortKey = material359.sortKey; + material->textureAtlasRowCount = material359.textureAtlasRowCount; + material->textureAtlasColumnCount = material359.textureAtlasColumnCount; + material->gameFlags = material359.gameFlags; + + // Probably wrong + material->surfaceTypeBits = 0;//material359.surfaceTypeBits; + + // Pretty sure that's wrong + // Actually, it's not + memcpy(material->drawSurf, material359.drawSurfBegin, 8); + + material->drawSurf[8] = material359.drawSurf[0]; + material->drawSurf[9] = material359.drawSurf[1]; + material->drawSurf[10] = material359.drawSurf[2]; + material->drawSurf[11] = material359.drawSurf[3]; + + if (material359.sGameFlags & 0x100) + { + //OutputDebugStringA(""); + } } return result; @@ -1236,11 +1334,19 @@ namespace Components bool Zones::LoadGfxWorld(bool atStreamStart, char* buffer, int size) { - bool result = Game::Load_Stream(atStreamStart, buffer, size + 968); + if (Zones::ZoneVersion >= 359) + { + size += 968; + } - int sunDiff = 8; // Stuff that is part of the sunflare we would overwrite - std::memmove(buffer + 348 + sunDiff, buffer + 1316 + sunDiff, 280 - sunDiff); - AssetHandler::Relocate(buffer + 1316, buffer + 348, 280); + bool result = Game::Load_Stream(atStreamStart, buffer, size); + + if (Zones::ZoneVersion >= 359) + { + int sunDiff = 8; // Stuff that is part of the sunflare we would overwrite + std::memmove(buffer + 348 + sunDiff, buffer + 1316 + sunDiff, 280 - sunDiff); + AssetHandler::Relocate(buffer + 1316, buffer + 348, 280); + } return result; } @@ -1249,19 +1355,22 @@ namespace Components { Game::Load_MaterialHandle(atStreamStart); - char* varsunflare_t = *reinterpret_cast(0x112A848); + if (Zones::ZoneVersion >= 359) + { + char* varsunflare_t = *reinterpret_cast(0x112A848); - *Game::varMaterialHandle = reinterpret_cast(varsunflare_t + 12); - Game::Load_MaterialHandle(atStreamStart); + *Game::varMaterialHandle = reinterpret_cast(varsunflare_t + 12); + Game::Load_MaterialHandle(atStreamStart); - *Game::varMaterialHandle = reinterpret_cast(varsunflare_t + 16); - Game::Load_MaterialHandle(atStreamStart); + *Game::varMaterialHandle = reinterpret_cast(varsunflare_t + 16); + Game::Load_MaterialHandle(atStreamStart); - std::memmove(varsunflare_t + 12, varsunflare_t + 20, 84); + std::memmove(varsunflare_t + 12, varsunflare_t + 20, 84); - // Copy the remaining struct data we couldn't copy in LoadGfxWorld - char* varGfxWorld = *reinterpret_cast(0x112A7F4); - std::memmove(varGfxWorld + 348, varGfxWorld + 1316, 8); + // Copy the remaining struct data we couldn't copy in LoadGfxWorld + char* varGfxWorld = *reinterpret_cast(0x112A7F4); + std::memmove(varGfxWorld + 348, varGfxWorld + 1316, 8); + } } bool Zones::LoadStatement(bool atStreamStart, char* buffer, int size) @@ -1311,9 +1420,6 @@ namespace Components // physpreset size Utils::Hook::Set(0x49CE0A, (patch) ? 68 : 44); - // XModel size - Utils::Hook::Set(0x410D8A, (patch) ? ((Zones::ZoneVersion == VERSION_ALPHA2) ? 0x16C : 0x168) : 0x130); - // XSurface size Utils::Hook::Set(0x48E84A, (patch) ? 48 : 36); @@ -1331,10 +1437,7 @@ namespace Components // PathData internal struct data size Utils::Hook::Set(0x463D63, (patch) ? 0x9090 : 0x048D); Utils::Hook::Set(0x463D65, (patch) ? 0x90 : 0x40); - Utils::Hook::Set(0x463D66, (patch) ? 0x9004E0C1 : 0xC003C003); // shl eax, 4 instead of add eax, eax * 2 - - // addon_map_ents asset type (we reuse it for weaponattach) - Utils::Hook::Set(0x418B30, (patch) ? 43 : Game::ASSET_TYPE_ADDON_MAP_ENTS); + Utils::Hook::Set(0x463D66, (patch) ? 0x9004E0C1 : 0xC003C003); // shl eax, 4 instead of add eax, eax * 2 // Change block for image load defs Utils::Hook::Set(0x4D3224, ((Zones::ZoneVersion >= 332) ? 3 : 0)); @@ -1352,102 +1455,6 @@ namespace Components Utils::Hook::Set(0x4AF680, 0xA1); } - if (patch) - { - Zones::LoadFxElemDefArrayHook.Install(); - Zones::LoadFxElemDefHook.Install(); - - Zones::LoadXModelLodInfoHook.Install(); - Zones::LoadXModelHook.Install(); - - Zones::LoadXSurfaceArrayHook.Install(); - Zones::LoadGameWorldSpHook.Install(); - Zones::LoadPathDataTailHook.Install(); - - Zones::LoadWeaponCompleteDefHook.Install(); - Zones::LoadVehicleDefHook.Install(); - - Zones::Loadsnd_alias_tArrayHook.Install(); - Zones::LoadLoadedSoundHook.Install(); - Zones::LoadmenuDef_tHook.Install(); - Zones::LoadFxEffectDefHook.Install(); - - Zones::LoadWeaponAttachHook.Install(); - - if (Zones::ZoneVersion >= VERSION_ALPHA3) - { - Zones::LoadPathDataHook.Install(); - } - else - { - Zones::LoadPathDataHook.Uninstall(); - } - - if (Zones::ZoneVersion >= 332) - { - Zones::LoadGfxImageHook.Install(); - } - else - { - Zones::LoadGfxImageHook.Uninstall(); - } - - if (Zones::ZoneVersion >= 359) - { - Zones::LoadXAssetHook.Install(); - Zones::LoadMaterialTechniqueHook.Install(); - Zones::LoadMaterialHook.Install(); - Zones::LoadGfxWorldHook.Install(); - Zones::Loadsunflare_tHook.Install(); - } - else - { - Zones::LoadXAssetHook.Uninstall(); - Zones::LoadMaterialTechniqueHook.Uninstall(); - Zones::LoadMaterialHook.Uninstall(); - Zones::LoadGfxWorldHook.Uninstall(); - Zones::Loadsunflare_tHook.Uninstall(); - } - - Zones::LoadMaterialShaderArgumentArrayHook.Install(); - Zones::LoadStructuredDataStructPropertyArrayHook.Install(); - } - else - { - Zones::LoadFxElemDefArrayHook.Uninstall(); - Zones::LoadFxElemDefHook.Uninstall(); - - Zones::LoadXModelLodInfoHook.Uninstall(); - Zones::LoadXModelHook.Uninstall(); - - Zones::LoadXSurfaceArrayHook.Uninstall(); - Zones::LoadGameWorldSpHook.Uninstall(); - Zones::LoadPathDataTailHook.Uninstall(); - - Zones::LoadWeaponCompleteDefHook.Uninstall(); - Zones::LoadVehicleDefHook.Uninstall(); - - Zones::Loadsnd_alias_tArrayHook.Uninstall(); - Zones::LoadLoadedSoundHook.Uninstall(); - Zones::LoadmenuDef_tHook.Uninstall(); - Zones::LoadFxEffectDefHook.Uninstall(); - - Zones::LoadWeaponAttachHook.Uninstall(); - - Zones::LoadPathDataHook.Uninstall(); - - Zones::LoadMaterialShaderArgumentArrayHook.Uninstall(); - Zones::LoadStructuredDataStructPropertyArrayHook.Uninstall(); - - Zones::LoadGfxImageHook.Uninstall(); - - Zones::LoadXAssetHook.Uninstall(); - Zones::LoadMaterialTechniqueHook.Uninstall(); - Zones::LoadMaterialHook.Uninstall(); - Zones::LoadGfxWorldHook.Uninstall(); - Zones::Loadsunflare_tHook.Uninstall(); - } - AntiCheat::EmptyHash(); } @@ -1465,38 +1472,49 @@ namespace Components // Block Mark_pathnode_constant_t Utils::Hook::Set(0x4F74B0, 0xC3); - Zones::LoadFxElemDefArrayHook.Initialize(0x495938, Zones::LoadFxElemDefArrayStub, HOOK_CALL); - Zones::LoadFxElemDefHook.Initialize(0x45ADA0, Zones::LoadFxElemDefStub, HOOK_CALL); - Zones::LoadXModelLodInfoHook.Initialize(0x4EA6FE, Zones::LoadXModelLodInfoStub, HOOK_CALL); - Zones::LoadXModelHook.Initialize(0x410D90, Zones::LoadXModel, HOOK_CALL); - Zones::LoadXSurfaceArrayHook.Initialize(0x4925C8, Zones::LoadXSurfaceArray, HOOK_CALL); - Zones::LoadGameWorldSpHook.Initialize(0x4F4D0D, Zones::LoadGameWorldSp, HOOK_CALL); - Zones::LoadWeaponCompleteDefHook.Initialize(0x47CCD2, Zones::LoadWeaponCompleteDef, HOOK_CALL); - Zones::LoadVehicleDefHook.Initialize(0x483DA0, Zones::LoadVehicleDef, HOOK_CALL); - Zones::Loadsnd_alias_tArrayHook.Initialize(0x4F0AC8, Zones::Loadsnd_alias_tArray, HOOK_CALL); - Zones::LoadLoadedSoundHook.Initialize(0x403A5D, Zones::LoadLoadedSound, HOOK_CALL); - Zones::LoadWeaponAttachHook.Initialize(0x463022, Zones::LoadWeaponAttach, HOOK_CALL); - Zones::LoadmenuDef_tHook.Initialize(0x41A570, Zones::LoadmenuDef_t, HOOK_CALL); - Zones::LoadFxEffectDefHook.Initialize(0x49591B, Zones::LoadFxEffectDef, HOOK_CALL); - Zones::LoadMaterialShaderArgumentArrayHook.Initialize(0x428F0A, Zones::LoadMaterialShaderArgumentArray, HOOK_CALL); - Zones::LoadStructuredDataStructPropertyArrayHook.Initialize(0x4B1EB8, Zones::LoadStructuredDataStructPropertyArray, HOOK_CALL); - - Zones::LoadGfxImageHook.Initialize(0x4471AD, Zones::LoadGfxImage, HOOK_CALL); + // addon_map_ents asset type (we reuse it for weaponattach) + Utils::Hook::Set(0x418B31, 0x72); - Zones::LoadXAssetHook.Initialize(0x5B9AA5, Zones::LoadXAsset, HOOK_CALL); - Zones::LoadMaterialTechniqueHook.Initialize(0x461710, Zones::LoadMaterialTechnique, HOOK_CALL); - Zones::LoadMaterialHook.Initialize(0x40330D, Zones::LoadMaterial, HOOK_CALL); - Zones::LoadGfxWorldHook.Initialize(0x4B8DC0, Zones::LoadGfxWorld, HOOK_CALL); - Zones::Loadsunflare_tHook.Initialize(0x4B8FF5, Zones::Loadsunflare_t, HOOK_CALL); + Utils::Hook(0x495938, Zones::LoadFxElemDefArrayStub, HOOK_CALL).Install()->Quick(); + Utils::Hook(0x45ADA0, Zones::LoadFxElemDefStub, HOOK_CALL).Install()->Quick(); + Utils::Hook(0x4EA6FE, Zones::LoadXModelLodInfoStub, HOOK_CALL).Install()->Quick(); + Utils::Hook(0x410D90, Zones::LoadXModel, HOOK_CALL).Install()->Quick(); + Utils::Hook(0x4925C8, Zones::LoadXSurfaceArray, HOOK_CALL).Install()->Quick(); + Utils::Hook(0x4F4D0D, Zones::LoadGameWorldSp, HOOK_CALL).Install()->Quick(); + Utils::Hook(0x47CCD2, Zones::LoadWeaponCompleteDef, HOOK_CALL).Install()->Quick(); + Utils::Hook(0x483DA0, Zones::LoadVehicleDef, HOOK_CALL).Install()->Quick(); + Utils::Hook(0x4F0AC8, Zones::Loadsnd_alias_tArray, HOOK_CALL).Install()->Quick(); + Utils::Hook(0x403A5D, Zones::LoadLoadedSound, HOOK_CALL).Install()->Quick(); + Utils::Hook(0x463022, Zones::LoadWeaponAttach, HOOK_CALL).Install()->Quick(); + Utils::Hook(0x41A570, Zones::LoadmenuDef_t, HOOK_CALL).Install()->Quick(); + Utils::Hook(0x49591B, Zones::LoadFxEffectDef, HOOK_CALL).Install()->Quick(); + Utils::Hook(0x428F0A, Zones::LoadMaterialShaderArgumentArray, HOOK_CALL).Install()->Quick(); + Utils::Hook(0x4B1EB8, Zones::LoadStructuredDataStructPropertyArray, HOOK_CALL).Install()->Quick(); - Zones::LoadPathDataTailHook.Initialize(0x427A1B, Zones::LoadPathDataTail, HOOK_JUMP); - Zones::LoadPathDataHook.Initialize(0x4F4D3B, [] () + Utils::Hook(0x4471AD, Zones::LoadGfxImage, HOOK_CALL).Install()->Quick(); + + Utils::Hook(0x5B9AA5, Zones::LoadXAsset, HOOK_CALL).Install()->Quick(); + Utils::Hook(0x461710, Zones::LoadMaterialTechnique, HOOK_CALL).Install()->Quick(); + Utils::Hook(0x40330D, Zones::LoadMaterial, HOOK_CALL).Install()->Quick(); + Utils::Hook(0x4B8DC0, Zones::LoadGfxWorld, HOOK_CALL).Install()->Quick(); + Utils::Hook(0x4B8FF5, Zones::Loadsunflare_t, HOOK_CALL).Install()->Quick(); + + Utils::Hook(0x427A1B, Zones::LoadPathDataTail, HOOK_JUMP).Install()->Quick(); + Utils::Hook(0x4F4D3B, [] () { - ZeroMemory(*Game::varPathData, sizeof(Game::PathData)); - }, HOOK_CALL); + if (Zones::ZoneVersion >= VERSION_ALPHA3) + { + ZeroMemory(*Game::varPathData, sizeof(Game::PathData)); + } + else + { + // Load_PathData + Utils::Hook::Call(0x4278A0)(false) + } + }, HOOK_CALL).Install()->Quick(); Utils::Hook(0x4597DD, Zones::LoadStatement, HOOK_CALL).Install()->Quick(); - //Utils::Hook(0x471A39, Zones::LoadWindowImage, HOOK_JUMP).Install()->Quick(); + Utils::Hook(0x471A39, Zones::LoadWindowImage, HOOK_JUMP).Install()->Quick(); #ifdef DEBUG // Easy dirty disk debugging diff --git a/src/Components/Modules/Zones.hpp b/src/Components/Modules/Zones.hpp index 4d2807b5..a9cde4a6 100644 --- a/src/Components/Modules/Zones.hpp +++ b/src/Components/Modules/Zones.hpp @@ -24,30 +24,6 @@ namespace Components static int FxEffectIndex; static char* FxEffectStrings[64]; - static Utils::Hook LoadFxElemDefHook; - static Utils::Hook LoadFxElemDefArrayHook; - static Utils::Hook LoadXModelLodInfoHook; - static Utils::Hook LoadXModelHook; - static Utils::Hook LoadXSurfaceArrayHook; - static Utils::Hook LoadGameWorldSpHook; - static Utils::Hook LoadPathDataHook; - static Utils::Hook LoadVehicleDefHook; - static Utils::Hook Loadsnd_alias_tArrayHook; - static Utils::Hook LoadLoadedSoundHook; - static Utils::Hook LoadmenuDef_tHook; - static Utils::Hook LoadFxEffectDefHook; - static Utils::Hook LoadMaterialShaderArgumentArrayHook; - static Utils::Hook LoadStructuredDataStructPropertyArrayHook; - static Utils::Hook LoadPathDataTailHook; - static Utils::Hook LoadWeaponAttachHook; - static Utils::Hook LoadWeaponCompleteDefHook; - static Utils::Hook LoadGfxImageHook; - static Utils::Hook LoadXAssetHook; - static Utils::Hook LoadMaterialTechniqueHook; - static Utils::Hook LoadMaterialHook; - static Utils::Hook LoadGfxWorldHook; - static Utils::Hook Loadsunflare_tHook; - static void LoadFxElemDefArrayStub(bool atStreamStart); static bool LoadFxElemDefStub(bool atStreamStart, Game::FxElemDef* fxElem, int size); @@ -55,10 +31,10 @@ namespace Components static void LoadXModelLodInfoStub(); static bool LoadXModel(bool atStreamStart, char* xmodel, int size); static bool LoadXSurfaceArray(bool atStreamStart, char* buffer, int size); - static bool LoadGameWorldSp(bool atStreamStart, char* buffer); - static bool LoadVehicleDef(bool atStreamStart, char* buffer); + static bool LoadGameWorldSp(bool atStreamStart, char* buffer, int size); + static bool LoadVehicleDef(bool atStreamStart, char* buffer, int size); static bool Loadsnd_alias_tArray(bool atStreamStart, char* buffer, int len); - static bool LoadLoadedSound(bool atStreamStart, char* buffer); + static bool LoadLoadedSound(bool atStreamStart, char* buffer, int size); static bool LoadmenuDef_t(bool atStreamStart, char* buffer, int size); static bool LoadFxEffectDef(bool atStreamStart, char* buffer, int size); static bool LoadMaterialShaderArgumentArray(bool atStreamStart, Game::MaterialShaderArgument* argument, int size);