Added Stratagus Game headers file for easy creating Game Launchers and Maemo Extractors

This commit is contained in:
Pali Rohár 2011-08-08 15:28:05 +02:00
parent 3ad80c26dc
commit 5e35702ba9
5 changed files with 584 additions and 0 deletions

View file

@ -475,6 +475,7 @@ option(WITH_THEORA "Compile Stratagus with Theroa video library" ON)
option(WITH_X11 "Compile Stratagus with X11 clipboard pasting support" ON)
option(ENABLE_DOC "Generate Stratagus source code documentation with Doxygen" OFF)
option(ENABLE_DEV "Install Stratagus game development headers files" OFF)
option(ENABLE_UPX "Compress Stratagus executable binary with UPX packer" OFF)
if(NOT WITH_RENDERER)
@ -757,6 +758,16 @@ if(WIN32 AND MINGW AND ENABLE_STATIC)
set_target_properties(png2stratagus PROPERTIES LINK_FLAGS "${LINK_FLAGS} -static-libgcc")
endif()
########### next target ###############
set(gameheaders_HDRS
gameheaders/stratagus-game-installer.nsi
gameheaders/stratagus-game-launcher.h
gameheaders/stratagus-maemo-extract.h
)
source_group(gameheaders FILES ${gameheaders_HDRS})
########### next target ###############
if(ENABLE_DOC AND DOXYGEN_FOUND)
@ -830,3 +841,7 @@ if(ENABLE_DOC AND DOXYGEN_FOUND)
)
install(DIRECTORY doc/graphics doc/scripts ${CMAKE_CURRENT_BINARY_DIR}/doxygen DESTINATION share/doc/stratagus)
endif(ENABLE_DOC AND DOXYGEN_FOUND)
if(ENABLE_DEV)
install(FILES ${gameheaders_HDRS} DESTINATION include)
endif()

View file

@ -570,6 +570,7 @@ WARN_LOGFILE =
INPUT = ${CMAKE_CURRENT_SOURCE_DIR}/metaserver \
${CMAKE_CURRENT_SOURCE_DIR}/src \
${CMAKE_CURRENT_SOURCE_DIR)/gameheaders \
${CMAKE_CURRENT_SOURCE_DIR}/tools
# This tag can be used to specify the character encoding of the source files

View file

View file

