From 6f1b8284f4ba62a52cda07993dfaafaf3c3798be Mon Sep 17 00:00:00 2001 From: sfan5 Date: Tue, 9 Aug 2016 17:31:56 +0200 Subject: [PATCH] Search for colors.txt in multiple locations, fixes #27 Locations (in order): * /colors.txt * $HOME/.minetest/colors.txt (Linux only) * /colors.txt (Linux only for now, defaults to /usr/local/share/minetest) * current directory (<< this is the old behavior) --- CMakeLists.txt | 60 +++++++++++++++++++++++++++++++++++++---------- cmake_config.h.in | 2 ++ mapper.cpp | 30 +++++++++++++++++++++--- 3 files changed, 77 insertions(+), 15 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8d727bc..75565e6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,6 +7,7 @@ set(VERSION_MAJOR 1) set(VERSION_MINOR 0) set(VERSION_STRING "${VERSION_MAJOR}.${VERSION_MINOR}") +# Stuff & Paths if(NOT CMAKE_BUILD_TYPE) set(CMAKE_BUILD_TYPE Release CACHE STRING "Choose the type of build.") @@ -23,7 +24,37 @@ endif(USE_CXX11) set(CMAKE_CXX_FLAGS_RELEASE "-O3 -Wall") set(CMAKE_CXX_FLAGS_DEBUG "-O0 -g2 -Wall") -# Find libgd +if(WIN32) + set(SHAREDIR ".") + set(BINDIR ".") + set(DOCDIR ".") +else() + set(SHAREDIR "${CMAKE_INSTALL_PREFIX}/share/minetest") # an extra dir. for just one file doesn't seem useful + set(BINDIR "${CMAKE_INSTALL_PREFIX}/bin") + set(DOCDIR "${CMAKE_INSTALL_PREFIX}/share/doc/${PROJECT_NAME}") +endif() + +set(CUSTOM_SHAREDIR "" CACHE STRING "Directory to install data files into") +if(NOT CUSTOM_SHAREDIR STREQUAL "") + set(SHAREDIR "${CUSTOM_SHAREDIR}") + message(STATUS "Using SHAREDIR=${SHAREDIR}") +endif() + +set(CUSTOM_BINDIR "" CACHE STRING "Directory to install binaries into") +if(NOT CUSTOM_BINDIR STREQUAL "") + set(BINDIR "${CUSTOM_BINDIR}") + message(STATUS "Using BINDIR=${BINDIR}") +endif() + +set(CUSTOM_DOCDIR "" CACHE STRING "Directory to install documentation into") +if(NOT CUSTOM_DOCDIR STREQUAL "") + set(DOCDIR "${CUSTOM_DOCDIR}") + message(STATUS "Using DOCDIR=${DOCDIR}") +endif() + + +# Libraries: gd + find_library(LIBGD_LIBRARY gd) find_path(LIBGD_INCLUDE_DIR gd.h) message (STATUS "libgd library: ${LIBGD_LIBRARY}") @@ -32,7 +63,8 @@ if(NOT LIBGD_LIBRARY OR NOT LIBGD_INCLUDE_DIR) message(FATAL_ERROR "libgd not found!") endif(NOT LIBGD_LIBRARY OR NOT LIBGD_INCLUDE_DIR) -# Find zlib +# Libraries: zlib + find_library(ZLIB_LIBRARY z) find_path(ZLIB_INCLUDE_DIR zlib.h) message (STATUS "zlib library: ${ZLIB_LIBRARY}") @@ -44,7 +76,8 @@ endif(NOT ZLIB_LIBRARY OR NOT ZLIB_INCLUDE_DIR) find_package(PkgConfig) include(FindPackageHandleStandardArgs) -# Find libsqlite3 +# Libraries: sqlite3 + find_library(SQLITE3_LIBRARY sqlite3) find_path(SQLITE3_INCLUDE_DIR zlib.h) message (STATUS "sqlite3 library: ${SQLITE3_LIBRARY}") @@ -53,7 +86,8 @@ if(NOT SQLITE3_LIBRARY OR NOT SQLITE3_INCLUDE_DIR) message(FATAL_ERROR "sqlite3 not found!") endif(NOT SQLITE3_LIBRARY OR NOT SQLITE3_INCLUDE_DIR) -# Find leveldb +# Libraries: leveldb + set(USE_LEVELDB 0) OPTION(ENABLE_LEVELDB "Enable LevelDB backend") @@ -73,7 +107,8 @@ if(ENABLE_LEVELDB) endif(LEVELDB_LIBRARY AND LEVELDB_INCLUDE_DIR) endif(ENABLE_LEVELDB) -# Find redis +# Libraries: redis + set(USE_REDIS 0) OPTION(ENABLE_REDIS "Enable redis backend") @@ -93,6 +128,8 @@ if(ENABLE_REDIS) endif(REDIS_LIBRARY AND REDIS_INCLUDE_DIR) endif(ENABLE_REDIS) +# Compiling & Linking + include_directories( "${PROJECT_BINARY_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}" @@ -139,12 +176,13 @@ target_link_libraries( ${ZLIB_LIBRARY} ) -install(FILES "AUTHORS" DESTINATION ".") -install(FILES "COPYING" DESTINATION ".") -install(FILES "README.rst" DESTINATION ".") -install(FILES "colors.txt" DESTINATION ".") +# Installing & Packaging -# CPack +install(TARGETS "${PROJECT_NAME}" DESTINATION "${BINDIR}") +install(FILES "AUTHORS" DESTINATION "${DOCDIR}") +install(FILES "COPYING" DESTINATION "${DOCDIR}") +install(FILES "README.rst" DESTINATION "${DOCDIR}") +install(FILES "colors.txt" DESTINATION "${SHAREDIR}") set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "Overview mapper for Minetest") set(CPACK_PACKAGE_VERSION_MAJOR ${VERSION_MAJOR}) @@ -153,11 +191,9 @@ set(CPACK_PACKAGE_VENDOR "celeron55") set(CPACK_PACKAGE_CONTACT "Perttu Ahola ") if(WIN32) - install(FILES "${PROJECT_BINARY_DIR}/minetestmapper.exe" DESTINATION ".") set(CPACK_PACKAGE_FILE_NAME "${PROJECT_NAME}-${VERSION_STRING}-win32") set(CPACK_GENERATOR ZIP) else() - install(FILES "${PROJECT_BINARY_DIR}/minetestmapper" DESTINATION ".") set(CPACK_PACKAGE_FILE_NAME "${PROJECT_NAME}-${VERSION_STRING}-linux") set(CPACK_GENERATOR TGZ) set(CPACK_SOURCE_GENERATOR TGZ) diff --git a/cmake_config.h.in b/cmake_config.h.in index 6858456..e0d9bb1 100644 --- a/cmake_config.h.in +++ b/cmake_config.h.in @@ -8,5 +8,7 @@ #define USE_CXX11 @USE_CXX11@ +#define SHAREDIR "@SHAREDIR@" + #endif diff --git a/mapper.cpp b/mapper.cpp index 0f60eb9..ab2376e 100644 --- a/mapper.cpp +++ b/mapper.cpp @@ -1,10 +1,12 @@ #include #include +#include #include #include #include #include #include +#include "cmake_config.h" #include "TileGenerator.h" void usage() @@ -31,9 +33,30 @@ void usage() std::cout << usage_text; } -std::string search_colors() +bool file_exists(const std::string &path) { - // TBD + std::ifstream ifs(path.c_str()); + return ifs.is_open(); +} + +std::string search_colors(const std::string &worldpath) +{ + if(file_exists(worldpath + "/colors.txt")) + return worldpath + "/colors.txt"; + +#ifndef _WIN32 + char *home = std::getenv("HOME"); + if(home) { + std::string check = ((std::string) home) + "/.minetest/colors.txt"; + if(file_exists(check)) + return check; + } +#endif + + if(!(SHAREDIR[0] == '.' || SHAREDIR[0] == '\0') && file_exists(SHAREDIR "/colors.txt")) + return SHAREDIR "/colors.txt"; + + std::cerr << "Warning: Falling back to using colors.txt from current directory." << std::endl; return "colors.txt"; } @@ -163,7 +186,8 @@ int main(int argc, char *argv[]) } } if(colors == "") - colors = search_colors(); + colors = search_colors(input); + std::cerr << "is at " << colors << std::endl; try { generator.parseColorsFile(colors); generator.generate(input, output);