| 
									
										
										
										
											2012-07-22 15:02:13 +00:00
										 |  |  | #include "StdInc.h"
 | 
					
						
							|  |  |  | #include "CFilesystemLoader.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "CFileInfo.h"
 | 
					
						
							|  |  |  | #include "CFileInputStream.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-07 11:28:52 +00:00
										 |  |  | CFilesystemLoader::CFilesystemLoader(const std::string & baseDirectory, size_t depth, bool initial): | 
					
						
							|  |  |  |     baseDirectory(baseDirectory), | 
					
						
							|  |  |  |     fileList(listFiles(depth, initial)) | 
					
						
							| 
									
										
										
										
											2012-07-22 15:02:13 +00:00
										 |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | std::unique_ptr<CInputStream> CFilesystemLoader::load(const std::string & resourceName) const | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2012-08-01 12:02:54 +00:00
										 |  |  | 	std::unique_ptr<CInputStream> stream(new CFileInputStream(getOrigin() + '/' + resourceName)); | 
					
						
							| 
									
										
										
										
											2012-07-22 15:02:13 +00:00
										 |  |  | 	return stream; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool CFilesystemLoader::existsEntry(const std::string & resourceName) const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	for(auto it = fileList.begin(); it != fileList.end(); ++it) | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2012-08-07 11:28:52 +00:00
										 |  |  | 		if(it->second == resourceName) | 
					
						
							| 
									
										
										
										
											2012-07-22 15:02:13 +00:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			return true; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return false; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-08 08:25:27 +00:00
										 |  |  | boost::unordered_map<ResourceID, std::string> CFilesystemLoader::getEntries() const | 
					
						
							| 
									
										
										
										
											2012-07-22 15:02:13 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2012-08-07 11:28:52 +00:00
										 |  |  | 	return fileList; | 
					
						
							| 
									
										
										
										
											2012-07-22 15:02:13 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2012-08-01 12:02:54 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | std::string CFilesystemLoader::getOrigin() const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return baseDirectory; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2012-08-07 11:28:52 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | bool CFilesystemLoader::createEntry(std::string filename) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	ResourceID res(filename); | 
					
						
							|  |  |  | 	if (fileList.find(res) != fileList.end()) | 
					
						
							|  |  |  | 		return false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	fileList[res] = filename; | 
					
						
							|  |  |  | 	return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-08 08:25:27 +00:00
										 |  |  | boost::unordered_map<ResourceID, std::string> CFilesystemLoader::listFiles(size_t depth, bool initial) const | 
					
						
							| 
									
										
										
										
											2012-08-07 11:28:52 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2012-09-17 17:25:54 +00:00
										 |  |  | 	std::set<EResType::Type> initialTypes; | 
					
						
							|  |  |  | 	initialTypes.insert(EResType::DIRECTORY); | 
					
						
							|  |  |  | 	initialTypes.insert(EResType::TEXT); | 
					
						
							|  |  |  | 	initialTypes.insert(EResType::ARCHIVE_LOD); | 
					
						
							|  |  |  | 	initialTypes.insert(EResType::ARCHIVE_VID); | 
					
						
							|  |  |  | 	initialTypes.insert(EResType::ARCHIVE_SND); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-07 11:28:52 +00:00
										 |  |  | 	assert(boost::filesystem::is_directory(baseDirectory)); | 
					
						
							| 
									
										
										
										
											2012-08-08 08:25:27 +00:00
										 |  |  | 	boost::unordered_map<ResourceID, std::string> fileList; | 
					
						
							| 
									
										
										
										
											2012-08-07 11:28:52 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	std::vector<std::string> path;//vector holding relative path to our file
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	boost::filesystem::recursive_directory_iterator enddir; | 
					
						
							|  |  |  | 	boost::filesystem::recursive_directory_iterator it(baseDirectory, boost::filesystem::symlink_option::recurse); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for(; it != enddir; ++it) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		EResType::Type type; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (boost::filesystem::is_directory(it->status())) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			path.resize(it.level()+1); | 
					
						
							|  |  |  | 			path.back() = it->path().leaf().string(); | 
					
						
							|  |  |  | 			it.no_push(depth <= it.level()); // don't iterate into directory if depth limit reached
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			type = EResType::DIRECTORY; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		else | 
					
						
							|  |  |  | 			type = EResTypeHelper::getTypeFromExtension(boost::filesystem::extension(*it)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-09-17 17:25:54 +00:00
										 |  |  | 		if (!initial || vstd::contains(initialTypes, type)) | 
					
						
							| 
									
										
										
										
											2012-08-07 11:28:52 +00:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			//reconstruct relative filename (not possible via boost AFAIK)
 | 
					
						
							|  |  |  | 			std::string filename; | 
					
						
							|  |  |  | 			for (size_t i=0; i<it.level() && i<path.size(); i++) | 
					
						
							|  |  |  | 				filename += path[i] + '/'; | 
					
						
							|  |  |  | 			filename += it->path().leaf().string(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			fileList[ResourceID(filename, type)] = filename; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return fileList; | 
					
						
							|  |  |  | } |