Add dumper and reader for IW4 GfxLightDef

This commit is contained in:
Jan
2022-01-02 18:01:24 +01:00
parent 77b6b7c87a
commit daa7008038
8 changed files with 177 additions and 1 deletions

View File

@ -1,6 +1,8 @@
#include "AssetLoaderGfxLightDef.h"
#include <cstring>
#include <iostream>
#include <sstream>
#include "ObjLoading.h"
#include "Game/IW4/IW4.h"
@ -8,6 +10,15 @@
using namespace IW4;
std::string AssetLoaderGfxLightDef::GetAssetFilename(const std::string& assetName)
{
std::ostringstream ss;
ss << "lights/" << assetName;
return ss.str();
}
void* AssetLoaderGfxLightDef::CreateEmptyAsset(const std::string& assetName, MemoryManager* memory)
{
auto* lightDef = memory->Create<GfxLightDef>();
@ -15,3 +26,46 @@ void* AssetLoaderGfxLightDef::CreateEmptyAsset(const std::string& assetName, Mem
lightDef->name = memory->Dup(assetName.c_str());
return lightDef;
}
bool AssetLoaderGfxLightDef::CanLoadFromRaw() const
{
return true;
}
bool AssetLoaderGfxLightDef::LoadFromRaw(const std::string& assetName, ISearchPath* searchPath, MemoryManager* memory, IAssetLoadingManager* manager, Zone* zone) const
{
const auto filename = GetAssetFilename(assetName);
const auto file = searchPath->Open(filename);
if (!file.IsOpen())
return false;
const auto imageNameSize = file.m_length - sizeof(char) - sizeof(char);
if (imageNameSize < 0 || imageNameSize > MAX_IMAGE_NAME_SIZE)
return false;
std::string imageName(static_cast<size_t>(imageNameSize), '\0');
int8_t samplerState;
int8_t lmapLookupStart;
file.m_stream->read(reinterpret_cast<char*>(&samplerState), sizeof(int8_t));
file.m_stream->read(&imageName[0], static_cast<size_t>(imageNameSize));
file.m_stream->read(reinterpret_cast<char*>(&lmapLookupStart), sizeof(int8_t));
auto* imageDependency = reinterpret_cast<XAssetInfo<GfxImage>*>(manager->LoadDependency(ASSET_TYPE_IMAGE, imageName));
if(!imageDependency)
{
std::cerr << "Could not load GfxLightDef \"" << assetName << "\" due to missing image \"" << imageName << "\"\n";
return false;
}
auto* lightDef = memory->Create<GfxLightDef>();
lightDef->name = memory->Dup(assetName.c_str());
lightDef->attenuation.samplerState = samplerState;
lightDef->attenuation.image = imageDependency->Asset();
lightDef->lmapLookupStart = static_cast<int>(static_cast<uint8_t>(lmapLookupStart));
manager->AddAsset(ASSET_TYPE_LIGHT_DEF, assetName, lightDef);
return true;
}

View File

@ -8,7 +8,13 @@ namespace IW4
{
class AssetLoaderGfxLightDef final : public BasicAssetLoader<ASSET_TYPE_LIGHT_DEF, GfxLightDef>
{
static constexpr auto MAX_IMAGE_NAME_SIZE = 0x800;
static std::string GetAssetFilename(const std::string& assetName);
public:
_NODISCARD void* CreateEmptyAsset(const std::string& assetName, MemoryManager* memory) override;
_NODISCARD bool CanLoadFromRaw() const override;
bool LoadFromRaw(const std::string& assetName, ISearchPath* searchPath, MemoryManager* memory, IAssetLoadingManager* manager, Zone* zone) const override;
};
}

View File

@ -648,6 +648,9 @@ namespace IW4
std::vector<MenuEventHandler*> elements;
ConvertEventHandlerElements(elements, eventHandlerSet, menu, item);
if (elements.empty())
return nullptr;
auto* outputSet = static_cast<MenuEventHandlerSet*>(m_memory->Alloc(sizeof(MenuEventHandlerSet) + sizeof(void*) * elements.size()));
auto* outputElements = reinterpret_cast<MenuEventHandler**>(reinterpret_cast<int8_t*>(outputSet) + sizeof(MenuEventHandlerSet));
memcpy(outputElements, &elements[0], sizeof(void*) * elements.size());