Skip to content

Commit b779795

Browse files
committed
ModpackConfiguration
1 parent 8f0ab10 commit b779795

17 files changed

Lines changed: 57 additions & 119 deletions

HMCL/src/main/java/org/jackhuang/hmcl/game/HMCLGameRepository.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -755,7 +755,7 @@ public LaunchOptions.Builder getLaunchOptions(String version, JavaRuntime javaVe
755755
try {
756756
String jsonText = Files.readString(json);
757757
ModpackConfiguration<?> modpackConfiguration = JsonUtils.GSON.fromJson(jsonText, ModpackConfiguration.class);
758-
ModpackProvider provider = ModpackHelper.getProviderByType(modpackConfiguration.getType());
758+
ModpackProvider provider = ModpackHelper.getProviderByType(modpackConfiguration.type());
759759
if (provider != null) provider.injectLaunchOptions(jsonText, builder);
760760
} catch (IOException | JsonParseException e) {
761761
LOG.warning("Failed to parse modpack configuration file " + json, e);

HMCL/src/main/java/org/jackhuang/hmcl/game/HMCLModpackInstallTask.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public HMCLModpackInstallTask(HMCLGameRepository repository, Path zipFile, Modpa
6767
if (Files.exists(json)) {
6868
config = JsonUtils.fromJsonFile(json, ModpackConfiguration.typeOf(Modpack.class));
6969

70-
if (!HMCLModpackProvider.INSTANCE.getName().equals(config.getType()))
70+
if (!HMCLModpackProvider.INSTANCE.getName().equals(config.type()))
7171
throw new IllegalArgumentException("Version " + name + " is not a HMCL modpack. Cannot update this version.");
7272
}
7373
} catch (JsonParseException | IOException ignore) {

HMCL/src/main/java/org/jackhuang/hmcl/game/LauncherHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ private void launch0() {
168168
Task.composeAsync(() -> {
169169
try {
170170
ModpackConfiguration<?> configuration = ModpackHelper.readModpackConfiguration(repository.getModpackConfiguration(selectedVersion));
171-
ModpackProvider provider = ModpackHelper.getProviderByType(configuration.getType());
171+
ModpackProvider provider = ModpackHelper.getProviderByType(configuration.type());
172172
if (provider == null) return null;
173173
else return provider.createCompletionTask(dependencyManager, selectedVersion);
174174
} catch (IOException e) {

HMCL/src/main/java/org/jackhuang/hmcl/game/ModpackHelper.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ else if (modpack.getManifest() instanceof McbbsModpackManifest)
236236
}
237237

238238
public static Task<Void> getUpdateTask(HMCLGameRepository repository, ServerModpackManifest manifest, Charset charset, String name, ModpackConfiguration<?> configuration) throws UnsupportedModpackException {
239-
switch (configuration.getType()) {
239+
switch (configuration.type()) {
240240
case ServerModpackRemoteInstallTask.MODPACK_TYPE:
241241
return new ModpackUpdateTask(repository, name, new ServerModpackRemoteInstallTask(repository.getDependency(), manifest, name))
242242
.thenComposeAsync(repository.refreshVersionsAsync())
@@ -248,7 +248,7 @@ public static Task<Void> getUpdateTask(HMCLGameRepository repository, ServerModp
248248

249249
public static Task<?> getUpdateTask(HMCLGameRepository repository, Path zipFile, Charset charset, String name, ModpackConfiguration<?> configuration) throws UnsupportedModpackException, ManuallyCreatedModpackException, MismatchedModpackTypeException {
250250
Modpack modpack = ModpackHelper.readModpackManifest(zipFile, charset);
251-
ModpackProvider provider = getProviderByType(configuration.getType());
251+
ModpackProvider provider = getProviderByType(configuration.type());
252252
if (provider == null) {
253253
throw new UnsupportedModpackException();
254254
}

HMCL/src/main/java/org/jackhuang/hmcl/ui/versions/GameItem.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ record Result(@Nullable String gameVersion, @Nullable String tag) {
8787
String modPackVersion = null;
8888
try {
8989
ModpackConfiguration<?> config = repository.readModpackConfiguration(id);
90-
modPackVersion = config != null ? config.getVersion() : null;
90+
modPackVersion = config != null ? config.version() : null;
9191
} catch (IOException e) {
9292
LOG.warning("Failed to read modpack configuration from " + id, e);
9393
}

HMCLCore/src/main/java/org/jackhuang/hmcl/modpack/ModpackConfiguration.java

Lines changed: 12 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -23,69 +23,34 @@
2323
import org.jackhuang.hmcl.util.gson.Validation;
2424
import org.jetbrains.annotations.Nullable;
2525

26-
import java.util.ArrayList;
27-
import java.util.Collections;
2826
import java.util.List;
2927

3028
@Immutable
31-
public final class ModpackConfiguration<T> implements Validation {
29+
public record ModpackConfiguration<T>(T manifest, String type, String name, @Nullable String version,
30+
List<FileInformation> overrides) implements Validation {
3231

3332
@SuppressWarnings("unchecked")
3433
public static <T> TypeToken<ModpackConfiguration<T>> typeOf(Class<T> clazz) {
3534
return (TypeToken<ModpackConfiguration<T>>) TypeToken.getParameterized(ModpackConfiguration.class, clazz);
3635
}
3736

38-
private final T manifest;
39-
private final String type;
40-
private final String name;
41-
private final String version;
42-
private final List<FileInformation> overrides;
43-
44-
public ModpackConfiguration() {
45-
this(null, null, "", null, Collections.emptyList());
46-
}
47-
48-
public ModpackConfiguration(T manifest, String type, String name, String version, List<FileInformation> overrides) {
49-
this.manifest = manifest;
50-
this.type = type;
51-
this.name = name;
52-
this.version = version;
53-
this.overrides = new ArrayList<>(overrides);
54-
}
55-
56-
public T getManifest() {
57-
return manifest;
58-
}
59-
60-
public String getType() {
61-
return type;
62-
}
63-
64-
public String getName() {
65-
return name;
37+
public ModpackConfiguration {
38+
if (name == null) name = "";
39+
overrides = overrides == null ? List.of() : List.copyOf(overrides);
6640
}
6741

68-
@Nullable
69-
public String getVersion() {
70-
return version;
71-
}
72-
73-
public ModpackConfiguration<T> setManifest(T manifest) {
42+
public ModpackConfiguration<T> withManifest(T manifest) {
7443
return new ModpackConfiguration<>(manifest, type, name, version, overrides);
7544
}
7645

77-
public ModpackConfiguration<T> setOverrides(List<FileInformation> overrides) {
46+
public ModpackConfiguration<T> withOverrides(List<FileInformation> overrides) {
7847
return new ModpackConfiguration<>(manifest, type, name, version, overrides);
7948
}
8049

81-
public ModpackConfiguration<T> setVersion(String version) {
50+
public ModpackConfiguration<T> withVersion(String version) {
8251
return new ModpackConfiguration<>(manifest, type, name, version, overrides);
8352
}
8453

85-
public List<FileInformation> getOverrides() {
86-
return Collections.unmodifiableList(overrides);
87-
}
88-
8954
@Override
9055
public void validate() throws JsonParseException {
9156
if (manifest == null)
@@ -94,43 +59,16 @@ public void validate() throws JsonParseException {
9459
throw new JsonParseException("MinecraftInstanceConfiguration missing `type`");
9560
}
9661

62+
/**
63+
* @param path the relative path to Minecraft run directory.
64+
*/
9765
@Immutable
98-
public static class FileInformation implements Validation {
99-
private final String path; // relative
100-
private final String hash;
101-
private final String downloadURL;
102-
103-
public FileInformation() {
104-
this(null, null);
105-
}
66+
public record FileInformation(String path, String hash, @Nullable String downloadURL) implements Validation {
10667

10768
public FileInformation(String path, String hash) {
10869
this(path, hash, null);
10970
}
11071

111-
public FileInformation(String path, String hash, String downloadURL) {
112-
this.path = path;
113-
this.hash = hash;
114-
this.downloadURL = downloadURL;
115-
}
116-
117-
/**
118-
* The relative path to Minecraft run directory
119-
*
120-
* @return the relative path to Minecraft run directory.
121-
*/
122-
public String getPath() {
123-
return path;
124-
}
125-
126-
public String getDownloadURL() {
127-
return downloadURL;
128-
}
129-
130-
public String getHash() {
131-
return hash;
132-
}
133-
13472
@Override
13573
public void validate() throws JsonParseException {
13674
if (path == null)

HMCLCore/src/main/java/org/jackhuang/hmcl/modpack/ModpackInstallTask.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public ModpackInstallTask(Path modpackFile, Path dest, Charset charset, List<Str
5454
if (oldConfiguration == null)
5555
overrides = Collections.emptyList();
5656
else
57-
overrides = oldConfiguration.getOverrides();
57+
overrides = oldConfiguration.overrides();
5858
}
5959

6060
@Override
@@ -64,7 +64,7 @@ public void execute() throws Exception {
6464

6565
HashMap<String, ModpackConfiguration.FileInformation> files = new HashMap<>();
6666
for (ModpackConfiguration.FileInformation file : overrides)
67-
files.put(file.getPath(), file);
67+
files.put(file.path(), file);
6868

6969

7070
for (String subDirectory : subDirectories) {
@@ -88,16 +88,16 @@ public void execute() throws Exception {
8888
// If both old and new modpacks have this entry, and user has modified this file,
8989
// we will not replace it since this modified file is what user expects.
9090
String fileHash = DigestUtils.digestToString("SHA-1", destFile);
91-
String oldHash = files.get(relativePath).getHash();
91+
String oldHash = files.get(relativePath).hash();
9292
return Objects.equals(oldHash, fileHash);
9393
}
9494
}).unzip();
9595
}
9696

9797
// If old modpack have this entry, and new modpack deleted it. Delete this file.
9898
for (ModpackConfiguration.FileInformation file : overrides) {
99-
Path original = dest.resolve(file.getPath());
100-
if (Files.exists(original) && !entries.contains(file.getPath()))
99+
Path original = dest.resolve(file.path());
100+
if (Files.exists(original) && !entries.contains(file.path()))
101101
Files.deleteIfExists(original);
102102
}
103103
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public CurseInstallTask(DefaultDependencyManager dependencyManager, Path zipFile
107107
if (Files.exists(json)) {
108108
config = JsonUtils.fromJsonFile(json, ModpackConfiguration.typeOf(CurseManifest.class));
109109

110-
if (!CurseModpackProvider.INSTANCE.getName().equals(config.getType()))
110+
if (!CurseModpackProvider.INSTANCE.getName().equals(config.type()))
111111
throw new IllegalArgumentException("Version " + name + " is not a Curse modpack. Cannot update this version.");
112112
}
113113
} catch (JsonParseException | IOException ignore) {
@@ -172,7 +172,7 @@ public void execute() throws Exception {
172172
// resolves those file names and writes the enriched manifest to
173173
// manifest.json, so read from there when available.
174174
Path oldManifestFile = repository.getVersionRoot(name).resolve("manifest.json");
175-
List<CurseManifestFile> oldFiles = config.getManifest().files();
175+
List<CurseManifestFile> oldFiles = config.manifest().files();
176176
if (Files.exists(oldManifestFile)) {
177177
try {
178178
CurseManifest oldManifest = JsonUtils.fromJsonFile(oldManifestFile, CurseManifest.class);

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public CompletableFuture<Void> getFuture(TaskCompletableFuture executor) {
8989
throw new IOException("Malformed modpack configuration");
9090
}
9191
}
92-
manifest = configuration.getManifest();
92+
manifest = configuration.manifest();
9393
if (manifest == null) throw new CustomException();
9494
})).thenComposeAsync(unused -> {
9595
// we first download latest manifest
@@ -173,7 +173,7 @@ public CompletableFuture<Void> getFuture(TaskCompletableFuture executor) {
173173
})).thenAcceptAsync(wrapConsumer(unused1 -> {
174174
Path manifestFile = repository.getModpackConfiguration(version);
175175
JsonUtils.writeToJsonFile(manifestFile,
176-
new ModpackConfiguration<>(manifest, this.configuration.getType(), this.manifest.getName(), this.manifest.getVersion(),
176+
new ModpackConfiguration<>(manifest, this.configuration.type(), this.manifest.getName(), this.manifest.getVersion(),
177177
this.manifest.getFiles().stream()
178178
.flatMap(file -> file instanceof McbbsModpackManifest.AddonFile
179179
? Stream.of((McbbsModpackManifest.AddonFile) file)
@@ -237,7 +237,7 @@ public CompletableFuture<Void> getFuture(TaskCompletableFuture executor) {
237237
.collect(Collectors.toList()));
238238

239239
manifest = newManifest;
240-
configuration = configuration.setManifest(newManifest);
240+
configuration = configuration.withManifest(newManifest);
241241
JsonUtils.writeToJsonFile(configurationFile, configuration);
242242

243243
for (McbbsModpackManifest.File file : newManifest.getFiles())

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public McbbsModpackLocalInstallTask(DefaultDependencyManager dependencyManager,
8181
if (Files.exists(json)) {
8282
config = JsonUtils.fromJsonFile(json, ModpackConfiguration.typeOf(McbbsModpackManifest.class));
8383

84-
if (!McbbsModpackProvider.INSTANCE.getName().equals(config.getType()))
84+
if (!McbbsModpackProvider.INSTANCE.getName().equals(config.type()))
8585
throw new IllegalArgumentException("Version " + name + " is not a Mcbbs modpack. Cannot update this version.");
8686
}
8787
} catch (JsonParseException | IOException ignore) {

0 commit comments

Comments
 (0)