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