-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathConfig.java
145 lines (132 loc) · 4.33 KB
/
Config.java
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package spaceinvaders;
import static java.util.logging.Level.SEVERE;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.Paths;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
/**
* Main configuraion manager.
*
* <p>Contains application global configuration. Is also used to retrive other configuration files.
*/
public class Config {
private static final transient Logger LOGGER = Logger.getLogger(Config.class.getName());
private static final transient String CONFIG_PATH = "/config/";
/**
* If set to {@code true}, resources are taken from inside the jar archive, otherwise, they are
* taken as regular files.
*/
private static final transient Boolean JAR_FILE = true;
private static transient Config singleton;
/**
* Game is played in LAN.
*
* <p>If set to {@code false}, only the TCP protocol is used for communication. Otherwise, UDP is
* used as well, thus increasing the speed.
*/
private Boolean lanGame;
/**
* Get the single instance of this class.
*
* <p>If an instance does not exist, one is created automatically.
*/
public static Config getInstance() {
if (singleton == null) {
try {
Config cfg = new Config();
singleton = cfg.getJsonResource(CONFIG_PATH + "app.json",cfg.getClass());
} catch (IOException ioException) {
LOGGER.log(SEVERE,ioException.toString(),ioException);
}
}
return singleton;
}
/**
* @return true if the game is played in LAN, false otherwise.
*/
public boolean isLanGame() {
return lanGame;
}
public String getGameConfigFile() {
return CONFIG_PATH + "game.json";
}
public String getResourcesConfigFile() {
return CONFIG_PATH + "resources.json";
}
public String getClientConfigFile() {
return CONFIG_PATH + "client.json";
}
/**
* Reads a json resource file and returns the object.
*
* @param path path to the resource file.
* @param classOfT the type of object to be converted from json.
*
* @return an object of type T, converted from json.
*
* @throws IOException if an I/O exception occurs while reading the file.
* @throws FileNotFoundException if the file cannot be found.
* @throws OutOfMemoryError if the file is too large.
* @throws JsonSyntaxException if the json is not a valid representation of {@code classOfT}.
* @throws InvalidPathException if the path to the file is invalid.
* @throws SecurityException if the file cannot be accessed.
* @throws NullPointerException if an argument is {@code null}.
*/
public <T> T getJsonResource(String path, Class<T> classOfT)
throws FileNotFoundException, IOException {
if (path == null || classOfT == null) {
throw new NullPointerException();
}
String json = null;
if (JAR_FILE) {
InputStream input = getClass().getResourceAsStream(path);
if (input == null) {
throw new FileNotFoundException();
}
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
final StringBuffer buf = new StringBuffer();
int piece = -1;
while ((piece = reader.read()) != -1) {
buf.append((char) piece);
}
reader.close();
json = buf.toString();
} else {
json = new String(Files.readAllBytes(Paths.get(path)));
}
final Gson gson = new Gson();
T obj = gson.fromJson(json,classOfT);
return obj;
}
/**
* Get an image resource.
*
* @param path path to the resource.
*
* @return the image object.
*
* @throws IOException if an error occurs during reading.
* @throws FileNotFoundException if the image cannot be found.
* @throws NullPointerException if argument is {@code null}.
*/
public BufferedImage getImageResource(String path) throws FileNotFoundException, IOException {
if (JAR_FILE) {
InputStream input = getClass().getResourceAsStream(path);
if (input == null) {
throw new FileNotFoundException();
}
return ImageIO.read(input);
}
return ImageIO.read(new File(path));
}
}