diff --git a/src/Components/Loader.cpp b/src/Components/Loader.cpp index bf25e9a5..8c8c97c0 100644 --- a/src/Components/Loader.cpp +++ b/src/Components/Loader.cpp @@ -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()); diff --git a/src/Components/Loader.hpp b/src/Components/Loader.hpp index 9d01410e..ab898db0 100644 --- a/src/Components/Loader.hpp +++ b/src/Components/Loader.hpp @@ -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" diff --git a/src/Components/Modules/StringTable.cpp b/src/Components/Modules/StringTable.cpp new file mode 100644 index 00000000..21bfcaff --- /dev/null +++ b/src/Components/Modules/StringTable.cpp @@ -0,0 +1,113 @@ +#include "STDInclude.hpp" + +namespace Components +{ + std::map 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(1); + + if (table) + { + table->name = Utils::Memory::DuplicateString(filename); + table->columnCount = parsedTable.GetColumns(); + table->rowCount = parsedTable.GetRows(); + + table->values = Utils::Memory::AllocateArray(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(); + } +} diff --git a/src/Components/Modules/StringTable.hpp b/src/Components/Modules/StringTable.hpp new file mode 100644 index 00000000..11217159 --- /dev/null +++ b/src/Components/Modules/StringTable.hpp @@ -0,0 +1,16 @@ +namespace Components +{ + class StringTable : public Component + { + public: + StringTable(); + ~StringTable(); + const char* GetName() { return "StringTable"; }; + + private: + static std::map StringTableMap; + + static int Hash(const char* data); + static Game::StringTable* LoadObject(const char* filename); + }; +} diff --git a/src/Utils/CSV.cpp b/src/Utils/CSV.cpp index 01d49b29..f674e533 100644 --- a/src/Utils/CSV.cpp +++ b/src/Utils/CSV.cpp @@ -2,9 +2,9 @@ namespace Utils { - CSV::CSV(std::string file) + CSV::CSV(std::string file, bool isFile) { - CSV::Parse(file); + CSV::Parse(file, isFile); } CSV::~CSV() @@ -37,6 +37,18 @@ namespace Utils return 0; } + int CSV::GetColumns() + { + int count = 0; + + for (int i = 0; i < CSV::GetRows(); i++) + { + count = max(CSV::GetColumns(i), count); + } + + return count; + } + std::string CSV::GetElementAt(size_t row, size_t column) { if (CSV::DataMap.size() > row) @@ -52,11 +64,19 @@ namespace Utils return ""; } - void CSV::Parse(std::string file) + void CSV::Parse(std::string file, bool isFile) { - if (!Utils::FileExists(file)) return; + std::string buffer; - std::string buffer = Utils::ReadFile(file); + if (isFile) + { + if (!Utils::FileExists(file)) return; + buffer = Utils::ReadFile(file); + } + else + { + buffer = file; + } if (buffer.size()) { @@ -74,7 +94,7 @@ namespace Utils bool isString = false; std::string element; std::vector _row; - char tempStr[2] = { 0, 0 }; + char tempStr = 0; for (unsigned int i = 0; i < row.size(); i++) { @@ -91,7 +111,7 @@ namespace Utils } else if (i < (row.size() - 1) && row[i] == '\\' &&row[i + 1] == '"' && isString) // Handle quotes in strings as \" { - tempStr[0] = '"'; + tempStr = '"'; ++i; } else if (!isString && (row[i] == '\n' || row[i] == '\x0D' || row[i] == '\x0A' || row[i] == '\t')) @@ -105,10 +125,10 @@ namespace Utils } else { - tempStr[0] = row[i]; + tempStr = row[i]; } - element.append(tempStr); + element.append(&tempStr, 1); } // Push last element diff --git a/src/Utils/CSV.hpp b/src/Utils/CSV.hpp index 76151d12..c339200d 100644 --- a/src/Utils/CSV.hpp +++ b/src/Utils/CSV.hpp @@ -3,17 +3,18 @@ namespace Utils class CSV { public: - CSV(std::string file); + CSV(std::string file, bool isFile = true); ~CSV(); int GetRows(); + int GetColumns(); int GetColumns(size_t row); std::string GetElementAt(size_t row, size_t column); private: - void Parse(std::string file); + void Parse(std::string file, bool isFile = true); void ParseRow(std::string row); std::vector> DataMap; };