diff --git a/.travis.yml b/.travis.yml index 20ecbf7d..5e35de02 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,7 +9,7 @@ before_install: - sudo mk/linux/setupBuildDeps.sh --quiet # INSTALL DEPENDENCIES HERE script: # ALL THE BUILD COMMANDS HERE - - ./build-mg.sh 4 + - ./build-mg.sh -c 4 #- dmesg notifications: irc: diff --git a/build-mg.sh b/build-mg.sh index 6c643b78..cfe8a6b7 100755 --- a/build-mg.sh +++ b/build-mg.sh @@ -13,15 +13,47 @@ # Default to English language output so we can understand your bug reports export LANG=C +CPU_COUNT=-1 +CMAKE_ONLY=0 +MAKE_ONLY=0 +CLANG_FORCED=0 + +while getopts "c:m:n:f:" option; do + case "${option}" in + c) + CPU_COUNT=${OPTARG} +# echo "${option} value: ${OPTARG}" + ;; + m) + CMAKE_ONLY=${OPTARG} +# echo "${option} value: ${OPTARG}" + ;; + n) + MAKE_ONLY=${OPTARG} +# echo "${option} value: ${OPTARG}" + ;; + + f) + CLANG_FORCED=${OPTARG} +# echo "${option} value: ${OPTARG}" + ;; + esac +done + +#echo "CPU_COUNT = ${CPU_COUNT} CMAKE_ONLY = ${CMAKE_ONLY} CLANG_FORCED = ${CLANG_FORCED}" +#exit; + # Compiler selection # Unless both the CC and CXX environment variables point to clang and clang++ # respectively, we use GCC. To enforce clang compilation: # 1. Install clang (sudo apt-get install clang) # 2. Set the two vars below: # WANT_CLANG=YES and CLANG_BIN_PATH= -WANT_CLANG=NO CLANG_BIN_PATH=/usr/bin/ +SCRIPTDIR="$(dirname $(readlink -f $0))" +cd ${SCRIPTDIR} + # Google breakpad integration (cross platform memory dumps) - OPTIONAL # Set this to the root path of your Google breakpad subversion working copy. # By default, this script looks for a "google-breakpad" sub-directory within @@ -44,17 +76,21 @@ EXTRA_CMAKE_OPTIONS= NUMCORES=`lscpu -p | grep -cv '^#'` echo "CPU cores detected: $NUMCORES" if [ "$NUMCORES" = '' ]; then NUMCORES=1; fi -if [ "$1." != '.' ]; then NUMCORES=$1; fi +if [ $CPU_COUNT != -1 ]; then NUMCORES=$CPU_COUNT; fi echo "CPU cores to be used: $NUMCORES" # ---------------------------------------------------------------------------- -mkdir -p build +if [ $MAKE_ONLY = 0 ]; then + mkdir -p build +fi + cd build CURRENTDIR="$(dirname $(readlink -f $0))" -if [ -f 'CMakeCache.txt' ]; then rm -f 'CMakeCache.txt'; fi - +if [ $MAKE_ONLY = 0 ]; then + if [ -f 'CMakeCache.txt' ]; then rm -f 'CMakeCache.txt'; fi +fi # Get distribution and architecture details if [ `which lsb_release`'x' = 'x' ] @@ -144,7 +180,7 @@ esac # If, in the configuration section on top of this script, the user has # indicated they want to use clang in favor of the default of GCC, use clang. -if [ "$WANT_CLANG" = 'YES' ]; then +if [ $CLANG_FORCED = 1 ]; then EXTRA_CMAKE_OPTIONS="${EXTRA_CMAKE_OPTIONS} -DCMAKE_C_COMPILER=${CLANG_BIN_PATH}clang -DCMAKE_CXX_COMPILER=${CLANG_BIN_PATH}clang++" echo "USER WANTS to use CLANG / LLVM compiler! EXTRA_CMAKE_OPTIONS = ${EXTRA_CMAKE_OPTIONS}" #exit 1; @@ -166,24 +202,29 @@ elif [ "`echo $CC | grep -Fq 'clang'`" = 'clang' -a "`echo $CXX | grep -Fq 'clan #exit 1; fi -echo "Calling cmake with EXTRA_CMAKE_OPTIONS = ${EXTRA_CMAKE_OPTIONS}" -cmake -DCMAKE_INSTALL_PREFIX='' -DWANT_DEV_OUTPATH=ON -DWANT_STATIC_LIBS=ON -DBUILD_MEGAGLEST_TESTS=ON -DBREAKPAD_ROOT=$BREAKPAD_ROOT $EXTRA_CMAKE_OPTIONS .. -if [ $? -ne 0 ]; then - echo 'ERROR: CMAKE failed.' >&2; exit 1 +if [ $MAKE_ONLY = 0 ]; then + echo "Calling cmake with EXTRA_CMAKE_OPTIONS = ${EXTRA_CMAKE_OPTIONS}" + cmake -DCMAKE_INSTALL_PREFIX='' -DWANT_DEV_OUTPATH=ON -DWANT_STATIC_LIBS=ON -DBUILD_MEGAGLEST_TESTS=ON -DBREAKPAD_ROOT=$BREAKPAD_ROOT $EXTRA_CMAKE_OPTIONS .. + if [ $? -ne 0 ]; then + echo 'ERROR: CMAKE failed.' >&2; exit 1 + fi fi -echo "==================> About to call make with $NUMCORES cores... <==================" -make -j$NUMCORES -if [ $? -ne 0 ]; then - echo 'ERROR: MAKE failed.' >&2; exit 2 +if [ $CMAKE_ONLY = 1 ]; then + echo "==================> You may now call make with $NUMCORES cores... <==================" +else + echo "==================> About to call make with $NUMCORES cores... <==================" + make -j$NUMCORES + if [ $? -ne 0 ]; then + echo 'ERROR: MAKE failed.' >&2; exit 2 + fi + + cd .. + echo '' + echo 'BUILD COMPLETE.' + echo '' + echo 'To launch MegaGlest from the current directory, use:' + echo ' mk/linux/megaglest --ini-path=mk/linux/ --data-path=mk/linux/' + echo 'Or change into mk/linux and run it from there:' + echo ' ./megaglest --ini-path=./ --data-path=./' fi - -cd .. -echo '' -echo 'BUILD COMPLETE.' -echo '' -echo 'To launch MegaGlest from the current directory, use:' -echo ' mk/linux/megaglest --ini-path=mk/linux/ --data-path=mk/linux/' -echo 'Or change into mk/linux and run it from there:' -echo ' ./megaglest --ini-path=./ --data-path=./' - diff --git a/source/glest_game/game/script_manager.cpp b/source/glest_game/game/script_manager.cpp index c27be099..b799cc74 100644 --- a/source/glest_game/game/script_manager.cpp +++ b/source/glest_game/game/script_manager.cpp @@ -1012,7 +1012,10 @@ void ScriptManager::destroyUnit(int unitId){ Unit *unit = world->findUnitById(unitId); if(unit != NULL) { // Make sure they die - unit->decHp(unit->getHp() * unit->getHp()); + bool unit_dead = unit->decHp(unit->getHp() * unit->getHp()); + if(unit_dead == false) { + throw megaglest_runtime_error("unit_dead == false"); + } unit->kill(); // If called from an existing die event we get a stack overflow //onUnitDied(unit); diff --git a/source/glest_game/main/main.cpp b/source/glest_game/main/main.cpp index e29de352..f2df0716 100644 --- a/source/glest_game/main/main.cpp +++ b/source/glest_game/main/main.cpp @@ -3201,7 +3201,14 @@ void CheckForDuplicateData() { oldFile = newFile + "/" + tilesetName + ".xml"; newFile = newFile + "/" + tilesetName + "_custom.xml"; - rename(oldFile.c_str(),newFile.c_str()); + result = rename(oldFile.c_str(),newFile.c_str()); + + if(result != 0) { + char *errmsg = strerror(errno); + snprintf(szBuf,8096,"Error [%s]\nCould not rename [%s] to [%s]!",errmsg,oldFile.c_str(),newFile.c_str()); + throw megaglest_runtime_error(szBuf,true); + } + } errorMsg += szBuf; } diff --git a/source/glest_game/menu/menu_state_custom_game.cpp b/source/glest_game/menu/menu_state_custom_game.cpp index b7b2c873..832a7cce 100644 --- a/source/glest_game/menu/menu_state_custom_game.cpp +++ b/source/glest_game/menu/menu_state_custom_game.cpp @@ -1743,15 +1743,15 @@ void MenuStateCustomGame::PlayNow(bool saveGame) { mainMessageBoxState=1; Lang &lang= Lang::getInstance(); - char szMsg[8096]=""; + string sMsg = ""; if(lang.hasString("NetworkSlotUnassignedErrorUI") == true) { - strcpy(szMsg,lang.getString("NetworkSlotUnassignedErrorUI").c_str()); + sMsg = lang.getString("NetworkSlotUnassignedErrorUI"); } else { - strcpy(szMsg,"Cannot start game.\nSome player(s) are not in a network game slot!"); + sMsg = "Cannot start game.\nSome player(s) are not in a network game slot!"; } - showMessageBox(szMsg, "", false); + showMessageBox(sMsg, "", false); const vector languageList = serverInterface->getGameSettings()->getUniqueNetworkPlayerLanguages(); for(unsigned int j = 0; j < languageList.size(); ++j) { diff --git a/source/shared_lib/include/util/gen_uuid.h b/source/shared_lib/include/util/gen_uuid.h index 6ccce0bd..eec9b5b6 100644 --- a/source/shared_lib/include/util/gen_uuid.h +++ b/source/shared_lib/include/util/gen_uuid.h @@ -175,8 +175,6 @@ inline static void get_random_info(char seed[16]) { seed[i++] = myrand & 0xff; seed[i++] = myrand >> 8; } while (i < 14); - - if(fp != NULL) fclose(fp); } #endif diff --git a/source/shared_lib/sources/compression/compression_utils.cpp b/source/shared_lib/sources/compression/compression_utils.cpp index 33b1d599..c6966b35 100644 --- a/source/shared_lib/sources/compression/compression_utils.cpp +++ b/source/shared_lib/sources/compression/compression_utils.cpp @@ -151,6 +151,7 @@ int zipfile_tool(int argc, const char *argv[]) { if (deflateInit(&stream, level) != Z_OK) { if(SystemFlags::VERBOSE_MODE_ENABLED) printf("deflateInit() failed!\n"); + if(pInfile) fclose(pInfile); return EXIT_FAILURE; } @@ -217,6 +218,7 @@ int zipfile_tool(int argc, const char *argv[]) { if (fread(s_inbuf, 1, n, pInfile) != n) { if(SystemFlags::VERBOSE_MODE_ENABLED) printf("Failed reading from input file!\n"); + if(pOutfile) fclose(pOutfile); return EXIT_FAILURE; } diff --git a/source/shared_lib/sources/feathery_ftp/ftpTargetPosix.c b/source/shared_lib/sources/feathery_ftp/ftpTargetPosix.c index 4c70986c..1f39eba9 100644 --- a/source/shared_lib/sources/feathery_ftp/ftpTargetPosix.c +++ b/source/shared_lib/sources/feathery_ftp/ftpTargetPosix.c @@ -272,6 +272,7 @@ socket_t ftpEstablishDataConnection(int passive, ip_t *ip, port_t *port, int ses myAddr.sin_family = AF_INET; myAddr.sin_addr.s_addr = INADDR_ANY; myAddr.sin_port = htons(20); + myAddr.sin_zero[0] = 0; if(bind(dataSocket, (struct sockaddr *)&myAddr, sizeof(myAddr))) { if(VERBOSE_MODE_ENABLED) printf("In ftpEstablishDataConnection #2 about to Close socket = %d, for sessionId = %d\n",dataSocket, sessionId); @@ -283,6 +284,8 @@ socket_t ftpEstablishDataConnection(int passive, ip_t *ip, port_t *port, int ses clientAddr.sin_family = AF_INET; clientAddr.sin_addr.s_addr = htonl(*ip); clientAddr.sin_port = htons(*port); + clientAddr.sin_zero[0] = 0; + if(connect(dataSocket, (struct sockaddr *)&clientAddr, sizeof(clientAddr))) { if(VERBOSE_MODE_ENABLED) printf("In ftpEstablishDataConnection #3 about to Close socket = %d, for sessionId = %d\n",dataSocket, sessionId); diff --git a/source/shared_lib/sources/graphics/model.cpp b/source/shared_lib/sources/graphics/model.cpp index f5bc089a..5bde980f 100644 --- a/source/shared_lib/sources/graphics/model.cpp +++ b/source/shared_lib/sources/graphics/model.cpp @@ -800,8 +800,10 @@ void Mesh::load(int meshIndex, const string &dir, FILE *f, TextureManager *textu uint32 flag= 1; for(int i = 0; i < meshTextureCount; ++i) { if(meshHeader.textures & flag) { - uint8 cMapPath[mapPathSize]; + uint8 cMapPath[mapPathSize+1]; + memset(&cMapPath[0],0,mapPathSize+1); readBytes = fread(cMapPath, mapPathSize, 1, f); + cMapPath[mapPathSize] = 0; if(readBytes != 1 && mapPathSize != 0) { char szBuf[8096]=""; snprintf(szBuf,8096,"fread returned wrong size = " MG_SIZE_T_SPECIFIER " [%u] on line: %d.",readBytes,mapPathSize,__LINE__); @@ -810,7 +812,7 @@ void Mesh::load(int meshIndex, const string &dir, FILE *f, TextureManager *textu Shared::PlatformByteOrder::fromEndianTypeArray(cMapPath, mapPathSize); char mapPathString[mapPathSize+1]=""; - memset(&mapPathString[0],0,mapPathSize); + memset(&mapPathString[0],0,mapPathSize+1); memcpy(&mapPathString[0],reinterpret_cast(cMapPath),mapPathSize); string mapPath= toLower(mapPathString); @@ -1211,13 +1213,14 @@ void Model::loadG3d(const string &path, bool deletePixMapAfterLoad, } fromEndianFileHeader(fileHeader); - if(strncmp(reinterpret_cast(fileHeader.id), "G3D", 3) != 0) { + char fileId[4] = ""; + memset(&fileId[0],0,4); + memcpy(&fileId[0],reinterpret_cast(fileHeader.id),3); + + if(strncmp(fileId, "G3D", 3) != 0) { fclose(f); f = NULL; - char fileType[4]=""; - memset(&fileType[0],0,4); - memcpy(&fileType[0],fileHeader.id,3); - printf("In [%s::%s] file = [%s] fileheader.id = [%s][%c]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,path.c_str(),fileType,fileHeader.id[0]); + printf("In [%s::%s] file = [%s] fileheader.id = [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,path.c_str(),fileId); throw megaglest_runtime_error("Not a valid G3D model",true); } fileVersion= fileHeader.version; diff --git a/source/shared_lib/sources/graphics/texture.cpp b/source/shared_lib/sources/graphics/texture.cpp index fc8766f4..fbad22d5 100644 --- a/source/shared_lib/sources/graphics/texture.cpp +++ b/source/shared_lib/sources/graphics/texture.cpp @@ -114,6 +114,10 @@ std::pair Texture2D::CreateSDLSurface(bool newPixel /* Use the surface width and height expanded to powers of 2 */ //int w = powerOfTwo(surface->w); //int h = powerOfTwo(surface->h); + if(result.first == NULL) { + throw megaglest_runtime_error("result.first == NULL"); + } + int w = result.first->w; int h = result.first->h; diff --git a/source/shared_lib/sources/map/map_preview.cpp b/source/shared_lib/sources/map/map_preview.cpp index 066fa3e5..63701398 100644 --- a/source/shared_lib/sources/map/map_preview.cpp +++ b/source/shared_lib/sources/map/map_preview.cpp @@ -920,7 +920,9 @@ void MapPreview::saveToFile(const string &path) { header.heightFactor = heightFactor; header.waterLevel = waterLevel; strncpy(header.title, title.c_str(), MAX_TITLE_LENGTH); + header.title[MAX_TITLE_LENGTH-1] = 0; strncpy(header.author, author.c_str(), MAX_AUTHOR_LENGTH); + header.author[MAX_AUTHOR_LENGTH-1] = 0; strncpy(header.version2.short_desc, desc.c_str(), MAX_DESCRIPTION_LENGTH_VERSION2); header.version2.short_desc[MAX_DESCRIPTION_LENGTH_VERSION2-1] = 0; header.version2.magic= 0x01020304; diff --git a/source/shared_lib/sources/miniz/miniz.c b/source/shared_lib/sources/miniz/miniz.c index 626cf55e..5fd5e601 100644 --- a/source/shared_lib/sources/miniz/miniz.c +++ b/source/shared_lib/sources/miniz/miniz.c @@ -4185,7 +4185,7 @@ mz_bool mz_zip_writer_add_mem_ex(mz_zip_archive *pZip, const char *pArchive_name { mz_uint16 method = 0, dos_time = 0, dos_date = 0; mz_uint level, ext_attributes = 0, num_alignment_padding_bytes; - mz_uint64 local_dir_header_ofs = pZip->m_archive_size, cur_archive_file_ofs = pZip->m_archive_size, comp_size = 0; + mz_uint64 local_dir_header_ofs = 0, cur_archive_file_ofs = 0, comp_size = 0; size_t archive_name_size; mz_uint8 local_dir_header[MZ_ZIP_LOCAL_DIR_HEADER_SIZE]; tdefl_compressor *pComp = NULL; @@ -4200,6 +4200,9 @@ mz_bool mz_zip_writer_add_mem_ex(mz_zip_archive *pZip, const char *pArchive_name if ((!pZip) || (!pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_WRITING) || ((buf_size) && (!pBuf)) || (!pArchive_name) || ((comment_size) && (!pComment)) || (pZip->m_total_files == 0xFFFF) || (level > MZ_UBER_COMPRESSION)) return MZ_FALSE; + local_dir_header_ofs = pZip->m_archive_size; + cur_archive_file_ofs = pZip->m_archive_size; + pState = pZip->m_pState; if ((!(level_and_flags & MZ_ZIP_FLAG_COMPRESSED_DATA)) && (uncomp_size)) @@ -4336,7 +4339,7 @@ mz_bool mz_zip_writer_add_file(mz_zip_archive *pZip, const char *pArchive_name, { mz_uint uncomp_crc32 = MZ_CRC32_INIT, level, num_alignment_padding_bytes; mz_uint16 method = 0, dos_time = 0, dos_date = 0, ext_attributes = 0; - mz_uint64 local_dir_header_ofs = pZip->m_archive_size, cur_archive_file_ofs = pZip->m_archive_size, uncomp_size = 0, comp_size = 0; + mz_uint64 local_dir_header_ofs = 0, cur_archive_file_ofs = 0, uncomp_size = 0, comp_size = 0; size_t archive_name_size; mz_uint8 local_dir_header[MZ_ZIP_LOCAL_DIR_HEADER_SIZE]; MZ_FILE *pSrc_file = NULL; @@ -4352,6 +4355,9 @@ mz_bool mz_zip_writer_add_file(mz_zip_archive *pZip, const char *pArchive_name, if (!mz_zip_writer_validate_archive_name(pArchive_name)) return MZ_FALSE; + local_dir_header_ofs = pZip->m_archive_size; + cur_archive_file_ofs = pZip->m_archive_size; + archive_name_size = strlen(pArchive_name); if (archive_name_size > 0xFFFF) return MZ_FALSE; diff --git a/source/shared_lib/sources/platform/posix/socket.cpp b/source/shared_lib/sources/platform/posix/socket.cpp index a306541f..f12a140f 100644 --- a/source/shared_lib/sources/platform/posix/socket.cpp +++ b/source/shared_lib/sources/platform/posix/socket.cpp @@ -2144,17 +2144,16 @@ void BroadCastClientSocketThread::execute() { if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] unknown error\n",__FILE__,__FUNCTION__,__LINE__); } } + } #ifndef WIN32 - ::close(bcfd); - bcfd = INVALID_SOCKET; + ::close(bcfd); + bcfd = INVALID_SOCKET; #else - ::closesocket(bcfd); - bcfd = INVALID_SOCKET; + ::closesocket(bcfd); + bcfd = INVALID_SOCKET; #endif - } - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); // Here we callback into the implementer class @@ -2866,6 +2865,15 @@ void BroadCastSocketThread::execute() { //char subnetmask[MAX_NIC_COUNT][100]; // Subnet mask to broadcast to //struct hostent* myhostent=NULL; + for(unsigned int idx = 0; idx < (unsigned int)MAX_NIC_COUNT; idx++) { + memset( &bcLocal[idx], 0, sizeof( struct sockaddr_in)); + +#ifdef WIN32 + bcfd[idx] = INVALID_SOCKET; +#else + bcfd[idx] = INVALID_SOCKET; +#endif + } /* get my host name */ gethostname(myhostname,100); //struct hostent*myhostent = gethostbyname(myhostname); @@ -2926,15 +2934,16 @@ void BroadCastSocketThread::execute() { // Send this machine's host name and address in hostname:n.n.n.n format snprintf(buff,1024,"%s",myhostname); for(unsigned int idx1 = 0; idx1 < ipList.size(); idx1++) { - strcat(buff,":"); - strcat(buff,ipList[idx1].c_str()); - strcat(buff,":"); - string port_string = intToStr(this->boundPort); -#ifdef WIN32 - strncat(buff,port_string.c_str(),min((int)port_string.length(),100)); -#else - strncat(buff,port_string.c_str(),std::min((int)port_string.length(),100)); -#endif +// strcat(buff,":"); +// strcat(buff,ipList[idx1].c_str()); +// strcat(buff,":"); +// string port_string = intToStr(this->boundPort); +//#ifdef WIN32 +// strncat(buff,port_string.c_str(),min((int)port_string.length(),100)); +//#else +// strncat(buff,port_string.c_str(),std::min((int)port_string.length(),100)); +//#endif + snprintf(buff,1024,"%s:%s:%d",buff,ipList[idx1].c_str(),this->boundPort); } if(difftime((long int)time(NULL),elapsed) >= 1 && getQuitStatus() == false) { diff --git a/source/shared_lib/sources/util/util.cpp b/source/shared_lib/sources/util/util.cpp index 00d877b8..e084c7f9 100644 --- a/source/shared_lib/sources/util/util.cpp +++ b/source/shared_lib/sources/util/util.cpp @@ -506,6 +506,16 @@ void SystemFlags::logDebugEntry(DebugType type, string debugEntry, time_t debugT int idx = 1; for(idx = 1; idx <= 100; ++idx) { newlockfile = lockfile + intToStr(idx); + + if(SystemFlags::lockFile != -1) { +#ifndef WIN32 + close(SystemFlags::lockFile); +#else + _close(SystemFlags::lockFile); +#endif + SystemFlags::lockFile = -1; + SystemFlags::lockFileCountIndex = -1; + } //SystemFlags::lockFile = open(newlockfile.c_str(), O_WRONLY | O_CREAT | O_EXCL, S_IRUSR|S_IWUSR); #ifndef WIN32 SystemFlags::lockFile = open(newlockfile.c_str(), O_WRONLY | O_CREAT, S_IREAD | S_IWRITE); diff --git a/source/tools/glexemel/g2xml.c b/source/tools/glexemel/g2xml.c index 31465fa4..4bb64a25 100644 --- a/source/tools/glexemel/g2xml.c +++ b/source/tools/glexemel/g2xml.c @@ -228,11 +228,11 @@ int g3d2xml(FILE *infile, FILE *outfile) { memset(&textureName[0],0,NAMESIZE+1); nBytes = NAMESIZE; - if (fread(&textureName, nBytes, 1, infile) != 1) - { + if (fread(&textureName, nBytes, 1, infile) != 1) { printf("Could not read texture name!\n"); return FALSE; } + textureName[NAMESIZE] = 0; fprintf(outfile, "\t\t\n", textureName); }