Forráskód Böngészése

added library support (starting merged scripts)

Kajetan Johannes Hammerle 6 éve
szülő
commit
1d116a411d

+ 1 - 1
src/me/hammerle/snuviscript/SnuviScript.java

@@ -61,7 +61,7 @@ public class SnuviScript
             }
         };
         SnuviParser parser = new SnuviParser(logger, scheduler);
-        parser.startScript("./test", ".sbasic", true);
+        parser.startScript(true, ".sbasic", "./test", "./test2");
         
         parser.callEvent("testevent", null, null);
     }  

+ 10 - 6
src/me/hammerle/snuviscript/code/SnuviParser.java

@@ -104,12 +104,16 @@ public class SnuviParser
         return scripts.values();
     }
     
-    private Script startScript(String path, String end, boolean rEventBroadcast, Runnable onStart, Runnable onTerm)
+    private Script startScript(boolean rEventBroadcast, Runnable onStart, Runnable onTerm, String end, String... paths)
     { 
+        if(paths.length == 0)
+        {
+            return null;
+        }
         try
         {            
-            List<String> code = Utils.readCode(path, end);
-            Script sc = new Script(logger, scheduler, code, path, idCounter++, onStart, onTerm, rEventBroadcast);
+            List<String> code = Utils.readCode(end, paths);
+            Script sc = new Script(logger, scheduler, code, paths[0], idCounter++, onStart, onTerm, rEventBroadcast);
             scripts.put(sc.id, sc);
             sc.onStart();
             sc.run();
@@ -118,14 +122,14 @@ public class SnuviParser
         }
         catch(PreScriptException ex)
         {
-            logger.print(ex.getLocalizedMessage(), ex, null, path, null, ex.getLine() + 1);
+            logger.print(ex.getLocalizedMessage(), ex, null, paths[0], null, ex.getLine() + 1);
             return null;
         }
     }
     
-    public Script startScript(String path, String end, boolean rEventBroadcast)
+    public Script startScript(boolean rEventBroadcast, String end, String... paths)
     { 
-        return startScript(path, end, rEventBroadcast, null, null);
+        return startScript(rEventBroadcast, null, null, end, paths);
     }
     
     // -----------------------------------------------------------------------------------

+ 29 - 12
src/me/hammerle/snuviscript/code/Utils.java

@@ -5,6 +5,7 @@ import java.io.IOException;
 import java.lang.reflect.Array;
 import java.nio.charset.MalformedInputException;
 import java.nio.file.Files;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.HashMap;
@@ -246,25 +247,41 @@ public class Utils
     // file stuff
     // -------------------------------------------------------------------------
     
-    public static List<String> readCode(String filename, String ending)
+    public static List<String> readCode(String ending, String... filenames)
     {
-        File script = new File("./" + filename + ending);  
-        if(script.exists())
+        LinkedList<List<String>> lists = new LinkedList<>();
+        List<String> list;
+        File script;
+        int lines = 0;
+        for(String filename : filenames)
         {
-            try
+            script = new File("./" + filename + ending);
+            if(script.exists())
             {
-                return Files.readAllLines(script.toPath());
-            } 
-            catch (MalformedInputException ex) 
-            {
-                throw new PreScriptException("'" + filename + "' contains an illegal character, change file encoding", 0);
+                try
+                {
+                    list = Files.readAllLines(script.toPath());
+                    lines += list.size();
+                    lists.add(list);
+                } 
+                catch (MalformedInputException ex) 
+                {
+                    throw new PreScriptException("'" + filename + "' contains an illegal character, change file encoding", 0);
+                }
+                catch (IOException ex) 
+                {
+                    throw new PreScriptException("file '" + filename + "' cannot be read", 0);
+                }
             }
-            catch (IOException ex) 
+            else
             {
-                throw new PreScriptException("file '" + filename + "' cannot be read", 0);
+                throw new PreScriptException("file '" + filename + "' does not exist", 0);
             }
         }
-        throw new PreScriptException("file '" + filename + "' does not exist", 0);
+        
+        ArrayList<String> mergedList = new ArrayList<>(lines);
+        lists.forEach(l -> mergedList.addAll(l));
+        return mergedList;
     }
     
     public static List<String> readCode(String filename)

+ 3 - 0
test2.sbasic

@@ -0,0 +1,3 @@
+hallo("1");
+hallo("2");
+hallo("3");