SnuviConfig.java 5.3 KB

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