23 package com.realtime.crossfire.jxclient.settings;
27 import java.io.BufferedReader;
28 import java.io.BufferedWriter;
29 import java.io.IOException;
30 import java.io.LineNumberReader;
31 import java.nio.charset.StandardCharsets;
32 import java.nio.file.Files;
33 import java.nio.file.NoSuchFileException;
34 import java.nio.file.Path;
35 import java.nio.file.StandardCopyOption;
37 import java.util.TreeMap;
38 import org.jetbrains.annotations.NotNull;
39 import org.jetbrains.annotations.Nullable;
57 private final Map<String, Entry>
values =
new TreeMap<>();
83 if (entry ==
null && (key.getKey().equals(
"sound_music_enabled") || key.getKey().equals(
"sound_effects_enabled"))) {
84 entry =
values.get(
"sound_enabled");
86 return entry ==
null ? key.getDefaultValue().toString() : entry.
getValue();
98 return Boolean.parseBoolean(value);
99 }
catch (
final NumberFormatException ignored) {
100 return key.getDefaultValue();
131 if (oldEntry ==
null) {
132 values.put(key.getKey(),
new Entry(value, key.getComment()));
135 final boolean documentationChanged = oldEntry.
setDocumentation(key.getComment());
136 final boolean valueChanged = !oldEntry.
getValue().equals(value);
140 if (documentationChanged || valueChanged) {
177 public void remove(@NotNull
final String key) {
178 if (
values.remove(key) !=
null) {
194 }
catch (
final IOException ex) {
195 System.err.println(
file+
": "+ex.getMessage());
206 try (BufferedReader reader = Files.newBufferedReader(
file, StandardCharsets.UTF_8)) {
207 try (LineNumberReader lnr =
new LineNumberReader(reader)) {
211 }
catch (
final NoSuchFileException ignored) {
213 }
catch (
final IOException ex) {
214 System.err.println(
file+
": "+ex.getMessage());
223 private void loadValues(@NotNull
final LineNumberReader lnr)
throws IOException {
224 @Nullable String comment =
null;
226 final String line2 = lnr.readLine();
231 if (line.isEmpty()) {
235 if (line.startsWith(
"#")) {
236 comment = line.substring(1).trim();
240 final String[] tmp = line.split(
"=", 2);
241 if (tmp.length != 2) {
242 System.err.println(
file+
":"+lnr.getLineNumber()+
": syntax error");
245 final String key = tmp[0];
246 final String value = tmp[1];
258 final Path tmpFile =
file.resolveSibling(
file.getFileName()+
".tmp");
259 try (BufferedWriter bw = Files.newBufferedWriter(tmpFile, StandardCharsets.UTF_8)) {
263 Files.move(tmpFile,
file, StandardCopyOption.REPLACE_EXISTING);
272 private static void saveNode(@NotNull
final BufferedWriter writer, @NotNull
final Map<String, Entry> node)
throws IOException {
273 if (node.isEmpty()) {
277 for (Map.Entry<String,
Entry> entry : node.entrySet()) {
283 if (documentation !=
null) {