Skip to content

Commit e8ebf6a

Browse files
committed
增加对旧版配置文件的读取支持
1 parent 8d668a9 commit e8ebf6a

File tree

1 file changed

+35
-31
lines changed
  • src/main/java/com/github/balloonupdate/mcpatch/client/config

1 file changed

+35
-31
lines changed

src/main/java/com/github/balloonupdate/mcpatch/client/config/AppConfig.java

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.github.balloonupdate.mcpatch.client.config;
22

3+
import java.util.ArrayList;
34
import java.util.HashMap;
45
import java.util.List;
56
import java.util.Map;
@@ -107,21 +108,24 @@ public class AppConfig {
107108

108109

109110
public AppConfig(Map<String, Object> map) {
110-
List<String> urls = getList(map, "urls");
111-
String versionFilePath = getString(map, "version-file-path");
112-
boolean allowError = getBoolean(map, "allow-error");
113-
boolean showFinishMessage = getBoolean(map, "show-finish-message");
114-
int autoCloseChangelogs = getInt(map, "auto-close-changelogs");
115-
boolean silentMode = getBoolean(map, "silent-mode");
116-
boolean disableTheme = getBoolean(map, "disable-theme");
117-
String windowTitle = getString(map, "window-title");
118-
String basePath = getString(map, "base-path");
119-
int privateTimeout = getInt(map, "private-timeout");
120-
Map<String, String> httpHeaders = getMap(map, "http-headers");
121-
int httpTimeout = getInt(map, "http-timeout");
122-
int reties = getInt(map, "retries");
123-
boolean ignoreSSLCertificate = getBoolean(map, "ignore-ssl-cert");
124-
boolean testMode = getBoolean(map, "test-mode");
111+
List<String> urls = getList(map, "urls", null, new ArrayList<>());
112+
String versionFilePath = getString(map, "version-file-path", null, "version-label.txt");
113+
boolean allowError = getBoolean(map, "allow-error", null, false);
114+
boolean showFinishMessage = getBoolean(map, "show-finish-message", null, true);
115+
int autoCloseChangelogs = getInt(map, "auto-close-changelogs", null, 0);
116+
boolean silentMode = getBoolean(map, "silent-mode", null, false);
117+
boolean disableTheme = getBoolean(map, "disable-theme", null, false);
118+
String windowTitle = getString(map, "window-title", null, "Mcpatch");
119+
String basePath = getString(map, "base-path", null, "");
120+
int privateTimeout = getInt(map, "private-timeout", null, 7000);
121+
Map<String, String> httpHeaders = getMap(map, "http-headers", null, new HashMap<>());
122+
int httpTimeout = getInt(map, "http-timeout", null, 7000);
123+
int reties = getInt(map, "retries", "http-retries", 3);
124+
boolean ignoreSSLCertificate = getBoolean(map, "ignore-ssl-cert", "http-ignore-certificate", false);
125+
boolean testMode = getBoolean(map, "test-mode", null, false);
126+
127+
// if (urls.contains("webda"))
128+
//
125129

126130
this.urls = urls;
127131
this.versionFilePath = versionFilePath;
@@ -141,15 +145,15 @@ public AppConfig(Map<String, Object> map) {
141145
}
142146

143147
@SuppressWarnings("unchecked")
144-
static <T> T getOption(Map<String, Object> map, String key, boolean allowNull, Class<T> clazz) {
148+
static <T> T getOption(Map<String, Object> map, String key, String alterKey, T defaultValue, Class<T> clazz) {
145149
Object value = map.get(key);
146150

147151
if (value == null) {
148-
if (allowNull) {
149-
return null;
150-
} else {
151-
throw new RuntimeException("配置文件中找不到 " + key + " 配置项");
152-
}
152+
value = map.get(alterKey);
153+
}
154+
155+
if (value == null) {
156+
return defaultValue;
153157
}
154158

155159
if (!clazz.isInstance(value)) {
@@ -159,24 +163,24 @@ static <T> T getOption(Map<String, Object> map, String key, boolean allowNull, C
159163
return clazz.isInstance(value) ? (T) value : null;
160164
}
161165

162-
static String getString(Map<String, Object> map, String key) {
163-
return getOption(map, key, false, String.class);
166+
static String getString(Map<String, Object> map, String key, String formerKey, String defaultValue) {
167+
return getOption(map, key, formerKey, defaultValue, String.class);
164168
}
165169

166-
static boolean getBoolean(Map<String, Object> map, String key) {
167-
return getOption(map, key, false, Boolean.class);
170+
static boolean getBoolean(Map<String, Object> map, String key, String formerKey, boolean defaultValue) {
171+
return getOption(map, key, formerKey, defaultValue, Boolean.class);
168172
}
169173

170-
static int getInt(Map<String, Object> map, String key) {
171-
return getOption(map, key, false, Integer.class);
174+
static int getInt(Map<String, Object> map, String key, String formerKey, int defaultValue) {
175+
return getOption(map, key, formerKey, defaultValue, Integer.class);
172176
}
173177

174-
static List<String> getList(Map<String, Object> map, String key) {
175-
return getOption(map, key, false, List.class);
178+
static List<String> getList(Map<String, Object> map, String key, String formerKey, List<String> defaultValue) {
179+
return getOption(map, key, formerKey, defaultValue, List.class);
176180
}
177181

178-
static Map<String, String> getMap(Map<String, Object> map, String key) {
179-
Map<String, String> result = getOption(map, key, true, Map.class);
182+
static Map<String, String> getMap(Map<String, Object> map, String key, String formerKey, Map<String, String> defaultValue) {
183+
Map<String, String> result = getOption(map, key, formerKey, defaultValue, Map.class);
180184

181185
return result != null ? result : new HashMap<>();
182186
}

0 commit comments

Comments
 (0)