Skip to content

Commit

Permalink
Switch mod icon uploading to just byte arrays rather than explicitly …
Browse files Browse the repository at this point in the history
…loading an image via ImageIO, then writing it back to a new byte array.
  • Loading branch information
AlexIIL committed Jan 19, 2024
1 parent 091dcbf commit 09157ac
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 25 deletions.
9 changes: 7 additions & 2 deletions src/main/java/org/quiltmc/loader/api/gui/QuiltLoaderGui.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,13 @@ public static void open(QuiltLoaderWindow<?> window, boolean shouldWait) throws

// Icons

public static QuiltLoaderIcon createIcon(BufferedImage image) {
return createIcon(Collections.singletonMap(image.getWidth(), image));
public static QuiltLoaderIcon createIcon(byte[] imageBytes) {
return createIcon(new byte[][] { imageBytes });
}

/** @param images Array of differently sized images, to be chosen by the UI. */
public static QuiltLoaderIcon createIcon(byte[][] images) {
return GuiManagerImpl.allocateIcons(images);
}

public static QuiltLoaderIcon createIcon(Map<Integer, BufferedImage> images) {
Expand Down
38 changes: 21 additions & 17 deletions src/main/java/org/quiltmc/loader/impl/gui/GuiManagerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
Expand All @@ -34,6 +36,7 @@
import org.quiltmc.loader.api.QuiltLoader;
import org.quiltmc.loader.api.gui.QuiltLoaderIcon;
import org.quiltmc.loader.api.plugin.gui.PluginGuiManager;
import org.quiltmc.loader.impl.util.FileUtil;
import org.quiltmc.loader.impl.util.QuiltLoaderInternal;
import org.quiltmc.loader.impl.util.QuiltLoaderInternalType;

Expand Down Expand Up @@ -93,6 +96,12 @@ public static QuiltLoaderIcon allocateIcons(Map<Integer, BufferedImage> imageSiz
return new PluginIconImpl(index);
}

public static QuiltLoaderIcon allocateIcons(byte[][] imageBytes) {
PluginIconImpl icon = new PluginIconImpl(imageBytes);
icon.send();
return icon;
}

public static QuiltLoaderIcon getModIcon(ModContainer mod) {
if (mod == null) {
return ICON_GENERIC_FILE;
Expand All @@ -104,28 +113,23 @@ public static QuiltLoaderIcon getModIcon(ModContainer mod) {
}

private static QuiltLoaderIcon computeModIcon(ModContainer mod) {
Map<Integer, BufferedImage> map = new HashMap<>();
Map<String, byte[]> images = new HashMap<>();
for (int size : new int[] { 16, 32 }) {
String pathStr = mod.metadata().icon(size);
if (pathStr == null) {
continue;
}
Path path = mod.getPath(pathStr);
if (!FasterFiles.isRegularFile(path)) {
continue;
}
try (InputStream stream = Files.newInputStream(path)) {
BufferedImage image = ImageIO.read(stream);
map.put(image.getWidth(), image);
} catch (IOException e) {
// TODO: Warn about this somewhere!
e.printStackTrace();
String iconPath = mod.metadata().icon(size);
if (iconPath != null && ! images.containsKey(iconPath)) {
Path path = mod.getPath(iconPath);
try (InputStream stream = Files.newInputStream(path)) {
images.put(iconPath, FileUtil.readAllBytes(stream));
} catch (IOException io) {
// TODO: Warn about this somewhere!
io.printStackTrace();
}
}
}
if (map.isEmpty()) {
if (images.isEmpty()) {
return ICON_GENERIC_FILE;
}
return allocateIcons(map);
return allocateIcons(images.values().toArray(new byte[0][]));
}

// Builtin
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.quiltmc.loader.api.ModMetadata.ProvidedMod;
import org.quiltmc.loader.api.VersionRange;
import org.quiltmc.loader.api.gui.QuiltDisplayedError;
import org.quiltmc.loader.api.gui.QuiltLoaderGui;
import org.quiltmc.loader.api.gui.QuiltLoaderText;
import org.quiltmc.loader.api.plugin.solver.AliasedLoadOption;
import org.quiltmc.loader.api.plugin.solver.LoadOption;
Expand All @@ -51,6 +52,7 @@
import org.quiltmc.loader.impl.plugin.quilt.OptionalModIdDefintion;
import org.quiltmc.loader.impl.plugin.quilt.QuiltRuleBreakOnly;
import org.quiltmc.loader.impl.plugin.quilt.QuiltRuleDepOnly;
import org.quiltmc.loader.impl.util.FileUtil;
import org.quiltmc.loader.impl.util.QuiltLoaderInternal;
import org.quiltmc.loader.impl.util.QuiltLoaderInternalType;

Expand Down Expand Up @@ -340,23 +342,22 @@ private boolean reportSingleMandatoryError(RuleLink rootRule) {
private static void setIconFromMod(QuiltPluginManagerImpl manager, ModLoadOption mandatoryMod,
QuiltDisplayedError error) {
// TODO: Only upload a ModLoadOption's icon once!
Map<Integer, BufferedImage> modIcons = new HashMap<>();
Map<String, byte[]> images = new HashMap<>();
for (int size : new int[] { 16, 32 }) {
String iconPath = mandatoryMod.metadata().icon(size);
if (iconPath != null) {
if (iconPath != null && ! images.containsKey(iconPath)) {
Path path = mandatoryMod.resourceRoot().resolve(iconPath);
try (InputStream stream = Files.newInputStream(path)) {
BufferedImage image = ImageIO.read(stream);
modIcons.put(image.getWidth(), image);
images.put(iconPath, FileUtil.readAllBytes(stream));
} catch (IOException io) {
// TODO: Warn about this somewhere!
io.printStackTrace();
}
}
}

if (!modIcons.isEmpty()) {
error.setIcon(manager.guiManager.allocateIcon(modIcons));
if (!images.isEmpty()) {
error.setIcon(QuiltLoaderGui.createIcon(images.values().toArray(new byte[0][])));
}
}

Expand Down

0 comments on commit 09157ac

Please sign in to comment.