Index: code/branches/resource2/data/levels/empty_level.oxw
===================================================================
--- code/branches/resource2/data/levels/empty_level.oxw	(revision 5653)
+++ code/branches/resource2/data/levels/empty_level.oxw	(revision 5654)
@@ -1,10 +1,10 @@
 <?lua
-  include("overlay/stats.oxo")
-  include("overlay/hudtemplates3.oxo")
+  include("stats.oxo")
+  include("hudtemplates3.oxo")
 ?>
 
 <?lua
-  include("levels/templates/spaceship_assff.oxt")
-  include("levels/templates/spaceship_pirate.oxt")
+  include("templates/spaceship_assff.oxt")
+  include("templates/spaceship_pirate.oxt")
 ?>
 
Index: code/branches/resource2/data/levels/gametype_asteroids.oxw
===================================================================
--- code/branches/resource2/data/levels/gametype_asteroids.oxw	(revision 5653)
+++ code/branches/resource2/data/levels/gametype_asteroids.oxw	(revision 5654)
@@ -1,6 +1,6 @@
 <?lua
-  include("overlay/hudtemplates3.oxo")
-  include("overlay/stats.oxo")
-  include("levels/templates/spaceship_assff.oxt")
+  include("hudtemplates3.oxo")
+  include("stats.oxo")
+  include("templates/spaceship_assff.oxt")
 ?>
 
Index: code/branches/resource2/data/levels/gametype_underattack.oxw
===================================================================
--- code/branches/resource2/data/levels/gametype_underattack.oxw	(revision 5653)
+++ code/branches/resource2/data/levels/gametype_underattack.oxw	(revision 5654)
@@ -1,7 +1,7 @@
 <?lua
-  include("overlay/hudtemplates3.oxo")
-  include("overlay/stats.oxo")
-  include("overlay/underattackhud.oxo")
-  include("levels/templates/spaceship_assff.oxt")
+  include("hudtemplates3.oxo")
+  include("stats.oxo")
+  include("underattackhud.oxo")
+  include("templates/spaceship_assff.oxt")
 ?>
 
Index: code/branches/resource2/data/levels/old/CuboidSpaceStation.oxw
===================================================================
--- code/branches/resource2/data/levels/old/CuboidSpaceStation.oxw	(revision 5653)
+++ code/branches/resource2/data/levels/old/CuboidSpaceStation.oxw	(revision 5654)
@@ -1,8 +1,8 @@
 <?lua
-	include("overlay/hudtemplates3.oxo")
+	include("hudtemplates3.oxo")
 ?>
 
 <?lua
-	include("levels/templates/spaceship_assff.oxt")
+	include("templates/spaceship_assff.oxt")
 ?>
 
@@ -20,5 +20,5 @@
 <!-------------------------------------------------------------------------------------------------------------------------------------------------------------------------->
 <?lua
-	dofile("../../media/levels/includes/CuboidSpaceStation.lua")
+	dofile("includes/CuboidSpaceStation.lua")
 ?>
 
Index: code/branches/resource2/data/levels/old/physicstest2.oxw
===================================================================
--- code/branches/resource2/data/levels/old/physicstest2.oxw	(revision 5653)
+++ code/branches/resource2/data/levels/old/physicstest2.oxw	(revision 5654)
@@ -1,7 +1,7 @@
 <?lua
-  include("levels/old/spaceshiptemplates_physics.oxw")
+  include("spaceshiptemplates_physics.oxw")
 ?>
 <?lua
-  include("overlay/hudtemplates3.oxo")
+  include("hudtemplates3.oxo")
 ?>
 
Index: code/branches/resource2/data/levels/old/planettest.oxw
===================================================================
--- code/branches/resource2/data/levels/old/planettest.oxw	(revision 5653)
+++ code/branches/resource2/data/levels/old/planettest.oxw	(revision 5654)
@@ -1,8 +1,8 @@
 <?lua
-  include("overlay/hudtemplates3.oxo")
+  include("hudtemplates3.oxo")
 ?>
 
 <?lua
-  include("levels/templates/spaceship_assff.oxt")
+  include("templates/spaceship_assff.oxt")
 ?>
 
Index: code/branches/resource2/data/levels/old/presentation_physics.oxw
===================================================================
--- code/branches/resource2/data/levels/old/presentation_physics.oxw	(revision 5653)
+++ code/branches/resource2/data/levels/old/presentation_physics.oxw	(revision 5654)
@@ -1,6 +1,6 @@
 <?lua
-  include("overlay/hudtemplates3.oxo")
-  include("overlay/stats.oxo")
-  include("levels/templates/spaceship_assff.oxt")
+  include("hudtemplates3.oxo")
+  include("stats.oxo")
+  include("templates/spaceship_assff.oxt")
   math.randomseed(344)
 ?>
Index: code/branches/resource2/data/levels/old/princessaeryn.oxw
===================================================================
--- code/branches/resource2/data/levels/old/princessaeryn.oxw	(revision 5653)
+++ code/branches/resource2/data/levels/old/princessaeryn.oxw	(revision 5654)
@@ -1,10 +1,10 @@
 <?lua
-  include("overlay/hudtemplates3.oxo")
-  include("overlay/stats.oxo")
-  include("levels/templates/spaceship_assff.oxt")
+  include("hudtemplates3.oxo")
+  include("stats.oxo")
+  include("templates/spaceship_assff.oxt")
 ?>
 
 <?lua
-	dofile("../../media/levels/includes/CuboidSpaceStation.lua")
+	dofile("includes/CuboidSpaceStation.lua")
 ?>
 
Index: code/branches/resource2/data/levels/old/questsystem.oxw
===================================================================
--- code/branches/resource2/data/levels/old/questsystem.oxw	(revision 5653)
+++ code/branches/resource2/data/levels/old/questsystem.oxw	(revision 5654)
@@ -1,6 +1,6 @@
 <?lua
-  include("overlay/hudtemplates3.oxo")
-  include("overlay/stats.oxo")
-  include("levels/templates/spaceship_assff.oxt")
+  include("hudtemplates3.oxo")
+  include("stats.oxo")
+  include("templates/spaceship_assff.oxt")
 ?>
 
Index: code/branches/resource2/data/levels/old/sample4.oxw
===================================================================
--- code/branches/resource2/data/levels/old/sample4.oxw	(revision 5653)
+++ code/branches/resource2/data/levels/old/sample4.oxw	(revision 5654)
@@ -1,8 +1,8 @@
 <?lua
-  include("overlay/hudtemplates3.oxo")
+  include("hudtemplates3.oxo")
 ?>
 
 <?lua
-  include("levels/templates/spaceship_assff.oxt")
+  include("templates/spaceship_assff.oxt")
 ?>
 
Index: code/branches/resource2/data/levels/pickuptest.oxw
===================================================================
--- code/branches/resource2/data/levels/pickuptest.oxw	(revision 5653)
+++ code/branches/resource2/data/levels/pickuptest.oxw	(revision 5654)
@@ -1,6 +1,6 @@
 <?lua
-  include("overlay/hudtemplates3.oxo")
-  include("overlay/stats.oxo")
-  include("levels/templates/spaceship_assff.oxt")
+  include("hudtemplates3.oxo")
+  include("stats.oxo")
+  include("templates/spaceship_assff.oxt")
 ?>
 
Index: code/branches/resource2/data/levels/presentation.oxw
===================================================================
--- code/branches/resource2/data/levels/presentation.oxw	(revision 5653)
+++ code/branches/resource2/data/levels/presentation.oxw	(revision 5654)
@@ -1,6 +1,6 @@
 <?lua
-  include("overlay/hudtemplates3.oxo")
-  include("overlay/stats.oxo")
-  include("levels/templates/spaceship_assff.oxt")
+  include("hudtemplates3.oxo")
+  include("stats.oxo")
+  include("templates/spaceship_assff.oxt")
 ?>
 
Index: code/branches/resource2/data/levels/presentation09.oxw
===================================================================
--- code/branches/resource2/data/levels/presentation09.oxw	(revision 5653)
+++ code/branches/resource2/data/levels/presentation09.oxw	(revision 5654)
@@ -1,7 +1,7 @@
 <?lua
-  include("overlay/hudtemplates3.oxo")
-  include("overlay/stats.oxo")
-  include("levels/templates/spaceship_assff.oxt")
-  include("levels/templates/spaceship_H2.oxt")
+  include("hudtemplates3.oxo")
+  include("stats.oxo")
+  include("templates/spaceship_assff.oxt")
+  include("templates/spaceship_H2.oxt")
 ?>
 
Index: code/branches/resource2/data/levels/presentation09b.oxw
===================================================================
--- code/branches/resource2/data/levels/presentation09b.oxw	(revision 5653)
+++ code/branches/resource2/data/levels/presentation09b.oxw	(revision 5654)
@@ -1,8 +1,8 @@
 <?lua
