mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2025-09-24 09:16:39 +00:00
chore: parse includes and assetlists while parsing zone definition
This commit is contained in:
14
src/ZoneCommon/Zone/AssetList/AssetListOutputStream.cpp
Normal file
14
src/ZoneCommon/Zone/AssetList/AssetListOutputStream.cpp
Normal file
@@ -0,0 +1,14 @@
|
||||
#include "AssetListOutputStream.h"
|
||||
|
||||
AssetListOutputStream::AssetListOutputStream(std::ostream& stream, const GameId game)
|
||||
: m_stream(stream),
|
||||
m_asset_name_resolver(IAssetNameResolver::GetResolverForGame(game))
|
||||
{
|
||||
}
|
||||
|
||||
void AssetListOutputStream::WriteEntry(const AssetListEntry& entry)
|
||||
{
|
||||
m_stream.WriteColumn(*m_asset_name_resolver->GetAssetTypeName(entry.m_type));
|
||||
m_stream.WriteColumn(entry.m_name);
|
||||
m_stream.NextRow();
|
||||
}
|
@@ -1,23 +1,12 @@
|
||||
#pragma once
|
||||
|
||||
#include "AssetList.h"
|
||||
#include "Csv/CsvStream.h"
|
||||
#include "Game/IGame.h"
|
||||
#include "Zone/AssetNameResolver.h"
|
||||
|
||||
#include <Zone/AssetNameResolver.h>
|
||||
#include <iostream>
|
||||
|
||||
class AssetListInputStream
|
||||
{
|
||||
public:
|
||||
AssetListInputStream(std::istream& stream, GameId game);
|
||||
|
||||
bool NextEntry(AssetListEntry& entry, bool* failure) const;
|
||||
|
||||
private:
|
||||
CsvInputStream m_stream;
|
||||
const IAssetNameResolver* m_asset_name_resolver;
|
||||
};
|
||||
|
||||
class AssetListOutputStream
|
||||
{
|
||||
public:
|
96
src/ZoneCommon/Zone/AssetList/AssetListReader.cpp
Normal file
96
src/ZoneCommon/Zone/AssetList/AssetListReader.cpp
Normal file
@@ -0,0 +1,96 @@
|
||||
#include "AssetListReader.h"
|
||||
|
||||
#include "Csv/CsvStream.h"
|
||||
#include "Game/IGame.h"
|
||||
#include "Zone/AssetNameResolver.h"
|
||||
|
||||
#include <format>
|
||||
|
||||
namespace
|
||||
{
|
||||
class AssetListInputStream
|
||||
{
|
||||
public:
|
||||
AssetListInputStream(std::istream& stream, GameId game)
|
||||
: m_stream(stream),
|
||||
m_asset_name_resolver(IAssetNameResolver::GetResolverForGame(game))
|
||||
{
|
||||
}
|
||||
|
||||
bool NextEntry(AssetListEntry& entry, bool* failure) const
|
||||
{
|
||||
std::vector<std::string> row;
|
||||
if (failure)
|
||||
*failure = false;
|
||||
|
||||
while (true)
|
||||
{
|
||||
if (!m_stream.NextRow(row))
|
||||
return false;
|
||||
|
||||
if (row.empty() || (row.size() == 1 && row[0].empty()))
|
||||
continue;
|
||||
|
||||
const auto& typeName = row[0];
|
||||
const auto maybeType = m_asset_name_resolver->GetAssetTypeByName(typeName);
|
||||
if (!maybeType)
|
||||
{
|
||||
std::cerr << std::format("Unknown asset type name \"{}\"\n", typeName);
|
||||
if (failure)
|
||||
*failure = true;
|
||||
return false;
|
||||
}
|
||||
|
||||
entry.m_type = *maybeType;
|
||||
if (row.size() >= 3 && row[1].empty())
|
||||
{
|
||||
entry.m_name = row[2];
|
||||
entry.m_is_reference = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
entry.m_name = row[1];
|
||||
entry.m_is_reference = false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
CsvInputStream m_stream;
|
||||
const IAssetNameResolver* m_asset_name_resolver;
|
||||
};
|
||||
} // namespace
|
||||
|
||||
AssetListReader::AssetListReader(ISearchPath& searchPath, const GameId game)
|
||||
: m_search_path(searchPath),
|
||||
m_game(game)
|
||||
{
|
||||
}
|
||||
|
||||
std::optional<AssetList> AssetListReader::ReadAssetList(const std::string& zoneName, const bool logMissing) const
|
||||
{
|
||||
const auto assetListFileName = std::format("assetlist/{}.csv", zoneName);
|
||||
const auto assetListStream = m_search_path.Open(assetListFileName);
|
||||
|
||||
if (assetListStream.IsOpen())
|
||||
{
|
||||
AssetList assetList;
|
||||
const AssetListInputStream stream(*assetListStream.m_stream, m_game);
|
||||
AssetListEntry entry;
|
||||
|
||||
bool failure;
|
||||
while (stream.NextEntry(entry, &failure))
|
||||
{
|
||||
assetList.m_entries.emplace_back(std::move(entry));
|
||||
}
|
||||
|
||||
if (!failure)
|
||||
return assetList;
|
||||
}
|
||||
else if (logMissing)
|
||||
std::cerr << std::format("Failed to open file for assetlist: {}\n", assetListFileName);
|
||||
|
||||
return std::nullopt;
|
||||
}
|
20
src/ZoneCommon/Zone/AssetList/AssetListReader.h
Normal file
20
src/ZoneCommon/Zone/AssetList/AssetListReader.h
Normal file
@@ -0,0 +1,20 @@
|
||||
#pragma once
|
||||
|
||||
#include "AssetList.h"
|
||||
#include "Game/IGame.h"
|
||||
#include "SearchPath/ISearchPath.h"
|
||||
|
||||
#include <optional>
|
||||
#include <string>
|
||||
|
||||
class AssetListReader
|
||||
{
|
||||
public:
|
||||
AssetListReader(ISearchPath& searchPath, GameId game);
|
||||
|
||||
std::optional<AssetList> ReadAssetList(const std::string& zoneName, bool logMissing = true) const;
|
||||
|
||||
private:
|
||||
ISearchPath& m_search_path;
|
||||
GameId m_game;
|
||||
};
|
@@ -1,62 +0,0 @@
|
||||
#include "AssetListStream.h"
|
||||
|
||||
#include <format>
|
||||
|
||||
AssetListInputStream::AssetListInputStream(std::istream& stream, const GameId game)
|
||||
: m_stream(stream),
|
||||
m_asset_name_resolver(IAssetNameResolver::GetResolverForGame(game))
|
||||
{
|
||||
}
|
||||
|
||||
bool AssetListInputStream::NextEntry(AssetListEntry& entry, bool* failure) const
|
||||
{
|
||||
std::vector<std::string> row;
|
||||
if (failure)
|
||||
*failure = false;
|
||||
|
||||
while (true)
|
||||
{
|
||||
if (!m_stream.NextRow(row))
|
||||
return false;
|
||||
|
||||
if (row.empty())
|
||||
continue;
|
||||
|
||||
const auto& typeName = row[0];
|
||||
const auto maybeType = m_asset_name_resolver->GetAssetTypeByName(typeName);
|
||||
if (!maybeType)
|
||||
{
|
||||
std::cerr << std::format("Unknown asset type name \"{}\"\n", typeName);
|
||||
if (failure)
|
||||
*failure = true;
|
||||
return false;
|
||||
}
|
||||
|
||||
entry.m_type = *maybeType;
|
||||
if (row.size() >= 3 && row[1].empty())
|
||||
{
|
||||
entry.m_name = row[2];
|
||||
entry.m_is_reference = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
entry.m_name = row[1];
|
||||
entry.m_is_reference = false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
AssetListOutputStream::AssetListOutputStream(std::ostream& stream, const GameId game)
|
||||
: m_stream(stream),
|
||||
m_asset_name_resolver(IAssetNameResolver::GetResolverForGame(game))
|
||||
{
|
||||
}
|
||||
|
||||
void AssetListOutputStream::WriteEntry(const AssetListEntry& entry)
|
||||
{
|
||||
m_stream.WriteColumn(*m_asset_name_resolver->GetAssetTypeName(entry.m_type));
|
||||
m_stream.WriteColumn(entry.m_name);
|
||||
m_stream.NextRow();
|
||||
}
|
Reference in New Issue
Block a user