AIR3加载目录到内存

时间:2011-12-18 16:41:09

标签: actionscript-3 air

我正在尝试动态加载mp3,xml,png,fnt,ttf文件(通过扫描目录) - 本质上是尝试在运行时模拟Embed标记。或者换一种方式 - 拥有同步文件系统对象。

有没有一种很好的方法可以完成所有这些不同的文件类型?

这是一个不起作用的例子。

public class FileSystemAssets 
{
    public var assets : Object = new Object();

    public static function sanitize( filename : String ): String
    {
        return filename.replace(/[.\/\\\:]/g, '_');
    }

    private function loadDirectory(dir : File): void 
    {
        for each ( var f : File in dir.getDirectoryListing() )
        {
            if ( f.isDirectory )
                loadDirectory( f )
            else if ( !f.isSymbolicLink && !f.isPackage )
                loadFile( f );
        }
    }

    private function loadFile(file : File ): void 
    {
        if ( ".mp3.xml.png.swf.fnt.ttf".indexOf(file.extension) == 0 ) 
            return;
        //
        // how to do this part properly???
        //
        var ldr : Loader = new Loader();
        var fs : FileStream = new FileStream();
        fs.open( file, FileMode.READ );
        var bytes : ByteArray = new ByteArray();
        fs.readBytes(bytes);
        ldr.loadBytes(bytes);

        // assets[ sanitize( file.nativePath ) ] = ldr.content;
        // content should be probably converted
    }

    public function getAsset( filename : String ): Object
    {
        if( assets[ sanitize(filename) ] == undefined )
            return null;
        return assets[ sanitize(filename) ];
    }

    public function reload(rootDir : String): void
    {
        assets = new Object(); // clear old stuff
        var root : File = new File( rootDir );
        loadDirectory( root );
    }
}

其他说明:在这种情况下,安全/内存/速度不是问题。所有加载的数据都可以被认为是可信的,并且足够小以适应内存。

1 个答案:

答案 0 :(得分:0)

这很有效。我手动包含其他文件,因为这些文件不会经常更改。

public class FileSystemAssets 
{
    private var assets : Object = new Object();

    public function FileSystemAssets() {
    }

    public static function sanitize( filename : String ): String {
        return filename.replace(/[.\/\\\:]/g, '_');
    }        

    private function loadDirectory(dir : File): void {
        for each ( var f : File in dir.getDirectoryListing() )
        {
            if ( f.isDirectory )
                loadDirectory( f )
            else if ( !f.isSymbolicLink && !f.isPackage )
                loadFile( f );
        }
    }

    private function loadFile(file : File ): void {
        if ( ".mp3.png.xml.".indexOf(file.extension) < 0 )
            return;
        var relativePath : String = file.nativePath.replace( Constants.DataDir, "" );
        var name : String = sanitize(relativePath);

        function onLoaded(e : Event) : void {
            if ( file.extension == "xml" ) {
                assets[ name ] = XML( e.target.data );
            } else {
                assets[ name ] = e.target.data;
            }
        }

        function onMP3Loaded( e : Event ): void {
            assets[ name ] = e.target;
        }

        function onPNGLoaded( e : Event ): void {
            var bitmap : Bitmap = e.target.content;
            assets[ name ] = Texture.fromBitmapData( bitmap.bitmapData );
        }

        var req : URLRequest = new URLRequest( file.url );

        if ( file.extension == "png" ) {
            var pngldr : Loader = new Loader();
            pngldr.contentLoaderInfo.addEventListener( Event.COMPLETE, onPNGLoaded );
            pngldr.load( req );
        } else if ( file.extension == "mp3" ) {
            var mp3ldr : Sound = new Sound();
            mp3ldr.addEventListener( Event.COMPLETE, onMP3Loaded );
            mp3ldr.load(req);
        } else {
            var ldr : URLLoader = new URLLoader();
            ldr.addEventListener( Event.COMPLETE, onLoaded );
            ldr.dataFormat = URLLoaderDataFormat.BINARY;
            ldr.load(req);
        }
    }

    public function reload(): void {
        var root : File = new File( Constants.DataDir );
        loadDirectory( root );
    }

    public function getAsset( name : String ): Object {
        if ( assets[ name ] == undefined )
            return null;
        return assets[ name ];
    }
}