OSDN Git Service

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