Enable mode selection via arguments

This commit is contained in:
momo5502 2019-01-09 00:02:59 +01:00
parent cb320a198a
commit 5d8507e672
4 changed files with 94 additions and 6 deletions

View File

@ -5,6 +5,7 @@
#include "game/game.hpp" #include "game/game.hpp"
#include "loader/binary_loader.hpp" #include "loader/binary_loader.hpp"
#include "utils/string.hpp" #include "utils/string.hpp"
#include "utils/flags.hpp"
//#define GENERATE_DIFFS //#define GENERATE_DIFFS
@ -37,6 +38,26 @@ void verify_tls()
} }
} }
launcher::mode detect_mode_from_arguments()
{
if (utils::flags::has_flag("dedicated"))
{
return launcher::mode::server;
}
if (utils::flags::has_flag("multiplayer"))
{
return launcher::mode::multiplayer;
}
if (utils::flags::has_flag("singleplayer"))
{
return launcher::mode::singleplayer;
}
return launcher::mode::none;
}
FARPROC load_binary(const launcher::mode mode) FARPROC load_binary(const launcher::mode mode)
{ {
loader loader(mode); loader loader(mode);
@ -64,10 +85,10 @@ int main()
FARPROC entry_point; FARPROC entry_point;
{ {
bool premature_shutdown = true; auto premature_shutdown = true;
const auto _ = gsl::finally( [&premature_shutdown]() const auto _ = gsl::finally([&premature_shutdown]()
{ {
if(premature_shutdown) if (premature_shutdown)
{ {
module_loader::pre_destroy(); module_loader::pre_destroy();
} }
@ -83,9 +104,13 @@ int main()
verify_tls(); verify_tls();
if (!module_loader::post_start()) return 0; if (!module_loader::post_start()) return 0;
launcher launcher; auto mode = detect_mode_from_arguments();
const auto mode = launcher.run(); if (mode == launcher::mode::none)
if (mode == launcher::mode::none) return 0; {
launcher launcher;
mode = launcher.run();
if (mode == launcher::mode::none) return 0;
}
entry_point = load_binary(mode); entry_point = load_binary(mode);
if (!entry_point) if (!entry_point)

View File

@ -16,6 +16,7 @@
#include <Ws2tcpip.h> #include <Ws2tcpip.h>
#include <corecrt_io.h> #include <corecrt_io.h>
#include <fcntl.h> #include <fcntl.h>
#include <shellapi.h>
// min and max is required by gdi, therefore NOMINMAX won't work // min and max is required by gdi, therefore NOMINMAX won't work
#ifdef max #ifdef max

52
src/utils/flags.cpp Normal file
View File

@ -0,0 +1,52 @@
#include <std_include.hpp>
#include "flags.hpp"
#include "string.hpp"
namespace utils
{
namespace flags
{
void parse_flags(std::vector<std::string>& flags)
{
int num_args;
const auto argv = CommandLineToArgvW(GetCommandLineW(), &num_args);
flags.clear();
if (argv)
{
for (auto i = 0; i < num_args; ++i)
{
std::wstring wide_flag(argv[i]);
if (wide_flag[0] == L'-')
{
flags.emplace_back(wide_flag.begin() + 1, wide_flag.end());
}
}
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);
}
for (const auto& entry : enabled_flags)
{
if (string::to_lower(entry) == string::to_lower(flag))
{
return true;
}
}
return false;
}
}
}

10
src/utils/flags.hpp Normal file
View File

@ -0,0 +1,10 @@
#pragma once
#include "memory.hpp"
namespace utils
{
namespace flags
{
bool has_flag(const std::string& flag);
}
}