Changeset 2d1909f5e2b47ba4dcaeb4fd0fb06671d95c8e0b
- Timestamp:
- 12/02/07 15:33:05
(1 year ago)
- Author:
- Christopher Jung <bktheg@web.de>
- git-committer:
- Christopher Jung <bktheg@web.de> 1196605985 +0100
- git-parent:
[5b67e17c38962e1103b45d7758a1d9b5b7ee9622]
- git-author:
- Christopher Jung <bktheg@web.de> 1196605985 +0100
- Message:
ScriptParser?-System in ersten Teilen zu javax.script kompatibel gemacht
-
Files:
-
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
| r953ed23 |
r2d1909f |
|
| 334 | 334 | scriptparser.setRegister("QUEST", "r"+rqid); |
|---|
| 335 | 335 | |
|---|
| 336 | | scriptparser.executeScript( runningquest.getString("uninstall"), "0" ); |
|---|
| | 336 | scriptparser.eval( runningquest.getString("uninstall"), "0" ); |
|---|
| 337 | 337 | } |
|---|
| 338 | 338 | |
|---|
| rc30c5b7 |
r2d1909f |
|
| 907 | 907 | } |
|---|
| 908 | 908 | |
|---|
| 909 | | scriptparser.executeScript(script.getScript(), execparameter); |
|---|
| | 909 | scriptparser.eval(script.getScript(), execparameter); |
|---|
| 910 | 910 | |
|---|
| 911 | 911 | usequest = scriptparser.getRegister("QUEST"); |
|---|
| … | … | |
| 913 | 913 | if( !usequest.equals("") ) { |
|---|
| 914 | 914 | try { |
|---|
| 915 | | scriptparser.getContext().toStream(execdata.setBinaryStream(1)); |
|---|
| | 915 | ScriptParserContext.toStream(scriptparser.getContext(), execdata.setBinaryStream(1)); |
|---|
| 916 | 916 | } |
|---|
| 917 | 917 | catch( Exception e ) { |
|---|
| rd220799 |
r2d1909f |
|
| 182 | 182 | scriptparser.setRegister("USER", Integer.toString(getUser().getID())); |
|---|
| 183 | 183 | scriptparser.setRegister("QUEST", "r"+questid); |
|---|
| 184 | | scriptparser.executeScript(":0\n!ENDQUEST\n!QUIT","0"); |
|---|
| | 184 | scriptparser.eval(":0\n!ENDQUEST\n!QUIT","0"); |
|---|
| 185 | 185 | |
|---|
| 186 | 186 | redirect(); |
|---|
| rf034a00 |
r2d1909f |
|
| 105 | 105 | scriptparser.setRegister("USER", userid); |
|---|
| 106 | 106 | scriptparser.setRegister("QUEST", "r"+rquest.getInt("id")); |
|---|
| 107 | | scriptparser.executeScript(":0\n!ENDQUEST\n!QUIT","0"); |
|---|
| | 107 | scriptparser.eval(":0\n!ENDQUEST\n!QUIT","0"); |
|---|
| 108 | 108 | } |
|---|
| 109 | 109 | catch( Exception e ) { |
|---|
| rf034a00 |
r2d1909f |
|
| 95 | 95 | scriptparser.setRegister("USER", rquest.getInt("userid")); |
|---|
| 96 | 96 | scriptparser.setRegister("QUEST", "r"+rquest.getInt("id")); |
|---|
| 97 | | scriptparser.executeScript(":0\n!ENDQUEST\n!QUIT","0"); |
|---|
| | 97 | scriptparser.eval(":0\n!ENDQUEST\n!QUIT","0"); |
|---|
| 98 | 98 | echo.append("Beende Quest bei Spieler "+rquest.getInt("userid")+"<br />\n"); |
|---|
| 99 | 99 | } |
|---|
| r3c6b450 |
r2d1909f |
|
| 25 | 25 | import java.util.List; |
|---|
| 26 | 26 | import java.util.Map; |
|---|
| | 27 | |
|---|
| | 28 | import javax.script.ScriptContext; |
|---|
| 27 | 29 | |
|---|
| 28 | 30 | import net.driftingsouls.ds2.server.ContextCommon; |
|---|
| … | … | |
| 687 | 689 | |
|---|
| 688 | 690 | scriptparser.setContext(new ScriptParserContext()); |
|---|
| 689 | | scriptparser.executeScript( runningdata.getString("uninstall"), "0" ); |
|---|
| | 691 | scriptparser.eval( runningdata.getString("uninstall"), "0" ); |
|---|
| 690 | 692 | |
|---|
| 691 | 693 | scriptparser.setContext(context); |
|---|
| … | … | |
| 1175 | 1177 | try { |
|---|
| 1176 | 1178 | Blob blob = questdata.getBlob("execdata"); |
|---|
| 1177 | | scriptparser.getContext().addContextData( |
|---|
| 1178 | | ScriptParserContext.fromStream(blob.getBinaryStream()) |
|---|
| | 1179 | scriptparser.getContext().getBindings(ScriptContext.ENGINE_SCOPE).putAll( |
|---|
| | 1180 | ScriptParserContext.fromStream(blob.getBinaryStream()).getBindings(ScriptContext.ENGINE_SCOPE) |
|---|
| 1179 | 1181 | ); |
|---|
| 1180 | 1182 | scriptparser.setRegister("QUEST","r"+questdata.getInt("id")); |
|---|
| … | … | |
| 1220 | 1222 | try { |
|---|
| 1221 | 1223 | Blob blob = questdata.getBlob("execdata"); |
|---|
| 1222 | | scriptparser.getContext().toStream(blob.setBinaryStream(1)); |
|---|
| | 1224 | ScriptParserContext.toStream(scriptparser.getContext(), blob.setBinaryStream(1)); |
|---|
| 1223 | 1225 | db.prepare("UPDATE quests_running SET execdata=? WHERE id=? ") |
|---|
| 1224 | 1226 | .update(blob, questdata.getInt("id")); |
|---|
| rf034a00 |
r2d1909f |
|
| 142 | 142 | scriptparser.setRegister("SCRIPT", Integer.toString(usescript)); |
|---|
| 143 | 143 | scriptparser.setRegister("LOCKEXEC", "1"); |
|---|
| 144 | | scriptparser.executeScript(script.getString("script"), execparameter); |
|---|
| | 144 | scriptparser.eval(script.getString("script"), execparameter); |
|---|
| 145 | 145 | |
|---|
| 146 | 146 | runningdata.free(); |
|---|
| … | … | |
| 268 | 268 | } |
|---|
| 269 | 269 | scriptparser.setRegister("SCRIPT", Integer.toString(usescript)); |
|---|
| 270 | | scriptparser.executeScript(script.getString("script"), execparameter); |
|---|
| | 270 | scriptparser.eval(script.getString("script"), execparameter); |
|---|
| 271 | 271 | |
|---|
| 272 | 272 | usequest = scriptparser.getRegister("QUEST"); |
|---|
| … | … | |
| 287 | 287 | try { |
|---|
| 288 | 288 | Blob execdata = runningdata.getBlob("execdata"); |
|---|
| 289 | | scriptparser.getContext().toStream( execdata.setBinaryStream(1) ); |
|---|
| | 289 | ScriptParserContext.toStream(scriptparser.getContext(), execdata.setBinaryStream(1)); |
|---|
| 290 | 290 | db.prepare("UPDATE quests_running SET execdata=? WHERE id=? ") |
|---|
| 291 | 291 | .update(execdata, runningdata.getInt("id")); |
|---|
| refe50c3 |
r2d1909f |
|
| 27 | 27 | import java.util.Stack; |
|---|
| 28 | 28 | |
|---|
| | 29 | import javax.script.ScriptContext; |
|---|
| | 30 | |
|---|
| 29 | 31 | import net.driftingsouls.ds2.server.comm.PM; |
|---|
| 30 | 32 | import net.driftingsouls.ds2.server.entities.User; |
|---|
| … | … | |
| 267 | 269 | * @return <code>true</code>, falls das Kommando neu registriert wurde und noch nicht registriert war |
|---|
| 268 | 270 | */ |
|---|
| 269 | | public boolean registerCommand( String command, SPFunction function, Args ... args ) { |
|---|
| | 271 | protected boolean registerCommand( String command, SPFunction function, Args ... args ) { |
|---|
| 270 | 272 | command = "!"+command; |
|---|
| 271 | 273 | if( !funcregister.containsKey(command) ) { |
|---|
| … | … | |
| 290 | 292 | * @param txt Der zu loggende String |
|---|
| 291 | 293 | */ |
|---|
| 292 | | public void log( String txt ) { |
|---|
| | 294 | protected void log( String txt ) { |
|---|
| 293 | 295 | logFunction.log(txt); |
|---|
| 294 | 296 | } |
|---|
| … | … | |
| 298 | 300 | * |
|---|
| 299 | 301 | */ |
|---|
| 300 | | public void startLogging() { |
|---|
| | 302 | private void startLogging() { |
|---|
| 301 | 303 | logFunction.start(); |
|---|
| 302 | 304 | } |
|---|
| … | … | |
| 306 | 308 | * |
|---|
| 307 | 309 | */ |
|---|
| 308 | | public void stopLogging() { |
|---|
| | 310 | private void stopLogging() { |
|---|
| 309 | 311 | logFunction.stop(); |
|---|
| 310 | 312 | } |
|---|
| … | … | |
| 315 | 317 | */ |
|---|
| 316 | 318 | public void setShip( Ship ship ) { |
|---|
| 317 | | context.setRegister("_SHIP", ship); |
|---|
| | 319 | context.getBindings(ScriptContext.ENGINE_SCOPE).put("_SHIP", ship); |
|---|
| 318 | 320 | } |
|---|
| 319 | 321 | |
|---|
| … | … | |
| 323 | 325 | */ |
|---|
| 324 | 326 | public Ship getShip() { |
|---|
| 325 | | return (Ship)context.getRegisterObject("_SHIP"); |
|---|
| | 327 | return (Ship)context.getBindings(ScriptContext.ENGINE_SCOPE).get("_SHIP"); |
|---|
| 326 | 328 | } |
|---|
| 327 | 329 | |
|---|
| … | … | |
| 332 | 334 | */ |
|---|
| 333 | 335 | public String getRegister( String reg ) { |
|---|
| 334 | | return context.getRegister(reg); |
|---|
| | 336 | if( reg.charAt(0) == '#' ) { |
|---|
| | 337 | reg = reg.substring(1); |
|---|
| | 338 | } |
|---|
| | 339 | |
|---|
| | 340 | Object val = context.getBindings(ScriptContext.ENGINE_SCOPE).get(reg); |
|---|
| | 341 | |
|---|
| | 342 | if( val == null ) { |
|---|
| | 343 | return ""; |
|---|
| | 344 | } |
|---|
| | 345 | return val.toString(); |
|---|
| 335 | 346 | } |
|---|
| 336 | 347 | |
|---|
| … | … | |
| 341 | 352 | */ |
|---|
| 342 | 353 | public Object getRegisterObject( String reg ) { |
|---|
| 343 | | return context.getRegisterObject(reg); |
|---|
| | 354 | if( reg.charAt(0) == '#' ) { |
|---|
| | 355 | reg = reg.substring(1); |
|---|
| | 356 | } |
|---|
| | 357 | |
|---|
| | 358 | return context.getBindings(ScriptContext.ENGINE_SCOPE).get(reg); |
|---|
| 344 | 359 | } |
|---|
| 345 | 360 | |
|---|
| … | … | |
| 350 | 365 | */ |
|---|
| 351 | 366 | public void setRegister( String reg, Object data ) { |
|---|
| 352 | | context.setRegister(reg, data); |
|---|
| | 367 | if( reg.charAt(0) == '#' ) { |
|---|
| | 368 | reg = reg.substring(1); |
|---|
| | 369 | } |
|---|
| | 370 | |
|---|
| | 371 | context.getBindings(ScriptContext.ENGINE_SCOPE).put(reg, data); |
|---|
| 353 | 372 | } |
|---|
| 354 | 373 | |
|---|
| … | … | |
| 450 | 469 | * @return Das Ergebnis |
|---|
| 451 | 470 | */ |
|---|
| 452 | | public String evalTerm( String term ) { |
|---|
| | 471 | private String evalTerm( String term ) { |
|---|
| 453 | 472 | int index = 0; |
|---|
| 454 | 473 | Stack<TermElement> stack = new Stack<TermElement>(); |
|---|
| … | … | |
| 531 | 550 | * @return Der Inhalt oder <code>null</code> |
|---|
| 532 | 551 | */ |
|---|
| 533 | | public String getParameter( int number ) { |
|---|
| | 552 | protected String getParameter( int number ) { |
|---|
| 534 | 553 | if( (number > -1) && (addparameterlist.size() > number) ) { |
|---|
| 535 | 554 | return this.addparameterlist.get(number); |
|---|
| … | … | |
| 558 | 577 | * @param script das Script |
|---|
| 559 | 578 | */ |
|---|
| 560 | | public void executeScript(String script) { |
|---|
| 561 | | executeScript(script, ""); |
|---|
| 562 | | } |
|---|
| 563 | | |
|---|
| | 579 | public void eval(String script) { |
|---|
| | 580 | eval(script, ""); |
|---|
| | 581 | } |
|---|
| | 582 | |
|---|
| 564 | 583 | /** |
|---|
| 565 | 584 | * Fuehrt das angegebene Script aus |
|---|
| … | … | |
| 567 | 586 | * @param parameter Ausfuehrungsparameter |
|---|
| 568 | 587 | */ |
|---|
| 569 | | public void executeScript(String script, String parameter) { |
|---|
| | 588 | public void eval(String script, String parameter) { |
|---|
| 570 | 589 | this.startLogging(); |
|---|
| 571 | 590 | |
|---|
| … | … | |
| 648 | 667 | } |
|---|
| 649 | 668 | |
|---|
| 650 | | // Ggf. Parameter behandeln |
|---|
| 651 | | if( parameter.length() > 0 ) { |
|---|
| 652 | | if( parameter.equals("-1") ) { |
|---|
| 653 | | context.out(context.getRegister("_OUTPUT")); |
|---|
| 654 | | this.stopLogging(); |
|---|
| 655 | | |
|---|
| 656 | | return; |
|---|
| 657 | | } |
|---|
| 658 | | else if( !validInternalParams.contains(parameter) && !parameterlist.containsKey(':'+parameter) ) { |
|---|
| 659 | | this.log("+++ Ungueltiger Parameter >"+parameter+"< - benutze >0<\n"); |
|---|
| 660 | | parameter = "0"; |
|---|
| 661 | | } |
|---|
| 662 | | else if( !validInternalParams.contains(parameter) && (validparameters.size() > 0) && !validparameters.contains(parameter) ) { |
|---|
| 663 | | this.log("+++ Parameter >"+parameter+"< gesperrt - benutze >0<\n"); |
|---|
| 664 | | parameter = "0"; |
|---|
| 665 | | } |
|---|
| 666 | | context.setLastCommand(parameterlist.get(':'+parameter)); |
|---|
| 667 | | } |
|---|
| 668 | | |
|---|
| 669 | 669 | try { |
|---|
| | 670 | // Ggf. Parameter behandeln |
|---|
| | 671 | if( parameter.length() > 0 ) { |
|---|
| | 672 | if( parameter.equals("-1") ) { |
|---|
| | 673 | context.getWriter().append(context.getBindings(ScriptContext.ENGINE_SCOPE).get("_OUTPUT").toString()); |
|---|
| | 674 | this.stopLogging(); |
|---|
| | 675 | |
|---|
| | 676 | return; |
|---|
| | 677 | } |
|---|
| | 678 | else if( !validInternalParams.contains(parameter) && !parameterlist.containsKey(':'+parameter) ) { |
|---|
| | 679 | this.log("+++ Ungueltiger Parameter >"+parameter+"< - benutze >0<\n"); |
|---|
| | 680 | parameter = "0"; |
|---|
| | 681 | } |
|---|
| | 682 | else if( !validInternalParams.contains(parameter) && (validparameters.size() > 0) && !validparameters.contains(parameter) ) { |
|---|
| | 683 | this.log("+++ Parameter >"+parameter+"< gesperrt - benutze >0<\n"); |
|---|
| | 684 | parameter = "0"; |
|---|
| | 685 | } |
|---|
| | 686 | context.setLastCommand(parameterlist.get(':'+parameter)); |
|---|
| | 687 | } |
|---|
| | 688 | |
|---|
| 670 | 689 | // Und los gehts! |
|---|
| 671 | 690 | while( true ) { |
|---|
| … | … | |
| 842 | 861 | if( dump.equals("register") ) { |
|---|
| 843 | 862 | this.log("################# register ###################\n"); |
|---|
| 844 | | for( String reg : context.getRegisterList() ) { |
|---|
| | 863 | for( String reg : context.getBindings(ScriptContext.ENGINE_SCOPE).keySet() ) { |
|---|
| 845 | 864 | if( reg.equals("_SHIP") ) { |
|---|
| 846 | 865 | continue; |
|---|
| 847 | 866 | } |
|---|
| 848 | | this.log(reg+" ("+context.getRegister(reg)+"), "); |
|---|
| | 867 | this.log(reg+" ("+context.getBindings(ScriptContext.ENGINE_SCOPE).get(reg)+"), "); |
|---|
| 849 | 868 | } |
|---|
| 850 | 869 | this.log("\n\n"); |
|---|
| … | … | |
| 886 | 905 | this.log("\n\n"); |
|---|
| 887 | 906 | this.log("################# register ###################\n"); |
|---|
| 888 | | for( String reg : context.getRegisterList() ) { |
|---|
| | 907 | for( String reg : context.getBindings(ScriptContext.ENGINE_SCOPE).keySet() ) { |
|---|
| 889 | 908 | if( reg.equals("_SHIP") ) { |
|---|
| 890 | 909 | continue; |
|---|
| 891 | 910 | } |
|---|
| 892 | | this.log(reg+" ("+context.getRegister(reg)+"), "); |
|---|
| | 911 | this.log(reg+" ("+context.getBindings(ScriptContext.ENGINE_SCOPE).get(reg)+"), "); |
|---|
| 893 | 912 | } |
|---|
| 894 | 913 | this.log("\n\n"); |
|---|
| … | … | |
| 902 | 921 | } |
|---|
| 903 | 922 | } |
|---|
| 904 | | catch( RuntimeException e ) { |
|---|
| | 923 | catch( Exception e ) { |
|---|
| 905 | 924 | if( this.namespace == NameSpace.ACTION ) { |
|---|
| 906 | 925 | StringBuilder text = new StringBuilder(); |
|---|
| … | … | |
| 920 | 939 | } |
|---|
| 921 | 940 | else { |
|---|
| 922 | | throw e; |
|---|
| | 941 | if( e instanceof RuntimeException ) { |
|---|
| | 942 | throw (RuntimeException)e; |
|---|
| | 943 | } |
|---|
| | 944 | |
|---|
| | 945 | throw new RuntimeException("Script execution failed", e); |
|---|
| 923 | 946 | } |
|---|
| 924 | 947 | } |
|---|
| r8f2568b |
r2d1909f |
|
| 19 | 19 | package net.driftingsouls.ds2.server.scripting; |
|---|
| 20 | 20 | |
|---|
| | 21 | import java.io.IOException; |
|---|
| 21 | 22 | import java.io.InputStream; |
|---|
| 22 | 23 | import java.io.ObjectInputStream; |
|---|
| … | … | |
| 24 | 25 | import java.io.OutputStream; |
|---|
| 25 | 26 | import java.io.Serializable; |
|---|
| | 27 | import java.io.StringWriter; |
|---|
| 26 | 28 | import java.util.HashMap; |
|---|
| 27 | 29 | import java.util.Map; |
|---|
| 28 | | import java.util.Set; |
|---|
| | 30 | |
|---|
| | 31 | import javax.script.ScriptContext; |
|---|
| | 32 | import javax.script.SimpleScriptContext; |
|---|
| 29 | 33 | |
|---|
| 30 | 34 | /** |
|---|
| … | … | |
| 35 | 39 | * |
|---|
| 36 | 40 | */ |
|---|
| 37 | | public class ScriptParserContext { |
|---|
| 38 | | private Map<String,Object> register; |
|---|
| 39 | | private int lastcommand; |
|---|
| 40 | | private StringBuffer out = new StringBuffer(); |
|---|
| 41 | | |
|---|
| | 41 | public class ScriptParserContext extends SimpleScriptContext { |
|---|
| 42 | 42 | /** |
|---|
| 43 | 43 | * Konstruktor |
|---|
| … | … | |
| 45 | 45 | */ |
|---|
| 46 | 46 | public ScriptParserContext() { |
|---|
| 47 | | this.register = new HashMap<String,Object>(); |
|---|
| 48 | | this.lastcommand = 0; |
|---|
| | 47 | super(); |
|---|
| | 48 | |
|---|
| | 49 | setWriter(new StringWriter()); |
|---|
| | 50 | engineScope.put("__INSTRUCTIONPOINTER", 0); |
|---|
| 49 | 51 | } |
|---|
| 50 | | |
|---|
| 51 | | /** |
|---|
| 52 | | * Gibt das angegebene Register zurueck |
|---|
| 53 | | * @param reg Das Register |
|---|
| 54 | | * @return der Inhalt des Registers |
|---|
| 55 | | */ |
|---|
| 56 | | public String getRegister( String reg ) { |
|---|
| 57 | | return getRegisterObject(reg).toString(); |
|---|
| 58 | | } |
|---|
| 59 | | |
|---|
| 60 | | /** |
|---|
| 61 | | * Gibt das angegebene Register zurueck |
|---|
| 62 | | * @param reg Das Register |
|---|
| 63 | | * @return der Inhalt des Registers |
|---|
| 64 | | */ |
|---|
| 65 | | public Object getRegisterObject( String reg ) { |
|---|
| 66 | | if( reg.charAt(0) == '#' ) { |
|---|
| 67 | | reg = reg.substring(1); |
|---|
| 68 | | } |
|---|
| 69 | | Object val = this.register.get(reg); |
|---|
| 70 | | if( val == null ) { |
|---|
| 71 | | return ""; |
|---|
| 72 | | } |
|---|
| 73 | | return val; |
|---|
| 74 | | } |
|---|
| 75 | | |
|---|
| 76 | | /** |
|---|
| 77 | | * Setzt das angegebene Register auf den angegebenen Wert |
|---|
| 78 | | * @param reg Der Name des Registers |
|---|
| 79 | | * @param data Der Wert |
|---|
| 80 | | */ |
|---|
| 81 | | public void setRegister( String reg, Object data ) { |
|---|
| 82 | | if( reg.charAt(0) == '#' ) { |
|---|
| 83 | | reg = reg.substring(1); |
|---|
| 84 | | } |
|---|
| 85 | | this.register.put(reg, data); |
|---|
| 86 | | } |
|---|
| 87 | | |
|---|
| 88 | | /** |
|---|
| 89 | | * Gibt die Liste aller Registernamen zurueck |
|---|
| 90 | | * @return Die Liste aller Registernamen |
|---|
| 91 | | */ |
|---|
| 92 | | public Set<String> getRegisterList() { |
|---|
| 93 | | return this.register.keySet(); |
|---|
| 94 | | } |
|---|
| 95 | | |
|---|
| | 52 | |
|---|
| 96 | 53 | /** |
|---|
| 97 | 54 | * Gibt den Index des zuletzt ausgefuehrten Komamndos zurueck |
|---|
| … | … | |
| 99 | 56 | */ |
|---|
| 100 | 57 | public int getLastCommand() { |
|---|
| 101 | | return this.lastcommand; |
|---|
| | 58 | return (Integer)engineScope.get("__INSTRUCTIONPOINTER"); |
|---|
| 102 | 59 | } |
|---|
| 103 | 60 | |
|---|
| … | … | |
| 107 | 64 | */ |
|---|
| 108 | 65 | public void setLastCommand(int cmd) { |
|---|
| 109 | | this.lastcommand = cmd; |
|---|
| | 66 | engineScope.put("__INSTRUCTIONPOINTER", cmd); |
|---|
| 110 | 67 | } |
|---|
| 111 | 68 | |
|---|
| … | … | |
| 115 | 72 | */ |
|---|
| 116 | 73 | public String getOutput() { |
|---|
| 117 | | return out.toString(); |
|---|
| | 74 | return ((StringWriter)getWriter()).getBuffer().toString(); |
|---|
| 118 | 75 | } |
|---|
| 119 | 76 | |
|---|
| … | … | |
| 123 | 80 | */ |
|---|
| 124 | 81 | public void out( String text ) { |
|---|
| 125 | | out.append(text); |
|---|
| 126 | | } |
|---|
| 127 | | |
|---|
| 128 | | /** |
|---|
| 129 | | * Leert die Scriptausgabe |
|---|
| 130 | | * |
|---|
| 131 | | */ |
|---|
| 132 | | public void clearOutput() { |
|---|
| 133 | | out.setLength(0); |
|---|
| | 82 | try { |
|---|
| | 83 | this.writer.append(text); |
|---|
| | 84 | } |
|---|
| | 85 | catch( IOException e ) { |
|---|
| | 86 | throw new RuntimeException(e); |
|---|
| | 87 | } |
|---|
| 134 | 88 | } |
|---|
| 135 | 89 | |
|---|
| 136 | 90 | private static class ExecData implements Serializable { |
|---|
| | 91 | // Achtung! Jede Veraenderung kann zu inkompatibilitaeten der serialisierten Daten fuehren! |
|---|
| 137 | 92 | private static final long serialVersionUID = 1L; |
|---|
| 138 | 93 | |
|---|
| … | … | |
| 142 | 97 | Map<String,Object> register; |
|---|
| 143 | 98 | int lastcommand; |
|---|
| 144 | | } |
|---|
| 145 | | |
|---|
| 146 | | /** |
|---|
| 147 | | * Fuegt Ausfuehrungsdaten (Register) aus dem angegebenen Kontext in den aktuellen Kontext ein |
|---|
| 148 | | * @param data Der Kontext |
|---|
| 149 | | * @throws Exception |
|---|
| 150 | | */ |
|---|
| 151 | | public void addContextData( ScriptParserContext data ) throws Exception { |
|---|
| 152 | | if( (data != null) && (data.register != null) && (data.register.size() > 0) ) { |
|---|
| 153 | | this.register.putAll(data.register); |
|---|
| 154 | | } |
|---|
| 155 | 99 | } |
|---|
| 156 | 100 | |
|---|
| … | … | |
| 169 | 113 | |
|---|
| 170 | 114 | ScriptParserContext context = new ScriptParserContext(); |
|---|
| 171 | | context.lastcommand = entry.lastcommand; |
|---|
| | 115 | context.engineScope.put("__INSTRUCTIONPOINTER", entry.lastcommand); |
|---|
| 172 | 116 | |
|---|
| 173 | 117 | if( (entry.register != null) && (entry.register.size() > 0) ) { |
|---|
| 174 | | context.register.putAll(entry.register); |
|---|
| | 118 | context.engineScope.putAll(entry.register); |
|---|
| 175 | 119 | } |
|---|
| 176 | 120 | |
|---|
| … | … | |
| 180 | 124 | /** |
|---|
| 181 | 125 | * Schreibt die Ausfuehrungsdaten des ScriptParsers in den angegebenen Stream |
|---|
| | 126 | * @param context Die zu schreibenden Ausfuehrungsdaten |
|---|
| 182 | 127 | * @param out Der OutputStream |
|---|
| 183 | 128 | * @throws Exception |
|---|
| 184 | 129 | */ |
|---|
| 185 | | public void toStream(OutputStream out) throws Exception { |
|---|
| | 130 | public static void toStream(ScriptContext context, OutputStream out) throws Exception { |
|---|
| 186 | 131 | if( out == null ) { |
|---|
| 187 | 132 | return; |
|---|
| 188 | 133 | } |
|---|
| 189 | 134 | // Schiffsdaten nicht speichern |
|---|
| 190 | | Object ship = this.register.remove("_SHIP"); |
|---|
| | 135 | Object ship = context.getBindings(ScriptContext.ENGINE_SCOPE).remove("_SHIP"); |
|---|
| 191 | 136 | |
|---|
| 192 | 137 | ObjectOutputStream oout = new ObjectOutputStream(out); |
|---|
| 193 | 138 | ExecData entry = new ExecData(); |
|---|
| 194 | | entry.register = this.register; |
|---|
| 195 | | entry.lastcommand = lastcommand; |
|---|
| | 139 | entry.register = new HashMap<String,Object>(context.getBindings(ScriptContext.ENGINE_SCOPE)); |
|---|
| | 140 | Integer ip = (Integer)context.getBindings(ScriptContext.ENGINE_SCOPE).get("__INSTRUCTIONPOINTER"); |
|---|
| | 141 | if( ip != null ) { |
|---|
| | 142 | entry.lastcommand = ip; |
|---|
| | 143 | } |
|---|
| 196 | 144 | oout.writeObject(entry); |
|---|
| 197 | 145 | oout.close(); |
|---|
| 198 | 146 | |
|---|
| 199 | | this.register.put("_SHIP", ship); |
|---|
| | 147 | context.getBindings(ScriptContext.ENGINE_SCOPE).put("_SHIP", ship); |
|---|
| 200 | 148 | } |
|---|
| 201 | 149 | } |
|---|
| r144f38e |
r2d1909f |
|
| 95 | 95 | |
|---|
| 96 | 96 | scriptparser.setShip( ship ); |
|---|
| 97 | | scriptparser.executeScript( ship.getScript() ); |
|---|
| | 97 | scriptparser.eval( ship.getScript() ); |
|---|
| 98 | 98 | |
|---|
| 99 | 99 | if( scriptExecData != null ) { |
|---|
| 100 | | scriptparser.getContext().toStream(scriptExecData.setBinaryStream(1)); |
|---|
| | 100 | ScriptParserContext.toStream(scriptparser.getContext(), scriptExecData.setBinaryStream(1)); |
|---|
| 101 | 101 | } |
|---|
| 102 | 102 | else { |
|---|
| 103 | 103 | ByteArrayOutputStream out = new ByteArrayOutputStream(); |
|---|
| 104 | | scriptparser.getContext().toStream(out); |
|---|
| | 104 | ScriptParserContext.toStream(scriptparser.getContext(), out); |
|---|
| 105 | 105 | scriptExecData = Hibernate.createBlob(out.toByteArray()); |
|---|
| 106 | 106 | } |
|---|
| rd9e8da0 |
r2d1909f |
|
| 323 | 323 | scriptparser.setRegister("QUEST", "r"+rquest.getInt("id")); |
|---|
| 324 | 324 | scriptparser.setRegister("SCRIPT", Integer.toString(rquest.getInt("ontick")) ); |
|---|
| 325 | | scriptparser.executeScript(script, "0"); |
|---|
| | 325 | scriptparser.eval(script, "0"); |
|---|
| 326 | 326 | |
|---|
| 327 | 327 | int usequest = Integer.parseInt(scriptparser.getRegister("QUEST")); |
|---|
| 328 | 328 | |
|---|
| 329 | 329 | if( usequest != 0 ) { |
|---|
| 330 | | scriptparser.getContext().toStream(execdata.setBinaryStream(1)); |
|---|
| | 330 | ScriptParserContext.toStream(scriptparser.getContext(), execdata.setBinaryStream(1)); |
|---|
| 331 | 331 | db.prepare("UPDATE quests_running SET execdata=? WHERE id=? ") |
|---|
| 332 | 332 | .update(execdata, rquest.getInt("id")); |
|---|