]> git.lizzy.rs Git - minetest.git/blob - src/porting.cpp
mapgen tweaking
[minetest.git] / src / porting.cpp
1 /*
2 Minetest-c55
3 Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 /*
21         Random portability stuff
22
23         See comments in porting.h
24 */
25
26 #include "porting.h"
27 #include "config.h"
28
29 namespace porting
30 {
31
32 std::string path_data = "../data";
33 std::string path_userdata = "../";
34
35 void pathRemoveFile(char *path, char delim)
36 {
37         // Remove filename and path delimiter
38         int i;
39         for(i = strlen(path)-1; i>=0; i--)
40         {
41                 if(path[i] == delim)
42                         break;
43         }
44         path[i] = 0;
45 }
46
47 void initializePaths()
48 {
49 #ifdef RUN_IN_PLACE
50         /*
51                 Use relative paths if RUN_IN_PLACE
52         */
53
54         dstream<<"Using relative paths (RUN_IN_PLACE)"<<std::endl;
55
56         /*
57                 Windows
58         */
59         #if defined(_WIN32)
60                 #include <windows.h>
61
62         const DWORD buflen = 1000;
63         char buf[buflen];
64         DWORD len;
65         
66         // Find path of executable and set path_data relative to it
67         len = GetModuleFileName(GetModuleHandle(NULL), buf, buflen);
68         assert(len < buflen);
69         pathRemoveFile(buf, '\\');
70
71         // Use "./bin/../data"
72         path_data = std::string(buf) + "/../data";
73                 
74         // Use "./bin/../"
75         path_userdata = std::string(buf) + "/../";
76
77         /*
78                 Linux
79         */
80         #elif defined(linux)
81                 #include <unistd.h>
82         
83         char buf[BUFSIZ];
84         // Get path to executable
85         readlink("/proc/self/exe", buf, BUFSIZ);
86         
87         pathRemoveFile(buf, '/');
88
89         // Use "./bin/../data"
90         path_data = std::string(buf) + "/../data";
91                 
92         // Use "./bin/../"
93         path_userdata = std::string(buf) + "/../";
94         
95         /*
96                 OS X
97         */
98         #elif defined(__APPLE__)
99         
100         //TODO: Get path of executable. This assumes working directory is bin/
101         dstream<<"WARNING: Relative path not properly supported on OS X"
102                         <<std::endl;
103         path_data = std::string("../data");
104         path_userdata = std::string("../");
105
106         #endif
107
108 #else // RUN_IN_PLACE
109
110         /*
111                 Use platform-specific paths otherwise
112         */
113
114         dstream<<"Using system-wide paths (NOT RUN_IN_PLACE)"<<std::endl;
115
116         /*
117                 Windows
118         */
119         #if defined(_WIN32)
120                 #include <windows.h>
121
122         const DWORD buflen = 1000;
123         char buf[buflen];
124         DWORD len;
125         
126         // Find path of executable and set path_data relative to it
127         len = GetModuleFileName(GetModuleHandle(NULL), buf, buflen);
128         assert(len < buflen);
129         pathRemoveFile(buf, '\\');
130         
131         // Use "./bin/../data"
132         path_data = std::string(buf) + "/../data";
133         //path_data = std::string(buf) + "/../share/" + APPNAME;
134                 
135         // Use "C:\Documents and Settings\user\Application Data\<APPNAME>"
136         len = GetEnvironmentVariable("APPDATA", buf, buflen);
137         assert(len < buflen);
138         path_userdata = std::string(buf) + "/" + APPNAME;
139
140         /*
141                 Linux
142         */
143         #elif defined(linux)
144                 #include <unistd.h>
145         
146         char buf[BUFSIZ];
147         // Get path to executable
148         readlink("/proc/self/exe", buf, BUFSIZ);
149         
150         pathRemoveFile(buf, '/');
151
152         path_data = std::string(buf) + "/../share/" + APPNAME;
153         //path_data = std::string(INSTALL_PREFIX) + "/share/" + APPNAME;
154         
155         path_userdata = std::string(getenv("HOME")) + "/." + APPNAME;
156
157         /*
158                 OS X
159         */
160         #elif defined(__APPLE__)
161                 #include <unistd.h>
162
163         path_userdata = std::string(getenv("HOME")) + "/Library/Application Support/" + APPNAME;
164         path_data = std::string("minetest-mac.app/Contents/Resources/data/");
165
166         #endif
167
168 #endif // RUN_IN_PLACE
169
170         dstream<<"path_data = "<<path_data<<std::endl;
171         dstream<<"path_userdata = "<<path_userdata<<std::endl;
172 }
173
174 } //namespace porting
175