2016-09-13 14:55:10 -04:00
|
|
|
#include "STDInclude.hpp"
|
|
|
|
|
|
|
|
namespace Components
|
|
|
|
{
|
2016-12-18 07:24:04 -05:00
|
|
|
std::unordered_map<void*, IUnknown*> ModelSurfs::BufferMap;
|
|
|
|
std::unordered_map<std::string, Game::CModelAllocData*> ModelSurfs::AllocMap;
|
2016-09-13 14:55:10 -04:00
|
|
|
|
|
|
|
IUnknown* ModelSurfs::GetBuffer(void* buffer)
|
|
|
|
{
|
|
|
|
return ModelSurfs::BufferMap[buffer];
|
|
|
|
}
|
|
|
|
|
|
|
|
void ModelSurfs::SetBuffer(char /*streamHandle*/, void* buffer, IUnknown** bufferOut, int* offsetOut)
|
|
|
|
{
|
2016-09-14 17:01:53 -04:00
|
|
|
*offsetOut = 0;
|
2016-09-13 14:55:10 -04:00
|
|
|
*bufferOut = ModelSurfs::BufferMap[buffer];
|
|
|
|
}
|
|
|
|
|
2016-09-14 17:01:53 -04:00
|
|
|
void ModelSurfs::CreateBuffers(Game::XModelSurfs* surfs)
|
2016-09-13 15:41:10 -04:00
|
|
|
{
|
2016-09-14 17:01:53 -04:00
|
|
|
for (int i = 0; i < surfs->numSurfaces; ++i)
|
|
|
|
{
|
|
|
|
Game::XSurface* surface = &surfs->surfaces[i];
|
|
|
|
if (surface->streamHandle == 0xFF)
|
|
|
|
{
|
|
|
|
IDirect3DVertexBuffer9* vertexBuffer;
|
|
|
|
IDirect3DIndexBuffer9* indexBuffer;
|
|
|
|
|
|
|
|
Game::Load_VertexBuffer(surface->vertexBuffer, &vertexBuffer, surface->numVertices * 32);
|
|
|
|
Game::Load_IndexBuffer(surface->indexBuffer, &indexBuffer, surface->numPrimitives * 3);
|
|
|
|
|
|
|
|
ModelSurfs::BufferMap[surface->vertexBuffer] = vertexBuffer;
|
|
|
|
ModelSurfs::BufferMap[surface->indexBuffer] = indexBuffer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Game::XModelSurfs* ModelSurfs::LoadXModelSurfaces(std::string name)
|
|
|
|
{
|
|
|
|
Utils::Memory::Allocator allocator;
|
2016-09-19 16:52:00 -04:00
|
|
|
FileSystem::FileReader model(fmt::sprintf("models/%s", name.data()));
|
2016-09-14 17:01:53 -04:00
|
|
|
|
2016-11-20 08:09:07 -05:00
|
|
|
if (!model.exists())
|
2016-09-14 17:01:53 -04:00
|
|
|
{
|
2016-11-11 14:35:25 -05:00
|
|
|
#ifdef DEBUG
|
|
|
|
if (Flags::HasFlag("dump"))
|
|
|
|
{
|
|
|
|
FILE* fp = nullptr;
|
|
|
|
if (!fopen_s(&fp, "dump.cfg", "a") && fp)
|
|
|
|
{
|
2016-11-20 08:09:07 -05:00
|
|
|
fprintf(fp, "dumpraw %s\n", model.getName().data());
|
2016-11-11 14:35:25 -05:00
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-09-14 17:01:53 -04:00
|
|
|
Logger::Error("Loading model %s failed!", name.data());
|
|
|
|
}
|
|
|
|
|
|
|
|
Game::CModelHeader header;
|
2016-11-20 08:09:07 -05:00
|
|
|
if (!model.read(&header, sizeof header))
|
2016-09-14 17:01:53 -04:00
|
|
|
{
|
|
|
|
Logger::Error("Reading header for model %s failed!", name.data());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (header.version != 1)
|
|
|
|
{
|
|
|
|
Logger::Error("Model %s has an invalid version %d (should be 1)!", name.data(), header.version);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allocate section buffers
|
|
|
|
header.sectionHeader[Game::SECTION_MAIN].buffer = Utils::Memory::Allocate(header.sectionHeader[Game::SECTION_MAIN].size);
|
|
|
|
header.sectionHeader[Game::SECTION_INDEX].buffer = Utils::Memory::AllocateAlign(header.sectionHeader[Game::SECTION_INDEX].size, 16);
|
|
|
|
header.sectionHeader[Game::SECTION_VERTEX].buffer = Utils::Memory::AllocateAlign(header.sectionHeader[Game::SECTION_VERTEX].size, 16);
|
2016-11-20 08:09:07 -05:00
|
|
|
header.sectionHeader[Game::SECTION_FIXUP].buffer = allocator.allocateArray<char>(header.sectionHeader[Game::SECTION_FIXUP].size);
|
2016-09-14 17:01:53 -04:00
|
|
|
|
|
|
|
// Load section data
|
|
|
|
for (int i = 0; i < ARRAY_SIZE(header.sectionHeader); ++i)
|
|
|
|
{
|
2016-11-20 08:09:07 -05:00
|
|
|
model.seek(header.sectionHeader[i].offset, FS_SEEK_SET);
|
|
|
|
if (!model.read(header.sectionHeader[i].buffer, header.sectionHeader[i].size))
|
2016-09-14 17:01:53 -04:00
|
|
|
{
|
|
|
|
Logger::Error("Reading section %d for model %s failed!", i, name.data());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fixup sections
|
|
|
|
unsigned int* fixups = reinterpret_cast<unsigned int*>(header.sectionHeader[Game::SECTION_FIXUP].buffer);
|
|
|
|
for (int i = 0; i < 3; ++i)
|
|
|
|
{
|
|
|
|
Game::CModelSectionHeader* section = &header.sectionHeader[i];
|
|
|
|
for (int j = section->fixupStart; j < section->fixupStart + section->fixupCount; ++j)
|
|
|
|
{
|
2016-09-18 12:08:30 -04:00
|
|
|
unsigned int fixup = fixups[j];
|
2016-09-14 17:01:53 -04:00
|
|
|
*reinterpret_cast<DWORD*>(reinterpret_cast<char*>(section->buffer) + (fixup >> 3)) += reinterpret_cast<DWORD>(header.sectionHeader[fixup & 3].buffer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store allocation data (not sure if this is correct)
|
|
|
|
Game::CModelAllocData* allocationData = Utils::Memory::AllocateArray<Game::CModelAllocData>();
|
|
|
|
allocationData->mainArray = header.sectionHeader[Game::SECTION_MAIN].buffer;
|
|
|
|
allocationData->indexBuffer = header.sectionHeader[Game::SECTION_INDEX].buffer;
|
|
|
|
allocationData->vertexBuffer = header.sectionHeader[Game::SECTION_VERTEX].buffer;
|
|
|
|
|
2016-11-20 08:09:07 -05:00
|
|
|
AssertSize(Game::XSurface, 64);
|
2016-09-14 17:01:53 -04:00
|
|
|
Game::XModelSurfs* modelSurfs = reinterpret_cast<Game::XModelSurfs*>(allocationData->mainArray);
|
2016-11-20 08:09:07 -05:00
|
|
|
Game::XSurface* tempSurfaces = allocator.allocateArray<Game::XSurface>(modelSurfs->numSurfaces);
|
2016-09-14 17:01:53 -04:00
|
|
|
char* surfaceData = reinterpret_cast<char*>(modelSurfs->surfaces);
|
|
|
|
|
2016-12-19 12:14:15 -05:00
|
|
|
if (ModelSurfs::AllocMap.find(modelSurfs->name) != ModelSurfs::AllocMap.end())
|
|
|
|
{
|
|
|
|
Game::CModelAllocData* allocData = ModelSurfs::AllocMap[modelSurfs->name];
|
|
|
|
|
|
|
|
if (allocData)
|
|
|
|
{
|
|
|
|
Utils::Memory::FreeAlign(allocData->indexBuffer);
|
|
|
|
Utils::Memory::FreeAlign(allocData->vertexBuffer);
|
|
|
|
Utils::Memory::Free(allocData->mainArray);
|
|
|
|
Utils::Memory::Free(allocData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-19 14:18:36 -04:00
|
|
|
ModelSurfs::AllocMap[modelSurfs->name] = allocationData;
|
|
|
|
*reinterpret_cast<void**>(reinterpret_cast<char*>(allocationData->mainArray) + 44) = allocationData;
|
|
|
|
|
2016-09-14 17:01:53 -04:00
|
|
|
for (int i = 0; i < modelSurfs->numSurfaces; ++i)
|
|
|
|
{
|
2016-09-21 17:19:16 -04:00
|
|
|
char* source = &surfaceData[i * 84];
|
|
|
|
|
2016-09-22 02:23:40 -04:00
|
|
|
std::memcpy(&tempSurfaces[i], source, 12);
|
|
|
|
std::memcpy(&tempSurfaces[i].indexBuffer, source + 16, 20);
|
|
|
|
std::memcpy(&tempSurfaces[i].numCT, source + 40, 8);
|
2016-12-20 12:15:28 -05:00
|
|
|
std::memcpy(&tempSurfaces[i].partBits, source + 52, 24);
|
2016-09-14 17:01:53 -04:00
|
|
|
tempSurfaces[i].streamHandle = 0xFF; // Fake handle for buffer interception
|
|
|
|
}
|
|
|
|
|
2016-09-22 02:23:40 -04:00
|
|
|
std::memcpy(surfaceData, tempSurfaces, 64 * modelSurfs->numSurfaces);
|
2016-09-14 17:01:53 -04:00
|
|
|
|
|
|
|
ModelSurfs::CreateBuffers(modelSurfs);
|
|
|
|
|
|
|
|
return modelSurfs;
|
|
|
|
}
|
|
|
|
|
2016-09-18 18:08:37 -04:00
|
|
|
bool ModelSurfs::LoadSurfaces(Game::XModel* model)
|
|
|
|
{
|
|
|
|
if (!model) return false;
|
|
|
|
|
2016-09-19 14:18:36 -04:00
|
|
|
bool changed = false;
|
2016-09-18 18:08:37 -04:00
|
|
|
short surfCount = 0;
|
|
|
|
|
|
|
|
for (char i = 0; i < model->numLods; ++i)
|
|
|
|
{
|
|
|
|
Game::XModelSurfs* surfs = model->lods[i].surfaces;
|
|
|
|
|
|
|
|
if (!surfs->surfaces)
|
|
|
|
{
|
2016-11-20 08:09:07 -05:00
|
|
|
AssertOffset(Game::XModelLodInfo, partBits, 12);
|
2016-09-18 18:08:37 -04:00
|
|
|
Game::XModelSurfs* newSurfs = ModelSurfs::LoadXModelSurfaces(surfs->name);
|
2016-11-11 14:35:25 -05:00
|
|
|
if (!newSurfs) continue;
|
2016-09-18 18:08:37 -04:00
|
|
|
|
2016-09-19 14:18:36 -04:00
|
|
|
surfs->surfaces = newSurfs->surfaces;
|
|
|
|
surfs->numSurfaces = newSurfs->numSurfaces;
|
2016-09-18 18:08:37 -04:00
|
|
|
|
|
|
|
model->lods[i].surfs = newSurfs->surfaces;
|
2016-12-27 09:33:47 -05:00
|
|
|
std::memcpy(&model->lods[i].partBits, &newSurfs->partBits, 16);
|
|
|
|
std::memcpy(&model->lods[i].pad3, &newSurfs->pad, 8);
|
2016-09-18 18:08:37 -04:00
|
|
|
|
|
|
|
short numSurfs = static_cast<short>(newSurfs->numSurfaces);
|
2016-09-19 14:18:36 -04:00
|
|
|
model->lods[i].numSurfs = numSurfs;
|
|
|
|
model->lods[i].maxSurfs = surfCount;
|
2016-09-18 18:08:37 -04:00
|
|
|
surfCount += numSurfs;
|
|
|
|
|
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return changed;
|
|
|
|
}
|
|
|
|
|
2016-09-13 14:55:10 -04:00
|
|
|
void ModelSurfs::ReleaseModelSurf(Game::XAssetHeader header)
|
|
|
|
{
|
2016-09-19 14:18:36 -04:00
|
|
|
bool hasCustomSurface = false;
|
2016-09-14 17:01:53 -04:00
|
|
|
for (int i = 0; i < header.surfaces->numSurfaces && header.surfaces->surfaces; ++i)
|
|
|
|
{
|
|
|
|
Game::XSurface* surface = &header.surfaces->surfaces[i];
|
2016-09-19 14:18:36 -04:00
|
|
|
|
2016-09-14 17:01:53 -04:00
|
|
|
if (surface->streamHandle == 0xFF)
|
|
|
|
{
|
2016-09-19 14:18:36 -04:00
|
|
|
hasCustomSurface = true;
|
|
|
|
|
2016-09-14 17:01:53 -04:00
|
|
|
auto buffer = ModelSurfs::BufferMap.find(surface->indexBuffer);
|
|
|
|
if (buffer != ModelSurfs::BufferMap.end())
|
|
|
|
{
|
|
|
|
buffer->second->Release();
|
|
|
|
ModelSurfs::BufferMap.erase(buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer = ModelSurfs::BufferMap.find(surface->vertexBuffer);
|
|
|
|
if (buffer != ModelSurfs::BufferMap.end())
|
|
|
|
{
|
|
|
|
buffer->second->Release();
|
|
|
|
ModelSurfs::BufferMap.erase(buffer);
|
|
|
|
}
|
2016-09-19 14:18:36 -04:00
|
|
|
}
|
|
|
|
}
|
2016-09-14 17:01:53 -04:00
|
|
|
|
2016-09-19 14:18:36 -04:00
|
|
|
if (hasCustomSurface)
|
|
|
|
{
|
|
|
|
auto allocData = ModelSurfs::AllocMap.find(header.surfaces->name);
|
|
|
|
if (allocData != ModelSurfs::AllocMap.end())
|
|
|
|
{
|
|
|
|
Utils::Memory::FreeAlign(allocData->second->indexBuffer);
|
|
|
|
Utils::Memory::FreeAlign(allocData->second->vertexBuffer);
|
|
|
|
Utils::Memory::Free(allocData->second->mainArray);
|
|
|
|
Utils::Memory::Free(allocData->second);
|
2016-09-14 17:01:53 -04:00
|
|
|
|
2016-09-19 14:18:36 -04:00
|
|
|
ModelSurfs::AllocMap.erase(allocData);
|
2016-09-14 17:01:53 -04:00
|
|
|
}
|
|
|
|
}
|
2016-09-13 14:55:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void ModelSurfs::BeginRecover()
|
|
|
|
{
|
|
|
|
for (auto& buffer : ModelSurfs::BufferMap)
|
|
|
|
{
|
|
|
|
buffer.second->Release();
|
|
|
|
}
|
|
|
|
|
|
|
|
ModelSurfs::BufferMap.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ModelSurfs::EndRecover()
|
|
|
|
{
|
|
|
|
Game::DB_EnumXAssets_Internal(Game::XAssetType::ASSET_TYPE_XMODELSURFS, [] (Game::XAssetHeader header, void* /*userdata*/)
|
|
|
|
{
|
2016-09-14 17:01:53 -04:00
|
|
|
ModelSurfs::CreateBuffers(header.surfaces);
|
2016-09-13 14:55:10 -04:00
|
|
|
}, nullptr, false);
|
|
|
|
}
|
|
|
|
|
2016-09-13 15:41:10 -04:00
|
|
|
void ModelSurfs::XModelSurfsFixup(Game::XModel* model)
|
|
|
|
{
|
2016-09-18 18:08:37 -04:00
|
|
|
if (!ModelSurfs::LoadSurfaces(model))
|
2016-09-13 15:41:10 -04:00
|
|
|
{
|
|
|
|
Game::DB_XModelSurfsFixup(model);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-13 14:55:10 -04:00
|
|
|
__declspec(naked) void ModelSurfs::GetIndexBufferStub()
|
|
|
|
{
|
|
|
|
__asm
|
|
|
|
{
|
|
|
|
mov eax, [esp + 4h]
|
|
|
|
cmp al, 0FFh
|
|
|
|
|
|
|
|
jne returnSafe
|
|
|
|
|
|
|
|
jmp ModelSurfs::SetBuffer
|
|
|
|
|
|
|
|
returnSafe:
|
|
|
|
movzx eax, [esp + 4h]
|
|
|
|
mov edx, 4B4DE5h
|
|
|
|
jmp edx
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
__declspec(naked) void ModelSurfs::GetIndexBufferStub2()
|
|
|
|
{
|
|
|
|
__asm
|
|
|
|
{
|
|
|
|
mov eax, [esp + 4h]
|
|
|
|
cmp al, 0FFh
|
|
|
|
|
|
|
|
jne returnSafe
|
|
|
|
|
|
|
|
mov eax, [edi + 0Ch]
|
|
|
|
push eax
|
|
|
|
call ModelSurfs::GetBuffer
|
|
|
|
add esp, 4h
|
|
|
|
retn
|
|
|
|
|
|
|
|
returnSafe:
|
|
|
|
mov eax, 4FDC20h
|
|
|
|
jmp eax
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-13 15:41:10 -04:00
|
|
|
__declspec(naked) void ModelSurfs::GetIndexBaseStub()
|
|
|
|
{
|
|
|
|
__asm
|
|
|
|
{
|
|
|
|
mov eax, [esp + 4h]
|
|
|
|
cmp al, 0FFh
|
|
|
|
|
|
|
|
jne returnSafe
|
|
|
|
|
|
|
|
xor eax, eax
|
|
|
|
retn
|
|
|
|
|
|
|
|
returnSafe:
|
|
|
|
mov eax, 48C5F0h
|
|
|
|
jmp eax
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-13 14:55:10 -04:00
|
|
|
__declspec(naked) void ModelSurfs::GetVertexBufferStub()
|
|
|
|
{
|
|
|
|
__asm
|
|
|
|
{
|
|
|
|
mov eax, [esp + 4h]
|
|
|
|
cmp al, 0FFh
|
|
|
|
|
|
|
|
jne returnSafe
|
|
|
|
|
|
|
|
jmp ModelSurfs::SetBuffer
|
|
|
|
|
|
|
|
returnSafe:
|
|
|
|
movzx eax, [esp + 4h]
|
|
|
|
mov edx, 5BC055h
|
|
|
|
jmp edx
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ModelSurfs::ModelSurfs()
|
|
|
|
{
|
|
|
|
ModelSurfs::BufferMap.clear();
|
|
|
|
|
|
|
|
// Install release handler
|
|
|
|
Game::DB_ReleaseXAssetHandlers[Game::XAssetType::ASSET_TYPE_XMODELSURFS] = ModelSurfs::ReleaseModelSurf;
|
|
|
|
|
|
|
|
// Install device recovery handlers
|
|
|
|
Renderer::OnDeviceRecoveryBegin(ModelSurfs::BeginRecover);
|
|
|
|
Renderer::OnDeviceRecoveryEnd(ModelSurfs::EndRecover);
|
|
|
|
|
|
|
|
// Install hooks
|
2016-11-20 08:09:07 -05:00
|
|
|
Utils::Hook(0x47A6BD, ModelSurfs::XModelSurfsFixup, HOOK_CALL).install()->quick();
|
|
|
|
Utils::Hook(0x558F12, ModelSurfs::GetIndexBaseStub, HOOK_CALL).install()->quick();
|
|
|
|
Utils::Hook(0x4B4DE0, ModelSurfs::GetIndexBufferStub, HOOK_JUMP).install()->quick();
|
|
|
|
Utils::Hook(0x558E70, ModelSurfs::GetIndexBufferStub2, HOOK_CALL).install()->quick();
|
|
|
|
Utils::Hook(0x5BC050, ModelSurfs::GetVertexBufferStub, HOOK_JUMP).install()->quick();
|
2016-09-13 14:55:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
ModelSurfs::~ModelSurfs()
|
|
|
|
{
|
|
|
|
assert(ModelSurfs::BufferMap.empty());
|
2016-09-14 17:01:53 -04:00
|
|
|
assert(ModelSurfs::AllocMap.empty());
|
2016-09-13 14:55:10 -04:00
|
|
|
}
|
|
|
|
}
|