Browse Source

loader: Remember type of loader object used to load models

Fixes #1657
rdb 4 months ago
parent
commit
052bd77030

+ 35 - 9
panda/src/pgraph/loader.cxx

@@ -305,21 +305,44 @@ try_load_file(const Filename &pathname, const LoaderOptions &options,
       (options.get_flags() & LoaderOptions::LF_no_disk_cache) == 0) {
     // See if the model can be found in the on-disk cache, if it is active.
     record = cache->lookup(pathname, "bam");
-    if (record != nullptr) {
-      if (record->has_data()) {
+    if (record != nullptr && record->has_data()) {
+      PT(PandaNode) result = DCAST(PandaNode, record->get_data());
+
+      ModelRoot *model_root = nullptr;
+      if (result != nullptr && result->is_of_type(ModelRoot::get_class_type())) {
+        model_root = DCAST(ModelRoot, result.p());
+
+        if (requested_type != nullptr &&
+            model_root->get_loader_type() != requested_type->get_type()) {
+          result.clear();
+          if (report_errors) {
+            loader_cat.info()
+              << "Model " << pathname << " found in disk cache, but using "
+              << "unexpected loader " << model_root->get_loader_type()
+              << " (expected " << requested_type->get_type() << ").\n";
+          }
+        }
+      }
+
+      if (result != nullptr) {
         if (report_errors) {
-          loader_cat.info()
-            << "Model " << pathname << " found in disk cache.\n";
+          TypeHandle loaded_type = model_root->get_loader_type();
+          if (loaded_type != TypeHandle::none()) {
+            loader_cat.info()
+              << "Model " << pathname << " found in disk cache (loaded using "
+              << loaded_type << ").\n";
+          } else {
+            loader_cat.info()
+              << "Model " << pathname << " found in disk cache.\n";
+          }
         }
-        PT(PandaNode) result = DCAST(PandaNode, record->get_data());
 
         if (premunge_data) {
           SceneGraphReducer sgr;
           sgr.premunge(result, RenderState::make_empty());
         }
 
-        if (result->is_of_type(ModelRoot::get_class_type())) {
-          ModelRoot *model_root = DCAST(ModelRoot, result.p());
+        if (model_root != nullptr) {
           model_root->set_fullpath(pathname);
           model_root->set_timestamp(record->get_source_timestamp());
 
@@ -336,8 +359,7 @@ try_load_file(const Filename &pathname, const LoaderOptions &options,
         return result;
       }
     }
-
-    if (loader_cat.is_debug()) {
+    else if (loader_cat.is_debug()) {
       loader_cat.debug()
         << "Model " << pathname << " not found in cache.\n";
     }
@@ -354,6 +376,10 @@ try_load_file(const Filename &pathname, const LoaderOptions &options,
     result = requested_type->load_file(pathname, options, record);
   }
   if (result != nullptr) {
+    if (result->is_of_type(ModelRoot::get_class_type())) {
+      ((ModelRoot *)result.p())->set_loader_type(requested_type->get_type());
+    }
+
     if (record != nullptr) {
       // Store the loaded model in the model cache.
       record->set_data(result);

+ 23 - 3
panda/src/pgraph/modelRoot.I

@@ -19,7 +19,8 @@ ModelRoot(const std::string &name) :
   ModelNode(name),
   _fullpath(name),
   _timestamp(0),
-  _reference(new ModelRoot::ModelReference)
+  _reference(new ModelRoot::ModelReference),
+  _loader_type(TypeHandle::none())
 {
 }
 
@@ -31,7 +32,8 @@ ModelRoot(const Filename &fullpath, time_t timestamp) :
   ModelNode(fullpath.get_basename()),
   _fullpath(fullpath),
   _timestamp(timestamp),
-  _reference(new ModelRoot::ModelReference)
+  _reference(new ModelRoot::ModelReference),
+  _loader_type(TypeHandle::none())
 {
 }
 
@@ -114,6 +116,23 @@ set_reference(ModelRoot::ModelReference *ref) {
   _reference = ref;
 }
 
+/**
+ * Returns the type of the loader object that was used to load this model.
+ */
+INLINE TypeHandle ModelRoot::
+get_loader_type() const {
+  return _loader_type;
+}
+
+/**
+ * Sets the type of the loader object used to load this model.  Normally this
+ * is only called by Loader.
+ */
+INLINE void ModelRoot::
+set_loader_type(TypeHandle type) {
+  _loader_type = type;
+}
+
 /**
  *
  */
@@ -122,7 +141,8 @@ ModelRoot(const ModelRoot &copy) :
   ModelNode(copy),
   _fullpath(copy._fullpath),
   _timestamp(copy._timestamp),
-  _reference(copy._reference)
+  _reference(copy._reference),
+  _loader_type(copy._loader_type)
 {
 }
 

+ 10 - 0
panda/src/pgraph/modelRoot.cxx

@@ -41,6 +41,10 @@ register_with_read_factory() {
 void ModelRoot::
 write_datagram(BamWriter *manager, Datagram &dg) {
   ModelNode::write_datagram(manager, dg);
+
+  if (manager->get_file_minor_ver() >= 46) {
+    manager->write_handle(dg, _loader_type);
+  }
 }
 
 /**
@@ -67,4 +71,10 @@ make_from_bam(const FactoryParams &params) {
 void ModelRoot::
 fillin(DatagramIterator &scan, BamReader *manager) {
   ModelNode::fillin(scan, manager);
+
+  if (manager->get_file_minor_ver() >= 46) {
+    _loader_type = manager->read_handle(scan);
+  } else {
+    _loader_type = TypeHandle::none();
+  }
 }

+ 8 - 0
panda/src/pgraph/modelRoot.h

@@ -50,6 +50,13 @@ PUBLISHED:
   void set_reference(ModelReference *ref);
   MAKE_PROPERTY(reference, get_reference, set_reference);
 
+public:
+  INLINE TypeHandle get_loader_type() const;
+  INLINE void set_loader_type(TypeHandle loader_type);
+
+PUBLISHED:
+  MAKE_PROPERTY(loader_type, get_loader_type);
+
 protected:
   INLINE ModelRoot(const ModelRoot &copy);
 
@@ -60,6 +67,7 @@ private:
   Filename _fullpath;
   time_t _timestamp;
   PT(ModelReference) _reference;
+  TypeHandle _loader_type;
 
 public:
   static void register_with_read_factory();

+ 3 - 2
panda/src/putil/bam.h

@@ -32,8 +32,8 @@ static const unsigned short _bam_major_ver = 6;
 // Bumped to major version 6 on 2006-02-11 to factor out PandaNode::CData.
 
 static const unsigned short _bam_first_minor_ver = 14;
-static const unsigned short _bam_last_minor_ver = 45;
-static const unsigned short _bam_minor_ver = 44;
+static const unsigned short _bam_last_minor_ver = 46;
+static const unsigned short _bam_minor_ver = 46;
 // Bumped to minor version 14 on 2007-12-19 to change default ColorAttrib.
 // Bumped to minor version 15 on 2008-04-09 to add TextureAttrib::_implicit_sort.
 // Bumped to minor version 16 on 2008-05-13 to add Texture::_quality_level.
@@ -66,5 +66,6 @@ static const unsigned short _bam_minor_ver = 44;
 // Bumped to minor version 43 on 2018-12-06 to expand BillboardEffect and CompassEffect.
 // Bumped to minor version 44 on 2018-12-23 to rename CollisionTube to CollisionCapsule.
 // Bumped to minor version 45 on 2020-03-18 to add Texture::_clear_color.
+// Bumped to minor version 46 on 2025-08-03 to add ModelRoot::_loader_type.
 
 #endif

+ 1 - 0
panda/src/putil/bamCacheRecord.I

@@ -30,6 +30,7 @@ operator == (const BamCacheRecord &other) const {
   return (_source_pathname == other._source_pathname &&
           _cache_filename == other._cache_filename &&
           _recorded_time == other._recorded_time &&
+          _loader_type == other._loader_type &&
           _record_size == other._record_size);
 }
 

+ 1 - 0
panda/src/putil/bamCacheRecord.h

@@ -93,6 +93,7 @@ private:
   time_t _recorded_time;
   std::streamsize _record_size;  // this is accurate only in the index file.
   time_t _source_timestamp;  // Not record to the cache file.
+  TypeHandle _loader_type;
 
   class DependentFile {
   public: