Friday, August 19, 2011

Use vLib.swc to manage flash project resources and protect swf assets

====================
Update: 2012/12/21
vLib v0.15 released!
https://flaswf.googlecode.com/svn/trunk/vLib/bin/v0.15
What's new:
Fixed several bugs, disable useless traces.
Add new sound API of Flash 11.

myLib.playSound("MP3SoundName");//Play the mp3 sound from mp3 file directly! 
====================

vLib - the virtual library project, is a AS3 swc lib to handle your flash project resources, such as bitmaps, swfs, mp3 sounds, texts, xmls and binary files.
It will ease you from importing assets into your .fla's library, writing lots of Embed stuffs for the Flex project, and save your efforts of using lots of URLLoader and Loader codes.
Only need to write a XML file and markup the name and URL address for all flash resources, you can then access them using simple AS3 api provided by vLib.swc.

Tutorial of using vLib.swc, Step by Step:

Step 1[Prepare the test resources files]:
Create a folder and put all the resources(such as Binary files, mp3 sounds, swfs, text files, xml files, .JPG and .PNG pictures) into it.

Step 2[Write the description xml]:
Write a xml file of the resources' names and URLs.
The XML must begin and end with

There're 6 kinds of resources, each kind has a tag, they're:
 for all text files
 for all picture files
 for all swf files
 for all mp3 files
 for all binary files
All resource files should be written into the corresponding tag of the format
  
The details of the description XML's format are showed in the example.
Example["vLib.xml" from vLib\examples\bin\DataFiles]:




  
  



  
  




  
  















  
  





Note: Due to formatting issues, the above xmls don't display correctly, all tag names should be in upper case in the first character, for details, please see the examples in the svn.


Step 3[Add the vLib.swc to library]:
Add the "vLib.swc" to your flash/flex project, so you can use it in your AS3 code.

Step 4[Initialize the virtual library]:

First declare an instance and provide the the description xml file "vLib.xml" to it.
import Lib.*;
var myLib:Lib = new Lib();
myLib.addEventListener(Event.COMPLETE, Libloaded);
myLib.init0("./DataFiles/Lib.xml");
function Libloaded(event:Event):void
{
//All Resources Loaded!
}

Step 5[Access resources from the virtual library]:
As showed in the code, once all resources are loaded, it will triger the callback function "Libloaded", that's the time you can use them.
function Libloaded(event:Event):void
{
//All Resources Loaded!
test();
}
function test():void
{
myLib.load(new DataFiles_Class());
addChild(myLib.getSwf("enemyfire"));
var test = myLib.getSwf("enemydead");
addChild(test);
test.x = 100;

trace(myLib.getXML("Xmlfile_0"));
trace(myLib.getXML("Xmlfile_1"));
   
trace(myLib.getText("Textfile_0"));
trace(myLib.getText("Textfile_1"));
   
trace(myLib.getBin("Binaryfile_0"));
trace(myLib.getBin("Binaryfile_1").length);
   
addChild(new Bitmap(myLib.getBitmapData("PNGfile")));
}//end of test

Step 6[Export Data files as compressed binary package]:
Export all resources as a single compressed binary file and protect your swf assets.
Add this line:
myLib.save("myLib.vLib"); 
in function "Libloaded".
You will get a binary file named "myLib.vLib" which contains all the resources.

Step 7[Use the compressed data file package]:
myLib.addEventListener(Event.COMPLETE, Libloaded);
myLib.init1("./DataFiles/myLib.vLib");
function Libloaded(event:Event):void
{
//All Resources Loaded!
test();
}
Or
Embed the file "myLib.vLib" as a binary asset:
[Embed(source="../bin/DataFiles/myLib.vLib",mimeType="application/octet-stream")]
private static const DataFiles_Class:Class;
myLib.load(new DataFiles_Class());
test();
You can use the data files directly after calling load().
If you Embed all resources using Embed tag or import them into .fla's library, all resources will be compiled into SWF tags. SWF decompilers can read those tags and extract the resources directly.
But in this step, because all resources are encoded into a single binary file, it's a good way to protect the swf assets.

