Skip to content

Commit

Permalink
Fixed #128
Browse files Browse the repository at this point in the history
  • Loading branch information
LMH01 committed Mar 8, 2023
1 parent 47479f8 commit 0b18569
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 71 deletions.
1 change: 1 addition & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
1 change: 1 addition & 0 deletions docs/changelog_dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Path> 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<Path> 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;
}
}

0 comments on commit 0b18569

Please sign in to comment.