-  include("overlay/hudtemplates3.oxo")
-  include("overlay/stats.oxo")
-  include("levels/templates/spaceship_assff.oxt")
-  include("levels/templates/spaceship_H2.oxt")
-  include("levels/templates/spaceship_pirate.oxt")
+  include("hudtemplates3.oxo")
+  include("stats.oxo")
+  include("templates/spaceship_assff.oxt")
+  include("templates/spaceship_H2.oxt")
+  include("templates/spaceship_pirate.oxt")
 ?>
 
Index: code/branches/resource2/data/levels/presentation_dm.oxw
===================================================================
--- code/branches/resource2/data/levels/presentation_dm.oxw	(revision 5653)
+++ code/branches/resource2/data/levels/presentation_dm.oxw	(revision 5654)
@@ -1,7 +1,7 @@
 <?lua
-  include("overlay/hudtemplates3.oxo")
-  include("overlay/stats.oxo")
-  include("levels/templates/spaceship_assff.oxt")
-  include("levels/templates/spaceship_H2.oxt")
+  include("hudtemplates3.oxo")
+  include("stats.oxo")
+  include("templates/spaceship_assff.oxt")
+  include("templates/spaceship_H2.oxt")
 ?>
 
Index: code/branches/resource2/data/levels/presentation_pong.oxw
===================================================================
--- code/branches/resource2/data/levels/presentation_pong.oxw	(revision 5653)
+++ code/branches/resource2/data/levels/presentation_pong.oxw	(revision 5654)
@@ -1,6 +1,6 @@
 <?lua
-  include("overlay/hudtemplates3.oxo")
-  include("overlay/stats.oxo")
-  include("overlay/ponghud.oxo")
+  include("hudtemplates3.oxo")
+  include("stats.oxo")
+  include("ponghud.oxo")
 ?>
 
@@ -91,5 +91,5 @@
 
   <?lua
-    dofile("../../media/levels/includes/CuboidSpaceStation.lua")
+    dofile("includes/CuboidSpaceStation.lua")
   ?>
 
Index: code/branches/resource2/data/levels/teambasematchlevel.oxw
===================================================================
--- code/branches/resource2/data/levels/teambasematchlevel.oxw	(revision 5653)
+++ code/branches/resource2/data/levels/teambasematchlevel.oxw	(revision 5654)
@@ -1,8 +1,8 @@
 <?lua
-  include("overlay/hudtemplates3.oxo")
-  include("overlay/stats.oxo")
-  include("overlay/teambasematchhud.oxo")
-  include("levels/templates/spaceship_assff.oxt")
-  include("levels/templates/spaceship_pirate.oxt")
+  include("hudtemplates3.oxo")
+  include("stats.oxo")
+  include("teambasematchhud.oxo")
+  include("templates/spaceship_assff.oxt")
+  include("templates/spaceship_pirate.oxt")
 ?>
 
Index: code/branches/resource2/data/levels/teamdeathmatch.oxw
===================================================================
--- code/branches/resource2/data/levels/teamdeathmatch.oxw	(revision 5653)
+++ code/branches/resource2/data/levels/teamdeathmatch.oxw	(revision 5654)
@@ -1,8 +1,8 @@
 <?lua
-  include("overlay/hudtemplates3.oxo")
-  include("overlay/stats.oxo")
-  include("levels/templates/spaceship_assff.oxt")
-  include("levels/templates/spaceship_H2.oxt")
-  include("levels/templates/spaceship_pirate.oxt")
+  include("hudtemplates3.oxo")
+  include("stats.oxo")
+  include("templates/spaceship_assff.oxt")
+  include("templates/spaceship_H2.oxt")
+  include("templates/spaceship_pirate.oxt")
 ?>
 
Index: code/branches/resource2/data/levels/templates/spaceship_H2.oxt
===================================================================
--- code/branches/resource2/data/levels/templates/spaceship_H2.oxt	(revision 5653)
+++ code/branches/resource2/data/levels/templates/spaceship_H2.oxt	(revision 5654)
@@ -23,5 +23,5 @@
 
 <?lua
-  include("levels/includes/weaponsettings_H2.oxi")
+  include("includes/weaponsettings_H2.oxi")
 ?>
 
Index: code/branches/resource2/data/levels/templates/spaceship_assff.oxt
===================================================================
--- code/branches/resource2/data/levels/templates/spaceship_assff.oxt	(revision 5653)
+++ code/branches/resource2/data/levels/templates/spaceship_assff.oxt	(revision 5654)
@@ -35,5 +35,5 @@
     </collisionShapes>
 <?lua
-  include("levels/includes/weaponsettings3.oxi")
+  include("includes/weaponsettings3.oxi")
 ?>
   </SpaceShip>
Index: code/branches/resource2/data/levels/templates/spaceship_pirate.oxt
===================================================================
--- code/branches/resource2/data/levels/templates/spaceship_pirate.oxt	(revision 5653)
+++ code/branches/resource2/data/levels/templates/spaceship_pirate.oxt	(revision 5654)
@@ -38,5 +38,5 @@
 
 <?lua
-  include("levels/includes/weaponsettings_pirate.oxi")
+  include("includes/weaponsettings_pirate.oxi")
 ?>
   </SpaceShip>
Index: code/branches/resource2/data/levels/tutorial.oxw
===================================================================
--- code/branches/resource2/data/levels/tutorial.oxw	(revision 5653)
+++ code/branches/resource2/data/levels/tutorial.oxw	(revision 5654)
@@ -1,6 +1,6 @@
 <?lua
-  include("overlay/hudtemplates3.oxo")
-  include("overlay/stats.oxo")
-  include("levels/templates/spaceship_assff.oxt")
+  include("hudtemplates3.oxo")
+  include("stats.oxo")
+  include("templates/spaceship_assff.oxt")
 ?>
 
Index: code/branches/resource2/data/levels/waypoints.oxw
===================================================================
--- code/branches/resource2/data/levels/waypoints.oxw	(revision 5653)
+++ code/branches/resource2/data/levels/waypoints.oxw	(revision 5654)
@@ -1,8 +1,8 @@
 <?lua
-  include("overlay/hudtemplates3.oxo")
-  include("overlay/stats.oxo")
-  include("levels/templates/spaceship_assff.oxt")
-  include("levels/templates/spaceship_H2.oxt")
-  include("levels/templates/spaceship_pirate.oxt")
+  include("hudtemplates3.oxo")
+  include("stats.oxo")
+  include("templates/spaceship_assff.oxt")
+  include("templates/spaceship_H2.oxt")
+  include("templates/spaceship_pirate.oxt")
 ?>
 
