HMCLauncher: verify if JVM version is between 8 and 10

This commit is contained in:
huanghongxun
2019-02-14 15:34:52 +08:00
parent 394873864f
commit eab4463c2d
12 changed files with 91 additions and 9 deletions

Binary file not shown.

Binary file not shown.

View File

@@ -24,6 +24,7 @@
<Keyword>Win32Proj</Keyword> <Keyword>Win32Proj</Keyword>
<RootNamespace>HMCL</RootNamespace> <RootNamespace>HMCL</RootNamespace>
<WindowsTargetPlatformVersion>7.0</WindowsTargetPlatformVersion> <WindowsTargetPlatformVersion>7.0</WindowsTargetPlatformVersion>
<ProjectName>HMCLauncher</ProjectName>
</PropertyGroup> </PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
@@ -94,6 +95,7 @@
<Link> <Link>
<SubSystem>Windows</SubSystem> <SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation> <GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>version.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link> </Link>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
@@ -129,6 +131,7 @@
<EnableCOMDATFolding>true</EnableCOMDATFolding> <EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences> <OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation> <GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>version.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link> </Link>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
@@ -153,6 +156,7 @@
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="java.h" /> <ClInclude Include="java.h" />
<ClInclude Include="lang.h" />
<ClInclude Include="main.h" /> <ClInclude Include="main.h" />
<ClInclude Include="os.h" /> <ClInclude Include="os.h" />
<ClInclude Include="Resource.h" /> <ClInclude Include="Resource.h" />

View File

@@ -36,6 +36,9 @@
<ClInclude Include="os.h"> <ClInclude Include="os.h">
<Filter>头文件</Filter> <Filter>头文件</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="lang.h">
<Filter>头文件</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="stdafx.cpp"> <ClCompile Include="stdafx.cpp">

View File

@@ -1,7 +1,9 @@
#include "stdafx.h" #include "stdafx.h"
#include "Version.h" #include "Version.h"
Version::Version(const std::wstring & rawString) using namespace std;
Version::Version(const wstring & rawString)
{ {
int idx = 0; int idx = 0;
ver[0] = ver[1] = ver[2] = ver[3] = 0; ver[0] = ver[1] = ver[2] = ver[3] = 0;

View File

@@ -9,6 +9,18 @@ public:
Version(const std::wstring &rawString); Version(const std::wstring &rawString);
template <typename T>
Version(std::initializer_list<T> ver_list)
{
int i = 0;
for (const auto &data : ver_list)
{
if (i >= 4)
break;
ver[i++] = data;
}
}
bool operator<(const Version &other) const bool operator<(const Version &other) const
{ {
for (int i = 0; i < 4; ++i) for (int i = 0; i < 4; ++i)
@@ -16,5 +28,13 @@ public:
return ver[i] < other.ver[i]; return ver[i] < other.ver[i];
return false; return false;
} }
bool operator<=(const Version &other) const
{
for (int i = 0; i < 4; ++i)
if (ver[i] != other.ver[i])
return ver[i] < other.ver[i];
return true;
}
}; };

View File

