From 9330deb75fefa5d121a1878d7ba6188ad59ae335 Mon Sep 17 00:00:00 2001 From: RatzzFatzz Date: Sat, 8 Apr 2023 21:07:44 +0200 Subject: [PATCH] Fix last-execution file saving --- .../config/Config.java | 27 +- .../config/validator/ConfigValidator.java | 270 +++++++++--------- .../config/validator/DateValidator.java | 4 +- .../impl/kernel/AttributeUpdaterKernel.java | 6 +- 4 files changed, 163 insertions(+), 144 deletions(-) diff --git a/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/config/Config.java b/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/config/Config.java index baf1913..77382e1 100644 --- a/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/config/Config.java +++ b/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/config/Config.java @@ -72,17 +72,22 @@ public class Config { @Override public String toString() { return new StringJoiner(", ", Config.class.getSimpleName() + "[", "]") - .add("parser=" + parser).add("\n") - .add("formatter=" + formatter).add("\n") - .add("configPath=" + configPath).add("\n") - .add("libraryPath=" + libraryPath).add("\n") - .add("isSafeMode=" + safeMode).add("\n") - .add("forcedKeywords=" + forcedKeywords).add("\n") - .add("commentaryKeywords=" + commentaryKeywords).add("\n") - .add("excludedDirectories=" + excludedDirectories).add("\n") - .add("threadCount=" + threads).add("\n") - .add("includePattern=" + includePattern).add("\n") - .add("mkvToolNixPath='" + mkvToolNix + "'").add("\n") + .add("parser=" + parser) + .add("formatter=" + formatter) + .add("configPath=" + configPath) + .add("libraryPath=" + libraryPath) + .add("mkvToolNix=" + mkvToolNix) + .add("threads=" + threads) + .add("includePattern=" + includePattern) + .add("safeMode=" + safeMode) + .add("coherent=" + coherent) + .add("forceCoherent=" + forceCoherent) + .add("onlyNewFiles=" + onlyNewFiles) + .add("filterDate=" + filterDate) + .add("forcedKeywords=" + forcedKeywords) + .add("commentaryKeywords=" + commentaryKeywords) + .add("excludedDirectories=" + excludedDirectories) + .add("preferredSubtitles=" + preferredSubtitles) .add("attributeConfig=" + attributeConfig) .toString(); } 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 4d2590e..cb15f34 100644 --- a/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/config/validator/ConfigValidator.java +++ b/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/config/validator/ConfigValidator.java @@ -6,155 +6,165 @@ import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.ConfigProperty; import at.pcgamingfreaks.yaml.YAML; import at.pcgamingfreaks.yaml.YamlKeyNotFoundException; import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; import org.apache.commons.cli.CommandLine; import org.apache.commons.lang3.StringUtils; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; -import java.util.List; import java.util.Optional; -import java.util.Set; +import java.util.StringJoiner; import java.util.function.BiFunction; import java.util.function.Predicate; +@Slf4j @RequiredArgsConstructor public abstract class ConfigValidator { - protected final ConfigProperty property; - protected final boolean required; - protected final FieldType defaultValue; + protected final ConfigProperty property; + protected final boolean required; + protected final FieldType defaultValue; - /** - * Validate the user input. Parameters of cmd are prioritised. - * - * @param yaml config file - * @param cmd command line parameters - * @return {@link ValidationResult} containing validity of input. - */ + /** + * Validate the user input. Parameters of cmd are prioritised. + * + * @param yaml config file + * @param cmd command line parameters + * @return {@link ValidationResult} containing validity of input. + */ public ValidationResult validate(YAML yaml, CommandLine cmd) { - System.out.printf("%s: ", property.prop()); - FieldType result; + System.out.printf("%s: ", property.prop()); + FieldType result; - Optional cmdResult = provideDataCmd().apply(cmd, property); - Optional yamlResult = provideDataYaml().apply(yaml, property); + Optional cmdResult = provideDataCmd().apply(cmd, property); + Optional yamlResult = provideDataYaml().apply(yaml, property); - if (isOverwritingNecessary()){ - result = overwriteValue(); - } else if (cmdResult.isPresent()) { - result = cmdResult.get(); - } else if (yamlResult.isPresent()) { - result = yamlResult.get(); - } else { - if (defaultValue != null) { - if (setValue(defaultValue)) { - System.out.println("default"); - return ValidationResult.DEFAULT; - } else { - System.out.println("invalid"); - return ValidationResult.INVALID; - } - } - if (required) { - System.out.println("missing"); - return ValidationResult.MISSING; - } else { - System.out.println("ok"); - return ValidationResult.NOT_PRESENT; - } - } + if (isOverwritingNecessary()) { + result = overwriteValue(); + } else if (cmdResult.isPresent()) { + result = cmdResult.get(); + } else if (yamlResult.isPresent()) { + result = yamlResult.get(); + } else { + if (defaultValue != null) { + if (setValue(defaultValue)) { + System.out.println("default"); + return ValidationResult.DEFAULT; + } else { + System.out.println("invalid"); + return ValidationResult.INVALID; + } + } + if (required) { + System.out.println("missing"); + return ValidationResult.MISSING; + } else { + System.out.println("ok"); + return ValidationResult.NOT_PRESENT; + } + } - if (!isValid(result) || !setValue(result)) { - System.out.println("invalid"); - return ValidationResult.INVALID; - } + if (!isValid(result) || !setValue(result)) { + System.out.println("invalid"); + return ValidationResult.INVALID; + } - System.out.println("ok"); - return ValidationResult.VALID; - } - - /** - * @return parsed input of yaml config for property - */ - protected BiFunction> provideDataYaml() { - return (yaml, property) -> { - if (yaml.isSet(property.prop())) { - try { - return Optional.of(parse(yaml.getString(property.prop()))); - } catch (YamlKeyNotFoundException e) { - throw new RuntimeException(e); - } - } - return Optional.empty(); - }; - } - - /** - * @return parsed input of command line parameters config for property - */ - protected BiFunction> provideDataCmd() { - return (cmd, property) -> { - if (cmd.hasOption(property.prop())) { - return Optional.of(parse(cmd.getOptionValue(property.prop()))); - } - return Optional.empty(); - }; - } - - protected boolean isOverwritingNecessary() { - return false; - } - - protected FieldType overwriteValue() { - return null; - } - - /** - * Parse input parameter to desired format. - * - * @param value input parameter - * @return parsed property - */ - abstract FieldType parse(String value); - - /** - * Validate if the data has the desired and allowed format. - * - * @param result parsed property - * @return true if data is in desired format. - */ - abstract boolean isValid(FieldType result); - - /** - * Sets valid properties to {@link Config} via reflections. - * - * @param result parsed property - * @return false if method invocation failed - */ - protected boolean setValue(FieldType result) { - 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; + System.out.println("ok"); + return ValidationResult.VALID; } - protected Predicate containsSetterOf(ConfigProperty property) { - return method -> StringUtils.startsWith(method.getName(), "set") - && StringUtils.equalsIgnoreCase(method.getName().replace("set", ""), property.prop().replace("-", "")); - } + /** + * @return parsed input of yaml config for property + */ + protected BiFunction> provideDataYaml() { + return (yaml, property) -> { + if (yaml.isSet(property.prop())) { + try { + return Optional.of(parse(yaml.getString(property.prop()))); + } catch (YamlKeyNotFoundException e) { + throw new RuntimeException(e); + } + } + return Optional.empty(); + }; + } - protected Predicate containsGetterOf(ConfigProperty property) { - return method -> StringUtils.startsWith(method.getName(), "get") - && StringUtils.equalsIgnoreCase(method.getName().replace("get", ""), property.prop().replace("-", "")); - } + /** + * @return parsed input of command line parameters config for property + */ + protected BiFunction> provideDataCmd() { + return (cmd, property) -> { + if (cmd.hasOption(property.prop())) { + return Optional.of(parse(cmd.getOptionValue(property.prop()))); + } + return Optional.empty(); + }; + } - public int getWeight() { - return 50; - } + protected boolean isOverwritingNecessary() { + return false; + } + + protected FieldType overwriteValue() { + return null; + } + + /** + * Parse input parameter to desired format. + * + * @param value input parameter + * @return parsed property + */ + abstract FieldType parse(String value); + + /** + * Validate if the data has the desired and allowed format. + * + * @param result parsed property + * @return true if data is in desired format. + */ + abstract boolean isValid(FieldType result); + + /** + * Sets valid properties to {@link Config} via reflections. + * + * @param result parsed property + * @return false if method invocation failed + */ + protected boolean setValue(FieldType result) { + 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.startsWith(method.getName(), "set") + && StringUtils.equalsIgnoreCase(method.getName().replace("set", ""), property.prop().replace("-", "")); + } + + protected Predicate containsGetterOf(ConfigProperty property) { + return method -> StringUtils.startsWith(method.getName(), "get") + && StringUtils.equalsIgnoreCase(method.getName().replace("get", ""), property.prop().replace("-", "")); + } + + public int getWeight() { + return 50; + } + + @Override + public String toString() { + return new StringJoiner(", ", ConfigValidator.class.getSimpleName() + "[", "]") + .add("property=" + property) + .add("required=" + required) + .add("defaultValue=" + defaultValue) + .toString(); + } } diff --git a/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/config/validator/DateValidator.java b/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/config/validator/DateValidator.java index 0976452..c783f42 100644 --- a/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/config/validator/DateValidator.java +++ b/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/config/validator/DateValidator.java @@ -17,7 +17,7 @@ import java.util.Date; @Slf4j public class DateValidator extends ConfigValidator { private static final Date INVALID_DATE = new Date(0); - private static final Date DEFAULT_DATE = new Date(1); + private static final Date DEFAULT_DATE = new Date(1000); public DateValidator(ConfigProperty property, boolean required) { super(property, required, null); @@ -37,7 +37,7 @@ public class DateValidator extends ConfigValidator { return DEFAULT_DATE; } YAML yaml = new YAML(lastExecutionFile); - return parse(yaml.getString(Config.getInstance().getLibraryPath().getAbsolutePath(), DateUtils.convert(DEFAULT_DATE))); + return parse(yaml.getString(Config.getInstance().getLibraryPath().getAbsolutePath().replace("\\", "/"), DateUtils.convert(DEFAULT_DATE))); } catch (YamlInvalidContentException | IOException e) { log.error("Couldn't open last-execution.properties"); return INVALID_DATE; 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 b13b364..239a4de 100644 --- a/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/impl/kernel/AttributeUpdaterKernel.java +++ b/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/impl/kernel/AttributeUpdaterKernel.java @@ -150,6 +150,10 @@ public abstract class AttributeUpdaterKernel { } protected void endProcess() { + if (Config.getInstance().isSafeMode()) { + return; + } + try { String filePath = AppDirsFactory.getInstance().getUserConfigDir(ProjectUtil.getProjectName(), null, null); @@ -162,7 +166,7 @@ public abstract class AttributeUpdaterKernel { } YAML yaml = new YAML(lastExecutionFile); - yaml.set(Config.getInstance().getLibraryPath().getAbsolutePath(), DateUtils.convert(new Date())); + yaml.set(Config.getInstance().getLibraryPath().getAbsolutePath().replace("\\", "/"), DateUtils.convert(new Date())); yaml.save(lastExecutionFile); } catch (IOException | YamlInvalidContentException e) { log.error("last-execution.yml could not be created: ", e); // TODO