Skip to content

Commit c8fade6

Browse files
JonathingLexManos
authored andcommitted
Add offline toggle to prevent downloads
1 parent dc2fb57 commit c8fade6

File tree

4 files changed

+38
-4
lines changed

4 files changed

+38
-4
lines changed

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

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,24 @@ public class Disco {
6363

6464
private final File cache;
6565
private final String provider;
66+
private final boolean offline;
6667

6768
public Disco(File cache) {
6869
this(cache, "https://api.foojay.io/disco/v3.0");
6970
}
7071

72+
public Disco(File cache, boolean offline) {
73+
this(cache, "https://api.foojay.io/disco/v3.0", offline);
74+
}
75+
7176
public Disco(File cache, String provider) {
77+
this(cache, provider, false);
78+
}
79+
80+
public Disco(File cache, String provider, boolean offline) {
7281
this.cache = cache;
7382
this.provider = provider;
83+
this.offline = offline;
7484
}
7585

7686
protected void debug(String message) {
@@ -87,6 +97,9 @@ public List<Package> getPackages() {
8797
if (ret != null)
8898
return ret;
8999

100+
if (offline)
101+
return null;
102+
90103
String url = provider + "/packages/?"
91104
+ "&package_type=jdk" // JDK has everything, could pull just the JRE but who cares.
92105
+ "&directly_downloadable=true" // This doesn't actually seem to do anything but it's in the spec...
@@ -162,6 +175,9 @@ public PackageInfo getInfo(Package pkg) {
162175
if (ret != null && ret.info != null)
163176
return ret.info;
164177

178+
if (offline)
179+
return null;
180+
165181
//debug("Downloading package info " + pkg.id);
166182
String url = provider + "/ids/" + pkg.id;
167183
String data = DownloadUtils.downloadString(url);
@@ -198,7 +214,7 @@ public File download(Package pkg) {
198214
checksums.put(func, info.checksum);
199215
else
200216
debug("Unknown Checksum " + info.checksum_type + ": " + info.checksum);
201-
} else if (info.checksum_uri != null) {
217+
} else if (info.checksum_uri != null && !offline) {
202218
String raw = DownloadUtils.downloadString(info.checksum_uri);
203219
if (raw != null) {
204220
String checksum = raw.split(" ")[0];
@@ -217,7 +233,10 @@ public File download(Package pkg) {
217233
File archive = new File(cache, pkg.filename);
218234
if (!archive.exists()) {
219235
if (download == null) {
220-
error("Failed to find download link for " + pkg.filename + " (" + pkg.id + ")");
236+
if (offline)
237+
error("Offline mode, can't download " + pkg.filename + " (" + pkg.id + ")");
238+
else
239+
error("Failed to find download link for " + pkg.filename + " (" + pkg.id + ")");
221240
return null;
222241
}
223242
debug("Downloading " + download);

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,15 @@
3131
*/
3232
public class DiscoLocator extends JavaHomeLocator {
3333
private final File cache;
34+
private final boolean offline;
3435

3536
public DiscoLocator(File cache) {
37+
this(cache, false);
38+
}
39+
40+
public DiscoLocator(File cache, boolean offline) {
3641
this.cache = cache;
42+
this.offline = offline;
3743
}
3844

3945
@Override
@@ -77,7 +83,7 @@ private List<IJavaInstall> findInternal(int version) {
7783
@Override
7884
public IJavaInstall provision(int version) {
7985
log("Locators failed to find any suitable installs, attempting Disco download");
80-
Disco disco = new Disco(cache) { // TODO: [DISCO][Logging] Add a proper logging handler sometime
86+
Disco disco = new Disco(cache, offline) { // TODO: [DISCO][Logging] Add a proper logging handler sometime
8187
@Override
8288
protected void debug(String message) {
8389
DiscoLocator.this.log(message);

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import java.util.Iterator;
1212
import java.util.List;
1313
import java.util.Set;
14+
15+
import joptsimple.AbstractOptionSpec;
1416
import joptsimple.OptionParser;
1517
import joptsimple.OptionSet;
1618
import joptsimple.OptionSpec;
@@ -30,6 +32,9 @@ public static void main(String[] args) throws Exception {
3032
"Directory to store data needed for this program")
3133
.withRequiredArg().ofType(File.class).defaultsTo(new File("cache"));
3234

35+
AbstractOptionSpec<Void> offlineO = parser.accepts("offline",
36+
"Do not attempt to download any JDKs, only use the cache");
37+
3338
OptionSpec<Integer> versionO = parser.accepts("version",
3439
"Major version of java to try and locate")
3540
.withOptionalArg().ofType(Integer.class);
@@ -43,7 +48,7 @@ public static void main(String[] args) throws Exception {
4348
return;
4449
}
4550
File cache = options.valueOf(cacheO);
46-
DiscoLocator disco = new DiscoLocator(cache);
51+
DiscoLocator disco = new DiscoLocator(cache, options.has(offlineO));
4752

4853
List<IJavaLocator> locators = new ArrayList<>();
4954
locators.add(new JavaHomeLocator());

src/main/java/net/minecraftforge/java_version/api/IJavaLocator.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,8 @@ static IJavaLocator home() {
6464
static IJavaLocator disco(File cache) {
6565
return new DiscoLocator(cache);
6666
}
67+
68+
static IJavaLocator disco(File cache, boolean offline) {
69+
return new DiscoLocator(cache, offline);
70+
}
6771
}

0 commit comments

Comments
 (0)