| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | #include <stdio.h> |
|---|
| 15 | #include <string.h> |
|---|
| 16 | |
|---|
| 17 | #include <osg/Notify> |
|---|
| 18 | #include <osg/Object> |
|---|
| 19 | #include <osg/Image> |
|---|
| 20 | #include <osg/Shader> |
|---|
| 21 | #include <osg/Node> |
|---|
| 22 | #include <osg/Group> |
|---|
| 23 | #include <osg/Geode> |
|---|
| 24 | #include <osg/ApplicationUsage> |
|---|
| 25 | #include <osg/Version> |
|---|
| 26 | #include <osg/Timer> |
|---|
| 27 | |
|---|
| 28 | #include <osgDB/Registry> |
|---|
| 29 | #include <osgDB/FileUtils> |
|---|
| 30 | #include <osgDB/FileNameUtils> |
|---|
| 31 | #include <osgDB/fstream> |
|---|
| 32 | #include <osgDB/Archive> |
|---|
| 33 | |
|---|
| 34 | #include <algorithm> |
|---|
| 35 | #include <set> |
|---|
| 36 | |
|---|
| 37 | #include <stdlib.h> |
|---|
| 38 | |
|---|
| 39 | #if defined(__sgi) |
|---|
| 40 | #include <ctype.h> |
|---|
| 41 | #elif defined(__GNUC__) || !defined(WIN32) || defined(__MWERKS__) |
|---|
| 42 | #include <cctype> |
|---|
| 43 | using std::tolower; |
|---|
| 44 | #endif |
|---|
| 45 | |
|---|
| 46 | #ifdef OSG_DEBUG_POSTFIX |
|---|
| 47 | #define OSG_DEBUG_POSTFIX_WITH_QUOTES ADDQUOTES(OSG_DEBUG_POSTFIX) |
|---|
| 48 | #else |
|---|
| 49 | #define OSG_DEBUG_POSTFIX_WITH_QUOTES "d" |
|---|
| 50 | #endif |
|---|
| 51 | |
|---|
| 52 | using namespace osg; |
|---|
| 53 | using namespace osgDB; |
|---|
| 54 | |
|---|
| 55 | #if !defined(WIN32) || defined(__CYGWIN__) |
|---|
| 56 | static osg::ApplicationUsageProxy Registry_e0(osg::ApplicationUsage::ENVIRONMENTAL_VARIABLE,"OSG_FILE_PATH <path>[:path]..","Paths for locating datafiles"); |
|---|
| 57 | static osg::ApplicationUsageProxy Registry_e1(osg::ApplicationUsage::ENVIRONMENTAL_VARIABLE,"OSG_LIBRARY_PATH <path>[:path]..","Paths for locating libraries/ plugins"); |
|---|
| 58 | #else |
|---|
| 59 | static osg::ApplicationUsageProxy Registry_e0(osg::ApplicationUsage::ENVIRONMENTAL_VARIABLE,"OSG_FILE_PATH <path>[;path]..","Paths for locating datafiles"); |
|---|
| 60 | static osg::ApplicationUsageProxy Registry_e1(osg::ApplicationUsage::ENVIRONMENTAL_VARIABLE,"OSG_LIBRARY_PATH <path>[;path]..","Paths for locating libraries/ plugins"); |
|---|
| 61 | #endif |
|---|
| 62 | |
|---|
| 63 | static osg::ApplicationUsageProxy Registry_e2(osg::ApplicationUsage::ENVIRONMENTAL_VARIABLE,"OSG_BUILD_KDTREES on/off","Enable/disable the automatic building of KdTrees for each loaded Geometry."); |
|---|
| 64 | |
|---|
| 65 | |
|---|
| 66 | class Registry::AvailableReaderWriterIterator |
|---|
| 67 | { |
|---|
| 68 | public: |
|---|
| 69 | AvailableReaderWriterIterator(Registry::ReaderWriterList& rwList, OpenThreads::ReentrantMutex& pluginMutex): |
|---|
| 70 | _rwList(rwList), |
|---|
| 71 | _pluginMutex(pluginMutex) {} |
|---|
| 72 | |
|---|
| 73 | |
|---|
| 74 | ReaderWriter& operator * () { return *get(); } |
|---|
| 75 | ReaderWriter* operator -> () { return get(); } |
|---|
| 76 | |
|---|
| 77 | bool valid() { return get()!=0; } |
|---|
| 78 | |
|---|
| 79 | void operator ++() |
|---|
| 80 | { |
|---|
| 81 | _rwUsed.insert(get()); |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | protected: |
|---|
| 86 | |
|---|
| 87 | AvailableReaderWriterIterator& operator = (const AvailableReaderWriterIterator&) { return *this; } |
|---|
| 88 | |
|---|
| 89 | Registry::ReaderWriterList& _rwList; |
|---|
| 90 | OpenThreads::ReentrantMutex& _pluginMutex; |
|---|
| 91 | |
|---|
| 92 | std::set<ReaderWriter*> _rwUsed; |
|---|
| 93 | |
|---|
| 94 | ReaderWriter* get() |
|---|
| 95 | { |
|---|
| 96 | OpenThreads::ScopedLock<OpenThreads::ReentrantMutex> lock(_pluginMutex); |
|---|
| 97 | Registry::ReaderWriterList::iterator itr=_rwList.begin(); |
|---|
| 98 | for(;itr!=_rwList.end();++itr) |
|---|
| 99 | { |
|---|
| 100 | if (_rwUsed.find(itr->get())==_rwUsed.end()) |
|---|
| 101 | { |
|---|
| 102 | return itr->get(); |
|---|
| 103 | } |
|---|
| 104 | } |
|---|
| 105 | return 0; |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | }; |
|---|
| 109 | |
|---|
| 110 | #if 0 |
|---|
| 111 | |
|---|
| 112 | enum Methods |
|---|
| 113 | { |
|---|
| 114 | SET_1, |
|---|
| 115 | SET_2, |
|---|
| 116 | END |
|---|
| 117 | }; |
|---|
| 118 | |
|---|
| 119 | |
|---|
| 120 | typedef std::pair<Methods,std::string> MethodPair; |
|---|
| 121 | |
|---|
| 122 | class Proxy |
|---|
| 123 | { |
|---|
| 124 | public: |
|---|
| 125 | Proxy(MethodPair* methods) |
|---|
| 126 | { |
|---|
| 127 | std::cout<<"methods "<<methods<<std::endl; |
|---|
| 128 | for(int i=0;methods[i].first!=END;++i) |
|---|
| 129 | { |
|---|
| 130 | std::cout<<"\t"<<methods[i].first<<"\t"<<methods[i].second<<std::endl; |
|---|
| 131 | } |
|---|
| 132 | } |
|---|
| 133 | }; |
|---|
| 134 | |
|---|
| 135 | |
|---|
| 136 | static MethodPair methods[] = |
|---|
| 137 | { |
|---|
| 138 | MethodPair(SET_1,"SET_1"), |
|---|
| 139 | MethodPair(SET_2,"SET_2"), |
|---|
| 140 | MethodPair(END,"") |
|---|
| 141 | }; |
|---|
| 142 | |
|---|
| 143 | Proxy myproxy(methods); |
|---|
| 144 | |
|---|
| 145 | #endif |
|---|
| 146 | |
|---|
| 147 | void PrintFilePathList(std::ostream& stream,const FilePathList& filepath) |
|---|
| 148 | { |
|---|
| 149 | for(FilePathList::const_iterator itr=filepath.begin(); |
|---|
| 150 | itr!=filepath.end(); |
|---|
| 151 | ++itr) |
|---|
| 152 | { |
|---|
| 153 | stream << " "<< *itr<<std::endl; |
|---|
| 154 | } |
|---|
| 155 | } |
|---|
| 156 | |
|---|
| 157 | Registry* Registry::instance(bool erase) |
|---|
| 158 | { |
|---|
| 159 | static ref_ptr<Registry> s_registry = new Registry; |
|---|
| 160 | if (erase) |
|---|
| 161 | { |
|---|
| 162 | s_registry->destruct(); |
|---|
| 163 | s_registry = 0; |
|---|
| 164 | } |
|---|
| 165 | return s_registry.get(); |
|---|
| 166 | } |
|---|
| 167 | |
|---|
| 168 | |
|---|
| 169 | |
|---|
| 170 | Registry::Registry() |
|---|
| 171 | { |
|---|
| 172 | |
|---|
| 173 | |
|---|
| 174 | |
|---|
| 175 | _buildKdTreesHint = ReaderWriter::Options::NO_PREFERENCE; |
|---|
| 176 | _kdTreeBuilder = new osg::KdTreeBuilder; |
|---|
| 177 | |
|---|
| 178 | const char* kdtree_str = getenv("OSG_BUILD_KDTREES"); |
|---|
| 179 | if (kdtree_str) |
|---|
| 180 | { |
|---|
| 181 | bool switchOff = (strcmp(kdtree_str, "off")==0 || strcmp(kdtree_str, "OFF")==0 || strcmp(kdtree_str, "Off")==0 ); |
|---|
| 182 | if (switchOff) _buildKdTreesHint = ReaderWriter::Options::DO_NOT_BUILD_KDTREES; |
|---|
| 183 | else _buildKdTreesHint = ReaderWriter::Options::BUILD_KDTREES; |
|---|
| 184 | } |
|---|
| 185 | |
|---|
| 186 | const char* fileCachePath = getenv("OSG_FILE_CACHE"); |
|---|
| 187 | if (fileCachePath) |
|---|
| 188 | { |
|---|
| 189 | _fileCache = new FileCache(fileCachePath); |
|---|
| 190 | } |
|---|
| 191 | |
|---|
| 192 | _createNodeFromImage = false; |
|---|
| 193 | _openingLibrary = false; |
|---|
| 194 | |
|---|
| 195 | |
|---|
| 196 | _archiveExtList.push_back("osga"); |
|---|
| 197 | |
|---|
| 198 | initFilePathLists(); |
|---|
| 199 | |
|---|
| 200 | |
|---|
| 201 | |
|---|
| 202 | |
|---|
| 203 | const char* flt_str = getenv("OSG_OPEN_FLIGHT_PLUGIN"); |
|---|
| 204 | if (flt_str) |
|---|
| 205 | { |
|---|
| 206 | if (strcmp(flt_str, "new")==0) |
|---|
| 207 | { |
|---|
| 208 | addFileExtensionAlias("flt", "OpenFlight"); |
|---|
| 209 | } |
|---|
| 210 | } |
|---|
| 211 | else |
|---|
| 212 | { |
|---|
| 213 | #ifndef COMPILE_WITH_OLD_OPENFLIGHT_PLUGIN_AS_DEFAULT |
|---|
| 214 | addFileExtensionAlias("flt", "OpenFlight"); |
|---|
| 215 | #endif |
|---|
| 216 | } |
|---|
| 217 | |
|---|
| 218 | addFileExtensionAlias("osgs", "osg"); |
|---|
| 219 | addFileExtensionAlias("shadow", "osgShadow"); |
|---|
| 220 | addFileExtensionAlias("terrain", "osgTerrain"); |
|---|
| 221 | addFileExtensionAlias("view", "osgViewer"); |
|---|
| 222 | |
|---|
| 223 | addFileExtensionAlias("sgi", "rgb"); |
|---|
| 224 | addFileExtensionAlias("rgba", "rgb"); |
|---|
| 225 | addFileExtensionAlias("int", "rgb"); |
|---|
| 226 | addFileExtensionAlias("inta", "rgb"); |
|---|
| 227 | addFileExtensionAlias("bw", "rgb"); |
|---|
| 228 | |
|---|
| 229 | addFileExtensionAlias("ivz", "gz"); |
|---|
| 230 | addFileExtensionAlias("ozg", "gz"); |
|---|
| 231 | |
|---|
| 232 | addFileExtensionAlias("mag", "dicom"); |
|---|
| 233 | addFileExtensionAlias("ph", "dicom"); |
|---|
| 234 | addFileExtensionAlias("ima", "dicom"); |
|---|
| 235 | addFileExtensionAlias("dcm", "dicom"); |
|---|
| 236 | addFileExtensionAlias("dic", "dicom"); |
|---|
| 237 | |
|---|
| 238 | addFileExtensionAlias("gl", "glsl"); |
|---|
| 239 | addFileExtensionAlias("vert", "glsl"); |
|---|
| 240 | addFileExtensionAlias("frag", "glsl"); |
|---|
| 241 | |
|---|
| 242 | |
|---|
| 243 | #if defined(DARWIN_IMAGEIO) |
|---|
| 244 | addFileExtensionAlias("jpg", "imageio"); |
|---|
| 245 | addFileExtensionAlias("jpe", "imageio"); |
|---|
| 246 | addFileExtensionAlias("jpeg", "imageio"); |
|---|
| 247 | addFileExtensionAlias("tif", "imageio"); |
|---|
| 248 | addFileExtensionAlias("tiff", "imageio"); |
|---|
| 249 | addFileExtensionAlias("gif", "imageio"); |
|---|
| 250 | addFileExtensionAlias("png", "imageio"); |
|---|
| 251 | addFileExtensionAlias("psd", "imageio"); |
|---|
| 252 | addFileExtensionAlias("tga", "imageio"); |
|---|
| 253 | #endif |
|---|
| 254 | |
|---|
| 255 | #if defined(DARWIN_QUICKTIME) |
|---|
| 256 | addFileExtensionAlias("jpg", "qt"); |
|---|
| 257 | addFileExtensionAlias("jpe", "qt"); |
|---|
| 258 | addFileExtensionAlias("jpeg", "qt"); |
|---|
| 259 | addFileExtensionAlias("tif", "qt"); |
|---|
| 260 | addFileExtensionAlias("tiff", "qt"); |
|---|
| 261 | addFileExtensionAlias("gif", "qt"); |
|---|
| 262 | addFileExtensionAlias("png", "qt"); |
|---|
| 263 | addFileExtensionAlias("psd", "qt"); |
|---|
| 264 | addFileExtensionAlias("tga", "qt"); |
|---|
| 265 | addFileExtensionAlias("mov", "qt"); |
|---|
| 266 | addFileExtensionAlias("avi", "qt"); |
|---|
| 267 | addFileExtensionAlias("mpg", "qt"); |
|---|
| 268 | addFileExtensionAlias("flv", "qt"); |
|---|
| 269 | addFileExtensionAlias("mpv", "qt"); |
|---|
| 270 | addFileExtensionAlias("dv", "qt"); |
|---|
| 271 | addFileExtensionAlias("mp4", "qt"); |
|---|
| 272 | addFileExtensionAlias("m4v", "qt"); |
|---|
| 273 | addFileExtensionAlias("3gp", "qt"); |
|---|
| 274 | |
|---|
| 275 | addFileExtensionAlias("live", "qt"); |
|---|
| 276 | #else |
|---|
| 277 | addFileExtensionAlias("jpg", "jpeg"); |
|---|
| 278 | addFileExtensionAlias("jpe", "jpeg"); |
|---|
| 279 | addFileExtensionAlias("tif", "tiff"); |
|---|
| 280 | |
|---|
| 281 | |
|---|
| 282 | #if defined(USE_XINE) |
|---|
| 283 | addFileExtensionAlias("mov", "xine"); |
|---|
| 284 | addFileExtensionAlias("mpg", "xine"); |
|---|
| 285 | addFileExtensionAlias("ogv", "xine"); |
|---|
| 286 | addFileExtensionAlias("mpv", "xine"); |
|---|
| 287 | addFileExtensionAlias("dv", "xine"); |
|---|
| 288 | addFileExtensionAlias("avi", "xine"); |
|---|
| 289 | addFileExtensionAlias("wmv", "xine"); |
|---|
| 290 | addFileExtensionAlias("flv", "xine"); |
|---|
| 291 | #endif |
|---|
| 292 | |
|---|
| 293 | |
|---|
| 294 | #if defined(USE_QUICKTIME) |
|---|
| 295 | addFileExtensionAlias("mov", "qt"); |
|---|
| 296 | addFileExtensionAlias("live", "qt"); |
|---|
| 297 | addFileExtensionAlias("mpg", "qt"); |
|---|
| 298 | addFileExtensionAlias("avi", "qt"); |
|---|
| 299 | #endif |
|---|
| 300 | #endif |
|---|
| 301 | |
|---|
| 302 | |
|---|
| 303 | |
|---|
| 304 | |
|---|
| 305 | |
|---|
| 306 | addFileExtensionAlias("lw", "lwo"); |
|---|
| 307 | |
|---|
| 308 | #if defined(USE_VRML) |
|---|
| 309 | addFileExtensionAlias("wrl", "vrml"); |
|---|
| 310 | #elif defined(USE_INVENTOR) |
|---|
| 311 | addFileExtensionAlias("wrl", "iv"); |
|---|
| 312 | #endif |
|---|
| 313 | |
|---|
| 314 | |
|---|
| 315 | addFileExtensionAlias("ttf", "freetype"); |
|---|
| 316 | addFileExtensionAlias("ttc", "freetype"); |
|---|
| 317 | addFileExtensionAlias("cid", "freetype"); |
|---|
| 318 | addFileExtensionAlias("cff", "freetype"); |
|---|
| 319 | addFileExtensionAlias("cef", "freetype"); |
|---|
| 320 | addFileExtensionAlias("fon", "freetype"); |
|---|
| 321 | addFileExtensionAlias("fnt", "freetype"); |
|---|
| 322 | |
|---|
| 323 | |
|---|
| 324 | |
|---|
| 325 | |
|---|
| 326 | |
|---|
| 327 | |
|---|
| 328 | |
|---|
| 329 | addFileExtensionAlias("pbm", "pnm"); |
|---|
| 330 | addFileExtensionAlias("pgm", "pnm"); |
|---|
| 331 | addFileExtensionAlias("ppm", "pnm"); |
|---|
| 332 | |
|---|
| 333 | } |
|---|
| 334 | |
|---|
| 335 | |
|---|
| 336 | Registry::~Registry() |
|---|
| 337 | { |
|---|
| 338 | destruct(); |
|---|
| 339 | } |
|---|
| 340 | |
|---|
| 341 | void Registry::destruct() |
|---|
| 342 | { |
|---|
| 343 | |
|---|
| 344 | |
|---|
| 345 | |
|---|
| 346 | _sharedStateManager = 0; |
|---|
| 347 | |
|---|
| 348 | |
|---|
| 349 | |
|---|
| 350 | _fileCache = 0; |
|---|
| 351 | |
|---|
| 352 | |
|---|
| 353 | |
|---|
| 354 | |
|---|
| 355 | |
|---|
| 356 | |
|---|
| 357 | |
|---|
| 358 | |
|---|
| 359 | clearObjectCache(); |
|---|
| 360 | clearArchiveCache(); |
|---|
| 361 | |
|---|
| 362 | |
|---|
| 363 | |
|---|
| 364 | closeAllLibraries(); |
|---|
| 365 | } |
|---|
| 366 | |
|---|
| 367 | #include <iostream> |
|---|
| 368 | |
|---|
| 369 | void Registry::initDataFilePathList() |
|---|
| 370 | { |
|---|
| 371 | FilePathList filepath; |
|---|
| 372 | |
|---|
| 373 | |
|---|
| 374 | |
|---|
| 375 | char *ptr; |
|---|
| 376 | |
|---|
| 377 | if( (ptr = getenv( "OSG_FILE_PATH" )) ) |
|---|
| 378 | { |
|---|
| 379 | |
|---|
| 380 | convertStringPathIntoFilePathList(ptr, filepath); |
|---|
| 381 | } |
|---|
| 382 | else if( (ptr = getenv( "OSGFILEPATH" )) ) |
|---|
| 383 | { |
|---|
| 384 | |
|---|
| 385 | convertStringPathIntoFilePathList(ptr, filepath); |
|---|
| 386 | } |
|---|
| 387 | |
|---|
| 388 | osgDB::appendPlatformSpecificResourceFilePaths(filepath); |
|---|
| 389 | setDataFilePathList(filepath); |
|---|
| 390 | |
|---|
| 391 | } |
|---|
| 392 | |
|---|
| 393 | void Registry::setDataFilePathList(const std::string& paths) |
|---|
| 394 | { |
|---|
| 395 | _dataFilePath.clear(); |
|---|
| 396 | convertStringPathIntoFilePathList(paths,_dataFilePath); |
|---|
| 397 | } |
|---|
| 398 | |
|---|
| 399 | void Registry::setLibraryFilePathList(const std::string& paths) { _libraryFilePath.clear(); convertStringPathIntoFilePathList(paths,_libraryFilePath); } |
|---|
| 400 | |
|---|
| 401 | |
|---|
| 402 | |
|---|
| 403 | void Registry::initLibraryFilePathList() |
|---|
| 404 | { |
|---|
| 405 | |
|---|
| 406 | |
|---|
| 407 | |
|---|
| 408 | char* ptr; |
|---|
| 409 | if( (ptr = getenv( "OSG_LIBRARY_PATH")) ) |
|---|
| 410 | { |
|---|
| 411 | |
|---|
| 412 | setLibraryFilePathList(ptr); |
|---|
| 413 | } |
|---|
| 414 | else if( (ptr = getenv( "OSG_LD_LIBRARY_PATH")) ) |
|---|
| 415 | { |
|---|
| 416 | |
|---|
| 417 | setLibraryFilePathList(ptr); |
|---|
| 418 | } |
|---|
| 419 | |
|---|
| 420 | appendPlatformSpecificLibraryFilePaths(_libraryFilePath); |
|---|
| 421 | |
|---|
| 422 | } |
|---|
| 423 | |
|---|
| 424 | |
|---|
| 425 | void Registry::readCommandLine(osg::ArgumentParser& arguments) |
|---|
| 426 | { |
|---|
| 427 | |
|---|
| 428 | if (arguments.getApplicationUsage()) |
|---|
| 429 | { |
|---|
| 430 | arguments.getApplicationUsage()->addCommandLineOption("-l <library>","Load the plugin"); |
|---|
| 431 | arguments.getApplicationUsage()->addCommandLineOption("-e <extension>","Load the plugin associated with handling files with specified extension"); |
|---|
| 432 | arguments.getApplicationUsage()->addCommandLineOption("-O <option_string>","Provide an option string to reader/writers used to load databases"); |
|---|
| 433 | } |
|---|
| 434 | |
|---|
| 435 | std::string value; |
|---|
| 436 | while(arguments.read("-l",value)) |
|---|
| 437 | { |
|---|
| 438 | loadLibrary(value); |
|---|
| 439 | } |
|---|
| 440 | |
|---|
| 441 | while(arguments.read("-e",value)) |
|---|
| 442 | { |
|---|
| 443 | std::string libName = createLibraryNameForExtension(value); |
|---|
| 444 | loadLibrary(libName); |
|---|
| 445 | } |
|---|
| 446 | |
|---|
| 447 | while(arguments.read("-O",value)) |
|---|
| 448 | { |
|---|
| 449 | setOptions(new ReaderWriter::Options(value)); |
|---|
| 450 | } |
|---|
| 451 | } |
|---|
| 452 | |
|---|
| 453 | void Registry::addDotOsgWrapper(DotOsgWrapper* wrapper) |
|---|
| 454 | { |
|---|
| 455 | if (wrapper==0L) return; |
|---|
| 456 | |
|---|
| 457 | |
|---|
| 458 | const DotOsgWrapper::Associates& assoc = wrapper->getAssociates(); |
|---|
| 459 | |
|---|
| 460 | for(DotOsgWrapper::Associates::const_iterator itr=assoc.begin(); |
|---|
| 461 | itr!=assoc.end(); |
|---|
| 462 | ++itr) |
|---|
| 463 | { |
|---|
| 464 | |
|---|
| 465 | } |
|---|
| 466 | |
|---|
| 467 | const std::string& name = wrapper->getName(); |
|---|
| 468 | const osg::Object* proto = wrapper->getPrototype(); |
|---|
| 469 | |
|---|
| 470 | _objectWrapperMap[name] = wrapper; |
|---|
| 471 | if (wrapper->getReadWriteMode()==DotOsgWrapper::READ_AND_WRITE) _classNameWrapperMap[name] = wrapper; |
|---|
| 472 | |
|---|
| 473 | if (proto) |
|---|
| 474 | { |
|---|
| 475 | std::string libraryName = proto->libraryName(); |
|---|
| 476 | std::string compositeName = libraryName + "::" + name; |
|---|
| 477 | |
|---|
| 478 | _objectWrapperMap[compositeName] = wrapper; |
|---|
| 479 | if (wrapper->getReadWriteMode()==DotOsgWrapper::READ_AND_WRITE) _classNameWrapperMap[compositeName] = wrapper; |
|---|
| 480 | |
|---|
| 481 | if (dynamic_cast<const Image*>(proto)) |
|---|
| 482 | { |
|---|
| 483 | _imageWrapperMap[name] = wrapper; |
|---|
| 484 | _imageWrapperMap[compositeName] = wrapper; |
|---|
| 485 | } |
|---|
| 486 | if (dynamic_cast<const Drawable*>(proto)) |
|---|
| 487 | { |
|---|
| 488 | _drawableWrapperMap[name] = wrapper; |
|---|
| 489 | _drawableWrapperMap[compositeName] = wrapper; |
|---|
| 490 | } |
|---|
| 491 | if (dynamic_cast<const StateAttribute*>(proto)) |
|---|
| 492 | { |
|---|
| 493 | _stateAttrWrapperMap[name] = wrapper; |
|---|
| 494 | _stateAttrWrapperMap[compositeName] = wrapper; |
|---|
| 495 | } |
|---|
| 496 | if (dynamic_cast<const Uniform*>(proto)) |
|---|
| 497 | { |
|---|
| 498 | _uniformWrapperMap[name] = wrapper; |
|---|
| 499 | _uniformWrapperMap[compositeName] = wrapper; |
|---|
| 500 | } |
|---|
| 501 | if (dynamic_cast<const Node*>(proto)) |
|---|
| 502 | { |
|---|
| 503 | _nodeWrapperMap[name] = wrapper; |
|---|
| 504 | _nodeWrapperMap[compositeName] = wrapper; |
|---|
| 505 | } |
|---|
| 506 | if (dynamic_cast<const Shader*>(proto)) |
|---|
| 507 | { |
|---|
| 508 | _shaderWrapperMap[name] = wrapper; |
|---|
| 509 | _shaderWrapperMap[compositeName] = wrapper; |
|---|
| 510 | } |
|---|
| 511 | |
|---|
| 512 | |
|---|
| 513 | } |
|---|
| 514 | } |
|---|
| 515 | |
|---|
| 516 | |
|---|
| 517 | |
|---|
| 518 | |
|---|
| 519 | void Registry::eraseWrapper(DotOsgWrapperMap& wrappermap,DotOsgWrapper* wrapper) |
|---|
| 520 | { |
|---|
| 521 | typedef std::vector<DotOsgWrapperMap::iterator> EraseList; |
|---|
| 522 | EraseList eraseList; |
|---|
| 523 | for(DotOsgWrapperMap::iterator witr=wrappermap.begin(); |
|---|
| 524 | witr!=wrappermap.end(); |
|---|
| 525 | ++witr) |
|---|
| 526 | { |
|---|
| 527 | if (witr->second==wrapper) eraseList.push_back(witr); |
|---|
| 528 | } |
|---|
| 529 | for(EraseList::iterator eitr=eraseList.begin(); |
|---|
| 530 | eitr!=eraseList.end(); |
|---|
| 531 | ++eitr) |
|---|
| 532 | { |
|---|
| 533 | wrappermap.erase(*eitr); |
|---|
| 534 | } |
|---|
| 535 | } |
|---|
| 536 | |
|---|
| 537 | void Registry::removeDotOsgWrapper(DotOsgWrapper* wrapper) |
|---|
| 538 | { |
|---|
| 539 | if (wrapper==0L) return; |
|---|
| 540 | |
|---|
| 541 | eraseWrapper(_objectWrapperMap,wrapper); |
|---|
| 542 | eraseWrapper(_classNameWrapperMap,wrapper); |
|---|
| 543 | eraseWrapper(_imageWrapperMap,wrapper); |
|---|
| 544 | eraseWrapper(_drawableWrapperMap,wrapper); |
|---|
| 545 | eraseWrapper(_uniformWrapperMap,wrapper); |
|---|
| 546 | eraseWrapper(_stateAttrWrapperMap,wrapper); |
|---|
| 547 | eraseWrapper(_nodeWrapperMap,wrapper); |
|---|
| 548 | eraseWrapper(_shaderWrapperMap,wrapper); |
|---|
| 549 | } |
|---|
| 550 | |
|---|
| 551 | void Registry::addReaderWriter(ReaderWriter* rw) |
|---|
| 552 | { |
|---|
| 553 | if (rw==0L) return; |
|---|
| 554 | |
|---|
| 555 | |
|---|
| 556 | |
|---|
| 557 | OpenThreads::ScopedLock<OpenThreads::ReentrantMutex> lock(_pluginMutex); |
|---|
| 558 | |
|---|
| 559 | _rwList.push_back(rw); |
|---|
| 560 | |
|---|
| 561 | } |
|---|
| 562 | |
|---|
| 563 | |
|---|
| 564 | void Registry::removeReaderWriter(ReaderWriter* rw) |
|---|
| 565 | { |
|---|
| 566 | if (rw==0L) return; |
|---|
| 567 | |
|---|
| 568 | |
|---|
| 569 | |
|---|
| 570 | OpenThreads::ScopedLock<OpenThreads::ReentrantMutex> lock(_pluginMutex); |
|---|
| 571 | |
|---|
| 572 | ReaderWriterList::iterator rwitr = std::find(_rwList.begin(),_rwList.end(),rw); |
|---|
| 573 | if (rwitr!=_rwList.end()) |
|---|
| 574 | { |
|---|
| 575 | _rwList.erase(rwitr); |
|---|
| 576 | } |
|---|
| 577 | |
|---|
| 578 | } |
|---|
| 579 | |
|---|
| 580 | |
|---|
| 581 | void Registry::addFileExtensionAlias(const std::string mapExt, const std::string toExt) |
|---|
| 582 | { |
|---|
| 583 | _extAliasMap[mapExt] = toExt; |
|---|
| 584 | } |
|---|
| 585 | |
|---|
| 586 | bool Registry::readPluginAliasConfigurationFile( const std::string& file ) |
|---|
| 587 | { |
|---|
| 588 | std::string fileName = osgDB::findDataFile( file ); |
|---|
| 589 | if (fileName.empty()) |
|---|
| 590 | { |
|---|
| 591 | osg::notify( osg::WARN) << "Can't find plugin alias config file \"" << file << "\"." << std::endl; |
|---|
| 592 | return false; |
|---|
| 593 | } |
|---|
| 594 | |
|---|
| 595 | osgDB::ifstream ifs; |
|---|
| 596 | ifs.open( fileName.c_str() ); |
|---|
| 597 | if (!ifs.good()) |
|---|
| 598 | { |
|---|
| 599 | osg::notify( osg::WARN) << "Can't open plugin alias config file \"" << fileName << "\"." << std::endl; |
|---|
| 600 | return false; |
|---|
| 601 | } |
|---|
| 602 | |
|---|
| 603 | int lineNum( 0 ); |
|---|
| 604 | while (ifs.good()) |
|---|
| 605 | { |
|---|
| 606 | std::string raw; |
|---|
| 607 | ++lineNum; |
|---|
| 608 | std::getline( ifs, raw ); |
|---|
| 609 | std::string ln = trim( raw ); |
|---|
| 610 | if (ln.empty()) continue; |
|---|
| 611 | if (ln[0] == '#') continue; |
|---|
| 612 | |
|---|
| 613 | std::string::size_type spIdx = ln.find_first_of( " \t" ); |
|---|
| 614 | if (spIdx == ln.npos) |
|---|
| 615 | { |
|---|
| 616 | |
|---|
| 617 | osg::notify( osg::WARN) << file << ", line " << lineNum << ": Syntax error: missing space in \"" << raw << "\"." << std::endl; |
|---|
| 618 | continue; |
|---|
| 619 | } |
|---|
| 620 | |
|---|
| 621 | const std::string mapExt = trim( ln.substr( 0, spIdx ) ); |
|---|
| 622 | const std::string toExt = trim( ln.substr( spIdx+1 ) ); |
|---|
| 623 | addFileExtensionAlias( mapExt, toExt ); |
|---|
| 624 | } |
|---|
| 625 | return true; |
|---|
| 626 | } |
|---|
| 627 | |
|---|
| 628 | std::string Registry::trim( const std::string& str ) |
|---|
| 629 | { |
|---|
| 630 | if (!str.size()) return str; |
|---|
| 631 | std::string::size_type first = str.find_first_not_of( " \t" ); |
|---|
| 632 | std::string::size_type last = str.find_last_not_of( " \t\r\n" ); |
|---|
| 633 | if ((first==str.npos) || (last==str.npos)) return std::string( "" ); |
|---|
| 634 | return str.substr( first, last-first+1 ); |
|---|
| 635 | } |
|---|
| 636 | |
|---|
| 637 | |
|---|
| 638 | std::string Registry::createLibraryNameForFile(const std::string& fileName) |
|---|
| 639 | { |
|---|
| 640 | return createLibraryNameForExtension(getFileExtension(fileName)); |
|---|
| 641 | } |
|---|
| 642 | |
|---|
| 643 | std::string Registry::createLibraryNameForExtension(const std::string& ext) |
|---|
| 644 | { |
|---|
| 645 | std::string lowercase_ext; |
|---|
| 646 | for(std::string::const_iterator sitr=ext.begin(); |
|---|
| 647 | sitr!=ext.end(); |
|---|
| 648 | ++sitr) |
|---|
| 649 | { |
|---|
| 650 | lowercase_ext.push_back(tolower(*sitr)); |
|---|
| 651 | } |
|---|
| 652 | |
|---|
| 653 | ExtensionAliasMap::iterator itr=_extAliasMap.find(lowercase_ext); |
|---|
| 654 | if (itr!=_extAliasMap.end() && ext != itr->second) return createLibraryNameForExtension(itr->second); |
|---|
| 655 | |
|---|
| 656 | #ifdef OSG_JAVA_BUILD |
|---|
| 657 | static std::string prepend = std::string("osgPlugins-")+std::string(osgGetVersion())+std::string("/java"); |
|---|
| 658 | #elseif defined(__APPLE__) |
|---|
| 659 | |
|---|
| 660 | |
|---|
| 661 | |
|---|
| 662 | static std::string prepend = ""; |
|---|
| 663 | #else |
|---|
| 664 | static std::string prepend = std::string("osgPlugins-")+std::string(osgGetVersion())+std::string("/"); |
|---|
| 665 | #endif |
|---|
| 666 | |
|---|
| 667 | #if defined(__CYGWIN__) |
|---|
| 668 | #ifdef _DEBUG |
|---|
| 669 | return prepend+"cygwin_"+"osgdb_"+lowercase_ext+OSG_DEBUG_POSTFIX_WITH_QUOTES+".dll"; |
|---|
| 670 | #else |
|---|
| 671 | return prepend+"cygwin_"+"osgdb_"+lowercase_ext+".dll"; |
|---|
| 672 | #endif |
|---|
| 673 | #elif defined(__MINGW32__) |
|---|
| 674 | return prepend+"mingw_"+"osgdb_"+lowercase_ext+".dll"; |
|---|
| 675 | #elif defined(WIN32) |
|---|
| 676 | #ifdef _DEBUG |
|---|
| 677 | return prepend+"osgdb_"+lowercase_ext+ OSG_DEBUG_POSTFIX_WITH_QUOTES +".dll"; |
|---|
| 678 | #else |
|---|
| 679 | return prepend+"osgdb_"+lowercase_ext+".dll"; |
|---|
| 680 | #endif |
|---|
| 681 | #elif macintosh |
|---|
| 682 | return prepend+"osgdb_"+lowercase_ext; |
|---|
| 683 | #elif defined(__hpux__) |
|---|
| 684 | |
|---|
| 685 | return prepend+"osgdb_"+lowercase_ext+".sl"; |
|---|
| 686 | #else |
|---|
| 687 | #ifdef _DEBUG |
|---|
| 688 | return prepend+"osgdb_"+lowercase_ext+ OSG_DEBUG_POSTFIX_WITH_QUOTES + ".so"; |
|---|
| 689 | #else |
|---|
| 690 | return prepend+"osgdb_"+lowercase_ext+".so"; |
|---|
| 691 | #endif |
|---|
| 692 | #endif |
|---|
| 693 | |
|---|
| 694 | } |
|---|
| 695 | |
|---|
| 696 | std::string Registry::createLibraryNameForNodeKit(const std::string& name) |
|---|
| 697 | { |
|---|
| 698 | #if defined(__CYGWIN__) |
|---|
| 699 | return "cyg"+name+".dll"; |
|---|
| 700 | #elif defined(__MINGW32__) |
|---|
| 701 | return "lib"+name+".dll"; |
|---|
| 702 | #elif defined(WIN32) |
|---|
| 703 | #ifdef _DEBUG |
|---|
| 704 | return name+OSG_DEBUG_POSTFIX_WITH_QUOTES +".dll"; |
|---|
| 705 | #else |
|---|
| 706 | return name+".dll"; |
|---|
| 707 | #endif |
|---|
| 708 | #elif macintosh |
|---|
| 709 | return name; |
|---|
| 710 | #elif defined(__hpux__) |
|---|
| 711 | |
|---|
| 712 | return "lib"+name+".sl"; |
|---|
| 713 | #else |
|---|
| 714 | #ifdef _DEBUG |
|---|
| 715 | return "lib"+name+OSG_DEBUG_POSTFIX_WITH_QUOTES +".so"; |
|---|
| 716 | #else |
|---|
| 717 | return "lib"+name+".so"; |
|---|
| 718 | #endif |
|---|
| 719 | #endif |
|---|
| 720 | } |
|---|
| 721 | |
|---|
| 722 | Registry::LoadStatus Registry::loadLibrary(const std::string& fileName) |
|---|
| 723 | { |
|---|
| 724 | OpenThreads::ScopedLock<OpenThreads::ReentrantMutex> lock(_pluginMutex); |
|---|
| 725 | |
|---|
| 726 | DynamicLibraryList::iterator ditr = getLibraryItr(fileName); |
|---|
| 727 | if (ditr!=_dlList.end()) return PREVIOUSLY_LOADED; |
|---|
| 728 | |
|---|
| 729 | _openingLibrary=true; |
|---|
| 730 | |
|---|
| 731 | DynamicLibrary* dl = DynamicLibrary::loadLibrary(fileName); |
|---|
| 732 | _openingLibrary=false; |
|---|
| 733 | |
|---|
| 734 | if (dl) |
|---|
| 735 | { |
|---|
| 736 | _dlList.push_back(dl); |
|---|
| 737 | return LOADED; |
|---|
| 738 | } |
|---|
| 739 | return NOT_LOADED; |
|---|
| 740 | } |
|---|
| 741 | |
|---|
| 742 | |
|---|
| 743 | bool Registry::closeLibrary(const std::string& fileName) |
|---|
| 744 | { |
|---|
| 745 | OpenThreads::ScopedLock<OpenThreads::ReentrantMutex> lock(_pluginMutex); |
|---|
| 746 | DynamicLibraryList::iterator ditr = getLibraryItr(fileName); |
|---|
| 747 | if (ditr!=_dlList.end()) |
|---|
| 748 | { |
|---|
| 749 | _dlList.erase(ditr); |
|---|
| 750 | return true; |
|---|
| 751 | } |
|---|
| 752 | return false; |
|---|
| 753 | } |
|---|
| 754 | |
|---|
| 755 | void Registry::closeAllLibraries() |
|---|
| 756 | { |
|---|
| 757 | |
|---|
| 758 | OpenThreads::ScopedLock<OpenThreads::ReentrantMutex> lock(_pluginMutex); |
|---|
| 759 | _dlList.clear(); |
|---|
| 760 | } |
|---|
| 761 | |
|---|
| 762 | Registry::DynamicLibraryList::iterator Registry::getLibraryItr(const std::string& fileName) |
|---|
| 763 | { |
|---|
| 764 | DynamicLibraryList::iterator ditr = _dlList.begin(); |
|---|
| 765 | for(;ditr!=_dlList.end();++ditr) |
|---|
| 766 | { |
|---|
| 767 | if ((*ditr)->getName()==fileName) return ditr; |
|---|
| 768 | } |
|---|
| 769 | return _dlList.end(); |
|---|
| 770 | } |
|---|
| 771 | |
|---|
| 772 | DynamicLibrary* Registry::getLibrary(const std::string& fileName) |
|---|
| 773 | { |
|---|
| 774 | OpenThreads::ScopedLock<OpenThreads::ReentrantMutex> lock(_pluginMutex); |
|---|
| 775 | DynamicLibraryList::iterator ditr = getLibraryItr(fileName); |
|---|
| 776 | if (ditr!=_dlList.end()) return ditr->get(); |
|---|
| 777 | else return NULL; |
|---|
| 778 | } |
|---|
| 779 | |
|---|
| 780 | ReaderWriter* Registry::getReaderWriterForExtension(const std::string& ext) |
|---|
| 781 | { |
|---|
| 782 | |
|---|
| 783 | std::set<ReaderWriter*> rwOriginal; |
|---|
| 784 | |
|---|
| 785 | OpenThreads::ScopedLock<OpenThreads::ReentrantMutex> lock(_pluginMutex); |
|---|
| 786 | |
|---|
| 787 | |
|---|
| 788 | for(ReaderWriterList::iterator itr=_rwList.begin(); |
|---|
| 789 | itr!=_rwList.end(); |
|---|
| 790 | ++itr) |
|---|
| 791 | { |
|---|
| 792 | rwOriginal.insert(itr->get()); |
|---|
| 793 | if((*itr)->acceptsExtension(ext)) return (*itr).get(); |
|---|
| 794 | } |
|---|
| 795 | |
|---|
| 796 | |
|---|
| 797 | std::string libraryName = createLibraryNameForExtension(ext); |
|---|
| 798 | notify(INFO) << "Now checking for plug-in "<<libraryName<< std::endl; |
|---|
| 799 | if (loadLibrary(libraryName)==LOADED) |
|---|
| 800 | { |
|---|
| 801 | for(ReaderWriterList::iterator itr=_rwList.begin(); |
|---|
| 802 | itr!=_rwList.end(); |
|---|
| 803 | ++itr) |
|---|
| 804 | { |
|---|
| 805 | if (rwOriginal.find(itr->get())==rwOriginal.end()) |
|---|
| 806 | { |
|---|
| 807 | if((*itr)->acceptsExtension(ext)) return (*itr).get(); |
|---|
| 808 | } |
|---|
| 809 | } |
|---|
| 810 | } |
|---|
| 811 | |
|---|
| 812 | return NULL; |
|---|
| 813 | |
|---|
| 814 | } |
|---|
| 815 | |
|---|
| 816 | struct concrete_wrapper: basic_type_wrapper |
|---|
| 817 | { |
|---|
| 818 | virtual ~concrete_wrapper() {} |
|---|
| 819 | concrete_wrapper(const osg::Object *myobj) : myobj_(myobj) {} |
|---|
| 820 | bool matches(const osg::Object *proto) const |
|---|
| 821 | { |
|---|
| 822 | return myobj_->isSameKindAs(proto); |
|---|
| 823 | } |
|---|
| 824 | const osg::Object *myobj_; |
|---|
| 825 | }; |
|---|
| 826 | |
|---|
| 827 | osg::Object* Registry::readObjectOfType(const osg::Object& compObj,Input& fr) |
|---|
| 828 | { |
|---|
| 829 | return readObjectOfType(concrete_wrapper(&compObj), fr); |
|---|
| 830 | } |
|---|
| 831 | |
|---|
| 832 | osg::Object* Registry::readObjectOfType(const basic_type_wrapper &btw,Input& fr) |
|---|
| 833 | { |
|---|
| 834 | const char *str = fr[0].getStr(); |
|---|
| 835 | if (str==NULL) return NULL; |
|---|
| 836 | |
|---|
| 837 | if (fr[0].matchWord("Use")) |
|---|
| 838 | { |
|---|
| 839 | if (fr[1].isString()) |
|---|
| 840 | { |
|---|
| 841 | Object* obj = fr.getObjectForUniqueID(fr[1].getStr()); |
|---|
| 842 | if (obj && btw.matches(obj)) |
|---|
| 843 | { |
|---|
| 844 | fr+=2; |
|---|
| 845 | return obj; |
|---|
| 846 | } |
|---|
| 847 | } |
|---|
| 848 | else return NULL; |
|---|
| 849 | |
|---|
| 850 | } |
|---|
| 851 | |
|---|
| 852 | std::string name = str; |
|---|
| 853 | DotOsgWrapperMap::iterator itr = _objectWrapperMap.find(name); |
|---|
| 854 | if (itr==_objectWrapperMap.end()) |
|---|
| 855 | { |
|---|
| 856 | |
|---|
| 857 | std::string token = fr[0].getStr(); |
|---|
| 858 | std::string::size_type posDoubleColon = token.rfind("::"); |
|---|
| 859 | if (posDoubleColon != std::string::npos) |
|---|
| 860 | { |
|---|
| 861 | |
|---|
| 862 | |
|---|
| 863 | |
|---|
| 864 | std::string libraryName = std::string(token,0,posDoubleColon); |
|---|
| 865 | |
|---|
| 866 | |
|---|
| 867 | std::string nodeKitLibraryName = createLibraryNameForNodeKit(libraryName); |
|---|
| 868 | if (loadLibrary(nodeKitLibraryName)==LOADED) return readObjectOfType(btw,fr); |
|---|
| 869 | |
|---|
| 870 | |
|---|
| 871 | std::string pluginLibraryName = createLibraryNameForExtension(libraryName); |
|---|
| 872 | if (loadLibrary(pluginLibraryName)==LOADED) return readObjectOfType(btw,fr); |
|---|
| 873 | } |
|---|
| 874 | } |
|---|
| 875 | else if (fr[1].isOpenBracket()) |
|---|
| 876 | { |
|---|
| 877 | DotOsgWrapper* wrapper = itr->second.get(); |
|---|
| 878 | const osg::Object* proto = wrapper->getPrototype(); |
|---|
| 879 | if (proto==NULL) |
|---|
| 880 | { |
|---|
| 881 | osg::notify(osg::WARN)<<"Token "<<fr[0].getStr()<<" read, but has no prototype, cannot load."<< std::endl; |
|---|
| 882 | return NULL; |
|---|
| 883 | } |
|---|
| 884 | |
|---|
| 885 | if (!btw.matches(proto)) |
|---|
| 886 | { |
|---|
| 887 | return NULL; |
|---|
| 888 | } |
|---|
| 889 | |
|---|
| 890 | |
|---|
| 891 | |
|---|
| 892 | int entry = fr[0].getNoNestedBrackets(); |
|---|
| 893 | fr+=2; |
|---|
| 894 | |
|---|
| 895 | const DotOsgWrapper::Associates& assoc = wrapper->getAssociates(); |
|---|
| 896 | osg::Object* obj = proto->cloneType(); |
|---|
| 897 | |
|---|
| 898 | while(!fr.eof() && fr[0].getNoNestedBrackets()>entry) |
|---|
| 899 | { |
|---|
| 900 | bool iteratorAdvanced = false; |
|---|
| 901 | if (fr[0].matchWord("UniqueID") && fr[1].isString()) |
|---|
| 902 | { |
|---|
| 903 | fr.registerUniqueIDForObject(fr[1].getStr(),obj); |
|---|
| 904 | fr += 2; |
|---|
| 905 | iteratorAdvanced = true; |
|---|
| 906 | } |
|---|
| 907 | |
|---|
| 908 | |
|---|
| 909 | |
|---|
| 910 | |
|---|
| 911 | for(DotOsgWrapper::Associates::const_iterator aitr=assoc.begin(); |
|---|
| 912 | aitr!=assoc.end(); |
|---|
| 913 | ++aitr) |
|---|
| 914 | { |
|---|
| 915 | DotOsgWrapperMap::iterator mitr = _objectWrapperMap.find(*aitr); |
|---|
| 916 | if (mitr==_objectWrapperMap.end()) |
|---|
| 917 | { |
|---|
| 918 | |
|---|
| 919 | std::string token = *aitr; |
|---|
| 920 | std::string::size_type posDoubleColon = token.rfind("::"); |
|---|
| 921 | if (posDoubleColon != std::string::npos) |
|---|
| 922 | { |
|---|
| 923 | |
|---|
| 924 | |
|---|
| 925 | |
|---|
| 926 | std::string libraryName = std::string(token,0,posDoubleColon); |
|---|
| 927 | |
|---|
| 928 | |
|---|
| 929 | std::string nodeKitLibraryName = createLibraryNameForNodeKit(libraryName); |
|---|
| 930 | if (loadLibrary(nodeKitLibraryName)==LOADED) |
|---|
| 931 | { |
|---|
| 932 | mitr = _objectWrapperMap.find(*aitr); |
|---|
| 933 | if (mitr==_objectWrapperMap.end()) |
|---|
| 934 | { |
|---|
| 935 | |
|---|
| 936 | std::string pluginLibraryName = createLibraryNameForExtension(libraryName); |
|---|
| 937 | if (loadLibrary(pluginLibraryName)==LOADED) |
|---|
| 938 | { |
|---|
| 939 | mitr = _objectWrapperMap.find(*aitr); |
|---|
| 940 | } |
|---|
| 941 | } |
|---|
| 942 | } |
|---|
| 943 | } |
|---|
| 944 | } |
|---|
| 945 | |
|---|
| 946 | if (mitr!=_objectWrapperMap.end()) |
|---|
| 947 | { |
|---|
| 948 | |
|---|
| 949 | DotOsgWrapper::ReadFunc rf = mitr->second->getReadFunc(); |
|---|
| 950 | if (rf && (*rf)(*obj,fr)) iteratorAdvanced = true; |
|---|
| 951 | } |
|---|
| 952 | |
|---|
| 953 | } |
|---|
| 954 | |
|---|
| 955 | if (!iteratorAdvanced) fr.advanceOverCurrentFieldOrBlock(); |
|---|
| 956 | } |
|---|
| 957 | ++fr; |
|---|
| 958 | |
|---|
| 959 | return obj; |
|---|
| 960 | |
|---|
| 961 | } |
|---|
| 962 | return 0L; |
|---|
| 963 | } |
|---|
| 964 | |
|---|
| 965 | |
|---|
| 966 | |
|---|
| 967 | |
|---|
| 968 | osg::Object* Registry::readObject(DotOsgWrapperMap& dowMap,Input& fr) |
|---|
| 969 | { |
|---|
| 970 | const char *str = fr[0].getStr(); |
|---|
| 971 | if (str==NULL) return NULL; |
|---|
| 972 | |
|---|
| 973 | std::string name = str; |
|---|
| 974 | DotOsgWrapperMap::iterator itr = dowMap.find(name); |
|---|
| 975 | if (itr==dowMap.end()) |
|---|
| 976 | { |
|---|
| 977 | |
|---|
| 978 | std::string token = fr[0].getStr(); |
|---|
| 979 | std::string::size_type posDoubleColon = token.rfind("::"); |
|---|
| 980 | if (posDoubleColon != std::string::npos) |
|---|
| 981 | { |
|---|
| 982 | |
|---|
| 983 | |
|---|
| 984 | |
|---|
| 985 | |
|---|
| 986 | std::string libraryName = std::string(token,0,posDoubleColon); |
|---|
| 987 | |
|---|
| 988 | |
|---|
| 989 | std::string nodeKitLibraryName = createLibraryNameForNodeKit(libraryName); |
|---|
| 990 | if (loadLibrary(nodeKitLibraryName)==LOADED) return readObject(dowMap,fr); |
|---|
| 991 | |
|---|
| 992 | |
|---|
| 993 | std::string pluginLibraryName = createLibraryNameForExtension(libraryName); |
|---|
| 994 | if (loadLibrary(pluginLibraryName)==LOADED) return readObject(dowMap,fr); |
|---|
| 995 | } |
|---|
| 996 | } |
|---|
| 997 | else if (fr[1].isOpenBracket()) |
|---|
| 998 | { |
|---|
| 999 | |
|---|
| 1000 | DotOsgWrapper* wrapper = itr->second.get(); |
|---|
| 1001 | const osg::Object* proto = wrapper->getPrototype(); |
|---|
| 1002 | if (proto==NULL) |
|---|
| 1003 | { |
|---|
| 1004 | osg::notify(osg::WARN)<<"Token "<<fr[0].getStr()<<" read, but has no prototype, cannot load."<< std::endl; |
|---|
| 1005 | return NULL; |
|---|
| 1006 | } |
|---|
| 1007 | |
|---|
| 1008 | |
|---|
| 1009 | |
|---|
| 1010 | int entry = fr[0].getNoNestedBrackets(); |
|---|
| 1011 | fr+=2; |
|---|
| 1012 | |
|---|
| 1013 | const DotOsgWrapper::Associates& assoc = wrapper->getAssociates(); |
|---|
| 1014 | osg::Object* obj = proto->cloneType(); |
|---|
| 1015 | |
|---|
| 1016 | while(!fr.eof() && fr[0].getNoNestedBrackets()>entry) |
|---|
| 1017 | { |
|---|
| 1018 | bool iteratorAdvanced = false; |
|---|
| 1019 | if (fr[0].matchWord("UniqueID") && fr[1].isString()) |
|---|
| 1020 | { |
|---|
| 1021 | fr.registerUniqueIDForObject(fr[1].getStr(),obj); |
|---|
| 1022 | fr += 2; |
|---|
| 1023 | iteratorAdvanced = true; |
|---|
| 1024 | } |
|---|
| 1025 | |
|---|
| 1026 | |
|---|
| 1027 | |
|---|
| 1028 | |
|---|
| 1029 | for(DotOsgWrapper::Associates::const_iterator aitr=assoc.begin(); |
|---|
| 1030 | aitr!=assoc.end(); |
|---|
| 1031 | ++aitr) |
|---|
| 1032 | { |
|---|
| 1033 | DotOsgWrapperMap::iterator mitr = _objectWrapperMap.find(*aitr); |
|---|
| 1034 | if (mitr==_objectWrapperMap.end()) |
|---|
| 1035 | { |
|---|
| 1036 | |
|---|
| 1037 | std::string token = *aitr; |
|---|
| 1038 | std::string::size_type posDoubleColon = token.rfind("::"); |
|---|
| 1039 | if (posDoubleColon != std::string::npos) |
|---|
| 1040 | { |
|---|
| 1041 | |
|---|
| 1042 | |
|---|
| 1043 | |
|---|
| 1044 | |
|---|
| 1045 | |
|---|
| 1046 | std::string libraryName = std::string(token,0,posDoubleColon); |
|---|
| 1047 | |
|---|
| 1048 | |
|---|
| 1049 | std::string nodeKitLibraryName = createLibraryNameForNodeKit(libraryName); |
|---|
| 1050 | if (loadLibrary(nodeKitLibraryName)==LOADED) |
|---|
| 1051 | { |
|---|
| 1052 | mitr = _objectWrapperMap.find(*aitr); |
|---|
| 1053 | } |
|---|
| 1054 | |
|---|
| 1055 | if (mitr==_objectWrapperMap.end()) |
|---|
| 1056 | { |
|---|
| 1057 | |
|---|
| 1058 | std::string pluginLibraryName = createLibraryNameForExtension(libraryName); |
|---|
| 1059 | if (loadLibrary(pluginLibraryName)==LOADED) |
|---|
| 1060 | { |
|---|
| 1061 | mitr = _objectWrapperMap.find(*aitr); |
|---|
| 1062 | } |
|---|
| 1063 | } |
|---|
| 1064 | |
|---|
| 1065 | } |
|---|
| 1066 | } |
|---|
| 1067 | |
|---|
| 1068 | if (mitr!=_objectWrapperMap.end()) |
|---|
| 1069 | { |
|---|
| 1070 | |
|---|
| 1071 | DotOsgWrapper::ReadFunc rf = mitr->second->getReadFunc(); |
|---|
| 1072 | if (rf && (*rf)(*obj,fr)) iteratorAdvanced = true; |
|---|
| 1073 | } |
|---|
| 1074 | |
|---|
| 1075 | } |
|---|
| 1076 | |
|---|
| 1077 | if (!iteratorAdvanced) fr.advanceOverCurrentFieldOrBlock(); |
|---|
| 1078 | } |
|---|
| 1079 | ++fr; |
|---|
| 1080 | |
|---|
| 1081 | return obj; |
|---|
| 1082 | |
|---|
| 1083 | } |
|---|
| 1084 | |
|---|
| 1085 | return 0L; |
|---|
| 1086 | } |
|---|
| 1087 | |
|---|
| 1088 | |
|---|
| 1089 | |
|---|
| 1090 | |
|---|
| 1091 | Object* Registry::readObject(Input& fr) |
|---|
| 1092 | { |
|---|
| 1093 | if (fr[0].matchWord("Use")) |
|---|
| 1094 | { |
|---|
| 1095 | if (fr[1].isString()) |
|---|
| 1096 | { |
|---|
| 1097 | Object* obj = fr.getObjectForUniqueID(fr[1].getStr()); |
|---|
| 1098 | if (obj) fr+=2; |
|---|
| 1099 | return obj; |
|---|
| 1100 | } |
|---|
| 1101 | else return NULL; |
|---|
| 1102 | |
|---|
| 1103 | } |
|---|
| 1104 | |
|---|
| 1105 | return readObject(_objectWrapperMap,fr); |
|---|
| 1106 | } |
|---|
| 1107 | |
|---|
| 1108 | |
|---|
| 1109 | |
|---|
| 1110 | |
|---|
| 1111 | |
|---|
| 1112 | Image* Registry::readImage(Input& fr) |
|---|
| 1113 | { |
|---|
| 1114 | if (fr[0].matchWord("Use")) |
|---|
| 1115 | { |
|---|
| 1116 | if (fr[1].isString()) |
|---|
| 1117 | { |
|---|
| 1118 | Image* image = dynamic_cast<Image*>(fr.getObjectForUniqueID(fr[1].getStr())); |
|---|
| 1119 | if (image) fr+=2; |
|---|
| 1120 | return image; |
|---|
| 1121 | } |
|---|
| 1122 | else return NULL; |
|---|
| 1123 | |
|---|
| 1124 | } |
|---|
| 1125 | |
|---|
| 1126 | osg::Object* obj = readObject(_imageWrapperMap,fr); |
|---|
| 1127 | osg::Image* image = dynamic_cast<Image*>(obj); |
|---|
| 1128 | if (image) return image; |
|---|
| 1129 | else if (obj) obj->unref(); |
|---|
| 1130 | |
|---|
| 1131 | return NULL; |
|---|
| 1132 | } |
|---|
| 1133 | |
|---|
| 1134 | |
|---|
| 1135 | |
|---|
| 1136 | |
|---|
| 1137 | |
|---|
| 1138 | Drawable* Registry::readDrawable(Input& fr) |
|---|
| 1139 | { |
|---|
| 1140 | if (fr[0].matchWord("Use")) |
|---|
| 1141 | { |
|---|
| 1142 | if (fr[1].isString()) |
|---|
| 1143 | { |
|---|
| 1144 | Drawable* drawable = dynamic_cast<Drawable*>(fr.getObjectForUniqueID(fr[1].getStr())); |
|---|
| 1145 | if (drawable) fr+=2; |
|---|
| 1146 | return drawable; |
|---|
| 1147 | } |
|---|
| 1148 | else return NULL; |
|---|
| 1149 | |
|---|
| 1150 | } |
|---|
| 1151 | |
|---|
| 1152 | osg::Object* obj = readObject(_drawableWrapperMap,fr); |
|---|
| 1153 | osg::Drawable* drawable = dynamic_cast<Drawable*>(obj); |
|---|
| 1154 | if (drawable) return drawable; |
|---|
| 1155 | else if (obj) obj->unref(); |
|---|
| 1156 | |
|---|
| 1157 | return NULL; |
|---|
| 1158 | } |
|---|
| 1159 | |
|---|
| 1160 | |
|---|
| 1161 | |
|---|
| 1162 | |
|---|
| 1163 | StateAttribute* Registry::readStateAttribute(Input& fr) |
|---|
| 1164 | { |
|---|
| 1165 | |
|---|
| 1166 | if (fr[0].matchWord("Use")) |
|---|
| 1167 | { |
|---|
| 1168 | if (fr[1].isString()) |
|---|
| 1169 | { |
|---|
| 1170 | StateAttribute* attribute = dynamic_cast<StateAttribute*>(fr.getObjectForUniqueID(fr[1].getStr())); |
|---|
| 1171 | if (attribute) fr+=2; |
|---|
| 1172 | return attribute; |
|---|
| 1173 | } |
|---|
| 1174 | else return NULL; |
|---|
| 1175 | |
|---|
| 1176 | } |
|---|
| 1177 | |
|---|
| 1178 | return dynamic_cast<StateAttribute*>(readObject(_stateAttrWrapperMap,fr)); |
|---|
| 1179 | } |
|---|
| 1180 | |
|---|
| 1181 | |
|---|
| 1182 | |
|---|
| 1183 | |
|---|
| 1184 | Uniform* Registry::readUniform(Input& fr) |
|---|
| 1185 | { |
|---|
| 1186 | |
|---|
| 1187 | if (fr[0].matchWord("Use")) |
|---|
| 1188 | { |
|---|
| 1189 | if (fr[1].isString()) |
|---|
| 1190 | { |
|---|
| 1191 | Uniform* attribute = dynamic_cast<Uniform*>(fr.getObjectForUniqueID(fr[1].getStr())); |
|---|
| 1192 | if (attribute) fr+=2; |
|---|
| 1193 | return attribute; |
|---|
| 1194 | } |
|---|
| 1195 | else return NULL; |
|---|
| 1196 | |
|---|
| 1197 | } |
|---|
| 1198 | |
|---|
| 1199 | return dynamic_cast<Uniform*>(readObject(_uniformWrapperMap,fr)); |
|---|
| 1200 | } |
|---|
| 1201 | |
|---|
| 1202 | |
|---|
| 1203 | |
|---|
| 1204 | |
|---|
| 1205 | Node* Registry::readNode(Input& fr) |
|---|
| 1206 | { |
|---|
| 1207 | if (fr[0].matchWord("Use")) |
|---|
| 1208 | { |
|---|
| 1209 | if (fr[1].isString()) |
|---|
| 1210 | { |
|---|
| 1211 | Node* node = dynamic_cast<Node*>(fr.getObjectForUniqueID(fr[1].getStr())); |
|---|
| 1212 | if (node) fr+=2; |
|---|
| 1213 | return node; |
|---|
| 1214 | } |
|---|
| 1215 | else return NULL; |
|---|
| 1216 | |
|---|
| 1217 | } |
|---|
| 1218 | |
|---|
| 1219 | osg::Object* obj = readObject(_nodeWrapperMap,fr); |
|---|
| 1220 | osg::Node* node = dynamic_cast<Node*>(obj); |
|---|
| 1221 | if (node) return node; |
|---|
| 1222 | else if (obj) obj->unref(); |
|---|
| 1223 | |
|---|
| 1224 | return NULL; |
|---|
| 1225 | } |
|---|
| 1226 | |
|---|
| 1227 | |
|---|
| 1228 | |
|---|
| 1229 | |
|---|
| 1230 | Shader* Registry::readShader(Input& fr) |
|---|
| 1231 | { |
|---|
| 1232 | if (fr[0].matchWord("Use")) |
|---|
| 1233 | { |
|---|
| 1234 | if (fr[1].isString()) |
|---|
| 1235 | { |
|---|
| 1236 | Shader* shader = dynamic_cast<Shader*>(fr.getObjectForUniqueID(fr[1].getStr())); |
|---|
| 1237 | if (shader) fr+=2; |
|---|
| 1238 | return shader; |
|---|
| 1239 | } |
|---|
| 1240 | else return NULL; |
|---|
| 1241 | |
|---|
| 1242 | } |
|---|
| 1243 | |
|---|
| 1244 | osg::Object* obj = readObject(_shaderWrapperMap,fr); |
|---|
| 1245 | osg::Shader* shader = dynamic_cast<Shader*>(obj); |
|---|
| 1246 | if (shader) return shader; |
|---|
| 1247 | else if (obj) obj->unref(); |
|---|
| 1248 | |
|---|
| 1249 | return NULL; |
|---|
| 1250 | } |
|---|
| 1251 | |
|---|
| 1252 | |
|---|
| 1253 | |
|---|
| 1254 | |
|---|
| 1255 | bool Registry::writeObject(const osg::Object& obj,Output& fw) |
|---|
| 1256 | { |
|---|
| 1257 | |
|---|
| 1258 | if (obj.referenceCount()>1) |
|---|
| 1259 | { |
|---|
| 1260 | std::string uniqueID; |
|---|
| 1261 | if (fw.getUniqueIDForObject(&obj,uniqueID)) |
|---|
| 1262 | { |
|---|
| 1263 | fw.writeUseID( uniqueID ); |
|---|
| 1264 | return true; |
|---|
| 1265 | } |
|---|
| 1266 | } |
|---|
| 1267 | |
|---|
| 1268 | const std::string classname( obj.className() ); |
|---|
| 1269 | const std::string libraryName( obj.libraryName() ); |
|---|
| 1270 | const std::string compositeName( libraryName + "::" + classname ); |
|---|
| 1271 | |
|---|
| 1272 | |
|---|
| 1273 | DotOsgWrapperMap::iterator itr = _classNameWrapperMap.find(compositeName); |
|---|
| 1274 | |
|---|
| 1275 | if (itr==_classNameWrapperMap.end()) |
|---|
| 1276 | { |
|---|
| 1277 | |
|---|
| 1278 | std::string nodeKitLibraryName = createLibraryNameForNodeKit(libraryName); |
|---|
| 1279 | if (loadLibrary(nodeKitLibraryName)==LOADED) return writeObject(obj,fw); |
|---|
| 1280 | |
|---|
| 1281 | |
|---|
| 1282 | std::string pluginLibraryName = createLibraryNameForExtension(libraryName); |
|---|
| 1283 | if (loadLibrary(pluginLibraryName)==LOADED) return writeObject(obj,fw); |
|---|
| 1284 | |
|---|
| 1285 | |
|---|
| 1286 | if (itr == _classNameWrapperMap.end()) |
|---|
| 1287 | itr = _classNameWrapperMap.find(classname); |
|---|
| 1288 | } |
|---|
| 1289 | |
|---|
| 1290 | if (itr!=_classNameWrapperMap.end()) |
|---|
| 1291 | { |
|---|
| 1292 | DotOsgWrapper* wrapper = itr->second.get(); |
|---|
| 1293 | const DotOsgWrapper::Associates& assoc = wrapper->getAssociates(); |
|---|
| 1294 | |
|---|
| 1295 | if (libraryName=="osg") |
|---|
| 1296 | { |
|---|
| 1297 | |
|---|
| 1298 | fw.writeBeginObject( wrapper->getName() ); |
|---|
| 1299 | } |
|---|
| 1300 | else |
|---|
| 1301 | { |
|---|
| 1302 | |
|---|
| 1303 | std::string::size_type posDoubleColon = wrapper->getName().find("::"); |
|---|
| 1304 | if (posDoubleColon != std::string::npos) |
|---|
| 1305 | { |
|---|
| 1306 | fw.writeBeginObject( wrapper->getName() ); |
|---|
| 1307 | } |
|---|
| 1308 | else |
|---|
| 1309 | { |
|---|
| 1310 | fw.writeBeginObject( libraryName + "::" + wrapper->getName() ); |
|---|
| 1311 | } |
|---|
| 1312 | } |
|---|
| 1313 | fw.moveIn(); |
|---|
| 1314 | |
|---|
| 1315 | |
|---|
| 1316 | |
|---|
| 1317 | if (obj.referenceCount()>1) |
|---|
| 1318 | { |
|---|
| 1319 | std::string uniqueID; |
|---|
| 1320 | fw.createUniqueIDForObject(&obj,uniqueID); |
|---|
| 1321 | fw.registerUniqueIDForObject(&obj,uniqueID); |
|---|
| 1322 | fw.writeUniqueID( uniqueID ); |
|---|
| 1323 | } |
|---|
| 1324 | |
|---|
| 1325 | |
|---|
| 1326 | |
|---|
| 1327 | |
|---|
| 1328 | for(DotOsgWrapper::Associates::const_iterator aitr=assoc.begin(); |
|---|
| 1329 | aitr!=assoc.end(); |
|---|
| 1330 | ++aitr) |
|---|
| 1331 | { |
|---|
| 1332 | DotOsgWrapperMap::iterator mitr = _objectWrapperMap.find(*aitr); |
|---|
| 1333 | if (mitr==_objectWrapperMap.end()) |
|---|
| 1334 | { |
|---|
| 1335 | |
|---|
| 1336 | std::string token = *aitr; |
|---|
| 1337 | std::string::size_type posDoubleColon = token.rfind("::"); |
|---|
| 1338 | if (posDoubleColon != std::string::npos) |
|---|
| 1339 | { |
|---|
| 1340 | |
|---|
| 1341 | |
|---|
| 1342 | |
|---|
| 1343 | |
|---|
| 1344 | |
|---|
| 1345 | std::string libraryName = std::string(token,0,posDoubleColon); |
|---|
| 1346 | |
|---|
| 1347 | |
|---|
| 1348 | std::string nodeKitLibraryName = createLibraryNameForNodeKit(libraryName); |
|---|
| 1349 | if (loadLibrary(nodeKitLibraryName)==LOADED) |
|---|
| 1350 | { |
|---|
| 1351 | mitr = _objectWrapperMap.find(*aitr); |
|---|
| 1352 | } |
|---|
| 1353 | |
|---|
| 1354 | if (mitr==_objectWrapperMap.end()) |
|---|
| 1355 | { |
|---|
| 1356 | |
|---|
| 1357 | std::string pluginLibraryName = createLibraryNameForExtension(libraryName); |
|---|
| 1358 | if (loadLibrary(pluginLibraryName)==LOADED) |
|---|
| 1359 | { |
|---|
| 1360 | mitr = _objectWrapperMap.find(*aitr); |
|---|
| 1361 | } |
|---|
| 1362 | } |
|---|
| 1363 | |
|---|
| 1364 | } |
|---|
| 1365 | } |
|---|
| 1366 | if (mitr!=_objectWrapperMap.end()) |
|---|
| 1367 | { |
|---|
| 1368 | |
|---|
| 1369 | DotOsgWrapper::WriteFunc wf = mitr->second->getWriteFunc(); |
|---|
| 1370 | if (wf) (*wf)(obj,fw); |
|---|
| 1371 | } |
|---|
| 1372 | |
|---|
| 1373 | } |
|---|
| 1374 | |
|---|
| 1375 | fw.moveOut(); |
|---|
| 1376 | fw.writeEndObject(); |
|---|
| 1377 | |
|---|
| 1378 | return true; |
|---|
| 1379 | } |
|---|
| 1380 | |
|---|
| 1381 | return false; |
|---|
| 1382 | } |
|---|
| 1383 | |
|---|
| 1384 | |
|---|
| 1385 | |
|---|
| 1386 | struct Registry::ReadObjectFunctor : public Registry::ReadFunctor |
|---|
| 1387 | { |
|---|
| 1388 | ReadObjectFunctor(const std::string& filename, const ReaderWriter::Options* options):ReadFunctor(filename,options) {} |
|---|
| 1389 | |
|---|
| 1390 | virtual ReaderWriter::ReadResult doRead(ReaderWriter& rw) const { return rw.readObject(_filename, _options); } |
|---|
| 1391 | virtual bool isValid(ReaderWriter::ReadResult& readResult) const { return readResult.validObject(); } |
|---|
| 1392 | virtual bool isValid(osg::Object* object) const { return object!=0; } |
|---|
| 1393 | }; |
|---|
| 1394 | |
|---|
| 1395 | struct Registry::ReadImageFunctor : public Registry::ReadFunctor |
|---|
| 1396 | { |
|---|
| 1397 | ReadImageFunctor(const std::string& filename, const ReaderWriter::Options* options):ReadFunctor(filename,options) {} |
|---|
| 1398 | |
|---|
| 1399 | virtual ReaderWriter::ReadResult doRead(ReaderWriter& rw)const { return rw.readImage(_filename, _options); } |
|---|
| 1400 | virtual bool isValid(ReaderWriter::ReadResult& readResult) const { return readResult.validImage(); } |
|---|
| 1401 | virtual bool isValid(osg::Object* object) const { return dynamic_cast<osg::Image*>(object)!=0; } |
|---|
| 1402 | }; |
|---|
| 1403 | |
|---|
| 1404 | struct Registry::ReadHeightFieldFunctor : public Registry::ReadFunctor |
|---|
| 1405 | { |
|---|
| 1406 | ReadHeightFieldFunctor(const std::string& filename, const ReaderWriter::Options* options):ReadFunctor(filename,options) {} |
|---|
| 1407 | |
|---|
| 1408 | virtual ReaderWriter::ReadResult doRead(ReaderWriter& rw) const { return rw.readHeightField(_filename, _options); } |
|---|
| 1409 | virtual bool isValid(ReaderWriter::ReadResult& readResult) const { return readResult.validHeightField(); } |
|---|
| 1410 | virtual bool isValid(osg::Object* object) const { return dynamic_cast<osg::HeightField*>(object)!=0; } |
|---|
| 1411 | }; |
|---|
| 1412 | |
|---|
| 1413 | struct Registry::ReadNodeFunctor : public Registry::ReadFunctor |
|---|
| 1414 | { |
|---|
| 1415 | ReadNodeFunctor(const std::string& filename, const ReaderWriter::Options* options):ReadFunctor(filename,options) {} |
|---|
| 1416 | |
|---|
| 1417 | virtual ReaderWriter::ReadResult doRead(ReaderWriter& rw) const { return rw.readNode(_filename, _options); } |
|---|
| 1418 | virtual bool isValid(ReaderWriter::ReadResult& readResult) const { return readResult.validNode(); } |
|---|
| 1419 | virtual bool isValid(osg::Object* object) const { return dynamic_cast<osg::Node*>(object)!=0; } |
|---|
| 1420 | |
|---|
| 1421 | }; |
|---|
| 1422 | |
|---|
| 1423 | struct Registry::ReadArchiveFunctor : public Registry::ReadFunctor |
|---|
| 1424 | { |
|---|
| 1425 | ReadArchiveFunctor(const std::string& filename, ReaderWriter::ArchiveStatus status, unsigned int indexBlockSizeHint, const ReaderWriter::Options* options): |
|---|
| 1426 | ReadFunctor(filename,options), |
|---|
| 1427 | _status(status), |
|---|
| 1428 | _indexBlockSizeHint(indexBlockSizeHint) {} |
|---|
| 1429 | |
|---|
| 1430 | ReaderWriter::ArchiveStatus _status; |
|---|
| 1431 | unsigned int _indexBlockSizeHint; |
|---|
| 1432 | |
|---|
| 1433 | virtual ReaderWriter::ReadResult doRead(ReaderWriter& rw) const { return rw.openArchive(_filename, _status, _indexBlockSizeHint, _options); } |
|---|
| 1434 | virtual bool isValid(ReaderWriter::ReadResult& readResult) const { return readResult.validArchive(); } |
|---|
| 1435 | virtual bool isValid(osg::Object* object) const { return dynamic_cast<osgDB::Archive*>(object)!=0; } |
|---|
| 1436 | |
|---|
| 1437 | }; |
|---|
| 1438 | |
|---|
| 1439 | struct Registry::ReadShaderFunctor : public Registry::ReadFunctor |
|---|
| 1440 | { |
|---|
| 1441 | ReadShaderFunctor(const std::string& filename, const ReaderWriter::Options* options):ReadFunctor(filename,options) {} |
|---|
| 1442 | |
|---|
| 1443 | virtual ReaderWriter::ReadResult doRead(ReaderWriter& rw)const { return rw.readShader(_filename, _options); } |
|---|
| 1444 | virtual bool isValid(ReaderWriter::ReadResult& readResult) const { return readResult.validShader(); } |
|---|
| 1445 | virtual bool isValid(osg::Object* object) const { return dynamic_cast<osg::Shader*>(object)!=0; } |
|---|
| 1446 | }; |
|---|
| 1447 | |
|---|
| 1448 | void Registry::addArchiveExtension(const std::string ext) |
|---|
| 1449 | { |
|---|
| 1450 | for(ArchiveExtensionList::iterator aitr=_archiveExtList.begin(); |
|---|
| 1451 | aitr!=_archiveExtList.end(); |
|---|
| 1452 | ++aitr) |
|---|
| 1453 | { |
|---|
| 1454 | if ( (*aitr) == ext) |
|---|
| 1455 | return; |
|---|
| 1456 | } |
|---|
| 1457 | _archiveExtList.push_back(ext); |
|---|
| 1458 | } |
|---|
| 1459 | |
|---|
| 1460 | ReaderWriter::ReadResult Registry::read(const ReadFunctor& readFunctor) |
|---|
| 1461 | { |
|---|
| 1462 | for(ArchiveExtensionList::iterator aitr=_archiveExtList.begin(); |
|---|
| 1463 | aitr!=_archiveExtList.end(); |
|---|
| 1464 | ++aitr) |
|---|
| 1465 | { |
|---|
| 1466 | std::string archiveExtension = "." + (*aitr); |
|---|
| 1467 | |
|---|
| 1468 | std::string::size_type positionArchive = readFunctor._filename.find(archiveExtension+'/'); |
|---|
| 1469 | if (positionArchive==std::string::npos) positionArchive = readFunctor._filename.find(archiveExtension+'\\'); |
|---|
| 1470 | if (positionArchive!=std::string::npos) |
|---|
| 1471 | { |
|---|
| 1472 | std::string::size_type endArchive = positionArchive + archiveExtension.length(); |
|---|
| 1473 | std::string archiveName( readFunctor._filename.substr(0,endArchive)); |
|---|
| 1474 | std::string fileName(readFunctor._filename.substr(endArchive+1,std::string::npos)); |
|---|
| 1475 | osg::notify(osg::INFO)<<"Contains archive : "<<readFunctor._filename<<std::endl; |
|---|
| 1476 | osg::notify(osg::INFO)<<" archive : "<<archiveName<<std::endl; |
|---|
| 1477 | osg::notify(osg::INFO)<<" filename : "<<fileName<<std::endl; |
|---|
| 1478 | |
|---|
| 1479 | ReaderWriter::ReadResult result = openArchiveImplementation(archiveName,ReaderWriter::READ, 4096, readFunctor._options); |
|---|
| 1480 | |
|---|
| 1481 | if (!result.validArchive()) return result; |
|---|
| 1482 | |
|---|
| 1483 | osgDB::Archive* archive = result.getArchive(); |
|---|
| 1484 | |
|---|
| 1485 | osg::ref_ptr<ReaderWriter::Options> options = new ReaderWriter::Options; |
|---|
| 1486 | options->setDatabasePath(archiveName); |
|---|
| 1487 | |
|---|
| 1488 | return archive->readObject(fileName,options.get()); |
|---|
| 1489 | } |
|---|
| 1490 | } |
|---|
| 1491 | |
|---|
| 1492 | |
|---|
| 1493 | typedef std::vector<ReaderWriter::ReadResult> Results; |
|---|
| 1494 | Results results; |
|---|
| 1495 | |
|---|
| 1496 | |
|---|
| 1497 | AvailableReaderWriterIterator itr(_rwList, _pluginMutex); |
|---|
| 1498 | for(;itr.valid();++itr) |
|---|
| 1499 | { |
|---|
| 1500 | ReaderWriter::ReadResult rr = readFunctor.doRead(*itr); |
|---|
| 1501 | if (readFunctor.isValid(rr)) return rr; |
|---|
| 1502 | else results.push_back(rr); |
|---|
| 1503 | } |
|---|
| 1504 | |
|---|
| 1505 | if (!results.empty()) |
|---|
| 1506 | { |
|---|
| 1507 | unsigned int num_FILE_NOT_HANDLED = 0; |
|---|
| 1508 | unsigned int num_FILE_NOT_FOUND = 0; |
|---|
| 1509 | unsigned int num_ERROR_IN_READING_FILE = 0; |
|---|
| 1510 | |
|---|
| 1511 | Results::iterator ritr; |
|---|
| 1512 | for(ritr=results.begin(); |
|---|
| 1513 | ritr!=results.end(); |
|---|
| 1514 | ++ritr) |
|---|
| 1515 | { |
|---|
| 1516 | if (ritr->status()==ReaderWriter::ReadResult::FILE_NOT_HANDLED) ++num_FILE_NOT_HANDLED; |
|---|
| 1517 | else if (ritr->status()==ReaderWriter::ReadResult::FILE_NOT_FOUND) ++num_FILE_NOT_FOUND; |
|---|
| 1518 | else if (ritr->status()==ReaderWriter::ReadResult::ERROR_IN_READING_FILE) ++num_ERROR_IN_READING_FILE; |
|---|
| 1519 | } |
|---|
| 1520 | |
|---|
| 1521 | if (num_FILE_NOT_HANDLED!=results.size()) |
|---|
| 1522 | { |
|---|
| 1523 | for(ritr=results.begin(); ritr!=results.end(); ++ritr) |
|---|
| 1524 | { |
|---|
| 1525 | if (ritr->status()==ReaderWriter::ReadResult::ERROR_IN_READING_FILE) |
|---|
| 1526 | { |
|---|
| 1527 | |
|---|
| 1528 | return *ritr; |
|---|
| 1529 | } |
|---|
| 1530 | } |
|---|
| 1531 | |
|---|
| 1532 | |
|---|
| 1533 | if (!osgDB::containsServerAddress(readFunctor._filename)) |
|---|
| 1534 | { |
|---|
| 1535 | for(ritr=results.begin(); ritr!=results.end(); ++ritr) |
|---|
| 1536 | { |
|---|
| 1537 | if (ritr->status()==ReaderWriter::ReadResult::FILE_NOT_FOUND) |
|---|
| 1538 | { |
|---|
| 1539 | |
|---|
| 1540 | return *ritr; |
|---|
| 1541 | } |
|---|
| 1542 | } |
|---|
| 1543 | } |
|---|
| 1544 | } |
|---|
| 1545 | } |
|---|
| 1546 | |
|---|
| 1547 | results.clear(); |
|---|
| 1548 | |
|---|
| 1549 | |
|---|
| 1550 | std::string libraryName = createLibraryNameForFile(readFunctor._filename); |
|---|
| 1551 | if (loadLibrary(libraryName)!=NOT_LOADED) |
|---|
| 1552 | { |
|---|
| 1553 | for(;itr.valid();++itr) |
|---|
| 1554 | { |
|---|
| 1555 | ReaderWriter::ReadResult rr = readFunctor.doRead(*itr); |
|---|
| 1556 | if (readFunctor.isValid(rr)) return rr; |
|---|
| 1557 | else results.push_back(rr); |
|---|
| 1558 | } |
|---|
| 1559 | } |
|---|
| 1560 | |
|---|
| 1561 | |
|---|
| 1562 | |
|---|
| 1563 | if (containsServerAddress(readFunctor._filename)) |
|---|
| 1564 | { |
|---|
| 1565 | ReaderWriter* rw = getReaderWriterForExtension("curl"); |
|---|
| 1566 | if (rw) |
|---|
| 1567 | { |
|---|
| 1568 | return readFunctor.doRead(*rw); |
|---|
| 1569 | } |
|---|
| 1570 | else |
|---|
| 1571 | { |
|---|
| 1572 | return ReaderWriter::ReadResult("Warning: Could not find the .curl plugin to read from server."); |
|---|
| 1573 | } |
|---|
| 1574 | } |
|---|
| 1575 | |
|---|
| 1576 | if (!results.empty()) |
|---|
| 1577 | { |
|---|
| 1578 | unsigned int num_FILE_NOT_HANDLED = 0; |
|---|
| 1579 | unsigned int num_FILE_NOT_FOUND = 0; |
|---|
| 1580 | unsigned int num_ERROR_IN_READING_FILE = 0; |
|---|
| 1581 | |
|---|
| 1582 | Results::iterator ritr; |
|---|
| 1583 | for(ritr=results.begin(); |
|---|
| 1584 | ritr!=results.end(); |
|---|
| 1585 | ++ritr) |
|---|
| 1586 | { |
|---|
| 1587 | if (ritr->status()==ReaderWriter::ReadResult::FILE_NOT_HANDLED) ++num_FILE_NOT_HANDLED; |
|---|
| 1588 | else if (ritr->status()==ReaderWriter::ReadResult::FILE_NOT_FOUND) ++num_FILE_NOT_FOUND; |
|---|
| 1589 | else if (ritr->status()==ReaderWriter::ReadResult::ERROR_IN_READING_FILE) ++num_ERROR_IN_READING_FILE; |
|---|
| 1590 | } |
|---|
| 1591 | |
|---|
| 1592 | if (num_FILE_NOT_HANDLED!=results.size()) |
|---|
| 1593 | { |
|---|
| 1594 | for(ritr=results.begin(); ritr!=results.end(); ++ritr) |
|---|
| 1595 | { |
|---|
| 1596 | if (ritr->status()==ReaderWriter::ReadResult::ERROR_IN_READING_FILE) |
|---|
| 1597 | { |
|---|
| 1598 | |
|---|
| 1599 | return *ritr; |
|---|
| 1600 | } |
|---|
| 1601 | } |
|---|
| 1602 | |
|---|
| 1603 | for(ritr=results.begin(); ritr!=results.end(); ++ritr) |
|---|
| 1604 | { |
|---|
| 1605 | if (ritr->status()==ReaderWriter::ReadResult::FILE_NOT_FOUND) |
|---|
| 1606 | { |
|---|
| 1607 | |
|---|
| 1608 | return *ritr; |
|---|
| 1609 | } |
|---|
| 1610 | } |
|---|
| 1611 | } |
|---|
| 1612 | } |
|---|
| 1613 | else |
|---|
| 1614 | { |
|---|
| 1615 | return ReaderWriter::ReadResult("Warning: Could not find plugin to read objects from file \""+readFunctor._filename+"\"."); |
|---|
| 1616 | } |
|---|
| 1617 | |
|---|
| 1618 | return results.front(); |
|---|
| 1619 | } |
|---|
| 1620 | |
|---|
| 1621 | ReaderWriter::ReadResult Registry::readImplementation(const ReadFunctor& readFunctor, bool useObjectCache) |
|---|
| 1622 | { |
|---|
| 1623 | std::string file(readFunctor._filename); |
|---|
| 1624 | |
|---|
| 1625 | if (useObjectCache) |
|---|
| 1626 | { |
|---|
| 1627 | |
|---|
| 1628 | { |
|---|
| 1629 | OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_objectCacheMutex); |
|---|
| 1630 | ObjectCache::iterator oitr=_objectCache.find(file); |
|---|
| 1631 | if (oitr!=_objectCache.end()) |
|---|
| 1632 | { |
|---|
| 1633 | notify(INFO)<<"returning cached instanced of "<<file<<std::endl; |
|---|
| 1634 | if (readFunctor.isValid(oitr->second.first.get())) return ReaderWriter::ReadResult(oitr->second.first.get(), ReaderWriter::ReadResult::FILE_LOADED_FROM_CACHE); |
|---|
| 1635 | else return ReaderWriter::ReadResult("Error file does not contain an osg::Object"); |
|---|
| 1636 | } |
|---|
| 1637 | } |
|---|
| 1638 | |
|---|
| 1639 | ReaderWriter::ReadResult rr = read(readFunctor); |
|---|
| 1640 | if (rr.validObject()) |
|---|
| 1641 | { |
|---|
| 1642 | |
|---|
| 1643 | notify(INFO)<<"Adding to object cache "<<file<<std::endl; |
|---|
| 1644 | addEntryToObjectCache(file,rr.getObject()); |
|---|
| 1645 | } |
|---|
| 1646 | else |
|---|
| 1647 | { |
|---|
| 1648 | notify(INFO)<<"No valid object found for "<<file<<std::endl; |
|---|
| 1649 | } |
|---|
| 1650 | |
|---|
| 1651 | return rr; |
|---|
| 1652 | |
|---|
| 1653 | } |
|---|
| 1654 | else |
|---|
| 1655 | { |
|---|
| 1656 | ObjectCache tmpObjectCache; |
|---|
| 1657 | |
|---|
| 1658 | { |
|---|
| 1659 | OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_objectCacheMutex); |
|---|
| 1660 | tmpObjectCache.swap(_objectCache); |
|---|
| 1661 | } |
|---|
| 1662 | |
|---|
| 1663 | ReaderWriter::ReadResult rr = read(readFunctor); |
|---|
| 1664 | |
|---|
| 1665 | { |
|---|
| 1666 | OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_objectCacheMutex); |
|---|
| 1667 | tmpObjectCache.swap(_objectCache); |
|---|
| 1668 | } |
|---|
| 1669 | |
|---|
| 1670 | return rr; |
|---|
| 1671 | } |
|---|
| 1672 | } |
|---|
| 1673 | |
|---|
| 1674 | |
|---|
| 1675 | ReaderWriter::ReadResult Registry::openArchiveImplementation(const std::string& fileName, ReaderWriter::ArchiveStatus status, unsigned int indexBlockSizeHint, const ReaderWriter::Options* options) |
|---|
| 1676 | { |
|---|
| 1677 | osgDB::Archive* archive = getFromArchiveCache(fileName); |
|---|
| 1678 | if (archive) return archive; |
|---|
| 1679 | |
|---|
| 1680 | ReaderWriter::ReadResult result = readImplementation(ReadArchiveFunctor(fileName, status, indexBlockSizeHint, options),false); |
|---|
| 1681 | |
|---|
| 1682 | |
|---|
| 1683 | |
|---|
| 1684 | if (result.validArchive() && |
|---|
| 1685 | (!options || (options->getObjectCacheHint() & ReaderWriter::Options::CACHE_ARCHIVES)) ) |
|---|
| 1686 | { |
|---|
| 1687 | addToArchiveCache(fileName,result.getArchive()); |
|---|
| 1688 | } |
|---|
| 1689 | return result; |
|---|
| 1690 | } |
|---|
| 1691 | |
|---|
| 1692 | |
|---|
| 1693 | ReaderWriter::ReadResult Registry::readObjectImplementation(const std::string& fileName,const ReaderWriter::Options* options) |
|---|
| 1694 | { |
|---|
| 1695 | return readImplementation(ReadObjectFunctor(fileName, options), |
|---|
| 1696 | options ? (options->getObjectCacheHint()&ReaderWriter::Options::CACHE_OBJECTS)!=0: false); |
|---|
| 1697 | } |
|---|
| 1698 | |
|---|
| 1699 | ReaderWriter::WriteResult Registry::writeObjectImplementation(const Object& obj,const std::string& fileName,const ReaderWriter::Options* options) |
|---|
| 1700 | { |
|---|
| 1701 | |
|---|
| 1702 | typedef std::vector<ReaderWriter::WriteResult> Results; |
|---|
| 1703 | Results results; |
|---|
| 1704 | |
|---|
| 1705 | |
|---|
| 1706 | AvailableReaderWriterIterator itr(_rwList, _pluginMutex); |
|---|
| 1707 | for(;itr.valid();++itr) |
|---|
| 1708 | { |
|---|
| 1709 | ReaderWriter::WriteResult rr = itr->writeObject(obj,fileName,options); |
|---|
| 1710 | if (rr.success()) return rr; |
|---|
| 1711 | else results.push_back(rr); |
|---|
| 1712 | } |
|---|
| 1713 | |
|---|
| 1714 | |
|---|
| 1715 | std::string libraryName = createLibraryNameForFile(fileName); |
|---|
| 1716 | if (loadLibrary(libraryName)==LOADED) |
|---|
| 1717 | { |
|---|
| 1718 | for(;itr.valid();++itr) |
|---|
| 1719 | { |
|---|
| 1720 | ReaderWriter::WriteResult rr = itr->writeObject(obj,fileName,options); |
|---|
| 1721 | if (rr.success()) return rr; |
|---|
| 1722 | else results.push_back(rr); |
|---|
| 1723 | } |
|---|
| 1724 | } |
|---|
| 1725 | |
|---|
| 1726 | if (results.empty()) |
|---|
| 1727 | { |
|---|
| 1728 | return ReaderWriter::WriteResult("Warning: Could not find plugin to write objects to file \""+fileName+"\"."); |
|---|
| 1729 | } |
|---|
| 1730 | |
|---|
| 1731 | if (results.front().message().empty()) |
|---|
| 1732 | { |
|---|
| 1733 | switch(results.front().status()) |
|---|
| 1734 | { |
|---|
| 1735 | case(ReaderWriter::WriteResult::FILE_NOT_HANDLED): results.front().message() = "Warning: Write to \""+fileName+"\" not supported."; break; |
|---|
| 1736 | case(ReaderWriter::WriteResult::ERROR_IN_WRITING_FILE): results.front().message() = "Warning: Error in writing to \""+fileName+"\"."; break; |
|---|
| 1737 | default: break; |
|---|
| 1738 | } |
|---|
| 1739 | } |
|---|
| 1740 | |
|---|
| 1741 | return results.front(); |
|---|
| 1742 | } |
|---|
| 1743 | |
|---|
| 1744 | |
|---|
| 1745 | |
|---|
| 1746 | ReaderWriter::ReadResult Registry::readImageImplementation(const std::string& fileName,const ReaderWriter::Options* options) |
|---|
| 1747 | { |
|---|
| 1748 | return readImplementation(ReadImageFunctor(fileName, options), |
|---|
| 1749 | options ? (options->getObjectCacheHint()&ReaderWriter::Options::CACHE_IMAGES)!=0: false); |
|---|
| 1750 | } |
|---|
| 1751 | |
|---|
| 1752 | ReaderWriter::WriteResult Registry::writeImageImplementation(const Image& image,const std::string& fileName,const ReaderWriter::Options* options) |
|---|
| 1753 | { |
|---|
| 1754 | |
|---|
| 1755 | typedef std::vector<ReaderWriter::WriteResult> Results; |
|---|
| 1756 | Results results; |
|---|
| 1757 | |
|---|
| 1758 | |
|---|
| 1759 | AvailableReaderWriterIterator itr(_rwList, _pluginMutex); |
|---|
| 1760 | for(;itr.valid();++itr) |
|---|
| 1761 | { |
|---|
| 1762 | ReaderWriter::WriteResult rr = itr->writeImage(image,fileName,options); |
|---|
| 1763 | if (rr.success()) return rr; |
|---|
| 1764 | else results.push_back(rr); |
|---|
| 1765 | } |
|---|
| 1766 | |
|---|
| 1767 | results.clear(); |
|---|
| 1768 | |
|---|
| 1769 | |
|---|
| 1770 | std::string libraryName = createLibraryNameForFile(fileName); |
|---|
| 1771 | if (loadLibrary(libraryName)==LOADED) |
|---|
| 1772 | { |
|---|
| 1773 | for(;itr.valid();++itr) |
|---|
| 1774 | { |
|---|
| 1775 | ReaderWriter::WriteResult rr = itr->writeImage(image,fileName,options); |
|---|
| 1776 | if (rr.success()) return rr; |
|---|
| 1777 | else results.push_back(rr); |
|---|
| 1778 | } |
|---|
| 1779 | } |
|---|
| 1780 | |
|---|
| 1781 | if (results.empty()) |
|---|
| 1782 | { |
|---|
| 1783 | return ReaderWriter::WriteResult("Warning: Could not find plugin to write image to file \""+fileName+"\"."); |
|---|
| 1784 | } |
|---|
| 1785 | |
|---|
| 1786 | if (results.front().message().empty()) |
|---|
| 1787 | { |
|---|
| 1788 | switch(results.front().status()) |
|---|
| 1789 | { |
|---|
| 1790 | case(ReaderWriter::WriteResult::FILE_NOT_HANDLED): results.front().message() = "Warning: Write to \""+fileName+"\" not supported."; break; |
|---|
| 1791 | case(ReaderWriter::WriteResult::ERROR_IN_WRITING_FILE): results.front().message() = "Warning: Error in writing to \""+fileName+"\"."; break; |
|---|
| 1792 | default: break; |
|---|
| 1793 | } |
|---|
| 1794 | } |
|---|
| 1795 | |
|---|
| 1796 | return results.front(); |
|---|
| 1797 | } |
|---|
| 1798 | |
|---|
| 1799 | |
|---|
| 1800 | ReaderWriter::ReadResult Registry::readHeightFieldImplementation(const std::string& fileName,const ReaderWriter::Options* options) |
|---|
| 1801 | { |
|---|
| 1802 | return readImplementation(ReadHeightFieldFunctor(fileName, options), |
|---|
| 1803 | options ? (options->getObjectCacheHint()&ReaderWriter::Options::CACHE_HEIGHTFIELDS)!=0: false); |
|---|
| 1804 | } |
|---|
| 1805 | |
|---|
| 1806 | ReaderWriter::WriteResult Registry::writeHeightFieldImplementation(const HeightField& HeightField,const std::string& fileName,const ReaderWriter::Options* options) |
|---|
| 1807 | { |
|---|
| 1808 | |
|---|
| 1809 | typedef std::vector<ReaderWriter::WriteResult> Results; |
|---|
| 1810 | Results results; |
|---|
| 1811 | |
|---|
| 1812 | |
|---|
| 1813 | AvailableReaderWriterIterator itr(_rwList, _pluginMutex); |
|---|
| 1814 | for(;itr.valid();++itr) |
|---|
| 1815 | { |
|---|
| 1816 | ReaderWriter::WriteResult rr = itr->writeHeightField(HeightField,fileName,options); |
|---|
| 1817 | if (rr.success()) return rr; |
|---|
| 1818 | else results.push_back(rr); |
|---|
| 1819 | } |
|---|
| 1820 | |
|---|
| 1821 | results.clear(); |
|---|
| 1822 | |
|---|
| 1823 | |
|---|
| 1824 | std::string libraryName = createLibraryNameForFile(fileName); |
|---|
| 1825 | if (loadLibrary(libraryName)==LOADED) |
|---|
| 1826 | { |
|---|
| 1827 | for(;itr.valid();++itr) |
|---|
| 1828 | { |
|---|
| 1829 | ReaderWriter::WriteResult rr = itr->writeHeightField(HeightField,fileName,options); |
|---|
| 1830 | if (rr.success()) return rr; |
|---|
| 1831 | else results.push_back(rr); |
|---|
| 1832 | } |
|---|
| 1833 | } |
|---|
| 1834 | |
|---|
| 1835 | if (results.empty()) |
|---|
| 1836 | { |
|---|
| 1837 | return ReaderWriter::WriteResult("Warning: Could not find plugin to write HeightField to file \""+fileName+"\"."); |
|---|
| 1838 | } |
|---|
| 1839 | |
|---|
| 1840 | if (results.front().message().empty()) |
|---|
| 1841 | { |
|---|
| 1842 | switch(results.front().status()) |
|---|
| 1843 | { |
|---|
| 1844 | case(ReaderWriter::WriteResult::FILE_NOT_HANDLED): results.front().message() = "Warning: Write to \""+fileName+"\" not supported."; break; |
|---|
| 1845 | case(ReaderWriter::WriteResult::ERROR_IN_WRITING_FILE): results.front().message() = "Warning: Error in writing to \""+fileName+"\"."; break; |
|---|
| 1846 | default: break; |
|---|
| 1847 | } |
|---|
| 1848 | } |
|---|
| 1849 | |
|---|
| 1850 | return results.front(); |
|---|
| 1851 | } |
|---|
| 1852 | |
|---|
| 1853 | |
|---|
| 1854 | ReaderWriter::ReadResult Registry::readNodeImplementation(const std::string& fileName,const ReaderWriter::Options* options) |
|---|
| 1855 | { |
|---|
| 1856 | #if 0 |
|---|
| 1857 | |
|---|
| 1858 | osg::Timer_t startTick = osg::Timer::instance()->tick(); |
|---|
| 1859 | ReaderWriter::ReadResult result = readImplementation(ReadNodeFunctor(fileName, options), |
|---|
| 1860 | options ? (options->getObjectCacheHint()&ReaderWriter::Options::CACHE_NODES)!=0: false); |
|---|
| 1861 | osg::Timer_t endTick = osg::Timer::instance()->tick(); |
|---|
| 1862 | osg::notify(osg::NOTICE)<<"time to load "<<fileName<<" "<<osg::Timer::instance()->delta_m(startTick, endTick)<<"ms"<<std::endl; |
|---|
| 1863 | return result; |
|---|
| 1864 | |
|---|
| 1865 | #else |
|---|
| 1866 | |
|---|
| 1867 | return readImplementation(ReadNodeFunctor(fileName, options), |
|---|
| 1868 | options ? (options->getObjectCacheHint()&ReaderWriter::Options::CACHE_NODES)!=0: false); |
|---|
| 1869 | |
|---|
| 1870 | #endif |
|---|
| 1871 | } |
|---|
| 1872 | |
|---|
| 1873 | ReaderWriter::WriteResult Registry::writeNodeImplementation(const Node& node,const std::string& fileName,const ReaderWriter::Options* options) |
|---|
| 1874 | { |
|---|
| 1875 | |
|---|
| 1876 | typedef std::vector<ReaderWriter::WriteResult> Results; |
|---|
| 1877 | Results results; |
|---|
| 1878 | |
|---|
| 1879 | |
|---|
| 1880 | AvailableReaderWriterIterator itr(_rwList, _pluginMutex); |
|---|
| 1881 | for(;itr.valid();++itr) |
|---|
| 1882 | { |
|---|
| 1883 | ReaderWriter::WriteResult rr = itr->writeNode(node,fileName,options); |
|---|
| 1884 | if (rr.success()) return rr; |
|---|
| 1885 | else results.push_back(rr); |
|---|
| 1886 | } |
|---|
| 1887 | |
|---|
| 1888 | results.clear(); |
|---|
| 1889 | |
|---|
| 1890 | |
|---|
| 1891 | std::string libraryName = createLibraryNameForFile(fileName); |
|---|
| 1892 | |
|---|
| 1893 | |
|---|
| 1894 | if (loadLibrary(libraryName)==LOADED) |
|---|
| 1895 | { |
|---|
| 1896 | for(;itr.valid();++itr) |
|---|
| 1897 | { |
|---|
| 1898 | ReaderWriter::WriteResult rr = itr->writeNode(node,fileName,options); |
|---|
| 1899 | |
|---|
| 1900 | if (rr.success()) return rr; |
|---|
| 1901 | else results.push_back(rr); |
|---|
| 1902 | } |
|---|
| 1903 | } |
|---|
| 1904 | |
|---|
| 1905 | if (results.empty()) |
|---|
| 1906 | { |
|---|
| 1907 | return ReaderWriter::WriteResult("Warning: Could not find plugin to write nodes to file \""+fileName+"\"."); |
|---|
| 1908 | } |
|---|
| 1909 | |
|---|
| 1910 | if (results.front().message().empty()) |
|---|
| 1911 | { |
|---|
| 1912 | switch(results.front().status()) |
|---|
| 1913 | { |
|---|
| 1914 | case(ReaderWriter::WriteResult::FILE_NOT_HANDLED): results.front().message() = "Warning: Write to \""+fileName+"\" not supported."; break; |
|---|
| 1915 | case(ReaderWriter::WriteResult::ERROR_IN_WRITING_FILE): results.front().message() = "Warning: Error in writing to \""+fileName+"\"."; break; |
|---|
| 1916 | default: break; |
|---|
| 1917 | } |
|---|
| 1918 | } |
|---|
| 1919 | |
|---|
| 1920 | return results.front(); |
|---|
| 1921 | } |
|---|
| 1922 | |
|---|
| 1923 | ReaderWriter::ReadResult Registry::readShaderImplementation(const std::string& fileName,const ReaderWriter::Options* options) |
|---|
| 1924 | { |
|---|
| 1925 | return readImplementation(ReadShaderFunctor(fileName, options), |
|---|
| 1926 | options ? (options->getObjectCacheHint()&ReaderWriter::Options::CACHE_SHADERS)!=0: false); |
|---|
| 1927 | } |
|---|
| 1928 | |
|---|
| 1929 | ReaderWriter::WriteResult Registry::writeShaderImplementation(const Shader& shader,const std::string& fileName,const ReaderWriter::Options* options) |
|---|
| 1930 | { |
|---|
| 1931 | |
|---|
| 1932 | typedef std::vector<ReaderWriter::WriteResult> Results; |
|---|
| 1933 | Results results; |
|---|
| 1934 | |
|---|
| 1935 | |
|---|
| 1936 | AvailableReaderWriterIterator itr(_rwList, _pluginMutex); |
|---|
| 1937 | for(;itr.valid();++itr) |
|---|
| 1938 | { |
|---|
| 1939 | ReaderWriter::WriteResult rr = itr->writeShader(shader,fileName,options); |
|---|
| 1940 | if (rr.success()) return rr; |
|---|
| 1941 | else results.push_back(rr); |
|---|
| 1942 | } |
|---|
| 1943 | |
|---|
| 1944 | results.clear(); |
|---|
| 1945 | |
|---|
| 1946 | |
|---|
| 1947 | std::string libraryName = createLibraryNameForFile(fileName); |
|---|
| 1948 | if (loadLibrary(libraryName)==LOADED) |
|---|
| 1949 | { |
|---|
| 1950 | for(;itr.valid();++itr) |
|---|
| 1951 | { |
|---|
| 1952 | ReaderWriter::WriteResult rr = itr->writeShader(shader,fileName,options); |
|---|
| 1953 | if (rr.success()) return rr; |
|---|
| 1954 | else results.push_back(rr); |
|---|
| 1955 | } |
|---|
| 1956 | } |
|---|
| 1957 | |
|---|
| 1958 | if (results.empty()) |
|---|
| 1959 | { |
|---|
| 1960 | return ReaderWriter::WriteResult("Warning: Could not find plugin to write shader to file \""+fileName+"\"."); |
|---|
| 1961 | } |
|---|
| 1962 | |
|---|
| 1963 | if (results.front().message().empty()) |
|---|
| 1964 | { |
|---|
| 1965 | switch(results.front().status()) |
|---|
| 1966 | { |
|---|
| 1967 | case(ReaderWriter::WriteResult::FILE_NOT_HANDLED): results.front().message() = "Warning: Write to \""+fileName+"\" not supported."; break; |
|---|
| 1968 | case(ReaderWriter::WriteResult::ERROR_IN_WRITING_FILE): results.front().message() = "Warning: Error in writing to \""+fileName+"\"."; break; |
|---|
| 1969 | default: break; |
|---|
| 1970 | } |
|---|
| 1971 | } |
|---|
| 1972 | |
|---|
| 1973 | return results.front(); |
|---|
| 1974 | } |
|---|
| 1975 | |
|---|
| 1976 | void Registry::addEntryToObjectCache(const std::string& filename, osg::Object* object, double timestamp) |
|---|
| 1977 | { |
|---|
| 1978 | OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_objectCacheMutex); |
|---|
| 1979 | _objectCache[filename]=ObjectTimeStampPair(object,timestamp); |
|---|
| 1980 | } |
|---|
| 1981 | osg::Object* Registry::getFromObjectCache(const std::string& fileName) |
|---|
| 1982 | { |
|---|
| 1983 | OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_objectCacheMutex); |
|---|
| 1984 | ObjectCache::iterator itr = _objectCache.find(fileName); |
|---|
| 1985 | if (itr!=_objectCache.end()) return itr->second.first.get(); |
|---|
| 1986 | else return 0; |
|---|
| 1987 | } |
|---|
| 1988 | |
|---|
| 1989 | void Registry::updateTimeStampOfObjectsInCacheWithExternalReferences(double currentTime) |
|---|
| 1990 | { |
|---|
| 1991 | OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_objectCacheMutex); |
|---|
| 1992 | |
|---|
| 1993 | |
|---|
| 1994 | for(ObjectCache::iterator itr=_objectCache.begin(); |
|---|
| 1995 | itr!=_objectCache.end(); |
|---|
| 1996 | ++itr) |
|---|
| 1997 | { |
|---|
| 1998 | |
|---|
| 1999 | if (itr->second.first->referenceCount()>1) |
|---|
| 2000 | { |
|---|
| 2001 | |
|---|
| 2002 | itr->second.second = currentTime; |
|---|
| 2003 | } |
|---|
| 2004 | } |
|---|
| 2005 | } |
|---|
| 2006 | |
|---|
| 2007 | void Registry::removeExpiredObjectsInCache(double expiryTime) |
|---|
| 2008 | { |
|---|
| 2009 | OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_objectCacheMutex); |
|---|
| 2010 | |
|---|
| 2011 | typedef std::vector<std::string> ObjectsToRemove; |
|---|
| 2012 | ObjectsToRemove objectsToRemove; |
|---|
| 2013 | |
|---|
| 2014 | |
|---|
| 2015 | for(ObjectCache::iterator oitr=_objectCache.begin(); |
|---|
| 2016 | oitr!=_objectCache.end(); |
|---|
| 2017 | ++oitr) |
|---|
| 2018 | { |
|---|
| 2019 | if (oitr->second.second<=expiryTime) |
|---|
| 2020 | { |
|---|
| 2021 | |
|---|
| 2022 | |
|---|
| 2023 | objectsToRemove.push_back(oitr->first); |
|---|
| 2024 | } |
|---|
| 2025 | } |
|---|
| 2026 | |
|---|
| 2027 | |
|---|
| 2028 | for(ObjectsToRemove::iterator ritr=objectsToRemove.begin(); |
|---|
| 2029 | ritr!=objectsToRemove.end(); |
|---|
| 2030 | ++ritr) |
|---|
| 2031 | { |
|---|
| 2032 | |
|---|
| 2033 | _objectCache.erase(*ritr); |
|---|
| 2034 | } |
|---|
| 2035 | |
|---|
| 2036 | } |
|---|
| 2037 | |
|---|
| 2038 | void Registry::clearObjectCache() |
|---|
| 2039 | { |
|---|
| 2040 | OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_objectCacheMutex); |
|---|
| 2041 | _objectCache.clear(); |
|---|
| 2042 | } |
|---|
| 2043 | |
|---|
| 2044 | void Registry::addToArchiveCache(const std::string& fileName, osgDB::Archive* archive) |
|---|
| 2045 | { |
|---|
| 2046 | OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_archiveCacheMutex); |
|---|
| 2047 | _archiveCache[fileName] = archive; |
|---|
| 2048 | } |
|---|
| 2049 | |
|---|
| 2050 | |
|---|
| 2051 | void Registry::removeFromArchiveCache(const std::string& fileName) |
|---|
| 2052 | { |
|---|
| 2053 | OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_archiveCacheMutex); |
|---|
| 2054 | ArchiveCache::iterator itr = _archiveCache.find(fileName); |
|---|
| 2055 | if (itr!=_archiveCache.end()) |
|---|
| 2056 | { |
|---|
| 2057 | _archiveCache.erase(itr); |
|---|
| 2058 | } |
|---|
| 2059 | } |
|---|
| 2060 | |
|---|
| 2061 | osgDB::Archive* Registry::getFromArchiveCache(const std::string& fileName) |
|---|
| 2062 | { |
|---|
| 2063 | OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_archiveCacheMutex); |
|---|
| 2064 | ArchiveCache::iterator itr = _archiveCache.find(fileName); |
|---|
| 2065 | if (itr!=_archiveCache.end()) return itr->second.get(); |
|---|
| 2066 | else return 0; |
|---|
| 2067 | } |
|---|
| 2068 | |
|---|
| 2069 | void Registry::clearArchiveCache() |
|---|
| 2070 | { |
|---|
| 2071 | OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_archiveCacheMutex); |
|---|
| 2072 | _archiveCache.clear(); |
|---|
| 2073 | } |
|---|
| 2074 | |
|---|
| 2075 | void Registry::releaseGLObjects(osg::State* state) |
|---|
| 2076 | { |
|---|
| 2077 | OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_objectCacheMutex); |
|---|
| 2078 | |
|---|
| 2079 | for(ObjectCache::iterator itr = _objectCache.begin(); |
|---|
| 2080 | itr != _objectCache.end(); |
|---|
| 2081 | ++itr) |
|---|
| 2082 | { |
|---|
| 2083 | osg::Object* object = itr->second.first.get(); |
|---|
| 2084 | object->releaseGLObjects(state); |
|---|
| 2085 | } |
|---|
| 2086 | } |
|---|
| 2087 | |
|---|
| 2088 | SharedStateManager* Registry::getOrCreateSharedStateManager() |
|---|
| 2089 | { |
|---|
| 2090 | if (!_sharedStateManager) _sharedStateManager = new SharedStateManager; |
|---|
| 2091 | |
|---|
| 2092 | return _sharedStateManager.get(); |
|---|
| 2093 | } |
|---|