11 #include <fc_config.h>
14 #include <QCoreApplication>
18 #include <QStandardPaths>
25 #ifdef FREECIV_MSWINDOWS
43 #include "packhand_gen.h"
46 #define WAIT_BETWEEN_TRIES 100000
47 #define NUMBER_OF_TRIES 500
73 QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
74 [=](
int exitCode, QProcess::ExitStatus exitStatus) {
76 qInfo() <<
_(
"Freeciv21 Server") << exitStatus;
181 QStringList arguments;
182 QString trueFcser, storage, port_buf, savesdir, scensdir;
184 int connect_tries = 0;
193 if (storage ==
nullptr) {
195 _(
"Cannot find Freeciv21 storage directory"));
197 ftc_client,
_(
"You'll have to start server manually. Sorry..."));
202 auto uuid = QUuid::createUuid().toString(QUuid::WithoutBraces);
205 savesdir = QStringLiteral(
"%1/saves").arg(storage);
206 scensdir = QStringLiteral(
"%1/scenarios").arg(storage);
208 arguments << QStringLiteral(
"--local") << uuid << QStringLiteral(
"-q")
209 << QStringLiteral(
"1") << QStringLiteral(
"-e")
210 << QStringLiteral(
"--saves") << savesdir
211 << QStringLiteral(
"--scenarios") << scensdir
212 << QStringLiteral(
"-A") << QStringLiteral(
"none");
215 << QStringLiteral(
"--log") <<
logfile;
218 arguments << QStringLiteral(
"--read") <<
scriptfile;
221 arguments << QStringLiteral(
"--file") <<
savefile;
225 const QString server_name = QStringLiteral(
"freeciv21-server");
229 QString location = QStandardPaths::findExecutable(
230 server_name, {QCoreApplication::applicationDirPath()});
231 if (location.isEmpty()) {
233 location = QStandardPaths::findExecutable(server_name);
237 qInfo(
_(
"Starting freeciv21-server at %s"), qUtf8Printable(location));
243 _(
"We probably couldn't start it from here."));
245 _(
"You'll have to start one manually. Sorry..."));
256 url.setScheme(QStringLiteral(
"fc21+local"));
257 url.setUserName(user_name);
267 qCritical(
"Last error from connect attempts: '%s'", buf);
277 qCritical(
"Failed to connect to spawned server!");
279 qDebug(
"Tried with commandline: '%s'",
280 QString(trueFcser + arguments.join(QStringLiteral(
" ")))
286 _(
"We probably couldn't start it from here."));
288 _(
"You'll have to start one manually. Sorry..."));
302 "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
305 for (i = 0; i < n - 1; i++) {
306 str[i] = chars[
fc_rand(
sizeof(chars) - 1)];
321 if (filename[0] !=
'\0') {
322 struct packet_single_want_hack_req req;
326 if (sdir.isEmpty()) {
337 qUtf8Printable(sdir), filename);
345 qCritical(
"Couldn't write token to temporary file: %s",
351 send_packet_single_want_hack_req(&
client.
conn, &req);
370 _(
"Established control over the server. "
371 "You have command access level 'hack'."));
376 _(
"Failed to obtain the required access "
377 "level to take control of the server. "
378 "Attempting to shut down server."));
400 QStringList rulesets;
403 for (i = 0; i < packet->ruleset_count; i++) {
404 QString r = packet->rulesets[i];
int send_chat_printf(const char *format,...)
Send the message as a chat to the server.
int send_chat(const char *message)
Send the message as a chat to the server.
void output_window_append(const struct ft_color color, const char *featured_text)
Add a line of text to the output ("chatline") window, like puts() would do it in the console.
Q_DISABLE_COPY(serverProcess)
static serverProcess * i()
static serverProcess * m_instance
int connect_to_server(const QUrl &url, char *errbuf, int errbufsize)
Connect to a freeciv21-server instance – or at least try to.
bool can_client_access_hack()
Returns TRUE if the client has hack access.
void handle_ruleset_choices(const struct packet_ruleset_choices *packet)
Handle the list of rulesets sent by the server.
void handle_single_want_hack_reply(bool you_have_hack)
Handle response (by the server) if the client has got hack or not.
void send_client_wants_hack(const char *filename)
If the client is capable of 'wanting hack', then the server will send the client a filename in the pa...
bool is_server_running()
The general chain of events:
static bool client_has_hack
void send_save_game(const char *filename)
Send server command to save game.
static void randomize_string(char *str, size_t n)
Generate a random string.
#define WAIT_BETWEEN_TRIES
void set_ruleset(const char *ruleset)
Called by the GUI code when the user sets the ruleset.
bool client_start_server(const QString &user_name)
Forks a server if it can.
void client_kill_server(bool force)
Kills the server if the client has started it.
static char challenge_fullname[MAX_LEN_PATH]
void set_rulesets(int num_rulesets, QStringList rulesets)
Set the list of available rulesets.
const struct ft_color ftc_client
const QString & log_get_level()
Retrieves the log level passed to log_init (even if log_init failed).
#define log_debug(message,...)
struct section_file * secfile_new(bool allow_duplicates)
Create a new empty section file.
void secfile_destroy(struct section_file *secfile)
Free a section file.
bool secfile_save(const struct section_file *secfile, QString filename)
Save the previously filled in section_file to disk.
#define secfile_insert_str(secfile, string, path,...)
bool is_safe_filename(const QString &name)
Check if the name is safe security-wise.
QString freeciv_storage_dir()
Returns string which gives freeciv storage dir.
int fc_snprintf(char *str, size_t n, const char *format,...)
See also fc_utf8_snprintf_trunc(), fc_utf8_snprintf_rep().
void fc_usleep(unsigned long usec)
Suspend execution for the specified number of microseconds.
int fc_remove(const char *filename)
Wrapper function for remove() with filename conversion to local encoding on Windows.