@ -0,0 +1,335 @@
/*
_________ __ __
/ _____// |_____________ _/ |______ ____ __ __ ______
\_____ \\ __\_ __ \__ \\ __\__ \ / ___\| | \/ ___/
/ \| | | | \// __ \| | / __ \_/ /_/ > | /\___ |
/_______ /|__| |__| (____ /__| (____ /\___ /|____//____ >
\/ \/ \//_____/ \/
______________________ ______________________
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 - Stratagus Game Launcher
Copyright (C) 2010-2011 Pali Rohár <pali.rohar@gmail.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @page GameLauncher Stratagus Game Launcher
*
* Stratagus Game Launcher is C code for generating launcher for any Stratagus game.
* Game launcher for concrete game check if game data exists in default Stratagus
* location and spawn Stratagus process with correct game data location. If does not
* exist it show GUI or console error message.
*
* Before including this header, you need to define:
*
* ::GAME_NAME
*
* ::GAME_CD
*
* ::GAME
*
* Example use of code:
*
* @code
*
* #define GAME_NAME "My Game Name"
* #define GAME_CD "Original Game CD Name"
* #define GAME "my_game"
* #include <stratagus-game-launcher.h>
*
* @endcode
**/
/**
* \def GAME_NAME
* Full name of your Game
**/
/**
* \def GAME_CD
* Full name of data CD
**/
/**
* \def GAME
* Short name of game (lower ascii chars without space)
**/
#if ! defined (GAME_NAME) || ! defined (GAME_CD) || ! defined (GAME)
#error You need to define all Game macros, see stratagus-game-launcher.h
#endif
#if defined (MAEMO_GTK) || defined (MAEMO_CHANGES)
#define MAEMO
#endif
#if ( defined (_MSC_VER) || defined (_WIN32) || defined (_WIN64) ) && ! defined (WIN32)
#define WIN32
#endif
#ifdef WIN32
#define WINVER 0x0501
#include <windows.h>
#include <wincon.h>
#include <process.h>
#include <errno.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef _MSC_VER
#include <sys/stat.h>
#include <sys/types.h>
#endif
#ifdef _MSC_VER
#include <direct.h>
#define inline __inline
#define chdir _chdir
#define getcwd _getcwd
#define spawnvp _spawnvp
#define stat _stat
#endif
#ifndef WIN32
#include <unistd.h>
#include <X11/Xlib.h>
#include <gtk/gtk.h>
#endif
#ifdef MAEMO
#include <hildon/hildon.h>
#endif
#ifdef _WIN64
#define REGKEY "Software\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Stratagus (64 bit)"
#elif defined (WIN32)
#define REGKEY "Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Stratagus"
#endif
#ifndef WIN32
#ifdef MAEMO
#define DATA_PATH "/home/user/MyDocs/stratagus/" GAME
#define SCRIPTS_PATH "/opt/stratagus/share/" GAME
#define STRATAGUS_BIN "/opt/stratagus/bin/stratagus"
#endif
#ifndef DATA_PATH
#define DATA_PATH "/usr/share/games/stratagus/" GAME
#endif
#ifndef SCRIPTS_PATH
#define SCRIPTS_PATH "/usr/share/games/stratagus/" GAME
#endif
#ifndef STRATAGUS_BIN
#define STRATAGUS_BIN "/usr/games/stratagus"
#endif
#endif
#define TITLE GAME_NAME
#define STRATAGUS_NOT_FOUND "Stratagus is not installed.\nYou need Stratagus to run " GAME_NAME "!\nFirst install Stratagus from https://launchpad.net/stratagus"
#define DATA_NOT_EXTRACTED GAME_NAME " data was not extracted yet.\nYou need extract data from original " GAME_CD " first!"
#define NO_X_DISPLAY "Cannot open X Display"
#define CONSOLE_MODE_NOT_ROOT "You must be root to run " GAME_NAME " in console framebuffer mode"
#define BUFF_SIZE 1024
#ifndef WIN32
int ConsoleMode = 0;
#endif
static inline void error(char * title, char * text) {
#ifdef WIN32
MessageBox(NULL, text, title, MB_OK | MB_ICONERROR);
#else
if ( ! ConsoleMode ) {
GtkWidget * window = NULL;
GtkWidget * dialog = NULL;
#ifdef MAEMO
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), title);
gtk_widget_show(window);
#endif
dialog = gtk_message_dialog_new(GTK_WINDOW(window), GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, text, NULL);
gtk_window_set_title(GTK_WINDOW(dialog), title);
gtk_window_set_skip_pager_hint(GTK_WINDOW(dialog), 0);
gtk_window_set_skip_taskbar_hint(GTK_WINDOW(dialog), 0);
gtk_label_set_selectable(GTK_LABEL(GTK_MESSAGE_DIALOG(dialog)->label), 0);
gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_destroy(dialog);
#ifdef MAEMO
gtk_widget_destroy(window);
#endif
} else {
fprintf(stderr, "%s -- Error: %s\n", title, text);
}
#endif
exit(1);
}
int main(int argc, char * argv[]) {
#ifndef WIN32
if ( ! XOpenDisplay(NULL) )
ConsoleMode = 1;
if ( ConsoleMode ) {
#ifdef MAEMO
error(TITLE, NO_X_DISPLAY);
#else
if ( getuid() != 0 )
error(TITLE, CONSOLE_MODE_NOT_ROOT);
#endif
} else {
gtk_init(&argc, &argv);
#ifdef MAEMO
hildon_init();
#endif
}
#endif
int i;
struct stat st;
char data_path[BUFF_SIZE];
char scripts_path[BUFF_SIZE];
char stratagus_bin[BUFF_SIZE];
char title_path[BUFF_SIZE];
#ifdef WIN32
size_t data_path_size = sizeof(data_path);
memset(data_path, 0, data_path_size);
getcwd(data_path, data_path_size);
char stratagus_path[BUFF_SIZE];
DWORD stratagus_path_size = sizeof(stratagus_path);
memset(stratagus_path, 0, stratagus_path_size);
HKEY key;
if ( RegOpenKeyEx(HKEY_LOCAL_MACHINE, REGKEY, 0, KEY_QUERY_VALUE, &key) == ERROR_SUCCESS ) {
if ( RegQueryValueEx(key, "InstallLocation", NULL, NULL, (LPBYTE) stratagus_path, &stratagus_path_size) == ERROR_SUCCESS )
if ( stratagus_path_size == 0 || strlen(stratagus_path) == 0 )
error(TITLE, STRATAGUS_NOT_FOUND);
RegCloseKey(key);
}
if ( chdir(stratagus_path) != 0 )
error(TITLE, STRATAGUS_NOT_FOUND);
strcpy(scripts_path, data_path);
sprintf(stratagus_bin, "%s\\stratagus.exe", stratagus_path);
#else
strcpy(data_path, DATA_PATH);
strcpy(scripts_path, SCRIPTS_PATH);
strcpy(stratagus_bin, STRATAGUS_BIN);
#endif
if ( stat(stratagus_bin, &st) != 0 )
error(TITLE, STRATAGUS_NOT_FOUND);
if ( stat(data_path, &st) != 0 )
error(TITLE, DATA_NOT_EXTRACTED);
#ifdef WIN32
sprintf(title_path, "%s\\graphics\\ui\\title.png", data_path);
int data_path_len = strlen(data_path);
for ( i = data_path_len - 1; i >= 0; --i )
data_path[i + 1] = data_path[i];
data_path[0] = '"';
data_path[data_path_len + 1] = '"';
data_path[data_path_len + 2] = 0;
#else
sprintf(title_path, "%s/graphics/ui/title.png", data_path);
#endif
if ( stat(title_path, &st) != 0 )
error(TITLE, DATA_NOT_EXTRACTED);
#ifndef WIN32
if ( strcmp(data_path, scripts_path) != 0 )
if ( chdir(data_path) != 0 )
error(TITLE, DATA_NOT_EXTRACTED);
#endif
char * stratagus_argv[argc + 3];
#ifdef WIN32
char stratagus_argv0_esc[BUFF_SIZE];
memset(stratagus_argv0_esc, 0, sizeof(stratagus_argv0_esc));
strcpy(stratagus_argv0_esc + 1, argv[0]);
stratagus_argv0_esc[0] = '"';
stratagus_argv0_esc[strlen(argv[0]) + 1] = '"';
stratagus_argv0_esc[strlen(argv[0]) + 2] = 0;
stratagus_argv[0] = stratagus_argv0_esc;
#else
stratagus_argv[0] = argv[0];
#endif
stratagus_argv[1] = "-d";
stratagus_argv[2] = scripts_path;
for ( i = 3; i < argc + 2; ++i )
stratagus_argv[i] = argv[i - 2];
stratagus_argv[argc + 2] = NULL;
#ifdef WIN32
AttachConsole(ATTACH_PARENT_PROCESS);
errno = 0;
int ret = spawnvp(_P_WAIT, stratagus_bin, stratagus_argv);
if ( errno == 0 )
return ret;
#else
execvp(stratagus_bin, stratagus_argv);
#endif
error(TITLE, STRATAGUS_NOT_FOUND);
return 1;
}

