update copyright date
[gnash.git] / libcore / asobj / MovieClipLoader.cpp
blobf4e717d4e3af6244ad702f0ef21afce670f73fd9
1 // MovieClipLoader.cpp: Implementation of ActionScript MovieClipLoader class.
2 //
3 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010,
4 // 2011 Free Software Foundation, Inc
5 //
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "MovieClipLoader.h"
23 #include <string>
25 #include "smart_ptr.h"
26 #include "as_value.h"
27 #include "as_object.h" // for inheritance
28 #include "movie_root.h"
29 #include "fn_call.h"
30 #include "Global_as.h"
31 #include "as_function.h"
32 #include "MovieClip.h"
33 #include "DisplayObject.h" // for loadClip (get_parent)
34 #include "log.h"
35 #include "VM.h" // for the string table.
36 #include "AsBroadcaster.h" // for initializing self as a broadcaster
37 #include "namedStrings.h"
38 #include "ExecutableCode.h"
39 #include "NativeFunction.h"
41 //#define GNASH_DEBUG 1
43 namespace gnash {
45 // Forward declarations
46 namespace {
47 as_value moviecliploader_loadClip(const fn_call& fn);
48 as_value moviecliploader_unloadClip(const fn_call& fn);
49 as_value moviecliploader_getProgress(const fn_call& fn);
50 as_value moviecliploader_new(const fn_call& fn);
51 void attachMovieClipLoaderInterface(as_object& o);
54 void
55 registerMovieClipLoaderNative(as_object& global)
57 VM& vm = getVM(global);
58 vm.registerNative(moviecliploader_loadClip, 112, 100);
59 vm.registerNative(moviecliploader_getProgress, 112, 101);
60 vm.registerNative(moviecliploader_unloadClip, 112, 102);
63 /// Extern.
64 void
65 moviecliploader_class_init(as_object& where, const ObjectURI& uri)
67 // This is going to be the where Number "class"/"function"
68 Global_as& gl = getGlobal(where);
70 as_object* proto = createObject(gl);;
72 as_object* cl = gl.createClass(&moviecliploader_new, proto);
73 attachMovieClipLoaderInterface(*proto);
75 AsBroadcaster::initialize(*proto);
77 as_object* null = 0;
78 callMethod(&gl, NSV::PROP_AS_SET_PROP_FLAGS, proto, null, 1027);
80 where.init_member(uri, cl, as_object::DefaultFlags);
84 namespace {
86 void
87 attachMovieClipLoaderInterface(as_object& o)
89 const int flags = PropFlags::onlySWF7Up;
91 VM& vm = getVM(o);
93 o.init_member("loadClip", vm.getNative(112, 100), flags);
94 o.init_member("getProgress", vm.getNative(112, 101), flags);
95 o.init_member("unloadClip", vm.getNative(112, 102), flags);
98 as_value
99 moviecliploader_loadClip(const fn_call& fn)
101 as_object* ptr = ensure<ValidThis>(fn);
103 if (fn.nargs < 2) {
104 IF_VERBOSE_ASCODING_ERRORS(
105 std::stringstream ss; fn.dump_args(ss);
106 log_aserror(_("MovieClipLoader.loadClip(%s): missing arguments"),
107 ss.str());
109 return as_value(false);
112 if (!fn.arg(0).is_string()) {
113 IF_VERBOSE_ASCODING_ERRORS(
114 std::stringstream ss; fn.dump_args(ss);
115 log_aserror(_("MovieClipLoader.loadClip(%s): first argument must"
116 "be a string"), ss.str());
118 return as_value(false);
121 const std::string& str_url = fn.arg(0).to_string();
123 as_value tgt_arg = fn.arg(1);
124 const std::string& tgt_str = tgt_arg.to_string();
126 movie_root& mr = getRoot(*ptr);
128 // TODO: check if this logic can be generic to movie_root::loadMovie
129 DisplayObject* target = findTarget(fn.env(), tgt_str);
130 unsigned int junk;
131 if (!target && ! isLevelTarget(getSWFVersion(fn), tgt_str, junk) ) {
132 IF_VERBOSE_ASCODING_ERRORS(
133 log_aserror(_("Could not find target %s (evaluated from %s)"),
134 tgt_str, tgt_arg);
136 return as_value(false);
139 // TODO: return code is based on target validity if I'm not wrong
140 mr.loadMovie(str_url, tgt_str, "", MovieClip::METHOD_NONE, ptr);
141 return as_value(true);
144 as_value
145 moviecliploader_unloadClip(const fn_call& fn)
147 if (!fn.nargs) {
148 IF_VERBOSE_ASCODING_ERRORS(
149 std::stringstream ss; fn.dump_args(ss);
150 log_aserror(_("MovieClipLoader.unloadClip(%s): expected at least"
151 "one argument"), ss.str());
153 return as_value();
156 const std::string filespec = fn.arg(0).to_string();
157 log_unimpl(_("MovieClipLoader.unloadClip: %s"), __PRETTY_FUNCTION__, filespec);
158 return as_value();
161 as_value
162 moviecliploader_new(const fn_call& fn)
164 as_object* ptr = ensure<ValidThis>(fn);
165 Global_as& gl = getGlobal(fn);
167 as_object* array = gl.createArray();
168 callMethod(array, NSV::PROP_PUSH, ptr);
169 ptr->set_member(NSV::PROP_uLISTENERS, array);
170 ptr->set_member_flags(NSV::PROP_uLISTENERS, as_object::DefaultFlags);
171 return as_value();
174 // Invoked every time the loading content is written to disk during
175 // the loading process.
176 as_value
177 moviecliploader_getProgress(const fn_call& fn)
179 if (!fn.nargs) {
180 IF_VERBOSE_ASCODING_ERRORS(
181 log_aserror(_("MovieClipLoader.getProgress(): missing argument"));
183 return as_value();
186 as_object* target = toObject(fn.arg(0), getVM(fn));
188 if (!target) {
189 IF_VERBOSE_ASCODING_ERRORS(
190 log_aserror(_("MovieClipLoader.getProgress(%s): first argument is "
191 "not an object"), fn.arg(0));
193 return as_value();
196 MovieClip* sp = get<MovieClip>(target);
197 if (!sp) {
198 IF_VERBOSE_ASCODING_ERRORS(
199 log_aserror(_("MovieClipLoader.getProgress(%s): first argument is "
200 "not an sprite"), fn.arg(0));
202 return as_value();
205 as_object* mcl_obj = new as_object(getGlobal(fn));
207 size_t bytesLoaded = sp->get_bytes_loaded();
208 size_t bytesTotal = sp->get_bytes_total();
210 VM& vm = getVM(fn);
212 // We want these to be enumerable
213 mcl_obj->set_member(getURI(vm, "bytesLoaded"), bytesLoaded);
214 mcl_obj->set_member(getURI(vm, "bytesTotal"), bytesTotal);
216 return as_value(mcl_obj);
219 } // anonymous namespace
220 } // end of gnash namespace