Panda3D
loaderFileTypeBam.cxx
1 // Filename: loaderFileTypeBam.cxx
2 // Created by: jason (21Jun00)
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 "loaderFileTypeBam.h"
16 #include "config_pgraph.h"
17 #include "bamFile.h"
18 #include "bamCacheRecord.h"
19 #include "modelRoot.h"
20 #include "loaderOptions.h"
21 
22 #include "dcast.h"
23 
24 TypeHandle LoaderFileTypeBam::_type_handle;
25 
26 ////////////////////////////////////////////////////////////////////
27 // Function: LoaderFileTypeBam::Constructor
28 // Access: Public
29 // Description:
30 ////////////////////////////////////////////////////////////////////
31 LoaderFileTypeBam::
32 LoaderFileTypeBam() {
33 }
34 
35 ////////////////////////////////////////////////////////////////////
36 // Function: LoaderFileTypeBam::get_name
37 // Access: Public, Virtual
38 // Description:
39 ////////////////////////////////////////////////////////////////////
40 string LoaderFileTypeBam::
41 get_name() const {
42  return "Bam";
43 }
44 
45 ////////////////////////////////////////////////////////////////////
46 // Function: LoaderFileTypeBam::get_extension
47 // Access: Public, Virtual
48 // Description:
49 ////////////////////////////////////////////////////////////////////
50 string LoaderFileTypeBam::
51 get_extension() const {
52  return "bam";
53 }
54 
55 ////////////////////////////////////////////////////////////////////
56 // Function: LoaderFileTypeBam::supports_compressed
57 // Access: Published, Virtual
58 // Description: Returns true if this file type can transparently load
59 // compressed files (with a .pz extension), false
60 // otherwise.
61 ////////////////////////////////////////////////////////////////////
64  return true;
65 }
66 
67 ////////////////////////////////////////////////////////////////////
68 // Function: LoaderFileTypeBam::supports_load
69 // Access: Published, Virtual
70 // Description: Returns true if the file type can be used to load
71 // files, and load_file() is supported. Returns false
72 // if load_file() is unimplemented and will always fail.
73 ////////////////////////////////////////////////////////////////////
75 supports_load() const {
76  return true;
77 }
78 
79 ////////////////////////////////////////////////////////////////////
80 // Function: LoaderFileTypeBam::supports_save
81 // Access: Published, Virtual
82 // Description: Returns true if the file type can be used to save
83 // files, and save_file() is supported. Returns false
84 // if save_file() is unimplemented and will always fail.
85 ////////////////////////////////////////////////////////////////////
87 supports_save() const {
88  return true;
89 }
90 
91 ////////////////////////////////////////////////////////////////////
92 // Function: LoaderFileTypeBam::load_file
93 // Access: Public, Virtual
94 // Description:
95 ////////////////////////////////////////////////////////////////////
96 PT(PandaNode) LoaderFileTypeBam::
97 load_file(const Filename &path, const LoaderOptions &options,
98  BamCacheRecord *record) const {
99  if (record != (BamCacheRecord *)NULL) {
100  record->add_dependent_file(path);
101  }
102 
103  bool report_errors = (options.get_flags() & LoaderOptions::LF_report_errors) != 0;
104 
105  BamFile bam_file;
106  if (!bam_file.open_read(path, report_errors)) {
107  return NULL;
108  }
109  bam_file.get_reader()->set_loader_options(options);
110  time_t timestamp = bam_file.get_reader()->get_source()->get_timestamp();
111 
112  PT(PandaNode) node = bam_file.read_node(report_errors);
113  if (node != (PandaNode *)NULL && node->is_of_type(ModelRoot::get_class_type())) {
114  ModelRoot *model_root = DCAST(ModelRoot, node.p());
115  model_root->set_fullpath(path);
116  model_root->set_timestamp(timestamp);
117  }
118 
119  return node;
120 }
121 
122 
123 ////////////////////////////////////////////////////////////////////
124 // Function: LoaderFileTypeBam::save_file
125 // Access: Public, Virtual
126 // Description:
127 ////////////////////////////////////////////////////////////////////
128 bool LoaderFileTypeBam::
129 save_file(const Filename &path, const LoaderOptions &options,
130  PandaNode *node) const {
131  BamFile bam_file;
132 
133  bool report_errors = (options.get_flags() & LoaderOptions::LF_report_errors) != 0;
134 
135  bool okflag = false;
136 
137  if (bam_file.open_write(path, report_errors)) {
138  if (bam_file.write_object(node)) {
139  okflag = true;
140  }
141  bam_file.close();
142  }
143  return okflag;
144 }
The principle public interface to reading and writing Bam disk files.
Definition: bamFile.h:45
A node of this type is created automatically at the root of each model file that is loaded...
Definition: modelRoot.h:31
bool write_object(const TypedWritable *object)
Writes the indicated object to the Bam file.
Definition: bamFile.cxx:258
A basic node of the scene graph or data graph.
Definition: pandaNode.h:72
virtual time_t get_timestamp() const
Returns the on-disk timestamp of the file that was read, at the time it was opened, if that is available, or 0 if it is not.
bool open_read(const Filename &bam_filename, bool report_errors=true)
Attempts to open the indicated filename for reading.
Definition: bamFile.cxx:56
Specifies parameters that may be passed to the loader.
Definition: loaderOptions.h:26
void set_fullpath(const Filename &fullpath)
Sets the full pathname of the model represented by this node, as found on disk.
Definition: modelRoot.I:87
virtual bool supports_load() const
Returns true if the file type can be used to load files, and load_file() is supported.
void close()
Closes the input or output stream.
Definition: bamFile.cxx:277
virtual bool supports_save() const
Returns true if the file type can be used to save files, and save_file() is supported.
BamReader * get_reader()
Returns the BamReader in charge of performing the read operations.
Definition: bamFile.cxx:393
void set_loader_options(const LoaderOptions &options)
Specifies the LoaderOptions for this BamReader.
Definition: bamReader.I:70
void set_timestamp(time_t timestamp)
Sets the timestamp of the file on disk that was read for this model.
Definition: modelRoot.I:115
virtual bool supports_compressed() const
Returns true if this file type can transparently load compressed files (with a .pz extension)...
The name of a file, such as a texture file or an Egg file.
Definition: filename.h:44
An instance of this class is written to the front of a Bam or Txo file to make the file a cached inst...
DatagramGenerator * get_source()
Returns the current source of the BamReader as set by set_source() or the constructor.
Definition: bamReader.I:32
bool is_of_type(TypeHandle handle) const
Returns true if the current object is or derives from the indicated type.
Definition: typedObject.I:63
void add_dependent_file(const Filename &pathname)
Adds the indicated file to the list of files that will be loaded to generate the data in this record...
bool open_write(const Filename &bam_filename, bool report_errors=true)
Attempts to open the indicated file for writing.
Definition: bamFile.cxx:217
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85