Changeset 2d1909f5e2b47ba4dcaeb4fd0fb06671d95c8e0b

Show
Ignore:
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
  • src/net/driftingsouls/ds2/server/AdminCommands.java

    r953ed23 r2d1909f  
    334334                                scriptparser.setRegister("QUEST", "r"+rqid); 
    335335                                 
    336                                 scriptparser.executeScript( runningquest.getString("uninstall"), "0" ); 
     336                                scriptparser.eval( runningquest.getString("uninstall"), "0" ); 
    337337                        }        
    338338                         
  • src/net/driftingsouls/ds2/server/modules/SchiffController.java

    rc30c5b7 r2d1909f  
    907907                } 
    908908 
    909                 scriptparser.executeScript(script.getScript(), execparameter); 
     909                scriptparser.eval(script.getScript(), execparameter); 
    910910         
    911911                usequest = scriptparser.getRegister("QUEST"); 
     
    913913                if( !usequest.equals("") ) { 
    914914                        try { 
    915                                 scriptparser.getContext().toStream(execdata.setBinaryStream(1)); 
     915                                ScriptParserContext.toStream(scriptparser.getContext(), execdata.setBinaryStream(1)); 
    916916                        } 
    917917                        catch( Exception e ) { 
  • src/net/driftingsouls/ds2/server/modules/UeberController.java

    rd220799 r2d1909f  
    182182                scriptparser.setRegister("USER", Integer.toString(getUser().getID())); 
    183183                scriptparser.setRegister("QUEST", "r"+questid); 
    184                 scriptparser.executeScript(":0\n!ENDQUEST\n!QUIT","0"); 
     184                scriptparser.eval(":0\n!ENDQUEST\n!QUIT","0"); 
    185185                 
    186186                redirect();      
  • src/net/driftingsouls/ds2/server/modules/admin/PlayerDelete.java

    rf034a00 r2d1909f  
    105105                                scriptparser.setRegister("USER", userid); 
    106106                                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"); 
    108108                        } 
    109109                        catch( Exception e ) { 
  • src/net/driftingsouls/ds2/server/modules/admin/QuestsQuick.java

    rf034a00 r2d1909f  
    9595                                scriptparser.setRegister("USER", rquest.getInt("userid")); 
    9696                                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"); 
    9898                                echo.append("Beende Quest bei Spieler "+rquest.getInt("userid")+"<br />\n"); 
    9999                        } 
  • src/net/driftingsouls/ds2/server/scripting/QuestFunctions.java

    r3c6b450 r2d1909f  
    2525import java.util.List; 
    2626import java.util.Map; 
     27 
     28import javax.script.ScriptContext; 
    2729 
    2830import net.driftingsouls.ds2.server.ContextCommon; 
     
    687689                                 
    688690                                scriptparser.setContext(new ScriptParserContext()); 
    689                                 scriptparser.executeScript( runningdata.getString("uninstall"), "0" ); 
     691                                scriptparser.eval( runningdata.getString("uninstall"), "0" ); 
    690692                                 
    691693                                scriptparser.setContext(context); 
     
    11751177                                try { 
    11761178                                        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) 
    11791181                                        ); 
    11801182                                        scriptparser.setRegister("QUEST","r"+questdata.getInt("id")); 
     
    12201222                                try { 
    12211223                                        Blob blob = questdata.getBlob("execdata"); 
    1222                                         scriptparser.getContext().toStream(blob.setBinaryStream(1)); 
     1224                                        ScriptParserContext.toStream(scriptparser.getContext(), blob.setBinaryStream(1)); 
    12231225                                        db.prepare("UPDATE quests_running SET execdata=? WHERE id=? ") 
    12241226                                                .update(blob, questdata.getInt("id")); 
  • src/net/driftingsouls/ds2/server/scripting/Quests.java

    rf034a00 r2d1909f  
    142142                scriptparser.setRegister("SCRIPT", Integer.toString(usescript));         
    143143                scriptparser.setRegister("LOCKEXEC", "1");       
    144                 scriptparser.executeScript(script.getString("script"), execparameter); 
     144                scriptparser.eval(script.getString("script"), execparameter); 
    145145                 
    146146                runningdata.free(); 
     
    268268                } 
    269269                scriptparser.setRegister("SCRIPT", Integer.toString(usescript));         
    270                 scriptparser.executeScript(script.getString("script"), execparameter); 
     270                scriptparser.eval(script.getString("script"), execparameter); 
    271271                 
    272272                usequest = scriptparser.getRegister("QUEST"); 
     
    287287                                try { 
    288288                                        Blob execdata = runningdata.getBlob("execdata"); 
    289                                         scriptparser.getContext().toStream( execdata.setBinaryStream(1) ); 
     289                                        ScriptParserContext.toStream(scriptparser.getContext(), execdata.setBinaryStream(1)); 
    290290                                        db.prepare("UPDATE quests_running SET execdata=? WHERE id=? ") 
    291291                                                .update(execdata, runningdata.getInt("id")); 
  • src/net/driftingsouls/ds2/server/scripting/ScriptParser.java

    refe50c3 r2d1909f  
    2727import java.util.Stack; 
    2828 
     29import javax.script.ScriptContext; 
     30 
    2931import net.driftingsouls.ds2.server.comm.PM; 
    3032import net.driftingsouls.ds2.server.entities.User; 
     
    267269         * @return <code>true</code>, falls das Kommando neu registriert wurde und noch nicht registriert war 
    268270         */ 
    269         public boolean registerCommand( String command, SPFunction function, Args ... args ) { 
     271        protected boolean registerCommand( String command, SPFunction function, Args ... args ) { 
    270272                command = "!"+command; 
    271273                if( !funcregister.containsKey(command) ) { 
     
    290292         * @param txt Der zu loggende String 
    291293         */ 
    292         public void log( String txt ) { 
     294        protected void log( String txt ) { 
    293295                logFunction.log(txt); 
    294296        } 
     
    298300         * 
    299301         */ 
    300         public void startLogging() { 
     302        private void startLogging() { 
    301303                logFunction.start(); 
    302304        } 
     
    306308         * 
    307309         */ 
    308         public void stopLogging() { 
     310        private void stopLogging() { 
    309311                logFunction.stop(); 
    310312        } 
     
    315317         */ 
    316318        public void setShip( Ship ship ) { 
    317                 context.setRegister("_SHIP", ship); 
     319                context.getBindings(ScriptContext.ENGINE_SCOPE).put("_SHIP", ship); 
    318320        } 
    319321         
     
    323325         */ 
    324326        public Ship getShip() { 
    325                 return (Ship)context.getRegisterObject("_SHIP"); 
     327                return (Ship)context.getBindings(ScriptContext.ENGINE_SCOPE).get("_SHIP"); 
    326328        } 
    327329                 
     
    332334         */ 
    333335        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(); 
    335346        } 
    336347         
     
    341352         */ 
    342353        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); 
    344359        } 
    345360         
     
    350365         */ 
    351366        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); 
    353372        } 
    354373         
     
    450469         * @return Das Ergebnis 
    451470         */ 
    452         public String evalTerm( String term ) { 
     471        private String evalTerm( String term ) { 
    453472                int index = 0; 
    454473                Stack<TermElement> stack = new Stack<TermElement>(); 
     
    531550         * @return Der Inhalt oder <code>null</code> 
    532551         */ 
    533         public String getParameter( int number ) { 
     552        protected String getParameter( int number ) { 
    534553                if( (number > -1) && (addparameterlist.size() > number) ) {  
    535554                        return this.addparameterlist.get(number); 
     
    558577         * @param script das Script 
    559578         */ 
    560         public void executeScript(String script) { 
    561                 executeScript(script, ""); 
    562         } 
    563          
     579        public void eval(String script) { 
     580                eval(script, ""); 
     581        } 
     582 
    564583        /** 
    565584         * Fuehrt das angegebene Script aus 
     
    567586         * @param parameter Ausfuehrungsparameter 
    568587         */ 
    569         public void executeScript(String script, String parameter) { 
     588        public void eval(String script, String parameter) { 
    570589                this.startLogging(); 
    571590                 
     
    648667                } 
    649668                 
    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                  
    669669                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                 
    670689                        // Und los gehts! 
    671690                        while( true ) { 
     
    842861                                        if( dump.equals("register") ) { 
    843862                                                this.log("################# register ###################\n"); 
    844                                                 for( String reg : context.getRegisterList() ) { 
     863                                                for( String reg : context.getBindings(ScriptContext.ENGINE_SCOPE).keySet() ) { 
    845864                                                        if( reg.equals("_SHIP") ) { 
    846865                                                                continue; 
    847866                                                        } 
    848                                                         this.log(reg+" ("+context.getRegister(reg)+"), "); 
     867                                                        this.log(reg+" ("+context.getBindings(ScriptContext.ENGINE_SCOPE).get(reg)+"), "); 
    849868                                                } 
    850869                                                this.log("\n\n"); 
     
    886905                                        this.log("\n\n"); 
    887906                                        this.log("################# register ###################\n"); 
    888                                         for( String reg : context.getRegisterList() ) { 
     907                                        for( String reg : context.getBindings(ScriptContext.ENGINE_SCOPE).keySet() ) { 
    889908                                                if( reg.equals("_SHIP") ) { 
    890909                                                        continue; 
    891910                                                } 
    892                                                 this.log(reg+" ("+context.getRegister(reg)+"), "); 
     911                                                this.log(reg+" ("+context.getBindings(ScriptContext.ENGINE_SCOPE).get(reg)+"), "); 
    893912                                        } 
    894913                                        this.log("\n\n"); 
     
    902921                        } 
    903922                } 
    904                 catch( RuntimeException e ) { 
     923                catch( Exception e ) { 
    905924                        if( this.namespace == NameSpace.ACTION ) { 
    906925                                StringBuilder text = new StringBuilder(); 
     
    920939                        } 
    921940                        else { 
    922                                 throw e; 
     941                                if( e instanceof RuntimeException ) { 
     942                                        throw (RuntimeException)e; 
     943                                } 
     944                                 
     945                                throw new RuntimeException("Script execution failed", e); 
    923946                        } 
    924947                } 
  • src/net/driftingsouls/ds2/server/scripting/ScriptParserContext.java

    r8f2568b r2d1909f  
    1919package net.driftingsouls.ds2.server.scripting; 
    2020 
     21import java.io.IOException; 
    2122import java.io.InputStream; 
    2223import java.io.ObjectInputStream; 
     
    2425import java.io.OutputStream; 
    2526import java.io.Serializable; 
     27import java.io.StringWriter; 
    2628import java.util.HashMap; 
    2729import java.util.Map; 
    28 import java.util.Set; 
     30 
     31import javax.script.ScriptContext; 
     32import javax.script.SimpleScriptContext; 
    2933 
    3034/** 
     
    3539 * 
    3640 */ 
    37 public class ScriptParserContext { 
    38         private Map<String,Object> register; 
    39         private int lastcommand; 
    40         private StringBuffer out = new StringBuffer(); 
    41          
     41public class ScriptParserContext extends SimpleScriptContext { 
    4242        /** 
    4343         * Konstruktor 
     
    4545         */ 
    4646        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); 
    4951        } 
    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 
    9653        /** 
    9754         * Gibt den Index des zuletzt ausgefuehrten Komamndos zurueck 
     
    9956         */ 
    10057        public int getLastCommand() { 
    101                 return this.lastcommand
     58                return (Integer)engineScope.get("__INSTRUCTIONPOINTER")
    10259        } 
    10360         
     
    10764         */ 
    10865        public void setLastCommand(int cmd) { 
    109                 this.lastcommand = cmd
     66                engineScope.put("__INSTRUCTIONPOINTER", cmd)
    11067        } 
    11168         
     
    11572         */ 
    11673        public String getOutput() { 
    117                 return out.toString(); 
     74                return ((StringWriter)getWriter()).getBuffer().toString(); 
    11875        } 
    11976         
     
    12380         */ 
    12481        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                } 
    13488        } 
    13589         
    13690        private static class ExecData implements Serializable { 
     91                // Achtung! Jede Veraenderung kann zu inkompatibilitaeten der serialisierten Daten fuehren! 
    13792                private static final long serialVersionUID = 1L; 
    13893                 
     
    14297                Map<String,Object> register; 
    14398                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                 } 
    15599        } 
    156100         
     
    169113                 
    170114                ScriptParserContext context = new ScriptParserContext(); 
    171                 context.lastcommand = entry.lastcommand
     115                context.engineScope.put("__INSTRUCTIONPOINTER", entry.lastcommand)
    172116                 
    173117                if( (entry.register != null) && (entry.register.size() > 0) ) { 
    174                         context.register.putAll(entry.register); 
     118                        context.engineScope.putAll(entry.register); 
    175119                } 
    176120                 
     
    180124        /** 
    181125         * Schreibt die Ausfuehrungsdaten des ScriptParsers in den angegebenen Stream 
     126         * @param context Die zu schreibenden Ausfuehrungsdaten 
    182127         * @param out Der OutputStream 
    183128         * @throws Exception 
    184129         */ 
    185         public void toStream(OutputStream out) throws Exception { 
     130        public static void toStream(ScriptContext context, OutputStream out) throws Exception { 
    186131                if( out == null ) { 
    187132                        return; 
    188133                } 
    189134                // Schiffsdaten nicht speichern 
    190                 Object ship = this.register.remove("_SHIP"); 
     135                Object ship = context.getBindings(ScriptContext.ENGINE_SCOPE).remove("_SHIP"); 
    191136                 
    192137                ObjectOutputStream oout = new ObjectOutputStream(out); 
    193138                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                } 
    196144                oout.writeObject(entry); 
    197145                oout.close(); 
    198146                 
    199                 this.register.put("_SHIP", ship); 
     147                context.getBindings(ScriptContext.ENGINE_SCOPE).put("_SHIP", ship); 
    200148        } 
    201149} 
  • src/net/driftingsouls/ds2/server/tick/regular/NPCScriptTick.java

    r144f38e r2d1909f  
    9595                                         
    9696                                        scriptparser.setShip( ship ); 
    97                                         scriptparser.executeScript( ship.getScript() ); 
     97                                        scriptparser.eval( ship.getScript() ); 
    9898                                         
    9999                                        if( scriptExecData != null ) { 
    100                                                 scriptparser.getContext().toStream(scriptExecData.setBinaryStream(1)); 
     100                                                ScriptParserContext.toStream(scriptparser.getContext(), scriptExecData.setBinaryStream(1)); 
    101101                                        } 
    102102                                        else { 
    103103                                                ByteArrayOutputStream out = new ByteArrayOutputStream(); 
    104                                                 scriptparser.getContext().toStream(out); 
     104                                                ScriptParserContext.toStream(scriptparser.getContext(), out); 
    105105                                                scriptExecData = Hibernate.createBlob(out.toByteArray()); 
    106106                                        } 
  • src/net/driftingsouls/ds2/server/tick/regular/RestTick.java

    rd9e8da0 r2d1909f  
    323323                                        scriptparser.setRegister("QUEST", "r"+rquest.getInt("id")); 
    324324                                        scriptparser.setRegister("SCRIPT", Integer.toString(rquest.getInt("ontick")) );          
    325                                         scriptparser.executeScript(script, "0"); 
     325                                        scriptparser.eval(script, "0"); 
    326326                                                 
    327327                                        int usequest = Integer.parseInt(scriptparser.getRegister("QUEST")); 
    328328                                                 
    329329                                        if( usequest != 0 ) { 
    330                                                 scriptparser.getContext().toStream(execdata.setBinaryStream(1));        
     330                                                ScriptParserContext.toStream(scriptparser.getContext(), execdata.setBinaryStream(1)); 
    331331                                                db.prepare("UPDATE quests_running SET execdata=? WHERE id=? ") 
    332332                                                        .update(execdata, rquest.getInt("id"));