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