1515import org .mcphackers .mcp .tasks .Task .Side ;
1616import org .mcphackers .mcp .tools .FileUtil ;
1717import org .mcphackers .mcp .tools .Util ;
18- import org .mcphackers .mcp .tools .versions .json .Artifact ;
1918import org .mcphackers .mcp .tools .versions .json .AssetIndex ;
2019import org .mcphackers .mcp .tools .versions .json .AssetIndex .Asset ;
2120import org .mcphackers .mcp .tools .versions .json .DependDownload ;
22- import org .mcphackers .mcp .tools .versions .json .Download ;
2321import org .mcphackers .mcp .tools .versions .json .Rule ;
2422import org .mcphackers .mcp .tools .versions .json .Version ;
2523
2624public class DownloadData {
2725
2826 private final Path gameDir ;
29- public long totalSize ;
30- public List <DownloadEntry > natives = new ArrayList <>();
31- protected List <DownloadEntry > downloadQueue = new ArrayList <>();
27+ public int totalSize ;
28+ protected List <Download > downloadQueue = new ArrayList <>();
3229 protected AssetIndex assets ;
3330
3431 public DownloadData (MCP mcp , Version version ) {
@@ -37,28 +34,13 @@ public DownloadData(MCP mcp, Version version) {
3734
3835 public DownloadData (Path libraries , Path gameDir , Path client , Path server , Version version ) {
3936 this .gameDir = gameDir ;
40- if (version .downloads .client != null && client != null ) {
41- queueDownload (version .downloads .client , client , true ); // TODO may want to make verify flag togglable
42- }
43- if (version .downloads .server != null && server != null ) {
44- queueDownload (version .downloads .server , server , true );
45- }
37+ queueDownload (version .downloads .artifacts .get ("client" ), client );
38+ queueDownload (version .downloads .artifacts .get ("server" ), server );
4639 for (DependDownload dependencyDownload : version .libraries ) {
4740 if (Rule .apply (dependencyDownload .rules )) {
48- if (dependencyDownload .downloads != null && dependencyDownload .downloads .artifact != null ) {
49- queueDownload (dependencyDownload .downloads .artifact , libraries .resolve (dependencyDownload .downloads .artifact .path ), true );
50- }
51-
52- if (dependencyDownload .downloads != null && dependencyDownload .downloads .classifiers != null ) {
53- Artifact artifact = dependencyDownload .downloads .classifiers .getNatives ();
54- if (artifact != null ) {
55- natives .add (queueDownload (artifact , libraries .resolve (artifact .path ), true ));
56- }
57- artifact = dependencyDownload .downloads .classifiers .sources ;
58- if (artifact != null ) {
59- queueDownload (artifact , libraries .resolve (artifact .path ), true );
60- }
61- }
41+ queueDownload (dependencyDownload .getDownload (null ), libraries );
42+ queueDownload (dependencyDownload .getDownload (dependencyDownload .getNatives ()), libraries );
43+ queueDownload (dependencyDownload .getDownload ("sources" ), libraries );
6244 }
6345 }
6446 try {
@@ -79,8 +61,7 @@ public static List<String> getLibraries(Version version) {
7961 List <String > retList = new ArrayList <>();
8062 for (DependDownload dependencyDownload : version .libraries ) {
8163 if (Rule .apply (dependencyDownload .rules )) {
82- String [] path = dependencyDownload .name .split (":" );
83- String lib = path [0 ].replace ('.' , '/' ) + "/" + path [1 ] + "/" + path [2 ] + "/" + path [1 ] + "-" + path [2 ];
64+ String lib = dependencyDownload .getArtifactPath (null );
8465 retList .add (lib );
8566 }
8667 }
@@ -91,8 +72,10 @@ public static List<Path> getLibraries(Path libDir, Version version) {
9172 List <Path > retList = new ArrayList <>();
9273 for (DependDownload dependencyDownload : version .libraries ) {
9374 if (Rule .apply (dependencyDownload .rules )) {
94- String [] path = dependencyDownload .name .split (":" );
95- String lib = path [0 ].replace ('.' , '/' ) + "/" + path [1 ] + "/" + path [2 ] + "/" + path [1 ] + "-" + path [2 ] + ".jar" ;
75+ String lib = dependencyDownload .getArtifactPath (null );
76+ if (lib == null ) {
77+ continue ;
78+ }
9679 retList .add ((libDir .resolve (lib )));
9780 }
9881 }
@@ -103,11 +86,13 @@ public static List<Path> getNatives(Path libDir, Version version) {
10386 List <Path > retList = new ArrayList <>();
10487 for (DependDownload dependencyDownload : version .libraries ) {
10588 if (Rule .apply (dependencyDownload .rules )) {
106- if (dependencyDownload .downloads != null && dependencyDownload .downloads .classifiers != null ) {
107- Artifact artifact = dependencyDownload .downloads .classifiers .getNatives ();
108- if (artifact != null ) {
109- retList .add (libDir .resolve (artifact .path ));
89+ String natives = dependencyDownload .getNatives ();
90+ if (natives != null ) {
91+ String lib = dependencyDownload .getArtifactPath (natives );
92+ if (lib == null ) {
93+ continue ;
11094 }
95+ retList .add (libDir .resolve (lib ));
11196 }
11297 }
11398 }
@@ -119,57 +104,43 @@ public void setAssets(AssetIndex assets) {
119104 return ;
120105 }
121106 this .assets = assets ;
107+ Path dir = gameDir .resolve (assets .map_to_resources ? "resources/" : "assets/objects/" );
122108 for (Entry <String , Asset > entry : assets .objects .entrySet ()) {
123- totalSize += entry .getValue (). size ( );
109+ queueDownload ( entry .getValue (), dir );
124110 }
125111 }
126112
127- public DownloadEntry queueDownload (Download dl , Path path , boolean verify ) {
113+ public void queueDownload (IDownload dl , Path baseDir ) {
128114 if (dl == null ) {
129- return null ;
115+ return ;
130116 }
131- DownloadEntry entry = new DownloadEntry (dl , path , verify );
132- totalSize += dl .size ();
133- downloadQueue .add (entry );
134- return entry ;
117+ totalSize += dl .downloadSize ();
118+ downloadQueue .add (new Download (dl , baseDir ));
135119 }
136120
137121 public void performDownload (DownloadListener listener ) throws IOException {
138- for (DownloadEntry dl : downloadQueue ) {
139- Path file = dl .path ;
140- Download dlObj = dl .dlObject ;
141- listener .notify (dlObj , totalSize );
142- if (!Files .exists (file ) || (dl .verifySHA1 && !dlObj .sha1 .equals (Util .getSHA1 (file )))) {
122+ for (Download dl : downloadQueue ) {
123+ IDownload download = dl .download ;
124+ Path baseDir = dl .dir ;
125+ String path = download .downloadPath ();
126+ // if downloadPath is null then baseDir is the location of downloaded file.
127+ Path file = path == null ? baseDir : baseDir .resolve (path );
128+ listener .notify (dl .download , totalSize );
129+ if (!Files .exists (file ) || (download .verify () && !download .downloadHash ().equals (Util .getSHA1 (file )))) {
143130 Path parent = file .getParent ();
144131 if (parent != null ) Files .createDirectories (parent );
145- FileUtil .downloadFile (dlObj .url , file );
146- }
147- }
148- if (assets != null ) {
149- for (Entry <String , Asset > entry : assets .objects .entrySet ()) {
150- Asset asset = entry .getValue ();
151- String hash = asset .hash .substring (0 , 2 ) + "/" + asset .hash ;
152- String filename = assets .map_to_resources ? "resources/" + entry .getKey () : "assets/objects/" + hash ;
153- Path file = gameDir .resolve (filename );
154- listener .notify (asset , totalSize );
155- if (!Files .exists (file )) {
156- Path parent = file .getParent ();
157- if (parent != null ) Files .createDirectories (parent );
158- FileUtil .downloadFile ("https://resources.download.minecraft.net/" + hash , file );
159- }
132+ FileUtil .downloadFile (download .downloadURL (), file );
160133 }
161134 }
162135 }
163136
164- public static class DownloadEntry {
165- public final Download dlObject ;
166- public final Path path ;
167- public final boolean verifySHA1 ;
137+ private static class Download {
138+ IDownload download ;
139+ Path dir ;
168140
169- public DownloadEntry (Download dl , Path filePath , boolean verify ) {
170- path = filePath ;
171- dlObject = dl ;
172- verifySHA1 = verify ;
141+ public Download (IDownload dl , Path path ) {
142+ download = dl ;
143+ dir = path ;
173144 }
174145 }
175146}
0 commit comments