1
1
forked from 0ad/0ad

move Paths into separate file (requires update-workspaces)

This was SVN commit r7088.
This commit is contained in:
janwas 2009-08-08 11:11:26 +00:00
parent 62122370b5
commit 10cc678ffb
3 changed files with 172 additions and 121 deletions

View File

@ -17,16 +17,12 @@
#include "precompiled.h"
#if OS_WIN
#include "lib/sysdep/os/win/wutil.h"
#endif
#include "lib/external_libraries/sdl.h"
#include "lib/ogl.h"
#include "lib/timer.h"
#include "lib/input.h"
#include "lib/lockfree.h"
#include "lib/app_hooks.h"
#include "lib/sysdep/sysdep.h"
#include "lib/sysdep/cpu.h"
#include "lib/sysdep/gfx.h"
#include "lib/res/h_mgr.h"
@ -92,6 +88,7 @@
#include "ps/Pyrogenesis.h" // psSetLogDir
#include "ps/GameSetup/Atlas.h"
#include "ps/GameSetup/GameSetup.h"
#include "ps/GameSetup/Paths.h"
#include "ps/GameSetup/Config.h"
#include "ps/GameSetup/CmdLineArgs.h"
@ -540,127 +537,11 @@ static size_t ChooseCacheSize()
}
class Paths
{
public:
Paths(const CStr& argv0, const char* subdirectoryName)
{
m_root = Root(argv0);
m_rdata = m_root/"data";
// everything is a subdirectory of the root
if(!subdirectoryName)
{
m_data = m_rdata;
m_config = m_data/"config";
m_cache = m_data/"cache";
m_logs = m_root/"logs";
}
else
{
#if OS_WIN
const fs::path appdata(fs::path(win_appdata_dir)/subdirectoryName);
m_data = appdata/"data";
m_config = appdata/"config";
m_cache = appdata/"cache";
m_logs = appdata/"logs";
#else
const char* envHome = getenv("HOME");
debug_assert(envHome);
const fs::path home(envHome);
m_data = XDG_Path("XDG_DATA_HOME", home, home/".local/share")/subdirectoryName;
m_config = XDG_Path("XDG_CONFIG_HOME", home, home/".config")/subdirectoryName;
m_cache = XDG_Path("XDG_CACHE_HOME", home, home/".cache")/subdirectoryName;
m_logs = m_config/"logs";
#endif
}
}
const fs::path& Root() const
{
return m_root;
}
const fs::path& RData() const
{
return m_rdata;
}
const fs::path& Data() const
{
return m_data;
}
const fs::path& Config() const
{
return m_config;
}
const fs::path& Cache() const
{
return m_cache;
}
const fs::path& Logs() const
{
return m_logs;
}
private:
static fs::path Root(const CStr& argv0)
{
// get full path to executable
char pathname[PATH_MAX];
// .. first try safe, but system-dependent version
if(sys_get_executable_name(pathname, PATH_MAX) < 0)
{
// .. failed; use argv[0]
errno = 0;
if(!realpath(argv0.c_str(), pathname))
WARN_ERR(LibError_from_errno(false));
}
// make sure it's valid
errno = 0;
if(access(pathname, X_OK) < 0)
WARN_ERR(LibError_from_errno(false));
fs::path path(pathname);
for(size_t i = 0; i < 3; i++) // remove "system/name.exe"
path.remove_leaf();
return path;
}
static fs::path XDG_Path(const char* envname, const fs::path& home, const fs::path& defaultPath)
{
const char* path = getenv(envname);
if(path)
{
if(path[0] != '/') // relative to $HOME
return home/path;
return fs::path(path);
}
return defaultPath;
}
// read-only directories, fixed paths relative to executable
fs::path m_root;
fs::path m_rdata;
// writable directories
fs::path m_data;
fs::path m_config;
fs::path m_cache;
fs::path m_logs; // special-cased in single-root-folder installations
};
static void InitVfs(const CmdLineArgs& args)
{
TIMER("InitVfs");
const char* subdirectory = args.Has("writableRoot")? 0 : "0ad";
const Paths paths(args.GetArg0(), subdirectory);
const Paths paths(args);
fs::path logs(paths.Logs());
CreateDirectories(logs, 0700);

View File

@ -0,0 +1,97 @@
/* Copyright (C) 2009 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* 0 A.D. is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
*/
#include "precompiled.h"
#include "Paths.h"
#if OS_WIN
#include "lib/sysdep/os/win/wutil.h" // win_appdata_dir
#endif
#include "lib/sysdep/sysdep.h" // sys_get_executable_name
Paths::Paths(const CmdLineArgs& args)
{
m_root = Root(args.GetArg0());
m_rdata = m_root/"data";
const char* subdirectoryName = args.Has("writableRoot")? 0 : "0ad";
// everything is a subdirectory of the root
if(!subdirectoryName)
{
m_data = m_rdata;
m_config = m_data/"config";
m_cache = m_data/"cache";
m_logs = m_root/"logs";
}
else
{
#if OS_WIN
const fs::path appdata(fs::path(win_appdata_dir)/subdirectoryName);
m_data = appdata/"data";
m_config = appdata/"config";
m_cache = appdata/"cache";
m_logs = appdata/"logs";
#else
const char* envHome = getenv("HOME");
debug_assert(envHome);
const fs::path home(envHome);
m_data = XDG_Path("XDG_DATA_HOME", home, home/".local/share")/subdirectoryName;
m_config = XDG_Path("XDG_CONFIG_HOME", home, home/".config")/subdirectoryName;
m_cache = XDG_Path("XDG_CACHE_HOME", home, home/".cache")/subdirectoryName;
m_logs = m_config/"logs";
#endif
}
}
/*static*/ fs::path Paths::Root(const CStr& argv0)
{
// get full path to executable
char pathname[PATH_MAX];
// .. first try safe, but system-dependent version
if(sys_get_executable_name(pathname, PATH_MAX) < 0)
{
// .. failed; use argv[0]
errno = 0;
if(!realpath(argv0.c_str(), pathname))
WARN_ERR(LibError_from_errno(false));
}
// make sure it's valid
errno = 0;
if(access(pathname, X_OK) < 0)
WARN_ERR(LibError_from_errno(false));
fs::path path(pathname);
for(size_t i = 0; i < 3; i++) // remove "system/name.exe"
path.remove_leaf();
return path;
}
/*static*/ fs::path Paths::XDG_Path(const char* envname, const fs::path& home, const fs::path& defaultPath)
{
const char* path = getenv(envname);
if(path)
{
if(path[0] != '/') // relative to $HOME
return home/path;
return fs::path(path);
}
return defaultPath;
}

View File

@ -0,0 +1,73 @@
/* Copyright (C) 2009 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* 0 A.D. is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef INCLUDED_PS_GAMESETUP_PATHS
#define INCLUDED_PS_GAMESETUP_PATHS
#include "CmdLineArgs.h"
class Paths
{
public:
Paths(const CmdLineArgs& args);
const fs::path& Root() const
{
return m_root;
}
const fs::path& RData() const
{
return m_rdata;
}
const fs::path& Data() const
{
return m_data;
}
const fs::path& Config() const
{
return m_config;
}
const fs::path& Cache() const
{
return m_cache;
}
const fs::path& Logs() const
{
return m_logs;
}
private:
static fs::path Root(const CStr& argv0);
static fs::path XDG_Path(const char* envname, const fs::path& home, const fs::path& defaultPath);
// read-only directories, fixed paths relative to executable
fs::path m_root;
fs::path m_rdata;
// writable directories
fs::path m_data;
fs::path m_config;
fs::path m_cache;
fs::path m_logs; // special-cased in single-root-folder installations
};
#endif // #ifndef INCLUDED_PS_GAMESETUP_PATHS