Skip to content

Commit

Permalink
feat: refactor ResourceManager to implement Singleton pattern and upd…
Browse files Browse the repository at this point in the history
…ate scene management methods
  • Loading branch information
MasterLaplace committed Nov 7, 2024
1 parent 571959c commit 90eb2bb
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 94 deletions.
6 changes: 4 additions & 2 deletions Flakkari/Server/Game/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ Game::Game(const std::string &name, std::shared_ptr<nlohmann::json> config)
throw std::runtime_error("Game: no scenes found");

loadScene((*_config)["startGame"]);
ResourceManager::addScene(config, (*_config)["startGame"]);
ResourceManager::GetInstance().addScene(config, (*_config)["startGame"]);
ResourceManager::UnlockInstance();
}

Game::~Game()
Expand Down Expand Up @@ -449,9 +450,10 @@ bool Game::addPlayer(std::shared_ptr<Client> player)

Engine::ECS::Entity newEntity = registry.spawn_entity();
auto p_Template = (*_config)["playerTemplate"];
auto player_info = ResourceManager::getTemplateById(_name, sceneGame, p_Template);
auto player_info = ResourceManager::GetInstance().getTemplateById(_name, sceneGame, p_Template);

loadComponents(registry, player_info.value_or(nullptr), newEntity);
ResourceManager::UnlockInstance();

registry.registerComponent<Engine::ECS::Components::Common::NetworkIp>();
registry.add_component<Engine::ECS::Components::Common::NetworkIp>(
Expand Down
51 changes: 12 additions & 39 deletions Flakkari/Server/Game/ResourceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,43 +11,7 @@

namespace Flakkari {

std::shared_ptr<ResourceManager> ResourceManager::_instance = nullptr;
std::mutex ResourceManager::_mutex;

std::shared_ptr<ResourceManager> ResourceManager::getInstance()
{
std::lock_guard<std::mutex> lock(_mutex);
if (_instance == nullptr)
_instance = std::make_shared<ResourceManager>();
return _instance;
}

void ResourceManager::addScene(std::shared_ptr<nlohmann::json> config, const std::string &scene)
{
std::lock_guard<std::mutex> lock(_mutex);
if (_instance == nullptr)
_instance = std::make_shared<ResourceManager>();
_instance->loadConfig(config, scene);
}

void ResourceManager::deleteScene(const std::string &game, const std::string &scene)
{
std::lock_guard<std::mutex> lock(_mutex);
if (_instance == nullptr)
return;
_instance->_templates[game].erase(scene);
}

std::optional<ResourceManager::nl_template>
ResourceManager::getTemplateById(const std::string &game, const std::string &scene, const std::string &templateId)
{
std::lock_guard<std::mutex> lock(_mutex);
if (_instance == nullptr)
return std::nullopt;
return _instance->_templates[game][scene][templateId];
}

void ResourceManager::loadConfig(std::shared_ptr<nlohmann::json> config, const std::string &scene)
{
for (auto &_scene : (*config)["scenes"].items())
{
Expand All @@ -59,12 +23,21 @@ void ResourceManager::loadConfig(std::shared_ptr<nlohmann::json> config, const s
for (auto &template_ : sceneInfo.value()["templates"].items())
{
for (auto &templateInfo : template_.value().items())
{
_templates[(*config)["title"]][sceneInfo.key()][templateInfo.key()] = templateInfo.value();
}
}
}
}
}

} // namespace Flakkari
void ResourceManager::deleteScene(const std::string &game, const std::string &scene)
{
_templates[game].erase(scene);
}

std::optional<ResourceManager::nl_template> ResourceManager::getTemplateById (
const std::string &game, const std::string &scene, const std::string &templateId
) {
return _templates[game][scene][templateId];
}

} // namespace Engine::Resource
90 changes: 37 additions & 53 deletions Flakkari/Server/Game/ResourceManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
#ifndef RESOURCEMANAGER_HPP_
#define RESOURCEMANAGER_HPP_

