From 429775d4855e2c8b2fc1a639cbd1e0fa844ea798 Mon Sep 17 00:00:00 2001 From: Jan-Peter Nilsson Date: Tue, 4 Jan 2011 01:34:55 +0100 Subject: [PATCH] Store configuration in /etc/openmw/ and ~/.openmw/ on linux Requires new openengine version. --- apps/openmw/engine.cpp | 22 +++++++++---- apps/openmw/engine.hpp | 4 +++ apps/openmw/main.cpp | 25 +++++++++------ apps/openmw/mwrender/sky.cpp | 13 +++++--- apps/openmw/mwrender/sky.hpp | 5 ++- apps/openmw/mwworld/world.cpp | 4 +-- apps/openmw/mwworld/world.hpp | 4 +-- apps/openmw/path.hpp | 59 +++++++++++++++++++++++++++++++++++ 8 files changed, 111 insertions(+), 25 deletions(-) create mode 100644 apps/openmw/path.hpp diff --git a/apps/openmw/engine.cpp b/apps/openmw/engine.cpp index fda8e8d78..31f59a420 100644 --- a/apps/openmw/engine.cpp +++ b/apps/openmw/engine.cpp @@ -41,6 +41,7 @@ #include #include "mwgui/class.hpp" +#include "path.hpp" //using namespace ESM; @@ -245,6 +246,12 @@ void OMW::Engine::setDataDir (const boost::filesystem::path& dataDir) mDataDir = boost::filesystem::system_complete (dataDir); } +// Set resource dir +void OMW::Engine::setResourceDir (const boost::filesystem::path& parResDir) +{ + mResDir = boost::filesystem::system_complete(parResDir); +} + // Set start cell name (only interiors for now) void OMW::Engine::setCell (const std::string& cellName) @@ -300,16 +307,20 @@ void OMW::Engine::go() std::cout << "Data directory: " << mDataDir << "\n"; - const char* plugCfg = "plugins.cfg"; + std::string cfgDir = OMW::Path::getPath(OMW::Path::USER_CFG_PATH, "openmw", ""); + std::string plugCfg = "plugins.cfg"; + std::string ogreCfg = "ogre.cfg"; + ogreCfg.insert(0, cfgDir); + plugCfg.insert(0, cfgDir); - mOgre.configure(!isFile("ogre.cfg"), plugCfg, false); + mOgre.configure(!isFile(ogreCfg.c_str()), cfgDir, plugCfg, false); addResourcesDirectory (mDataDir / "Meshes"); addResourcesDirectory (mDataDir / "Textures"); // This has to be added BEFORE MyGUI is initialized, as it needs // to find core.xml here. - addResourcesDirectory("resources/mygui/"); + addResourcesDirectory(mResDir / "mygui"); // Create the window mOgre.createWindow("OpenMW"); @@ -317,11 +328,10 @@ void OMW::Engine::go() loadBSA(); // Create the world - mEnvironment.mWorld = new MWWorld::World (mOgre, mDataDir, mMaster, mNewGame, mEnvironment); + mEnvironment.mWorld = new MWWorld::World (mOgre, mDataDir, mMaster, mResDir, mNewGame, mEnvironment); // Set up the GUI system - mGuiManager = new OEngine::GUI::MyGUIManager(mOgre.getWindow(), - mOgre.getScene()); + mGuiManager = new OEngine::GUI::MyGUIManager(mOgre.getWindow(), mOgre.getScene(), false, cfgDir); MyGUI::FactoryManager::getInstance().registerFactory("Widget"); MyGUI::FactoryManager::getInstance().registerFactory("Widget"); MyGUI::FactoryManager::getInstance().registerFactory("Widget"); diff --git a/apps/openmw/engine.hpp b/apps/openmw/engine.hpp index eeefbc65d..5b636398c 100644 --- a/apps/openmw/engine.hpp +++ b/apps/openmw/engine.hpp @@ -57,6 +57,7 @@ namespace OMW //int nFiles; boost::filesystem::path mDataDir; + boost::filesystem::path mResDir; OEngine::Render::OgreRenderer mOgre; std::string mCellName; std::string mMaster; @@ -107,6 +108,9 @@ namespace OMW /// Set data dir void setDataDir (const boost::filesystem::path& dataDir); + /// Set resource dir + void setResourceDir (const boost::filesystem::path& parResDir); + /// Set start cell name (only interiors for now) void setCell (const std::string& cellName); diff --git a/apps/openmw/main.cpp b/apps/openmw/main.cpp index 393228777..990e6b51c 100644 --- a/apps/openmw/main.cpp +++ b/apps/openmw/main.cpp @@ -7,9 +7,10 @@ #include #include "engine.hpp" +#include "path.hpp" #if defined(_WIN32) && !defined(_CONSOLE) -#include +#include #include # if !defined(_DEBUG) @@ -42,6 +43,8 @@ bool parseOptions (int argc, char**argv, OMW::Engine& engine) ("help", "print help message") ("data", bpo::value()->default_value ("data"), "set data directory") + ("resources", bpo::value()->default_value ("resources"), + "set resources directory") ("start", bpo::value()->default_value ("Beshara"), "set initial cell") ("master", bpo::value()->default_value ("Morrowind"), @@ -55,20 +58,23 @@ bool parseOptions (int argc, char**argv, OMW::Engine& engine) bpo::variables_map variables; -#if OGRE_PLATFORM == OGRE_PLATFORM_APPLE - std::string configFilePath(macBundlePath() + "/Contents/MacOS/openmw.cfg"); - std::ifstream configFile (configFilePath.c_str()); -#else - std::ifstream configFile ("openmw.cfg"); -#endif + std::string cfgFile = OMW::Path::getPath(OMW::Path::GLOBAL_CFG_PATH, "openmw", "openmw.cfg"); + std::cout << "Using global config file: " << cfgFile << std::endl; + std::ifstream globalConfigFile(cfgFile.c_str()); + + cfgFile = OMW::Path::getPath(OMW::Path::USER_CFG_PATH, "openmw", "openmw.cfg"); + std::cout << "Using user config file: " << cfgFile << std::endl; + std::ifstream userConfigFile(cfgFile.c_str()); bpo::parsed_options valid_opts = bpo::command_line_parser(argc, argv).options(desc).allow_unregistered().run(); bpo::store(valid_opts, variables); bpo::notify(variables); - if (configFile.is_open()) - bpo::store ( bpo::parse_config_file (configFile, desc), variables); + if (userConfigFile.is_open()) + bpo::store ( bpo::parse_config_file (userConfigFile, desc), variables); + if (globalConfigFile.is_open()) + bpo::store ( bpo::parse_config_file (globalConfigFile, desc), variables); if (variables.count ("help")) { @@ -77,6 +83,7 @@ bool parseOptions (int argc, char**argv, OMW::Engine& engine) } engine.setDataDir (variables["data"].as()); + engine.setResourceDir (variables["resources"].as()); engine.setCell (variables["start"].as()); engine.addMaster (variables["master"].as()); diff --git a/apps/openmw/mwrender/sky.cpp b/apps/openmw/mwrender/sky.cpp index d86ef35fb..95601d043 100644 --- a/apps/openmw/mwrender/sky.cpp +++ b/apps/openmw/mwrender/sky.cpp @@ -19,7 +19,8 @@ namespace MWRender public: CaelumManager (Ogre::RenderWindow* pRenderWindow, - Ogre::Camera* pCamera); + Ogre::Camera* pCamera, + const boost::filesystem::path& resDir); virtual ~CaelumManager (); virtual void enable() {} @@ -44,7 +45,8 @@ namespace MWRender }; CaelumManager::CaelumManager (Ogre::RenderWindow* pRenderWindow, - Ogre::Camera* pCamera) + Ogre::Camera* pCamera, + const boost::filesystem::path& resDir) : mpCaelumSystem (NULL) { using namespace Ogre; @@ -55,7 +57,7 @@ namespace MWRender // Load the Caelum resources // - ResourceGroupManager::getSingleton().addResourceLocation("resources/caelum", "FileSystem", "Caelum"); + ResourceGroupManager::getSingleton().addResourceLocation((resDir / "caelum").string(), "FileSystem", "Caelum"); ResourceGroupManager::getSingleton().initialiseAllResourceGroups(); // Load the Caelum resources @@ -89,13 +91,14 @@ namespace MWRender /// \return NULL on failure. /// SkyManager* SkyManager::create (Ogre::RenderWindow* pRenderWindow, - Ogre::Camera* pCamera) + Ogre::Camera* pCamera, + const boost::filesystem::path& resDir) { SkyManager* pSkyManager = NULL; try { - pSkyManager = new CaelumManager(pRenderWindow, pCamera); + pSkyManager = new CaelumManager(pRenderWindow, pCamera, resDir); } catch (Ogre::Exception& e) { diff --git a/apps/openmw/mwrender/sky.hpp b/apps/openmw/mwrender/sky.hpp index 1aae5126e..446ed3f1c 100644 --- a/apps/openmw/mwrender/sky.hpp +++ b/apps/openmw/mwrender/sky.hpp @@ -1,6 +1,8 @@ #ifndef _GAME_RENDER_SKY_H #define _GAME_RENDER_SKY_H +#include + namespace Ogre { class RenderWindow; @@ -16,7 +18,8 @@ namespace MWRender { public: static SkyManager* create (Ogre::RenderWindow* pRenderWindow, - Ogre::Camera* pCamera); + Ogre::Camera* pCamera, + const boost::filesystem::path& resDir); virtual ~SkyManager() {} virtual void enable() = 0; diff --git a/apps/openmw/mwworld/world.cpp b/apps/openmw/mwworld/world.cpp index 8dfc8c966..44bfa9112 100644 --- a/apps/openmw/mwworld/world.cpp +++ b/apps/openmw/mwworld/world.cpp @@ -316,7 +316,7 @@ namespace MWWorld } World::World (OEngine::Render::OgreRenderer& renderer, const boost::filesystem::path& dataDir, - const std::string& master, bool newGame, Environment& environment) + const std::string& master, const boost::filesystem::path& resDir, bool newGame, Environment& environment) : mSkyManager (0), mScene (renderer), mPlayer (0), mCurrentCell (0), mGlobalVariables (0), mSky (false), mCellChanged (false), mEnvironment (environment) { @@ -341,7 +341,7 @@ namespace MWWorld } mSkyManager = - MWRender::SkyManager::create(renderer.getWindow(), mScene.getCamera()); + MWRender::SkyManager::create(renderer.getWindow(), mScene.getCamera(), resDir); } World::~World() diff --git a/apps/openmw/mwworld/world.hpp b/apps/openmw/mwworld/world.hpp index 4e9e32ecc..ce66a63d7 100644 --- a/apps/openmw/mwworld/world.hpp +++ b/apps/openmw/mwworld/world.hpp @@ -89,8 +89,8 @@ namespace MWWorld public: - World (OEngine::Render::OgreRenderer& renderer, const boost::filesystem::path& master, - const std::string& dataDir, bool newGame, Environment& environment); + World (OEngine::Render::OgreRenderer& renderer, const boost::filesystem::path& dataDir, + const std::string& master, const boost::filesystem::path& resDir, bool newGame, Environment& environment); ~World(); diff --git a/apps/openmw/path.hpp b/apps/openmw/path.hpp new file mode 100644 index 000000000..8e8dac09e --- /dev/null +++ b/apps/openmw/path.hpp @@ -0,0 +1,59 @@ +#ifndef PATH__HPP +#define PATH__HPP + +#include +#include + +#if OGRE_PLATFORM_LINUX +#include +#include +#include //getenv +#endif + +namespace OMW +{ + class Path + { + public: + enum PathTypeEnum + { + USER_CFG_PATH, + GLOBAL_CFG_PATH + }; + + //TODO use application data dir on windows? + static std::string getPath(PathTypeEnum parType, const std::string parApp, const std::string parFile) + { + std::string theBasePath; + if(parType == GLOBAL_CFG_PATH) + { +#if OGRE_PLATFORM == OGRE_PLATFORM_APPLE + theBasePath = macBundlePath() + "/Contents/MacOS/"; //FIXME do we have global/local with OSX? +#elif OGRE_PLATFORM_LINUX + theBasePath = "/etc/"+parApp+"/"; +#else + theBasePath = ""; +#endif + + } + else + { +#if OGRE_PLATFORM == OGRE_PLATFORM_APPLE + theBasePath = macBundlePath() + "/Contents/MacOS/"; //FIXME do we have global/local with OSX? +#elif OGRE_PLATFORM_LINUX + const char* homedir; + if ((homedir = getenv("HOME")) == NULL) + return NULL; + theBasePath = std::string(homedir)+"/."+parApp+"/"; + mkdir(theBasePath.c_str(), 0777); +#else + theBasePath = ""; +#endif + } + + theBasePath.append(parFile); + return theBasePath; + } + }; +} +#endif