SnuviConfig.java 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 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, "'" + file.getPath() + "' contains an illegal character, change file encoding", ex);
  59. } catch(OutOfMemoryError ex) {
  60. print(sc, "'" + file.getPath() + "' is too big");
  61. } catch(SecurityException ex) {
  62. print(sc, "'" + file.getPath() + "' is not accessable", ex);
  63. } catch(IOException ex) {
  64. print(sc, "'" + file.getPath() + "' cannot be read", ex);
  65. }
  66. }
  67. public final boolean exists() {
  68. return file.exists();
  69. }
  70. public final boolean delete() {
  71. return file.delete();
  72. }
  73. public final boolean save(Script sc) {
  74. if(conf.isEmpty() || !dirty) {
  75. return false;
  76. }
  77. dirty = false;
  78. try {
  79. if(file.getParentFile() != null) {
  80. file.getParentFile().mkdirs();
  81. }
  82. if(!file.exists()) {
  83. try {
  84. file.createNewFile();
  85. } catch(IOException ex) {
  86. print(sc, "'" + file.getPath() + "' cannot be created", ex);
  87. return false;
  88. }
  89. }
  90. Files.write(Paths.get(file.toURI()), conf.entrySet().stream()
  91. .map(e -> {
  92. if(e.getValue().getClass() == String.class) {
  93. return e.getKey() + "=\"" + e.getValue() + "\"";
  94. }
  95. return e.getKey() + "=" + String.valueOf(e.getValue());
  96. })
  97. .collect(Collectors.toList()), StandardCharsets.UTF_8);
  98. return true;
  99. } catch(UnsupportedOperationException ex) {
  100. print(sc, "an unsupported operation was used", ex);
  101. return false;
  102. } catch(SecurityException ex) {
  103. print(sc, "'" + file.getPath() + "' is not accessable", ex);
  104. return false;
  105. } catch(IOException ex) {
  106. print(sc, "cannot write to '" + file.getPath() + "'", ex);
  107. return false;
  108. }
  109. }
  110. public final <T> T get(Script sc, String key, Class<T> c, T error) {
  111. try {
  112. Object o = conf.get(key);
  113. if(o == null) {
  114. return error;
  115. }
  116. return c.cast(o);
  117. } catch(ClassCastException ex) {
  118. print(sc, "invalid get", ex);
  119. return error;
  120. }
  121. }
  122. public final String getString(Script sc, String key, String error) {
  123. return get(sc, key, String.class, error);
  124. }
  125. public final String getString(Script sc, String key) {
  126. return getString(sc, key, null);
  127. }
  128. public final float getFloat(Script sc, String key, float error) {
  129. return get(sc, key, Double.class, (double) error).floatValue();
  130. }
  131. public final double getDouble(Script sc, String key, double error) {
  132. return get(sc, key, Double.class, error);
  133. }
  134. public final long getLong(Script sc, String key, long error) {
  135. return get(sc, key, Double.class, (double) error).longValue();
  136. }
  137. public final int getInt(Script sc, String key, int error) {
  138. return get(sc, key, Double.class, (double) error).intValue();
  139. }
  140. public final boolean getBoolean(Script sc, String key, boolean error) {
  141. return get(sc, key, Boolean.class, error);
  142. }
  143. public final void set(String key, Object o) {
  144. dirty = true;
  145. conf.put(key, o);
  146. }
  147. }