2121package com .mcmoddev .relauncher .selfupdate ;
2222
2323import java .io .BufferedInputStream ;
24- import java .io .BufferedReader ;
25- import java .io .File ;
24+ import java .io .ByteArrayInputStream ;
2625import java .io .IOException ;
27- import java .io .InputStreamReader ;
26+ import java .io .InputStream ;
2827import java .net .URL ;
28+ import java .nio .charset .StandardCharsets ;
2929import java .nio .file .Files ;
3030import java .nio .file .Path ;
31+ import java .nio .file .StandardCopyOption ;
3132import java .util .List ;
3233import java .util .Locale ;
34+ import java .util .Objects ;
3335
3436public class SelfUpdate {
3537
@@ -44,23 +46,14 @@ public static void main(String[] args) throws IOException, InterruptedException
4446 Files .createDirectories (parent );
4547 }
4648 Files .deleteIfExists (jarPath );
49+
4750 try (final var is = new BufferedInputStream (new URL (url ).openStream ())) {
4851 Files .copy (is , jarPath );
4952 System .out .println (colour ("Update to '" + url + "' successful! Re-starting the launcher..." ));
50- final var process = Runtime .getRuntime ()
51- .exec (System .getProperty ("os.name" ).toLowerCase (Locale .ROOT ).contains ("win" ) ? "cmd /c " : "" + relaunchCmd ,
52- null ,
53- parent == null ? Path .of ("" ).toFile () : parent .toFile ()
54- );
5553
56- try (final var reader = new BufferedReader (new InputStreamReader (process .getInputStream ()))) {
57- // Redirect console
58- // TODO find a better way to inherit IO, or use ProcessBuilder and find a way to split the command
59- String line ;
60- while (process .isAlive () && (line = reader .readLine ()) != null ) {
61- System .out .println (line );
62- }
63- }
54+ new ProcessBuilder (resolveScript (relaunchCmd ))
55+ .inheritIO ()
56+ .start ();
6457
6558 System .exit (0 );
6659 }
@@ -71,5 +64,26 @@ public static String colour(String text) {
7164 + " \033 [94;1m====\033 [0m" ;
7265 }
7366
67+ public static String readAllLines (InputStream is ) throws IOException {
68+ return new String (is .readAllBytes ());
69+ }
70+
71+ public static List <String > resolveScript (String script ) throws IOException {
72+ final var directory = Path .of (".relauncher" );
73+ var scriptFull = readAllLines (Objects .requireNonNull (SelfUpdate .class .getResourceAsStream ("/relauncher-restart" )));
74+ final Path path ;
75+ final List <String > cmd ;
76+ if (System .getProperty ("os.name" ).toLowerCase (Locale .ROOT ).contains ("win" )) {
77+ path = directory .resolve ("relaunch.bat" ).toAbsolutePath ();
78+ scriptFull = scriptFull .formatted ("cmd /c " + script );
79+ cmd = List .of (path .toString ());
80+ } else {
81+ path = directory .resolve ("relaunch.sh" ).toAbsolutePath ();
82+ scriptFull = scriptFull .formatted (script );
83+ cmd = List .of ("sh" , path .toString ());
84+ }
85+ Files .copy (new ByteArrayInputStream (scriptFull .getBytes (StandardCharsets .UTF_8 )), path , StandardCopyOption .REPLACE_EXISTING );
86+ return cmd ;
87+ }
7488
7589}
0 commit comments