Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 6736


Ignore:
Timestamp:
Apr 16, 2010, 12:11:57 PM (15 years ago)
Author:
rgrieder
Message:

All CEGUI calls that can execute Lua code should be protected against ScriptException.
Although this is bad design (can leave the game in an undefined state), the game doesn't terminate just because of simple script problem.
When the GUI code is more mature, we should probably remove this again.

Location:
code/branches/gamestates2/src/libraries/core
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • code/branches/gamestates2/src/libraries/core/GUIManager.cc

    r6722 r6736  
    3030#include "GUIManager.h"
    3131
     32#include <boost/bind.hpp>
    3233#include <memory>
    3334extern "C" {
     
    175176    {
    176177        assert(guiSystem_);
    177         guiSystem_->injectTimePulse(time.getDeltaTime());
     178        this->protectedCall(boost::bind(&CEGUI::System::injectTimePulse, _1, time.getDeltaTime()));
    178179    }
    179180
     
    298299    void GUIManager::keyPressed(const KeyEvent& evt)
    299300    {
    300         guiSystem_->injectKeyDown(evt.getKeyCode());
    301         guiSystem_->injectChar(evt.getText());
    302     }
     301        this->protectedCall(boost::bind(&CEGUI::System::injectKeyDown, _1, evt.getKeyCode()));
     302        this->protectedCall(boost::bind(&CEGUI::System::injectChar, _1, evt.getText()));
     303    }
     304
    303305    void GUIManager::keyReleased(const KeyEvent& evt)
    304306    {
    305         guiSystem_->injectKeyUp(evt.getKeyCode());
     307        this->protectedCall(boost::bind(&CEGUI::System::injectKeyUp, _1, evt.getKeyCode()));
    306308    }
    307309
     
    317319    void GUIManager::buttonPressed(MouseButtonCode::ByEnum id)
    318320    {
    319         try
    320         {
    321             guiSystem_->injectMouseButtonDown(convertButton(id));
    322         }
    323         catch (CEGUI::ScriptException& ex)
    324         {
    325             // We simply ignore the exception and proceed
    326             COUT(1) << ex.getMessage() << std::endl;
    327         }
     321        this->protectedCall(boost::bind(&CEGUI::System::injectMouseButtonDown, _1, convertButton(id)));
    328322    }
    329323
     
    339333    void GUIManager::buttonReleased(MouseButtonCode::ByEnum id)
    340334    {
    341         try
    342         {
    343             guiSystem_->injectMouseButtonUp(convertButton(id));
    344         }
    345         catch (CEGUI::ScriptException& ex)
    346         {
    347             // We simply ignore the exception and proceed
    348             COUT(1) << ex.getMessage() << std::endl;
    349         }
     335        this->protectedCall(boost::bind(&CEGUI::System::injectMouseButtonUp, _1, convertButton(id)));
    350336    }
    351337
    352338    void GUIManager::mouseMoved(IntVector2 abs, IntVector2 rel, IntVector2 clippingSize)
    353339    {
    354         guiSystem_->injectMousePosition(static_cast<float>(abs.x), static_cast<float>(abs.y));
    355     }
     340        this->protectedCall(boost::bind(&CEGUI::System::injectMousePosition, _1, (float)abs.x, (float)abs.y));
     341    }
     342
    356343    void GUIManager::mouseScrolled(int abs, int rel)
    357344    {
    358         guiSystem_->injectMouseWheelChange(static_cast<float>(rel));
     345        this->protectedCall(boost::bind(&CEGUI::System::injectMouseWheelChange, _1, (float)rel));
    359346    }
    360347
     
    393380    }
    394381
     382    /** Executes a CEGUI function normally, but catches CEGUI::ScriptException.
     383        When a ScriptException occurs, the error message will be displayed and
     384        the program carries on.
     385    @remarks
     386        The exception behaviour may pose problems if the code is not written
     387        exception-safe (and you can forget about that in Lua). The program might
     388        be left in an undefined state. But otherwise one script error would
     389        terminate the whole program...
     390    @note
     391        Your life gets easier if you use boost::bind to create the object/function.
     392    @param function
     393        Any callable object/function that takes this->guiSystem_ as its only parameter.
     394    @return
     395        True if input was handled, false otherwise. A caught exception yields true.
     396    */
     397    template <typename FunctionType>
     398    bool GUIManager::protectedCall(FunctionType function)
     399    {
     400        try
     401        {
     402            return function(this->guiSystem_);
     403        }
     404        catch (CEGUI::ScriptException& ex)
     405        {
     406            // Display the error and proceed. See @remarks why this can be dangerous.
     407            COUT(1) << ex.getMessage() << std::endl;
     408            return true;
     409        }
     410    }
     411
    395412    void GUIManager::subscribeEventHelper(CEGUI::Window* window, const std::string& event, const std::string& function)
    396413    {
  • code/branches/gamestates2/src/libraries/core/GUIManager.h

    r6722 r6736  
    101101        void executeCode(const std::string& str);
    102102
     103        template <typename FunctionType>
     104        bool protectedCall(FunctionType function);
     105
    103106        // keyHandler functions
    104107        void keyPressed (const KeyEvent& evt);
Note: See TracChangeset for help on using the changeset viewer.