15 |
GNU General Public License for more details. |
GNU General Public License for more details. |
16 |
|
|
17 |
You should have received a copy of the GNU General Public License |
You should have received a copy of the GNU General Public License |
18 |
along with Adonthell; if not, write to the Free Software |
along with Adonthell; if not, write to the Free Software |
19 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
20 |
*/ |
*/ |
21 |
|
|
22 |
/** |
/** |
23 |
* @file base/paths.cc |
* @file base/paths.cc |
24 |
* @author Alexandre Courbot <alexandrecourbot@linuxgames.com> |
* @author Alexandre Courbot <alexandrecourbot@linuxgames.com> |
25 |
* |
* |
26 |
* @brief Defines some primitives to get essential game paths. |
* @brief Defines some primitives to get essential game paths. |
27 |
* |
* |
28 |
* |
* |
29 |
*/ |
*/ |
30 |
|
|
31 |
#include <cstdlib> |
#include <cstdlib> |
47 |
lt_dlhandle get_module (const std::string & modname) |
lt_dlhandle get_module (const std::string & modname) |
48 |
{ |
{ |
49 |
lt_dlhandle ret; |
lt_dlhandle ret; |
50 |
|
|
51 |
/* Try the MODULES_ENV variable first */ |
/* Try the MODULES_ENV variable first */ |
52 |
const char * mod_env_str = getenv(MODULES_ENV); |
const char * mod_env_str = getenv(MODULES_ENV); |
53 |
std::string mod_env = (mod_env_str ? mod_env_str : ""); |
std::string mod_env = (mod_env_str ? mod_env_str : ""); |
54 |
|
|
55 |
if (!mod_env.empty()) |
if (!mod_env.empty()) |
56 |
{ |
{ |
57 |
mod_env += modname; |
mod_env += modname; |
58 |
ret = lt_dlopenext(mod_env.c_str()); |
ret = lt_dlopenext(mod_env.c_str()); |
59 |
if (ret) return ret; |
if (ret) return ret; |
60 |
} |
} |
61 |
|
|
62 |
/* Try the hard-coded path then */ |
/* Try the hard-coded path then */ |
63 |
mod_env = PKGLIBDIR + modname; |
mod_env = PKGLIBDIR + modname; |
64 |
ret = lt_dlopenext(mod_env.c_str()); |
ret = lt_dlopenext(mod_env.c_str()); |
65 |
if (ret) return ret; |
if (ret) return ret; |
66 |
|
|
67 |
cerr << "Failed to load module " << modname << ": " << lt_dlerror() << endl; |
cerr << "Failed to load module " << modname << ": " << lt_dlerror() << endl; |
68 |
|
|
69 |
return NULL; |
return NULL; |
70 |
} |
} |
71 |
} |
} |
72 |
|
|
73 |
// initialize data search paths |
// initialize data search paths |
76 |
// no save game directory unless we actually load a game |
// no save game directory unless we actually load a game |
77 |
IncludeSaveDir = false; |
IncludeSaveDir = false; |
78 |
IncludeUserDir = false; |
IncludeUserDir = false; |
79 |
|
|
80 |
// set OS specific directory containing configuration and saved games |
// set OS specific directory containing configuration and saved games |
81 |
#if defined(__APPLE__) // OSX |
#if defined(__APPLE__) // OSX |
82 |
CfgDataDir = string (getenv ("HOME")) + "/Library/Adonthell/"; |
CfgDataDir = string (getenv ("HOME")) + "/Library/Adonthell/"; |
93 |
|
|
94 |
// user data dir might be optional |
// user data dir might be optional |
95 |
UserDataDir = userdatadir; |
UserDataDir = userdatadir; |
96 |
if (UserDataDir != "") |
if (UserDataDir != "") |
97 |
{ |
{ |
98 |
if (UserDataDir[UserDataDir.size () - 1] != '/') UserDataDir += "/"; |
if (UserDataDir[UserDataDir.size () - 1] != '/') UserDataDir += "/"; |
99 |
UserDataDir += game + "/"; |
UserDataDir += game + "/"; |
100 |
|
|
101 |
// make sure the given user data dir is actually accessible |
// make sure the given user data dir is actually accessible |
102 |
if (exists (UserDataDir)) IncludeUserDir = true; |
if (exists (UserDataDir)) IncludeUserDir = true; |
103 |
} |
} |
104 |
|
|
105 |
// builtin data directory |
// builtin data directory |
106 |
GameDataDir = DATA_DIR; |
GameDataDir = DATA_DIR; |
107 |
GameDataDir += "/games/"; |
GameDataDir += "/games/"; |
108 |
GameDataDir += game + "/"; |
GameDataDir += game + "/"; |
109 |
|
|
110 |
// make sure game data dir exists |
// make sure game data dir exists |
111 |
return exists (GameDataDir); |
return exists (GameDataDir); |
112 |
} |
} |
117 |
if (dir != "" && exists (CfgDataDir + dir)) |
if (dir != "" && exists (CfgDataDir + dir)) |
118 |
{ |
{ |
119 |
SaveDataDir = CfgDataDir + dir; |
SaveDataDir = CfgDataDir + dir; |
120 |
IncludeSaveDir = true; |
IncludeSaveDir = true; |
121 |
} |
} |
122 |
else |
else |
123 |
{ |
{ |
124 |
IncludeSaveDir = false; |
IncludeSaveDir = false; |
125 |
SaveDataDir = ""; |
SaveDataDir = ""; |
126 |
} |
} |
127 |
} |
} |
130 |
bool paths::open (igzstream & file, const std::string & path) |
bool paths::open (igzstream & file, const std::string & path) |
131 |
{ |
{ |
132 |
if (file.is_open ()) file.close (); |
if (file.is_open ()) file.close (); |
133 |
|
|
134 |
if (IncludeSaveDir && file.open (SaveDataDir + path)) return true; |
if (IncludeSaveDir && file.open (SaveDataDir + path)) return true; |
135 |
if (IncludeUserDir && file.open (UserDataDir + path)) return true; |
if (IncludeUserDir && file.open (UserDataDir + path)) return true; |
136 |
if (file.open (GameDataDir + path)) return true; |
if (file.open (GameDataDir + path)) return true; |
152 |
closedir (dir); |
closedir (dir); |
153 |
return true; |
return true; |
154 |
} |
} |
155 |
|
|
156 |
// dir doesn't exist or not enough privileges ... |
// dir doesn't exist or not enough privileges ... |
157 |
fprintf (stderr, "*** warning: directory '%s' cannot be accessed!\n", path.c_str ()); |
fprintf (stderr, "*** warning: directory '%s' cannot be accessed!\n", path.c_str ()); |
158 |
return false; |
return false; |
|
} |
|
159 |
|
} |
160 |
|
|