feat: display mod description and website url. Closes #945.

This commit is contained in:
huanghongxun
2021-08-07 16:18:23 +08:00
parent 1ddcc23b89
commit b2594122a8
18 changed files with 330 additions and 66 deletions

View File

@@ -45,16 +45,18 @@ public final class FabricModMetadata {
private final String name;
private final String version;
private final String description;
private final String icon;
private final List<FabricModAuthor> authors;
private final Map<String, String> contact;
public FabricModMetadata() {
this("", "", "", Collections.emptyList(), Collections.emptyMap());
this("", "", "", "", Collections.emptyList(), Collections.emptyMap());
}
public FabricModMetadata(String name, String version, String description, List<FabricModAuthor> authors, Map<String, String> contact) {
public FabricModMetadata(String name, String version, String icon, String description, List<FabricModAuthor> authors, Map<String, String> contact) {
this.name = name;
this.version = version;
this.icon = icon;
this.description = description;
this.authors = authors;
this.contact = contact;
@@ -68,7 +70,7 @@ public final class FabricModMetadata {
FabricModMetadata metadata = JsonUtils.fromNonNullJson(FileUtils.readText(mcmod), FabricModMetadata.class);
String authors = metadata.authors == null ? "" : metadata.authors.stream().map(author -> author.name).collect(Collectors.joining(", "));
return new ModInfo(modManager, modFile, metadata.name, new ModInfo.Description(metadata.description),
authors, metadata.version, "", metadata.contact != null ? metadata.contact.getOrDefault("homepage", "") : "");
authors, metadata.version, "", metadata.contact != null ? metadata.contact.getOrDefault("homepage", "") : "", metadata.icon);
}
}

View File

@@ -0,0 +1,129 @@
package org.jackhuang.hmcl.mod;
import com.google.gson.JsonParseException;
import com.moandjiezana.toml.Toml;
import org.jackhuang.hmcl.util.Immutable;
import org.jackhuang.hmcl.util.io.CompressingUtils;
import org.jackhuang.hmcl.util.io.FileUtils;
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 ForgeNewModMetadata {
private final String modLoader;
private final String loaderVersion;
private final String logoFile;
private final String license;
private final List<Mod> mods;
public ForgeNewModMetadata() {
this("", "", "", "", Collections.emptyList());
}
public ForgeNewModMetadata(String modLoader, String loaderVersion, String logoFile, String license, List<Mod> mods) {
this.modLoader = modLoader;
this.loaderVersion = loaderVersion;
this.logoFile = logoFile;
this.license = license;
this.mods = mods;
}
public String getModLoader() {
return modLoader;
}
public String getLoaderVersion() {
return loaderVersion;
}
public String getLogoFile() {
return logoFile;
}
public String getLicense() {
return license;
}
public List<Mod> getMods() {
return mods;
}
public static class Mod {
private final String modId;
private final String version;
private final String displayName;
private final String side;
private final String displayURL;
private final String authors;
private final String description;
public Mod() {
this("", "", "", "", "", "", "");
}
public Mod(String modId, String version, String displayName, String side, String displayURL, String authors, String description) {
this.modId = modId;
this.version = version;
this.displayName = displayName;
this.side = side;
this.displayURL = displayURL;
this.authors = authors;
this.description = description;
}
public String getModId() {
return modId;
}
public String getVersion() {
return version;
}
public String getDisplayName() {
return displayName;
}
public String getSide() {
return side;
}
public String getDisplayURL() {
return displayURL;
}
public String getAuthors() {
return authors;
}
public String getDescription() {
return description;
}
}
public static ModInfo fromFile(ModManager modManager, File modFile) throws IOException, JsonParseException {
try (FileSystem fs = CompressingUtils.createReadOnlyZipFileSystem(modFile.toPath())) {
Path modstoml = fs.getPath("META-INF/mods.toml");
if (Files.notExists(modstoml))
throw new IOException("File " + modFile + " is not a Forge 1.13+ mod.");
ForgeNewModMetadata metadata = new Toml().read(FileUtils.readText(modstoml)).to(ForgeNewModMetadata.class);
if (metadata == null || metadata.getMods().isEmpty())
throw new IOException("Mod " + modFile + " `mods.toml` is malformed..");
Mod mod = metadata.getMods().get(0);
return new ModInfo(modManager, modFile, mod.getDisplayName(), new ModInfo.Description(mod.getDescription()),
mod.getAuthors(), mod.getVersion(), "",
mod.getDisplayURL(),
metadata.getLogoFile());
}
}
}

View File

@@ -37,7 +37,7 @@ import java.util.List;
* @author huangyuhui
*/
@Immutable
public final class ForgeModMetadata {
public final class ForgeOldModMetadata {
@SerializedName("modid")
private final String modId;
@@ -45,6 +45,7 @@ public final class ForgeModMetadata {
private final String description;
private final String author;
private final String version;
private final String logoFile;
private final String mcversion;
private final String url;
private final String updateUrl;
@@ -52,16 +53,17 @@ public final class ForgeModMetadata {
private final String[] authorList;
private final String[] authors;
public ForgeModMetadata() {
this("", "", "", "", "", "", "", "", "", new String[0], new String[0]);
public ForgeOldModMetadata() {
this("", "", "", "", "", "", "", "", "", "", new String[0], new String[0]);
}
public ForgeModMetadata(String modId, String name, String description, String author, String version, String mcversion, String url, String updateUrl, String credits, String[] authorList, String[] authors) {
public ForgeOldModMetadata(String modId, String name, String description, String author, String version, String logoFile, String mcversion, String url, String updateUrl, String credits, String[] authorList, String[] authors) {
this.modId = modId;
this.name = name;
this.description = description;
this.author = author;
this.version = version;
this.logoFile = logoFile;
this.mcversion = mcversion;
this.url = url;
this.updateUrl = updateUrl;
@@ -90,6 +92,10 @@ public final class ForgeModMetadata {
return version;
}
public String getLogoFile() {
return logoFile;
}
public String getGameVersion() {
return mcversion;
}
@@ -119,12 +125,12 @@ public final class ForgeModMetadata {
Path mcmod = fs.getPath("mcmod.info");
if (Files.notExists(mcmod))
throw new IOException("File " + modFile + " is not a Forge mod.");
List<ForgeModMetadata> modList = JsonUtils.GSON.fromJson(FileUtils.readText(mcmod),
new TypeToken<List<ForgeModMetadata>>() {
List<ForgeOldModMetadata> modList = JsonUtils.GSON.fromJson(FileUtils.readText(mcmod),
new TypeToken<List<ForgeOldModMetadata>>() {
}.getType());
if (modList == null || modList.isEmpty())
throw new IOException("Mod " + modFile + " `mcmod.info` is malformed..");
ForgeModMetadata metadata = modList.get(0);
ForgeOldModMetadata metadata = modList.get(0);
String authors = metadata.getAuthor();
if (StringUtils.isBlank(authors) && metadata.getAuthors().length > 0)
authors = String.join(", ", metadata.getAuthors());
@@ -134,7 +140,8 @@ public final class ForgeModMetadata {
authors = metadata.getCredits();
return new ModInfo(modManager, modFile, metadata.getName(), new ModInfo.Description(metadata.getDescription()),
authors, metadata.getVersion(), metadata.getGameVersion(),
StringUtils.isBlank(metadata.getUrl()) ? metadata.getUpdateUrl() : metadata.url);
StringUtils.isBlank(metadata.getUrl()) ? metadata.getUpdateUrl() : metadata.url,
metadata.getLogoFile());
}
}
}

View File

@@ -117,7 +117,7 @@ public final class LiteModMetadata {
if (metadata == null)
throw new IOException("Mod " + modFile + " `litemod.json` is malformed.");
return new ModInfo(modManager, modFile, metadata.getName(), new ModInfo.Description(metadata.getDescription()), metadata.getAuthor(),
metadata.getVersion(), metadata.getGameVersion(), metadata.getUpdateURI());
metadata.getVersion(), metadata.getGameVersion(), metadata.getUpdateURI(), "");
}
}

View File

@@ -45,13 +45,14 @@ public final class ModInfo implements Comparable<ModInfo> {
private final String gameVersion;
private final String url;
private final String fileName;
private final String logoPath;
private final BooleanProperty activeProperty;
public ModInfo(ModManager modManager, File file, String name, Description description) {
this(modManager, file, name, description, "", "", "", "");
this(modManager, file, name, description, "", "", "", "", "");
}
public ModInfo(ModManager modManager, File file, String name, Description description, String authors, String version, String gameVersion, String url) {
public ModInfo(ModManager modManager, File file, String name, Description description, String authors, String version, String gameVersion, String url, String logoPath) {
this.file = file.toPath();
this.name = name;
this.description = description;
@@ -59,6 +60,7 @@ public final class ModInfo implements Comparable<ModInfo> {
this.version = version;
this.gameVersion = gameVersion;
this.url = url;
this.logoPath = logoPath;
activeProperty = new SimpleBooleanProperty(this, "active", !modManager.isDisabled(file)) {
@Override
@@ -107,6 +109,10 @@ public final class ModInfo implements Comparable<ModInfo> {
return url;
}
public String getLogoPath() {
return logoPath;
}
public BooleanProperty activeProperty() {
return activeProperty;
}

View File

@@ -68,7 +68,12 @@ public final class ModManager {
case "zip":
case "jar":
try {
return ForgeModMetadata.fromFile(this, modFile);
return ForgeOldModMetadata.fromFile(this, modFile);
} catch (Exception ignore) {
}
try {
return ForgeNewModMetadata.fromFile(this, modFile);
} catch (Exception ignore) {
}

View File

@@ -150,7 +150,7 @@ public class PackMcMeta implements Validation {
if (Files.notExists(mcmod))
throw new IOException("File " + modFile + " is not a resource pack.");
PackMcMeta metadata = JsonUtils.fromNonNullJson(FileUtils.readText(mcmod), PackMcMeta.class);
return new ModInfo(modManager, modFile, FileUtils.getNameWithoutExtension(modFile), metadata.pack.description, "", "", "", "");
return new ModInfo(modManager, modFile, FileUtils.getNameWithoutExtension(modFile), metadata.pack.description, "", "", "", "", "");
}
}
}