Skip to content

Commit

Permalink
Improve error handling of IDS parser
Browse files Browse the repository at this point in the history
Suppresses unneeded error messages for IDS resources that are known to
be malformed.
  • Loading branch information
Argent77 committed Apr 21, 2024
1 parent 0b6fe8e commit a4cce77
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,12 @@ public static List<IniMap> createIniMaps(int animationId) {
return retVal;
}

IdsMap table = new IdsMap(entry);
retVal.addAll(processTable(table, animationId));
try {
IdsMap table = new IdsMap(entry);
retVal.addAll(processTable(table, animationId));
} catch (Exception e) {
e.printStackTrace();
}

return retVal;
}
Expand Down
21 changes: 8 additions & 13 deletions src/org/infinity/util/IdsMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
import java.util.TreeMap;
import java.util.TreeSet;

import javax.swing.JOptionPane;

import org.infinity.resource.bcs.ScriptInfo;
import org.infinity.resource.bcs.Signatures;
import org.infinity.resource.key.ResourceEntry;
Expand All @@ -26,18 +24,18 @@ public class IdsMap {
private final ResourceEntry entry;
private final boolean caseSensitive;

public IdsMap(ResourceEntry entry) {
public IdsMap(ResourceEntry entry) throws Exception {
this.entry = entry;
this.caseSensitive = IdsMapCache.isCaseSensitiveMatch(entry.getResourceName());
try {
// try {
if (entry.getExtension().equalsIgnoreCase("IDS")) {
parseIDS();
} else if (entry.getExtension().equalsIgnoreCase("2DA")) {
parse2DA();
}
} catch (Exception e) {
e.printStackTrace();
}
// } catch (Exception e) {
// e.printStackTrace();
// }
}

@Override
Expand Down Expand Up @@ -119,8 +117,7 @@ private void parse2DA() throws Exception {
try {
extract2DA(token);
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null, "Error interpreting " + entry + ": " + token, "Error",
JOptionPane.ERROR_MESSAGE);
throw new IllegalArgumentException(String.format("Unexpected token [resource=%s, token=\"%s\"]", entry, token));
}
}
}
Expand All @@ -133,8 +130,7 @@ private void parseIDS() throws Exception {
try {
extractIDS(token);
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null, "Error interpreting " + entry + ": " + token, "Error",
JOptionPane.ERROR_MESSAGE);
throw new IllegalArgumentException(String.format("Unexpected token [resource=%s, token=\"%s\"]", entry, token));
}
}

Expand All @@ -150,8 +146,7 @@ private void parseIDS() throws Exception {
try {
extractIDS(token);
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null, "Error interpreting " + entry + ": " + token, "Error",
JOptionPane.ERROR_MESSAGE);
throw new IllegalArgumentException(String.format("Unexpected token [resource=%s, token=\"%s\"]", entry, token));
}
}
}
Expand Down
56 changes: 54 additions & 2 deletions src/org/infinity/util/IdsMapCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Locale;
import java.util.Map;
import java.util.Set;

import org.infinity.resource.Profile;
import org.infinity.resource.ResourceFactory;
import org.infinity.resource.bcs.ScriptInfo;
import org.infinity.resource.key.ResourceEntry;
Expand All @@ -18,6 +21,11 @@ public class IdsMapCache {
/** Maps upper-cased name of IDS resource to parsed resource. */
private static final Map<String, IdsMap> CACHE = new HashMap<>();

/** List of IDS resource names that are known to be malformed. */
private static final Set<String> BLACKLIST = new HashSet<>();

private static boolean blackListInitialized = false;

public static void remove(ResourceEntry entry) {
if (entry != null) {
CACHE.remove(entry.getResourceName().toUpperCase(Locale.ENGLISH));
Expand All @@ -26,10 +34,16 @@ public static void remove(ResourceEntry entry) {

public static void clearCache() {
CACHE.clear();
blackListInitialized = false;
}

public static synchronized IdsMap get(String name) {
IdsMap retVal = null;

if (isBlackListed(name)) {
return retVal;
}

if (name != null) {
name = name.trim().toUpperCase(Locale.ENGLISH);
retVal = CACHE.get(name);
Expand All @@ -43,8 +57,12 @@ public static synchronized IdsMap get(String name) {
}
}
if (entry != null) {
retVal = new IdsMap(entry);
CACHE.put(name, retVal);
try {
retVal = new IdsMap(entry);
CACHE.put(name, retVal);
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
}
}
Expand Down Expand Up @@ -246,4 +264,38 @@ private static String prettifyName(String name) {
retVal = Character.toUpperCase(retVal.charAt(0)) + retVal.substring(1);
return retVal;
}

/** Returns {@code true} if the specified IDS resref is blacklisted. */
private static boolean isBlackListed(String name) {
updateBlackList(false);
boolean retVal = true;
if (name != null) {
final String entry = name.trim().toUpperCase(Locale.ROOT);
retVal = BLACKLIST.contains(entry);
}
return retVal;
}

/** Creates a blacklist of IDS resource names which are known to be malformed. */
private static void updateBlackList(boolean forced) {
if (forced || !blackListInitialized) {
synchronized (BLACKLIST) {
BLACKLIST.clear();
switch (Profile.getGame()) {
case PSTEE:
BLACKLIST.add("COLOR.IDS");
break;
case IWD:
case IWDHoW:
case IWDHowTotLM:
case IWD2:
case IWD2EE:
BLACKLIST.add("PREFAB.IDS");
break;
default:
}
blackListInitialized = true;
}
}
}
}

0 comments on commit a4cce77

Please sign in to comment.