Skip to content

Commit

Permalink
fix(Flakkari): game directory setted by FLAKKARY_GAME_DIR env var
Browse files Browse the repository at this point in the history
  • Loading branch information
MasterLaplace committed May 31, 2024
1 parent a4750e8 commit 0415a41
Show file tree
Hide file tree
Showing 11 changed files with 120 additions and 196 deletions.
6 changes: 3 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Flakkari/CMakeLists.txt
cmake_minimum_required(VERSION 3.26.4)
cmake_minimum_required(VERSION 3.22.0)

project(R-Type_Server)

Expand Down Expand Up @@ -117,9 +117,9 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
if(MSVC)
add_compile_options(/W4)
elseif(WIN32)
add_compile_options(-Wall -Wextra -pedantic)
add_compile_options(-Wall -Wextra)
else()
add_compile_options(-Wall -Wextra -pedantic)
add_compile_options(-Wall -Wextra)
endif()

# Create the executable
Expand Down
172 changes: 4 additions & 168 deletions Flakkari/Logger/Logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

using namespace Flakkari;

#if defined(DEBUG) || defined(_DEBUG)
#if defined(DEBUG) || defined(_DEBUG) || !defined(DEBUG)
static Logger::Mode _mode = Logger::Mode::DEBUG;
#else
static Logger::Mode _mode = Logger::Mode::NORMAL;
Expand Down Expand Up @@ -57,7 +57,7 @@ const std::string Logger::fatal_error_message() noexcept
COLOR_RESET
"\nPlease report this error to the developers by opening an issue on GitHub:\n"
COLOR_BRIGHT_CYAN
"\thttps://github.com/MasterLaplace/Flakkari/issues/new?assignees=&labels=bug&projects=&template=bug_report.yml&title=[Bug :: "
"\thttps://github.com/MasterLaplace/Flakkari/issues/new?assignees=&labels=bug&projects=&template=bug_report.yml&title=[Bug%20::%20"
#ifdef __APPLE__
"macOS]"
#elif __linux__
Expand Down Expand Up @@ -92,7 +92,7 @@ const std::string Logger::fatal_error_message() noexcept
#endif
}

void Logger::log(int level, std::string message, std::string file, int line)
void Logger::log(int level, std::string message, int line, std::string file) noexcept
{
if (_mode == Logger::Mode::SILENT && level != LOG_FATAL)
return;
Expand Down Expand Up @@ -174,7 +174,7 @@ void Logger::log(int level, std::string message, std::string file, int line)
std::cout << fatal_error_message() << std::endl;
}

void Logger::log(int level, std::string message)
void Logger::log(int level, std::string message) noexcept
{
if (_mode == Logger::Mode::SILENT && level != LOG_FATAL)
return;
Expand Down Expand Up @@ -255,167 +255,3 @@ void Logger::log(int level, std::string message)
if (level == LOG_FATAL)
std::cout << fatal_error_message() << std::endl;
}

