Professional, Software

Pax-Reflector Plugin

Technorati Tags: ,

Logical progression for PAX was build some kind of UI around it. I thought of writing my UI but then I realized the building  a UI for assembly parts and letting people look at the code inside it is not a small task and there is already a tool for it i.e. .Net Reflector. There is even nice Add-in model that lets you extend reflector functionality. Here are few things I found that were helpful for me to create this add-in.

This is add-in is pretty simple. It adds a menu item to File menu. Open Xap menu launches a OpenFileDialog that lets user pick a Xap file that they would like to see.

Fileopen

When user picks the file, it opens the xap in tree view structure. User can then expand them to see individual files inside it.

treeview expandedtreeview

User can click on some of the files to see the content. E.g. for Xaml File text is shown. For image, image is shown and for Media files, media is played.

xamlview mediaview

Here is the source code for the project.

Standard
Professional, Software

Pax

Silverlight app file (.XAP) is essentially a zip file. For testing purposes, many times we needed to look inside the XAP file from customers to see what was causing an issue.

It is possible to day to see what exactly is inside the .Xap file today but it involves few  steps….

  1. Extract all the contents out of Zip file
  2. you should be having bunch of resource files (.mpg, jpg, .wmv etc) and assemblies (.dll) and Application manifest (app.xaml)
  3. Then most of the pages etc are embedded in the assembly so now you can use one of the tools (such as ILDASM) to extract resources out of there to see what is embedded in side assembly

In short it was kind of painful process. I wanted to write some utility assembly that exposes all this information out of Xap in a easier way. These utility APIs could be used to build UI around it.

[Before we get into details of PAX, big Thanks to Eduardo for helping me cleanup the code and fix some of the issues]

There are two DLLs….

ArchiveModel

This DLL contains classes and APIs that read the Zip (Deflate) format that is used for creating .Xap files for silverlight. It has following classes

image

Archive

This class represents a zip file. It exposes the Files collection that are inside zip as well as ReadFiles method that reads individual files (stored in Files Collection).

ArchiveFile

This class represents individual files inside Zip file. Every ArchiveFile has ArchiveFileHeader as well as method ReadFile to read the file. ReadFile method is actuall what decompresses the content using DeflateStream class.

ArchiveFileHeader

Each file in the zip format has a header. Header contains lot of information that is used to read the actual content of the file. Header exposes properties such as Compressed/Uncompressed size, last modified date, filename etc

ByteReader

This is a utility class that converts the array of Bytes into types that are needed for PAX.

XapModel

This DLL that contains classes and APIs that introduces the concept of XAP. e.g. Every XAP has a manifest file, it can contain assemblies and resources etc. It has following classes

image

image

XapArchive

This class represents individual XAP file. It exposes Files collection that contains individual files inside Xap. It also exposes property for manifest.

File and its Sub classes

This class represents generic file inside a xap. File is a base class for different type of files (assemblyfile, manifestfile, xamlfile etc). File can be inside xap or inside assembly. Currently only ManifestFile class implements validation logic using Schemas but in general every file class could have some validation logic. That is the reason why VideoFile/AudioFile classes exists but are empty today.

FileProperties

This class represents the metadata about a file in xap such as  compressed size, uncompressed size etc

Use of Pax

Here is a very simple way I can use the PAX to list the properties of files inside a given xap.

using System;
using XapModel;
using System.IO;

namespace PaxConsoleTest
{
    class Program
    {
        static XapArchive _xap;

        static void Main(string[] args)
        {
            using (FileStream fs = new FileStream("TestPax.xap", FileMode.Open))
            {
                _xap = new XapArchive(fs);
                WriteFileProperties(_xap.Manifest);
                foreach (XapModel.FileTypes.File file in _xap.Files)
                {
                    WriteFileProperties(file);
                    if (file is XapModel.FileTypes.AssemblyFile)
                    {
                        foreach (XapModel.FileTypes.File embeddedfile in 
((XapModel.FileTypes.AssemblyFile)file).Resources) { WriteFileProperties(embeddedfile); } } } } } private static void WriteFileProperties(XapModel.FileTypes.File file) { Console.WriteLine(); Console.WriteLine("************************************"); Console.WriteLine( "File Name: " + file.FileProperties.FileName + " Last Modified: " + file.FileProperties.LastModified); Console.WriteLine( "Compressed Size: " + file.FileProperties.CompressedSize + " Uncompressed Size: " + file.FileProperties.UncompressedSize); Console.Write("************************************"); Console.WriteLine(); } } }

which produces output such as

************************************

File Name: AppManifest.xaml         Last Modified: 10/22/2008 10:06:14 PM

Compressed Size: 300         Uncompressed Size: 337

************************************

************************************

File Name: TestPax.dll         Last Modified: 10/22/2008 10:06:14 PM

Compressed Size: 21189226         Uncompressed Size: 21720064

************************************

************************************

File Name: TestPax.g.resources/page.xaml         Last Modified: 1/1/0001 12:00:00 AM

Compressed Size: 0         Uncompressed Size: 286

************************************

************************************

File Name: TestPax.g.resources/asmembedded/asmembeddedvideowmv.wmv         Last Modified: 1/1/0001

2:00:00 AM

Compressed Size: 0         Uncompressed Size: 4045744

************************************

************************************

File Name: TestPax.g.resources/asmembedded/asmembdimgjpeg.jpeg         Last Modified: 1/1/0001 12:

:00 AM

Compressed Size: 0         Uncompressed Size: 3495

Technorati Tags: ,,,

Here is the link to code to code for Pax

Standard