Panda3D
load_prc_file.cxx
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file load_prc_file.cxx
10  * @author drose
11  * @date 2004-10-22
12  */
13 
14 #include "load_prc_file.h"
15 #include "configPageManager.h"
16 #include "configVariableManager.h"
17 #include "virtualFileSystem.h"
18 #include "config_express.h"
19 #include "config_putil.h"
20 #include "hashVal.h"
21 
22 /**
23  * A convenience function for loading explicit prc files from a disk file or
24  * from within a multifile (via the virtual file system). Save the return
25  * value and pass it to unload_prc_file() if you ever want to unload this file
26  * later.
27  *
28  * The filename is first searched along the default prc search path, and then
29  * also along the model path, for convenience.
30  *
31  * This function is defined in putil instead of in dtool with the read of the
32  * prc stuff, so that it can take advantage of the virtual file system (which
33  * is defined in express), and the model path (which is in putil).
34  */
35 ConfigPage *
36 load_prc_file(const Filename &filename) {
37  Filename path = filename;
38  path.set_text();
39 
40  ConfigPageManager *cp_mgr = ConfigPageManager::get_global_ptr();
41 
43  vfs->resolve_filename(path, cp_mgr->get_search_path()) ||
44  vfs->resolve_filename(path, get_model_path());
45 
46  std::istream *file = vfs->open_read_file(path, true);
47  if (file == nullptr) {
48  util_cat.error()
49  << "Unable to open " << path << "\n";
50  return nullptr;
51  }
52 
53  util_cat.info()
54  << "Reading " << path << "\n";
55 
56  ConfigPage *page = cp_mgr->make_explicit_page(path);
57  bool read_ok = page->read_prc(*file);
58  vfs->close_read_file(file);
59 
60  if (read_ok) {
61  return page;
62 
63  } else {
64  util_cat.info()
65  << "Unable to read " << path << "\n";
66  cp_mgr->delete_explicit_page(page);
67  return nullptr;
68  }
69 }
70 
71 /**
72  * Another convenience function to load a prc file from an explicit string,
73  * which represents the contents of the prc file.
74  *
75  * The first parameter is an arbitrary name to assign to this in-memory prc
76  * file. Supply a filename if the data was read from a file, or use any other
77  * name that is meaningful to you. The name is only used when the set of
78  * loaded prc files is listed.
79  */
80 EXPCL_PANDA_PUTIL ConfigPage *
81 load_prc_file_data(const std::string &name, const std::string &data) {
82  std::istringstream strm(data);
83 
84  ConfigPageManager *cp_mgr = ConfigPageManager::get_global_ptr();
85 
86  ConfigPage *page = cp_mgr->make_explicit_page(name);
87  bool read_ok = page->read_prc(strm);
88 
89  if (read_ok) {
90  page->set_trust_level(1); // temp hack
91  return page;
92 
93  } else {
94  util_cat.info()
95  << "Unable to read explicit prc data " << name << "\n";
96  cp_mgr->delete_explicit_page(page);
97  return nullptr;
98  }
99 }
100 
101 /**
102  * Unloads (and deletes) a ConfigPage that represents a prc file that was
103  * previously loaded by load_prc_file(). Returns true if successful, false if
104  * the file was unknown.
105  *
106  * After this function has been called, the ConfigPage pointer is no longer
107  * valid and should not be used again.
108  */
109 bool
111  ConfigPageManager *cp_mgr = ConfigPageManager::get_global_ptr();
112  return cp_mgr->delete_explicit_page(page);
113 }
114 
115 
116 #ifdef HAVE_OPENSSL
117 
118 /**
119  * Fills HashVal with the hash from the current prc file state as reported by
120  * ConfigVariableManager::write_prc_variables().
121  */
122 void
123 hash_prc_variables(HashVal &hash) {
124  std::ostringstream strm;
125  ConfigVariableManager *cv_mgr = ConfigVariableManager::get_global_ptr();
126  cv_mgr->write_prc_variables(strm);
127  hash.hash_string(strm.str());
128 }
129 
130 #endif // HAVE_OPENSSL
set_trust_level
Explicitly sets the trust level on this particular page.
Definition: configPage.h:59
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
A hierarchy of directories and files that appears to be one continuous file system,...
std::istream * open_read_file(const Filename &filename, bool auto_unwrap) const
Convenience function; returns a newly allocated istream if the file exists and can be read,...
void set_text()
Indicates that the filename represents a text file.
Definition: filename.I:424
Stores a 128-bit value that represents the hashed contents (typically MD5) of a file or buffer.
Definition: hashVal.h:31
bool resolve_filename(Filename &filename, const DSearchPath &searchpath, const std::string &default_extension=std::string()) const
Searches the given search path for the filename.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
bool read_prc(std::istream &in)
Reads the contents of a complete prc file, as returned by the indicated istream, into the current pag...
Definition: configPage.cxx:124
A global object that maintains the set of ConfigVariables (actually, ConfigVariableCores) everywhere ...
A global object that maintains the set of ConfigPages everywhere in the world, and keeps them in sort...
static void close_read_file(std::istream *stream)
Closes a file opened by a previous call to open_read_file().
ConfigPage * load_prc_file(const Filename &filename)
A convenience function for loading explicit prc files from a disk file or from within a multifile (vi...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
The name of a file, such as a texture file or an Egg file.
Definition: filename.h:39
EXPCL_PANDA_PUTIL ConfigPage * load_prc_file_data(const std::string &name, const std::string &data)
Another convenience function to load a prc file from an explicit string, which represents the content...
bool unload_prc_file(ConfigPage *page)
Unloads (and deletes) a ConfigPage that represents a prc file that was previously loaded by load_prc_...
static VirtualFileSystem * get_global_ptr()
Returns the default global VirtualFileSystem.
ConfigPage * make_explicit_page(const std::string &name)
Creates and returns a new, empty ConfigPage.
A page of ConfigDeclarations that may be loaded or unloaded.
Definition: configPage.h:30
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
bool delete_explicit_page(ConfigPage *page)
Removes a previously-constructed ConfigPage from the set of active pages, and deletes it.
DSearchPath & get_search_path()
Returns the search path used to locate implicit .prc files.
void write_prc_variables(std::ostream &out) const
Writes all of the prc-set config variables, as they appear in a prc file somewhere,...