Kajetan Johannes Hammerle преди 7 години
родител
ревизия
8f03408d3c

+ 6 - 1
src/me/hammerle/code/Code.java

@@ -918,7 +918,7 @@ public class Code implements Comparable<Code>
             }
             return s.substring(1, s.length() - 1);
         }
-        else if(s.startsWith("#"))
+        else if(variable && s.startsWith("#"))
         {
             return convertInput(strings.get(s), variable);
         }
@@ -947,4 +947,9 @@ public class Code implements Comparable<Code>
             return s;
         }
     }
+    
+    public static Object convertInput(String s)
+    {
+        return convertInput(s, false);
+    }
 }

+ 3 - 0
src/me/hammerle/code/ISnuviLogger.java

@@ -6,4 +6,7 @@ public interface ISnuviLogger
 {
     public void printException(SnuviException ex);
     public void printException(SnuviException ex, Script s);
+    
+    public void printWarning(String s);
+    public void printInfo(String s);
 }

+ 0 - 4
src/me/hammerle/code/Script.java

@@ -50,10 +50,6 @@ public class Script
         returnStack = new Stack<>();
         
         this.code = Code.generate(parser, name, code, gotos);
-        /*for(Code c : this.code)
-        {
-            System.out.println(c);
-        }*/
         
         position = 0;
 

+ 203 - 0
src/me/hammerle/config/SnuviConfig.java

@@ -0,0 +1,203 @@
+package me.hammerle.config;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.charset.Charset;
+import java.nio.charset.MalformedInputException;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.util.TreeMap;
+import java.util.stream.Collectors;
+import me.hammerle.code.Code;
+import me.hammerle.code.ISnuviLogger;
+
+public class SnuviConfig
+{            
+    protected final ISnuviLogger logger;
+    protected final TreeMap<String, Object> conf;
+    private final File file;
+    
+    public SnuviConfig(ISnuviLogger logger, String path, String name, String ending)
+    {    
+        this.logger = logger;
+        StringBuilder sb = new StringBuilder("./");
+        sb.append(path);
+        sb.append("/");
+        sb.append(name);
+        sb.append(".");
+        sb.append(ending);
+        file = new File(sb.toString());
+        conf = new TreeMap<>();
+    }
+    
+    public SnuviConfig(ISnuviLogger logger, String path, String name)
+    {    
+        this(logger, path, name, "snuvic");
+    }
+    
+    public final void load()
+    {
+        if(!exists())
+        {
+            throw new IllegalStateException("cannot load non existent file '" + file.getPath() + "'");
+        }
+        try
+        {
+            String warning = "wrong syntax in '" + file.getPath() + "'";
+            Files.readAllLines(file.toPath()).stream().forEach(s -> 
+            {
+                int b = s.indexOf("=");
+                if(b == -1)
+                {
+                    logger.printWarning(warning);
+                    logger.printWarning(s);
+                }
+                else
+                {
+                    conf.put(s.substring(0, b).trim(), Code.convertInput(s.substring(b + 1)));
+                }
+            });
+        } 
+        catch(MalformedInputException ex) 
+        {
+            logger.printWarning("'" + file.getPath() + "' contains an illegal character, change file encoding");
+        }
+        catch(OutOfMemoryError ex) 
+        {
+            logger.printWarning("'" + file.getPath() + "' is too big");
+        }
+        catch(SecurityException ex) 
+        {
+            logger.printWarning("'" + file.getPath() + "' is not accessable");
+            logger.printWarning(ex.getMessage());
+        }
+        catch(IOException ex) 
+        {
+            logger.printWarning("'" + file.getPath() + "' cannot be read");
+            logger.printWarning(ex.getMessage());
+        }
+    }   
+    
+    public final boolean exists()
+    {
+        return file.exists();
+    }
+    
+    public final boolean delete()
+    {
+        return file.delete();
+    }
+    
+    public final boolean save()
+    {
+        try
+        {
+            if(!file.getParentFile().exists())
+            {
+                file.getParentFile().mkdirs();
+            }
+            if(!file.exists())
+            {
+                try
+                {
+                    file.createNewFile();
+                }
+                catch(IOException ex)
+                {
+                    logger.printWarning("'" + file.getPath() + "' cannot be created");
+                    logger.printWarning(ex.getMessage());
+                    return false;
+                }
+            }
+            Files.write(Paths.get(file.toURI()), conf.entrySet().stream()
+                            .map(e -> 
+                            {
+                                if(e.getValue().getClass() == String.class)
+                                {
+                                    return e.getKey() + "=\"" + e.getValue() + "\"";
+                                }
+                                return e.getKey() + "=" + String.valueOf(e.getValue());
+                            })
+                            .collect(Collectors.toList()), Charset.forName("UTF-8"));
+            return true;
+        }
+        catch(UnsupportedOperationException ex)
+        {
+            logger.printWarning("an unsupported operation was used");
+            logger.printWarning(ex.getMessage());
+            return false;
+        }
+        catch(SecurityException ex)
+        {
+            logger.printWarning("'" + file.getPath() + "' is not accessable");
+            logger.printWarning(ex.getMessage());
+            return false;
+        }
+        catch(IOException ex)
+        {
+            logger.printWarning("cannot write to '" + file.getPath() + "'");
+            logger.printWarning(ex.getMessage());
+            return false;
+        }
+    }
+    
+    // -----------------------------------------------------------------------------------
+    // getter
+    // -----------------------------------------------------------------------------------
+    
+    protected final <T> T get(String key, Class<T> c, T error)
+    {
+        try
+        {
+            Object o = conf.get(key);
+            if(o == null)
+            {
+                return error;
+            }
+            return c.cast(o);
+        }
+        catch(ClassCastException ex)
+        {
+            return error;
+        }
+    }
+    
+    public final String getString(String key, String error)
+    {
+        return get(key, String.class, error);
+    }
+    
+    public final String getString(String key)
+    {
+        return getString(key, null);
+    }
+    
+    public final float getFloat(String key, float error)
+    {
+        return get(key, Double.class, Double.valueOf(error)).floatValue();
+    }
+    
+    public final double getDouble(String key, double error)
+    {
+        return get(key, Double.class, error);
+    }
+    
+    public final int getInt(String key, int error)
+    {
+        return get(key, Double.class, Double.valueOf(error)).intValue();
+    }
+    
+    public final boolean getBoolean(String key, boolean error)
+    {
+        return get(key, Boolean.class, error);
+    }
+    
+    // -----------------------------------------------------------------------------------
+    // set
+    // -----------------------------------------------------------------------------------
+    
+    public final void set(String key, Object o)
+    {
+        conf.put(key, o);
+    }
+}

