diff --git a/megamek/src/megamek/utilities/BoardsTagger.java b/megamek/src/megamek/utilities/BoardsTagger.java index cb381a87a33..9328a158fad 100644 --- a/megamek/src/megamek/utilities/BoardsTagger.java +++ b/megamek/src/megamek/utilities/BoardsTagger.java @@ -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; @@ -141,6 +134,8 @@ public static void main(String... args) { * them. */ private static void scanForBoards(File file) throws IOException { + Map> boardCheckSum = new HashMap<>(); + if (file.isDirectory()) { String[] fileList = file.list(); for (String filename : fileList) { @@ -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); + } + }); } /** @@ -347,4 +351,37 @@ private static void tagBoard(File boardFile) { } } } + + private static void checkSum(Map> boardCheckSum, File boardFile) { + MessageDigest md; + + try { + md = MessageDigest.getInstance("SHA-256"); + + String line; + List 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"); + } + } }