|
|
@ -1,12 +1,16 @@ |
|
|
|
package de.yannicpunktdee.yoshibot.main; |
|
|
|
|
|
|
|
import de.yannicpunktdee.yoshibot.utils.Logger; |
|
|
|
import de.yannicpunktdee.yoshibot.utils.Logger.Type; |
|
|
|
|
|
|
|
import java.io.File; |
|
|
|
import java.io.FileInputStream; |
|
|
|
import java.io.IOException; |
|
|
|
import java.util.Properties; |
|
|
|
|
|
|
|
public class Resources { |
|
|
|
|
|
|
|
private static final String default_propertiesFilePath = "Config.properties"; |
|
|
|
private static final String default_propertiesFilePath = "./rsc/Config.properties"; |
|
|
|
private static String propertiesFilePath = default_propertiesFilePath; |
|
|
|
private static Properties propertiesFile; |
|
|
|
|
|
|
@ -19,45 +23,71 @@ public class Resources { |
|
|
|
private static String restrict_commands_to_channel = default_restrict_commands_to_channel; |
|
|
|
|
|
|
|
|
|
|
|
public synchronized static void init(String pathToConfig) { |
|
|
|
if(pathToConfig != null) propertiesFilePath = pathToConfig; |
|
|
|
if(!(new File(propertiesFilePath)).exists()) propertiesFilePath = default_propertiesFilePath; |
|
|
|
|
|
|
|
public synchronized static boolean init(String pathToConfig) { |
|
|
|
Logger.log("Lade Config.properties ...", Type.INFO); |
|
|
|
|
|
|
|
if(pathToConfig != null){ |
|
|
|
if(!(new File(pathToConfig)).exists()){ |
|
|
|
Logger.log("Der in den Argumenten angegebene Pfad zur Config.properties existiert nicht.", Type.ERROR); |
|
|
|
return false; |
|
|
|
} |
|
|
|
propertiesFilePath = pathToConfig; |
|
|
|
}else if(!(new File(propertiesFilePath)).exists()){ |
|
|
|
Logger.log("Es wurde keine Config-Datei über den Pfad \"" + propertiesFilePath + "\" gefunden.", Type.ERROR); |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
propertiesFile = new Properties(); |
|
|
|
try { |
|
|
|
propertiesFile.load(Resources.class.getClassLoader().getResourceAsStream(propertiesFilePath)); |
|
|
|
System.out.println("Properties-Datei erfolgreich geladen."); |
|
|
|
propertiesFile.load( |
|
|
|
new FileInputStream(default_propertiesFilePath) |
|
|
|
); |
|
|
|
Logger.log("Config-Datei erfolgreich geladen.", Type.INFO); |
|
|
|
} catch (IOException e) { |
|
|
|
System.err.println("Es wurde keine Config-Datei gefunden. Benutze Standards."); |
|
|
|
return; |
|
|
|
Logger.log("Es ist ein Fehler beim Öffnen der Config.propeties aufgetreten.", Type.ERROR); |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
initJdaBuilderString(); |
|
|
|
initAudio(); |
|
|
|
initChannelRestrict(); |
|
|
|
boolean isOk = true; |
|
|
|
if(isOk) isOk &= initJdaBuilderString(); |
|
|
|
if(isOk) isOk &= initAudio(); |
|
|
|
if(isOk) isOk &= initChannelRestrict(); |
|
|
|
|
|
|
|
if(isOk) Logger.log("Die Konfigurationen wurden erfolgreich geladen.", Type.INFO); |
|
|
|
else Logger.log("Die Konfiguration konnte nicht geladen werden", Type.ERROR); |
|
|
|
|
|
|
|
return isOk; |
|
|
|
} |
|
|
|
|
|
|
|
private static void initJdaBuilderString() { |
|
|
|
private static boolean initJdaBuilderString() { |
|
|
|
if(!propertiesFile.containsKey("jda_builder_string")) { |
|
|
|
System.err.println("Es wurde kein jda_builder_string gefunden."); |
|
|
|
YoshiBot.stop(); |
|
|
|
} else jda_builder_string = propertiesFile.getProperty("jda_builder_string"); |
|
|
|
Logger.log("Die Config.properties benötigt das Attribut jda_builder_string.", Type.ERROR); |
|
|
|
return false; |
|
|
|
} |
|
|
|
jda_builder_string = propertiesFile.getProperty("jda_builder_string"); |
|
|
|
return true; |
|
|
|
} |
|
|
|
public static String getJdaBuilderString() { |
|
|
|
return jda_builder_string; |
|
|
|
} |
|
|
|
|
|
|
|
private static void initAudio() { |
|
|
|
if(!propertiesFile.containsKey("audio_source_directory")) return; |
|
|
|
private static boolean initAudio() { |
|
|
|
if(propertiesFile.containsKey("audio_source_directory")) { |
|
|
|
audio_source_directory = propertiesFile.getProperty("audio_source_directory"); |
|
|
|
}else{ |
|
|
|
Logger.log("Die Config.properties spezifiziert kein audio_source_directory. Lade default.", Type.WARNING); |
|
|
|
} |
|
|
|
|
|
|
|
String dir = propertiesFile.getProperty("audio_source_directory"); |
|
|
|
File file = new File(dir); |
|
|
|
File file = new File(audio_source_directory); |
|
|
|
if(!file.exists() || !file.isDirectory()){ |
|
|
|
System.err.println("Das Audio-Verzeichnis wurde nicht gefunden"); |
|
|
|
return; |
|
|
|
Logger.log("Das Audio-Verzeichnis wurde nicht gefunden.", Type.ERROR); |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
audio_source_directory = dir; |
|
|
|
if(file.listFiles().length < 1) |
|
|
|
Logger.log("Das Audio-Verzeichnis ist leer.", Type.WARNING); |
|
|
|
|
|
|
|
return true; |
|
|
|
} |
|
|
|
public static String getAudioFilePath(String name) { |
|
|
|
name = audio_source_directory + (audio_source_directory.endsWith("/")? "" : "/") + name + ".opus"; |
|
|
@ -68,9 +98,10 @@ public class Resources { |
|
|
|
return audio_source_directory; |
|
|
|
} |
|
|
|
|
|
|
|
private static void initChannelRestrict() { |
|
|
|
private static boolean initChannelRestrict() { |
|
|
|
if(propertiesFile.containsKey("restrict_commands_to_channel")) |
|
|
|
restrict_commands_to_channel = propertiesFile.getProperty("restrict_commands_to_channel"); |
|
|
|
return true; |
|
|
|
} |
|
|
|
public static String getRestrictCommandsToChannel() { |
|
|
|
return restrict_commands_to_channel; |
|
|
|