Improve colors file searching on Windows

On Windows, minetestmapper now uses the colors files from the
directory where it is installed.

If the last directory is named 'bin', that part is stripped,
and the resulting directory, and a subdirectory named 'colors'
are searched.

All depends on the whether on invocation, argv[0] contains the full
path of the exe file. It works under Wine when compiled with MinGW.

I haven't been able to test on a real copy of windows, or when
compiled using msvc, but I assume that will work too.
master
Rogier 2015-03-03 11:21:57 +01:00
parent f853915404
commit 10be8ebfe6
2 changed files with 74 additions and 14 deletions

View File

@ -1427,33 +1427,56 @@ Colors Files Search Locations
-----------------------------
When minetestmapper needs a colors file (colors.txt, heightmap-nodes.txt and
heightmap-colors.txt), it will search for in a few predefined locations, which
depend on your system and the way minetestmapper was built. In general, the
following locations can be searched:
heightmap-colors.txt), it will search for it in a few predefined locations, which
depend on the system it was built for, and the way minetestmapper was built.
In general, the following locations can be searched (ordered from most preferred
to least preferred):
In order to find out exactly where a specific copy of minetestmapper did look
for its files, use the option ``--verbose-search-colors=2``,
* The file specified on the command line. If a colors file of the appropriate type
was specified on the command-line, that file is used and no further locations
are searched, even if it does not exist, or cannot be found.
* The directory of the world being mapped
* The directory two levels up from the directory of the world being mapped,
(i.e. the global minetest configuration directory) provided that that directory
* The directory two levels up from the directory of the world being mapped
(which would be the minetest configuration directory), provided that that directory
contains a file 'minetest.conf'
* The user's private minetest directory (``$HOME/.minetest``) - if the environment
variable ``$HOME`` exists.
variable ``$HOME`` exists. (it would probably be called ``%HOME%`` on Windows).
NOTE: on Windows, it would be more sensible to use ``%USERPROFILE%``, and search
another subdirectory than ``.minetest``. Please advise me about a suitable directory
to search - if at all (I am not a Windows user - I don't even own a copy of Windows...).
* On Windows only: if minetestmapper can determine its own location, which would
have one of the following formats:
``<path-with-drive>\bin\minetestmapper.exe``
``<path-with-drive>\minetestmapper.exe``
It searches the following directories:
* The directory ``<path-with-drive>\colors\``
* The directory ``<path-with-drive>\``
I.e.: if the last directory is '``bin``' (or '``BIN``', etc.), then that part
is removed from the path, and then the resulting path, with and without
'``colors``' appended, is searched.
* The system directory corresponding to the location where minetestmapper
is installed. Usually, this would be ``/usr/share/games/minetestmapper/``
or ``/usr/local/share/games/minetestmapper/``.
or ``/usr/local/share/games/minetestmapper/``. This location was configured
at *compile time*: moving minetestmapper around will not affect the search location.
* For compatibility, in the current directory as a last resort.
This causes a warning message to be printed.
If the location of a colors file was specified using the appropriate option
on the command-line, no further locations are searched for that type of
colors file.
In order to find out exactly where a specific copy of minetestmapper searched
its files, use the option ``--verbose-search-colors=2``,
More Information
================

View File

@ -42,6 +42,7 @@ using namespace std;
// Will be replaced with the actual name and location of the executable (if found)
string executableName = "minetestmapper";
string executablePath; // ONLY for use on windows
string installPrefix = INSTALL_PREFIX;
string nodeColorsDefaultFile = "colors.txt";
string heightMapNodesDefaultFile = "heightmap-nodes.txt";
@ -170,9 +171,43 @@ void parseDataFile(TileGenerator &generator, const string &input, string dataFil
if ((homedir = getenv("HOME"))) {
colorPaths.push_back(string(homedir) + PATH_SEPARATOR + ".minetest");
}
// TODO: test/verify this (probably another subdirectory ('application data' or so) should be preferred)
//#if MSDOS || __OS2__ || __NT__ || _WIN32
// if ((homedir = getenv("USERPROFILE"))) {
// colorPaths.push_back(string(homedir) + PATH_SEPARATOR + ".minetest");
// }
//#endif
#if MSDOS || __OS2__ || __NT__ || _WIN32 || DEBUG
// On windows, assume that argv[0] contains the full path location of minetestmapper.exe
// (i.e. where it is installed)
// On Unix, the path is usually absent from argv[0], and we don't want the behavior to
// depend on how it was invoked anyway.
// In DEBUG mode, do check the command-line path; so this code can at least be tested on
// Linux...
if (executablePath != "") {
size_t binpos = executablePath.find_last_of(PATH_SEPARATOR);
if (binpos != string::npos) {
string lastDir = executablePath.substr(binpos + 1);
for (size_t i=0; i < lastDir.size(); i++)
lastDir[i] = tolower(lastDir[i]);
if (lastDir == "bin") {
colorPaths.push_back(executablePath.substr(0, binpos) + PATH_SEPARATOR + "colors");
colorPaths.push_back(executablePath.substr(0, binpos));
}
else {
colorPaths.push_back(executablePath);
colorPaths.push_back(executablePath + PATH_SEPARATOR + "colors");
}
}
else {
colorPaths.push_back(executablePath);
}
}
#endif
if (!installPrefix.empty()) {
#if PACKAGING_FLAT
colorPaths.push_back(installPrefix + PATH_SEPARATOR + "colors");
colorPaths.push_back(installPrefix);
#else
colorPaths.push_back(installPrefix + "/share/games/minetestmapper");
@ -529,9 +564,11 @@ int main(int argc, char *argv[])
if (pos == string::npos) {
if (!argv0.empty())
executableName = argv0;
executablePath = "";
}
else {
executableName = argv0.substr(pos + 1);
executablePath = argv0.substr(0, pos);
}
}