$treeview $search $mathjax
Stratagus
2.2.7
$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 (_MSC_VER) || defined (_WIN32) || defined (_WIN64) ) && ! defined (WIN32) 00121 #define WIN32 00122 #endif 00123 00124 #ifndef WIN32 00125 #if ! defined (DATA_PATH) || ! defined (SCRIPTS_PATH) || ! defined (STRATAGUS_BIN) 00126 #error You need to define paths, see stratagus-game-launcher.h 00127 #endif 00128 #endif 00129 00130 #if ( defined (MAEMO_GTK) || defined (MAEMO_CHANGES) ) && ! defined (MAEMO) 00131 #define MAEMO 00132 #endif 00133 00134 #ifdef WIN32 00135 #define WINVER 0x0501 00136 #include <windows.h> 00137 #include <wincon.h> 00138 #include <process.h> 00139 #include <errno.h> 00140 #endif 00141 00142 #include <stdio.h> 00143 #include <stdlib.h> 00144 #include <string.h> 00145 00146 #include <sys/stat.h> 00147 #include <sys/types.h> 00148 00149 #if defined(_MSC_VER) || defined(__MINGW32__) 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 GtkWidget * window = NULL; 00193 GtkWidget * dialog = NULL; 00194 00195 #ifdef MAEMO 00196 window = gtk_window_new(GTK_WINDOW_TOPLEVEL); 00197 gtk_window_set_title(GTK_WINDOW(window), title); 00198 gtk_widget_show(window); 00199 #endif 00200 00201 dialog = gtk_message_dialog_new(GTK_WINDOW(window), GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "%s", text); 00202 gtk_window_set_title(GTK_WINDOW(dialog), title); 00203 gtk_window_set_skip_pager_hint(GTK_WINDOW(dialog), 0); 00204 gtk_window_set_skip_taskbar_hint(GTK_WINDOW(dialog), 0); 00205 gtk_label_set_selectable(GTK_LABEL(GTK_MESSAGE_DIALOG(dialog)->label), 0); 00206 gtk_dialog_run(GTK_DIALOG(dialog)); 00207 gtk_widget_destroy(dialog); 00208 00209 #ifdef MAEMO 00210 gtk_widget_destroy(window); 00211 #endif 00212 } else { 00213 fprintf(stderr, "%s -- Error: %s\n", title, text); 00214 } 00215 #endif 00216 exit(1); 00217 } 00218 00219 int main(int argc, char * argv[]) { 00220 00221 #ifndef WIN32 00222 if ( ! XOpenDisplay(NULL) ) { 00223 ConsoleMode = 1; 00224 } 00225 if ( ConsoleMode ) { 00226 #ifdef MAEMO 00227 error(TITLE, NO_X_DISPLAY); 00228 #else 00229 if ( getuid() != 0 ) { 00230 error(TITLE, CONSOLE_MODE_NOT_ROOT); 00231 } 00232 #endif 00233 } else { 00234 gtk_init(&argc, &argv); 00235 #ifdef MAEMO 00236 hildon_init(); 00237 #endif 00238 } 00239 #endif 00240 00241 struct stat st; 00242 char data_path[BUFF_SIZE]; 00243 char scripts_path[BUFF_SIZE]; 00244 char stratagus_bin[BUFF_SIZE]; 00245 char title_path[BUFF_SIZE]; 00246 00247 #ifdef WIN32 00248 char executable_path[BUFF_SIZE]; 00249 memset(executable_path, 0, sizeof(executable_path)); 00250 GetModuleFileName(NULL, executable_path, sizeof(executable_path)-1); 00251 00252 char executable_drive[_MAX_DRIVE]; 00253 char executable_dir[_MAX_DIR]; 00254 memset(executable_drive, 0, sizeof(executable_drive)); 00255 memset(executable_dir, 0, sizeof(executable_dir)); 00256 _splitpath(executable_path, executable_drive, executable_dir, NULL, NULL); 00257 00258 size_t data_path_size = sizeof(data_path); 00259 memset(data_path, 0, data_path_size); 00260 00261 if (executable_path[0] && executable_drive[0] && executable_dir[0]) { 00262 strcpy(data_path, executable_drive); 00263 strcpy(data_path+strlen(executable_drive), executable_dir); 00264 } else { 00265 getcwd(data_path, data_path_size); 00266 } 00267 const size_t data_path_length = strlen(data_path); 00268 if (data_path_length != 0 && data_path[data_path_length - 1] == '\\') { 00269 data_path[data_path_length - 1] = '\0'; 00270 } 00271 00272 char stratagus_path[BUFF_SIZE]; 00273 DWORD stratagus_path_size = sizeof(stratagus_path); 00274 memset(stratagus_path, 0, stratagus_path_size); 00275 HKEY key; 00276 00277 if ( RegOpenKeyEx(HKEY_LOCAL_MACHINE, REGKEY, 0, KEY_QUERY_VALUE, &key) == ERROR_SUCCESS ) { 00278 if ( RegQueryValueEx(key, "InstallLocation", NULL, NULL, (LPBYTE) stratagus_path, &stratagus_path_size) == ERROR_SUCCESS ) { 00279 if ( stratagus_path_size == 0 || strlen(stratagus_path) == 0 ) { 00280 error(TITLE, STRATAGUS_NOT_FOUND); 00281 } 00282 } 00283 RegCloseKey(key); 00284 } 00285 00286 if ( chdir(stratagus_path) != 0 ) { 00287 error(TITLE, STRATAGUS_NOT_FOUND); 00288 } 00289 sprintf(scripts_path, "\"%s\"", data_path); 00290 sprintf(stratagus_bin, "%s\\stratagus.exe", stratagus_path); 00291 #else 00292 strcpy(data_path, DATA_PATH); 00293 strcpy(scripts_path, SCRIPTS_PATH); 00294 strcpy(stratagus_bin, STRATAGUS_BIN); 00295 #endif 00296 00297 if ( stat(stratagus_bin, &st) != 0 ) { 00298 error(TITLE, STRATAGUS_NOT_FOUND); 00299 } 00300 if ( stat(data_path, &st) != 0 ) { 00301 error(TITLE, DATA_NOT_EXTRACTED); 00302 } 00303 #ifdef WIN32 00304 sprintf(title_path, "%s\\graphics\\ui\\title.png", data_path); 00305 00306 int data_path_len = strlen(data_path); 00307 00308 for (int i = data_path_len - 1; i >= 0; --i) { 00309 data_path[i + 1] = data_path[i]; 00310 } 00311 data_path[0] = '"'; 00312 data_path[data_path_len + 1] = '"'; 00313 data_path[data_path_len + 2] = 0; 00314 #else 00315 sprintf(title_path, "%s/graphics/ui/title.png", data_path); 00316 #endif 00317 00318 if ( stat(title_path, &st) != 0 ) { 00319 error(TITLE, DATA_NOT_EXTRACTED); 00320 } 00321 #ifndef WIN32 00322 if ( strcmp(data_path, scripts_path) != 0 ) { 00323 if ( chdir(data_path) != 0 ) { 00324 error(TITLE, DATA_NOT_EXTRACTED); 00325 } 00326 } 00327 #endif 00328 00329 #ifdef _MSC_VER 00330 char** stratagus_argv; 00331 stratagus_argv = (char**) malloc((argc + 3) * sizeof (*stratagus_argv)); 00332 #else 00333 char * stratagus_argv[argc + 3]; 00334 #endif 00335 00336 #ifdef WIN32 00337 char stratagus_argv0_esc[BUFF_SIZE]; 00338 memset(stratagus_argv0_esc, 0, sizeof(stratagus_argv0_esc)); 00339 strcpy(stratagus_argv0_esc + 1, argv[0]); 00340 stratagus_argv0_esc[0] = '"'; 00341 stratagus_argv0_esc[strlen(argv[0]) + 1] = '"'; 00342 stratagus_argv0_esc[strlen(argv[0]) + 2] = 0; 00343 stratagus_argv[0] = stratagus_argv0_esc; 00344 #else 00345 stratagus_argv[0] = argv[0]; 00346 #endif 00347 00348 stratagus_argv[1] = "-d"; 00349 stratagus_argv[2] = scripts_path; 00350 00351 for (int i = 3; i < argc + 2; ++i ) { 00352 stratagus_argv[i] = argv[i - 2]; 00353 } 00354 stratagus_argv[argc + 2] = NULL; 00355 00356 #ifdef WIN32 00357 AttachConsole(ATTACH_PARENT_PROCESS); 00358 00359 errno = 0; 00360 int ret = spawnvp(_P_WAIT, stratagus_bin, stratagus_argv); 00361 #ifdef _MSC_VER 00362 free (stratagus_argv); 00363 #endif 00364 if ( errno == 0 ) { 00365 return ret; 00366 } 00367 #else 00368 execvp(stratagus_bin, stratagus_argv); 00369 #endif 00370 00371 error(TITLE, STRATAGUS_NOT_FOUND); 00372 return 1; 00373 }