Index: code/branches/resource2/data/lua/LuaStateInit.lua
===================================================================
--- code/branches/resource2/data/lua/LuaStateInit.lua	(revision 5654)
+++ code/branches/resource2/data/lua/LuaStateInit.lua	(revision 5654)
@@ -0,0 +1,35 @@
+-- Note: luaState is a pointer to the LuaState instance that created this lua state
+
+-- Redirect debug to print
+debug = print
+
+-- Redirect print to the C++ print function
+print = function(s)
+  luaState:luaPrint(s)
+end
+
+-- Create function to log text like COUT, but always prints a line!
+logMessage = function(level, message)
+  luaState:luaLog(level, message)
+end
+
+-- Redirect dofile in order to load with the resource manager
+-- Note: The function does not behave exactly like LuaState::doFile because the
+--       default argument here for the group is not "General" but
+--       "NoResourceGroupProvided". This resolves to the resource group used to
+--       do the current file.
+doFile = function(filename, resourceGroup)
+  local bSearchOtherPaths = (resourceGroup == nil) or false
+  resourceGroup = resourceGroup or "NoResourceGroupProvided"
+  luaState:doFile(filename, resourceGroup, bSearchOtherPaths)
+end
+dofile = doFile
+
+-- Create includeFile function that preparses the file according
+-- to a function provided to the LuaState constructor (in C++)
+-- Note: See the same notes as for doFile
+include = function(filename, resourceGroup)
+  local bSearchOtherPaths = (resourceGroup == nil) or false
+  resourceGroup = resourceGroup or "NoResourceGroupProvided"
+  luaState:includeFile(filename, resourceGroup, bSearchOtherPaths)
+end
Index: code/branches/resource2/data/resources.cfg
===================================================================
--- code/branches/resource2/data/resources.cfg	(revision 5653)
+++ 	(revision )
@@ -1,34 +1,0 @@
-# Resource locations to be added to the 'boostrap' path 
-# This also contains the minimum you need to use the Ogre example framework
-[Bootstrap]
-Zip=packs/OgreCore.zip
-
-# Resource locations to be added to the default path
-[General]
-FileSystem=.
-FileSystem=fonts
-FileSystem=materials/programs
-FileSystem=materials/scripts
-FileSystem=materials/textures
-FileSystem=models
-FileSystem=overlay
-FileSystem=particle
-#FileSystem=../Media/levels
-#FileSystem=../Media/audio
-Zip=packs/cubemap.zip
-#Zip=../Media/packs/cubemapsJS.zip
-#Zip=../Media/packs/dragon.zip
-#Zip=../Media/packs/fresneldemo.zip
-#Zip=../Media/packs/ogretestmap.zip
-Zip=packs/skybox.zip
-FileSystem=gui/fonts
-
-# CEGUI files
-[GUI]
-FileSystem=gui/configs
-FileSystem=gui/fonts
-FileSystem=gui/imagesets
-FileSystem=gui/layouts
-FileSystem=gui/looknfeel
-FileSystem=gui/schemes
-FileSystem=gui/scripts
Index: code/branches/resource2/data/resources.oxr
===================================================================
--- code/branches/resource2/data/resources.oxr	(revision 5653)
+++ code/branches/resource2/data/resources.oxr	(revision 5654)
@@ -1,19 +1,6 @@
-<ResourceGroup name = "General" >
-    <ResourceLocation path = overlay />
-    <ResourceLocation path = particle />
-    <ResourceLocation path = defaultConfig />
-    <ResourceLocation path = levels />
-    <ResourceLocation path = tcl />
-    <ResourceLocation
-      path = test.zip
-      archive = Zip
-      recursive = true
-    />
-</ResourceGroup>
-
-<ResourceGroup name = "GUI" >
-    <ResourceLocation path = gui/configs />
-    <ResourceLocation path = gui/layouts />
-    <ResourceLocation path = gui/schemas />
-    <ResourceLocation path = gui/scripts />
-</ResourceGroup>
+<ResourceCollection resourceGroup = "General" >
+    <ResourceLocation path = "levels" />
+    <ResourceLocation path = "lua" />
+    <ResourceLocation path = "overlay" />
+    <ResourceLocation path = "tcl" />
+</ResourceCollection>
Index: code/branches/resource2/data/resources_graphics.oxr
===================================================================
--- code/branches/resource2/data/resources_graphics.oxr	(revision 5654)
+++ code/branches/resource2/data/resources_graphics.oxr	(revision 5654)
@@ -0,0 +1,10 @@
+<ResourceCollection resourceGroup = "General" >
+    <ResourceLocation path = "particle" />
+</ResourceCollection>
+
+<ResourceCollection resourceGroup = "GUI" >
+    <ResourceLocation path = "gui/configs" />
+    <ResourceLocation path = "gui/layouts" />
+    <ResourceLocation path = "gui/schemes" />
+    <ResourceLocation path = "gui/scripts" />
+</ResourceCollection>
Index: code/branches/resource2/src/core/CMakeLists.txt
===================================================================
--- code/branches/resource2/src/core/CMakeLists.txt	(revision 5653)
+++ code/branches/resource2/src/core/CMakeLists.txt	(revision 5654)
@@ -30,5 +30,5 @@
   GUIManager.cc
   Language.cc
-  LuaBind.cc
+  LuaState.cc
   ObjectListBase.cc
   OrxonoxClass.cc
@@ -76,5 +76,6 @@
   TOLUA_FILES
     CommandExecutor.h
-    LuaBind.h
+    Loader.h
+    LuaState.h
   DEFINE_SYMBOL
     "CORE_SHARED_BUILD"
Index: code/branches/resource2/src/core/Core.cc
===================================================================
--- code/branches/resource2/src/core/Core.cc	(revision 5653)
+++ code/branches/resource2/src/core/Core.cc	(revision 5654)
@@ -74,8 +74,9 @@
 #include "Identifier.h"
 #include "Language.h"
-#include "LuaBind.h"
+#include "LuaState.h"
 #include "Shell.h"
 #include "TclBind.h"
 #include "TclThreadManager.h"
+#include "ToluaBindCore.h"
 #include "input/InputManager.h"
 
@@ -266,4 +267,6 @@
             setThreadAffinity(static_cast<unsigned int>(limitToCPU));
 #endif
+        // Add tolua interface
+        LuaState::addToluaInterface(&tolua_Core_open, "Core");
 
         // Manage ini files and set the default settings file (usually orxonox.ini)
@@ -275,10 +278,13 @@
         this->languageInstance_.reset(new Language());
 
+        // creates the class hierarchy for all classes with factories
+        Factory::createClassHierarchy();
+
         // Do this soon after the ConfigFileManager has been created to open up the
         // possibility to configure everything below here
         this->configuration_->initialise();
 
-        // Create the lua interface
-        this->luaBind_.reset(new LuaBind());
+        // Load OGRE excluding the renderer and the render window
+        this->graphicsManager_.reset(new GraphicsManager(false));
 
         // initialise Tcl
@@ -288,15 +294,9 @@
         // create a shell
         this->shell_.reset(new Shell());
-
-        // creates the class hierarchy for all classes with factories
-        Factory::createClassHierarchy();
-
-        // Load OGRE excluding the renderer and the render window
-        this->graphicsManager_.reset(new GraphicsManager(false));
     }
 
     /**
     @brief
-        All destruction code is handled by scoped_ptrs and SimpleScopeGuards.
+        All destruction code is handled by scoped_ptrs and ScopeGuards.
     */
     Core::~Core()
@@ -561,5 +561,5 @@
             COUT(1) << "Running from the build tree." << std::endl;
             Core::bDevRun_ = true;
-            configuration_->dataPath_  = specialConfig::dataDevDirectory;
+            configuration_->dataPath_   = specialConfig::dataDevDirectory;
             configuration_->externalDataPath_ = specialConfig::externalDataDevDirectory;
             configuration_->configPath_ = specialConfig::configDevDirectory;
@@ -579,5 +579,5 @@
 
             // Using paths relative to the install prefix, complete them
-            configuration_->dataPath_  = configuration_->rootPath_ / specialConfig::defaultDataPath;
+            configuration_->dataPath_   = configuration_->rootPath_ / specialConfig::defaultDataPath;
             configuration_->configPath_ = configuration_->rootPath_ / specialConfig::defaultConfigPath;
             configuration_->logPath_    = configuration_->rootPath_ / specialConfig::defaultLogPath;
Index: code/branches/resource2/src/core/Core.h
===================================================================
--- code/branches/resource2/src/core/Core.h	(revision 5653)
+++ code/branches/resource2/src/core/Core.h	(revision 5654)
@@ -129,5 +129,4 @@
             scoped_ptr<Language>          languageInstance_;
             scoped_ptr<CoreConfiguration> configuration_;
-            scoped_ptr<LuaBind>           luaBind_;
             scoped_ptr<TclBind>           tclBind_;
             scoped_ptr<TclThreadManager>  tclThreadManager_;
Index: code/branches/resource2/src/core/CorePrereqs.h
===================================================================
--- code/branches/resource2/src/core/CorePrereqs.h	(revision 5653)
+++ code/branches/resource2/src/core/CorePrereqs.h	(revision 5654)
@@ -133,5 +133,5 @@
     class LanguageEntry;
     class Loader;
-    class LuaBind;
+    class LuaState;
     class MetaObjectList;
     class MetaObjectListElement;
Index: code/branches/resource2/src/core/GUIManager.cc
===================================================================
--- code/branches/resource2/src/core/GUIManager.cc	(revision 5653)
+++ code/branches/resource2/src/core/GUIManager.cc	(revision 5654)
@@ -59,5 +59,5 @@
 #include "Core.h"
 #include "Clock.h"
-#include "LuaBind.h"
+#include "LuaState.h"
 
 namespace orxonox
@@ -132,5 +132,5 @@
 
             // do this after 'new CEGUI::Sytem' because that creates the lua state in the first place
-            LuaBind::getInstance().openToluaInterfaces(this->luaState_);
+            LuaState::openToluaInterfaces(this->luaState_);
 
             // initialise the basic lua code
@@ -157,5 +157,5 @@
     {
         // destroy our own tolua interfaces
-        LuaBind::getInstance().closeToluaInterfaces(this->luaState_);
+        LuaState::closeToluaInterfaces(this->luaState_);
     }
 
Index: code/branches/resource2/src/core/GraphicsManager.cc
===================================================================
--- code/branches/resource2/src/core/GraphicsManager.cc	(revision 5653)
+++ code/branches/resource2/src/core/GraphicsManager.cc	(revision 5654)
@@ -45,4 +45,5 @@
 #include <OgreRenderWindow.h>
 #include <OgreRenderSystem.h>
+#include <OgreResourceGroupManager.h>
 #include <OgreViewport.h>
 #include <OgreWindowEventUtilities.h>
@@ -59,5 +60,7 @@
 #include "Game.h"
 #include "GameMode.h"
+#include "Loader.h"
 #include "WindowEventListener.h"
+#include "XMLFile.h"
 
 namespace orxonox
@@ -96,8 +99,22 @@
         this->loadOgrePlugins();
 
+        // At first, add the root paths of the data directories as resource locations
+        Ogre::ResourceGroupManager::getSingleton().addResourceLocation(Core::getDataPathString(), "FileSystem", "dataRoot", false);
+        // Load resources
+        resources_.reset(new XMLFile("resources.oxr", "dataRoot"));
+        Loader::open(resources_.get());
+
+        // Only for development runs
+        if (Core::isDevelopmentRun())
+        {
+            Ogre::ResourceGroupManager::getSingleton().addResourceLocation(Core::getExternalDataPathString(), "FileSystem", "externalDataRoot", false);
+            extResources_.reset(new XMLFile("resources.oxr", "externalDataRoot"));
+            Loader::open(extResources_.get());
+        }
+
         if (bLoadRenderer)
         {
             // Reads the ogre config and creates the render window
-            this->loadRenderer();
+            this->upgradeToGraphics();
         }
     }
@@ -109,4 +126,5 @@
     GraphicsManager::~GraphicsManager()
     {
+        // TODO: Destroy the console command
     }
 
@@ -137,8 +155,27 @@
     void GraphicsManager::upgradeToGraphics()
     {
-        if (renderWindow_ == NULL)
-        {
-            this->loadRenderer();
-        }
+        if (renderWindow_ != NULL)
+            return;
+
+        this->loadRenderer();
+
+        // RESOURCE MANAGEMENT
+
+        // Load graphical resources
+        resourcesGraphics_.reset(new XMLFile("resources_graphics.oxr", "dataRoot"));
+        Loader::open(resourcesGraphics_.get());
+
+        // Consider external data folder for dev runs
+        if (Core::isDevelopmentRun())
+        {
+            extResourcesGraphics_.reset(new XMLFile("resources_graphics.oxr", "externalDataRoot"));
+            Loader::open(extResourcesGraphics_.get());
+        }
+
+        // Initialise all resources
+        // Note: You can only do this once! Ogre will check whether a resource group has
+        // already been initialised. If you need to load resources later, you will have to
+        // choose another resource group.
+        Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
     }
 
Index: code/branches/resource2/src/core/GraphicsManager.h
===================================================================
--- code/branches/resource2/src/core/GraphicsManager.h	(revision 5653)
+++ code/branches/resource2/src/core/GraphicsManager.h	(revision 5654)
@@ -43,4 +43,5 @@
 #include <OgreLog.h>
 #include <boost/scoped_ptr.hpp>
+#include <boost/shared_ptr.hpp>
 
 #include "util/Singleton.h"
@@ -87,5 +88,4 @@
         void printScreen();
 
-    private:
         scoped_ptr<OgreWindowEventListener> ogreWindowEventListener_; //!< Pimpl to hide OgreWindowUtilities.h
         scoped_ptr<Ogre::LogManager>        ogreLogger_;
@@ -93,4 +93,10 @@
         Ogre::RenderWindow* renderWindow_;             //!< the one and only render window
         Ogre::Viewport*     viewport_;                 //!< default full size viewport
+
+        // XML files for the resources
+        shared_ptr<XMLFile> resources_;                //!< XML with resource locations
+        shared_ptr<XMLFile> extResources_;             //!< XML with resource locations in the external path (only for dev runs)
+        shared_ptr<XMLFile> resourcesGraphics_;        //!< XML with resource locations (with renderer)
+        shared_ptr<XMLFile> extResourcesGraphics_;     //!< XML with resource locations in the external path (only for dev runs) (with renderer)
 
         // config values
Index: code/branches/resource2/src/core/Loader.cc
===================================================================
--- code/branches/resource2/src/core/Loader.cc	(revision 5653)
+++ code/branches/resource2/src/core/Loader.cc	(revision 5654)
@@ -29,13 +29,17 @@
 #include "Loader.h"
 
+#include <sstream>
 #include <tinyxml/ticpp.h>
+#include <boost/scoped_ptr.hpp>
 
 #include "util/Debug.h"
 #include "util/Exception.h"
+#include "util/StringUtils.h"
 #include "BaseObject.h"
 #include "Iterator.h"
 #include "ObjectList.h"
-#include "LuaBind.h"
+#include "LuaState.h"
 #include "Namespace.h"
+#include "Resource.h"
 #include "XMLFile.h"
 
@@ -118,9 +122,9 @@
         Loader::currentMask_s = file->getMask() * mask;
 
-        // let Lua work this out:
-        LuaBind& lua = LuaBind::getInstance();
-        lua.clearLuaOutput();
-        lua.loadFile(file->getFilename(), true);
-        lua.run();
+        // Use the LuaState to replace the XML tags (calls our function)
+        scoped_ptr<LuaState> luaState(new LuaState());
+        luaState->setIncludeParser(&Loader::replaceLuaTags);
+        luaState->includeFile(file->getFilename(), file->getResourceGroup(), false);
+        //luaState->doString(luaInput);
 
         try
@@ -129,10 +133,6 @@
             COUT(3) << "Mask: " << Loader::currentMask_s << std::endl;
 
-            //ticpp::Document xmlfile(file->getFilename());
-            //xmlfile.LoadFile();
-            //ticpp::Element myelement(*Script::getFileString());
-            ticpp::Document xmlfile;
-            //xmlfile.ToDocument();
-            xmlfile.Parse(lua.getLuaOutput(), true);
+            ticpp::Document xmlfile(file->getFilename());
+            xmlfile.Parse(luaState->getOutput().str(), true);
 
             ticpp::Element rootElement;
@@ -208,3 +208,154 @@
         return Loader::load(file, mask);
     }
+
+    std::string Loader::replaceLuaTags(const std::string& text)
+    {
+        // chreate map with all Lua tags
+        std::map<size_t, bool> luaTags;
+        {
+            size_t pos = 0;
+            while ((pos = text.find("<?lua", pos)) != std::string::npos)
+                luaTags[pos++] = true;
+        }
+        {
+            size_t pos = 0;
+            while ((pos = text.find("?>", pos)) != std::string::npos)
+                luaTags[pos++] = false;
+        }
+
+        // erase all tags from the map that are between two quotes
+        {
+            std::map<size_t, bool>::iterator it = luaTags.begin();
+            std::map<size_t, bool>::iterator it2 = it;
+            bool bBetweenQuotes = false;
+            size_t pos = 0;
+            while ((pos = getNextQuote(text, pos)) != std::string::npos)
+            {
+                while ((it != luaTags.end()) && (it->first < pos))
+                {
+                    if (bBetweenQuotes)
+                    {
+                        it2++;
+                        if(it->second && !(it2->second) && it2->first < pos)
+                            it = ++it2;
+                        else
+                            luaTags.erase(it++);
+                    }
+                    else
+                        ++it;
+                }
+                bBetweenQuotes = !bBetweenQuotes;
+                pos++;
+            }
+        }
+
+        // check whether on every opening <?lua tag a closing ?> tag follows
+        {
+            bool expectedValue = true;
+            for (std::map<size_t, bool>::iterator it = luaTags.begin(); it != luaTags.end(); ++it)
+            {
+                if (it->second == expectedValue)
+                    expectedValue = !expectedValue;
+                else
+                {
+                    expectedValue = false;
+                    break;
+                }
+            }
+            if (!expectedValue)
+            {
+                COUT(2) << "Warning: Error in level file" << std::endl;
+                // todo: errorhandling
+                return "";
+            }
+        }
+
+        // Use a stringstream object to speed up the parsing
+        std::ostringstream output;
+
+        // cut the original string into pieces and put them together with print() instead of lua tags
+        {
+            std::map<size_t, bool>::iterator it = luaTags.begin();
+            bool bInPrintFunction = true;
+            size_t start = 0;
+            size_t end = 0;
+
+            do
+            {
+                if (it != luaTags.end())
+                    end = (*(it++)).first;
+                else
+                    end = std::string::npos;
+
+                unsigned int equalSignCounter = 0;
+
+                if (bInPrintFunction)
+                {
+                    // count ['='[ and ]'='] and replace tags with print([[ and ]])
+                    std::string temp = text.substr(start, end - start);
+                    {
+                    size_t pos = 0;
+                    while ((pos = temp.find('[', pos)) != std::string::npos)
+                    {
+                        unsigned int tempCounter = 1;
+                        size_t tempPos = pos++;
+                        while(temp[++tempPos] == '=')
+                        {
+                            tempCounter++;
+                        }
+                        if(temp[tempPos] != '[')
+                        {
+                            tempCounter = 0;
+                        }
+                        else if(tempCounter == 0)
+                        {
+                            tempCounter = 1;
+                        }
+                        if (tempCounter > equalSignCounter)
+                            equalSignCounter = tempCounter;
+                        }
+                    }
+                    {
+                        size_t pos = 0;
+                        while ((pos = temp.find(']', pos)) != std::string::npos)
+                        {
+                            unsigned int tempCounter = 1;
+                            size_t tempPos = pos++;
+                            while(temp[++tempPos] == '=')
+                            {
+                                tempCounter++;
+                            }
+                            if(temp[tempPos] != ']')
+                            {
+                                tempCounter = 0;
+                            }
+                            else if(tempCounter == 0)
+                            {
+                                tempCounter = 1;
+                            }
+                            if (tempCounter > equalSignCounter)
+                                equalSignCounter = tempCounter;
+                        }
+                    }
+                    std::string equalSigns = "";
+                    for(unsigned int i = 0; i < equalSignCounter; i++)
+                    {
+                        equalSigns += "=";
+                    }
+                    output << "print([" + equalSigns + "[" + temp + "]" + equalSigns +"])";
+                    start = end + 5;
+                }
+                else
+                {
+                    output << text.substr(start, end - start);
+                    start = end + 2;
+                }
+
+                bInPrintFunction = !bInPrintFunction;
+            }
+            while (end != std::string::npos);
+        }
+
+        return output.str();
+    }
 }
