Embedding a file with a variable in AS3 + flixel - actionscript-3

I have recently picked up flixel (I have programmed before, but I have not in a while) and I have come across a problem. I am attempting to create maps, and eventually there will be multiple maps available.
I currently have a .txt file that has information that eventually goes into an array. Then I go from array to map with loadmap. It is maybe a simple way to accomplish this task, and maybe their are better ways (I have not explored all the possibilities with flixel, and if there are any opinions, go ahead and let me know) but it works good for now.
As I have previously said, I am trying to do this with multiple maps. I could do this by using [Embed(source = "")] for each .txt file, but this may end up being annoying. So, here is my question: Is there a possible way Embed a file based upon a variable?
My Map class looks like this:
public function Map(MapSet:String, TileSet:String)
{
super(MapSet, TileSet);
//more stuff
}
Now I have tried:
[Embed(scource="data/MapSets/" + MapSet + ".txt", mimeType = "application/octet-stream")]private var loadedMap:Class
and then I use:
map = new Map("Map1x1", "ForestTiles");
add(map);
Is there a possibility of doing this in a different way? Or maybe I am doing something wrong? All opinions are welcome.

It's beneficial to know what code does when using it.
Embed is a meta tag. It tells the compiler to include a certain file into the .swf file.
That means this does not happen at runtime.
When this embed code is "executed", your variables don't even exist yet.
That's why your code cannot work.
Despite not working, your solution is still valid:
If you find it tedious to generate code, write a program that does this for you. Create/use a program that finds all valid files in the given directory and creates all the embed tags. Run this program before the compiler.

To embed a text file and use as a string, try this:
// create source var TextSource
[Embed(source="textFile.txt",mimeType="application/octet-stream")]
private var TextSource:Class;
var myByteArray:ByteArray = new TextSource();
var myString:String = myByteArray.readUTFBytes(myByteArray.length);
// then use for your function
map = new Map("Map1x1", myString);

Related

Design patterns for libraries in GAS?

I have been trying to figure out the best way to write GAS libraries for a while now but I have a hart time figuring it out. I read Douglas Crockford's - Javascript: The good parts and I'm trying to implement these lessons in GAS. Every imported library adds global variable to your project (of the ScriptModule type), so the modular design pattern seems like a good place to start. Borrowing from the article I linked to such a pattern might look like this:
var MODULE = (function () {
var my = {},
privateVariable = 1;
function privateMethod() {
// ...
}
my.moduleProperty = 1;
my.moduleMethod = function () {
// ...
};
return my;
}());
This module could then be called like this:
var module = LibName.MODULE;
var property = module.moduleProperty; // 1
var method = module.moduleMethod; // ...
From what I gather it is best have as few global variables as possible, so the general advice seems to be to save everything in one global variable. So the naming convention should then look something like this: LibName.PROJECT_NAME, where project name is the name of your single global variable which holds a module with everything else.
My goal here is to design secure, non-conflicting libraries. Am I right to use this design pattern? Has anyone developed their own robust design patterns for GAS libraries yet?
When you import a script as a library in GAS a new "namespace" is already created for it, so there's no need or gain in creating another one yourself. You'd have have to "deference" it, like you did:
//there's no purpose for this
var module = LibName.MODULE;
var method = module.method;
//comparing if you write code directly on the library
var method1 = LibName.method1;
GAS is not client-side javascript, most of things you learn don't really apply to Apps Script, e.g. there's no DOM, no namespace conflict, no browser compatibility issues, etc.
By the way, I don't think this object nesting structure even works on Apps Script libraries.

Access all symbols in a *.swf file

Is there a way to access all exported symbols in a *.swf file?
I mean all the symbols that are marked Export to Actionscript in the Library of Flash IDE. That way I could use getDefinition() class without having to know the name of the symbol beforehand.
This is for an internal tool made in AS3 that will work along a framework, which needs to perform some operations on every symbol of a *.swf file.
So performance isn't required, and a hackish solution (accessing ByteArray of the swf or something like that) is valid.
Thanks
As Daniel suggested, I ended up using as3swf to parse the ByteCode of the *.swf file and read the class.
Here's a simple function I made that returns an array containing the symbol names, ready to be used with getDefinition(). You must pass in a ByteArray of the *.swf file.
private function getSymbolList(bytes:ByteArray):Array {
var swf:SWF = new SWF(bytes);
var ret:Array = [];
for each(var tag:ITag in swf.tags) {
if(tag is TagSymbolClass) {
var symbolClass:TagSymbolClass = tag as TagSymbolClass;
for (var i:int = 0; i < symbolClass.symbols.length; i++) {
ret.push(symbolClass.symbols[i].name);
} return ret;
}
} return ret;
}
With the new 11.3 Flash API, this feature is built-in.
var definitions:Vector.<String> =
this.loaderInfo.applicationDomain.getQualifiedDefinitionNames();
It is possible by reading the bytecode of the swf, de-serializing it and reading the class names.
This is complicated work though, so it's best to look to low level libraries that already do that. the as3commons bytecode class does this.
http://www.google.com/codesearch#Ueq88nAe-j0/trunk/as3-commons-bytecode/
as3swf can also get at this data. you can have a look through the classes and see how they do it, or just use the classes.
I haven't actually used them, so I don't have any code to share, but hope this helps.

Writing special characters with purepdf in AS3

