Skip to content

Commit

Permalink
Minor cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
modmuss50 committed Sep 25, 2023
1 parent 6fd61f6 commit 7bcda3d
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 61 deletions.
9 changes: 5 additions & 4 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@ jobs:
strategy:
matrix:
platform: [ x64, Win32, ARM64 ]
configuration: [ Release, Debug ]
runs-on: windows-2022
steps:
- uses: actions/checkout@v2
- uses: microsoft/setup-msbuild@v1.1
with:
msbuild-architecture: x64
- run: msbuild.exe fabric-installer-native-bootstrap.sln /property:Configuration=Release /p:Platform=${{ matrix.platform }}
- run: msbuild.exe fabric-installer-native-bootstrap.sln /property:Configuration=${{ matrix.configuration }} /p:Platform=${{ matrix.platform }}
- uses: actions/upload-artifact@v2
with:
name: Artifacts ${{ matrix.platform }}
name: Artifacts ${{ matrix.platform }} ${{ matrix.configuration }}
path: |
${{ matrix.platform }}/Release/fabric-installer-native-bootstrap.exe
${{ matrix.platform }}/Release/fabric-installer-native-bootstrap.pdb
${{ matrix.platform }}/${{ matrix.configuration }}/fabric-installer-native-bootstrap.exe
${{ matrix.platform }}/${{ matrix.configuration }}/fabric-installer-native-bootstrap.pdb
36 changes: 27 additions & 9 deletions fabric-installer-native-bootstrap.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@
<LinkIncremental>true</LinkIncremental>
<OutDir>$(SolutionDir)$(Platform)\$(Configuration)\</OutDir>
<IntDir>$(Platform)\$(Configuration)\</IntDir>
<RunCodeAnalysis>true</RunCodeAnalysis>
<CodeAnalysisRuleSet>NativeRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
Expand All @@ -109,9 +111,13 @@
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LinkIncremental>true</LinkIncremental>
<RunCodeAnalysis>true</RunCodeAnalysis>
<CodeAnalysisRuleSet>NativeRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|ARM64'">
<LinkIncremental>true</LinkIncremental>
<RunCodeAnalysis>true</RunCodeAnalysis>
<CodeAnalysisRuleSet>NativeRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LinkIncremental>false</LinkIncremental>
Expand All @@ -121,13 +127,15 @@
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<WarningLevel>Level4</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp20</LanguageStandard>
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
<ControlFlowGuard>Guard</ControlFlowGuard>
<ControlFlowGuard>false</ControlFlowGuard>
<EnablePREfast>true</EnablePREfast>
<TreatWarningAsError>true</TreatWarningAsError>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
Expand All @@ -140,7 +148,7 @@
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<WarningLevel>Level4</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
Expand All @@ -149,26 +157,30 @@
<LanguageStandard>stdcpp20</LanguageStandard>
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
<ControlFlowGuard>Guard</ControlFlowGuard>
<TreatWarningAsError>true</TreatWarningAsError>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
<CETCompat>true</CETCompat>
</Link>
<Manifest>
<AdditionalManifestFiles>$(ProjectDir)fabric-installer-native-bootstrap.manifest %(AdditionalManifestFiles)</AdditionalManifestFiles>
</Manifest>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<WarningLevel>Level4</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp20</LanguageStandard>
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
<ControlFlowGuard>Guard</ControlFlowGuard>
<ControlFlowGuard>false</ControlFlowGuard>
<EnablePREfast>true</EnablePREfast>
<TreatWarningAsError>true</TreatWarningAsError>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
Expand All @@ -181,13 +193,15 @@
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|ARM64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<WarningLevel>Level4</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp20</LanguageStandard>
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
<ControlFlowGuard>Guard</ControlFlowGuard>
<ControlFlowGuard>false</ControlFlowGuard>
<EnablePREfast>true</EnablePREfast>
<TreatWarningAsError>true</TreatWarningAsError>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
Expand All @@ -200,7 +214,7 @@
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<WarningLevel>Level4</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
Expand All @@ -209,20 +223,22 @@
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
<LanguageStandard>stdcpp20</LanguageStandard>
<ControlFlowGuard>Guard</ControlFlowGuard>
<TreatWarningAsError>true</TreatWarningAsError>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
<CETCompat>true</CETCompat>
</Link>
<Manifest>
<AdditionalManifestFiles>$(ProjectDir)fabric-installer-native-bootstrap.manifest %(AdditionalManifestFiles)</AdditionalManifestFiles>
</Manifest>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|ARM64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<WarningLevel>Level4</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
Expand All @@ -231,12 +247,14 @@
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
<LanguageStandard>stdcpp20</LanguageStandard>
<ControlFlowGuard>Guard</ControlFlowGuard>
<TreatWarningAsError>true</TreatWarningAsError>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
<CETCompat>false</CETCompat>
</Link>
<Manifest>
<AdditionalManifestFiles>$(ProjectDir)fabric-installer-native-bootstrap.manifest %(AdditionalManifestFiles)</AdditionalManifestFiles>
Expand Down
66 changes: 33 additions & 33 deletions src/Bootstrap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,29 @@
#include <iostream>
#include <vector>