Index: code/branches/resource2/src/core/Loader.h
===================================================================
--- code/branches/resource2/src/core/Loader.h	(revision 5653)
+++ code/branches/resource2/src/core/Loader.h	(revision 5654)
@@ -55,4 +55,6 @@
             static bool reload(const XMLFile* file, const ClassTreeMask& mask = ClassTreeMask());
 
+            static std::string replaceLuaTags(const std::string& text);
+
             static ClassTreeMask currentMask_s;
 
Index: code/branches/resource2/src/core/LuaBind.cc
===================================================================
--- code/branches/resource2/src/core/LuaBind.cc	(revision 5653)
+++ 	(revision )
@@ -1,343 +1,0 @@
-/*
- *   ORXONOX - the hottest 3D action shooter ever to exist
- *                    > www.orxonox.net <
- *
- *
- *   License notice:
- *
- *   This program is free software; you can redistribute it and/or
- *   modify it under the terms of the GNU General Public License
- *   as published by the Free Software Foundation; either version 2
- *   of the License, or (at your option) any later version.
- *
- *   This program is distributed in the hope that it will be useful,
- *   but WITHOUT ANY WARRANTY; without even the implied warranty of
- *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *   GNU General Public License for more details.
- *
- *   You should have received a copy of the GNU General Public License
- *   along with this program; if not, write to the Free Software
- *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
- *
- *   Author:
- *      Benjamin Knecht
- *   Co-authors:
- *      ...
- *
- */
-
-#include "LuaBind.h"
-
-#include <fstream>
-#include <map>
-extern "C" {
-#include <lualib.h>
-}
-#include <tolua/tolua++.h>
-#include <boost/filesystem.hpp>
-
-#include "util/Debug.h"
-#include "util/StringUtils.h"
-#include "Core.h"
-
-namespace orxonox
-{
-  LuaBind* LuaBind::singletonPtr_s = NULL;
-
-  LuaBind::LuaBind()
-  {
-    this->includePath_ = Core::getDataPathString();
-
-    luaState_ = lua_open();
-    luaSource_ = "";
-#if LUA_VERSION_NUM == 501
-    luaL_openlibs(luaState_);
-#else
-    luaopen_base(luaState_);
-    luaopen_string(luaState_);
-    luaopen_table(luaState_);
-    luaopen_math(luaState_);
-    luaopen_io(luaState_);
-    luaopen_debug(luaState_);
-#endif
-
-    // Open all available tolua interfaces
-    this->openToluaInterfaces(luaState_);
-
-    output_ = "";
-    isRunning_ = false;
-  }
-
-  LuaBind::~LuaBind()
-  {
-    this->closeToluaInterfaces(luaState_);
-  };
-
-  void LuaBind::luaPrint(const std::string& str)
-  {
-    output_ += str;
-//    COUT(4) << "Lua_output!:" << std::endl << str << std::endl << "***" << std::endl;
-    COUT(5) << str;
-  }
-
-  /**
-      @brief Loads the specified file line by line
-      @param filename The filename of the file
-      @param luaTags if true, the loaded file gets stripped off luaTags
-  */
-  void LuaBind::loadFile(const std::string& filename, bool luaTags)
-  {
-    boost::filesystem::path filepath(filename);
-
-    output_ = "";
-    std::ifstream file;
-    file.open(filepath.string().c_str(), std::fstream::in);
-
-    if (!file.is_open())
-    {
-      // some error msg
-    }
-
-    std::string levelString = "";
-
-    while (file.good() && !file.eof())
-    {
-      std::string line;
-      std::getline(file, line);
-      levelString += line;
-      levelString += "\n";
-    }
-
-    file.close();
-    //std::string output;
-
-    if (luaTags)
-      luaSource_ = replaceLuaTags(levelString);
-    else
-      luaSource_ = levelString;
-    COUT(5) << "ParsedSourceCode: " << luaSource_ << std::endl;
-  }
-
-  void LuaBind::loadString(const std::string& code)
-  {
-    luaSource_ = code;
-    output_ = "";
-  }
-
-#if LUA_VERSION_NUM != 501
-  const char * LuaBind::lua_Chunkreader(lua_State *L, void *data, size_t *size)
-  {
-    LoadS* ls = static_cast<LoadS*>(data);
-    if (ls->size == 0) return NULL;
-    *size = ls->size;
-    ls->size = 0;
-    return ls->s;
-  }
-#endif
-  void LuaBind::run()
-  {
-    if (!isRunning_)
-    {
-      isRunning_ = true;
-      int error = 0;
-      std::string init =
-         "local scr = orxonox.LuaBind:getInstance()\n \
-          local debug = print\n \
-          print = function(s)\n \
-              scr:luaPrint(s)\n \
-          end\n \
-          include = function(f)\n \
-              file = assert(io.open(\"" + this->includePath_ + "\"..\"/\"..f))\n \
-              content = file:read(\"*a\")\n \
-              file:close()\n \
-              source = scr:replaceLuaTags(content)\n \
-              assert(loadstring(source))()\n \
-          end\n";
-      init += luaSource_;
-  #if LUA_VERSION_NUM == 501
-      error = luaL_loadstring(luaState_, init.c_str());
-  #else
-      LoadS ls;
-      ls.s = init.c_str();
-      ls.size = init.size();
-      error = lua_load(luaState_, &orxonox::LuaBind::lua_Chunkreader, &ls, init.c_str());
-  #endif
-      if (error == 0)
-      {
-        error = lua_pcall(luaState_, 0, 0, 0);
-      }
-      if (error != 0)
-      {
-        COUT(2) << "Error in Lua-script: " << lua_tostring(luaState_, -1) << std::endl;
-      }
-      isRunning_ = false;
-    }
-    else
-    {
-      COUT(2) << "Warning: Lua's run is called while running!" << std::endl;
-    }
-  }
-
-  std::string LuaBind::replaceLuaTags(const std::string& text)
-  {
-    // chreate map with all Lua tags
-    std::map<size_t, bool> luaTags;
-    {
-      size_t pos = 0;
-      while ((pos = text.find("<?lua", pos)) != std::string::npos)
-        luaTags[pos++] = true;
-    }
-    {
-      size_t pos = 0;
-      while ((pos = text.find("?>", pos)) != std::string::npos)
-        luaTags[pos++] = false;
-    }
-
-    // erase all tags from the map that are between two quotes
-    {
-      std::map<size_t, bool>::iterator it = luaTags.begin();
-      std::map<size_t, bool>::iterator it2 = it;
-      bool bBetweenQuotes = false;
-      size_t pos = 0;
-      while ((pos = getNextQuote(text, pos)) != std::string::npos)
-      {
-        while ((it != luaTags.end()) && (it->first < pos))
-        {
-          if (bBetweenQuotes) {
-            it2++;
-            if(it->second && !(it2->second) && it2->first < pos)
-              it = ++it2;
-            else
-              luaTags.erase(it++);
-          }
-          else
-            ++it;
-        }
-        bBetweenQuotes = !bBetweenQuotes;
-        pos++;
-      }
-    }
-
-    // check whether on every opening <?lua tag a closing ?> tag follows
-    {
-      bool expectedValue = true;
-      for (std::map<size_t, bool>::iterator it = luaTags.begin(); it != luaTags.end(); ++it)
-      {
-        if (it->second == expectedValue)
-          expectedValue = !expectedValue;
-        else
-        {
-          expectedValue = false;
-          break;
-        }
-      }
-      if (!expectedValue) {
-        COUT(2) << "Warning: Error in level file" << std::endl;
-        // todo: errorhandling
-        return "";
-      }
-    }
-
-    // cut the original string into pieces and put them together with print() instead of lua tags
-    std::string output;
-    {
-      std::map<size_t, bool>::iterator it = luaTags.begin();
-      bool bInPrintFunction = true;
-      size_t start = 0;
-      size_t end = 0;
-
-      do
-      {
-        if (it != luaTags.end())
-          end = (*(it++)).first;
-        else
-          end = std::string::npos;
-
-        unsigned int equalSignCounter = 0;
-
-        if (bInPrintFunction)
-        {
-          // count ['='[ and ]'='] and replace tags with print([[ and ]])
-          std::string temp = text.substr(start, end - start);
-          {
-            size_t pos = 0;
-            while ((pos = temp.find('[', pos)) != std::string::npos)
-            {
-              unsigned int tempCounter = 1;
-              size_t tempPos = pos++;
-              while(temp[++tempPos] == '=') {
-                tempCounter++;
-              }
-              if(temp[tempPos] != '[') {
-                tempCounter = 0;
-              }
-              else if(tempCounter == 0) {
-                tempCounter = 1;
-              }
-              if (tempCounter > equalSignCounter)
-                equalSignCounter = tempCounter;
-            }
-          }
-          {
-            size_t pos = 0;
-            while ((pos = temp.find(']', pos)) != std::string::npos)
-            {
-              unsigned int tempCounter = 1;
-              size_t tempPos = pos++;
-              while(temp[++tempPos] == '=') {
-                tempCounter++;
-              }
-              if(temp[tempPos] != ']') {
-                tempCounter = 0;
-              }
-              else if(tempCounter == 0) {
-                tempCounter = 1;
-              }
-              if (tempCounter > equalSignCounter)
-                equalSignCounter = tempCounter;
-            }
-          }
-          std::string equalSigns = "";
-          for(unsigned int i = 0; i < equalSignCounter; i++) {
-            equalSigns += "=";
-          }
-          output += "print([" + equalSigns + "[" + temp + "]" + equalSigns +"])";
-          start = end + 5;
-        }
-        else
-        {
-          output += text.substr(start, end - start);
-          start = end + 2;
-        }
-
-        bInPrintFunction = !bInPrintFunction;
-      }
-      while (end != std::string::npos);
-    }
-
-    return output;
-  }
-
-  void LuaBind::addToluaInterface(int (*function)(lua_State*), const std::string& name)
-  {
-    toluaInterfaces_.push_back(std::make_pair(name, function));
-    // Apply changes to our own lua state as well 
-    (*function)(luaState_);
-  }
-
-  void LuaBind::openToluaInterfaces(lua_State* state)
-  {
-    for (unsigned int i = 0; i < toluaInterfaces_.size(); ++i)
-      (*toluaInterfaces_[i].second)(state);
-  }
-
-  void LuaBind::closeToluaInterfaces(lua_State* state)
-  {
-    for (unsigned int i = 0; i < toluaInterfaces_.size(); ++i)
-    {
-      lua_pushnil(state);
-      lua_setglobal(state, toluaInterfaces_[i].first.c_str());
-    }
-  }
-
-}
Index: code/branches/resource2/src/core/LuaBind.h
===================================================================
--- code/branches/resource2/src/core/LuaBind.h	(revision 5653)
+++ 	(revision )
@@ -1,105 +1,0 @@
-/*
- *   ORXONOX - the hottest 3D action shooter ever to exist
- *                    > www.orxonox.net <
- *
- *
- *   License notice:
- *
- *   This program is free software; you can redistribute it and/or
- *   modify it under the terms of the GNU General Public License
- *   as published by the Free Software Foundation; either version 2
- *   of the License, or (at your option) any later version.
- *
- *   This program is distributed in the hope that it will be useful,
- *   but WITHOUT ANY WARRANTY; without even the implied warranty of
- *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *   GNU General Public License for more details.
- *
- *   You should have received a copy of the GNU General Public License
- *   along with this program; if not, write to the Free Software
- *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
- *
- *   Author:
- *      Benjamin Knecht
- *   Co-authors:
- *      ...
- *
- */
-
-/**
- @file
- @brief Representation of an interface to lua
- @author Benjamin Knecht <beni_at_orxonox.net>
- */
-
-#ifndef _LuaBind_H__
-#define _LuaBind_H__
-
-#include "CorePrereqs.h"
-
-#include <cassert>
-#include <string>
-#include <vector>
-extern "C" {
-#include <lua.h>
-}
-
-#include "util/Singleton.h"
-
-// tolua_begin
-namespace orxonox
-{
-  class _CoreExport LuaBind : public Singleton<LuaBind>
-  {
-// tolua_end
-    friend class Singleton<LuaBind>;
-
-    struct LoadS {
-      const char *s;
-      size_t size;
-    };
-
-    public:
-      LuaBind();
-      ~LuaBind();
-
-      static LuaBind& getInstance() { return Singleton<LuaBind>::getInstance(); } // tolua_export
-
-    void loadFile(const std::string& filename, bool luaTags);
-    void loadString(const std::string& code);
-    //void init(lua_State *state_);
-    //void xmlToLua();
-    void run();
-    void luaPrint(const std::string& str); // tolua_export
-
-#if LUA_VERSION_NUM != 501
-    static const char * lua_Chunkreader(lua_State *L, void *data, size_t *size);
-#endif
-
-    inline lua_State* getLuaState() { return luaState_; };
-    inline const std::string& getLuaOutput() { return output_; };
-    //inline std::string* getFileString() { return &fileString_; };
-    inline void clearLuaOutput() { output_ = ""; }
-
-    std::string replaceLuaTags(const std::string& text); // tolua_export
-
-    inline void setIncludePath(const std::string& includepath)
-        { this->includePath_ = includepath; }
-
-    void addToluaInterface(int (*function)(lua_State*), const std::string& name);
-    void openToluaInterfaces(lua_State* state);
-    void closeToluaInterfaces(lua_State* state);
-
-    private:
-      static LuaBind* singletonPtr_s;
-
-      std::string luaSource_;
-      std::string output_;
-      lua_State* luaState_;
-      bool isRunning_;
-      std::string includePath_;
-      std::vector<std::pair<std::string, int (*)(lua_State *L)> > toluaInterfaces_;
-
-  }; // tolua_export
-} // tolua_export
-#endif /* _LuaBind_H__ */
Index: code/branches/resource2/src/core/LuaState.cc
===================================================================
--- code/branches/resource2/src/core/LuaState.cc	(revision 5654)
+++ code/branches/resource2/src/core/LuaState.cc	(revision 5654)
@@ -0,0 +1,261 @@
+/*
+ *   ORXONOX - the hottest 3D action shooter ever to exist
+ *                    > www.orxonox.net <
+ *
+ *
+ *   License notice:
+ *
+ *   This program is free software; you can redistribute it and/or
+ *   modify it under the terms of the GNU General Public License
+ *   as published by the Free Software Foundation; either version 2
+ *   of the License, or (at your option) any later version.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ *
+ *   You should have received a copy of the GNU General Public License
+ *   along with this program; if not, write to the Free Software
+ *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ *
+ *   Author:
+ *      Benjamin Knecht
+ *      Reto Grieder
+ *   Co-authors:
+ *      ...
+ *
+ */
+
+#include "LuaState.h"
+
+#include <tolua/tolua++.h>
+extern "C" {
+#include <lua.h>
+#include <lualib.h>
+}
+
+#include "util/Debug.h"
+#include "Core.h"
+#include "Resource.h"
+
+namespace orxonox
+{
+    LuaState::ToluaInterfaceMap LuaState::toluaInterfaces_s;
+    std::vector<LuaState*> LuaState::instances_s;
+
+    LuaState::LuaState()
+        : bIsRunning_(false)
+        , includeParseFunction_(NULL)
+    {
+        // Create new lua state and configure it
+        luaState_ = lua_open();
+#if LUA_VERSION_NUM == 501
+        luaL_openlibs(luaState_);
+#else
+        luaopen_base(luaState_);
+        luaopen_string(luaState_);
+        luaopen_table(luaState_);
+        luaopen_math(luaState_);
+        luaopen_io(luaState_);
+        luaopen_debug(luaState_);
+#endif
+
+        // Open all available tolua interfaces
+        this->openToluaInterfaces(luaState_);
+
+        // Create dummy file info
+        sourceFileInfo_.reset(new ResourceInfo());
+        sourceFileInfo_->group = "General";
+        sourceFileInfo_->size = 0;
+
+        // Push this pointer
+        tolua_pushusertype(luaState_, static_cast<void*>(this), "orxonox::LuaState");
+        lua_setglobal(luaState_, "luaState");
+
+        // Parse init script
+        // Note: We have to use a hard coded path because the script is required for the resource loading
+        this->doString("dofile(\"" + Core::getDataPathString() + "lua/LuaStateInit.lua\")");
+    }
+
+    LuaState::~LuaState()
+    {
+        lua_close(luaState_);
+    }
+
+    shared_ptr<ResourceInfo> LuaState::getFileInfo(const std::string& filename, const std::string& resourceGroup, bool bSearchOtherPaths)
+    {
+        shared_ptr<ResourceInfo> sourceInfo;
+        if (resourceGroup != "NoResourceGroupProvided")
+            sourceInfo = Resource::getInfo(filename, resourceGroup);
+
+        // Continue search if not explicitely forbidden
+        if (bSearchOtherPaths && sourceInfo == NULL)
+        {
+            // Call might be relative to the file currently being processed
+            sourceInfo = Resource::getInfo(sourceFileInfo_->path + filename, sourceFileInfo_->group);
+            if (sourceInfo == NULL)
+            {
+                // Maybe find something in the same group but in the root path
+                sourceInfo = Resource::getInfo(filename, sourceFileInfo_->group);
+            }
+        }
+        return sourceInfo;
+    }
+
+    void LuaState::includeFile(const std::string& filename, const std::string& resourceGroup, bool bSearchOtherPaths)
+    {
+        shared_ptr<ResourceInfo> sourceInfo = this->getFileInfo(filename, resourceGroup, bSearchOtherPaths);
+        if (sourceInfo != NULL)
+            this->includeString(Resource::open(sourceInfo->filename, sourceInfo->group)->getAsString(), sourceInfo);
+        else
+            COUT(2) << "LuaState: Cannot include file '" << filename << "' in resource group '"
+                    << (resourceGroup == "NoResourceGroupProvided" ? sourceFileInfo_->group : resourceGroup) << "': group not found." << std::endl;
+    }
+
+    void LuaState::includeString(const std::string& code, shared_ptr<ResourceInfo> sourceFileInfo)
+    {
+        // Parse string with provided include parser (otherwise don't preparse at all)
+        std::string luaInput;
+        if (includeParseFunction_ != NULL)
+            luaInput = (*includeParseFunction_)(code);
+        else
+            luaInput = code;
+
+        this->doString(luaInput, sourceFileInfo);
+    }
+
+    void LuaState::doFile(const std::string& filename, const std::string& resourceGroup, bool bSearchOtherPaths)
+    {
+        shared_ptr<ResourceInfo> sourceInfo = this->getFileInfo(filename, resourceGroup, bSearchOtherPaths);
+        if (sourceInfo != NULL)
+            this->doString(Resource::open(sourceInfo->filename, sourceInfo->group)->getAsString(), sourceInfo);
+        else
+            COUT(2) << "LuaState: Cannot do file '" << filename << "' in resource group '"
+                << (resourceGroup == "NoResourceGroupProvided" ? sourceFileInfo_->group : resourceGroup) << "': group not found." << std::endl;
+    }
+
+    void LuaState::doString(const std::string& code, shared_ptr<ResourceInfo> sourceFileInfo)
+    {
+        // Save the oold source file info
+        shared_ptr<ResourceInfo> oldSourceFileInfo = sourceFileInfo_;
+        // Only override if sourceFileInfo provides useful information
+        if (sourceFileInfo != NULL)
+            sourceFileInfo_ = sourceFileInfo;
+
+        //if (!bIsRunning_)
+        //{
+        //    bIsRunning_ = true;
+
+        int error = 0;
+#if LUA_VERSION_NUM != 501
+        LoadS ls;
+        ls.s = code.c_str();
+        ls.size = code.size();
+        error = lua_load(luaState_, &orxonox::LuaState::lua_Chunkreader, &ls, code.c_str());
+#else
+        error = luaL_loadstring(luaState_, code.c_str());
+#endif
+
+        // execute the chunk
+        if (error == 0)
+            error = lua_pcall(luaState_, 0, 0, 0);
+        if (error != 0)
+        {
+            std::string origin;
+            if (sourceFileInfo != NULL)
+                origin = " originating from " + sourceFileInfo_->filename;
+            COUT(2) << "Error in Lua-script" << origin << ": " << lua_tostring(luaState_, -1) << std::endl;
+        }
+
+        //    bIsRunning_ = false;
+        //}
+        //else
+        //{
+        //    COUT(2) << "Warning: LuaState do function called while running!" << std::endl;
+        //}
+
+        // Load the old info again
+        sourceFileInfo_ = oldSourceFileInfo;
+    }
+
+    void LuaState::luaPrint(const std::string& str)
+    {
+        output_ << str;
+    }
+
+    void LuaState::luaLog(unsigned int level, const std::string& message)
+    {
+        OutputHandler::getOutStream().setOutputLevel(level) << message << std::endl;
+    }
+
+#if LUA_VERSION_NUM != 501
+    const char * LuaState::lua_Chunkreader(lua_State *L, void *data, size_t *size)
+    {
+        LoadS* ls = static_cast<LoadS*>(data);
+        if (ls->size == 0)
+            return NULL;
+        *size = ls->size;
+        ls->size = 0;
+        return ls->s;
+    }
+#endif
+
+    /*static*/ bool LuaState::addToluaInterface(int (*function)(lua_State*), const std::string& name)
+    {
+        for (ToluaInterfaceMap::const_iterator it = toluaInterfaces_s.begin(); it != toluaInterfaces_s.end(); ++it)
+        {
+            if (it->first == name || it->second == function)
+            {
+                COUT(2) << "Warning: Trying to add a Tolua interface with the same name or function." << std::endl;
+                return true;
+            }
+        }
+        toluaInterfaces_s[name] = function;
+
+        // Open interface in all LuaStates
+        for (std::vector<LuaState*>::const_iterator it = instances_s.begin(); it != instances_s.end(); ++it)
+            (*function)((*it)->luaState_);
+
+        // Return dummy bool
+        return true;
+    }
+
+    /*static*/ bool LuaState::removeToluaInterface(const std::string& name)
+    {
+        ToluaInterfaceMap::iterator it = toluaInterfaces_s.find(name);
+        if (it == toluaInterfaces_s.end())
+        {
+            COUT(2) << "Warning: Cannot remove Tolua interface '" << name << "': Not found" << std::endl;
+            return true;
+        }
+
+        // Close interface in all LuaStates
+        for (std::vector<LuaState*>::const_iterator itState = instances_s.begin(); itState != instances_s.end(); ++itState)
+        {
+            lua_pushnil((*itState)->luaState_);
+            lua_setglobal((*itState)->luaState_, it->first.c_str());
+        }
+
+        // Remove entry
+        toluaInterfaces_s.erase(it);
+
+        // Return dummy bool
+        return true;
+    }
+
+    /*static*/ void LuaState::openToluaInterfaces(lua_State* state)
+    {
+        for (ToluaInterfaceMap::const_iterator it = toluaInterfaces_s.begin(); it != toluaInterfaces_s.end(); ++it)
+            (*it->second)(state);
+    }
+
+    /*static*/ void LuaState::closeToluaInterfaces(lua_State* state)
+    {
+        for (ToluaInterfaceMap::const_iterator it = toluaInterfaces_s.begin(); it != toluaInterfaces_s.end(); ++it)
+        {
+            lua_pushnil(state);
+            lua_setglobal(state, it->first.c_str());
+        }
+    }
+}
Index: code/branches/resource2/src/core/LuaState.h
===================================================================
--- code/branches/resource2/src/core/LuaState.h	(revision 5654)
+++ code/branches/resource2/src/core/LuaState.h	(revision 5654)
@@ -0,0 +1,102 @@
+/*
+ *   ORXONOX - the hottest 3D action shooter ever to exist
+ *                    > www.orxonox.net <
+ *
+ *
+ *   License notice:
+ *
+ *   This program is free software; you can redistribute it and/or
+ *   modify it under the terms of the GNU General Public License
+ *   as published by the Free Software Foundation; either version 2
+ *   of the License, or (at your option) any later version.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ *
+ *   You should have received a copy of the GNU General Public License
+ *   along with this program; if not, write to the Free Software
+ *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ *
+ *   Author:
+ *      Benjamin Knecht
+ *      Reto Grieder
+ *   Co-authors:
+ *      ...
+ *
+ */
+
+#ifndef _LuaState_H__
+#define _LuaState_H__
+
+#include "CorePrereqs.h"
+
+#include <map>
+#include <sstream>
+#include <string>
+#include <vector>
+#include <boost/shared_ptr.hpp>
+
+#include "util/ScopeGuard.h"
+
+// tolua_begin
+namespace orxonox
+{
+    /**
+    @brief
+        Representation of an interface to lua
+    */
+    class _CoreExport LuaState
+    {
+// tolua_end
+    public:
+        LuaState();
+        ~LuaState();
+
+        void doFile(const std::string& filename, const std::string& resourceGroup = "General", bool bSearchOtherPaths = true); // tolua_export
+        void doString(const std::string& code, shared_ptr<ResourceInfo> sourceFileInfo = shared_ptr<ResourceInfo>());
+
+        void includeFile(const std::string& filename, const std::string& resourceGroup = "Genreal", bool bSearchOtherPaths = true); // tolua_export
+        void includeString(const std::string& code, shared_ptr<ResourceInfo> sourceFileInfo = shared_ptr<ResourceInfo>());
+
+        void luaPrint(const std::string& str); // tolua_export
+        void luaLog(unsigned int level, const std::string& message); // tolua_export
+
+        const std::stringstream& getOutput() const { return output_; }
+        void clearOutput() { output_.clear(); } // tolua_export
+
+        void setIncludeParser(std::string (*function)(const std::string&)) { includeParseFunction_ = function; }
+        lua_State* getInternalLuaState() { return luaState_; }
+
+        static bool addToluaInterface(int (*function)(lua_State*), const std::string& name);
+        static bool removeToluaInterface(const std::string& name);
+        static void openToluaInterfaces(lua_State* state);
+        static void closeToluaInterfaces(lua_State* state);
+
+    private:
+        shared_ptr<ResourceInfo> getFileInfo(const std::string& filename, const std::string& resourceGroup, bool bSearchOtherPaths);
+
+#if LUA_VERSION_NUM != 501
+        struct LoadS
+        {
+            const char* s;
+            size_t size;
+        };
+
+        static const char * lua_Chunkreader(lua_State *L, void *data, size_t *size);
+#endif
+
+        std::stringstream output_;
+        lua_State* luaState_;
+        bool bIsRunning_;
+        shared_ptr<ResourceInfo> sourceFileInfo_;
+        std::string (*includeParseFunction_)(const std::string&);
+
+        typedef std::map<std::string, int (*)(lua_State *L)> ToluaInterfaceMap;
+        static ToluaInterfaceMap toluaInterfaces_s;
+        static std::vector<LuaState*> instances_s;
+    }; // tolua_export
+} // tolua_export
+
+#endif /* _LuaState_H__ */
Index: code/branches/resource2/src/orxonox/gamestates/GSGraphics.cc
===================================================================
--- code/branches/resource2/src/orxonox/gamestates/GSGraphics.cc	(revision 5653)
+++ code/branches/resource2/src/orxonox/gamestates/GSGraphics.cc	(revision 5654)
@@ -96,5 +96,5 @@
         // load debug overlay
         COUT(3) << "Loading Debug Overlay..." << std::endl;
