Skip to content

Commit

Permalink
Merge pull request #5467 from SJuliez/musing-validity-reporting
Browse files Browse the repository at this point in the history
Modernize board validity checking code
  • Loading branch information
HammerGS authored May 16, 2024
2 parents 0896ab4 + 71995ac commit 97ab538
Show file tree
Hide file tree
Showing 10 changed files with 259 additions and 283 deletions.
122 changes: 61 additions & 61 deletions megamek/data/boards/unofficial/Derv_Maps/60x60 Nanking.board
Original file line number Diff line number Diff line change
Expand Up @@ -1766,9 +1766,9 @@ hex 1830 0 "road:1:18" "grass"
hex 1930 0 "road:1:18" "grass"
hex 2030 0 "road:1:18" "grass"
hex 2130 0 "road:1:18" "grass"
hex 2230 0 "" """
hex 2330 0 "" """
hex 2430 0 "" """
hex 2230 0 "" "grass"
hex 2330 0 "" "grass"
hex 2430 0 "" "grass"
hex 2530 0 "woods:1;foliage_elev:2" "grass"
hex 2630 0 "woods:1;foliage_elev:2" "grass"
hex 2730 1 "road:1:36" "grass"
Expand Down Expand Up @@ -1824,13 +1824,13 @@ hex 1631 0 "road:1:27" "grass"
hex 1731 0 "road:1:50" "grass"
hex 1831 0 "road:1:18" "grass"
hex 1931 0 "road:1:18" "grass"
hex 2031 0 "" """
hex 2131 0 "" """
hex 2231 0 "" """
hex 2331 0 "" """
hex 2431 0 "" """
hex 2531 0 "" """
hex 2631 0 "" """
hex 2031 0 "" "grass"
hex 2131 0 "" "grass"
hex 2231 0 "" "grass"
hex 2331 0 "" "grass"
hex 2431 0 "" "grass"
hex 2531 0 "" "grass"
hex 2631 0 "" "grass"
hex 2731 0 "woods:1;foliage_elev:2" "grass"
hex 2831 0 "woods:1;foliage_elev:2" "grass"
hex 2931 1 "road:1:36" "grass"
Expand Down Expand Up @@ -1883,15 +1883,15 @@ hex 1532 0 "road:1:18" "grass"
hex 1632 0 "road:1:19" "grass"
hex 1732 0 "road:1:22" "grass"
hex 1832 0 "road:1:34" "grass"
hex 1932 0 "" """
hex 2032 0 "" """
hex 2132 0 "" """
hex 2232 0 "" """
hex 2332 0 "" """
hex 2432 0 "" """
hex 2532 0 "" """
hex 2632 0 "" """
hex 2732 0 "" """
hex 1932 0 "" "grass"
hex 2032 0 "" "grass"
hex 2132 0 "" "grass"
hex 2232 0 "" "grass"
hex 2332 0 "" "grass"
hex 2432 0 "" "grass"
hex 2532 0 "" "grass"
hex 2632 0 "" "grass"
hex 2732 0 "" "grass"
hex 2832 0 "" "grass"
hex 2932 0 "" "grass"
hex 3032 1 "" "grass"
Expand Down Expand Up @@ -1943,15 +1943,15 @@ hex 1533 0 "road:1:18" "grass"
hex 1633 0 "" "grass"
hex 1733 0 "" "grass"
hex 1833 0 "" "grass"
hex 1933 0 "" """
hex 2033 0 "" """
hex 2133 0 "" """
hex 2233 0 "" """
hex 2333 0 "" """
hex 2433 0 "" """
hex 2533 0 "" """
hex 2633 0 "" """
hex 2733 0 "" """
hex 1933 0 "" "grass"
hex 2033 0 "" "grass"
hex 2133 0 "" "grass"
hex 2233 0 "" "grass"
hex 2333 0 "" "grass"
hex 2433 0 "" "grass"
hex 2533 0 "" "grass"
hex 2633 0 "" "grass"
hex 2733 0 "" "grass"
hex 2833 0 "" "grass"
hex 2933 0 "" "grass"
hex 3033 0 "" "grass"
Expand Down Expand Up @@ -2003,15 +2003,15 @@ hex 1534 0 "" "grass"
hex 1634 0 "" "grass"
hex 1734 0 "" "grass"
hex 1834 0 "" "grass"
hex 1934 0 "" """
hex 2034 0 "" """
hex 2134 0 "" """
hex 2234 0 "" """
hex 2334 0 "" """
hex 2434 0 "" """
hex 2534 0 "" """
hex 2634 0 "" """
hex 2734 0 "" """
hex 1934 0 "" "grass"
hex 2034 0 "" "grass"
hex 2134 0 "" "grass"
hex 2234 0 "" "grass"
hex 2334 0 "" "grass"
hex 2434 0 "" "grass"
hex 2534 0 "" "grass"
hex 2634 0 "" "grass"
hex 2734 0 "" "grass"
hex 2834 0 "" "grass"
hex 2934 0 "" "grass"
hex 3034 0 "" "grass"
Expand Down Expand Up @@ -2063,15 +2063,15 @@ hex 1535 0 "" "grass"
hex 1635 0 "" "grass"
hex 1735 0 "" "grass"
hex 1835 0 "" "grass"
hex 1935 0 "" """
hex 2035 0 "" """
hex 2135 0 "" """
hex 2235 0 "" """
hex 2335 0 "" """
hex 2435 0 "" """
hex 2535 0 "" """
hex 2635 0 "" """
hex 2735 0 "" """
hex 1935 0 "" "grass"
hex 2035 0 "" "grass"
hex 2135 0 "" "grass"
hex 2235 0 "" "grass"
hex 2335 0 "" "grass"
hex 2435 0 "" "grass"
hex 2535 0 "" "grass"
hex 2635 0 "" "grass"
hex 2735 0 "" "grass"
hex 2835 0 "" "grass"
hex 2935 0 "" "grass"
hex 3035 0 "" "grass"
Expand Down Expand Up @@ -2123,15 +2123,15 @@ hex 1536 0 "" "grass"
hex 1636 0 "" "grass"
hex 1736 0 "" "grass"
hex 1836 0 "" "grass"
hex 1936 0 "" """
hex 2036 0 "" """
hex 2136 0 "" """
hex 2236 0 "" """
hex 2336 0 "" """
hex 2436 0 "" """
hex 2536 0 "" """
hex 2636 0 "" """
hex 2736 0 "" """
hex 1936 0 "" "grass"
hex 2036 0 "" "grass"
hex 2136 0 "" "grass"
hex 2236 0 "" "grass"
hex 2336 0 "" "grass"
hex 2436 0 "" "grass"
hex 2536 0 "" "grass"
hex 2636 0 "" "grass"
hex 2736 0 "" "grass"
hex 2836 0 "rough:1" "grass"
hex 2936 0 "" "grass"
hex 3036 1 "" "grass"
Expand Down Expand Up @@ -2185,11 +2185,11 @@ hex 1737 1 "" "grass"
hex 1837 1 "" "grass"
hex 1937 0 "" "grass"
hex 2037 0 "" "grass"
hex 2137 0 "" """
hex 2237 0 "" """
hex 2337 0 "" """
hex 2437 0 "" """
hex 2537 0 "" """
hex 2137 0 "" "grass"
hex 2237 0 "" "grass"
hex 2337 0 "" "grass"
hex 2437 0 "" "grass"
hex 2537 0 "" "grass"
hex 2637 0 "" "grass"
hex 2737 0 "" "grass"
hex 2837 0 "" "grass"
Expand Down Expand Up @@ -2247,7 +2247,7 @@ hex 1938 0 "" "grass"
hex 2038 0 "" "grass"
hex 2138 0 "" "grass"
hex 2238 0 "" "grass"
hex 2338 0 "" """
hex 2338 0 "" "grass"
hex 2438 0 "" "grass"
hex 2538 1 "" "grass"
hex 2638 1 "" "grass"
Expand Down
21 changes: 10 additions & 11 deletions megamek/src/megamek/client/ui/swing/BoardEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -1735,23 +1735,23 @@ private void setupUiFreshBoard() {
* be shown.
*/
private void validateBoard(boolean showPositiveResult) {
StringBuffer errBuff = new StringBuffer();
board.isValid(errBuff);
if ((errBuff.length() > 0) || showPositiveResult) {
showBoardValidationReport(errBuff);
List<String> errors = new ArrayList<>();
board.isValid(errors);
if ((!errors.isEmpty()) || showPositiveResult) {
showBoardValidationReport(errors);
}
}

/**
* Shows a board validation report dialog, reporting either
* the contents of errBuff or that the board has no errors.
*/
private void showBoardValidationReport(StringBuffer errBuff) {
private void showBoardValidationReport(List<String> errors) {
ignoreHotKeys = true;
if ((errBuff != null) && errBuff.length() > 0) {
if ((errors != null) && !errors.isEmpty()) {
String title = Messages.getString("BoardEditor.invalidBoard.title");
String msg = Messages.getString("BoardEditor.invalidBoard.report");
msg += errBuff;
msg += String.join("\n", errors);
JTextArea textArea = new JTextArea(msg);
JScrollPane scrollPane = new JScrollPane(textArea);
textArea.setLineWrap(true);
Expand Down Expand Up @@ -2315,11 +2315,10 @@ public void paintComponent(Graphics g) {
g.setColor(getForeground());
g.setFont(new Font(MMConstants.FONT_SANS_SERIF, Font.PLAIN, 9));
g.drawString(Messages.getString("BoardEditor.LEVEL") + curHex.getLevel(), 24, 70);
StringBuffer errBuf = new StringBuffer();
if (!curHex.isValid(errBuf)) {
List<String> errors = new ArrayList<>();
if (!curHex.isValid(errors)) {
invalidString.draw(g);
String tooltip = Messages.getString("BoardEditor.invalidHex") + errBuf;
tooltip = tooltip.replace("\n", "<br>");
String tooltip = Messages.getString("BoardEditor.invalidHex") + String.join("<BR>", errors);
setToolTipText(tooltip);
} else {
setToolTipText(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,12 @@ public String getTooltip(Point point, Coords movementTarget) {
}

// Invalid hex notification
StringBuffer errBuff = new StringBuffer();
if (!hex.isValid(errBuff)) {
String errors = errBuff.toString().replace("\n", "<BR>");
List<String> errors = new ArrayList<>();
if (!hex.isValid(errors)) {
result.append(paragraphHTMLOpen(padding))
.append(guiScaledFontHTML(GUIP.getWarningColor())).append(UIUtil.WARNING_SIGN).append("</FONT>")
.append(Messages.getString("BoardView1.invalidHex")).append("<BR>")
.append(errors)
.append(String.join("<BR>", errors))
.append("</p>");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,11 @@ public String getTooltip(Point point, Coords movementTarget) {
result += sbBuildings.toString();

if (bv.displayInvalidFields()) {
StringBuffer errBuff = new StringBuffer();
if (!mhex.isValid(errBuff)) {
List<String> errors = new ArrayList<>();
if (!mhex.isValid(errors)) {
String sInvalidHex = Messages.getString("BoardView1.invalidHex");
sInvalidHex += "<BR>";
String errors = errBuff.toString();
errors = errors.replace("\n", "<BR>");
sInvalidHex += errors;
sInvalidHex += String.join("<BR>", errors);
sInvalidHex = guiScaledFontHTML(GUIP.getUnitToolTipFGColor()) + sInvalidHex + "</FONT>";
result += "<BR>" + sInvalidHex;
}
Expand Down
11 changes: 5 additions & 6 deletions megamek/src/megamek/client/ui/swing/lobby/ChatLounge.java
Original file line number Diff line number Diff line change
Expand Up @@ -1100,9 +1100,8 @@ private void refreshMapButtons() {
if (boardFile.exists()) {
buttonBoard = new Board(16, 17);
buttonBoard.load(new MegaMekFile(Configuration.boardsDir(), boardForImage + CL_KEY_FILEEXTENTION_BOARD).getFile());
StringBuffer errs = new StringBuffer();
try (InputStream is = new FileInputStream(new MegaMekFile(Configuration.boardsDir(), boardForImage + CL_KEY_FILEEXTENTION_BOARD).getFile())) {
buttonBoard.load(is, errs, true);
buttonBoard.load(is, null, true);
BoardUtilities.flip(buttonBoard, rotateBoard, rotateBoard);
} catch (IOException ex) {
buttonBoard = Board.createEmptyBoard(mapSettings.getBoardWidth(), mapSettings.getBoardHeight());
Expand Down Expand Up @@ -3293,11 +3292,11 @@ private synchronized void add(String name) {
private Image prepareImage(String boardName) {
File boardFile = new MegaMekFile(Configuration.boardsDir(), boardName + CL_KEY_FILEEXTENTION_BOARD).getFile();
Board board;
StringBuffer errs = new StringBuffer();
List<String> errors = new ArrayList<>();
if (boardFile.exists()) {
board = new Board();
try (InputStream is = new FileInputStream(boardFile)) {
board.load(is, errs, true);
board.load(is, errors, true);
} catch (IOException ex) {
board = Board.createEmptyBoard(mapSettings.getBoardWidth(), mapSettings.getBoardHeight());
}
Expand Down Expand Up @@ -3337,10 +3336,10 @@ private Image prepareImage(String boardName) {
// Add the board name label and the server-side board label if necessary
String text = LobbyUtility.cleanBoardName(boardName, mapSettings);
Graphics g = bufImage.getGraphics();
if (errs.length() != 0) {
if (!errors.isEmpty()) {
invalidBoards.add(boardName);
}
drawMinimapLabel(text, bufImage.getWidth(), bufImage.getHeight(), g, errs.length() != 0);
drawMinimapLabel(text, bufImage.getWidth(), bufImage.getHeight(), g, !errors.isEmpty());
if (!boardFile.exists() && !boardName.startsWith(MapSettings.BOARD_GENERATED)) {
serverBoards.add(boardName);
markServerSideBoard(bufImage);
Expand Down
Loading

0 comments on commit 97ab538

Please sign in to comment.