Freeciv21
Develop your civilization from humble roots to a global empire
ruledit.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 // ANSI
17 #include <cstdlib>
18 
19 #ifdef FREECIV_MSWINDOWS
20 #include <windows.h>
21 #endif
22 
23 // Qt
24 #include <QApplication>
25 #include <QCommandLineParser>
26 
27 // utility
28 #include "fciconv.h"
29 #include "fcintl.h"
30 #include "log.h"
31 #include "version.h"
32 
33 // common
34 #include "fc_interface.h"
35 #include "game.h"
36 
37 // server
38 #include "sernet.h"
39 #include "settings.h"
40 
41 /* tools/shared */
42 #include "tools_fc_interface.h"
43 
44 // ruledit
45 #include "comments.h"
46 #include "ruledit_qt.h"
47 
48 #include "ruledit.h"
49 
50 static void re_parse_cmdline(const QCoreApplication &app);
51 
53 
57 int main(int argc, char **argv)
58 {
59  QApplication app(argc, argv);
60  QCoreApplication::setApplicationVersion(freeciv21_version());
61  app.setDesktopFileName(QStringLiteral("net.longturn.freeciv21.ruledit"));
62 
63  // Load window icons
64  QIcon::setThemeSearchPaths(get_data_dirs() + QIcon::themeSearchPaths());
65  QIcon::setFallbackThemeName(QIcon::themeName());
66  QIcon::setThemeName(QStringLiteral("icons"));
67 
68  qApp->setWindowIcon(QIcon::fromTheme(QStringLiteral("freeciv21-ruledit")));
69 
70  log_init();
71 
72  init_nls();
73 
74 #ifdef ENABLE_NLS
75  (void) bindtextdomain("freeciv21-ruledit", get_locale_dir());
76 #endif
77 
79 #ifdef ENABLE_NLS
80  bind_textdomain_codeset("freeciv21-ruledit", get_internal_encoding());
81 #endif
82 
83  // Initialize command line arguments.
84  re_parse_cmdline(app);
85 
87 
88  settings_init(false);
89 
90  // Reset aifill to zero
91  game.info.aifill = 0;
92 
93  game_init(false);
94  i_am_tool();
95 
96  // Initialize the fc_interface functions needed to understand rules.
98 
99  if (comments_load()) {
100  auto *main = new ruledit_main;
101  new ruledit_gui(main);
102 
103  main->show();
104 
105  QObject::connect(&app, &QCoreApplication::aboutToQuit, main,
106  &QObject::deleteLater);
107 
108  app.exec();
109 
110  comments_free();
111  } else {
112  /* TRANS: 'Failed to load comments-x.y.txt' where x.y is
113  * freeciv version */
114  qCritical(R__("Failed to load %s."), COMMENTS_FILE_NAME);
115  }
116 
117  log_close();
118  free_libfreeciv();
119  free_nls();
120 
121  return EXIT_SUCCESS;
122 }
123 
127 static void re_parse_cmdline(const QCoreApplication &app)
128 {
129  QCommandLineParser parser;
130  parser.addHelpOption();
131  parser.addVersionOption();
132 
133  bool ok = parser.addOptions({
134  {{"F", "Fatal"}, _("Raise a signal on failed assertion")},
135  {{"r", "ruleset"},
136  R__("Ruleset to use as the starting point."),
137  // TRANS: Command-line argument
138  R__("RULESET")},
139  });
140  if (!ok) {
141  qFatal("Adding command line arguments failed");
142  exit(EXIT_FAILURE);
143  }
144 
145  // Parse
146  parser.process(app);
147 
148  // Process the parsed options
149  fc_assert_set_fatal(parser.isSet(QStringLiteral("Fatal")));
150  if (parser.isSet(QStringLiteral("ruleset"))) {
151  if (parser.values(QStringLiteral("ruleset")).size() >= 1) {
152  fc_fprintf(stderr, R__("Can only edit one ruleset at a time.\n"));
153  exit(EXIT_FAILURE);
154  } else {
155  reargs.ruleset = parser.value(QStringLiteral("ruleset"));
156  }
157  }
158 }
159 
163 void show_experimental(QWidget *wdg)
164 {
165 #ifdef RULEDIT_EXPERIMENTAL
166  wdg->show();
167 #else
168  wdg->hide();
169 #endif
170 }
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
const char * get_internal_encoding()
Return the internal encoding.
Definition: fciconv.cpp:90
#define FC_DEFAULT_DATA_ENCODING
Definition: fciconv.h:69
const char * get_locale_dir()
Return directory containing locales.
Definition: fcintl.cpp:94
#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
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
struct ruledit_arguments reargs
Definition: ruledit.cpp:52
int main(int argc, char **argv)
Main entry point for freeciv-ruledit.
Definition: ruledit.cpp:57
void show_experimental(QWidget *wdg)
Show widget if experimental features enabled, hide otherwise.
Definition: ruledit.cpp:163
static void re_parse_cmdline(const QCoreApplication &app)
Parse freeciv-ruledit commandline.
Definition: ruledit.cpp:127
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
const QStringList & get_data_dirs()
Returns a list of data directory paths, in the order in which they should be searched.
Definition: shared.cpp:533
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 packet_game_info info
Definition: game.h:80
QString ruleset
Definition: ruledit.h:20
void fc_interface_init_tool()
Initialize tool specific functions.
const char * freeciv21_version()
Returns the raw version string.
Definition: version.cpp:29