-        this->debugOverlay_ = new XMLFile(Core::getDataPathString() + "overlay/debug.oxo");
+        this->debugOverlay_ = new XMLFile("debug.oxo");
         Loader::open(debugOverlay_);
 
Index: code/branches/resource2/src/orxonox/gamestates/GSLevel.cc
===================================================================
--- code/branches/resource2/src/orxonox/gamestates/GSLevel.cc	(revision 5653)
+++ code/branches/resource2/src/orxonox/gamestates/GSLevel.cc	(revision 5654)
@@ -246,5 +246,5 @@
         // call the loader
         COUT(0) << "Loading level..." << std::endl;
-        startFile_s = new XMLFile(Core::getDataPathString() + "levels" + '/' + LevelManager::getInstance().getDefaultLevel());
+        startFile_s = new XMLFile(LevelManager::getInstance().getDefaultLevel());
         Loader::open(startFile_s);
     }
Index: code/branches/resource2/src/orxonox/gamestates/GSRoot.cc
===================================================================
--- code/branches/resource2/src/orxonox/gamestates/GSRoot.cc	(revision 5653)
+++ code/branches/resource2/src/orxonox/gamestates/GSRoot.cc	(revision 5654)
@@ -33,7 +33,6 @@
 #include "core/Game.h"
 #include "core/GameMode.h"
