change model_path logic

This commit is contained in:
David Rose 2002-09-16 16:48:23 +00:00
parent 2f3bcb2b6e
commit 39e03a8d0e
11 changed files with 290 additions and 277 deletions

View File

@ -52,18 +52,6 @@ get_extension() const {
return "egg";
}
////////////////////////////////////////////////////////////////////
// Function: LoaderFileTypeEgg::resolve_filename
// Access: Public, Virtual
// Description: Searches for the indicated filename on whatever paths
// are appropriate to this file type, and updates it if
// it is found.
////////////////////////////////////////////////////////////////////
void LoaderFileTypeEgg::
resolve_filename(Filename &path) const {
EggData::resolve_egg_filename(path);
}
////////////////////////////////////////////////////////////////////
// Function: LoaderFileTypeEgg::load_file
// Access: Public, Virtual

View File

@ -34,7 +34,6 @@ public:
virtual string get_name() const;
virtual string get_extension() const;
virtual void resolve_filename(Filename &path) const;
virtual PT(PandaNode) load_file(const Filename &path, bool report_errors) const;
public:

View File

@ -408,6 +408,37 @@ resolve_filename(Filename &filename,
return false;
}
////////////////////////////////////////////////////////////////////
// Function: VirtualFileSystem::find_all_files
// Access: Public
// Description: Searches all the directories in the search list for
// the indicated file, in order. Fills up the results
// list with *all* of the matching filenames found, if
// any. Returns the number of matches found.
//
// It is the responsibility of the the caller to clear
// the results list first; otherwise, the newly-found
// files will be appended to the list.
////////////////////////////////////////////////////////////////////
int VirtualFileSystem::
find_all_files(const Filename &filename, const DSearchPath &searchpath,
DSearchPath::Results &results) const {
int num_added = 0;
if (filename.is_local()) {
int num_directories = searchpath.get_num_directories();
for (int i = 0; i < num_directories; i++) {
Filename match(searchpath.get_directory(i), filename);
if (exists(match)) {
results.add_file(match);
num_added++;
}
}
}
return num_added;
}
////////////////////////////////////////////////////////////////////
// Function: VirtualFileSystem::write
// Access: Published

View File

