summaryrefslogtreecommitdiff
path: root/src/main/java/de/romjaki/selfbot/Config.java
blob: 6ed13c7150a636fac52c950d24814b2661ae975c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package de.romjaki.selfbot;

import net.dv8tion.jda.core.utils.SimpleLog;

import java.io.*;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Hashtable;
import java.util.Properties;
import java.util.stream.Stream;

/**
 * Created by RGR on 21.05.2017.
 */
public class Config {
    @Configurable
    public String TOKEN;
    @Configurable
    public String MAIL;
    @Configurable
    public String PASSWORD;
    @Configurable
    public String AUTH_METHOD;
    @Configurable
    public String WEBLINK = "http://google.de/";


    private Config() {
    }

    public static Config getConfig(String file) {
        return getConfig(new File(file));
    }

    public static Config getConfig(File file) {
        file = file.getAbsoluteFile();
        if (file.isDirectory()) {
            SimpleLog.getLog("startup").fatal("Config file is a directory");
            System.exit(1);
        }
        Properties props = new Properties();
        try (InputStream is = new FileInputStream(file)) {
            props.load(is);
        } catch (IOException e) {
            SimpleLog.getLog("startup").fatal("Config not found. Trying to generate file. Fill in the information and restart.");
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
                if (!file.getParentFile().exists()) {
                    SimpleLog.getLog("startup").fatal(String.format("Failed to create config directory. %s", e));
                    System.exit(1);
                }
            }
            if (!file.exists()) {
                Config.writeTemplateToFile(file);
            }
            System.exit(1);
        }
        Config c = new Config();
        Class<? extends Config> clazz = c.getClass();
        ((Hashtable<String, String>) (Hashtable)props).forEach((key, val) -> {
            if (val.isEmpty()) return;
            try {
                Field f = clazz.getField(key.toUpperCase());
                if (f == null || !f.isAnnotationPresent(Configurable.class) || Modifier.isStatic(f.getModifiers()))
                    return;
                f.set(c, val);
            } catch (Exception e) {
                return;
            }
        });
        return c;
    }

    private static void writeTemplateToFile(File file) {
        try (PrintStream ps = new PrintStream(new BufferedOutputStream(new FileOutputStream(file)))) {
            ps.print(buildTemplate());
        } catch (IOException e) {
            SimpleLog.getLog("startup").fatal(String.format("Failed to write template to file. %s", e));
            System.exit(1);
        }
    }

    private static String buildTemplate() {
        StringBuilder sb = new StringBuilder();
        Stream.of(Config.class.getFields()).forEach(s -> sb.append(s.getName().toUpperCase()).append('=').append(System.lineSeparator()));
        return sb.toString();
    }
}