+ 1 - 1
src/me/hammerle/math/Fraction.java

@@ -225,7 +225,7 @@ public class Fraction implements Cloneable, Comparable<Fraction>
         throw new ArithmeticException();
     }
     
-    private Fraction power(long p)
+    public Fraction power(long p)
     {
         if(p < 0)
         {

+ 24 - 5
src/me/hammerle/snuviscript/SnuviScript.java

@@ -6,12 +6,13 @@ import me.hammerle.code.SnuviParser;
 import me.hammerle.exceptions.SnuviException;
 import me.hammerle.code.ISnuviScheduler;
 import me.hammerle.exceptions.PreScriptException;
+import me.hammerle.math.Fraction;
 
 public class SnuviScript 
 {
     public static void main(String[] args) 
     {
-        SnuviParser parser = new SnuviParser(new ISnuviLogger() 
+        ISnuviLogger logger = new ISnuviLogger() 
         {
             @Override
             public void printException(SnuviException ex, Script s) 
@@ -40,7 +41,21 @@ public class SnuviScript
                 System.out.println(ex.getLine());
                 System.out.println(ex.getScriptName());
             }
-        }, new ISnuviScheduler() 
+
+            @Override
+            public void printWarning(String s) 
+            {
+                System.out.println("Warning: " + s);
+            }
+
+            @Override
+            public void printInfo(String s) 
+            {
+                System.out.println("Info: " + s);
+            }
+        };
+        
+        SnuviParser parser = new SnuviParser(logger, new ISnuviScheduler() 
         {
             @Override
             public int scheduleTask(Runnable r) 
@@ -80,14 +95,18 @@ public class SnuviScript
         }
         sb.append("debug(wusi);");*/
         
-        String s = "setVar(\"kürbis\",read.item(\"{id:'minecraft:pumpki"
+        /*String s = "setVar(\"kürbis\",read.item(\"{id:'minecraft:pumpki"
                 + "n_pie',Count:64b,tag:{display:{Lore:[0:'Ein fast ganze"
                 + "r Kürbiskuchen,',1:'bestehend aus sieben Achteln.'],Nam"
-                + "e:'KürbisMinusEinAchtelKuchen'}},Damage:0s}\")); debug(kürbis);";
+                + "e:'KürbisMinusEinAchtelKuchen'}},Damage:0s}\")); debug(kürbis);";*/
         //System.out.println(s);
         //System.out.println("___________");
         //parser.startScript(Script.class, "test", sb.toString(), true);
-        parser.startScript(Script.class, "test", s, true);
+        //parser.startScript(Script.class, "test", s, true);
         //parser.getScript(0).runCode();
+        
+        //Fraction f = new Fraction(2);
+        //Fraction f2 = new Fraction(2, 6);
+        System.out.println(Fraction.getE());
     }   
 }

+ 3 - 0
testfile.snuvic

@@ -0,0 +1,3 @@
+number=4
+test=true
+text="Das ist ein Text"