Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated FunctionUtil to handle lib:// assets via AssetResolver #4629

Merged
merged 3 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading