mirror of
https://github.com/diamante0018/MW3ServerFreezer.git
synced 2025-06-30 16:21:52 +00:00
Refactor console ending this saga
This commit is contained in:
46
src/common/utils/flags.cpp
Normal file
46
src/common/utils/flags.cpp
Normal file
@ -0,0 +1,46 @@
|
||||
#include <string>
|
||||
|
||||
#include "../utils/nt.hpp"
|
||||
#include "../utils/string.hpp"
|
||||
#include "flags.hpp"
|
||||
|
||||
#include <cassert>
|
||||
#include <shellapi.h>
|
||||
|
||||
namespace utils::flags {
|
||||
void parse_flags(std::vector<std::string>& flags) {
|
||||
int num_args;
|
||||
auto* const argv = CommandLineToArgvW(GetCommandLineW(), &num_args);
|
||||
|
||||
assert(flags.empty());
|
||||
|
||||
if (argv == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (auto i = 0; i < num_args; ++i) {
|
||||
std::wstring wide_flag(argv[i]);
|
||||
if (wide_flag[0] == L'-') {
|
||||
wide_flag.erase(wide_flag.begin());
|
||||
const auto converted_string = string::convert(wide_flag);
|
||||
flags.emplace_back(string::to_lower(converted_string));
|
||||
}
|
||||
}
|
||||
|
||||
LocalFree(argv);
|
||||
}
|
||||
|
||||
bool has_flag(const std::string& flag) {
|
||||
static auto parsed = false;
|
||||
static std::vector<std::string> enabled_flags;
|
||||
|
||||
if (!parsed) {
|
||||
parse_flags(enabled_flags);
|
||||
parsed = true;
|
||||
}
|
||||
|
||||
return std::ranges::any_of(
|
||||
enabled_flags.cbegin(), enabled_flags.cend(),
|
||||
[flag](const auto& elem) { return elem == string::to_lower(flag); });
|
||||
}
|
||||
} // namespace utils::flags
|
5
src/common/utils/flags.hpp
Normal file
5
src/common/utils/flags.hpp
Normal file
@ -0,0 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
namespace utils::flags {
|
||||
bool has_flag(const std::string& flag);
|
||||
}
|
Reference in New Issue
Block a user