61 lines
1.7 KiB
C++
61 lines
1.7 KiB
C++
#include "stdafx.h"
|
|
#include "TetrisAssets.h"
|
|
|
|
/**
|
|
* @brief 根据程序所在目录拼出项目资源文件的绝对路径。
|
|
*/
|
|
std::wstring BuildAssetPath(const wchar_t* relativePath)
|
|
{
|
|
wchar_t modulePath[MAX_PATH] = {};
|
|
GetModuleFileNameW(nullptr, modulePath, MAX_PATH);
|
|
|
|
std::wstring basePath(modulePath);
|
|
size_t lastSlash = basePath.find_last_of(L"\\/");
|
|
if (lastSlash != std::wstring::npos)
|
|
{
|
|
basePath.resize(lastSlash);
|
|
}
|
|
|
|
std::wstring projectRelative = basePath + L"\\..\\..\\" + relativePath;
|
|
wchar_t fullPath[MAX_PATH] = {};
|
|
DWORD result = GetFullPathNameW(projectRelative.c_str(), MAX_PATH, fullPath, nullptr);
|
|
if (result > 0 && result < MAX_PATH)
|
|
{
|
|
return fullPath;
|
|
}
|
|
|
|
return projectRelative;
|
|
}
|
|
|
|
/**
|
|
* @brief 根据当前工作目录拼出项目资源文件的绝对路径。
|
|
*/
|
|
std::wstring BuildWorkingDirAssetPath(const wchar_t* relativePath)
|
|
{
|
|
wchar_t currentDirectory[MAX_PATH] = {};
|
|
DWORD length = GetCurrentDirectoryW(MAX_PATH, currentDirectory);
|
|
if (length == 0 || length >= MAX_PATH)
|
|
{
|
|
return L"";
|
|
}
|
|
|
|
std::wstring candidate = std::wstring(currentDirectory) + L"\\" + relativePath;
|
|
wchar_t fullPath[MAX_PATH] = {};
|
|
DWORD result = GetFullPathNameW(candidate.c_str(), MAX_PATH, fullPath, nullptr);
|
|
if (result > 0 && result < MAX_PATH)
|
|
{
|
|
return fullPath;
|
|
}
|
|
|
|
return candidate;
|
|
}
|
|
|
|
/**
|
|
* @brief 判断指定路径是否存在且不是目录。
|
|
*/
|
|
bool FileExists(const std::wstring& path)
|
|
{
|
|
DWORD attributes = GetFileAttributesW(path.c_str());
|
|
return attributes != INVALID_FILE_ATTRIBUTES && (attributes & FILE_ATTRIBUTE_DIRECTORY) == 0;
|
|
}
|