From 0b18569adb39fcc4f2d4145052bed6344a7c6188 Mon Sep 17 00:00:00 2001 From: LMH01 Date: Wed, 8 Mar 2023 22:54:48 +0100 Subject: [PATCH] Fixed #128 --- docs/changelog.md | 1 + docs/changelog_dev.md | 1 + .../mgt2mt/content/instances/Platform.java | 1 + .../mgt2mt/data_stream/DataStreamHelper.java | 81 +++---------------- 4 files changed, 13 insertions(+), 71 deletions(-) diff --git a/docs/changelog.md b/docs/changelog.md index eab02cde..672c6844 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -18,6 +18,7 @@ - Fixed NullPointException when no `requires_pictures` key was set in any mod map inside the `.toml` file when mods where imported - Fixed #118 - The customized publisher icon was not used when publishers where imported, instead the default icon name was usedgitui - Fixed #120 - Image files should now be found under Linux +- Fixed #128 - Sometimes the false platform image where deleted ## [v4.8.0](https://github.com/LMH01/MGT2_Mod_Tool/releases/tag/v4.8.0) (Latest Version) diff --git a/docs/changelog_dev.md b/docs/changelog_dev.md index f601c808..e02312db 100644 --- a/docs/changelog_dev.md +++ b/docs/changelog_dev.md @@ -4,6 +4,7 @@ ### Bug fixes - Fixed #126 - Import failed when content was modified and the import was costomized +- Fixed #128 - False image files where deleted when platforms where deleted ## [v4.9.0-beta4] diff --git a/src/main/java/com/github/lmh01/mgt2mt/content/instances/Platform.java b/src/main/java/com/github/lmh01/mgt2mt/content/instances/Platform.java index b27929d7..86f51a7c 100644 --- a/src/main/java/com/github/lmh01/mgt2mt/content/instances/Platform.java +++ b/src/main/java/com/github/lmh01/mgt2mt/content/instances/Platform.java @@ -7,6 +7,7 @@ import com.github.lmh01.mgt2mt.data_stream.DataStreamHelper; import com.github.lmh01.mgt2mt.util.I18n; import com.github.lmh01.mgt2mt.util.Utils; +import com.github.lmh01.mgt2mt.util.helper.TextAreaHelper; import com.github.lmh01.mgt2mt.util.manager.TranslationManager; import java.io.File; diff --git a/src/main/java/com/github/lmh01/mgt2mt/data_stream/DataStreamHelper.java b/src/main/java/com/github/lmh01/mgt2mt/data_stream/DataStreamHelper.java index 21a6b911..1a657ac9 100644 --- a/src/main/java/com/github/lmh01/mgt2mt/data_stream/DataStreamHelper.java +++ b/src/main/java/com/github/lmh01/mgt2mt/data_stream/DataStreamHelper.java @@ -397,82 +397,21 @@ public static long getFileCount(Path dir) throws IOException { } /** - * Returns the path of the file in the specified folder that has the best match - * against the given file name. The file name comparison is case-insensitive. - * Only regular image files are considered for matching. + * Searches the folder for the file with `name`, ignores case. + * Returns the file when found, otherwise null is returned. * - * This function was generated by ChatGPT. - * - * @param folder the path of the folder to search in - * @param name the name of the file to match against - * @return the path of the best match file, or null if no match was found - * @throws IOException if an I/O error occurs during the search + * @param folder The folder that should be searched for the file + * @param name The name of the file to search + * @return The file if found */ public static Path getImageFromFolder(Path folder, String name) throws IOException { - // Get the name of the file to search for, in lowercase - String fileName = Paths.get(name).getFileName().toString().toLowerCase(); - - // Initialize the best match file and score - Path bestMatch = null; - int bestScore = 0; - - // Iterate over the files in the folder - try (DirectoryStream stream = Files.newDirectoryStream(folder)) { - for (Path file : stream) { - // Skip non-image files - if (!Files.isRegularFile(file) || !isImage(file)) { - continue; - } - - // Get the score of the file name against the search name - int score = matchScore(file.getFileName().toString().toLowerCase(), fileName); - - // Update the best match if the score is higher - if (score > bestScore) { - bestMatch = file; - bestScore = score; - } + DirectoryStream stream = Files.newDirectoryStream(folder); + for (Path file : stream) { + if (file.getFileName().toString().equalsIgnoreCase(name)) { + return file; } } - - return bestMatch; + return null; } - /** - * Returns true if the specified file is a regular file with a MIME type that - * starts with "image/", indicating that it is an image file. - * - * This function was generated by ChatGPT. - * - * @param file the path of the file to check - * @return true if the file is an image file, false otherwise - * @throws IOException if an I/O error occurs during the MIME type detection - */ - private static boolean isImage(Path file) throws IOException { - String fileType = Files.probeContentType(file); - return fileType != null && fileType.startsWith("image/"); - } - - /** - * Computes a simple matching score between the given file name and search name. - * The score is the number of characters in the search name that are present in the - * file name, ignoring case. - * - * This function was generated by ChatGPT. - * - * @param fileName the name of the file to match against - * @param searchName the name to search for - * @return the matching score - */ - private static int matchScore(String fileName, String searchName) { - // Compute the score as the number of characters in the search name - // that are present in the file name, ignoring case - int score = 0; - for (int i = 0; i < searchName.length(); i++) { - if (fileName.indexOf(searchName.charAt(i)) >= 0) { - score++; - } - } - return score; - } }