Gnash  0.8.11dev
ImportAssetsTag.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
3 // Free Software Foundation, Inc
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 
19 #ifndef GNASH_SWF_IMPORTASSETSTAG_H
20 #define GNASH_SWF_IMPORTASSETSTAG_H
21 
22 #include <vector>
23 #include <utility>
24 #include <string>
25 #include <memory>
26 
27 #include "ControlTag.h"
28 #include "Movie.h"
29 #include "MovieClip.h"
30 #include "SWFStream.h"
31 #include "MovieFactory.h"
32 #include "log.h"
33 #include "StreamProvider.h"
34 
35 namespace gnash {
36 namespace SWF {
37 
39 {
40 public:
41 
42  typedef std::pair<int, std::string> Import;
43  typedef std::vector<Import> Imports;
44 
45  static void loader(SWFStream& in, TagType tag, movie_definition& m,
46  const RunResources& r)
47  {
48  assert(tag == SWF::IMPORTASSETS || tag == SWF::IMPORTASSETS2);
49 
50  boost::intrusive_ptr<ControlTag> p(new ImportAssetsTag(tag, in, m, r));
51  m.addControlTag(p);
52  }
53 
54 
56  //
60  virtual void executeState(MovieClip* m, DisplayList& /*l*/) const {
61  Movie* mov = m->get_root();
62  for (const Import& import : _imports) {
63  mov->addCharacter(import.first);
64  }
65  }
66 
67 private:
68 
70  const RunResources& r)
71  {
72  read(t, in, m, r);
73  }
74 
75  void read(TagType t, SWFStream& in, movie_definition& m,
76  const RunResources& r) {
77 
78  std::string source_url;
79  in.read_string(source_url);
80 
81  // Resolve relative urls against baseurl
82  URL abs_url(source_url, r.streamProvider().baseURL());
83 
84  unsigned char import_version = 0;
85 
86  if (t == SWF::IMPORTASSETS2) {
87  in.ensureBytes(2);
88  import_version = in.read_uint(8);
89  std::uint8_t reserved = in.read_uint(8);
90  UNUSED(reserved);
91  }
92 
93  in.ensureBytes(2);
94  const std::uint16_t count = in.read_u16();
95 
97  log_parse(_(" import: version = %u, source_url = %s (%s), "
98  "count = %d"), import_version, abs_url.str(), source_url,
99  count);
100  );
101 
102  // Try to load the source movie into the movie library.
103  boost::intrusive_ptr<movie_definition> source_movie;
104 
105  try {
106  source_movie = MovieFactory::makeMovie(abs_url, r);
107  }
108  catch (gnash::GnashException& e) {
109  log_error(_("Exception: %s"), e.what());
110  }
111 
112  if (!source_movie) {
113  // Give up on imports.
114  log_error(_("can't import movie from url %s"), abs_url.str());
115  return;
116  }
117 
118  // Quick consistency check, we might as well do
119  // something smarter, if we agree on semantic
120  if (source_movie == &m) {
122  log_swferror(_("Movie attempts to import symbols from "
123  "itself."));
124  );
125  return;
126  }
127 
128  // Get the imports.
129  for (size_t i = 0; i < count; ++i)
130  {
131  in.ensureBytes(2);
132  const std::uint16_t id = in.read_u16();
133 
134  // We don't consider 0 valid.
135  if (!id) continue;
136 
137  std::string symbolName;
138  in.read_string(symbolName);
140  log_parse(_(" import: id = %d, name = %s"), id, symbolName);
141  );
142  _imports.push_back(std::make_pair(id, symbolName));
143  }
144 
145  m.importResources(source_movie, _imports);
146  }
147 
148 private:
149 
150  Imports _imports;
151 
152 };
153 
154 } // namespace SWF
155 } // namespace gnash
156 
157 #endif
virtual void addCharacter(std::uint16_t)
Add a character to the list of known characters.
Definition: Movie.h:97
std::uint16_t read_u16()
Read a aligned unsigned 16-bit value from the stream.
Definition: SWFStream.cpp:332
Client program&#39;s interface to the definition of a movie or sprite.
Definition: movie_definition.h:95
std::pair< int, std::string > Import
Definition: ImportAssetsTag.h:42
A MovieClip is a container for DisplayObjects.
Definition: MovieClip.h:83
void read_string(std::string &to)
Reads a null-terminated string from the given file and assigns it to the given std::string, overriding any previous value of it.
Definition: SWFStream.cpp:395
TagType
SWF tag types. Symbolic names copied from Ming.
Definition: SWF.h:30
virtual Movie * get_root() const
Return the relative root of this DisplayObject.
Definition: MovieClip.cpp:2049
#define UNUSED(x)
Definition: utility.h:113
A top-level, standalone Movie that can be loaded and played.
Definition: Movie.h:46
Anonymous namespace for callbacks, local functions, event handlers etc.
Definition: dbus_ext.cpp:40
#define IF_VERBOSE_MALFORMED_SWF(x)
Definition: log.h:404
virtual void executeState(MovieClip *m, DisplayList &) const
Execute an ImportAssetsTag.
Definition: ImportAssetsTag.h:60
Definition: ImportAssetsTag.h:38
#define IF_VERBOSE_PARSE(x)
Definition: log.h:378
Control tags are swf tags that control the operation of the movie.
Definition: ControlTag.h:43
void log_parse(StringType msg, Args... args)
Definition: log.h:313
Definition: GnashKey.h:164
static DSOEXPORT boost::intrusive_ptr< movie_definition > makeMovie(const URL &url, const RunResources &runResources, const char *real_url=nullptr, bool startLoaderThread=true, const std::string *postdata=nullptr)
Create a gnash::movie_definition from the given URL.
Definition: MovieFactory.cpp:109
Definition: GnashKey.h:166
#define _(String)
Definition: log.h:44
Class to group together per-run and external resources for Gnash.
Definition: RunResources.h:53
void log_error(StringType msg, Args... args)
Definition: log.h:283
void log_swferror(StringType msg, Args... args)
Definition: log.h:325
unsigned read_uint(unsigned short bitcount)
Reads a bit-packed unsigned integer from the stream and returns it. The given bitcount determines the...
Definition: SWFStream.cpp:102
virtual void importResources(boost::intrusive_ptr< movie_definition >, const Imports &)
Import resources.
Definition: movie_definition.h:176
const URL & baseURL() const
The base URL that should be used to resolve all relative URLs.
Definition: StreamProvider.h:117
static void loader(SWFStream &in, TagType tag, movie_definition &m, const RunResources &r)
Definition: ImportAssetsTag.h:45
Definition: GnashKey.h:162
Definition: GnashKey.h:155
Definition: GnashKey.h:151
Definition: GnashKey.h:159
Definition: SWF.h:87
std::vector< Import > Imports
Definition: ImportAssetsTag.h:43
const StreamProvider & streamProvider() const
Get a StreamProvider instance.
Definition: RunResources.h:76
virtual void addControlTag(boost::intrusive_ptr< SWF::ControlTag >)
Add an ControlTag to this movie_definition&#39;s playlist.
Definition: movie_definition.h:273
A list of on-stage DisplayObjects, ordered by depth.
Definition: DisplayList.h:64
Definition: GnashKey.h:331
SWF stream wrapper class.
Definition: SWFStream.h:58
Uniform Resource Locator.
Definition: URL.h:34
Top-level gnash exception.
Definition: GnashException.h:30
Definition: SWF.h:101
void ensureBytes(unsigned long needed)
Ensure the requested number of bytes are available for an aligned read in the currently opened tag...
Definition: SWFStream.cpp:50