summaryrefslogtreecommitdiffstats
path: root/src/main.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.cpp')
-rw-r--r--src/main.cpp114
1 files changed, 77 insertions, 37 deletions
diff --git a/src/main.cpp b/src/main.cpp
index 1c34b8f61..5cd057278 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -2,6 +2,7 @@
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
#include "Root.h"
+#include "tclap/CmdLine.h"
#include <exception>
#include <csignal>
@@ -14,7 +15,7 @@
#include "OSSupport/NetworkSingleton.h"
#include "BuildInfo.h"
-
+#include "MemorySettingsRepository.h"
@@ -206,7 +207,7 @@ BOOL CtrlHandler(DWORD fdwCtrlType)
////////////////////////////////////////////////////////////////////////////////
// universalMain - Main startup logic for both standard running and as a service
-void universalMain()
+void universalMain(std::unique_ptr<cSettingsRepositoryInterface> overridesRepo)
{
#ifdef _WIN32
if (!SetConsoleCtrlHandler((PHANDLER_ROUTINE)CtrlHandler, TRUE))
@@ -226,7 +227,7 @@ void universalMain()
#endif
{
cRoot Root;
- Root.Start();
+ Root.Start(std::move(overridesRepo));
}
#if !defined(ANDROID_NDK)
catch (std::exception & e)
@@ -257,7 +258,7 @@ DWORD WINAPI serviceWorkerThread(LPVOID lpParam)
UNREFERENCED_PARAMETER(lpParam);
// Do the normal startup
- universalMain();
+ universalMain(cpp14::make_unique<cMemorySettingsRepository>());
return ERROR_SUCCESS;
}
@@ -363,14 +364,79 @@ void WINAPI serviceMain(DWORD argc, TCHAR *argv[])
+std::unique_ptr<cMemorySettingsRepository> parseArguments(int argc, char **argv)
+{
+ try
+ {
+ TCLAP::CmdLine cmd("MCServer");
+
+ TCLAP::ValueArg<int> slotsArg("s", "max-players", "Maximum number of slots for the server to use, overrides setting in setting.ini", false, -1, "number", cmd);
+
+ TCLAP::MultiArg<int> portsArg("p", "port", "The port number the server should listen to", false, "port", cmd);
+
+ TCLAP::SwitchArg commLogArg("", "log-comm", "Log server client communications to file", cmd);
+
+ TCLAP::SwitchArg commLogInArg("", "log-comm-in", "Log inbound server client communications to file", cmd);
+
+ TCLAP::SwitchArg commLogOutArg("", "log-comm-out", "Log outbound server client communications to file", cmd);
+
+ TCLAP::SwitchArg noBufArg("", "no-output-buffering", "Disable output buffering", cmd);
+
+ cmd.parse(argc, argv);
+
+ auto repo = cpp14::make_unique<cMemorySettingsRepository>();
+
+ if (slotsArg.isSet())
+ {
+
+ int slots = slotsArg.getValue();
+
+ repo->AddValue("Server", "MaxPlayers", static_cast<Int64>(slots));
+
+ }
+
+ if (portsArg.isSet())
+ {
+ std::vector<int> ports = portsArg.getValue();
+ for (auto port : ports)
+ {
+ repo->AddValue("Server", "Port", static_cast<Int64>(port));
+ }
+ }
+
+ if (commLogArg.getValue())
+ {
+ g_ShouldLogCommIn = true;
+ g_ShouldLogCommOut = true;
+ }
+ else
+ {
+ g_ShouldLogCommIn = commLogInArg.getValue();
+ g_ShouldLogCommOut = commLogOutArg.getValue();
+ }
+
+ if (noBufArg.getValue())
+ {
+ setvbuf(stdout, nullptr, _IONBF, 0);
+ }
+
+ repo->SetReadOnly();
+
+ return repo;
+ }
+ catch (TCLAP::ArgException &e)
+ {
+ printf("error reading command line %s for arg %s", e.error().c_str(), e.argId().c_str());
+ return cpp14::make_unique<cMemorySettingsRepository>();
+ }
+}
+
////////////////////////////////////////////////////////////////////////////////
// main:
-int main( int argc, char **argv)
+int main(int argc, char **argv)
{
- UNUSED(argc);
- UNUSED(argv);
#if defined(_MSC_VER) && defined(_DEBUG) && defined(ENABLE_LEAK_FINDER)
InitLeakFinder();
@@ -425,39 +491,13 @@ int main( int argc, char **argv)
// DEBUG: test the dumpfile creation:
// *((int *)0) = 0;
+ auto argsRepo = parseArguments(argc, argv);
+
// Check if comm logging is to be enabled:
for (int i = 0; i < argc; i++)
{
AString Arg(argv[i]);
- if (
- (NoCaseCompare(Arg, "/commlog") == 0) ||
- (NoCaseCompare(Arg, "/logcomm") == 0)
- )
- {
- g_ShouldLogCommIn = true;
- g_ShouldLogCommOut = true;
- }
- else if (
- (NoCaseCompare(Arg, "/commlogin") == 0) ||
- (NoCaseCompare(Arg, "/comminlog") == 0) ||
- (NoCaseCompare(Arg, "/logcommin") == 0)
- )
- {
- g_ShouldLogCommIn = true;
- }
- else if (
- (NoCaseCompare(Arg, "/commlogout") == 0) ||
- (NoCaseCompare(Arg, "/commoutlog") == 0) ||
- (NoCaseCompare(Arg, "/logcommout") == 0)
- )
- {
- g_ShouldLogCommOut = true;
- }
- else if (NoCaseCompare(Arg, "nooutbuf") == 0)
- {
- setvbuf(stdout, nullptr, _IONBF, 0);
- }
- else if (NoCaseCompare(Arg, "/service") == 0)
+ if (NoCaseCompare(Arg, "/service") == 0)
{
cRoot::m_RunAsService = true;
}
@@ -483,7 +523,7 @@ int main( int argc, char **argv)
#endif
{
// Not running as a service, do normal startup
- universalMain();
+ universalMain(std::move(argsRepo));
}
#if defined(_MSC_VER) && defined(_DEBUG) && defined(ENABLE_LEAK_FINDER)