Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,7 @@ public LaunchOptions.Builder getLaunchOptions(String version, JavaRuntime javaVe
try {
String jsonText = Files.readString(json);
ModpackConfiguration<?> modpackConfiguration = JsonUtils.GSON.fromJson(jsonText, ModpackConfiguration.class);
ModpackProvider provider = ModpackHelper.getProviderByType(modpackConfiguration.getType());
ModpackProvider provider = ModpackHelper.getProviderByType(modpackConfiguration.type());
if (provider != null) provider.injectLaunchOptions(jsonText, builder);
} catch (IOException | JsonParseException e) {
LOG.warning("Failed to parse modpack configuration file " + json, e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public HMCLModpackInstallTask(HMCLGameRepository repository, Path zipFile, Modpa
if (Files.exists(json)) {
config = JsonUtils.fromJsonFile(json, ModpackConfiguration.typeOf(Modpack.class));

if (!HMCLModpackProvider.INSTANCE.getName().equals(config.getType()))
if (!HMCLModpackProvider.INSTANCE.getName().equals(config.type()))
throw new IllegalArgumentException("Version " + name + " is not a HMCL modpack. Cannot update this version.");
}
} catch (JsonParseException | IOException ignore) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ private void launch0() {
Task.composeAsync(() -> {
try {
ModpackConfiguration<?> configuration = ModpackHelper.readModpackConfiguration(repository.getModpackConfiguration(selectedVersion));
ModpackProvider provider = ModpackHelper.getProviderByType(configuration.getType());
ModpackProvider provider = ModpackHelper.getProviderByType(configuration.type());
if (provider == null) return null;
else return provider.createCompletionTask(dependencyManager, selectedVersion);
} catch (IOException e) {
Expand Down
4 changes: 2 additions & 2 deletions HMCL/src/main/java/org/jackhuang/hmcl/game/ModpackHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ else if (modpack.getManifest() instanceof McbbsModpackManifest)
}

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

public static Task<?> getUpdateTask(HMCLGameRepository repository, Path zipFile, Charset charset, String name, ModpackConfiguration<?> configuration) throws UnsupportedModpackException, ManuallyCreatedModpackException, MismatchedModpackTypeException {
Modpack modpack = ModpackHelper.readModpackManifest(zipFile, charset);
ModpackProvider provider = getProviderByType(configuration.getType());
ModpackProvider provider = getProviderByType(configuration.type());
if (provider == null) {
throw new UnsupportedModpackException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ public RemoteModpackPage(WizardController controller) {
return;
}

nameProperty.set(manifest.getName());
versionProperty.set(manifest.getVersion());
authorProperty.set(manifest.getAuthor());
nameProperty.set(manifest.name());
versionProperty.set(manifest.version());
authorProperty.set(manifest.author());

HMCLGameRepository repository = controller.getSettings().get(ModpackPage.REPOSITORY);
String name = controller.getSettings().get(MODPACK_NAME);
Expand All @@ -63,14 +63,14 @@ public RemoteModpackPage(WizardController controller) {
txtModpackName.setDisable(true);
} else {
// trim: https://github.com/HMCL-dev/HMCL/issues/962
txtModpackName.setText(manifest.getName().trim());
txtModpackName.setText(manifest.name().trim());
txtModpackName.getValidators().addAll(
new RequiredValidator(),
new Validator(i18n("install.new_game.already_exists"), str -> !repository.versionIdConflicts(str)),
new Validator(i18n("install.new_game.malformed"), HMCLGameRepository::isValidVersionId));
}

btnDescription.setVisible(StringUtils.isNotBlank(manifest.getDescription()));
btnDescription.setVisible(StringUtils.isNotBlank(manifest.description()));
}

@Override
Expand All @@ -85,7 +85,7 @@ protected void onInstall() {
}

protected void onDescribe() {
Controllers.navigate(new WebPage(i18n("modpack.description"), manifest.getDescription()));
Controllers.navigate(new WebPage(i18n("modpack.description"), manifest.description()));
}

public static final SettingsMap.Key<ServerModpackManifest> MODPACK_SERVER_MANIFEST = new SettingsMap.Key<>("MODPACK_SERVER_MANIFEST");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ record Result(@Nullable String gameVersion, @Nullable String tag) {
String modPackVersion = null;
try {
ModpackConfiguration<?> config = repository.readModpackConfiguration(id);
modPackVersion = config != null ? config.getVersion() : null;
modPackVersion = config != null ? config.version() : null;
} catch (IOException e) {
LOG.warning("Failed to read modpack configuration from " + id, e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,69 +23,34 @@
import org.jackhuang.hmcl.util.gson.Validation;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Immutable
public final class ModpackConfiguration<T> implements Validation {
public record ModpackConfiguration<T>(T manifest, String type, String name, @Nullable String version,
List<FileInformation> overrides) implements Validation {

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

private final T manifest;
private final String type;
private final String name;
private final String version;
private final List<FileInformation> overrides;

public ModpackConfiguration() {
this(null, null, "", null, Collections.emptyList());
}

public ModpackConfiguration(T manifest, String type, String name, String version, List<FileInformation> overrides) {
this.manifest = manifest;
this.type = type;
this.name = name;
this.version = version;
this.overrides = new ArrayList<>(overrides);
}

public T getManifest() {
return manifest;
}

public String getType() {
return type;
}

public String getName() {
return name;
public ModpackConfiguration {
if (name == null) name = "";
overrides = overrides == null ? List.of() : List.copyOf(overrides);
}

@Nullable
public String getVersion() {
return version;
}

public ModpackConfiguration<T> setManifest(T manifest) {
public ModpackConfiguration<T> withManifest(T manifest) {
return new ModpackConfiguration<>(manifest, type, name, version, overrides);
}

public ModpackConfiguration<T> setOverrides(List<FileInformation> overrides) {
public ModpackConfiguration<T> withOverrides(List<FileInformation> overrides) {
return new ModpackConfiguration<>(manifest, type, name, version, overrides);
}

public ModpackConfiguration<T> setVersion(String version) {
public ModpackConfiguration<T> withVersion(String version) {
return new ModpackConfiguration<>(manifest, type, name, version, overrides);
}

public List<FileInformation> getOverrides() {
return Collections.unmodifiableList(overrides);
}

@Override
public void validate() throws JsonParseException {
if (manifest == null)
Expand All @@ -94,43 +59,16 @@ public void validate() throws JsonParseException {
throw new JsonParseException("MinecraftInstanceConfiguration missing `type`");
}

/**
* @param path the relative path to Minecraft run directory.
*/
@Immutable
public static class FileInformation implements Validation {
private final String path; // relative
private final String hash;
private final String downloadURL;

public FileInformation() {
this(null, null);
}
public record FileInformation(String path, String hash, @Nullable String downloadURL) implements Validation {

public FileInformation(String path, String hash) {
this(path, hash, null);
}

public FileInformation(String path, String hash, String downloadURL) {
this.path = path;
this.hash = hash;
this.downloadURL = downloadURL;
}

/**
* The relative path to Minecraft run directory
*
* @return the relative path to Minecraft run directory.
*/
public String getPath() {
return path;
}

public String getDownloadURL() {
return downloadURL;
}

public String getHash() {
return hash;
}

@Override
public void validate() throws JsonParseException {
if (path == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public ModpackInstallTask(Path modpackFile, Path dest, Charset charset, List<Str
if (oldConfiguration == null)
overrides = Collections.emptyList();
else
overrides = oldConfiguration.getOverrides();
overrides = oldConfiguration.overrides();
}

@Override
Expand All @@ -64,7 +64,7 @@ public void execute() throws Exception {

HashMap<String, ModpackConfiguration.FileInformation> files = new HashMap<>();
for (ModpackConfiguration.FileInformation file : overrides)
files.put(file.getPath(), file);
files.put(file.path(), file);


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

// If old modpack have this entry, and new modpack deleted it. Delete this file.
for (ModpackConfiguration.FileInformation file : overrides) {
Path original = dest.resolve(file.getPath());
if (Files.exists(original) && !entries.contains(file.getPath()))
Path original = dest.resolve(file.path());
if (Files.exists(original) && !entries.contains(file.path()))
Files.deleteIfExists(original);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public CurseInstallTask(DefaultDependencyManager dependencyManager, Path zipFile
if (Files.exists(json)) {
config = JsonUtils.fromJsonFile(json, ModpackConfiguration.typeOf(CurseManifest.class));

if (!CurseModpackProvider.INSTANCE.getName().equals(config.getType()))
if (!CurseModpackProvider.INSTANCE.getName().equals(config.type()))
throw new IllegalArgumentException("Version " + name + " is not a Curse modpack. Cannot update this version.");
}
} catch (JsonParseException | IOException ignore) {
Expand Down Expand Up @@ -172,7 +172,7 @@ public void execute() throws Exception {
// resolves those file names and writes the enriched manifest to
// manifest.json, so read from there when available.
Path oldManifestFile = repository.getVersionRoot(name).resolve("manifest.json");
List<CurseManifestFile> oldFiles = config.getManifest().files();
List<CurseManifestFile> oldFiles = config.manifest().files();
if (Files.exists(oldManifestFile)) {
try {
CurseManifest oldManifest = JsonUtils.fromJsonFile(oldManifestFile, CurseManifest.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,43 +26,14 @@
* https://addons-ecs.forgesvc.net/api/v2/addon/&lt;projectID&gt;/file/&lt;fileID&gt;
*/
@Immutable
public final class CurseMetaMod {
@SerializedName(value = "Id", alternate = "id")
private final int id;

@SerializedName(value = "FileName", alternate = "fileName")
private final String fileName;

@SerializedName(value = "FileNameOnDisk")
private final String fileNameOnDisk;

@SerializedName(value = "DownloadURL", alternate = "downloadUrl")
private final String downloadURL;

public CurseMetaMod() {
this(0, "", "", "");
}

public CurseMetaMod(int id, String fileName, String fileNameOnDisk, String downloadURL) {
this.id = id;
this.fileName = fileName;
this.fileNameOnDisk = fileNameOnDisk;
this.downloadURL = downloadURL;
}

public int getId() {
return id;
}

public String getFileName() {
return fileName;
}

public String getFileNameOnDisk() {
return fileNameOnDisk;
}

public String getDownloadURL() {
return downloadURL;
public record CurseMetaMod(@SerializedName(value = "Id", alternate = "id") int id,
@SerializedName(value = "FileName", alternate = "fileName") String fileName,
@SerializedName(value = "FileNameOnDisk") String fileNameOnDisk,
@SerializedName(value = "DownloadURL", alternate = "downloadUrl") String downloadURL) {

public CurseMetaMod {
if (fileName == null) fileName = "";
if (fileNameOnDisk == null) fileNameOnDisk = "";
if (downloadURL == null) downloadURL = "";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public CompletableFuture<Void> getFuture(TaskCompletableFuture executor) {
throw new IOException("Malformed modpack configuration");
}
}
manifest = configuration.getManifest();
manifest = configuration.manifest();
if (manifest == null) throw new CustomException();
})).thenComposeAsync(unused -> {
// we first download latest manifest
Expand Down Expand Up @@ -173,7 +173,7 @@ public CompletableFuture<Void> getFuture(TaskCompletableFuture executor) {
})).thenAcceptAsync(wrapConsumer(unused1 -> {
Path manifestFile = repository.getModpackConfiguration(version);
JsonUtils.writeToJsonFile(manifestFile,
new ModpackConfiguration<>(manifest, this.configuration.getType(), this.manifest.getName(), this.manifest.getVersion(),
new ModpackConfiguration<>(manifest, this.configuration.type(), this.manifest.getName(), this.manifest.getVersion(),
this.manifest.getFiles().stream()
.flatMap(file -> file instanceof McbbsModpackManifest.AddonFile
? Stream.of((McbbsModpackManifest.AddonFile) file)
Expand Down Expand Up @@ -204,7 +204,7 @@ public CompletableFuture<Void> getFuture(TaskCompletableFuture executor) {
try {
String result = NetworkUtils.doGet(String.format("https://cursemeta.dries007.net/%d/%d.json", file.getProjectID(), file.getFileID()));
CurseMetaMod mod = JsonUtils.fromNonNullJson(result, CurseMetaMod.class);
return file.withFileName(mod.getFileNameOnDisk()).withURL(mod.getDownloadURL());
return file.withFileName(mod.fileNameOnDisk()).withURL(mod.downloadURL());
} catch (FileNotFoundException fof) {
LOG.warning("Could not query cursemeta for deleted mods: " + file.getUrl(), fof);
notFound.set(true);
Expand All @@ -213,7 +213,7 @@ public CompletableFuture<Void> getFuture(TaskCompletableFuture executor) {
try {
String result = NetworkUtils.doGet(String.format("https://addons-ecs.forgesvc.net/api/v2/addon/%d/file/%d", file.getProjectID(), file.getFileID()));
CurseMetaMod mod = JsonUtils.fromNonNullJson(result, CurseMetaMod.class);
return file.withFileName(mod.getFileName()).withURL(mod.getDownloadURL());
return file.withFileName(mod.fileName()).withURL(mod.downloadURL());
} catch (FileNotFoundException fof) {
LOG.warning("Could not query forgesvc for deleted mods: " + file.getUrl(), fof);
notFound.set(true);
Expand All @@ -237,7 +237,7 @@ public CompletableFuture<Void> getFuture(TaskCompletableFuture executor) {
.collect(Collectors.toList()));

manifest = newManifest;
configuration = configuration.setManifest(newManifest);
configuration = configuration.withManifest(newManifest);
JsonUtils.writeToJsonFile(configurationFile, configuration);

for (McbbsModpackManifest.File file : newManifest.getFiles())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public McbbsModpackLocalInstallTask(DefaultDependencyManager dependencyManager,
if (Files.exists(json)) {
config = JsonUtils.fromJsonFile(json, ModpackConfiguration.typeOf(McbbsModpackManifest.class));

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