196 lines
5.6 KiB
C++
196 lines
5.6 KiB
C++
/******************************************************************************
|
|
* \file SHAssetMetaHandler.cpp
|
|
* \author Loh Xiao Qi
|
|
* \brief Implementations for SHAssetMetaHandler.h
|
|
*
|
|
* \copyright Copyright (c) 2021 Digipen Institute of Technology. Reproduction
|
|
* or disclosure of this file or its contents without the prior
|
|
* written consent of Digipen Institute of Technology is prohibited
|
|
******************************************************************************/
|
|
#include "SHpch.h"
|
|
#include "SHAssetMetaHandler.h"
|
|
#include <sstream>
|
|
|
|
namespace SHADE
|
|
{
|
|
/****************************************************************************
|
|
* \param reference to ifstream file to read line from
|
|
* \param reference to string to store line into
|
|
|
|
* \brief Helper function to retrieve field value from meta data file
|
|
* for processing
|
|
****************************************************************************/
|
|
bool GetFieldValue(std::ifstream& file, std::string& line) noexcept
|
|
{
|
|
line = "";
|
|
if (std::getline(file, line))
|
|
{
|
|
line = line.substr(line.find_last_of(':') + 2, line.length());
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/****************************************************************************
|
|
* \param String containing extension of resource file
|
|
|
|
* \brief Get correct resource type from file extension of resource.
|
|
****************************************************************************/
|
|
AssetType SHAssetMetaHandler::GetTypeFromExtension(AssetExtension ext) noexcept
|
|
{
|
|
for (auto i{0}; i < EXTENSIONS_COUNT; ++i)
|
|
{
|
|
if (strcmp(ext.c_str(), EXTENSIONS[i].data()) == 0)
|
|
{
|
|
return static_cast<AssetType>(i);
|
|
}
|
|
}
|
|
|
|
return AssetType::INVALID;
|
|
}
|
|
|
|
/****************************************************************************
|
|
* \param String containing extension of resource file
|
|
|
|
* \brief Get correct resource type from file extension of resource.
|
|
****************************************************************************/
|
|
AssetExtension SHAssetMetaHandler::GetExtensionFromType(AssetType type) noexcept
|
|
{
|
|
return AssetExtension(EXTENSIONS[static_cast<size_t>(type)]);
|
|
}
|
|
|
|
/****************************************************************************
|
|
* \param Create class containing meta data from meta file
|
|
|
|
* \brief path to meta data file
|
|
****************************************************************************/
|
|
SHAsset SHAssetMetaHandler::RetrieveMetaData(AssetPath const& path) noexcept
|
|
{
|
|
std::ifstream metaFile{ path.string(), std::ios_base::in };
|
|
if (!metaFile.is_open())
|
|
{
|
|
SHLOG_ERROR("Unable to open meta file: {}", path.string());
|
|
return {};
|
|
}
|
|
|
|
std::string line;
|
|
SHAsset meta;
|
|
|
|
// Get resource name
|
|
GetFieldValue(metaFile, line);
|
|
std::stringstream nameStream{ line };
|
|
AssetName name;
|
|
nameStream >> name;
|
|
meta.name = name;
|
|
|
|
// Get resource id
|
|
GetFieldValue(metaFile, line);
|
|
std::stringstream idStream{ line };
|
|
AssetID id;
|
|
idStream >> id;
|
|
meta.id = id;
|
|
|
|
// Get resource type
|
|
GetFieldValue(metaFile, line);
|
|
std::stringstream typeStream{ line };
|
|
AssetTypeMeta type;
|
|
typeStream >> type;
|
|
meta.type = static_cast<AssetType>(type);
|
|
|
|
meta.isSubAsset = false;
|
|
meta.parent = 0;
|
|
|
|
// Burn Line
|
|
if (std::getline(metaFile, line))
|
|
{
|
|
// Name Line
|
|
while(GetFieldValue(metaFile, line))
|
|
{
|
|
auto subAsset = new SHAsset();
|
|
|
|
// Get resource name
|
|
std::stringstream nameStream{ line };
|
|
AssetName name;
|
|
nameStream >> name;
|
|
subAsset->name = name;
|
|
|
|
// Get resource id
|
|
GetFieldValue(metaFile, line);
|
|
std::stringstream idStream{ line };
|
|
AssetID id;
|
|
idStream >> id;
|
|
subAsset->id = id;
|
|
|
|
// Get resource type
|
|
GetFieldValue(metaFile, line);
|
|
std::stringstream typeStream{ line };
|
|
AssetTypeMeta type;
|
|
typeStream >> type;
|
|
subAsset->type = static_cast<AssetType>(type);
|
|
|
|
subAsset->isSubAsset = true;
|
|
subAsset->parent = meta.id;
|
|
|
|
meta.subAssets.push_back(subAsset);
|
|
}
|
|
}
|
|
|
|
metaFile.close();
|
|
|
|
meta.path = path.parent_path().string() + "/" + path.stem().string();
|
|
meta.path.make_preferred();
|
|
|
|
return meta;
|
|
}
|
|
|
|
/****************************************************************************
|
|
* \param Asset meta data to be written into
|
|
* \param Path to be written into
|
|
|
|
* \brief Writes meta data into text file
|
|
****************************************************************************/
|
|
void SHAssetMetaHandler::WriteMetaData(SHAsset const& meta) noexcept
|
|
{
|
|
if (meta.isSubAsset)
|
|
{
|
|
SHLOG_WARNING("Cannot write subasset meta: {}", meta.name);
|
|
return;
|
|
}
|
|
|
|
//TODO: Write into binary eventually
|
|
std::string path{ meta.path.string() };
|
|
path.append(META_EXTENSION);
|
|
|
|
std::ofstream metaFile{ path, std::ios_base::out | std::ios_base::trunc };
|
|
|
|
if (!metaFile.is_open())
|
|
{
|
|
SHLOG_ERROR("Asset write path is invalid: {}", path);
|
|
return;
|
|
}
|
|
|
|
metaFile << "Name: " << meta.name << "\n";
|
|
metaFile << "ID: " << meta.id << "\n";
|
|
metaFile << "Type: " << static_cast<AssetTypeMeta>(meta.type) << std::endl;
|
|
|
|
if (!meta.subAssets.empty())
|
|
{
|
|
metaFile << "Sub Assets:\n";
|
|
|
|
for (auto const& subAsset : meta.subAssets)
|
|
{
|
|
WriteSubAssetMeta(metaFile, *subAsset);
|
|
}
|
|
}
|
|
|
|
metaFile.close();
|
|
}
|
|
|
|
void SHAssetMetaHandler::WriteSubAssetMeta(std::ofstream& metaFile, SHAsset const& subAsset) noexcept
|
|
{
|
|
metaFile << "Name: " << subAsset.name << "\n";
|
|
metaFile << "ID: " << subAsset.id << "\n";
|
|
metaFile << "Type: " << static_cast<AssetTypeMeta>(subAsset.type) << std::endl;
|
|
}
|
|
}
|