constexpr LPCWSTR ERROR_TITLE = L"Fabric Installer";
constexpr LPCWSTR ERROR_MESSAGE = L"The Fabric Installer could not find a valid Java installation.\n\nWould you like to open the Fabric wiki to find out how to fix this?\n\nURL: https://fabricmc.net/wiki/player:tutorials:java:windows";
constexpr LPCWSTR ERROR_URL = L"https://fabricmc.net/wiki/player:tutorials:java:windows";

constexpr LPCWSTR MC_LAUNCH_REG_PATH = LR"(SOFTWARE\Mojang\InstalledProducts\Minecraft Launcher)";
constexpr LPCWSTR MC_LAUNCH_REG_KEY = L"InstallLocation";

static const LPCWSTR UWP_PATH = LR"(\Packages\Microsoft.4297127D64EC6_8wekyb3d8bbwe\LocalCache\Local\)";

static const std::vector<LPCWSTR> MC_JAVA_PATHS = {
LR"(runtime\java-runtime-gamma\windows-x64\java-runtime-gamma\bin\javaw.exe)", // Java 17.0.3
LR"(runtime\java-runtime-gamma\windows-x86\java-runtime-gamma\bin\javaw.exe)",
LR"(runtime\java-runtime-beta\windows-x64\java-runtime-beta\bin\javaw.exe)", // Java 17.0.1
LR"(runtime\java-runtime-beta\windows-x86\java-runtime-beta\bin\javaw.exe)",
LR"(runtime\java-runtime-alpha\windows-x64\java-runtime-alpha\bin\javaw.exe)", // Java 16
LR"(runtime\java-runtime-alpha\windows-x86\java-runtime-alpha\bin\javaw.exe)",
LR"(runtime\jre-legacy\windows-x64\jre-legacy\bin\javaw.exe)", // Java 8 new location
LR"(runtime\jre-legacy\windows-x86\jre-legacy\bin\javaw.exe)",
LR"(runtime\jre-x64\bin\javaw.exe)", // Java 8 old location
LR"(runtime\jre-x86\bin\javaw.exe)",
};
namespace {
constexpr LPCWSTR ERROR_TITLE = L"Fabric Installer";
constexpr LPCWSTR ERROR_MESSAGE = L"The Fabric Installer could not find a valid Java installation.\n\nWould you like to open the Fabric wiki to find out how to fix this?\n\nURL: https://fabricmc.net/wiki/player:tutorials:java:windows";
constexpr LPCWSTR ERROR_URL = L"https://fabricmc.net/wiki/player:tutorials:java:windows";

constexpr LPCWSTR MC_LAUNCH_REG_PATH = LR"(SOFTWARE\Mojang\InstalledProducts\Minecraft Launcher)";
constexpr LPCWSTR MC_LAUNCH_REG_KEY = L"InstallLocation";

constexpr const LPCWSTR UWP_PATH = LR"(\Packages\Microsoft.4297127D64EC6_8wekyb3d8bbwe\LocalCache\Local\)";

static const std::vector<LPCWSTR> MC_JAVA_PATHS = {
LR"(runtime\java-runtime-gamma\windows-x64\java-runtime-gamma\bin\javaw.exe)", // Java 17.0.8
LR"(runtime\java-runtime-gamma\windows-x86\java-runtime-gamma\bin\javaw.exe)",
LR"(runtime\java-runtime-beta\windows-x64\java-runtime-beta\bin\javaw.exe)", // Java 17.0.1
LR"(runtime\java-runtime-beta\windows-x86\java-runtime-beta\bin\javaw.exe)",
LR"(runtime\java-runtime-alpha\windows-x64\java-runtime-alpha\bin\javaw.exe)", // Java 16
LR"(runtime\java-runtime-alpha\windows-x86\java-runtime-alpha\bin\javaw.exe)",
LR"(runtime\jre-legacy\windows-x64\jre-legacy\bin\javaw.exe)", // Java 8 new location
LR"(runtime\jre-legacy\windows-x86\jre-legacy\bin\javaw.exe)",
LR"(runtime\jre-x64\bin\javaw.exe)", // Java 8 old location
LR"(runtime\jre-x86\bin\javaw.exe)",
};
}

Bootstrap::Bootstrap(const std::shared_ptr<ISystemHelper>& systemHelper) : systemHelper(systemHelper) {}

