Changeset 5781 for code/trunk/src/libraries/core/LuaState.cc
- Timestamp:
- Sep 24, 2009, 11:02:42 AM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/trunk/src/libraries/core/LuaState.cc
r5774 r5781 30 30 #include "LuaState.h" 31 31 32 #include <boost/filesystem.hpp>33 32 #include <tolua/tolua++.h> 34 33 extern "C" { … … 39 38 #include "util/Debug.h" 40 39 #include "Core.h" 40 #include "Resource.h" 41 41 #include "ToluaBindCore.h" 42 42 … … 71 71 // Create dummy file info 72 72 sourceFileInfo_.reset(new ResourceInfo()); 73 sourceFileInfo_->group = "General"; 74 sourceFileInfo_->size = 0; 73 75 74 76 // Push 'this' pointer … … 85 87 } 86 88 87 shared_ptr<ResourceInfo> LuaState::getFileInfo(const std::string& filename, bool bSearchOtherPaths)89 shared_ptr<ResourceInfo> LuaState::getFileInfo(const std::string& filename, const std::string& resourceGroup, bool bSearchOtherPaths) 88 90 { 89 91 shared_ptr<ResourceInfo> sourceInfo; 90 sourceInfo = this->getFileInfo(filename); 92 if (resourceGroup != "NoResourceGroupProvided") 93 sourceInfo = Resource::getInfo(filename, resourceGroup); 91 94 92 95 // Continue search if not explicitely forbidden … … 94 97 { 95 98 // Call might be relative to the file currently being processed 96 sourceInfo = this->getFileInfo(sourceFileInfo_->path + filename);99 sourceInfo = Resource::getInfo(sourceFileInfo_->path + filename, sourceFileInfo_->group); 97 100 if (sourceInfo == NULL) 98 101 { 99 102 // Maybe find something in the same group but in the root path 100 sourceInfo = this->getFileInfo(filename);103 sourceInfo = Resource::getInfo(filename, sourceFileInfo_->group); 101 104 } 102 105 } … … 104 107 } 105 108 106 shared_ptr<ResourceInfo> LuaState::getFileInfo(const std::string& filename) 107 { 108 boost::filesystem::path filepath = Core::getDataPath() / "lua" / filename; 109 if (boost::filesystem::exists(filepath)) 110 { 111 shared_ptr<ResourceInfo> info(new ResourceInfo()); 112 info->filename = filepath.string(); 113 info->path = filepath.branch_path().string(); 114 info->basename = filepath.leaf(); 115 return info; 116 } 117 else 118 return shared_ptr<ResourceInfo>(); 119 } 120 121 std::string LuaState::loadFile(const std::string& filename) 122 { 123 std::ifstream file(filename.c_str()); 124 std::ostringstream oss; 125 oss << file.rdbuf(); 126 return oss.str(); 127 } 128 129 void LuaState::includeFile(const std::string& filename, bool bSearchOtherPaths) 130 { 131 shared_ptr<ResourceInfo> sourceInfo = this->getFileInfo(filename, bSearchOtherPaths); 109 void LuaState::includeFile(const std::string& filename, const std::string& resourceGroup, bool bSearchOtherPaths) 110 { 111 shared_ptr<ResourceInfo> sourceInfo = this->getFileInfo(filename, resourceGroup, bSearchOtherPaths); 132 112 if (sourceInfo != NULL) 133 this->includeString(this->loadFile(sourceInfo->filename), sourceInfo); 134 else 135 COUT(2) << "LuaState: Cannot include file '" << filename << std::endl; 113 this->includeString(Resource::open(sourceInfo->filename, sourceInfo->group)->getAsString(), sourceInfo); 114 else 115 COUT(2) << "LuaState: Cannot include file '" << filename << "' in resource group '" 116 << (resourceGroup == "NoResourceGroupProvided" ? sourceFileInfo_->group : resourceGroup) << "': group not found." << std::endl; 136 117 } 137 118 … … 148 129 } 149 130 150 void LuaState::doFile(const std::string& filename, bool bSearchOtherPaths)151 { 152 shared_ptr<ResourceInfo> sourceInfo = this->getFileInfo(filename, bSearchOtherPaths);131 void LuaState::doFile(const std::string& filename, const std::string& resourceGroup, bool bSearchOtherPaths) 132 { 133 shared_ptr<ResourceInfo> sourceInfo = this->getFileInfo(filename, resourceGroup, bSearchOtherPaths); 153 134 if (sourceInfo != NULL) 154 this->doString(this->loadFile(sourceInfo->filename), sourceInfo); 155 else 156 COUT(2) << "LuaState: Cannot do file '" << filename << std::endl; 135 this->doString(Resource::open(sourceInfo->filename, sourceInfo->group)->getAsString(), sourceInfo); 136 else 137 COUT(2) << "LuaState: Cannot do file '" << filename << "' in resource group '" 138 << (resourceGroup == "NoResourceGroupProvided" ? sourceFileInfo_->group : resourceGroup) << "': group not found." << std::endl; 157 139 } 158 140 … … 204 186 } 205 187 206 bool LuaState::fileExists(const std::string& filename, bool bSearchOtherPaths)207 { 208 shared_ptr<ResourceInfo> info = this->getFileInfo(filename, bSearchOtherPaths);188 bool LuaState::fileExists(const std::string& filename, const std::string& resourceGroup, bool bSearchOtherPaths) 189 { 190 shared_ptr<ResourceInfo> info = this->getFileInfo(filename, resourceGroup, bSearchOtherPaths); 209 191 if (info == NULL) 210 192 return false;
Note: See TracChangeset
for help on using the changeset viewer.