Skip to content

Commit

Permalink
Merge pull request #6203 from kuronekochomusuke/dupBoards
Browse files Browse the repository at this point in the history
find duplicate boards
  • Loading branch information
HammerGS authored Nov 23, 2024
2 parents d602cee + 34c8d20 commit d574367
Showing 1 changed file with 52 additions and 15 deletions.
67 changes: 52 additions & 15 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 @@ -126,8 +119,17 @@ public static Tags parse(String tag) {

public static void main(String... args) {
try {
Map<String, List<String>> boardCheckSum = new HashMap<>();

File boardDir = Configuration.boardsDir();
scanForBoards(boardDir);
scanForBoards(boardDir, boardCheckSum);

boardCheckSum.forEach((key, value) -> {
if (value.size() > 1) {
String message = key + " : " + value.stream().sorted().collect(Collectors.joining(", "));
logger.info(message);
}
});
} catch (Exception ex) {
logger.error(ex, "Board tagger cannot scan boards");
System.exit(64);
Expand All @@ -140,19 +142,21 @@ public static void main(String... args) {
* Recursively scans the supplied file/directory for any boards and auto-tags
* them.
*/
private static void scanForBoards(File file) throws IOException {
private static void scanForBoards(File file, Map<String, List<String>> boardCheckSum) throws IOException {
if (file.isDirectory()) {
String[] fileList = file.list();
for (String filename : fileList) {
File filepath = new File(file, filename);
if (filepath.isDirectory()) {
scanForBoards(new File(file, filename));
scanForBoards(new File(file, filename), boardCheckSum);
} else {
tagBoard(filepath);
checkSum(boardCheckSum, filepath);
}
}
} else {
tagBoard(file);
checkSum(boardCheckSum, file);
}
}

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 d574367

Please sign in to comment.