Skip to content

Commit 6f1b828

Browse files
committedAug 9, 2016
Search for colors.txt in multiple locations, fixes #27
Locations (in order): * <world path>/colors.txt * $HOME/.minetest/colors.txt (Linux only) * <share dir>/colors.txt (Linux only for now, defaults to /usr/local/share/minetest) * current directory (<< this is the old behavior)
1 parent 73dab34 commit 6f1b828

File tree

3 files changed

+77
-15
lines changed

3 files changed

+77
-15
lines changed
 

Diff for: ‎CMakeLists.txt

+48-12
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ set(VERSION_MAJOR 1)
77
set(VERSION_MINOR 0)
88
set(VERSION_STRING "${VERSION_MAJOR}.${VERSION_MINOR}")
99

10+
# Stuff & Paths
1011

1112
if(NOT CMAKE_BUILD_TYPE)
1213
set(CMAKE_BUILD_TYPE Release CACHE STRING "Choose the type of build.")
@@ -23,7 +24,37 @@ endif(USE_CXX11)
2324
set(CMAKE_CXX_FLAGS_RELEASE "-O3 -Wall")
2425
set(CMAKE_CXX_FLAGS_DEBUG "-O0 -g2 -Wall")
2526

26-
# Find libgd
27+
if(WIN32)
28+
set(SHAREDIR ".")
29+
set(BINDIR ".")
30+
set(DOCDIR ".")
31+
else()
32+
set(SHAREDIR "${CMAKE_INSTALL_PREFIX}/share/minetest") # an extra dir. for just one file doesn't seem useful
33+
set(BINDIR "${CMAKE_INSTALL_PREFIX}/bin")
34+
set(DOCDIR "${CMAKE_INSTALL_PREFIX}/share/doc/${PROJECT_NAME}")
35+
endif()
36+
37+
set(CUSTOM_SHAREDIR "" CACHE STRING "Directory to install data files into")
38+
if(NOT CUSTOM_SHAREDIR STREQUAL "")
39+
set(SHAREDIR "${CUSTOM_SHAREDIR}")
40+
message(STATUS "Using SHAREDIR=${SHAREDIR}")
41+
endif()
42+
43+
set(CUSTOM_BINDIR "" CACHE STRING "Directory to install binaries into")
44+
if(NOT CUSTOM_BINDIR STREQUAL "")
45+
set(BINDIR "${CUSTOM_BINDIR}")
46+
message(STATUS "Using BINDIR=${BINDIR}")
47+
endif()
48+
49+
set(CUSTOM_DOCDIR "" CACHE STRING "Directory to install documentation into")
50+
if(NOT CUSTOM_DOCDIR STREQUAL "")
51+
set(DOCDIR "${CUSTOM_DOCDIR}")
52+
message(STATUS "Using DOCDIR=${DOCDIR}")
53+
endif()
54+
55+
56+
# Libraries: gd
57+
2758
find_library(LIBGD_LIBRARY gd)
2859
find_path(LIBGD_INCLUDE_DIR gd.h)
2960
message (STATUS "libgd library: ${LIBGD_LIBRARY}")
@@ -32,7 +63,8 @@ if(NOT LIBGD_LIBRARY OR NOT LIBGD_INCLUDE_DIR)
3263
message(FATAL_ERROR "libgd not found!")
3364
endif(NOT LIBGD_LIBRARY OR NOT LIBGD_INCLUDE_DIR)
3465

35-
# Find zlib
66+
# Libraries: zlib
67+
3668
find_library(ZLIB_LIBRARY z)
3769
find_path(ZLIB_INCLUDE_DIR zlib.h)
3870
message (STATUS "zlib library: ${ZLIB_LIBRARY}")
@@ -44,7 +76,8 @@ endif(NOT ZLIB_LIBRARY OR NOT ZLIB_INCLUDE_DIR)
4476
find_package(PkgConfig)
4577
include(FindPackageHandleStandardArgs)
4678

47-
# Find libsqlite3
79+
# Libraries: sqlite3
80+
4881
find_library(SQLITE3_LIBRARY sqlite3)
4982
find_path(SQLITE3_INCLUDE_DIR zlib.h)
5083
message (STATUS "sqlite3 library: ${SQLITE3_LIBRARY}")
@@ -53,7 +86,8 @@ if(NOT SQLITE3_LIBRARY OR NOT SQLITE3_INCLUDE_DIR)
5386
message(FATAL_ERROR "sqlite3 not found!")
5487
endif(NOT SQLITE3_LIBRARY OR NOT SQLITE3_INCLUDE_DIR)
5588

56-
# Find leveldb
89+
# Libraries: leveldb
90+
5791
set(USE_LEVELDB 0)
5892

