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

Add property default and reset js bindings #5156

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 @@ -23,7 +23,6 @@
import net.rptools.maptool.language.I18N;
import net.rptools.maptool.model.GUID;
import net.rptools.maptool.model.Token;
import net.rptools.maptool.model.TokenProperty;
import net.rptools.maptool.model.Zone;
import net.rptools.parser.ParserException;
import org.graalvm.polyglot.HostAccess;
Expand Down Expand Up @@ -140,33 +139,34 @@ public String getRawProperty(String name) {
public String getProperty(String name) {
boolean trusted = JSScriptEngine.inTrustedContext();
String playerId = MapTool.getPlayer().getName();
if (trusted || token.isOwner(playerId)) {
Object val = this.token.getProperty(name);
// Fall back to the property type's default value
// since it's not useful to return nulls and require
// javascript to have to handle defaults when getInfo isn't even bound,
// especially when the value gets unset if it matches the default.
// Evaluation is not performed automatically, use getEvaluatedProperty for that.
if (val == null) {
List<TokenProperty> propertyList =
MapTool.getCampaign()
.getCampaignProperties()
.getTokenPropertyList(this.token.getPropertyType());
if (propertyList != null) {
for (TokenProperty property : propertyList) {
if (name.equalsIgnoreCase(property.getName())) {
val = property.getDefaultValue();
break;
}
}
}
}
if (val == null) {
return null;
}
return "" + val;
if (!trusted && !token.isOwner(playerId)) {
return null;
}
return null;

Object val = this.token.getProperty(name);
// Fall back to the property type's default value
// since it's not useful to return nulls and require
// javascript to have to handle defaults when getInfo isn't even bound,
// especially when the value gets unset if it matches the default.
// Evaluation is not performed automatically, use getEvaluatedProperty for that.
if (val == null) {
val = this.token.getPropertyDefault(name);
}
if (val == null) {
return null;
}
return "" + val;
}

@HostAccess.Export
public String getPropertyDefault(String name) {
boolean trusted = JSScriptEngine.inTrustedContext();
String playerId = MapTool.getPlayer().getName();
if (!trusted && !token.isOwner(playerId)) {
return null;
}

return this.token.getPropertyDefault(name);
}

@HostAccess.Export
Expand All @@ -180,6 +180,22 @@ public String getEvaluatedProperty(String name) {
return "";
}

@HostAccess.Export
public String getEvaluatedPropertyDefault(String name) {
boolean trusted = JSScriptEngine.inTrustedContext();
String playerId = MapTool.getPlayer().getName();
if (!trusted && !token.isOwner(playerId)) {
return null;
}

var res = this.token.getPropertyDefault(name);
if (res == null) {
return null;
}

return "" + this.token.evaluateProperty(null, name, res);
}

@HostAccess.Export
public void setProperty(String name, Object value) {
boolean trusted = JSScriptEngine.inTrustedContext();
Expand All @@ -191,6 +207,19 @@ public void setProperty(String name, Object value) {
}
}

@HostAccess.Export
public boolean resetProperty(String name) {
boolean trusted = JSScriptEngine.inTrustedContext();
String playerId = MapTool.getPlayer().getName();
if (!trusted && !token.isOwner(playerId)) {
return false;
}

this.token.resetProperty(name);
MapTool.serverCommand().updateTokenProperty(token, Token.Update.resetProperty, name);
return true;
}

@HostAccess.Export
public int getX() throws ParserException {
boolean trusted = JSScriptEngine.inTrustedContext();
Expand Down
91 changes: 61 additions & 30 deletions src/main/java/net/rptools/maptool/model/Token.java
Original file line number Diff line number Diff line change
Expand Up @@ -1865,35 +1865,41 @@ public Object getProperty(String key) {
return getPropertyMap().get(key);
}

public Object getEvaluatedProperty(String key) {
return getEvaluatedProperty(null, key);
/**
* Returns the default value of this key for the token's property type.
*
* <p>Searches the property list case-insensitively.
*
* @return null if the property doesn't exist, otherwise the value expression
*/
@Nullable
public String getPropertyDefault(@Nonnull String key) {
List<TokenProperty> propertyList =
MapTool.getCampaign().getCampaignProperties().getTokenPropertyList(propertyType);
if (propertyList == null) {
return null;
}

for (TokenProperty property : propertyList) {
if (key.equalsIgnoreCase(property.getName())) {
return property.getDefaultValue();
}
}

return null;
}

/**
* Returns the evaluated property corresponding to the key.
* Evaluate the provided expression with the resolver
*
* @param resolver the variable resolver to parse code inside the property
* @param key the key of the value
* @return the value
* @param val An expression to evaluate
* @return The evaluated value
*/
public Object getEvaluatedProperty(MapToolVariableResolver resolver, String key) {
Object val = getProperty(key);
if (val == null) {
// Global default ?
List<TokenProperty> propertyList =
MapTool.getCampaign().getCampaignProperties().getTokenPropertyList(propertyType);
if (propertyList != null) {
for (TokenProperty property : propertyList) {
if (key.equalsIgnoreCase(property.getName())) {
val = property.getDefaultValue();
break;
}
}
}
}
if (val == null) {
return "";
}
@Nonnull
public Object evaluateProperty(
@Nullable MapToolVariableResolver resolver, @Nonnull String key, @Nonnull Object val) {
if (val.toString().trim().startsWith("{")) {
/*
* The normal Gson evaluator was too lenient in identifying JSON objects, so we had to move
Expand Down Expand Up @@ -1935,19 +1941,44 @@ public Object getEvaluatedProperty(MapToolVariableResolver resolver, String key)
val = val.toString();
}
if (val == null) {
val = "";
} else {
// Finally we try convert it to a JSON object. Fixes #1560.
if (val.toString().trim().startsWith("{")) {
JsonElement json = JSONMacroFunctions.getInstance().asJsonElement(val.toString());
if (json.isJsonObject()) {
return json;
}
return "";
}
// Finally we try convert it to a JSON object. Fixes #1560.
if (val.toString().trim().startsWith("{")) {
JsonElement json = JSONMacroFunctions.getInstance().asJsonElement(val.toString());
if (json.isJsonObject()) {
return json;
}
}

return val;
}

@Nonnull
public Object getEvaluatedProperty(@Nonnull String key) {
return getEvaluatedProperty(null, key);
}

/**
* Returns the evaluated property corresponding to the key.
*
* @param resolver the variable resolver to parse code inside the property
* @param key the key of the value
* @return the value
*/
@Nonnull
public Object getEvaluatedProperty(
@Nullable MapToolVariableResolver resolver, @Nonnull String key) {
Object val = getProperty(key);
if (val == null) {
val = getPropertyDefault(key);
}
if (val == null) {
return "";
}
return evaluateProperty(resolver, key, val);
}

/**
* @return all property names, all in lowercase.
*/
Expand Down