void Logger::log(int level, std::string message, std::string file)
{
if (_mode == Logger::Mode::SILENT && level != LOG_FATAL)
return;

if (_mode == Logger::Mode::NORMAL && level == LOG_DEBUG)
return;

#if !defined(_WIN32) || !defined(_WIN64)
std::string color = COLOR_RESET;
std::string levelStr = "INFO";

switch (level) {
case LOG_INFO:
color = COLOR_CYAN;
levelStr = "INFO";
break;
case LOG_LOG:
color = COLOR_GREEN;
levelStr = "LOG";
break;
case LOG_DEBUG:
color = COLOR_MAGENTA;
levelStr = "DEBUG";
break;
case LOG_WARNING:
color = COLOR_YELLOW;
levelStr = "WARNING";
break;
case LOG_ERROR:
color = COLOR_ORANGE;
levelStr = "ERROR";
break;
case LOG_FATAL:
color = COLOR_BRIGHT_RED;
levelStr = "FATAL";
break;
}

std::cout << get_current_time();
std::cout << color << " [" << levelStr << "] " << message << " (" << file << ")" << COLOR_RESET << std::endl;

#else

std::string levelStr = "INFO";
std::cout << get_current_time();

switch (level) {
case LOG_INFO:
levelStr = "INFO";
setColor(COLOR_CYAN);
break;
case LOG_LOG:
levelStr = "LOG";
setColor(COLOR_GREEN);
break;
case LOG_DEBUG:
levelStr = "DEBUG";
setColor(COLOR_MAGENTA);
break;
case LOG_WARNING:
levelStr = "WARNING";
setColor(COLOR_YELLOW);
break;
case LOG_ERROR:
levelStr = "ERROR";
setColor(COLOR_ORANGE);
break;
case LOG_FATAL:
levelStr = "FATAL";
setColor(COLOR_BRIGHT_RED);
break;
}

std::cout << " [" << levelStr << "] " << message << " (" << file << ")" << std::endl;
setColor(COLOR_RESET);
#endif

if (level == LOG_FATAL)
std::cout << fatal_error_message() << std::endl;
}

void Logger::log(int level, std::string message, int line)
{
if (_mode == Logger::Mode::SILENT && level != LOG_FATAL)
return;

if (_mode == Logger::Mode::NORMAL && level == LOG_DEBUG)
return;

#if !defined(_WIN32) || !defined(_WIN64)
std::string color = COLOR_RESET;
std::string levelStr = "INFO";

switch (level) {
case LOG_INFO:
color = COLOR_CYAN;
levelStr = "INFO";
break;
case LOG_LOG:
color = COLOR_GREEN;
levelStr = "LOG";
break;
case LOG_DEBUG:
color = COLOR_MAGENTA;
levelStr = "DEBUG";
break;
case LOG_WARNING:
color = COLOR_YELLOW;
levelStr = "WARNING";
break;
case LOG_ERROR:
color = COLOR_ORANGE;
levelStr = "ERROR";
break;
case LOG_FATAL:
color = COLOR_BRIGHT_RED;
levelStr = "FATAL";
break;
}

std::cout << get_current_time();
std::cout << color << " [" << levelStr << "] " << message << " (" << line << ")" << COLOR_RESET << std::endl;

#else

std::string levelStr = "INFO";
std::cout << get_current_time();

switch (level) {
case LOG_INFO:
levelStr = "INFO";
setColor(COLOR_CYAN);
break;
case LOG_LOG:
levelStr = "LOG";
setColor(COLOR_GREEN);
break;
case LOG_DEBUG:
levelStr = "DEBUG";
setColor(COLOR_MAGENTA);
break;
case LOG_WARNING:
levelStr = "WARNING";
setColor(COLOR_YELLOW);
break;
case LOG_ERROR:
levelStr = "ERROR";
setColor(COLOR_ORANGE);
break;
case LOG_FATAL:
levelStr = "FATAL";
setColor(COLOR_BRIGHT_RED);
break;
}

std::cout << " [" << levelStr << "] " << message << " (" << line << ")" << std::endl;
setColor(COLOR_RESET);
#endif

if (level == LOG_FATAL)
std::cout << fatal_error_message() << std::endl;
}
8 changes: 3 additions & 5 deletions Flakkari/Logger/Logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#include <chrono>
#include <ctime>

#define FLAKKARI_LOG(level, message) Flakkari::Logger::log(level, message, __FILE__, __LINE__)
#define FLAKKARI_LOG(level, message) Flakkari::Logger::log(level, message, __LINE__, __FILE__)
#define FLAKKARI_LOG_INFO(message) FLAKKARI_LOG(LOG_INFO, message)
#define FLAKKARI_LOG_LOG(message) FLAKKARI_LOG(LOG_LOG, message)
#define FLAKKARI_LOG_DEBUG(message) FLAKKARI_LOG(LOG_DEBUG, message)
Expand Down Expand Up @@ -83,10 +83,8 @@ namespace Flakkari {
static void setMode(Mode mode) noexcept;
static const std::string get_current_time() noexcept;
static const std::string fatal_error_message() noexcept;
static void log(int level, std::string message, std::string file, int line);
static void log(int level, std::string message);
static void log(int level, std::string message, std::string file);
static void log(int level, std::string message, int line);
static void log(int level, std::string message, int line, std::string file = "") noexcept;
static void log(int level, std::string message) noexcept;
};
} /* namespace Flakkari */

