SnuviConfig.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package me.hammerle.snuviscript.config;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.nio.charset.MalformedInputException;
  5. import java.nio.charset.StandardCharsets;
  6. import java.nio.file.Files;
  7. import java.nio.file.Paths;
  8. import java.util.TreeMap;
  9. import java.util.stream.Collectors;
  10. import me.hammerle.snuviscript.code.Script;
  11. import me.hammerle.snuviscript.code.ScriptManager;
  12. import me.hammerle.snuviscript.code.SnuviUtils;
  13. import me.hammerle.snuviscript.exceptions.StackTrace;
  14. public class SnuviConfig {
  15. private final TreeMap<String, Object> conf = new TreeMap<>();
  16. private final File file;
  17. private boolean dirty = false;
  18. public SnuviConfig(String path, String name) {
  19. StringBuilder sb = new StringBuilder("./");
  20. sb.append(path);
  21. sb.append("/");
  22. sb.append(name);
  23. sb.append(".snuvic");
  24. file = new File(sb.toString());
  25. }
  26. private void print(Script sc, String message, Exception ex) {
  27. if(sc == null) {
  28. System.out.println(message);
  29. ex.printStackTrace();
  30. return;
  31. }
  32. ScriptManager sm = sc.getScriptManager();
  33. final StackTrace trace = sc.getStackTrace();
  34. sm.getScheduler().scheduleTask(() -> {
  35. sm.getLogger().print(message, ex, null, sc.getName(), sc, trace);
  36. });
  37. }
  38. private void print(Script sc, String message) {
  39. print(sc, message, null);
  40. }
  41. public final synchronized void load(Script sc) {
  42. if(!exists()) {
  43. print(sc, "cannot load non existent file '" + file.getPath() + "'");
  44. return;
  45. }
  46. try {
  47. String warning = "wrong syntax in '" + file.getPath() + "'";
  48. Files.readAllLines(file.toPath()).stream().forEach(s -> {
  49. int b = s.indexOf("=");
  50. if(b == -1) {
  51. print(sc, warning);
  52. print(sc, s);
  53. } else {
  54. conf.put(s.substring(0, b).trim(), SnuviUtils.convert(s.substring(b + 1)));
  55. }
  56. });
  57. } catch(MalformedInputException ex) {
  58. print(sc,
  59. "'" + file.getPath() + "' contains an illegal character, change file encoding",
  60. ex);
  61. } catch(OutOfMemoryError ex) {
  62. print(sc, "'" + file.getPath() + "' is too big");
  63. } catch(SecurityException ex) {
  64. print(sc, "'" + file.getPath() + "' is not accessable", ex);
  65. } catch(IOException ex) {
  66. print(sc, "'" + file.getPath() + "' cannot be read", ex);
  67. }
  68. }
  69. public final boolean exists() {
  70. return file.exists();
  71. }
  72. public final synchronized boolean delete() {
  73. return file.delete();
  74. }
  75. public final synchronized boolean save(Script sc) {
  76. if(conf.isEmpty() || !dirty) {
  77. return false;
  78. }
  79. dirty = false;
  80. try {
  81. if(file.getParentFile() != null) {
  82. file.getParentFile().mkdirs();
  83. }
  84. if(!file.exists()) {
  85. try {
  86. file.createNewFile();
  87. } catch(IOException ex) {
  88. print(sc, "'" + file.getPath() + "' cannot be created", ex);
  89. return false;
  90. }
  91. }
  92. Files.write(Paths.get(file.toURI()), conf.entrySet().stream().map(e -> {
  93. if(e.getValue().getClass() == String.class) {
  94. return String.format("%s=\"%s\"", e.getKey(),
  95. e.getValue().toString().replaceAll("\n", "\\n"));
  96. }
  97. return String.format("%s=%s", e.getKey(), e.getValue());
  98. }).collect(Collectors.toList()), StandardCharsets.UTF_8);
  99. return true;
  100. } catch(UnsupportedOperationException ex) {
  101. print(sc, "an unsupported operation was used", ex);
  102. return false;
  103. } catch(SecurityException ex) {
  104. print(sc, "'" + file.getPath() + "' is not accessable", ex);
  105. return false;
  106. } catch(IOException ex) {
  107. print(sc, "cannot write to '" + file.getPath() + "'", ex);
  108. return false;
  109. }
  110. }
  111. public final synchronized <T> T get(Script sc, String key, Class<T> c, T error) {
  112. try {
  113. Object o = conf.get(key);
  114. if(o == null) {
  115. return error;
  116. }
  117. return c.cast(o);
  118. } catch(ClassCastException ex) {
  119. print(sc, "invalid get", ex);
  120. return error;
  121. }
  122. }
  123. public final String getString(Script sc, String key, String error) {
  124. return get(sc, key, String.class, error);
  125. }
  126. public final String getString(Script sc, String key) {
  127. return getString(sc, key, null);
  128. }
  129. public final float getFloat(Script sc, String key, float error) {
  130. return get(sc, key, Double.class, (double) error).floatValue();
  131. }
  132. public final double getDouble(Script sc, String key, double error) {
  133. return get(sc, key, Double.class, error);
  134. }
  135. public final long getLong(Script sc, String key, long error) {
  136. return get(sc, key, Double.class, (double) error).longValue();
  137. }
  138. public final int getInt(Script sc, String key, int error) {
  139. return get(sc, key, Double.class, (double) error).intValue();
  140. }
  141. public final boolean getBoolean(Script sc, String key, boolean error) {
  142. return get(sc, key, Boolean.class, error);
  143. }
  144. public final synchronized void set(String key, Object o) {
  145. dirty = true;
  146. conf.put(key, o);
  147. }
  148. }