Restructure files & remove unused code & simplify few statements

This commit is contained in:
2022-03-23 21:02:28 +01:00
parent 1df06e8a08
commit a1e9031cbc
7 changed files with 46 additions and 66 deletions

View File

@@ -1,10 +1,10 @@
package at.pcgamingfreaks.mkvaudiosubtitlechanger; package at.pcgamingfreaks.mkvaudiosubtitlechanger;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.config.AttributeConfig; import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.AttributeConfig;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.config.ConfigProcessor; import at.pcgamingfreaks.mkvaudiosubtitlechanger.intimpl.ConfigProcessor;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.intimpl.MkvFileCollector; import at.pcgamingfreaks.mkvaudiosubtitlechanger.intimpl.MkvFileCollector;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.FileAttribute; import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.FileAttribute;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.util.ConfigUtil; import at.pcgamingfreaks.mkvaudiosubtitlechanger.config.Config;
import lombok.SneakyThrows; import lombok.SneakyThrows;
import lombok.extern.log4j.Log4j2; import lombok.extern.log4j.Log4j2;
@@ -20,10 +20,10 @@ public class AttributeUpdaterKernel {
long runtime = 0; long runtime = 0;
@SneakyThrows @SneakyThrows
public void execute(String path) { public void execute() {
List<AttributeConfig> configPattern = ConfigUtil.getInstance().getAttributeConfig(); List<AttributeConfig> configPattern = Config.getInstance().getAttributeConfig();
List<File> allValidPaths = collector.loadFiles(path); List<File> allValidPaths = collector.loadFiles(Config.getInstance().getLibraryPath());
ExecutorService executor = Executors.newFixedThreadPool(ConfigUtil.getInstance().getThreadCount()); ExecutorService executor = Executors.newFixedThreadPool(Config.getInstance().getThreadCount());
long beforeTimer = System.currentTimeMillis(); long beforeTimer = System.currentTimeMillis();
if(allValidPaths != null && configPattern != null){ if(allValidPaths != null && configPattern != null){
@@ -37,10 +37,10 @@ public class AttributeUpdaterKernel {
runtime = System.currentTimeMillis() - beforeTimer; runtime = System.currentTimeMillis() - beforeTimer;
System.out.printf("%nFiles %schanged: %s%n", System.out.printf("%nFiles %schanged: %s%n",
ConfigUtil.getInstance().isSafeMode() ? "would " : "", Config.getInstance().isSafeMode() ? "would " : "",
filesChangedAmount); filesChangedAmount);
System.out.printf("Files %s not changed: %s%n", System.out.printf("Files %s not changed: %s%n",
ConfigUtil.getInstance().isSafeMode() ? "would " : "", Config.getInstance().isSafeMode() ? "would " : "",
filesNotChangedAmount); filesNotChangedAmount);
System.out.printf("Runtime: %ss%n", runtime / 1000); System.out.printf("Runtime: %ss%n", runtime / 1000);
} }

View File

@@ -1,43 +1,17 @@
package at.pcgamingfreaks.mkvaudiosubtitlechanger; package at.pcgamingfreaks.mkvaudiosubtitlechanger;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.util.ConfigUtil; import at.pcgamingfreaks.mkvaudiosubtitlechanger.config.Config;
import at.pcgamingfreaks.yaml.YAML;
import at.pcgamingfreaks.yaml.YamlInvalidContentException;
import at.pcgamingfreaks.yaml.YamlKeyNotFoundException;
import lombok.extern.log4j.Log4j2; import lombok.extern.log4j.Log4j2;
import org.apache.commons.cli.*; import org.apache.commons.cli.*;
import java.io.File;
import java.io.IOException;
import static java.lang.Integer.parseInt; import static java.lang.Integer.parseInt;
@Log4j2 @Log4j2
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
System.out.println(String.join(", ", args));
initConfig(args); initConfig(args);
AttributeUpdaterKernel kernel = new AttributeUpdaterKernel(); AttributeUpdaterKernel kernel = new AttributeUpdaterKernel();
kernel.execute(ConfigUtil.getInstance().getLibraryPath()); kernel.execute();
}
private static boolean checkIfMKVToolNixIsValid() {
try {
String path = new YAML(new File("config.yaml")).getString("mkvtoolnixPath");
if (!path.endsWith(File.separator)) {
path += File.separator;
}
if (System.getProperty("os.name").toLowerCase().contains("windows")) {
MKVToolProperties.getInstance().setMkvmergePath(path + "mkvmerge.exe");
MKVToolProperties.getInstance().setMkvpropeditPath(path + "mkvpropedit.exe");
} else {
MKVToolProperties.getInstance().setMkvmergePath(path + "mkvmerge");
MKVToolProperties.getInstance().setMkvpropeditPath(path + "mkvpropedit");
}
} catch (YamlKeyNotFoundException | IOException | YamlInvalidContentException e) {
e.printStackTrace();
}
return new File(MKVToolProperties.getInstance().getMkvmergePath()).isFile() && new File(MKVToolProperties.getInstance().getMkvpropeditPath()).isFile();
} }
private static void initConfig(String[] args) { private static void initConfig(String[] args) {
@@ -53,12 +27,17 @@ public class Main {
try { try {
CommandLine cmd = parser.parse(options, args); CommandLine cmd = parser.parse(options, args);
ConfigUtil configUtil = ConfigUtil.getInstance(); if (cmd.hasOption("help")) {
configUtil.loadConfig(cmd.getOptionValue("config", "config.yaml")); // use cmd input formatter.printHelp("java -jar MKVAudioSubtitlesChanger.jar -p <path_to_library>", options);
configUtil.setLibraryPath(cmd.getOptionValue("library")); System.exit(0);
if (cmd.hasOption("threads")) configUtil.setThreadCount(parseInt(cmd.getOptionValue("threads"))); }
configUtil.setSafeMode(cmd.hasOption("safe-mode"));
configUtil.isValid(); Config config = Config.getInstance();
config.loadConfig(cmd.getOptionValue("config", "config.yaml")); // use cmd input
config.setLibraryPath(cmd.getOptionValue("library"));
if (cmd.hasOption("threads")) config.setThreadCount(parseInt(cmd.getOptionValue("threads")));
config.setSafeMode(cmd.hasOption("safe-mode"));
config.isValid();
} catch (ParseException e) { } catch (ParseException e) {
log.error(e); log.error(e);
formatter.printHelp("java -jar MKVAudioSubtitlesChanger.jar -p <path_to_library>", options); formatter.printHelp("java -jar MKVAudioSubtitlesChanger.jar -p <path_to_library>", options);

View File

@@ -1,6 +1,7 @@
package at.pcgamingfreaks.mkvaudiosubtitlechanger.util; package at.pcgamingfreaks.mkvaudiosubtitlechanger.config;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.config.AttributeConfig; import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.AttributeConfig;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.MkvToolNix;
import at.pcgamingfreaks.yaml.YAML; import at.pcgamingfreaks.yaml.YAML;
import at.pcgamingfreaks.yaml.YamlInvalidContentException; import at.pcgamingfreaks.yaml.YamlInvalidContentException;
import at.pcgamingfreaks.yaml.YamlKeyNotFoundException; import at.pcgamingfreaks.yaml.YamlKeyNotFoundException;
@@ -17,10 +18,10 @@ import java.util.stream.Collectors;
@Log4j2 @Log4j2
@Getter @Getter
@Setter @Setter
public class ConfigUtil { public class Config {
@Getter(AccessLevel.NONE) @Getter(AccessLevel.NONE)
@Setter(AccessLevel.NONE) @Setter(AccessLevel.NONE)
private static ConfigUtil configUtil = null; private static Config config = null;
private List<AttributeConfig> attributeConfig; private List<AttributeConfig> attributeConfig;
private int threadCount; private int threadCount;
@@ -28,12 +29,13 @@ public class ConfigUtil {
private String mkvtoolnixPath; private String mkvtoolnixPath;
private String libraryPath; private String libraryPath;
private boolean isSafeMode; private boolean isSafeMode;
private boolean isWindows;
public static ConfigUtil getInstance() { public static Config getInstance() {
if(configUtil == null) { if(config == null) {
configUtil = new ConfigUtil(); config = new Config();
} }
return configUtil; return config;
} }
public void isValid() throws RuntimeException{ public void isValid() throws RuntimeException{
@@ -55,6 +57,7 @@ public class ConfigUtil {
setAttributeConfig(loadAttributeConfig(config)); setAttributeConfig(loadAttributeConfig(config));
setThreadCount(loadThreadCount(config)); setThreadCount(loadThreadCount(config));
setMkvtoolnixPath(loadMkvToolNixPath(config)); setMkvtoolnixPath(loadMkvToolNixPath(config));
setWindows(System.getProperty("os.name").toLowerCase().contains("windows"));
}catch(YamlInvalidContentException | YamlKeyNotFoundException | IOException e){ }catch(YamlInvalidContentException | YamlKeyNotFoundException | IOException e){
log.fatal("Config could not be loaded: {}", e.getMessage()); log.fatal("Config could not be loaded: {}", e.getMessage());
} }

View File

@@ -1,8 +1,10 @@
package at.pcgamingfreaks.mkvaudiosubtitlechanger.config; package at.pcgamingfreaks.mkvaudiosubtitlechanger.intimpl;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.MKVToolProperties; import at.pcgamingfreaks.mkvaudiosubtitlechanger.MKVToolProperties;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.config.Config;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.AttributeConfig;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.FileAttribute; import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.FileAttribute;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.util.ConfigUtil; import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.MkvToolNix;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import lombok.extern.log4j.Log4j2; import lombok.extern.log4j.Log4j2;
@@ -12,6 +14,7 @@ import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.function.Supplier;
import static java.lang.String.format; import static java.lang.String.format;
@@ -92,11 +95,11 @@ public class ConfigProcessor {
StringBuilder stringBuffer = new StringBuilder(); StringBuilder stringBuffer = new StringBuilder();
if(System.getProperty("os.name").toLowerCase().contains("windows")){ if(System.getProperty("os.name").toLowerCase().contains("windows")){
stringBuffer.append(format("\"%s\" \"%s\" ", stringBuffer.append(format("\"%s\" \"%s\" ",
MKVToolProperties.getInstance().getMkvpropeditPath(), Config.getInstance().getPathFor(MkvToolNix.MKV_PROP_EDIT),
file.getAbsolutePath())); file.getAbsolutePath()));
}else{ }else{
stringBuffer.append(format("%s %s ", stringBuffer.append(format("%s %s ",
MKVToolProperties.getInstance().getMkvpropeditPath(), Config.getInstance().getPathFor(MkvToolNix.MKV_PROP_EDIT),
file.getAbsolutePath())); file.getAbsolutePath()));
} }
if(audioDefault != - 1){ if(audioDefault != - 1){
@@ -122,7 +125,7 @@ public class ConfigProcessor {
return true; return true;
} }
try{ try{
if(!ConfigUtil.getInstance().isSafeMode()) { if(!Config.getInstance().isSafeMode()) {
Runtime.getRuntime().exec(stringBuffer.toString()); Runtime.getRuntime().exec(stringBuffer.toString());
} }
}catch(IOException e){ }catch(IOException e){

View File

@@ -1,8 +1,8 @@
package at.pcgamingfreaks.mkvaudiosubtitlechanger.intimpl; package at.pcgamingfreaks.mkvaudiosubtitlechanger.intimpl;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.FileAttribute; import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.FileAttribute;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.util.ConfigUtil; import at.pcgamingfreaks.mkvaudiosubtitlechanger.config.Config;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.util.MkvToolNix; import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.MkvToolNix;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.log4j.Log4j2; import lombok.extern.log4j.Log4j2;
@@ -58,12 +58,7 @@ public class MkvFileCollector implements FileCollector {
Map<String, Object> jsonMap; Map<String, Object> jsonMap;
List<FileAttribute> fileAttributes = new ArrayList<>(); List<FileAttribute> fileAttributes = new ArrayList<>();
try { try {
String command = ""; String command = String.format("\"%s\"", Config.getInstance().getPathFor(MkvToolNix.MKV_MERGER));
if (System.getProperty("os.name").toLowerCase().contains("windows")) {
command = "\"" + ConfigUtil.getInstance().getPathFor(MkvToolNix.MKV_MERGER) + "\"";
} else {
command = ConfigUtil.getInstance().getPathFor(MkvToolNix.MKV_MERGER);
}
String[] array = new String[]{ String[] array = new String[]{
command, command,
"--identify", "--identify",

View File

@@ -1,4 +1,4 @@
package at.pcgamingfreaks.mkvaudiosubtitlechanger.config; package at.pcgamingfreaks.mkvaudiosubtitlechanger.model;
import lombok.Getter; import lombok.Getter;
import lombok.extern.log4j.Log4j2; import lombok.extern.log4j.Log4j2;

View File

@@ -1,4 +1,4 @@
package at.pcgamingfreaks.mkvaudiosubtitlechanger.util; package at.pcgamingfreaks.mkvaudiosubtitlechanger.model;
public enum MkvToolNix { public enum MkvToolNix {
MKV_MERGER("mkvmerge.exe"), MKV_MERGER("mkvmerge.exe"),