@ -23,6 +23,7 @@
#include "virtualFile.h"
#include "filename.h"
#include "dSearchPath.h"
#include "pointerTo.h"
#include "pmap.h"
#include "config_express.h"
@ -65,9 +66,10 @@ PUBLISHED:
PT(VirtualFile) get_file(const Filename &filename) const;
PT(VirtualFile) find_file(const Filename &filename,
const DSearchPath &searchpath) const;
bool resolve_filename(Filename &filename,
const DSearchPath &searchpath,
bool resolve_filename(Filename &filename, const DSearchPath &searchpath,
const string &default_extension = string()) const;
int find_all_files(const Filename &filename, const DSearchPath &searchpath,
DSearchPath::Results &results) const;
INLINE bool exists(const Filename &filename) const;
INLINE bool is_directory(const Filename &filename) const;

View File

@ -17,9 +17,104 @@
////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
// Function: Loader::Results::Constructor
// Access: Published
// Description:
////////////////////////////////////////////////////////////////////
INLINE Loader::Results::
Results() {
}
////////////////////////////////////////////////////////////////////
// Function: Loader::Results::Copy Constructor
// Access: Published
// Description:
////////////////////////////////////////////////////////////////////
INLINE Loader::Results::
Results(const Loader::Results &copy) :
_files(copy._files)
{
}
////////////////////////////////////////////////////////////////////
// Function: Loader::Results::Copy Assignment Operator
// Access: Published
// Description:
////////////////////////////////////////////////////////////////////
INLINE void Loader::Results::
operator = (const Loader::Results &copy) {
_files = copy._files;
}
////////////////////////////////////////////////////////////////////
// Function: Loader::Results::Destructor
// Access: Published
// Description:
////////////////////////////////////////////////////////////////////
INLINE Loader::Results::
~Results() {
}
////////////////////////////////////////////////////////////////////
// Function: Loader::Results::clear
// Access: Published
// Description: Removes all the files from the list.
////////////////////////////////////////////////////////////////////
INLINE void Loader::Results::
clear() {
_files.clear();
}
////////////////////////////////////////////////////////////////////
// Function: Loader::Results::get_num_files
// Access: Published
// Description: Returns the number of files on the result list.
////////////////////////////////////////////////////////////////////
INLINE int Loader::Results::
get_num_files() const {
return _files.size();
}
////////////////////////////////////////////////////////////////////
// Function: Loader::Results::get_file
// Access: Published
// Description: Returns the nth file on the result list.
////////////////////////////////////////////////////////////////////
INLINE const Filename &Loader::Results::
get_file(int n) const {
nassertr(n >= 0 && n < (int)_files.size(), _files[0]._path);
return _files[n]._path;
}
////////////////////////////////////////////////////////////////////
// Function: Loader::Results::get_file_type
// Access: Published
// Description: Returns the file type of the nth file on the result
// list.
////////////////////////////////////////////////////////////////////
INLINE LoaderFileType *Loader::Results::
get_file_type(int n) const {
nassertr(n >= 0 && n < (int)_files.size(), NULL);
return _files[n]._type;
}
////////////////////////////////////////////////////////////////////
// Function: Loader::Results::add_file
// Access: Published
// Description: Adds a new file to the result list.
////////////////////////////////////////////////////////////////////
INLINE void Loader::Results::
add_file(const Filename &file, LoaderFileType *type) {
ConsiderFile cf;
cf._path = file;
cf._type = type;
_files.push_back(cf);
}
////////////////////////////////////////////////////////////////////
// Function: Loader::load_sync
// Access: Public
// Access: Published
// Description: Loads the file immediately, waiting for it to complete.
////////////////////////////////////////////////////////////////////
INLINE PT(PandaNode) Loader::

View File

@ -59,7 +59,7 @@ public:
////////////////////////////////////////////////////////////////////
// Function: Loader::Constructor
// Access: Public
// Access: Published
// Description:
////////////////////////////////////////////////////////////////////
Loader::
@ -69,7 +69,7 @@ Loader() : AsyncUtility() {
////////////////////////////////////////////////////////////////////
// Function: Loader::Destructor
// Access: Public
// Access: Published
// Description:
////////////////////////////////////////////////////////////////////
Loader::
@ -79,44 +79,111 @@ Loader::
}
////////////////////////////////////////////////////////////////////
// Function: Loader::resolve_filename
// Access: Public
// Description: Looks for the given filename somewhere on the various
// model paths. (The filename extension is used to
// determine which model paths are searched.) If the
// filename is found, updates the Filename to indicate
// the full path; otherwise, leaves the Filename alone.
//
// It is not necessary to call this before loading a
// model; this is just a useful thing to have in case
// you want to look for a file without loading it
// immediately.
// Function: Loader::find_all_files
// Access: Published
// Description: Searches along the model path for the given file
// name, and fills up the results list with all possible
// matches and their associated types, in order.
////////////////////////////////////////////////////////////////////
void Loader::
resolve_filename(Filename &filename) const {
if (filename.is_fully_qualified()) {
return;
int Loader::
find_all_files(const Filename &filename, Loader::Results &results) const {
if (!_file_types_loaded) {
load_file_types();
}
string extension = filename.get_extension();
if (extension.empty()) {
resolve_unknown_file_type(filename);
return;
int num_added = 0;
if (!extension.empty()) {
// If the extension is not empty, it specifies a single file type.
LoaderFileTypeRegistry *reg = LoaderFileTypeRegistry::get_ptr();
LoaderFileType *requested_type =
reg->get_type_from_extension(extension);
if (requested_type != (LoaderFileType *)NULL) {
if (!filename.is_local()) {
// Global filename, take it as it is.
results.add_file(filename, requested_type);
num_added++;
} else {
// Local filename, search along the path.
DSearchPath::Results files;
if (use_vfs) {
VirtualFileSystem *vfs = VirtualFileSystem::get_global_ptr();
num_added = vfs->find_all_files(filename, get_model_path(), files);
} else {
num_added = get_model_path().find_all_files(filename, files);
}
for (int i = 0; i < num_added; i++) {
results.add_file(files.get_file(i), requested_type);
}
}
}
} else {
// If the extension *is* empty, we have to search for all possible
// file types.
LoaderFileTypeRegistry *reg = LoaderFileTypeRegistry::get_ptr();
int num_types = reg->get_num_types();
if (!filename.is_local()) {
// Global filename, take it as it is.
for (int t = 0; t < num_types; t++) {
LoaderFileType *type = reg->get_type(t);
Filename file(filename);
file.set_extension(type->get_extension());
if (use_vfs) {
VirtualFileSystem *vfs = VirtualFileSystem::get_global_ptr();
if (vfs->exists(file)) {
results.add_file(file, type);
num_added++;
}
} else {
if (file.exists()) {
results.add_file(file, type);
num_added++;
}
}
}
} else {
// Local filename, look it up on the model path.
const DSearchPath &model_path = get_model_path();
int num_dirs = model_path.get_num_directories();
for (int i = 0; i < num_dirs; i++) {
const Filename &directory = model_path.get_directory(i);
for (int t = 0; t < num_types; t++) {
LoaderFileType *type = reg->get_type(t);
Filename file(directory, filename);
file.set_extension(type->get_extension());
if (use_vfs) {
VirtualFileSystem *vfs = VirtualFileSystem::get_global_ptr();
if (vfs->exists(file)) {
results.add_file(file, type);
num_added++;
}
} else {
if (file.exists()) {
results.add_file(file, type);
num_added++;
}
}
}
}
}
}
LoaderFileTypeRegistry *reg = LoaderFileTypeRegistry::get_ptr();
LoaderFileType *requested_type =
reg->get_type_from_extension(extension);
if (requested_type != (LoaderFileType *)NULL) {
requested_type->resolve_filename(filename);
}
return num_added;
}
////////////////////////////////////////////////////////////////////
// Function: Loader::request_load
// Access: Public
// Access: Published
// Description: Requests an asynchronous load of a file. The request
// will be queued and served by the asynchronous thread.
// If event_name is nonempty, it is the name of the
@ -192,7 +259,7 @@ request_load(const Filename &filename, const string &event_name) {
////////////////////////////////////////////////////////////////////
// Function: Loader::check_load
// Access: Public
// Access: Published
// Description: Returns true if the indicated load-request has
// completed and not yet been fetched, false otherwise.
////////////////////////////////////////////////////////////////////
@ -203,7 +270,7 @@ check_load(uint id) {
////////////////////////////////////////////////////////////////////
// Function: Loader::fetch_load
// Access: Public
// Access: Published
// Description: Returns the Node associated with the indicated id
// number (returned by a previous call to request_load),
// or NULL if the request has not yet completed.
@ -301,203 +368,44 @@ process_request() {
////////////////////////////////////////////////////////////////////
PT(PandaNode) Loader::
load_file(const Filename &filename) const {
string extension = filename.get_extension();
if (extension.empty()) {
return load_unknown_file_type(filename);
}
LoaderFileTypeRegistry *reg = LoaderFileTypeRegistry::get_ptr();
LoaderFileType *requested_type =
reg->get_type_from_extension(extension);
if (requested_type == (LoaderFileType *)NULL) {
// First, look for the file along the search path.
Results results;
int num_files = find_all_files(filename, results);
if (num_files == 0) {
// Couldn't find the file. Either it doesn't exist, or it's an
// unknown file type. Report a useful message either way.
string extension = filename.get_extension();
if (!extension.empty()) {
LoaderFileTypeRegistry *reg = LoaderFileTypeRegistry::get_ptr();
LoaderFileType *requested_type =
reg->get_type_from_extension(extension);
if (requested_type == (LoaderFileType *)NULL) {
loader_cat.error()
<< "Extension of file " << filename
<< " is unrecognized; cannot load.\n";
loader_cat.error(false)
<< "Currently known scene file types are:\n";
reg->write_types(loader_cat.error(false), 2);
return NULL;
}
}
loader_cat.error()
<< "Extension of file " << filename
<< " is unrecognized; cannot load.\n";
loader_cat.error(false)
<< "Currently known scene file types are:\n";
reg->write_types(loader_cat.error(false), 2);
<< "Couldn't load file " << filename << ": not found on model path.\n";
return NULL;
}
Filename requested_filename = filename;
if (!requested_filename.is_fully_qualified()) {
// Ask the loader type to look for the file along its paths.
requested_type->resolve_filename(requested_filename);
}
if (loader_cat.is_debug()) {
loader_cat.debug()
<< "Loading " << requested_type->get_name() << " file: "
<< requested_filename << "\n";
}
PT(PandaNode) result = requested_type->load_file(requested_filename, true);
return result;
}
class LoaderConsiderFile {
public:
Filename _path;
LoaderFileType *_type;
bool operator < (const LoaderConsiderFile &other) const {
return _path.compare_timestamps(other._path) > 0;
}
};
////////////////////////////////////////////////////////////////////
// Function: Loader::load_unknown_file_type
// Access: Private
// Description: Attempts to guess which file is meant when a file
// with no extension is given. Looks around for a file
// with a suitable extension for each of our known file
// types, and loads the most recent file available of
// any file type.
////////////////////////////////////////////////////////////////////
PT(PandaNode) Loader::
load_unknown_file_type(const Filename &filename) const {
typedef pvector<LoaderConsiderFile> Files;
Files files;
// First, build up a list of all of the possible files it could be.
LoaderFileTypeRegistry *reg = LoaderFileTypeRegistry::get_ptr();
int num_types = reg->get_num_types();
if (num_types == 0) {
loader_cat.error()
<< "Can't load file " << filename
<< "; no scene file types are known.\n";
return (PandaNode *)NULL;
}
VirtualFileSystem *vfs = VirtualFileSystem::get_global_ptr();
for (int i = 0; i < num_types; i++) {
LoaderConsiderFile consider;
consider._type = reg->get_type(i);
consider._path = filename;
consider._path.set_extension(consider._type->get_extension());
if (!consider._path.is_fully_qualified()) {
// Ask the loader type to look for the file along its paths.
consider._type->resolve_filename(consider._path);
}
if (use_vfs) {
if (vfs->exists(consider._path)) {
files.push_back(consider);
}
} else {
if (consider._path.exists()) {
files.push_back(consider);
}
}
}
if (files.empty()) {
loader_cat.error()
<< "Couldn't find file " << filename << " as:\n";
for (int i = 0; i < num_types; i++) {
Filename p = filename;
p.set_extension(reg->get_type(i)->get_extension());
loader_cat.error(false)
<< " " << p << "\n";
}
return (PandaNode *)NULL;
}
// Now sort the list into order by timestamp, from newest to oldest.
sort(files.begin(), files.end());
// And try to load each file one at a time.
Files::const_iterator fi;
if (loader_cat.is_debug()) {
loader_cat.debug()
<< "Loading " << filename << ", one of " << files.size()
<< " possible types:\n";
for (fi = files.begin(); fi != files.end(); ++fi) {
loader_cat.debug(false)
<< " " << (*fi)._path << "\n";
}
}
for (fi = files.begin(); fi != files.end(); ++fi) {
const LoaderConsiderFile &consider = (*fi);
PT(PandaNode) result = consider._type->load_file(consider._path, false);
for (int i = 0; i < num_files; i++) {
const Filename &path = results.get_file(i);
LoaderFileType *type = results.get_file_type(i);
PT(PandaNode) result = type->load_file(path, true);
if (result != (PandaNode *)NULL) {
return result;
}
if (loader_cat.is_debug()) {
loader_cat.debug()
<< "Couldn't read " << consider._type->get_name()
<< " file " << consider._path << "\n";
}
}
// None of the matching files could be loaded. Oh well.
loader_cat.error()
<< "Cannot read " << files.front()._path << "\n";
return (PandaNode *)NULL;
<< "Couldn't load file " << filename << ": all matching files on model path invalid.\n";
return NULL;
}
////////////////////////////////////////////////////////////////////
// Function: Loader::resolve_unknown_file_type
// Access: Private
// Description: Attempts to guess which file is meant when a file
// with no extension is given. Looks around for a file
// with a suitable extension for each of our known file
// types, and updates the filename if a suitable match
// is found.
////////////////////////////////////////////////////////////////////
void Loader::
resolve_unknown_file_type(Filename &filename) const {
typedef pvector<LoaderConsiderFile> Files;
Files files;
// First, build up a list of all of the possible files it could be.
LoaderFileTypeRegistry *reg = LoaderFileTypeRegistry::get_ptr();
int num_types = reg->get_num_types();
if (num_types == 0) {
// No known file types!
return;
}
VirtualFileSystem *vfs = VirtualFileSystem::get_global_ptr();
for (int i = 0; i < num_types; i++) {
LoaderConsiderFile consider;
consider._type = reg->get_type(i);
consider._path = filename;
consider._path.set_extension(consider._type->get_extension());
if (!consider._path.is_fully_qualified()) {
// Ask the loader type to look for the file along its paths.
consider._type->resolve_filename(consider._path);
}
if (use_vfs) {
if (vfs->exists(consider._path)) {
files.push_back(consider);
}
} else {
if (consider._path.exists()) {
files.push_back(consider);
}
}
}
if (files.empty()) {
// Couldn't find it anywhere.
return;
}
// Now sort the list into order by timestamp, from newest to oldest.
sort(files.begin(), files.end());
// And get the first one.
filename = files.front()._path;
}

View File

@ -25,8 +25,10 @@
#include "filename.h"
#include "tokenBoard.h"
#include "asyncUtility.h"
#include "dSearchPath.h"
class LoaderToken;
class LoaderFileType;
////////////////////////////////////////////////////////////////////
// Class : Loader
@ -34,11 +36,38 @@ class LoaderToken;
// threading
////////////////////////////////////////////////////////////////////
class EXPCL_PANDA Loader : public AsyncUtility {
private:
class ConsiderFile {
public:
Filename _path;
LoaderFileType *_type;
};
PUBLISHED:
class EXPCL_PANDA Results {
PUBLISHED:
INLINE Results();
INLINE Results(const Results &copy);
INLINE void operator = (const Results &copy);
INLINE ~Results();
INLINE void clear();
INLINE int get_num_files() const;
INLINE const Filename &get_file(int n) const;
INLINE LoaderFileType *get_file_type(int n) const;
public:
INLINE void add_file(const Filename &file, LoaderFileType *type);
private:
typedef pvector<ConsiderFile> Files;
Files _files;
};
Loader();
~Loader();
void resolve_filename(Filename &filename) const;
int find_all_files(const Filename &filename, Results &results) const;
INLINE PT(PandaNode) load_sync(const Filename &filename) const;
@ -52,8 +81,6 @@ private:
virtual bool process_request(void);
PT(PandaNode) load_file(const Filename &filename) const;
PT(PandaNode) load_unknown_file_type(const Filename &filename) const;
void resolve_unknown_file_type(Filename &filename) const;
typedef TokenBoard<LoaderToken> LoaderTokenBoard;
LoaderTokenBoard *_token_board;

View File

@ -38,20 +38,6 @@ LoaderFileType::
~LoaderFileType() {
}
////////////////////////////////////////////////////////////////////
// Function: LoaderFileType::resolve_filename
// Access: Public, Virtual
// Description: Searches for the indicated filename on whatever paths
// are appropriate to this file type, and updates it if
// it is found. It is not necessary to call this before
// calling load_file(), but it doesn't hurt; this is
// useful for when the loader needs to know the full
// pathname to the exact file it will be loading.
////////////////////////////////////////////////////////////////////
void LoaderFileType::
resolve_filename(Filename &) const {
}
////////////////////////////////////////////////////////////////////
// Function: LoaderFileType::load_file
// Access: Public, Virtual

View File

@ -25,6 +25,7 @@
#include "filename.h"
#include "pandaNode.h"
#include "pointerTo.h"
#include "dSearchPath.h"
////////////////////////////////////////////////////////////////////
// Class : LoaderFileType
@ -43,7 +44,6 @@ public:
virtual string get_name() const=0;
virtual string get_extension() const=0;
virtual void resolve_filename(Filename &path) const;
virtual PT(PandaNode) load_file(const Filename &path, bool report_errors) const;
public:

View File

@ -20,9 +20,6 @@
#include "config_pgraph.h"
#include "bamFile.h"
#include "config_util.h"
#include "config_express.h"
#include "virtualFileSystem.h"
#include "dcast.h"
TypeHandle LoaderFileTypeBam::_type_handle;
@ -56,25 +53,6 @@ get_extension() const {
return "bam";
}
////////////////////////////////////////////////////////////////////
// Function: LoaderFileTypeBam::resolve_filename
// Access: Public, Virtual
// Description: Searches for the indicated filename on whatever paths
// are appropriate to this file type, and updates it if
// it is found.
////////////////////////////////////////////////////////////////////
void LoaderFileTypeBam::
resolve_filename(Filename &path) const {
if (use_vfs) {
VirtualFileSystem *vfs = VirtualFileSystem::get_global_ptr();
vfs->resolve_filename(path, get_bam_path());
vfs->resolve_filename(path, get_model_path());
} else {
path.resolve_filename(get_bam_path());
path.resolve_filename(get_model_path());
}
}
////////////////////////////////////////////////////////////////////
// Function: LoaderFileTypeBam::load_file
// Access: Public, Virtual

View File

@ -34,7 +34,6 @@ public:
virtual string get_name() const;
virtual string get_extension() const;
virtual void resolve_filename(Filename &path) const;
virtual PT(PandaNode) load_file(const Filename &path, bool report_errors) const;
public: