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

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 
128 #ifndef TITLE_PNG
129 #ifdef WIN32
130 #define TITLE_PNG "%s\\graphics\\ui\\title.png"
131 #else
132 #define TITLE_PNG "%s/graphics/ui/title.png"
133 #endif
134 #endif
135 
136 #ifndef WIN32
137 #if ! defined (DATA_PATH) || ! defined (SCRIPTS_PATH) || ! defined (STRATAGUS_BIN)
138 #error You need to define paths, see stratagus-game-launcher.h
139 #endif
140 #endif
141 
142 #ifdef WIN32
143 #define WINVER 0x0501
144 #include <windows.h>
145 #include <wincon.h>
146 #include <process.h>
147 #include <errno.h>
148 #endif
149 
150 #include <stdio.h>
151 #include <stdlib.h>
152 #include <string.h>
153 
154 #include <sys/stat.h>
155 #include <sys/types.h>
156 
157 #if defined(_MSC_VER) || defined(__MINGW32__)
158 #include <direct.h>
159 #define inline __inline
160 #define chdir _chdir
161 #define getcwd _getcwd
162 #define spawnvp _spawnvp
163 #define stat _stat
164 #endif
165 
166 #ifdef _MSC_VER
167 #pragma comment(linker, "/SUBSYSTEM:WINDOWS /ENTRY:mainCRTStartup")
168 #endif
169 
170 #ifndef WIN32
171 #include <unistd.h>
172 #include <X11/Xlib.h>
173 #ifndef NOGTK
174 #include <gtk/gtk.h>
175 #endif
176 #endif
177 
178 #ifdef _WIN64
179 #define REGKEY "Software\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Stratagus (64 bit)"
180 #elif defined (WIN32)
181 #define REGKEY "Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Stratagus"
182 #endif
183 
184 #define TITLE GAME_NAME
185 #define STRATAGUS_NOT_FOUND "Stratagus is not installed.\nYou need Stratagus to run " GAME_NAME "!\nFirst install Stratagus from https://launchpad.net/stratagus"
186 #define DATA_NOT_EXTRACTED GAME_NAME " data was not extracted yet.\nYou need extract data from original " GAME_CD " first!"
187 #define NO_X_DISPLAY "Cannot open X Display"
188 #define CONSOLE_MODE_NOT_ROOT "You must be root to run " GAME_NAME " in console framebuffer mode"
189 
190 #define BUFF_SIZE 1024
191 
192 #ifndef WIN32
193 int ConsoleMode = 0;
194 #endif
195 
196 static void error(char * title, char * text) {
197 
198 #ifdef WIN32
199  MessageBox(NULL, text, title, MB_OK | MB_ICONERROR);
200 #else
201 #ifdef NOGTK
202  {
203 #else
204  if ( ! ConsoleMode ) {
205  GtkWidget * window = NULL;
206  GtkWidget * dialog = NULL;
207 
208  dialog = gtk_message_dialog_new(GTK_WINDOW(window), GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "%s", text);
209  gtk_window_set_title(GTK_WINDOW(dialog), title);
210  gtk_window_set_skip_pager_hint(GTK_WINDOW(dialog), 0);
211  gtk_window_set_skip_taskbar_hint(GTK_WINDOW(dialog), 0);
212  gtk_label_set_selectable(GTK_LABEL(GTK_MESSAGE_DIALOG(dialog)->label), 0);
213  gtk_dialog_run(GTK_DIALOG(dialog));
214  gtk_widget_destroy(dialog);
215 
216  } else {
217 #endif
218  fprintf(stderr, "%s -- Error: %s\n", title, text);
219  }
220 #endif
221  exit(1);
222 }
223 
224 int main(int argc, char * argv[]) {
225 
226 #ifndef WIN32
227  if ( ! XOpenDisplay(NULL) ) {
228  ConsoleMode = 1;
229  }
230  if ( !ConsoleMode ) {
231 #ifndef NOGTK
232  gtk_init(&argc, &argv);
233 #endif
234  }
235 #endif
236 
237  struct stat st;
238  char data_path[BUFF_SIZE];
239  char scripts_path[BUFF_SIZE];
240  char stratagus_bin[BUFF_SIZE];
241  char title_path[BUFF_SIZE];
242 
243 #ifdef WIN32
244  char executable_path[BUFF_SIZE];
245  memset(executable_path, 0, sizeof(executable_path));
246  GetModuleFileName(NULL, executable_path, sizeof(executable_path)-1);
247 
248  char executable_drive[_MAX_DRIVE];
249  char executable_dir[_MAX_DIR];
250  memset(executable_drive, 0, sizeof(executable_drive));
251  memset(executable_dir, 0, sizeof(executable_dir));
252  _splitpath(executable_path, executable_drive, executable_dir, NULL, NULL);
253 
254  size_t data_path_size = sizeof(data_path);
255  memset(data_path, 0, data_path_size);
256 
257  if (executable_path[0] && executable_drive[0] && executable_dir[0]) {
258  strcpy(data_path, executable_drive);
259  strcpy(data_path+strlen(executable_drive), executable_dir);
260  } else {
261  getcwd(data_path, data_path_size);
262  }
263  const size_t data_path_length = strlen(data_path);
264  if (data_path_length != 0 && data_path[data_path_length - 1] == '\\') {
265  data_path[data_path_length - 1] = '\0';
266  }
267  sprintf(scripts_path, "\"%s\"", data_path);
268 
269  char stratagus_path[BUFF_SIZE];
270 
271  // Try to use stratagus.exe from data (install) directory first
272  sprintf(stratagus_bin, "%s\\stratagus.exe", data_path);
273  if (stat(stratagus_bin, &st) != 0) {
274  // If no local stratagus.exe is present, look for a globally installed version
275  DWORD stratagus_path_size = sizeof(stratagus_path);
276  memset(stratagus_path, 0, stratagus_path_size);
277  HKEY key;
278 
279  if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, REGKEY, 0, KEY_QUERY_VALUE, &key) == ERROR_SUCCESS) {
280  if (RegQueryValueEx(key, "InstallLocation", NULL, NULL, (LPBYTE)stratagus_path, &stratagus_path_size) == ERROR_SUCCESS) {
281  if (stratagus_path_size == 0 || strlen(stratagus_path) == 0) {
283  }
284  }
285  RegCloseKey(key);
286  }
287 
288  if (chdir(stratagus_path) != 0) {
290  }
291  sprintf(stratagus_bin, "%s\\stratagus.exe", stratagus_path);
292  }
293 #else
294  strcpy(data_path, DATA_PATH);
295  strcpy(scripts_path, SCRIPTS_PATH);
296  strcpy(stratagus_bin, STRATAGUS_BIN);
297 #endif
298 
299  if ( stat(stratagus_bin, &st) != 0 ) {
301  }
302  if ( stat(data_path, &st) != 0 ) {
304  }
305  sprintf(title_path, TITLE_PNG, data_path);
306 #ifdef WIN32
307  int data_path_len = strlen(data_path);
308 
309  for (int i = data_path_len - 1; i >= 0; --i) {
310  data_path[i + 1] = data_path[i];
311  }
312  data_path[0] = '"';
313  data_path[data_path_len + 1] = '"';
314  data_path[data_path_len + 2] = 0;
315 #endif
316 
317  if ( stat(title_path, &st) != 0 ) {
319  }
320 #ifndef WIN32
321  if ( strcmp(data_path, scripts_path) != 0 ) {
322  if ( chdir(data_path) != 0 ) {
324  }
325  }
326 #endif
327 
328 #ifdef _MSC_VER
329  char** stratagus_argv;
330  stratagus_argv = (char**) malloc((argc + 3) * sizeof (*stratagus_argv));
331 #else
332  char * stratagus_argv[argc + 3];
333 #endif
334 
335 #ifdef WIN32
336  char stratagus_argv0_esc[BUFF_SIZE];
337  memset(stratagus_argv0_esc, 0, sizeof(stratagus_argv0_esc));
338  strcpy(stratagus_argv0_esc + 1, argv[0]);
339  stratagus_argv0_esc[0] = '"';
340  stratagus_argv0_esc[strlen(argv[0]) + 1] = '"';
341  stratagus_argv0_esc[strlen(argv[0]) + 2] = 0;
342  stratagus_argv[0] = stratagus_argv0_esc;
343 #else
344  stratagus_argv[0] = argv[0];
345 #endif
346 
347  stratagus_argv[1] = "-d";
348  stratagus_argv[2] = scripts_path;
349 
350  for (int i = 3; i < argc + 2; ++i ) {
351  stratagus_argv[i] = argv[i - 2];
352  }
353  stratagus_argv[argc + 2] = NULL;
354 
355 #ifdef WIN32
356  AttachConsole(ATTACH_PARENT_PROCESS);
357 
358  errno = 0;
359  int ret = spawnvp(_P_WAIT, stratagus_bin, stratagus_argv);
360 #ifdef _MSC_VER
361  free (stratagus_argv);
362 #endif
363  if ( errno == 0 ) {
364  return ret;
365  }
366 #else
367  execvp(stratagus_bin, stratagus_argv);
368 #endif
369 
371  return 1;
372 }
#define REGKEY
Definition: stratagus-game-launcher.h:181
#define SCRIPTS_PATH
Definition: stratagus-game-launcher.h:112
#define STRATAGUS_NOT_FOUND
Definition: stratagus-game-launcher.h:185
#define DATA_NOT_EXTRACTED
Definition: stratagus-game-launcher.h:186
#define BUFF_SIZE
Definition: stratagus-game-launcher.h:190
#define STRATAGUS_BIN
Definition: stratagus-game-launcher.h:113
#define TITLE_PNG
Definition: stratagus-game-launcher.h:130
int main(int argc, char *argv[])
Definition: stratagus-game-launcher.h:224
#define DATA_PATH
Definition: stratagus-game-launcher.h:111
#define TITLE
Definition: stratagus-game-launcher.h:184
static void error(char *title, char *text)
Definition: stratagus-game-launcher.h:196
(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.