5993
OPTION(ENABLE_LEVELDB "Enable LevelDB backend")
@@ -73,7 +107,8 @@ if(ENABLE_LEVELDB)
73107
endif(LEVELDB_LIBRARY AND LEVELDB_INCLUDE_DIR)
74108
endif(ENABLE_LEVELDB)
75109

76-
# Find redis
110+
# Libraries: redis
111+
77112
set(USE_REDIS 0)
78113

79114
OPTION(ENABLE_REDIS "Enable redis backend")
@@ -93,6 +128,8 @@ if(ENABLE_REDIS)
93128
endif(REDIS_LIBRARY AND REDIS_INCLUDE_DIR)
94129
endif(ENABLE_REDIS)
95130

131+
# Compiling & Linking
132+
96133
include_directories(
97134
"${PROJECT_BINARY_DIR}"
98135
"${CMAKE_CURRENT_SOURCE_DIR}"
@@ -139,12 +176,13 @@ target_link_libraries(
139176
${ZLIB_LIBRARY}
140177
)
141178

142-
install(FILES "AUTHORS" DESTINATION ".")
143-
install(FILES "COPYING" DESTINATION ".")
144-
install(FILES "README.rst" DESTINATION ".")
145-
install(FILES "colors.txt" DESTINATION ".")
179+
# Installing & Packaging
146180

147-
# CPack
181+
install(TARGETS "${PROJECT_NAME}" DESTINATION "${BINDIR}")
182+
install(FILES "AUTHORS" DESTINATION "${DOCDIR}")
183+
install(FILES "COPYING" DESTINATION "${DOCDIR}")
184+
install(FILES "README.rst" DESTINATION "${DOCDIR}")
185+
install(FILES "colors.txt" DESTINATION "${SHAREDIR}")
148186

149187
set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "Overview mapper for Minetest")
150188
set(CPACK_PACKAGE_VERSION_MAJOR ${VERSION_MAJOR})
@@ -153,11 +191,9 @@ set(CPACK_PACKAGE_VENDOR "celeron55")
153191
set(CPACK_PACKAGE_CONTACT "Perttu Ahola <celeron55@gmail.com>")
154192

155193
if(WIN32)
156-
install(FILES "${PROJECT_BINARY_DIR}/minetestmapper.exe" DESTINATION ".")
157194
set(CPACK_PACKAGE_FILE_NAME "${PROJECT_NAME}-${VERSION_STRING}-win32")
158195
set(CPACK_GENERATOR ZIP)
159196
else()
160-
install(FILES "${PROJECT_BINARY_DIR}/minetestmapper" DESTINATION ".")
161197
set(CPACK_PACKAGE_FILE_NAME "${PROJECT_NAME}-${VERSION_STRING}-linux")
162198
set(CPACK_GENERATOR TGZ)
163199
set(CPACK_SOURCE_GENERATOR TGZ)

Diff for: ‎cmake_config.h.in

+2
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,7 @@
88

99
#define USE_CXX11 @USE_CXX11@
1010

11+
#define SHAREDIR "@SHAREDIR@"
12+
1113
#endif
1214

Diff for: ‎mapper.cpp

+27-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
#include <cstdlib>
22
#include <getopt.h>
3+
#include <fstream>
34
#include <iostream>
45
#include <map>
56
#include <string>
67
#include <sstream>
78
#include <stdexcept>
9+
#include "cmake_config.h"
810
#include "TileGenerator.h"
911

1012
void usage()
@@ -31,9 +33,30 @@ void usage()
3133
std::cout << usage_text;
3234
}
3335

34-
std::string search_colors()
36+
bool file_exists(const std::string &path)
3537
{
36-
// TBD
38+
std::ifstream ifs(path.c_str());
39+
return ifs.is_open();
40+
}
41+
42+
std::string search_colors(const std::string &worldpath)
43+
{
44+
if(file_exists(worldpath + "/colors.txt"))
45+
return worldpath + "/colors.txt";
46+
47+
#ifndef _WIN32
48+
char *home = std::getenv("HOME");
49+
if(home) {
50+
std::string check = ((std::string) home) + "/.minetest/colors.txt";
51+
if(file_exists(check))
52+
return check;
53+
}
54+
#endif
55+
56+
if(!(SHAREDIR[0] == '.' || SHAREDIR[0] == '\0') && file_exists(SHAREDIR "/colors.txt"))
57+
return SHAREDIR "/colors.txt";
58+
59+
std::cerr << "Warning: Falling back to using colors.txt from current directory." << std::endl;
3760
return "colors.txt";
3861
}
3962

@@ -163,7 +186,8 @@ int main(int argc, char *argv[])
163186
}
164187
}
165188
if(colors == "")
166-
colors = search_colors();
189+
colors = search_colors(input);
190+
std::cerr << "is at " << colors << std::endl;
167191
try {
168192
generator.parseColorsFile(colors);
169193
generator.generate(input, output);

0 commit comments

Comments
 (0)
Please sign in to comment.