$treeview $search $mathjax
Stratagus
2.2.6
$projectbrief
|
$projectbrief
|
$searchbox |
_________ __ __ / _____// |_____________ _/ |______ ____ __ __ ______ \_____ \\ __\_ __ \__ \\ __\__ \ / ___\| | \/ ___/ / \| | | | \// __ \| | / __ \_/ /_/ > | /\___ \ /_______ /|__| |__| (____ /__| (____ /\___ /|____//____ > \/ \/ \//_____/ \/ ______________________ ______________________ T H E W A R B E G I N S Stratagus - A free fantasy real time strategy game engine
00001 /* 00002 _________ __ __ 00003 / _____// |_____________ _/ |______ ____ __ __ ______ 00004 \_____ \\ __\_ __ \__ \\ __\__ \ / ___\| | \/ ___/ 00005 / \| | | | \// __ \| | / __ \_/ /_/ > | /\___ | 00006 /_______ /|__| |__| (____ /__| (____ /\___ /|____//____ > 00007 \/ \/ \//_____/ \/ 00008 ______________________ ______________________ 00009 T H E W A R B E G I N S 00010 Stratagus - A free fantasy real time strategy game engine 00011 00012 stratagus-game-launcher.h - Stratagus Game Launcher 00013 Copyright (C) 2010-2011 Pali Rohár <pali.rohar@gmail.com> 00014 00015 This program is free software: you can redistribute it and/or modify 00016 it under the terms of the GNU General Public License as published by 00017 the Free Software Foundation, either version 2 of the License, or 00018 (at your option) any later version. 00019 00020 This program is distributed in the hope that it will be useful, 00021 but WITHOUT ANY WARRANTY; without even the implied warranty of 00022 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00023 GNU General Public License for more details. 00024 00025 You should have received a copy of the GNU General Public License 00026 along with this program. If not, see <http://www.gnu.org/licenses/>. 00027 00028 */ 00029 00106 /* Fake definitions for Doxygen */ 00107 #ifdef DOXYGEN 00108 #define GAME_NAME 00109 #define GAME_CD 00110 #define GAME 00111 #define DATA_PATH 00112 #define SCRIPTS_PATH 00113 #define STRATAGUS_BIN 00114 #endif 00115 00116 #if ! defined (GAME_NAME) || ! defined (GAME_CD) || ! defined (GAME) 00117 #error You need to define all Game macros, see stratagus-game-launcher.h 00118 #endif 00119 00120 #if ! defined (DATA_PATH) || ! defined (SCRIPTS_PATH) || ! defined (STRATAGUS_BIN) 00121 #error You need to define paths, see stratagus-game-launcher.h 00122 #endif 00123 00124 #if ( defined (MAEMO_GTK) || defined (MAEMO_CHANGES) ) && ! defined (MAEMO) 00125 #define MAEMO 00126 #endif 00127 00128 #if ( defined (_MSC_VER) || defined (_WIN32) || defined (_WIN64) ) && ! defined (WIN32) 00129 #define WIN32 00130 #endif 00131 00132 #ifdef WIN32 00133 #define WINVER 0x0501 00134 #include <windows.h> 00135 #include <wincon.h> 00136 #include <process.h> 00137 #include <errno.h> 00138 #endif 00139 00140 #include <stdio.h> 00141 #include <stdlib.h> 00142 #include <string.h> 00143 00144 #ifndef _MSC_VER 00145 #include <sys/stat.h> 00146 #include <sys/types.h> 00147 #endif 00148 00149 #ifdef _MSC_VER 00150 #include <direct.h> 00151 #define inline __inline 00152 #define chdir _chdir 00153 #define getcwd _getcwd 00154 #define spawnvp _spawnvp 00155 #define stat _stat 00156 #endif 00157 00158 #ifndef WIN32 00159 #include <unistd.h> 00160 #include <X11/Xlib.h> 00161 #include <gtk/gtk.h> 00162 #endif 00163 00164 #ifdef MAEMO 00165 #include <hildon/hildon.h> 00166 #endif 00167 00168 #ifdef _WIN64 00169 #define REGKEY "Software\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Stratagus (64 bit)" 00170 #elif defined (WIN32) 00171 #define REGKEY "Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Stratagus" 00172 #endif 00173 00174 #define TITLE GAME_NAME 00175 #define STRATAGUS_NOT_FOUND "Stratagus is not installed.\nYou need Stratagus to run " GAME_NAME "!\nFirst install Stratagus from https://launchpad.net/stratagus" 00176 #define DATA_NOT_EXTRACTED GAME_NAME " data was not extracted yet.\nYou need extract data from original " GAME_CD " first!" 00177 #define NO_X_DISPLAY "Cannot open X Display" 00178 #define CONSOLE_MODE_NOT_ROOT "You must be root to run " GAME_NAME " in console framebuffer mode" 00179 00180 #define BUFF_SIZE 1024 00181 00182 #ifndef WIN32 00183 int ConsoleMode = 0; 00184 #endif 00185 00186 static void error(char * title, char * text) { 00187 00188 #ifdef WIN32 00189 MessageBox(NULL, text, title, MB_OK | MB_ICONERROR); 00190 #else 00191 if ( ! ConsoleMode ) { 00192 00193 GtkWidget * window = NULL; 00194 GtkWidget * dialog = NULL; 00195 00196 #ifdef MAEMO 00197 window = gtk_window_new(GTK_WINDOW_TOPLEVEL); 00198 gtk_window_set_title(GTK_WINDOW(window), title); 00199 gtk_widget_show(window); 00200 #endif 00201 00202 dialog = gtk_message_dialog_new(GTK_WINDOW(window), GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, text, NULL); 00203 gtk_window_set_title(GTK_WINDOW(dialog), title); 00204 gtk_window_set_skip_pager_hint(GTK_WINDOW(dialog), 0); 00205 gtk_window_set_skip_taskbar_hint(GTK_WINDOW(dialog), 0); 00206 gtk_label_set_selectable(GTK_LABEL(GTK_MESSAGE_DIALOG(dialog)->label), 0); 00207 gtk_dialog_run(GTK_DIALOG(dialog)); 00208 gtk_widget_destroy(dialog); 00209 00210 #ifdef MAEMO 00211 gtk_widget_destroy(window); 00212 #endif 00213 00214 } else { 00215 00216 fprintf(stderr, "%s -- Error: %s\n", title, text); 00217 00218 } 00219 #endif 00220 00221 exit(1); 00222 00223 } 00224 00225 int main(int argc, char * argv[]) { 00226 00227 #ifndef WIN32 00228 00229 if ( ! XOpenDisplay(NULL) ) 00230 ConsoleMode = 1; 00231 00232 if ( ConsoleMode ) { 00233 00234 #ifdef MAEMO 00235 error(TITLE, NO_X_DISPLAY); 00236 #else 00237 00238 if ( getuid() != 0 ) 00239 error(TITLE, CONSOLE_MODE_NOT_ROOT); 00240 00241 #endif 00242 00243 } else { 00244 00245 gtk_init(&argc, &argv); 00246 00247 #ifdef MAEMO 00248 hildon_init(); 00249 #endif 00250 00251 } 00252 00253 #endif 00254 00255 int i; 00256 struct stat st; 00257 char data_path[BUFF_SIZE]; 00258 char scripts_path[BUFF_SIZE]; 00259 char stratagus_bin[BUFF_SIZE]; 00260 char title_path[BUFF_SIZE]; 00261 00262 #ifdef WIN32 00263 size_t data_path_size = sizeof(data_path); 00264 memset(data_path, 0, data_path_size); 00265 getcwd(data_path, data_path_size); 00266 00267 char stratagus_path[BUFF_SIZE]; 00268 DWORD stratagus_path_size = sizeof(stratagus_path); 00269 memset(stratagus_path, 0, stratagus_path_size); 00270 HKEY key; 00271 00272 if ( RegOpenKeyEx(HKEY_LOCAL_MACHINE, REGKEY, 0, KEY_QUERY_VALUE, &key) == ERROR_SUCCESS ) { 00273 00274 if ( RegQueryValueEx(key, "InstallLocation", NULL, NULL, (LPBYTE) stratagus_path, &stratagus_path_size) == ERROR_SUCCESS ) 00275 if ( stratagus_path_size == 0 || strlen(stratagus_path) == 0 ) 00276 error(TITLE, STRATAGUS_NOT_FOUND); 00277 00278 RegCloseKey(key); 00279 00280 } 00281 00282 if ( chdir(stratagus_path) != 0 ) 00283 error(TITLE, STRATAGUS_NOT_FOUND); 00284 00285 strcpy(scripts_path, data_path); 00286 sprintf(stratagus_bin, "%s\\stratagus.exe", stratagus_path); 00287 #else 00288 strcpy(data_path, DATA_PATH); 00289 strcpy(scripts_path, SCRIPTS_PATH); 00290 strcpy(stratagus_bin, STRATAGUS_BIN); 00291 #endif 00292 00293 if ( stat(stratagus_bin, &st) != 0 ) 00294 error(TITLE, STRATAGUS_NOT_FOUND); 00295 00296 if ( stat(data_path, &st) != 0 ) 00297 error(TITLE, DATA_NOT_EXTRACTED); 00298 00299 #ifdef WIN32 00300 sprintf(title_path, "%s\\graphics\\ui\\title.png", data_path); 00301 00302 int data_path_len = strlen(data_path); 00303 00304 for ( i = data_path_len - 1; i >= 0; --i ) 00305 data_path[i + 1] = data_path[i]; 00306 00307 data_path[0] = '"'; 00308 data_path[data_path_len + 1] = '"'; 00309 data_path[data_path_len + 2] = 0; 00310 #else 00311 sprintf(title_path, "%s/graphics/ui/title.png", data_path); 00312 #endif 00313 00314 if ( stat(title_path, &st) != 0 ) 00315 error(TITLE, DATA_NOT_EXTRACTED); 00316 00317 #ifndef WIN32 00318 if ( strcmp(data_path, scripts_path) != 0 ) 00319 if ( chdir(data_path) != 0 ) 00320 error(TITLE, DATA_NOT_EXTRACTED); 00321 #endif 00322 00323 char * stratagus_argv[argc + 3]; 00324 00325 #ifdef WIN32 00326 char stratagus_argv0_esc[BUFF_SIZE]; 00327 memset(stratagus_argv0_esc, 0, sizeof(stratagus_argv0_esc)); 00328 strcpy(stratagus_argv0_esc + 1, argv[0]); 00329 stratagus_argv0_esc[0] = '"'; 00330 stratagus_argv0_esc[strlen(argv[0]) + 1] = '"'; 00331 stratagus_argv0_esc[strlen(argv[0]) + 2] = 0; 00332 stratagus_argv[0] = stratagus_argv0_esc; 00333 #else 00334 stratagus_argv[0] = argv[0]; 00335 #endif 00336 00337 stratagus_argv[1] = "-d"; 00338 stratagus_argv[2] = scripts_path; 00339 00340 for ( i = 3; i < argc + 2; ++i ) 00341 stratagus_argv[i] = argv[i - 2]; 00342 00343 stratagus_argv[argc + 2] = NULL; 00344 00345 #ifdef WIN32 00346 AttachConsole(ATTACH_PARENT_PROCESS); 00347 00348 errno = 0; 00349 int ret = spawnvp(_P_WAIT, stratagus_bin, stratagus_argv); 00350 00351 if ( errno == 0 ) 00352 return ret; 00353 #else 00354 execvp(stratagus_bin, stratagus_argv); 00355 #endif 00356 00357 error(TITLE, STRATAGUS_NOT_FOUND); 00358 return 1; 00359 00360 }