1 // MovieClipLoader.cpp: Implementation of ActionScript MovieClipLoader class.
3 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010,
4 // 2011 Free Software Foundation, Inc
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.
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"
25 #include "smart_ptr.h"
27 #include "as_object.h" // for inheritance
28 #include "movie_root.h"
30 #include "Global_as.h"
31 #include "as_function.h"
32 #include "MovieClip.h"
33 #include "DisplayObject.h" // for loadClip (get_parent)
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
45 // Forward declarations
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
);
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);
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
);
78 callMethod(&gl
, NSV::PROP_AS_SET_PROP_FLAGS
, proto
, null
, 1027);
80 where
.init_member(uri
, cl
, as_object::DefaultFlags
);
87 attachMovieClipLoaderInterface(as_object
& o
)
89 const int flags
= PropFlags::onlySWF7Up
;
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
);
99 moviecliploader_loadClip(const fn_call
& fn
)
101 as_object
* ptr
= ensure
<ValidThis
>(fn
);
104 IF_VERBOSE_ASCODING_ERRORS(
105 std::stringstream ss
; fn
.dump_args(ss
);
106 log_aserror(_("MovieClipLoader.loadClip(%s): missing arguments"),
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
);
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)"),
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);
145 moviecliploader_unloadClip(const fn_call
& fn
)
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());
156 const std::string filespec
= fn
.arg(0).to_string();
157 log_unimpl(_("MovieClipLoader.unloadClip: %s"), __PRETTY_FUNCTION__
, filespec
);
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
);
174 // Invoked every time the loading content is written to disk during
175 // the loading process.
177 moviecliploader_getProgress(const fn_call
& fn
)
180 IF_VERBOSE_ASCODING_ERRORS(
181 log_aserror(_("MovieClipLoader.getProgress(): missing argument"));
186 as_object
* target
= toObject(fn
.arg(0), getVM(fn
));
189 IF_VERBOSE_ASCODING_ERRORS(
190 log_aserror(_("MovieClipLoader.getProgress(%s): first argument is "
191 "not an object"), fn
.arg(0));
196 MovieClip
* sp
= get
<MovieClip
>(target
);
198 IF_VERBOSE_ASCODING_ERRORS(
199 log_aserror(_("MovieClipLoader.getProgress(%s): first argument is "
200 "not an sprite"), fn
.arg(0));
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();
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