AS3 to Generate Dynamic SWF? - actionscript-3

Is it possible, using AS3, to programmatically generate a .swf file? What my little app does is load a bunch of images, using FileReference, now I want to compile them into a stand-alone swf that the user can download (the swf will be a slideshow).
[edit]
There seems to be some mis-understanding of what I am asking. I want to have one compiled swf from which the user may select some images and press a button whereby the swf will create and compile a second swf containing said images. As this is a stand-alone app, there will be no server-side interaction.
What I am thinking is that there will be a string representing a dynamic class. When the user selects their images the appropriate embed tags are added to this string, which is then compiled and saved to the user's desktop.
So my question is, can a compiled swf generate and compile a second swf?

Take a look at this library, this allows you to create a .swf at runtime.
https://github.com/claus/as3swf

It should be possible to do server side, by generating the relevant code and running the as3 compiler.

I haven't investigated the package thoroughly, but the As3Eval library seems to be doing what you're asking - realtime compilation of AS3 into executable bytecode (on the client with no server interaction). Check out the demo.
I'm not sure it would support image embedding - perhaps you could encode the image data as a base64 string in your dynamically built source code.

You could also use swfmill, a command-line tool to create SWF files from XML documents.
It's not well documented but for basic usages (such as creating asset libraries) you should find what you need.

Related

Possible to include mxmlc flex compiler inside an AIR app?

Im currently developing an swf 3D Player for the web with Flex SDK. My goal is to create an air application that lets the user load models and export them to .swf 3D player to make those 3d models viewable over the web. Im using FlashDevelop.
My original goal was to include the mxmlc compiler and the whole flex sdk inside my air apps bin directory and run those with airs native extensions with arguments(command line). The problem is that flex sdk is about 800Mb size and again required JRE to run mxmlc.
Is there a way to include a minimalistic compiler inside my air application which doesn't require 800mb+ ? I know I just could output a general swf that first loads a config file with paths to the model and textures and then loads those, but I want the models and textures to be included inside the swf to make those safe from being easily downloaded. Any suggestions?
This doesn't answer the specific question asked, but it's too long for a comment.
Your problem is not how to include the flex compiler with AIR, it's how to stop the models and textures in the SWF from being downloaded easily.
First of all, there's no 100% secure answer; the SWF format is easily decompiled (so embedding 1 file per SWF is out), and if determined enough, someone can look at the memory of your application to find what they need; this is all about stopping the easy copy.
Off the top of my head, these are some of the ways you can do it, assuming that your end goal is an online 3D model viewer:
1) Have your own format
While technically not impossible, it is pretty awkward; basically you write your own format for storing and displaying models (essentially what most 3D modelling software does). This means that if someone gets hold of a model file, it's useless to them unless they write a parser (naturally, you'll need to protect your display SWF). If the purpose of your viewer is for other users to buy the model, then they'll need it in a format that they can use, so your workflow goes something like this:
Create model in common format -> convert to custom format for display in your player -> user browses and hits "buy" -> they download the common format file
2) Load the 3D model from your site
The simplest option; just load the model directly from your server, similar to how you'd load an image. To stop people from downloading the file directly, store them above the htdocs folder, and access them through a local URI in flash. So your server file system looks something like this:
models
Model1
Model2
...
htdocs
index.html
3DPlayer.swf // loads models using a "../models/Model1" path
Thus only your SWF can access the models (though technically, as it's a file download, it might be in the browser cache - you should test it). To actually put the models into the models directory, you'll either need FTP access to the server, or need to create a simple script (using PHP or the like) to upload them.
3) Stream the assets to your SWF
As already mentioned, you can use something like ColdFusion to stream the assets directly to your SWF on demand. I've no experience in ColdFusion though, so I can't really say how this would work, but it shouldn't be too hard
4) DB connection
Assuming your models are stored as text blobs in a DB (e.g. MySQL or MongoDB), you can either connect directly with the SWF or use PHP and AMFPHP to connect to the db and stream the model that way.
You'll need to implement some security to make sure that people can't spoof your server calls, but the actual code to connect and return the value isn't too hard. As an added bonus, you're not tied to the SWF format, and can implement JS displayers etc if needed.

How to create a Flash Movieclip via a web interface?

Forgive me if this is not the correct place to ask such a question.
Basically, I am looking for a way to allow someone to easily (at least, easier then diving into Flash) create a basic Flash animation (movieclip) with some placed image assets and text, all via a web interface.
I was thinking along the lines of using jQuery to allow drag and drop assets, of which you'd "record" the co-ordinates and relay that to Flash somehow to create the SWF file.
Or would it be better to create a tool in Flash (surely making operations easier) and then have some kind of "save" button to render out the SWF?
I'm totally open to suggestions.
Thank you
If you really need to produce a stand-alone SWF file (and not just a config file for you own "player"), I would probably do it like this;
1) Create your editor in whatever system you feel like (flash, jquery etc).
2) Build a config file in the client. This is used, together with all the resources the user added, to play back the animation.
3) Upload said assets and config file to your web server.
4) Use the flex compiler (on the server) to produce the resulting SWF, combining your player with generated AS3 which embeds the uploaded resources as needed, to make it available to the player.
5) Give the user a download link for the newly generated SWF file.
I don't think there is any swf compiler available for Flash (actionescript). You may create a swf that allows users to create an animation, save it as a home-made vector format, and then replay it. But I don't believe you can create and independant swf file with only the created animation in it.
Just think about a player in Flash, and a format that the player will read (xml, json, name it...). You can either generate the input with jQuery or Flash, and then feed it into your player to display it. You will eventually need two files.
Apparently this library allows you to compile SWFs at runtime. I haven't used it myself (yet) and don't know how stable or flexible it is, but it appears to be what you're looking for. I'd recommend giving it a spin and seeing if it's sufficient.
I'm not exactly sure how it saves the file, so you might run into security problems since it's a web application. Hopefully it should be OK though.

