Skip to content

Commit 76a9cf8

Browse files
committed
Fixed updater
1 parent e842475 commit 76a9cf8

File tree

5 files changed

+47
-30
lines changed

5 files changed

+47
-30
lines changed

src/main/java/org/mcphackers/mcp/MCP.java

+12-23
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,16 @@
44
import org.fusesource.jansi.Ansi;
55
import org.fusesource.jansi.AnsiConsole;
66
import org.mcphackers.mcp.tasks.info.TaskInfo;
7-
import org.mcphackers.mcp.tools.Util;
87
import org.mcphackers.mcp.tools.VersionsParser;
98

109
import java.io.IOException;
11-
import java.net.URISyntaxException;
1210
import java.nio.file.Files;
1311
import java.nio.file.Paths;
1412
import java.util.*;
1513

1614
public class MCP {
1715

18-
public static final String VERSION = "Test";
16+
public static final String VERSION = "v0.2";
1917
public static EnumMode mode = null;
2018
public static EnumMode helpCommand = null;
2119
public static MCPLogger logger;
@@ -31,31 +29,22 @@ public class MCP {
3129
.fgCyan().a(" |_| \\_\\___|\\__|_| \\___/").fgYellow().a("|_| |_|\\_____|_| ").a('\n')
3230
.fgDefault();
3331

34-
private static boolean checkIfUpdating(String[] args) throws URISyntaxException, IOException {
35-
System.out.println(Paths.get(MCP.class
36-
.getProtectionDomain()
37-
.getCodeSource()
38-
.getLocation()
39-
.toURI()));
40-
if(args.length >= 2) {
41-
if(args[0].equals("update")) {
42-
Files.deleteIfExists(Paths.get(args[1]));
43-
Files.copy(Paths.get(MCPConfig.UPDATE_JAR), Paths.get(args[1]));
44-
Util.runCommand(new String[] {
45-
Util.getJava(),
46-
"-jar",
47-
args[1]
48-
});
32+
private static void attemptToDeleteUpdateJar() {
33+
long startTime = System.currentTimeMillis();
34+
boolean keepTrying = true;
35+
while(keepTrying) {
36+
try {
37+
Files.deleteIfExists(Paths.get(MCPConfig.UPDATE_JAR));
38+
keepTrying = false;
39+
} catch (IOException e) {
40+
keepTrying = System.currentTimeMillis() - startTime < 10000;
4941
}
50-
return true;
51-
}
52-
return false;
42+
}
5343
}
5444

5545
public static void main(String[] args) throws Exception {
5646
SelfCommandPrompt.runWithCMD(SelfCommandPrompt.suggestAppId(), "RetroMCP " + VERSION, args);
57-
if(checkIfUpdating(args)) System.exit(0);
58-
//Files.deleteIfExists(Paths.get(MCPConfig.UPDATE_JAR));
47+
attemptToDeleteUpdateJar();
5948
AnsiConsole.systemInstall();
6049
logger = new MCPLogger();
6150
config = new MCPConfig();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.mcphackers.mcp;
2+
3+
import java.io.IOException;
4+
import java.nio.file.Files;
5+
import java.nio.file.Paths;
6+
7+
import org.mcphackers.mcp.tools.Util;
8+
9+
public class Update {
10+
11+
public static void main(String[] args) {
12+
if(args.length >= 1) {
13+
boolean keepTrying = true;
14+
long startTime = System.currentTimeMillis();
15+
while(keepTrying) {
16+
try {
17+
Files.deleteIfExists(Paths.get(args[0]));
18+
Files.copy(Paths.get(MCPConfig.UPDATE_JAR), Paths.get(args[0]));
19+
Util.runCommand(new String[] {
20+
Util.getJava(),
21+
"-jar",
22+
args[0]
23+
});
24+
keepTrying = false;
25+
} catch (IOException e) {
26+
keepTrying = System.currentTimeMillis() - startTime < 10000;
27+
}
28+
}
29+
}
30+
}
31+
}

src/main/java/org/mcphackers/mcp/tasks/TaskDownloadUpdate.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,9 @@ public void doTask() throws Exception {
4141
continue;
4242
}
4343
FileUtil.downloadFile(new URL(assetObj.getString("browser_download_url")), Paths.get(MCPConfig.UPDATE_JAR));
44-
//if(!Files.exists(Paths.get(MCPConfig.UPDATE_JAR)))Files.copy(Paths.get("test.jar"), Paths.get(MCPConfig.UPDATE_JAR));
4544
}
4645
}
47-
MCP.logger.info("Press return key to continue");
46+
MCP.logger.info("Press ENTER key to continue");
4847
MCP.input.nextLine();
4948
Path jarPath = Paths.get(MCP.class
5049
.getProtectionDomain()
@@ -54,9 +53,9 @@ public void doTask() throws Exception {
5453
if(!Files.isDirectory(jarPath)) {
5554
String[] cmd = new String[] {
5655
Util.getJava(),
57-
"-jar",
56+
"-cp",
5857
MCPConfig.UPDATE_JAR,
59-
"update",
58+
"org.mcphackers.mcp.Update",
6059
jarPath.toString()
6160
};
6261
Util.runCommand(cmd);

src/main/java/org/mcphackers/mcp/tools/Util.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,7 @@ public void run() {
5151

5252
public static void runCommand(String[] cmd) throws IOException {
5353
ProcessBuilder procBuilder = new ProcessBuilder(cmd);
54-
Process proc = procBuilder.start();
55-
while(proc.isAlive());
56-
proc.exitValue();
54+
procBuilder.start();
5755
}
5856

5957
public static OS getOperatingSystem() {

test.jar

-1.92 MB
Binary file not shown.

0 commit comments

Comments
 (0)