2020-07-28 00:53:29 +08:00
|
|
|
#include "pch.h"
|
|
|
|
|
2021-06-14 17:55:59 +08:00
|
|
|
#include "DotnetInstallation.h"
|
2020-07-28 00:53:29 +08:00
|
|
|
|
2021-06-14 17:55:59 +08:00
|
|
|
#include <common/utils/exec.h>
|
|
|
|
#include <common/utils/HttpClient.h>
|
|
|
|
#include <common/utils/winapi_error.h>
|
2020-07-28 00:53:29 +08:00
|
|
|
namespace fs = std::filesystem;
|
|
|
|
|
|
|
|
namespace updating
|
|
|
|
{
|
2021-07-22 00:02:45 +08:00
|
|
|
bool dotnet_is_installed(const size_t major, const size_t minor, const size_t requiredMinimalPatch)
|
2020-07-28 00:53:29 +08:00
|
|
|
{
|
|
|
|
auto runtimes = exec_and_read_output(LR"(dotnet --list-runtimes)");
|
|
|
|
if (!runtimes)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2021-07-22 00:02:45 +08:00
|
|
|
std::array<char, 512> regexBuffer;
|
|
|
|
sprintf_s(regexBuffer.data(),
|
|
|
|
regexBuffer.size(),
|
|
|
|
R"(Microsoft\.WindowsDesktop\.App\s%zu\.%zu\.(\d+))",
|
|
|
|
major,
|
|
|
|
minor);
|
|
|
|
std::regex dotnetRegex{ regexBuffer.data() };
|
2021-01-13 02:52:17 +08:00
|
|
|
|
|
|
|
size_t latestPatchInstalled = 0;
|
|
|
|
using rexit = std::sregex_iterator;
|
2021-07-22 00:02:45 +08:00
|
|
|
for (auto it = rexit{ begin(*runtimes), end(*runtimes), dotnetRegex }; it != rexit{}; ++it)
|
2021-01-13 02:52:17 +08:00
|
|
|
{
|
|
|
|
if (!it->ready() || it->size() < 2)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
auto patchNumberGroup = (*it)[1];
|
|
|
|
if (!patchNumberGroup.matched)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const auto patchString = patchNumberGroup.str();
|
|
|
|
size_t patch = 0;
|
|
|
|
if (auto [_, ec] = std::from_chars(&*begin(patchString), &*end(patchString), patch); ec == std::errc())
|
|
|
|
{
|
|
|
|
latestPatchInstalled = std::max(patch, latestPatchInstalled);
|
|
|
|
}
|
|
|
|
}
|
2021-07-22 00:02:45 +08:00
|
|
|
return latestPatchInstalled >= requiredMinimalPatch;
|
2020-07-28 00:53:29 +08:00
|
|
|
}
|
|
|
|
|
2021-07-22 00:02:45 +08:00
|
|
|
std::optional<fs::path> download_dotnet(const wchar_t* dotnetDesktopDownloadLink)
|
2020-07-28 00:53:29 +08:00
|
|
|
{
|
|
|
|
const wchar_t DOTNET_DESKTOP_FILENAME[] = L"windowsdesktop-runtime.exe";
|
|
|
|
|
|
|
|
auto dotnet_download_path = fs::temp_directory_path() / DOTNET_DESKTOP_FILENAME;
|
2021-07-22 00:02:45 +08:00
|
|
|
winrt::Windows::Foundation::Uri download_link{ dotnetDesktopDownloadLink };
|
2020-07-28 00:53:29 +08:00
|
|
|
|
|
|
|
const size_t max_attempts = 3;
|
|
|
|
bool download_success = false;
|
|
|
|
for (size_t i = 0; i < max_attempts; ++i)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
http::HttpClient client;
|
|
|
|
client.download(download_link, dotnet_download_path).wait();
|
|
|
|
download_success = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
// couldn't download
|
|
|
|
}
|
|
|
|
}
|
2020-11-27 05:31:00 +08:00
|
|
|
return download_success ? std::make_optional(dotnet_download_path) : std::nullopt;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool install_dotnet(fs::path dotnet_download_path, const bool silent = false)
|
|
|
|
{
|
2020-07-28 00:53:29 +08:00
|
|
|
SHELLEXECUTEINFOW sei{ sizeof(sei) };
|
|
|
|
sei.fMask = { SEE_MASK_NOASYNC | SEE_MASK_NOCLOSEPROCESS | SEE_MASK_NO_CONSOLE };
|
|
|
|
sei.lpFile = dotnet_download_path.c_str();
|
|
|
|
sei.nShow = SW_SHOWNORMAL;
|
2020-08-26 18:46:19 +08:00
|
|
|
std::wstring dotnet_flags = L"/install ";
|
|
|
|
dotnet_flags += silent ? L"/quiet" : L"/passive";
|
|
|
|
sei.lpParameters = dotnet_flags.c_str();
|
2020-07-28 00:53:29 +08:00
|
|
|
if (ShellExecuteExW(&sei) != TRUE)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
WaitForSingleObject(sei.hProcess, INFINITE);
|
|
|
|
CloseHandle(sei.hProcess);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|