SnuviConfig.java 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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.ISnuviLogger;
  11. import me.hammerle.snuviscript.code.Script;
  12. import me.hammerle.snuviscript.code.SnuviUtils;
  13. public class SnuviConfig
  14. {
  15. protected final ISnuviLogger logger;
  16. protected final TreeMap<String, Object> conf;
  17. private final File file;
  18. private Script sc;
  19. private SnuviConfig(Script sc, ISnuviLogger logger, String path, String name)
  20. {
  21. this.sc = sc;
  22. this.logger = logger;
  23. StringBuilder sb = new StringBuilder("./");
  24. sb.append(path);
  25. sb.append("/");
  26. sb.append(name);
  27. sb.append(".snuvic");
  28. file = new File(sb.toString());
  29. conf = new TreeMap<>();
  30. }
  31. public SnuviConfig(ISnuviLogger logger, String path, String name)
  32. {
  33. this(null, logger, path, name);
  34. }
  35. public SnuviConfig(Script sc, String path, String name)
  36. {
  37. this(sc, sc.getScriptManager().getLogger(), path, name);
  38. }
  39. private void print(String message, Exception ex)
  40. {
  41. logger.print(message, ex, null, sc == null ? null : sc.getName(), sc, sc == null ? -1 : sc.getActiveSourceLine());
  42. }
  43. private void print(String message)
  44. {
  45. print(message, null);
  46. }
  47. public final void load()
  48. {
  49. if(!exists())
  50. {
  51. print("cannot load non existent file '" + file.getPath() + "'");
  52. return;
  53. }
  54. try
  55. {
  56. String warning = "wrong syntax in '" + file.getPath() + "'";
  57. Files.readAllLines(file.toPath()).stream().forEach(s ->
  58. {
  59. int b = s.indexOf("=");
  60. if(b == -1)
  61. {
  62. print(warning);
  63. print(s);
  64. }
  65. else
  66. {
  67. conf.put(s.substring(0, b).trim(), SnuviUtils.convert(s.substring(b + 1)));
  68. }
  69. });
  70. }
  71. catch(MalformedInputException ex)
  72. {
  73. print("'" + file.getPath() + "' contains an illegal character, change file encoding", ex);
  74. }
  75. catch(OutOfMemoryError ex)
  76. {
  77. print("'" + file.getPath() + "' is too big");
  78. }
  79. catch(SecurityException ex)
  80. {
  81. print("'" + file.getPath() + "' is not accessable", ex);
  82. }
  83. catch(IOException ex)
  84. {
  85. print("'" + file.getPath() + "' cannot be read", ex);
  86. }
  87. }
  88. public final boolean exists()
  89. {
  90. return file.exists();
  91. }
  92. public final boolean delete()
  93. {
  94. return file.delete();
  95. }
  96. public final boolean save()
  97. {
  98. try
  99. {
  100. if(file.getParentFile() != null)
  101. {
  102. file.getParentFile().mkdirs();
  103. }
  104. if(!file.exists())
  105. {
  106. try
  107. {
  108. file.createNewFile();
  109. }
  110. catch(IOException ex)
  111. {
  112. print("'" + file.getPath() + "' cannot be created", ex);
  113. return false;
  114. }
  115. }
  116. Files.write(Paths.get(file.toURI()), conf.entrySet().stream()
  117. .map(e ->
  118. {
  119. if(e.getValue().getClass() == String.class)
  120. {
  121. return e.getKey() + "=\"" + e.getValue() + "\"";
  122. }
  123. return e.getKey() + "=" + String.valueOf(e.getValue());
  124. })
  125. .collect(Collectors.toList()), StandardCharsets.UTF_8);
  126. return true;
  127. }
  128. catch(UnsupportedOperationException ex)
  129. {
  130. print("an unsupported operation was used", ex);
  131. return false;
  132. }
  133. catch(SecurityException ex)
  134. {
  135. print("'" + file.getPath() + "' is not accessable", ex);
  136. return false;
  137. }
  138. catch(IOException ex)
  139. {
  140. print("cannot write to '" + file.getPath() + "'", ex);
  141. return false;
  142. }
  143. }
  144. // -----------------------------------------------------------------------------------
  145. // getter
  146. // -----------------------------------------------------------------------------------
  147. public final <T> T get(String key, Class<T> c, T error)
  148. {
  149. try
  150. {
  151. Object o = conf.get(key);
  152. if(o == null)
  153. {
  154. return error;
  155. }
  156. return c.cast(o);
  157. }
  158. catch(ClassCastException ex)
  159. {
  160. print("invalid get", ex);
  161. return error;
  162. }
  163. }
  164. public final String getString(String key, String error)
  165. {
  166. return get(key, String.class, error);
  167. }
  168. public final String getString(String key)
  169. {
  170. return getString(key, null);
  171. }
  172. public final float getFloat(String key, float error)
  173. {
  174. return get(key, Double.class, (double) error).floatValue();
  175. }
  176. public final double getDouble(String key, double error)
  177. {
  178. return get(key, Double.class, error);
  179. }
  180. public final long getLong(String key, long error)
  181. {
  182. return get(key, Double.class, (double) error).longValue();
  183. }
  184. public final int getInt(String key, int error)
  185. {
  186. return get(key, Double.class, (double) error).intValue();
  187. }
  188. public final boolean getBoolean(String key, boolean error)
  189. {
  190. return get(key, Boolean.class, error);
  191. }
  192. // -----------------------------------------------------------------------------------
  193. // set
  194. // -----------------------------------------------------------------------------------
  195. public final void set(String key, Object o)
  196. {
  197. conf.put(key, o);
  198. }
  199. }