72 lines
1.7 KiB
C++
72 lines
1.7 KiB
C++
/********************\
|
|
| Copyright 2024, |
|
|
| Ulysse Cura |
|
|
\********************/
|
|
|
|
///////////////////////////////////////////
|
|
// //
|
|
// Definition de la classe MapManager. //
|
|
// //
|
|
///////////////////////////////////////////
|
|
|
|
#include <fstream> // ifstream
|
|
#include <iostream> // cerr
|
|
#include <stdexcept> // exception, runtime_error
|
|
#include "MapManager.hpp" // MapManager definition
|
|
|
|
using std::ifstream, std::cerr, std::runtime_error, std::exception, std::make_unique;
|
|
|
|
json *MapManager::LoadMap(const string &path)
|
|
{
|
|
// Si la map est déja chargée, on la renvoie
|
|
auto it = m_maps.find(path);
|
|
|
|
if (it != m_maps.end())
|
|
return it->second.get();
|
|
|
|
// Sinon on charge le fichier
|
|
ifstream mapFile {path};
|
|
if(!mapFile)
|
|
{
|
|
cerr << "Erreur : Impossible d'ouvrir le fichier \"" << path << "\".\n";
|
|
throw runtime_error("Impossible d'ouvrir le fichier.\n");
|
|
}
|
|
|
|
// Puis on créé notre mapData et on la charge depuis le fichier
|
|
json mapData;
|
|
|
|
try
|
|
{
|
|
mapData = json::parse(mapFile);
|
|
}
|
|
catch (exception const &e)
|
|
{
|
|
cerr << "Erreur : " << e.what() << std::endl;
|
|
mapFile.close();
|
|
throw runtime_error("Impossible de lire correctement le fichier.\n");
|
|
}
|
|
|
|
mapFile.close();
|
|
|
|
// On ajoute la mapData dans la memoire
|
|
m_maps.emplace(path, make_unique<json>(mapData));
|
|
|
|
// Puis on la renvoie
|
|
it = m_maps.find(path);
|
|
|
|
if(it != m_maps.end())
|
|
return it->second.get();
|
|
|
|
throw runtime_error("Impossible de trouver la map dans l'unordered_map.\n");
|
|
}
|
|
|
|
void MapManager::Erase(const string &path)
|
|
{
|
|
m_maps.erase(path);
|
|
}
|
|
|
|
void MapManager::Clear()
|
|
{
|
|
m_maps.clear();
|
|
}
|