author | bjarni |
Wed, 22 Dec 2004 21:12:36 +0000 | |
changeset 770 | 89dab23f04ca |
parent 704 | a526dc96fbfc |
child 826 | 0e2b569b737b |
permissions | -rw-r--r-- |
0 | 1 |
#include "stdafx.h" |
2 |
#include "ttd.h" |
|
507
8aa8100b0b22
(svn r815) Include strings.h only in the files which need it.
tron
parents:
437
diff
changeset
|
3 |
#include "table/strings.h" |
0 | 4 |
#include "hal.h" |
5 |
||
6 |
#include <dirent.h> |
|
7 |
#include <unistd.h> |
|
8 |
#include <sys/stat.h> |
|
9 |
#include <time.h> |
|
10 |
#include <pwd.h> |
|
11 |
||
12 |
#if defined(__linux__) |
|
13 |
#include <sys/statvfs.h> |
|
14 |
#endif |
|
15 |
||
16 |
||
17 |
#if defined(WITH_SDL) |
|
18 |
#include <SDL.h> |
|
19 |
#endif |
|
20 |
||
21 |
#ifdef __MORPHOS__ |
|
22 |
#include <exec/types.h> |
|
23 |
ULONG __stack = (1024*1024)*2; // maybe not that much is needed actually ;) |
|
24 |
#endif /* __MORPHOS__ */ |
|
25 |
||
770
89dab23f04ca
(svn r1236) MorphOS: added make release like in OSX (tokai)
bjarni
parents:
704
diff
changeset
|
26 |
#ifdef __AMIGA__ |
89dab23f04ca
(svn r1236) MorphOS: added make release like in OSX (tokai)
bjarni
parents:
704
diff
changeset
|
27 |
#warning add stack symbol to avoid that user needs to set stack manually (tokai) |
89dab23f04ca
(svn r1236) MorphOS: added make release like in OSX (tokai)
bjarni
parents:
704
diff
changeset
|
28 |
// ULONG __stack = |
89dab23f04ca
(svn r1236) MorphOS: added make release like in OSX (tokai)
bjarni
parents:
704
diff
changeset
|
29 |
#endif |
89dab23f04ca
(svn r1236) MorphOS: added make release like in OSX (tokai)
bjarni
parents:
704
diff
changeset
|
30 |
|
0 | 31 |
static char *_fios_path; |
32 |
static char *_fios_save_path; |
|
33 |
static char *_fios_scn_path; |
|
34 |
static FiosItem *_fios_items; |
|
35 |
static int _fios_count, _fios_alloc; |
|
36 |
||
37 |
static FiosItem *FiosAlloc() |
|
38 |
{ |
|
39 |
if (_fios_count == _fios_alloc) { |
|
40 |
_fios_alloc += 256; |
|
41 |
_fios_items = realloc(_fios_items, _fios_alloc * sizeof(FiosItem)); |
|
42 |
} |
|
43 |
return &_fios_items[_fios_count++]; |
|
44 |
} |
|
45 |
||
46 |
int compare_FiosItems (const void *a, const void *b) { |
|
47 |
const FiosItem *da = (const FiosItem *) a; |
|
48 |
const FiosItem *db = (const FiosItem *) b; |
|
49 |
int r; |
|
50 |
||
51 |
if (_savegame_sort_order < 2) // sort by date |
|
164
0cbdf3c9bde1
(svn r165) -Feature: Option to sort vehicles in vehicle-list window by different criteria. Total independent sort for all types and players. Periodic resort of list every 10 TTD days. Thank you for your graphical inspiration follow and buxo (since none of you provided any code).
darkvater
parents:
0
diff
changeset
|
52 |
r = da->mtime < db->mtime ? -1 : 1; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
179
diff
changeset
|
53 |
else |
437
b66c60ebaa8c
(svn r646) -Fix: braindead case-sensitive file extension filter replaced with a case-INsensitive one (joint effort of Bjarni and Darkvater).
darkvater
parents:
410
diff
changeset
|
54 |
r = strcasecmp(da->title[0] ? da->title : da->name, db->title[0] ? db->title : db->name); |
0 | 55 |
|
56 |
if (_savegame_sort_order & 1) r = -r; |
|
57 |
return r; |
|
58 |
} |
|
59 |
||
60 |
||
61 |
// Get a list of savegames |
|
62 |
FiosItem *FiosGetSavegameList(int *num, int mode) |
|
63 |
{ |
|
64 |
FiosItem *fios; |
|
65 |
DIR *dir; |
|
66 |
struct dirent *dirent; |
|
67 |
struct stat sb; |
|
68 |
int sort_start; |
|
69 |
char filename[MAX_PATH]; |
|
70 |
||
71 |
if (_fios_save_path == NULL) { |
|
72 |
_fios_save_path = malloc(MAX_PATH); |
|
73 |
strcpy(_fios_save_path, _path.save_dir); |
|
74 |
} |
|
75 |
||
76 |
if(_game_mode==GM_EDITOR) |
|
77 |
_fios_path = _fios_scn_path; |
|
78 |
else |
|
79 |
_fios_path = _fios_save_path; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
179
diff
changeset
|
80 |
|
0 | 81 |
// Parent directory, only if not in root already. |
82 |
if (_fios_path[1] != 0) { |
|
83 |
fios = FiosAlloc(); |
|
84 |
fios->type = FIOS_TYPE_PARENT; |
|
85 |
fios->mtime = 0; |
|
86 |
sprintf(fios->title, ".. (Parent directory)"); |
|
87 |
} |
|
88 |
||
89 |
// Show subdirectories first |
|
90 |
dir = opendir(_fios_path[0] ? _fios_path : "/"); |
|
91 |
if (dir != NULL) { |
|
92 |
while ((dirent = readdir(dir))) { |
|
93 |
sprintf (filename, "%s/%s", _fios_path, dirent->d_name); |
|
94 |
if (!stat(filename, &sb)) { |
|
95 |
if (S_ISDIR(sb.st_mode)) { |
|
96 |
if (dirent->d_name[0] != '.') { |
|
97 |
fios = FiosAlloc(); |
|
98 |
fios->mtime = 0; |
|
99 |
fios->type = FIOS_TYPE_DIR; |
|
100 |
fios->title[0] = 0; |
|
101 |
sprintf(fios->name, "%s/ (Directory)", dirent->d_name); |
|
102 |
} |
|
103 |
} |
|
104 |
} |
|
105 |
} |
|
106 |
closedir(dir); |
|
107 |
} |
|
108 |
||
307
d251be6c0d54
(svn r313) -Feature/Fix: directories in *nix are now sorted alphabetically in ascending order (Tron).
darkvater
parents:
268
diff
changeset
|
109 |
{ |
d251be6c0d54
(svn r313) -Feature/Fix: directories in *nix are now sorted alphabetically in ascending order (Tron).
darkvater
parents:
268
diff
changeset
|
110 |
/* XXX ugly global variables ... */ |
d251be6c0d54
(svn r313) -Feature/Fix: directories in *nix are now sorted alphabetically in ascending order (Tron).
darkvater
parents:
268
diff
changeset
|
111 |
byte order = _savegame_sort_order; |
d251be6c0d54
(svn r313) -Feature/Fix: directories in *nix are now sorted alphabetically in ascending order (Tron).
darkvater
parents:
268
diff
changeset
|
112 |
_savegame_sort_order = 2; // sort ascending by name |
d251be6c0d54
(svn r313) -Feature/Fix: directories in *nix are now sorted alphabetically in ascending order (Tron).
darkvater
parents:
268
diff
changeset
|
113 |
qsort(_fios_items, _fios_count, sizeof(FiosItem), compare_FiosItems); |
d251be6c0d54
(svn r313) -Feature/Fix: directories in *nix are now sorted alphabetically in ascending order (Tron).
darkvater
parents:
268
diff
changeset
|
114 |
_savegame_sort_order = order; |
d251be6c0d54
(svn r313) -Feature/Fix: directories in *nix are now sorted alphabetically in ascending order (Tron).
darkvater
parents:
268
diff
changeset
|
115 |
} |
d251be6c0d54
(svn r313) -Feature/Fix: directories in *nix are now sorted alphabetically in ascending order (Tron).
darkvater
parents:
268
diff
changeset
|
116 |
|
0 | 117 |
// this is where to start sorting |
118 |
sort_start = _fios_count; |
|
119 |
||
120 |
/* Show savegame files |
|
121 |
* .SAV OpenTTD saved game |
|
122 |
* .SS1 Transport Tycoon Deluxe preset game |
|
123 |
* .SV1 Transport Tycoon Deluxe (Patch) saved game |
|
124 |
* .SV2 Transport Tycoon Deluxe (Patch) saved 2-player game |
|
125 |
*/ |
|
126 |
dir = opendir(_fios_path[0] ? _fios_path : "/"); |
|
127 |
if (dir != NULL) { |
|
128 |
while ((dirent = readdir(dir))) { |
|
129 |
sprintf (filename, "%s/%s", _fios_path, dirent->d_name); |
|
130 |
if (!stat(filename, &sb)) { |
|
131 |
if (!S_ISDIR(sb.st_mode)) { |
|
132 |
char *t = strrchr(dirent->d_name, '.'); |
|
437
b66c60ebaa8c
(svn r646) -Fix: braindead case-sensitive file extension filter replaced with a case-INsensitive one (joint effort of Bjarni and Darkvater).
darkvater
parents:
410
diff
changeset
|
133 |
if (t && !strcasecmp(t, ".sav")) { // OpenTTD |
0 | 134 |
*t = 0; // cut extension |
135 |
fios = FiosAlloc(); |
|
136 |
fios->type = FIOS_TYPE_FILE; |
|
137 |
fios->mtime = sb.st_mtime; |
|
138 |
fios->title[0] = 0; |
|
139 |
ttd_strlcpy(fios->name, dirent->d_name, sizeof(fios->name)); |
|
140 |
} else if (mode == SLD_LOAD_GAME || mode == SLD_LOAD_SCENARIO) { |
|
141 |
int ext = 0; // start of savegame extensions in _old_extensions[] |
|
437
b66c60ebaa8c
(svn r646) -Fix: braindead case-sensitive file extension filter replaced with a case-INsensitive one (joint effort of Bjarni and Darkvater).
darkvater
parents:
410
diff
changeset
|
142 |
if (t && ((ext++, !strcasecmp(t, ".ss1")) || (ext++, !strcasecmp(t, ".sv1")) || (ext++, !strcasecmp(t, ".sv2"))) ) { // TTDLX(Patch) |
0 | 143 |
*t = 0; // cut extension |
144 |
fios = FiosAlloc(); |
|
145 |
fios->old_extension = ext-1; |
|
146 |
fios->type = FIOS_TYPE_OLDFILE; |
|
147 |
fios->mtime = sb.st_mtime; |
|
148 |
ttd_strlcpy(fios->name, dirent->d_name, sizeof(fios->name)); |
|
149 |
GetOldSaveGameName(fios->title, filename); |
|
150 |
} |
|
151 |
} |
|
152 |
} |
|
153 |
} |
|
154 |
} |
|
155 |
closedir(dir); |
|
156 |
} |
|
157 |
||
158 |
*num = _fios_count; |
|
159 |
qsort(_fios_items + sort_start, _fios_count - sort_start, sizeof(FiosItem), compare_FiosItems); |
|
160 |
return _fios_items; |
|
161 |
} |
|
162 |
||
163 |
// Get a list of scenarios |
|
164 |
FiosItem *FiosGetScenarioList(int *num, int mode) |
|
165 |
{ |
|
166 |
FiosItem *fios; |
|
167 |
DIR *dir; |
|
168 |
struct dirent *dirent; |
|
169 |
struct stat sb; |
|
170 |
int sort_start; |
|
171 |
char filename[MAX_PATH]; |
|
172 |
||
173 |
if (_fios_scn_path == NULL) { |
|
174 |
_fios_scn_path = malloc(MAX_PATH); |
|
175 |
strcpy(_fios_scn_path, _path.scenario_dir); |
|
176 |
} |
|
177 |
_fios_path = _fios_scn_path; |
|
178 |
||
179 |
// Show subdirectories first |
|
180 |
dir = opendir(_fios_path[0] ? _fios_path : "/"); |
|
181 |
if (dir != NULL) { |
|
182 |
while ((dirent = readdir(dir))) { |
|
183 |
sprintf (filename, "%s/%s", _fios_path, dirent->d_name); |
|
184 |
if (!stat(filename, &sb)) { |
|
185 |
if (S_ISDIR(sb.st_mode)) { |
|
186 |
if (dirent->d_name[0] != '.') { |
|
187 |
fios = FiosAlloc(); |
|
188 |
fios->mtime = 0; |
|
189 |
fios->type = FIOS_TYPE_DIR; |
|
190 |
fios->title[0] = 0; |
|
191 |
sprintf(fios->name, "%s/ (Directory)", dirent->d_name); |
|
192 |
} |
|
193 |
} |
|
194 |
} |
|
195 |
} |
|
196 |
closedir(dir); |
|
197 |
} |
|
198 |
||
199 |
// this is where to start sorting |
|
200 |
sort_start = _fios_count; |
|
201 |
||
202 |
/* Show scenario files |
|
203 |
* .SCN OpenTTD style scenario file |
|
204 |
* .SV0 Transport Tycoon Deluxe (Patch) scenario |
|
205 |
* .SS0 Transport Tycoon Deluxe preset scenario |
|
206 |
*/ |
|
207 |
dir = opendir(_fios_path[0] ? _fios_path : "/"); |
|
208 |
if (dir != NULL) { |
|
209 |
while ((dirent = readdir(dir))) { |
|
210 |
sprintf (filename, "%s/%s", _fios_path, dirent->d_name); |
|
211 |
if (!stat(filename, &sb)) { |
|
212 |
if (!S_ISDIR(sb.st_mode)) { |
|
213 |
char *t = strrchr(dirent->d_name, '.'); |
|
437
b66c60ebaa8c
(svn r646) -Fix: braindead case-sensitive file extension filter replaced with a case-INsensitive one (joint effort of Bjarni and Darkvater).
darkvater
parents:
410
diff
changeset
|
214 |
if (t && !strcasecmp(t, ".scn")) { // OpenTTD |
0 | 215 |
*t = 0; // cut extension |
216 |
fios = FiosAlloc(); |
|
217 |
fios->type = FIOS_TYPE_SCENARIO; |
|
218 |
fios->mtime = sb.st_mtime; |
|
219 |
fios->title[0] = 0; |
|
220 |
ttd_strlcpy(fios->name, dirent->d_name, sizeof(fios->name)-3); |
|
221 |
} else if (mode == SLD_LOAD_GAME || mode == SLD_LOAD_SCENARIO || mode == SLD_NEW_GAME) { |
|
222 |
int ext = 3; // start of scenario extensions in _old_extensions[] |
|
437
b66c60ebaa8c
(svn r646) -Fix: braindead case-sensitive file extension filter replaced with a case-INsensitive one (joint effort of Bjarni and Darkvater).
darkvater
parents:
410
diff
changeset
|
223 |
if (t && ((ext++, !strcasecmp(t, ".sv0")) || (ext++, !strcasecmp(t, ".ss0"))) ) { // TTDLX(Patch) |
0 | 224 |
*t = 0; // cut extension |
225 |
fios = FiosAlloc(); |
|
226 |
fios->old_extension = ext-1; |
|
227 |
fios->type = FIOS_TYPE_OLD_SCENARIO; |
|
228 |
fios->mtime = sb.st_mtime; |
|
229 |
GetOldScenarioGameName(fios->title, filename); |
|
230 |
ttd_strlcpy(fios->name, dirent->d_name, sizeof(fios->name)-3); |
|
231 |
} |
|
232 |
} |
|
233 |
} |
|
234 |
} |
|
235 |
} |
|
236 |
closedir(dir); |
|
237 |
} |
|
238 |
||
239 |
*num = _fios_count; |
|
240 |
qsort(_fios_items + sort_start, _fios_count - sort_start, sizeof(FiosItem), compare_FiosItems); |
|
241 |
return _fios_items; |
|
242 |
} |
|
243 |
||
244 |
||
245 |
// Free the list of savegames |
|
246 |
void FiosFreeSavegameList() |
|
247 |
{ |
|
248 |
free(_fios_items); |
|
249 |
_fios_items = NULL; |
|
250 |
_fios_alloc = _fios_count = 0; |
|
251 |
} |
|
252 |
||
253 |
// Browse to |
|
254 |
char *FiosBrowseTo(const FiosItem *item) |
|
255 |
{ |
|
256 |
char *path = _fios_path; |
|
257 |
char *s; |
|
258 |
||
259 |
switch(item->type) { |
|
260 |
case FIOS_TYPE_PARENT: |
|
261 |
s = strrchr(path, '/'); |
|
262 |
if (s != NULL) *s = 0; |
|
263 |
break; |
|
264 |
||
265 |
case FIOS_TYPE_DIR: |
|
266 |
s = strchr((char*)item->name, '/'); |
|
267 |
if (s) *s = 0; |
|
268 |
while (*path) path++; |
|
269 |
*path++ = '/'; |
|
270 |
strcpy(path, item->name); |
|
271 |
break; |
|
272 |
||
273 |
case FIOS_TYPE_FILE: |
|
274 |
FiosMakeSavegameName(str_buffr, item->name); |
|
275 |
return str_buffr; |
|
276 |
||
277 |
case FIOS_TYPE_OLDFILE: |
|
278 |
sprintf(str_buffr, "%s/%s.%s", _fios_path, item->name, _old_extensions[item->old_extension]); |
|
279 |
return str_buffr; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
179
diff
changeset
|
280 |
|
0 | 281 |
case FIOS_TYPE_SCENARIO: |
282 |
sprintf(str_buffr, "%s/%s.scn", path, item->name); |
|
283 |
return str_buffr; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
179
diff
changeset
|
284 |
|
0 | 285 |
case FIOS_TYPE_OLD_SCENARIO: |
286 |
sprintf(str_buffr, "%s/%s.%s", path, item->name, _old_extensions[item->old_extension]); |
|
287 |
return str_buffr; |
|
288 |
} |
|
289 |
||
290 |
return NULL; |
|
291 |
} |
|
292 |
||
293 |
// Get descriptive texts. |
|
294 |
// Returns a path as well as a |
|
295 |
// string describing the path. |
|
222
b88456001397
(svn r223) -Fix: Const correctness and miscellaneous fixes. Thank you Tron for your diligent fixing of warnings (and some possibly bugs) (Tron)
darkvater
parents:
206
diff
changeset
|
296 |
StringID FiosGetDescText(const char **path) |
0 | 297 |
{ |
298 |
*path = _fios_path[0] ? _fios_path : "/"; |
|
299 |
||
300 |
#if defined(__linux__) |
|
301 |
{ |
|
302 |
struct statvfs s; |
|
303 |
||
304 |
if (statvfs(*path, &s) == 0) |
|
305 |
{ |
|
306 |
uint64 tot = (uint64)s.f_bsize * s.f_bavail; |
|
534
17ab2f22ff74
(svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents:
529
diff
changeset
|
307 |
SetDParam(0, (uint32)(tot >> 20)); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
179
diff
changeset
|
308 |
return STR_4005_BYTES_FREE; |
0 | 309 |
} |
310 |
else |
|
311 |
return STR_4006_UNABLE_TO_READ_DRIVE; |
|
312 |
} |
|
313 |
#else |
|
534
17ab2f22ff74
(svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents:
529
diff
changeset
|
314 |
SetDParam(0, 0); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
179
diff
changeset
|
315 |
return STR_4005_BYTES_FREE; |
0 | 316 |
#endif |
317 |
} |
|
318 |
||
319 |
void FiosMakeSavegameName(char *buf, const char *name) |
|
320 |
{ |
|
321 |
if(_game_mode==GM_EDITOR) |
|
322 |
sprintf(buf, "%s/%s.scn", _fios_path, name); |
|
323 |
else |
|
324 |
sprintf(buf, "%s/%s.sav", _fios_path, name); |
|
325 |
} |
|
326 |
||
327 |
void FiosDelete(const char *name) |
|
328 |
{ |
|
329 |
char *path = str_buffr; |
|
330 |
FiosMakeSavegameName(path, name); |
|
331 |
unlink(path); |
|
332 |
} |
|
333 |
||
334 |
const DriverDesc _video_driver_descs[] = { |
|
179
003096efeb9d
(svn r180) -Fix: some more warning fixes for C99 (Tron)
darkvater
parents:
164
diff
changeset
|
335 |
{"null", "Null Video Driver", &_null_video_driver, 0}, |
0 | 336 |
#if defined(WITH_SDL) |
179
003096efeb9d
(svn r180) -Fix: some more warning fixes for C99 (Tron)
darkvater
parents:
164
diff
changeset
|
337 |
{ "sdl", "SDL Video Driver", &_sdl_video_driver, 1}, |
0 | 338 |
#endif |
543
e3b43338096b
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
339 |
{ "dedicated", "Dedicated Video Driver", &_dedicated_video_driver, 0}, |
179
003096efeb9d
(svn r180) -Fix: some more warning fixes for C99 (Tron)
darkvater
parents:
164
diff
changeset
|
340 |
{ NULL, NULL, NULL, 0} |
0 | 341 |
}; |
342 |
||
343 |
const DriverDesc _sound_driver_descs[] = { |
|
179
003096efeb9d
(svn r180) -Fix: some more warning fixes for C99 (Tron)
darkvater
parents:
164
diff
changeset
|
344 |
{"null", "Null Sound Driver", &_null_sound_driver, 0}, |
0 | 345 |
#if defined(WITH_SDL) |
179
003096efeb9d
(svn r180) -Fix: some more warning fixes for C99 (Tron)
darkvater
parents:
164
diff
changeset
|
346 |
{ "sdl", "SDL Sound Driver", &_sdl_sound_driver, 1}, |
0 | 347 |
#endif |
179
003096efeb9d
(svn r180) -Fix: some more warning fixes for C99 (Tron)
darkvater
parents:
164
diff
changeset
|
348 |
{ NULL, NULL, NULL, 0} |
0 | 349 |
}; |
350 |
||
351 |
#if defined(__APPLE__) |
|
352 |
#define EXTMIDI_PRI 2 |
|
353 |
#else |
|
354 |
#define EXTMIDI_PRI 0 |
|
355 |
#endif |
|
356 |
||
357 |
const DriverDesc _music_driver_descs[] = { |
|
358 |
#ifndef __BEOS__ |
|
569
2e86ae517874
(svn r980) Fixed issues where MorphOS would get problems if AmigaOS would get a port, since MorphOS also have the flag __AMIGA__ defined (Tokai)
bjarni
parents:
561
diff
changeset
|
359 |
#if !defined(__MORPHOS__) && !defined(__AMIGA__) |
2e86ae517874
(svn r980) Fixed issues where MorphOS would get problems if AmigaOS would get a port, since MorphOS also have the flag __AMIGA__ defined (Tokai)
bjarni
parents:
561
diff
changeset
|
360 |
// MorphOS and AmigaOS have no music support |
179
003096efeb9d
(svn r180) -Fix: some more warning fixes for C99 (Tron)
darkvater
parents:
164
diff
changeset
|
361 |
{"extmidi", "External MIDI Driver", &_extmidi_music_driver, EXTMIDI_PRI}, |
0 | 362 |
#endif |
363 |
#endif |
|
364 |
#ifdef __BEOS__ |
|
179
003096efeb9d
(svn r180) -Fix: some more warning fixes for C99 (Tron)
darkvater
parents:
164
diff
changeset
|
365 |
{ "bemidi", "BeOS MIDI Driver", &_bemidi_music_driver, 1}, |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
179
diff
changeset
|
366 |
#endif |
179
003096efeb9d
(svn r180) -Fix: some more warning fixes for C99 (Tron)
darkvater
parents:
164
diff
changeset
|
367 |
{ "null", "Null Music Driver", &_null_music_driver, 1}, |
003096efeb9d
(svn r180) -Fix: some more warning fixes for C99 (Tron)
darkvater
parents:
164
diff
changeset
|
368 |
{ NULL, NULL, NULL, 0} |
0 | 369 |
}; |
370 |
||
223
0e5cc5a65df6
(svn r224) -Fix: Music now finally works on WinXP. DirectMusic is now default for an OS >= WinNT4 (WinNT4, Win2k, WinXP), and MIDI driver for lower OS's (Win95, Win98, WinME, etc).
darkvater
parents:
222
diff
changeset
|
371 |
/* GetOSVersion returns the minimal required version of OS to be able to use that driver. |
0e5cc5a65df6
(svn r224) -Fix: Music now finally works on WinXP. DirectMusic is now default for an OS >= WinNT4 (WinNT4, Win2k, WinXP), and MIDI driver for lower OS's (Win95, Win98, WinME, etc).
darkvater
parents:
222
diff
changeset
|
372 |
Not needed for *nix. */ |
0e5cc5a65df6
(svn r224) -Fix: Music now finally works on WinXP. DirectMusic is now default for an OS >= WinNT4 (WinNT4, Win2k, WinXP), and MIDI driver for lower OS's (Win95, Win98, WinME, etc).
darkvater
parents:
222
diff
changeset
|
373 |
byte GetOSVersion() |
0e5cc5a65df6
(svn r224) -Fix: Music now finally works on WinXP. DirectMusic is now default for an OS >= WinNT4 (WinNT4, Win2k, WinXP), and MIDI driver for lower OS's (Win95, Win98, WinME, etc).
darkvater
parents:
222
diff
changeset
|
374 |
{ |
268
00fabd0160bc
(svn r274) Mac: restored extmusic to be default, which were accidently turned off by a windows music fix
bjarni
parents:
238
diff
changeset
|
375 |
return 2; // any arbitrary number bigger then 0 |
00fabd0160bc
(svn r274) Mac: restored extmusic to be default, which were accidently turned off by a windows music fix
bjarni
parents:
238
diff
changeset
|
376 |
// numbers lower than 2 breaks default music selection on mac |
223
0e5cc5a65df6
(svn r224) -Fix: Music now finally works on WinXP. DirectMusic is now default for an OS >= WinNT4 (WinNT4, Win2k, WinXP), and MIDI driver for lower OS's (Win95, Win98, WinME, etc).
darkvater
parents:
222
diff
changeset
|
377 |
} |
0e5cc5a65df6
(svn r224) -Fix: Music now finally works on WinXP. DirectMusic is now default for an OS >= WinNT4 (WinNT4, Win2k, WinXP), and MIDI driver for lower OS's (Win95, Win98, WinME, etc).
darkvater
parents:
222
diff
changeset
|
378 |
|
0 | 379 |
bool FileExists(const char *filename) |
380 |
{ |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
179
diff
changeset
|
381 |
return access(filename, 0) == 0; |
0 | 382 |
} |
383 |
||
384 |
static int LanguageCompareFunc(const void *a, const void *b) |
|
385 |
{ |
|
222
b88456001397
(svn r223) -Fix: Const correctness and miscellaneous fixes. Thank you Tron for your diligent fixing of warnings (and some possibly bugs) (Tron)
darkvater
parents:
206
diff
changeset
|
386 |
return strcmp(*(const char* const *)a, *(const char* const *)b); |
0 | 387 |
} |
388 |
||
389 |
int GetLanguageList(char **languages, int max) |
|
390 |
{ |
|
391 |
DIR *dir; |
|
392 |
struct dirent *dirent; |
|
393 |
int num = 0; |
|
394 |
||
395 |
dir = opendir(_path.lang_dir); |
|
396 |
if (dir != NULL) { |
|
397 |
while ((dirent = readdir(dir))) { |
|
398 |
char *t = strrchr(dirent->d_name, '.'); |
|
399 |
if (t && !strcmp(t, ".lng")) { |
|
400 |
languages[num++] = strdup(dirent->d_name); |
|
401 |
if (num == max) break; |
|
402 |
} |
|
403 |
} |
|
404 |
closedir(dir); |
|
405 |
} |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
179
diff
changeset
|
406 |
|
0 | 407 |
qsort(languages, num, sizeof(char*), LanguageCompareFunc); |
408 |
return num; |
|
409 |
} |
|
410 |
||
410 | 411 |
static void ChangeWorkingDirectory(char *exe) |
0 | 412 |
{ |
413 |
char *s = strrchr(exe, '/'); |
|
414 |
if (s != NULL) { |
|
415 |
*s = 0; |
|
416 |
chdir(exe); |
|
417 |
*s = '/'; |
|
418 |
} |
|
419 |
} |
|
420 |
||
421 |
void ShowInfo(const char *str) |
|
422 |
{ |
|
423 |
puts(str); |
|
424 |
} |
|
425 |
||
426 |
void ShowOSErrorBox(const char *buf) |
|
427 |
{ |
|
529
3b23d5073d32
(svn r869) Mac: finally made asserts open the console, so people should be able to see what went wrong even if they opened the game in the GUI
bjarni
parents:
507
diff
changeset
|
428 |
#if defined(__APPLE__) |
3b23d5073d32
(svn r869) Mac: finally made asserts open the console, so people should be able to see what went wrong even if they opened the game in the GUI
bjarni
parents:
507
diff
changeset
|
429 |
// this creates an error in the console and then opens the console. |
3b23d5073d32
(svn r869) Mac: finally made asserts open the console, so people should be able to see what went wrong even if they opened the game in the GUI
bjarni
parents:
507
diff
changeset
|
430 |
// Colourcodes are not used in the console, so they are skipped here |
3b23d5073d32
(svn r869) Mac: finally made asserts open the console, so people should be able to see what went wrong even if they opened the game in the GUI
bjarni
parents:
507
diff
changeset
|
431 |
fprintf(stderr, "Error: %s", buf); |
561
e3b9689745ab
(svn r970) Added 2nd data path for all non-windows OSes
bjarni
parents:
543
diff
changeset
|
432 |
system("/Applications/Utilities/Console.app/Contents/MacOS/Console &"); |
529
3b23d5073d32
(svn r869) Mac: finally made asserts open the console, so people should be able to see what went wrong even if they opened the game in the GUI
bjarni
parents:
507
diff
changeset
|
433 |
#else |
3b23d5073d32
(svn r869) Mac: finally made asserts open the console, so people should be able to see what went wrong even if they opened the game in the GUI
bjarni
parents:
507
diff
changeset
|
434 |
// all systems, but OSX |
0 | 435 |
fprintf(stderr, "\033[1;31mError: %s\033[0;39m\n", buf); |
436 |
#endif |
|
437 |
} |
|
438 |
||
439 |
int CDECL main(int argc, char* argv[]) |
|
440 |
{ |
|
441 |
// change the working directory to enable doubleclicking in UIs |
|
442 |
#if defined(__BEOS__) |
|
443 |
ChangeWorkingDirectory(argv[0]); |
|
444 |
#endif |
|
445 |
#if defined(__linux__) |
|
446 |
ChangeWorkingDirectory(argv[0]); |
|
447 |
#endif |
|
448 |
||
206 | 449 |
_random_seeds[0][1] = _random_seeds[0][0] = time(NULL); |
0 | 450 |
|
451 |
||
452 |
return ttd_main(argc, argv); |
|
453 |
} |
|
454 |
||
455 |
void DeterminePaths() |
|
456 |
{ |
|
457 |
char *s; |
|
458 |
||
459 |
_path.game_data_dir = malloc( MAX_PATH ); |
|
561
e3b9689745ab
(svn r970) Added 2nd data path for all non-windows OSes
bjarni
parents:
543
diff
changeset
|
460 |
ttd_strlcpy(_path.game_data_dir, GAME_DATA_DIR, MAX_PATH); |
e3b9689745ab
(svn r970) Added 2nd data path for all non-windows OSes
bjarni
parents:
543
diff
changeset
|
461 |
#if defined SECOND_DATA_DIR |
e3b9689745ab
(svn r970) Added 2nd data path for all non-windows OSes
bjarni
parents:
543
diff
changeset
|
462 |
_path.second_data_dir = malloc( MAX_PATH ); |
e3b9689745ab
(svn r970) Added 2nd data path for all non-windows OSes
bjarni
parents:
543
diff
changeset
|
463 |
ttd_strlcpy( _path.second_data_dir, SECOND_DATA_DIR, MAX_PATH); |
e3b9689745ab
(svn r970) Added 2nd data path for all non-windows OSes
bjarni
parents:
543
diff
changeset
|
464 |
#endif |
0 | 465 |
|
466 |
#if defined(USE_HOMEDIR) |
|
467 |
{ |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
179
diff
changeset
|
468 |
char *homedir; |
0 | 469 |
homedir = getenv("HOME"); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
179
diff
changeset
|
470 |
|
0 | 471 |
if(!homedir) { |
472 |
struct passwd *pw = getpwuid(getuid()); |
|
473 |
if (pw) homedir = pw->pw_dir; |
|
474 |
} |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
179
diff
changeset
|
475 |
|
0 | 476 |
_path.personal_dir = str_fmt("%s" PATHSEP "%s", homedir, PERSONAL_DIR); |
477 |
} |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
179
diff
changeset
|
478 |
|
0 | 479 |
#else /* not defined(USE_HOMEDIR) */ |
480 |
||
481 |
_path.personal_dir = malloc( MAX_PATH ); |
|
561
e3b9689745ab
(svn r970) Added 2nd data path for all non-windows OSes
bjarni
parents:
543
diff
changeset
|
482 |
ttd_strlcpy(_path.personal_dir, PERSONAL_DIR, MAX_PATH); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
179
diff
changeset
|
483 |
|
0 | 484 |
// check if absolute or relative path |
485 |
s = strchr(_path.personal_dir, '/'); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
179
diff
changeset
|
486 |
|
0 | 487 |
// add absolute path |
488 |
if (s==NULL || _path.personal_dir != s) { |
|
489 |
getcwd(_path.personal_dir, MAX_PATH); |
|
490 |
s = strchr(_path.personal_dir, 0); |
|
491 |
*s++ = '/'; |
|
561
e3b9689745ab
(svn r970) Added 2nd data path for all non-windows OSes
bjarni
parents:
543
diff
changeset
|
492 |
ttd_strlcpy(s, PERSONAL_DIR, MAX_PATH); |
0 | 493 |
} |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
179
diff
changeset
|
494 |
|
0 | 495 |
#endif /* defined(USE_HOMEDIR) */ |
496 |
||
497 |
s = strchr(_path.personal_dir, 0); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
179
diff
changeset
|
498 |
|
0 | 499 |
// append a / ? |
500 |
if (s[-1] != '/') { s[0] = '/'; s[1] = 0; } |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
179
diff
changeset
|
501 |
|
0 | 502 |
_path.save_dir = str_fmt("%ssave", _path.personal_dir); |
503 |
_path.autosave_dir = str_fmt("%s/autosave", _path.save_dir); |
|
504 |
_path.scenario_dir = str_fmt("%sscenario", _path.personal_dir); |
|
505 |
_path.gm_dir = str_fmt("%sgm/", _path.game_data_dir); |
|
506 |
_path.data_dir = str_fmt("%sdata/", _path.game_data_dir); |
|
561
e3b9689745ab
(svn r970) Added 2nd data path for all non-windows OSes
bjarni
parents:
543
diff
changeset
|
507 |
_config_file = str_fmt("%sopenttd.cfg", _path.personal_dir); |
704
a526dc96fbfc
(svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents:
569
diff
changeset
|
508 |
_log_file = str_fmt("%sopenttd.log", _path.personal_dir); |
561
e3b9689745ab
(svn r970) Added 2nd data path for all non-windows OSes
bjarni
parents:
543
diff
changeset
|
509 |
|
e3b9689745ab
(svn r970) Added 2nd data path for all non-windows OSes
bjarni
parents:
543
diff
changeset
|
510 |
#if defined CUSTOM_LANG_DIR |
e3b9689745ab
(svn r970) Added 2nd data path for all non-windows OSes
bjarni
parents:
543
diff
changeset
|
511 |
// sets the search path for lng files to the custom one |
e3b9689745ab
(svn r970) Added 2nd data path for all non-windows OSes
bjarni
parents:
543
diff
changeset
|
512 |
_path.lang_dir = malloc( MAX_PATH ); |
e3b9689745ab
(svn r970) Added 2nd data path for all non-windows OSes
bjarni
parents:
543
diff
changeset
|
513 |
ttd_strlcpy( _path.lang_dir, CUSTOM_LANG_DIR, MAX_PATH); |
e3b9689745ab
(svn r970) Added 2nd data path for all non-windows OSes
bjarni
parents:
543
diff
changeset
|
514 |
#else |
0 | 515 |
_path.lang_dir = str_fmt("%slang/", _path.game_data_dir); |
561
e3b9689745ab
(svn r970) Added 2nd data path for all non-windows OSes
bjarni
parents:
543
diff
changeset
|
516 |
#endif |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
179
diff
changeset
|
517 |
|
238
a73a3e50c7f9
(svn r239) Fix: [ 1025361 ] PERSONAL_DIR is created at startup now
dominik
parents:
223
diff
changeset
|
518 |
// create necessary folders |
a73a3e50c7f9
(svn r239) Fix: [ 1025361 ] PERSONAL_DIR is created at startup now
dominik
parents:
223
diff
changeset
|
519 |
mkdir(_path.personal_dir, 0755); |
0 | 520 |
mkdir(_path.save_dir, 0755); |
521 |
mkdir(_path.autosave_dir, 0755); |
|
522 |
mkdir(_path.scenario_dir, 0755); |
|
523 |
} |
|
524 |