iw4x-client/src/Components/Modules/StringTable.cpp

80 lines
2.0 KiB
C++
Raw Normal View History

2022-02-27 07:53:44 -05:00
#include <STDInclude.hpp>
2017-01-19 16:23:59 -05:00
namespace Components
{
std::unordered_map<std::string, Game::StringTable*> StringTable::StringTableMap;
Game::StringTable* StringTable::LoadObject(std::string filename)
{
2017-06-02 09:36:20 -04:00
Utils::Memory::Allocator* allocator = Utils::Memory::GetAllocator();
2017-01-19 16:23:59 -05:00
filename = Utils::String::ToLower(filename);
Game::StringTable* table = nullptr;
FileSystem::File rawTable(filename);
if (rawTable.exists())
{
Utils::CSV parsedTable(rawTable.getBuffer(), false, false);
table = allocator->allocate<Game::StringTable>();
2017-01-19 16:23:59 -05:00
if (table)
{
table->name = allocator->duplicateString(filename);
2017-01-19 16:23:59 -05:00
table->columnCount = parsedTable.getColumns();
table->rowCount = parsedTable.getRows();
table->values = allocator->allocateArray<Game::StringTableCell>(table->columnCount * table->rowCount);
2017-01-19 16:23:59 -05:00
if (!table->values)
{
return nullptr;
}
for (int i = 0; i < table->rowCount; ++i)
{
for (int j = 0; j < table->columnCount; ++j)
{
std::string value = parsedTable.getElementAt(i, j);
Game::StringTableCell* cell = &table->values[i * table->columnCount + j];
cell->hash = Game::StringTable_HashString(value.data());
cell->string = allocator->duplicateString(value);
2017-01-19 16:23:59 -05:00
//if (!cell->string) cell->string = ""; // We have to assume it allocated successfully
}
}
StringTable::StringTableMap[filename] = table;
}
}
else
{
StringTable::StringTableMap[filename] = nullptr;
}
return table;
}
StringTable::StringTable()
{
2018-12-17 08:29:18 -05:00
AssetHandler::OnFind(Game::XAssetType::ASSET_TYPE_STRINGTABLE, [](Game::XAssetType, const std::string& _filename)
2017-01-19 16:23:59 -05:00
{
Game::XAssetHeader header = { nullptr };
2017-01-19 16:23:59 -05:00
2018-12-17 08:29:18 -05:00
std::string filename = Utils::String::ToLower(_filename);
2017-01-19 16:23:59 -05:00
if (StringTable::StringTableMap.find(filename) != StringTable::StringTableMap.end())
{
header.stringTable = StringTable::StringTableMap[filename];
}
else
{
header.stringTable = StringTable::LoadObject(filename);
}
return header;
});
}
}