mirror of
https://github.com/alterware/iw4-validator.git
synced 2025-06-27 14:51:49 +00:00
init
This commit is contained in:
248
src/component/console.cpp
Normal file
248
src/component/console.cpp
Normal file
@ -0,0 +1,248 @@
|
||||
#include "std_include.hpp"
|
||||
#include "console.hpp"
|
||||
|
||||
#ifdef _WIN32
|
||||
#define COLOR_LOG_INFO 11
|
||||
#define COLOR_LOG_WARN 14
|
||||
#define COLOR_LOG_ERROR 12
|
||||
#define COLOR_LOG_DEBUG 15
|
||||
#else
|
||||
#define COLOR_LOG_INFO "\033[0;36m"
|
||||
#define COLOR_LOG_WARN "\033[0;33m"
|
||||
#define COLOR_LOG_ERROR "\033[0;31m"
|
||||
#define COLOR_LOG_DEBUG "\033[0m"
|
||||
#endif
|
||||
|
||||
namespace console
|
||||
{
|
||||
namespace
|
||||
{
|
||||
std::mutex signal_mutex;
|
||||
std::function<void()> signal_callback;
|
||||
|
||||
#ifdef _WIN32
|
||||
#define COLOR(win, posix) win
|
||||
using color_type = WORD;
|
||||
#else
|
||||
#define COLOR(win, posix) posix
|
||||
using color_type = const char*;
|
||||
#endif
|
||||
|
||||
const color_type color_array[] =
|
||||
{
|
||||
COLOR(0x8, "\033[0;90m"), // 0 - black
|
||||
COLOR(0xC, "\033[0;91m"), // 1 - red
|
||||
COLOR(0xA, "\033[0;92m"), // 2 - green
|
||||
COLOR(0xE, "\033[0;93m"), // 3 - yellow
|
||||
COLOR(0x9, "\033[0;94m"), // 4 - blue
|
||||
COLOR(0xB, "\033[0;96m"), // 5 - cyan
|
||||
COLOR(0xD, "\033[0;95m"), // 6 - pink
|
||||
COLOR(0xF, "\033[0;97m"), // 7 - white
|
||||
};
|
||||
|
||||
#ifdef _WIN32
|
||||
BOOL WINAPI handler(const DWORD signal)
|
||||
{
|
||||
if (signal == CTRL_C_EVENT && signal_callback)
|
||||
{
|
||||
signal_callback();
|
||||
}
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
#else
|
||||
void handler(int signal)
|
||||
{
|
||||
if (signal == SIGINT && signal_callback)
|
||||
{
|
||||
signal_callback();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
std::string format(va_list* ap, const char* message)
|
||||
{
|
||||
static thread_local char buffer[0x1000];
|
||||
|
||||
#ifdef _WIN32
|
||||
const int count = vsnprintf_s(buffer, _TRUNCATE, message, *ap);
|
||||
#else
|
||||
const int count = vsnprintf(buffer, sizeof(buffer), message, *ap);
|
||||
#endif
|
||||
|
||||
if (count < 0) return {};
|
||||
return {buffer, static_cast<size_t>(count)};
|
||||
}
|
||||
|
||||
#ifdef _WIN32
|
||||
HANDLE get_console_handle()
|
||||
{
|
||||
return GetStdHandle(STD_OUTPUT_HANDLE);
|
||||
}
|
||||
#endif
|
||||
|
||||
void set_color(const color_type color)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
SetConsoleTextAttribute(get_console_handle(), color);
|
||||
#else
|
||||
printf("%s", color);
|
||||
#endif
|
||||
}
|
||||
|
||||
bool apply_color(const std::string& data, const size_t index, const color_type base_color)
|
||||
{
|
||||
if (data[index] != '^' || (index + 1) >= data.size())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
auto code = data[index + 1] - '0';
|
||||
if (code < 0 || code > 11)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
code = std::min(code, 7); // Everything above white is white
|
||||
if (code == 7)
|
||||
{
|
||||
set_color(base_color);
|
||||
}
|
||||
else
|
||||
{
|
||||
set_color(color_array[code]);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void print_colored(const std::string& line, const color_type base_color)
|
||||
{
|
||||
lock _{};
|
||||
set_color(base_color);
|
||||
|
||||
for (std::size_t i = 0; i < line.size(); ++i)
|
||||
{
|
||||
if (apply_color(line, i, base_color))
|
||||
{
|
||||
++i;
|
||||
continue;
|
||||
}
|
||||
|
||||
std::putchar(line[i]);
|
||||
}
|
||||
|
||||
reset_color();
|
||||
}
|
||||
}
|
||||
|
||||
lock::lock()
|
||||
{
|
||||
#ifdef _WIN32
|
||||
_lock_file(stdout);
|
||||
#else
|
||||
flockfile(stdout);
|
||||
#endif
|
||||
}
|
||||
|
||||
lock::~lock()
|
||||
{
|
||||
#ifdef _WIN32
|
||||
_unlock_file(stdout);
|
||||
#else
|
||||
funlockfile(stdout);
|
||||
#endif
|
||||
}
|
||||
|
||||
void reset_color()
|
||||
{
|
||||
lock _{};
|
||||
#ifdef _WIN32
|
||||
SetConsoleTextAttribute(get_console_handle(), 7);
|
||||
#else
|
||||
printf("\033[0m");
|
||||
#endif
|
||||
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
void info(const char* message, ...)
|
||||
{
|
||||
va_list ap;
|
||||
va_start(ap, message);
|
||||
|
||||
const auto data = format(&ap, message);
|
||||
print_colored("[+] " + data + "\n", COLOR_LOG_INFO);
|
||||
|
||||
va_end(ap);
|
||||
}
|
||||
|
||||
void warn(const char* message, ...)
|
||||
{
|
||||
va_list ap;
|
||||
va_start(ap, message);
|
||||
|
||||
const auto data = format(&ap, message);
|
||||
print_colored("[!] " + data + "\n", COLOR_LOG_WARN);
|
||||
|
||||
va_end(ap);
|
||||
}
|
||||
|
||||
void error(const char* message, ...)
|
||||
{
|
||||
va_list ap;
|
||||
va_start(ap, message);
|
||||
|
||||
const auto data = format(&ap, message);
|
||||
print_colored("[-] " + data + "\n", COLOR_LOG_ERROR);
|
||||
|
||||
va_end(ap);
|
||||
}
|
||||
|
||||
void log(const char* message, ...)
|
||||
{
|
||||
va_list ap;
|
||||
va_start(ap, message);
|
||||
|
||||
const auto data = format(&ap, message);
|
||||
print_colored("[*] " + data + "\n", COLOR_LOG_DEBUG);
|
||||
|
||||
va_end(ap);
|
||||
}
|
||||
|
||||
void set_title(const std::string& title)
|
||||
{
|
||||
lock _{};
|
||||
|
||||
#ifdef _WIN32
|
||||
SetConsoleTitleA(title.data());
|
||||
#else
|
||||
printf("\033]0;%s\007", title.data());
|
||||
fflush(stdout);
|
||||
#endif
|
||||
}
|
||||
|
||||
signal_handler::signal_handler(std::function<void()> callback)
|
||||
: std::lock_guard<std::mutex>(signal_mutex)
|
||||
{
|
||||
signal_callback = std::move(callback);
|
||||
|
||||
#ifdef _WIN32
|
||||
SetConsoleCtrlHandler(handler, TRUE);
|
||||
#else
|
||||
signal(SIGINT, handler);
|
||||
#endif
|
||||
}
|
||||
|
||||
signal_handler::~signal_handler()
|
||||
{
|
||||
#ifdef _WIN32
|
||||
SetConsoleCtrlHandler(handler, FALSE);
|
||||
#else
|
||||
signal(SIGINT, SIG_DFL);
|
||||
#endif
|
||||
|
||||
signal_callback = {};
|
||||
}
|
||||
}
|
32
src/component/console.hpp
Normal file
32
src/component/console.hpp
Normal file
@ -0,0 +1,32 @@
|
||||
#pragma once
|
||||
|
||||
namespace console
|
||||
{
|
||||
class lock
|
||||
{
|
||||
public:
|
||||
lock();
|
||||
~lock();
|
||||
|
||||
lock(lock&&) = delete;
|
||||
lock(const lock&) = delete;
|
||||
lock& operator=(lock&&) = delete;
|
||||
lock& operator=(const lock&) = delete;
|
||||
};
|
||||
|
||||
void reset_color();
|
||||
|
||||
void info(const char* message, ...);
|
||||
void warn(const char* message, ...);
|
||||
void error(const char* message, ...);
|
||||
void log(const char* message, ...);
|
||||
|
||||
void set_title(const std::string& title);
|
||||
|
||||
class signal_handler : std::lock_guard<std::mutex>
|
||||
{
|
||||
public:
|
||||
signal_handler(std::function<void()> callback);
|
||||
~signal_handler();
|
||||
};
|
||||
}
|
72
src/component/map_rotation.cpp
Normal file
72
src/component/map_rotation.cpp
Normal file
@ -0,0 +1,72 @@
|
||||
#include <std_include.hpp>
|
||||
|
||||
#include <utils/string.hpp>
|
||||
|
||||
#include "map_rotation.hpp"
|
||||
|
||||
using namespace std::literals;
|
||||
|
||||
namespace map_rotation
|
||||
{
|
||||
rotation_data::rotation_data()
|
||||
: index_(0)
|
||||
{
|
||||
}
|
||||
|
||||
void rotation_data::randomize()
|
||||
{
|
||||
std::random_device rd;
|
||||
std::mt19937 gen(rd());
|
||||
|
||||
std::ranges::shuffle(this->rotation_entries_, gen);
|
||||
}
|
||||
|
||||
void rotation_data::add_entry(const std::string& key, const std::string& value)
|
||||
{
|
||||
this->rotation_entries_.emplace_back(std::make_pair(key, value));
|
||||
}
|
||||
|
||||
bool rotation_data::contains(const std::string& key, const std::string& value) const
|
||||
{
|
||||
return std::ranges::any_of(this->rotation_entries_, [&](const auto& entry)
|
||||
{
|
||||
return entry.first == key && entry.second == value;
|
||||
});
|
||||
}
|
||||
|
||||
bool rotation_data::empty() const noexcept
|
||||
{
|
||||
return this->rotation_entries_.empty();
|
||||
}
|
||||
|
||||
std::size_t rotation_data::get_entries_size() const noexcept
|
||||
{
|
||||
return this->rotation_entries_.size();
|
||||
}
|
||||
|
||||
rotation_data::rotation_entry& rotation_data::get_next_entry()
|
||||
{
|
||||
const auto index = this->index_;
|
||||
++this->index_ %= this->rotation_entries_.size();
|
||||
return this->rotation_entries_.at(index);
|
||||
}
|
||||
|
||||
void rotation_data::parse(const std::string& data)
|
||||
{
|
||||
const auto tokens = utils::string::split(data, ' ');
|
||||
for (std::size_t i = 0; !tokens.empty() && i < (tokens.size() - 1); i += 2)
|
||||
{
|
||||
const auto& key = tokens[i];
|
||||
const auto& value = tokens[i + 1];
|
||||
|
||||
if (key == "map"s || key == "gametype"s)
|
||||
{
|
||||
this->add_entry(key, value);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw map_rotation_parse_error();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
32
src/component/map_rotation.hpp
Normal file
32
src/component/map_rotation.hpp
Normal file
@ -0,0 +1,32 @@
|
||||
#pragma once
|
||||
|
||||
namespace map_rotation
|
||||
{
|
||||
struct map_rotation_parse_error : public std::exception
|
||||
{
|
||||
[[nodiscard]] const char* what() const noexcept override { return "Map Rotation Parse Error"; }
|
||||
};
|
||||
|
||||
class rotation_data
|
||||
{
|
||||
public:
|
||||
using rotation_entry = std::pair<std::string, std::string>;
|
||||
|
||||
rotation_data();
|
||||
|
||||
void randomize();
|
||||
|
||||
void add_entry(const std::string& key, const std::string& value);
|
||||
|
||||
[[nodiscard]] bool contains(const std::string& key, const std::string& value) const;
|
||||
[[nodiscard]] bool empty() const noexcept;
|
||||
[[nodiscard]] std::size_t get_entries_size() const noexcept;
|
||||
[[nodiscard]] rotation_entry& get_next_entry();
|
||||
|
||||
void parse(const std::string& data);
|
||||
|
||||
private:
|
||||
std::vector<rotation_entry> rotation_entries_;
|
||||
std::size_t index_;
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user