I'm working with flex 4 and need to export some pdf files. I'm useing purePDF library for this. Can anyone explain me how to make possible to write romanian characters in pdf files with purePDF -> I need to write characters like ăîşţ, etc.
I look in the wiki documentation of that library but cannot understand enough what I need. Appreciate any help.
Thanks
I'll ask my own question, taking in consideration that it'll be useful for other one.
Note: Taking in account that purepdf is the iText library equivalent from Java, when you encounter any problems and need documentation, you can consult iText documentation for inspiration with purePDF.
So this is what you need to do:
find a suitable "True Type" font that contains your special characters (with "ttf" extension). For me, I found that in the /usr/share/fonts/truetype/*.
copy the *.ttf font file in a directory for use in your application (in my case "assets" directory)
after you find that, you can try it if matches your needs by the following code :
public static const SERIF_NORMAL : String = "FreeSerif.ttf";
//"assets/fonts/FreeSerif.ttf" is the directory where I copied my *.ttf files
[Embed(source="assets/fonts/FreeSerif.ttf", mimeType="application/octet-stream")]
private var serifNormalCls : Class;
private var normalFont : Font;
var bfNormal : BaseFont;
//...
//in youre initialization function :
FontsResourceFactory.getInstance().registerFont(SERIF_NORMAL, new this.serifNormalCls());
bfNormal = BaseFont.createFont(SERIF_NORMAL, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
//and when you need to use your the special characters, you will use that font
this.normalFont = new Font(Font.UNDEFINED, 10, Font.UNDEFINED, null, bfNormal);
Cheers!

Best Class for base64 encoding/decoding Action Script?

What would be a best Class for base64 encryption/decryption in Action Script?
Adobe has two utils for this - Base64Encoder & Base64Decoder. Both are located in the mx.utils package. Although, I had to track them down here - encoder & decoder.
The usage would be something like:
var bmd:BitmapData = myBitmap.bitmapData;
var ba:ByteArray = bmd.getPixels(new Rectangle(0,0,bmd.width,bmd.height));
var b64:Base64Encoder = new Base64Encoder();
b64.encodeBytes(ba);
trace(b64.toString());
Similarly, 'b64.encode' would encode a String rather than a ByteArray.
Both the encoder and decoder add their respective results to an internal buffer. So, you just have to use 'toString' to return the current buffer.
This one seems to have some legs/supporters: http://garry-lachman.com/2010/04/21/base64-encoding-class-in-actionscript-3/
At this link you will find a good Base64 class:
http://www.sociodox.com/base64.html
blooddy_crypto claims (according to it's benchmark) to have a faster base64 encoder/decoder than the mx.utils one.
Most of the packages that I have seen that include one as a support function use the one that is credited to Steve Webster. I do not know which package this started out in, but it appears in several libraries, including the as3crypto lib on Google Code.

General approach to reading lnk files

Several frameworks and languages seem to have lnk file parsers (C#, Java, Python, certainly countless others), to get to their targets, properties, etc. I'd like to know what is the general approach to reading lnk files, if I want to parse the lnk in another language that does not have said feature. Is there a Windows API for this?
There is not an official document from Microsoft describing lnk file format but there are some documents which have description of the format. Here is one of them: Shortcut File Format (.lnk)
As for the API you can use IShellLink Interface
Simply use lnk file parser at J.A.F.A.T. Archive of Forensics Analysis Tools project.
See lnk-parse-1.0.pl at http://jafat.sourceforge.net
There seems no have no dependencies. Syntax is simple and link file becomes a simple text in standard output and to be usable on Linux.
This is an old post, but here is my C# implementation for lnk processing that handles the entire spec, more info and command line tool on this blogspot page.
Using WSH-related components seems the most convenient option to read .lnk files in most languages on a post-XP windows system. You just need access to the COM environment and instantiate the WScript.Shell Component. (remember that on win, references to the Shell usually refer to explorer.exe)
The following snippet, e.g. does the thing on PHP: (PHP 5, using the COM object)
<?php
$wsh=new COM('WScript.Shell'); // the wsh object
// please note $wsh->CreateShortcut method actually
// DOES THE READING, if the file already exists.
$lnk=$wsh->CreateShortcut('./Shortcut.lnk');
echo $lnk->TargetPath,"\n";
This other one, instead, does the same on VBScript:
set sh = WScript.CreateObject("WScript.Shell")
set lnk = sh.CreateShortcut("./Shortcut.lnk")
MsgBox lnk.TargetPath
Most examples in the field are written in VB/VBS, but they translate well on the whole range of languages supporting COM and WSH interaction in a form or another.
This simple tutorial may come handy, as it lists and exemplifies some of the most interesting properties of a .lnk file other than the most important: TargetPath. Those are:
WindowStyle,
Hotkey,
IconLocation,
Description,
WorkingDirectory
here's some C# code using the Shell32 API, from my "ClearRecentLinks" project at https://github.com/jmaton/ClearRecentLinks
To use this your C# project has to reference c:\windows\system32\shell32.dll
string linksPath = "c:\some\folder";
Type shell32Type = Type.GetTypeFromProgID("Shell.Application");
Object shell = Activator.CreateInstance(shell32Type);
Shell32.Folder s32Folder = (Shell32.Folder)shell32Type.InvokeMember("NameSpace", System.Reflection.BindingFlags.InvokeMethod, null, shell, new object[] { linksPath });
foreach (Shell32.FolderItem2 item in s32Folder.Items())
{
if (item.IsLink)
{
var link = (Shell32.ShellLinkObject)item.GetLink;
if (link != null && !String.IsNullOrEmpty(link.Target.Path))
{
string linkTarget = link.Target.Path.ToLower();
// do something...
}
}
}
#Giorgi: Actually, there is an official specification of lnk files, at least it is claimed so.
However, for some reason, the link seems to be dead, and after downloading the whole (45 MB) doc package (Application_Services_and_NET_Framework.zip), it appears that it does not include the file MS-SHLLINK.pdf.
But is this really surprising ?
Once you got the file format, shouldn't be too hard to write code to read it.