• Main Page
  • Related Pages
  • Namespaces
  • Classes
  • Files
  • Examples
  • File List
  • File Members

ImportAssetsTag.h

Go to the documentation of this file.
00001 // 
00002 //   Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Free Software
00003 //   Foundation, Inc
00004 // 
00005 // This program is free software; you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation; either version 3 of the License, or
00008 // (at your option) any later version.
00009 // 
00010 // This program is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 // 
00015 // You should have received a copy of the GNU General Public License
00016 // along with this program; if not, write to the Free Software
00017 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018 
00019 #ifndef GNASH_SWF_IMPORTASSETSTAG_H
00020 #define GNASH_SWF_IMPORTASSETSTAG_H
00021 
00022 #include <vector>
00023 #include <utility>
00024 #include <string>
00025 #include <memory>
00026 
00027 #include "ControlTag.h"
00028 #include "Movie.h"
00029 #include "MovieClip.h"
00030 #include "SWFStream.h"
00031 #include "MovieFactory.h"
00032 #include "log.h"
00033 
00034 namespace gnash {
00035 namespace SWF {
00036 
00037 class ImportAssetsTag : public ControlTag
00038 {
00039 public:
00040 
00041     typedef std::pair<int, std::string> Import;
00042     typedef std::vector<Import> Imports;
00043     
00044     static void loader(SWFStream& in, TagType tag, movie_definition& m,
00045             const RunResources& r)
00046     {
00047         assert(tag == SWF::IMPORTASSETS || tag == SWF::IMPORTASSETS2);
00048 
00049         std::auto_ptr<ControlTag> p(new ImportAssetsTag(tag, in, m, r));
00050         m.addControlTag(p.release());
00051     }
00052 
00053 
00055     //
00059     virtual void executeState(MovieClip* m, DisplayList& /*l*/) const {
00060         Movie* mov = m->get_root();
00061         for (Imports::const_iterator it = _imports.begin(), e = _imports.end();
00062                 it != e; ++it) {
00063             mov->addCharacter(it->first);
00064         }
00065     }
00066 
00067 private:
00068 
00069     ImportAssetsTag(TagType t, SWFStream& in, movie_definition& m,
00070             const RunResources& r)
00071     {
00072         read(t, in, m, r);
00073     }
00074 
00075     void read(TagType t, SWFStream& in, movie_definition& m,
00076             const RunResources& r) {
00077 
00078         std::string source_url;
00079         in.read_string(source_url);
00080 
00081         // Resolve relative urls against baseurl
00082         URL abs_url(source_url, r.baseURL());
00083 
00084         unsigned char import_version = 0;
00085 
00086         if (t == SWF::IMPORTASSETS2) {
00087             in.ensureBytes(2);
00088             import_version = in.read_uint(8);
00089             boost::uint8_t reserved = in.read_uint(8);
00090             UNUSED(reserved);
00091         }
00092 
00093         in.ensureBytes(2);
00094         const boost::uint16_t count = in.read_u16();
00095 
00096         IF_VERBOSE_PARSE(
00097             log_parse(_("  import: version = %u, source_url = %s (%s), "
00098                 "count = %d"), import_version, abs_url.str(), source_url,
00099                 count);
00100         );
00101 
00102         // Try to load the source movie into the movie library.
00103         boost::intrusive_ptr<movie_definition> source_movie;
00104 
00105         try {
00106             source_movie = MovieFactory::makeMovie(abs_url, r);
00107         }
00108         catch (gnash::GnashException& e) {
00109             log_error(_("Exception: %s"), e.what());
00110         }
00111 
00112         if (!source_movie) {
00113             // Give up on imports.
00114             log_error(_("can't import movie from url %s"), abs_url.str());
00115             return;
00116         }
00117 
00118         // Quick consistency check, we might as well do
00119         // something smarter, if we agree on semantic
00120         if (source_movie == &m) {
00121             IF_VERBOSE_MALFORMED_SWF(
00122                 log_swferror(_("Movie attempts to import symbols from "
00123                         "itself."));
00124             );
00125             return;
00126         }
00127         
00128         // Get the imports.
00129         for (size_t i = 0; i < count; ++i)
00130         {
00131             in.ensureBytes(2);
00132             const boost::uint16_t id = in.read_u16();
00133 
00134             // We don't consider 0 valid.
00135             if (!id) continue;
00136 
00137             std::string symbolName;
00138             in.read_string(symbolName);
00139             IF_VERBOSE_PARSE (
00140                 log_parse(_("  import: id = %d, name = %s"), id, symbolName);
00141             );
00142             _imports.push_back(std::make_pair(id, symbolName));
00143         }
00144         
00145         m.importResources(source_movie, _imports);
00146     }
00147 
00148 private:
00149 
00150     Imports _imports;
00151 
00152 };
00153 
00154 } // namespace SWF
00155 } // namespace gnash
00156 
00157 #endif

Generated on Thu Sep 30 2010 14:34:59 for Gnash by  doxygen 1.7.1