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