Skip to content

Commit

Permalink
Merge pull request #4900 from fishface60/getProperty
Browse files Browse the repository at this point in the history
Rework javascript getProperty API to handle missing properties better
  • Loading branch information
cwisniew authored Sep 12, 2024
2 parents c0df1db + 2377222 commit aed33aa
Showing 1 changed file with 51 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
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 @@ -119,12 +120,61 @@ public String getId() {
return "" + token.getId();
}

@HostAccess.Export
public String getRawProperty(String name) {
boolean trusted = JSScriptEngine.inTrustedContext();
String playerId = MapTool.getPlayer().getName();
if (trusted || token.isOwner(playerId)) {
Object val = this.token.getProperty(name);
// Short-circuit to returning null so we don't return "null"
if (val == null) {
return null;
}
return "" + val;
}
return null;
}

@HostAccess.Export
public String getProperty(String name) {
boolean trusted = JSScriptEngine.inTrustedContext();
String playerId = MapTool.getPlayer().getName();
if (trusted || token.isOwner(playerId)) {
return "" + this.token.getProperty(name);
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;
}
return null;
}

@HostAccess.Export
public String getEvaluatedProperty(String name) {
boolean trusted = JSScriptEngine.inTrustedContext();
String playerId = MapTool.getPlayer().getName();
if (trusted || token.isOwner(playerId)) {
Object val = this.token.getEvaluatedProperty(name);
return "" + val;
}
return "";
}
Expand Down

0 comments on commit aed33aa

Please sign in to comment.