2005-07-24 14:12:37 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2004-12-24 00:00:10 +00:00
|
|
|
#include "stdafx.h"
|
2005-06-02 19:39:29 +00:00
|
|
|
#include "openttd.h"
|
2005-07-27 19:57:12 +00:00
|
|
|
#include "hal.h"
|
2005-07-22 16:14:24 +00:00
|
|
|
#include "variables.h"
|
2005-02-06 13:41:02 +00:00
|
|
|
#include "string.h"
|
2004-12-24 00:00:10 +00:00
|
|
|
#include "table/strings.h"
|
2005-06-02 10:39:46 +00:00
|
|
|
#include "gfx.h"
|
|
|
|
#include "gui.h"
|
2005-07-22 16:14:24 +00:00
|
|
|
#include "functions.h"
|
|
|
|
#include "macros.h"
|
2004-12-24 00:00:10 +00:00
|
|
|
|
|
|
|
#include <direct.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/stat.h>
|
2005-06-02 09:51:01 +00:00
|
|
|
#include <stdlib.h>
|
2004-12-24 00:00:10 +00:00
|
|
|
#include <time.h>
|
|
|
|
#include <dos.h>
|
|
|
|
|
2004-12-28 12:11:34 +00:00
|
|
|
#define INCL_WIN
|
2005-03-19 22:36:13 +00:00
|
|
|
#define INCL_WINCLIPBOARD
|
2004-12-27 10:40:04 +00:00
|
|
|
|
2004-12-24 00:00:10 +00:00
|
|
|
#include <os2.h>
|
|
|
|
|
2005-07-22 16:14:24 +00:00
|
|
|
#include <i86.h>
|
|
|
|
|
2005-06-02 17:17:24 +00:00
|
|
|
static inline int strcasecmp(const char* s1, const char* s2)
|
|
|
|
{
|
|
|
|
return stricmp(s1, s2);
|
|
|
|
}
|
|
|
|
|
2004-12-24 00:00:10 +00:00
|
|
|
static char *_fios_path;
|
|
|
|
static char *_fios_save_path;
|
|
|
|
static char *_fios_scn_path;
|
|
|
|
static FiosItem *_fios_items;
|
|
|
|
static int _fios_count, _fios_alloc;
|
|
|
|
|
2005-03-11 14:14:54 +00:00
|
|
|
static FiosItem *FiosAlloc(void)
|
2004-12-24 00:00:10 +00:00
|
|
|
{
|
|
|
|
if (_fios_count == _fios_alloc) {
|
|
|
|
_fios_alloc += 256;
|
|
|
|
_fios_items = realloc(_fios_items, _fios_alloc * sizeof(FiosItem));
|
|
|
|
}
|
|
|
|
return &_fios_items[_fios_count++];
|
|
|
|
}
|
|
|
|
|
2005-03-11 14:14:54 +00:00
|
|
|
int compare_FiosItems(const void *a, const void *b)
|
|
|
|
{
|
|
|
|
const FiosItem *da = (const FiosItem *)a;
|
|
|
|
const FiosItem *db = (const FiosItem *)b;
|
2004-12-24 00:00:10 +00:00
|
|
|
int r;
|
|
|
|
|
2005-10-17 19:57:00 +00:00
|
|
|
if (_savegame_sort_order & SORT_BY_NAME) {
|
2005-07-17 15:58:47 +00:00
|
|
|
r = strcasecmp(da->title, db->title);
|
2005-10-17 19:57:00 +00:00
|
|
|
} else {
|
|
|
|
r = da->mtime < db->mtime ? -1 : 1;
|
|
|
|
}
|
2004-12-24 00:00:10 +00:00
|
|
|
|
2005-10-17 19:57:00 +00:00
|
|
|
if (_savegame_sort_order & SORT_DESCENDING) r = -r;
|
2004-12-24 00:00:10 +00:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-03-11 14:14:54 +00:00
|
|
|
static void append_path(char *out, const char *path, const char *file)
|
2004-12-27 10:40:04 +00:00
|
|
|
{
|
2006-06-27 21:25:53 +00:00
|
|
|
if (path[2] == '\\' && path[3] == '\0') {
|
2004-12-27 10:40:04 +00:00
|
|
|
sprintf(out, "%s%s", path, file);
|
2006-06-27 21:25:53 +00:00
|
|
|
} else {
|
2004-12-27 10:40:04 +00:00
|
|
|
sprintf(out, "%s\\%s", path, file);
|
2006-06-27 21:25:53 +00:00
|
|
|
}
|
2004-12-27 10:40:04 +00:00
|
|
|
}
|
|
|
|
|
2004-12-24 00:00:10 +00:00
|
|
|
// Get a list of savegames
|
|
|
|
FiosItem *FiosGetSavegameList(int *num, int mode)
|
|
|
|
{
|
|
|
|
FiosItem *fios;
|
|
|
|
DIR *dir;
|
|
|
|
struct dirent *dirent;
|
|
|
|
struct stat sb;
|
|
|
|
int sort_start;
|
|
|
|
char filename[MAX_PATH];
|
|
|
|
|
|
|
|
if (_fios_save_path == NULL) {
|
|
|
|
_fios_save_path = malloc(MAX_PATH);
|
|
|
|
strcpy(_fios_save_path, _path.save_dir);
|
|
|
|
}
|
|
|
|
|
2005-08-12 06:37:48 +00:00
|
|
|
_fios_path = _fios_save_path;
|
2004-12-24 00:00:10 +00:00
|
|
|
|
2004-12-27 10:40:04 +00:00
|
|
|
// Parent directory, only if not of the type C:\.
|
2005-03-11 14:14:54 +00:00
|
|
|
if (_fios_path[3] != '\0') {
|
2004-12-24 00:00:10 +00:00
|
|
|
fios = FiosAlloc();
|
|
|
|
fios->type = FIOS_TYPE_PARENT;
|
2005-03-11 14:14:54 +00:00
|
|
|
fios->mtime = 0;
|
2005-03-26 18:04:42 +00:00
|
|
|
strcpy(fios->name, "..");
|
2004-12-27 10:40:04 +00:00
|
|
|
strcpy(fios->title, ".. (Parent directory)");
|
2004-12-24 00:00:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Show subdirectories first
|
2005-08-12 06:37:48 +00:00
|
|
|
dir = opendir(_fios_path);
|
2004-12-24 00:00:10 +00:00
|
|
|
if (dir != NULL) {
|
2005-03-11 14:14:54 +00:00
|
|
|
while ((dirent = readdir(dir)) != NULL) {
|
2004-12-27 10:40:04 +00:00
|
|
|
append_path(filename, _fios_path, dirent->d_name);
|
2005-03-11 14:14:54 +00:00
|
|
|
if (!stat(filename, &sb) && S_ISDIR(sb.st_mode) &&
|
2005-05-15 14:01:35 +00:00
|
|
|
strcmp(dirent->d_name, ".") != 0 &&
|
|
|
|
strcmp(dirent->d_name, "..") != 0) {
|
2005-03-11 14:14:54 +00:00
|
|
|
fios = FiosAlloc();
|
|
|
|
fios->type = FIOS_TYPE_DIR;
|
|
|
|
fios->mtime = 0;
|
|
|
|
ttd_strlcpy(fios->name, dirent->d_name, lengthof(fios->name));
|
2006-03-25 09:22:10 +00:00
|
|
|
snprintf(fios->title, lengthof(fios->title), "%s\\ (Directory)", FS2OTTD(dirent->d_name));
|
2006-01-31 12:08:49 +00:00
|
|
|
str_validate(fios->title);
|
2004-12-24 00:00:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir(dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
/* XXX ugly global variables ... */
|
|
|
|
byte order = _savegame_sort_order;
|
2005-10-17 19:57:00 +00:00
|
|
|
_savegame_sort_order = SORT_BY_NAME | SORT_ASCENDING;
|
2004-12-24 00:00:10 +00:00
|
|
|
qsort(_fios_items, _fios_count, sizeof(FiosItem), compare_FiosItems);
|
|
|
|
_savegame_sort_order = order;
|
|
|
|
}
|
|
|
|
|
|
|
|
// this is where to start sorting
|
|
|
|
sort_start = _fios_count;
|
|
|
|
|
2005-03-11 14:14:54 +00:00
|
|
|
/* Show savegame files
|
|
|
|
* .SAV OpenTTD saved game
|
|
|
|
* .SS1 Transport Tycoon Deluxe preset game
|
|
|
|
* .SV1 Transport Tycoon Deluxe (Patch) saved game
|
|
|
|
* .SV2 Transport Tycoon Deluxe (Patch) saved 2-player game
|
2004-12-24 00:00:10 +00:00
|
|
|
*/
|
2005-08-12 06:37:48 +00:00
|
|
|
dir = opendir(_fios_path);
|
2004-12-24 00:00:10 +00:00
|
|
|
if (dir != NULL) {
|
2005-03-11 14:14:54 +00:00
|
|
|
while ((dirent = readdir(dir)) != NULL) {
|
|
|
|
char *t;
|
|
|
|
|
2004-12-27 10:40:04 +00:00
|
|
|
append_path(filename, _fios_path, dirent->d_name);
|
2005-03-11 14:14:54 +00:00
|
|
|
if (stat(filename, &sb) || S_ISDIR(sb.st_mode)) continue;
|
|
|
|
|
|
|
|
t = strrchr(dirent->d_name, '.');
|
2005-08-13 21:35:31 +00:00
|
|
|
if (t == NULL) continue;
|
|
|
|
|
|
|
|
if (strcasecmp(t, ".sav") == 0) { // OpenTTD
|
2005-03-11 14:14:54 +00:00
|
|
|
fios = FiosAlloc();
|
|
|
|
fios->type = FIOS_TYPE_FILE;
|
|
|
|
fios->mtime = sb.st_mtime;
|
|
|
|
ttd_strlcpy(fios->name, dirent->d_name, lengthof(fios->name));
|
2005-07-17 15:58:47 +00:00
|
|
|
|
|
|
|
*t = '\0'; // strip extension
|
2006-03-25 09:22:10 +00:00
|
|
|
ttd_strlcpy(fios->title, FS2OTTD(dirent->d_name), lengthof(fios->title));
|
2006-01-31 12:08:49 +00:00
|
|
|
str_validate(fios->title);
|
2005-03-11 14:14:54 +00:00
|
|
|
} else if (mode == SLD_LOAD_GAME || mode == SLD_LOAD_SCENARIO) {
|
2005-08-13 21:35:31 +00:00
|
|
|
if (strcasecmp(t, ".ss1") == 0 ||
|
|
|
|
strcasecmp(t, ".sv1") == 0 ||
|
|
|
|
strcasecmp(t, ".sv2") == 0) { // TTDLX(Patch)
|
2005-03-11 14:14:54 +00:00
|
|
|
fios = FiosAlloc();
|
|
|
|
fios->type = FIOS_TYPE_OLDFILE;
|
|
|
|
fios->mtime = sb.st_mtime;
|
|
|
|
ttd_strlcpy(fios->name, dirent->d_name, lengthof(fios->name));
|
|
|
|
GetOldSaveGameName(fios->title, filename);
|
2004-12-24 00:00:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir(dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
qsort(_fios_items + sort_start, _fios_count - sort_start, sizeof(FiosItem), compare_FiosItems);
|
|
|
|
|
|
|
|
// Drives
|
|
|
|
{
|
2005-03-11 14:14:54 +00:00
|
|
|
uint save;
|
|
|
|
uint disk;
|
|
|
|
uint total;
|
2004-12-24 00:00:10 +00:00
|
|
|
|
|
|
|
/* save original drive */
|
|
|
|
_dos_getdrive(&save);
|
|
|
|
|
|
|
|
/* get available drive letters */
|
2005-08-12 06:37:48 +00:00
|
|
|
for (disk = 1; disk < 27; ++disk) {
|
2005-03-11 14:14:54 +00:00
|
|
|
uint disk2;
|
|
|
|
|
2004-12-24 00:00:10 +00:00
|
|
|
_dos_setdrive(disk, &total);
|
|
|
|
_dos_getdrive(&disk2);
|
2005-01-06 18:45:28 +00:00
|
|
|
|
2005-08-12 06:37:48 +00:00
|
|
|
if (disk == disk2) {
|
2004-12-24 00:00:10 +00:00
|
|
|
fios = FiosAlloc();
|
|
|
|
fios->type = FIOS_TYPE_DRIVE;
|
2005-03-27 10:35:01 +00:00
|
|
|
sprintf(fios->name, "%c:", 'A' + disk - 1);
|
2005-03-11 14:14:54 +00:00
|
|
|
sprintf(fios->title, "%c:", 'A' + disk - 1);
|
2004-12-24 00:00:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_dos_setdrive(save, &total);
|
|
|
|
}
|
|
|
|
|
|
|
|
*num = _fios_count;
|
|
|
|
return _fios_items;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get a list of scenarios
|
|
|
|
FiosItem *FiosGetScenarioList(int *num, int mode)
|
|
|
|
{
|
|
|
|
FiosItem *fios;
|
|
|
|
DIR *dir;
|
|
|
|
struct dirent *dirent;
|
|
|
|
struct stat sb;
|
|
|
|
int sort_start;
|
|
|
|
char filename[MAX_PATH];
|
|
|
|
|
2005-07-17 15:58:47 +00:00
|
|
|
if (_fios_scn_path == NULL) {
|
|
|
|
_fios_scn_path = malloc(MAX_PATH);
|
2004-12-24 00:00:10 +00:00
|
|
|
strcpy(_fios_scn_path, _path.scenario_dir);
|
|
|
|
}
|
2004-12-27 10:40:04 +00:00
|
|
|
|
2004-12-24 00:00:10 +00:00
|
|
|
_fios_path = _fios_scn_path;
|
|
|
|
|
2004-12-27 10:40:04 +00:00
|
|
|
// Parent directory, only if not of the type C:\.
|
2005-03-11 14:14:54 +00:00
|
|
|
if (_fios_path[3] != '\0' && mode != SLD_NEW_GAME) {
|
2004-12-27 10:40:04 +00:00
|
|
|
fios = FiosAlloc();
|
|
|
|
fios->type = FIOS_TYPE_PARENT;
|
2005-03-11 14:14:54 +00:00
|
|
|
fios->mtime = 0;
|
2004-12-27 10:40:04 +00:00
|
|
|
strcpy(fios->title, ".. (Parent directory)");
|
|
|
|
}
|
|
|
|
|
2004-12-24 00:00:10 +00:00
|
|
|
// Show subdirectories first
|
2005-08-12 06:37:48 +00:00
|
|
|
dir = opendir(_fios_path);
|
2004-12-24 00:00:10 +00:00
|
|
|
if (dir != NULL) {
|
2005-03-11 14:14:54 +00:00
|
|
|
while ((dirent = readdir(dir)) != NULL) {
|
2004-12-27 10:40:04 +00:00
|
|
|
append_path(filename, _fios_path, dirent->d_name);
|
2005-03-15 12:21:15 +00:00
|
|
|
if (!stat(filename, &sb) && S_ISDIR(sb.st_mode) &&
|
2005-05-15 14:01:35 +00:00
|
|
|
strcmp(dirent->d_name, ".") != 0 &&
|
|
|
|
strcmp(dirent->d_name, "..") != 0) {
|
2005-03-11 14:14:54 +00:00
|
|
|
fios = FiosAlloc();
|
|
|
|
fios->type = FIOS_TYPE_DIR;
|
|
|
|
fios->mtime = 0;
|
|
|
|
ttd_strlcpy(fios->name, dirent->d_name, lengthof(fios->name));
|
2006-03-25 09:22:10 +00:00
|
|
|
snprintf(fios->title, lengthof(fios->title), "%s\\ (Directory)", FS2OTTD(dirent->d_name));
|
2006-01-31 12:08:49 +00:00
|
|
|
str_validate(fios->title);
|
2004-12-24 00:00:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir(dir);
|
|
|
|
}
|
|
|
|
|
2005-08-12 06:37:48 +00:00
|
|
|
{
|
|
|
|
/* XXX ugly global variables ... */
|
|
|
|
byte order = _savegame_sort_order;
|
2005-10-17 19:57:00 +00:00
|
|
|
_savegame_sort_order = SORT_BY_NAME | SORT_ASCENDING;
|
2005-08-12 06:37:48 +00:00
|
|
|
qsort(_fios_items, _fios_count, sizeof(FiosItem), compare_FiosItems);
|
|
|
|
_savegame_sort_order = order;
|
|
|
|
}
|
|
|
|
|
2004-12-24 00:00:10 +00:00
|
|
|
// this is where to start sorting
|
|
|
|
sort_start = _fios_count;
|
|
|
|
|
2005-03-11 14:14:54 +00:00
|
|
|
/* Show scenario files
|
|
|
|
* .SCN OpenTTD style scenario file
|
|
|
|
* .SV0 Transport Tycoon Deluxe (Patch) scenario
|
|
|
|
* .SS0 Transport Tycoon Deluxe preset scenario
|
2004-12-24 00:00:10 +00:00
|
|
|
*/
|
2005-08-12 06:37:48 +00:00
|
|
|
dir = opendir(_fios_path);
|
2004-12-24 00:00:10 +00:00
|
|
|
if (dir != NULL) {
|
2005-03-11 14:14:54 +00:00
|
|
|
while ((dirent = readdir(dir)) != NULL) {
|
|
|
|
char *t;
|
|
|
|
|
2004-12-27 10:40:04 +00:00
|
|
|
append_path(filename, _fios_path, dirent->d_name);
|
2005-03-11 14:14:54 +00:00
|
|
|
if (stat(filename, &sb) || S_ISDIR(sb.st_mode)) continue;
|
|
|
|
|
|
|
|
t = strrchr(dirent->d_name, '.');
|
2005-08-13 21:35:31 +00:00
|
|
|
if (t == NULL) continue;
|
|
|
|
|
|
|
|
if (strcasecmp(t, ".scn") == 0) { // OpenTTD
|
2005-03-11 14:14:54 +00:00
|
|
|
fios = FiosAlloc();
|
|
|
|
fios->type = FIOS_TYPE_SCENARIO;
|
|
|
|
fios->mtime = sb.st_mtime;
|
|
|
|
ttd_strlcpy(fios->name, dirent->d_name, lengthof(fios->name));
|
2005-07-17 15:58:47 +00:00
|
|
|
|
|
|
|
*t = '\0'; // strip extension
|
2006-03-25 09:22:10 +00:00
|
|
|
ttd_strlcpy(fios->title, FS2OTTD(dirent->d_name), lengthof(fios->title));
|
2006-01-31 12:08:49 +00:00
|
|
|
str_validate(fios->title);
|
2005-03-11 14:14:54 +00:00
|
|
|
} else if (mode == SLD_LOAD_GAME || mode == SLD_LOAD_SCENARIO ||
|
|
|
|
mode == SLD_NEW_GAME) {
|
2005-08-13 21:35:31 +00:00
|
|
|
if (strcasecmp(t, ".sv0") == 0 ||
|
|
|
|
strcasecmp(t, ".ss0") == 0) { // TTDLX(Patch)
|
2005-03-11 14:14:54 +00:00
|
|
|
fios = FiosAlloc();
|
|
|
|
fios->type = FIOS_TYPE_OLD_SCENARIO;
|
|
|
|
fios->mtime = sb.st_mtime;
|
|
|
|
GetOldScenarioGameName(fios->title, filename);
|
|
|
|
ttd_strlcpy(fios->name, dirent->d_name, lengthof(fios->name));
|
2004-12-24 00:00:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir(dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
qsort(_fios_items + sort_start, _fios_count - sort_start, sizeof(FiosItem), compare_FiosItems);
|
|
|
|
|
|
|
|
// Drives
|
2005-08-12 06:37:48 +00:00
|
|
|
if (mode != SLD_NEW_GAME) {
|
2004-12-24 00:00:10 +00:00
|
|
|
unsigned save, disk, disk2, total;
|
|
|
|
|
|
|
|
/* save original drive */
|
|
|
|
_dos_getdrive(&save);
|
|
|
|
|
|
|
|
/* get available drive letters */
|
|
|
|
|
2005-08-12 06:37:48 +00:00
|
|
|
for (disk = 1; disk < 27; ++disk) {
|
2004-12-24 00:00:10 +00:00
|
|
|
_dos_setdrive(disk, &total);
|
|
|
|
_dos_getdrive(&disk2);
|
2005-01-06 18:45:28 +00:00
|
|
|
|
2005-08-12 06:37:48 +00:00
|
|
|
if (disk == disk2) {
|
2004-12-24 00:00:10 +00:00
|
|
|
fios = FiosAlloc();
|
|
|
|
fios->type = FIOS_TYPE_DRIVE;
|
2005-03-27 10:35:01 +00:00
|
|
|
sprintf(fios->name, "%c:", 'A' + disk - 1);
|
|
|
|
sprintf(fios->title, "%c:", 'A' + disk - 1);
|
2004-12-24 00:00:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_dos_setdrive(save, &total);
|
|
|
|
}
|
|
|
|
|
|
|
|
*num = _fios_count;
|
|
|
|
return _fios_items;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Free the list of savegames
|
2005-03-11 14:14:54 +00:00
|
|
|
void FiosFreeSavegameList(void)
|
2004-12-24 00:00:10 +00:00
|
|
|
{
|
|
|
|
free(_fios_items);
|
|
|
|
_fios_items = NULL;
|
|
|
|
_fios_alloc = _fios_count = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Browse to
|
|
|
|
char *FiosBrowseTo(const FiosItem *item)
|
|
|
|
{
|
|
|
|
char *path = _fios_path;
|
|
|
|
char *s;
|
|
|
|
|
2005-03-11 14:14:54 +00:00
|
|
|
switch (item->type) {
|
|
|
|
case FIOS_TYPE_DRIVE:
|
|
|
|
sprintf(path, "%c:\\", item->title[0]);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FIOS_TYPE_PARENT:
|
|
|
|
s = strrchr(path, '\\');
|
2006-06-27 21:25:53 +00:00
|
|
|
if (s != path + 2) {
|
2005-08-12 06:37:48 +00:00
|
|
|
s[0] = '\0';
|
2006-06-27 21:25:53 +00:00
|
|
|
} else {
|
2005-08-12 06:37:48 +00:00
|
|
|
s[1] = '\0';
|
2006-06-27 21:25:53 +00:00
|
|
|
}
|
2005-03-11 14:14:54 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case FIOS_TYPE_DIR:
|
2005-08-12 06:37:48 +00:00
|
|
|
if (path[3] != '\0') strcat(path, "\\");
|
2005-03-11 14:14:54 +00:00
|
|
|
strcat(path, item->name);
|
|
|
|
break;
|
|
|
|
|
2005-10-28 00:09:59 +00:00
|
|
|
case FIOS_TYPE_DIRECT:
|
|
|
|
sprintf(path, "%s\\", item->name);
|
2005-10-28 00:18:57 +00:00
|
|
|
s = strrchr(path, '\\');
|
|
|
|
if (s[1] == '\0') s[0] = '\0'; // strip trailing slash
|
2005-10-28 00:09:59 +00:00
|
|
|
break;
|
|
|
|
|
2005-03-11 14:14:54 +00:00
|
|
|
case FIOS_TYPE_FILE:
|
|
|
|
case FIOS_TYPE_OLDFILE:
|
|
|
|
case FIOS_TYPE_SCENARIO:
|
|
|
|
case FIOS_TYPE_OLD_SCENARIO: {
|
|
|
|
static char str_buffr[512];
|
|
|
|
|
|
|
|
sprintf(str_buffr, "%s\\%s", path, item->name);
|
|
|
|
return str_buffr;
|
2004-12-27 10:40:04 +00:00
|
|
|
}
|
2004-12-24 00:00:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2005-03-28 13:30:51 +00:00
|
|
|
/**
|
|
|
|
* Get descriptive texts. Returns the path and free space
|
|
|
|
* left on the device
|
|
|
|
* @param path string describing the path
|
|
|
|
* @param tfs total free space in megabytes, optional (can be NULL)
|
|
|
|
* @return StringID describing the path (free space or failure)
|
|
|
|
*/
|
|
|
|
StringID FiosGetDescText(const char **path, uint32 *tot)
|
2004-12-24 00:00:10 +00:00
|
|
|
{
|
|
|
|
struct diskfree_t free;
|
|
|
|
char drive;
|
2005-01-06 18:45:28 +00:00
|
|
|
|
2004-12-27 10:40:04 +00:00
|
|
|
*path = _fios_path;
|
2005-03-11 14:14:54 +00:00
|
|
|
drive = *path[0] - 'A' + 1;
|
2004-12-24 00:00:10 +00:00
|
|
|
|
2005-03-28 13:30:51 +00:00
|
|
|
if (tot != NULL && _getdiskfree(drive, &free) == 0) {
|
|
|
|
*tot = free.avail_clusters * free.sectors_per_cluster * free.bytes_per_sector;
|
|
|
|
return STR_4005_BYTES_FREE;
|
|
|
|
}
|
2004-12-27 10:40:04 +00:00
|
|
|
|
2005-03-28 13:30:51 +00:00
|
|
|
return STR_4006_UNABLE_TO_READ_DRIVE;
|
2004-12-24 00:00:10 +00:00
|
|
|
}
|
|
|
|
|
2006-03-21 22:08:15 +00:00
|
|
|
void FiosMakeSavegameName(char *buf, const char *name, size_t size)
|
2004-12-24 00:00:10 +00:00
|
|
|
{
|
2005-03-15 14:06:28 +00:00
|
|
|
const char* extension;
|
|
|
|
const char* period;
|
|
|
|
|
2006-06-27 21:25:53 +00:00
|
|
|
extension = (_game_mode == GM_EDITOR ? ".scn" : ".sav");
|
2005-03-15 14:06:28 +00:00
|
|
|
|
|
|
|
// Don't append the extension, if it is already there
|
|
|
|
period = strrchr(name, '.');
|
2005-06-02 17:17:24 +00:00
|
|
|
if (period != NULL && strcasecmp(period, extension) == 0) extension = "";
|
2005-03-15 14:06:28 +00:00
|
|
|
|
2006-03-21 22:08:15 +00:00
|
|
|
snprintf(buf, size, "%s\\%s%s", _fios_path, name, extension);
|
2004-12-24 00:00:10 +00:00
|
|
|
}
|
|
|
|
|
2005-08-01 00:14:22 +00:00
|
|
|
bool FiosDelete(const char *name)
|
2004-12-24 00:00:10 +00:00
|
|
|
{
|
2005-02-07 12:32:35 +00:00
|
|
|
char path[512];
|
|
|
|
|
2006-03-21 22:08:15 +00:00
|
|
|
FiosMakeSavegameName(path, name, sizeof(path));
|
2005-08-01 00:14:22 +00:00
|
|
|
return unlink(path) == 0;
|
2004-12-24 00:00:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool FileExists(const char *filename)
|
|
|
|
{
|
|
|
|
return access(filename, 0) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int LanguageCompareFunc(const void *a, const void *b)
|
|
|
|
{
|
|
|
|
return strcmp(*(const char* const *)a, *(const char* const *)b);
|
|
|
|
}
|
|
|
|
|
|
|
|
int GetLanguageList(char **languages, int max)
|
|
|
|
{
|
|
|
|
DIR *dir;
|
|
|
|
struct dirent *dirent;
|
|
|
|
int num = 0;
|
|
|
|
|
|
|
|
dir = opendir(_path.lang_dir);
|
|
|
|
if (dir != NULL) {
|
2005-08-12 06:37:48 +00:00
|
|
|
while ((dirent = readdir(dir)) != NULL) {
|
2004-12-24 00:00:10 +00:00
|
|
|
char *t = strrchr(dirent->d_name, '.');
|
2005-08-12 06:37:48 +00:00
|
|
|
|
|
|
|
if (t != NULL && strcmp(t, ".lng") == 0) {
|
2004-12-24 00:00:10 +00:00
|
|
|
languages[num++] = strdup(dirent->d_name);
|
|
|
|
if (num == max) break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir(dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
qsort(languages, num, sizeof(char*), LanguageCompareFunc);
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ChangeWorkingDirectory(char *exe)
|
|
|
|
{
|
|
|
|
char *s = strrchr(exe, '\\');
|
|
|
|
if (s != NULL) {
|
2005-03-11 14:14:54 +00:00
|
|
|
*s = '\0';
|
2004-12-24 00:00:10 +00:00
|
|
|
chdir(exe);
|
|
|
|
*s = '\\';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ShowInfo(const char *str)
|
|
|
|
{
|
2004-12-28 12:11:34 +00:00
|
|
|
HAB hab;
|
|
|
|
HMQ hmq;
|
|
|
|
ULONG rc;
|
2005-01-06 18:45:28 +00:00
|
|
|
|
2004-12-28 12:11:34 +00:00
|
|
|
// init PM env.
|
|
|
|
hmq = WinCreateMsgQueue((hab = WinInitialize(0)), 0);
|
|
|
|
|
|
|
|
// display the box
|
|
|
|
rc = WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, str, "OpenTTD", 0, MB_OK | MB_MOVEABLE | MB_INFORMATION);
|
|
|
|
|
|
|
|
// terminate PM env.
|
|
|
|
WinDestroyMsgQueue(hmq);
|
|
|
|
WinTerminate(hab);
|
2004-12-24 00:00:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ShowOSErrorBox(const char *buf)
|
|
|
|
{
|
2004-12-28 12:11:34 +00:00
|
|
|
HAB hab;
|
|
|
|
HMQ hmq;
|
|
|
|
ULONG rc;
|
2005-01-06 18:45:28 +00:00
|
|
|
|
2004-12-28 12:11:34 +00:00
|
|
|
// init PM env.
|
|
|
|
hmq = WinCreateMsgQueue((hab = WinInitialize(0)), 0);
|
|
|
|
|
|
|
|
// display the box
|
|
|
|
rc = WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, buf, "OpenTTD", 0, MB_OK | MB_MOVEABLE | MB_ERROR);
|
|
|
|
|
|
|
|
// terminate PM env.
|
|
|
|
WinDestroyMsgQueue(hmq);
|
|
|
|
WinTerminate(hab);
|
2004-12-24 00:00:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int CDECL main(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
// change the working directory to enable doubleclicking in UIs
|
|
|
|
ChangeWorkingDirectory(argv[0]);
|
|
|
|
|
|
|
|
_random_seeds[0][1] = _random_seeds[0][0] = time(NULL);
|
|
|
|
|
|
|
|
|
|
|
|
return ttd_main(argc, argv);
|
|
|
|
}
|
|
|
|
|
2005-02-21 18:59:54 +00:00
|
|
|
void DeterminePaths(void)
|
2004-12-24 00:00:10 +00:00
|
|
|
{
|
|
|
|
char *s;
|
|
|
|
|
2005-03-11 14:14:54 +00:00
|
|
|
_path.game_data_dir = malloc(MAX_PATH);
|
2004-12-24 00:00:10 +00:00
|
|
|
ttd_strlcpy(_path.game_data_dir, GAME_DATA_DIR, MAX_PATH);
|
|
|
|
#if defined SECOND_DATA_DIR
|
2005-03-11 14:14:54 +00:00
|
|
|
_path.second_data_dir = malloc(MAX_PATH);
|
|
|
|
ttd_strlcpy(_path.second_data_dir, SECOND_DATA_DIR, MAX_PATH);
|
2004-12-24 00:00:10 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(USE_HOMEDIR)
|
|
|
|
{
|
2005-03-11 14:14:54 +00:00
|
|
|
const char *homedir = getenv("HOME");
|
2004-12-24 00:00:10 +00:00
|
|
|
|
2005-03-11 14:14:54 +00:00
|
|
|
if (homedir == NULL) {
|
|
|
|
const struct passwd *pw = getpwuid(getuid());
|
|
|
|
if (pw != NULL) homedir = pw->pw_dir;
|
2004-12-24 00:00:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_path.personal_dir = str_fmt("%s" PATHSEP "%s", homedir, PERSONAL_DIR);
|
|
|
|
}
|
|
|
|
|
|
|
|
#else /* not defined(USE_HOMEDIR) */
|
|
|
|
|
2005-03-11 14:14:54 +00:00
|
|
|
_path.personal_dir = malloc(MAX_PATH);
|
2004-12-24 00:00:10 +00:00
|
|
|
ttd_strlcpy(_path.personal_dir, PERSONAL_DIR, MAX_PATH);
|
|
|
|
|
|
|
|
// check if absolute or relative path
|
|
|
|
s = strchr(_path.personal_dir, '\\');
|
|
|
|
|
|
|
|
// add absolute path
|
2005-03-11 14:14:54 +00:00
|
|
|
if (s == NULL || _path.personal_dir != s) {
|
2004-12-24 00:00:10 +00:00
|
|
|
getcwd(_path.personal_dir, MAX_PATH);
|
|
|
|
s = strchr(_path.personal_dir, 0);
|
|
|
|
*s++ = '\\';
|
|
|
|
ttd_strlcpy(s, PERSONAL_DIR, MAX_PATH);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* defined(USE_HOMEDIR) */
|
|
|
|
|
|
|
|
s = strchr(_path.personal_dir, 0);
|
|
|
|
|
|
|
|
// append a / ?
|
2005-03-11 14:14:54 +00:00
|
|
|
if (s[-1] != '\\') strcpy(s, "\\");
|
2004-12-24 00:00:10 +00:00
|
|
|
|
|
|
|
_path.save_dir = str_fmt("%ssave", _path.personal_dir);
|
|
|
|
_path.autosave_dir = str_fmt("%s\\autosave", _path.save_dir);
|
|
|
|
_path.scenario_dir = str_fmt("%sscenario", _path.personal_dir);
|
|
|
|
_path.gm_dir = str_fmt("%sgm\\", _path.game_data_dir);
|
|
|
|
_path.data_dir = str_fmt("%sdata\\", _path.game_data_dir);
|
2005-03-10 21:44:17 +00:00
|
|
|
|
|
|
|
if (_config_file == NULL)
|
|
|
|
_config_file = str_fmt("%sopenttd.cfg", _path.personal_dir);
|
2005-01-06 18:45:28 +00:00
|
|
|
|
2005-03-11 14:14:54 +00:00
|
|
|
_highscore_file = str_fmt("%shs.dat", _path.personal_dir);
|
|
|
|
_log_file = str_fmt("%sopenttd.log", _path.personal_dir);
|
|
|
|
|
2004-12-24 00:00:10 +00:00
|
|
|
#if defined CUSTOM_LANG_DIR
|
|
|
|
// sets the search path for lng files to the custom one
|
|
|
|
_path.lang_dir = malloc( MAX_PATH );
|
|
|
|
ttd_strlcpy( _path.lang_dir, CUSTOM_LANG_DIR, MAX_PATH);
|
|
|
|
#else
|
|
|
|
_path.lang_dir = str_fmt("%slang\\", _path.game_data_dir);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// create necessary folders
|
|
|
|
mkdir(_path.personal_dir);
|
|
|
|
mkdir(_path.save_dir);
|
|
|
|
mkdir(_path.autosave_dir);
|
|
|
|
mkdir(_path.scenario_dir);
|
|
|
|
}
|
|
|
|
|
2005-06-02 10:39:46 +00:00
|
|
|
/**
|
|
|
|
* Insert a chunk of text from the clipboard onto the textbuffer. Get TEXT clipboard
|
|
|
|
* and append this up to the maximum length (either absolute or screenlength). If maxlength
|
|
|
|
* is zero, we don't care about the screenlength but only about the physical length of the string
|
|
|
|
* @param tb @Textbuf type to be changed
|
|
|
|
* @return Return true on successfull change of Textbuf, or false otherwise
|
|
|
|
*/
|
2005-03-11 14:14:54 +00:00
|
|
|
bool InsertTextBufferClipboard(Textbuf *tb)
|
|
|
|
{
|
2005-06-02 19:55:37 +00:00
|
|
|
HAB hab = 0;
|
2005-03-19 22:36:13 +00:00
|
|
|
|
|
|
|
if (WinOpenClipbrd(hab))
|
|
|
|
{
|
2005-06-02 19:55:37 +00:00
|
|
|
const char* text = (const char*)WinQueryClipbrdData(hab, CF_TEXT);
|
2005-06-02 10:39:46 +00:00
|
|
|
|
2005-06-02 19:55:37 +00:00
|
|
|
if (text != NULL)
|
|
|
|
{
|
|
|
|
uint length = 0;
|
|
|
|
uint width = 0;
|
|
|
|
const char* i;
|
2005-03-19 22:36:13 +00:00
|
|
|
|
2005-06-02 19:55:37 +00:00
|
|
|
for (i = text; IsValidAsciiChar(*i); i++)
|
|
|
|
{
|
|
|
|
uint w;
|
2005-06-02 10:39:46 +00:00
|
|
|
|
2005-06-02 19:55:37 +00:00
|
|
|
if (tb->length + length >= tb->maxlength - 1) break;
|
2005-06-02 10:39:46 +00:00
|
|
|
|
2006-05-09 15:43:40 +00:00
|
|
|
w = GetCharacterWidth(FS_NORMAL, (byte)*i);
|
2005-06-02 19:55:37 +00:00
|
|
|
if (tb->maxwidth != 0 && width + tb->width + w > tb->maxwidth) break;
|
2005-06-02 10:39:46 +00:00
|
|
|
|
2005-06-02 19:55:37 +00:00
|
|
|
width += w;
|
|
|
|
length++;
|
|
|
|
}
|
|
|
|
|
|
|
|
memmove(tb->buf + tb->caretpos + length, tb->buf + tb->caretpos, tb->length - tb->caretpos + 1);
|
|
|
|
memcpy(tb->buf + tb->caretpos, text, length);
|
|
|
|
tb->width += width;
|
|
|
|
tb->caretxoffs += width;
|
|
|
|
tb->length += length;
|
|
|
|
tb->caretpos += length;
|
|
|
|
|
|
|
|
WinCloseClipbrd(hab);
|
|
|
|
return true;
|
|
|
|
}
|
2005-06-02 10:39:46 +00:00
|
|
|
|
2005-06-02 19:55:37 +00:00
|
|
|
WinCloseClipbrd(hab);
|
|
|
|
}
|
2005-06-02 10:39:46 +00:00
|
|
|
|
2005-06-02 19:55:37 +00:00
|
|
|
return false;
|
2005-03-11 14:14:54 +00:00
|
|
|
}
|
2005-06-02 09:51:01 +00:00
|
|
|
|
2005-07-22 16:14:24 +00:00
|
|
|
|
|
|
|
void CSleep(int milliseconds)
|
|
|
|
{
|
|
|
|
delay(milliseconds);
|
|
|
|
}
|