]> git.jsancho.org Git - lugaru.git/blob - Source/MacCompatibility.cpp
First step at cleaning image loading.
[lugaru.git] / Source / MacCompatibility.cpp
1 /*
2 Copyright (C) 2003, 2010 - Wolfire Games
3
4 This file is part of Lugaru.
5
6 Lugaru is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 Lugaru is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Lugaru.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 /**> HEADER FILES <**/
21 #include "MacCompatibility.h"
22
23 #ifdef WIN32
24 #include <windows.h>
25 #endif
26
27 #include <errno.h>
28 #include <time.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32
33 #if PLATFORM_UNIX
34 #include <unistd.h>
35 #include <sys/time.h>
36 #include <sys/stat.h>
37 #include <assert.h>
38 typedef long long __int64;
39 typedef __int64 LARGE_INTEGER;
40 static int QueryPerformanceFrequency(LARGE_INTEGER *liptr)
41 {
42     assert(sizeof (__int64) == 8);
43     assert(sizeof (LARGE_INTEGER) == 8);
44     *liptr = 1000;
45     return(1);
46 }
47
48 static int QueryPerformanceCounter(LARGE_INTEGER *liptr)
49 {
50     struct timeval tv;
51     gettimeofday(&tv, NULL);
52     *liptr = ( (((LARGE_INTEGER) tv.tv_sec) * 1000) +
53                (((LARGE_INTEGER) tv.tv_usec) / 1000) );
54     return(1);
55 }
56 #endif
57
58 class AppTime
59 {
60 public:
61     AppTime() {
62         counterRate = 1;
63         baseCounter = 0;
64         QueryPerformanceFrequency( (LARGE_INTEGER*)&counterRate);
65         QueryPerformanceCounter( (LARGE_INTEGER*)&baseCounter);
66     }
67     __int64 counterRate; // LARGE_INTEGER type has no math functions so use int64
68     __int64 baseCounter;
69 };
70 static AppTime g_appTime;
71
72 AbsoluteTime UpTime()
73 {
74     __int64 counter;
75     QueryPerformanceCounter( (LARGE_INTEGER*)&counter);
76
77     counter -= g_appTime.baseCounter;
78
79     AbsoluteTime time;
80     time.lo = (unsigned long)counter;
81     time.hi = (unsigned long)(counter >> 32);
82     return time;
83 }
84
85
86 Duration AbsoluteDeltaToDuration( AbsoluteTime& a, AbsoluteTime& b)
87 {
88     __int64 value = a.hi;
89     value <<= 32;
90     value |= a.lo;
91     __int64 value2 = b.hi;
92     value2 <<= 32;
93     value2 |= b.lo;
94     value -= value2;
95
96     if (value <= 0)
97         return durationImmediate;
98
99     __int64 frac = value % g_appTime.counterRate;
100     value /= g_appTime.counterRate;
101
102     Duration time;
103
104     if (value == 0) {
105         frac *= -1000000;
106         frac /= g_appTime.counterRate;
107         time = (Duration)frac;
108     } else {
109         frac *= 1000;
110         frac /= g_appTime.counterRate;
111         value *= 1000;
112         value += frac;
113         time = (Duration)value;
114     }
115
116     return time;
117 }
118
119
120 #if PLATFORM_UNIX
121 #include <sys/types.h>
122 #include <pwd.h>
123 #include <fcntl.h>
124 #include <unistd.h>
125 #include <dirent.h>
126
127 // some but not all of this is code from PhysicsFS: http://icculus.org/physfs/
128 //  the zlib license on physfs allows this cut-and-pasting.
129 static int locateOneElement(char *buf)
130 {
131     char *ptr;
132     DIR *dirp;
133
134     //if (PHYSFS_exists(buf))
135     if (access(buf, F_OK) == 0)
136         return(1);  /* quick rejection: exists in current case. */
137
138     ptr = strrchr(buf, '/');  /* find entry at end of path. */
139     if (ptr == NULL) {
140         dirp = opendir(".");
141         ptr = buf;
142     } /* if */
143     else {
144         *ptr = '\0';
145         dirp = opendir(buf);
146         *ptr = '/';
147         ptr++;  /* point past dirsep to entry itself. */
148     } /* else */
149
150     struct dirent *dent;
151     while ((dent = readdir(dirp)) != NULL) {
152         if (strcasecmp(dent->d_name, ptr) == 0) {
153             strcpy(ptr, dent->d_name); /* found a match. Overwrite with this case. */
154             closedir(dirp);
155             return(1);
156         } /* if */
157     } /* for */
158
159     /* no match at all... */
160     closedir(dirp);
161     return(0);
162 } /* locateOneElement */
163
164
165 static inline const char *getUserDirByUID(void)
166 {
167     struct passwd *pw = getpwuid(getuid());
168     if (pw != NULL)
169         return(pw->pw_dir);
170     return(NULL);
171 } /* getUserDirByUID */
172
173
174 static inline const char *getPrefPath(void)
175 {
176     static char *prefpath = NULL;
177     if (prefpath == NULL) {
178         const char *homedir = getenv("HOME");
179         if (homedir == NULL)
180             homedir = getUserDirByUID();
181         if (homedir == NULL)
182             homedir = ".";  // oh well.
183
184 #if (defined(__APPLE__) && defined(__MACH__))
185         const char *PREFPATHNAME = "Library/Application Support/Lugaru";
186 #else
187         const char *PREFPATHNAME = ".lugaru";
188 #endif
189         size_t len = strlen(homedir) + strlen(PREFPATHNAME) + 2;
190         prefpath = new char[len];
191         snprintf(prefpath, len, "%s/%s", homedir, PREFPATHNAME);
192     }
193     return(prefpath);
194 }
195
196 static int locateCorrectCase(char *buf, bool makedirs)
197 {
198     int rc;
199     char *ptr = buf;
200
201     while (ptr = strchr(ptr + 1, '/')) {
202         *ptr = '\0';  /* block this path section off */
203         rc = locateOneElement(buf);
204         if (!rc) {
205             if (makedirs)  /* normal if we're writing; build dirs! */
206                 mkdir(buf, S_IRWXU);
207             else {
208                 *ptr = '/'; /* restore path separator */
209                 return(-2);  /* missing element in path. */
210             } /* else */
211         } /* if */
212         *ptr = '/'; /* restore path separator */
213     } /* while */
214
215     /* check final element... */
216     return(locateOneElement(buf) ? 0 : -1);
217 }
218
219
220 static int locateCorrectFile(char *buf, const char *mode)
221 {
222     if (*buf == '\0')
223         return(0);  /* Uh...I guess that's failure. */
224
225     assert((mode[0] == 'w') || (mode[0] == 'r'));
226
227     bool iswriting = (mode[0] == 'w');
228     const char *prefpath = getPrefPath();
229     size_t len = strlen(buf) + strlen(prefpath) + 2;
230     char *prefpathfile = (char *) alloca(len);
231     snprintf(prefpathfile, len, "%s/%s", prefpath, buf);
232
233     int rc = locateCorrectCase(prefpathfile, iswriting);  /* favor prefpath. */
234     if ( (rc == 0) || ((rc == -1) && (iswriting)) ) // found or create?
235         strcpy(buf, prefpathfile);
236     else if ((rc < 0) && (!iswriting))  /* not writing? Try game dir... */
237         rc = locateCorrectCase(buf, iswriting);
238
239     return(rc);
240 } /* locateCorrectFile */
241 #endif
242
243
244 static char g_filename[4096];
245 char* ConvertFileName( const char* orgfilename, const char *mode)
246 {
247     if (orgfilename == g_filename) // recursion?
248         return g_filename;
249
250     // translate filename into proper path name
251     if (orgfilename[ 0] == ':')
252         orgfilename++;
253     strcpy( g_filename, orgfilename);
254
255     for (int n = 0; g_filename[ n]; n++) {
256         if (g_filename[ n] == ':')
257             g_filename[ n] = '/';
258
259         else if (g_filename[ n] == '\\')
260             g_filename[ n] = '/';
261     }
262
263 #if PLATFORM_UNIX
264     locateCorrectFile(g_filename, mode);
265 #endif
266
267     return g_filename;
268 }