2016-09-03 10:00:08 +00:00
|
|
|
/*
|
|
|
|
** i_specialpaths.cpp
|
|
|
|
** Gets special system folders where data should be stored. (Windows version)
|
|
|
|
**
|
|
|
|
**---------------------------------------------------------------------------
|
|
|
|
** Copyright 2013-2016 Randy Heit
|
|
|
|
** Copyright 2016 Christoph Oelckers
|
|
|
|
** All rights reserved.
|
|
|
|
**
|
|
|
|
** Redistribution and use in source and binary forms, with or without
|
|
|
|
** modification, are permitted provided that the following conditions
|
|
|
|
** are met:
|
|
|
|
**
|
|
|
|
** 1. Redistributions of source code must retain the above copyright
|
|
|
|
** notice, this list of conditions and the following disclaimer.
|
|
|
|
** 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
** notice, this list of conditions and the following disclaimer in the
|
|
|
|
** documentation and/or other materials provided with the distribution.
|
|
|
|
** 3. The name of the author may not be used to endorse or promote products
|
|
|
|
** derived from this software without specific prior written permission.
|
|
|
|
**
|
|
|
|
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
**---------------------------------------------------------------------------
|
|
|
|
**
|
|
|
|
*/
|
2013-09-15 02:02:47 +00:00
|
|
|
|
|
|
|
#include <windows.h>
|
|
|
|
#include <lmcons.h>
|
|
|
|
#include <shlobj.h>
|
|
|
|
#define USE_WINDOWS_DWORD
|
|
|
|
|
|
|
|
#include "cmdlib.h"
|
|
|
|
#include "m_misc.h"
|
2013-09-15 04:07:59 +00:00
|
|
|
#include "version.h" // for GAMENAME
|
2015-04-25 02:26:57 +00:00
|
|
|
#include "i_system.h"
|
|
|
|
|
2016-11-12 23:32:09 +00:00
|
|
|
#include "optwin32.h"
|
2013-09-15 04:07:59 +00:00
|
|
|
|
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// IsProgramDirectoryWritable
|
|
|
|
//
|
|
|
|
// If the program directory is writable, then dump everything in there for
|
|
|
|
// historical reasons. Otherwise, known folders get used instead.
|
|
|
|
//
|
|
|
|
//===========================================================================
|
|
|
|
|
|
|
|
bool UseKnownFolders()
|
|
|
|
{
|
|
|
|
// Cache this value so the semantics don't change during a single run
|
|
|
|
// of the program. (e.g. Somebody could add write access while the
|
|
|
|
// program is running.)
|
|
|
|
static INTBOOL iswritable = -1;
|
|
|
|
FString testpath;
|
|
|
|
HANDLE file;
|
|
|
|
|
|
|
|
if (iswritable >= 0)
|
|
|
|
{
|
|
|
|
return !iswritable;
|
|
|
|
}
|
|
|
|
testpath << progdir << "writest";
|
|
|
|
file = CreateFile(testpath, GENERIC_READ | GENERIC_WRITE, 0, NULL,
|
|
|
|
CREATE_ALWAYS,
|
|
|
|
FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE, NULL);
|
|
|
|
if (file != INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
CloseHandle(file);
|
2016-02-09 13:20:49 +00:00
|
|
|
if (!batchrun) Printf("Using program directory for storage\n");
|
2013-09-15 04:07:59 +00:00
|
|
|
iswritable = true;
|
|
|
|
return false;
|
|
|
|
}
|
2016-02-09 13:20:49 +00:00
|
|
|
if (!batchrun) Printf("Using known folders for storage\n");
|
2013-09-15 04:07:59 +00:00
|
|
|
iswritable = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// GetKnownFolder
|
|
|
|
//
|
|
|
|
// Returns the known_folder if SHGetKnownFolderPath is available, otherwise
|
|
|
|
// returns the shell_folder using SHGetFolderPath.
|
|
|
|
//
|
|
|
|
//===========================================================================
|
|
|
|
|
|
|
|
bool GetKnownFolder(int shell_folder, REFKNOWNFOLDERID known_folder, bool create, FString &path)
|
|
|
|
{
|
2016-11-12 23:32:09 +00:00
|
|
|
using OptWin32::SHGetFolderPathA;
|
|
|
|
using OptWin32::SHGetKnownFolderPath;
|
2013-09-15 04:07:59 +00:00
|
|
|
|
|
|
|
char pathstr[MAX_PATH];
|
|
|
|
|
|
|
|
// SHGetKnownFolderPath knows about more folders than SHGetFolderPath, but is
|
|
|
|
// new to Vista, hence the reason we support both.
|
2016-11-12 23:32:09 +00:00
|
|
|
if (!SHGetKnownFolderPath)
|
2013-09-15 04:07:59 +00:00
|
|
|
{
|
2015-04-25 02:26:57 +00:00
|
|
|
// NT4 doesn't even have this function.
|
2016-11-12 23:32:09 +00:00
|
|
|
if (!SHGetFolderPathA)
|
2015-04-25 02:26:57 +00:00
|
|
|
return false;
|
|
|
|
|
2013-09-15 04:07:59 +00:00
|
|
|
if (shell_folder < 0)
|
|
|
|
{ // Not supported by SHGetFolderPath
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (create)
|
|
|
|
{
|
|
|
|
shell_folder |= CSIDL_FLAG_CREATE;
|
|
|
|
}
|
2016-11-12 23:32:09 +00:00
|
|
|
if (FAILED(SHGetFolderPathA(NULL, shell_folder, NULL, 0, pathstr)))
|
2013-09-15 04:07:59 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
path = pathstr;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
PWSTR wpath;
|
2016-11-12 23:32:09 +00:00
|
|
|
if (FAILED(SHGetKnownFolderPath(known_folder, create ? KF_FLAG_CREATE : 0, NULL, &wpath)))
|
2013-09-15 04:07:59 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// FIXME: Support Unicode, at least for filenames. This function
|
|
|
|
// has no MBCS equivalent, so we have to convert it since we don't
|
|
|
|
// support Unicode. :(
|
|
|
|
bool converted = false;
|
|
|
|
if (WideCharToMultiByte(GetACP(), WC_NO_BEST_FIT_CHARS, wpath, -1,
|
|
|
|
pathstr, countof(pathstr), NULL, NULL) > 0)
|
|
|
|
{
|
|
|
|
path = pathstr;
|
|
|
|
converted = true;
|
|
|
|
}
|
|
|
|
CoTaskMemFree(wpath);
|
|
|
|
return converted;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-15 02:02:47 +00:00
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// M_GetCachePath Windows
|
|
|
|
//
|
|
|
|
// Returns the path for cache GL nodes.
|
|
|
|
//
|
|
|
|
//===========================================================================
|
|
|
|
|
2013-09-15 04:07:59 +00:00
|
|
|
FString M_GetCachePath(bool create)
|
2013-09-15 02:02:47 +00:00
|
|
|
{
|
|
|
|
FString path;
|
|
|
|
|
2013-09-15 04:07:59 +00:00
|
|
|
if (!GetKnownFolder(CSIDL_LOCAL_APPDATA, FOLDERID_LocalAppData, create, path))
|
2013-09-15 02:02:47 +00:00
|
|
|
{ // Failed (e.g. On Win9x): use program directory
|
|
|
|
path = progdir;
|
|
|
|
}
|
|
|
|
// Don't use GAME_DIR and such so that ZDoom and its child ports can
|
|
|
|
// share the node cache.
|
|
|
|
path += "/zdoom/cache";
|
2015-05-06 22:45:36 +00:00
|
|
|
path.Substitute("//", "/"); // needed because progdir ends with a slash.
|
2013-09-15 02:02:47 +00:00
|
|
|
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;
|
|
|
|
|
2016-04-27 11:04:37 +00:00
|
|
|
path.Format("%s" GAMENAME "_portable.ini", progdir.GetChars());
|
|
|
|
if (FileExists(path))
|
|
|
|
{
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
path = "";
|
|
|
|
|
2013-09-15 04:07:59 +00:00
|
|
|
// Construct a user-specific config name
|
|
|
|
if (UseKnownFolders() && GetKnownFolder(CSIDL_APPDATA, FOLDERID_RoamingAppData, true, path))
|
2013-09-15 02:02:47 +00:00
|
|
|
{
|
2013-09-18 22:29:42 +00:00
|
|
|
path += "/" GAME_DIR;
|
2013-09-15 04:07:59 +00:00
|
|
|
CreatePath(path);
|
2015-04-02 11:27:52 +00:00
|
|
|
path += "/" GAMENAMELOWERCASE ".ini";
|
2013-09-15 04:07:59 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{ // construct "$PROGDIR/zdoom-$USER.ini"
|
|
|
|
TCHAR uname[UNLEN+1];
|
|
|
|
DWORD unamelen = countof(uname);
|
2013-09-15 02:02:47 +00:00
|
|
|
|
|
|
|
path = progdir;
|
2013-09-15 04:07:59 +00:00
|
|
|
hr = GetUserName(uname, &unamelen);
|
|
|
|
if (SUCCEEDED(hr) && uname[0] != 0)
|
2013-09-15 02:02:47 +00:00
|
|
|
{
|
2013-09-15 04:07:59 +00:00
|
|
|
// Is it valid for a user name to have slashes?
|
|
|
|
// Check for them and substitute just in case.
|
|
|
|
char *probe = uname;
|
|
|
|
while (*probe != 0)
|
2013-09-15 02:02:47 +00:00
|
|
|
{
|
2013-09-15 04:07:59 +00:00
|
|
|
if (*probe == '\\' || *probe == '/')
|
|
|
|
*probe = '_';
|
|
|
|
++probe;
|
2013-09-15 02:02:47 +00:00
|
|
|
}
|
2015-04-02 11:27:52 +00:00
|
|
|
path << GAMENAMELOWERCASE "-" << uname << ".ini";
|
2013-09-15 02:02:47 +00:00
|
|
|
}
|
|
|
|
else
|
2013-09-15 04:07:59 +00:00
|
|
|
{ // Couldn't get user name, so just use zdoom.ini
|
2015-04-02 11:27:52 +00:00
|
|
|
path += GAMENAMELOWERCASE ".ini";
|
2013-09-15 02:02:47 +00:00
|
|
|
}
|
|
|
|
}
|
2013-09-15 04:07:59 +00:00
|
|
|
|
|
|
|
// If we are reading the config file, check if it exists. If not, fallback
|
|
|
|
// to $PROGDIR/zdoom.ini
|
|
|
|
if (for_reading)
|
|
|
|
{
|
|
|
|
if (!FileExists(path))
|
|
|
|
{
|
|
|
|
path = progdir;
|
2015-04-02 11:27:52 +00:00
|
|
|
path << GAMENAMELOWERCASE ".ini";
|
2013-09-15 04:07:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-15 02:02:47 +00:00
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// M_GetScreenshotsPath Windows
|
|
|
|
//
|
|
|
|
// Returns the path to the default screenshots directory.
|
|
|
|
//
|
|
|
|
//===========================================================================
|
|
|
|
|
2013-09-15 04:07:59 +00:00
|
|
|
// I'm not sure when FOLDERID_Screenshots was added, but it was probably
|
|
|
|
// for Windows 8, since it's not in the v7.0 Windows SDK.
|
|
|
|
static const GUID MyFOLDERID_Screenshots = { 0xb7bede81, 0xdf94, 0x4682, 0xa7, 0xd8, 0x57, 0xa5, 0x26, 0x20, 0xb8, 0x6f };
|
|
|
|
|
2013-09-15 02:02:47 +00:00
|
|
|
FString M_GetScreenshotsPath()
|
|
|
|
{
|
|
|
|
FString path;
|
2013-09-15 04:07:59 +00:00
|
|
|
|
|
|
|
if (!UseKnownFolders())
|
|
|
|
{
|
|
|
|
return progdir;
|
|
|
|
}
|
|
|
|
else if (GetKnownFolder(-1, MyFOLDERID_Screenshots, true, path))
|
|
|
|
{
|
|
|
|
path << "/" GAMENAME;
|
|
|
|
}
|
|
|
|
else if (GetKnownFolder(CSIDL_MYPICTURES, FOLDERID_Pictures, true, path))
|
|
|
|
{
|
|
|
|
path << "/Screenshots/" GAMENAME;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return progdir;
|
|
|
|
}
|
|
|
|
CreatePath(path);
|
2013-09-15 02:02:47 +00:00
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// M_GetSavegamesPath Windows
|
|
|
|
//
|
|
|
|
// Returns the path to the default save games directory.
|
|
|
|
//
|
|
|
|
//===========================================================================
|
|
|
|
|
|
|
|
FString M_GetSavegamesPath()
|
|
|
|
{
|
|
|
|
FString path;
|
2013-09-15 04:07:59 +00:00
|
|
|
|
|
|
|
if (!UseKnownFolders())
|
|
|
|
{
|
|
|
|
return progdir;
|
|
|
|
}
|
|
|
|
// Try standard Saved Games folder
|
|
|
|
else if (GetKnownFolder(-1, FOLDERID_SavedGames, true, path))
|
|
|
|
{
|
|
|
|
path << "/" GAMENAME;
|
|
|
|
}
|
|
|
|
// Try defacto My Documents/My Games folder
|
|
|
|
else if (GetKnownFolder(CSIDL_PERSONAL, FOLDERID_Documents, true, path))
|
|
|
|
{
|
|
|
|
// I assume since this isn't a standard folder, it doesn't have
|
|
|
|
// a localized name either.
|
|
|
|
path << "/My Games/" GAMENAME;
|
|
|
|
CreatePath(path);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
path = progdir;
|
|
|
|
}
|
2013-09-15 02:02:47 +00:00
|
|
|
return path;
|
|
|
|
}
|