Skip to content

Commit

Permalink
Merge pull request #4629 from ColdAnkles/createMap-lib-asset
Browse files Browse the repository at this point in the history
Updated FunctionUtil to handle `lib://` assets via AssetResolver
  • Loading branch information
cwisniew authored Jan 12, 2024
2 parents 2af826c + 8fe4e6c commit de08ce2
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,14 @@
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;
import net.rptools.maptool.client.functions.json.JSONMacroFunctions;
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;
Expand Down Expand Up @@ -130,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();
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,6 @@ public Object childEvaluate(
throw new ParserException(
I18N.getText("macro.function.map.invalidAsset", functionName, mapAssetId));
}

final var mapAsset = AssetManager.getAsset(mapAssetKey);
if (mapAsset != null) {
AssetManager.putAsset(mapAsset);
Expand Down
21 changes: 18 additions & 3 deletions src/main/java/net/rptools/maptool/util/FunctionUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -519,16 +519,31 @@ 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;
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 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;
}

Expand Down

0 comments on commit de08ce2

Please sign in to comment.