Skip to content

Commit 8f0ab10

Browse files
committed
mr & cf
1 parent 5e621ce commit 8f0ab10

6 files changed

Lines changed: 28 additions & 147 deletions

File tree

HMCLCore/src/main/java/org/jackhuang/hmcl/modpack/curse/CurseMetaMod.java

Lines changed: 9 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -26,43 +26,14 @@
2626
* https://addons-ecs.forgesvc.net/api/v2/addon/<projectID>/file/<fileID>
2727
*/
2828
@Immutable
29-
public final class CurseMetaMod {
30-
@SerializedName(value = "Id", alternate = "id")
31-
private final int id;
32-
33-
@SerializedName(value = "FileName", alternate = "fileName")
34-
private final String fileName;
35-
36-
@SerializedName(value = "FileNameOnDisk")
37-
private final String fileNameOnDisk;
38-
39-
@SerializedName(value = "DownloadURL", alternate = "downloadUrl")
40-
private final String downloadURL;
41-
42-
public CurseMetaMod() {
43-
this(0, "", "", "");
44-
}
45-
46-
public CurseMetaMod(int id, String fileName, String fileNameOnDisk, String downloadURL) {
47-
this.id = id;
48-
this.fileName = fileName;
49-
this.fileNameOnDisk = fileNameOnDisk;
50-
this.downloadURL = downloadURL;
51-
}
52-
53-
public int getId() {
54-
return id;
55-
}
56-
57-
public String getFileName() {
58-
return fileName;
59-
}
60-
61-
public String getFileNameOnDisk() {
62-
return fileNameOnDisk;
63-
}
64-
65-
public String getDownloadURL() {
66-
return downloadURL;
29+
public record CurseMetaMod(@SerializedName(value = "Id", alternate = "id") int id,
30+
@SerializedName(value = "FileName", alternate = "fileName") String fileName,
31+
@SerializedName(value = "FileNameOnDisk") String fileNameOnDisk,
32+
@SerializedName(value = "DownloadURL", alternate = "downloadUrl") String downloadURL) {
33+
34+
public CurseMetaMod {
35+
if (fileName == null) fileName = "";
36+
if (fileNameOnDisk == null) fileNameOnDisk = "";
37+
if (downloadURL == null) downloadURL = "";
6738
}
6839
}

HMCLCore/src/main/java/org/jackhuang/hmcl/modpack/mcbbs/McbbsModpackCompletionTask.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ public CompletableFuture<Void> getFuture(TaskCompletableFuture executor) {
204204
try {
205205
String result = NetworkUtils.doGet(String.format("https://cursemeta.dries007.net/%d/%d.json", file.getProjectID(), file.getFileID()));
206206
CurseMetaMod mod = JsonUtils.fromNonNullJson(result, CurseMetaMod.class);
207-
return file.withFileName(mod.getFileNameOnDisk()).withURL(mod.getDownloadURL());
207+
return file.withFileName(mod.fileNameOnDisk()).withURL(mod.downloadURL());
208208
} catch (FileNotFoundException fof) {
209209
LOG.warning("Could not query cursemeta for deleted mods: " + file.getUrl(), fof);
210210
notFound.set(true);
@@ -213,7 +213,7 @@ public CompletableFuture<Void> getFuture(TaskCompletableFuture executor) {
213213
try {
214214
String result = NetworkUtils.doGet(String.format("https://addons-ecs.forgesvc.net/api/v2/addon/%d/file/%d", file.getProjectID(), file.getFileID()));
215215
CurseMetaMod mod = JsonUtils.fromNonNullJson(result, CurseMetaMod.class);
216-
return file.withFileName(mod.getFileName()).withURL(mod.getDownloadURL());
216+
return file.withFileName(mod.fileName()).withURL(mod.downloadURL());
217217
} catch (FileNotFoundException fof) {
218218
LOG.warning("Could not query forgesvc for deleted mods: " + file.getUrl(), fof);
219219
notFound.set(true);

HMCLCore/src/main/java/org/jackhuang/hmcl/modpack/modrinth/ModrinthCompletionTask.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,23 +105,23 @@ public void execute() throws Exception {
105105
Path runDirectory = FileUtils.toAbsolute(repository.getRunDirectory(version));
106106
Path modsDirectory = runDirectory.resolve("mods");
107107

108-
for (ModrinthManifest.File file : manifest.getFiles()) {
109-
if (file.getEnv() != null && file.getEnv().getOrDefault("client", "required").equals("unsupported"))
108+
for (ModrinthManifest.File file : manifest.files()) {
109+
if (file.env() != null && file.env().getOrDefault("client", "required").equals("unsupported"))
110110
continue;
111-
if (file.getDownloads().isEmpty())
111+
if (file.downloads().isEmpty())
112112
continue;
113113

114-
Path filePath = runDirectory.resolve(file.getPath()).toAbsolutePath().normalize();
114+
Path filePath = runDirectory.resolve(file.path()).toAbsolutePath().normalize();
115115
if (!filePath.startsWith(runDirectory))
116-
throw new IOException("Unsecure path: " + file.getPath());
116+
throw new IOException("Unsecure path: " + file.path());
117117

118118
if (Files.exists(filePath))
119119
continue;
120120
if (modsDirectory.equals(filePath.getParent()) && this.modManager.hasSimpleMod(FileUtils.getName(filePath)))
121121
continue;
122122

123123
var task = new FileDownloadTask(
124-
dependency.getDownloadProvider().injectURLsWithCandidates(file.getDownloads()),
124+
dependency.getDownloadProvider().injectURLsWithCandidates(file.downloads()),
125125
filePath);
126126
task.setCacheRepository(dependency.getCacheRepository());
127127
task.setCaching(true);

HMCLCore/src/main/java/org/jackhuang/hmcl/modpack/modrinth/ModrinthInstallTask.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public ModrinthInstallTask(DefaultDependencyManager dependencyManager, Path zipF
6868
throw new IllegalArgumentException("Version " + name + " already exists.");
6969

7070
GameBuilder builder = dependencyManager.gameBuilder().name(name).gameVersion(manifest.getGameVersion());
71-
for (Map.Entry<String, String> modLoader : manifest.getDependencies().entrySet()) {
71+
for (Map.Entry<String, String> modLoader : manifest.dependencies().entrySet()) {
7272
switch (modLoader.getKey()) {
7373
case "minecraft":
7474
break;
@@ -115,7 +115,7 @@ public ModrinthInstallTask(DefaultDependencyManager dependencyManager, Path zipF
115115
this.config = config;
116116
List<String> subDirectories = Arrays.asList("/client-overrides", "/overrides");
117117
dependents.add(new ModpackInstallTask<>(zipFile, run, modpack.getEncoding(), subDirectories, any -> true, config).withStage("hmcl.modpack"));
118-
dependents.add(new MinecraftInstanceTask<>(zipFile, modpack.getEncoding(), subDirectories, manifest, ModrinthModpackProvider.INSTANCE, manifest.getName(), manifest.getVersionId(), repository.getModpackConfiguration(name)).withStage("hmcl.modpack"));
118+
dependents.add(new MinecraftInstanceTask<>(zipFile, modpack.getEncoding(), subDirectories, manifest, ModrinthModpackProvider.INSTANCE, manifest.name(), manifest.versionId(), repository.getModpackConfiguration(name)).withStage("hmcl.modpack"));
119119

120120
URI iconUri = NetworkUtils.toURIOrNull(iconUrl);
121121
if (iconUri != null) {
@@ -143,10 +143,10 @@ public Collection<Task<?>> getDependencies() {
143143
public void execute() throws Exception {
144144
if (config != null) {
145145
// For update, remove mods not listed in new manifest
146-
for (ModrinthManifest.File oldManifestFile : config.getManifest().getFiles()) {
147-
Path oldFile = run.resolve(oldManifestFile.getPath());
146+
for (ModrinthManifest.File oldManifestFile : config.getManifest().files()) {
147+
Path oldFile = run.resolve(oldManifestFile.path());
148148
if (!Files.exists(oldFile)) continue;
149-
if (manifest.getFiles().stream().noneMatch(oldManifestFile::equals)) {
149+
if (manifest.files().stream().noneMatch(oldManifestFile::equals)) {
150150
Files.deleteIfExists(oldFile);
151151
}
152152
}

HMCLCore/src/main/java/org/jackhuang/hmcl/modpack/modrinth/ModrinthManifest.java

Lines changed: 5 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -26,54 +26,12 @@
2626

2727
import java.util.List;
2828
import java.util.Map;
29-
import java.util.Objects;
3029

31-
public class ModrinthManifest implements ModpackManifest, Validation {
30+
public record ModrinthManifest(String game, int formatVersion, String versionId, String name, @Nullable String summary,
31+
List<File> files, Map<String, String> dependencies) implements ModpackManifest, Validation {
3232

33-
private final String game;
34-
private final int formatVersion;
35-
private final String versionId;
36-
private final String name;
37-
private final @Nullable String summary;
38-
private final List<File> files;
39-
private final Map<String, String> dependencies;
40-
41-
public ModrinthManifest(String game, int formatVersion, String versionId, String name, @Nullable String summary, List<File> files, Map<String, String> dependencies) {
42-
this.game = game;
43-
this.formatVersion = formatVersion;
44-
this.versionId = versionId;
45-
this.name = name;
46-
this.summary = summary;
47-
this.files = files;
48-
this.dependencies = dependencies;
49-
}
50-
51-
public String getGame() {
52-
return game;
53-
}
54-
55-
public int getFormatVersion() {
56-
return formatVersion;
57-
}
58-
59-
public String getVersionId() {
60-
return versionId;
61-
}
62-
63-
public String getName() {
64-
return name;
65-
}
66-
67-
public String getSummary() {
68-
return summary == null ? "" : summary;
69-
}
70-
71-
public List<File> getFiles() {
72-
return files;
73-
}
74-
75-
public Map<String, String> getDependencies() {
76-
return dependencies;
33+
public ModrinthManifest {
34+
if (summary == null) summary = "";
7735
}
7836

7937
public String getGameVersion() {
@@ -92,55 +50,7 @@ public void validate() throws JsonParseException, TolerableValidationException {
9250
}
9351
}
9452

95-
public static class File {
96-
private final String path;
97-
private final Map<String, String> hashes;
98-
@Nullable
99-
private final Map<String, String> env;
100-
private final List<String> downloads;
101-
private final int fileSize;
102-
103-
public File(String path, Map<String, String> hashes, @Nullable Map<String, String> env, List<String> downloads, int fileSize) {
104-
this.path = path;
105-
this.hashes = hashes;
106-
this.env = env;
107-
this.downloads = downloads;
108-
this.fileSize = fileSize;
109-
}
110-
111-
public String getPath() {
112-
return path;
113-
}
114-
115-
public Map<String, String> getHashes() {
116-
return hashes;
117-
}
118-
119-
@Nullable
120-
public Map<String, String> getEnv() {
121-
return env;
122-
}
123-
124-
public List<String> getDownloads() {
125-
return downloads;
126-
}
127-
128-
public int getFileSize() {
129-
return fileSize;
130-
}
131-
132-
@Override
133-
public boolean equals(Object o) {
134-
if (this == o) return true;
135-
if (o == null || getClass() != o.getClass()) return false;
136-
File file = (File) o;
137-
return fileSize == file.fileSize && path.equals(file.path) && hashes.equals(file.hashes) && Objects.equals(this.env, file.env) && downloads.equals(file.downloads);
138-
}
139-
140-
@Override
141-
public int hashCode() {
142-
return Objects.hash(path, hashes, env, downloads, fileSize);
143-
}
53+
public record File(String path, Map<String, String> hashes, @Nullable Map<String, String> env, List<String> downloads, int fileSize) {
14454
}
14555

14656
}

HMCLCore/src/main/java/org/jackhuang/hmcl/modpack/modrinth/ModrinthModpackProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public Task<?> createUpdateTask(DefaultDependencyManager dependencyManager, Stri
5656
@Override
5757
public Modpack readManifest(ZipArchiveReader zip, Path file, Charset encoding) throws IOException, JsonParseException {
5858
ModrinthManifest manifest = JsonUtils.fromNonNullJson(CompressingUtils.readTextZipEntry(zip, "modrinth.index.json"), ModrinthManifest.class);
59-
return new Modpack(manifest.getName(), "", manifest.getVersionId(), manifest.getGameVersion(), manifest.getSummary(), encoding, manifest) {
59+
return new Modpack(manifest.name(), "", manifest.versionId(), manifest.getGameVersion(), manifest.summary(), encoding, manifest) {
6060
@Override
6161
public Task<?> getInstallTask(DefaultDependencyManager dependencyManager, Path zipFile, String name, String iconUrl) {
6262
return new ModrinthInstallTask(dependencyManager, zipFile, this, manifest, name, iconUrl);

0 commit comments

Comments
 (0)