Skip to content

Commit

Permalink
Merge pull request #4583 from bubblobill/macrofunction_fix
Browse files Browse the repository at this point in the history
Added parameter checks to metamacro functions
  • Loading branch information
cwisniew authored Dec 24, 2023
2 parents f4a63e5 + d8a9cd2 commit 736655e
Showing 1 changed file with 61 additions and 42 deletions.
103 changes: 61 additions & 42 deletions src/main/java/net/rptools/maptool/client/functions/MacroFunctions.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ public Object childEvaluate(
throws ParserException {

if (functionName.equalsIgnoreCase("createMacro")) {
if (parameters.size() > 3) {
FunctionUtil.checkNumberParam(functionName, parameters, 1, 6);
if (parameters.size() > 4) {
if (parameters.get(4).toString().equalsIgnoreCase("campaign")) {
return createMacro(parameters, false);
} else if (parameters.get(4).toString().equalsIgnoreCase("gm")) {
Expand All @@ -100,10 +101,12 @@ public Object childEvaluate(
} else if (functionName.equalsIgnoreCase("hasMacro")) {
FunctionUtil.checkNumberParam(functionName, parameters, 1, 3);
String label = parameters.get(0).toString();
if (parameters.get(1).toString().equalsIgnoreCase("campaign")) {
return hasMacro(label, false) ? BigDecimal.ONE : BigDecimal.ZERO;
} else if (parameters.get(1).toString().equalsIgnoreCase("gm")) {
return hasMacro(label, true) ? BigDecimal.ONE : BigDecimal.ZERO;
if (parameters.size() > 1) {
if (parameters.get(1).toString().equalsIgnoreCase("campaign")) {
return hasMacro(label, false) ? BigDecimal.ONE : BigDecimal.ZERO;
} else if (parameters.get(1).toString().equalsIgnoreCase("gm")) {
return hasMacro(label, true) ? BigDecimal.ONE : BigDecimal.ZERO;
}
} else {
Token token = FunctionUtil.getTokenFromParam(resolver, functionName, parameters, 1, 2);
return token.getMacroNames(false).contains(label) ? BigDecimal.ONE : BigDecimal.ZERO;
Expand All @@ -112,10 +115,12 @@ public Object childEvaluate(
} else if (functionName.equalsIgnoreCase("getMacros")) {
FunctionUtil.checkNumberParam(functionName, parameters, 0, 3);
String delim = parameters.size() > 0 ? parameters.get(0).toString() : ",";
if (parameters.get(1).toString().equalsIgnoreCase("campaign")) {
return getMacros(delim, MapTool.getCampaign().getMacroButtonPropertiesArray());
} else if (parameters.get(1).toString().equalsIgnoreCase("gm")) {
return getMacros(delim, MapTool.getCampaign().getGmMacroButtonPropertiesArray());
if (parameters.size() > 1) {
if (parameters.get(1).toString().equalsIgnoreCase("campaign")) {
return getMacros(delim, MapTool.getCampaign().getMacroButtonPropertiesArray());
} else if (parameters.get(1).toString().equalsIgnoreCase("gm")) {
return getMacros(delim, MapTool.getCampaign().getGmMacroButtonPropertiesArray());
}
} else {
Token token = FunctionUtil.getTokenFromParam(resolver, functionName, parameters, 1, 2);
return getMacros(delim, token);
Expand All @@ -125,10 +130,12 @@ public Object childEvaluate(
FunctionUtil.checkNumberParam(functionName, parameters, 1, 4);
int index = FunctionUtil.paramAsInteger(functionName, parameters, 0, false);
String delim = parameters.size() > 1 ? parameters.get(1).toString() : ";";
if (parameters.get(2).toString().equalsIgnoreCase("campaign")) {
return getMacroButtonProps(index, delim, false);
} else if (parameters.get(2).toString().equalsIgnoreCase("gm")) {
return getMacroButtonProps(index, delim, true);
if (parameters.size() > 2) {
if (parameters.get(2).toString().equalsIgnoreCase("campaign")) {
return getMacroButtonProps(index, delim, false);
} else if (parameters.get(2).toString().equalsIgnoreCase("gm")) {
return getMacroButtonProps(index, delim, true);
}
} else {
Token token = FunctionUtil.getTokenFromParam(resolver, functionName, parameters, 2, 3);
return getMacroButtonProps(token, index, delim);
Expand All @@ -139,10 +146,12 @@ public Object childEvaluate(
Object value = parameters.get(0);
String props = parameters.get(1).toString();
String delim = parameters.size() > 2 ? parameters.get(2).toString() : ";";
if (parameters.get(3).toString().equalsIgnoreCase("campaign")) {
return setMacroProps(value, props, delim, false);
} else if (parameters.get(3).toString().equalsIgnoreCase("gm")) {
return setMacroProps(value, props, delim, true);
if (parameters.size() > 3) {
if (parameters.get(3).toString().equalsIgnoreCase("campaign")) {
return setMacroProps(value, props, delim, false);
} else if (parameters.get(3).toString().equalsIgnoreCase("gm")) {
return setMacroProps(value, props, delim, true);
}
} else {
Token token = FunctionUtil.getTokenFromParam(resolver, functionName, parameters, 3, 4);
return setMacroProps(value, props, delim, token);
Expand All @@ -153,11 +162,14 @@ public Object childEvaluate(
FunctionUtil.checkNumberParam(functionName, parameters, 1, 4);
String label = parameters.get(0).toString();
String delim = parameters.size() > 1 ? parameters.get(1).toString() : ",";
if (parameters.get(2).toString().equalsIgnoreCase("campaign")) {
return getMacroIndexes(label, delim, MapTool.getCampaign().getMacroButtonPropertiesArray());
} else if (parameters.get(2).toString().equalsIgnoreCase("gm")) {
return getMacroIndexes(
label, delim, MapTool.getCampaign().getGmMacroButtonPropertiesArray());
if (parameters.size() > 2) {
if (parameters.get(2).toString().equalsIgnoreCase("campaign")) {
return getMacroIndexes(
label, delim, MapTool.getCampaign().getMacroButtonPropertiesArray());
} else if (parameters.get(2).toString().equalsIgnoreCase("gm")) {
return getMacroIndexes(
label, delim, MapTool.getCampaign().getGmMacroButtonPropertiesArray());
}
} else {
Token token = FunctionUtil.getTokenFromParam(resolver, functionName, parameters, 2, 3);
return getMacroIndexes(label, delim, token);
Expand All @@ -174,10 +186,12 @@ public Object childEvaluate(
FunctionUtil.blockUntrustedMacro(functionName);
int index = FunctionUtil.paramAsInteger(functionName, parameters, 0, false);
String command = FunctionUtil.paramAsString(functionName, parameters, 1, true);
if (parameters.get(2).toString().equalsIgnoreCase("gm")) {
return setMacroCommand(index, command, true);
} else if (parameters.get(2).toString().equalsIgnoreCase("campaign")) {
return setMacroCommand(index, command, false);
if (parameters.size() > 2) {
if (parameters.get(2).toString().equalsIgnoreCase("gm")) {
return setMacroCommand(index, command, true);
} else if (parameters.get(2).toString().equalsIgnoreCase("campaign")) {
return setMacroCommand(index, command, false);
}
} else {
Token token = FunctionUtil.getTokenFromParam(resolver, functionName, parameters, 2, 3);
return setMacroCommand(index, command, token);
Expand All @@ -186,10 +200,12 @@ public Object childEvaluate(
} else if (functionName.equalsIgnoreCase("getMacroCommand")) {
FunctionUtil.checkNumberParam(functionName, parameters, 1, 3);
int index = FunctionUtil.paramAsInteger(functionName, parameters, 0, false);
if (parameters.get(1).toString().equalsIgnoreCase("gm")) {
return getMacroCommand(index, true);
} else if (parameters.get(1).toString().equalsIgnoreCase("campaign")) {
return getMacroCommand(index, false);
if (parameters.size() > 1) {
if (parameters.get(1).toString().equalsIgnoreCase("gm")) {
return getMacroCommand(index, true);
} else if (parameters.get(1).toString().equalsIgnoreCase("campaign")) {
return getMacroCommand(index, false);
}
} else {
Token token = FunctionUtil.getTokenFromParam(resolver, functionName, parameters, 1, 2);
return getMacroCommand(index, token);
Expand All @@ -202,10 +218,12 @@ public Object childEvaluate(
FunctionUtil.checkNumberParam(functionName, parameters, 1, 4);
String group = parameters.get(0).toString();
String delim = parameters.size() > 1 ? parameters.get(1).toString() : ",";
if (parameters.get(2).toString().equalsIgnoreCase("gm")) {
return getMacroGroup(group, delim, true);
} else if (parameters.get(2).toString().equalsIgnoreCase("campaign")) {
return getMacroGroup(group, delim, false);
if (parameters.size() > 2) {
if (parameters.get(2).toString().equalsIgnoreCase("gm")) {
return getMacroGroup(group, delim, true);
} else if (parameters.get(2).toString().equalsIgnoreCase("campaign")) {
return getMacroGroup(group, delim, false);
}
} else {
Token token = FunctionUtil.getTokenFromParam(resolver, functionName, parameters, 2, 3);
return getMacroGroup(group, delim, token);
Expand All @@ -214,18 +232,19 @@ public Object childEvaluate(
} else if (functionName.equalsIgnoreCase("removeMacro")) {
FunctionUtil.checkNumberParam(functionName, parameters, 1, 3);
int index = FunctionUtil.paramAsInteger(functionName, parameters, 0, false);
if (parameters.get(1).toString().equalsIgnoreCase("gm")) {
return removeMacro(index, true);
} else if (parameters.get(1).toString().equalsIgnoreCase("campaign")) {
return removeMacro(index, false);
if (parameters.size() > 1) {
if (parameters.get(1).toString().equalsIgnoreCase("gm")) {
return removeMacro(index, true);
} else if (parameters.get(1).toString().equalsIgnoreCase("campaign")) {
return removeMacro(index, false);
}
} else {
Token token = FunctionUtil.getTokenFromParam(resolver, functionName, parameters, 1, 2);
return removeMacro(index, token);
}

} else { // should never happen, hopefully ;)
throw new ParserException(I18N.getText(KEY_UNKNOWN_MACRO, functionName));
}
/* code should never happen, hopefully ;) */
throw new ParserException(I18N.getText(KEY_UNKNOWN_MACRO, functionName));
}

/**
Expand Down Expand Up @@ -1053,7 +1072,7 @@ private MacroButtonProperties macroButtonPropertiesFromJSON(
if (json == null) json = JSONMacroFunctions.getInstance().asJsonElement(propString);
JsonObject jobj = json.getAsJsonObject();
if (jobj != null) {
if (!jobj.has("label"))
if (!jobj.has("label") && mbp.getLabel().isEmpty())
throw new ParserException(I18N.getText(KEY_MISSING_LABEL, "createMacro"));
if (jobj.has("command") && !MapTool.getParser().isMacroTrusted()) {
int index = mbp.getIndex();
Expand Down

0 comments on commit 736655e

Please sign in to comment.