1717 */
1818package org .jackhuang .hmcl ;
1919
20- import javafx .application .Platform ;
21- import javafx .scene .control .Alert ;
2220import org .jackhuang .hmcl .util .FileSaver ;
2321import org .jackhuang .hmcl .ui .AwtUtils ;
2422import org .jackhuang .hmcl .util .ModuleHelper ;
2523import org .jackhuang .hmcl .util .SelfDependencyPatcher ;
26- import org .jackhuang .hmcl .ui .SwingUtils ;
24+ import org .jackhuang .hmcl .util .SwingUtils ;
2725import org .jackhuang .hmcl .java .JavaRuntime ;
2826import org .jackhuang .hmcl .util .platform .OperatingSystem ;
2927
30- import javax .net .ssl .HttpsURLConnection ;
31- import javax .net .ssl .SSLContext ;
32- import javax .net .ssl .TrustManagerFactory ;
33- import java .io .File ;
3428import java .io .IOException ;
35- import java .io .InputStream ;
3629import java .lang .reflect .Method ;
3730import java .nio .file .Files ;
38- import java .nio .file .Path ;
39- import java .nio .file .Paths ;
40- import java .security .KeyManagementException ;
41- import java .security .KeyStore ;
42- import java .security .KeyStoreException ;
43- import java .security .NoSuchAlgorithmException ;
44- import java .security .cert .CertificateException ;
45- import java .util .Collections ;
4631import java .util .concurrent .CancellationException ;
4732
48- import static org .jackhuang .hmcl .util .Lang .thread ;
4933import static org .jackhuang .hmcl .util .logging .Logger .LOG ;
5034import static org .jackhuang .hmcl .util .i18n .I18n .i18n ;
5135
52- public final class Main {
36+ public final class EntryPoint {
5337
54- private Main () {
38+ private EntryPoint () {
5539 }
5640
5741 public static void main (String [] args ) {
@@ -64,10 +48,6 @@ public static void main(String[] args) {
6448
6549 checkDirectoryPath ();
6650
67- if (JavaRuntime .CURRENT_VERSION < 9 )
68- // This environment check will take ~300ms
69- thread (Main ::fixLetsEncrypt , "CA Certificate Check" , true );
70-
7151 if (OperatingSystem .CURRENT_OS == OperatingSystem .MACOS )
7252 initIcon ();
7353
@@ -97,6 +77,8 @@ private static void createHMCLDirectories() {
9777 }
9878 }
9979 } catch (IOException e ) {
80+ // Logger has not been started yet, so print directly to System.err
81+ System .err .println ("Failed to create HMCL directory: " + Metadata .HMCL_CURRENT_DIRECTORY );
10082 e .printStackTrace (System .err );
10183 showErrorAndExit (i18n ("fatal.create_hmcl_current_directory_failure" , Metadata .HMCL_CURRENT_DIRECTORY ));
10284 }
@@ -112,27 +94,28 @@ private static void createHMCLDirectories() {
11294 }
11395
11496 private static void initIcon () {
115- java .awt .Image image = java .awt .Toolkit .getDefaultToolkit ().getImage (Main .class .getResource ("/assets/img/icon-mac.png" ));
97+ java .awt .Image image = java .awt .Toolkit .getDefaultToolkit ().getImage (EntryPoint .class .getResource ("/assets/img/icon-mac.png" ));
11698 AwtUtils .setAppleIcon (image );
11799 }
118100
119101 private static void checkDirectoryPath () {
120- String currentDirectory = new File ("" ).getAbsolutePath ();
121- if (currentDirectory .contains ("!" )) {
102+ String currentDir = System .getProperty ("user.dir" , "" );
103+ if (currentDir .contains ("!" )) {
104+ LOG .error ("The current working path contains an exclamation mark: " + currentDir );
122105 // No Chinese translation because both Swing and JavaFX cannot render Chinese character properly when exclamation mark exists in the path.
123106 showErrorAndExit ("Exclamation mark(!) is not allowed in the path where HMCL is in.\n "
124- + "The path is " + currentDirectory );
107+ + "The path is " + currentDir );
125108 }
126109 }
127110
128111 private static void checkJavaFX () {
129112 try {
130113 SelfDependencyPatcher .patch ();
131114 } catch (SelfDependencyPatcher .PatchException e ) {
132- LOG .error ("unable to patch JVM" , e );
115+ LOG .error ("Unable to patch JVM" , e );
133116 showErrorAndExit (i18n ("fatal.javafx.missing" ));
134117 } catch (SelfDependencyPatcher .IncompatibleVersionException e ) {
135- LOG .error ("unable to patch JVM" , e );
118+ LOG .error ("Unable to patch JVM" , e );
136119 showErrorAndExit (i18n ("fatal.javafx.incompatible" ));
137120 } catch (CancellationException e ) {
138121 LOG .error ("User cancels downloading JavaFX" , e );
@@ -149,7 +132,7 @@ private static void verifyJavaFX() {
149132 Class .forName ("javafx.stage.Stage" ); // javafx.graphics
150133 Class .forName ("javafx.scene.control.Skin" ); // javafx.controls
151134 } catch (Exception e ) {
152- e . printStackTrace ( System . err );
135+ LOG . warning ( "JavaFX is incomplete or not found" , e );
153136 showErrorAndExit (i18n ("fatal.javafx.incomplete" ));
154137 }
155138 }
@@ -159,7 +142,7 @@ private static void addEnableNativeAccess() {
159142 try {
160143 ModuleHelper .addEnableNativeAccess (Class .forName ("javafx.stage.Stage" )); // javafx.graphics
161144 } catch (ClassNotFoundException e ) {
162- e . printStackTrace ( System . err );
145+ LOG . error ( "Failed to add enable native access for JavaFX" , e );
163146 showErrorAndExit (i18n ("fatal.javafx.incomplete" ));
164147 }
165148 }
@@ -174,78 +157,16 @@ private static void enableUnsafeMemoryAccess() {
174157 trySetMemoryAccessWarned .setAccessible (true );
175158 trySetMemoryAccessWarned .invoke (null );
176159 } catch (Throwable e ) {
177- e . printStackTrace ( System . err );
160+ LOG . warning ( "Failed to enable unsafe memory access" , e );
178161 }
179162 }
180163 }
181164
182165 /**
183166 * Indicates that a fatal error has occurred, and that the application cannot start.
184167 */
185- static void showErrorAndExit (String message ) {
186- System .err .println (message );
187- System .err .println ("A fatal error has occurred, forcibly exiting." );
188-
189- try {
190- if (Platform .isFxApplicationThread ()) {
191- new Alert (Alert .AlertType .ERROR , message ).showAndWait ();
192- exit (1 );
193- }
194- } catch (Throwable ignored ) {
195- }
196-
168+ private static void showErrorAndExit (String message ) {
197169 SwingUtils .showErrorDialog (message );
198170 exit (1 );
199171 }
200-
201- /**
202- * Indicates that potential issues have been detected, and that the application may not function properly (but it can still run).
203- */
204- static void showWarningAndContinue (String message ) {
205- System .err .println (message );
206- System .err .println ("Potential issues have been detected." );
207-
208- try {
209- if (Platform .isFxApplicationThread ()) {
210- new Alert (Alert .AlertType .WARNING , message ).showAndWait ();
211- return ;
212- }
213- } catch (Throwable ignored ) {
214- }
215-
216- SwingUtils .showWarningDialog (message );
217- }
218-
219- private static void fixLetsEncrypt () {
220- try {
221- KeyStore defaultKeyStore = KeyStore .getInstance (KeyStore .getDefaultType ());
222- Path ksPath = Paths .get (System .getProperty ("java.home" ), "lib" , "security" , "cacerts" );
223-
224- try (InputStream ksStream = Files .newInputStream (ksPath )) {
225- defaultKeyStore .load (ksStream , "changeit" .toCharArray ());
226- }
227-
228- KeyStore letsEncryptKeyStore = KeyStore .getInstance (KeyStore .getDefaultType ());
229- try (InputStream letsEncryptFile = Main .class .getResourceAsStream ("/assets/lekeystore.jks" )) {
230- letsEncryptKeyStore .load (letsEncryptFile , "supersecretpassword" .toCharArray ());
231- }
232-
233- KeyStore merged = KeyStore .getInstance (KeyStore .getDefaultType ());
234- merged .load (null , new char [0 ]);
235- for (String alias : Collections .list (letsEncryptKeyStore .aliases ()))
236- merged .setCertificateEntry (alias , letsEncryptKeyStore .getCertificate (alias ));
237- for (String alias : Collections .list (defaultKeyStore .aliases ()))
238- merged .setCertificateEntry (alias , defaultKeyStore .getCertificate (alias ));
239-
240- TrustManagerFactory instance = TrustManagerFactory .getInstance (TrustManagerFactory .getDefaultAlgorithm ());
241- instance .init (merged );
242- SSLContext tls = SSLContext .getInstance ("TLS" );
243- tls .init (null , instance .getTrustManagers (), null );
244- HttpsURLConnection .setDefaultSSLSocketFactory (tls .getSocketFactory ());
245- LOG .info ("Added Lets Encrypt root certificates as additional trust" );
246- } catch (KeyStoreException | IOException | NoSuchAlgorithmException | CertificateException |
247- KeyManagementException e ) {
248- LOG .error ("Failed to load lets encrypt certificate. Expect problems" , e );
249- }
250- }
251172}
0 commit comments