diff options
author | dshaligram <dshaligram@c06c8d41-db1a-0410-9941-cceddc491573> | 2007-05-04 07:11:20 +0000 |
---|---|---|
committer | dshaligram <dshaligram@c06c8d41-db1a-0410-9941-cceddc491573> | 2007-05-04 07:11:20 +0000 |
commit | c8df9af2cdf0cc4d5b845d3200be3ffdb4727e87 (patch) | |
tree | 72f3d9b4fc91226f5ce8ecc3d2e99efce8f2eda4 /crawl-ref/source | |
parent | fa61313c503ba66c00ad5ab4fc75dd032fb76a34 (diff) | |
download | crawl-ref-c8df9af2cdf0cc4d5b845d3200be3ffdb4727e87.tar.gz crawl-ref-c8df9af2cdf0cc4d5b845d3200be3ffdb4727e87.zip |
Allow Mac users to double-click to launch Crawl:
- Use argv[0] to figure out where Crawl lives and where to find data files.
The Mac Finder sets the working directory to / and the full path to the
executable is available in argv[0] when the user double-clicks.
- Converted some of the old char*s to std::string.
git-svn-id: https://crawl-ref.svn.sourceforge.net/svnroot/crawl-ref/trunk@1407 c06c8d41-db1a-0410-9941-cceddc491573
Diffstat (limited to 'crawl-ref/source')
-rw-r--r-- | crawl-ref/source/AppHdr.h | 1 | ||||
-rw-r--r-- | crawl-ref/source/chardump.cc | 2 | ||||
-rw-r--r-- | crawl-ref/source/externs.h | 12 | ||||
-rw-r--r-- | crawl-ref/source/files.cc | 25 | ||||
-rw-r--r-- | crawl-ref/source/files.h | 5 | ||||
-rw-r--r-- | crawl-ref/source/food.cc | 4 | ||||
-rw-r--r-- | crawl-ref/source/initfile.cc | 56 | ||||
-rw-r--r-- | crawl-ref/source/macro.cc | 2 | ||||
-rw-r--r-- | crawl-ref/source/makefile.unix | 6 | ||||
-rw-r--r-- | crawl-ref/source/newgame.cc | 4 |
10 files changed, 78 insertions, 39 deletions
diff --git a/crawl-ref/source/AppHdr.h b/crawl-ref/source/AppHdr.h index 48011145ff..b390230841 100644 --- a/crawl-ref/source/AppHdr.h +++ b/crawl-ref/source/AppHdr.h @@ -180,6 +180,7 @@ // NT and better are happy with /; I'm not sure how 9x reacts. #define FILE_SEPARATOR '/' + #define ALT_FILE_SEPARATOR '\\' // Uncomment to play sounds. winmm must be linked in if this is uncommented. // #define WINMM_PLAY_SOUNDS diff --git a/crawl-ref/source/chardump.cc b/crawl-ref/source/chardump.cc index 0458a9a594..2a78fd6e41 100644 --- a/crawl-ref/source/chardump.cc +++ b/crawl-ref/source/chardump.cc @@ -944,7 +944,7 @@ static std::string morgue_directory() { std::string dir = !Options.morgue_dir.empty()? Options.morgue_dir : - SysEnv.crawl_dir ? SysEnv.crawl_dir : ""; + !SysEnv.crawl_dir.empty() ? SysEnv.crawl_dir : ""; if (!dir.empty() && dir[dir.length() - 1] != FILE_SEPARATOR) dir += FILE_SEPARATOR; diff --git a/crawl-ref/source/externs.h b/crawl-ref/source/externs.h index 7eaad6c76a..e251d15ba5 100644 --- a/crawl-ref/source/externs.h +++ b/crawl-ref/source/externs.h @@ -1170,11 +1170,13 @@ extern std::vector<ghost_demon> ghosts; struct system_environment { - char *crawl_name; - char *crawl_pizza; - char *crawl_rc; - char *crawl_dir; - char *home; // only used by MULTIUSER systems + std::string crawl_name; + std::string crawl_pizza; + std::string crawl_rc; + std::string crawl_dir; + std::string crawl_base; // Directory from argv[0], may be used to + // locate datafiles. + std::string home; // only used by MULTIUSER systems bool board_with_nail; // Easter Egg silliness #ifdef DGL_SIMPLE_MESSAGING diff --git a/crawl-ref/source/files.cc b/crawl-ref/source/files.cc index 26f716d6ac..b9c14f74b3 100644 --- a/crawl-ref/source/files.cc +++ b/crawl-ref/source/files.cc @@ -213,6 +213,19 @@ std::vector<std::string> get_dir_files(const std::string &dirname) return (files); } +std::string get_parent_directory(const std::string &filename) +{ + std::string::size_type pos = filename.rfind(FILE_SEPARATOR); + if (pos != std::string::npos) + return filename.substr(0, pos + 1); +#ifdef ALT_FILE_SEPARATOR + pos = filename.rfind(ALT_FILE_SEPARATOR); + if (pos != std::string::npos) + return filename.substr(0, pos + 1); +#endif + return (""); +} + static bool file_exists(const std::string &name) { FILE *f = fopen(name.c_str(), "r"); @@ -272,9 +285,14 @@ static bool create_dirs(const std::string &dir) return (true); } -std::string datafile_path(const std::string &basename, bool croak_on_fail) +std::string datafile_path(const std::string &basename, + bool croak_on_fail, + bool test_base_path) { - std::string cdir = SysEnv.crawl_dir? SysEnv.crawl_dir : ""; + if (test_base_path && file_exists(basename)) + return (basename); + + std::string cdir = !SysEnv.crawl_dir.empty()? SysEnv.crawl_dir : ""; const std::string rawbases[] = { #ifdef DATA_DIR_PATH @@ -286,7 +304,6 @@ std::string datafile_path(const std::string &basename, bool croak_on_fail) const std::string prefixes[] = { std::string("dat") + FILE_SEPARATOR, - std::string("data") + FILE_SEPARATOR, std::string("docs") + FILE_SEPARATOR, std::string("..")+FILE_SEPARATOR+std::string("docs")+FILE_SEPARATOR, std::string("..") + FILE_SEPARATOR, @@ -307,6 +324,8 @@ std::string datafile_path(const std::string &basename, bool croak_on_fail) } #ifndef DATA_DIR_PATH + if (!SysEnv.crawl_base.empty()) + bases.push_back(SysEnv.crawl_base); bases.push_back(""); #endif diff --git a/crawl-ref/source/files.h b/crawl-ref/source/files.h index ecd63dcf98..1e480ba90e 100644 --- a/crawl-ref/source/files.h +++ b/crawl-ref/source/files.h @@ -27,8 +27,9 @@ extern FixedArray<bool, MAX_LEVELS, NUM_BRANCHES> tmp_file_pairs; std::string datafile_path(const std::string &basename, - bool croak_on_fail = true); - + bool croak_on_fail = true, + bool test_base_path = false); +std::string get_parent_directory(const std::string &filename); bool check_dir(const std::string &what, std::string &dir); bool travel_load_map( char branch, int absdepth ); diff --git a/crawl-ref/source/food.cc b/crawl-ref/source/food.cc index 49513143f8..0dda8af87f 100644 --- a/crawl-ref/source/food.cc +++ b/crawl-ref/source/food.cc @@ -1020,8 +1020,8 @@ static void eating(unsigned char item_class, int item_type) restore_stat(STAT_ALL, false); break; case FOOD_PIZZA: - if (SysEnv.crawl_pizza && !one_chance_in(3)) - mprf("Mmm... %s.", SysEnv.crawl_pizza); + if (!SysEnv.crawl_pizza.empty() && !one_chance_in(3)) + mprf("Mmm... %s.", SysEnv.crawl_pizza.c_str()); else { temp_rand = random2(9); diff --git a/crawl-ref/source/initfile.cc b/crawl-ref/source/initfile.cc index a41adc1d74..d10f5ccde0 100644 --- a/crawl-ref/source/initfile.cc +++ b/crawl-ref/source/initfile.cc @@ -881,11 +881,11 @@ void game_options::add_cset_override(char_set_type set, dungeon_char_type dc, std::string read_init_file(bool runscript) { const char* locations_data[][2] = { - { SysEnv.crawl_rc, "" }, - { SysEnv.crawl_dir, "init.txt" }, + { SysEnv.crawl_rc.c_str(), "" }, + { SysEnv.crawl_dir.c_str(), "init.txt" }, #ifdef MULTIUSER - { SysEnv.home, "/.crawlrc" }, - { SysEnv.home, "init.txt" }, + { SysEnv.home.c_str(), "/.crawlrc" }, + { SysEnv.home.c_str(), "init.txt" }, #endif { "", "init.txt" }, #ifdef WIN32CONSOLE @@ -1526,10 +1526,17 @@ void game_options::read_option_line(const std::string &str, bool runscript) else if (key == "lua_file" && runscript) { #ifdef CLUA_BINDINGS - clua.execfile(field.c_str()); - if (clua.error.length()) - fprintf(stderr, "Lua error: %s\n", - clua.error.c_str()); + const std::string lua_file = datafile_path(field, false, true); + if (lua_file.empty()) + { + fprintf(stderr, "Unable to find lua file: %s\n", field.c_str()); + } + else + { + clua.execfile(lua_file.c_str()); + if (clua.error.length()) + fprintf(stderr, "Lua error: %s\n", clua.error.c_str()); + } #endif } else if (key == "colour" || key == "color") @@ -1785,14 +1792,7 @@ void game_options::read_option_line(const std::string &str, bool runscript) { // We shouldn't bother to allocate this a second time // if the user puts two crawl_dir lines in the init file. - if (!SysEnv.crawl_dir) - SysEnv.crawl_dir = (char*)calloc(kPathLen, sizeof(char)); - - if (SysEnv.crawl_dir) - { - strncpy(SysEnv.crawl_dir, field.c_str(), kPathLen - 1); - SysEnv.crawl_dir[ kPathLen - 1 ] = 0; - } + SysEnv.crawl_dir = field; } else if (key == "race") { @@ -2359,17 +2359,22 @@ void game_options::read_option_line(const std::string &str, bool runscript) } } +static std::string check_string(const char *s) +{ + return (s? s : ""); +} + void get_system_environment(void) { // The player's name - SysEnv.crawl_name = getenv("CRAWL_NAME"); + SysEnv.crawl_name = check_string( getenv("CRAWL_NAME") ); // The player's pizza - SysEnv.crawl_pizza = getenv("CRAWL_PIZZA"); + SysEnv.crawl_pizza = check_string( getenv("CRAWL_PIZZA") ); // The directory which contians init.txt, macro.txt, morgue.txt // This should end with the appropriate path delimiter. - SysEnv.crawl_dir = getenv("CRAWL_DIR"); + SysEnv.crawl_dir = check_string( getenv("CRAWL_DIR") ); #ifdef DGL_SIMPLE_MESSAGING // Enable DGL_SIMPLE_MESSAGING only if SIMPLEMAIL and MAIL are set. @@ -2382,17 +2387,24 @@ void get_system_environment(void) #endif // The full path to the init file -- this over-rides CRAWL_DIR - SysEnv.crawl_rc = getenv("CRAWL_RC"); + SysEnv.crawl_rc = check_string( getenv("CRAWL_RC") ); // rename giant and giant spiked clubs SysEnv.board_with_nail = (getenv("BOARD_WITH_NAIL") != NULL); #ifdef MULTIUSER // The user's home directory (used to look for ~/.crawlrc file) - SysEnv.home = getenv("HOME"); + SysEnv.home = check_string( getenv("HOME") ); #endif } // end get_system_environment() +static void set_crawl_base_dir(const char *arg) +{ + if (!arg) + return; + + SysEnv.crawl_base = get_parent_directory(arg); +} // parse args, filling in Options and game environment as we go. // returns true if no unknown or malformed arguments were found. @@ -2426,6 +2438,8 @@ bool arg_seen[num_cmd_ops]; bool parse_args( int argc, char **argv, bool rc_only ) { + set_crawl_base_dir(argv[0]); + if (argc < 2) // no args! return (true); diff --git a/crawl-ref/source/macro.cc b/crawl-ref/source/macro.cc index 4eac355975..b665301499 100644 --- a/crawl-ref/source/macro.cc +++ b/crawl-ref/source/macro.cc @@ -171,7 +171,7 @@ static std::string get_macro_file() { std::string dir = !Options.macro_dir.empty()? Options.macro_dir : - SysEnv.crawl_dir? SysEnv.crawl_dir : ""; + !SysEnv.crawl_dir.empty()? SysEnv.crawl_dir : ""; if (!dir.empty()) { diff --git a/crawl-ref/source/makefile.unix b/crawl-ref/source/makefile.unix index ca2fc003be..b0e2c1c27a 100644 --- a/crawl-ref/source/makefile.unix +++ b/crawl-ref/source/makefile.unix @@ -195,8 +195,10 @@ install: $(GAME) ifeq ($(DATADIR),) $(error DATADIR not set! Set DATADIR and run make clean install again) endif - mkdir -p $(DATADIR)/data - cp dat/*.des $(DATADIR)/data + mkdir -p $(DATADIR)/dat + cp dat/*.des $(DATADIR)/dat + cp dat/*.txt $(DATADIR)/dat + cp -r lua $(DATADIR)/dat mkdir -p $(DATADIR)/docs cp ../docs/*.txt $(DATADIR)/docs chown -R $(INSTALL_UGRP) $(DATADIR) diff --git a/crawl-ref/source/newgame.cc b/crawl-ref/source/newgame.cc index 1e18f56fe0..223c0d7938 100644 --- a/crawl-ref/source/newgame.cc +++ b/crawl-ref/source/newgame.cc @@ -702,9 +702,9 @@ bool new_game(void) // copy name into you.your_name if set from environment -- // note that you.your_name could already be set from init.txt // this, clearly, will overwrite such information {dlb} - if (SysEnv.crawl_name) + if (!SysEnv.crawl_name.empty()) { - strncpy( you.your_name, SysEnv.crawl_name, kNameLen ); + strncpy( you.your_name, SysEnv.crawl_name.c_str(), kNameLen ); you.your_name[ kNameLen - 1 ] = 0; } |