Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Aug 17, 2009, 4:37:10 PM (15 years ago)
Author:
rgrieder
Message:
  • Implemented file management via resource manager and loading of resource locations via XML. Changes made:
    • SoundManager loads via memory stream rather than via file
    • Loader uses LuaState::includeFile() to load an XML file and passes the lua tag remover function to its LuaState.
    • ConfigFileManager still loads with hard paths because the files are required before Ogre gets created
  • Renamed LuaBind to LuaState, deSingletonised it and added new features:
    • doFile(), doString(), includeFile(), includeString() where include will preparse the string with a function provided with LuaState::setIncludeParser
    • Moved lua tags replace function to Loader (since it's actually an XML related task)
    • Using data_path/lua/LuaInitScript.lua to provide the following functions
      • logMessage(level, message)
      • doFile, dofile, include (all working with relative paths but within the same resource group)
  • Modified Script class to work with LuaState and fixed its XML Loader
  • Adjusted all level and include files (both "include" and "dofile" lua commands)
Location:
code/branches/resource2
Files:
2 added
1 deleted
41 edited
1 copied
2 moved

Legend:

Unmodified
Added
Removed
  • code/branches/resource2/data/levels/empty_level.oxw

    r5528 r5654  
    11<?lua
    2   include("overlay/stats.oxo")
    3   include("overlay/hudtemplates3.oxo")
     2  include("stats.oxo")
     3  include("hudtemplates3.oxo")
    44?>
    55
    66<?lua
    7   include("levels/templates/spaceship_assff.oxt")
    8   include("levels/templates/spaceship_pirate.oxt")
     7  include("templates/spaceship_assff.oxt")
     8  include("templates/spaceship_pirate.oxt")
    99?>
    1010
  • code/branches/resource2/data/levels/gametype_asteroids.oxw

    r5556 r5654  
    11<?lua
    2   include("overlay/hudtemplates3.oxo")
    3   include("overlay/stats.oxo")
    4   include("levels/templates/spaceship_assff.oxt")
     2  include("hudtemplates3.oxo")
     3  include("stats.oxo")
     4  include("templates/spaceship_assff.oxt")
    55?>
    66
  • code/branches/resource2/data/levels/gametype_underattack.oxw

    r5572 r5654  
    11<?lua
    2   include("overlay/hudtemplates3.oxo")
    3   include("overlay/stats.oxo")
    4   include("overlay/underattackhud.oxo")
    5   include("levels/templates/spaceship_assff.oxt")
     2  include("hudtemplates3.oxo")
     3  include("stats.oxo")
     4  include("underattackhud.oxo")
     5  include("templates/spaceship_assff.oxt")
    66?>
    77
  • code/branches/resource2/data/levels/old/CuboidSpaceStation.oxw

    r5528 r5654  
    11<?lua
    2         include("overlay/hudtemplates3.oxo")
     2        include("hudtemplates3.oxo")
    33?>
    44
    55<?lua
    6         include("levels/templates/spaceship_assff.oxt")
     6        include("templates/spaceship_assff.oxt")
    77?>
    88
     
    2020<!-------------------------------------------------------------------------------------------------------------------------------------------------------------------------->
    2121<?lua
    22         dofile("../../media/levels/includes/CuboidSpaceStation.lua")
     22        dofile("includes/CuboidSpaceStation.lua")
    2323?>
    2424
  • code/branches/resource2/data/levels/old/physicstest2.oxw

    r5528 r5654  
    11<?lua
    2   include("levels/old/spaceshiptemplates_physics.oxw")
     2  include("spaceshiptemplates_physics.oxw")
    33?>
    44<?lua
    5   include("overlay/hudtemplates3.oxo")
     5  include("hudtemplates3.oxo")
    66?>
    77
  • code/branches/resource2/data/levels/old/planettest.oxw

    r5528 r5654  
    11<?lua
    2   include("overlay/hudtemplates3.oxo")
     2  include("hudtemplates3.oxo")
    33?>
    44
    55<?lua
    6   include("levels/templates/spaceship_assff.oxt")
     6  include("templates/spaceship_assff.oxt")
    77?>
    88
  • code/branches/resource2/data/levels/old/presentation_physics.oxw

    r5528 r5654  
    11<?lua
    2   include("overlay/hudtemplates3.oxo")
    3   include("overlay/stats.oxo")
    4   include("levels/templates/spaceship_assff.oxt")
     2  include("hudtemplates3.oxo")
     3  include("stats.oxo")
     4  include("templates/spaceship_assff.oxt")
    55  math.randomseed(344)
    66?>
  • code/branches/resource2/data/levels/old/princessaeryn.oxw

    r5528 r5654  
    11<?lua
    2   include("overlay/hudtemplates3.oxo")
    3   include("overlay/stats.oxo")
    4   include("levels/templates/spaceship_assff.oxt")
     2  include("hudtemplates3.oxo")
     3  include("stats.oxo")
     4  include("templates/spaceship_assff.oxt")
    55?>
    66
    77<?lua
    8         dofile("../../media/levels/includes/CuboidSpaceStation.lua")
     8        dofile("includes/CuboidSpaceStation.lua")
    99?>
    1010
  • code/branches/resource2/data/levels/old/questsystem.oxw

    r5528 r5654  
    11<?lua
    2   include("overlay/hudtemplates3.oxo")
    3   include("overlay/stats.oxo")
    4   include("levels/templates/spaceship_assff.oxt")
     2  include("hudtemplates3.oxo")
     3  include("stats.oxo")
     4  include("templates/spaceship_assff.oxt")
    55?>
    66
  • code/branches/resource2/data/levels/old/sample4.oxw

    r5528 r5654  
    11<?lua
    2   include("overlay/hudtemplates3.oxo")
     2  include("hudtemplates3.oxo")
    33?>
    44
    55<?lua
    6   include("levels/templates/spaceship_assff.oxt")
     6  include("templates/spaceship_assff.oxt")
    77?>
    88
  • code/branches/resource2/data/levels/pickuptest.oxw

    r5529 r5654  
    11<?lua
    2   include("overlay/hudtemplates3.oxo")
    3   include("overlay/stats.oxo")
    4   include("levels/templates/spaceship_assff.oxt")
     2  include("hudtemplates3.oxo")
     3  include("stats.oxo")
     4  include("templates/spaceship_assff.oxt")
    55?>
    66
  • code/branches/resource2/data/levels/presentation.oxw

    r5537 r5654  
    11<?lua
    2   include("overlay/hudtemplates3.oxo")
    3   include("overlay/stats.oxo")
    4   include("levels/templates/spaceship_assff.oxt")
     2  include("hudtemplates3.oxo")
     3  include("stats.oxo")
     4  include("templates/spaceship_assff.oxt")
    55?>
    66
  • code/branches/resource2/data/levels/presentation09.oxw

    r5565 r5654  
    11<?lua
    2   include("overlay/hudtemplates3.oxo")
    3   include("overlay/stats.oxo")
    4   include("levels/templates/spaceship_assff.oxt")
    5   include("levels/templates/spaceship_H2.oxt")
     2  include("hudtemplates3.oxo")
     3  include("stats.oxo")
     4  include("templates/spaceship_assff.oxt")
     5  include("templates/spaceship_H2.oxt")
    66?>
    77
  • code/branches/resource2/data/levels/presentation09b.oxw

    r5575 r5654  
    11<?lua
    2   include("overlay/hudtemplates3.oxo")
    3   include("overlay/stats.oxo")
    4   include("levels/templates/spaceship_assff.oxt")
    5   include("levels/templates/spaceship_H2.oxt")
    6   include("levels/templates/spaceship_pirate.oxt")
     2  include("hudtemplates3.oxo")
     3  include("stats.oxo")
     4  include("templates/spaceship_assff.oxt")
     5  include("templates/spaceship_H2.oxt")
     6  include("templates/spaceship_pirate.oxt")
    77?>
    88
  • code/branches/resource2/data/levels/presentation_dm.oxw

    r5550 r5654  
    11<?lua
    2   include("overlay/hudtemplates3.oxo")
    3   include("overlay/stats.oxo")
    4   include("levels/templates/spaceship_assff.oxt")
    5   include("levels/templates/spaceship_H2.oxt")
     2  include("hudtemplates3.oxo")
     3  include("stats.oxo")
     4  include("templates/spaceship_assff.oxt")
     5  include("templates/spaceship_H2.oxt")
    66?>
    77
  • code/branches/resource2/data/levels/presentation_pong.oxw

    r5528 r5654  
    11<?lua
    2   include("overlay/hudtemplates3.oxo")
    3   include("overlay/stats.oxo")
    4   include("overlay/ponghud.oxo")
     2  include("hudtemplates3.oxo")
     3  include("stats.oxo")
     4  include("ponghud.oxo")
    55?>
    66
     
    9191
    9292  <?lua
    93     dofile("../../media/levels/includes/CuboidSpaceStation.lua")
     93    dofile("includes/CuboidSpaceStation.lua")
    9494  ?>
    9595
  • code/branches/resource2/data/levels/teambasematchlevel.oxw

    r5572 r5654  
    11<?lua
    2   include("overlay/hudtemplates3.oxo")
    3   include("overlay/stats.oxo")
    4   include("overlay/teambasematchhud.oxo")
    5   include("levels/templates/spaceship_assff.oxt")
    6   include("levels/templates/spaceship_pirate.oxt")
     2  include("hudtemplates3.oxo")
     3  include("stats.oxo")
     4  include("teambasematchhud.oxo")
     5  include("templates/spaceship_assff.oxt")
     6  include("templates/spaceship_pirate.oxt")
    77?>
    88
  • code/branches/resource2/data/levels/teamdeathmatch.oxw

    r5534 r5654  
    11<?lua
    2   include("overlay/hudtemplates3.oxo")
    3   include("overlay/stats.oxo")
    4   include("levels/templates/spaceship_assff.oxt")
    5   include("levels/templates/spaceship_H2.oxt")
    6   include("levels/templates/spaceship_pirate.oxt")
     2  include("hudtemplates3.oxo")
     3  include("stats.oxo")
     4  include("templates/spaceship_assff.oxt")
     5  include("templates/spaceship_H2.oxt")
     6  include("templates/spaceship_pirate.oxt")
    77?>
    88
  • code/branches/resource2/data/levels/templates/spaceship_H2.oxt

    r5573 r5654  
    2323
    2424<?lua
    25   include("levels/includes/weaponsettings_H2.oxi")
     25  include("includes/weaponsettings_H2.oxi")
    2626?>
    2727
  • code/branches/resource2/data/levels/templates/spaceship_assff.oxt

    r5550 r5654  
    3535    </collisionShapes>
    3636<?lua
    37   include("levels/includes/weaponsettings3.oxi")
     37  include("includes/weaponsettings3.oxi")
    3838?>
    3939  </SpaceShip>
  • code/branches/resource2/data/levels/templates/spaceship_pirate.oxt

    r5528 r5654  
    3838
    3939<?lua
    40   include("levels/includes/weaponsettings_pirate.oxi")
     40  include("includes/weaponsettings_pirate.oxi")
    4141?>
    4242  </SpaceShip>
  • code/branches/resource2/data/levels/tutorial.oxw

    r5528 r5654  
    11<?lua
    2   include("overlay/hudtemplates3.oxo")
    3   include("overlay/stats.oxo")
    4   include("levels/templates/spaceship_assff.oxt")
     2  include("hudtemplates3.oxo")
     3  include("stats.oxo")
     4  include("templates/spaceship_assff.oxt")
    55?>
    66
  • code/branches/resource2/data/levels/waypoints.oxw

    r5560 r5654  
    11<?lua
    2   include("overlay/hudtemplates3.oxo")
    3   include("overlay/stats.oxo")
    4   include("levels/templates/spaceship_assff.oxt")
    5   include("levels/templates/spaceship_H2.oxt")
    6   include("levels/templates/spaceship_pirate.oxt")
     2  include("hudtemplates3.oxo")
     3  include("stats.oxo")
     4  include("templates/spaceship_assff.oxt")
     5  include("templates/spaceship_H2.oxt")
     6  include("templates/spaceship_pirate.oxt")
    77?>
    88
  • code/branches/resource2/data/resources.oxr

    r5644 r5654  
    1 <ResourceGroup name = "General" >
    2     <ResourceLocation path = overlay />
    3     <ResourceLocation path = particle />
    4     <ResourceLocation path = defaultConfig />
    5     <ResourceLocation path = levels />
    6     <ResourceLocation path = tcl />
    7     <ResourceLocation
    8       path = test.zip
    9       archive = Zip
    10       recursive = true
    11     />
    12 </ResourceGroup>
    13 
    14 <ResourceGroup name = "GUI" >
    15     <ResourceLocation path = gui/configs />
    16     <ResourceLocation path = gui/layouts />
    17     <ResourceLocation path = gui/schemas />
    18     <ResourceLocation path = gui/scripts />
    19 </ResourceGroup>
     1<ResourceCollection resourceGroup = "General" >
     2    <ResourceLocation path = "levels" />
     3    <ResourceLocation path = "lua" />
     4    <ResourceLocation path = "overlay" />
     5    <ResourceLocation path = "tcl" />
     6</ResourceCollection>
  • code/branches/resource2/data/resources_graphics.oxr

    r5646 r5654  
    1 <ResourceGroup name = "General" >
    2     <ResourceLocation path = overlay />
    3     <ResourceLocation path = particle />
    4     <ResourceLocation path = defaultConfig />
    5     <ResourceLocation path = levels />
    6     <ResourceLocation path = tcl />
    7     <ResourceLocation
    8       path = test.zip
    9       archive = Zip
    10       recursive = true
    11     />
    12 </ResourceGroup>
     1<ResourceCollection resourceGroup = "General" >
     2    <ResourceLocation path = "particle" />
     3</ResourceCollection>
    134
    14 <ResourceGroup name = "GUI" >
    15     <ResourceLocation path = gui/configs />
    16     <ResourceLocation path = gui/layouts />
    17     <ResourceLocation path = gui/schemas />
    18     <ResourceLocation path = gui/scripts />
    19 </ResourceGroup>
     5<ResourceCollection resourceGroup = "GUI" >
     6    <ResourceLocation path = "gui/configs" />
     7    <ResourceLocation path = "gui/layouts" />
     8    <ResourceLocation path = "gui/schemes" />
     9    <ResourceLocation path = "gui/scripts" />
     10</ResourceCollection>
  • code/branches/resource2/src/core/CMakeLists.txt

    r5653 r5654  
    3030  GUIManager.cc
    3131  Language.cc
    32   LuaBind.cc
     32  LuaState.cc
    3333  ObjectListBase.cc
    3434  OrxonoxClass.cc
     
    7676  TOLUA_FILES
    7777    CommandExecutor.h
    78     LuaBind.h
     78    Loader.h
     79    LuaState.h
    7980  DEFINE_SYMBOL
    8081    "CORE_SHARED_BUILD"
  • code/branches/resource2/src/core/Core.cc

    r5645 r5654  
    7474#include "Identifier.h"
    7575#include "Language.h"
    76 #include "LuaBind.h"
     76#include "LuaState.h"
    7777#include "Shell.h"
    7878#include "TclBind.h"
    7979#include "TclThreadManager.h"
     80#include "ToluaBindCore.h"
    8081#include "input/InputManager.h"
    8182
     
    266267            setThreadAffinity(static_cast<unsigned int>(limitToCPU));
    267268#endif
     269        // Add tolua interface
     270        LuaState::addToluaInterface(&tolua_Core_open, "Core");
    268271
    269272        // Manage ini files and set the default settings file (usually orxonox.ini)
     
    275278        this->languageInstance_.reset(new Language());
    276279
     280        // creates the class hierarchy for all classes with factories
     281        Factory::createClassHierarchy();
     282
    277283        // Do this soon after the ConfigFileManager has been created to open up the
    278284        // possibility to configure everything below here
    279285        this->configuration_->initialise();
    280286
    281         // Create the lua interface
    282         this->luaBind_.reset(new LuaBind());
     287        // Load OGRE excluding the renderer and the render window
     288        this->graphicsManager_.reset(new GraphicsManager(false));
    283289
    284290        // initialise Tcl
     
    288294        // create a shell
    289295        this->shell_.reset(new Shell());
    290 
    291         // creates the class hierarchy for all classes with factories
    292         Factory::createClassHierarchy();
    293 
    294         // Load OGRE excluding the renderer and the render window
    295         this->graphicsManager_.reset(new GraphicsManager(false));
    296296    }
    297297
    298298    /**
    299299    @brief
    300         All destruction code is handled by scoped_ptrs and SimpleScopeGuards.
     300        All destruction code is handled by scoped_ptrs and ScopeGuards.
    301301    */
    302302    Core::~Core()
     
    561561            COUT(1) << "Running from the build tree." << std::endl;
    562562            Core::bDevRun_ = true;
    563             configuration_->dataPath_  = specialConfig::dataDevDirectory;
     563            configuration_->dataPath_   = specialConfig::dataDevDirectory;
    564564            configuration_->externalDataPath_ = specialConfig::externalDataDevDirectory;
    565565            configuration_->configPath_ = specialConfig::configDevDirectory;
     
    579579
    580580            // Using paths relative to the install prefix, complete them
    581             configuration_->dataPath_  = configuration_->rootPath_ / specialConfig::defaultDataPath;
     581            configuration_->dataPath_   = configuration_->rootPath_ / specialConfig::defaultDataPath;
    582582            configuration_->configPath_ = configuration_->rootPath_ / specialConfig::defaultConfigPath;
    583583            configuration_->logPath_    = configuration_->rootPath_ / specialConfig::defaultLogPath;
  • code/branches/resource2/src/core/Core.h

    r5651 r5654  
    129129            scoped_ptr<Language>          languageInstance_;
    130130            scoped_ptr<CoreConfiguration> configuration_;
    131             scoped_ptr<LuaBind>           luaBind_;
    132131            scoped_ptr<TclBind>           tclBind_;
    133132            scoped_ptr<TclThreadManager>  tclThreadManager_;
  • code/branches/resource2/src/core/CorePrereqs.h

    r5653 r5654  
    133133    class LanguageEntry;
    134134    class Loader;
    135     class LuaBind;
     135    class LuaState;
    136136    class MetaObjectList;
    137137    class MetaObjectListElement;
  • code/branches/resource2/src/core/GUIManager.cc

    r5645 r5654  
    5959#include "Core.h"
    6060#include "Clock.h"
    61 #include "LuaBind.h"
     61#include "LuaState.h"
    6262
    6363namespace orxonox
     
    132132
    133133            // do this after 'new CEGUI::Sytem' because that creates the lua state in the first place
    134             LuaBind::getInstance().openToluaInterfaces(this->luaState_);
     134            LuaState::openToluaInterfaces(this->luaState_);
    135135
    136136            // initialise the basic lua code
     
    157157    {
    158158        // destroy our own tolua interfaces
    159         LuaBind::getInstance().closeToluaInterfaces(this->luaState_);
     159        LuaState::closeToluaInterfaces(this->luaState_);
    160160    }
    161161
  • code/branches/resource2/src/core/GraphicsManager.cc

    r5641 r5654  
    4545#include <OgreRenderWindow.h>
    4646#include <OgreRenderSystem.h>
     47#include <OgreResourceGroupManager.h>
    4748#include <OgreViewport.h>
    4849#include <OgreWindowEventUtilities.h>
     
    5960#include "Game.h"
    6061#include "GameMode.h"
     62#include "Loader.h"
    6163#include "WindowEventListener.h"
     64#include "XMLFile.h"
    6265
    6366namespace orxonox
     
    9699        this->loadOgrePlugins();
    97100
     101        // At first, add the root paths of the data directories as resource locations
     102        Ogre::ResourceGroupManager::getSingleton().addResourceLocation(Core::getDataPathString(), "FileSystem", "dataRoot", false);
     103        // Load resources
     104        resources_.reset(new XMLFile("resources.oxr", "dataRoot"));
     105        Loader::open(resources_.get());
     106
     107        // Only for development runs
     108        if (Core::isDevelopmentRun())
     109        {
     110            Ogre::ResourceGroupManager::getSingleton().addResourceLocation(Core::getExternalDataPathString(), "FileSystem", "externalDataRoot", false);
     111            extResources_.reset(new XMLFile("resources.oxr", "externalDataRoot"));
     112            Loader::open(extResources_.get());
     113        }
     114
    98115        if (bLoadRenderer)
    99116        {
    100117            // Reads the ogre config and creates the render window
    101             this->loadRenderer();
     118            this->upgradeToGraphics();
    102119        }
    103120    }
     
    109126    GraphicsManager::~GraphicsManager()
    110127    {
     128        // TODO: Destroy the console command
    111129    }
    112130
     
    137155    void GraphicsManager::upgradeToGraphics()
    138156    {
    139         if (renderWindow_ == NULL)
    140         {
    141             this->loadRenderer();
    142         }
     157        if (renderWindow_ != NULL)
     158            return;
     159
     160        this->loadRenderer();
     161
     162        // RESOURCE MANAGEMENT
     163
     164        // Load graphical resources
     165        resourcesGraphics_.reset(new XMLFile("resources_graphics.oxr", "dataRoot"));
     166        Loader::open(resourcesGraphics_.get());
     167
     168        // Consider external data folder for dev runs
     169        if (Core::isDevelopmentRun())
     170        {
     171            extResourcesGraphics_.reset(new XMLFile("resources_graphics.oxr", "externalDataRoot"));
     172            Loader::open(extResourcesGraphics_.get());
     173        }
     174
     175        // Initialise all resources
     176        // Note: You can only do this once! Ogre will check whether a resource group has
     177        // already been initialised. If you need to load resources later, you will have to
     178        // choose another resource group.
     179        Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
    143180    }
    144181
  • code/branches/resource2/src/core/GraphicsManager.h

    r5651 r5654  
    4343#include <OgreLog.h>
    4444#include <boost/scoped_ptr.hpp>
     45#include <boost/shared_ptr.hpp>
    4546
    4647#include "util/Singleton.h"
     
    8788        void printScreen();
    8889
    89     private:
    9090        scoped_ptr<OgreWindowEventListener> ogreWindowEventListener_; //!< Pimpl to hide OgreWindowUtilities.h
    9191        scoped_ptr<Ogre::LogManager>        ogreLogger_;
     
    9393        Ogre::RenderWindow* renderWindow_;             //!< the one and only render window
    9494        Ogre::Viewport*     viewport_;                 //!< default full size viewport
     95
     96        // XML files for the resources
     97        shared_ptr<XMLFile> resources_;                //!< XML with resource locations
     98        shared_ptr<XMLFile> extResources_;             //!< XML with resource locations in the external path (only for dev runs)
     99        shared_ptr<XMLFile> resourcesGraphics_;        //!< XML with resource locations (with renderer)
     100        shared_ptr<XMLFile> extResourcesGraphics_;     //!< XML with resource locations in the external path (only for dev runs) (with renderer)
    95101
    96102        // config values
  • code/branches/resource2/src/core/Loader.cc

    r3370 r5654  
    2929#include "Loader.h"
    3030
     31#include <sstream>
    3132#include <tinyxml/ticpp.h>
     33#include <boost/scoped_ptr.hpp>
    3234
    3335#include "util/Debug.h"
    3436#include "util/Exception.h"
     37#include "util/StringUtils.h"
    3538#include "BaseObject.h"
    3639#include "Iterator.h"
    3740#include "ObjectList.h"
    38 #include "LuaBind.h"
     41#include "LuaState.h"
    3942#include "Namespace.h"
     43#include "Resource.h"
    4044#include "XMLFile.h"
    4145
     
    118122        Loader::currentMask_s = file->getMask() * mask;
    119123
    120         // let Lua work this out:
    121         LuaBind& lua = LuaBind::getInstance();
    122         lua.clearLuaOutput();
    123         lua.loadFile(file->getFilename(), true);
    124         lua.run();
     124        // Use the LuaState to replace the XML tags (calls our function)
     125        scoped_ptr<LuaState> luaState(new LuaState());
     126        luaState->setIncludeParser(&Loader::replaceLuaTags);
     127        luaState->includeFile(file->getFilename(), file->getResourceGroup(), false);
     128        //luaState->doString(luaInput);
    125129
    126130        try
     
    129133            COUT(3) << "Mask: " << Loader::currentMask_s << std::endl;
    130134
    131             //ticpp::Document xmlfile(file->getFilename());
    132             //xmlfile.LoadFile();
    133             //ticpp::Element myelement(*Script::getFileString());
    134             ticpp::Document xmlfile;
    135             //xmlfile.ToDocument();
    136             xmlfile.Parse(lua.getLuaOutput(), true);
     135            ticpp::Document xmlfile(file->getFilename());
     136            xmlfile.Parse(luaState->getOutput().str(), true);
    137137
    138138            ticpp::Element rootElement;
     
    208208        return Loader::load(file, mask);
    209209    }
     210
     211    std::string Loader::replaceLuaTags(const std::string& text)
     212    {
     213        // chreate map with all Lua tags
     214        std::map<size_t, bool> luaTags;
     215        {
     216            size_t pos = 0;
     217            while ((pos = text.find("<?lua", pos)) != std::string::npos)
     218                luaTags[pos++] = true;
     219        }
     220        {
     221            size_t pos = 0;
     222            while ((pos = text.find("?>", pos)) != std::string::npos)
     223                luaTags[pos++] = false;
     224        }
     225
     226        // erase all tags from the map that are between two quotes
     227        {
     228            std::map<size_t, bool>::iterator it = luaTags.begin();
     229            std::map<size_t, bool>::iterator it2 = it;
     230            bool bBetweenQuotes = false;
     231            size_t pos = 0;
     232            while ((pos = getNextQuote(text, pos)) != std::string::npos)
     233            {
     234                while ((it != luaTags.end()) && (it->first < pos))
     235                {
     236                    if (bBetweenQuotes)
     237                    {
     238                        it2++;
     239                        if(it->second && !(it2->second) && it2->first < pos)
     240                            it = ++it2;
     241                        else
     242                            luaTags.erase(it++);
     243                    }
     244                    else
     245                        ++it;
     246                }
     247                bBetweenQuotes = !bBetweenQuotes;
     248                pos++;
     249            }
     250        }
     251
     252        // check whether on every opening <?lua tag a closing ?> tag follows
     253        {
     254            bool expectedValue = true;
     255            for (std::map<size_t, bool>::iterator it = luaTags.begin(); it != luaTags.end(); ++it)
     256            {
     257                if (it->second == expectedValue)
     258                    expectedValue = !expectedValue;
     259                else
     260                {
     261                    expectedValue = false;
     262                    break;
     263                }
     264            }
     265            if (!expectedValue)
     266            {
     267                COUT(2) << "Warning: Error in level file" << std::endl;
     268                // todo: errorhandling
     269                return "";
     270            }
     271        }
     272
     273        // Use a stringstream object to speed up the parsing
     274        std::ostringstream output;
     275
     276        // cut the original string into pieces and put them together with print() instead of lua tags
     277        {
     278            std::map<size_t, bool>::iterator it = luaTags.begin();
     279            bool bInPrintFunction = true;
     280            size_t start = 0;
     281            size_t end = 0;
     282
     283            do
     284            {
     285                if (it != luaTags.end())
     286                    end = (*(it++)).first;
     287                else
     288                    end = std::string::npos;
     289
     290                unsigned int equalSignCounter = 0;
     291
     292                if (bInPrintFunction)
     293                {
     294                    // count ['='[ and ]'='] and replace tags with print([[ and ]])
     295                    std::string temp = text.substr(start, end - start);
     296                    {
     297                    size_t pos = 0;
     298                    while ((pos = temp.find('[', pos)) != std::string::npos)
     299                    {
     300                        unsigned int tempCounter = 1;
     301                        size_t tempPos = pos++;
     302                        while(temp[++tempPos] == '=')
     303                        {
     304                            tempCounter++;
     305                        }
     306                        if(temp[tempPos] != '[')
     307                        {
     308                            tempCounter = 0;
     309                        }
     310                        else if(tempCounter == 0)
     311                        {
     312                            tempCounter = 1;
     313                        }
     314                        if (tempCounter > equalSignCounter)
     315                            equalSignCounter = tempCounter;
     316                        }
     317                    }
     318                    {
     319                        size_t pos = 0;
     320                        while ((pos = temp.find(']', pos)) != std::string::npos)
     321                        {
     322                            unsigned int tempCounter = 1;
     323                            size_t tempPos = pos++;
     324                            while(temp[++tempPos] == '=')
     325                            {
     326                                tempCounter++;
     327                            }
     328                            if(temp[tempPos] != ']')
     329                            {
     330                                tempCounter = 0;
     331                            }
     332                            else if(tempCounter == 0)
     333                            {
     334                                tempCounter = 1;
     335                            }
     336                            if (tempCounter > equalSignCounter)
     337                                equalSignCounter = tempCounter;
     338                        }
     339                    }
     340                    std::string equalSigns = "";
     341                    for(unsigned int i = 0; i < equalSignCounter; i++)
     342                    {
     343                        equalSigns += "=";
     344                    }
     345                    output << "print([" + equalSigns + "[" + temp + "]" + equalSigns +"])";
     346                    start = end + 5;
     347                }
     348                else
     349                {
     350                    output << text.substr(start, end - start);
     351                    start = end + 2;
     352                }
     353
     354                bInPrintFunction = !bInPrintFunction;
     355            }
     356            while (end != std::string::npos);
     357        }
     358
     359        return output.str();
     360    }
    210361}
  • code/branches/resource2/src/core/Loader.h

    r3370 r5654  
    5555            static bool reload(const XMLFile* file, const ClassTreeMask& mask = ClassTreeMask());
    5656
     57            static std::string replaceLuaTags(const std::string& text);
     58
    5759            static ClassTreeMask currentMask_s;
    5860
  • code/branches/resource2/src/core/LuaState.cc

    r5645 r5654  
    2222 *   Author:
    2323 *      Benjamin Knecht
     24 *      Reto Grieder
    2425 *   Co-authors:
    2526 *      ...
     
    2728 */
    2829
    29 #include "LuaBind.h"
    30 
    31 #include <fstream>
    32 #include <map>
     30#include "LuaState.h"
     31
     32#include <tolua/tolua++.h>
    3333extern "C" {
     34#include <lua.h>
    3435#include <lualib.h>
    3536}
    36 #include <tolua/tolua++.h>
    37 #include <boost/filesystem.hpp>
    3837
    3938#include "util/Debug.h"
    40 #include "util/StringUtils.h"
    4139#include "Core.h"
     40#include "Resource.h"
    4241
    4342namespace orxonox
    4443{
    45   LuaBind* LuaBind::singletonPtr_s = NULL;
    46 
    47   LuaBind::LuaBind()
    48   {
    49     this->includePath_ = Core::getDataPathString();
    50 
    51     luaState_ = lua_open();
    52     luaSource_ = "";
     44    LuaState::ToluaInterfaceMap LuaState::toluaInterfaces_s;
     45    std::vector<LuaState*> LuaState::instances_s;
     46
     47    LuaState::LuaState()
     48        : bIsRunning_(false)
     49        , includeParseFunction_(NULL)
     50    {
     51        // Create new lua state and configure it
     52        luaState_ = lua_open();
    5353#if LUA_VERSION_NUM == 501
    54     luaL_openlibs(luaState_);
     54        luaL_openlibs(luaState_);
    5555#else
    56     luaopen_base(luaState_);
    57     luaopen_string(luaState_);
    58     luaopen_table(luaState_);
    59     luaopen_math(luaState_);
    60     luaopen_io(luaState_);
    61     luaopen_debug(luaState_);
     56        luaopen_base(luaState_);
     57        luaopen_string(luaState_);
     58        luaopen_table(luaState_);
     59        luaopen_math(luaState_);
     60        luaopen_io(luaState_);
     61        luaopen_debug(luaState_);
    6262#endif
    6363
    64     // Open all available tolua interfaces
    65     this->openToluaInterfaces(luaState_);
    66 
    67     output_ = "";
    68     isRunning_ = false;
    69   }
    70 
    71   LuaBind::~LuaBind()
    72   {
    73     this->closeToluaInterfaces(luaState_);
    74   };
    75 
    76   void LuaBind::luaPrint(const std::string& str)
    77   {
    78     output_ += str;
    79 //    COUT(4) << "Lua_output!:" << std::endl << str << std::endl << "***" << std::endl;
    80     COUT(5) << str;
    81   }
    82 
    83   /**
    84       @brief Loads the specified file line by line
    85       @param filename The filename of the file
    86       @param luaTags if true, the loaded file gets stripped off luaTags
    87   */
    88   void LuaBind::loadFile(const std::string& filename, bool luaTags)
    89   {
    90     boost::filesystem::path filepath(filename);
    91 
    92     output_ = "";
    93     std::ifstream file;
    94     file.open(filepath.string().c_str(), std::fstream::in);
    95 
    96     if (!file.is_open())
    97     {
    98       // some error msg
    99     }
    100 
    101     std::string levelString = "";
    102 
    103     while (file.good() && !file.eof())
    104     {
    105       std::string line;
    106       std::getline(file, line);
    107       levelString += line;
    108       levelString += "\n";
    109     }
    110 
    111     file.close();
    112     //std::string output;
    113 
    114     if (luaTags)
    115       luaSource_ = replaceLuaTags(levelString);
    116     else
    117       luaSource_ = levelString;
    118     COUT(5) << "ParsedSourceCode: " << luaSource_ << std::endl;
    119   }
    120 
    121   void LuaBind::loadString(const std::string& code)
    122   {
    123     luaSource_ = code;
    124     output_ = "";
    125   }
    126 
     64        // Open all available tolua interfaces
     65        this->openToluaInterfaces(luaState_);
     66
     67        // Create dummy file info
     68        sourceFileInfo_.reset(new ResourceInfo());
     69        sourceFileInfo_->group = "General";
     70        sourceFileInfo_->size = 0;
     71
     72        // Push this pointer
     73        tolua_pushusertype(luaState_, static_cast<void*>(this), "orxonox::LuaState");
     74        lua_setglobal(luaState_, "luaState");
     75
     76        // Parse init script
     77        // Note: We have to use a hard coded path because the script is required for the resource loading
     78        this->doString("dofile(\"" + Core::getDataPathString() + "lua/LuaStateInit.lua\")");
     79    }
     80
     81    LuaState::~LuaState()
     82    {
     83        lua_close(luaState_);
     84    }
     85
     86    shared_ptr<ResourceInfo> LuaState::getFileInfo(const std::string& filename, const std::string& resourceGroup, bool bSearchOtherPaths)
     87    {
     88        shared_ptr<ResourceInfo> sourceInfo;
     89        if (resourceGroup != "NoResourceGroupProvided")
     90            sourceInfo = Resource::getInfo(filename, resourceGroup);
     91
     92        // Continue search if not explicitely forbidden
     93        if (bSearchOtherPaths && sourceInfo == NULL)
     94        {
     95            // Call might be relative to the file currently being processed
     96            sourceInfo = Resource::getInfo(sourceFileInfo_->path + filename, sourceFileInfo_->group);
     97            if (sourceInfo == NULL)
     98            {
     99                // Maybe find something in the same group but in the root path
     100                sourceInfo = Resource::getInfo(filename, sourceFileInfo_->group);
     101            }
     102        }
     103        return sourceInfo;
     104    }
     105
     106    void LuaState::includeFile(const std::string& filename, const std::string& resourceGroup, bool bSearchOtherPaths)
     107    {
     108        shared_ptr<ResourceInfo> sourceInfo = this->getFileInfo(filename, resourceGroup, bSearchOtherPaths);
     109        if (sourceInfo != NULL)
     110            this->includeString(Resource::open(sourceInfo->filename, sourceInfo->group)->getAsString(), sourceInfo);
     111        else
     112            COUT(2) << "LuaState: Cannot include file '" << filename << "' in resource group '"
     113                    << (resourceGroup == "NoResourceGroupProvided" ? sourceFileInfo_->group : resourceGroup) << "': group not found." << std::endl;
     114    }
     115
     116    void LuaState::includeString(const std::string& code, shared_ptr<ResourceInfo> sourceFileInfo)
     117    {
     118        // Parse string with provided include parser (otherwise don't preparse at all)
     119        std::string luaInput;
     120        if (includeParseFunction_ != NULL)
     121            luaInput = (*includeParseFunction_)(code);
     122        else
     123            luaInput = code;
     124
     125        this->doString(luaInput, sourceFileInfo);
     126    }
     127
     128    void LuaState::doFile(const std::string& filename, const std::string& resourceGroup, bool bSearchOtherPaths)
     129    {
     130        shared_ptr<ResourceInfo> sourceInfo = this->getFileInfo(filename, resourceGroup, bSearchOtherPaths);
     131        if (sourceInfo != NULL)
     132            this->doString(Resource::open(sourceInfo->filename, sourceInfo->group)->getAsString(), sourceInfo);
     133        else
     134            COUT(2) << "LuaState: Cannot do file '" << filename << "' in resource group '"
     135                << (resourceGroup == "NoResourceGroupProvided" ? sourceFileInfo_->group : resourceGroup) << "': group not found." << std::endl;
     136    }
     137
     138    void LuaState::doString(const std::string& code, shared_ptr<ResourceInfo> sourceFileInfo)
     139    {
     140        // Save the oold source file info
     141        shared_ptr<ResourceInfo> oldSourceFileInfo = sourceFileInfo_;
     142        // Only override if sourceFileInfo provides useful information
     143        if (sourceFileInfo != NULL)
     144            sourceFileInfo_ = sourceFileInfo;
     145
     146        //if (!bIsRunning_)
     147        //{
     148        //    bIsRunning_ = true;
     149
     150        int error = 0;
    127151#if LUA_VERSION_NUM != 501
    128   const char * LuaBind::lua_Chunkreader(lua_State *L, void *data, size_t *size)
    129   {
    130     LoadS* ls = static_cast<LoadS*>(data);
    131     if (ls->size == 0) return NULL;
    132     *size = ls->size;
    133     ls->size = 0;
    134     return ls->s;
    135   }
     152        LoadS ls;
     153        ls.s = code.c_str();
     154        ls.size = code.size();
     155        error = lua_load(luaState_, &orxonox::LuaState::lua_Chunkreader, &ls, code.c_str());
     156#else
     157        error = luaL_loadstring(luaState_, code.c_str());
    136158#endif
    137   void LuaBind::run()
    138   {
    139     if (!isRunning_)
    140     {
    141       isRunning_ = true;
    142       int error = 0;
    143       std::string init =
    144          "local scr = orxonox.LuaBind:getInstance()\n \
    145           local debug = print\n \
    146           print = function(s)\n \
    147               scr:luaPrint(s)\n \
    148           end\n \
    149           include = function(f)\n \
    150               file = assert(io.open(\"" + this->includePath_ + "\"..\"/\"..f))\n \
    151               content = file:read(\"*a\")\n \
    152               file:close()\n \
    153               source = scr:replaceLuaTags(content)\n \
    154               assert(loadstring(source))()\n \
    155           end\n";
    156       init += luaSource_;
    157   #if LUA_VERSION_NUM == 501
    158       error = luaL_loadstring(luaState_, init.c_str());
    159   #else
    160       LoadS ls;
    161       ls.s = init.c_str();
    162       ls.size = init.size();
    163       error = lua_load(luaState_, &orxonox::LuaBind::lua_Chunkreader, &ls, init.c_str());
    164   #endif
    165       if (error == 0)
    166       {
    167         error = lua_pcall(luaState_, 0, 0, 0);
    168       }
    169       if (error != 0)
    170       {
    171         COUT(2) << "Error in Lua-script: " << lua_tostring(luaState_, -1) << std::endl;
    172       }
    173       isRunning_ = false;
    174     }
    175     else
    176     {
    177       COUT(2) << "Warning: Lua's run is called while running!" << std::endl;
    178     }
    179   }
    180 
    181   std::string LuaBind::replaceLuaTags(const std::string& text)
    182   {
    183     // chreate map with all Lua tags
    184     std::map<size_t, bool> luaTags;
    185     {
    186       size_t pos = 0;
    187       while ((pos = text.find("<?lua", pos)) != std::string::npos)
    188         luaTags[pos++] = true;
    189     }
    190     {
    191       size_t pos = 0;
    192       while ((pos = text.find("?>", pos)) != std::string::npos)
    193         luaTags[pos++] = false;
    194     }
    195 
    196     // erase all tags from the map that are between two quotes
    197     {
    198       std::map<size_t, bool>::iterator it = luaTags.begin();
    199       std::map<size_t, bool>::iterator it2 = it;
    200       bool bBetweenQuotes = false;
    201       size_t pos = 0;
    202       while ((pos = getNextQuote(text, pos)) != std::string::npos)
    203       {
    204         while ((it != luaTags.end()) && (it->first < pos))
    205         {
    206           if (bBetweenQuotes) {
    207             it2++;
    208             if(it->second && !(it2->second) && it2->first < pos)
    209               it = ++it2;
    210             else
    211               luaTags.erase(it++);
    212           }
    213           else
    214             ++it;
    215         }
    216         bBetweenQuotes = !bBetweenQuotes;
    217         pos++;
    218       }
    219     }
    220 
    221     // check whether on every opening <?lua tag a closing ?> tag follows
    222     {
    223       bool expectedValue = true;
    224       for (std::map<size_t, bool>::iterator it = luaTags.begin(); it != luaTags.end(); ++it)
    225       {
    226         if (it->second == expectedValue)
    227           expectedValue = !expectedValue;
    228         else
    229         {
    230           expectedValue = false;
    231           break;
    232         }
    233       }
    234       if (!expectedValue) {
    235         COUT(2) << "Warning: Error in level file" << std::endl;
    236         // todo: errorhandling
    237         return "";
    238       }
    239     }
    240 
    241     // cut the original string into pieces and put them together with print() instead of lua tags
    242     std::string output;
    243     {
    244       std::map<size_t, bool>::iterator it = luaTags.begin();
    245       bool bInPrintFunction = true;
    246       size_t start = 0;
    247       size_t end = 0;
    248 
    249       do
    250       {
    251         if (it != luaTags.end())
    252           end = (*(it++)).first;
    253         else
    254           end = std::string::npos;
    255 
    256         unsigned int equalSignCounter = 0;
    257 
    258         if (bInPrintFunction)
    259         {
    260           // count ['='[ and ]'='] and replace tags with print([[ and ]])
    261           std::string temp = text.substr(start, end - start);
    262           {
    263             size_t pos = 0;
    264             while ((pos = temp.find('[', pos)) != std::string::npos)
     159
     160        // execute the chunk
     161        if (error == 0)
     162            error = lua_pcall(luaState_, 0, 0, 0);
     163        if (error != 0)
     164        {
     165            std::string origin;
     166            if (sourceFileInfo != NULL)
     167                origin = " originating from " + sourceFileInfo_->filename;
     168            COUT(2) << "Error in Lua-script" << origin << ": " << lua_tostring(luaState_, -1) << std::endl;
     169        }
     170
     171        //    bIsRunning_ = false;
     172        //}
     173        //else
     174        //{
     175        //    COUT(2) << "Warning: LuaState do function called while running!" << std::endl;
     176        //}
     177
     178        // Load the old info again
     179        sourceFileInfo_ = oldSourceFileInfo;
     180    }
     181
     182    void LuaState::luaPrint(const std::string& str)
     183    {
     184        output_ << str;
     185    }
     186
     187    void LuaState::luaLog(unsigned int level, const std::string& message)
     188    {
     189        OutputHandler::getOutStream().setOutputLevel(level) << message << std::endl;
     190    }
     191
     192#if LUA_VERSION_NUM != 501
     193    const char * LuaState::lua_Chunkreader(lua_State *L, void *data, size_t *size)
     194    {
     195        LoadS* ls = static_cast<LoadS*>(data);
     196        if (ls->size == 0)
     197            return NULL;
     198        *size = ls->size;
     199        ls->size = 0;
     200        return ls->s;
     201    }
     202#endif
     203
     204    /*static*/ bool LuaState::addToluaInterface(int (*function)(lua_State*), const std::string& name)
     205    {
     206        for (ToluaInterfaceMap::const_iterator it = toluaInterfaces_s.begin(); it != toluaInterfaces_s.end(); ++it)
     207        {
     208            if (it->first == name || it->second == function)
    265209            {
    266               unsigned int tempCounter = 1;
    267               size_t tempPos = pos++;
    268               while(temp[++tempPos] == '=') {
    269                 tempCounter++;
    270               }
    271               if(temp[tempPos] != '[') {
    272                 tempCounter = 0;
    273               }
    274               else if(tempCounter == 0) {
    275                 tempCounter = 1;
    276               }
    277               if (tempCounter > equalSignCounter)
    278                 equalSignCounter = tempCounter;
     210                COUT(2) << "Warning: Trying to add a Tolua interface with the same name or function." << std::endl;
     211                return true;
    279212            }
    280           }
    281           {
    282             size_t pos = 0;
    283             while ((pos = temp.find(']', pos)) != std::string::npos)
    284             {
    285               unsigned int tempCounter = 1;
    286               size_t tempPos = pos++;
    287               while(temp[++tempPos] == '=') {
    288                 tempCounter++;
    289               }
    290               if(temp[tempPos] != ']') {
    291                 tempCounter = 0;
    292               }
    293               else if(tempCounter == 0) {
    294                 tempCounter = 1;
    295               }
    296               if (tempCounter > equalSignCounter)
    297                 equalSignCounter = tempCounter;
    298             }
    299           }
    300           std::string equalSigns = "";
    301           for(unsigned int i = 0; i < equalSignCounter; i++) {
    302             equalSigns += "=";
    303           }
    304           output += "print([" + equalSigns + "[" + temp + "]" + equalSigns +"])";
    305           start = end + 5;
    306         }
    307         else
    308         {
    309           output += text.substr(start, end - start);
    310           start = end + 2;
    311         }
    312 
    313         bInPrintFunction = !bInPrintFunction;
    314       }
    315       while (end != std::string::npos);
    316     }
    317 
    318     return output;
    319   }
    320 
    321   void LuaBind::addToluaInterface(int (*function)(lua_State*), const std::string& name)
    322   {
    323     toluaInterfaces_.push_back(std::make_pair(name, function));
    324     // Apply changes to our own lua state as well
    325     (*function)(luaState_);
    326   }
    327 
    328   void LuaBind::openToluaInterfaces(lua_State* state)
    329   {
    330     for (unsigned int i = 0; i < toluaInterfaces_.size(); ++i)
    331       (*toluaInterfaces_[i].second)(state);
    332   }
    333 
    334   void LuaBind::closeToluaInterfaces(lua_State* state)
    335   {
    336     for (unsigned int i = 0; i < toluaInterfaces_.size(); ++i)
    337     {
    338       lua_pushnil(state);
    339       lua_setglobal(state, toluaInterfaces_[i].first.c_str());
    340     }
    341   }
    342 
     213        }
     214        toluaInterfaces_s[name] = function;
     215
     216        // Open interface in all LuaStates
     217        for (std::vector<LuaState*>::const_iterator it = instances_s.begin(); it != instances_s.end(); ++it)
     218            (*function)((*it)->luaState_);
     219
     220        // Return dummy bool
     221        return true;
     222    }
     223
     224    /*static*/ bool LuaState::removeToluaInterface(const std::string& name)
     225    {
     226        ToluaInterfaceMap::iterator it = toluaInterfaces_s.find(name);
     227        if (it == toluaInterfaces_s.end())
     228        {
     229            COUT(2) << "Warning: Cannot remove Tolua interface '" << name << "': Not found" << std::endl;
     230            return true;
     231        }
     232
     233        // Close interface in all LuaStates
     234        for (std::vector<LuaState*>::const_iterator itState = instances_s.begin(); itState != instances_s.end(); ++itState)
     235        {
     236            lua_pushnil((*itState)->luaState_);
     237            lua_setglobal((*itState)->luaState_, it->first.c_str());
     238        }
     239
     240        // Remove entry
     241        toluaInterfaces_s.erase(it);
     242
     243        // Return dummy bool
     244        return true;
     245    }
     246
     247    /*static*/ void LuaState::openToluaInterfaces(lua_State* state)
     248    {
     249        for (ToluaInterfaceMap::const_iterator it = toluaInterfaces_s.begin(); it != toluaInterfaces_s.end(); ++it)
     250            (*it->second)(state);
     251    }
     252
     253    /*static*/ void LuaState::closeToluaInterfaces(lua_State* state)
     254    {
     255        for (ToluaInterfaceMap::const_iterator it = toluaInterfaces_s.begin(); it != toluaInterfaces_s.end(); ++it)
     256        {
     257            lua_pushnil(state);
     258            lua_setglobal(state, it->first.c_str());
     259        }
     260    }
    343261}
  • code/branches/resource2/src/core/LuaState.h

    r5645 r5654  
    2222 *   Author:
    2323 *      Benjamin Knecht
     24 *      Reto Grieder
    2425 *   Co-authors:
    2526 *      ...
     
    2728 */
    2829
    29 /**
    30  @file
    31  @brief Representation of an interface to lua
    32  @author Benjamin Knecht <beni_at_orxonox.net>
    33  */
    34 
    35 #ifndef _LuaBind_H__
    36 #define _LuaBind_H__
     30#ifndef _LuaState_H__
     31#define _LuaState_H__
    3732
    3833#include "CorePrereqs.h"
    3934
    40 #include <cassert>
     35#include <map>
     36#include <sstream>
    4137#include <string>
    4238#include <vector>
    43 extern "C" {
    44 #include <lua.h>
    45 }
     39#include <boost/shared_ptr.hpp>
    4640
    47 #include "util/Singleton.h"
     41#include "util/ScopeGuard.h"
    4842
    4943// tolua_begin
    5044namespace orxonox
    5145{
    52   class _CoreExport LuaBind : public Singleton<LuaBind>
    53   {
     46    /**
     47    @brief
     48        Representation of an interface to lua
     49    */
     50    class _CoreExport LuaState
     51    {
    5452// tolua_end
    55     friend class Singleton<LuaBind>;
     53    public:
     54        LuaState();
     55        ~LuaState();
    5656
    57     struct LoadS {
    58       const char *s;
    59       size_t size;
    60     };
     57        void doFile(const std::string& filename, const std::string& resourceGroup = "General", bool bSearchOtherPaths = true); // tolua_export
     58        void doString(const std::string& code, shared_ptr<ResourceInfo> sourceFileInfo = shared_ptr<ResourceInfo>());
    6159
    62     public:
    63       LuaBind();
    64       ~LuaBind();
     60        void includeFile(const std::string& filename, const std::string& resourceGroup = "Genreal", bool bSearchOtherPaths = true); // tolua_export
     61        void includeString(const std::string& code, shared_ptr<ResourceInfo> sourceFileInfo = shared_ptr<ResourceInfo>());
    6562
    66       static LuaBind& getInstance() { return Singleton<LuaBind>::getInstance(); } // tolua_export
     63        void luaPrint(const std::string& str); // tolua_export
     64        void luaLog(unsigned int level, const std::string& message); // tolua_export
    6765
    68     void loadFile(const std::string& filename, bool luaTags);
    69     void loadString(const std::string& code);
    70     //void init(lua_State *state_);
    71     //void xmlToLua();
    72     void run();
    73     void luaPrint(const std::string& str); // tolua_export
     66        const std::stringstream& getOutput() const { return output_; }
     67        void clearOutput() { output_.clear(); } // tolua_export
     68
     69        void setIncludeParser(std::string (*function)(const std::string&)) { includeParseFunction_ = function; }
     70        lua_State* getInternalLuaState() { return luaState_; }
     71
     72        static bool addToluaInterface(int (*function)(lua_State*), const std::string& name);
     73        static bool removeToluaInterface(const std::string& name);
     74        static void openToluaInterfaces(lua_State* state);
     75        static void closeToluaInterfaces(lua_State* state);
     76
     77    private:
     78        shared_ptr<ResourceInfo> getFileInfo(const std::string& filename, const std::string& resourceGroup, bool bSearchOtherPaths);
    7479
    7580#if LUA_VERSION_NUM != 501
    76     static const char * lua_Chunkreader(lua_State *L, void *data, size_t *size);
     81        struct LoadS
     82        {
     83            const char* s;
     84            size_t size;
     85        };
     86
     87        static const char * lua_Chunkreader(lua_State *L, void *data, size_t *size);
    7788#endif
    7889
    79     inline lua_State* getLuaState() { return luaState_; };
    80     inline const std::string& getLuaOutput() { return output_; };
    81     //inline std::string* getFileString() { return &fileString_; };
    82     inline void clearLuaOutput() { output_ = ""; }
     90        std::stringstream output_;
     91        lua_State* luaState_;
     92        bool bIsRunning_;
     93        shared_ptr<ResourceInfo> sourceFileInfo_;
     94        std::string (*includeParseFunction_)(const std::string&);
    8395
    84     std::string replaceLuaTags(const std::string& text); // tolua_export
     96        typedef std::map<std::string, int (*)(lua_State *L)> ToluaInterfaceMap;
     97        static ToluaInterfaceMap toluaInterfaces_s;
     98        static std::vector<LuaState*> instances_s;
     99    }; // tolua_export
     100} // tolua_export
    85101
    86     inline void setIncludePath(const std::string& includepath)
    87         { this->includePath_ = includepath; }
    88 
    89     void addToluaInterface(int (*function)(lua_State*), const std::string& name);
    90     void openToluaInterfaces(lua_State* state);
    91     void closeToluaInterfaces(lua_State* state);
    92 
    93     private:
    94       static LuaBind* singletonPtr_s;
    95 
    96       std::string luaSource_;
    97       std::string output_;
    98       lua_State* luaState_;
    99       bool isRunning_;
    100       std::string includePath_;
    101       std::vector<std::pair<std::string, int (*)(lua_State *L)> > toluaInterfaces_;
    102 
    103   }; // tolua_export
    104 } // tolua_export
    105 #endif /* _LuaBind_H__ */
     102#endif /* _LuaState_H__ */
  • code/branches/resource2/src/orxonox/gamestates/GSGraphics.cc

    r5645 r5654  
    9696        // load debug overlay
    9797        COUT(3) << "Loading Debug Overlay..." << std::endl;
    98         this->debugOverlay_ = new XMLFile(Core::getDataPathString() + "overlay/debug.oxo");
     98        this->debugOverlay_ = new XMLFile("debug.oxo");
    9999        Loader::open(debugOverlay_);
    100100
  • code/branches/resource2/src/orxonox/gamestates/GSLevel.cc

    r5645 r5654  
    246246        // call the loader
    247247        COUT(0) << "Loading level..." << std::endl;
    248         startFile_s = new XMLFile(Core::getDataPathString() + "levels" + '/' + LevelManager::getInstance().getDefaultLevel());
     248        startFile_s = new XMLFile(LevelManager::getInstance().getDefaultLevel());
    249249        Loader::open(startFile_s);
    250250    }
  • code/branches/resource2/src/orxonox/gamestates/GSRoot.cc

    r3370 r5654  
    3333#include "core/Game.h"
    3434#include "core/GameMode.h"
    35 #include "core/LuaBind.h"
     35#include "core/LuaState.h"
    3636#include "network/NetworkFunction.h"
    37 #include "ToluaBindCore.h"
    3837#include "ToluaBindOrxonox.h"
    3938#include "tools/Timer.h"
     
    5655
    5756        // Tell LuaBind about all tolua interfaces
    58         LuaBind::getInstance().addToluaInterface(&tolua_Core_open, "Core");
    59         LuaBind::getInstance().addToluaInterface(&tolua_Orxonox_open, "Orxonox");
     57        LuaState::addToluaInterface(&tolua_Orxonox_open, "Orxonox");
    6058    }
    6159
     
    8684        }
    8785
    88         // create the global LevelManager
     86        // create the LevelManager
    8987        this->levelManager_ = new LevelManager();
    9088    }
  • code/branches/resource2/src/orxonox/objects/Level.cc

    r5653 r5654  
    5353        this->registerVariables();
    5454        this->xmlfilename_ = this->getFilename();
    55 
    56         if (this->xmlfilename_.length() >= Core::getDataPathString().length())
    57             this->xmlfilename_ = this->xmlfilename_.substr(Core::getDataPathString().length());
    5855    }
    5956
  • code/branches/resource2/src/orxonox/objects/Script.cc

    r3196 r5654  
    2929#include "Script.h"
    3030
    31 #include <tinyxml/ticpp.h>
    3231#include "core/CoreIncludes.h"
    33 #include "core/LuaBind.h"
     32#include "core/LuaState.h"
     33#include "core/XMLPort.h"
    3434
    3535namespace orxonox
    3636{
    37   CreateFactory(Script);
     37    CreateFactory(Script);
    3838
    39   Script::Script(BaseObject* creator) : BaseObject(creator)
    40   {
    41     RegisterObject(Script);
     39    Script::Script(BaseObject* creator) : BaseObject(creator)
     40    {
     41        RegisterObject(Script);
    4242
    43     code_ = "";
    44   }
     43        // Get a new LuaState
     44        luaState_ = new LuaState();
     45    }
    4546
    46   Script::~Script()
    47   {
    48   }
     47    Script::~Script()
     48    {
     49        if (this->isInitialized())
     50            delete luaState_;
     51    }
    4952
    50   /**
    51   @brief XML loading and saving.
    52   @param xmlelement The XML-element
    53   @param loading Loading (true) or saving (false)
    54    */
    55   void Script::XMLPort(Element& xmlelement, XMLPort::Mode mode)
    56   {
    57     BaseObject::XMLPort(xmlelement, mode);
     53    void Script::XMLPort(Element& xmlelement, XMLPort::Mode mode)
     54    {
     55        BaseObject::XMLPort(xmlelement, mode);
    5856
    59     code_ = xmlelement.GetText(false);
    60   }
     57        XMLPortParam(Script, "code", setCode, getCode, xmlelement, mode);
     58    }
    6159
    62   void Script::execute()
    63   {
    64     LuaBind& lua = LuaBind::getInstance();
    65     lua.loadString(this->code_);
    66     lua.run();
    67   }
     60    void Script::execute()
     61    {
     62        luaState_->doString(code_);
     63    }
    6864}
  • code/branches/resource2/src/orxonox/objects/Script.h

    r3196 r5654  
    3737namespace orxonox
    3838{
    39   class _OrxonoxExport Script : public BaseObject
    40   {
     39    class _OrxonoxExport Script : public BaseObject
     40    {
    4141    public:
    42       Script(BaseObject* creator);
    43       ~Script();
    44       void XMLPort(Element& xmlelement, XMLPort::Mode mode);
    45       void execute();
     42        Script(BaseObject* creator);
     43        ~Script();
     44        void XMLPort(Element& xmlelement, XMLPort::Mode mode);
     45        void execute();
     46
     47        void setCode(const std::string& code) { code_ = code; }
     48        const std::string& getCode() const { return code_; }
    4649
    4750    private:
    48       std::string code_;
    49   };
     51        std::string code_;
     52        LuaState* luaState_;
     53    };
    5054}
    5155
  • code/branches/resource2/src/orxonox/sound/SoundBase.cc

    r5645 r5654  
    3636#include "util/Math.h"
    3737#include "core/Core.h"
     38#include "core/Resource.h"
    3839#include "orxonox/objects/worldentities/WorldEntity.h"
    3940#include "SoundManager.h"
     
    134135    }
    135136
    136     bool SoundBase::loadFile(std::string filename) {
    137         filename = Core::getDataPathString() + "/audio/" + filename;
    138 
     137    bool SoundBase::loadFile(const std::string& filename) {
    139138        if(!SoundManager::getInstance().isSoundAvailable())
    140139        {
     
    144143
    145144        COUT(3) << "Sound: OpenAL ALUT: loading file " << filename << std::endl;
    146         this->buffer_ = alutCreateBufferFromFile(filename.c_str());
     145        // Get DataStream from the resources
     146        shared_ptr<ResourceInfo> fileInfo = Resource::getInfo(filename);
     147        if (fileInfo == NULL) {
     148            COUT(2) << "Warning: Sound file '" << filename << "' not found" << std::endl;
     149            return false;
     150        }
     151        DataStreamPtr stream = Resource::open(filename);
     152        // Read everything into a temporary buffer
     153        char* buffer = new char[fileInfo->size];
     154        stream->read(buffer, fileInfo->size);
     155
     156        this->buffer_ = alutCreateBufferFromFileImage(buffer, fileInfo->size);
     157        delete[] buffer;
     158
    147159        if(this->buffer_ == AL_NONE) {
    148160            COUT(2) << "Sound: OpenAL ALUT: " << alutGetErrorString(alutGetError()) << std::endl;
  • code/branches/resource2/src/orxonox/sound/SoundBase.h

    r3196 r5654  
    5555        bool isStopped();
    5656
    57         bool loadFile(std::string filename);
     57        bool loadFile(const std::string& filename);
    5858
    5959    private:
Note: See TracChangeset for help on using the changeset viewer.