From fb82de22fbd20bd94870a06ed94ffab748dc2fd0 Mon Sep 17 00:00:00 2001 From: cwisniew Date: Mon, 16 Dec 2024 20:25:26 +1030 Subject: [PATCH] Fix error loading HeroLab data with no images --- .../rptools/maptool/model/HeroLabData.java | 141 +++++++++++------- 1 file changed, 89 insertions(+), 52 deletions(-) diff --git a/src/main/java/net/rptools/maptool/model/HeroLabData.java b/src/main/java/net/rptools/maptool/model/HeroLabData.java index f13358f655..13fd3db1b2 100644 --- a/src/main/java/net/rptools/maptool/model/HeroLabData.java +++ b/src/main/java/net/rptools/maptool/model/HeroLabData.java @@ -40,6 +40,8 @@ import net.rptools.maptool.client.ui.theme.Images; import net.rptools.maptool.client.ui.theme.RessourceManager; import net.rptools.maptool.server.proto.HeroLabDataDto; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; /** * @author Jamz @@ -70,6 +72,8 @@ public class HeroLabData { private final Map heroImageAssets = new HashMap<>(); + private static final Logger log = LogManager.getLogger(HeroLabData.class); + private static interface DefaultAssetKey { final String PORTRAIT_KEY = "0"; final String TOKEN_KEY = "1"; @@ -101,11 +105,13 @@ public HeroLabData(String name) { e.printStackTrace(); } - if (!AssetManager.hasAsset(DEFAULT_HERO_LAB_TOKEN_ASSET)) + if (!AssetManager.hasAsset(DEFAULT_HERO_LAB_TOKEN_ASSET)) { AssetManager.putAsset(DEFAULT_HERO_LAB_TOKEN_ASSET); + } - if (!AssetManager.hasAsset(DEFAULT_HERO_LAB_PORTRAIT_ASSET)) + if (!AssetManager.hasAsset(DEFAULT_HERO_LAB_PORTRAIT_ASSET)) { AssetManager.putAsset(DEFAULT_HERO_LAB_PORTRAIT_ASSET); + } heroImageAssets.put(DefaultAssetKey.TOKEN_KEY, DEFAULT_HERO_LAB_TOKEN_ASSET.getMD5Key()); heroImageAssets.put(DefaultAssetKey.PORTRAIT_KEY, DEFAULT_HERO_LAB_PORTRAIT_ASSET.getMD5Key()); @@ -124,13 +130,14 @@ public HeroLabData(String name) { * text or attribute nodes. */ public String parseXML(String xPathExpression, String delim) { - if (xPathExpression.isEmpty()) throw new IllegalArgumentException("Empty XPath expression"); + if (xPathExpression.isEmpty()) { + throw new IllegalArgumentException("Empty XPath expression"); + } String results; XML xmlObj = new XMLDocument(getStatBlock_xml()); results = String.join(delim, xmlObj.xpath(xPathExpression)); - // System.out.println("HeroLabData parseXML(" + xPathExpression + ") :: '" + results + "'"); return results; } @@ -145,7 +152,9 @@ public String parseXML(String xPathExpression, String delim) { * text or attribute nodes. */ public JsonArray parseXmlToJson(String xPathExpression) { - if (xPathExpression.isEmpty()) throw new IllegalArgumentException("Empty XPath expression"); + if (xPathExpression.isEmpty()) { + throw new IllegalArgumentException("Empty XPath expression"); + } JsonArray results = new JsonArray(); XML xmlObj = new XMLDocument(getStatBlock_xml()); @@ -156,14 +165,6 @@ public JsonArray parseXmlToJson(String xPathExpression) { return results; } - /* - * Other xpath tests... String xml = heroData.getStatBlock_xml(); - * - * XPathFactory xpathFactory = XPathFactory.newInstance(); XPath xpath = xpathFactory.newXPath(); - * - * InputSource source = new InputSource(new StringReader(xml)); String results = ""; try { results = xpath.evaluate(searchText, source); } catch (XPathExpressionException e1) { // TODO - * Auto-generated catch block e1.printStackTrace(); } System.out.println(searchText); System.out.println(results); - */ public boolean refresh() { return lastModified != getPortfolioLastModified(); @@ -173,22 +174,23 @@ public boolean refresh() { public Map> getStatBlocks() { Map> statBlocks = new HashMap<>(); - if (heroLabStatblockAssetID == null) return statBlocks; + if (heroLabStatblockAssetID == null) { + return statBlocks; + } Asset statBlockAsset = AssetManager.getAsset(heroLabStatblockAssetID); if (statBlockAsset == null) { - System.out.println("Requesting asset from the server..."); + log.debug("Requesting asset from the server..."); statBlockAsset = AssetManager.requestAssetFromServer(heroLabStatblockAssetID); int maxWait = 100; while (!AssetManager.hasAsset(heroLabStatblockAssetID) && maxWait-- > 0) { try { Thread.sleep(100); } catch (InterruptedException e) { - // TODO Auto-generated catch block - e.printStackTrace(); + // Do nothing } - System.out.println("Waiting on asset from server... " + maxWait); + log.debug("Waiting on asset from server... {}", maxWait); } statBlockAsset = AssetManager.getAsset(heroLabStatblockAssetID); @@ -201,8 +203,8 @@ public Map> getStatBlocks() { byteIn.close(); in.close(); } catch (ClassNotFoundException | IOException e) { - System.out.println("ERROR: Attempting to read Asset ID: " + heroLabStatblockAssetID); - e.printStackTrace(); + log.error("ERROR: Attempting to read Asset ID: {}", heroLabStatblockAssetID); + log.error(e.getStackTrace()); } return statBlocks; @@ -211,7 +213,9 @@ public Map> getStatBlocks() { public void setStatBlocks(Map> statBlocks) { // Jamz: Since statblocks do not change or accessed often, moved object data to an Asset // as heroLabData could be pretty large with XML data causing lag on token transfers - if (heroLabStatblockAssetID != null) AssetManager.removeAsset(heroLabStatblockAssetID); + if (heroLabStatblockAssetID != null) { + AssetManager.removeAsset(heroLabStatblockAssetID); + } // Convert Map to byte array ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); @@ -230,7 +234,7 @@ public void setStatBlocks(Map> statBlocks) { byteOut.close(); } } catch (IOException e) { - e.printStackTrace(); + log.error(e.getStackTrace()); } } @@ -239,18 +243,29 @@ public String getStatBlock_location(String type) { } public String getStatBlock_data(String type) { - if (getStatBlocks() == null) return ""; + if (getStatBlocks() == null) { + return ""; + } - if (type.equalsIgnoreCase(StatBlockType.HTML)) type = StatBlockType.HTML; - else if (type.equalsIgnoreCase(StatBlockType.XML)) type = StatBlockType.XML; - else type = StatBlockType.TEXT; + if (type.equalsIgnoreCase(StatBlockType.HTML)) { + type = StatBlockType.HTML; + } else if (type.equalsIgnoreCase(StatBlockType.XML)) { + type = StatBlockType.XML; + } else { + type = StatBlockType.TEXT; + } - if (getStatBlocks().get(type) == null) return ""; + if (getStatBlocks().get(type) == null) { + return ""; + } String statBlock = getStatBlocks().get(type).get(StatBlockKey.DATA); - if (statBlock == null) return ""; - else return statBlock; + if (statBlock == null) { + return ""; + } else { + return statBlock; + } } public String getStatBlock_text() { @@ -268,20 +283,25 @@ public String getStatBlock_xml() { public boolean isDirty() { boolean hasChanged = refresh(); - if (hasChanged) isDirty = true; + if (hasChanged) { + isDirty = true; + } return isDirty; } public void setDirty(boolean isDirty) { - if (!isDirty) lastModified = getPortfolioLastModified(); - // lastModified = portfolioWatcher.getLastModified(); + if (!isDirty) { + lastModified = getPortfolioLastModified(); + } this.isDirty = isDirty; } public String getPortfolioPath() { - if (portfolioPath == null) setPortfolioPath(""); + if (portfolioPath == null) { + setPortfolioPath(""); + } return portfolioPath; } @@ -296,8 +316,12 @@ public File getPortfolioFile() { if (portfolioPath == null || portfolioPath.isEmpty() || fileSyncPath.isEmpty()) { return portfolioFile; } else { - if (portfolioPath.startsWith(fileSyncPath)) return new File(portfolioPath); - else return new File(fileSyncPath, portfolioPath); + if (portfolioPath.startsWith(fileSyncPath)) { + return new File(portfolioPath); + } + else { + return new File(fileSyncPath, portfolioPath); + } } } @@ -312,12 +336,9 @@ public void setPortfolioFile(File portfolioFile) { .relativize(portfolioFile.toPath()) .toString(); } catch (IllegalArgumentException e) { - System.out.println( - "Unable to relativize paths for: [" - + portfolioFile - + "] [" - + AppPreferences.fileSyncPath.get() - + "]"); + log.error("Unable to relativize paths for: [{}] [{}]", + portfolioFile, + AppPreferences.fileSyncPath.get()); portfolioPath = ""; } } else { @@ -326,14 +347,19 @@ public void setPortfolioFile(File portfolioFile) { } private long getPortfolioLastModified() { - if (getPortfolioFile() != null) return getPortfolioFile().lastModified(); - else return 0L; + if (getPortfolioFile() != null) { + return getPortfolioFile().lastModified(); + } + + return 0L; } public String getLastModifiedDateString() { - if (lastModified != getPortfolioLastModified()) + if (lastModified != getPortfolioLastModified()) { return "" + new Date(lastModified).toString() + ""; - else return new Date(lastModified).toString(); + } + + return new Date(lastModified).toString(); } public String getName() { @@ -409,8 +435,9 @@ public void setGameSystem(String gameSystem) { } public MD5Key getTokenImage() { - if (!heroImageAssets.containsKey(DefaultAssetKey.TOKEN_KEY)) + if (!heroImageAssets.containsKey(DefaultAssetKey.TOKEN_KEY)) { heroImageAssets.put(DefaultAssetKey.TOKEN_KEY, DEFAULT_HERO_LAB_TOKEN_ASSET.getMD5Key()); + } return heroImageAssets.get(DefaultAssetKey.TOKEN_KEY); } @@ -420,9 +447,10 @@ public void setTokenImage(MD5Key imageAsset) { } public MD5Key getPortraitImage() { - if (!heroImageAssets.containsKey(DefaultAssetKey.PORTRAIT_KEY)) + if (!heroImageAssets.containsKey(DefaultAssetKey.PORTRAIT_KEY)) { heroImageAssets.put( DefaultAssetKey.PORTRAIT_KEY, DEFAULT_HERO_LAB_PORTRAIT_ASSET.getMD5Key()); + } return heroImageAssets.get(DefaultAssetKey.PORTRAIT_KEY); } @@ -450,9 +478,13 @@ public Map getAssetMap() { public Collection getAllAssetIDs() { HashMap allAssetIDs = new HashMap<>(); - if (heroImageAssets != null) allAssetIDs.putAll(heroImageAssets); + if (heroImageAssets != null) { + allAssetIDs.putAll(heroImageAssets); + } - if (heroLabStatblockAssetID != null) allAssetIDs.put("statBlocks", heroLabStatblockAssetID); + if (heroLabStatblockAssetID != null) { + allAssetIDs.put("statBlocks", heroLabStatblockAssetID); + } return allAssetIDs.values(); } @@ -460,7 +492,9 @@ public Collection getAllAssetIDs() { public List getAllImageAssetsURLs() { List assetSet = new ArrayList(); - for (MD5Key assetKey : heroImageAssets.values()) assetSet.add("asset://" + assetKey.toString()); + for (MD5Key assetKey : heroImageAssets.values()) { + assetSet.add("asset://" + assetKey.toString()); + } return assetSet; } @@ -474,7 +508,7 @@ public void addImage(String imageName, BufferedImage image) { try { ImageIO.write(image, "png", baos); } catch (IOException e) { - e.printStackTrace(); + log.error(e.getStackTrace()); } Asset imageAsset = Asset.createImageAsset(imageName, baos.toByteArray()); @@ -596,7 +630,10 @@ public HeroLabDataDto toDto() { dto.setPortfolioFile(StringValue.of(portfolioFile.toString())); } - heroImageAssets.forEach((key, value) -> dto.putHeroImageAssets(key, value.toString())); + if (heroImageAssets != null) { + heroImageAssets.forEach((key, value) -> dto.putHeroImageAssets(key, value.toString())); + } + return dto.build(); } }