Skip to content

Commit

Permalink
feat(client): add new global managers
Browse files Browse the repository at this point in the history
Was added locale, player and configuration managers.
  • Loading branch information
VladimirBalun committed Jul 7, 2019
1 parent 5f055dc commit ffe0b18
Show file tree
Hide file tree
Showing 22 changed files with 592 additions and 45 deletions.
8 changes: 8 additions & 0 deletions ClientSide/Resources/Configuration.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0"?>
<configuration>
<language data="English"/>
<server address="127.0.0.1" port="1001"/>
<locales filename="Locales.xls"/>
<player filename="Player.xml"/>
<resources filename=""/>
</configuration>
Binary file added ClientSide/Resources/Locales.xls
Binary file not shown.
2 changes: 2 additions & 0 deletions ClientSide/Resources/Player.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.1" encoding="UTF-8"?>
<player email="[email protected]" password="difficult_pass"/>
Binary file removed ClientSide/Resources/Strings.xlsx
Binary file not shown.
25 changes: 25 additions & 0 deletions ClientSide/Sources/Core/Managers.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright 2018 Vladimir Balun
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

#include "Managers/LocaleManager.hpp"
#include "Managers/PlayerManager.hpp"
#include "Managers/ConfigurationManager.hpp"

#define g_locale_manager Core::Managers::LocaleManager::getInstance()
#define g_player_manager Core::Managers::PlayerManager::getInstance()
#define g_configuration_manager Core::Managers::ConfigurationManager::getInstance()
132 changes: 132 additions & 0 deletions ClientSide/Sources/Core/Managers/ConfigurationManager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/*
* Copyright 2018 Vladimir Balun
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "ConfigurationManager.hpp"

#include <filesystem>
#include <boost/foreach.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>

#include "../../EventSystem.hpp"

Core::Managers::ConfigurationManager& Core::Managers::ConfigurationManager::getInstance() noexcept
{
static ConfigurationManager instance;
return instance;
}

void Core::Managers::ConfigurationManager::initialize()
{
const std::string configuration_filename = "Configuration.xml";
const std::string configuration_file_full_path = getResourcesPath() + configuration_filename;

try
{
boost::property_tree::ptree xml_configuration{};
boost::property_tree::read_xml(configuration_file_full_path, xml_configuration);
BOOST_FOREACH(const auto& xml_data, xml_configuration.get_child("configuration"))
{
if (xml_data.first == "server")
{
m_server_port = xml_data.second.get<std::uint16_t>("<xmlattr>.port", 0u);
m_server_address = xml_data.second.get<std::string>("<xmlattr>.address", "");
}
if (xml_data.first == "locales")
{
m_locales_file_configuration_path = xml_data.second.get<std::string>("<xmlattr>.filename", "");;
}
if (xml_data.first == "player")
{
m_player_file_configuration_path = xml_data.second.get<std::string>("<xmlattr>.filename", "");;
}
if (xml_data.first == "resources")
{
m_resources_file_configuration_path = xml_data.second.get<std::string>("<xmlattr>.filename", "");;
}
if (xml_data.first == "language")
{
m_current_language = xml_data.second.get<std::string>("<xmlattr>.data", "");;
}
}
}
catch (const boost::property_tree::xml_parser_error&)
{
NOTIFY_EVENT(GLOBAL_ERROR_EVENT_TYPE, "Global configuration was not loaded.");
}

if (!isInitialized())
{
NOTIFY_EVENT(GLOBAL_ERROR_EVENT_TYPE, "Global configuration was not loaded.");
}
}

bool Core::Managers::ConfigurationManager::isInitialized() const noexcept
{
return m_server_port != 0u
&& !m_server_address.empty()
&& !m_locales_file_configuration_path.empty()
&& !m_player_file_configuration_path.empty()
&& !m_resources_file_configuration_path.empty();
}

std::string Core::Managers::ConfigurationManager::getCurrentLanguage() const noexcept
{
return m_current_language;
}

std::uint16_t Core::Managers::ConfigurationManager::getServerPort() const noexcept
{
return m_server_port;
}

std::string Core::Managers::ConfigurationManager::getServerAddress() const noexcept
{
return m_server_address;
}

std::string Core::Managers::ConfigurationManager::getModelsPath() const noexcept
{
static const std::string models_path = getResourcesPath() + "/Models/";
return models_path;
}

std::string Core::Managers::ConfigurationManager::getShadersPath() const noexcept
{
static const std::string shaders_path = getResourcesPath() + "/Shaders/";
return shaders_path;
}

std::string Core::Managers::ConfigurationManager::getResourcesPath() const noexcept
{
static const std::string resources_path = (std::filesystem::current_path().parent_path() / "Resources" / "").string();
return resources_path;
}

std::string Core::Managers::ConfigurationManager::getLocalesConfigurationFilename() const noexcept
{
return m_locales_file_configuration_path;
}

std::string Core::Managers::ConfigurationManager::getPlayerConfigurationFilename() const noexcept
{
return m_player_file_configuration_path;
}

std::string Core::Managers::ConfigurationManager::getResourcesConfigurationFilename() const noexcept
{
return m_resources_file_configuration_path;
}
60 changes: 60 additions & 0 deletions ClientSide/Sources/Core/Managers/ConfigurationManager.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright 2018 Vladimir Balun
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

#include <string>
#include <cstdint>

#include "IManager.hpp"

#ifndef g_configuration_manager
#define g_configuration_manager Core::Managers::ConfigurationManager::getInstance()
#endif // g_configuration_manager

namespace Core { namespace Managers {

// Singleton
class ConfigurationManager : public IManager<ConfigurationManager>
{
public:
static ConfigurationManager& getInstance() noexcept;
public:
void initialize();
bool isInitialized() const noexcept;
std::string getCurrentLanguage() const noexcept;
std::uint16_t getServerPort() const noexcept;
std::string getServerAddress() const noexcept;
std::string getModelsPath() const noexcept;
std::string getShadersPath() const noexcept;
std::string getResourcesPath() const noexcept;
std::string getLocalesConfigurationFilename() const noexcept;
std::string getPlayerConfigurationFilename() const noexcept;
std::string getResourcesConfigurationFilename() const noexcept;
private:
ConfigurationManager() noexcept = default;
explicit ConfigurationManager(const ConfigurationManager& other) noexcept = delete;
ConfigurationManager& operator = (const ConfigurationManager& other) noexcept = delete;
private:
std::string m_current_language{};
std::uint16_t m_server_port = 0u;
std::string m_server_address{};
std::string m_player_file_configuration_path{};
std::string m_locales_file_configuration_path{};
std::string m_resources_file_configuration_path{};
};

}}
41 changes: 41 additions & 0 deletions ClientSide/Sources/Core/Managers/IManager.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2018 Vladimir Balun
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

namespace Core { namespace Managers {

// CRTP Interface
template<class T>
struct IManager
{
void initialize();
bool isInitialized() const noexcept;
};

template <class T>
void IManager<T>::initialize()
{
static_cast<T*>(this)->initialize();
}

template <class T>
bool IManager<T>::isInitialized() const noexcept
{
return static_cast<T*>(this)->isInitialized();
}

}}
92 changes: 92 additions & 0 deletions ClientSide/Sources/Core/Managers/LocaleManager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* Copyright 2018 Vladimir Balun
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "LocaleManager.hpp"

#include "ConfigurationManager.hpp"
#include "../../EventSystem.hpp"

Core::Managers::LocaleManager& Core::Managers::LocaleManager::getInstance() noexcept
{
static LocaleManager instance{};
return instance;
}

void Core::Managers::LocaleManager::initialize()
{
if (libxl::Book* book = xlCreateBook())
{
const std::string resources_path = g_configuration_manager.getResourcesPath();
const std::string locales_config_filename = g_configuration_manager.getLocalesConfigurationFilename();
const std::string locales_config_file_full_path = resources_path + locales_config_filename;
if (book->load(locales_config_file_full_path.c_str()))
{
if (libxl::Sheet* sheet = book->getSheet(0))
{
int key_index_cow = 0;
int data_index_cow = 0;
findNecessaryColIndexesInSheet(sheet, key_index_cow, data_index_cow);
readAllStringFromSheet(sheet, key_index_cow, data_index_cow);
}
}
book->release();
}

if (!isInitialized())
{
NOTIFY_EVENT(GLOBAL_ERROR_EVENT_TYPE, "Locale configuration was not loaded.");
}
}

void Core::Managers::LocaleManager::findNecessaryColIndexesInSheet(libxl::Sheet* sheet, int& key_index, int& data_index) const noexcept
{
const int header_row = 0;
const std::string key_header_name = "Key";
const std::string data_header_name = g_configuration_manager.getCurrentLanguage();
for (int col = sheet->firstCol(); col < sheet->lastCol(); ++col)
{
const std::string data = sheet->readStr(header_row, col);
if (data == key_header_name)
{
key_index = col;
}
if (data == data_header_name)
{
data_index = col;
}
}
}

void Core::Managers::LocaleManager::readAllStringFromSheet(libxl::Sheet* sheet, const int key_index, const int data_index) noexcept
{
for (int row = sheet->firstRow(); row < sheet->lastRow(); ++row)
{
const std::string key = sheet->readStr(row, key_index);
const std::string data = sheet->readStr(row, data_index);
m_strings.emplace(key, data);
}
}

bool Core::Managers::LocaleManager::isInitialized() const noexcept
{
return !m_strings.empty();
}

std::string Core::Managers::LocaleManager::getString(const std::string& key) const noexcept
{
const auto& it = m_strings.find(key);
return (it != end(m_strings)) ? (it->second) : ("");
}
Loading

0 comments on commit ffe0b18

Please sign in to comment.