StringTables (aka. )
This commit is contained in:
@ -39,6 +39,7 @@ namespace Components
|
||||
Loader::Register(new QuickPatch());
|
||||
Loader::Register(new ServerInfo());
|
||||
Loader::Register(new ServerList());
|
||||
Loader::Register(new StringTable());
|
||||
Loader::Register(new ZoneBuilder());
|
||||
Loader::Register(new AssetHandler());
|
||||
Loader::Register(new Localization());
|
||||
|
@ -52,6 +52,7 @@ namespace Components
|
||||
#include "Modules\QuickPatch.hpp"
|
||||
#include "Modules\ServerInfo.hpp"
|
||||
#include "Modules\ServerList.hpp"
|
||||
#include "Modules\StringTable.hpp"
|
||||
#include "Modules\ZoneBuilder.hpp"
|
||||
#include "Modules\AssetHandler.hpp"
|
||||
#include "Modules\Localization.hpp"
|
||||
|
113
src/Components/Modules/StringTable.cpp
Normal file
113
src/Components/Modules/StringTable.cpp
Normal file
@ -0,0 +1,113 @@
|
||||
#include "STDInclude.hpp"
|
||||
|
||||
namespace Components
|
||||
{
|
||||
std::map<std::string, Game::StringTable*> StringTable::StringTableMap;
|
||||
|
||||
int StringTable::Hash(const char* data)
|
||||
{
|
||||
int hash = 0;
|
||||
|
||||
while (*data != 0)
|
||||
{
|
||||
hash = tolower(*data) + (31 * hash);
|
||||
|
||||
data++;
|
||||
}
|
||||
|
||||
return hash;
|
||||
}
|
||||
|
||||
Game::StringTable* StringTable::LoadObject(const char* filename)
|
||||
{
|
||||
Game::StringTable* table = nullptr;
|
||||
FileSystem::File rawTable(filename);
|
||||
|
||||
if (rawTable.Exists())
|
||||
{
|
||||
Utils::CSV parsedTable(rawTable.GetBuffer(), false);
|
||||
|
||||
table = Utils::Memory::AllocateArray<Game::StringTable>(1);
|
||||
|
||||
if (table)
|
||||
{
|
||||
table->name = Utils::Memory::DuplicateString(filename);
|
||||
table->columnCount = parsedTable.GetColumns();
|
||||
table->rowCount = parsedTable.GetRows();
|
||||
|
||||
table->values = Utils::Memory::AllocateArray<Game::StringTableCell>(table->columnCount * table->rowCount);
|
||||
|
||||
if (!table->values)
|
||||
{
|
||||
Utils::Memory::Free(table);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
for (int i = 0; i < table->rowCount; i++)
|
||||
{
|
||||
for (int j = 0; j < table->columnCount; j++)
|
||||
{
|
||||
Game::StringTableCell* cell = &table->values[i * table->columnCount + j];
|
||||
cell->hash = StringTable::Hash(parsedTable.GetElementAt(i, j).data());
|
||||
cell->string = Utils::Memory::DuplicateString(parsedTable.GetElementAt(i, j));
|
||||
//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()
|
||||
{
|
||||
AssetHandler::OnFind(Game::XAssetType::ASSET_TYPE_STRINGTABLE, [] (Game::XAssetType, const char* filename)
|
||||
{
|
||||
Game::XAssetHeader header = { 0 };
|
||||
|
||||
if (StringTable::StringTableMap.find(filename) != StringTable::StringTableMap.end())
|
||||
{
|
||||
header.stringTable = StringTable::StringTableMap[filename];
|
||||
}
|
||||
else
|
||||
{
|
||||
header.stringTable = StringTable::LoadObject(filename);
|
||||
}
|
||||
|
||||
return header;
|
||||
});
|
||||
}
|
||||
|
||||
StringTable::~StringTable()
|
||||
{
|
||||
for (auto i = StringTable::StringTableMap.begin(); i != StringTable::StringTableMap.end(); i++)
|
||||
{
|
||||
Game::StringTable* table = i->second;
|
||||
if (table)
|
||||
{
|
||||
if (table->values)
|
||||
{
|
||||
for (int i = 0; i < table->rowCount * table->columnCount; i++)
|
||||
{
|
||||
if (table->values[i].string)
|
||||
{
|
||||
Utils::Memory::Free(table->values[i].string);
|
||||
}
|
||||
}
|
||||
|
||||
Utils::Memory::Free(table->values);
|
||||
}
|
||||
|
||||
Utils::Memory::Free(table);
|
||||
}
|
||||
}
|
||||
|
||||
StringTable::StringTableMap.clear();
|
||||
}
|
||||
}
|
16
src/Components/Modules/StringTable.hpp
Normal file
16
src/Components/Modules/StringTable.hpp
Normal file
@ -0,0 +1,16 @@
|
||||
namespace Components
|
||||
{
|
||||
class StringTable : public Component
|
||||
{
|
||||
public:
|
||||
StringTable();
|
||||
~StringTable();
|
||||
const char* GetName() { return "StringTable"; };
|
||||
|
||||
private:
|
||||
static std::map<std::string, Game::StringTable*> StringTableMap;
|
||||
|
||||
static int Hash(const char* data);
|
||||
static Game::StringTable* LoadObject(const char* filename);
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user