]> git.lizzy.rs Git - minetest.git/blob - src/porting.cpp
fixed bug in sneaking
[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         memset(buf, 0, BUFSIZ);
85         // Get path to executable
86         readlink("/proc/self/exe", buf, BUFSIZ-1);
87         
88         pathRemoveFile(buf, '/');
89
90         // Use "./bin/../data"
91         path_data = std::string(buf) + "/../data";
92                 
93         // Use "./bin/../"
94         path_userdata = std::string(buf) + "/../";
95         
96         /*
97                 OS X
98         */
99         #elif defined(__APPLE__)
100         
101         //TODO: Get path of executable. This assumes working directory is bin/
102         dstream<<"WARNING: Relative path not properly supported on OS X"
103                         <<std::endl;
104         path_data = std::string("../data");
105         path_userdata = std::string("../");
106
107         #endif
108
109 #else // RUN_IN_PLACE
110
111         /*
112                 Use platform-specific paths otherwise
113         */
114
115         dstream<<"Using system-wide paths (NOT RUN_IN_PLACE)"<<std::endl;
116
117         /*
118                 Windows
119         */
120         #if defined(_WIN32)
121                 #include <windows.h>
122
123         const DWORD buflen = 1000;
124         char buf[buflen];
125         DWORD len;
126         
127         // Find path of executable and set path_data relative to it
128         len = GetModuleFileName(GetModuleHandle(NULL), buf, buflen);
129         assert(len < buflen);
130         pathRemoveFile(buf, '\\');
131         
132         // Use "./bin/../data"
133         path_data = std::string(buf) + "/../data";
134         //path_data = std::string(buf) + "/../share/" + APPNAME;
135                 
136         // Use "C:\Documents and Settings\user\Application Data\<APPNAME>"
137         len = GetEnvironmentVariable("APPDATA", buf, buflen);
138         assert(len < buflen);
139         path_userdata = std::string(buf) + "/" + APPNAME;
140
141         /*
142                 Linux
143         */
144         #elif defined(linux)
145                 #include <unistd.h>
146         
147         char buf[BUFSIZ];
148         memset(buf, 0, BUFSIZ);
149         // Get path to executable
150         readlink("/proc/self/exe", buf, BUFSIZ-1);
151         
152         pathRemoveFile(buf, '/');
153
154         path_data = std::string(buf) + "/../share/" + APPNAME;
155         //path_data = std::string(INSTALL_PREFIX) + "/share/" + APPNAME;
156         
157         path_userdata = std::string(getenv("HOME")) + "/." + APPNAME;
158
159         /*
160                 OS X
161         */
162         #elif defined(__APPLE__)
163                 #include <unistd.h>
164
165         path_userdata = std::string(getenv("HOME")) + "/Library/Application Support/" + APPNAME;
166         path_data = std::string("minetest-mac.app/Contents/Resources/data/");
167
168         #endif
169
170 #endif // RUN_IN_PLACE
171
172         dstream<<"path_data = "<<path_data<<std::endl;
173         dstream<<"path_userdata = "<<path_userdata<<std::endl;
174 }
175
176 } //namespace porting
177