Browse Source

removed stuff for player scripts

Kajetan Johannes Hammerle 3 years ago
parent
commit
e78905d8e6

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

@@ -15,7 +15,7 @@ public class SnuviScript {
         }
         ConsoleScheduler cs = new ConsoleScheduler();
         ScriptManager sm = new ScriptManager(new ConsoleLogger(), cs);
-        sm.startScript(true, args[0], args);
+        sm.startScript(args[0], args);
         cs.tick();
     }
 }

+ 11 - 32
src/me/hammerle/snuviscript/code/Script.java

@@ -22,7 +22,7 @@ public final class Script {
 
     private final int id;
     private final String name;
-    private final ScriptManager sm;
+    private final ScriptManager scriptManager;
 
     private int lineIndex = 0;
     private final Instruction[] code;
@@ -41,8 +41,6 @@ public final class Script {
     private Stack<String> inFunction = new Stack<>();
     private Stack<Boolean> returnVarPop = new Stack<>();
 
-    // states if event broadcasts should be received, otherwise only direct event calls work
-    private boolean eventBroadcast;
     // waiting scripts stop executing and run again on an event
     private boolean isWaiting;
     // holded scripts do not receive events
@@ -51,17 +49,15 @@ public final class Script {
 
     private HashSet<String> loadedEvents = new HashSet<>();
 
-    private final Consumer<Script> onStart;
     private final Consumer<Script> onTerm;
 
     private final ArrayList<AutoCloseable> closeables = new ArrayList<>();
 
-    public Script(ScriptManager sm, Consumer<Script> onStart, Consumer<Script> onTerm, String name, String... path) {
+    public Script(ScriptManager sm, Consumer<Script> onTerm, String name, String... path) {
         ifState.push(true);
         this.id = idCounter++;
         this.name = name;
-        this.sm = sm;
-        this.onStart = onStart;
+        this.scriptManager = sm;
         this.onTerm = onTerm;
         Tokenizer t = new Tokenizer();
         InputStream[] streams = new InputStream[path.length];
@@ -118,13 +114,13 @@ public final class Script {
                     errorLine = -1;
                     continue;
                 }
-                sm.getLogger().print(null, ex, code[lineIndex].getName(), name, this, new StackTrace(code[lineIndex].getLine(), returnStack, code));
+                scriptManager.getLogger().print(null, ex, code[lineIndex].getName(), name, this, new StackTrace(code[lineIndex].getLine(), returnStack, code));
                 break;
             }
 
             if(System.nanoTime() > endTime) {
                 isHolded = true;
-                sm.getScheduler().scheduleTask(() -> {
+                scriptManager.getScheduler().scheduleTask(() -> {
                     if(!shouldTerm()) {
                         isHolded = false;
                         run();
@@ -135,7 +131,7 @@ public final class Script {
         }
         //System.out.println(count + " " + (15_000_000 / count));
         if(shouldTerm() && !dataStack.isEmpty()) {
-            sm.getLogger().print(String.format("data stack is not empty %s", dataStack));
+            scriptManager.getLogger().print(String.format("data stack is not empty %s", dataStack));
         }
     }
 
@@ -155,7 +151,7 @@ public final class Script {
     }
 
     public ScriptManager getScriptManager() {
-        return sm;
+        return scriptManager;
     }
 
     private HashMap<String, Integer> getLabels() {
@@ -251,14 +247,6 @@ public final class Script {
         return dataStack.peek();
     }
 
-    public void setEventBroadcast(boolean eventBroadcast) {
-        this.eventBroadcast = eventBroadcast;
-    }
-
-    public boolean shouldReceiveEventBroadcast() {
-        return eventBroadcast;
-    }
-
     public void term() {
         lineIndex = code.length;
         isWaiting = false;
@@ -269,26 +257,17 @@ public final class Script {
     }
 
     public void onTerm() {
-        if(onTerm != null) {
-            onTerm.accept(this);
-        }
-        closeables.forEach(c
-                -> {
-            sm.getLogger().print("prepared statement not closed", null, null, name, this, null);
+        onTerm.accept(this);
+        closeables.forEach(c -> {
+            scriptManager.getLogger().print("prepared statement not closed", null, null, name, this, null);
             try {
                 c.close();
             } catch(Exception ex) {
-                sm.getLogger().print("cannot close closeable in script", ex, null, name, this, null);
+                scriptManager.getLogger().print("cannot close closeable in script", ex, null, name, this, null);
             }
         });
     }
 
-    public void onStart() {
-        if(onStart != null) {
-            onStart.accept(this);
-        }
-    }
-
     public void setHolded(boolean b) {
         isHolded = b;
     }

+ 8 - 19
src/me/hammerle/snuviscript/code/ScriptManager.java

@@ -29,9 +29,6 @@ public class ScriptManager {
         return scheduler;
     }
 
-    // -------------------------------------------------------------------------
-    // function registry
-    // -------------------------------------------------------------------------
     public void registerFunction(String s, ExceptionBiFunction<Script, InputProvider[], Object> f) {
         FunctionRegistry.registerFunction(s, f);
     }
@@ -44,9 +41,6 @@ public class ScriptManager {
         FunctionRegistry.registerAlias(original, alias);
     }
 
-    // -------------------------------------------------------------------------
-    // script controller
-    // -------------------------------------------------------------------------
     public Script getScript(int id) {
         return scripts.get(id);
     }
@@ -60,7 +54,6 @@ public class ScriptManager {
 
     private void addScript(Script sc) {
         scripts.put(sc.getId(), sc);
-        sc.onStart();
         sc.run();
         if(sc.shouldTerm()) {
             removeScript(sc);
@@ -80,13 +73,12 @@ public class ScriptManager {
         return scripts.values();
     }
 
-    public Script startScript(boolean rEventBroadcast, Consumer<Script> onStart, Consumer<Script> onTerm, String name, String... paths) {
+    public Script startScript(Consumer<Script> onTerm, String name, String... paths) {
         if(paths.length == 0) {
             return null;
         }
         try {
-            Script sc = new Script(this, onStart, onTerm, name, paths);
-            sc.setEventBroadcast(rEventBroadcast);
+            Script sc = new Script(this, onTerm, name, paths);
             addScript(sc);
             return sc;
         } catch(PreScriptException ex) {
@@ -95,8 +87,9 @@ public class ScriptManager {
         }
     }
 
-    public Script startScript(boolean rEventBroadcast, String name, String... paths) {
-        return startScript(rEventBroadcast, null, null, name, paths);
+    public Script startScript(String name, String... paths) {
+        return startScript(sc -> {
+        }, name, paths);
     }
 
     public void loadEvent(String event, Script sc) {
@@ -122,7 +115,7 @@ public class ScriptManager {
         }
         try {
             set.stream()
-                    .filter(sc -> sc.shouldReceiveEventBroadcast() && !sc.isHolded() && sc.isWaiting())
+                    .filter(sc -> !sc.isHolded() && sc.isWaiting())
                     .forEach(sc -> runEvent(name, sc, before, after));
         } catch(Exception ex) {
             ex.printStackTrace();
@@ -139,13 +132,9 @@ public class ScriptManager {
 
     private void runEvent(String name, Script sc, Consumer<Script> before, Consumer<Script> after) {
         sc.setVar("event", name);
-        if(before != null) {
-            before.accept(sc);
-        }
+        before.accept(sc);
         sc.run();
-        if(after != null) {
-            after.accept(sc);
-        }
+        after.accept(sc);
         if(sc.shouldTerm()) {
             removeScript(sc);
         }

+ 4 - 4
src/me/hammerle/snuviscript/test/Test.java

@@ -24,10 +24,10 @@ public class Test {
         testCompiler();
         testOutput();
         
-        //LOGGER.reset();
-        //PARSER.startScript(true, "test", "./test/test.test");
-        //SCHEDULER.execute();
-        //LOGGER.printAll();
+        LOGGER.reset();
+        PARSER.startScript("test", "./test/test.test");
+        SCHEDULER.execute();
+        LOGGER.printAll();
     }
 
     private static void testOutput() {

+ 6 - 6
test/test.test

@@ -1,8 +1,8 @@
 c = 0;
-@loop
-c++;
-print(c);
-if(c < 40) {
-    sgoto(40, "loop");
+time = -time.getMillis();
+for(i = 0; i < 10000; i++) {
+    c += c;
 }
-wait();
+time += time.getMillis();
+print(c);
+print(time);