Skip to content

Commit

Permalink
Add js getRawProperty and getEvaluatedProperty
Browse files Browse the repository at this point in the history
Previously you could tell that the property was unset by if it returned
the string "null", so getRawProperty gives that feature back
but returning a null so you can use ?? to default it yourself.

getEvaluatedProperty adds the ability to handle calculated defaults.
getProperty was not made to use getEvaluatedProperty
because a javascript function that knows the campaign doesn't use
calculated defaults could use it to save on performance,
but also that switching to evaluating by default is more likely to be a
breaking change.

The code for handling a missing parameter in earlier versions is awkward
so it's likely that code that uses getProperty is used in campaigns
where there is no default value to be reset to and all properties are
explicitly set so doesn't trip the awkward behaviour.

However, if this code contained objects that could be mis-parsed as
macros beginning to use macros by default would be a breaking change.
  • Loading branch information
fishface60 committed Sep 8, 2024
1 parent 95e95fd commit 23a7803
Showing 1 changed file with 27 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,21 @@ 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();
Expand All @@ -130,6 +145,7 @@ public String getProperty(String name) {
// 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());
Expand All @@ -150,6 +166,17 @@ public String getProperty(String name) {
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 "";
}

@HostAccess.Export
public void setProperty(String name, Object value) {
boolean trusted = JSScriptEngine.inTrustedContext();
Expand Down

0 comments on commit 23a7803

Please sign in to comment.