From 22219201f18fc51b1081d4af574f2e816eaaedac Mon Sep 17 00:00:00 2001 From: Matthias Faust Date: Fri, 25 Aug 2023 16:39:17 +0200 Subject: [PATCH] Improved EM scanning --- .../server/highscores/HighscoreResolver.java | 34 ++++++++++++++++--- .../vpin/server/util/VPXFileScanner.java | 24 ++++++++++--- .../TablesSidebarHighscoresController.java | 2 +- .../ResetHighscoreDialogController.java | 8 ++++- 4 files changed, 58 insertions(+), 10 deletions(-) diff --git a/vpin-studio-server/src/main/java/de/mephisto/vpin/server/highscores/HighscoreResolver.java b/vpin-studio-server/src/main/java/de/mephisto/vpin/server/highscores/HighscoreResolver.java index 489c9bc5d..498e22f39 100644 --- a/vpin-studio-server/src/main/java/de/mephisto/vpin/server/highscores/HighscoreResolver.java +++ b/vpin-studio-server/src/main/java/de/mephisto/vpin/server/highscores/HighscoreResolver.java @@ -1,7 +1,7 @@ package de.mephisto.vpin.server.highscores; -import de.mephisto.vpin.restclient.HighscoreType; import de.mephisto.vpin.commons.utils.SystemCommandExecutor; +import de.mephisto.vpin.restclient.HighscoreType; import de.mephisto.vpin.server.games.Game; import de.mephisto.vpin.server.system.SystemService; import de.mephisto.vpin.server.util.vpreg.VPReg; @@ -14,7 +14,9 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.io.*; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; import java.nio.charset.Charset; import java.util.Arrays; import java.util.Date; @@ -51,7 +53,11 @@ public HighscoreMetadata readHighscore(Game game) { try { String romName = game.getRom(); if (StringUtils.isEmpty(romName)) { - String msg = "No rom name found."; + romName = game.getTableName(); + } + + if (StringUtils.isEmpty(romName)) { + String msg = "No rom or table name found."; metadata.setStatus(msg); return metadata; } @@ -112,6 +118,26 @@ private String parseHSFileHighscore(Game game, HighscoreMetadata metadata) throw index++; } + return builder.toString(); + } + else if (lines.size() == 8) { + StringBuilder builder = new StringBuilder("HIGHEST SCORES\n"); + + String score1 = lines.get(1); + String score2 = lines.get(2); + builder.append("#1"); + builder.append(" "); + builder.append("???"); + builder.append(" "); + builder.append(score2); + builder.append("\n"); + + builder.append("#2"); + builder.append(" "); + builder.append("???"); + builder.append(" "); + builder.append(score1); + builder.append("\n"); return builder.toString(); } @@ -198,7 +224,7 @@ private String executePINemHi(String nvRamFileName, HighscoreMetadata metadata) StringBuilder standardOutputFromCommand = executor.getStandardOutputFromCommand(); StringBuilder standardErrorFromCommand = executor.getStandardErrorFromCommand(); if (!StringUtils.isEmpty(standardErrorFromCommand.toString())) { - String error = "Pinemhi command (" + commandFile.getCanonicalPath() + " "+ nvRamFileName + ") failed: " + standardErrorFromCommand; + String error = "Pinemhi command (" + commandFile.getCanonicalPath() + " " + nvRamFileName + ") failed: " + standardErrorFromCommand; LOG.error(error); metadata.setStatus(error); throw new Exception(error); diff --git a/vpin-studio-server/src/main/java/de/mephisto/vpin/server/util/VPXFileScanner.java b/vpin-studio-server/src/main/java/de/mephisto/vpin/server/util/VPXFileScanner.java index 045c7adc4..dcfc20e6c 100644 --- a/vpin-studio-server/src/main/java/de/mephisto/vpin/server/util/VPXFileScanner.java +++ b/vpin-studio-server/src/main/java/de/mephisto/vpin/server/util/VPXFileScanner.java @@ -4,6 +4,7 @@ import de.mephisto.vpin.server.vpx.VPXUtil; import edu.umd.cs.findbugs.annotations.NonNull; import edu.umd.cs.findbugs.annotations.Nullable; +import org.apache.commons.io.FilenameUtils; import org.apache.commons.lang3.StringUtils; import org.jetbrains.annotations.NotNull; import org.slf4j.Logger; @@ -56,14 +57,19 @@ public static ScanResult scan(@NonNull File gameFile) { Collections.reverse(split); scanLines(gameFile, start, result, split); - if(StringUtils.isEmpty(result.getRom())) { + if (StringUtils.isEmpty(result.getRom())) { LOG.info("Regular scan failed, running deep scan for " + gameFile.getAbsolutePath()); runDeepScan(gameFile, result); } if (!StringUtils.isEmpty(result.getRom())) { LOG.info("Finished scan of table " + gameFile.getAbsolutePath() + ", found ROM '" + result.getRom() + "', took " + (System.currentTimeMillis() - start) + " ms."); - } else { + } + else if(StringUtils.isEmpty(result.getRom()) && StringUtils.isEmpty(result.getTableName()) && !StringUtils.isEmpty(result.getHsFileName())) { + result.setTableName(FilenameUtils.getBaseName(result.getHsFileName())); + LOG.info("Finished scan of table " + gameFile.getAbsolutePath() + ", found EM highscore filename '" + result.getHsFileName() + "', took " + (System.currentTimeMillis() - start) + " ms."); + } + else { LOG.info("Finished scan of table " + gameFile.getAbsolutePath() + ", no ROM found" + "', took " + (System.currentTimeMillis() - start) + " ms."); } return result; @@ -99,6 +105,7 @@ private static void runDeepScan(File gameFile, ScanResult result) { lineSearchNvOffset(result, line); lineSearchHsFileName(result, line); lineSearchAsset(gameFile, result, line); + lineSearchEMHighscore(gameFile, result, line); } } catch (Exception e) { LOG.error("Failed to read rom line '" + line + "' for " + gameFile.getAbsolutePath() + ": " + e.getMessage(), e); @@ -117,6 +124,16 @@ private static void runDeepScan(File gameFile, ScanResult result) { } } + private static void lineSearchEMHighscore(File gameFile, ScanResult result, String line) { + if (!StringUtils.isEmpty(line) && line.contains(".txt\"")) { + String emFilename = line.substring(0, line.lastIndexOf(".txt\"") + 4); + emFilename = emFilename.substring(emFilename.lastIndexOf("\"") + 1); + if (result.getHsFileName() == null) { + result.setHsFileName(emFilename); + } + } + } + private static void scanLines(@NotNull File gameFile, long start, ScanResult result, List split) { String l; for (String line : split) { @@ -200,8 +217,7 @@ private static void lineSearchNvOffset(@NonNull ScanResult result, @NonNull Stri try { nvOffsetString = line.substring(line.indexOf("(") + 1, line.indexOf(")")); result.setNvOffset(Integer.parseInt(nvOffsetString)); - } - catch (Exception e) { + } catch (Exception e) { LOG.error("Failed to read NVOffset from line \"" + line + "\" and segment \"" + nvOffsetString + "\": " + e.getMessage()); } } diff --git a/vpin-studio-ui/src/main/java/de/mephisto/vpin/ui/tables/TablesSidebarHighscoresController.java b/vpin-studio-ui/src/main/java/de/mephisto/vpin/ui/tables/TablesSidebarHighscoresController.java index 94631a5ab..6b2a74e95 100644 --- a/vpin-studio-ui/src/main/java/de/mephisto/vpin/ui/tables/TablesSidebarHighscoresController.java +++ b/vpin-studio-ui/src/main/java/de/mephisto/vpin/ui/tables/TablesSidebarHighscoresController.java @@ -275,7 +275,7 @@ public void refreshView(Optional g, boolean forceRescan) { if (!summary.getScores().isEmpty()) { cardBtn.setDisable(false); - resetBtn.setDisable(false); + resetBtn.setDisable(StringUtils.isEmpty(rom)); rawTitleLabel.setVisible(true); rawScoreWrapper.setVisible(true); diff --git a/vpin-studio-ui/src/main/java/de/mephisto/vpin/ui/tables/dialogs/ResetHighscoreDialogController.java b/vpin-studio-ui/src/main/java/de/mephisto/vpin/ui/tables/dialogs/ResetHighscoreDialogController.java index e64322905..69bae343e 100644 --- a/vpin-studio-ui/src/main/java/de/mephisto/vpin/ui/tables/dialogs/ResetHighscoreDialogController.java +++ b/vpin-studio-ui/src/main/java/de/mephisto/vpin/ui/tables/dialogs/ResetHighscoreDialogController.java @@ -9,6 +9,7 @@ import javafx.scene.control.Label; import javafx.scene.control.TextField; import javafx.stage.Stage; +import org.apache.commons.lang3.StringUtils; public class ResetHighscoreDialogController implements DialogController { @@ -52,8 +53,13 @@ public void setGame(GameRepresentation game) { this.game = game; okButton.setDisable(true); + String rom = game.getRom(); + if (!StringUtils.isEmpty(rom)) { + rom = game.getTableName(); + } + this.textLabel.setText("Reset the highscore of \"" + game.getGameDisplayName() + "\"?"); - this.descriptionLabel.setText("Enter the ROM name (\"" + game.getRom() + "\") to confirm the reset:"); + this.descriptionLabel.setText("Enter the ROM name (\"" + rom + "\") to confirm the reset:"); textField.requestFocus(); textField.textProperty().addListener((observable, oldValue, newValue) -> { String romName = game.getRom();