Skip to content

Commit c601e73

Browse files
JonathingLexManos
authored andcommitted
Use util libraries
1 parent b78db55 commit c601e73

File tree

8 files changed

+71
-325
lines changed

8 files changed

+71
-325
lines changed

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ dependencies {
5050
implementation libs.jopt
5151
implementation libs.gson
5252
implementation libs.jtar
53+
implementation libs.bundles.utils
5354
implementation javaProbeJar.outputs.files
5455
}
5556

settings.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ plugins {
44

55
dependencyResolutionManagement {
66
repositories {
7+
maven { url = 'https://maven.minecraftforge.net/' }
78
mavenCentral()
89
}
910

src/main/java/net/minecraftforge/java_version/Disco.java

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.nio.file.attribute.PosixFilePermission;
2020
import java.util.ArrayList;
2121
import java.util.Collections;
22+
import java.util.EnumMap;
2223
import java.util.HashSet;
2324
import java.util.Iterator;
2425
import java.util.List;
@@ -30,6 +31,13 @@
3031
import java.util.zip.ZipEntry;
3132
import java.util.zip.ZipFile;
3233

34+
import com.google.gson.GsonBuilder;
35+
import com.google.gson.TypeAdapter;
36+
import com.google.gson.internal.bind.TypeAdapters;
37+
import com.google.gson.stream.JsonWriter;
38+
import net.minecraftforge.util.download.DownloadUtils;
39+
import net.minecraftforge.util.hash.HashFunction;
40+
import net.minecraftforge.util.logging.Log;
3341
import org.kamranzafar.jtar.TarEntry;
3442
import org.kamranzafar.jtar.TarInputStream;
3543

@@ -41,8 +49,6 @@
4149
import com.google.gson.reflect.TypeToken;
4250
import com.google.gson.stream.JsonReader;
4351

44-
import net.minecraftforge.java_version.util.DownloadUtils;
45-
import net.minecraftforge.java_version.util.HashFunction;
4652
import net.minecraftforge.java_version.util.OS;
4753
import net.minecraftforge.java_version.util.ProcessUtils;
4854

@@ -55,11 +61,30 @@
5561
* <p>
5662
*
5763
* TODO: [DISCO][Threads] Locking files for multiple processes accessing the same cache directory
58-
* TODO: [DISCO][Log] Proper logging API instead of System.out?
5964
*/
6065
public class Disco {
6166
private static final int CACHE_TIMEOUT = 1000 * 60 * 60 * 12; // 12 hours
62-
private static final Gson GSON = DownloadUtils.GSON;
67+
68+
// A GSO parser that prints good looking output, and treats empty strings as nulls
69+
private static final Gson GSON = new GsonBuilder()
70+
.setLenient()
71+
.setPrettyPrinting()
72+
.registerTypeAdapter(String.class, new TypeAdapter<String>() {
73+
@Override
74+
public void write(final JsonWriter out, final String value) throws IOException {
75+
if (value == null || value.isEmpty())
76+
out.nullValue();
77+
else
78+
TypeAdapters.STRING.write(out, value);
79+
}
80+
81+
@Override
82+
public String read(final JsonReader in) throws IOException {
83+
String value = TypeAdapters.STRING.read(in);
84+
return value != null && value.isEmpty() ? null : value; // Read empty strings as null
85+
}
86+
})
87+
.create();
6388

6489
private final File cache;
6590
private final String provider;
@@ -84,11 +109,11 @@ public Disco(File cache, String provider, boolean offline) {
84109
}
85110

86111
protected void debug(String message) {
87-
System.out.println(message);
112+
Log.debug(message);
88113
}
89114

90115
protected void error(String message) {
91-
System.out.println(message);
116+
Log.error(message);
92117
}
93118

94119
public List<Package> getPackages() {
@@ -107,7 +132,7 @@ public List<Package> getPackages() {
107132
;
108133

109134
debug("Downloading package list");
110-
String data = DownloadUtils.downloadString(url);
135+
String data = DownloadUtils.tryDownloadString(true, url);
111136
if (data == null)
112137
return null;
113138

@@ -180,7 +205,7 @@ public PackageInfo getInfo(Package pkg) {
180205

181206
//debug("Downloading package info " + pkg.id);
182207
String url = provider + "/ids/" + pkg.id;
183-
String data = DownloadUtils.downloadString(url);
208+
String data = DownloadUtils.tryDownloadString(true, url);
184209
if (data == null)
185210
return null;
186211

@@ -201,7 +226,7 @@ public PackageInfo getInfo(Package pkg) {
201226
public File download(Package pkg) {
202227
PackageInfo info = getInfo(pkg);
203228

204-
Map<HashFunction, String> checksums = new TreeMap<>();
229+
Map<HashFunction, String> checksums = new EnumMap<>(HashFunction.class);
205230
String download = pkg.links.pkg_download_redirect;
206231

207232
//debug("Downloading " + pkg.filename);
@@ -215,7 +240,7 @@ public File download(Package pkg) {
215240
else
216241
debug("Unknown Checksum " + info.checksum_type + ": " + info.checksum);
217242
} else if (info.checksum_uri != null && !offline) {
218-
String raw = DownloadUtils.downloadString(info.checksum_uri);
243+
String raw = DownloadUtils.tryDownloadString(true, info.checksum_uri);
219244
if (raw != null) {
220245
String checksum = raw.split(" ")[0];
221246
HashFunction func = HashFunction.findByHash(checksum);
@@ -240,7 +265,7 @@ public File download(Package pkg) {
240265
return null;
241266
}
242267
debug("Downloading " + download);
243-
if (!DownloadUtils.downloadFile(archive, download)) {
268+
if (!DownloadUtils.tryDownloadFile(true, archive, download)) {
244269
error("Failed to download " + pkg.filename + " from " + download);
245270
return null;
246271
}
@@ -746,7 +771,7 @@ private static final LibC getCurrent() {
746771
return MUSL;
747772
}
748773
} else {
749-
System.out.println("Failed to run `getconf GNU_LIBC_VERSION`: " + getconf.lines.get(0));
774+
Log.error("Failed to run `getconf GNU_LIBC_VERSION`: " + getconf.lines.get(0));
750775
}
751776

752777
ProcessUtils.Result ldd = ProcessUtils.runCommand("ldd", "--version");
@@ -756,7 +781,7 @@ private static final LibC getCurrent() {
756781
return MUSL;
757782
}
758783
} else {
759-
System.out.println("Failed to run `ldd --version`: " + ldd.lines.get(0));
784+
Log.error("Failed to run `ldd --version`: " + ldd.lines.get(0));
760785
}
761786
return GLIBC;
762787
}

src/main/java/net/minecraftforge/java_version/DiscoMain.java

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import joptsimple.util.EnumConverter;
1515
import net.minecraftforge.java_version.util.OS;
1616
import net.minecraftforge.java_version.util.ProcessUtils;
17+
import net.minecraftforge.util.logging.Log;
1718

1819
public class DiscoMain {
1920
public static void main(String[] args) throws Exception {
@@ -48,7 +49,7 @@ public static void main(String[] args) throws Exception {
4849

4950
boolean success = true;
5051
if (options.has(helpO)) {
51-
parser.printHelpOn(System.out);
52+
parser.printHelpOn(Log.INFO);
5253
} else if (options.has(downloadJdkO)) {
5354
success = downloadJdk(
5455
options.hasArgument(javeVersionO) ? javeVersionO.value(options) : -1,
@@ -59,7 +60,7 @@ public static void main(String[] args) throws Exception {
5960
cache
6061
);
6162
} else {
62-
parser.printHelpOn(System.out);
63+
parser.printHelpOn(Log.INFO);
6364
}
6465

6566
if (!success)
@@ -70,48 +71,44 @@ private static List<String> l(String...strings) {
7071
return Arrays.asList(strings);
7172
}
7273

73-
private static void log(String message) {
74-
System.out.println(message);
75-
}
76-
7774
private static boolean downloadJdk(
7875
int javaVersion, Disco.Arch arch, OS os, Disco.Distro distro,
7976
boolean auto, File cache
8077
) {
8178

8279
if (arch == Disco.Arch.UNKNOWN) {
8380
arch = Disco.Arch.X64;
84-
log("Unknown Architecture (" + System.getProperty("os.arch") + ")" +
81+
Log.warn("Unknown Architecture (" + System.getProperty("os.arch") + ")" +
8582
" Defaulting to " + arch.name() + "." +
8683
" Use --arch to specify an alternative.");
8784
}
8885

8986
if (os == OS.UNKNOWN) {
9087
os = OS.LINUX;
91-
log("Unknown Operating System (" + System.getProperty("os.name") + ")" +
88+
Log.warn("Unknown Operating System (" + System.getProperty("os.name") + ")" +
9289
" Defaulting to " + os.name() + "." +
9390
" Use --os to specify an alternative.");
9491
}
9592

96-
log("Downloading JDK:");
97-
log(" Version: " + (javaVersion == -1 ? "latest" : javaVersion));
98-
log(" Arch: " + (arch == null ? "null" : arch .name()));
99-
log(" OS: " + (os == null ? "null" : os .name()));
100-
log(" Distro: " + (distro == null ? "null" : distro.name()));
101-
log(" Cache: " + cache.getAbsolutePath());
93+
Log.info("Downloading JDK:");
94+
Log.info(" Version: " + (javaVersion == -1 ? "latest" : javaVersion));
95+
Log.info(" Arch: " + (arch == null ? "null" : arch .name()));
96+
Log.info(" OS: " + (os == null ? "null" : os .name()));
97+
Log.info(" Distro: " + (distro == null ? "null" : distro.name()));
98+
Log.info(" Cache: " + cache.getAbsolutePath());
10299
Disco disco = new Disco(new File(cache, "jdks"));
103100

104101
List<Disco.Package> jdks = disco.getPackages(javaVersion, os, distro, arch);
105102
Disco.Package pkg = null;
106103
if (jdks == null || jdks.isEmpty()) {
107-
log("Failed to find any download, try specifying a different java version or distro");
104+
Log.error("Failed to find any download, try specifying a different java version or distro");
108105
return false;
109106
} else if (jdks.size() == 1 || auto) {
110107
pkg = jdks.get(0);
111108
} else if (jdks.size() > 1) {
112109
for (int x = 0; x < jdks.size(); x++) {
113110
Disco.Package jdk = jdks.get(x);
114-
log(String.format("[%2d] %s: %s", x+1, jdk.distribution, jdk.filename));
111+
Log.info(String.format("[%2d] %s: %s", x+1, jdk.distribution, jdk.filename));
115112
//log(Disco.GSON.toJson(jdk));
116113
}
117114

@@ -124,17 +121,17 @@ private static boolean downloadJdk(
124121
try {
125122
selected = Integer.parseInt(line);
126123
} catch (NumberFormatException e) {
127-
System.out.println("Invalid selection \"" + line + "\" is not a number.");
124+
Log.error("Invalid selection \"" + line + "\" is not a number.");
128125
}
129126

130127
if (selected <= 0 || selected > jdks.size()) {
131-
log("Invalid selection, must be between 1 and " + jdks.size());
128+
Log.error("Invalid selection, must be between 1 and " + jdks.size());
132129
return false;
133130
} else
134131
pkg = jdks.get(selected - 1);
135132
}
136133

137-
log("");
134+
Log.info();
138135

139136
File java_home = disco.extract(pkg);
140137

@@ -143,9 +140,9 @@ private static boolean downloadJdk(
143140

144141
ProcessUtils.Result result = ProcessUtils.testJdk(java_home);
145142
if (result.exitCode != 0) {
146-
log("Fialed to run extracted java:");
143+
Log.error("Failed to run extracted java:");
147144
for (String line : result.lines)
148-
log(line);
145+
Log.error(line);
149146

150147
return false;
151148
}

src/main/java/net/minecraftforge/java_version/Main.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import net.minecraftforge.java_version.api.IJavaInstall;
2222
import net.minecraftforge.java_version.api.IJavaLocator;
2323
import net.minecraftforge.java_version.util.OS;
24+
import net.minecraftforge.util.logging.Log;
2425

2526
public class Main {
2627
public static void main(String[] args) throws Exception {
@@ -44,7 +45,7 @@ public static void main(String[] args) throws Exception {
4445

4546
OptionSet options = parser.parse(args);
4647
if (options.has(helpO)) {
47-
parser.printHelpOn(System.out);
48+
parser.printHelpOn(Log.INFO);
4849
return;
4950
}
5051
File cache = options.valueOf(cacheO);
@@ -93,13 +94,13 @@ private static void findSpecificVersion(List<IJavaLocator> locators, DiscoLocato
9394
String home = result.getAbsolutePath();
9495
if (!home.endsWith(File.separator))
9596
home += File.separatorChar;
96-
System.out.println(home);
97+
Log.info(home);
9798
} else {
98-
System.out.println("Failed to find sutable java for version " + version);
99+
Log.error("Failed to find sutable java for version " + version);
99100
for (IJavaLocator locator : locators) {
100-
System.out.println("Locator: " + locator.getClass().getSimpleName());
101+
Log.error("Locator: " + locator.getClass().getSimpleName());
101102
for (String line : locator.logOutput()) {
102-
System.out.println(" " + line);
103+
Log.error(" " + line);
103104
}
104105
}
105106
System.exit(1);
@@ -125,10 +126,10 @@ private static void listAllJavaInstalls(List<IJavaLocator> locators) {
125126
Collections.sort(installs);
126127

127128
for (IJavaInstall install : installs) {
128-
System.out.println(install.home().getAbsolutePath());
129-
System.out.println(" Vendor: " + install.vendor());
130-
System.out.println(" Type: " + (install.isJdk() ? "JDK" : "JRE"));
131-
System.out.println(" Version: " + install.version());
129+
Log.info(install.home().getAbsolutePath());
130+
Log.info(" Vendor: " + install.vendor());
131+
Log.info(" Type: " + (install.isJdk() ? "JDK" : "JRE"));
132+
Log.info(" Version: " + install.version());
132133
}
133134

134135
}

0 commit comments

Comments
 (0)