2 // Getdown - application installer, patcher and launcher
3 // Copyright (C) 2004-2018 Getdown authors
4 // https://github.com/threerings/getdown/blob/master/LICENSE
6 package com.threerings.getdown.util;
8 import static java.nio.charset.StandardCharsets.UTF_8;
10 import java.io.BufferedReader;
12 import java.io.FileInputStream;
13 import java.io.IOException;
14 import java.io.InputStream;
15 import java.io.InputStreamReader;
16 import java.io.Reader;
17 import java.net.MalformedURLException;
19 import java.nio.charset.StandardCharsets;
20 import java.util.ArrayList;
21 import java.util.Arrays;
22 import java.util.HashMap;
23 import java.util.List;
24 import java.util.Locale;
27 import static com.threerings.getdown.Log.log;
30 * Handles parsing and runtime access for Getdown's config files (mainly {@code getdown.txt}).
31 * These files contain zero or more mappings for a particular string key. Config values can be
32 * fetched as single strings, lists of strings, or parsed into primitives or compound data types
33 * like colors and rectangles.
37 /** Empty configuration. */
38 public static final Config EMPTY = new Config(new HashMap<String, Object>());
40 /** Options that control the {@link #parsePairs} function. */
41 public static class ParseOpts {
42 // these should be tweaked as desired by the caller
43 public boolean biasToKey = false;
44 public boolean strictComments = false;
46 // these are filled in by parseConfig
47 public String osname = null;
48 public String osarch = null;
52 * Creates a parse configuration, filling in the platform filters (or not) depending on the
53 * value of {@code checkPlatform}.
55 public static ParseOpts createOpts (boolean checkPlatform) {
56 ParseOpts opts = new ParseOpts();
58 opts.osname = StringUtil.deNull(System.getProperty("os.name")).toLowerCase(Locale.ROOT);
59 opts.osarch = StringUtil.deNull(System.getProperty("os.arch")).toLowerCase(Locale.ROOT);
65 * Parses a configuration file containing key/value pairs. The file must be in the UTF-8
68 * @param opts options that influence the parsing. See {@link #createOpts}.
70 * @return a list of <code>String[]</code> instances containing the key/value pairs in the
71 * order they were parsed from the file.
73 public static List<String[]> parsePairs (File source, ParseOpts opts)
76 // annoyingly FileReader does not allow encoding to be specified (uses platform default)
77 try (FileInputStream fis = new FileInputStream(source);
78 InputStreamReader input = new InputStreamReader(fis, StandardCharsets.UTF_8)) {
79 return parsePairs(input, opts);
84 * See {@link #parsePairs(File,ParseOpts)}.
86 public static List<String[]> parsePairs (Reader source, ParseOpts opts) throws IOException
88 List<String[]> pairs = new ArrayList<>();
89 for (String line : FileUtil.readLines(source)) {
91 int cidx = line.indexOf("#");
92 if (opts.strictComments ? cidx == 0 : cidx != -1) {
93 line = line.substring(0, cidx);
96 // trim whitespace and skip blank lines
98 if (StringUtil.isBlank(line)) {
102 // parse our key/value pair
103 String[] pair = new String[2];
104 // if we're biasing toward key, put all the extra = in the key rather than the value
105 int eidx = opts.biasToKey ? line.lastIndexOf("=") : line.indexOf("=");
107 pair[0] = line.substring(0, eidx).trim();
108 pair[1] = line.substring(eidx+1).trim();
114 // if the pair has an os qualifier, we need to process it
115 if (pair[1].startsWith("[")) {
116 int qidx = pair[1].indexOf("]");
118 log.warning("Bogus platform specifier", "key", pair[0], "value", pair[1]);
119 continue; // omit the pair entirely
121 // if we're checking qualifiers and the os doesn't match this qualifier, skip it
122 String quals = pair[1].substring(1, qidx);
123 if (opts.osname != null && !checkQualifiers(quals, opts.osname, opts.osarch)) {
124 log.debug("Skipping", "quals", quals,
125 "osname", opts.osname, "osarch", opts.osarch,
126 "key", pair[0], "value", pair[1]);
129 // otherwise filter out the qualifier text
130 pair[1] = pair[1].substring(qidx+1).trim();
140 * Takes a comma-separated String of four integers and returns a rectangle using those ints as
141 * the its x, y, width, and height.
143 public static Rectangle parseRect (String name, String value)
145 if (!StringUtil.isBlank(value)) {
146 int[] v = StringUtil.parseIntArray(value);
147 if (v != null && v.length == 4) {
148 return new Rectangle(v[0], v[1], v[2], v[3]);
150 log.warning("Ignoring invalid rect '" + name + "' config '" + value + "'.");
156 * Parses the given hex color value (e.g. FFCC99) and returns an {@code Integer} with that
157 * value. If the given value is null or not a valid hexadecimal number, this will return null.
159 public static Integer parseColor (String hexValue)
161 if (!StringUtil.isBlank(hexValue)) {
163 // if no alpha channel is specified, use 255 (full alpha)
164 int alpha = hexValue.length() > 6 ? 0 : 0xFF000000;
165 return Integer.parseInt(hexValue, 16) | alpha;
166 } catch (NumberFormatException e) {
167 log.warning("Ignoring invalid color", "hexValue", hexValue, "exception", e);
174 * Parses a configuration file containing key/value pairs. The file must be in the UTF-8
177 * @return a map from keys to values, where a value will be an array of strings if more than
178 * one key/value pair in the config file was associated with the same key.
180 public static Config parseConfig (File source, ParseOpts opts)
183 Map<String, Object> data = new HashMap<>();
185 // I thought that we could use HashMap<String, String[]> and put new String[] {pair[1]} for
186 // the null case, but it mysteriously dies on launch, so leaving it as HashMap<String,
188 for (String[] pair : parsePairs(source, opts)) {
189 Object value = data.get(pair[0]);
191 data.put(pair[0], pair[1]);
192 } else if (value instanceof String) {
193 data.put(pair[0], new String[] { (String)value, pair[1] });
194 } else if (value instanceof String[]) {
195 String[] values = (String[])value;
196 String[] nvalues = new String[values.length+1];
197 System.arraycopy(values, 0, nvalues, 0, values.length);
198 nvalues[values.length] = pair[1];
199 data.put(pair[0], nvalues);
203 // special magic for the getdown.txt config: if the parsed data contains 'strict_comments =
204 // true' then we reparse the file with strict comments (i.e. # is only assumed to start a
205 // comment in column 0)
206 if (!opts.strictComments && Boolean.parseBoolean((String)data.get("strict_comments"))) {
207 opts.strictComments = true;
208 return parseConfig(source, opts);
211 return new Config(data);
214 public static Config parseConfig (Reader source, ParseOpts opts)
217 Map<String, Object> data = new HashMap<>();
219 // I thought that we could use HashMap<String, String[]> and put new String[] {pair[1]} for
220 // the null case, but it mysteriously dies on launch, so leaving it as HashMap<String,
222 for (String[] pair : parsePairs(source, opts)) {
223 Object value = data.get(pair[0]);
225 data.put(pair[0], pair[1]);
226 } else if (value instanceof String) {
227 data.put(pair[0], new String[] { (String)value, pair[1] });
228 } else if (value instanceof String[]) {
229 String[] values = (String[])value;
230 String[] nvalues = new String[values.length+1];
231 System.arraycopy(values, 0, nvalues, 0, values.length);
232 nvalues[values.length] = pair[1];
233 data.put(pair[0], nvalues);
237 // special magic for the getdown.txt config: if the parsed data contains 'strict_comments =
238 // true' then we reparse the file with strict comments (i.e. # is only assumed to start a
239 // comment in column 0)
240 if (!opts.strictComments && Boolean.parseBoolean((String)data.get("strict_comments"))) {
241 opts.strictComments = true;
243 return parseConfig(source, opts);
246 return new Config(data);
249 public Config (Map<String, Object> data) {
254 * Returns whether {@code name} has a value in this config.
256 public boolean hasValue (String name) {
257 return _data.containsKey(name);
261 * Returns the raw-value for {@code name}. This may be a {@code String}, {@code String[]}, or
264 public Object getRaw (String name) {
265 return _data.get(name);
269 * Returns the specified config value as a string, or {@code null}.
271 public String getString (String name) {
272 return (String)_data.get(name);
276 * Returns the specified config value as a string, or {@code def}.
278 public String getString (String name, String def) {
279 String value = (String)_data.get(name);
280 return value == null ? def : value;
284 * Returns the specified config value as a boolean.
286 public boolean getBoolean (String name) {
287 return Boolean.parseBoolean(getString(name));
291 * Massages a single string into an array and leaves existing array values as is. Simplifies
292 * access to parameters that are expected to be arrays.
294 public String[] getMultiValue (String name)
296 Object value = _data.get(name);
298 return new String[] {};
300 if (value instanceof String) {
301 return new String[] { (String)value };
303 return (String[])value;
307 /** Used to parse rectangle specifications from the config file. */
308 public Rectangle getRect (String name, Rectangle def)
310 String value = getString(name);
311 Rectangle rect = parseRect(name, value);
312 return (rect == null) ? def : rect;
316 * Parses and returns the config value for {@code name} as an int. If no value is provided,
317 * {@code def} is returned. If the value is invalid, a warning is logged and {@code def} is
320 public int getInt (String name, int def) {
321 String value = getString(name);
323 return value == null ? def : Integer.parseInt(value);
324 } catch (Exception e) {
325 log.warning("Ignoring invalid int '" + name + "' config '" + value + "',");
331 * Parses and returns the config value for {@code name} as a long. If no value is provided,
332 * {@code def} is returned. If the value is invalid, a warning is logged and {@code def} is
335 public long getLong (String name, long def) {
336 String value = getString(name);
338 return value == null ? def : Long.parseLong(value);
339 } catch (Exception e) {
340 log.warning("Ignoring invalid long '" + name + "' config '" + value + "',");
345 /** Used to parse color specifications from the config file. */
346 public int getColor (String name, int def)
348 String value = getString(name);
349 Integer color = parseColor(value);
350 return (color == null) ? def : color;
353 /** Parses a list of strings from the config file. */
354 public String[] getList (String name)
356 String value = getString(name);
357 return (value == null) ? new String[0] : StringUtil.parseStringArray(value);
361 * Parses a URL from the config file, checking first for a localized version.
363 public String getUrl (String name, String def)
365 String value = getString(name + "." + Locale.getDefault().getLanguage());
366 if (StringUtil.isBlank(value)) {
367 value = getString(name);
369 if (StringUtil.isBlank(value)) {
372 if (!StringUtil.isBlank(value)) {
374 HostWhitelist.verify(new URL(value));
375 } catch (MalformedURLException e) {
376 log.warning("Invalid URL.", "url", value, e);
384 * A helper function for {@link #parsePairs(Reader,ParseOpts)}. Qualifiers have the following
391 * Examples: [linux-amd64,linux-x86_64], [windows], [mac os x], [!windows]. Negative qualifiers
392 * must appear alone, they cannot be used with other qualifiers (positive or negative).
394 protected static boolean checkQualifiers (String quals, String osname, String osarch)
396 if (quals.startsWith("!")) {
397 if (quals.indexOf(",") != -1) { // sanity check
398 log.warning("Multiple qualifiers cannot be used when one of the qualifiers " +
399 "is negative", "quals", quals);
402 return !checkQualifier(quals.substring(1), osname, osarch);
404 for (String qual : quals.split(",")) {
405 if (checkQualifier(qual, osname, osarch)) {
406 return true; // if we have a positive match, we can immediately return true
409 return false; // we had no positive matches, so return false
412 /** A helper function for {@link #checkQualifiers}. */
413 protected static boolean checkQualifier (String qual, String osname, String osarch)
415 String[] bits = qual.trim().toLowerCase(Locale.ROOT).split("-");
416 String os = bits[0], arch = (bits.length > 1) ? bits[1] : "";
417 return (osname.indexOf(os) != -1) && (osarch.indexOf(arch) != -1);
420 public void mergeConfig(Config newValues, boolean merge) {
422 for (Map.Entry<String, Object> entry : newValues.getData().entrySet()) {
424 String key = entry.getKey();
425 Object nvalue = entry.getValue();
427 String mkey = key.indexOf('.') > -1 ? key.substring(key.indexOf('.') + 1) : key;
428 if (merge && allowedMergeKeys.contains(mkey)) {
430 // merge multi values
432 Object value = _data.get(key);
435 _data.put(key, nvalue);
436 } else if (value instanceof String) {
437 if (nvalue instanceof String) {
439 // value is String, nvalue is String
440 _data.put(key, new String[] { (String)value, (String)nvalue });
442 } else if (nvalue instanceof String[]) {
444 // value is String, nvalue is String[]
445 String[] nvalues = (String[])nvalue;
446 String[] newvalues = new String[nvalues.length+1];
447 newvalues[0] = (String)value;
448 System.arraycopy(nvalues, 0, newvalues, 1, nvalues.length);
449 _data.put(key, newvalues);
452 } else if (value instanceof String[]) {
453 if (nvalue instanceof String) {
455 // value is String[], nvalue is String
456 String[] values = (String[])value;
457 String[] newvalues = new String[values.length+1];
458 System.arraycopy(values, 0, newvalues, 0, values.length);
459 newvalues[values.length] = (String)nvalue;
460 _data.put(key, newvalues);
462 } else if (nvalue instanceof String[]) {
464 // value is String[], nvalue is String[]
465 String[] values = (String[])value;
466 String[] nvalues = (String[])nvalue;
467 String[] newvalues = new String[values.length + nvalues.length];
468 System.arraycopy(values, 0, newvalues, 0, values.length);
469 System.arraycopy(nvalues, 0, newvalues, values.length, newvalues.length);
470 _data.put(key, newvalues);
475 } else if (allowedReplaceKeys.contains(mkey)){
478 _data.put(key, nvalue);
481 log.warning("Not merging key '"+key+"' into config");
488 public String toString() {
489 StringBuilder sb = new StringBuilder();
490 for (Map.Entry<String, Object> entry : getData().entrySet()) {
491 String key = entry.getKey();
492 Object val = entry.getValue();
495 if (val instanceof String) {
496 sb.append((String)val);
497 } else if (val instanceof String[]) {
498 sb.append(Arrays.toString((String[])val));
500 sb.append("Value not String or String[]");
504 return sb.toString();
507 public Map<String, Object> getData() {
511 private final Map<String, Object> _data;
513 public static final List<String> allowedReplaceKeys = Arrays.asList("appbase","apparg","jvmarg","jvmmempc","jvmmemmax"); // these are the ones we might use
514 public static final List<String> allowedMergeKeys = Arrays.asList("apparg","jvmarg"); // these are the ones we might use
515 //private final List<String> allowedMergeKeys = Arrays.asList("apparg","jvmarg","resource","code","java_location"); // (not exhaustive list here)