From ee78c0ff33c4bb05276e2b1fc7ddea89bacb4f0b Mon Sep 17 00:00:00 2001 From: Joshua Granick Date: Thu, 2 Oct 2014 14:45:01 -0700 Subject: [PATCH] Use older SWF embed format (as Haxe rejects some valid files), but keeping the newer 'assets.swf' -swf-lib system to prevent memory problems --- templates/haxe/DefaultAssetLibrary.hx | 4 +- tools/helpers/FlashHelper.hx | 132 ++++++++++++++++++++++---- 2 files changed, 118 insertions(+), 18 deletions(-) diff --git a/templates/haxe/DefaultAssetLibrary.hx b/templates/haxe/DefaultAssetLibrary.hx index 098102f2e..5a7e27fb5 100644 --- a/templates/haxe/DefaultAssetLibrary.hx +++ b/templates/haxe/DefaultAssetLibrary.hx @@ -637,8 +637,8 @@ class DefaultAssetLibrary extends AssetLibrary { #if !display #if flash -//::foreach assets::::if (embed)::::if (type == "image")::@:keep class __ASSET__::flatName:: extends flash.display.BitmapData { public function new () { super (0, 0, true, 0); } }::else::@:keep class __ASSET__::flatName:: extends ::flashClass:: { }::end::::end:: -//::end:: +::foreach assets::::if (embed)::::if (type == "image")::@:keep @:bind class __ASSET__::flatName:: extends flash.display.BitmapData { public function new () { super (0, 0, true, 0); } }::else::@:keep @:bind class __ASSET__::flatName:: extends ::flashClass:: { }::end::::end:: +::end:: #elseif html5 diff --git a/tools/helpers/FlashHelper.hx b/tools/helpers/FlashHelper.hx index fd363806a..62faef19c 100644 --- a/tools/helpers/FlashHelper.hx +++ b/tools/helpers/FlashHelper.hx @@ -3,11 +3,11 @@ package helpers; //import openfl.text.Font; //import openfl.utils.ByteArray; -//import format.swf.Data; -//import format.swf.Constants; -//import format.swf.Reader; -//import format.swf.Writer; -//import format.wav.Data; +import format.swf.Data; +import format.swf.Constants; +import format.swf.Reader; +import format.swf.Writer; +import format.wav.Data; import haxe.io.Bytes; import haxe.io.Path; import helpers.LogHelper; @@ -24,10 +24,10 @@ import sys.io.FileSeek; class FlashHelper { - //private static var swfAssetID = 1000; + private static var swfAssetID = 1000; - /*private static function embedAsset (inAsset:Asset, packageName:String, outTags:Array) { + private static function embedAsset (inAsset:Asset, packageName:String, outTags:Array) { var embed = inAsset.embed; var name = inAsset.sourcePath; @@ -456,7 +456,7 @@ class FlashHelper { return true; - }*/ + } /*public static function embedAssets (targetPath:String, assets:Array , packageName:String = ""):Void { @@ -531,7 +531,101 @@ class FlashHelper { }*/ - private static function compileSWC (project:HXProject, embed:String, id:Int):Void { + private static function compileSWC (project:HXProject, assets:Array, id:Int):Void { + + var destination = project.app.path + "/flash/obj"; + PathHelper.mkdir (destination); + + var label = (id > 0 ? Std.string (id + 1) : ""); + + var swfVersions = [ 9, 10, /*10.1,*/ 10.2, 10.3, 11, 11.1, 11.2, 11.3, 11.4, 11.5, 11.6, 11.7, 11.8, 12, 13, 14 ]; + var flashVersion = 9; + + for (swfVersion in swfVersions) { + + if (project.app.swfVersion > swfVersion) { + + flashVersion++; + + } + + } + + var header:SWFHeader = + { + version : flashVersion, + compressed : true, + width : (project.window.width == 0 ? 800 : project.window.width), + height : (project.window.height == 0 ? 500 : project.window.height), + fps : project.window.fps * 256, + nframes : 2 + }; + + var tags = new Array (); + var packageName = ""; + var inserted = false; + + tags.push (TBackgroundColor (project.window.background)); + tags.push (TShowFrame); + + // Might generate ABC later, so we don't need the @:bind calls in DefaultAssetLibrary? + + /*var abc = new haxe.io.BytesOutput (); + var abcWriter = new format.abc.Writer (abc); + + for (asset in assets) { + + var classDef:ClassDef = { + var name : packageName + "__ASSET__" + asset.flatName; + var superclass : asset.flashClass; + var interfaces : []]; + var constructor : null; + var fields : []; + var namespace : null; + var isSealed : false; + var isFinal : false; + var isInterface : false; + var statics : []; + var staticFields : []; + } + abcWriter.writeClass (classDef); + + }*/ + + for (asset in assets) { + + try { + + if (asset.type != AssetType.TEMPLATE && embedAsset (asset, packageName, tags)) { + + inserted = true; + + } + + } catch (e:Dynamic) { + + Sys.println ("Error embedding \"" + asset.sourcePath + "\": " + e); + + } + + } + + tags.push (TShowFrame); + + if (inserted) { + + var swf:SWF = { header: header, tags: tags }; + var output = File.write (destination + "/assets.swf", true); + var writer = new Writer (output); + writer.write (swf); + output.close (); + + } + + } + + + /*private static function compileSWC (project:HXProject, embed:String, id:Int):Void { var destination = project.app.path + "/flash/obj"; PathHelper.mkdir (destination); @@ -575,12 +669,13 @@ class FlashHelper { } - } + }*/ public static function embedAssets (project:HXProject):Bool { var embed = ""; + var assets = []; var maxSize = 1024 * 1024 * 16; var currentSize = 0; var id = 0; @@ -651,15 +746,17 @@ class FlashHelper { } else { - if (currentSize + stat.size >= maxSize) { + /*if (currentSize + stat.size >= maxSize) { - compileSWC (project, embed, id); + //compileSWC (project, embed, id); + compileSWC (project, assets, id); id++; currentSize = 0; embed = ""; + assets = []; - } + }*/ currentSize += stat.size; @@ -685,6 +782,8 @@ class FlashHelper { } else { + assets.push (asset); + if (asset.type == IMAGE) { embed += "@:keep " + tagName + "('" + sourcePath + "') class __ASSET__" + asset.flatName + " extends " + flashClass + " { public function new () { super (0, 0, true, 0); } }\n"; @@ -703,7 +802,8 @@ class FlashHelper { if (embed != "") { - compileSWC (project, embed, id); + //compileSWC (project, embed, id); + compileSWC (project, assets, id); } @@ -731,11 +831,11 @@ class FlashHelper { } - /*private static function nextAssetID () { + private static function nextAssetID () { return swfAssetID++; - }*/ + } public static function run (project:HXProject, workingDirectory:String, targetPath:String):Void {