Skip to content

Commit

Permalink
Merge pull request #3528 from cwisniew/fix-prop-string
Browse files Browse the repository at this point in the history
Fix for add on libproperty compat layer treating everything as strings
  • Loading branch information
Phergus authored Aug 8, 2022
2 parents 454f96c + 3a59cb7 commit 90f8763
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,8 @@
import java.math.BigDecimal;
import java.util.List;
import java.util.concurrent.ExecutionException;
import net.rptools.lib.MD5Key;
import net.rptools.maptool.client.MapTool;
import net.rptools.maptool.client.functions.json.JSONMacroFunctions;
import net.rptools.maptool.language.I18N;
import net.rptools.maptool.model.AssetManager;
import net.rptools.maptool.model.gamedata.DataStore;
import net.rptools.maptool.model.gamedata.DataStoreManager;
import net.rptools.maptool.model.gamedata.MTScriptDataConversion;
Expand All @@ -43,15 +40,14 @@ public DataFunctions() {
super(
0,
4,
/* Commented out are only used for debugging.
"data.listTypes",
"data.listNamespaces",
"data.createNamespace",
/*"data.createNamespace",*/
"data.setData",
"data.getData",
"data.listData",
"data.clearData",
"data.clearNamespace",
"data.removeData",
/*"data.clearNamespace",
"data.clearAllData",*/
"data.getStaticData");
}
Expand All @@ -62,21 +58,22 @@ public Object childEvaluate(
throws ParserException {

String fName = functionName.toLowerCase();
if (!MapTool.getParser().isMacroTrusted()) {
throw new ParserException(I18N.getText("macro.function.general.noPerm", functionName));
}

try {
var dataStore = new DataStoreManager().getDefaultDataStore();

switch (fName) {
case "data.listtypes" -> {
MapTool.addLocalMessage(I18N.getText("msg.warning.prerelease.only", functionName));
FunctionUtil.checkNumberParam(functionName, parameters, 0, 0);
JsonArray json = new JsonArray();
for (String type : dataStore.getPropertyTypes().get()) {
json.add(type);
}
return json;
}
case "data.listnamespaces" -> {
MapTool.addLocalMessage(I18N.getText("msg.warning.prerelease.only", functionName));
FunctionUtil.checkNumberParam(functionName, parameters, 1, 1);
JsonArray json = new JsonArray();
for (String namespace :
Expand All @@ -86,63 +83,27 @@ public Object childEvaluate(
return json;
}
case "data.createnamespace" -> {
MapTool.addLocalMessage(I18N.getText("msg.warning.prerelease.only", functionName));
FunctionUtil.checkNumberParam(functionName, parameters, 2, 2);
dataStore.createNamespace(parameters.get(0).toString(), parameters.get(1).toString());
return "";
}
case "data.setdata" -> {
MapTool.addLocalMessage(I18N.getText("msg.warning.prerelease.only", functionName));
FunctionUtil.checkNumberParam(functionName, parameters, 4, 4);
String type = parameters.get(0).toString();
String namespace = parameters.get(1).toString();
String name = parameters.get(2).toString();
Object value = parameters.get(3);
if (value instanceof JsonObject jo) {
dataStore.setJsonObjectProperty(type, namespace, name, jo);
} else if (value instanceof JsonArray ja) {
dataStore.setJsonArrayProperty(type, namespace, name, ja);
} else if (value instanceof String s) {
if (s.trim().startsWith("asset://")) {
var asset = AssetManager.getAsset(new MD5Key(s.substring(8)));
dataStore.setAssetProperty(type, namespace, name, asset);
} else {
if (s.trim().startsWith("{") || s.trim().startsWith("[")) {
var json = JSONMacroFunctions.getInstance().asJsonElement(s);
if (json.isJsonObject()) {
dataStore.setJsonObjectProperty(type, namespace, name, json.getAsJsonObject());
} else if (json.isJsonArray()) {
dataStore.setJsonArrayProperty(type, namespace, name, json.getAsJsonArray());
} else {
dataStore.setStringProperty(type, namespace, name, s);
}
} else {
dataStore.setStringProperty(type, namespace, name, s);
}
}
} else if (value instanceof Boolean b) {
dataStore.setBooleanProperty(type, namespace, name, b);
} else if (value instanceof Number n) {
if (n.intValue() == n.doubleValue()) {
dataStore.setLongProperty(type, namespace, name, n.longValue());
} else {
dataStore.setDoubleProperty(type, namespace, name, n.doubleValue());
}
} else {
dataStore.setStringProperty(type, namespace, name, value.toString());
}

dataStore.setProperty(
type, namespace, new MTScriptDataConversion().parseMTScriptString(name, value));
return "";
}
case "data.listdata" -> {
MapTool.addLocalMessage(I18N.getText("msg.warning.prerelease.only", functionName));
FunctionUtil.checkNumberParam(functionName, parameters, 2, 2);
String type = parameters.get(0).toString();
String namespace = parameters.get(1).toString();
return listData(dataStore, type, namespace);
}
case "data.getdata" -> {
MapTool.addLocalMessage(I18N.getText("msg.warning.prerelease.only", functionName));
FunctionUtil.checkNumberParam(functionName, parameters, 3, 3);
String type = parameters.get(0).toString();
String namespace = parameters.get(1).toString();
Expand All @@ -166,7 +127,6 @@ public Object childEvaluate(
return "";
}
case "data.clearnamespace" -> {
MapTool.addLocalMessage(I18N.getText("msg.warning.prerelease.only", functionName));
FunctionUtil.checkNumberParam(functionName, parameters, 2, 2);
String type = parameters.get(0).toString();
String namespace = parameters.get(1).toString();
Expand All @@ -177,8 +137,7 @@ public Object childEvaluate(

return "";
}
case "data.cleardata" -> {
MapTool.addLocalMessage(I18N.getText("msg.warning.prerelease.only", functionName));
case "data.removedata" -> {
FunctionUtil.checkNumberParam(functionName, parameters, 3, 3);
String type = parameters.get(0).toString();
String namespace = parameters.get(1).toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ public Object childEvaluate(
if (functionName.equalsIgnoreCase("setLibProperty")) {
FunctionUtil.checkNumberParam(functionName, parameters, 2, 3);
String property = parameters.get(0).toString();
String value = parameters.get(1).toString();
Object value = parameters.get(1);

String location;
if (parameters.size() > 2) {
Expand All @@ -575,7 +575,12 @@ public Object childEvaluate(
"macro.function.tokenProperty.unknownLibToken",
functionName,
libName)));
library.getLibraryData().thenCompose(libData -> libData.setStringData(property, value));
library
.getLibraryData()
.thenCompose(
libData ->
libData.setData(
new MTScriptDataConversion().parseMTScriptString(property, value)));
return "";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@
*/
package net.rptools.maptool.model.gamedata;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.math.BigDecimal;
import net.rptools.lib.MD5Key;
import net.rptools.maptool.model.gamedata.data.DataType;
import net.rptools.maptool.model.gamedata.data.DataValue;
import net.rptools.maptool.model.gamedata.data.DataValueFactory;

/** Class for converting between GameData and MT Macro Script types. */
public class MTScriptDataConversion {
Expand Down Expand Up @@ -65,4 +69,64 @@ public Object convertToMTScriptDereferenceType(DataValue value) {
return convertToMTScriptType(value);
}
}

/**
* Converts a MT Script type to a DataValue. For Asset types the asset handle is returned.
*
* @param name The name of the value.
* @param value The value to convert.
* @return A DataValue from the given value.
*/
public DataValue parseMTScriptString(String name, Object value) {
if (value == null) {
return DataValueFactory.undefined(name);
}

if (value instanceof String strval) {
if (strval.isEmpty()) {
return DataValueFactory.undefined(name);
}
if (strval.trim().startsWith("{") || strval.trim().startsWith("[")) {
try {
JsonObject jobj = JsonParser.parseString(strval).getAsJsonObject();
if (jobj.isJsonArray()) {
return DataValueFactory.fromJsonArray(name, jobj.getAsJsonArray());
} else if (jobj.isJsonObject()) {
return DataValueFactory.fromJsonObject(name, jobj);
}
} catch (Exception e) {
// Ignore
}
}
if (strval.startsWith("asset://")) {
return DataValueFactory.fromAsset(name, new MD5Key(strval.substring("asset://".length())));
}

if (strval.equalsIgnoreCase("true") || strval.equalsIgnoreCase("false")) {
return DataValueFactory.fromBoolean(name, strval.equalsIgnoreCase("true"));
}
}

if (value instanceof Number nval) {
if (nval.longValue() == nval.doubleValue()) {
return DataValueFactory.fromLong(name, nval.longValue());
} else {
return DataValueFactory.fromDouble(name, nval.doubleValue());
}
}

if (value instanceof Boolean bval) {
return DataValueFactory.fromBoolean(name, bval);
}

if (value instanceof JsonObject jo) {
if (jo.isJsonArray()) {
return DataValueFactory.fromJsonArray(name, jo.getAsJsonArray());
} else if (jo.isJsonObject()) {
return DataValueFactory.fromJsonObject(name, jo);
}
}

return DataValueFactory.fromString(name, value.toString());
}
}

0 comments on commit 90f8763

Please sign in to comment.