Expand Down
16 changes: 8 additions & 8 deletions Flakkari/Protocol/Packet.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ inline namespace V_0 {
return str;
}

friend std::ostream& operator<<(std::ostream& os, const Packet& packet)
friend std::ostream &operator<<(std::ostream &os, const Packet &packet)
{
os << "Packet<Id: "
<< htons(packet.header._commandId)
Expand All @@ -78,7 +78,7 @@ inline namespace V_0 {
void injectComponent(T component)
{
int intValue = (int)component.size();
const byte* dataBytes = reinterpret_cast<const byte*>(&intValue);
const byte *dataBytes = reinterpret_cast<const byte*>(&intValue);
payload.insert(payload.end(), dataBytes, dataBytes + sizeof(intValue));
payload += component;
header._contentLength += payload.size() + sizeof(intValue);
Expand All @@ -90,15 +90,15 @@ inline namespace V_0 {
* @tparam DataType Type of the data to add.
* @param packet The packet to add the data to.
* @param data The data to add.
* @return Packet<Id>& The packet with the data added.
* @return Packet<Id> & The packet with the data added.
*/
template<typename DataType>
friend Packet<Id>& operator<<(Packet<Id>& packet, const DataType& data)
friend Packet<Id> &operator<<(Packet<Id> &packet, const DataType &data)
{
static_assert(std::is_trivially_copyable<DataType>::value, "DataType must be trivially copyable to be used in a packet.");
static_assert(std::is_standard_layout<DataType>::value, "DataType must be standard layout to be used in a packet.");

const byte* dataBytes = reinterpret_cast<const byte*>(&data);
const byte *dataBytes = reinterpret_cast<const byte*>(&data);
packet.payload.insert(packet.payload.end(), dataBytes, dataBytes + sizeof(data));
packet.header._contentLength += packet.payload.size() + sizeof(data);
return packet;
Expand All @@ -110,13 +110,13 @@ inline namespace V_0 {
* @tparam DataType Type of the data to extract.
* @param packet The packet to extract the data from.
* @param data The data to extract.
* @return Packet<Id>& The packet with the data extracted.
* @return Packet<Id> & The packet with the data extracted.
*
* @deprecated This function is deprecated. Don't work with std::string.
* Use the other operator>> instead.
*/
template<typename DataType>
friend Packet<Id>& operator>>(Packet<Id>& packet, DataType& data)
friend Packet<Id> &operator>>(Packet<Id> &packet, DataType &data)
{
static_assert(std::is_trivially_copyable<DataType>::value, "DataType must be trivially copyable to be used in a packet.");
static_assert(std::is_standard_layout<DataType>::value, "DataType must be standard layout to be used in a packet.");
Expand All @@ -133,7 +133,7 @@ inline namespace V_0 {
void injectString(std::string str)
{
int intValue = (int)str.size();
const byte* dataBytes = reinterpret_cast<const byte*>(&intValue);
const byte *dataBytes = reinterpret_cast<const byte*>(&intValue);
payload.insert(payload.end(), dataBytes, dataBytes + sizeof(intValue));
payload += str;
header._contentLength += payload.size() + sizeof(intValue);
Expand Down
2 changes: 0 additions & 2 deletions Flakkari/Server/Client/Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ Client::~Client() {

bool Client::isConnected(float timeout)
{
if (!this)
return false;
if (!_isConnected)
return false;
return std::chrono::duration_cast<std::chrono::seconds>(std::chrono::steady_clock::now() - _lastActivity).count() < timeout;
Expand Down
26 changes: 21 additions & 5 deletions Flakkari/Server/Game/GameManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,25 @@ std::mutex GameManager::_mutex;

GameManager::GameManager()
{
for (const auto & entry : std::filesystem::directory_iterator("../Games")) {
std::string gameName = entry.path().string().substr(9);
#if !defined(_WIN32) && !defined(_WIN64) && !defined( MSVC) && !defined(_MSC_VER)
_game_dir = std::getenv("FLAKKARI_GAME_DIR");
#else
errno_t err = _dupenv_s(&_game_dir.data(), &_game_dir.size(), "FLAKKARI_GAME_DIR");
#endif
if (_game_dir.empty())
FLAKKARI_LOG_FATAL("No game directory set: please set \"FLAKKARI_GAME_DIR\" environment variable");

for (const auto & entry : std::filesystem::directory_iterator(_game_dir))
{
std::string gameName = entry.path().string();
gameName = gameName.substr(std::max(gameName.find_last_of("/") + 1, gameName.find_last_of("\\") + 1));

if (_gamesStore.find(gameName) != _gamesStore.end()) {
FLAKKARI_LOG_ERROR("game already loaded");
continue;
}
std::ifstream configFile("../Games/" + gameName + "/config.cfg");

std::ifstream configFile(_game_dir + "/" + gameName + "/config.cfg");
nlohmann::json config;

if (!configFile.is_open()) {
Expand Down Expand Up @@ -57,9 +69,11 @@ std::shared_ptr<GameManager> GameManager::getInstance()
int GameManager::addGame(std::string gameName)
{
auto &_gamesStore = getInstance()->_gamesStore;
auto _game_dir = getInstance()->_game_dir;

if (_gamesStore.find(gameName) != _gamesStore.end())
return FLAKKARI_LOG_ERROR("game already loaded"), 1;
std::ifstream configFile("../Games/" + gameName + "/config.cfg");
std::ifstream configFile(_game_dir + "/" + gameName + "/config.cfg");
nlohmann::json config;

if (!configFile.is_open())
Expand Down Expand Up @@ -95,9 +109,11 @@ std::vector<std::shared_ptr<Game>> GameManager::getGamesInstances()
int GameManager::updateGame(std::string gameName)
{
auto &_gamesStore = getInstance()->_gamesStore;
auto _game_dir = getInstance()->_game_dir;

if (_gamesStore.find(gameName) == _gamesStore.end())
return FLAKKARI_LOG_ERROR("game not found"), 1;
std::ifstream configFile("../Games/" + gameName + "/config.cfg");
std::ifstream configFile(_game_dir + "/" + gameName + "/config.cfg");
nlohmann::json config;

if (!configFile.is_open())
Expand Down
1 change: 1 addition & 0 deletions Flakkari/Server/Game/GameManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class GameManager {
std::unordered_map<std::string /*gameName*/, std::queue<std::shared_ptr<Client>> /*waitingClients*/> _waitingClients;
std::unordered_map<std::string /*gameName*/, std::vector<std::shared_ptr<Game>> /*gamesInstances*/> _gamesInstances;
std::unordered_map<std::string /*gameName*/, std::shared_ptr<nlohmann::json> /*data*/> _gamesStore;
std::string _game_dir;

public:
GameManager(const GameManager &) = delete;
Expand Down
2 changes: 1 addition & 1 deletion Flakkari/core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
int main(int ac, const char *av[])
{
if (ac != 2)
return FLAKKARI_LOG_FATAL("Usage: ./r-type_server <ip>"), 84;
return FLAKKARI_LOG_ERROR("Usage: ./r-type_server <ip>"), 84;
try {
Flakkari::UDPServer server(av[1], 8080);
server.run();
Expand Down
Loading

0 comments on commit 0415a41

Please sign in to comment.