#define SINGLETON_IMPLEMENTATION
#include <Singleton.hpp>
#include <nlohmann/json.hpp>
#include <fstream>
#include <iostream>
#include <map>
Expand Down Expand Up @@ -47,63 +50,44 @@ namespace Flakkari {
* TODO: add a way to unload resources
* TODO: add a way to load multiple config files cause multiple scenes could be used for multiple windows
*/
class ResourceManager {
class ResourceManager : public Singleton<ResourceManager> {
private:
static std::shared_ptr<ResourceManager> _instance;
static std::mutex _mutex;

using nl_template = nlohmann::json;

public:
std::map<std::string /*game*/, std::map<std::string /*scene*/, std::map<std::string /*template*/, nl_template>>>
_templates;
using nl_template = nlohmann::json;

ResourceManager() = default;
~ResourceManager() = default;
private:
std::map<std::string/*game*/, std::map<std::string/*scene*/, std::map<std::string/*template*/, nl_template>>> _templates;

public:
ResourceManager(const ResourceManager &) = delete;
ResourceManager(const std::shared_ptr<ResourceManager> &) = delete;
void operator=(const ResourceManager &) = delete;
void operator=(const std::shared_ptr<ResourceManager> &) = delete;

/**
* @brief Get the ResourceManager instance
*
* @param configPath The path to the config file
* @return ResourceManager & The ResourceManager instance
*/
static std::shared_ptr<ResourceManager> getInstance();

/**
* @brief Add a scene to the ResourceManager instance
*
* @param configPath The path to the config file of the game to add
* @param scene The scene to add to the ResourceManager instance
*/
static void addScene(std::shared_ptr<nlohmann::json> config, const std::string &scene);

/**
* @brief Delete a scene from the ResourceManager instance
*
* @param configPath The path to the config file of the game to delete
* @param scene The scene to delete from the ResourceManager instance
*/
static void deleteScene(const std::string &game, const std::string &scene);

/**
* @brief Get the Template By Id object from the config file of the game
*
* @param game The game to get the template from (name of the file in Games/ folder)
* @param scene The scene to get the template from (name of the file in Games/<game>/Scenes/ folder)
* @param templateId The id of the template to get (name of the template in the config file)
* @return std::optional<nl_template> The template if found, std::nullopt otherwise
*/
[[nodiscard]] static std::optional<nl_template> getTemplateById(const std::string &game, const std::string &scene,
const std::string &templateId);

private:
void loadConfig(std::shared_ptr<nlohmann::json> config, const std::string &scene);
explicit ResourceManager() = default;
~ResourceManager() = default;

/**
* @brief Add a scene to the ResourceManager instance
*
* @param configPath The path to the config file of the game to add
* @param scene The scene to add to the ResourceManager instance
*/
void addScene(std::shared_ptr<nlohmann::json> config, const std::string &scene);

/**
* @brief Delete a scene from the ResourceManager instance
*
* @param configPath The path to the config file of the game to delete
* @param scene The scene to delete from the ResourceManager instance
*/
void deleteScene(const std::string &game, const std::string &scene);

/**
* @brief Get the Template By Id object from the config file of the game
*
* @param game The game to get the template from (name of the file in Games/ folder)
* @param scene The scene to get the template from (name of the file in Games/<game>/Scenes/ folder)
* @param templateId The id of the template to get (name of the template in the config file)
* @return std::optional<nl_template> The template if found, std::nullopt otherwise
*/
[[nodiscard]] std::optional<nl_template> getTemplateById (
const std::string &game, const std::string &scene, const std::string &templateId
);
};

} /* namespace Flakkari */
Expand Down
2 changes: 2 additions & 0 deletions Flakkari/Server/UDPServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ UDPServer::UDPServer(std::string ip, unsigned short port)
_io->addSocket(STDIN_FILENO);

ClientManager::CreateInstance(_socket);
ResourceManager::CreateInstance();
}

UDPServer::~UDPServer()
{
ClientManager::DestroyInstance();
ResourceManager::DestroyInstance();
Network::cleanup();
FLAKKARI_LOG_INFO("UDPServer is now stopped");
}
Expand Down

0 comments on commit 90eb2bb

Please sign in to comment.