Skip to content

Commit e30553f

Browse files
Merge pull request #5708 from kwvanderlinde/bugfix/5706-defineFunction-this
1.18 - fix for lib: prefix handling
2 parents a7939f8 + a6a3a9e commit e30553f

File tree

12 files changed

+75
-91
lines changed

12 files changed

+75
-91
lines changed

src/main/java/net/rptools/maptool/client/MapToolLineParser.java

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1232,28 +1232,24 @@ public String runMacro(
12321232
switch (mloc.getSource()) {
12331233
case library, uri -> {
12341234
try {
1235-
String namespace;
1236-
String macro;
1237-
switch (mloc.getSource()) {
1238-
case uri -> {
1239-
namespace = mloc.getUri().getHost();
1240-
macro = mloc.getUri().toString();
1241-
}
1242-
case library -> {
1243-
namespace = mloc.getLocation().replaceFirst("^(?i)lib:", "");
1244-
macro = mloc.getName();
1245-
}
1246-
case null, default -> {
1247-
throw new IllegalStateException("Unexpected value: " + mloc.getSource());
1248-
}
1249-
}
1235+
var namespace = mloc.getLocation();
12501236
var lib = new LibraryManager().getLibrary(namespace);
12511237
if (lib.isEmpty()) {
12521238
throw new ParserException(
12531239
I18N.getText("lineParser.unknownLibToken", mloc.getLocation()));
12541240
}
12551241
var library = lib.get();
1256-
var macroInfo = library.getMTScriptMacroInfo(macro).get();
1242+
1243+
var macroInfoFuture =
1244+
switch (mloc.getSource()) {
1245+
case uri -> library.getMTScriptMacroInfoForUriPath(mloc.getName());
1246+
case library -> library.getMTScriptMacroInfo(mloc.getName());
1247+
case null, default -> {
1248+
throw new IllegalStateException("Unexpected value: " + mloc.getSource());
1249+
}
1250+
};
1251+
1252+
var macroInfo = macroInfoFuture.get();
12571253
if (macroInfo.isEmpty()) {
12581254
// if the macro source is the same as the location then check private macros.
12591255
if (!contextStackEmpty()) {

src/main/java/net/rptools/maptool/client/functions/DefineMacroFunction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public Object childEvaluate(
6060
if (macro.toLowerCase().endsWith("@this")) {
6161
macro =
6262
macro.substring(0, macro.length() - 4)
63-
+ MapTool.getParser().getMacroSource().getLocation();
63+
+ MapTool.getParser().getMacroSource().getCallableLocation();
6464
}
6565

6666
boolean ignoreOutput = false;

src/main/java/net/rptools/maptool/client/functions/ParserPropertyFunctions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public Object childEvaluate(
8787
MapToolMacroContext mc = mtlParser.getContext();
8888
mco.addProperty("stackSize", mtlParser.getContextStackSize());
8989
mco.addProperty("name", mc.getName());
90-
mco.addProperty("source", mc.getSource().getLocation());
90+
mco.addProperty("source", mc.getSource().getCallableLocation());
9191
mco.addProperty("trusted", mc.isTrusted());
9292
if (mc.getMacroButtonIndex() >= 0) {
9393
mco.addProperty("buttonIndex", mc.getMacroButtonIndex());

src/main/java/net/rptools/maptool/client/functions/TokenPropertyFunctions.java

Lines changed: 18 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -508,19 +508,8 @@ public Object childEvaluate(
508508
if (functionName.equalsIgnoreCase("getLibProperty")) {
509509
FunctionUtil.checkNumberParam(functionName, parameters, 1, 2);
510510
String propertyName = parameters.get(0).toString();
511-
String location;
512-
if (parameters.size() > 1) {
513-
location = parameters.get(1).toString();
514-
} else {
515-
location = MapTool.getParser().getMacroSource().getLocation();
516-
}
517511

518-
String libName;
519-
if (location.toLowerCase().startsWith("lib:")) {
520-
libName = location.substring(4);
521-
} else {
522-
libName = location;
523-
}
512+
String libName = findLibNamespaceFromParams(parameters, 1, false);
524513

525514
try {
526515
var library =
@@ -550,20 +539,7 @@ public Object childEvaluate(
550539
FunctionUtil.checkNumberParam(functionName, parameters, 2, 3);
551540
String property = parameters.get(0).toString();
552541
Object value = parameters.get(1);
553-
554-
String location;
555-
if (parameters.size() > 2) {
556-
location = parameters.get(2).toString();
557-
} else {
558-
location = MapTool.getParser().getMacroSource().getLocation();
559-
}
560-
561-
String libName;
562-
if (location.toLowerCase().startsWith("lib:")) {
563-
libName = location.substring(4);
564-
} else {
565-
libName = location;
566-
}
542+
String libName = findLibNamespaceFromParams(parameters, 2, false);
567543

568544
var library =
569545
new LibraryManager()
@@ -589,22 +565,7 @@ public Object childEvaluate(
589565
*/
590566
if (functionName.equalsIgnoreCase("getLibPropertyNames")) {
591567
FunctionUtil.checkNumberParam(functionName, parameters, 0, 2);
592-
String location;
593-
if (parameters.size() > 0) {
594-
location = parameters.get(0).toString();
595-
if (location.equals("*") || location.equalsIgnoreCase("this")) {
596-
location = MapTool.getParser().getMacroSource().getLocation();
597-
}
598-
} else {
599-
location = MapTool.getParser().getMacroSource().getLocation();
600-
}
601-
602-
String libName;
603-
if (location.toLowerCase().startsWith("lib:")) {
604-
libName = location.substring(4);
605-
} else {
606-
libName = location;
607-
}
568+
String libName = findLibNamespaceFromParams(parameters, 0, true);
608569
String delim = parameters.size() > 1 ? parameters.get(1).toString() : ",";
609570
return getMatchingLibProperties(libName, delim, ".*", functionName);
610571
}
@@ -614,23 +575,8 @@ public Object childEvaluate(
614575
*/
615576
if (functionName.equalsIgnoreCase("getMatchingLibProperties")) {
616577
FunctionUtil.checkNumberParam(functionName, parameters, 1, 3);
617-
String location;
618578
String pattern = parameters.get(0).toString();
619-
if (parameters.size() > 1) {
620-
location = parameters.get(1).toString();
621-
if (location.equals("*") || location.equalsIgnoreCase("this")) {
622-
location = MapTool.getParser().getMacroSource().getLocation();
623-
}
624-
} else {
625-
location = MapTool.getParser().getMacroSource().getLocation();
626-
}
627-
628-
String libName;
629-
if (location.toLowerCase().startsWith("lib:")) {
630-
libName = location.substring(4);
631-
} else {
632-
libName = location;
633-
}
579+
String libName = findLibNamespaceFromParams(parameters, 1, true);
634580

635581
String delim = parameters.size() > 2 ? parameters.get(2).toString() : ",";
636582
return getMatchingLibProperties(libName, delim, pattern, functionName);
@@ -1338,4 +1284,18 @@ private BigDecimal getBigDecimalFromParam(String functionName, List<Object> para
13381284
"macro.function.general.argumentTypeN", functionName, index, param.toString()));
13391285
}
13401286
}
1287+
1288+
private String findLibNamespaceFromParams(
1289+
List<Object> parameters, int libIndex, boolean allowWildcards) {
1290+
if (parameters.size() <= libIndex) {
1291+
return MapTool.getParser().getMacroSource().getLocation();
1292+
}
1293+
1294+
var location = parameters.get(libIndex).toString();
1295+
if (allowWildcards && (location.equals("*") || location.equalsIgnoreCase("this"))) {
1296+
return MapTool.getParser().getMacroSource().getLocation();
1297+
}
1298+
1299+
return location.toLowerCase().startsWith("lib:") ? location.substring(4) : location;
1300+
}
13411301
}

src/main/java/net/rptools/maptool/client/macro/impl/SayMacro.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public void execute(MacroContext context, String macro, MapToolMacroContext exec
3737
null,
3838
MapTool.getParser().isMacroPathTrusted(),
3939
executionContext.getName(),
40-
executionContext.getSource().getLocation());
40+
executionContext.getSource().getCallableLocation());
4141
} else {
4242
msg = MessageUtil.getFormattedSay(macro, null, false, null, null);
4343
}

src/main/java/net/rptools/maptool/client/macro/impl/ToGMMacro.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public void execute(MacroContext context, String macro, MapToolMacroContext exec
3636
MapTool.getPlayer().getName(),
3737
MapTool.getParser().isMacroPathTrusted(),
3838
executionContext.getName(),
39-
executionContext.getSource().getLocation());
39+
executionContext.getSource().getCallableLocation());
4040
} else {
4141
toGmMsg =
4242
MessageUtil.getFormattedToGmRecipient(

src/main/java/net/rptools/maptool/model/library/Library.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,14 @@ public interface Library {
154154
*/
155155
CompletableFuture<LibraryInfo> getLibraryInfo();
156156

157+
/**
158+
* Returns the information about MapTool Macro Script on this library.
159+
*
160+
* @param macroPath The path to the macro, as used in lib: URIs.
161+
* @return the information about the MapTool Macro Script.
162+
*/
163+
CompletableFuture<Optional<MTScriptMacroInfo>> getMTScriptMacroInfoForUriPath(String macroPath);
164+
157165
/**
158166
* Returns the information about MapTool Macro Script on this library.
159167
*

src/main/java/net/rptools/maptool/model/library/addon/AddOnLibrary.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,13 @@ private CompletableFuture<Optional<MTScriptMacroInfo>> getMacroInfo(
359359
});
360360
}
361361

362+
@Override
363+
public CompletableFuture<Optional<MTScriptMacroInfo>> getMTScriptMacroInfoForUriPath(
364+
String macroPath) {
365+
// For add-ons, the path and the macro name are the same.
366+
return getMTScriptMacroInfo(macroPath);
367+
}
368+
362369
@Override
363370
public CompletableFuture<Optional<MTScriptMacroInfo>> getMTScriptMacroInfo(String macroName) {
364371
var macro = mtsFunctionAssetMap.get(MTSCRIPT_PUBLIC_DIR + macroName);

src/main/java/net/rptools/maptool/model/library/builtin/ClassPathAddOnLibrary.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,12 @@ public CompletableFuture<LibraryInfo> getLibraryInfo() {
131131
return addOnLibrary.getLibraryInfo();
132132
}
133133

134+
@Override
135+
public CompletableFuture<Optional<MTScriptMacroInfo>> getMTScriptMacroInfoForUriPath(
136+
String macroPath) {
137+
return addOnLibrary.getMTScriptMacroInfoForUriPath(macroPath);
138+
}
139+
134140
@Override
135141
public CompletableFuture<Optional<MTScriptMacroInfo>> getMTScriptMacroInfo(String macroName) {
136142
return addOnLibrary.getMTScriptMacroInfo(macroName);

src/main/java/net/rptools/maptool/model/library/builtin/MapToolBuiltInLibrary.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,12 @@ public CompletableFuture<LibraryInfo> getLibraryInfo() {
223223
licenseFile.isEmpty() ? null : licenseFile));
224224
}
225225

226+
@Override
227+
public CompletableFuture<Optional<MTScriptMacroInfo>> getMTScriptMacroInfoForUriPath(
228+
String macroPath) {
229+
return CompletableFuture.completedFuture(Optional.empty());
230+
}
231+
226232
@Override
227233
public CompletableFuture<Optional<MTScriptMacroInfo>> getMTScriptMacroInfo(String macroName) {
228234
return CompletableFuture.completedFuture(Optional.empty());

0 commit comments

Comments
 (0)