Execute dynamic algorithms in AS3 - actionscript-3

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.

Related

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.

AS3 to Generate Dynamic SWF?

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.

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.

What are some good ways to Save/Load files in AS3?

I am writing a game in ActionScript3 using Flixel as a base. I have been unable to find a good method for saving and loading files from the player's local hard drive. I know Flixel has a way to save game data to...I think...cookies that the player doesn't really have access to, and I want to avoid this.
I first learned programming in Java, and one thing I've seen a lot of is scanning (Scanner) and printing (PrintWriter) lines of strings to/from text files. Something like this for AS3 would be my ideal, but if there are other methods I'm open.
Does AS3 have anything in its basic library that can do this task well? Are there libraries I can download and include that do this well/better?
If your game is going to run on a webpage your options are limited. Due to security concerns Flash is limited to reading and writing files specifically selected by the user.
Should you be using Air to make a standalone game, you will have regular access to the filesystem and can write files much like with Java.
If not, you will be pretty much stuck with Flash cookie equivalent, SharedObject.

AS3 and hashing

Is it possible to hash flv videos so it is unplayable by itself and the format is unrecognizable by softwares, but i could actually de-hash them and play in my as3 script?
This is going to be realized on a CD so I can't use server scripts.
no, you can't. not with flv. you could however embed the video into an swf and encrypt the SWF. please note however, using a strong encryption for video data may lead to performance problems.
anyhow: you can load the binary data into a ByteArray, perform the decryption and then load the SWF from the ByteArray.
I'm assuming you mean that you'd like to do this "real time" in the app.
If so, there are two issues with that:
you will have to decrypt your custom video in software, so its going to run very slow. And depending on the end user's computer, unpredictably so.
As ALOToverflow previously mentioned, the key to your hashing algorithm will be in your .swf which can be simply be decompiled to reveal it.
Assuming you are using as3 with adobe AIR you could potentially ship your users "broken" flv's and then simply repair them before you attempt to play them back.
Depending on the resolution and length of the videos this might be a viable alternative.
(you'd utilize the FileStream Class for this.) http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/filesystem/FileStream.html
FLV's all start with a standard header, so you could start by just breaking the header, and repairing it later. (I'm talking about getting down and dirty with the binary code btw.)
http://en.wikipedia.org/wiki/Flash_Video
I created a similar project once.
this is what I did:
I encrypt the videos somehow, does not mater how, put them into one single big file.
A bulk data file. Then I run the swf, NOT from browser but rather run it from a JAVA application, which can decrypt the bulk video-file waaay faster, than the Adobe swf interpreter.
In the Java app, I pass the decrypted video to the swf as a parameter.
It is good to use Java, because all the clients can open it. Mac users, Windows users, anyone. It is possible to do it with C++, but that would limit your user-base.
Of course anyone with decent knowledge can reverse-engineer the encryption process, but it is harder to get the encryption from a Java compiled software, than from an AS3 swf.
Use Blowfish, Twofish or DES. Do not use RSA, it is slow.
You have to make the Java app extremely fast and simple, so also it is advised, not to use external cryptography libraries.