Consolidate special path functions into m_specialpaths.cpp

- Also remove CDROM_DIR while I'm at it.
This commit is contained in:
Randy Heit 2013-09-14 21:02:47 -05:00
parent e4e26e7aa7
commit da02a44126
11 changed files with 541 additions and 299 deletions

View file

@ -701,6 +701,7 @@ add_executable( zdoom WIN32
m_misc.cpp
m_png.cpp
m_random.cpp
m_specialpaths.cpp
memarena.cpp
md5.cpp
name.cpp

View file

@ -505,31 +505,13 @@ bool FCajunMaster::LoadBots ()
bool gotteam = false;
bglobal.ForgetBots ();
#ifndef __unix__
tmp = progdir;
tmp += "zcajun/" BOTFILENAME;
if (!FileExists (tmp))
tmp = M_GetCajunPath(BOTFILENAME);
if (tmp.IsEmpty())
{
DPrintf ("No " BOTFILENAME ", so no bots\n");
return false;
}
#else
tmp = GetUserFile (BOTFILENAME);
if (!FileExists (tmp))
{
if (!FileExists (SHARE_DIR BOTFILENAME))
{
DPrintf ("No " BOTFILENAME ", so no bots\n");
return false;
}
else
sc.OpenFile (SHARE_DIR BOTFILENAME);
}
#endif
else
{
sc.OpenFile(tmp);
}
while (sc.GetString ())
{

View file

@ -2137,17 +2137,6 @@ static void CheckCmdLine()
Printf ("%s", GStrings("D_DEVSTR"));
}
#if !defined(__unix__) && !defined(__APPLE__)
// We do not need to support -cdrom under Unix, because all the files
// that would go to c:\\zdoomdat are already stored in .zdoom inside
// the user's home directory.
if (Args->CheckParm("-cdrom"))
{
Printf ("%s", GStrings("D_CDROM"));
mkdir (CDROM_DIR, 0);
}
#endif
// turbo option // [RH] (now a cvar)
v = Args->CheckValue("-turbo");
if (v != NULL)

View file

@ -1918,33 +1918,11 @@ FString G_BuildSaveName (const char *prefix, int slot)
leader = Args->CheckValue ("-savedir");
if (leader.IsEmpty())
{
#if !defined(__unix__) && !defined(__APPLE__)
if (Args->CheckParm ("-cdrom"))
{
leader = CDROM_DIR "/";
}
else
#endif
{
leader = save_dir;
}
if (leader.IsEmpty())
{
#ifdef __unix__
leader = "~/" GAME_DIR;
#elif defined(__APPLE__)
char cpath[PATH_MAX];
FSRef folder;
if (noErr == FSFindFolder(kUserDomain, kDocumentsFolderType, kCreateFolder, &folder) &&
noErr == FSRefMakePath(&folder, (UInt8*)cpath, PATH_MAX))
{
leader << cpath << "/" GAME_DIR "/Savegames/";
}
#else
leader = progdir;
#endif
leader = M_GetSavegamesPath();
}
}
size_t len = leader.Len();

View file

@ -42,8 +42,6 @@
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <lmcons.h>
#include <shlobj.h>
extern HWND Window;
#define USE_WINDOWS_DWORD
#endif
@ -588,106 +586,20 @@ void FGameConfigFile::ArchiveGlobalData ()
FString FGameConfigFile::GetConfigPath (bool tryProg)
{
const char *pathval;
FString path;
pathval = Args->CheckValue ("-config");
if (pathval != NULL)
{
return FString(pathval);
}
#ifdef _WIN32
path = NULL;
HRESULT hr;
TCHAR uname[UNLEN+1];
DWORD unamelen = countof(uname);
// Because people complained, try for a user-specific .ini in the program directory first.
// If that is not writeable, use the one in the home directory instead.
hr = GetUserName (uname, &unamelen);
if (SUCCEEDED(hr) && uname[0] != 0)
{
// Is it valid for a user name to have slashes?
// Check for them and substitute just in case.
char *probe = uname;
while (*probe != 0)
{
if (*probe == '\\' || *probe == '/')
*probe = '_';
++probe;
}
path = progdir;
path += "zdoom-";
path += uname;
path += ".ini";
if (tryProg)
{
if (!FileExists (path.GetChars()))
{
path = "";
}
}
else
{ // check if writeable
FILE *checker = fopen (path.GetChars(), "a");
if (checker == NULL)
{
path = "";
}
else
{
fclose (checker);
}
}
}
if (path.IsEmpty())
{
if (Args->CheckParm ("-cdrom"))
return CDROM_DIR "\\zdoom.ini";
path = progdir;
path += "zdoom.ini";
}
return path;
#elif defined(__APPLE__)
char cpath[PATH_MAX];
FSRef folder;
if (noErr == FSFindFolder(kUserDomain, kPreferencesFolderType, kCreateFolder, &folder) &&
noErr == FSRefMakePath(&folder, (UInt8*)cpath, PATH_MAX))
{
path = cpath;
path += "/zdoom.ini";
return path;
}
// Ungh.
return "zdoom.ini";
#else
return GetUserFile ("zdoom.ini");
#endif
return M_GetConfigPath(tryProg);
}
void FGameConfigFile::CreateStandardAutoExec(const char *section, bool start)
{
if (!SetSection(section))
{
FString path;
#ifdef __APPLE__
char cpath[PATH_MAX];
FSRef folder;
if (noErr == FSFindFolder(kUserDomain, kDocumentsFolderType, kCreateFolder, &folder) &&
noErr == FSRefMakePath(&folder, (UInt8*)cpath, PATH_MAX))
{
path << cpath << "/" GAME_DIR "/autoexec.cfg";
}
#elif !defined(__unix__)
path = "$PROGDIR/autoexec.cfg";
#else
path = GetUserFile ("autoexec.cfg");
#endif
FString path = M_GetAutoexecPath();
SetSection (section, true);
SetValueForKey ("Path", path.GetChars());
}
@ -794,6 +706,6 @@ void FGameConfigFile::SetRavenDefaults (bool isHexen)
CCMD (whereisini)
{
FString path = GameConfig->GetConfigPath (false);
FString path = M_GetConfigPath(false);
Printf ("%s\n", path.GetChars());
}

View file

@ -31,9 +31,6 @@
#include <errno.h>
#include <stdlib.h>
#include <time.h>
#ifdef __APPLE__
#include <CoreServices/CoreServices.h>
#endif
#include "doomtype.h"
#include "version.h"
@ -333,65 +330,6 @@ static long ParseCommandLine (const char *args, int *argc, char **argv)
}
#if defined(__unix__)
FString GetUserFile (const char *file)
{
FString path;
struct stat info;
path = NicePath("~/" GAME_DIR "/");
if (stat (path, &info) == -1)
{
struct stat extrainfo;
// Sanity check for ~/.config
FString configPath = NicePath("~/.config/");
if (stat (configPath, &extrainfo) == -1)
{
if (mkdir (configPath, S_IRUSR | S_IWUSR | S_IXUSR) == -1)
{
I_FatalError ("Failed to create ~/.config directory:\n%s", strerror(errno));
}
}
else if (!S_ISDIR(extrainfo.st_mode))
{
I_FatalError ("~/.config must be a directory");
}
// This can be removed after a release or two
// Transfer the old zdoom directory to the new location
bool moved = false;
FString oldpath = NicePath("~/.zdoom/");
if (stat (oldpath, &extrainfo) != -1)
{
if (rename(oldpath, path) == -1)
{
I_Error ("Failed to move old zdoom directory (%s) to new location (%s).",
oldpath.GetChars(), path.GetChars());
}
else
moved = true;
}
if (!moved && mkdir (path, S_IRUSR | S_IWUSR | S_IXUSR) == -1)
{
I_FatalError ("Failed to create %s directory:\n%s",
path.GetChars(), strerror (errno));
}
}
else
{
if (!S_ISDIR(info.st_mode))
{
I_FatalError ("%s must be a directory", path.GetChars());
}
}
path += file;
return path;
}
#endif
//
// M_SaveDefaults
//
@ -697,14 +635,6 @@ void M_ScreenShot (const char *filename)
// find a file name to save it to
if (filename == NULL || filename[0] == '\0')
{
#if !defined(__unix__) && !defined(__APPLE__)
if (Args->CheckParm ("-cdrom"))
{
autoname = CDROM_DIR "\\";
}
else
#endif
{
size_t dirlen;
autoname = Args->CheckValue("-shotdir");
@ -715,24 +645,7 @@ void M_ScreenShot (const char *filename)
dirlen = autoname.Len();
if (dirlen == 0)
{
#ifdef __unix__
autoname = "~/" GAME_DIR "/screenshots/";
#elif defined(__APPLE__)
char cpath[PATH_MAX];
FSRef folder;
if (noErr == FSFindFolder(kUserDomain, kDocumentsFolderType, kCreateFolder, &folder) &&
noErr == FSRefMakePath(&folder, (UInt8*)cpath, PATH_MAX))
{
autoname << cpath << "/" GAME_DIR "/Screenshots/";
}
else
{
autoname = "~";
}
#else
autoname = progdir;
#endif
autoname = M_GetScreenshotsPath();
}
else if (dirlen > 0)
{
@ -741,7 +654,6 @@ void M_ScreenShot (const char *filename)
autoname += '/';
}
}
}
autoname = NicePath(autoname);
CreatePath(autoname);
if (!FindFreeName (autoname, writepcx ? "pcx" : "png"))

View file

@ -24,6 +24,7 @@
#define __M_MISC__
#include "basictypes.h"
#include "zstring.h"
class FConfigFile;
class FGameConfigFile;
@ -44,9 +45,19 @@ bool M_SaveDefaults (const char *filename);
void M_SaveCustomKeys (FConfigFile *config, char *section, char *subsection, size_t sublen);
// Prepends ~/.zdoom to path
FString GetUserFile (const char *path);
FString M_ZLibError(int zerrnum);
// Get special directory paths (defined in m_specialpaths.cpp)
#ifdef __unix__
FString GetUserFile (const char *path); // Prepends ~/.zdoom to path
#endif
FString M_GetCachePath();
FString M_GetAutoexecPath();
FString M_GetCajunPath(const char *filename);
FString M_GetConfigPath(bool for_reading);
FString M_GetScreenshotsPath();
FString M_GetSavegamesPath();
#endif

500
src/m_specialpaths.cpp Normal file
View file

@ -0,0 +1,500 @@
#ifdef __APPLE__
#include <CoreServices/CoreServices.h>
#endif
#ifdef _WIN32
#include <windows.h>
#include <lmcons.h>
#include <shlobj.h>
#define USE_WINDOWS_DWORD
#endif
#include "cmdlib.h"
#include "m_misc.h"
#if defined(_WIN32)
//===========================================================================
//
// M_GetCachePath Windows
//
// Returns the path for cache GL nodes.
//
//===========================================================================
FString M_GetCachePath()
{
FString path;
char pathstr[MAX_PATH];
if (0 != SHGetFolderPathA(NULL, CSIDL_LOCAL_APPDATA, NULL, 0, pathstr))
{ // Failed (e.g. On Win9x): use program directory
path = progdir;
}
else
{
path = pathstr;
}
// Don't use GAME_DIR and such so that ZDoom and its child ports can
// share the node cache.
path += "/zdoom/cache";
return path;
}
//===========================================================================
//
// M_GetAutoexecPath Windows
//
// Returns the expected location of autoexec.cfg.
//
//===========================================================================
FString M_GetAutoexecPath()
{
return "$PROGDIR/autoexec.cfg";
}
//===========================================================================
//
// M_GetCajunPath Windows
//
// Returns the location of the Cajun Bot definitions.
//
//===========================================================================
FString M_GetCajunPath(const char *botfilename)
{
FString path;
path << progdir << "zcajun/" << botfilename;
if (!FileExists(path))
{
path = "";
}
return path;
}
//===========================================================================
//
// M_GetConfigPath Windows
//
// Returns the path to the config file. On Windows, this can vary for reading
// vs writing. i.e. If $PROGDIR/zdoom-<user>.ini does not exist, it will try
// to read from $PROGDIR/zdoom.ini, but it will never write to zdoom.ini.
//
//===========================================================================
FString M_GetConfigPath(bool for_reading)
{
FString path;
HRESULT hr;
TCHAR uname[UNLEN+1];
DWORD unamelen = countof(uname);
// Because people complained, try for a user-specific .ini in the program directory first.
// If that is not writeable, use the one in the home directory instead.
hr = GetUserName(uname, &unamelen);
if (SUCCEEDED(hr) && uname[0] != 0)
{
// Is it valid for a user name to have slashes?
// Check for them and substitute just in case.
char *probe = uname;
while (*probe != 0)
{
if (*probe == '\\' || *probe == '/')
*probe = '_';
++probe;
}
path = progdir;
path += "zdoom-";
path += uname;
path += ".ini";
if (for_reading)
{
if (!FileExists(path.GetChars()))
{
path = "";
}
}
else
{ // check if writeable
FILE *checker = fopen (path.GetChars(), "a");
if (checker == NULL)
{
path = "";
}
else
{
fclose (checker);
}
}
}
return path;
}
//===========================================================================
//
// M_GetScreenshotsPath Windows
//
// Returns the path to the default screenshots directory.
//
//===========================================================================
FString M_GetScreenshotsPath()
{
FString path;
path = progdir;
return path;
}
//===========================================================================
//
// M_GetSavegamesPath Windows
//
// Returns the path to the default save games directory.
//
//===========================================================================
FString M_GetSavegamesPath()
{
FString path;
path = progdir;
return path;
}
#elif defined(__APPLE__)
//===========================================================================
//
// M_GetCachePath Mac OS X
//
// Returns the path for cache GL nodes.
//
//===========================================================================
FString M_GetCachePath()
{
FString path;
char pathstr[PATH_MAX];
FSRef folder;
if (noErr == FSFindFolder(kLocalDomain, kApplicationSupportFolderType, kCreateFolder, &folder) &&
noErr == FSRefMakePath(&folder, (UInt8*)pathstr, PATH_MAX))
{
path = pathstr;
}
else
{
path = progdir;
}
path += "/zdoom/cache";
return path;
}
//===========================================================================
//
// M_GetAutoexecPath Mac OS X
//
// Returns the expected location of autoexec.cfg.
//
//===========================================================================
FString M_GetAutoexecPath()
{
FString path;
char cpath[PATH_MAX];
FSRef folder;
if (noErr == FSFindFolder(kUserDomain, kDocumentsFolderType, kCreateFolder, &folder) &&
noErr == FSRefMakePath(&folder, (UInt8*)cpath, PATH_MAX))
{
path << cpath << "/" GAME_DIR "/autoexec.cfg";
}
return path;
}
//===========================================================================
//
// M_GetCajunPath Mac OS X
//
// Returns the location of the Cajun Bot definitions.
//
//===========================================================================
FString M_GetCajunPath(const char *botfilename)
{
FString path;
// Just copies the Windows code. Should this be more Mac-specific?
path << progdir << "zcajun/" << botfilename;
if (!FileExists(path))
{
path = "";
}
return path;
}
//===========================================================================
//
// M_GetConfigPath Mac OS X
//
// Returns the path to the config file. On Windows, this can vary for reading
// vs writing. i.e. If $PROGDIR/zdoom-<user>.ini does not exist, it will try
// to read from $PROGDIR/zdoom.ini, but it will never write to zdoom.ini.
//
//===========================================================================
FString M_GetConfigPath(bool for_reading)
{
char cpath[PATH_MAX];
FSRef folder;
if (noErr == FSFindFolder(kUserDomain, kPreferencesFolderType, kCreateFolder, &folder) &&
noErr == FSRefMakePath(&folder, (UInt8*)cpath, PATH_MAX))
{
FString path;
path << cpath << "/zdoom.ini";
return path;
}
// Ungh.
return "zdoom.ini";
}
//===========================================================================
//
// M_GetScreenshotsPath Mac OS X
//
// Returns the path to the default screenshots directory.
//
//===========================================================================
FString M_GetScreenshotsPath()
{
FString path;
char cpath[PATH_MAX];
FSRef folder;
if (noErr == FSFindFolder(kUserDomain, kDocumentsFolderType, kCreateFolder, &folder) &&
noErr == FSRefMakePath(&folder, (UInt8*)cpath, PATH_MAX))
{
path << cpath << "/" GAME_DIR "/Screenshots/";
}
else
{
path = "~/";
}
return path;
}
//===========================================================================
//
// M_GetSavegamesPath Mac OS X
//
// Returns the path to the default save games directory.
//
//===========================================================================
FString M_GetSavegamesPath()
{
FString path;
char cpath[PATH_MAX];
FSRef folder;
if (noErr == FSFindFolder(kUserDomain, kDocumentsFolderType, kCreateFolder, &folder) &&
noErr == FSRefMakePath(&folder, (UInt8*)cpath, PATH_MAX))
{
path << cpath << "/" GAME_DIR "/Savegames/";
}
return path;
}
#else // Linux, et al.
FString GetUserFile(const char *file)
{
FString path;
struct stat info;
path = NicePath("~/" GAME_DIR "/");
if (stat(path, &info) == -1)
{
struct stat extrainfo;
// Sanity check for ~/.config
FString configPath = NicePath("~/.config/");
if (stat(configPath, &extrainfo) == -1)
{
if (mkdir(configPath, S_IRUSR | S_IWUSR | S_IXUSR) == -1)
{
I_FatalError("Failed to create ~/.config directory:\n%s", strerror(errno));
}
}
else if (!S_ISDIR(extrainfo.st_mode))
{
I_FatalError("~/.config must be a directory");
}
// This can be removed after a release or two
// Transfer the old zdoom directory to the new location
bool moved = false;
FString oldpath = NicePath("~/.zdoom/");
if (stat #if defined(__unix__)
FString GetUserFile (const char *file)
{
FString path;
struct stat info;
path = NicePath("~/" GAME_DIR "/");
if (stat (path, &info) == -1)
{
struct stat extrainfo;
// Sanity check for ~/.config
FString configPath = NicePath("~/.config/");
if (stat (configPath, &extrainfo) == -1)
{
if (mkdir (configPath, S_IRUSR | S_IWUSR | S_IXUSR) == -1)
{
I_FatalError ("Failed to create ~/.config directory:\n%s", strerror(errno));
}
}
else if (!S_ISDIR(extrainfo.st_mode))
{
I_FatalError ("~/.config must be a directory");
}
// This can be removed after a release or two
// Transfer the old zdoom directory to the new location
bool moved = false;
FString oldpath = NicePath("~/.zdoom/");
if (stat (oldpath, &extrainfo) != -1)
{
if (rename(oldpath, path) == -1)
{
I_Error ("Failed to move old zdoom directory (%s) to new location (%s).",
oldpath.GetChars(), path.GetChars());
}
else
moved = true;
}
if (!moved && mkdir (path, S_IRUSR | S_IWUSR | S_IXUSR) == -1)
{
I_FatalError ("Failed to create %s directory:\n%s",
path.GetChars(), strerror (errno));
}
}
else
{
if (!S_ISDIR(info.st_mode))
{
I_FatalError ("%s must be a directory", path.GetChars());
}
}
path += file;
return path;
}
//===========================================================================
//
// M_GetCachePath Unix
//
// Returns the path for cache GL nodes.
//
//===========================================================================
FString M_GetCachePath()
{
// Don't use GAME_DIR and such so that ZDoom and its child ports can
// share the node cache.
return NicePath("~/.config/zdoom/cache");
}
//===========================================================================
//
// M_GetAutoexecPath Unix
//
// Returns the expected location of autoexec.cfg.
//
//===========================================================================
FString M_GetAutoexecPath()
{
return GetUserFile("autoexec.cfg");
}
//===========================================================================
//
// M_GetCajunPath Unix
//
// Returns the location of the Cajun Bot definitions.
//
//===========================================================================
FString M_GetCajunPath(const char *botfilename)
{
FString path;
// Check first in ~/.config/zdoom./botfilename.
path = GetUserFile(BOTFILENAME);
if (!FileExists(tmp))
{
// Then check in SHARE_DIR/botfilename. (Some allowance for Macs
// should probably be made here.)
path = SHARE_DIR BOTFILENAME;
if (!FileExists(path))
{
path ="";
}
}
return path;
}
//===========================================================================
//
// M_GetConfigPath Unix
//
// Returns the path to the config file. On Windows, this can vary for reading
// vs writing. i.e. If $PROGDIR/zdoom-<user>.ini does not exist, it will try
// to read from $PROGDIR/zdoom.ini, but it will never write to zdoom.ini.
//
//===========================================================================
FString M_GetConfigPath(bool for_reading)
{
return GetUserFile("zdoom.ini");
}
//===========================================================================
//
// M_GetScreenshotsPath Unix
//
// Returns the path to the default screenshots directory.
//
//===========================================================================
FString M_GetScreenshotsPath()
{
return "~/" GAME_DIR "/screenshots/";
}
//===========================================================================
//
// M_GetSavegamesPath Unix
//
// Returns the path to the default save games directory.
//
//===========================================================================
FString M_GetSavegamesPath()
{
return = "~/" GAME_DIR;
}
#endif

View file

@ -43,17 +43,6 @@
#define rmdir _rmdir
// TODO, maybe: stop using DWORD so I don't need to worry about conflicting
// with Windows' typedef. Then I could just include the header file instead
// of declaring everything here.
#define MAX_PATH 260
#define CSIDL_LOCAL_APPDATA 0x001c
extern "C" __declspec(dllimport) long __stdcall SHGetFolderPathA(void *hwnd, int csidl, void *hToken, unsigned long dwFlags, char *pszPath);
#endif
#ifdef __APPLE__
#include <CoreServices/CoreServices.h>
#endif
#include "templates.h"
@ -75,6 +64,7 @@ extern "C" __declspec(dllimport) long __stdcall SHGetFolderPathA(void *hwnd, int
#include "x86.h"
#include "version.h"
#include "md5.h"
#include "m_misc.h"
void P_GetPolySpots (MapData * lump, TArray<FNodeBuilder::FPolyStart> &spots, TArray<FNodeBuilder::FPolyStart> &anchors);
@ -1048,45 +1038,10 @@ bool P_CheckNodes(MapData * map, bool rebuilt, int buildtime)
typedef TArray<BYTE> MemFile;
static FString GetCachePath()
{
FString path;
#ifdef _WIN32
char pathstr[MAX_PATH];
if (0 != SHGetFolderPathA(NULL, CSIDL_LOCAL_APPDATA, NULL, 0, pathstr))
{ // Failed (e.g. On Win9x): use program directory
path = progdir;
}
else
{
path = pathstr;
}
path += "/zdoom/cache";
#elif defined(__APPLE__)
char pathstr[PATH_MAX];
FSRef folder;
if (noErr == FSFindFolder(kLocalDomain, kApplicationSupportFolderType, kCreateFolder, &folder) &&
noErr == FSRefMakePath(&folder, (UInt8*)pathstr, PATH_MAX))
{
path = pathstr;
}
else
{
path = progdir;
}
path += "/zdoom/cache";
#else
// Don't use GAME_DIR and such so that ZDoom and its child ports can share the node cache.
path = NicePath("~/.config/zdoom/cache");
#endif
return path;
}
static FString CreateCacheName(MapData *map, bool create)
{
FString path = GetCachePath();
FString path = M_GetCachePath();
FString lumpname = Wads.GetLumpFullPath(map->lumpnum);
int separator = lumpname.IndexOf(':');
path << '/' << lumpname.Left(separator);
@ -1299,7 +1254,7 @@ errorout:
CCMD(clearnodecache)
{
TArray<FFileList> list;
FString path = GetCachePath();
FString path = M_GetCachePath();
path += "/";
try

View file

@ -95,8 +95,6 @@ const char *GetVersionString();
#define GAME_DIR ".config/zdoom"
#elif defined(__APPLE__)
#define GAME_DIR GAMENAME
#else
#define CDROM_DIR "C:\\ZDOOMDAT"
#endif

View file

@ -710,6 +710,10 @@
RelativePath=".\src\m_random.cpp"
>
</File>
<File
RelativePath=".\src\m_specialpaths.cpp"
>
</File>
<File
RelativePath=".\src\md5.cpp"
>