View file

@ -0,0 +1,233 @@
/*
_________ __ __
/ _____// |_____________ _/ |______ ____ __ __ ______
\_____ \\ __\_ __ \__ \\ __\__ \ / ___\| | \/ ___/
/ \| | | | \// __ \| | / __ \_/ /_/ > | /\___ |
/_______ /|__| |__| (____ /__| (____ /\___ /|____//____ >
\/ \/ \//_____/ \/
______________________ ______________________
T H E W A R B E G I N S
Stratagus - A free fantasy real time strategy game engine
stratagus-maemo-extract.h - Stratagus Game data extractor for Maemo
Copyright (C) 2010-2011 Pali Rohár <pali.rohar@gmail.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @page MaemoExtract Stratagus Game data extractor for Maemo
*
* Stratagus Game Launcher is C code for generating launcher for any Stratagus game.
* Game launcher for concrete game check if game data exists in default Stratagus
* location and spawn Stratagus process with correct game data location. If does not
* exist it show GUI or console error message.
*
* Before including this header, you need to define:
*
* ::GAME_NAME
*
* ::GAME_CD
*
* ::GAME_CD_DIR
*
* ::GAME_CD_FILE
*
* ::GAME
*
*
* ::EXTRACT_BIN
*
* ::EXTRACT_COMMAND
*
* ::EXTRACT_INFO
*
* Example use of code:
*
* @code
*
* #define GAME_NAME "My Game Name"
* #define GAME_CD "Original Game CD Name"
* #define GAME_CD_DIR "MyDocs/game"
* #define GAME_CD_FILE "datafile.bin"
* #define GAME "my_game"
* #define EXTRACT_BIN "/opt/stratagus/bin/mygametool"
* #define EXTRACT_COMMAND "/opt/stratagus/bin/mygametool /home/user/MyDocs/game /home/user/stratagus/my_game"
* #define EXTRACT_INFO ""
* #include <stratagus-maemo-extract.h>
*
* @endcode
**/
/**
* \def GAME_NAME
* Full name of your Game
**/
/**
* \def GAME_CD
* Full name of data CD
**/
/**
* \def GAME_CD_DIR
* Relative path of /home/user where is data CD
**/
/**
* \def GAME_CD_FILE
* Filename of one data file
**/
/**
* \def GAME
* Short name of game (lower ascii chars without space)
**/
/**
* \def EXTRACT_BIN
* Full path to program which extract data
**/
/**
* \def EXTRACT_COMMAND
* Full patch to extract program with command line arguments for extracting data
**/
/**
* \def EXTRACT_INFO
* Additional extract data game info
**/
#if ! defined (GAME_NAME) || ! defined (GAME_CD) || ! defined (GAME_CD_DIR) || ! defined (GAME_CD_FILE) || ! defined (GAME) || ! defined (EXTRACT_BIN) || ! defined (EXTRACT_COMMAND) || ! defined (EXTRACT_INFO)
#error You need to define all Game macros, see stratagus-maemo-extract.h
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <gtk/gtk.h>
#include <hildon/hildon.h>
#define DATA_NEED_COPY "Note: You need the original " GAME_CD "\n(Battle.net edition doesn't work)\nto extract the game data files.\nData files are needed to run " GAME_NAME ".\n\nFirst copy " GAME_CD " to folder " GAME_CD_DIR "\n, then press OK." EXTRACT_INFO
#define DATA_FOUND GAME_CD " data files was found in folder " GAME_CD_DIR "\n\nPlease be patient, the data may take\na couple of minutes to extract...\n\nPress OK to start extracting data now."
#define DATA_NOT_FOUND "Error: " GAME_CD " data files was not found.\n\nCheck if you have file\n" GAME_CD_DIR"/"GAME_CD_FILE
#define EXTRACT_OK GAME_CD " data files was successfull extracted."
#define EXTRACT_FAILED "Error: Cannot extract " GAME_CD " data files,\nextract program crashed."
#define DATA_DIR "/home/user/" GAME_CD_DIR
#define EXTRACT_DIR "/home/user/MyDocs/stratagus/" GAME
static inline void message(char * title, char * text, int error) {
GtkWidget * window = NULL;
GtkWidget * dialog = NULL;
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), title);
gtk_widget_show(window);
dialog = gtk_message_dialog_new(GTK_WINDOW(window), GTK_DIALOG_DESTROY_WITH_PARENT, error ? GTK_MESSAGE_ERROR : GTK_MESSAGE_INFO, GTK_BUTTONS_OK, text, NULL);
gtk_window_set_title(GTK_WINDOW(dialog), title);
gtk_window_set_skip_pager_hint(GTK_WINDOW(dialog), 0);
gtk_window_set_skip_taskbar_hint(GTK_WINDOW(dialog), 0);
gtk_label_set_selectable(GTK_LABEL(GTK_MESSAGE_DIALOG(dialog)->label), 0);
gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_destroy(dialog);
gtk_widget_destroy(window);
if ( error )
exit(error);
}
int main(int argc, char * argv[]) {
FILE * file;
struct stat st;
hildon_gtk_init(&argc, &argv);
file = fopen(EXTRACT_DIR "/extracted", "r");
if ( file ) {
char act_version[20];
fgets(act_version, 20, file);
fclose(file);
file = popen(EXTRACTBIN " -V", "r");
if ( file ) {
char new_version[20];
fgets(new_version, 20, file);
pclose(file);
if ( strncmp(act_version, new_version, 19) == 0 )
return 0;
}
}
message(GAME_NAME, DATA_NEED_COPY, 0);
if ( stat(GAME_DIR "/" GAME_CD_FILE, &st) != 0 ) {
char * buf = strdup(GAME_CD_FILE);
char * path = calloc(strlen(GAME_CD_FILE) + strlen(GAME_DIR) + 2, sizeof(char));
char * ptr = buf;
while ( *ptr ) {
*ptr = toupper(*ptr);
++ptr;
}
sprintf(path, GAME_DIR "/%s", buf);
if ( stat(path, &st) != 0 )
message(GAME_NAME, DATA_NOT_FOUND, 1);
free(buf);
free(path);
}
if ( stat("/home/user/MyDocs/stratagus", &st) != 0 )
mkdir("/home/user/MyDocs/stratagus", 0777);
if ( stat("/home/user/MyDocs/stratagus/" GAME, &st) != 0 )
mkdir("/home/user/MyDocs/stratagus/" GAME, 0777);
message(GAME_NAME, DATA_FOUND, 0);
int ret = system(EXTRACT_COMMAND);
if ( ret != 0 )
message(GAMENAME, EXTRACT_FAILED, ret);
message(GAME_NAME, EXTRACT_OK, 0);
return 0;
}