Skip to content

Commit

Permalink
check for duplicate boards and log any found
Browse files Browse the repository at this point in the history
  • Loading branch information
kuronekochomusuke committed Nov 16, 2024
1 parent 92c4f1b commit 0dbd73b
Showing 1 changed file with 49 additions and 12 deletions.
61 changes: 49 additions & 12 deletions megamek/src/megamek/utilities/BoardsTagger.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,11 @@
import static java.util.stream.Collectors.toSet;
import static megamek.common.Terrains.*;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.io.*;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;
import java.util.stream.Collectors;

import megamek.common.Board;
import megamek.common.Building;
Expand Down Expand Up @@ -141,6 +134,8 @@ public static void main(String... args) {
* them.
*/
private static void scanForBoards(File file) throws IOException {
Map<String, List<String>> boardCheckSum = new HashMap<>();

if (file.isDirectory()) {
String[] fileList = file.list();
for (String filename : fileList) {
Expand All @@ -149,11 +144,20 @@ private static void scanForBoards(File file) throws IOException {
scanForBoards(new File(file, filename));
} else {
tagBoard(filepath);
checkSum(boardCheckSum, filepath);
}
}
} else {
tagBoard(file);
checkSum(boardCheckSum, file);
}

boardCheckSum.forEach((key, value) -> {
if (value.size() > 1) {
String message = key + " : " + value.stream().sorted().collect(Collectors.joining(", "));
logger.info(message);
}
});
}

/**
Expand Down Expand Up @@ -347,4 +351,37 @@ private static void tagBoard(File boardFile) {
}
}
}

private static void checkSum(Map<String, List<String>> boardCheckSum, File boardFile) {
MessageDigest md;

try {
md = MessageDigest.getInstance("SHA-256");

String line;
List<String> lines = new ArrayList<>();

// remove tag lines
try (BufferedReader br = new BufferedReader(new FileReader(boardFile));) {
while ((line = br.readLine()) != null) {
if (!line.startsWith("tag ")) {
lines.add(line);
}
}
} catch (Exception e) {
logger.error(e, "Error Calculating Hash");
}

String sortedLines = lines.stream().sorted().collect(Collectors.joining());

md.update(sortedLines.getBytes(), 0, sortedLines.length());
HexFormat hexFormat = HexFormat.of();
String cs = hexFormat.formatHex(md.digest()).toUpperCase();
boardCheckSum.putIfAbsent(cs, new ArrayList<>());

boardCheckSum.get(cs).add(boardFile.getPath());
} catch (NoSuchAlgorithmException e) {
logger.error(e, "SHA-256 Algorithm Can't be Found");
}
}
}

0 comments on commit 0dbd73b

Please sign in to comment.