21ModelPool *ModelPool::_global_ptr =
nullptr;
28write(std::ostream &out) {
29 get_ptr()->ns_list_contents(out);
36ns_has_model(
const Filename &filename) {
38 Models::const_iterator ti;
39 ti = _models.find(filename);
40 if (ti != _models.end() && (*ti).second !=
nullptr) {
52ns_get_model(
const Filename &filename,
bool verify) {
54 PT(ModelRoot) cached_model;
55 bool got_cached_model =
false;
58 LightMutexHolder holder(_lock);
59 Models::const_iterator ti;
60 ti = _models.find(filename);
61 if (ti != _models.end()) {
63 cached_model = (*ti).second;
64 got_cached_model =
true;
68 if (got_cached_model && verify) {
69 if (pgraph_cat.is_debug()) {
71 <<
"ModelPool found " << cached_model <<
" for " << filename <<
"\n";
74 if (cached_model ==
nullptr) {
77 if (cache_check_timestamps) {
80 if (vfs->
exists(filename)) {
82 got_cached_model =
false;
87 if (cache_check_timestamps && cached_model->get_timestamp() != 0 &&
88 !cached_model->get_fullpath().empty()) {
91 PT(VirtualFile) vfile = vfs->
get_file(cached_model->get_fullpath());
92 if (vfile ==
nullptr) {
94 got_cached_model =
false;
96 }
else if (vfile->get_timestamp() > cached_model->get_timestamp()) {
99 got_cached_model =
false;
105 if (got_cached_model) {
106 if (pgraph_cat.is_debug()) {
108 <<
"ModelPool returning " << cached_model <<
" for " << filename <<
"\n";
123 PT(ModelRoot) cached_model = ns_get_model(filename,
true);
124 if (cached_model !=
nullptr) {
129 LoaderOptions new_options(options);
130 new_options.set_flags((new_options.get_flags() | LoaderOptions::LF_no_ram_cache) &
131 ~LoaderOptions::LF_search);
134 PT(PandaNode) panda_node = model_loader->load_sync(filename, new_options);
137 if (panda_node.is_null()) {
141 if (panda_node->is_of_type(ModelRoot::get_class_type())) {
142 node = DCAST(ModelRoot, panda_node);
146 node =
new ModelRoot(filename);
147 node->add_child(panda_node);
149 node->set_fullpath(filename);
153 LightMutexHolder holder(_lock);
157 Models::const_iterator ti;
158 ti = _models.find(filename);
159 if (ti != _models.end() && (*ti).second != cached_model) {
164 _models[filename] = node;
175 LightMutexHolder holder(_lock);
176 if (pgraph_cat.is_debug()) {
178 <<
"ModelPool storing " << model <<
" for " << filename <<
"\n";
181 _models[filename] = model;
188ns_release_model(
const Filename &filename) {
189 LightMutexHolder holder(_lock);
191 ti = _models.find(filename);
192 if (ti != _models.end()) {
202 LightMutexHolder holder(_lock);
212 LightMutexHolder holder(_lock);
215 if (ti != _models.end()) {
224ns_release_all_models() {
225 LightMutexHolder holder(_lock);
233ns_garbage_collect() {
234 LightMutexHolder holder(_lock);
236 int num_released = 0;
240 for (ti = _models.begin(); ti != _models.end(); ++ti) {
241 ModelRoot *node = (*ti).second;
242 if (node ==
nullptr ||
244 if (loader_cat.is_debug()) {
246 <<
"Releasing " << (*ti).first <<
"\n";
250 new_set.insert(new_set.end(), *ti);
254 _models.swap(new_set);
262ns_list_contents(std::ostream &out)
const {
263 LightMutexHolder holder(_lock);
265 out <<
"model pool contents:\n";
267 Models::const_iterator ti;
269 for (ti = _models.begin(); ti != _models.end(); ++ti) {
270 if ((*ti).second !=
nullptr) {
272 out << (*ti).first <<
"\n"
273 <<
" (count = " << (*ti).second->get_model_ref_count()
278 out <<
"total number of models: " << num_models <<
" (plus "
279 << _models.size() - num_models <<
" entries for nonexistent files)\n";
288 if (_global_ptr ==
nullptr) {
289 _global_ptr =
new ModelPool;
The name of a file, such as a texture file or an Egg file.
Similar to MutexHolder, but for a light mutex.
Specifies parameters that may be passed to the loader.
static Loader * get_global_ptr()
Returns a pointer to the global Loader.
This class unifies all references to the same filename, so that multiple attempts to load the same mo...
static void write(std::ostream &out)
Lists the contents of the model pool to the indicated output stream.
A node of this type is created automatically at the root of each model file that is loaded.
get_fullpath
Returns the full pathname of the model represented by this node, as found on disk.
get_model_ref_count
Returns the number of copies that exist of this particular ModelRoot node.
bool exists(const Filename &filename) const
Convenience function; returns true if the named file exists in the virtual file system hierarchy.
PointerTo< VirtualFile > get_file(const Filename &filename, bool status_only=false) const
Looks up the file by the indicated name in the file system.
static VirtualFileSystem * get_global_ptr()
Returns the default global VirtualFileSystem.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.