mirror of https://github.com/OpenTTD/OpenTTD
(svn r9266) -Codechange: unify the retrieval of the base paths a little more.
parent
92486ac980
commit
50b2088674
|
@ -12,6 +12,7 @@
|
||||||
#include "debug.h"
|
#include "debug.h"
|
||||||
#include "fios.h"
|
#include "fios.h"
|
||||||
#ifndef WIN32
|
#ifndef WIN32
|
||||||
|
#include <unistd.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -258,24 +259,98 @@ void AppendPathSeparator(char *buf, size_t buflen)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if defined(WIN32) || defined(WINCE)
|
||||||
/**
|
/**
|
||||||
* Determine the base (personal dir and game data dir) paths
|
* Determine the base (personal dir and game data dir) paths
|
||||||
* @note defined in the OS related files (os2.cpp, win32.cpp, unix.cpp etc)
|
* @param exe the path to the executable
|
||||||
*/
|
*/
|
||||||
extern void DetermineBasePaths();
|
extern void DetermineBasePaths(const char *exe);
|
||||||
|
#else /* defined(WIN32) || defined(WINCE) */
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Changes the working directory to the path of the give executable.
|
||||||
|
* For OSX application bundles '.app' is the required extension of the bundle,
|
||||||
|
* so when we crop the path to there, when can remove the name of the bundle
|
||||||
|
* in the same way we remove the name from the executable name.
|
||||||
|
* @param exe the path to the executable
|
||||||
|
*/
|
||||||
|
void ChangeWorkingDirectory(const char *exe)
|
||||||
|
{
|
||||||
|
#ifdef WITH_COCOA
|
||||||
|
char *app_bundle = strchr(exe, '.');
|
||||||
|
while (app_bundle != NULL && strncasecmp(app_bundle, ".app", 4) != 0) app_bundle = strchr(&app_bundle[1], '.');
|
||||||
|
|
||||||
|
if (app_bundle != NULL) app_bundle[0] = '\0';
|
||||||
|
#endif /* WITH_COCOA */
|
||||||
|
char *s = strrchr(exe, PATHSEPCHAR);
|
||||||
|
if (s != NULL) {
|
||||||
|
*s = '\0';
|
||||||
|
chdir(exe);
|
||||||
|
*s = PATHSEPCHAR;
|
||||||
|
}
|
||||||
|
#ifdef WITH_COCOA
|
||||||
|
if (app_bundle != NULL) app_bundle[0] = '.';
|
||||||
|
#endif /* WITH_COCOA */
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determine the base (personal dir and game data dir) paths
|
||||||
|
* @param exe the path to the executable
|
||||||
|
*/
|
||||||
|
void DetermineBasePaths(const char *exe)
|
||||||
|
{
|
||||||
|
/* Change the working directory to enable doubleclicking in UIs */
|
||||||
|
ChangeWorkingDirectory(exe);
|
||||||
|
|
||||||
|
_paths.game_data_dir = MallocT<char>(MAX_PATH);
|
||||||
|
ttd_strlcpy(_paths.game_data_dir, GAME_DATA_DIR, MAX_PATH);
|
||||||
|
#if defined(SECOND_DATA_DIR)
|
||||||
|
_paths.second_data_dir = MallocT<char>(MAX_PATH);
|
||||||
|
ttd_strlcpy(_paths.second_data_dir, SECOND_DATA_DIR, MAX_PATH);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(USE_HOMEDIR)
|
||||||
|
const char *homedir = getenv("HOME");
|
||||||
|
|
||||||
|
if (homedir == NULL) {
|
||||||
|
const struct passwd *pw = getpwuid(getuid());
|
||||||
|
if (pw != NULL) homedir = pw->pw_dir;
|
||||||
|
}
|
||||||
|
|
||||||
|
_paths.personal_dir = str_fmt("%s" PATHSEP "%s", homedir, PERSONAL_DIR);
|
||||||
|
#else /* not defined(USE_HOMEDIR) */
|
||||||
|
_paths.personal_dir = MallocT<char>(MAX_PATH);
|
||||||
|
ttd_strlcpy(_paths.personal_dir, PERSONAL_DIR, MAX_PATH);
|
||||||
|
|
||||||
|
/* check if absolute or relative path */
|
||||||
|
const char *s = strchr(_paths.personal_dir, PATHSEPCHAR);
|
||||||
|
|
||||||
|
/* add absolute path */
|
||||||
|
if (s == NULL || _paths.personal_dir != s) {
|
||||||
|
getcwd(_paths.personal_dir, MAX_PATH);
|
||||||
|
AppendPathSeparator(_paths.personal_dir, MAX_PATH);
|
||||||
|
ttd_strlcat(_paths.personal_dir, PERSONAL_DIR, MAX_PATH);
|
||||||
|
}
|
||||||
|
#endif /* defined(USE_HOMEDIR) */
|
||||||
|
|
||||||
|
AppendPathSeparator(_paths.personal_dir, MAX_PATH);
|
||||||
|
AppendPathSeparator(_paths.game_data_dir, MAX_PATH);
|
||||||
|
}
|
||||||
|
#endif /* defined(WIN32) || defined(WINCE) */
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Acquire the base paths (personal dir and game data dir),
|
* Acquire the base paths (personal dir and game data dir),
|
||||||
* fill all other paths (save dir, autosave dir etc) and
|
* fill all other paths (save dir, autosave dir etc) and
|
||||||
* make the save and scenario directories.
|
* make the save and scenario directories.
|
||||||
|
* @param exe the path to the executable
|
||||||
* @todo for save_dir, autosave_dir, scenario_dir and heightmap_dir the
|
* @todo for save_dir, autosave_dir, scenario_dir and heightmap_dir the
|
||||||
* assumption is that there is no path separator, however for gm_dir
|
* assumption is that there is no path separator, however for gm_dir
|
||||||
* lang_dir and data_dir that assumption is made.
|
* lang_dir and data_dir that assumption is made.
|
||||||
* This inconsistency should be resolved.
|
* This inconsistency should be resolved.
|
||||||
*/
|
*/
|
||||||
void DeterminePaths()
|
void DeterminePaths(const char *exe)
|
||||||
{
|
{
|
||||||
DetermineBasePaths();
|
DetermineBasePaths(exe);
|
||||||
|
|
||||||
_paths.save_dir = str_fmt("%ssave", _paths.personal_dir);
|
_paths.save_dir = str_fmt("%ssave", _paths.personal_dir);
|
||||||
_paths.autosave_dir = str_fmt("%s" PATHSEP "autosave", _paths.save_dir);
|
_paths.autosave_dir = str_fmt("%s" PATHSEP "autosave", _paths.save_dir);
|
||||||
|
|
|
@ -21,6 +21,6 @@ bool FioCheckFileExists(const char *filename);
|
||||||
void FioCreateDirectory(const char *filename);
|
void FioCreateDirectory(const char *filename);
|
||||||
|
|
||||||
void AppendPathSeparator(char *buf, size_t buflen);
|
void AppendPathSeparator(char *buf, size_t buflen);
|
||||||
void DeterminePaths();
|
void DeterminePaths(const char *exe);
|
||||||
|
|
||||||
#endif /* FILEIO_H */
|
#endif /* FILEIO_H */
|
||||||
|
|
|
@ -433,7 +433,7 @@ int ttd_main(int argc, char *argv[])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
DeterminePaths();
|
DeterminePaths(argv[0]);
|
||||||
CheckExternalFiles();
|
CheckExternalFiles();
|
||||||
|
|
||||||
#if defined(UNIX) && !defined(__MORPHOS__)
|
#if defined(UNIX) && !defined(__MORPHOS__)
|
||||||
|
|
49
src/os2.cpp
49
src/os2.cpp
|
@ -122,18 +122,6 @@ bool FiosIsHiddenFile(const struct dirent *ent)
|
||||||
return ent->d_name[0] == '.';
|
return ent->d_name[0] == '.';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void ChangeWorkingDirectory(char *exe)
|
|
||||||
{
|
|
||||||
char *s = strrchr(exe, PATHSEPCHAR);
|
|
||||||
|
|
||||||
if (s != NULL) {
|
|
||||||
*s = '\0';
|
|
||||||
chdir(exe);
|
|
||||||
*s = PATHSEPCHAR;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void ShowInfo(const unsigned char *str)
|
void ShowInfo(const unsigned char *str)
|
||||||
{
|
{
|
||||||
HAB hab;
|
HAB hab;
|
||||||
|
@ -178,43 +166,6 @@ int CDECL main(int argc, char* argv[])
|
||||||
return ttd_main(argc, argv);
|
return ttd_main(argc, argv);
|
||||||
}
|
}
|
||||||
|
|
||||||
void DetermineBasePaths()
|
|
||||||
{
|
|
||||||
_paths.game_data_dir = MallocT<char>(MAX_PATH);
|
|
||||||
ttd_strlcpy(_paths.game_data_dir, GAME_DATA_DIR, MAX_PATH);
|
|
||||||
#if defined(SECOND_DATA_DIR)
|
|
||||||
_paths.second_data_dir = MallocT<char>(MAX_PATH);
|
|
||||||
ttd_strlcpy(_paths.second_data_dir, SECOND_DATA_DIR, MAX_PATH);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(USE_HOMEDIR)
|
|
||||||
const char *homedir = getenv("HOME");
|
|
||||||
|
|
||||||
if (homedir == NULL) {
|
|
||||||
const struct passwd *pw = getpwuid(getuid());
|
|
||||||
if (pw != NULL) homedir = pw->pw_dir;
|
|
||||||
}
|
|
||||||
|
|
||||||
_paths.personal_dir = str_fmt("%s" PATHSEP "%s", homedir, PERSONAL_DIR);
|
|
||||||
#else /* not defined(USE_HOMEDIR) */
|
|
||||||
_paths.personal_dir = MallocT<char>(MAX_PATH);
|
|
||||||
ttd_strlcpy(_paths.personal_dir, PERSONAL_DIR, MAX_PATH);
|
|
||||||
|
|
||||||
/* check if absolute or relative path */
|
|
||||||
const char *s = strchr(_paths.personal_dir, PATHSEPCHAR);
|
|
||||||
|
|
||||||
/* add absolute path */
|
|
||||||
if (s == NULL || _paths.personal_dir != s) {
|
|
||||||
getcwd(_paths.personal_dir, MAX_PATH);
|
|
||||||
AppendPathSeparator(_paths.personal_dir, MAX_PATH);
|
|
||||||
ttd_strlcat(_paths.personal_dir, PERSONAL_DIR, MAX_PATH);
|
|
||||||
}
|
|
||||||
#endif /* defined(USE_HOMEDIR) */
|
|
||||||
|
|
||||||
AppendPathSeparator(_paths.personal_dir, MAX_PATH);
|
|
||||||
AppendPathSeparator(_paths.game_data_dir, MAX_PATH);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Insert a chunk of text from the clipboard onto the textbuffer. Get TEXT clipboard
|
* 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
|
* and append this up to the maximum length (either absolute or screenlength). If maxlength
|
||||||
|
|
57
src/unix.cpp
57
src/unix.cpp
|
@ -7,7 +7,6 @@
|
||||||
#include "string.h"
|
#include "string.h"
|
||||||
#include "table/strings.h"
|
#include "table/strings.h"
|
||||||
#include "variables.h"
|
#include "variables.h"
|
||||||
#include "fileio.h"
|
|
||||||
|
|
||||||
#include <dirent.h>
|
#include <dirent.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
@ -101,18 +100,6 @@ bool FiosIsHiddenFile(const struct dirent *ent)
|
||||||
return ent->d_name[0] == '.';
|
return ent->d_name[0] == '.';
|
||||||
}
|
}
|
||||||
|
|
||||||
#if defined(__BEOS__) || defined(__linux__)
|
|
||||||
static void ChangeWorkingDirectory(char *exe)
|
|
||||||
{
|
|
||||||
char *s = strrchr(exe, '/');
|
|
||||||
if (s != NULL) {
|
|
||||||
*s = '\0';
|
|
||||||
chdir(exe);
|
|
||||||
*s = '/';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
void ShowInfo(const char *str)
|
void ShowInfo(const char *str)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "%s\n", str);
|
fprintf(stderr, "%s\n", str);
|
||||||
|
@ -131,7 +118,6 @@ void ShowOSErrorBox(const char *buf)
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef WITH_COCOA
|
#ifdef WITH_COCOA
|
||||||
void cocoaSetWorkingDirectory();
|
|
||||||
void cocoaSetupAutoreleasePool();
|
void cocoaSetupAutoreleasePool();
|
||||||
void cocoaReleaseAutoreleasePool();
|
void cocoaReleaseAutoreleasePool();
|
||||||
#endif
|
#endif
|
||||||
|
@ -146,15 +132,9 @@ int CDECL main(int argc, char* argv[])
|
||||||
if (argc >= 2 && strncmp(argv[1], "-psn", 4) == 0) {
|
if (argc >= 2 && strncmp(argv[1], "-psn", 4) == 0) {
|
||||||
argv[1] = NULL;
|
argv[1] = NULL;
|
||||||
argc = 1;
|
argc = 1;
|
||||||
cocoaSetWorkingDirectory();
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// change the working directory to enable doubleclicking in UIs
|
|
||||||
#if defined(__BEOS__) || defined(__linux__)
|
|
||||||
ChangeWorkingDirectory(argv[0]);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
_random_seeds[1][1] = _random_seeds[1][0] = _random_seeds[0][1] = _random_seeds[0][0] = time(NULL);
|
_random_seeds[1][1] = _random_seeds[1][0] = _random_seeds[0][1] = _random_seeds[0][0] = time(NULL);
|
||||||
SeedMT(_random_seeds[0][1]);
|
SeedMT(_random_seeds[0][1]);
|
||||||
|
|
||||||
|
@ -169,43 +149,6 @@ int CDECL main(int argc, char* argv[])
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
void DetermineBasePaths()
|
|
||||||
{
|
|
||||||
_paths.game_data_dir = MallocT<char>(MAX_PATH);
|
|
||||||
ttd_strlcpy(_paths.game_data_dir, GAME_DATA_DIR, MAX_PATH);
|
|
||||||
#if defined(SECOND_DATA_DIR)
|
|
||||||
_paths.second_data_dir = MallocT<char>(MAX_PATH);
|
|
||||||
ttd_strlcpy(_paths.second_data_dir, SECOND_DATA_DIR, MAX_PATH);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(USE_HOMEDIR)
|
|
||||||
const char *homedir = getenv("HOME");
|
|
||||||
|
|
||||||
if (homedir == NULL) {
|
|
||||||
const struct passwd *pw = getpwuid(getuid());
|
|
||||||
if (pw != NULL) homedir = pw->pw_dir;
|
|
||||||
}
|
|
||||||
|
|
||||||
_paths.personal_dir = str_fmt("%s" PATHSEP "%s", homedir, PERSONAL_DIR);
|
|
||||||
#else /* not defined(USE_HOMEDIR) */
|
|
||||||
_paths.personal_dir = MallocT<char>(MAX_PATH);
|
|
||||||
ttd_strlcpy(_paths.personal_dir, PERSONAL_DIR, MAX_PATH);
|
|
||||||
|
|
||||||
/* check if absolute or relative path */
|
|
||||||
const char *s = strchr(_paths.personal_dir, PATHSEPCHAR);
|
|
||||||
|
|
||||||
/* add absolute path */
|
|
||||||
if (s == NULL || _paths.personal_dir != s) {
|
|
||||||
getcwd(_paths.personal_dir, MAX_PATH);
|
|
||||||
AppendPathSeparator(_paths.personal_dir, MAX_PATH);
|
|
||||||
ttd_strlcat(_paths.personal_dir, PERSONAL_DIR, MAX_PATH);
|
|
||||||
}
|
|
||||||
#endif /* defined(USE_HOMEDIR) */
|
|
||||||
|
|
||||||
AppendPathSeparator(_paths.personal_dir, MAX_PATH);
|
|
||||||
AppendPathSeparator(_paths.game_data_dir, MAX_PATH);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool InsertTextBufferClipboard(Textbuf *tb)
|
bool InsertTextBufferClipboard(Textbuf *tb)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
|
|
|
@ -2047,22 +2047,6 @@ void CocoaDialog(const char* title, const char* message, const char* buttonLabel
|
||||||
_cocoa_video_dialog = false;
|
_cocoa_video_dialog = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* This is needed since OS X applications are started with the working dir set to / when double-clicked */
|
|
||||||
void cocoaSetWorkingDirectory()
|
|
||||||
{
|
|
||||||
char parentdir[MAXPATHLEN];
|
|
||||||
int chdir_ret;
|
|
||||||
CFURLRef url = CFBundleCopyBundleURL(CFBundleGetMainBundle());
|
|
||||||
CFURLRef url2 = CFURLCreateCopyDeletingLastPathComponent(0, url);
|
|
||||||
if (CFURLGetFileSystemRepresentation(url2, true, (unsigned char*)parentdir, MAXPATHLEN)) {
|
|
||||||
chdir_ret = chdir(parentdir); /* chdir to the binary app's parent */
|
|
||||||
assert(chdir_ret == 0);
|
|
||||||
}
|
|
||||||
CFRelease(url);
|
|
||||||
CFRelease(url2);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* These are called from main() to prevent a _NSAutoreleaseNoPool error when
|
/* These are called from main() to prevent a _NSAutoreleaseNoPool error when
|
||||||
* exiting before the cocoa video driver has been loaded
|
* exiting before the cocoa video driver has been loaded
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -943,7 +943,7 @@ void GetCurrentDirectoryW(int length, wchar_t *path)
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
void DetermineBasePaths()
|
void DetermineBasePaths(const char *exe)
|
||||||
{
|
{
|
||||||
_paths.personal_dir = _paths.game_data_dir = MallocT<char>(MAX_PATH);
|
_paths.personal_dir = _paths.game_data_dir = MallocT<char>(MAX_PATH);
|
||||||
#if defined(UNICODE)
|
#if defined(UNICODE)
|
||||||
|
|
Loading…
Reference in New Issue