os2.c
changeset 782 f75387ede653
parent 781 4c9177888196
child 801 7b6cb481359c
equal deleted inserted replaced
781:4c9177888196 782:f75387ede653
   527 	mkdir(_path.save_dir);
   527 	mkdir(_path.save_dir);
   528 	mkdir(_path.autosave_dir);
   528 	mkdir(_path.autosave_dir);
   529 	mkdir(_path.scenario_dir);
   529 	mkdir(_path.scenario_dir);
   530 }
   530 }
   531 
   531 
   532 #include "stdafx.h"
       
   533 #include "ttd.h"
       
   534 #include "table/strings.h"
       
   535 #include "hal.h"
       
   536 
       
   537 #include <direct.h>
       
   538 #include <unistd.h>
       
   539 #include <sys/stat.h>
       
   540 #include <time.h>
       
   541 #include <dos.h>
       
   542 
       
   543 #include <os2.h>
       
   544 
       
   545 #if defined(WITH_SDL)
       
   546 #include <SDL.h>
       
   547 #endif
       
   548 
       
   549 static char *_fios_path;
       
   550 static char *_fios_save_path;
       
   551 static char *_fios_scn_path;
       
   552 static FiosItem *_fios_items;
       
   553 static int _fios_count, _fios_alloc;
       
   554 
       
   555 static FiosItem *FiosAlloc()
       
   556 {
       
   557 	if (_fios_count == _fios_alloc) {
       
   558 		_fios_alloc += 256;
       
   559 		_fios_items = realloc(_fios_items, _fios_alloc * sizeof(FiosItem));
       
   560 	}
       
   561 	return &_fios_items[_fios_count++];
       
   562 }
       
   563 
       
   564 int compare_FiosItems (const void *a, const void *b) {
       
   565 	const FiosItem *da = (const FiosItem *) a;
       
   566 	const FiosItem *db = (const FiosItem *) b;
       
   567 	int r;
       
   568 
       
   569 	if (_savegame_sort_order < 2) // sort by date
       
   570 	r = da->mtime < db->mtime ? -1 : 1;
       
   571 	else
       
   572 		r = stricmp(da->title[0] ? da->title : da->name, db->title[0] ? db->title : db->name);
       
   573 
       
   574 	if (_savegame_sort_order & 1) r = -r;
       
   575 	return r;
       
   576 }
       
   577 
       
   578 
       
   579 // Get a list of savegames
       
   580 FiosItem *FiosGetSavegameList(int *num, int mode)
       
   581 {
       
   582 	FiosItem *fios;
       
   583 	DIR *dir;
       
   584 	struct dirent *dirent;
       
   585 	struct stat sb;
       
   586 	int sort_start;
       
   587 	char filename[MAX_PATH];
       
   588 
       
   589 	if (_fios_save_path == NULL) {
       
   590 		_fios_save_path = malloc(MAX_PATH);
       
   591 		strcpy(_fios_save_path, _path.save_dir);
       
   592 	}
       
   593 
       
   594 	if(_game_mode==GM_EDITOR)
       
   595 		_fios_path = _fios_scn_path;
       
   596 	else
       
   597 		_fios_path = _fios_save_path;
       
   598 
       
   599 	// Parent directory, only if not in root already.
       
   600 	if (_fios_path[1] != 0) {
       
   601 		fios = FiosAlloc();
       
   602 		fios->type = FIOS_TYPE_PARENT;
       
   603 		fios->mtime = 0;
       
   604 		sprintf(fios->title, ".. (Parent directory)");
       
   605 	}
       
   606 
       
   607 	// Show subdirectories first
       
   608 	dir = opendir(_fios_path[0] ? _fios_path : "C:\\");
       
   609 	if (dir != NULL) {
       
   610 		while ((dirent = readdir(dir))) {
       
   611 			sprintf (filename, "%s\\%s", _fios_path, dirent->d_name);
       
   612 			if (!stat(filename, &sb)) {
       
   613 				if (S_ISDIR(sb.st_mode)) {
       
   614 					if (dirent->d_name[0] != '.') {
       
   615 						fios = FiosAlloc();
       
   616 						fios->mtime = 0;
       
   617 						fios->type = FIOS_TYPE_DIR;
       
   618 						fios->title[0] = 0;
       
   619 						sprintf(fios->name, "%s\\ (Directory)", dirent->d_name);
       
   620 					}
       
   621 				}
       
   622 			}
       
   623 		}
       
   624 		closedir(dir);
       
   625 	}
       
   626 
       
   627 	{
       
   628 		/* XXX ugly global variables ... */
       
   629 		byte order = _savegame_sort_order;
       
   630 		_savegame_sort_order = 2; // sort ascending by name
       
   631 		qsort(_fios_items, _fios_count, sizeof(FiosItem), compare_FiosItems);
       
   632 		_savegame_sort_order = order;
       
   633 	}
       
   634 
       
   635 	// this is where to start sorting
       
   636 	sort_start = _fios_count;
       
   637 
       
   638 	/*      Show savegame files
       
   639 	 *      .SAV    OpenTTD saved game
       
   640 	 *      .SS1    Transport Tycoon Deluxe preset game
       
   641 	 *      .SV1    Transport Tycoon Deluxe (Patch) saved game
       
   642 	 *      .SV2    Transport Tycoon Deluxe (Patch) saved 2-player game
       
   643 	 */
       
   644 	dir = opendir(_fios_path[0] ? _fios_path : "C:\\");
       
   645 	if (dir != NULL) {
       
   646 		while ((dirent = readdir(dir))) {
       
   647 			sprintf (filename, "%s\\%s", _fios_path, dirent->d_name);
       
   648 			if (!stat(filename, &sb)) {
       
   649 				if (!S_ISDIR(sb.st_mode)) {
       
   650 					char *t = strrchr(dirent->d_name, '.');
       
   651 					if (t && !stricmp(t, ".sav")) { // OpenTTD
       
   652 						*t = 0; // cut extension
       
   653 						fios = FiosAlloc();
       
   654 						fios->type = FIOS_TYPE_FILE;
       
   655 						fios->mtime = sb.st_mtime;
       
   656 						fios->title[0] = 0;
       
   657 						ttd_strlcpy(fios->name, dirent->d_name, sizeof(fios->name));
       
   658 					} else if (mode == SLD_LOAD_GAME || mode == SLD_LOAD_SCENARIO) {
       
   659 						int ext = 0; // start of savegame extensions in _old_extensions[]
       
   660 						if (t && ((ext++, !stricmp(t, ".ss1")) || (ext++, !stricmp(t, ".sv1")) || (ext++, !stricmp(t, ".sv2"))) ) { // TTDLX(Patch)
       
   661 							*t = 0; // cut extension
       
   662 							fios = FiosAlloc();
       
   663 							fios->old_extension = ext-1;
       
   664 							fios->type = FIOS_TYPE_OLDFILE;
       
   665 							fios->mtime = sb.st_mtime;
       
   666 							ttd_strlcpy(fios->name, dirent->d_name, sizeof(fios->name));
       
   667 							GetOldSaveGameName(fios->title, filename);
       
   668 						}
       
   669 					}
       
   670 				}
       
   671 			}
       
   672 		}
       
   673 		closedir(dir);
       
   674 	}
       
   675 
       
   676 	qsort(_fios_items + sort_start, _fios_count - sort_start, sizeof(FiosItem), compare_FiosItems);
       
   677 
       
   678 	// Drives
       
   679 	{
       
   680 		unsigned save, disk, disk2, total;
       
   681 
       
   682 		/* save original drive */
       
   683 		_dos_getdrive(&save);
       
   684 
       
   685 		/* get available drive letters */
       
   686 
       
   687 		for (disk = 1; disk < 27; ++disk)
       
   688 		{
       
   689 			_dos_setdrive(disk, &total);
       
   690 			_dos_getdrive(&disk2);
       
   691 			
       
   692 			if (disk == disk2)
       
   693 			{
       
   694 				fios = FiosAlloc();
       
   695 				fios->type = FIOS_TYPE_DRIVE;
       
   696 				fios->title[0] = disk + 'A'-1;
       
   697 				fios->title[1] = ':';
       
   698 				fios->title[2] = 0;
       
   699 			}
       
   700 		}
       
   701 
       
   702 		_dos_setdrive(save, &total);
       
   703 	}
       
   704 
       
   705 	*num = _fios_count;
       
   706 	return _fios_items;
       
   707 }
       
   708 
       
   709 // Get a list of scenarios
       
   710 FiosItem *FiosGetScenarioList(int *num, int mode)
       
   711 {
       
   712 	FiosItem *fios;
       
   713 	DIR *dir;
       
   714 	struct dirent *dirent;
       
   715 	struct stat sb;
       
   716 	int sort_start;
       
   717 	char filename[MAX_PATH];
       
   718 
       
   719 	if (_fios_scn_path == NULL) {
       
   720 		_fios_scn_path = malloc(MAX_PATH);
       
   721 		strcpy(_fios_scn_path, _path.scenario_dir);
       
   722 	}
       
   723 	_fios_path = _fios_scn_path;
       
   724 
       
   725 	// Show subdirectories first
       
   726 	dir = opendir(_fios_path[0] ? _fios_path : "C:\\");
       
   727 	if (dir != NULL) {
       
   728 		while ((dirent = readdir(dir))) {
       
   729 			sprintf (filename, "%s\\%s", _fios_path, dirent->d_name);
       
   730 			if (!stat(filename, &sb)) {
       
   731 				if (S_ISDIR(sb.st_mode)) {
       
   732 					if (dirent->d_name[0] != '.') {
       
   733 						fios = FiosAlloc();
       
   734 						fios->mtime = 0;
       
   735 						fios->type = FIOS_TYPE_DIR;
       
   736 						fios->title[0] = 0;
       
   737 						sprintf(fios->name, "%s\\ (Directory)", dirent->d_name);
       
   738 					}
       
   739 				}
       
   740 			}
       
   741 		}
       
   742 		closedir(dir);
       
   743 	}
       
   744 
       
   745 	// this is where to start sorting
       
   746 	sort_start = _fios_count;
       
   747 
       
   748 	/*      Show scenario files
       
   749 	 *      .SCN    OpenTTD style scenario file
       
   750 	 *      .SV0    Transport Tycoon Deluxe (Patch) scenario
       
   751 	 *      .SS0    Transport Tycoon Deluxe preset scenario
       
   752 	 */
       
   753 	dir = opendir(_fios_path[0] ? _fios_path : "C:\\");
       
   754 	if (dir != NULL) {
       
   755 		while ((dirent = readdir(dir))) {
       
   756 			sprintf (filename, "%s\\%s", _fios_path, dirent->d_name);
       
   757 			if (!stat(filename, &sb)) {
       
   758 				if (!S_ISDIR(sb.st_mode)) {
       
   759 					char *t = strrchr(dirent->d_name, '.');
       
   760 					if (t && !stricmp(t, ".scn")) { // OpenTTD
       
   761 						*t = 0; // cut extension
       
   762 						fios = FiosAlloc();
       
   763 						fios->type = FIOS_TYPE_SCENARIO;
       
   764 						fios->mtime = sb.st_mtime;
       
   765 						fios->title[0] = 0;
       
   766 						ttd_strlcpy(fios->name, dirent->d_name, sizeof(fios->name)-3);
       
   767 					} else if (mode == SLD_LOAD_GAME || mode == SLD_LOAD_SCENARIO || mode == SLD_NEW_GAME) {
       
   768 						int ext = 3; // start of scenario extensions in _old_extensions[]
       
   769 						if (t && ((ext++, !stricmp(t, ".sv0")) || (ext++, !stricmp(t, ".ss0"))) ) { // TTDLX(Patch)
       
   770 							*t = 0; // cut extension
       
   771 							fios = FiosAlloc();
       
   772 							fios->old_extension = ext-1;
       
   773 							fios->type = FIOS_TYPE_OLD_SCENARIO;
       
   774 							fios->mtime = sb.st_mtime;
       
   775 							GetOldScenarioGameName(fios->title, filename);
       
   776 							ttd_strlcpy(fios->name, dirent->d_name, sizeof(fios->name)-3);
       
   777 						}
       
   778 					}
       
   779 				}
       
   780 			}
       
   781 		}
       
   782 		closedir(dir);
       
   783 	}
       
   784 
       
   785 	qsort(_fios_items + sort_start, _fios_count - sort_start, sizeof(FiosItem), compare_FiosItems);
       
   786 
       
   787 	// Drives
       
   788 	if (mode != SLD_NEW_GAME)
       
   789 	{
       
   790 		unsigned save, disk, disk2, total;
       
   791 
       
   792 		/* save original drive */
       
   793 		_dos_getdrive(&save);
       
   794 
       
   795 		/* get available drive letters */
       
   796 
       
   797 		for (disk = 1; disk < 27; ++disk)
       
   798 		{
       
   799 			_dos_setdrive(disk, &total);
       
   800 			_dos_getdrive(&disk2);
       
   801 			
       
   802 			if (disk == disk2)
       
   803 			{
       
   804 				fios = FiosAlloc();
       
   805 				fios->type = FIOS_TYPE_DRIVE;
       
   806 				fios->title[0] = disk + 'A'-1;
       
   807 				fios->title[1] = ':';
       
   808 				fios->title[2] = 0;
       
   809 			}
       
   810 		}
       
   811 
       
   812 		_dos_setdrive(save, &total);
       
   813 	}
       
   814 
       
   815 	*num = _fios_count;
       
   816 	return _fios_items;
       
   817 }
       
   818 
       
   819 
       
   820 // Free the list of savegames
       
   821 void FiosFreeSavegameList()
       
   822 {
       
   823 	free(_fios_items);
       
   824 	_fios_items = NULL;
       
   825 	_fios_alloc = _fios_count = 0;
       
   826 }
       
   827 
       
   828 // Browse to
       
   829 char *FiosBrowseTo(const FiosItem *item)
       
   830 {
       
   831 	char *path = _fios_path;
       
   832 	char *s;
       
   833 
       
   834 	switch(item->type) {
       
   835 	case FIOS_TYPE_PARENT:
       
   836 		s = strrchr(path, '\\');
       
   837 		if (s != NULL) *s = 0;
       
   838 		break;
       
   839 
       
   840 	case FIOS_TYPE_DIR:
       
   841 		s = strchr((char*)item->name, '\\');
       
   842 		if (s) *s = 0;
       
   843 		while (*path) path++;
       
   844 		*path++ = '\\';
       
   845 		strcpy(path, item->name);
       
   846 		break;
       
   847 
       
   848 	case FIOS_TYPE_FILE:
       
   849 		FiosMakeSavegameName(str_buffr, item->name);
       
   850 		return str_buffr;
       
   851 
       
   852 	case FIOS_TYPE_OLDFILE:
       
   853 		sprintf(str_buffr, "%s\\%s.%s", _fios_path, item->name, _old_extensions[item->old_extension]);
       
   854 		return str_buffr;
       
   855 
       
   856 	case FIOS_TYPE_SCENARIO:
       
   857 		sprintf(str_buffr, "%s\\%s.scn", path, item->name);
       
   858 		return str_buffr;
       
   859 
       
   860 	case FIOS_TYPE_OLD_SCENARIO:
       
   861 		sprintf(str_buffr, "%s\\%s.%s", path, item->name, _old_extensions[item->old_extension]);
       
   862 		return str_buffr;
       
   863 	}
       
   864 
       
   865 	return NULL;
       
   866 }
       
   867 
       
   868 // Get descriptive texts.
       
   869 // Returns a path as well as a
       
   870 //  string describing the path.
       
   871 StringID FiosGetDescText(const char **path)
       
   872 {
       
   873 	struct diskfree_t free;
       
   874 	char drive;
       
   875 	
       
   876 	*path = _fios_path[0] ? _fios_path : "C:\\";
       
   877 	drive = 'B' - *path[0];
       
   878 
       
   879 	_getdiskfree(drive, &free);
       
   880 		
       
   881 	SetDParam(0, free.avail_clusters * free.sectors_per_cluster * free.bytes_per_sector);
       
   882 	return STR_4005_BYTES_FREE;
       
   883 }
       
   884 
       
   885 void FiosMakeSavegameName(char *buf, const char *name)
       
   886 {
       
   887 	if(_game_mode==GM_EDITOR)
       
   888 		sprintf(buf, "%s\\%s.scn", _fios_path, name);
       
   889 	else
       
   890 		sprintf(buf, "%s\\%s.sav", _fios_path, name);
       
   891 }
       
   892 
       
   893 void FiosDelete(const char *name)
       
   894 {
       
   895 	char *path = str_buffr;
       
   896 	FiosMakeSavegameName(path, name);
       
   897 	unlink(path);
       
   898 }
       
   899 
       
   900 const DriverDesc _video_driver_descs[] = {
       
   901 	{"null",	"Null Video Driver",    &_null_video_driver,    0},
       
   902 #if defined(WITH_SDL)
       
   903 	{ "sdl",	"SDL Video Driver",	     &_sdl_video_driver,	     1},
       
   904 #endif
       
   905 	{ "dedicated", "Dedicated Video Driver", &_dedicated_video_driver, 0},
       
   906 	{ NULL,	 NULL,								   NULL,								   0}
       
   907 };
       
   908 
       
   909 const DriverDesc _sound_driver_descs[] = {
       
   910 	{"null",	"Null Sound Driver",    &_null_sound_driver,	    0},
       
   911 #if defined(WITH_SDL)
       
   912 	{ "sdl",	"SDL Sound Driver",	     &_sdl_sound_driver,		     1},
       
   913 #endif
       
   914 	{ NULL,	 NULL,								   NULL,									   0}
       
   915 };
       
   916 
       
   917 const DriverDesc _music_driver_descs[] = {
       
   918 	{   "null",     "Null Music Driver",	    &_null_music_driver,	    0},
       
   919 	{     NULL,     NULL,									   NULL,									   0}
       
   920 };
       
   921 
       
   922 /* GetOSVersion returns the minimal required version of OS to be able to use that driver.
       
   923 	 Not needed for *nix. */
       
   924 byte GetOSVersion()
       
   925 {
       
   926 	return 2;  // any arbitrary number bigger then 0
       
   927 				// numbers lower than 2 breaks default music selection on mac
       
   928 }
       
   929 
       
   930 bool FileExists(const char *filename)
       
   931 {
       
   932 	return access(filename, 0) == 0;
       
   933 }
       
   934 
       
   935 static int LanguageCompareFunc(const void *a, const void *b)
       
   936 {
       
   937 	return strcmp(*(const char* const *)a, *(const char* const *)b);
       
   938 }
       
   939 
       
   940 int GetLanguageList(char **languages, int max)
       
   941 {
       
   942 	DIR *dir;
       
   943 	struct dirent *dirent;
       
   944 	int num = 0;
       
   945 
       
   946 	dir = opendir(_path.lang_dir);
       
   947 	if (dir != NULL) {
       
   948 		while ((dirent = readdir(dir))) {
       
   949 			char *t = strrchr(dirent->d_name, '.');
       
   950 			if (t && !strcmp(t, ".lng")) {
       
   951 				languages[num++] = strdup(dirent->d_name);
       
   952 				if (num == max) break;
       
   953 			}
       
   954 		}
       
   955 		closedir(dir);
       
   956 	}
       
   957 
       
   958 	qsort(languages, num, sizeof(char*), LanguageCompareFunc);
       
   959 	return num;
       
   960 }
       
   961 
       
   962 static void ChangeWorkingDirectory(char *exe)
       
   963 {
       
   964 	char *s = strrchr(exe, '\\');
       
   965 	if (s != NULL) {
       
   966 		*s = 0;
       
   967 		chdir(exe);
       
   968 		*s = '\\';
       
   969 	}
       
   970 }
       
   971 
       
   972 void ShowInfo(const char *str)
       
   973 {
       
   974 	puts(str);
       
   975 }
       
   976 
       
   977 void ShowOSErrorBox(const char *buf)
       
   978 {
       
   979 	WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, buf, "OpenTTD", 263, MB_OK | MB_APPLMODAL | MB_MOVEABLE | MB_ERROR);
       
   980 // TODO: FIX, doesn't always appear
       
   981 }
       
   982 
       
   983 int CDECL main(int argc, char* argv[])
       
   984 {
       
   985 	// change the working directory to enable doubleclicking in UIs
       
   986 	ChangeWorkingDirectory(argv[0]);
       
   987 
       
   988 	_random_seeds[0][1] = _random_seeds[0][0] = time(NULL);
       
   989 
       
   990 
       
   991 	return ttd_main(argc, argv);
       
   992 }
       
   993 
       
   994 void DeterminePaths()
       
   995 {
       
   996 	char *s;
       
   997 
       
   998 	_path.game_data_dir = malloc( MAX_PATH );
       
   999 	ttd_strlcpy(_path.game_data_dir, GAME_DATA_DIR, MAX_PATH);
       
  1000 	#if defined SECOND_DATA_DIR
       
  1001 	_path.second_data_dir = malloc( MAX_PATH );
       
  1002 	ttd_strlcpy( _path.second_data_dir, SECOND_DATA_DIR, MAX_PATH);
       
  1003 	#endif
       
  1004 
       
  1005 #if defined(USE_HOMEDIR)
       
  1006 	{
       
  1007 		char *homedir;
       
  1008 		homedir = getenv("HOME");
       
  1009 
       
  1010 		if(!homedir) {
       
  1011 			struct passwd *pw = getpwuid(getuid());
       
  1012 			if (pw) homedir = pw->pw_dir;
       
  1013 		}
       
  1014 
       
  1015 		_path.personal_dir = str_fmt("%s" PATHSEP "%s", homedir, PERSONAL_DIR);
       
  1016 	}
       
  1017 
       
  1018 #else /* not defined(USE_HOMEDIR) */
       
  1019 
       
  1020 	_path.personal_dir = malloc( MAX_PATH );
       
  1021 	ttd_strlcpy(_path.personal_dir, PERSONAL_DIR, MAX_PATH);
       
  1022 
       
  1023 	// check if absolute or relative path
       
  1024 	s = strchr(_path.personal_dir, '\\');
       
  1025 
       
  1026 	// add absolute path
       
  1027 	if (s==NULL || _path.personal_dir != s) {
       
  1028 		getcwd(_path.personal_dir, MAX_PATH);
       
  1029 		s = strchr(_path.personal_dir, 0);
       
  1030 		*s++ = '\\';
       
  1031 		ttd_strlcpy(s, PERSONAL_DIR, MAX_PATH);
       
  1032 	}
       
  1033 
       
  1034 #endif /* defined(USE_HOMEDIR) */
       
  1035 
       
  1036 	s = strchr(_path.personal_dir, 0);
       
  1037 
       
  1038 	// append a / ?
       
  1039 	if (s[-1] != '\\') { s[0] = '\\'; s[1] = 0; }
       
  1040 
       
  1041 	_path.save_dir = str_fmt("%ssave", _path.personal_dir);
       
  1042 	_path.autosave_dir = str_fmt("%s\\autosave", _path.save_dir);
       
  1043 	_path.scenario_dir = str_fmt("%sscenario", _path.personal_dir);
       
  1044 	_path.gm_dir = str_fmt("%sgm\\", _path.game_data_dir);
       
  1045 	_path.data_dir = str_fmt("%sdata\\", _path.game_data_dir);
       
  1046 	_config_file = str_fmt("%sopenttd.cfg", _path.personal_dir);
       
  1047 	
       
  1048 #if defined CUSTOM_LANG_DIR
       
  1049 	// sets the search path for lng files to the custom one
       
  1050 	_path.lang_dir = malloc( MAX_PATH );
       
  1051 	ttd_strlcpy( _path.lang_dir, CUSTOM_LANG_DIR, MAX_PATH);
       
  1052 #else
       
  1053 	_path.lang_dir = str_fmt("%slang\\", _path.game_data_dir);
       
  1054 #endif
       
  1055 
       
  1056 	// create necessary folders
       
  1057 	mkdir(_path.personal_dir);
       
  1058 	mkdir(_path.save_dir);
       
  1059 	mkdir(_path.autosave_dir);
       
  1060 	mkdir(_path.scenario_dir);
       
  1061 }
       
  1062