From 03413b101fc7abded35676f29efcd499f459f094 Mon Sep 17 00:00:00 2001 From: ColdAnkles <13864745+ColdAnkles@users.noreply.github.com> Date: Fri, 12 Jan 2024 08:14:15 +1030 Subject: [PATCH 1/3] Updated createMap to handle `lib://` assets via AssetResolver --- .../maptool/client/functions/MapFunctions.java | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/main/java/net/rptools/maptool/client/functions/MapFunctions.java b/src/main/java/net/rptools/maptool/client/functions/MapFunctions.java index 8c21c2c9fd..61ab14c8c4 100644 --- a/src/main/java/net/rptools/maptool/client/functions/MapFunctions.java +++ b/src/main/java/net/rptools/maptool/client/functions/MapFunctions.java @@ -20,6 +20,7 @@ import java.util.List; import java.util.Map; import java.util.function.Consumer; +import net.rptools.lib.MD5Key; import net.rptools.maptool.client.AppPreferences; import net.rptools.maptool.client.MapTool; import net.rptools.maptool.client.MapToolUtil; @@ -32,6 +33,7 @@ import net.rptools.maptool.model.Zone; import net.rptools.maptool.model.ZoneFactory; import net.rptools.maptool.model.drawing.DrawablePaint; +import net.rptools.maptool.util.AssetResolver; import net.rptools.maptool.util.FunctionUtil; import net.rptools.parser.Parser; import net.rptools.parser.ParserException; @@ -280,13 +282,14 @@ public Object childEvaluate( if (config.has("map asset")) { final var mapAssetId = config.getAsJsonPrimitive("map asset").getAsString(); - final var mapAssetKey = FunctionUtil.getAssetKeyFromString(mapAssetId); - if (mapAssetKey == null) { - throw new ParserException( - I18N.getText("macro.function.map.invalidAsset", functionName, mapAssetId)); + final var mapAssetKey = new AssetResolver().getAssetKey(mapAssetId); + MD5Key assetMD5 = null; + if (mapAssetKey.isPresent()) { + assetMD5 = new MD5Key(mapAssetKey.get().toString()); + } else { + assetMD5 = new MD5Key(mapAssetId); } - - final var mapAsset = AssetManager.getAsset(mapAssetKey); + final var mapAsset = AssetManager.getAsset(assetMD5); if (mapAsset != null) { AssetManager.putAsset(mapAsset); if (!MapTool.isHostingServer()) { @@ -294,7 +297,7 @@ public Object childEvaluate( } } - newMap.setMapAsset(mapAssetKey); + newMap.setMapAsset(assetMD5); } MapTool.addZone(newMap, false); From f8da024073d06904a69fe1f78ac4b46d183c233e Mon Sep 17 00:00:00 2001 From: ColdAnkles <13864745+ColdAnkles@users.noreply.github.com> Date: Fri, 12 Jan 2024 13:10:55 +1030 Subject: [PATCH 2/3] Centralised asset MD5 retrieval in FunctionUtil.getAssetKeyFromString() Updated MapFunctions CreateMap to use this. Updated InputFunction to use this. --- .../maptool/client/functions/InputFunction.java | 15 ++------------- .../maptool/client/functions/MapFunctions.java | 16 ++++++---------- .../net/rptools/maptool/util/FunctionUtil.java | 10 +++++++++- 3 files changed, 17 insertions(+), 24 deletions(-) diff --git a/src/main/java/net/rptools/maptool/client/functions/InputFunction.java b/src/main/java/net/rptools/maptool/client/functions/InputFunction.java index 8530689854..be91375a67 100644 --- a/src/main/java/net/rptools/maptool/client/functions/InputFunction.java +++ b/src/main/java/net/rptools/maptool/client/functions/InputFunction.java @@ -66,7 +66,6 @@ import javax.swing.border.EmptyBorder; import javax.swing.border.EtchedBorder; import javax.swing.border.TitledBorder; -import net.rptools.lib.MD5Key; import net.rptools.maptool.client.MapTool; import net.rptools.maptool.client.MapToolVariableResolver; import net.rptools.maptool.client.functions.InputFunction.InputType.OptionException; @@ -74,7 +73,7 @@ import net.rptools.maptool.client.ui.htmlframe.HTMLPane; import net.rptools.maptool.language.I18N; import net.rptools.maptool.model.Token; -import net.rptools.maptool.util.AssetResolver; +import net.rptools.maptool.util.FunctionUtil; import net.rptools.maptool.util.ImageManager; import net.rptools.maptool.util.StringUtil; import net.rptools.parser.Parser; @@ -1276,17 +1275,7 @@ private ImageIcon getIcon(String id, int size, ImageObserver io) { if (id == null) { return null; } - var assetKey = new AssetResolver().getAssetKey(id); - String assetId = null; - if (assetKey.isPresent()) { - assetId = assetKey.get().toString(); - } - MD5Key assetMD5 = null; - if (assetId != null) { - assetMD5 = new MD5Key(assetId); - } else { - assetMD5 = new MD5Key(id); - } + var assetMD5 = FunctionUtil.getAssetKeyFromString(id); // Get the base image && find the new size for the icon BufferedImage assetImage = ImageManager.getImage(assetMD5, io); diff --git a/src/main/java/net/rptools/maptool/client/functions/MapFunctions.java b/src/main/java/net/rptools/maptool/client/functions/MapFunctions.java index 61ab14c8c4..1575a63630 100644 --- a/src/main/java/net/rptools/maptool/client/functions/MapFunctions.java +++ b/src/main/java/net/rptools/maptool/client/functions/MapFunctions.java @@ -20,7 +20,6 @@ import java.util.List; import java.util.Map; import java.util.function.Consumer; -import net.rptools.lib.MD5Key; import net.rptools.maptool.client.AppPreferences; import net.rptools.maptool.client.MapTool; import net.rptools.maptool.client.MapToolUtil; @@ -33,7 +32,6 @@ import net.rptools.maptool.model.Zone; import net.rptools.maptool.model.ZoneFactory; import net.rptools.maptool.model.drawing.DrawablePaint; -import net.rptools.maptool.util.AssetResolver; import net.rptools.maptool.util.FunctionUtil; import net.rptools.parser.Parser; import net.rptools.parser.ParserException; @@ -282,14 +280,12 @@ public Object childEvaluate( if (config.has("map asset")) { final var mapAssetId = config.getAsJsonPrimitive("map asset").getAsString(); - final var mapAssetKey = new AssetResolver().getAssetKey(mapAssetId); - MD5Key assetMD5 = null; - if (mapAssetKey.isPresent()) { - assetMD5 = new MD5Key(mapAssetKey.get().toString()); - } else { - assetMD5 = new MD5Key(mapAssetId); + final var mapAssetKey = FunctionUtil.getAssetKeyFromString(mapAssetId); + if (mapAssetKey == null) { + throw new ParserException( + I18N.getText("macro.function.map.invalidAsset", functionName, mapAssetId)); } - final var mapAsset = AssetManager.getAsset(assetMD5); + final var mapAsset = AssetManager.getAsset(mapAssetKey); if (mapAsset != null) { AssetManager.putAsset(mapAsset); if (!MapTool.isHostingServer()) { @@ -297,7 +293,7 @@ public Object childEvaluate( } } - newMap.setMapAsset(assetMD5); + newMap.setMapAsset(mapAssetKey); } MapTool.addZone(newMap, false); diff --git a/src/main/java/net/rptools/maptool/util/FunctionUtil.java b/src/main/java/net/rptools/maptool/util/FunctionUtil.java index 8932040a07..42ce2d510c 100644 --- a/src/main/java/net/rptools/maptool/util/FunctionUtil.java +++ b/src/main/java/net/rptools/maptool/util/FunctionUtil.java @@ -519,13 +519,21 @@ public static DrawablePaint getPaintFromString(String paint) { /** * Parses a string as an asset URL. * - * @param assetUrlOrId String containing the asset ID or asset URL. + * @param assetUrlOrId String containing the asset ID (ID), asset URL (asset://ID), or addon + * URL(lib://PATH). * @return The MD5 key present in {@code assetUrlOrId}, or null. */ public static @Nullable MD5Key getAssetKeyFromString(String assetUrlOrId) { final String id; if (assetUrlOrId.toLowerCase().startsWith("asset://")) { id = assetUrlOrId.substring("asset://".length()); + } else if (assetUrlOrId.toLowerCase().startsWith("lib://")) { + var assetKey = new AssetResolver().getAssetKey(assetUrlOrId); + if (assetKey.isPresent()) { + id = assetKey.get().toString(); + } else { + return null; + } } else if (assetUrlOrId.length() == 32) { id = assetUrlOrId; } else { From 8fe4e6c4db66ec9235a14d3482c693fabbdaa379 Mon Sep 17 00:00:00 2001 From: ColdAnkles <13864745+ColdAnkles@users.noreply.github.com> Date: Fri, 12 Jan 2024 15:43:05 +1030 Subject: [PATCH 3/3] Added support for image token names --- .../maptool/client/functions/InputFunction.java | 2 +- .../net/rptools/maptool/util/FunctionUtil.java | 15 +++++++++++---- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/main/java/net/rptools/maptool/client/functions/InputFunction.java b/src/main/java/net/rptools/maptool/client/functions/InputFunction.java index be91375a67..95e682c209 100644 --- a/src/main/java/net/rptools/maptool/client/functions/InputFunction.java +++ b/src/main/java/net/rptools/maptool/client/functions/InputFunction.java @@ -129,7 +129,7 @@ public class InputFunction extends AbstractFunction { private static final Pattern ASSET_PATTERN = - Pattern.compile("^(.*)((?:asset|lib)://[0-9a-z-A-Z ./]+)"); + Pattern.compile("^(.*)((?:asset|lib|Image):(//)?[0-9a-z-A-Z ./]+)"); /** The singleton instance. */ private static final InputFunction instance = new InputFunction(); diff --git a/src/main/java/net/rptools/maptool/util/FunctionUtil.java b/src/main/java/net/rptools/maptool/util/FunctionUtil.java index 42ce2d510c..e2ab99ea21 100644 --- a/src/main/java/net/rptools/maptool/util/FunctionUtil.java +++ b/src/main/java/net/rptools/maptool/util/FunctionUtil.java @@ -524,19 +524,26 @@ public static DrawablePaint getPaintFromString(String paint) { * @return The MD5 key present in {@code assetUrlOrId}, or null. */ public static @Nullable MD5Key getAssetKeyFromString(String assetUrlOrId) { - final String id; + String id = null; if (assetUrlOrId.toLowerCase().startsWith("asset://")) { id = assetUrlOrId.substring("asset://".length()); } else if (assetUrlOrId.toLowerCase().startsWith("lib://")) { var assetKey = new AssetResolver().getAssetKey(assetUrlOrId); if (assetKey.isPresent()) { id = assetKey.get().toString(); - } else { - return null; + } + } else if (assetUrlOrId.toLowerCase().startsWith("image:")) { + for (ZoneRenderer z : MapTool.getFrame().getZoneRenderers()) { + Token t = z.getZone().getTokenByName(assetUrlOrId); + if (t != null) { + id = t.getImageAssetId().toString(); + } } } else if (assetUrlOrId.length() == 32) { id = assetUrlOrId; - } else { + } + + if (id == null) { return null; }