Skip to content

Commit

Permalink
merge
Browse files Browse the repository at this point in the history
  • Loading branch information
cwisniew committed Sep 29, 2023
2 parents 3ff4834 + e479bb1 commit 368d241
Show file tree
Hide file tree
Showing 34 changed files with 468 additions and 64 deletions.
4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,10 @@ dependencies {
implementation 'com.github.jknack:handlebars:4.3.1'
implementation 'com.github.jknack:handlebars-helpers:4.3.1'


// Built In Add-on Libraries
implementation 'com.github.RPTools:maptool-builtin-addons:1.3'

// For advanced dice roller
implementation 'com.github.RPTools:advanced-dice-roller:1.0.3'

Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ org.gradle.jvmargs=--add-exports jdk.compiler/com.sun.tools.javac.api=ALL-UNNAME
--add-exports jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED \
--add-exports jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED \
--add-exports jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED \
--add-exports jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED
--add-exports jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED
3 changes: 3 additions & 0 deletions src/main/java/net/rptools/maptool/client/MapTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
import net.rptools.maptool.client.ui.zone.ZoneRenderer;
import net.rptools.maptool.client.ui.zone.ZoneRendererFactory;
import net.rptools.maptool.events.MapToolEventBus;
import net.rptools.maptool.events.ZoneLoadedListener;
import net.rptools.maptool.language.I18N;
import net.rptools.maptool.model.AssetManager;
import net.rptools.maptool.model.Campaign;
Expand Down Expand Up @@ -158,6 +159,7 @@ public class MapTool {
private static List<Player> playerList;
private static LocalPlayer player;
private static PlayerZoneListener playerZoneListener;
private static ZoneLoadedListener zoneLoadedListener;

private static MapToolConnection conn;
private static ClientMessageHandler handler;
Expand Down Expand Up @@ -679,6 +681,7 @@ private static void initialize() {
try {
player = new LocalPlayer("", Player.Role.GM, ServerConfig.getPersonalServerGMPassword());
playerZoneListener = new PlayerZoneListener();
zoneLoadedListener = new ZoneLoadedListener();
Campaign cmpgn = CampaignFactory.createBasicCampaign();
// This was previously being done in the server thread and didn't always get done
// before the campaign was accessed by the postInitialize() method below.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,13 @@ public Object HostObjectToJavaScriptType(Object obj) {
return jPrim.getAsString();
}
}
} else if (obj instanceof BigDecimal) {
BigDecimal bd = (BigDecimal) obj;
try {
return Long.valueOf(bd.longValueExact());
} catch (ArithmeticException e) {
return Double.valueOf(bd.doubleValue());
}
}
return obj;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import net.rptools.parser.function.AbstractFunction;

public class MapFunctions extends AbstractFunction {
public static final String ON_CHANGE_MAP_CALLBACK = "onChangeMap";
private static final MapFunctions instance = new MapFunctions();

private MapFunctions() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.Option;
import com.jayway.jsonpath.PathNotFoundException;
import com.jayway.jsonpath.spi.json.GsonJsonProvider;
import com.jayway.jsonpath.spi.mapper.GsonMappingProvider;
import java.math.BigDecimal;
Expand Down Expand Up @@ -886,7 +887,12 @@ private JsonElement jsonPathDelete(JsonElement json, String path) {
private JsonElement jsonPathPut(JsonElement json, String path, String key, Object info) {
Object value = asJsonElement(info);

return JsonPath.using(jaywayConfig).parse(shallowCopy(json)).put(path, key, value).json();
try {
return JsonPath.using(jaywayConfig).parse(shallowCopy(json)).put(path, key, value).json();
} catch (PathNotFoundException ex) {
// Return original json, this is to preserve backwards compatability pre library update
return json;
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/
package net.rptools.maptool.client.script.javascript;

import com.google.gson.*;
import java.util.*;
import net.rptools.maptool.client.MapToolVariableResolver;
import net.rptools.maptool.client.functions.*;
Expand All @@ -25,6 +26,7 @@
import org.graalvm.polyglot.*;

public class JSMacro extends AbstractFunction {
private static final Gson gson = new GsonBuilder().setPrettyPrinting().create();
private static JSMacro instance = new JSMacro();
private static HashMap<String, JSAPIRegisteredMacro> macros = new HashMap<>();

Expand Down Expand Up @@ -60,7 +62,11 @@ public Object childEvaluate(
if (ret instanceof Value val) {
return MacroJavaScriptBridge.getInstance().ValueToMTScriptType(val, new ArrayList());
}
return MacroJavaScriptBridge.getInstance().HostObjectToMTScriptType(ret, new ArrayList());
Object r = MacroJavaScriptBridge.getInstance().HostObjectToMTScriptType(ret, new ArrayList());
if (r instanceof List || r instanceof AbstractMap) {
return gson.toJson(r);
}
return r;
}
return "";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import net.rptools.maptool.language.I18N;
import net.rptools.maptool.model.GUID;
import net.rptools.maptool.model.Token;
import net.rptools.maptool.model.Zone;
import net.rptools.parser.ParserException;
import org.graalvm.polyglot.HostAccess;

Expand All @@ -33,6 +34,7 @@ public String serializeToString() {
private final Token token;
private Set<String> names;
private Iterator<String> names_iter;
private Zone map;

public JSAPIToken(Token token) {
this.token = token;
Expand All @@ -58,6 +60,7 @@ public void setNotes(String notes) {
String playerId = MapTool.getPlayer().getName();
if (trusted || token.isOwner(playerId)) {
token.setNotes(notes);
MapTool.serverCommand().updateTokenProperty(token, Token.Update.setNotes, notes);
}
}

Expand All @@ -77,6 +80,7 @@ public void setName(String name) {
String playerId = MapTool.getPlayer().getName();
if (trusted || token.isOwner(playerId)) {
token.setName(name);
MapTool.serverCommand().updateTokenProperty(token, Token.Update.setName, name);
}
}

Expand Down Expand Up @@ -130,6 +134,8 @@ public void setProperty(String name, Object value) {
String playerId = MapTool.getPlayer().getName();
if (trusted || token.isOwner(playerId)) {
this.token.setProperty(name, value.toString());
MapTool.serverCommand()
.updateTokenProperty(token, Token.Update.setProperty, name, value.toString());
}
}

Expand Down Expand Up @@ -177,4 +183,20 @@ public void setY(int y) {
public boolean isOwner(String playerID) {
return this.token.isOwner(playerID);
}

@HostAccess.Export
public boolean isOnCurrentMap() {
Token findToken =
MapTool.getFrame().getCurrentZoneRenderer().getZone().getToken(new GUID(this.getId()));
return this.token == findToken;
}

public void setMap(Zone m) {
this.map = m;
}

@HostAccess.Export
public String getMapName() {
return this.map.getDisplayName();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import net.rptools.maptool.client.MapTool;
import net.rptools.maptool.client.script.javascript.*;
import net.rptools.maptool.client.ui.zone.ZoneRenderer;
import net.rptools.maptool.model.GUID;
import net.rptools.maptool.model.Token;
import org.graalvm.polyglot.HostAccess;

Expand Down Expand Up @@ -91,10 +92,40 @@ public JSAPIToken getSelected() {

@HostAccess.Export
public JSAPIToken getTokenByID(String uuid) {
JSAPIToken token = new JSAPIToken(uuid);
if (JSScriptEngine.inTrustedContext() || token.isOwner(MapTool.getPlayer().getName())) {
JSAPIToken token = null;
Token findToken =
MapTool.getFrame().getCurrentZoneRenderer().getZone().getToken(new GUID(uuid));
if (findToken != null) {
token = new JSAPIToken(findToken);
token.setMap(MapTool.getFrame().getCurrentZoneRenderer().getZone());
} else {
List<ZoneRenderer> zrenderers = MapTool.getFrame().getZoneRenderers();
for (ZoneRenderer zr : zrenderers) {
findToken = zr.getZone().resolveToken(uuid);
if (findToken != null) {
token = new JSAPIToken(findToken);
token.setMap(zr.getZone());
break;
}
}
}
if (token != null
&& (JSScriptEngine.inTrustedContext() || token.isOwner(MapTool.getPlayer().getName()))) {
return token;
}
return null;
}

@HostAccess.Export
public JSAPIToken getMapTokenByID(String uuid) {
JSAPIToken token = null;
Token findToken =
MapTool.getFrame().getCurrentZoneRenderer().getZone().getToken(new GUID(uuid));
if (findToken != null
&& (JSScriptEngine.inTrustedContext() || token.isOwner(MapTool.getPlayer().getName()))) {
token = new JSAPIToken(findToken);
token.setMap(MapTool.getFrame().getCurrentZoneRenderer().getZone());
}
return token;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -153,43 +153,6 @@ public void handleAddedNode(Object object) {
target.addEventListener("click", HTMLWebViewManager.this::getDataAndSubmit, true);
}
}

// Add listeners to the node's descendant as they don't trigger mutation observer.
NodeList nodeList;

// Add event handlers for <a> hyperlinks.
nodeList = addedNode.getElementsByTagName("a");
for (int i = 0; i < nodeList.getLength(); i++) {
EventTarget node = (EventTarget) nodeList.item(i);
node.addEventListener("click", HTMLWebViewManager.this::fixHref, true);
}

// Add event handlers for hyperlinks for maps.
nodeList = addedNode.getElementsByTagName("area");
for (int i = 0; i < nodeList.getLength(); i++) {
EventTarget node = (EventTarget) nodeList.item(i);
node.addEventListener("click", HTMLWebViewManager.this::fixHref, true);
}

// Set the "submit" handler to get the data on submission not based on buttons
nodeList = addedNode.getElementsByTagName("form");
for (int i = 0; i < nodeList.getLength(); i++) {
EventTarget target = (EventTarget) nodeList.item(i);
target.addEventListener("submit", HTMLWebViewManager.this::getDataAndSubmit, true);
}

// Set the "submit" handler to get the data on submission based on input
nodeList = addedNode.getElementsByTagName("input");
for (int i = 0; i < nodeList.getLength(); i++) {
EventTarget target = (EventTarget) nodeList.item(i);
target.addEventListener("click", HTMLWebViewManager.this::getDataAndSubmit, true);
}
// Set the "submit" handler to get the data on submission based on button
nodeList = addedNode.getElementsByTagName("button");
for (int i = 0; i < nodeList.getLength(); i++) {
EventTarget target = (EventTarget) nodeList.item(i);
target.addEventListener("click", HTMLWebViewManager.this::getDataAndSubmit, true);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ public class MapToolScriptSyntax extends MapToolScriptTokenMaker {

static String[] RESERVED_WORDS_2 = {
"onCampaignLoad",
"onChangeMap",
"onChangeSelection",
"onMouseOverEvent",
TokenMoveFunctions.ON_MULTIPLE_TOKENS_MOVED_COMPLETE_CALLBACK,
Expand Down
41 changes: 41 additions & 0 deletions src/main/java/net/rptools/maptool/events/ZoneLoadedListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* This software Copyright by the RPTools.net development team, and
* licensed under the Affero GPL Version 3 or, at your option, any later
* version.
*
* MapTool Source Code is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU Affero General Public
* License * along with this source Code. If not, please visit
* <http://www.gnu.org/licenses/> and specifically the Affero license
* text at <http://www.gnu.org/licenses/agpl.html>.
*/
package net.rptools.maptool.events;

import static net.rptools.maptool.client.functions.MapFunctions.ON_CHANGE_MAP_CALLBACK;

import com.google.common.eventbus.Subscribe;
import java.util.Collections;
import net.rptools.maptool.client.events.ZoneLoaded;
import net.rptools.maptool.model.Token;
import net.rptools.maptool.util.EventMacroUtil;

public class ZoneLoadedListener {

public ZoneLoadedListener() {
new MapToolEventBus().getMainEventBus().register(this);
}

@Subscribe
public void OnChangedMap(ZoneLoaded event) {
var libTokens = EventMacroUtil.getEventMacroTokens(ON_CHANGE_MAP_CALLBACK);
String prefix = ON_CHANGE_MAP_CALLBACK + "@";

for (Token handler : libTokens) {
EventMacroUtil.callEventHandlerOld(
prefix + handler.getName(), "", handler, Collections.emptyMap(), true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public class LibraryManager {

static {
libraryTokenManager.init();
builtInLibraryManager.loadBuiltIns();
new MapToolEventBus().getMainEventBus().register(addOnSlashCommandManager);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ void initialize() {
}

/** Registers the stat sheets that this add-on defines. */
private void registerSheets() {
public void registerSheets() {
var statSheetManager = new StatSheetManager();
statSheetManager.removeNamespace(namespace);
for (StatSheet sheet : statSheets) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,33 @@ public AddOnLibrary importFromFile(File file) throws IOException {
}
}

public AddOnLibrary importFromClassPath(String path) throws IOException {
// Copy the data to temporary file, its a bit hacky, but it works, and we can't create a
// ZipFile from anything but a file.
if (!path.startsWith("/")) {
path = "/" + path;
}

File tempFile = File.createTempFile("mtlib", "tmp");
tempFile.deleteOnExit();

try (var outputStream = Files.newOutputStream(tempFile.toPath())) {
try (var inputStream = AddOnLibraryImporter.class.getResourceAsStream(path)) {
inputStream.transferTo(outputStream);
}
}

return importFromFile(tempFile);
}

/**
* Adds the metadata from the root directory of the zip file to the metadata directory.
*
* @param namespace namespace of the add-on library.
* @param zip the zipfile containing the add-on library.
* @param pathAssetMap the map of asset paths and asset details.
* @throws IOException
*/
private void addMetaData(
String namespace, ZipFile zip, Map<String, Pair<MD5Key, Type>> pathAssetMap)
throws IOException {
Expand Down
Loading

0 comments on commit 368d241

Please sign in to comment.