How to generate SWF from FLA using command line on Ubuntu Server?

I have a situation where I need to generate SWF from the FLA that user uploads on to the server. We use python and bash scripts, and work on Ubuntu Server 10.04 (64bit), any help on how to generate the SWF would be a great help.
Thanks.
Flash needs Flash IDE for compilation.
You probably can compile code using flex AS3 compiler on linux, but if there are graphical elements in Flash, you cannot compile them.
However, we had a similar situation, which we fixed by installing Flash in a MAC OS X Snow Leopard(10.6) and then wrote some php, some shell script, some applescript and made a command line compilation mechanism for AS3.
Mac OS X is a unix based os, so the command line is similar to any other unix. it comes with built in php, so you don't have to install anything.
Basically using script we dynamically generate something called .jsfl file. the jsfl contains the details of the .fla that needs to be compiled. these jsfl files can be executed by Flash IDE. so we invoke the flash IDE and pass on the generated .jsfl file. (http://gskinner.com/blog/archives/2004/08/jsfl_fla_batch.html)
This gskinner process works in windows also, but using mac we can make it command line. so that users can just ssh to the mac and execute the command to build the file.
FLA file format has changed significantly in the latest version of Flash. New FLAs are archives containing source files and assets, so you might be able to write a script unpacking a FLA and assembling an SWF from it. Prior versions of FLA contained proprietary format, not documented and there were no tools to process it in an automatic fashion, not on Linux for certain.
Depending on ActionScript version and your requirement to resource handling (by resources I mean images, fonts, sound tracks, videos etc) there are several options available:
Flex SDK, the material found in the blog post #ntidote refers to is somewhat dated, but it's OK / should work. You would need to consult MXMLC usage documentation to find out all available options (and some new required ones, not covered in the blog post). This is what you can do, if your goal is to compile AS3. This does not include video transcoding and you might find it challenging to embed vector graphics. MXMLC is, however, capable of compiling a large subset of SVG. It can also compile FXG - a new interchange graphic format that can describe vector shapes and text.
ActionScript 2 can also be compiled, but Flex doesn't offer a compiler for that. There is a very good, in fact a much better than the one written by Adobe, compiler for AS2: http://tech.motion-twin.com/mtasc.html .
There are other tools that are capable of generating SWFs - all depends on what exactly you need to do. Here's a good collection of different utilities: http://www.swftools.org/
Haxe is a whole other programming language that can compile to SWF. It also has its own linkers and ways of managing resources. http://haxe.org/
There is this project, which allows you to compile complex vector graphics and even animations from XML descriptions: http://code.google.com/p/hxswfml/

edit actionscript from swf - compiled file

I have one swf file, it is JW Player plugin. It have one actionscript file in which is whole plugin code, and rest of files are jwplayer api files. So i need to open that file (where whole plugin code is) and edit few lines. But there is a problem, because when i try to decompile file with sothink swf decompiler (and few others) - to convert it to .FLA, decompiles simply crashes. It is because of some file from jwplayer api library. So, next thing i tried is to edit via HEX (because there is not much to be edited), but i couldn't find what i needed. So what i need is - is there any way or not? If yes, which program can be used for this?
I've had to do something similar in the past. In my scenario, the client had the SWF but no FLA. I needed to change a hardcoded url and sothink wasn't decompiling but I could view the AS in the sothink previewer. Luckily for me the flash wasn't very complex so I was able to copy the AS and graphics out and start a new project. I would imagine that JW Player is much more complex so this may not be an option for you, but thought I'd mention it anyway.
Depending on your needs, you may be able to get what you want by using the plugin API to extend the functionality or maybe even by customizing a skin.

Execute dynamic algorithms in AS3

I have a Flash in AS3. I want to dynamically load code that performs some kind of calculations. Is it doable with AS3 (I've read that there is no eval)? Should I use an interpreter in AS3 that gets the data and does the calculations?
My first thought was to load an external swf that does all the calculations but I think having an engine that gets input from a socket is more elegant as a solution.
So, how is it possible to execute algorithms that come from a server in a swf file?
Well, there is an AS3 Eval Library by metalbot. You should probably take a look.
Also you may use browser JS engine to perform JavaScript eval with code downloaded from server. Look at ExternalInterface AS3 class for that.
If number of your code snippets is limited you could use special swfs (prepared beforehand) with those code snippets, downloaded from server. After loading an swf you can use it's codebase without any evals.