2
0
mirror of https://github.com/Laupetin/OpenAssetTools.git synced 2025-07-01 00:31:56 +00:00

more t5 stuffs

This commit is contained in:
Jan
2021-04-27 19:31:26 +02:00
parent 2edca7a57e
commit 157e540302
28 changed files with 901 additions and 0 deletions

View File

@ -0,0 +1,51 @@
#include "AssetLoaderLocalizeEntry.h"
#include <sstream>
#include "Localize/LocalizeCommon.h"
#include "Parsing/LocalizeFile/LocalizeFileReader.h"
using namespace T5;
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
{
std::string fileName;
{
std::ostringstream str;
str << LocalizeCommon::GetNameOfLanguage(zone->m_language) << "/localizedstrings/" << assetName << ".str";
fileName = str.str();
}
const auto file = searchPath->Open(fileName);
if (!file.IsOpen())
return false;
LocalizeFileReader reader(*file.m_stream, assetName, zone->m_language);
const auto localizeEntries = reader.ReadLocalizeFile();
for (const auto& entry : localizeEntries)
{
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 true;
}

View File

@ -0,0 +1,17 @@
#pragma once
#include "Game/T5/T5.h"
#include "AssetLoading/BasicAssetLoader.h"
#include "AssetLoading/IAssetLoadingManager.h"
#include "SearchPath/ISearchPath.h"
namespace T5
{
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;
};
}

View File

@ -0,0 +1,43 @@
#include "AssetLoaderRawFile.h"
#include <cstring>
#include "Game/T5/T5.h"
#include "Pool/GlobalAssetPool.h"
using namespace T5;
void* AssetLoaderRawFile::CreateEmptyAsset(const std::string& assetName, MemoryManager* memory)
{
auto* rawFile = memory->Create<RawFile>();
memset(rawFile, 0, sizeof(RawFile));
rawFile->name = memory->Dup(assetName.c_str());
return rawFile;
}
bool AssetLoaderRawFile::CanLoadFromRaw() const
{
return true;
}
bool AssetLoaderRawFile::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* rawFile = memory->Create<RawFile>();
rawFile->name = memory->Dup(assetName.c_str());
rawFile->len = static_cast<int>(file.m_length);
auto* fileBuffer = static_cast<char*>(memory->Alloc(static_cast<size_t>(file.m_length + 1)));
file.m_stream->read(fileBuffer, file.m_length);
if (file.m_stream->gcount() != file.m_length)
return false;
fileBuffer[rawFile->len] = '\0';
rawFile->buffer = fileBuffer;
manager->AddAsset(ASSET_TYPE_RAWFILE, assetName, rawFile);
return true;
}

View File

@ -0,0 +1,16 @@
#pragma once
#include "Game/T5/T5.h"
#include "AssetLoading/BasicAssetLoader.h"
#include "AssetLoading/IAssetLoadingManager.h"
#include "SearchPath/ISearchPath.h"
namespace T5
{
class AssetLoaderRawFile final : public BasicAssetLoader<ASSET_TYPE_RAWFILE, RawFile>
{
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;
};
}

View File

@ -0,0 +1,92 @@
#include "AssetLoaderStringTable.h"
#include <cstring>
#include "Csv/CsvStream.h"
#include "Game/T5/CommonT5.h"
#include "Game/T5/T5.h"
#include "Pool/GlobalAssetPool.h"
using namespace T5;
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;
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));
}
stringTable->columnCount = maxCols;
stringTable->rowCount = 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 = CommonT5::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;
}
manager->AddAsset(ASSET_TYPE_STRINGTABLE, assetName, stringTable);
return true;
}

View File

@ -0,0 +1,16 @@
#pragma once
#include "Game/T5/T5.h"
#include "AssetLoading/BasicAssetLoader.h"
#include "AssetLoading/IAssetLoadingManager.h"
#include "SearchPath/ISearchPath.h"
namespace T5
{
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;
};
}