Gnash  0.8.10
ImportAssetsTag.h
Go to the documentation of this file.
00001 // 
00002 //   Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
00003 //   Free Software 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 #include "StreamProvider.h"
00034 
00035 namespace gnash {
00036 namespace SWF {
00037 
00038 class ImportAssetsTag : public ControlTag
00039 {
00040 public:
00041 
00042     typedef std::pair<int, std::string> Import;
00043     typedef std::vector<Import> Imports;
00044     
00045     static void loader(SWFStream& in, TagType tag, movie_definition& m,
00046             const RunResources& r)
00047     {
00048         assert(tag == SWF::IMPORTASSETS || tag == SWF::IMPORTASSETS2);
00049 
00050         boost::intrusive_ptr<ControlTag> p(new ImportAssetsTag(tag, in, m, r));
00051         m.addControlTag(p);
00052     }
00053 
00054 
00056     //
00060     virtual void executeState(MovieClip* m, DisplayList& /*l*/) const {
00061         Movie* mov = m->get_root();
00062         for (Imports::const_iterator it = _imports.begin(), e = _imports.end();
00063                 it != e; ++it) {
00064             mov->addCharacter(it->first);
00065         }
00066     }
00067 
00068 private:
00069 
00070     ImportAssetsTag(TagType t, SWFStream& in, movie_definition& m,
00071             const RunResources& r)
00072     {
00073         read(t, in, m, r);
00074     }
00075 
00076     void read(TagType t, SWFStream& in, movie_definition& m,
00077             const RunResources& r) {
00078 
00079         std::string source_url;
00080         in.read_string(source_url);
00081 
00082         // Resolve relative urls against baseurl
00083         URL abs_url(source_url, r.streamProvider().baseURL());
00084 
00085         unsigned char import_version = 0;
00086 
00087         if (t == SWF::IMPORTASSETS2) {
00088             in.ensureBytes(2);
00089             import_version = in.read_uint(8);
00090             boost::uint8_t reserved = in.read_uint(8);
00091             UNUSED(reserved);
00092         }
00093 
00094         in.ensureBytes(2);
00095         const boost::uint16_t count = in.read_u16();
00096 
00097         IF_VERBOSE_PARSE(
00098             log_parse(_("  import: version = %u, source_url = %s (%s), "
00099                 "count = %d"), import_version, abs_url.str(), source_url,
00100                 count);
00101         );
00102 
00103         // Try to load the source movie into the movie library.
00104         boost::intrusive_ptr<movie_definition> source_movie;
00105 
00106         try {
00107             source_movie = MovieFactory::makeMovie(abs_url, r);
00108         }
00109         catch (gnash::GnashException& e) {
00110             log_error(_("Exception: %s"), e.what());
00111         }
00112 
00113         if (!source_movie) {
00114             // Give up on imports.
00115             log_error(_("can't import movie from url %s"), abs_url.str());
00116             return;
00117         }
00118 
00119         // Quick consistency check, we might as well do
00120         // something smarter, if we agree on semantic
00121         if (source_movie == &m) {
00122             IF_VERBOSE_MALFORMED_SWF(
00123                 log_swferror(_("Movie attempts to import symbols from "
00124                         "itself."));
00125             );
00126             return;
00127         }
00128         
00129         // Get the imports.
00130         for (size_t i = 0; i < count; ++i)
00131         {
00132             in.ensureBytes(2);
00133             const boost::uint16_t id = in.read_u16();
00134 
00135             // We don't consider 0 valid.
00136             if (!id) continue;
00137 
00138             std::string symbolName;
00139             in.read_string(symbolName);
00140             IF_VERBOSE_PARSE (
00141                 log_parse(_("  import: id = %d, name = %s"), id, symbolName);
00142             );
00143             _imports.push_back(std::make_pair(id, symbolName));
00144         }
00145         
00146         m.importResources(source_movie, _imports);
00147     }
00148 
00149 private:
00150 
00151     Imports _imports;
00152 
00153 };
00154 
00155 } // namespace SWF
00156 } // namespace gnash
00157 
00158 #endif