diff --git a/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/config/Config.java b/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/config/Config.java index c50f02c..3fae293 100644 --- a/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/config/Config.java +++ b/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/config/Config.java @@ -37,6 +37,7 @@ public class Config { private boolean safeMode; private Integer coherent; + private boolean forceCoherent; private Set forcedKeywords = new HashSet<>(Arrays.asList("forced", "signs", "songs")); private Set commentaryKeywords = new HashSet<>(Arrays.asList("commentary", "director")); diff --git a/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/config/ConfigLoader.java b/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/config/ConfigLoader.java index 7673311..9abab3c 100644 --- a/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/config/ConfigLoader.java +++ b/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/config/ConfigLoader.java @@ -29,7 +29,8 @@ public class ConfigLoader { new SetValidator(COMMENTARY_KEYWORDS, false, true), new SetValidator(EXCLUDED_DIRECTORY, false, true), new AttributeConfigValidator(), - new CoherentConfigValidator(COHERENT, false) + new CoherentConfigValidator(COHERENT, false), + new BooleanValidator(FORCE_COHERENT, false) ); public static void initConfig(String[] args) { diff --git a/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/config/validator/ConfigValidator.java b/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/config/validator/ConfigValidator.java index 0aa4fc3..80e701d 100644 --- a/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/config/validator/ConfigValidator.java +++ b/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/config/validator/ConfigValidator.java @@ -121,27 +121,27 @@ public abstract class ConfigValidator { * @return false if method invocation failed */ protected boolean setValue(FieldType result) { - List methods = Arrays.stream(Config.getInstance().getClass().getDeclaredMethods()) - .filter(containsSetterOf(property)) - .collect(Collectors.toList()); - if (methods.size() != 1) { - return false; - } - try { - methods.get(0).invoke(Config.getInstance(), result); - } catch (IllegalAccessException | InvocationTargetException e) { - throw new RuntimeException(e); - } - return true; + for (Method method: Config.getInstance().getClass().getDeclaredMethods()) { + if(containsSetterOf(property).test(method)) { + try { + method.invoke(Config.getInstance(), result); + return true; + } catch (IllegalAccessException | InvocationTargetException e) { + throw new RuntimeException(e); + } + } + } + + return false; } protected Predicate containsSetterOf(ConfigProperty property) { - return method -> StringUtils.containsIgnoreCase(method.getName(), "set") - && StringUtils.containsIgnoreCase(method.getName(), property.prop().replace("-", "")); + return method -> StringUtils.startsWith(method.getName(), "set") + && StringUtils.equalsIgnoreCase(method.getName().replace("set", ""), property.prop().replace("-", "")); } protected Predicate containsGetterOf(ConfigProperty property) { - return method -> StringUtils.containsIgnoreCase(method.getName(), "get") + return method -> StringUtils.startsWith(method.getName(), "get") && StringUtils.containsIgnoreCase(method.getName(), property.prop().replace("-", "")); } diff --git a/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/impl/kernel/AttributeUpdaterKernel.java b/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/impl/kernel/AttributeUpdaterKernel.java index 7adce3f..440806a 100644 --- a/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/impl/kernel/AttributeUpdaterKernel.java +++ b/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/impl/kernel/AttributeUpdaterKernel.java @@ -4,6 +4,7 @@ import at.pcgamingfreaks.mkvaudiosubtitlechanger.config.Config; import at.pcgamingfreaks.mkvaudiosubtitlechanger.exceptions.MkvToolNixException; import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.FileCollector; import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.FileProcessor; +import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.FileAttribute; import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.FileInfoDto; import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.ResultStatistic; import lombok.RequiredArgsConstructor; @@ -84,7 +85,18 @@ public abstract class AttributeUpdaterKernel { * * @param file file or directory to update */ - abstract void process(File file); + void process(File file) { + FileInfoDto fileInfo = new FileInfoDto(file); + List attributes = processor.loadAttributes(file); + + List nonForcedTracks = processor.retrieveNonForcedTracks(attributes); + List nonCommentaryTracks = processor.retrieveNonCommentaryTracks(attributes); + + processor.detectDefaultTracks(fileInfo, attributes, nonForcedTracks); + processor.detectDesiredTracks(fileInfo, nonForcedTracks, nonCommentaryTracks); + + updateFile(fileInfo); + } /** * Persist file changes. @@ -92,7 +104,6 @@ public abstract class AttributeUpdaterKernel { * @param fileInfoDto contains information about file and desired configuration. */ protected void updateFile(FileInfoDto fileInfoDto) { - statistic.total(); switch (fileInfoDto.getStatus()) { case CHANGE_NECESSARY: statistic.shouldChange(); @@ -117,6 +128,7 @@ public abstract class AttributeUpdaterKernel { } try { + statistic.total(); processor.update(fileInfo.getFile(), fileInfo); statistic.success(); log.info("Updated {}", fileInfo.getFile().getAbsolutePath()); diff --git a/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/impl/kernel/CoherentAttributeUpdaterKernel.java b/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/impl/kernel/CoherentAttributeUpdaterKernel.java index 1f0acf8..068d4c1 100644 --- a/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/impl/kernel/CoherentAttributeUpdaterKernel.java +++ b/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/impl/kernel/CoherentAttributeUpdaterKernel.java @@ -61,6 +61,7 @@ public class CoherentAttributeUpdaterKernel extends AttributeUpdaterKernel { } for (AttributeConfig config : Config.getInstance().getAttributeConfig()) { + for (FileInfoDto fileInfo : fileInfos) { List attributes = fileAttributeCache.get(fileInfo); @@ -72,8 +73,10 @@ public class CoherentAttributeUpdaterKernel extends AttributeUpdaterKernel { } if (fileInfos.stream().allMatch(elem -> elem.getDesiredSubtitleLane() != null && elem.getDesiredAudioLane() != null)) { - log.debug("Found {}/{} match for {}", config.getAudioLanguage(), config.getSubtitleLanguage(), file.getAbsolutePath()); - break; + log.info("Found {}/{} match for {}", config.getAudioLanguage(), config.getSubtitleLanguage(), file.getAbsolutePath()); + statistic.increaseTotalBy(fileInfos.size()); + fileInfos.forEach(this::updateFile); + return; // match found, end process here } fileInfos.forEach(f -> { @@ -82,8 +85,13 @@ public class CoherentAttributeUpdaterKernel extends AttributeUpdaterKernel { }); } - // apply config - - // apply default process if nothing was found (if parameter is set) + for(FileInfoDto fileInfo: fileInfos) { + statistic.total(); + if (Config.getInstance().isForceCoherent()) { + super.process(fileInfo.getFile()); + } else { + statistic.excluded(); + } + } } } diff --git a/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/impl/kernel/DefaultAttributeUpdaterKernel.java b/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/impl/kernel/DefaultAttributeUpdaterKernel.java index e6ab6e7..69982e2 100644 --- a/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/impl/kernel/DefaultAttributeUpdaterKernel.java +++ b/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/impl/kernel/DefaultAttributeUpdaterKernel.java @@ -3,13 +3,9 @@ package at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.kernel; import at.pcgamingfreaks.mkvaudiosubtitlechanger.config.Config; import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.FileCollector; import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.FileProcessor; -import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.FileAttribute; -import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.FileInfoDto; import lombok.extern.slf4j.Slf4j; -import org.apache.commons.lang3.StringUtils; import java.io.File; -import java.util.Collection; import java.util.List; import java.util.stream.Collectors; @@ -30,21 +26,4 @@ public class DefaultAttributeUpdaterKernel extends AttributeUpdaterKernel { .filter(file -> !excludedFiles.contains(file)) .collect(Collectors.toList()); } - - /** - * {@inheritDoc} - */ - @Override - void process(File file) { - FileInfoDto fileInfo = new FileInfoDto(file); - List attributes = processor.loadAttributes(file); - - List nonForcedTracks = processor.retrieveNonForcedTracks(attributes); - List nonCommentaryTracks = processor.retrieveNonCommentaryTracks(attributes); - - processor.detectDefaultTracks(fileInfo, attributes, nonForcedTracks); - processor.detectDesiredTracks(fileInfo, nonForcedTracks, nonCommentaryTracks); - - updateFile(fileInfo); - } } diff --git a/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/model/ConfigProperty.java b/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/model/ConfigProperty.java index de8275d..58debfd 100644 --- a/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/model/ConfigProperty.java +++ b/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/model/ConfigProperty.java @@ -10,12 +10,13 @@ import java.util.stream.Collectors; @AllArgsConstructor public enum ConfigProperty { - LIBRARY("library", "Path to library", "l", 1), + LIBRARY("library-path", "Path to library", "l", 1), ATTRIBUTE_CONFIG("attribute-config", "Attribute config to decide which tracks to choose when", "a", Option.UNLIMITED_VALUES), CONFIG_PATH("config-path", "Path to config file", "p", 1), MKV_TOOL_NIX("mkvtoolnix", "Path to mkv tool nix installation", "m", 1), SAFE_MODE("safe-mode", "Test run (no files will be changes)", "s", 0), - COHERENT("coherent", "Try to match whole series with same config", "c", 1), + COHERENT("coherent", "Try to match all files in dir of depth with the same config", "c", 1), + FORCE_COHERENT("force-coherent", "Force coherent and don't update anything if config fits not whole config (default: false)", "cf", 0), WINDOWS("windows", "Is operating system windows", null, 0), THREADS("threads", "Thread count (default: 2)", "t", 1), INCLUDE_PATTERN("include-pattern", "Include files matching pattern (default: \".*\")", "i", 1), diff --git a/src/test/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/config/validator/PathValidatorTest.java b/src/test/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/config/validator/PathValidatorTest.java index 4e91a56..a4d6842 100644 --- a/src/test/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/config/validator/PathValidatorTest.java +++ b/src/test/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/config/validator/PathValidatorTest.java @@ -39,7 +39,7 @@ class PathValidatorTest { private static Stream provideTestCases() { return Stream.of( - argumentsOf(LIBRARY, false, null, "library: " + TEST_DIR, new String[]{}, VALID), + argumentsOf(LIBRARY, false, null, "library-path: " + TEST_DIR, new String[]{}, VALID), argumentsOf(LIBRARY, true, null, "", new String[]{"-l", TEST_FILE}, VALID), argumentsOf(LIBRARY, false, TEST_DIR, "", new String[]{}, DEFAULT), @@ -48,7 +48,7 @@ class PathValidatorTest { argumentsOf(LIBRARY, false, null, "", new String[]{}, NOT_PRESENT), argumentsOf(LIBRARY, true, null, "", new String[]{"-l", TEST_DIR + "/invalid"}, INVALID), - argumentsOf(LIBRARY, false, null, "library: " + TEST_DIR + "/invalid", new String[]{}, INVALID), + argumentsOf(LIBRARY, false, null, "library-path: " + TEST_DIR + "/invalid", new String[]{}, INVALID), argumentsOf(LIBRARY, true, TEST_DIR, "", new String[]{"-l", TEST_DIR + "/invalid"}, INVALID) ); }