mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2025-04-20 00:02:55 +00:00
Add implementation for stringtable assetloader iw4
This commit is contained in:
parent
8e2b9f4a62
commit
87c2e58c4b
@ -4,6 +4,22 @@
|
|||||||
|
|
||||||
using namespace IW4;
|
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)
|
PackedTexCoords Common::Vec2PackTexCoords(const vec2_t* in)
|
||||||
{
|
{
|
||||||
return PackedTexCoords{Pack32::Vec2PackTexCoords(reinterpret_cast<const float*>(in))};
|
return PackedTexCoords{Pack32::Vec2PackTexCoords(reinterpret_cast<const float*>(in))};
|
||||||
|
@ -198,6 +198,8 @@ namespace IW4
|
|||||||
class Common
|
class Common
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
static int StringTable_HashString(const char* str);
|
||||||
|
|
||||||
static PackedTexCoords Vec2PackTexCoords(const vec2_t* in);
|
static PackedTexCoords Vec2PackTexCoords(const vec2_t* in);
|
||||||
static PackedUnitVec Vec3PackUnitVec(const vec3_t* in);
|
static PackedUnitVec Vec3PackUnitVec(const vec3_t* in);
|
||||||
static GfxColor Vec4PackGfxColor(const vec4_t* in);
|
static GfxColor Vec4PackGfxColor(const vec4_t* in);
|
||||||
|
@ -3,6 +3,8 @@
|
|||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
|
||||||
#include "ObjLoading.h"
|
#include "ObjLoading.h"
|
||||||
|
#include "Csv/CsvStream.h"
|
||||||
|
#include "Game/IW4/CommonIW4.h"
|
||||||
#include "Game/IW4/IW4.h"
|
#include "Game/IW4/IW4.h"
|
||||||
#include "Pool/GlobalAssetPool.h"
|
#include "Pool/GlobalAssetPool.h"
|
||||||
|
|
||||||
@ -15,3 +17,63 @@ void* AssetLoaderStringTable::CreateEmptyAsset(const std::string& assetName, Mem
|
|||||||
stringTable->name = memory->Dup(assetName.c_str());
|
stringTable->name = memory->Dup(assetName.c_str());
|
||||||
return stringTable;
|
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;
|
||||||
|
}
|
||||||
|
@ -10,5 +10,7 @@ namespace IW4
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
_NODISCARD void* CreateEmptyAsset(const std::string& assetName, MemoryManager* memory) override;
|
_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;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -41,10 +41,11 @@ bool AssetLoaderStringTable::LoadFromRaw(const std::string& assetName, ISearchPa
|
|||||||
if (currentLine.size() > maxCols)
|
if (currentLine.size() > maxCols)
|
||||||
maxCols = currentLine.size();
|
maxCols = currentLine.size();
|
||||||
csvLines.emplace_back(std::move(currentLine));
|
csvLines.emplace_back(std::move(currentLine));
|
||||||
|
currentLine = std::vector<std::string>();
|
||||||
}
|
}
|
||||||
|
|
||||||
stringTable->columnCount = maxCols;
|
stringTable->columnCount = static_cast<int>(maxCols);
|
||||||
stringTable->rowCount = csvLines.size();
|
stringTable->rowCount = static_cast<int>(csvLines.size());
|
||||||
const auto cellCount = static_cast<unsigned>(stringTable->rowCount) * static_cast<unsigned>(stringTable->columnCount);
|
const auto cellCount = static_cast<unsigned>(stringTable->rowCount) * static_cast<unsigned>(stringTable->columnCount);
|
||||||
|
|
||||||
if (cellCount)
|
if (cellCount)
|
||||||
|
@ -41,10 +41,11 @@ bool AssetLoaderStringTable::LoadFromRaw(const std::string& assetName, ISearchPa
|
|||||||
if (currentLine.size() > maxCols)
|
if (currentLine.size() > maxCols)
|
||||||
maxCols = currentLine.size();
|
maxCols = currentLine.size();
|
||||||
csvLines.emplace_back(std::move(currentLine));
|
csvLines.emplace_back(std::move(currentLine));
|
||||||
|
currentLine = std::vector<std::string>();
|
||||||
}
|
}
|
||||||
|
|
||||||
stringTable->columnCount = maxCols;
|
stringTable->columnCount = static_cast<int>(maxCols);
|
||||||
stringTable->rowCount = csvLines.size();
|
stringTable->rowCount = static_cast<int>(csvLines.size());
|
||||||
const auto cellCount = static_cast<unsigned>(stringTable->rowCount) * static_cast<unsigned>(stringTable->columnCount);
|
const auto cellCount = static_cast<unsigned>(stringTable->rowCount) * static_cast<unsigned>(stringTable->columnCount);
|
||||||
|
|
||||||
if (cellCount)
|
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)
|
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;
|
auto compareResult = stringTable->values[a].hash - stringTable->values[b].hash;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user