views:

128

answers:

4

We are making a system that has a main swf for the application, and loads separate tools from separate swfs -- there will be versioning issues in the future since the separate swfs are coming from a cms (especially now since we're still developing, but also in the future when other developers might create an incompatible tool). I'm trying hard to prevent them as much as possible but I'd really like to be able to display a message to the user of the system when an incompatible swf is loaded.

This would mean that we need to catch that VerifyError or at least determine the loading failed for some reason - I've got no idea how to handle that at the moment. I suspect it might be possible using 10.1 and the uncaughtError system, but we are currently targeting flash player 10. Does anyone have a good idea? (we are already handling IOErrorEvent.IO_ERROR)

UPDATE: I've built a solution that scans the bytecode before importing, looks like that will work. I'll post the solution later.

A: 

I've worked with that kind of application in the past but I think it would be better to fix the SWF loaded rather than handling VerifyError. VeriyError indicates that the SWF loaded is corrupted or malformed.

And it's natural that the SWF itself is malformed rather than that the SWF is corrupted during the transfer. I guess you are trying to load png or other format named ".swf" or the SWF is generated by some software other than Flex compiler or Flash such as swfmill(In the latter case, there would be a bug in that software).

9re
thanks for your response, however, 'fixing' the swf is not an option since I won't be the one making it - I'm ok with it not working, I just want to handle the error in a sensible way. (If I kan handle the verifyerror I can display a 'this thing is not working anymore' error message)
Simon Groenewolt
A: 

I think there is a way to workaround this issue.

  1. load the swf using a URLLoader or URLStream into a ByteArray.
  2. Use any open source library for parsing SWF binary like this or this.
  3. check if it verifies that the whole byte array represents valid SWF file.
  4. If above test succeeds then load this ByteArray into loader using loadBytes method.
  5. Else show user that this is not working.

Disclaimer: A binary file can be a valid SWF still might not be render-able, but with this you can discard all the invalid SWFs or any other formats whose extension are changed to swf.

bhups
I'm not to worried about it not being a swf file - but how would I detect a swf has dependencies on classes that are not available in the loading swf? The verifyerror generally pops up because of missing dependencies my case.
Simon Groenewolt
+1  A: 

Hi there,

The best way to do this is by using one of the libraries bhups suggested. I used senocular's for the next example. Also, because the senocular's library provides only basic operations for the parsed SWF you may need the SWF Format Spec (adobe.com/devnet/swf/pdf/swf_file_format_spec_v10.pdf) to get the info you want out of the loaded SWF.

The next example lists all the class names from a loaded SWF:

package swf
{
 import flash.events.Event;
 import flash.net.URLRequest;
 import flash.net.URLStream;
 import flash.utils.ByteArray;
 import flash.utils.Endian;

 import swf.SWFReader;

 public class GetSWFInfo
 {

  private var swfInfo:SWFReader;

  public function GetSWFInfo()
  {
   var urlRequest:URLRequest = new URLRequest("theswf.swf");
   var loader:URLStream = new URLStream();   
   loader.load(urlRequest);
   loader.addEventListener(Event.COMPLETE, onComplete);
  }


  public function onComplete(e:Event):void {
   var recivedByteArray :ByteArray = new ByteArray();
   URLStream(e.currentTarget).readBytes(recivedByteArray);


   //create a new instance of SWFReader
   swfInfo = new SWFReader();
   //readTag it's a callback function that will be called when a tag is read during the SWF parse process.
   //read more on tags in the SWF specification document
   swfInfo.tagCallback =  readTag;
   //start parsing
   swfInfo.parse(recivedByteArray); 
  }



  public function readTag(tag:uint, bytes:ByteArray):void {


   //76 it's the tag type for SymbolClass tag
   //read more in the SWF specification document
   if (76 == tag) {


    var classesArray:Array = new Array();
    var symbolsNumber:uint = 0;
    var currentId:uint = 0;

    bytes.endian = Endian.LITTLE_ENDIAN;

    //read the symbols Number
    //again read more in the SWF specification document
    symbolsNumber = bytes.readShort();

    bytes.position = 4;

    while (true) {

     var i:uint = bytes.position;

     //every string name ends with a null byte
     //again read more in the SWF specification document
     while(bytes[i] != 0) i++;

     var readAmount:uint = i - bytes.position;

     classesArray.push(bytes.readUTFBytes(readAmount));

     //the last ID is always the base class Id, and it's 0
     currentId=bytes.readUnsignedShort();

     bytes.position++;     

     if (currentId==0) {
      break;
     }
    }

    //this two should be equal
    trace(classesArray.length + 1);//the number of elements in the classesArray
    trace(symbolsNumber);//the number of classes retrived from the SWF

    //list the names
    var name:String;
    for each (name in classesArray) {
     trace(name);
    }

    //now you have an array with all the class names that you can use to compare

   }
  }
 }

}

mindnoise
Thanks for your suggestion, but how will this help me prevent a VerifyError? I don't see a way to find the classes a swf _depends_ on -- just being able to list the classnames in the swf does not help me, or am I missing something?
Simon Groenewolt
Well, if you load the swf with URLStream you will get no Error that's for sure. Then you can examine the content, see if the it's valid, ApplicationDomain.hasDefinition and ApplicationDomain. getDefinition may come handy here (you can use this methods without parsing the swf as well, but parsing the swf gives you the power to read any info you need from it)This is how I would do it. I mean maybe I don't understand the question, but if you want to make sure that 3rd party SWFs have the classes you need to use in them, I think this is the way.
mindnoise
What I need is the other way around - a 3rd party swf is depending on a class that should be in my swf -- if that class is missing I get the VerifyError. This is why I need to know the classes it needs but does not have itself, I'll see what I can find using the swfreader, but I suspect I have to read the ABC to find out the info I need.
Simon Groenewolt
+1  A: 

Hi!

I did misunderstand what you are trying to do.

Well, actually, I guess there is no handler for verify error and to detect it, you have to fight with byte-codes.

By the way, I have and idea which is not the very answer for your question but may helps you.

a 3rd party swf is depending on a class that should be in my swf -- if that class is missing I get the VerifyError.

From this point, I can advice that if you link the 'missing class' into your swf and load the 3rd party swf into ApplicationDomain.currentDomain or new ApplicationDomain(ApplicationDomain.currentDomain), you can avoid the 'Verify Error'. (This is because the flash player will find the diffinition of the missing class in the parent swf.)

Here is my sample code which loads a swf with verify error(http://teionclub.com/test/xml/main.swf).

Avoiding VerifyError - wonderfl build flash online
9re
Thanks for your suggestion, your suggestion works if the third party swf is known and I only have to fix a couple of them. Unfortunately this is not the case. This weekend I built a solution for my own problem that scans the avm2 bytecode and checks all classes used in the class hierarchy. I'll post my solution later.
Simon Groenewolt
Wow! increadible!!That's must be a great effort!!
9re