2
0
mirror of https://github.com/Laupetin/OpenAssetTools.git synced 2025-09-05 16:27:27 +00:00

chore: adjust IW3 asset loaders to fit IW4 format

This commit is contained in:
Jan
2024-12-26 11:18:26 +01:00
parent 7ef944ebd4
commit a5873a301f
10 changed files with 246 additions and 201 deletions

View File

@@ -6,29 +6,48 @@
using namespace IW3;
AssetLoaderRawFile::AssetLoaderRawFile(MemoryManager& memory, ISearchPath& searchPath)
: m_memory(memory),
m_search_path(searchPath)
namespace
{
}
class RawFileLoader final : public AssetCreator<AssetRawFile>
{
public:
RawFileLoader(MemoryManager& memory, ISearchPath& searchPath)
: m_memory(memory),
m_search_path(searchPath)
{
}
AssetCreationResult AssetLoaderRawFile::CreateAsset(const std::string& assetName, AssetCreationContext& context)
AssetCreationResult CreateAsset(const std::string& assetName, AssetCreationContext& context) override
{
const auto file = m_search_path.Open(assetName);
if (!file.IsOpen())
return AssetCreationResult::NoAction();
auto* rawFile = m_memory.Alloc<RawFile>();
rawFile->name = m_memory.Dup(assetName.c_str());
rawFile->len = static_cast<int>(file.m_length);
auto* fileBuffer = m_memory.Alloc<char>(static_cast<size_t>(file.m_length + 1));
file.m_stream->read(fileBuffer, file.m_length);
if (file.m_stream->gcount() != file.m_length)
return AssetCreationResult::Failure();
fileBuffer[rawFile->len] = '\0';
rawFile->buffer = fileBuffer;
return AssetCreationResult::Success(context.AddAsset<AssetRawFile>(assetName, rawFile));
}
private:
MemoryManager& m_memory;
ISearchPath& m_search_path;
};
} // namespace
namespace IW3
{
const auto file = m_search_path.Open(assetName);
if (!file.IsOpen())
return AssetCreationResult::NoAction();
auto* rawFile = m_memory.Alloc<RawFile>();
rawFile->name = m_memory.Dup(assetName.c_str());
rawFile->len = static_cast<int>(file.m_length);
auto* fileBuffer = m_memory.Alloc<char>(static_cast<size_t>(file.m_length + 1));
file.m_stream->read(fileBuffer, file.m_length);
if (file.m_stream->gcount() != file.m_length)
return AssetCreationResult::Failure();
fileBuffer[rawFile->len] = '\0';
rawFile->buffer = fileBuffer;
return AssetCreationResult::Success(context.AddAsset<AssetRawFile>(assetName, rawFile));
}
std::unique_ptr<AssetCreator<AssetRawFile>> CreateRawFileLoader(MemoryManager& memory, ISearchPath& searchPath)
{
return std::make_unique<RawFileLoader>(memory, searchPath);
}
} // namespace IW3

View File

@@ -5,17 +5,9 @@
#include "SearchPath/ISearchPath.h"
#include "Utils/MemoryManager.h"
#include <memory>
namespace IW3
{
class AssetLoaderRawFile final : public AssetCreator<AssetRawFile>
{
public:
AssetLoaderRawFile(MemoryManager& memory, ISearchPath& searchPath);
AssetCreationResult CreateAsset(const std::string& assetName, AssetCreationContext& context) override;
private:
MemoryManager& m_memory;
ISearchPath& m_search_path;
};
std::unique_ptr<AssetCreator<AssetRawFile>> CreateRawFileLoader(MemoryManager& memory, ISearchPath& searchPath);
} // namespace IW3