181 lines
5.1 KiB
Java
181 lines
5.1 KiB
Java
package manager;
|
|
|
|
import java.io.BufferedReader;
|
|
import java.io.File;
|
|
import java.io.FileInputStream;
|
|
import java.io.FileOutputStream;
|
|
import java.io.FileReader;
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.io.OutputStream;
|
|
import java.util.Properties;
|
|
|
|
import main.Start;
|
|
|
|
public class SettingManager {
|
|
|
|
|
|
|
|
private static boolean loadFileOnBoot = true;
|
|
private static boolean checkUptdateOnBoot = true;
|
|
private static int key1 = -1,key2 = -1;
|
|
private static OperatingSystem currentOS = null;
|
|
|
|
private static String jarDirectory = "";
|
|
|
|
public SettingManager(){
|
|
if(System.getProperty("os.name").toLowerCase().contains("nux")){
|
|
currentOS = OperatingSystem.LINUX;
|
|
}
|
|
else if(System.getProperty("os.name").toLowerCase().contains("win")) {
|
|
currentOS = OperatingSystem.WINDOWS;
|
|
}
|
|
else if(System.getProperty("os.name").toLowerCase().contains("mac")) {
|
|
currentOS = OperatingSystem.OSX;
|
|
}
|
|
|
|
|
|
String path = Start.class.getProtectionDomain().getCodeSource().getLocation().getPath();
|
|
jarDirectory = new File(path).getParentFile().getPath();
|
|
}
|
|
|
|
public static void loadSettings(){
|
|
try{
|
|
File settingFile = new File(jarDirectory+File.separator+"settings.properties");
|
|
File legacyFile = new File(jarDirectory+File.separator+"settings.txt");
|
|
if(settingFile.exists()){
|
|
if(legacyFile.exists()){
|
|
legacyFile.delete();
|
|
}
|
|
InputStream input = null;
|
|
Properties prop = new Properties();
|
|
try{
|
|
input = new FileInputStream(settingFile);
|
|
prop.load(input);
|
|
loadFileOnBoot = Boolean.parseBoolean(prop.getProperty("Load-File-on-startup", "true"));
|
|
checkUptdateOnBoot = Boolean.parseBoolean(prop.getProperty("Check-for-updates-on-startup", "true"));
|
|
key1 = Integer.parseInt(prop.getProperty("key1", "-1"));
|
|
key2 = Integer.parseInt(prop.getProperty("key2", "-1"));
|
|
}catch (IOException e) {
|
|
e.printStackTrace();
|
|
}finally{
|
|
input.close();
|
|
}
|
|
}
|
|
else{
|
|
if(legacyFile.exists()){
|
|
BufferedReader bfr = new BufferedReader(new FileReader(legacyFile));
|
|
/////////////////////////////////////
|
|
String currentLine = bfr.readLine();
|
|
while(currentLine != null){
|
|
if(currentLine.contains("Load File")){
|
|
if(currentLine.contains("true")){
|
|
loadFileOnBoot = true;
|
|
}
|
|
else if(currentLine.contains("false")){
|
|
loadFileOnBoot = false;
|
|
}
|
|
}
|
|
else if(currentLine.contains("Check for newer")){
|
|
if(currentLine.contains("true")){
|
|
checkUptdateOnBoot = true;
|
|
}
|
|
else if(currentLine.contains("false")){
|
|
checkUptdateOnBoot = false;
|
|
}
|
|
}
|
|
else if(currentLine.contains(";")){
|
|
key1 = Integer.parseInt(currentLine.substring(0, 2));
|
|
key2 = Integer.parseInt(currentLine.substring(3, 5));
|
|
}
|
|
currentLine = bfr.readLine();
|
|
}
|
|
bfr.close();
|
|
}
|
|
else{
|
|
System.out.println("No setting file found probably first start");
|
|
saveSettings();
|
|
System.out.println("Setting file created");
|
|
}
|
|
}
|
|
|
|
}catch(NullPointerException npe){
|
|
saveSettings();
|
|
System.out.println("npe There is an error in your setting file\nYour settings File was updated");
|
|
}catch(NumberFormatException nfe){
|
|
saveSettings();
|
|
System.out.println("There is an error in your setting file\nYour settings File was updated");
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
}
|
|
|
|
public static void saveSettings(){
|
|
OutputStream out = null;
|
|
try {
|
|
File settingFile = new File(jarDirectory+File.separator+"settings.properties");
|
|
Properties prop = new Properties();
|
|
out = new FileOutputStream(settingFile);
|
|
prop.setProperty("Load-File-on-startup", ""+loadFileOnBoot);
|
|
prop.setProperty("Check-for-updates-on-startup", ""+checkUptdateOnBoot);
|
|
prop.setProperty("key1", ""+key1);
|
|
prop.setProperty("key2", ""+key2);
|
|
|
|
prop.store(out, null);
|
|
} catch (IOException e) {
|
|
System.out.println("Writing permission denied");
|
|
} finally{
|
|
try {
|
|
out.close();
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|
|
|
|
public static OperatingSystem getOperatingSystem() {
|
|
return currentOS;
|
|
}
|
|
|
|
public static boolean isLoadFileOnBoot() {
|
|
return loadFileOnBoot;
|
|
}
|
|
|
|
public static void setLoadFileOnBoot(boolean loadFileOnBoot) {
|
|
SettingManager.loadFileOnBoot = loadFileOnBoot;
|
|
}
|
|
|
|
public static boolean isCheckUptdateOnBoot() {
|
|
return checkUptdateOnBoot;
|
|
}
|
|
|
|
public static void setCheckUptdateOnBoot(boolean checkUptdateOnBoot) {
|
|
SettingManager.checkUptdateOnBoot = checkUptdateOnBoot;
|
|
}
|
|
|
|
public static int getKey1() {
|
|
return key1;
|
|
}
|
|
|
|
public static void setKey1(int key1) {
|
|
SettingManager.key1 = key1;
|
|
}
|
|
|
|
public static int getKey2() {
|
|
return key2;
|
|
}
|
|
|
|
public static void setKey2(int key2) {
|
|
SettingManager.key2 = key2;
|
|
}
|
|
|
|
public static String getJarDirectory() {
|
|
return jarDirectory;
|
|
}
|
|
|
|
public static void setJarDirectory(String jarDirectory) {
|
|
SettingManager.jarDirectory = jarDirectory;
|
|
}
|
|
}
|