Panda3D
loaderFileTypeBam.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 loaderFileTypeBam.cxx
10  * @author jason
11  * @date 2000-06-21
12  */
13 
14 #include "loaderFileTypeBam.h"
15 #include "config_pgraph.h"
16 #include "bamFile.h"
17 #include "bamCacheRecord.h"
18 #include "modelRoot.h"
19 #include "loaderOptions.h"
20 
21 #include "dcast.h"
22 
23 TypeHandle LoaderFileTypeBam::_type_handle;
24 
25 /**
26  *
27  */
28 LoaderFileTypeBam::
29 LoaderFileTypeBam() {
30 }
31 
32 /**
33  *
34  */
35 std::string LoaderFileTypeBam::
36 get_name() const {
37  return "Bam";
38 }
39 
40 /**
41  *
42  */
43 std::string LoaderFileTypeBam::
44 get_extension() const {
45  return "bam";
46 }
47 
48 /**
49  * Returns true if this file type can transparently load compressed files
50  * (with a .pz or .gz extension), false otherwise.
51  */
53 supports_compressed() const {
54  return true;
55 }
56 
57 /**
58  * Returns true if the file type can be used to load files, and load_file() is
59  * supported. Returns false if load_file() is unimplemented and will always
60  * fail.
61  */
63 supports_load() const {
64  return true;
65 }
66 
67 /**
68  * Returns true if the file type can be used to save files, and save_file() is
69  * supported. Returns false if save_file() is unimplemented and will always
70  * fail.
71  */
73 supports_save() const {
74  return true;
75 }
76 
77 /**
78  *
79  */
80 PT(PandaNode) LoaderFileTypeBam::
81 load_file(const Filename &path, const LoaderOptions &options,
82  BamCacheRecord *record) const {
83  if (record != nullptr) {
84  record->add_dependent_file(path);
85  }
86 
87  bool report_errors = (options.get_flags() & LoaderOptions::LF_report_errors) != 0;
88 
89  BamFile bam_file;
90  if (!bam_file.open_read(path, report_errors)) {
91  return nullptr;
92  }
93  bam_file.get_reader()->set_loader_options(options);
94  time_t timestamp = bam_file.get_reader()->get_source()->get_timestamp();
95 
96  PT(PandaNode) node = bam_file.read_node(report_errors);
97  if (node != nullptr && node->is_of_type(ModelRoot::get_class_type())) {
98  ModelRoot *model_root = DCAST(ModelRoot, node.p());
99  model_root->set_fullpath(path);
100  model_root->set_timestamp(timestamp);
101  }
102 
103  return node;
104 }
105 
106 
107 /**
108  *
109  */
110 bool LoaderFileTypeBam::
111 save_file(const Filename &path, const LoaderOptions &options,
112  PandaNode *node) const {
113  BamFile bam_file;
114 
115  bool report_errors = (options.get_flags() & LoaderOptions::LF_report_errors) != 0;
116 
117  bool okflag = false;
118 
119  if (bam_file.open_write(path, report_errors)) {
120  if (bam_file.write_object(node)) {
121  okflag = true;
122  }
123  bam_file.close();
124  }
125  return okflag;
126 }
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
An instance of this class is written to the front of a Bam or Txo file to make the file a cached inst...
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.
The principle public interface to reading and writing Bam disk files.
Definition: bamFile.h:41
void close()
Closes the input or output stream.
Definition: bamFile.cxx:243
bool open_write(const Filename &bam_filename, bool report_errors=true)
Attempts to open the indicated file for writing.
Definition: bamFile.cxx:190
bool open_read(const Filename &bam_filename, bool report_errors=true)
Attempts to open the indicated filename for reading.
Definition: bamFile.cxx:51
get_reader
Returns the BamReader in charge of performing the read operations.
Definition: bamFile.h:81
bool write_object(const TypedWritable *object)
Writes the indicated object to the Bam file.
Definition: bamFile.cxx:226
The name of a file, such as a texture file or an Egg file.
Definition: filename.h:39
virtual bool supports_compressed() const
Returns true if this file type can transparently load compressed files (with a .pz or ....
virtual bool supports_load() const
Returns true if the file type can be used to load files, and load_file() is supported.
virtual bool supports_save() const
Returns true if the file type can be used to save files, and save_file() is supported.
Specifies parameters that may be passed to the loader.
Definition: loaderOptions.h:23
A node of this type is created automatically at the root of each model file that is loaded.
Definition: modelRoot.h:27
set_fullpath
Sets the full pathname of the model represented by this node, as found on disk.
Definition: modelRoot.h:37
set_timestamp
Sets the timestamp of the file on disk that was read for this model.
Definition: modelRoot.h:41
A basic node of the scene graph or data graph.
Definition: pandaNode.h:65
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
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.