2010-03-14 01:49:19 +01:00
// ==============================================================
// This file is part of Glest (www.glest.org)
//
2011-06-07 04:37:58 +02:00
// Copyright (C) 2001-2008 Martiño Figueroa
2010-03-14 01:49:19 +01:00
//
// You can redistribute this code 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
// ==============================================================
# include "game_util.h"
# include "util.h"
# include "lang.h"
# include "game_constants.h"
# include "config.h"
2010-03-22 00:40:35 +01:00
# include <stdlib.h>
# include "platform_util.h"
2010-04-30 04:20:27 +02:00
# include "conversion.h"
2010-12-09 21:41:11 +01:00
# include "cache_manager.h"
2014-12-31 06:40:51 +01:00
# include "errno.h"
2010-03-14 01:49:19 +01:00
# include "leak_dumper.h"
using namespace Shared : : Util ;
2010-03-22 00:40:35 +01:00
using namespace Shared : : Platform ;
2010-03-14 01:49:19 +01:00
2010-09-16 09:00:50 +02:00
namespace Glest { namespace Game {
2010-03-14 01:49:19 +01:00
2011-10-18 03:40:49 +02:00
const char * mailString = " http://bugs.megaglest.org " ;
2014-12-26 21:33:54 +01:00
// !! Use minor versions !! Only major and minor version control compatibility!
2016-02-04 19:29:12 +01:00
// typical version numbers look like this: v3.13-beta1.0 v3.12-dev v3.12.1
// don't forget to update file: source/version.txt
2017-03-08 21:44:28 +01:00
const string glestVersionString = " v3.13-dev " ;
2017-03-05 01:57:31 +01:00
const string lastCompatibleSaveGameVersionString = " v3.11.1 " ;
2014-12-26 21:33:54 +01:00
2016-02-04 19:29:12 +01:00
# if defined(GITVERSIONHEADER)
# include "gitversion.h"
# endif
# if defined(GITVERSION) || defined(GITVERSIONHEADER)
const string GIT_RawRev = string ( GITVERSION ) ;
2010-12-10 02:53:51 +01:00
# else
2017-03-08 21:44:28 +01:00
const string GIT_RawRev = " $5608.a3c8464$ " ;
2010-12-10 02:53:51 +01:00
# endif
2016-02-04 19:29:12 +01:00
const string GIT_Rev = string ( " Rev: " ) + string ( GIT_RawRev ) ;
2010-03-14 01:49:19 +01:00
2013-11-29 22:13:30 +01:00
string getRAWGITRevisionString ( ) {
return GIT_RawRev ;
2013-10-18 17:47:17 +02:00
}
2010-03-14 01:49:19 +01:00
string getCrashDumpFileName ( ) {
2010-12-13 01:32:59 +01:00
return " megaglest " + glestVersionString + " .dmp " ;
2010-03-14 01:49:19 +01:00
}
2014-12-30 21:16:35 +01:00
string getPlatformTypeNameString ( ) {
2012-10-06 09:06:40 +02:00
static string platform ;
2011-11-23 09:00:09 +01:00
if ( platform = = " " ) {
2010-05-13 00:54:42 +02:00
# if defined(WIN32)
2012-10-06 09:06:40 +02:00
2010-12-08 08:01:24 +01:00
# if defined(__MINGW32__)
2012-10-28 00:38:14 +02:00
platform = " W-Ming32 " ;
2012-10-06 09:06:40 +02:00
# else
platform = " Windows " ;
2010-12-08 08:01:24 +01:00
# endif
2010-09-25 01:28:54 +02:00
# elif defined(__FreeBSD__)
platform = " FreeBSD " ;
2012-10-26 21:03:58 +02:00
# elif defined(__NetBSD__)
platform = " NetBSD " ;
# elif defined(__OpenBSD__)
platform = " OpenBSD " ;
2010-09-27 22:28:08 +02:00
# elif defined(__APPLE__)
2016-11-18 02:07:44 +01:00
platform = " MacOS " ;
2012-10-26 21:03:58 +02:00
# elif defined(_AIX)
platform = " AIX " ;
# elif defined(__ANDROID__)
2012-10-28 00:38:14 +02:00
platform = " Android " ;
2012-10-26 21:03:58 +02:00
# elif defined(__BEOS__)
platform = " BEOS " ;
# elif defined(__gnu_linux__)
2012-10-28 00:38:14 +02:00
platform = " Linux " ;
2012-10-26 21:03:58 +02:00
# elif defined(__sun)
2012-10-28 00:38:14 +02:00
platform = " Solaris " ;
2012-10-26 21:03:58 +02:00
2010-04-30 04:20:27 +02:00
# elif defined(__GNUC__)
2012-10-06 09:06:40 +02:00
2010-12-08 08:01:24 +01:00
# if defined(__MINGW32__)
2012-10-28 00:38:14 +02:00
platform = " L-Ming32 " ;
2012-10-06 09:06:40 +02:00
# else
platform = " GNU " ;
2010-12-08 08:01:24 +01:00
# endif
2010-05-13 00:54:42 +02:00
# else
platform = " ??? " ;
# endif
2014-12-30 21:16:35 +01:00
}
return platform ;
}
string getPlatformArchTypeNameString ( ) {
static string platform ;
if ( platform = = " " ) {
2010-06-27 00:49:59 +02:00
2012-10-26 21:03:58 +02:00
# if defined(_M_X64) || defined(_M_AMD64) || defined(__x86_64__) || defined(_WIN64)
2014-12-30 21:16:35 +01:00
platform = " -X64 " ;
2012-10-26 21:03:58 +02:00
# elif defined(_M_ALPHA) || defined(__alpha__)
2014-12-30 21:16:35 +01:00
platform = " -ALPHA " ;
2012-10-26 21:03:58 +02:00
# elif defined(_M_IA64) || defined(__ia64__)
2014-12-30 21:16:35 +01:00
platform = " -IA64 " ;
2012-10-26 21:03:58 +02:00
# elif defined(_M_MRX000) || defined(__mips__)
2014-12-30 21:16:35 +01:00
platform = " -MIPS " ;
2012-10-26 21:03:58 +02:00
# elif defined(_M_PPC) || defined(__powerpc__)
2014-12-30 21:16:35 +01:00
platform = " -POWERPC " ;
2012-10-26 21:03:58 +02:00
# elif defined(__sparc__)
2014-12-30 21:16:35 +01:00
platform = " -SPARC " ;
2012-10-26 21:03:58 +02:00
# elif defined(_M_ARM_FP) || defined(__arm__) || defined(_M_ARM)
2014-12-30 21:16:35 +01:00
platform = " -ARM " ;
2012-10-26 21:03:58 +02:00
2010-06-27 00:49:59 +02:00
# endif
2011-11-23 09:00:09 +01:00
}
2010-05-13 00:54:42 +02:00
return platform ;
}
2010-04-30 04:20:27 +02:00
2014-12-30 21:16:35 +01:00
string getPlatformNameString ( ) {
static string platform ;
if ( platform = = " " ) {
platform = getPlatformTypeNameString ( ) + getPlatformArchTypeNameString ( ) ;
}
return platform ;
}
2013-11-29 22:13:30 +01:00
string getGITRevisionString ( ) {
return GIT_Rev ;
2010-10-08 04:05:42 +02:00
}
2010-05-13 00:54:42 +02:00
string getCompilerNameString ( ) {
2011-11-23 09:00:09 +01:00
static string version = " " ;
if ( version = = " " ) {
2010-05-13 00:54:42 +02:00
# if defined(WIN32) && defined(_MSC_VER)
version = " VC++: " + intToStr ( _MSC_VER ) ;
2010-06-22 01:45:49 +02:00
2014-01-22 02:06:10 +01:00
# elif defined(__clang__)
version = " Clang: " + intToStr ( __clang_major__ ) + " . " + intToStr ( __clang_minor__ ) + " . " + intToStr ( __clang_patchlevel__ ) ;
2010-05-13 00:54:42 +02:00
# elif defined(__GNUC__)
2010-04-30 04:20:27 +02:00
# if defined(__GNUC__)
# if defined(__GNUC_PATCHLEVEL__)
# define __GNUC_VERSION__ (__GNUC__ * 10000 \
+ __GNUC_MINOR__ * 100 \
+ __GNUC_PATCHLEVEL__ )
# else
# define __GNUC_VERSION__ (__GNUC__ * 10000 \
+ __GNUC_MINOR__ * 100 )
# endif
# endif
2010-12-08 08:01:24 +01:00
version = " GNUC " ;
# if defined(__MINGW32__)
version + = " -MINGW " ;
# endif
version + = " : " + intToStr ( __GNUC_VERSION__ ) ;
2010-06-22 01:45:49 +02:00
2010-05-13 00:54:42 +02:00
# else
version = " ??? " ;
2010-04-30 04:20:27 +02:00
# endif
2010-06-27 00:49:59 +02:00
# if defined(DEBUG) || defined(_DEBUG)
version + = " [DEBUG] " ;
# endif
2010-06-27 07:10:41 +02:00
# if defined(_M_X64) || defined(_M_IA64) || defined(_M_AMD64) || defined(__x86_64__) || defined(_WIN64)
2010-06-27 00:49:59 +02:00
version + = " [64bit] " ;
# endif
2011-11-23 09:00:09 +01:00
}
2010-04-30 04:20:27 +02:00
return version ;
2010-04-30 03:32:37 +02:00
}
2010-05-13 00:54:42 +02:00
string getNetworkVersionString ( ) {
2011-11-23 09:00:09 +01:00
static string version = " " ;
if ( version = = " " ) {
version = glestVersionString + " - " + getCompilerNameString ( ) + " - " + getCompileDateTime ( ) ;
}
2010-05-13 00:54:42 +02:00
return version ;
}
2013-11-29 22:13:30 +01:00
string getNetworkVersionGITString ( ) {
2011-11-23 09:00:09 +01:00
static string version = " " ;
if ( version = = " " ) {
2015-02-06 00:54:18 +01:00
version = glestVersionString + " - " + getGITRevisionString ( ) + " - " + getCompilerNameString ( ) ;
2011-11-23 09:00:09 +01:00
}
2011-04-30 03:05:53 +02:00
return version ;
}
2010-05-13 00:44:41 +02:00
string getCompileDateTime ( ) {
2011-11-23 09:00:09 +01:00
static string result = " " ;
2015-11-15 10:23:17 +01:00
// if(result == "") {
// result = string(__DATE__) + " " + string(__TIME__);
// }
2011-11-23 09:00:09 +01:00
return result ;
2010-05-13 00:44:41 +02:00
}
2010-04-30 03:32:37 +02:00
string getNetworkPlatformFreeVersionString ( ) {
return glestVersionString ;
2010-03-14 01:49:19 +01:00
}
2010-10-08 04:05:42 +02:00
string getAboutString1 ( int i ) {
2015-11-15 10:23:17 +01:00
//case 1: return "Built: " + string(__DATE__) + " " + GIT_Rev;
2011-11-23 09:00:09 +01:00
switch ( i ) {
2010-12-13 01:32:59 +01:00
case 0 : return " MegaGlest " + glestVersionString + " ( " + " Shared Library " + sharedLibVersionString + " ) " ;
2015-11-15 10:23:17 +01:00
case 1 : return GIT_Rev ;
2014-01-31 02:45:24 +01:00
case 2 : return " Copyright 2001-2010 The Glest Team " ;
2021-05-22 12:43:51 +02:00
case 3 : return " Copyright 2010-2021 The MegaGlest Team " ;
2010-03-14 01:49:19 +01:00
}
return " " ;
}
2011-11-23 09:00:09 +01:00
string getAboutString2 ( int i ) {
switch ( i ) {
2021-05-22 12:43:51 +02:00
case 0 : return " Web: https://megaglest.org " ;
2011-10-18 03:40:49 +02:00
case 1 : return " Bug reports: " + string ( mailString ) ;
2021-05-22 12:43:51 +02:00
case 2 : return " IRC: irc://irc.libera.chat/megaglest " ;
2010-03-14 01:49:19 +01:00
}
return " " ;
}
2010-09-16 09:03:38 +02:00
string getTeammateName ( int i ) {
switch ( i ) {
2011-06-28 17:39:19 +02:00
case 0 : return " Martiño Figueroa " ;
//case 0: return "Martino Figueroa";
case 1 : return " José Luis González " ;
//case 1: return "Jose Luis Gonzalez";
case 2 : return " Tucho Fernández " ;
//case 2: return "Tucho Fernandez";
case 3 : return " José Zanni " ;
//case 3: return "Jose Zanni";
case 4 : return " Félix Menéndez " ;
//case 4: return "Felix Menendez";
2010-03-14 01:49:19 +01:00
case 5 : return " Marcos Caruncho " ;
case 6 : return " Matthias Braun " ;
case 7 : return " Titus Tscharntke " ;
2010-09-16 09:03:38 +02:00
case 8 : return " Mark Vejvoda " ;
}
2010-03-14 01:49:19 +01:00
return " " ;
}
2010-09-16 09:03:38 +02:00
string getTeammateRole ( int i ) {
2013-10-29 07:13:38 +01:00
Lang & lang = Lang : : getInstance ( ) ;
switch ( i ) {
case 0 : return lang . getString ( " Programming " ) ;
case 1 : return lang . getString ( " SoundAndMusic " ) ;
case 2 : return lang . getString ( " 3dAnd2dArt " ) ;
case 3 : return lang . getString ( " 2dArtAndWeb " ) ;
case 4 : return lang . getString ( " Animation " ) ;
case 5 : return lang . getString ( " 3dArt " ) ;
case 6 : return lang . getString ( " LinuxPort " ) ;
case 7 : return lang . getString ( " Megaglest3d2dProgramming " ) ;
case 8 : return lang . getString ( " MegaglestProgramming " ) ;
2010-03-14 01:49:19 +01:00
}
return " " ;
}
2011-11-23 09:00:09 +01:00
string formatString ( string str ) {
2010-03-14 01:49:19 +01:00
string outStr = str ;
if ( ! outStr . empty ( ) ) {
outStr [ 0 ] = toupper ( outStr [ 0 ] ) ;
}
bool afterSeparator = false ;
2013-11-19 07:14:06 +01:00
for ( int i = 0 ; i < ( int ) str . size ( ) ; + + i ) {
2010-03-14 01:49:19 +01:00
if ( outStr [ i ] = = ' _ ' ) {
outStr [ i ] = ' ' ;
}
else if ( afterSeparator ) {
outStr [ i ] = toupper ( outStr [ i ] ) ;
afterSeparator = false ;
}
if ( outStr [ i ] = = ' \n ' | | outStr [ i ] = = ' ( ' | | outStr [ i ] = = ' ' ) {
afterSeparator = true ;
}
}
return outStr ;
}
2011-11-02 18:17:28 +01:00
string getGameCustomCoreDataPath ( string originalBasePath , string uniqueFilePath ) {
2011-11-23 19:02:00 +01:00
// original file path setup
2011-11-02 18:17:28 +01:00
if ( originalBasePath ! = " " ) {
endPathWithSlash ( originalBasePath ) ;
}
2011-11-23 19:02:00 +01:00
//
2011-11-02 18:17:28 +01:00
2011-11-23 19:02:00 +01:00
// mydata user data override
2011-11-02 18:17:28 +01:00
Config & config = Config : : getInstance ( ) ;
string data_path = config . getString ( " UserData_Root " , " " ) ;
if ( data_path ! = " " ) {
endPathWithSlash ( data_path ) ;
}
2011-11-23 19:02:00 +01:00
//
2011-11-02 18:17:28 +01:00
2011-11-23 19:02:00 +01:00
// if set this is the current active mod
string custom_mod_path = config . getCustomRuntimeProperty ( Config : : ACTIVE_MOD_PROPERTY_NAME ) ;
if ( custom_mod_path ! = " " ) {
endPathWithSlash ( custom_mod_path ) ;
}
//
// decide which file to use
string result = " " ;
2014-01-03 16:28:31 +01:00
if ( SystemFlags : : VERBOSE_MODE_ENABLED ) printf ( " Looking for [%s] in \n #1: [%s] \n #2: [%s] \n #3: [%s] \n " , uniqueFilePath . c_str ( ) , custom_mod_path . c_str ( ) , data_path . c_str ( ) , originalBasePath . c_str ( ) ) ;
2011-11-23 19:02:00 +01:00
if ( custom_mod_path ! = " " & &
( uniqueFilePath = = " " | | fileExists ( custom_mod_path + uniqueFilePath ) = = true ) ) {
result = custom_mod_path + uniqueFilePath ;
}
else if ( data_path ! = " " & &
( uniqueFilePath = = " " | | fileExists ( data_path + uniqueFilePath ) = = true ) ) {
2011-11-02 18:17:28 +01:00
result = data_path + uniqueFilePath ;
}
2011-11-23 19:02:00 +01:00
else {
result = originalBasePath + uniqueFilePath ;
}
2011-11-02 18:17:28 +01:00
2014-01-03 16:28:31 +01:00
if ( SystemFlags : : VERBOSE_MODE_ENABLED ) printf ( " result [%s] \n " , result . c_str ( ) ) ;
2011-11-02 18:17:28 +01:00
return result ;
}
2017-10-10 08:09:52 +02:00
string getGameReadWritePath ( const string & lookupKey ) {
2010-12-09 21:41:11 +01:00
string path = " " ;
if ( lookupKey ! = " " ) {
2011-01-20 16:56:30 +01:00
std : : map < string , string > & pathCache = CacheManager : : getCachedItem < std : : map < string , string > > ( GameConstants : : pathCacheLookupKey ) ;
2010-12-09 21:41:11 +01:00
std : : map < string , string > : : const_iterator iterFind = pathCache . find ( lookupKey ) ;
if ( iterFind ! = pathCache . end ( ) ) {
path = iterFind - > second ;
if ( path ! = " " & & EndsWith ( path , " / " ) = = false & & EndsWith ( path , " \\ " ) = = false ) {
path + = " / " ;
}
2010-12-21 06:07:10 +01:00
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] path to be used for [%s] files [%s]\n",__FILE__,__FUNCTION__,__LINE__,lookupKey.c_str(),path.c_str());
2011-01-20 16:56:30 +01:00
}
2010-12-09 21:41:11 +01:00
}
2013-12-15 07:37:15 +01:00
if ( path = = " " ) {
path = safeCharPtrCopy ( getenv ( " GLESTHOME " ) , 8095 ) ;
2010-03-22 00:40:35 +01:00
if ( path ! = " " & & EndsWith ( path , " / " ) = = false & & EndsWith ( path , " \\ " ) = = false ) {
path + = " / " ;
}
2010-12-21 06:07:10 +01:00
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] path to be used for read/write files [%s]\n",__FILE__,__FUNCTION__,__LINE__,path.c_str());
2010-03-22 00:40:35 +01:00
}
return path ;
}
2011-11-23 09:00:09 +01:00
void initSpecialStrings ( ) {
getCrashDumpFileName ( ) ;
getPlatformNameString ( ) ;
2013-11-29 22:13:30 +01:00
getGITRevisionString ( ) ;
2011-11-23 09:00:09 +01:00
getCompilerNameString ( ) ;
getNetworkVersionString ( ) ;
2013-11-29 22:13:30 +01:00
getNetworkVersionGITString ( ) ;
2011-11-23 09:00:09 +01:00
getNetworkPlatformFreeVersionString ( ) ;
getCompileDateTime ( ) ;
}
2014-12-31 00:05:24 +01:00
bool upgradeFilesInTemp ( ) {
// Get path to temp files
string tempFilePath = " temp/ " ;
if ( getGameReadWritePath ( GameConstants : : path_logs_CacheLookupKey ) ! = " " ) {
tempFilePath = getGameReadWritePath ( GameConstants : : path_logs_CacheLookupKey ) + tempFilePath ;
}
else {
Config & config = Config : : getInstance ( ) ;
string userData = config . getString ( " UserData_Root " , " " ) ;
if ( userData ! = " " ) {
endPathWithSlash ( userData ) ;
}
tempFilePath = userData + tempFilePath ;
}
if ( SystemFlags : : VERBOSE_MODE_ENABLED ) printf ( " Temp files path [%s] \n " , tempFilePath . c_str ( ) ) ;
// Move all files into binary folder
bool anyFailures = false ;
vector < string > fileList = getFolderTreeContentsListRecursively ( tempFilePath , " " , false , NULL ) ;
for ( unsigned int index = 0 ; index < fileList . size ( ) ; + + index ) {
string fileName = fileList [ index ] ;
string newFileName = Properties : : getApplicationPath ( ) + extractFileFromDirectoryPath ( fileName ) ;
bool result = renameFile ( fileName , newFileName ) ;
if ( result = = false ) {
2014-12-31 06:40:51 +01:00
printf ( " FAILED Rename: [%s] to [%s] result = %d errno = %d \n " , fileName . c_str ( ) , newFileName . c_str ( ) , result , errno ) ;
2014-12-31 00:05:24 +01:00
anyFailures = true ;
}
if ( SystemFlags : : VERBOSE_MODE_ENABLED ) printf ( " Rename: [%s] to [%s] result = %d \n " , fileName . c_str ( ) , newFileName . c_str ( ) , result ) ;
}
if ( SystemFlags : : VERBOSE_MODE_ENABLED ) printf ( " Successfully updated! \n " ) ;
return ( fileList . size ( ) > 0 & & anyFailures = = false ) ;
}
2010-03-14 01:49:19 +01:00
} } //end namespace