Panda3D
fltExternalReference.cxx
1 // Filename: fltExternalReference.cxx
2 // Created by: drose (30Aug00)
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 "fltExternalReference.h"
16 #include "fltRecordReader.h"
17 #include "fltRecordWriter.h"
18 #include "fltHeader.h"
19 #include "pathReplace.h"
20 
21 TypeHandle FltExternalReference::_type_handle;
22 
23 ////////////////////////////////////////////////////////////////////
24 // Function: FltExternalReference::Constructor
25 // Access: Public
26 // Description:
27 ////////////////////////////////////////////////////////////////////
28 FltExternalReference::
29 FltExternalReference(FltHeader *header) : FltBead(header) {
30  _flags = 0;
31 }
32 
33 ////////////////////////////////////////////////////////////////////
34 // Function: FltExternalReference::apply_converted_filenames
35 // Access: Public, Virtual
36 // Description: Walks the hierarchy at this record and below and
37 // copies the _converted_filename record into the
38 // _orig_filename record, so the flt file will be
39 // written out with the converted filename instead of
40 // what was originally read in.
41 ////////////////////////////////////////////////////////////////////
44  _orig_filename = _converted_filename.to_os_generic();
46 }
47 
48 ////////////////////////////////////////////////////////////////////
49 // Function: FltExternalReference::output
50 // Access: Public, Virtual
51 // Description: Writes a quick one-line description of the record, but
52 // not its children. This is a human-readable
53 // description, primarily for debugging; to write a flt
54 // file, use FltHeader::write_flt().
55 ////////////////////////////////////////////////////////////////////
57 output(ostream &out) const {
58  out << "External " << get_ref_filename();
59  if (!_bead_id.empty()) {
60  out << " (" << _bead_id << ")";
61  }
62 }
63 
64 ////////////////////////////////////////////////////////////////////
65 // Function: FltExternalReference::get_ref_filename
66 // Access: Public
67 // Description: Returns the name of the referenced file.
68 ////////////////////////////////////////////////////////////////////
71  return _converted_filename;
72 }
73 
74 ////////////////////////////////////////////////////////////////////
75 // Function: FltExternalReference::set_ref_filename
76 // Access: Public
77 // Description: Changes the name of the referenced file.
78 ////////////////////////////////////////////////////////////////////
80 set_ref_filename(const Filename &filename) {
81  _converted_filename = filename;
82  _orig_filename = _converted_filename.to_os_generic();
83 }
84 
85 ////////////////////////////////////////////////////////////////////
86 // Function: FltExternalReference::extract_record
87 // Access: Protected, Virtual
88 // Description: Fills in the information in this bead based on the
89 // information given in the indicated datagram, whose
90 // opcode has already been read. Returns true on
91 // success, false if the datagram is invalid.
92 ////////////////////////////////////////////////////////////////////
93 bool FltExternalReference::
94 extract_record(FltRecordReader &reader) {
95  if (!FltBead::extract_record(reader)) {
96  return false;
97  }
98 
99  nassertr(reader.get_opcode() == FO_external_ref, false);
100  DatagramIterator &iterator = reader.get_iterator();
101 
102  string name = iterator.get_fixed_string(200);
103  iterator.skip_bytes(1 + 1);
104  iterator.skip_bytes(2); // Undocumented additional padding.
105  _flags = iterator.get_be_uint32();
106  iterator.skip_bytes(2);
107  iterator.skip_bytes(2); // Undocumented additional padding.
108 
109  _orig_filename = name;
110 
111  if (!name.empty() && name[name.length() - 1] == '>') {
112  // Extract out the bead name.
113  size_t open = name.rfind('<');
114  if (open != string::npos) {
115  _orig_filename = name.substr(0, open);
116  _bead_id = name.substr(open + 1, name.length() - open - 2);
117  }
118  }
119  _converted_filename = _header->convert_path(Filename::from_os_specific(_orig_filename));
120 
121  check_remaining_size(iterator);
122  return true;
123 }
124 
125 ////////////////////////////////////////////////////////////////////
126 // Function: FltExternalReference::build_record
127 // Access: Protected, Virtual
128 // Description: Fills up the current record on the FltRecordWriter with
129 // data for this record, but does not advance the
130 // writer. Returns true on success, false if there is
131 // some error.
132 ////////////////////////////////////////////////////////////////////
133 bool FltExternalReference::
134 build_record(FltRecordWriter &writer) const {
135  if (!FltBead::build_record(writer)) {
136  return false;
137  }
138 
139  writer.set_opcode(FO_external_ref);
140  Datagram &datagram = writer.update_datagram();
141 
142  string name = _orig_filename;
143  if (!_bead_id.empty()) {
144  name += "<" + _bead_id + ">";
145  }
146 
147  datagram.add_fixed_string(name.substr(0, 199), 200);
148  datagram.pad_bytes(1 + 1);
149  datagram.pad_bytes(2); // Undocumented additional padding.
150  datagram.add_be_uint32(_flags);
151  datagram.pad_bytes(2);
152  datagram.pad_bytes(2); // Undocumented additional padding.
153 
154  return true;
155 }
virtual void output(ostream &out) const
Writes a quick one-line description of the record, but not its children.
This class writes a sequence of FltRecords to an ostream, handling opcode and size counts properly...
This class turns an istream into a sequence of FltRecords by reading a sequence of Datagrams and extr...
virtual void apply_converted_filenames()
Walks the hierarchy at this record and below and copies the _converted_filename record into the _orig...
virtual void apply_converted_filenames()
Walks the hierarchy at this record and below and copies the _converted_filename record into the _orig...
Definition: fltRecord.cxx:340
void check_remaining_size(const DatagramIterator &di, const string &name=string()) const
Checks that the iterator has no bytes left, as it should at the end of a successfully read record...
Definition: fltRecord.cxx:313
void set_ref_filename(const Filename &filename)
Changes the name of the referenced file.
A base class for any of a broad family of flt records that represent particular beads in the hierarch...
Definition: fltBead.h:33
DatagramIterator & get_iterator()
Returns an iterator suitable for extracting data from the current record.
void pad_bytes(size_t size)
Adds the indicated number of zero bytes to the datagram.
Definition: datagram.cxx:111
string to_os_generic() const
This is similar to to_os_specific(), but it is designed to generate a filename that can be understood...
Definition: filename.cxx:1261
This is the first bead in the file, the top of the bead hierarchy, and the primary interface to readi...
Definition: fltHeader.h:48
The name of a file, such as a texture file or an Egg file.
Definition: filename.h:44
Filename get_ref_filename() const
Returns the name of the referenced file.
FltOpcode get_opcode() const
Returns the opcode associated with the current record.
void add_fixed_string(const string &str, size_t size)
Adds a fixed-length string to the datagram.
Definition: datagram.I:404
A class to retrieve the individual data elements previously stored in a Datagram. ...
void add_be_uint32(PN_uint32 value)
Adds an unsigned 32-bit big-endian integer to the datagram.
Definition: datagram.I:303
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85
void set_opcode(FltOpcode opcode)
Sets the opcode associated with the current record.
Datagram & update_datagram()
Returns a modifiable reference to the datagram associated with the current record.
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:43
string get_fixed_string(size_t size)
Extracts a fixed-length string.
static Filename from_os_specific(const string &os_specific, Type type=T_general)
This named constructor returns a Panda-style filename (that is, using forward slashes, and no drive letter) based on the supplied filename string that describes a filename in the local system conventions (for instance, on Windows, it may use backslashes or begin with a drive letter and a colon).
Definition: filename.cxx:332