@@ -58,7 +58,7 @@ bool FindJavaByRegistryKey(HKEY rootKey, LPCWSTR subKey, std::wstring & path)
{ {
if (Version(javaVer) < JAVA_8) if (Version(javaVer) < JAVA_8)
oldJavaFound = true; oldJavaFound = true;
else if (!(Version(javaVer) < JAVA_11)) else if (JAVA_11 <= Version(javaVer))
newJavaFound = true; newJavaFound = true;
else else
flag = true; flag = true;

4
HMCLauncher/HMCL/lang.h Normal file
View File

@@ -0,0 +1,4 @@
#pragma once
#define ERROR_PROMPT L"Java installation cannot be found in this computer, please download it from https://java.com \n" \
L"未能在这台电脑上找到Java 8~Java 10请从 https://java.com 下载安装Java"

View File

@@ -2,16 +2,30 @@
#include "main.h" #include "main.h"
#include "os.h" #include "os.h"
#include "java.h" #include "java.h"
#include "lang.h"
using namespace std; using namespace std;
Version J8(TEXT("8")), J11(TEXT("11"));
void LaunchJVM(const wstring &javaPath, const wstring &jarPath) void RawLaunchJVM(const wstring &javaPath, const wstring &jarPath)
{ {
if (MyCreateProcess(L"\"" + javaPath + L"\" -XX:MinHeapFreeRatio=5 -XX:MaxHeapFreeRatio=15 -jar \"" + jarPath + L"\"")) if (MyCreateProcess(L"\"" + javaPath + L"\" -XX:MinHeapFreeRatio=5 -XX:MaxHeapFreeRatio=15 -jar \"" + jarPath + L"\""))
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }
void LaunchJVM(const wstring &javaPath, const wstring &jarPath)
{
Version javaVersion(L"");
if (!MyGetFileVersionInfo(javaPath, javaVersion))
return;
if (J8 <= javaVersion && javaVersion < J11)
{
RawLaunchJVM(javaPath, jarPath);
}
}
int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow) int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow)
{ {
wstring path, exeName; wstring path, exeName;
@@ -37,9 +51,6 @@ int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmd
if (FindJava(path)) if (FindJava(path))
LaunchJVM(path + L"\\bin\\javaw.exe", exeName); LaunchJVM(path + L"\\bin\\javaw.exe", exeName);
// Try java in PATH
LaunchJVM(L"javaw", exeName);
// Or we try to search Java in C:\Program Files. // Or we try to search Java in C:\Program Files.
{ {
WIN32_FIND_DATA data; WIN32_FIND_DATA data;
@@ -72,8 +83,10 @@ int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmd
} }
} }
MessageBox(NULL, L"Java installation cannot be found in this computer, please download it from https://java.com \n" // Try java in PATH
L"未能在这台电脑上找到Java 8~Java 10请从 https://java.com 下载安装Java", L"Error", MB_ICONERROR | MB_OK); RawLaunchJVM(L"javaw", exeName);
MessageBox(NULL, ERROR_PROMPT, L"Error", MB_ICONERROR | MB_OK);
ShellExecute(0, 0, L"https://java.com/", 0, 0, SW_SHOW); ShellExecute(0, 0, L"https://java.com/", 0, 0, SW_SHOW);
return 1; return 1;
} }

View File

@@ -106,3 +106,36 @@ bool GetArch(bool & is64Bit)
#error _WIN64 and _WIN32 are both undefined. #error _WIN64 and _WIN32 are both undefined.
#endif #endif
} }
bool MyGetFileVersionInfo(const std::wstring & filePath, Version &version)
{
DWORD verHandle = 0;
UINT size = 0;
LPBYTE lpBuffer = NULL;
VS_FIXEDFILEINFO *pFileInfo;
DWORD dwSize = GetFileVersionInfoSize(filePath.c_str(), NULL);
if (!dwSize)
return false;
LPBYTE data = new BYTE[dwSize];
if (!GetFileVersionInfo(filePath.c_str(), 0, dwSize, data))
{
delete[] data;
return false;
}
if (!VerQueryValue(data, TEXT("\\"), (LPVOID*)&pFileInfo, &size))
{
delete[] data;
return false;
}
version = Version{
(pFileInfo->dwFileVersionMS >> 16) & 0xFFFF,
(pFileInfo->dwFileVersionMS >> 0) & 0xFFFF,
(pFileInfo->dwFileVersionLS >> 16) & 0xFFFF,
(pFileInfo->dwFileVersionLS >> 0) & 0xFFFF
};
return true;
}

View File

@@ -1,6 +1,7 @@
#pragma once #pragma once
#include <string> #include <string>
#include <windows.h> #include <windows.h>
#include "Version.h"
const int MAX_KEY_LENGTH = 255; const int MAX_KEY_LENGTH = 255;
const int MAX_VALUE_NAME = 16383; const int MAX_VALUE_NAME = 16383;
@@ -20,4 +21,6 @@ bool MyCreateProcess(const std::wstring &command);
// Check if file lpPath exists. // Check if file lpPath exists.
bool FindFirstFileExists(LPCWSTR lpPath, DWORD dwFilter); bool FindFirstFileExists(LPCWSTR lpPath, DWORD dwFilter);
bool GetArch(bool &is64Bit); bool GetArch(bool &is64Bit);
bool MyGetFileVersionInfo(const std::wstring &filePath, Version &version);