15
15
import org .mcphackers .mcp .tasks .Task .Side ;
16
16
import org .mcphackers .mcp .tools .FileUtil ;
17
17
import org .mcphackers .mcp .tools .Util ;
18
- import org .mcphackers .mcp .tools .versions .json .Artifact ;
19
18
import org .mcphackers .mcp .tools .versions .json .AssetIndex ;
20
19
import org .mcphackers .mcp .tools .versions .json .AssetIndex .Asset ;
21
20
import org .mcphackers .mcp .tools .versions .json .DependDownload ;
22
- import org .mcphackers .mcp .tools .versions .json .Download ;
23
21
import org .mcphackers .mcp .tools .versions .json .Rule ;
24
22
import org .mcphackers .mcp .tools .versions .json .Version ;
25
23
26
24
public class DownloadData {
27
25
28
26
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 <>();
32
29
protected AssetIndex assets ;
33
30
34
31
public DownloadData (MCP mcp , Version version ) {
@@ -37,28 +34,13 @@ public DownloadData(MCP mcp, Version version) {
37
34
38
35
public DownloadData (Path libraries , Path gameDir , Path client , Path server , Version version ) {
39
36
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 );
46
39
for (DependDownload dependencyDownload : version .libraries ) {
47
40
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 );
62
44
}
63
45
}
64
46
try {
@@ -79,8 +61,7 @@ public static List<String> getLibraries(Version version) {
79
61
List <String > retList = new ArrayList <>();
80
62
for (DependDownload dependencyDownload : version .libraries ) {
81
63
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 );
84
65
retList .add (lib );
85
66
}
86
67
}
@@ -91,8 +72,10 @@ public static List<Path> getLibraries(Path libDir, Version version) {
91
72
List <Path > retList = new ArrayList <>();
92
73
for (DependDownload dependencyDownload : version .libraries ) {
93
74
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
+ }
96
79
retList .add ((libDir .resolve (lib )));
97
80
}
98
81
}
@@ -103,11 +86,13 @@ public static List<Path> getNatives(Path libDir, Version version) {
103
86
List <Path > retList = new ArrayList <>();
104
87
for (DependDownload dependencyDownload : version .libraries ) {
105
88
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 ;
110
94
}
95
+ retList .add (libDir .resolve (lib ));
111
96
}
112
97
}
113
98
}
@@ -119,57 +104,43 @@ public void setAssets(AssetIndex assets) {
119
104
return ;
120
105
}
121
106
this .assets = assets ;
107
+ Path dir = gameDir .resolve (assets .map_to_resources ? "resources/" : "assets/objects/" );
122
108
for (Entry <String , Asset > entry : assets .objects .entrySet ()) {
123
- totalSize += entry .getValue (). size ( );
109
+ queueDownload ( entry .getValue (), dir );
124
110
}
125
111
}
126
112
127
- public DownloadEntry queueDownload (Download dl , Path path , boolean verify ) {
113
+ public void queueDownload (IDownload dl , Path baseDir ) {
128
114
if (dl == null ) {
129
- return null ;
115
+ return ;
130
116
}
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 ));
135
119
}
136
120
137
121
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 )))) {
143
130
Path parent = file .getParent ();
144
131
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 );
160
133
}
161
134
}
162
135
}
163
136
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 ;
168
140
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 ;
173
144
}
174
145
}
175
146
}
0 commit comments