Panda3D
 All Classes Functions Variables Enumerations
fltExternalReference.cxx
00001 // Filename: fltExternalReference.cxx
00002 // Created by:  drose (30Aug00)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 #include "fltExternalReference.h"
00016 #include "fltRecordReader.h"
00017 #include "fltRecordWriter.h"
00018 #include "fltHeader.h"
00019 #include "pathReplace.h"
00020 
00021 TypeHandle FltExternalReference::_type_handle;
00022 
00023 ////////////////////////////////////////////////////////////////////
00024 //     Function: FltExternalReference::Constructor
00025 //       Access: Public
00026 //  Description:
00027 ////////////////////////////////////////////////////////////////////
00028 FltExternalReference::
00029 FltExternalReference(FltHeader *header) : FltBead(header) {
00030   _flags = 0;
00031 }
00032 
00033 ////////////////////////////////////////////////////////////////////
00034 //     Function: FltExternalReference::apply_converted_filenames
00035 //       Access: Public, Virtual
00036 //  Description: Walks the hierarchy at this record and below and
00037 //               copies the _converted_filename record into the
00038 //               _orig_filename record, so the flt file will be
00039 //               written out with the converted filename instead of
00040 //               what was originally read in.
00041 ////////////////////////////////////////////////////////////////////
00042 void FltExternalReference::
00043 apply_converted_filenames() {
00044   _orig_filename = _converted_filename.to_os_generic();
00045   FltBead::apply_converted_filenames();
00046 }
00047 
00048 ////////////////////////////////////////////////////////////////////
00049 //     Function: FltExternalReference::output
00050 //       Access: Public, Virtual
00051 //  Description: Writes a quick one-line description of the record, but
00052 //               not its children.  This is a human-readable
00053 //               description, primarily for debugging; to write a flt
00054 //               file, use FltHeader::write_flt().
00055 ////////////////////////////////////////////////////////////////////
00056 void FltExternalReference::
00057 output(ostream &out) const {
00058   out << "External " << get_ref_filename();
00059   if (!_bead_id.empty()) {
00060     out << " (" << _bead_id << ")";
00061   }
00062 }
00063 
00064 ////////////////////////////////////////////////////////////////////
00065 //     Function: FltExternalReference::get_ref_filename
00066 //       Access: Public
00067 //  Description: Returns the name of the referenced file.
00068 ////////////////////////////////////////////////////////////////////
00069 Filename FltExternalReference::
00070 get_ref_filename() const {
00071   return _converted_filename;
00072 }
00073 
00074 ////////////////////////////////////////////////////////////////////
00075 //     Function: FltExternalReference::set_ref_filename
00076 //       Access: Public
00077 //  Description: Changes the name of the referenced file.
00078 ////////////////////////////////////////////////////////////////////
00079 void FltExternalReference::
00080 set_ref_filename(const Filename &filename) {
00081   _converted_filename = filename; 
00082   _orig_filename = _converted_filename.to_os_generic();
00083 }
00084 
00085 ////////////////////////////////////////////////////////////////////
00086 //     Function: FltExternalReference::extract_record
00087 //       Access: Protected, Virtual
00088 //  Description: Fills in the information in this bead based on the
00089 //               information given in the indicated datagram, whose
00090 //               opcode has already been read.  Returns true on
00091 //               success, false if the datagram is invalid.
00092 ////////////////////////////////////////////////////////////////////
00093 bool FltExternalReference::
00094 extract_record(FltRecordReader &reader) {
00095   if (!FltBead::extract_record(reader)) {
00096     return false;
00097   }
00098 
00099   nassertr(reader.get_opcode() == FO_external_ref, false);
00100   DatagramIterator &iterator = reader.get_iterator();
00101 
00102   string name = iterator.get_fixed_string(200);
00103   iterator.skip_bytes(1 + 1);
00104   iterator.skip_bytes(2);   // Undocumented additional padding.
00105   _flags = iterator.get_be_uint32();
00106   iterator.skip_bytes(2);
00107   iterator.skip_bytes(2);   // Undocumented additional padding.
00108 
00109   _orig_filename = name;
00110 
00111   if (!name.empty() && name[name.length() - 1] == '>') {
00112     // Extract out the bead name.
00113     size_t open = name.rfind('<');
00114     if (open != string::npos) {
00115       _orig_filename = name.substr(0, open);
00116       _bead_id = name.substr(open + 1, name.length() - open - 2);
00117     }
00118   }
00119   _converted_filename = _header->convert_path(Filename::from_os_specific(_orig_filename));
00120 
00121   check_remaining_size(iterator);
00122   return true;
00123 }
00124 
00125 ////////////////////////////////////////////////////////////////////
00126 //     Function: FltExternalReference::build_record
00127 //       Access: Protected, Virtual
00128 //  Description: Fills up the current record on the FltRecordWriter with
00129 //               data for this record, but does not advance the
00130 //               writer.  Returns true on success, false if there is
00131 //               some error.
00132 ////////////////////////////////////////////////////////////////////
00133 bool FltExternalReference::
00134 build_record(FltRecordWriter &writer) const {
00135   if (!FltBead::build_record(writer)) {
00136     return false;
00137   }
00138 
00139   writer.set_opcode(FO_external_ref);
00140   Datagram &datagram = writer.update_datagram();
00141 
00142   string name = _orig_filename;
00143   if (!_bead_id.empty()) {
00144     name += "<" + _bead_id + ">";
00145   }
00146 
00147   datagram.add_fixed_string(name.substr(0, 199), 200);
00148   datagram.pad_bytes(1 + 1);
00149   datagram.pad_bytes(2);   // Undocumented additional padding.
00150   datagram.add_be_uint32(_flags);
00151   datagram.pad_bytes(2);
00152   datagram.pad_bytes(2);   // Undocumented additional padding.
00153 
00154   return true;
00155 }
 All Classes Functions Variables Enumerations