server: added --world, --simulate, --online, --power and --run command line options

Dieser Commit ist enthalten in:
Reinder Feenstra 2022-10-27 23:46:08 +02:00
Ursprung fc734313f7
Commit e45dac60a8
5 geänderte Dateien mit 73 neuen und 12 gelöschten Zeilen

Datei anzeigen

@ -21,11 +21,20 @@ Command line options available for Traintastic server depend on the used operati
Traintastic server command line options available on all supported operating systems:
| Short | Long | Description |
|-----------|------------------|-------------------------------------|
| `-h` | `--help` | Display help text and exit |
| `-v` | `--version` | Output version information and exit |
| `-D PATH` | `--datadir PATH` | Data directory |
| Short | Long | Description |
|-----------|------------------|------------------------------------------|
| `-h` | `--help` | Display help text and exit |
| `-v` | `--version` | Output version information and exit |
| `-D PATH` | `--datadir PATH` | Data directory |
| `-W UUID` | `--world UUID` | World UUID to load |
| | `--simulate` | Enable simulation after loading world |
| | `--online` | Enable communication after loading world |
| | `--power` | Enable power after loading world |
| | `--run` | Start after loading world |
Note: `--simulate`, `--online`, `--power` and `--run` options only apply to the world loaded at startup.
Note: `--run` option requires `--power`, `--power` option must be set for `--run` to work.
#### Data directory

Datei anzeigen

@ -118,15 +118,19 @@ int main(int argc, char* argv[])
Windows::TrayIcon::add(restart);
#endif
restart = false;
try
{
Traintastic::instance = std::make_shared<Traintastic>(dataDir);
switch(Traintastic::instance->run())
if(!restart) // initial startup
status = Traintastic::instance->run(options.world, options.simulate, options.online, options.power, options.run);
else
status = Traintastic::instance->run();
switch(status)
{
case Traintastic::ExitSuccess:
restart = false;
status = EXIT_SUCCESS;
break;
@ -135,6 +139,7 @@ int main(int argc, char* argv[])
break;
case Traintastic::ExitFailure:
restart = false;
status = EXIT_FAILURE;
break;
}
@ -142,6 +147,7 @@ int main(int argc, char* argv[])
catch(const std::exception& e)
{
std::cerr << e.what() << std::endl;
restart = false;
status = EXIT_FAILURE;
}

Datei anzeigen

@ -39,6 +39,10 @@ struct Options
#endif
std::string dataDir;
std::string world;
bool simulate;
bool online;
bool power;
bool run;
Options(int argc , char* argv[])
{
@ -56,6 +60,11 @@ struct Options
("pidfile,P", boost::program_options::value<std::string>(&pidFile)->value_name("FILENAME")->default_value("")->implicit_value("/run/traintastic-server.pid"), "write pid file")
#endif
("datadir,D", boost::program_options::value<std::string>(&dataDir)->value_name("PATH"), "data directory")
("world,W", boost::program_options::value<std::string>(&world)->value_name("UUID"), "world UUID to load")
("simulate", "enable simulation after loading world")
("online", "enable communication after loading world")
("power", "enable power after loading world")
("run", "start after loading world")
;
boost::program_options::variables_map vm;
@ -66,7 +75,13 @@ struct Options
if(vm.count("help"))
{
std::cout << desc << std::endl;
std::cout
<< desc << std::endl
<< "NOTES:"<< std::endl
<< "1. --simulate, --online, --power and --run options only apply to the world loaded at startup." << std::endl
<< "2. --run option requires --power, --power option must be set for --run to work."
<< std::endl
;
exit(EXIT_SUCCESS);
}
@ -84,6 +99,17 @@ struct Options
daemonize = vm.count("daemonize");
#endif
simulate = vm.count("simulate");
online = vm.count("online");
power = vm.count("power");
run = vm.count("run");
if(!power && run)
{
std::cerr << "Error: --run option requires --power option to be set too." << std::endl;
exit(EXIT_FAILURE);
}
boost::program_options::notify(vm);
}
catch(const boost::program_options::required_option& e)

Datei anzeigen

@ -153,7 +153,7 @@ bool Traintastic::importWorld(const std::vector<std::byte>& worldData)
return false;
}
Traintastic::RunStatus Traintastic::run()
Traintastic::RunStatus Traintastic::run(const std::string& worldUUID, bool simulate, bool online, bool power, bool run)
{
static const std::string boostVersion = std::string("boost ").append(std::to_string(BOOST_VERSION / 100000)).append(".").append(std::to_string(BOOST_VERSION / 100 % 100)).append(".").append(std::to_string(BOOST_VERSION % 100));
Log::log(*this, LogMessage::I1001_TRAINTASTIC_VX, std::string_view{TRAINTASTIC_VERSION_FULL});
@ -169,7 +169,13 @@ Traintastic::RunStatus Traintastic::run()
worldList = std::make_shared<WorldList>(worldDir());
if(settings->loadLastWorldOnStartup && !settings->lastWorld.value().empty())
if(!worldUUID.empty())
{
loadWorld(worldUUID);
if(!world)
return ExitFailure;
}
else if(settings->loadLastWorldOnStartup && !settings->lastWorld.value().empty())
loadWorld(settings->lastWorld.value());
try
@ -182,6 +188,20 @@ Traintastic::RunStatus Traintastic::run()
return ExitFailure;
}
if(world)
{
if(simulate)
world->simulation = true;
if(online)
world->online();
if(power && run)
world->run();
else if(power)
world->powerOn();
}
EventLoop::exec();
return m_restart ? Restart : ExitSuccess;

Datei anzeigen

@ -87,7 +87,7 @@ class Traintastic final : public Object
bool importWorld(const std::vector<std::byte>& worldData);
RunStatus run();
RunStatus run(const std::string& worldUUID = {}, bool simulate = false, bool online = false, bool power = false, bool run = false);
void exit();
};