2022-09-13 13:37:32 +08:00
|
|
|
/******************************************************************************
|
|
|
|
* \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
|
|
|
|
{
|
2022-09-13 14:23:33 +08:00
|
|
|
for (int i{0}; i < EXTENSIONS->size(); ++i)
|
|
|
|
{
|
|
|
|
if (ext == EXTENSIONS[i])
|
|
|
|
{
|
|
|
|
return static_cast<AssetType>(i);
|
|
|
|
}
|
|
|
|
}
|
2022-09-13 13:37:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* \param String containing extension of resource file
|
|
|
|
|
|
|
|
* \brief Get correct resource type from file extension of resource.
|
|
|
|
****************************************************************************/
|
|
|
|
AssetExtension SHAssetMetaHandler::GetExtensionFromType(AssetType type) noexcept
|
|
|
|
{
|
2022-09-13 14:23:33 +08:00
|
|
|
return EXTENSIONS[static_cast<size_t>(type)];
|
2022-09-13 13:37:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* \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 file version
|
|
|
|
GetFieldValue(metaFile, line);
|
|
|
|
meta.SetVersion(line);
|
|
|
|
|
|
|
|
// Get resource id
|
|
|
|
GetFieldValue(metaFile, line);
|
|
|
|
std::stringstream idStream{ line };
|
|
|
|
AssetID id;
|
|
|
|
idStream >> id;
|
|
|
|
meta.SetID(id);
|
|
|
|
|
|
|
|
// Get resource type
|
|
|
|
GetFieldValue(metaFile, line);
|
|
|
|
std::stringstream typeStream{ line };
|
|
|
|
AssetTypeMeta type;
|
|
|
|
typeStream >> type;
|
2022-09-13 14:23:33 +08:00
|
|
|
meta.SetType(static_cast<AssetType>(type));
|
2022-09-13 13:37:32 +08:00
|
|
|
|
|
|
|
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 << "Meta Version: " << meta.GetVersion() << "\n";
|
|
|
|
metaFile << "ID: " << meta.GetID() << "\n";
|
2022-09-13 14:23:33 +08:00
|
|
|
metaFile << "Type: " << static_cast<int>(meta.GetType()) << std::endl;
|
2022-09-13 13:37:32 +08:00
|
|
|
metaFile.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|