OSDN Git Service

7f7f57e605a5b8713cd299ebb93d4c5fa945e5b9
[delesterandomselector/DelesteRandomSelector.git] / src / com / ranfa / lib / Settings.java
1 package com.ranfa.lib;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.nio.file.Files;
6 import java.nio.file.Path;
7 import java.nio.file.Paths;
8
9 import org.slf4j.Logger;
10 import org.slf4j.LoggerFactory;
11
12 import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
13 import com.fasterxml.jackson.databind.JsonNode;
14 import com.fasterxml.jackson.databind.ObjectMapper;
15 import com.fasterxml.jackson.databind.ObjectWriter;
16
17 /**
18  * 「settings.json」ファイルから各種設定を読み込みます
19  * 設定できる内容は
20  * ・起動時のバージョンチェック (checkVersion)
21  * ・起動時にライブラリの更新有無のチェック (checkLibraryUpdates)
22  * ・ウィンドウサイズ (windowWidth), (windowHeight)
23  * ・デフォルトの曲指定上限数 (songsLimit)
24  * ・スコアログの保存 (saveScoreLog)
25  * の5つ(括弧内はフィールド名)
26  * 今後、設定可能事項は増える可能性あり
27  * @author hizum
28  * @since v1.0.0
29  *
30  */
31 public class Settings {
32
33         // 設定ファイルパス
34         private final static String FILEPATH = "generated/settings.json";
35         private static Logger logger = LoggerFactory.getLogger(Settings.class);
36
37         public static boolean fileExists() {
38                 Path path = Paths.get(FILEPATH);
39                 return Files.exists(path);
40         }
41
42         public static boolean needToCheckVersion() {
43                 boolean res = true;
44                 ObjectMapper mapper = new ObjectMapper();
45                 try {
46                         JsonNode node = mapper.readTree(new File(FILEPATH));
47                         res = node.get("checkVersion").asBoolean();
48                 } catch (IOException e) {
49                         logger.error("Couldn't read setting file.", e);
50                 }
51                 return res;
52         }
53
54         public static boolean needToCheckLibraryUpdates() {
55                 boolean res = true;
56                 ObjectMapper mapper = new ObjectMapper();
57                 try {
58                         JsonNode node = mapper.readTree(new File(FILEPATH));
59                         res = node.get("checkLibraryUpdates").asBoolean();
60                 } catch (IOException e) {
61                         logger.error("Couldn't read setting file.", e);
62                 }
63                 return res;
64         }
65
66         public static int getWindowWidth() {
67                 int res = 960;
68                 ObjectMapper mapper = new ObjectMapper();
69                 try {
70                         JsonNode node = mapper.readTree(new File(FILEPATH));
71                         res = node.get("windowWidth").asInt();
72                 } catch (IOException e) {
73                         logger.error("Couldn't read setting file.", e);
74                 }
75                 return res < 1 ? 640 : res;
76         }
77
78         public static int getWindowHeight() {
79                 int res = 540;
80                 ObjectMapper mapper = new ObjectMapper();
81                 try {
82                         JsonNode node = mapper.readTree(new File(FILEPATH));
83                         res = node.get("windowHeight").asInt();
84                 } catch (IOException e) {
85                         logger.error("Couldn't read setting file.", e);
86                 }
87                 return res < 1 ? 360 : res;
88         }
89
90         public static int getSongsLimit() {
91                 int res = 3;
92                 ObjectMapper mapper = new ObjectMapper();
93                 try {
94                         JsonNode node = mapper.readTree(new File(FILEPATH));
95                         res = node.get("songLimit").asInt();
96                 } catch (IOException e) {
97                         logger.error("Couldn't read setting file.", e);
98                 }
99                 return res < 1 ? 3 : res;
100         }
101
102         public static boolean saveScoreLog() {
103                 boolean res = false;
104                 ObjectMapper mapper = new ObjectMapper();
105                 try {
106                         JsonNode node = mapper.readTree(new File(FILEPATH));
107                         res = node.get("saveScoreLog").asBoolean();
108                 } catch (IOException e) {
109                         logger.error("Couldn't read setting file.", e);
110                 }
111                 return res;
112         }
113
114         public static boolean writeDownJSON() {
115                 boolean res = true;
116                 SettingJSONProperty property = new SettingJSONProperty();
117                 property.setCheckVersion(true);
118                 property.setCheckLibraryUpdates(true);
119                 property.setWindowWidth(640);
120                 property.setWindowHeight(360);
121                 property.setSongLimit(3);
122                 property.setSaveScoreLog(false);
123                 ObjectWriter writer = new ObjectMapper().writer(new DefaultPrettyPrinter());
124                 try {
125                         writer.writeValue(Paths.get(FILEPATH).toFile(), property);
126                 } catch (IOException e) {
127                         logger.error("Couldn't write down setting file.", e);
128                         res = false;
129                 }
130                 return res;
131         }
132 }