Skip to content

Commit

Permalink
Merge pull request #4923 from Jmr3366/j_develop_createAsset
Browse files Browse the repository at this point in the history
New function createAsset
  • Loading branch information
cwisniew authored Oct 16, 2024
2 parents ff8cedf + 1445040 commit 2cd1d38
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 2 deletions.
65 changes: 63 additions & 2 deletions src/main/java/net/rptools/maptool/client/functions/TokenImage.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,17 @@
package net.rptools.maptool.client.functions;

import com.google.gson.JsonObject;
import com.jidesoft.utils.Base64;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.math.BigDecimal;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.List;
import javax.imageio.ImageIO;
import net.rptools.lib.MD5Key;
import net.rptools.maptool.client.MapTool;
import net.rptools.maptool.client.ui.zone.renderer.ZoneRenderer;
Expand Down Expand Up @@ -57,6 +65,9 @@ final int getValue() {
public static final String SET_IMAGE = "setImage";
public static final String SET_PORTRAIT = "setTokenPortrait";
public static final String SET_HANDOUT = "setTokenHandout";
public static final String FILE_HEADER_WEBP = "RIFF";
public static final String FILE_HEADER_JPG = "ÿØÿà";
public static final String FILE_HEADER_PNG = "‰PNG";

private TokenImage() {
super(
Expand All @@ -71,7 +82,8 @@ private TokenImage() {
"getImage",
"setTokenOpacity",
"getAssetProperties",
"getTokenOpacity");
"getTokenOpacity",
"createAsset");
}

/**
Expand Down Expand Up @@ -171,6 +183,56 @@ public Object childEvaluate(
}
}

StringBuilder assetId = new StringBuilder("asset://");
if (functionName.equalsIgnoreCase("createAsset")) {
FunctionUtil.checkNumberParam(functionName, args, 2, 2);
String imageName = args.get(0).toString();
String imageString = args.get(1).toString();
if (imageName.isEmpty() || imageString.isEmpty()) {
throw new ParserException(
I18N.getText("macro.function.general.paramCannotBeEmpty", functionName));
} else if (imageString.length() > 8) {
Asset asset;
if (imageString.toLowerCase().startsWith("https://")
&& (imageString.toLowerCase().endsWith(".jpg")
|| imageString.toLowerCase().endsWith(".png")
|| imageString.toLowerCase().endsWith(".webp"))) {
try {
URI uri = new URI(imageString);
URL url = uri.toURL();
BufferedImage imageRAW = ImageIO.read(url);
asset = Asset.createImageAsset(imageName, imageRAW);
} catch (URISyntaxException | MalformedURLException | IllegalArgumentException e) {
throw new ParserException(
I18N.getText("macro.function.input.illegalArgumentType", imageString));
} catch (IOException e1) {
throw new ParserException(I18N.getText("macro.function.html5.invalidURI", imageString));
}
} else {
byte[] imageBytes = Base64.decode(imageString);
String imageCheck;
try {
imageCheck = new String(imageBytes, 0, 4);
} catch (Exception e) {
throw new ParserException(I18N.getText("dragdrop.unsupportedType", functionName));
}
if (imageCheck.equals(FILE_HEADER_WEBP)
|| imageCheck.equals(FILE_HEADER_JPG)
|| imageCheck.equals(FILE_HEADER_PNG)) {
asset = Asset.createImageAsset(imageName, imageBytes);
} else {
throw new ParserException(I18N.getText("dragdrop.unsupportedType", functionName));
}
}
AssetManager.putAsset(asset);
assetId.append(asset.getMD5Key().toString());
return assetId;
} else {
throw new ParserException(
I18N.getText("macro.function.general.wrongParamType", functionName));
}
}

/* getImage, getTokenImage, getTokenPortrait, or getTokenHandout */
int indexSize = -1; // by default, no size added to asset id
if (functionName.equalsIgnoreCase("getImage")) {
Expand All @@ -195,7 +257,6 @@ public Object childEvaluate(
token = FunctionUtil.getTokenFromParam(resolver, functionName, args, 1, 2);
}

StringBuilder assetId = new StringBuilder("asset://");
if (functionName.equalsIgnoreCase("getTokenImage")) {
if (token.getImageAssetId() == null) {
return "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2013,6 +2013,8 @@ macro.function.general.unknownProperty = Error executing "{0}": the
macro.function.general.unknownTokenOnMap = Error executing "{0}": the token name or id "{1}" is unknown on map "{2}".
macro.function.general.wrongNumParam = Function "{0}" requires exactly {1} parameters; {2} were provided.
macro.function.general.listCannotBeEmpty = {0}: string list at argument {1} cannot be empty
macro.function.general.wrongParamType = A parameter is the wrong type.
macro.function.general.paramCannotBeEmpty = A parameter is empty.
# Token Distance functions
# I.e. ONE_TWO_ONE or ONE_ONE_ONE
macro.function.getDistance.invalidMetric = Invalid metric type "{0}".
Expand Down

0 comments on commit 2cd1d38

Please sign in to comment.