-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathIntegrityCommand.java
93 lines (82 loc) · 3.96 KB
/
IntegrityCommand.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package me.earth.headlessmc.launcher.command;
import lombok.val;
import me.earth.headlessmc.api.command.CommandException;
import me.earth.headlessmc.api.command.CommandUtil;
import me.earth.headlessmc.launcher.Launcher;
import me.earth.headlessmc.launcher.download.AssetsDownloader;
import me.earth.headlessmc.launcher.version.Library;
import me.earth.headlessmc.launcher.version.Version;
import org.jetbrains.annotations.Nullable;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class IntegrityCommand extends AbstractVersionCommand {
public IntegrityCommand(Launcher ctx) {
super(ctx, "integrity", "Checks the integrity of the libraries of a version.");
}
@Override
public void execute(Version version, String... args) throws CommandException {
try {
int failed = 0;
int successful = 0;
int notThere = 0;
// TODO: make this a service instead
for (Library library : version.getLibraries()) {
String libPath = library.getPath(ctx.getProcessFactory().getOs());
Path path = Paths.get(ctx.getProcessFactory().getFiles().getDir("libraries") + File.separator + libPath);
if (Files.exists(path)) {
ctx.log("Checking " + libPath);
if (!ctx.getSha1Service().checkIntegrity(path, library.getSize(), library.getSha1())) {
ctx.log("Integrity check failed! Deleting " + libPath);
failed++;
continue;
}
successful++;
} else {
ctx.log("Failed to find " + libPath);
notThere++;
}
}
int[] values = { failed, successful, notThere };
checkAssets(version, values, args);
ctx.log("Integrity check finished, " + values[0] + " failed, " + values[1] + " successful and " + values[2] + " not found.");
} catch (IOException e) {
throw new CommandException("Failed to check integrity: " + e.getMessage());
}
}
private void checkAssets(Version version, int[] values, String...args) throws IOException {
if (CommandUtil.hasFlag("-assets", args)) {
ctx.log("Checking assets of version " + version.getName());
AssetsDownloader assetsDownloader = new AssetsDownloader(ctx.getCommandLine(), ctx.getDownloadService(), ctx, ctx.getMcFiles(), version.getAssetsUrl(), version.getAssets()) {
@Override
protected void downloadAsset(String progress, String name, String hash, @Nullable Long size, boolean mapToResources) throws IOException {
val firstTwo = hash.substring(0, 2);
val to = ctx.getMcFiles().getDir("assets").toPath().resolve("objects").resolve(firstTwo).resolve(hash);
Path file = getAssetsFile(name, to, hash, size);
copyToLegacy(name, file, hash, size, false);
mapToResources(name, file, mapToResources, hash, size, false);
}
@Override
protected boolean shouldCheckFileHash() {
return true;
}
@Override
protected boolean integrityCheck(String type, Path file, String hash, @Nullable Long size) throws IOException {
if (!Files.exists(file)) {
values[2]++; // notThere
return true;
} else if (super.integrityCheck(type, file, hash, size)) {
values[1]++; // successful
return true;
} else {
values[0]++; // unsuccessful
return false;
}
}
};
assetsDownloader.download();
}
}
}