Expand Down Expand Up @@ -116,20 +118,18 @@ bool Bootstrap::attemptLaunch(const std::wstring& path, bool checkExists) {

std::wcout << "Testing for valid java @ (" << path << ")" << std::endl;
DWORD exit = systemHelper->createProcess({ path, L"-version" });
if (exit == 0) {
// -version returned a successful exit code.
std::wcout << "Found valid java @ (" << path << ")" << std::endl;
if (exit != 0) {
std::wcout << "Java @ (" << path << ") returned an exit code of: " << std::to_wstring(exit) << std::endl;
return false;
}

if (systemHelper->createProcess({ path, L"-jar", systemHelper->getBootstrapFilename(), L"-fabricInstallerBootstrap", L"true" }) != 0) {
// The installer returned a none 0 exit code, meaning that most likely the installer crashed.
throw std::runtime_error("Installer returned a none 0 exit code");
}
std::wcout << "Found valid java @ (" << path << ")" << std::endl;

return true;
}
else {
std::wcout << "Java @ (" << path << ") returned an exit code of: " << std::to_wstring(exit) << std::endl;
exit = systemHelper->createProcess({ path, L"-jar", systemHelper->getBootstrapFilename(), L"-fabricInstallerBootstrap", L"true" });
if (exit != 0) {
// The installer returned a none 0 exit code, meaning that most likely the installer crashed.
throw std::runtime_error("Installer returned a none 0 exit code: " + exit);
}

return false;
return true;
}
19 changes: 9 additions & 10 deletions src/SystemHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ std::optional<std::wstring> SystemHelper::getRegValue(HKEY hive, const std::wstr
return std::nullopt;
}

std::wstring data;
data.resize(dataSize / sizeof(wchar_t));
std::wstring value;
value.resize(dataSize / sizeof(wchar_t));

retCode = ::RegGetValueW(
hive,
path.c_str(),
key.c_str(),
RRF_RT_REG_SZ,
nullptr,
&data[0],
value.data(),
&dataSize
);

Expand All @@ -38,9 +38,8 @@ std::optional<std::wstring> SystemHelper::getRegValue(HKEY hive, const std::wstr

DWORD stringLengthInWchars = dataSize / sizeof(wchar_t);
stringLengthInWchars--; // Exclude the NUL written by the Win32 API
data.resize(stringLengthInWchars);

return data;
value.resize(stringLengthInWchars);
return value;
}

std::optional<std::wstring> SystemHelper::getEnvVar(const std::wstring& key)
Expand All @@ -53,7 +52,7 @@ std::optional<std::wstring> SystemHelper::getEnvVar(const std::wstring& key)

// Read the env var
std::wstring value(size, L'\0');
size = ::GetEnvironmentVariableW(key.c_str(), &value[0], size);
size = ::GetEnvironmentVariableW(key.c_str(), value.data(), size);
if (!size || size >= value.size()) {
return std::nullopt;
}
Expand All @@ -63,15 +62,15 @@ std::optional<std::wstring> SystemHelper::getEnvVar(const std::wstring& key)
}

void SystemHelper::showErrorMessage(const std::wstring& title, const std::wstring& message, const std::wstring& url) {
int result = MessageBoxW(
const int result = ::MessageBoxW(
nullptr,
message.c_str(),
title.c_str(),
MB_ICONWARNING | MB_YESNO
);

if (result == IDYES) {
ShellExecuteW(nullptr, nullptr, url.c_str(), nullptr, nullptr, SW_SHOW);
::ShellExecuteW(nullptr, nullptr, url.c_str(), nullptr, nullptr, SW_SHOW);
}
}

Expand Down Expand Up @@ -117,7 +116,7 @@ DWORD SystemHelper::createProcess(std::vector<std::wstring> args)
}
else {
std::string msg = "Failed to create process: " + std::to_string(GetLastError());
return -1;
return 255;
// throw std::exception(msg.c_str());
}
}
Expand Down
24 changes: 19 additions & 5 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,30 @@
#include "Bootstrap.h"
#include "SystemHelper.h"

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow) {
// https://docs.microsoft.com/en-us/windows/win32/api/heapapi/nf-heapapi-heapsetinformation
HeapSetInformation(GetProcessHeap(), HeapEnableTerminationOnCorruption, NULL, 0);
#include <stdexcept>
#include <iostream>

_Use_decl_annotations_ int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow) {
UNREFERENCED_PARAMETER(hInstance);
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(pCmdLine);
UNREFERENCED_PARAMETER(nCmdShow);

// We dont want to try and load DLLs from the current directory (Quite often the downloads dir)
SetDefaultDllDirectories(LOAD_LIBRARY_SEARCH_SYSTEM32);
::SetDefaultDllDirectories(LOAD_LIBRARY_SEARCH_SYSTEM32);
// https://docs.microsoft.com/en-us/windows/win32/api/heapapi/nf-heapapi-heapsetinformation
::HeapSetInformation(::GetProcessHeap(), HeapEnableTerminationOnCorruption, NULL, 0);

const auto systemHelper = std::make_shared<SystemHelper>();

auto bs = Bootstrap(systemHelper);
bs.launch();

try {
bs.launch();
} catch (const std::runtime_error& error) {
std::cout << error.what() << std::endl;
return 1;
}

return 0;
}

0 comments on commit 7bcda3d

Please sign in to comment.