Skip to content

Commit

Permalink
Log: prevent exception in log for missing MegaMek.jar and improve gam…
Browse files Browse the repository at this point in the history
…e options load logging
  • Loading branch information
SJuliez committed Jul 21, 2023
1 parent a6b38aa commit 9154889
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
5 changes: 5 additions & 0 deletions megamek/src/megamek/MegaMek.java
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,11 @@ public static MMOptions getMMOptions() {
filename = "MegaMek.app/Contents/Resources/Java/" + filename;
}

if (!new File(filename).exists()) {
LogManager.getLogger().warn("MegaMek.jar not found. Returning null checksum.");
return null;
}

MessageDigest md;
// Calculate the digest for the given file.
try {
Expand Down
12 changes: 7 additions & 5 deletions megamek/src/megamek/common/options/GameOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -333,17 +333,19 @@ public synchronized Vector<IOption> loadOptions(File file, boolean print) {
InputStream is = new FileInputStream(file);
GameOptionsXML opts = (GameOptionsXML) um.unmarshal(MMXMLUtility.createSafeXmlSource(is));

StringBuilder logMessages = new StringBuilder("\n");
for (IBasicOption bo : opts.getOptions()) {
changedOptions.add(parseOptionNode(bo, print));
changedOptions.add(parseOptionNode(bo, print, logMessages));
}
LogManager.getLogger().info(logMessages.toString());
} catch (Exception e) {
LogManager.getLogger().error("Error loading XML for game options: " + e.getMessage(), e);
}

return changedOptions;
}

private IOption parseOptionNode(final IBasicOption node, final boolean print) {
private IOption parseOptionNode(final IBasicOption node, final boolean print, final StringBuilder logMessages) {
IOption option = null;

String name = node.getName();
Expand Down Expand Up @@ -372,17 +374,17 @@ private IOption parseOptionNode(final IBasicOption node, final boolean print) {
}

if (print) {
LogManager.getLogger().info(String.format("Set option '%s' to '%s'.", name, value));
logMessages.append(String.format("\tSet option '%s' to '%s'\n", name, value));
}

option = tempOption;
} catch (Exception ex) {
LogManager.getLogger().error(String.format(
"Error trying to load option '%s' with a value of '%s'.", name, value));
"Error trying to load option '%s' with a value of '%s'!", name, value));
}
}
} else {
LogManager.getLogger().warn("Invalid option '" + name + "' when trying to load options file.");
LogManager.getLogger().warn("Invalid option '" + name + "' when trying to load options file!");
}
}

Expand Down

0 comments on commit 9154889

Please sign in to comment.