Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

find duplicate boards #6203

Merged
merged 2 commits into from
Nov 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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");
}
}
}