Add implementation for stringtable assetloader iw4

This commit is contained in:
Jan 2022-01-02 01:34:42 +01:00
parent 8e2b9f4a62
commit 87c2e58c4b
6 changed files with 88 additions and 5 deletions

View File

@ -4,6 +4,22 @@
using namespace IW4;
int Common::StringTable_HashString(const char* str)
{
if (!str)
return 0;
auto result = 0;
auto offset = 0;
while (str[offset])
{
const auto c = tolower(str[offset++]);
result = c + 31 * result;
}
return result;
}
PackedTexCoords Common::Vec2PackTexCoords(const vec2_t* in)
{
return PackedTexCoords{Pack32::Vec2PackTexCoords(reinterpret_cast<const float*>(in))};

View File

@ -198,6 +198,8 @@ namespace IW4
class Common
{
public:
static int StringTable_HashString(const char* str);
static PackedTexCoords Vec2PackTexCoords(const vec2_t* in);
static PackedUnitVec Vec3PackUnitVec(const vec3_t* in);
static GfxColor Vec4PackGfxColor(const vec4_t* in);

View File

@ -3,6 +3,8 @@
#include <cstring>
#include "ObjLoading.h"
#include "Csv/CsvStream.h"
#include "Game/IW4/CommonIW4.h"
#include "Game/IW4/IW4.h"
#include "Pool/GlobalAssetPool.h"
@ -15,3 +17,63 @@ void* AssetLoaderStringTable::CreateEmptyAsset(const std::string& assetName, Mem
stringTable->name = memory->Dup(assetName.c_str());
return stringTable;
}
bool AssetLoaderStringTable::CanLoadFromRaw() const
{
return true;
}
bool AssetLoaderStringTable::LoadFromRaw(const std::string& assetName, ISearchPath* searchPath, MemoryManager* memory, IAssetLoadingManager* manager, Zone* zone) const
{
const auto file = searchPath->Open(assetName);
if (!file.IsOpen())
return false;
auto* stringTable = memory->Create<StringTable>();
stringTable->name = memory->Dup(assetName.c_str());
std::vector<std::vector<std::string>> csvLines;
std::vector<std::string> currentLine;
auto maxCols = 0u;
const CsvInputStream csv(*file.m_stream);
while (csv.NextRow(currentLine))
{
if (currentLine.size() > maxCols)
maxCols = currentLine.size();
csvLines.emplace_back(std::move(currentLine));
currentLine = std::vector<std::string>();
}
stringTable->columnCount = static_cast<int>(maxCols);
stringTable->rowCount = static_cast<int>(csvLines.size());
const auto cellCount = static_cast<unsigned>(stringTable->rowCount) * static_cast<unsigned>(stringTable->columnCount);
if (cellCount)
{
stringTable->values = static_cast<StringTableCell*>(memory->Alloc(sizeof(StringTableCell) * cellCount));
for (auto row = 0u; row < csvLines.size(); row++)
{
const auto& rowValues = csvLines[row];
for (auto col = 0u; col < maxCols; col++)
{
auto& cell = stringTable->values[row * maxCols + col];
if (col >= rowValues.size() || rowValues[col].empty())
cell.string = "";
else
cell.string = memory->Dup(rowValues[col].c_str());
cell.hash = Common::StringTable_HashString(cell.string);
}
}
}
else
{
stringTable->values = nullptr;
}
manager->AddAsset(ASSET_TYPE_STRINGTABLE, assetName, stringTable);
return true;
}

View File

@ -10,5 +10,7 @@ namespace IW4
{
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

@ -41,10 +41,11 @@ bool AssetLoaderStringTable::LoadFromRaw(const std::string& assetName, ISearchPa
if (currentLine.size() > maxCols)
maxCols = currentLine.size();
csvLines.emplace_back(std::move(currentLine));
currentLine = std::vector<std::string>();
}
stringTable->columnCount = maxCols;
stringTable->rowCount = csvLines.size();
stringTable->columnCount = static_cast<int>(maxCols);
stringTable->rowCount = static_cast<int>(csvLines.size());
const auto cellCount = static_cast<unsigned>(stringTable->rowCount) * static_cast<unsigned>(stringTable->columnCount);
if (cellCount)

View File

@ -41,10 +41,11 @@ bool AssetLoaderStringTable::LoadFromRaw(const std::string& assetName, ISearchPa
if (currentLine.size() > maxCols)
maxCols = currentLine.size();
csvLines.emplace_back(std::move(currentLine));
currentLine = std::vector<std::string>();
}
stringTable->columnCount = maxCols;
stringTable->rowCount = csvLines.size();
stringTable->columnCount = static_cast<int>(maxCols);
stringTable->rowCount = static_cast<int>(csvLines.size());
const auto cellCount = static_cast<unsigned>(stringTable->rowCount) * static_cast<unsigned>(stringTable->columnCount);
if (cellCount)
@ -70,7 +71,6 @@ bool AssetLoaderStringTable::LoadFromRaw(const std::string& assetName, ISearchPa
}
}
std::sort(&stringTable->cellIndex[0], &stringTable->cellIndex[cellCount - 1], [stringTable, maxCols](const int16_t a, const int16_t b)
{
auto compareResult = stringTable->values[a].hash - stringTable->values[b].hash;