Step 8[Export Data files as a .as class]:
Export .as Class with Embeds:
myLib.XML2AS("./DataFiles/Lib.xml", "myLibT" /*, "myPackageT"*/);
You will get a .as file named "myLibT.as" as follows:
package 
{
     import flash.display.*;
     import flash.media.*;
     import flash.text.*;
     import flash.utils.*;
//Export from vLib-v0.1 by Bruce Jawn (zhoubu1988@gmail.com)

     public class myLibT
     {
     [Embed(source="DataFiles/texts/Textfile_0.txt", mimeType="application/octet-stream")]
     private static const Textfile_0:Class;
     [Embed(source="DataFiles/texts/Textfile_1.txt", mimeType="application/octet-stream")]
     private static const Textfile_1:Class;
     [Embed(source="DataFiles/xmls/Xmlfile_0.xml", mimeType="application/octet-stream")]
     private static const Xmlfile_0:Class;
     [Embed(source="DataFiles/xmls/Xmlfile_1.xml", mimeType="application/octet-stream")]
     private static const Xmlfile_1:Class;
     [Embed(source="DataFiles/textures/JPGfile.JPG")]
     private static const JPGfile:Class;
     [Embed(source="DataFiles/textures/PNGfile.PNG")]
     private static const PNGfile:Class;
     [Embed(source="DataFiles/swfs/enemydead.swf")]
     private static const enemydead:Class;
     [Embed(source="DataFiles/swfs/enemyfire.swf")]
     private static const enemyfire:Class;
     [Embed(source="DataFiles/sounds/sound1.mp3")]
     private static const S0:Class;
     [Embed(source="DataFiles/sounds/sound2.mp3")]
     private static const S1:Class;
     [Embed(source="DataFiles/sounds/sound3.mp3")]
     private static const S2:Class;
     [Embed(source="DataFiles/bins/Binaryfile_0.bin", mimeType="application/octet-stream")]
     private static const Binaryfile_0:Class;
     [Embed(source="DataFiles/bins/Binaryfile_1.bin", mimeType="application/octet-stream")]
     private static const Binaryfile_1:Class;

        private function getClass(name:String):Class
        {
            var ClassReference:Class = getDefinitionByName("myLibT_"+name) as Class;
            return ClassReference;
        }      

  private function getInstance(name:String):Object
  {
   var ClassReference:Class = getClass(name);
   return new ClassReference();
  }
  public function getBin (name:String) : ByteArray
  {
   return ByteArray(getInstance(name));
  }
  
  public function getBitmapData (name:String) : BitmapData
  {
   return Bitmap(getInstance(name)).bitmapData;
  }

        public function getFont (name:String) : Class
  {
   return getClass(name);
   //return Font(getInstance(name));
   //Font.registerFont(EmbeddedFont); 
  }
  
  public function getSound (name:String) : Sound
  {
   return Sound(getInstance(name));
  }

  public function getSwf (name:String) : Sprite
  {
   return Sprite(getInstance(name));
  }

  public function getText (name:String) : String
  {
   return String(getInstance(name));
  }

  public function getXML (name:String) : XML
  {
   return XML(getInstance(name));
  }
 }//end of class 
}//end of package
Then you can import this class into your AS3 projects and compile everything into the final single SWF.

Links:
vLib.swc API references:
http://code.google.com/p/flaswf/wiki/vLib
vLib.swc v0.1 download:
http://flaswf.googlecode.com/svn/trunk/vLib/bin/
Source code of vLib.swc examples:
https://flaswf.googlecode.com/svn/trunk/vLib/examples

3 comments:

  1. Fantastic work!
    I'm using it in an AIR Project but I get an error related to LoaderContext when I try to call getSwf(). It would be great a method override for passing LoaderContext in getSwf().

    This is the error I get:

    SecurityError: Error #3226: Cannot import a SWF file when LoaderContext.allowCodeImport is false.

    ReplyDelete
    Replies
    1. Thanks for reporting, I may solve the problem in the future version. Currently, you can just embed the swf as binary data and use the "getBin" method to return the binary data, then create your custom Loader instance for passing the LoaderContext.

      Delete
  2. Ich genoss das Lesen Sie Ihre Blog-Post. Es hat eine Menge geholfen.
    Wenn möglich, da mehr Wissen kommt dem Weg, ich hoffe, Sie zu aktualisieren könnte, um mehr Informationen mit uns zu teilen. Es ist sehr hilfreich.
    Ich weiß auch von Informationsquellen, die Ihre Leser profitieren könnten, finden Sie Links unten
    BDSwiss

    ReplyDelete

Sponsors