Skip to content

Commit 3030386

Browse files
author
Philipp Gersch
authored
Add files via upload
1 parent 9fff9f1 commit 3030386

4 files changed

Lines changed: 149 additions & 33 deletions

File tree

com/devkev/devscript/nativecommands/NativeLibrary.java

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import com.devkev.devscript.raw.Library;
2020
import com.devkev.devscript.raw.Output;
2121
import com.devkev.devscript.raw.Process;
22-
import com.devkev.devscript.raw.Process.GeneratedLibrary;
22+
import com.devkev.devscript.raw.Process.HookedLibrary;
2323
import com.devkev.devscript.raw.ApplicationBuilder.Type;
2424

2525
public class NativeLibrary extends Library {
@@ -153,38 +153,69 @@ public Object execute(Object[] args, Process application, Block block) throws Ex
153153
}
154154
},
155155

156-
new Command("exec", "string", "Executes a shell command") {
156+
new Command("exec", "string", "Executes a shell command. Output only and limited to one command.") {
157157
public Object execute(Object[] args, Process application, Block block) throws Exception {
158158
String OS = System.getProperty("os.name", "generic").toLowerCase();
159159

160160
if ((OS.indexOf("mac") >= 0) || (OS.indexOf("darwin") >= 0)) {
161161
//mac
162162
ProcessBuilder builder = new ProcessBuilder("/bin/bash", "-c", args[0].toString());
163163
java.lang.Process process = builder.start();
164+
164165
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
165166
String line = null;
166167
while ( (line = reader.readLine()) != null) {
167168
application.log(line, true);
168169
}
170+
reader.close();
171+
172+
BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
173+
line = null;
174+
while ( (line = errorReader.readLine()) != null) {
175+
application.log(line, true);
176+
}
177+
errorReader.close();
178+
process.destroy();
169179

170180
} else if (OS.indexOf("win") >= 0) {
171181
//windows
172182
ProcessBuilder builder = new ProcessBuilder("cmd", "/c", args[0].toString());
173183
java.lang.Process process = builder.start();
184+
174185
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
175186
String line = null;
176187
while ( (line = reader.readLine()) != null) {
177188
application.log(line, true);
178189
}
190+
reader.close();
191+
192+
BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
193+
line = null;
194+
while ( (line = errorReader.readLine()) != null) {
195+
application.log(line, true);
196+
}
197+
errorReader.close();
198+
process.destroy();
199+
179200
} else if (OS.indexOf("nux") >= 0) {
180201
//linux
181202
ProcessBuilder builder = new ProcessBuilder("/bin/bash", "-c", args[0].toString());
182203
java.lang.Process process = builder.start();
204+
183205
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
184206
String line = null;
185207
while ( (line = reader.readLine()) != null) {
186208
application.log(line, true);
187209
}
210+
reader.close();
211+
212+
BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
213+
line = null;
214+
while ( (line = errorReader.readLine()) != null) {
215+
application.log(line, true);
216+
}
217+
errorReader.close();
218+
process.destroy();
188219

189220
} else application.error("Failed to determine operating system");
190221
return null;
@@ -716,7 +747,7 @@ public Object execute(Object[] args, Process application, Block block) throws Ex
716747
application.log("onexit = {}; Fires when the application is finished. Useful for closing sockets etc.", true);
717748
application.log("", true);
718749
int maxLength = 0;
719-
for(GeneratedLibrary lib : application.getLibraries()) {
750+
for(HookedLibrary lib : application.getLibraries()) {
720751
application.log("\nLIBRARY: '" + lib.name + "' (" + lib.commands.length + (lib.commands.length > 1 ? " commands)\n" : " command)\n"), true);
721752
for(Command c : lib.commands) {
722753
String example = "";
@@ -764,6 +795,9 @@ public Object execute(Object[] args, Process application, Block block) throws Ex
764795
Class<?> pluginClass = loader.loadClass("CustomLibrary");
765796

766797
lib = (Library) pluginClass.getConstructor().newInstance();
798+
lib.bound = application;
799+
lib.scriptImport(application);
800+
767801
} catch(Exception e) {
768802
application.kill(block, "Java error occurred, while importing library: " + e.toString());
769803
return null;
@@ -900,4 +934,9 @@ public Object execute(Object[] args, Process application, Block block) throws Ex
900934
};
901935
}
902936

937+
@Override
938+
public void scriptImport(Process process) {}
939+
940+
@Override
941+
public void scriptExit(Process process, int exitCode, String errorMessage) {}
903942
}

com/devkev/devscript/raw/ConsoleMain.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import com.devkev.gui.Window;
1010

1111
/**@author Philipp Gersch
12-
* @version 1.8.2 (stable)
12+
* @version 1.9.6 (stable)
1313
* Valid arguments are: <br>-e or --execute [some code] - Executes the String<br>
1414
* -f or --file [path] - Reads the file and executes the code from it.<br>
1515
* If no argument was passed, the default Editor GUI will open.*/

com/devkev/devscript/raw/Library.java

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,42 @@
44

55
public abstract class Library {
66

7-
public final String COMPATIBLE_VERSION = "1.9.1";
7+
public final String COMPATIBLE_VERSION = "1.10.0";
88
private final ArrayList<Command> collection = new ArrayList<Command>();
99
private String name = "";
1010

11+
public Process bound;
12+
1113
public abstract Command[] createLib();
1214

1315
public Library(String name) {
1416
this.name = name;
1517
Command[] lib = createLib();
1618
if(lib != null) addCommand(lib);
1719
}
20+
21+
/**Called once when the library is initialized and the "import" command is called at script runtime.*/
22+
public abstract void scriptImport(Process process);
1823

19-
24+
/**Called when the JVM exits or the script finished
25+
* Exit codes: 0 = ok, 1 = error*/
26+
public abstract void scriptExit(Process process, int exitCode, String errorMessage);
27+
28+
2029
public static final Library merge(String mergedName, Library... lib) {
2130
Library merge = new Library(mergedName) {
2231
@Override
2332
public Command[] createLib() {
2433
return null;
2534
}
35+
36+
@Override
37+
public void scriptImport(Process process) {
38+
}
39+
40+
@Override
41+
public void scriptExit(Process process, int exitCode, String errorMessage) {
42+
}
2643
};
2744
for(Library l : lib) merge.collection.addAll(l.collection);
2845
return merge;
@@ -47,7 +64,24 @@ public final String getName() {
4764
public String toString() {
4865
return name;
4966
}
50-
67+
68+
/**Manually executes a function while the process is running<br>
69+
* The function needs to be in the main block.*/
70+
public void executeEventFunction(String functionName, Object ... variables) throws IllegalAccessError {
71+
if(bound != null) {
72+
if(bound.isRunning()) {
73+
Object obj = bound.getVariable(functionName, null);
74+
if(obj != null) {
75+
if(obj instanceof Block) {
76+
Block b = (Block) obj;
77+
bound.executeBlock(b, true, variables);
78+
79+
} else throw new IllegalAccessError("Variable " + functionName + " is not a function!");
80+
} else throw new IllegalAccessError("Event funtion with the name " + functionName + " not registered!");
81+
} else throw new IllegalAccessError("Process not running!");
82+
} else throw new IllegalAccessError("Library was not added to any processes!");
83+
}
84+
5185
public final boolean removeCommand(String commandName) {
5286
for(Command c : collection) {
5387
if(c.name.equals(commandName)) {

com/devkev/devscript/raw/Process.java

Lines changed: 69 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class Process {
1414

1515
private static long process_id = 0L;
1616

17-
private ArrayList<GeneratedLibrary> libraries = new ArrayList<GeneratedLibrary>();
17+
private ArrayList<HookedLibrary> libraries = new ArrayList<HookedLibrary>();
1818
private ArrayList<Output> output = new ArrayList<Output>(1);
1919
private ArrayList<Variable> variables = new ArrayList<Variable>(5);
2020
private ApplicationListener listener;
@@ -33,7 +33,7 @@ public class Process {
3333

3434
public long maxRuntime = 0; //Runtime in ms. If < 0, runtime is infinite
3535
private long currentChar = 0;
36-
public final String version = "1.9.1";
36+
public final String version = "1.9.6";
3737

3838
/**The file, the script is executed from. May be null. Just useful for some Native commands*/
3939
public File file = null;
@@ -46,11 +46,14 @@ public class Process {
4646
// public volatile int execution_time = 0;
4747
// public volatile float average_commands_per_sec = 0;
4848

49-
public class GeneratedLibrary {
49+
public class HookedLibrary {
50+
5051
public final Command[] commands;
5152
public final String name;
53+
public final Library lib;
5254

53-
public GeneratedLibrary(Library library) {
55+
public HookedLibrary(Library library) {
56+
this.lib = library;
5457
this.commands = library.createLib();
5558
this.name = library.getName();
5659
}
@@ -94,9 +97,20 @@ boolean setValue(Object value, boolean ignoreCheck) {
9497
* If you don't want these native commands, call {@link Process#clearLibraries()} before adding your custom ones*/
9598
public Process(boolean useCache) {
9699
includeLibrary(new NativeLibrary());
100+
101+
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
102+
@Override
103+
public void run() {
104+
//Cant execute onexit function. Just execute the library listener
105+
if(isRunning())
106+
kill(main, "JVM killed");
107+
}
108+
}));
97109
}
98110

99111
public Thread execute(String script, boolean newThread) {
112+
finalizing = false;
113+
100114
if(main != null) {
101115
if(main.alive) {
102116
warning("Java Environment was trying to start a new process, while its already running");
@@ -259,16 +273,8 @@ private void start(Block block) {
259273
// }
260274

261275
//Searches for a variable called onexit with the type BLOCK
262-
263-
264-
265-
if(block.equals(main) && listener != null) {
266-
Object exitFunction = getVariable("onexit", null);
267-
268-
if(exitFunction != null)
269-
executeBlock(((Block) exitFunction), true);
270-
271-
listener.done(main.exitCode);
276+
if(main == block) {
277+
finalizeExit(0, "finished");
272278
}
273279
block.alive = false;
274280
aliveBlocks.remove(block);
@@ -349,7 +355,7 @@ else if(!block.cached.isEmpty()) {
349355
addToBlockCache = true;
350356
}
351357

352-
for(GeneratedLibrary lib : libraries) {
358+
for(HookedLibrary lib : libraries) {
353359
for(Command c : lib.commands) {
354360
if(c.commandNameOffset >= args.size()) continue;
355361

@@ -648,9 +654,10 @@ public void setVariable(String name, Object value, boolean FINAL, boolean perman
648654
if(stack1 <= stack2) {
649655
if(!v.FINAL) {// || v.permanent) {
650656
if(!v.setValue(value, getMain() == null)) kill(block, "Failed to assign type " + value + " to existing variable " + v.value);
651-
} else if(getMain() != null) {
652-
kill(block,"Tried to modyfy a constant: " + name);
653657
}
658+
//} else if(v.FINAL && getMain() != null) {
659+
// kill(block,"Tried to modyfy a constant: " + name);
660+
//}
654661
return;
655662
}
656663
}
@@ -679,7 +686,12 @@ public Object getVariable(String name, Block block) {
679686
}
680687

681688
public void includeLibrary(Library lib) {
682-
libraries.add(new GeneratedLibrary(lib));
689+
if(lib.bound != null) {
690+
if(lib.bound != this) {
691+
throw new IllegalAccessError("This Library instance is already part of a different process. Try creating a new one: includeLibrary(new Library())");
692+
}
693+
}
694+
libraries.add(new HookedLibrary(lib));
683695
}
684696

685697
public void clearLibraries() {
@@ -719,12 +731,12 @@ public void warning(String message) {
719731
});
720732
}
721733

722-
public ArrayList<GeneratedLibrary> getLibraries() {
734+
public ArrayList<HookedLibrary> getLibraries() {
723735
return libraries;
724736
}
725737

726-
public GeneratedLibrary getLibrary(String name) {
727-
for(GeneratedLibrary lib : libraries) {
738+
public HookedLibrary getLibrary(String name) {
739+
for(HookedLibrary lib : libraries) {
728740
if(lib.name.equals(name)) return lib;
729741
}
730742
return null;
@@ -739,15 +751,19 @@ public synchronized void kill(Block block, String errorMessage) {
739751
block.currentCommand = block.currentCommand.subSequence(0, 10) + " ... " + block.currentCommand.substring(block.currentCommand.length() - 10, block.currentCommand.length());
740752
}
741753
if(!errorMessage.isEmpty()) error("Error at [" + block.currentCommand + "]> " + errorMessage);
742-
743-
for(Block b : aliveBlocks) {
744-
b.cached.clear();
745-
b.alive = false;
746-
b.interrupted = true;
754+
755+
finalizeExit(1, errorMessage);
756+
757+
for(int i = 0; i < aliveBlocks.size(); i++) {
758+
aliveBlocks.get(i).cached.clear();
759+
aliveBlocks.get(i).alive = false;
760+
aliveBlocks.get(i).interrupted = true;
747761
}
748762
block.alive = false;
749763
block.interrupted = true;
750764
block.currentCommand = "";
765+
766+
//System.out.println("Cleaning stack ...");
751767
}
752768

753769
block.exitCode = Block.ERROR;
@@ -817,6 +833,7 @@ public String waitForInput() throws IOException {
817833
return inputReader.readLine();
818834
}
819835

836+
/**You can use {@link ApplicationInput}*/
820837
public void setInput(InputStream input) {
821838
if(input == null) return;
822839

@@ -890,4 +907,30 @@ public static String findMatching(String string, int start, int skip, char openC
890907
}
891908
return null;
892909
}
910+
911+
boolean finalizing = false;
912+
913+
private void finalizeExit(int exitCode, String errorMessage) {
914+
if(!finalizing) return;
915+
916+
finalizing = true;
917+
918+
Object exitFunction = getVariable("onexit", null);
919+
920+
if(exitFunction != null)
921+
executeBlock(((Block) exitFunction), true, exitCode, errorMessage);
922+
923+
for(HookedLibrary lib : libraries) {
924+
lib.lib.scriptExit(this, exitCode, errorMessage);
925+
}
926+
}
927+
928+
@Override
929+
protected void finalize() throws Throwable {
930+
finalizeExit(1, "JVM Killed");
931+
932+
if(listener != null) {
933+
listener.done(main.exitCode);
934+
}
935+
}
893936
}

0 commit comments

Comments
 (0)