colobot/colobot-base/common/system/system_windows.cpp

155 lines
4.7 KiB
C++
Raw Normal View History

/*
* This file is part of the Colobot: Gold Edition source code
2023-08-06 21:15:48 +00:00
* Copyright (C) 2001-2023, Daniel Roux, EPSITEC SA & TerranovaTeam
2015-08-22 14:40:02 +00:00
* http://epsitec.ch; http://colobot.info; http://github.com/colobot
*
* This program 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 3 of the License, or
* (at your option) any later version.
*
* This program 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 this program. If not, see http://gnu.org/licenses
*/
#include "common/system/system_windows.h"
#include "common/logger.h"
#include <windows.h>
#include <filesystem>
void CSystemUtilsWindows::Init()
{
}
SystemDialogResult CSystemUtilsWindows::SystemDialog(SystemDialogType type, const std::string& title, const std::string& message)
{
unsigned int windowsType = 0;
std::wstring windowsMessage = UTF8_Decode(message);
std::wstring windowsTitle = UTF8_Decode(title);
switch (type)
{
case SystemDialogType::INFO:
default:
windowsType = MB_ICONINFORMATION|MB_OK;
break;
case SystemDialogType::WARNING:
windowsType = MB_ICONWARNING|MB_OK;
break;
case SystemDialogType::ERROR_MSG:
windowsType = MB_ICONERROR|MB_OK;
break;
case SystemDialogType::YES_NO:
windowsType = MB_ICONQUESTION|MB_YESNO;
break;
case SystemDialogType::OK_CANCEL:
windowsType = MB_ICONWARNING|MB_OKCANCEL;
break;
}
2015-08-16 10:43:42 +00:00
switch (MessageBoxW(nullptr, windowsMessage.c_str(), windowsTitle.c_str(), windowsType))
{
case IDOK:
return SystemDialogResult::OK;
case IDCANCEL:
return SystemDialogResult::CANCEL;
case IDYES:
return SystemDialogResult::YES;
case IDNO:
return SystemDialogResult::NO;
default:
break;
}
return SystemDialogResult::OK;
}
//! Converts a wide Unicode string to an UTF8 string
std::string CSystemUtilsWindows::UTF8_Encode(const std::wstring& wstr)
{
2015-08-16 10:43:42 +00:00
int size_needed = WideCharToMultiByte(CP_UTF8, 0, &wstr[0], static_cast<int>(wstr.size()), nullptr, 0, nullptr, nullptr);
std::string strTo(size_needed, 0);
2015-08-16 10:43:42 +00:00
WideCharToMultiByte(CP_UTF8, 0, &wstr[0], static_cast<int>(wstr.size()), &strTo[0], size_needed, nullptr, nullptr);
return strTo;
}
//! Converts an UTF8 string to a wide Unicode String
std::wstring CSystemUtilsWindows::UTF8_Decode(const std::string& str)
{
2015-08-16 10:43:42 +00:00
int size_needed = MultiByteToWideChar(CP_UTF8, 0, &str[0], static_cast<int>(str.size()), nullptr, 0);
std::wstring wstrTo(size_needed, 0);
MultiByteToWideChar(CP_UTF8, 0, &str[0], static_cast<int>(str.size()), &wstrTo[0], size_needed);
return wstrTo;
}
2014-07-24 21:38:13 +00:00
std::string CSystemUtilsWindows::GetSaveDir()
{
2018-05-16 11:28:06 +00:00
#if PORTABLE_SAVES || DEV_BUILD
return CSystemUtils::GetSaveDir();
2018-05-16 11:28:06 +00:00
#else
2014-07-24 21:38:13 +00:00
std::string savegameDir;
2014-09-07 17:26:06 +00:00
2020-04-05 11:24:45 +00:00
auto envUSERPROFILE = GetEnvVar("USERPROFILE");
if (envUSERPROFILE.empty())
{
2020-04-05 11:24:45 +00:00
GetLogger()->Warn("Unable to find directory for saves - using default directory");
savegameDir = CSystemUtils::GetSaveDir();
}
else
{
2020-04-05 11:24:45 +00:00
savegameDir = envUSERPROFILE + "\\colobot";
}
2014-07-24 21:38:13 +00:00
GetLogger()->Trace("Saved game files are going to %s\n", savegameDir.c_str());
2014-09-07 17:26:06 +00:00
2014-07-24 21:38:13 +00:00
return savegameDir;
2018-05-16 11:28:06 +00:00
#endif
}
2020-04-03 18:15:24 +00:00
std::string CSystemUtilsWindows::GetEnvVar(const std::string& name)
{
std::wstring wname(name.begin(), name.end());
wchar_t* envVar = _wgetenv(wname.c_str());
if (envVar == nullptr)
{
return "";
}
else
{
std::string var = UTF8_Encode(std::wstring(envVar));
GetLogger()->Trace("Detected environment variable %s = %s\n", name.c_str(), var.c_str());
return var;
}
}
bool CSystemUtilsWindows::OpenPath(const std::string& path)
{
int result = system(("start explorer \"" + std::filesystem::u8path(path).make_preferred().string() + "\"").c_str());
if (result != 0)
{
GetLogger()->Error("Failed to open path: %s, error code: %i\n", path.c_str(), result);
return false;
}
return true;
}
bool CSystemUtilsWindows::OpenWebsite(const std::string& url)
{
int result = system(("rundll32 url.dll,FileProtocolHandler \"" + url + "\"").c_str());
if (result != 0)
{
GetLogger()->Error("Failed to open website: %s, error code: %i\n", url.c_str(), result);
return false;
}
return true;
}