From 868238e42b46f5afad8e34058ee4301da7657e5f Mon Sep 17 00:00:00 2001 From: Nomango <569629550@qq.com> Date: Thu, 24 May 2018 14:33:16 +0800 Subject: [PATCH] =?UTF-8?q?Path::createFolder=E6=94=AF=E6=8C=81=E4=B8=80?= =?UTF-8?q?=E6=AC=A1=E6=80=A7=E5=88=9B=E5=BB=BA=E5=A4=9A=E7=BA=A7=E7=9B=AE?= =?UTF-8?q?=E5=BD=95=EF=BC=9BString=E4=B8=8D=E5=86=8D=E5=85=81=E8=AE=B8?= =?UTF-8?q?=E9=9A=90=E5=BC=8F=E8=BD=AC=E6=8D=A2=E6=88=90const=20wchar=5Ft*?= =?UTF-8?q?=20=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/Base/Game.cpp | 11 ++-- core/Base/Renderer.cpp | 2 +- core/Base/Window.cpp | 8 +-- core/Common/Image.cpp | 4 +- core/Common/String.cpp | 9 ++- core/Node/Text.cpp | 8 +-- core/Tool/Data.cpp | 34 +++------- core/Tool/Music.cpp | 4 +- core/Tool/Path.cpp | 138 ++++++++++++++++------------------------- core/e2dcommon.h | 9 ++- core/e2dtool.h | 19 +++--- 11 files changed, 104 insertions(+), 142 deletions(-) diff --git a/core/Base/Game.cpp b/core/Base/Game.cpp index 9faf9e28..ff79ba5c 100644 --- a/core/Base/Game.cpp +++ b/core/Base/Game.cpp @@ -25,7 +25,8 @@ bool e2d::Game::init(const String& name, const String& mutexName) if (!mutexName.isEmpty()) { // 创建进程互斥体 - HANDLE hMutex = ::CreateMutex(nullptr, TRUE, L"Easy2DApp-" + mutexName); + String fullMutexName = L"Easy2DApp-" + mutexName; + HANDLE hMutex = ::CreateMutex(nullptr, TRUE, (LPCWSTR)fullMutexName); if (hMutex == nullptr) { @@ -114,15 +115,15 @@ bool e2d::Game::init(const String& name, const String& mutexName) throw SystemException(L"初始化 XAudio2 失败"); } - // 保存游戏名称 - s_sGameName = name; - // 初始化路径 - if (!Path::__init()) + if (!Path::__init(name)) { WARN("Path::__init failed!"); } + // 保存游戏名称 + s_sGameName = name; + // 初始化成功 s_bInitialized = true; diff --git a/core/Base/Renderer.cpp b/core/Base/Renderer.cpp index 20ba0001..0821737d 100644 --- a/core/Base/Renderer.cpp +++ b/core/Base/Renderer.cpp @@ -194,7 +194,7 @@ void e2d::Renderer::__render() IDWriteTextLayout * pTextLayout = nullptr; hr = s_pDWriteFactory->CreateTextLayout( - s_sFpsText, + (const WCHAR *)s_sFpsText, (UINT32)s_sFpsText.getLength(), s_pTextFormat, 0, diff --git a/core/Base/Window.cpp b/core/Base/Window.cpp index 252378ae..4b381770 100644 --- a/core/Base/Window.cpp +++ b/core/Base/Window.cpp @@ -177,7 +177,7 @@ void e2d::Window::setSize(int width, int height) void e2d::Window::setTitle(const String& title) { // 设置窗口标题 - ::SetWindowText(s_HWnd, title); + ::SetWindowText(s_HWnd, (LPCWSTR)title); } void e2d::Window::setIcon(int iconID) @@ -293,19 +293,19 @@ void e2d::Window::setTypewritingEnable(bool enable) void e2d::Window::info(const String & text, const String & title) { - ::MessageBox(s_HWnd, text, title, MB_ICONINFORMATION | MB_OK); + ::MessageBox(s_HWnd, (LPCWSTR)text, (LPCWSTR)title, MB_ICONINFORMATION | MB_OK); Game::reset(); } void e2d::Window::warning(const String& title, const String& text) { - ::MessageBox(s_HWnd, text, title, MB_ICONWARNING | MB_OK); + ::MessageBox(s_HWnd, (LPCWSTR)text, (LPCWSTR)title, MB_ICONWARNING | MB_OK); Game::reset(); } void e2d::Window::error(const String & text, const String & title) { - ::MessageBox(s_HWnd, text, title, MB_ICONERROR | MB_OK); + ::MessageBox(s_HWnd, (LPCWSTR)text, (LPCWSTR)title, MB_ICONERROR | MB_OK); Game::reset(); } diff --git a/core/Common/Image.cpp b/core/Common/Image.cpp index 0cea0147..bceb4654 100644 --- a/core/Common/Image.cpp +++ b/core/Common/Image.cpp @@ -170,7 +170,7 @@ bool e2d::Image::preload(const String& filePath) // 创建解码器 hr = Renderer::getIWICImagingFactory()->CreateDecoderFromFilename( - filePath, + (LPCWSTR)filePath, nullptr, GENERIC_READ, WICDecodeMetadataCacheOnLoad, @@ -254,7 +254,7 @@ bool e2d::Image::preload(int resNameId, const String& resType) DWORD imageFileSize = 0; // 定位资源 - imageResHandle = ::FindResourceW(HINST_THISCOMPONENT, MAKEINTRESOURCE(resNameId), resType); + imageResHandle = ::FindResourceW(HINST_THISCOMPONENT, MAKEINTRESOURCE(resNameId), (LPCWSTR)resType); hr = imageResHandle ? S_OK : E_FAIL; if (SUCCEEDED(hr)) diff --git a/core/Common/String.cpp b/core/Common/String.cpp index 53bd6d55..c36d2634 100644 --- a/core/Common/String.cpp +++ b/core/Common/String.cpp @@ -364,6 +364,11 @@ std::string e2d::String::getCString() const return std::move(str); } +wchar_t e2d::String::at(int index) const +{ + return _str[size_t(index)]; +} + int e2d::String::compare(const String & str) const { return _str.compare(str._str); @@ -449,9 +454,9 @@ void e2d::String::replace(const String & from, const String & to) return; size_t start_pos = 0; - while ((start_pos = _str.find(from, start_pos)) != std::string::npos) + while ((start_pos = _str.find((const wchar_t *)from, start_pos)) != std::string::npos) { - _str.replace(start_pos, from._str.length(), to); + _str.replace(start_pos, from._str.length(), (const wchar_t *)to); start_pos += to._str.length(); } } diff --git a/core/Node/Text.cpp b/core/Node/Text.cpp index 4945dead..82c00625 100644 --- a/core/Node/Text.cpp +++ b/core/Node/Text.cpp @@ -321,7 +321,7 @@ void e2d::Text::_createFormat() SafeRelease(_textFormat); HRESULT hr = Renderer::getIDWriteFactory()->CreateTextFormat( - _font.family, + (const WCHAR *)_font.family, nullptr, DWRITE_FONT_WEIGHT(_font.weight), _font.italic ? DWRITE_FONT_STYLE_ITALIC : DWRITE_FONT_STYLE_NORMAL, @@ -391,7 +391,7 @@ void e2d::Text::_createLayout() if (_style.wrapping) { hr = Renderer::getIDWriteFactory()->CreateTextLayout( - _text, + (const WCHAR *)_text, length, _textFormat, float(_style.wrappingWidth), @@ -409,7 +409,7 @@ void e2d::Text::_createLayout() } else { - hr = Renderer::getIDWriteFactory()->CreateTextLayout(_text, length, _textFormat, 0, 0, &_textLayout); + hr = Renderer::getIDWriteFactory()->CreateTextLayout((const WCHAR *)_text, length, _textFormat, 0, 0, &_textLayout); // 为防止文本对齐问题,根据刚才创建的 layout 宽度重新创建它 if (_textLayout) { @@ -420,7 +420,7 @@ void e2d::Text::_createLayout() this->setSize(metrics.width, metrics.height); // 重新创建 layout SafeRelease(_textLayout); - hr = Renderer::getIDWriteFactory()->CreateTextLayout(_text, length, _textFormat, _width, 0, &_textLayout); + hr = Renderer::getIDWriteFactory()->CreateTextLayout((const WCHAR *)_text, length, _textFormat, _width, 0, &_textLayout); } } diff --git a/core/Tool/Data.cpp b/core/Tool/Data.cpp index 6faddef2..4ad87b3a 100644 --- a/core/Tool/Data.cpp +++ b/core/Tool/Data.cpp @@ -1,64 +1,48 @@ #include "..\e2dtool.h" -static e2d::String s_sDataFileName = L"DefaultData.ini"; void e2d::Data::saveInt(const String& key, int value, const String& field) { - ::WritePrivateProfileString(field, key, String::parse(value), Data::getDataFilePath()); + ::WritePrivateProfileString((LPCWSTR)field, (LPCWSTR)key, (LPCWSTR)String::parse(value), (LPCWSTR)Path::getDataSavePath()); } void e2d::Data::saveDouble(const String& key, double value, const String& field) { - ::WritePrivateProfileString(field, key, String::parse(value), Data::getDataFilePath()); + ::WritePrivateProfileString((LPCWSTR)field, (LPCWSTR)key, (LPCWSTR)String::parse(value), (LPCWSTR)Path::getDataSavePath()); } void e2d::Data::saveBool(const String& key, bool value, const String& field) { - const wchar_t* sValue = value ? L"1" : L"0"; - ::WritePrivateProfileString(field, key, sValue, Data::getDataFilePath()); + ::WritePrivateProfileString((LPCWSTR)field, (LPCWSTR)key, (value ? L"1" : L"0"), (LPCWSTR)Path::getDataSavePath()); } void e2d::Data::saveString(const String& key, const String& value, const String& field) { - ::WritePrivateProfileString(field, key, value, Data::getDataFilePath()); + ::WritePrivateProfileString((LPCWSTR)field, (LPCWSTR)key, (LPCWSTR)value, (LPCWSTR)Path::getDataSavePath()); } int e2d::Data::getInt(const String& key, int defaultValue, const String& field) { - return ::GetPrivateProfileInt(field, key, defaultValue, Data::getDataFilePath()); + return ::GetPrivateProfileInt((LPCWSTR)field, (LPCWSTR)key, defaultValue, (LPCWSTR)Path::getDataSavePath()); } double e2d::Data::getDouble(const String& key, double defaultValue, const String& field) { wchar_t temp[32] = { 0 }; - ::GetPrivateProfileString(field, key, String::parse(defaultValue), temp, 31, Data::getDataFilePath()); + ::GetPrivateProfileString((LPCWSTR)field, (LPCWSTR)key, (LPCWSTR)String::parse(defaultValue), temp, 31, (LPCWSTR)Path::getDataSavePath()); return std::stof(temp); } bool e2d::Data::getBool(const String& key, bool defaultValue, const String& field) { int nDefaultValue = defaultValue ? 1 : 0; - int nValue = ::GetPrivateProfileInt(field, key, nDefaultValue, Data::getDataFilePath()); + int nValue = ::GetPrivateProfileInt((LPCWSTR)field, (LPCWSTR)key, nDefaultValue, (LPCWSTR)Path::getDataSavePath()); return nValue != 0; } e2d::String e2d::Data::getString(const String& key, const String& defaultValue, const String& field) { wchar_t temp[256] = { 0 }; - ::GetPrivateProfileString(field, key, defaultValue, temp, 255, Data::getDataFilePath()); + ::GetPrivateProfileString((LPCWSTR)field, (LPCWSTR)key, (LPCWSTR)defaultValue, temp, 255, (LPCWSTR)Path::getDataSavePath()); return temp; -} - -void e2d::Data::setDataFileName(const String& fileName) -{ - if (!fileName.isEmpty()) - { - s_sDataFileName.clear(); - s_sDataFileName << fileName << L".ini"; - } -} - -e2d::String e2d::Data::getDataFilePath() -{ - return Path::getDefaultSavePath() + s_sDataFileName; -} +} \ No newline at end of file diff --git a/core/Tool/Music.cpp b/core/Tool/Music.cpp index c0ababb6..e1fb154e 100644 --- a/core/Tool/Music.cpp +++ b/core/Tool/Music.cpp @@ -93,7 +93,7 @@ bool e2d::Music::open(const e2d::String& filePath) // 定位 wave 文件 wchar_t pFilePath[MAX_PATH]; - if (!_findMediaFileCch(pFilePath, MAX_PATH, filePath)) + if (!_findMediaFileCch(pFilePath, MAX_PATH, (const wchar_t *)filePath)) { WARN("Failed to find media file: %s", pFilePath); return false; @@ -162,7 +162,7 @@ bool e2d::Music::open(int resNameId, const e2d::String& resType) return false; } - if (nullptr == (hResInfo = FindResourceW(HINST_THISCOMPONENT, MAKEINTRESOURCE(resNameId), resType))) + if (nullptr == (hResInfo = FindResourceW(HINST_THISCOMPONENT, MAKEINTRESOURCE(resNameId), (LPCWSTR)resType))) return TraceError(L"FindResource"); if (nullptr == (hResData = LoadResource(HINST_THISCOMPONENT, hResInfo))) diff --git a/core/Tool/Path.cpp b/core/Tool/Path.cpp index 27cd8434..d523b394 100644 --- a/core/Tool/Path.cpp +++ b/core/Tool/Path.cpp @@ -11,9 +11,9 @@ DEFINE_KNOWN_FOLDER(FOLDERID_LocalAppData, 0xF1B32785, 0x6FBA, 0x4FCF, 0x9D, 0x5 static e2d::String s_sLocalAppDataPath; static e2d::String s_sTempPath; -static e2d::String s_sDefaultSavePath; +static e2d::String s_sDataSavePath; -bool e2d::Path::__init() +bool e2d::Path::__init(const String& gameName) { // 获取 AppData\Local 文件夹的路径 typedef HRESULT(WINAPI* pFunSHGetKnownFolderPath)(const GUID& rfid, DWORD dwFlags, HANDLE hToken, PWSTR *ppszPath); @@ -30,93 +30,47 @@ bool e2d::Path::__init() } else { - WARN("Cannot get local AppData path!"); + WARN("Get local AppData path failed!"); + return false; } - - // 获取游戏名称 - String sGameName = Game::getName(); - // 获取默认保存路径 - bool bInitSavePath = false; - do + // 获取数据的默认保存路径 + s_sDataSavePath = s_sLocalAppDataPath + L"\\Easy2DGameData\\"; + if (!gameName.isEmpty()) { - String localAppPath = s_sLocalAppDataPath; - if (localAppPath.isEmpty()) + s_sDataSavePath << gameName << L"\\"; + } + if (!Path::existsFolder(s_sDataSavePath)) + { + if (!Path::createFolder(s_sDataSavePath)) { - - break; + s_sDataSavePath = L""; } - else - { - s_sDefaultSavePath = localAppPath; - } - - localAppPath << L"\\Easy2DGameData"; - if (Path::createFolder(localAppPath)) - { - s_sDefaultSavePath = localAppPath; - } - else - { - break; - } - - if (!sGameName.isEmpty()) - { - localAppPath << L"\\" << sGameName; - // 创建文件夹 - if (Path::createFolder(localAppPath)) - { - s_sDefaultSavePath = localAppPath; - } - } - - s_sDefaultSavePath << L"\\"; - bInitSavePath = true; - } while (0); + } + s_sDataSavePath << L"Data.ini"; // 获取临时文件目录 - bool bInitTempPath = false; - do + wchar_t path[_MAX_PATH]; + if (0 == ::GetTempPath(_MAX_PATH, path)) { - wchar_t path[_MAX_PATH]; - if (0 == ::GetTempPath(_MAX_PATH, path)) - { - break; - } - else - { - s_sTempPath = path; - } + return false; + } - // 创建临时文件目录 - String tempPath; - tempPath << s_sTempPath << L"\\Easy2DGameTemp"; - // 创建文件夹 - if (Path::createFolder(tempPath)) - { - s_sTempPath = path; - } - else - { - break; - } + s_sTempPath << path << L"\\Easy2DGameTemp\\"; + if (!gameName.isEmpty()) + { + s_sTempPath << gameName << L"\\"; + } - if (!sGameName.isEmpty()) + if (!Path::existsFolder(s_sTempPath)) + { + if (!Path::createFolder(s_sTempPath)) { - tempPath << L"\\" << sGameName; - // 创建文件夹 - if (Path::createFolder(tempPath)) - { - s_sTempPath = tempPath; - } + s_sTempPath = L""; } + } - s_sTempPath << L"\\"; - bInitTempPath = true; - } while (0); - - return SUCCEEDED(hr) && bInitSavePath && bInitTempPath; + return true; } e2d::String e2d::Path::getTempPath() @@ -124,9 +78,9 @@ e2d::String e2d::Path::getTempPath() return s_sTempPath; } -e2d::String e2d::Path::getDefaultSavePath() +e2d::String e2d::Path::getDataSavePath() { - return s_sDefaultSavePath; + return s_sDataSavePath; } e2d::String e2d::Path::getFileExtension(const String& filePath) @@ -159,8 +113,8 @@ e2d::String e2d::Path::getSaveFilePath(const String& title, const String& defExt ofn.nMaxFile = sizeof(strFilename); // 缓冲区长度 ofn.lpstrInitialDir = nullptr; // 初始目录为默认 ofn.Flags = OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT; - ofn.lpstrTitle = title; // 标题 - ofn.lpstrDefExt = defExt; // 默认追加的扩展名 + ofn.lpstrTitle = (LPCWSTR)title; // 标题 + ofn.lpstrDefExt = (LPCWSTR)defExt; // 默认追加的扩展名 if (GetSaveFileName(&ofn)) { @@ -171,18 +125,32 @@ e2d::String e2d::Path::getSaveFilePath(const String& title, const String& defExt bool e2d::Path::createFolder(const String& dirPath) { - if (dirPath.isEmpty()) + if (dirPath.isEmpty() || dirPath.getLength() >= MAX_PATH) { - WARN("Path::createFolder Failed: Invalid directory path!"); return false; } - if (-1 == ::_waccess(dirPath, 0)) + wchar_t tmpDirPath[_MAX_PATH] = { 0 }; + int length = dirPath.getLength(); + + for (int i = 0; i < length; ++i) { - if (0 != ::_wmkdir(dirPath)) + tmpDirPath[i] = dirPath.at(i); + if (tmpDirPath[i] == L'\\' || tmpDirPath[i] == L'/' || i == (length - 1)) { - return false; + if (::_waccess(tmpDirPath, 0) != 0) + { + if (::_wmkdir(tmpDirPath) != 0) + { + return false; + } + } } } return true; } + +bool e2d::Path::existsFolder(const String & dirPath) +{ + return ::_waccess((const wchar_t *)dirPath, 0) == 0; +} diff --git a/core/e2dcommon.h b/core/e2dcommon.h index 343ea0a0..8be9a677 100644 --- a/core/e2dcommon.h +++ b/core/e2dcommon.h @@ -147,6 +147,11 @@ public: // 获取 ANSI 字符串 std::string getCString() const; + // 获取指定位置字符 + wchar_t at( + int index + ) const; + // 比较字符串 int compare( const String & str @@ -231,8 +236,8 @@ public: friend class String operator+ (const wchar_t*, const String &); // 类型转换操作符 - operator const wchar_t* () const; - operator wchar_t* () const; + explicit operator const wchar_t* () const; + explicit operator wchar_t* () const; // 比较运算符 bool operator== (const String &) const; diff --git a/core/e2dtool.h b/core/e2dtool.h index 7965d910..be9f6318 100644 --- a/core/e2dtool.h +++ b/core/e2dtool.h @@ -433,14 +433,6 @@ public: const String& defaultValue, /* 默认值 */ const String& field = L"Defalut" /* 字段名称 */ ); - - // 修改数据文件的名称 - static void setDataFileName( - const String& filePath /* 文件名称 */ - ); - - // 获取数据文件的完整路径 - static String getDataFilePath(); }; @@ -451,7 +443,7 @@ class Path public: // 获取数据的默认保存路径 - static String getDefaultSavePath(); + static String getDataSavePath(); // 获取临时文件目录 static String getTempPath(); @@ -472,9 +464,16 @@ public: const String& dirPath /* 文件夹路径 */ ); + // 判断文件夹是否存在 + static bool existsFolder( + const String& dirPath /* 文件夹路径 */ + ); + private: // 初始化 - static bool __init(); + static bool __init( + const String& gameName + ); }; } \ No newline at end of file