From 53aecf38e02e90d9927d0770564000008900c088 Mon Sep 17 00:00:00 2001 From: huangyuhui Date: Fri, 17 Aug 2018 22:09:37 +0800 Subject: [PATCH] Support displaying Rift mod information --- .../java/org/jackhuang/hmcl/ui/ModItem.java | 11 ++- .../java/org/jackhuang/hmcl/mod/ModInfo.java | 10 ++- .../jackhuang/hmcl/mod/RiftModMetadata.java | 70 +++++++++++++++++++ 3 files changed, 88 insertions(+), 3 deletions(-) create mode 100644 HMCLCore/src/main/java/org/jackhuang/hmcl/mod/RiftModMetadata.java diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/ModItem.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/ModItem.java index b4c74eb90..ec7362e44 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/ui/ModItem.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/ModItem.java @@ -26,6 +26,8 @@ import javafx.scene.layout.BorderPane; import org.jackhuang.hmcl.mod.ModInfo; import org.jackhuang.hmcl.setting.Theme; +import org.jackhuang.hmcl.util.StringUtils; + import static org.jackhuang.hmcl.util.i18n.I18n.i18n; import java.util.function.Consumer; @@ -54,7 +56,14 @@ public final class ModItem extends BorderPane { setStyle("-fx-background-radius: 2; -fx-background-color: white; -fx-padding: 8;"); JFXDepthManager.setDepth(this, 1); modItem.setTitle(info.getFileName()); - modItem.setSubtitle(info.getName() + ", " + i18n("archive.version") + ": " + info.getVersion() + ", " + i18n("archive.game_version") + ": " + info.getGameVersion() + ", " + i18n("archive.author") + ": " + info.getAuthors()); + StringBuilder message = new StringBuilder(info.getName()); + if (StringUtils.isNotBlank(info.getVersion())) + message.append(", ").append(i18n("archive.version")).append(": ").append(info.getVersion()); + if (StringUtils.isNotBlank(info.getGameVersion())) + message.append(", ").append(i18n("archive.game_version")).append(": ").append(info.getGameVersion()); + if (StringUtils.isNotBlank(info.getAuthors())) + message.append(", ").append(i18n("archive.author")).append(": ").append(info.getAuthors()); + modItem.setSubtitle(message.toString()); chkEnabled.setSelected(info.isActive()); chkEnabled.selectedProperty().addListener((a, b, newValue) -> info.activeProperty().set(newValue)); diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/mod/ModInfo.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/mod/ModInfo.java index 45216a3f6..2de2c1442 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/mod/ModInfo.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/mod/ModInfo.java @@ -152,14 +152,20 @@ public final class ModInfo implements Comparable { try { return ForgeModMetadata.fromFile(modFile); } catch (Exception ignore) { - description = "May be Forge mod"; } + + try { + return RiftModMetadata.fromFile(modFile); + } catch (Exception ignore) { + } + + description = ""; break; case "litemod": try { return LiteModMetadata.fromFile(modFile); } catch (Exception ignore) { - description = "May be LiteLoader mod"; + description = "LiteLoader Mod"; } break; default: diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/mod/RiftModMetadata.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/mod/RiftModMetadata.java new file mode 100644 index 000000000..710e0a570 --- /dev/null +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/mod/RiftModMetadata.java @@ -0,0 +1,70 @@ +/* + * Hello Minecraft! Launcher. + * Copyright (C) 2018 huangyuhui + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see {http://www.gnu.org/licenses/}. + */ +package org.jackhuang.hmcl.mod; + +import com.google.gson.JsonParseException; +import org.jackhuang.hmcl.util.*; + +import java.io.File; +import java.io.IOException; +import java.nio.file.FileSystem; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Collections; +import java.util.List; + +@Immutable +public final class RiftModMetadata { + private final String id; + private final String name; + private final List authors; + + public RiftModMetadata() { + this("", "", Collections.emptyList()); + } + + public RiftModMetadata(String id, String name, List authors) { + this.id = id; + this.name = name; + this.authors = authors; + } + + public String getId() { + return id; + } + + public String getName() { + return name; + } + + public List getAuthors() { + return authors; + } + + public static ModInfo fromFile(File modFile) throws IOException, JsonParseException { + try (FileSystem fs = CompressingUtils.createReadOnlyZipFileSystem(modFile.toPath())) { + Path mcmod = fs.getPath("riftmod.json"); + if (Files.notExists(mcmod)) + throw new IOException("File " + modFile + " is not a Forge mod."); + RiftModMetadata metadata = JsonUtils.fromNonNullJson(IOUtils.readFullyAsString(Files.newInputStream(mcmod)), RiftModMetadata.class); + String authors = metadata.getAuthors() == null ? "" : String.join(", ", metadata.getAuthors()); + return new ModInfo(modFile, metadata.getName(), "", + authors, "", "", ""); + } + } +}