26
26
import java .net .PasswordAuthentication ;
27
27
import java .net .URI ;
28
28
import java .net .URISyntaxException ;
29
+ import java .net .URL ;
30
+ import java .net .URLClassLoader ;
29
31
import java .security .GeneralSecurityException ;
30
32
import java .security .cert .X509Certificate ;
31
33
import java .text .ParseException ;
32
34
import java .util .Map ;
35
+ import java .util .jar .JarFile ;
33
36
import javax .net .ssl .HostnameVerifier ;
34
37
import javax .net .ssl .HttpsURLConnection ;
35
38
import javax .net .ssl .SSLContext ;
36
39
import javax .net .ssl .X509TrustManager ;
37
40
import javax .swing .ImageIcon ;
41
+ import javax .swing .SwingUtilities ;
38
42
import javax .swing .UIManager ;
39
43
import javax .swing .UnsupportedLookAndFeelException ;
40
44
import org .jackhuang .hellominecraft .C ;
41
- import org .jackhuang .hellominecraft .utils .functions .NonConsumer ;
42
45
import org .jackhuang .hellominecraft .HMCLog ;
43
46
import org .jackhuang .hellominecraft .launcher .launch .GameLauncher ;
44
47
import org .jackhuang .hellominecraft .launcher .utils .CrashReporter ;
47
50
import org .jackhuang .hellominecraft .logging .layout .DefaultLayout ;
48
51
import org .jackhuang .hellominecraft .views .LogWindow ;
49
52
import org .jackhuang .hellominecraft .launcher .settings .Settings ;
53
+ import org .jackhuang .hellominecraft .launcher .utils .upgrade .Upgrader ;
50
54
import org .jackhuang .hellominecraft .launcher .views .MainFrame ;
51
55
import org .jackhuang .hellominecraft .lookandfeel .HelloMinecraftLookAndFeel ;
56
+ import org .jackhuang .hellominecraft .tasks .TaskWindow ;
57
+ import org .jackhuang .hellominecraft .utils .ArrayUtils ;
52
58
import org .jackhuang .hellominecraft .utils .system .MessageBox ;
53
59
import org .jackhuang .hellominecraft .utils .StrUtils ;
60
+ import org .jackhuang .hellominecraft .utils .VersionNumber ;
61
+ import org .jackhuang .hellominecraft .utils .system .FileUtils ;
62
+ import org .jackhuang .hellominecraft .utils .system .IOUtils ;
54
63
import org .jackhuang .hellominecraft .utils .system .OS ;
55
64
56
65
/**
57
66
*
58
67
* @author huangyuhui
59
68
*/
60
- public final class Main implements NonConsumer {
61
-
69
+ public final class Main implements Runnable {
70
+
62
71
private static final X509TrustManager xtm = new X509TrustManager () {
63
72
@ Override
64
73
public void checkClientTrusted (X509Certificate [] chain , String authType ) {
@@ -91,7 +100,7 @@ public X509Certificate[] getAcceptedIssuers() {
91
100
}
92
101
93
102
public static String launcherName = "Hello Minecraft! Launcher" ;
94
- public static byte firstVer = 2 , secondVer = 3 , thirdVer = 4 , forthVer = 12 ;
103
+ public static byte firstVer = 2 , secondVer = 3 , thirdVer = 4 , forthVer = 13 ;
95
104
public static int minimumLauncherVersion = 16 ;
96
105
97
106
/**
@@ -116,6 +125,31 @@ public static String makeTitle() {
116
125
117
126
public static void main (String [] args ) {
118
127
{
128
+ if (!ArrayUtils .contains (args , "nofound" ))
129
+ try {
130
+ File f = Upgrader .HMCL_VER_FILE ;
131
+ if (f .exists ()) {
132
+ Map <String , String > m = C .gson .fromJson (FileUtils .readFileToString (f ), Map .class );
133
+ String s = m .get ("ver" );
134
+ if (s != null && VersionNumber .check (s ).compareTo (new VersionNumber (firstVer , secondVer , thirdVer )) > 0 ) {
135
+ String j = m .get ("loc" );
136
+ if (j != null ) {
137
+ File jar = new File (j );
138
+ if (jar .exists ()) {
139
+ JarFile jarFile = new JarFile (jar );
140
+ String mainClass = jarFile .getManifest ().getMainAttributes ().getValue ("Main-Class" );
141
+ if (mainClass != null ) {
142
+ new URLClassLoader (new URL []{jar .toURI ().toURL ()}, URLClassLoader .getSystemClassLoader ().getParent ()).loadClass (mainClass ).getMethod ("main" , String [].class ).invoke (null , new Object []{new String []{"nofound" }});
143
+ return ;
144
+ }
145
+ }
146
+ }
147
+ }
148
+ }
149
+ } catch (Throwable t ) {
150
+ t .printStackTrace ();
151
+ }
152
+
119
153
Thread .setDefaultUncaughtExceptionHandler (new CrashReporter (true ));
120
154
121
155
try {
@@ -152,43 +186,54 @@ protected PasswordAuthentication getPasswordAuthentication() {
152
186
}
153
187
});
154
188
}
155
-
189
+
156
190
MainFrame .showMainFrame (Settings .isFirstLoad ());
157
191
}
158
192
}
159
193
160
194
@ Override
161
- public void onDone () {
195
+ public void run () {
162
196
GameLauncher .PROCESS_MANAGER .stopAllProcesses ();
163
197
}
164
198
165
199
public static void update () {
166
- if (MessageBox .Show (C .i18n ("update.newest_version" ) + Settings .UPDATE_CHECKER .getNewVersion ().firstVer + "." + Settings .UPDATE_CHECKER .getNewVersion ().secondVer + "." + Settings .UPDATE_CHECKER .getNewVersion ().thirdVer + "\n "
167
- + C .i18n ("update.should_open_link" ),
168
- MessageBox .YES_NO_OPTION ) == MessageBox .YES_OPTION ) {
169
- Map <String , String > map = Settings .UPDATE_CHECKER .download_link ;
170
- String url = C .URL_PUBLISH ;
171
- if (map != null )
172
- if (map .containsKey (OS .os ().checked_name ))
173
- url = map .get (OS .os ().checked_name );
174
- else if (map .containsKey (OS .UNKOWN .checked_name ))
175
- url = map .get (OS .UNKOWN .checked_name );
176
- if (url == null ) url = C .URL_PUBLISH ;
177
- try {
178
- java .awt .Desktop .getDesktop ().browse (new URI (url ));
179
- } catch (URISyntaxException | IOException e ) {
180
- HMCLog .warn ("Failed to browse uri: " + url , e );
181
-
182
- Clipboard cb = Toolkit .getDefaultToolkit ().getSystemClipboard ();
183
- cb .setContents (new StringSelection (url ), null );
184
- MessageBox .Show (C .i18n ("update.no_browser" ));
185
- }
186
- } else
187
- Settings .getInstance ().setCheckUpdate (false );
200
+ Settings .UPDATE_CHECKER .requestDownloadLink (() -> {
201
+ SwingUtilities .invokeLater (() -> {
202
+ Map <String , String > map = Settings .UPDATE_CHECKER .download_link ;
203
+ if (map != null && map .containsKey ("pack" ))
204
+ try {
205
+ if (TaskWindow .getInstance ().addTask (new Upgrader (map .get ("pack" ), Settings .UPDATE_CHECKER .versionString )).start ()) {
206
+ new ProcessBuilder (new String []{IOUtils .getJavaDir (), "-jar" , Upgrader .getSelf (Settings .UPDATE_CHECKER .versionString ).getAbsolutePath ()}).directory (new File ("." )).start ();
207
+ System .exit (0 );
208
+ }
209
+ } catch (IOException ex ) {
210
+ HMCLog .warn ("Failed to create upgrader" , ex );
211
+ }
212
+ if (MessageBox .Show (C .i18n ("update.newest_version" ) + Settings .UPDATE_CHECKER .getNewVersion ().firstVer + "." + Settings .UPDATE_CHECKER .getNewVersion ().secondVer + "." + Settings .UPDATE_CHECKER .getNewVersion ().thirdVer + "\n "
213
+ + C .i18n ("update.should_open_link" ),
214
+ MessageBox .YES_NO_OPTION ) == MessageBox .YES_OPTION ) {
215
+ String url = C .URL_PUBLISH ;
216
+ if (map != null )
217
+ if (map .containsKey (OS .os ().checked_name ))
218
+ url = map .get (OS .os ().checked_name );
219
+ else if (map .containsKey (OS .UNKOWN .checked_name ))
220
+ url = map .get (OS .UNKOWN .checked_name );
221
+ if (url == null ) url = C .URL_PUBLISH ;
222
+ try {
223
+ java .awt .Desktop .getDesktop ().browse (new URI (url ));
224
+ } catch (URISyntaxException | IOException e ) {
225
+ HMCLog .warn ("Failed to browse uri: " + url , e );
226
+
227
+ Clipboard cb = Toolkit .getDefaultToolkit ().getSystemClipboard ();
228
+ cb .setContents (new StringSelection (url ), null );
229
+ MessageBox .Show (C .i18n ("update.no_browser" ));
230
+ }
231
+ }
232
+ });
233
+ });
188
234
}
189
235
190
236
public static void invokeUpdate () {
191
- if (Settings .getInstance ().isCheckUpdate ()) update ();
192
237
MainFrame .instance .invokeUpdate ();
193
238
}
194
239
0 commit comments