1919import java .nio .file .attribute .PosixFilePermission ;
2020import java .util .ArrayList ;
2121import java .util .Collections ;
22+ import java .util .EnumMap ;
2223import java .util .HashSet ;
2324import java .util .Iterator ;
2425import java .util .List ;
3031import java .util .zip .ZipEntry ;
3132import 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 ;
3341import org .kamranzafar .jtar .TarEntry ;
3442import org .kamranzafar .jtar .TarInputStream ;
3543
4149import com .google .gson .reflect .TypeToken ;
4250import com .google .gson .stream .JsonReader ;
4351
44- import net .minecraftforge .java_version .util .DownloadUtils ;
45- import net .minecraftforge .java_version .util .HashFunction ;
4652import net .minecraftforge .java_version .util .OS ;
4753import net .minecraftforge .java_version .util .ProcessUtils ;
4854
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 */
6065public 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 }
0 commit comments