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

Rework javascript getProperty API to handle missing properties better #4900

Merged
merged 2 commits into from
Sep 12, 2024
Merged
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 @@ -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
Loading