mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2025-06-12 07:48:16 -05:00
Merge branch 'Laupetin:main' into main
This commit is contained in:
@ -0,0 +1,36 @@
|
||||
#include "AssetLoaderLocalizeEntry.h"
|
||||
|
||||
#include "Localize/LocalizeCommonAssetLoader.h"
|
||||
|
||||
using namespace IW3;
|
||||
|
||||
XAssetInfoGeneric* AssetLoaderLocalizeEntry::LoadFromGlobalAssetPools(const std::string& assetName) const
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void* AssetLoaderLocalizeEntry::CreateEmptyAsset(const std::string& assetName, MemoryManager* memory)
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
bool AssetLoaderLocalizeEntry::CanLoadFromRaw() const
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
bool AssetLoaderLocalizeEntry::LoadFromRaw(
|
||||
const std::string& assetName, ISearchPath* searchPath, MemoryManager* memory, IAssetLoadingManager* manager, Zone* zone) const
|
||||
{
|
||||
const LocalizeCommonAssetLoader commonLoader(
|
||||
[memory, manager](const CommonLocalizeEntry& entry)
|
||||
{
|
||||
auto* localizeEntry = memory->Create<LocalizeEntry>();
|
||||
localizeEntry->name = memory->Dup(entry.m_key.c_str());
|
||||
localizeEntry->value = memory->Dup(entry.m_value.c_str());
|
||||
|
||||
manager->AddAsset(ASSET_TYPE_LOCALIZE_ENTRY, entry.m_key, localizeEntry);
|
||||
});
|
||||
|
||||
return commonLoader.LoadLocalizeAsset(assetName, searchPath, manager, zone);
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
#pragma once
|
||||
|
||||
#include "AssetLoading/BasicAssetLoader.h"
|
||||
#include "AssetLoading/IAssetLoadingManager.h"
|
||||
#include "Game/IW3/IW3.h"
|
||||
#include "SearchPath/ISearchPath.h"
|
||||
|
||||
namespace IW3
|
||||
{
|
||||
class AssetLoaderLocalizeEntry final : public BasicAssetLoader<ASSET_TYPE_LOCALIZE_ENTRY, LocalizeEntry>
|
||||
{
|
||||
public:
|
||||
_NODISCARD XAssetInfoGeneric* LoadFromGlobalAssetPools(const std::string& assetName) const 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;
|
||||
};
|
||||
} // namespace IW3
|
@ -0,0 +1,39 @@
|
||||
#include "AssetLoaderStringTable.h"
|
||||
|
||||
#include "Csv/CsvStream.h"
|
||||
#include "Game/IW3/CommonIW3.h"
|
||||
#include "ObjLoading.h"
|
||||
#include "Pool/GlobalAssetPool.h"
|
||||
#include "StringTable/StringTableLoader.h"
|
||||
|
||||
#include <cstring>
|
||||
|
||||
using namespace IW3;
|
||||
|
||||
void* AssetLoaderStringTable::CreateEmptyAsset(const std::string& assetName, MemoryManager* memory)
|
||||
{
|
||||
auto* stringTable = memory->Create<StringTable>();
|
||||
memset(stringTable, 0, sizeof(StringTable));
|
||||
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;
|
||||
|
||||
string_table::StringTableLoaderV1<StringTable> loader;
|
||||
auto* stringTable = loader.LoadFromStream(assetName, *memory, *file.m_stream);
|
||||
|
||||
manager->AddAsset(ASSET_TYPE_STRINGTABLE, assetName, stringTable);
|
||||
|
||||
return true;
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
#pragma once
|
||||
|
||||
#include "AssetLoading/BasicAssetLoader.h"
|
||||
#include "Game/IW3/IW3.h"
|
||||
#include "SearchPath/ISearchPath.h"
|
||||
|
||||
namespace IW3
|
||||
{
|
||||
class AssetLoaderStringTable final : public BasicAssetLoader<ASSET_TYPE_STRINGTABLE, StringTable>
|
||||
{
|
||||
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;
|
||||
};
|
||||
} // namespace IW3
|
@ -1,6 +1,7 @@
|
||||
#include "ObjLoaderIW3.h"
|
||||
|
||||
#include "AssetLoaders/AssetLoaderGfxImage.h"
|
||||
#include "AssetLoaders/AssetLoaderLocalizeEntry.h"
|
||||
#include "AssetLoaders/AssetLoaderRawFile.h"
|
||||
#include "AssetLoading/AssetLoadingManager.h"
|
||||
#include "Game/IW3/GameAssetPoolIW3.h"
|
||||
@ -43,7 +44,7 @@ ObjLoader::ObjLoader()
|
||||
REGISTER_ASSET_LOADER(BASIC_LOADER(ASSET_TYPE_FONT, Font_s))
|
||||
REGISTER_ASSET_LOADER(BASIC_LOADER(ASSET_TYPE_MENULIST, MenuList))
|
||||
REGISTER_ASSET_LOADER(BASIC_LOADER(ASSET_TYPE_MENU, menuDef_t))
|
||||
REGISTER_ASSET_LOADER(BASIC_LOADER(ASSET_TYPE_LOCALIZE_ENTRY, LocalizeEntry))
|
||||
REGISTER_ASSET_LOADER(AssetLoaderLocalizeEntry)
|
||||
REGISTER_ASSET_LOADER(BASIC_LOADER(ASSET_TYPE_WEAPON, WeaponDef))
|
||||
REGISTER_ASSET_LOADER(BASIC_LOADER(ASSET_TYPE_SNDDRIVER_GLOBALS, SndDriverGlobals))
|
||||
REGISTER_ASSET_LOADER(BASIC_LOADER(ASSET_TYPE_FX, FxEffectDef))
|
||||
|
@ -22,7 +22,7 @@ bool AssetLoaderLocalizeEntry::CanLoadFromRaw() const
|
||||
bool AssetLoaderLocalizeEntry::LoadFromRaw(
|
||||
const std::string& assetName, ISearchPath* searchPath, MemoryManager* memory, IAssetLoadingManager* manager, Zone* zone) const
|
||||
{
|
||||
LocalizeCommonAssetLoader commonLoader(
|
||||
const LocalizeCommonAssetLoader commonLoader(
|
||||
[memory, manager](const CommonLocalizeEntry& entry)
|
||||
{
|
||||
auto* localizeEntry = memory->Create<LocalizeEntry>();
|
||||
|
@ -5,6 +5,7 @@
|
||||
#include "Game/IW4/IW4.h"
|
||||
#include "ObjLoading.h"
|
||||
#include "Pool/GlobalAssetPool.h"
|
||||
#include "StringTable/StringTableLoader.h"
|
||||
|
||||
#include <cstring>
|
||||
|
||||
@ -30,49 +31,8 @@ bool AssetLoaderStringTable::LoadFromRaw(
|
||||
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;
|
||||
}
|
||||
string_table::StringTableLoaderV2<StringTable, Common::StringTable_HashString> loader;
|
||||
auto* stringTable = loader.LoadFromStream(assetName, *memory, *file.m_stream);
|
||||
|
||||
manager->AddAsset(ASSET_TYPE_STRINGTABLE, assetName, stringTable);
|
||||
|
||||
|
@ -22,7 +22,7 @@ bool AssetLoaderLocalizeEntry::CanLoadFromRaw() const
|
||||
bool AssetLoaderLocalizeEntry::LoadFromRaw(
|
||||
const std::string& assetName, ISearchPath* searchPath, MemoryManager* memory, IAssetLoadingManager* manager, Zone* zone) const
|
||||
{
|
||||
LocalizeCommonAssetLoader commonLoader(
|
||||
const LocalizeCommonAssetLoader commonLoader(
|
||||
[memory, manager](const CommonLocalizeEntry& entry)
|
||||
{
|
||||
auto* localizeEntry = memory->Create<LocalizeEntry>();
|
||||
|
@ -5,6 +5,7 @@
|
||||
#include "Game/IW5/IW5.h"
|
||||
#include "ObjLoading.h"
|
||||
#include "Pool/GlobalAssetPool.h"
|
||||
#include "StringTable/StringTableLoader.h"
|
||||
|
||||
#include <cstring>
|
||||
|
||||
@ -30,49 +31,8 @@ bool AssetLoaderStringTable::LoadFromRaw(
|
||||
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;
|
||||
}
|
||||
string_table::StringTableLoaderV2<StringTable, Common::StringTable_HashString> loader;
|
||||
auto* stringTable = loader.LoadFromStream(assetName, *memory, *file.m_stream);
|
||||
|
||||
manager->AddAsset(ASSET_TYPE_STRINGTABLE, assetName, stringTable);
|
||||
|
||||
|
@ -22,7 +22,7 @@ bool AssetLoaderLocalizeEntry::CanLoadFromRaw() const
|
||||
bool AssetLoaderLocalizeEntry::LoadFromRaw(
|
||||
const std::string& assetName, ISearchPath* searchPath, MemoryManager* memory, IAssetLoadingManager* manager, Zone* zone) const
|
||||
{
|
||||
LocalizeCommonAssetLoader commonLoader(
|
||||
const LocalizeCommonAssetLoader commonLoader(
|
||||
[memory, manager](const CommonLocalizeEntry& entry)
|
||||
{
|
||||
auto* localizeEntry = memory->Create<LocalizeEntry>();
|
||||
|
@ -4,6 +4,7 @@
|
||||
#include "Game/T5/CommonT5.h"
|
||||
#include "Game/T5/T5.h"
|
||||
#include "Pool/GlobalAssetPool.h"
|
||||
#include "StringTable/StringTableLoader.h"
|
||||
|
||||
#include <cstring>
|
||||
|
||||
@ -29,65 +30,8 @@ bool AssetLoaderStringTable::LoadFromRaw(
|
||||
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));
|
||||
stringTable->cellIndex = static_cast<int16_t*>(memory->Alloc(sizeof(int16_t) * cellCount));
|
||||
|
||||
for (auto c = 0u; c < cellCount; c++)
|
||||
stringTable->cellIndex[c] = static_cast<int16_t>(c);
|
||||
|
||||
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::Com_HashString(cell.string);
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
if (compareResult == 0)
|
||||
compareResult = a % maxCols - b % maxCols;
|
||||
return compareResult < 0;
|
||||
});
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
stringTable->values = nullptr;
|
||||
stringTable->cellIndex = nullptr;
|
||||
}
|
||||
string_table::StringTableLoaderV3<StringTable, Common::Com_HashString> loader;
|
||||
auto* stringTable = loader.LoadFromStream(assetName, *memory, *file.m_stream);
|
||||
|
||||
manager->AddAsset(ASSET_TYPE_STRINGTABLE, assetName, stringTable);
|
||||
|
||||
|
@ -22,7 +22,7 @@ bool AssetLoaderLocalizeEntry::CanLoadFromRaw() const
|
||||
bool AssetLoaderLocalizeEntry::LoadFromRaw(
|
||||
const std::string& assetName, ISearchPath* searchPath, MemoryManager* memory, IAssetLoadingManager* manager, Zone* zone) const
|
||||
{
|
||||
LocalizeCommonAssetLoader commonLoader(
|
||||
const LocalizeCommonAssetLoader commonLoader(
|
||||
[memory, manager](const CommonLocalizeEntry& entry)
|
||||
{
|
||||
auto* localizeEntry = memory->Create<LocalizeEntry>();
|
||||
|
@ -4,6 +4,7 @@
|
||||
#include "Game/T6/CommonT6.h"
|
||||
#include "Game/T6/T6.h"
|
||||
#include "Pool/GlobalAssetPool.h"
|
||||
#include "StringTable/StringTableLoader.h"
|
||||
|
||||
#include <cstring>
|
||||
|
||||
@ -29,65 +30,8 @@ bool AssetLoaderStringTable::LoadFromRaw(
|
||||
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));
|
||||
stringTable->cellIndex = static_cast<int16_t*>(memory->Alloc(sizeof(int16_t) * cellCount));
|
||||
|
||||
for (auto c = 0u; c < cellCount; c++)
|
||||
stringTable->cellIndex[c] = static_cast<int16_t>(c);
|
||||
|
||||
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::Com_HashString(cell.string);
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
if (compareResult == 0)
|
||||
compareResult = a % maxCols - b % maxCols;
|
||||
return compareResult < 0;
|
||||
});
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
stringTable->values = nullptr;
|
||||
stringTable->cellIndex = nullptr;
|
||||
}
|
||||
string_table::StringTableLoaderV3<StringTable, Common::Com_HashString> loader;
|
||||
auto* stringTable = loader.LoadFromStream(assetName, *memory, *file.m_stream);
|
||||
|
||||
manager->AddAsset(ASSET_TYPE_STRINGTABLE, assetName, stringTable);
|
||||
|
||||
|
147
src/ObjLoading/StringTable/StringTableLoader.h
Normal file
147
src/ObjLoading/StringTable/StringTableLoader.h
Normal file
@ -0,0 +1,147 @@
|
||||
#pragma once
|
||||
|
||||
#include "Csv/CsvStream.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <istream>
|
||||
#include <type_traits>
|
||||
#include <vector>
|
||||
|
||||
namespace string_table
|
||||
{
|
||||
template<typename StringTableType, typename CellType> class AbstractStringTableLoader
|
||||
{
|
||||
protected:
|
||||
AbstractStringTableLoader() = default;
|
||||
virtual ~AbstractStringTableLoader() = default;
|
||||
AbstractStringTableLoader(const AbstractStringTableLoader& other) = default;
|
||||
AbstractStringTableLoader(AbstractStringTableLoader&& other) noexcept = default;
|
||||
AbstractStringTableLoader& operator=(const AbstractStringTableLoader& other) = default;
|
||||
AbstractStringTableLoader& operator=(AbstractStringTableLoader&& other) noexcept = default;
|
||||
|
||||
virtual void SetCellContent(CellType& cell, const char* content) = 0;
|
||||
|
||||
virtual void PostProcessStringTable(StringTableType* stringTable, const unsigned cellCount, MemoryManager& memory) {}
|
||||
|
||||
public:
|
||||
StringTableType* LoadFromStream(const std::string& assetName, MemoryManager& memory, std::istream& stream)
|
||||
{
|
||||
auto* stringTable = memory.Create<StringTableType>();
|
||||
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(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<CellType*>(memory.Alloc(sizeof(CellType) * 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())
|
||||
SetCellContent(cell, "");
|
||||
else
|
||||
SetCellContent(cell, memory.Dup(rowValues[col].c_str()));
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
stringTable->values = nullptr;
|
||||
}
|
||||
|
||||
PostProcessStringTable(stringTable, cellCount, memory);
|
||||
|
||||
return stringTable;
|
||||
}
|
||||
};
|
||||
|
||||
// =================================
|
||||
// V1: IW3
|
||||
// - Cells are const char*
|
||||
// =================================
|
||||
template<typename StringTableType> class StringTableLoaderV1 final : public AbstractStringTableLoader<StringTableType, const char*>
|
||||
{
|
||||
protected:
|
||||
void SetCellContent(const char*& cell, const char* content) override
|
||||
{
|
||||
cell = content;
|
||||
}
|
||||
};
|
||||
|
||||
// =================================
|
||||
// V2: IW4, IW5
|
||||
// - Cells are a struct and have a hash
|
||||
// =================================
|
||||
template<typename StringTableType, int (*HashFunc)(const char*)>
|
||||
class StringTableLoaderV2 final : public AbstractStringTableLoader<StringTableType, std::remove_pointer_t<decltype(StringTableType::values)>>
|
||||
{
|
||||
using CellType_t = decltype(*StringTableType::values);
|
||||
|
||||
protected:
|
||||
void SetCellContent(CellType_t& cell, const char* content) override
|
||||
{
|
||||
cell.string = content;
|
||||
cell.hash = HashFunc(content);
|
||||
}
|
||||
};
|
||||
|
||||
// =================================
|
||||
// V3: T5, T6
|
||||
// - Cells are a struct and have a hash
|
||||
// - StringTable has an index array for binary search
|
||||
// =================================
|
||||
template<typename StringTableType, int (*HashFunc)(const char*)>
|
||||
class StringTableLoaderV3 final : public AbstractStringTableLoader<StringTableType, std::remove_pointer_t<decltype(StringTableType::values)>>
|
||||
{
|
||||
using CellType_t = decltype(*StringTableType::values);
|
||||
|
||||
protected:
|
||||
void SetCellContent(CellType_t& cell, const char* content) override
|
||||
{
|
||||
cell.string = content;
|
||||
cell.hash = HashFunc(content);
|
||||
}
|
||||
|
||||
void PostProcessStringTable(StringTableType* stringTable, const unsigned cellCount, MemoryManager& memory) override
|
||||
{
|
||||
if (!cellCount)
|
||||
{
|
||||
stringTable->cellIndex = nullptr;
|
||||
return;
|
||||
}
|
||||
|
||||
stringTable->cellIndex = static_cast<int16_t*>(memory.Alloc(sizeof(int16_t) * cellCount));
|
||||
for (auto i = 0u; i < cellCount; i++)
|
||||
stringTable->cellIndex[i] = i;
|
||||
|
||||
std::sort(&stringTable->cellIndex[0],
|
||||
&stringTable->cellIndex[cellCount - 1],
|
||||
[stringTable](const int16_t a, const int16_t b)
|
||||
{
|
||||
auto compareResult = stringTable->values[a].hash - stringTable->values[b].hash;
|
||||
if (compareResult == 0)
|
||||
compareResult = (a % stringTable->columnCount) - (b % stringTable->columnCount);
|
||||
return compareResult < 0;
|
||||
});
|
||||
}
|
||||
};
|
||||
} // namespace string_table
|
Reference in New Issue
Block a user