From 9b5445b6866453396b68326c0e47d9a65d8e6833 Mon Sep 17 00:00:00 2001 From: Zkitefly Date: Sun, 25 Aug 2024 02:55:55 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=A8=A1=E7=BB=84=E8=AF=A6?= =?UTF-8?q?=E6=83=85=E9=A1=B5=E5=9B=BE=E6=A0=87=E9=97=AE=E9=A2=98=20(#3227?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Update ModListPageSkin.java * Update ModListPageSkin.java * Update ModListPageSkin.java * Update ModListPageSkin.java * Update ModListPageSkin.java * Update ModListPageSkin.java * Update ModListPageSkin.java * Update ModListPageSkin.java * Update ModListPageSkin.java * Update ModListPageSkin.java * Update ModListPageSkin.java * update * 调整判断逻辑 * update * update --------- Co-authored-by: Glavo --- .../hmcl/ui/versions/ModListPageSkin.java | 78 ++++++++++++++----- 1 file changed, 57 insertions(+), 21 deletions(-) diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/versions/ModListPageSkin.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/versions/ModListPageSkin.java index acd647a5fd..4c35fe6308 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/ui/versions/ModListPageSkin.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/versions/ModListPageSkin.java @@ -59,15 +59,11 @@ import org.jackhuang.hmcl.util.io.NetworkUtils; import org.jetbrains.annotations.NotNull; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; +import java.io.InputStream; import java.nio.file.FileSystem; import java.nio.file.Files; import java.nio.file.Path; -import java.util.Arrays; -import java.util.List; -import java.util.Locale; -import java.util.Optional; +import java.util.*; import java.util.function.Predicate; import java.util.regex.Pattern; import java.util.stream.Collectors; @@ -297,25 +293,65 @@ class ModInfoDialog extends JFXDialogLayout { titleContainer.setSpacing(8); ImageView imageView = new ImageView(); - if (StringUtils.isNotBlank(modInfo.getModInfo().getLogoPath())) { - Task.supplyAsync(() -> { - try (FileSystem fs = CompressingUtils.createReadOnlyZipFileSystem(modInfo.getModInfo().getFile())) { - Path iconPath = fs.getPath(modInfo.getModInfo().getLogoPath()); + Task.supplyAsync(() -> { + try (FileSystem fs = CompressingUtils.createReadOnlyZipFileSystem(modInfo.getModInfo().getFile())) { + String logoPath = modInfo.getModInfo().getLogoPath(); + if (StringUtils.isNotBlank(logoPath)) { + Path iconPath = fs.getPath(logoPath); if (Files.exists(iconPath)) { - ByteArrayOutputStream stream = new ByteArrayOutputStream(); - Files.copy(iconPath, stream); - return new ByteArrayInputStream(stream.toByteArray()); + try (InputStream stream = Files.newInputStream(iconPath)) { + Image image = new Image(stream, 40, 40, true, true); + if (!image.isError() && image.getWidth() == image.getHeight()) + return image; + } catch (Throwable e) { + LOG.warning("Failed to load image " + logoPath, e); + } } } - return null; - }).whenComplete(Schedulers.javafx(), (stream, exception) -> { - if (stream != null) { - imageView.setImage(new Image(stream, 40, 40, true, true)); - } else { - imageView.setImage(FXUtils.newBuiltinImage("/assets/img/command.png", 40, 40, true, true)); + + List defaultPaths = new ArrayList<>(Arrays.asList( + "icon.png", + "logo.png", + "mod_logo.png", + "pack.png", + "logoFile.png" + )); + + String id = modInfo.getModInfo().getId(); + if (StringUtils.isNotBlank(id)) { + defaultPaths.addAll(Arrays.asList( + "assets/" + id + "/icon.png", + "assets/" + id.replace("-", "") + "/icon.png", + id + ".png", + id + "-logo.png", + id + "-icon.png", + id + "_logo.png", + id + "_icon.png" + )); } - }).start(); - } + + for (String path : defaultPaths) { + Path iconPath = fs.getPath(path); + if (Files.exists(iconPath)) { + try (InputStream stream = Files.newInputStream(iconPath)) { + Image image = new Image(stream, 40, 40, true, true); + if (!image.isError() && image.getWidth() == image.getHeight()) + return image; + } + } + } + } catch (Exception e) { + LOG.warning("Failed to load icon", e); + } + + return null; + }).whenComplete(Schedulers.javafx(), (image, exception) -> { + if (image != null) { + imageView.setImage(image); + } else { + imageView.setImage(FXUtils.newBuiltinImage("/assets/img/command.png", 40, 40, true, true)); + } + }).start(); TwoLineListItem title = new TwoLineListItem(); title.setTitle(modInfo.getModInfo().getName());