SHADE_Y3/SHADE_Engine/src/Assets/SHAssetMetaHandler.cpp

132 lines
4.0 KiB
C++
Raw Normal View History

/******************************************************************************
* \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())
}
/****************************************************************************
* \param String containing extension of resource file
* \brief Get correct resource type from file extension of resource.
****************************************************************************/
AssetExtension SHAssetMetaHandler::GetExtensionFromType(AssetType type) noexcept
{
switch (type)
{
case AssetType::AUDIO:
return AUDIO_EXTENSION;
case AssetType::AUDIO_WAV:
return AUDIO_WAV_EXTENSION;
case AssetType::IMAGE:
return IMAGE_EXTENSION;
case AssetType::SPRITE_SET:
return SPRITE_SET_EXTENSION;
case AssetType::SCRIPT:
return SCRIPT_EXTENSION;
case AssetType::WAYPOINTS_C:
return WAYPOINT_EXTENSION;
case AssetType::PREFAB:
return PREFAB_EXTENSION;
default:
break;
}
return EXT_DOES_NOT_EXIST;
}
/****************************************************************************
* \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;
meta.SetType(SHAssetMetaHandler::GetTypeFromString(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 << "Meta Version: " << meta.GetVersion() << "\n";
metaFile << "ID: " << meta.GetID() << "\n";
metaFile << "Type: " << GetStringFromType(meta.GetType()) << std::endl;
metaFile.close();
}
}