_________ __                 __
        /   _____//  |_____________ _/  |______     ____  __ __  ______
        \_____  \\   __\_  __ \__  \\   __\__  \   / ___\|  |  \/  ___/
        /        \|  |  |  | \// __ \|  |  / __ \_/ /_/  >  |  /\___ \
       /_______  /|__|  |__|  (____  /__| (____  /\___  /|____//____  >
               \/                  \/          \//_____/            \/
    ______________________                           ______________________
                          T H E   W A R   B E G I N S
                   Stratagus - A free fantasy real time strategy game engine

stratagus-game-launcher.h
Go to the documentation of this file.
1 /*
2  _________ __ __
3  / _____// |_____________ _/ |______ ____ __ __ ______
4  \_____ \\ __\_ __ \__ \\ __\__ \ / ___\| | \/ ___/
5  / \| | | | \// __ \| | / __ \_/ /_/ > | /\___ |
6  /_______ /|__| |__| (____ /__| (____ /\___ /|____//____ >
7  \/ \/ \//_____/ \/
8  ______________________ ______________________
9  T H E W A R B E G I N S
10  Stratagus - A free fantasy real time strategy game engine
11 
12  stratagus-game-launcher.h - Stratagus Game Launcher
13  Copyright (C) 2010-2011 Pali Rohár <pali.rohar@gmail.com>
14 
15  This program is free software: you can redistribute it and/or modify
16  it under the terms of the GNU General Public License as published by
17  the Free Software Foundation, either version 2 of the License, or
18  (at your option) any later version.
19 
20  This program is distributed in the hope that it will be useful,
21  but WITHOUT ANY WARRANTY; without even the implied warranty of
22  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23  GNU General Public License for more details.
24 
25  You should have received a copy of the GNU General Public License
26  along with this program. If not, see <http://www.gnu.org/licenses/>.
27 
28 */
29 
106 /* Fake definitions for Doxygen */
107 #ifdef DOXYGEN
108 #define GAME_NAME
109 #define GAME_CD
110 #define GAME
111 #define DATA_PATH
112 #define SCRIPTS_PATH
113 #define STRATAGUS_BIN
114 #endif
115 
116 #if ! defined (GAME_NAME) || ! defined (GAME_CD) || ! defined (GAME)
117 #error You need to define all Game macros, see stratagus-game-launcher.h
118 #endif
119 
120 #if ( defined (_MSC_VER) || defined (_WIN32) || defined (_WIN64) ) && ! defined (WIN32)
121 #define WIN32
122 #endif
123 
124 #ifndef WIN32
125 #if ! defined (DATA_PATH) || ! defined (SCRIPTS_PATH) || ! defined (STRATAGUS_BIN)
126 #error You need to define paths, see stratagus-game-launcher.h
127 #endif
128 #endif
129 
130 #if ( defined (MAEMO_GTK) || defined (MAEMO_CHANGES) ) && ! defined (MAEMO)
131 #define MAEMO
132 #endif
133 
134 #ifdef WIN32
135 #define WINVER 0x0501
136 #include <windows.h>
137 #include <wincon.h>
138 #include <process.h>
139 #include <errno.h>
140 #endif
141 
142 #include <stdio.h>
143 #include <stdlib.h>
144 #include <string.h>
145 
146 #include <sys/stat.h>
147 #include <sys/types.h>
148 
149 #if defined(_MSC_VER) || defined(__MINGW32__)
150 #include <direct.h>
151 #define inline __inline
152 #define chdir _chdir
153 #define getcwd _getcwd
154 #define spawnvp _spawnvp
155 #define stat _stat
156 #endif
157 
158 #ifndef WIN32
159 #include <unistd.h>
160 #include <X11/Xlib.h>
161 #include <gtk/gtk.h>
162 #endif
163 
164 #ifdef MAEMO
165 #include <hildon/hildon.h>
166 #endif
167 
168 #ifdef _WIN64
169 #define REGKEY "Software\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Stratagus (64 bit)"
170 #elif defined (WIN32)
171 #define REGKEY "Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Stratagus"
172 #endif
173 
174 #define TITLE GAME_NAME
175 #define STRATAGUS_NOT_FOUND "Stratagus is not installed.\nYou need Stratagus to run " GAME_NAME "!\nFirst install Stratagus from https://launchpad.net/stratagus"
176 #define DATA_NOT_EXTRACTED GAME_NAME " data was not extracted yet.\nYou need extract data from original " GAME_CD " first!"
177 #define NO_X_DISPLAY "Cannot open X Display"
178 #define CONSOLE_MODE_NOT_ROOT "You must be root to run " GAME_NAME " in console framebuffer mode"
179 
180 #define BUFF_SIZE 1024
181 
182 #ifndef WIN32
183 int ConsoleMode = 0;
184 #endif
185 
186 static void error(char * title, char * text) {
187 
188 #ifdef WIN32
189  MessageBox(NULL, text, title, MB_OK | MB_ICONERROR);
190 #else
191  if ( ! ConsoleMode ) {
192  GtkWidget * window = NULL;
193  GtkWidget * dialog = NULL;
194 
195 #ifdef MAEMO
196  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
197  gtk_window_set_title(GTK_WINDOW(window), title);
198  gtk_widget_show(window);
199 #endif
200 
201  dialog = gtk_message_dialog_new(GTK_WINDOW(window), GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "%s", text);
202  gtk_window_set_title(GTK_WINDOW(dialog), title);
203  gtk_window_set_skip_pager_hint(GTK_WINDOW(dialog), 0);
204  gtk_window_set_skip_taskbar_hint(GTK_WINDOW(dialog), 0);
205  gtk_label_set_selectable(GTK_LABEL(GTK_MESSAGE_DIALOG(dialog)->label), 0);
206  gtk_dialog_run(GTK_DIALOG(dialog));
207  gtk_widget_destroy(dialog);
208 
209 #ifdef MAEMO
210  gtk_widget_destroy(window);
211 #endif
212  } else {
213  fprintf(stderr, "%s -- Error: %s\n", title, text);
214  }
215 #endif
216  exit(1);
217 }
218 
219 int main(int argc, char * argv[]) {
220 
221 #ifndef WIN32
222  if ( ! XOpenDisplay(NULL) ) {
223  ConsoleMode = 1;
224  }
225  if ( ConsoleMode ) {
226 #ifdef MAEMO
228 #else
229  if ( getuid() != 0 ) {
231  }
232 #endif
233  } else {
234  gtk_init(&argc, &argv);
235 #ifdef MAEMO
236  hildon_init();
237 #endif
238  }
239 #endif
240 
241  struct stat st;
242  char data_path[BUFF_SIZE];
243  char scripts_path[BUFF_SIZE];
244  char stratagus_bin[BUFF_SIZE];
245  char title_path[BUFF_SIZE];
246 
247 #ifdef WIN32
248  char executable_path[BUFF_SIZE];
249  memset(executable_path, 0, sizeof(executable_path));
250  GetModuleFileName(NULL, executable_path, sizeof(executable_path)-1);
251 
252  char executable_drive[_MAX_DRIVE];
253  char executable_dir[_MAX_DIR];
254  memset(executable_drive, 0, sizeof(executable_drive));
255  memset(executable_dir, 0, sizeof(executable_dir));
256  _splitpath(executable_path, executable_drive, executable_dir, NULL, NULL);
257 
258  size_t data_path_size = sizeof(data_path);
259  memset(data_path, 0, data_path_size);
260 
261  if (executable_path[0] && executable_drive[0] && executable_dir[0]) {
262  strcpy(data_path, executable_drive);
263  strcpy(data_path+strlen(executable_drive), executable_dir);
264  } else {
265  getcwd(data_path, data_path_size);
266  }
267  const size_t data_path_length = strlen(data_path);
268  if (data_path_length != 0 && data_path[data_path_length - 1] == '\\') {
269  data_path[data_path_length - 1] = '\0';
270  }
271 
272  char stratagus_path[BUFF_SIZE];
273  DWORD stratagus_path_size = sizeof(stratagus_path);
274  memset(stratagus_path, 0, stratagus_path_size);
275  HKEY key;
276 
277  if ( RegOpenKeyEx(HKEY_LOCAL_MACHINE, REGKEY, 0, KEY_QUERY_VALUE, &key) == ERROR_SUCCESS ) {
278  if ( RegQueryValueEx(key, "InstallLocation", NULL, NULL, (LPBYTE) stratagus_path, &stratagus_path_size) == ERROR_SUCCESS ) {
279  if ( stratagus_path_size == 0 || strlen(stratagus_path) == 0 ) {
281  }
282  }
283  RegCloseKey(key);
284  }
285 
286  if ( chdir(stratagus_path) != 0 ) {
288  }
289  sprintf(scripts_path, "\"%s\"", data_path);
290  sprintf(stratagus_bin, "%s\\stratagus.exe", stratagus_path);
291 #else
292  strcpy(data_path, DATA_PATH);
293  strcpy(scripts_path, SCRIPTS_PATH);
294  strcpy(stratagus_bin, STRATAGUS_BIN);
295 #endif
296 
297  if ( stat(stratagus_bin, &st) != 0 ) {
299  }
300  if ( stat(data_path, &st) != 0 ) {
302  }
303 #ifdef WIN32
304  sprintf(title_path, "%s\\graphics\\ui\\title.png", data_path);
305 
306  int data_path_len = strlen(data_path);
307 
308  for (int i = data_path_len - 1; i >= 0; --i) {
309  data_path[i + 1] = data_path[i];
310  }
311  data_path[0] = '"';
312  data_path[data_path_len + 1] = '"';
313  data_path[data_path_len + 2] = 0;
314 #else
315  sprintf(title_path, "%s/graphics/ui/title.png", data_path);
316 #endif
317 
318  if ( stat(title_path, &st) != 0 ) {
320  }
321 #ifndef WIN32
322  if ( strcmp(data_path, scripts_path) != 0 ) {
323  if ( chdir(data_path) != 0 ) {
325  }
326  }
327 #endif
328 
329 #ifdef _MSC_VER
330  char** stratagus_argv;
331  stratagus_argv = (char**) malloc((argc + 3) * sizeof (*stratagus_argv));
332 #else
333  char * stratagus_argv[argc + 3];
334 #endif
335 
336 #ifdef WIN32
337  char stratagus_argv0_esc[BUFF_SIZE];
338  memset(stratagus_argv0_esc, 0, sizeof(stratagus_argv0_esc));
339  strcpy(stratagus_argv0_esc + 1, argv[0]);
340  stratagus_argv0_esc[0] = '"';
341  stratagus_argv0_esc[strlen(argv[0]) + 1] = '"';
342  stratagus_argv0_esc[strlen(argv[0]) + 2] = 0;
343  stratagus_argv[0] = stratagus_argv0_esc;
344 #else
345  stratagus_argv[0] = argv[0];
346 #endif
347 
348  stratagus_argv[1] = "-d";
349  stratagus_argv[2] = scripts_path;
350 
351  for (int i = 3; i < argc + 2; ++i ) {
352  stratagus_argv[i] = argv[i - 2];
353  }
354  stratagus_argv[argc + 2] = NULL;
355 
356 #ifdef WIN32
357  AttachConsole(ATTACH_PARENT_PROCESS);
358 
359  errno = 0;
360  int ret = spawnvp(_P_WAIT, stratagus_bin, stratagus_argv);
361 #ifdef _MSC_VER
362  free (stratagus_argv);
363 #endif
364  if ( errno == 0 ) {
365  return ret;
366  }
367 #else
368  execvp(stratagus_bin, stratagus_argv);
369 #endif
370 
372  return 1;
373 }
#define CONSOLE_MODE_NOT_ROOT
Definition: stratagus-game-launcher.h:178
#define REGKEY
Definition: stratagus-game-launcher.h:171
#define SCRIPTS_PATH
Definition: stratagus-game-launcher.h:112
#define STRATAGUS_NOT_FOUND
Definition: stratagus-game-launcher.h:175
#define DATA_NOT_EXTRACTED
Definition: stratagus-game-launcher.h:176
#define BUFF_SIZE
Definition: stratagus-game-launcher.h:180
#define STRATAGUS_BIN
Definition: stratagus-game-launcher.h:113
int main(int argc, char *argv[])
Definition: stratagus-game-launcher.h:219
#define DATA_PATH
Definition: stratagus-game-launcher.h:111
#define TITLE
Definition: stratagus-game-launcher.h:174
#define NO_X_DISPLAY
Definition: stratagus-game-launcher.h:177
static void error(char *title, char *text)
Definition: stratagus-game-launcher.h:186
(C) Copyright 1998-2012 by The Stratagus Project under the GNU General Public License.
All trademarks and copyrights on this page are owned by their respective owners.