-#include "core/LuaBind.h"
+#include "core/LuaState.h"
 #include "network/NetworkFunction.h"
-#include "ToluaBindCore.h"
 #include "ToluaBindOrxonox.h"
 #include "tools/Timer.h"
@@ -56,6 +55,5 @@
 
         // Tell LuaBind about all tolua interfaces
-        LuaBind::getInstance().addToluaInterface(&tolua_Core_open, "Core");
-        LuaBind::getInstance().addToluaInterface(&tolua_Orxonox_open, "Orxonox");
+        LuaState::addToluaInterface(&tolua_Orxonox_open, "Orxonox");
     }
 
@@ -86,5 +84,5 @@
         }
 
-        // create the global LevelManager
+        // create the LevelManager
         this->levelManager_ = new LevelManager();
     }
Index: code/branches/resource2/src/orxonox/objects/Level.cc
===================================================================
--- code/branches/resource2/src/orxonox/objects/Level.cc	(revision 5653)
+++ code/branches/resource2/src/orxonox/objects/Level.cc	(revision 5654)
@@ -53,7 +53,4 @@
         this->registerVariables();
         this->xmlfilename_ = this->getFilename();
-
-        if (this->xmlfilename_.length() >= Core::getDataPathString().length())
-            this->xmlfilename_ = this->xmlfilename_.substr(Core::getDataPathString().length());
     }
 
