SHADE_Y3/SHADE_Engine/src/Assets/SHAssetMetaHandler.cpp

113 lines
3.5 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 <fstream>
#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
****************************************************************************/
void GetFieldValue(std::ifstream& file, std::string& line) noexcept
{
line = "";
std::getline(file, line);
line = line.substr(line.find_last_of(':') + 2, line.length());
}
/****************************************************************************
* \param String containing extension of resource file
* \brief Get correct resource type from file extension of resource.
****************************************************************************/
AssetType SHAssetMetaHandler::GetTypeFromExtension(AssetExtension ext) noexcept
{
for (int i{0}; i < EXTENSIONS->size(); ++i)
{
if (ext == EXTENSIONS[i])
{
return static_cast<AssetType>(i);
}
}
}
/****************************************************************************
* \param String containing extension of resource file
* \brief Get correct resource type from file extension of resource.
****************************************************************************/
AssetExtension SHAssetMetaHandler::GetExtensionFromType(AssetType type) noexcept
{
return EXTENSIONS[static_cast<size_t>(type)];
}
/****************************************************************************
* \param Create class containing meta data from meta file
* \brief path to meta data file
****************************************************************************/
SHAssetMeta SHAssetMetaHandler::RetrieveMetaData(AssetPath const& path) noexcept
{
std::ifstream metaFile{ path.string(), std::ios_base::in };
if (!metaFile.is_open())
{
// Error unable to open
}
std::string line;
SHAssetMeta meta;
// 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);
metaFile.close();
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(SHAssetMeta const& meta, AssetPath const& path) noexcept
{
std::ofstream metaFile{ path, std::ios_base::out };
if (!metaFile.is_open())
{
// Log error
}
metaFile << "ID: " << meta.id << "\n";
metaFile << "Type: " << static_cast<int>(meta.type) << std::endl;
metaFile.close();
}
}