Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
May 21, 2008, 1:33:42 AM (16 years ago)
Author:
landauf
Message:

big commit, but not much changes in code:

  • put CommandEvaluation into it's own .cc and .h files
  • put some basic ConsoleCommands into ConsoleCommandCompilation.cc and .h
  • created a new class, ConsoleCommand, inheriting from ExecutorStatic, implementing all command-related features that were located in the Executor until now (at the moment only accessLevel_, but more will come - from reto and me)
  • renamed ConsoleCommand-macros to SetConsoleCommand (all related macros were changed the same way)
  • added a new command named "killdelays", that kills all delayed commands. helpful to stop disco ;)
File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/branches/console/src/core/CommandExecutor.cc

    r1276 r1341  
    4444namespace orxonox
    4545{
    46     ConsoleCommandShortcutGeneric(keyword1, createExecutor((FunctorStatic*)0, "set", AccessLevel::User));
    47     ConsoleCommandShortcutGeneric(keyword2, createExecutor((FunctorStatic*)0, "tset", AccessLevel::User));
    48     ConsoleCommandShortcutGeneric(keyword3, createExecutor((FunctorStatic*)0, "bind", AccessLevel::User));
    49 
    50     ConsoleCommandShortcutExtern(source, AccessLevel::None);
    51     ConsoleCommandShortcutExtern(echo, AccessLevel::None);
    52     ConsoleCommandShortcutExtern(puts, AccessLevel::None);
    53 
    54     ConsoleCommandShortcutExtern(read, AccessLevel::None);
    55     ConsoleCommandShortcutExtern(append, AccessLevel::None);
    56     ConsoleCommandShortcutExtern(write, AccessLevel::None);
    57 
    58     void source(const std::string& filename)
    59     {
    60         static std::set<std::string> executingFiles;
    61 
    62         std::set<std::string>::const_iterator it = executingFiles.find(filename);
    63         if (it != executingFiles.end())
    64         {
    65             COUT(1) << "Error: Recurring source command in \"" << filename << "\". Stopped execution." << std::endl;
    66             return;
    67         }
    68 
    69         // Open the file
    70         std::ifstream file;
    71         file.open(filename.c_str(), std::fstream::in);
    72 
    73         if (!file.is_open())
    74         {
    75             COUT(1) << "Error: Couldn't open file \"" << filename << "\"." << std::endl;
    76             return;
    77         }
    78 
    79         executingFiles.insert(filename);
    80 
    81         // Iterate through the file and put the lines into the CommandExecutor
    82         char line[1024];
    83         while (file.good() && !file.eof())
    84         {
    85             file.getline(line, 1024);
    86             CommandExecutor::execute(line);
    87         }
    88 
    89         executingFiles.erase(filename);
    90         file.close();
    91     }
    92 
    93     std::string echo(const std::string& text)
    94     {
    95         std::cout << text << std::endl;
    96         return text;
    97     }
    98 
    99     void puts(bool newline, const std::string& text)
    100     {
    101         if (newline)
    102             COUT(0) << text << std::endl;
    103         else
    104             COUT(0) << text;
    105     }
    106 
    107     void write(const std::string& filename, const std::string& text)
    108     {
    109         std::ofstream file;
    110         file.open(filename.c_str(), std::fstream::out);
    111 
    112         if (!file.is_open())
    113         {
    114             COUT(1) << "Error: Couldn't write to file \"" << filename << "\"." << std::endl;
    115             return;
    116         }
    117 
    118         file << text << std::endl;
    119         file.close();
    120     }
    121 
    122     void append(const std::string& filename, const std::string& text)
    123     {
    124         std::ofstream file;
    125         file.open(filename.c_str(), std::fstream::app);
    126 
    127         if (!file.is_open())
    128         {
    129             COUT(1) << "Error: Couldn't append to file \"" << filename << "\"." << std::endl;
    130             return;
    131         }
    132 
    133         file << text << std::endl;
    134         file.close();
    135     }
    136 
    137     std::string read(const std::string& filename)
    138     {
    139         std::ifstream file;
    140         file.open(filename.c_str(), std::fstream::in);
    141 
    142         if (!file.is_open())
    143         {
    144             COUT(1) << "Error: Couldn't read from file \"" << filename << "\"." << std::endl;
    145             return "";
    146         }
    147 
    148         std::string output = "";
    149         char line[1024];
    150         while (file.good() && !file.eof())
    151         {
    152             file.getline(line, 1024);
    153             output += line;
    154             output += "\n";
    155         }
    156 
    157         file.close();
    158 
    159         return output;
    160     }
    161 
    162 
    163     ///////////////////////
    164     // CommandEvaluation //
    165     ///////////////////////
    166     CommandEvaluation::CommandEvaluation()
    167     {
    168         this->processedCommand_ = "";
    169         this->additionalParameter_ = "";
    170 
    171         this->functionclass_ = 0;
    172         this->configvalueclass_ = 0;
    173         this->shortcut_ = 0;
    174         this->function_ = 0;
    175         this->configvalue_ = 0;
    176         this->key_ = 0;
    177 
    178         this->errorMessage_ = "";
    179         this->state_ = CS_Uninitialized;
    180 
    181         this->bEvaluatedParams_ = false;
    182         this->evaluatedExecutor_ = 0;
    183     }
    184 
    185     KeybindMode CommandEvaluation::getKeybindMode()
    186     {
    187         if (this->state_ == CS_Shortcut_Params || this->state_ == CS_Shortcut_Finished)
    188         {
    189 //            if (this->shortcut_ != 0)
    190 //                return this->shortcut_->getKeybindMode();
    191         }
    192         else if (this->state_ == CS_Function_Params || this->state_ == CS_Function_Finished)
    193         {
    194 //            if (this->function_ != 0)
    195 //                return this->function_->getKeybindMode();
    196         }
    197         else if (this->state_ == CS_ConfigValueType || this->state_ == CS_ConfigValueFinished)
    198         {
    199 //            return KeybindMode::onPress;
    200         }
    201         else if (this->state_ == CS_KeybindCommand || this->state_ == CS_KeybindFinished)
    202         {
    203 //            return KeybindMode::onPress;
    204         }
    205         else
    206         {
    207 //            return KeybindMode::onPress;
    208         }
    209         // FIXME: Had to insert a return statement
    210         return (KeybindMode)0;
    211     }
    212 
    213     bool CommandEvaluation::isValid() const
    214     {
    215         if (this->state_ == CS_Shortcut_Params || this->state_ == CS_Shortcut_Finished)
    216         {
    217             return this->shortcut_;
    218         }
    219         else if (this->state_ == CS_Function_Params || this->state_ == CS_Function_Finished)
    220         {
    221             return (this->functionclass_ && this->function_);
    222         }
    223         else if (this->state_ == CS_ConfigValueType || this->state_ == CS_ConfigValueFinished)
    224         {
    225             return (this->configvalueclass_ && this->configvalue_);
    226         }
    227         else if (this->state_ == CS_KeybindCommand || this->state_ == CS_KeybindFinished)
    228         {
    229             return this->key_;
    230         }
    231         else
    232         {
    233             return false;
    234         }
    235     }
    236 
    237     void CommandEvaluation::evaluateParams()
    238     {
    239         this->bEvaluatedParams_ = false;
    240         this->evaluatedExecutor_ = 0;
    241 
    242         for (unsigned int i = 0; i < MAX_FUNCTOR_ARGUMENTS; i++)
    243             this->param_[i] = MT_null;
    244 
    245         if (this->state_ == CS_Shortcut_Params || this->state_ == CS_Shortcut_Finished)
    246         {
    247             if (this->shortcut_)
    248             {
    249                 if (this->tokens_.size() <= 1)
    250                 {
    251                     if (this->shortcut_->evaluate(this->getAdditionalParameter(), this->param_, " "))
    252                     {
    253                         this->bEvaluatedParams_ = true;
    254                         this->evaluatedExecutor_ = this->shortcut_;
    255                     }
    256                 }
    257                 else if (this->tokens_.size() > 1)
    258                 {
    259                     if (this->shortcut_->evaluate(this->tokens_.subSet(1).join() + this->getAdditionalParameter(), this->param_, " "))
    260                     {
    261                         this->bEvaluatedParams_ = true;
    262                         this->evaluatedExecutor_ = this->shortcut_;
    263                     }
    264                 }
    265             }
    266         }
    267         else if (this->state_ == CS_Function_Params || this->state_ == CS_Function_Finished)
    268         {
    269             if (this->function_)
    270             {
    271                 if (this->tokens_.size() <= 2)
    272                 {
    273                     if (this->function_->evaluate(this->getAdditionalParameter(), this->param_, " "))
    274                     {
    275                         this->bEvaluatedParams_ = true;
    276                         this->evaluatedExecutor_ = this->function_;
    277                     }
    278                 }
    279                 else if (this->tokens_.size() > 2)
    280                 {
    281                     if (this->function_->evaluate(this->tokens_.subSet(2).join() + this->getAdditionalParameter(), this->param_, " "))
    282                     {
    283                         this->bEvaluatedParams_ = true;
    284                         this->evaluatedExecutor_ = this->function_;
    285                     }
    286                 }
    287             }
    288         }
    289     }
    290 
    291     void CommandEvaluation::setEvaluatedParameter(unsigned int index, MultiTypeMath param)
    292     {
    293         if (index >= 0 && index < MAX_FUNCTOR_ARGUMENTS)
    294             this->param_[index] = param;
    295     }
    296 
    297     MultiTypeMath CommandEvaluation::getEvaluatedParameter(unsigned int index) const
    298     {
    299         if (index >= 0 && index < MAX_FUNCTOR_ARGUMENTS)
    300             return this->param_[index];
    301 
    302         return MT_null;
    303     }
    304 
    305     bool CommandEvaluation::hasReturnvalue() const
    306     {
    307         if (this->state_ == CS_Shortcut_Params || this->state_ == CS_Shortcut_Finished)
    308         {
    309             if (this->shortcut_)
    310                 return this->shortcut_->hasReturnvalue();
    311         }
    312         else if (this->state_ == CS_Function_Params || this->state_ == CS_Function_Finished)
    313         {
    314             if (this->function_)
    315                 return this->function_->hasReturnvalue();
    316         }
    317 
    318         return MT_null;
    319     }
    320 
    321     MultiTypeMath CommandEvaluation::getReturnvalue() const
    322     {
    323         if (this->state_ == CS_Shortcut_Params || this->state_ == CS_Shortcut_Finished)
    324         {
    325             if (this->shortcut_)
    326                 return this->shortcut_->getReturnvalue();
    327         }
    328         else if (this->state_ == CS_Function_Params || this->state_ == CS_Function_Finished)
    329         {
    330             if (this->function_)
    331                 return this->function_->getReturnvalue();
    332         }
    333 
    334         return MT_null;
    335     }
    336 
    337 
    338     /////////////////////
    339     // CommandExecutor //
    340     /////////////////////
     46    SetConsoleCommandShortcutGeneric(keyword1, createExecutor((FunctorStatic*)0, "set")).setAccessLevel(AccessLevel::User);
     47    SetConsoleCommandShortcutGeneric(keyword2, createExecutor((FunctorStatic*)0, "tset")).setAccessLevel(AccessLevel::User);
     48    SetConsoleCommandShortcutGeneric(keyword3, createExecutor((FunctorStatic*)0, "bind")).setAccessLevel(AccessLevel::User);
     49
    34150    CommandExecutor& CommandExecutor::getInstance()
    34251    {
     
    35564    }
    35665
    357     Executor& CommandExecutor::addConsoleCommandShortcut(ExecutorStatic* executor)
    358     {
    359         CommandExecutor::getInstance().consoleCommandShortcuts_[executor->getName()] = executor;
    360         CommandExecutor::getInstance().consoleCommandShortcuts_LC_[getLowercase(executor->getName())] = executor;
    361         return (*executor);
     66    ConsoleCommand& CommandExecutor::addConsoleCommandShortcut(ConsoleCommand* command)
     67    {
     68        CommandExecutor::getInstance().consoleCommandShortcuts_[command->getName()] = command;
     69        CommandExecutor::getInstance().consoleCommandShortcuts_LC_[getLowercase(command->getName())] = command;
     70        return (*command);
    36271    }
    36372
     
    36776        @return The executor of the requested console command shortcut
    36877    */
    369     ExecutorStatic* CommandExecutor::getConsoleCommandShortcut(const std::string& name)
    370     {
    371         std::map<std::string, ExecutorStatic*>::const_iterator it = CommandExecutor::getInstance().consoleCommandShortcuts_.find(name);
     78    ConsoleCommand* CommandExecutor::getConsoleCommandShortcut(const std::string& name)
     79    {
     80        std::map<std::string, ConsoleCommand*>::const_iterator it = CommandExecutor::getInstance().consoleCommandShortcuts_.find(name);
    37281        if (it != CommandExecutor::getInstance().consoleCommandShortcuts_.end())
    37382            return (*it).second;
     
    38190        @return The executor of the requested console command shortcut
    38291    */
    383     ExecutorStatic* CommandExecutor::getLowercaseConsoleCommandShortcut(const std::string& name)
    384     {
    385         std::map<std::string, ExecutorStatic*>::const_iterator it = CommandExecutor::getInstance().consoleCommandShortcuts_LC_.find(name);
     92    ConsoleCommand* CommandExecutor::getLowercaseConsoleCommandShortcut(const std::string& name)
     93    {
     94        std::map<std::string, ConsoleCommand*>::const_iterator it = CommandExecutor::getInstance().consoleCommandShortcuts_LC_.find(name);
    38695        if (it != CommandExecutor::getInstance().consoleCommandShortcuts_LC_.end())
    38796            return (*it).second;
     
    1187896    void CommandExecutor::createListOfPossibleShortcuts(const std::string& fragment)
    1188897    {
    1189         for (std::map<std::string, ExecutorStatic*>::const_iterator it = CommandExecutor::getLowercaseConsoleCommandShortcutMapBegin(); it != CommandExecutor::getLowercaseConsoleCommandShortcutMapEnd(); ++it)
     898        for (std::map<std::string, ConsoleCommand*>::const_iterator it = CommandExecutor::getLowercaseConsoleCommandShortcutMapBegin(); it != CommandExecutor::getLowercaseConsoleCommandShortcutMapEnd(); ++it)
    1190899        {
    1191900            if ((*it).first.find(getLowercase(fragment)) == 0 || fragment == "")
     
    1200909    void CommandExecutor::createListOfPossibleFunctions(const std::string& fragment, Identifier* identifier)
    1201910    {
    1202         for (std::map<std::string, ExecutorStatic*>::const_iterator it = identifier->getLowercaseConsoleCommandMapBegin(); it != identifier->getLowercaseConsoleCommandMapEnd(); ++it)
     911        for (std::map<std::string, ConsoleCommand*>::const_iterator it = identifier->getLowercaseConsoleCommandMapBegin(); it != identifier->getLowercaseConsoleCommandMapEnd(); ++it)
    1203912        {
    1204913            if ((*it).first.find(getLowercase(fragment)) == 0 || fragment == "")
     
    1261970    }
    1262971
    1263     ExecutorStatic* CommandExecutor::getExecutorOfPossibleShortcut(const std::string& name)
    1264     {
    1265         std::map<std::string, ExecutorStatic*>::const_iterator it = CommandExecutor::getLowercaseConsoleCommandShortcutMap().find(getLowercase(name));
     972    ConsoleCommand* CommandExecutor::getExecutorOfPossibleShortcut(const std::string& name)
     973    {
     974        std::map<std::string, ConsoleCommand*>::const_iterator it = CommandExecutor::getLowercaseConsoleCommandShortcutMap().find(getLowercase(name));
    1266975        if (it != CommandExecutor::getLowercaseConsoleCommandShortcutMapEnd())
    1267976            return (*it).second;
     
    1270979    }
    1271980
    1272     ExecutorStatic* CommandExecutor::getExecutorOfPossibleFunction(const std::string& name, Identifier* identifier)
    1273     {
    1274         std::map<std::string, ExecutorStatic*>::const_iterator it = identifier->getLowercaseConsoleCommandMap().find(getLowercase(name));
     981    ConsoleCommand* CommandExecutor::getExecutorOfPossibleFunction(const std::string& name, Identifier* identifier)
     982    {
     983        std::map<std::string, ConsoleCommand*>::const_iterator it = identifier->getLowercaseConsoleCommandMap().find(getLowercase(name));
    1275984        if (it != identifier->getLowercaseConsoleCommandMapEnd())
    1276985            return (*it).second;
     
    13191028    }
    13201029
    1321     std::string CommandExecutor::dump(const ExecutorStatic* executor)
     1030    std::string CommandExecutor::dump(const ConsoleCommand* command)
    13221031    {
    13231032        std::string output = "";
    1324         for (unsigned int i = 0; i < executor->getParamCount(); i++)
     1033        for (unsigned int i = 0; i < command->getParamCount(); i++)
    13251034        {
    13261035            if (i != 0)
    13271036                output += " ";
    13281037
    1329             if (executor->defaultValueSet(i))
     1038            if (command->defaultValueSet(i))
    13301039                output += "[";
    13311040            else
    13321041                output += "{";
    13331042
    1334             output += executor->getTypenameParam(i);
    1335 
    1336             if (executor->defaultValueSet(i))
    1337                 output += "=" + executor->getDefaultValue(i).toString() + "]";
     1043            output += command->getTypenameParam(i);
     1044
     1045            if (command->defaultValueSet(i))
     1046                output += "=" + command->getDefaultValue(i).toString() + "]";
    13381047            else
    13391048                output += "}";
Note: See TracChangeset for help on using the changeset viewer.