Index: code/branches/resource2/src/orxonox/objects/Script.cc
===================================================================
--- code/branches/resource2/src/orxonox/objects/Script.cc	(revision 5653)
+++ code/branches/resource2/src/orxonox/objects/Script.cc	(revision 5654)
@@ -29,40 +29,36 @@
 #include "Script.h"
 
-#include <tinyxml/ticpp.h>
 #include "core/CoreIncludes.h"
-#include "core/LuaBind.h"
+#include "core/LuaState.h"
+#include "core/XMLPort.h"
 
 namespace orxonox
 {
-  CreateFactory(Script);
+    CreateFactory(Script);
 
-  Script::Script(BaseObject* creator) : BaseObject(creator)
-  {
-    RegisterObject(Script);
+    Script::Script(BaseObject* creator) : BaseObject(creator)
+    {
+        RegisterObject(Script);
 
-    code_ = "";
-  }
+        // Get a new LuaState
+        luaState_ = new LuaState();
+    }
 
-  Script::~Script()
-  {
-  }
+    Script::~Script()
+    {
+        if (this->isInitialized())
+            delete luaState_;
+    }
 
-  /**
-  @brief XML loading and saving.
-  @param xmlelement The XML-element
-  @param loading Loading (true) or saving (false)
-   */
-  void Script::XMLPort(Element& xmlelement, XMLPort::Mode mode)
-  {
-    BaseObject::XMLPort(xmlelement, mode);
+    void Script::XMLPort(Element& xmlelement, XMLPort::Mode mode)
+    {
+        BaseObject::XMLPort(xmlelement, mode);
 
-    code_ = xmlelement.GetText(false);
-  }
+        XMLPortParam(Script, "code", setCode, getCode, xmlelement, mode);
+    }
 
-  void Script::execute()
-  {
-    LuaBind& lua = LuaBind::getInstance();
-    lua.loadString(this->code_);
-    lua.run();
-  }
+    void Script::execute()
+    {
+        luaState_->doString(code_);
+    }
 }
Index: code/branches/resource2/src/orxonox/objects/Script.h
===================================================================
--- code/branches/resource2/src/orxonox/objects/Script.h	(revision 5653)
+++ code/branches/resource2/src/orxonox/objects/Script.h	(revision 5654)
@@ -37,15 +37,19 @@
 namespace orxonox
 {
-  class _OrxonoxExport Script : public BaseObject
-  {
+    class _OrxonoxExport Script : public BaseObject
+    {
     public:
-      Script(BaseObject* creator);
-      ~Script();
-      void XMLPort(Element& xmlelement, XMLPort::Mode mode);
-      void execute();
+        Script(BaseObject* creator);
+        ~Script();
+        void XMLPort(Element& xmlelement, XMLPort::Mode mode);
+        void execute();
+
+        void setCode(const std::string& code) { code_ = code; }
+        const std::string& getCode() const { return code_; }
 
     private:
-      std::string code_;
-  };
+        std::string code_;
+        LuaState* luaState_;
+    };
 }
 
Index: code/branches/resource2/src/orxonox/sound/SoundBase.cc
===================================================================
--- code/branches/resource2/src/orxonox/sound/SoundBase.cc	(revision 5653)
+++ code/branches/resource2/src/orxonox/sound/SoundBase.cc	(revision 5654)
@@ -36,4 +36,5 @@
 #include "util/Math.h"
 #include "core/Core.h"
+#include "core/Resource.h"
 #include "orxonox/objects/worldentities/WorldEntity.h"
 #include "SoundManager.h"
@@ -134,7 +135,5 @@
     }
 
-    bool SoundBase::loadFile(std::string filename) {
-        filename = Core::getDataPathString() + "/audio/" + filename;
-
+    bool SoundBase::loadFile(const std::string& filename) {
         if(!SoundManager::getInstance().isSoundAvailable())
         {
@@ -144,5 +143,18 @@
 
         COUT(3) << "Sound: OpenAL ALUT: loading file " << filename << std::endl;
-        this->buffer_ = alutCreateBufferFromFile(filename.c_str());
+        // Get DataStream from the resources
+        shared_ptr<ResourceInfo> fileInfo = Resource::getInfo(filename);
+        if (fileInfo == NULL) {
+            COUT(2) << "Warning: Sound file '" << filename << "' not found" << std::endl;
+            return false;
+        }
+        DataStreamPtr stream = Resource::open(filename);
+        // Read everything into a temporary buffer
+        char* buffer = new char[fileInfo->size];
+        stream->read(buffer, fileInfo->size);
+
+        this->buffer_ = alutCreateBufferFromFileImage(buffer, fileInfo->size);
+        delete[] buffer;
+
         if(this->buffer_ == AL_NONE) {
             COUT(2) << "Sound: OpenAL ALUT: " << alutGetErrorString(alutGetError()) << std::endl;
Index: code/branches/resource2/src/orxonox/sound/SoundBase.h
===================================================================
--- code/branches/resource2/src/orxonox/sound/SoundBase.h	(revision 5653)
+++ code/branches/resource2/src/orxonox/sound/SoundBase.h	(revision 5654)
@@ -55,5 +55,5 @@
         bool isStopped();
 
-        bool loadFile(std::string filename);
+        bool loadFile(const std::string& filename);
 
     private:
