|
From: Lasse Kärkkäi. <tr...@us...> - 2009-11-11 17:15:48
|
Module: performous
Branch: master
Commit: a7ba20a91ad31c982af182bce2f4105893657db8
Author: Lasse Karkkainen <tro...@tr...>
Date: Wed Nov 11 19:12:24 2009 +0200
Rewrite most of the data path code.
Rename getXdgPath to getPath to avoid clutter and confusion.
Make the config file path setting empty by default.
---
data/schema.xml | 4 ---
game/fs.cc | 70 +++++++++++++++++++++++++++++------------------
game/fs.hh | 2 +-
game/guitargraph.cc | 24 ++++++++--------
game/screen_practice.cc | 12 ++++----
5 files changed, 62 insertions(+), 50 deletions(-)
diff --git a/data/schema.xml b/data/schema.xml
index 93a9dde..4275468 100644
--- a/data/schema.xml
+++ b/data/schema.xml
@@ -237,10 +237,6 @@ to save the current settings to XML.
</locale>
</entry>
<entry name="system/path" type="string_list">
- <stringvalue>/usr/local/share/games/performous/</stringvalue>
- <stringvalue>/usr/share/games/performous/</stringvalue>
- <stringvalue>../</stringvalue><!-- For Windows where the program is started in bin/ -->
- <stringvalue>~/.performous/</stringvalue>
<locale name="C">
<short>System folders</short>
<long>Where to look for performous data.</long>
diff --git a/game/fs.cc b/game/fs.cc
index ee54c24..e1ace89 100644
--- a/game/fs.cc
+++ b/game/fs.cc
@@ -4,6 +4,8 @@
#include <plugin++/execname.hpp>
#include <cstdlib>
#include <iostream>
+#include <list>
+#include <sstream>
fs::path getHomeDir() {
static fs::path dir;
@@ -33,52 +35,66 @@ std::string getThemePath(std::string const& filename) {
if (theme.empty()) throw std::runtime_error("Configuration value game/theme is empty");
// Figure out theme folder (if theme name rather than path was given)
if (theme.find('/') == std::string::npos) {
- return getXdgPath(fs::path("themes") / theme / filename);
+ return getPath(fs::path("themes") / theme / filename);
} else {
if (*theme.rbegin() == '/') theme.erase(theme.size() - 1); // Remove trailing slash
return theme + "/" + filename;
}
}
-std::string getXdgPath(fs::path const& filename) {
- static std::vector<fs::path> dir;
+namespace {
+ bool pathNotExist(fs::path const& p) {
+ if (exists(p)) {
+ std::cout << ">>> Using data path \"" << p.string() << "\"" << std::endl;
+ return false;
+ }
+ std::cout << ">>> Not using \"" << p.string() << "\" (does not exist)" << std::endl;
+ return true;
+ }
+}
+
+std::string getPath(fs::path const& filename) {
+ typedef std::list<fs::path> Dirs;
+ static Dirs dirs;
static bool initialized = false;
if (!initialized) {
initialized = true;
- // Adding PERFORMOUS_DATA_DIR (compatibility)
- char const* env_data_dir = getenv("PERFORMOUS_DATA_DIR");
- if (env_data_dir) {
- dir.push_back(env_data_dir);
+ fs::path shortDir = "performous";
+ fs::path shareDir = "share/games" / shortDir;
+#ifdef _WIN32
+ // Add APPLIC~1 (user-specific application data) FIXME: Not tested
+ {
+ char const* appdata = getenv("APPDATA");
+ if (appdata) dirs.push_back(appdata / shortDir);
}
+#else
// Adding XDG_DATA_HOME
- char const* xdg_data_home = getenv("XDG_DATA_HOME");
- if (xdg_data_home) {
- dir.push_back(fs::path(xdg_data_home) / "performous");
- } else {
- dir.push_back(getHomeDir() / ".local" / "share" / "performous" );
+ {
+ char const* xdg_data_home = getenv("XDG_DATA_HOME");
+ // FIXME: Should this use "games" or not?
+ dirs.push_back(xdg_data_home ? xdg_data_home / shortDir : getHomeDir() / ".local" / shareDir);
}
+#endif
// Adding relative path from executable
- dir.push_back(fs::path(plugin::execname()).parent_path() / ".." / "share" / "performous");
+ dirs.push_back(fs::path(plugin::execname()).parent_path().parent_path() / shareDir);
+#ifndef _WIN32
// Adding XDG_DATA_DIRS
- char const* xdg_data_dirs = getenv("XDG_DATA_DIRS");
- if (xdg_data_dirs) {
- // here explode ":"
- } else {
- dir.push_back( "/usr/local/share/performous/" );
- dir.push_back( "/usr/share/performous/" );
+ {
+ char const* xdg_data_dirs = getenv("XDG_DATA_DIRS");
+ std::istringstream iss(xdg_data_dirs ? xdg_data_dirs : "/usr/local/share/:/usr/share/");
+ for (std::string p; std::getline(iss, p, ':'); dirs.push_back(p / shortDir)) {}
}
- // Adding some defaults
+#endif
+ // Adding paths from config file
std::vector<std::string> pathes = config["system/path"].sl();
- std::transform(pathes.begin(), pathes.end(), std::inserter(dir, dir.end()), pathMangle);
- // Some output
- for (std::vector<fs::path>::const_iterator it = dir.begin(); it != dir.end(); ++it) {
- std::cout << ">>> Adding XDG search path \"" << it->string() << "\"" << std::endl;
- }
+ std::transform(pathes.begin(), pathes.end(), std::inserter(dirs, dirs.end()), pathMangle);
+ // Check if they actually exist and print debug
+ dirs.remove_if(pathNotExist);
}
- for (std::vector<fs::path>::const_iterator it = dir.begin(); it != dir.end(); ++it) {
+ for (Dirs::const_iterator it = dirs.begin(); it != dirs.end(); ++it) {
fs::path p = *it;
p /= filename;
if( fs::exists(p) ) return p.string();
}
- throw std::runtime_error("Cannot find file \"" + filename.string() + "\" inside XDG data pathes");
+ throw std::runtime_error("Cannot find file \"" + filename.string() + "\" in any of Performous data folders");
}
diff --git a/game/fs.hh b/game/fs.hh
index 4566149..fd63e86 100644
--- a/game/fs.hh
+++ b/game/fs.hh
@@ -15,4 +15,4 @@ fs::path pathMangle(fs::path const& dir);
std::string getThemePath(std::string const& filename);
/** Get full path to a share file **/
-std::string getXdgPath(fs::path const& filename);
+std::string getPath(fs::path const& filename);
diff --git a/game/guitargraph.cc b/game/guitargraph.cc
index 9d6c996..fde67e5 100644
--- a/game/guitargraph.cc
+++ b/game/guitargraph.cc
@@ -76,18 +76,18 @@ GuitarGraph::GuitarGraph(Audio& audio, Song const& song, std::string track):
}
unsigned int sr = m_audio.getSR();
if (g_samplesD.empty()) {
- g_samplesD.push_back(Sample(getXdgPath("sounds/drum_bass.ogg"), sr));
- g_samplesD.push_back(Sample(getXdgPath("sounds/drum_snare.ogg"), sr));
- g_samplesD.push_back(Sample(getXdgPath("sounds/drum_hi-hat.ogg"), sr));
- g_samplesD.push_back(Sample(getXdgPath("sounds/drum_tom1.ogg"), sr));
- g_samplesD.push_back(Sample(getXdgPath("sounds/drum_cymbal.ogg"), sr));
- //g_samplesD.push_back(Sample(getXdgPath("sounds/drum_tom2.ogg"), sr));
- g_samplesG.push_back(Sample(getXdgPath("sounds/guitar_fail1.ogg"), sr));
- g_samplesG.push_back(Sample(getXdgPath("sounds/guitar_fail2.ogg"), sr));
- g_samplesG.push_back(Sample(getXdgPath("sounds/guitar_fail3.ogg"), sr));
- g_samplesG.push_back(Sample(getXdgPath("sounds/guitar_fail4.ogg"), sr));
- g_samplesG.push_back(Sample(getXdgPath("sounds/guitar_fail5.ogg"), sr));
- g_samplesG.push_back(Sample(getXdgPath("sounds/guitar_fail6.ogg"), sr));
+ g_samplesD.push_back(Sample(getPath("sounds/drum_bass.ogg"), sr));
+ g_samplesD.push_back(Sample(getPath("sounds/drum_snare.ogg"), sr));
+ g_samplesD.push_back(Sample(getPath("sounds/drum_hi-hat.ogg"), sr));
+ g_samplesD.push_back(Sample(getPath("sounds/drum_tom1.ogg"), sr));
+ g_samplesD.push_back(Sample(getPath("sounds/drum_cymbal.ogg"), sr));
+ //g_samplesD.push_back(Sample(getPath("sounds/drum_tom2.ogg"), sr));
+ g_samplesG.push_back(Sample(getPath("sounds/guitar_fail1.ogg"), sr));
+ g_samplesG.push_back(Sample(getPath("sounds/guitar_fail2.ogg"), sr));
+ g_samplesG.push_back(Sample(getPath("sounds/guitar_fail3.ogg"), sr));
+ g_samplesG.push_back(Sample(getPath("sounds/guitar_fail4.ogg"), sr));
+ g_samplesG.push_back(Sample(getPath("sounds/guitar_fail5.ogg"), sr));
+ g_samplesG.push_back(Sample(getPath("sounds/guitar_fail6.ogg"), sr));
}
unsigned int i = 0;
for (TrackMap::const_iterator it = m_song.track_map.begin(); it != m_song.track_map.end(); ++it,++i) {
diff --git a/game/screen_practice.cc b/game/screen_practice.cc
index 505db7b..1dcb9fa 100644
--- a/game/screen_practice.cc
+++ b/game/screen_practice.cc
@@ -21,12 +21,12 @@ void ScreenPractice::enter() {
drums.reset(new input::InputDev(input::DRUMS));
} catch (std::runtime_error&) {drums.reset();}
unsigned int sr = m_audio.getSR();
- m_samples.push_back(Sample(getXdgPath("sounds/drum_bass.ogg"), sr));
- m_samples.push_back(Sample(getXdgPath("sounds/drum_snare.ogg"), sr));
- m_samples.push_back(Sample(getXdgPath("sounds/drum_hi-hat.ogg"), sr));
- m_samples.push_back(Sample(getXdgPath("sounds/drum_tom1.ogg"), sr));
- m_samples.push_back(Sample(getXdgPath("sounds/drum_cymbal.ogg"), sr));
- //m_samples.push_back(Sample(getXdgPath("sounds/drum_tom2.ogg"), sr));
+ m_samples.push_back(Sample(getPath("sounds/drum_bass.ogg"), sr));
+ m_samples.push_back(Sample(getPath("sounds/drum_snare.ogg"), sr));
+ m_samples.push_back(Sample(getPath("sounds/drum_hi-hat.ogg"), sr));
+ m_samples.push_back(Sample(getPath("sounds/drum_tom1.ogg"), sr));
+ m_samples.push_back(Sample(getPath("sounds/drum_cymbal.ogg"), sr));
+ //m_samples.push_back(Sample(getPath("sounds/drum_tom2.ogg"), sr));
}
void ScreenPractice::exit() {
|