Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #include <vlCore/LoadWriterManager.hpp>
00033 #include <vlCore/Log.hpp>
00034 #include <vlCore/Say.hpp>
00035
00036 using namespace vl;
00037
00038
00039 const ResourceLoadWriter* LoadWriterManager::findLoader(VirtualFile* file) const
00040 {
00041 if (file->path().empty())
00042 Log::warning("findLoader(VirtualFile* file): cannot check the file extension, file->path() is empty!\n");
00043 return findLoader(file->path());
00044 }
00045
00046 const ResourceLoadWriter* LoadWriterManager::findWriter(VirtualFile* file) const
00047 {
00048 if (file->path().empty())
00049 Log::warning("findWriter(VirtualFile* file): cannot check the file extension, file->path() is empty!\n");
00050 return findWriter(file->path());
00051 }
00052
00053 ref<ResourceDatabase> LoadWriterManager::loadResource(const String& path, bool quick) const
00054 {
00055 ref<VirtualFile> file = locateFile(path);
00056 if (file)
00057 return loadResource(file.get(), quick);
00058 else
00059 return NULL;
00060 }
00061
00062 ref<ResourceDatabase> LoadWriterManager::loadResource(VirtualFile* file, bool quick) const
00063 {
00064 const ResourceLoadWriter* loadwriter = findLoader(file);
00065 if (loadwriter)
00066 {
00067 ref<ResourceDatabase> db;
00068 if (quick)
00069 {
00070
00071 ref<MemoryFile> memfile = new MemoryFile;
00072 memfile->allocateBuffer(file->size());
00073 file->open(OM_ReadOnly);
00074 file->read(memfile->ptr(),file->size());
00075 file->close();
00076 memfile->setPath(file->path());
00077 db = loadwriter->loadResource(memfile.get());
00078 }
00079 else
00080 db = loadwriter->loadResource(file);
00081
00082 for(int i=0; db && i<loadCallbacks()->size(); ++i)
00083 const_cast<LoadCallback*>(loadCallbacks()->at(i))->operator()(db.get());
00084 return db;
00085 }
00086 else
00087 {
00088 Log::error( Say("no ResourceLoadWriter registered to load '%s'.\n") << file->path() );
00089 return NULL;
00090 }
00091 }
00092
00093 bool LoadWriterManager::writeResource(const String& path, ResourceDatabase* resource) const
00094 {
00095 const ResourceLoadWriter* loadwriter = findWriter(path);
00096 if (loadwriter)
00097 {
00098
00099 for(int i=0; i<writeCallbacks()->size(); ++i)
00100 const_cast<WriteCallback*>(writeCallbacks()->at(i))->operator()(resource);
00101
00102 return loadwriter->writeResource(path,resource);
00103 }
00104 else
00105 {
00106 Log::error( Say("no ResourceLoadWriter registered to write '%s'.\n") << path );
00107 return false;
00108 }
00109 }
00110
00111 bool LoadWriterManager::writeResource(VirtualFile* file, ResourceDatabase* resource) const
00112 {
00113 const ResourceLoadWriter* loadwriter = findWriter(file);
00114 if (loadwriter)
00115 {
00116
00117 for(int i=0; i<writeCallbacks()->size(); ++i)
00118 const_cast<WriteCallback*>(writeCallbacks()->at(i))->operator()(resource);
00119
00120 return loadwriter->writeResource(file,resource);
00121 }
00122 else
00123 {
00124 Log::error( Say("no ResourceLoadWriter registered to write '%s'.\n") << file->path() );
00125 return false;
00126 }
00127 }
00128
00129 const ResourceLoadWriter* LoadWriterManager::findLoader(const String& path) const
00130 {
00131 String ext = path.extractFileExtension(false).toLowerCase();
00132 for(int i=0; i<loadWriters()->size(); ++i)
00133 if (loadWriters()->at(i)->canLoad(ext))
00134 return loadWriters()->at(i);
00135
00136 return NULL;
00137 }
00138
00139 const ResourceLoadWriter* LoadWriterManager::findWriter(const String& path) const
00140 {
00141 String ext = path.extractFileExtension(false).toLowerCase();
00142 for(int i=0; i<loadWriters()->size(); ++i)
00143 if (loadWriters()->at(i)->canWrite(ext))
00144 return loadWriters()->at(i);
00145
00146 return NULL;
00147 }
00148
00149 void LoadWriterManager::registerLoadWriter(ResourceLoadWriter* load_writer)
00150 {
00151 ref<ResourceLoadWriter> lowr = load_writer;
00152 LoadWriterManager::loadWriters()->erase( load_writer );
00153 LoadWriterManager::loadWriters()->push_back( load_writer );
00154 }
00155
00156 bool vl::canLoad(const String& path) { return defLoadWriterManager()->canLoad(path); }
00157
00158 bool vl::canWrite(const String& path) { return defLoadWriterManager()->canWrite(path); }
00159
00160 bool vl::canLoad(VirtualFile* file) { return defLoadWriterManager()->canLoad(file); }
00161
00162 bool vl::canWrite(VirtualFile* file) { return defLoadWriterManager()->canWrite(file); }
00163
00164 ref<ResourceDatabase> vl::loadResource(const String& path, bool quick) { return defLoadWriterManager()->loadResource(path,quick); }
00165
00166 ref<ResourceDatabase> vl::loadResource(VirtualFile* file, bool quick) { return defLoadWriterManager()->loadResource(file,quick); }
00167
00168
00169