Freeciv21
Develop your civilization from humble roots to a global empire
ruleup.cpp
Go to the documentation of this file.
1 /*__ ___ ***************************************
2 / \ / \ Copyright (c) 1996-2020 Freeciv21 and Freeciv
3 \_ \ / __/ contributors. This file is part of Freeciv21.
4  _\ \ / /__ Freeciv21 is free software: you can redistribute it
5  \___ \____/ __/ and/or modify it under the terms of the GNU General
6  \_ _/ Public License as published by the Free Software
7  | @ @ \_ Foundation, either version 3 of the License,
8  | or (at your option) any later version.
9  _/ /\ You should have received a copy of the GNU
10  /o) (o/\ \_ General Public License along with Freeciv21.
11  \_____/ / If not, see https://www.gnu.org/licenses/.
12  \____/ ********************************************************/
13 
14 #include <fc_config.h>
15 
16 #ifdef FREECIV_MSWINDOWS
17 #include <windows.h>
18 #endif
19 
20 // Qt
21 #include <QCommandLineParser>
22 #include <QCoreApplication>
23 
24 // utility
25 #include "fciconv.h"
26 #include "version.h"
27 
28 // common
29 #include "fc_interface.h"
30 #include "game.h"
31 
32 // server
33 #include "ruleset.h"
34 #include "sernet.h"
35 #include "settings.h"
36 
37 /* tools/shared */
38 #include "tools_fc_interface.h"
39 
40 /* tools/ruleutil */
41 #include "comments.h"
42 #include "rulesave.h"
43 
44 static QString rs_selected;
45 static QString od_selected;
46 
50 static void rup_parse_cmdline(const QCoreApplication &app)
51 {
52  QCommandLineParser parser;
53  parser.addHelpOption();
54  parser.addVersionOption();
55 
56  bool ok = parser.addOptions({
57  {{"F", "Fatal"}, _("Raise a signal on failed assertion")},
58  {{"r", "ruleset"},
59  _("Update RULESET"),
60  // TRANS: Command-line argument
61  _("RULESET")},
62  {{"o", "output"},
63  _("Create directory DIRECTORY for output"),
64  // TRANS: Command-line argument
65  _("DIRECTORY")},
66  });
67  if (!ok) {
68  qFatal("Adding command line arguments failed");
69  exit(EXIT_FAILURE);
70  }
71 
72  // Parse
73  parser.process(app);
74 
75  // Process the parsed options
76  fc_assert_set_fatal(parser.isSet(QStringLiteral("Fatal")));
77  if (parser.isSet(QStringLiteral("ruleset"))) {
78  if (parser.values(QStringLiteral("ruleset")).size() > 1) {
79  fc_fprintf(stderr, _("Multiple rulesets requested. Only one ruleset "
80  "at time supported.\n"));
81  exit(EXIT_FAILURE);
82  } else {
83  rs_selected = parser.value(QStringLiteral("ruleset"));
84  }
85  }
86  if (parser.isSet(QStringLiteral("output"))) {
87  if (parser.values(QStringLiteral("output")).size() > 1) {
88  fc_fprintf(stderr, _("Multiple output directories given.\n"));
89  exit(EXIT_FAILURE);
90  } else {
91  od_selected = parser.value(QStringLiteral("output"));
92  }
93  }
94 }
95 
99 static void conv_log(const char *msg) { qInfo("%s", msg); }
100 
104 int main(int argc, char **argv)
105 {
106  QCoreApplication app(argc, argv);
107  QCoreApplication::setApplicationVersion(freeciv21_version());
108 
109  log_init();
110 
111  init_nls();
112 
114 
115  rup_parse_cmdline(app);
116 
118 
119  settings_init(false);
120 
121  game_init(false);
122  i_am_tool();
123 
124  // Initialize the fc_interface functions needed to understand rules.
126 
127  // Set ruleset user requested to use
128  if (rs_selected == nullptr) {
130  }
131  sz_strlcpy(game.server.rulesetdir, qUtf8Printable(rs_selected));
132 
133  // Reset aifill to zero
134  game.info.aifill = 0;
135 
136  if (load_rulesets(nullptr, nullptr, true, conv_log, false, true, true)) {
137  struct rule_data data;
138  QString tgt_dir;
139 
140  data.nationlist = game.server.ruledit.nationlist;
141 
142  if (!od_selected.isEmpty()) {
143  tgt_dir = od_selected;
144  } else {
145  tgt_dir = rs_selected + ".ruleup";
146  }
147 
148  if (!comments_load()) {
149  /* TRANS: 'Failed to load comments-x.y.txt' where x.y is
150  * freeciv version */
151  qCritical(R__("Failed to load %s."), COMMENTS_FILE_NAME);
152  }
153 
154  save_ruleset(qUtf8Printable(tgt_dir), game.control.name, &data);
155  qInfo("Saved %s", qUtf8Printable(tgt_dir));
156  comments_free();
157  } else {
158  qCritical(_("Can't load ruleset %s"), qUtf8Printable(rs_selected));
159  }
160 
161  log_close();
162  free_libfreeciv();
163  free_nls();
164 
165  return EXIT_SUCCESS;
166 }
bool comments_load()
Load comments to add to the saved rulesets.
Definition: comments.cpp:55
void comments_free()
Free comments.
Definition: comments.cpp:129
#define COMMENTS_FILE_NAME
Definition: comments.h:16
void free_libfreeciv()
Free misc resources allocated for libfreeciv.
void fc_fprintf(FILE *stream, const char *format,...)
Do a fprintf from the internal charset into the local charset.
Definition: fciconv.cpp:132
void init_character_encodings(const char *my_internal_encoding, bool my_use_transliteration)
Must be called during the initialization phase of server and client to initialize the character encod...
Definition: fciconv.cpp:39
#define FC_DEFAULT_DATA_ENCODING
Definition: fciconv.h:69
#define _(String)
Definition: fcintl.h:50
#define R__(String)
Definition: fcintl.h:58
struct civ_game game
Definition: game.cpp:47
void game_init(bool keep_ruleset_value)
Initialise all game settings.
Definition: game.cpp:420
#define GAME_DEFAULT_RULESETDIR
Definition: game.h:630
static void i_am_tool()
Definition: game.h:295
bool log_init(const QString &level_str, const QStringList &extra_rules)
Parses a log level string as provided by the user on the command line, and installs the corresponding...
Definition: log.cpp:55
void fc_assert_set_fatal(bool fatal)
Set what signal the assert* macros should raise on failed assertion (-1 to disable).
Definition: log.cpp:226
void log_close()
Deinitialize logging module.
Definition: log.cpp:212
bool save_ruleset(const char *path, const char *name, struct rule_data *data)
Save ruleset to directory given.
Definition: rulesave.cpp:3013
bool load_rulesets(const char *restore, const char *alt, bool compat_mode, rs_conversion_logger logger, bool act, bool buffer_script, bool load_luadata)
Loads the rulesets.
Definition: ruleset.cpp:8582
int main(int argc, char **argv)
Main entry point for freeciv-ruleup.
Definition: ruleup.cpp:104
static void conv_log(const char *msg)
Conversion log callback.
Definition: ruleup.cpp:99
static QString rs_selected
Definition: ruleup.cpp:44
static QString od_selected
Definition: ruleup.cpp:45
static void rup_parse_cmdline(const QCoreApplication &app)
Parse freeciv-ruleup commandline parameters.
Definition: ruleup.cpp:50
void init_connections()
Initialize connection related stuff.
Definition: sernet.cpp:452
void settings_init(bool act)
Initialize stuff related to this code module.
Definition: settings.cpp:4761
void free_nls()
Free memory allocated by Native Language Support.
Definition: shared.cpp:931
void init_nls()
Setup for Native Language Support, if configured to use it.
Definition: shared.cpp:871
size_t size
Definition: specvec.h:64
struct civ_game::@28::@32 server
struct packet_ruleset_control control
Definition: game.h:74
struct packet_game_info info
Definition: game.h:80
char * nationlist
Definition: rulesave.h:15
#define sz_strlcpy(dest, src)
Definition: support.h:140
void fc_interface_init_tool()
Initialize tool specific functions.
const char * freeciv21_version()
Returns the raw version string.
Definition: version.cpp:29