From 2a4aa2bfee9eaa2cec2015f080cee8e08771dcd8 Mon Sep 17 00:00:00 2001 From: Joshua Granick Date: Wed, 15 Nov 2017 16:40:02 -0800 Subject: [PATCH] Don't pack sounds on HTML5, fix cache break --- lime/tools/helpers/AssetHelper.hx | 412 +++++++++++++++++------------- lime/utils/PackedAssetLibrary.hx | 17 ++ 2 files changed, 246 insertions(+), 183 deletions(-) diff --git a/lime/tools/helpers/AssetHelper.hx b/lime/tools/helpers/AssetHelper.hx index b25a46e84..bed09bb34 100644 --- a/lime/tools/helpers/AssetHelper.hx +++ b/lime/tools/helpers/AssetHelper.hx @@ -13,6 +13,7 @@ import lime.utils.compress.Deflate; import lime.utils.compress.GZip; import lime.utils.AssetManifest; import sys.io.File; +import sys.io.FileOutput; import sys.FileSystem; @@ -26,11 +27,8 @@ class AssetHelper { var manifest = new AssetManifest (); var pathGroups = new Map> (); - var size, soundName; - var assetData:Dynamic; var libraries = new Map (); - if (library == null) library = DEFAULT_LIBRARY_NAME; for (lib in project.libraries) { @@ -39,106 +37,18 @@ class AssetHelper { } + var assetData; + for (asset in project.assets) { - if ((asset.library != null && asset.library != library) || asset.type == TEMPLATE) continue; - if (asset.library == null && library != DEFAULT_LIBRARY_NAME) continue; + assetData = getAssetData (project, pathGroups, libraries, library, asset); - size = 100; - - if (FileSystem.exists (asset.sourcePath)) { + if (assetData != null) { - size = FileSystem.stat (asset.sourcePath).size; + manifest.assets.push (assetData); } - assetData = { - - id: asset.id, - size: size, - type: Std.string (asset.type) - - }; - - if (project.target == FLASH || project.target == AIR) { - - if (asset.embed != false || asset.type == FONT) { - - assetData.className = "__ASSET__" + asset.flatName; - - } else { - - assetData.path = asset.resourceName; - - } - - if (asset.embed == false && asset.library != null && libraries.exists (asset.library)) { - - assetData.preload = libraries[asset.library].preload; - - } - - } else if (project.target == HTML5) { - - if (asset.type == FONT) { - - assetData.className = "__ASSET__" + asset.flatName; - assetData.preload = true; - - } else { - - assetData.path = asset.resourceName; - - if (asset.embed != false || (asset.library != null && libraries.exists (asset.library) && libraries[asset.library].preload)) { - - assetData.preload = true; - - } - - if (asset.type == MUSIC || asset.type == SOUND) { - - soundName = Path.withoutExtension (assetData.path); - - if (!pathGroups.exists (soundName)) { - - pathGroups.set (soundName, [ assetData.path ]); - - } else { - - pathGroups[soundName].push (assetData.path); - Reflect.deleteField (assetData, "preload"); - - } - - Reflect.deleteField (assetData, "path"); - assetData.pathGroup = pathGroups[soundName]; - - } - - } - - } else { - - if (project.target == EMSCRIPTEN && (asset.embed != false || (asset.library != null && libraries.exists (asset.library) && libraries[asset.library].preload))) { - - assetData.preload = true; - - } - - if (asset.embed == true || asset.type == FONT) { - - assetData.className = "__ASSET__" + asset.flatName; - - } else { - - assetData.path = asset.resourceName; - - } - - } - - manifest.assets.push (assetData); - } if (targetPath != null) { @@ -196,6 +106,216 @@ class AssetHelper { } + private static function getAssetData (project:HXProject, pathGroups:Map>, libraries:Map, library:String, asset:Asset):Dynamic { + + if ((asset.library != null && asset.library != library) || asset.type == TEMPLATE) return null; + if (asset.library == null && library != DEFAULT_LIBRARY_NAME) return null; + + var size = 100; + + if (FileSystem.exists (asset.sourcePath)) { + + size = FileSystem.stat (asset.sourcePath).size; + + } + + var assetData:Dynamic = { + + id: asset.id, + size: size, + type: Std.string (asset.type) + + }; + + if (project.target == FLASH || project.target == AIR) { + + if (asset.embed != false || asset.type == FONT) { + + assetData.className = "__ASSET__" + asset.flatName; + + } else { + + assetData.path = asset.resourceName; + + } + + if (asset.embed == false && asset.library != null && libraries.exists (asset.library)) { + + assetData.preload = libraries[asset.library].preload; + + } + + } else if (project.target == HTML5) { + + if (asset.type == FONT) { + + assetData.className = "__ASSET__" + asset.flatName; + assetData.preload = true; + + } else { + + assetData.path = asset.resourceName; + + if (asset.embed != false || (asset.library != null && libraries.exists (asset.library) && libraries[asset.library].preload)) { + + assetData.preload = true; + + } + + if (asset.type == MUSIC || asset.type == SOUND) { + + var soundName = Path.withoutExtension (assetData.path); + + if (!pathGroups.exists (soundName)) { + + pathGroups.set (soundName, [ assetData.path ]); + + } else { + + pathGroups[soundName].push (assetData.path); + Reflect.deleteField (assetData, "preload"); + + } + + Reflect.deleteField (assetData, "path"); + assetData.pathGroup = pathGroups[soundName]; + + } + + } + + } else { + + if (project.target == EMSCRIPTEN && (asset.embed != false || (asset.library != null && libraries.exists (asset.library) && libraries[asset.library].preload))) { + + assetData.preload = true; + + } + + if (asset.embed == true || asset.type == FONT) { + + assetData.className = "__ASSET__" + asset.flatName; + + } else { + + assetData.path = asset.resourceName; + + } + + } + + return assetData; + + } + + + private static function getPackedAssetData (project:HXProject, output:FileOutput, pathGroups:Map>, libraries:Map, library:Library, asset:Asset):Dynamic { + + if (project.target == HTML5 && (asset.type == MUSIC || asset.type == SOUND || asset.type == FONT)) { + + return getAssetData (project, pathGroups, libraries, library.name, asset); + + } + + if (asset.type == TEMPLATE) return null; + if (asset.library == library.name || (asset.library == null && library.name == DEFAULT_LIBRARY_NAME)) { + + var assetData:Dynamic = { + + id: asset.id, + size: 0, + type: Std.string (asset.type), + position: output.tell () + + }; + + if (project.target == HTML5 && asset.type == FONT) { + + assetData.className = "__ASSET__" + asset.flatName; + assetData.preload = true; + + } else { + + switch (library.type) { + + case "deflate", "zip": + + if (asset.data != null) { + + output.writeBytes (Deflate.compress (asset.data), 0, asset.data.length); + + } else if (asset.sourcePath != null) { + + var tempBytes = File.getBytes (asset.sourcePath); + tempBytes = Deflate.compress (tempBytes); + output.writeBytes (tempBytes, 0, tempBytes.length); + + } + + case "gzip": + + if (asset.data != null) { + + output.writeBytes (GZip.compress (asset.data), 0, asset.data.length); + + } else if (asset.sourcePath != null) { + + var tempBytes = File.getBytes (asset.sourcePath); + tempBytes = GZip.compress (tempBytes); + output.writeBytes (tempBytes, 0, tempBytes.length); + + } + + default: + + if (asset.data != null) { + + output.writeBytes (asset.data, 0, asset.data.length); + + } else if (asset.sourcePath != null) { + + var input = File.read (asset.sourcePath, true); + output.writeInput (input); + input.close (); + + } + + } + + } + + if (project.target == HTML5 && asset.type == IMAGE) { + + assetData.preload = true; + + } + + var position = output.tell (); + assetData.length = position - assetData.position; + + asset.library = library.name; + + // asset.sourcePath = ""; + + if (project.target != HTML5 || asset.type != FONT) { + + asset.targetPath = null; + + } + + asset.data = null; + + return assetData; + + } else { + + return null; + + } + + } + + private static function isPackedType (type:String) { return switch (type) { @@ -396,7 +516,14 @@ class AssetHelper { var type, asset, cacheAvailable, cacheDirectory, filename; var output, manifest, position, assetData:Dynamic, input; var embeddedLibrary = false; - var tempBytes; + + var pathGroups = new Map> (); + var libraries = new Map (); + for (lib in project.libraries) { + + libraries[lib.name] = lib; + + } for (library in project.libraries) { @@ -433,97 +560,16 @@ class AssetHelper { try { + var assetData; + for (asset in project.assets) { - if (asset.library == library.name || (asset.library == null && library.name == DEFAULT_LIBRARY_NAME)) { - - assetData = { - - id: asset.id, - size: 0, - type: Std.string (asset.type), - position: position - - }; - - if (project.target == HTML5 && asset.type == FONT) { - - assetData.className = "__ASSET__" + asset.flatName; - assetData.preload = true; - - } else { - - switch (library.type) { - - case "deflate", "zip": - - if (asset.data != null) { - - output.writeBytes (Deflate.compress (asset.data), 0, asset.data.length); - - } else if (asset.sourcePath != null) { - - tempBytes = File.getBytes (asset.sourcePath); - tempBytes = Deflate.compress (tempBytes); - output.writeBytes (tempBytes, 0, tempBytes.length); - - } - - case "gzip": - - if (asset.data != null) { - - output.writeBytes (GZip.compress (asset.data), 0, asset.data.length); - - } else if (asset.sourcePath != null) { - - tempBytes = File.getBytes (asset.sourcePath); - tempBytes = GZip.compress (tempBytes); - output.writeBytes (tempBytes, 0, tempBytes.length); - - } - - default: - - if (asset.data != null) { - - output.writeBytes (asset.data, 0, asset.data.length); - - } else if (asset.sourcePath != null) { - - input = File.read (asset.sourcePath, true); - output.writeInput (input); - input.close (); - - } - - } - - } - - if (project.target == HTML5 && asset.type == IMAGE) { - - assetData.preload = true; - - } - - position = output.tell (); - assetData.length = position - assetData.position; + assetData = getPackedAssetData (project, output, pathGroups, libraries, library, asset); + + if (assetData != null) { manifest.assets.push (assetData); - asset.library = library.name; - - // asset.sourcePath = ""; - - if (project.target != HTML5 || asset.type != FONT) { - - asset.targetPath = null; - - } - - asset.data = null; - } } diff --git a/lime/utils/PackedAssetLibrary.hx b/lime/utils/PackedAssetLibrary.hx index 92823b434..3475491ca 100644 --- a/lime/utils/PackedAssetLibrary.hx +++ b/lime/utils/PackedAssetLibrary.hx @@ -67,6 +67,12 @@ class PackedAssetLibrary extends AssetLibrary { public override function getAudioBuffer (id:String):AudioBuffer { + #if (js && html5) + + return super.getAudioBuffer (id); + + #else + if (cachedAudioBuffers.exists (id)) { return cachedAudioBuffers.get (id); @@ -82,6 +88,8 @@ class PackedAssetLibrary extends AssetLibrary { } + #end + } @@ -281,6 +289,7 @@ class PackedAssetLibrary extends AssetLibrary { } else { var path = paths.exists (id) ? paths.get (id) : id; + path = __cacheBreak (path); Bytes.loadFromFile (path).onError (promise.error).onComplete (packedData_onComplete); @@ -295,6 +304,12 @@ class PackedAssetLibrary extends AssetLibrary { public override function loadAudioBuffer (id:String):Future { + #if (js && html5) + + return super.loadAudioBuffer (id); + + #else + if (cachedAudioBuffers.exists (id)) { return Future.withValue (cachedAudioBuffers.get (id)); @@ -310,6 +325,8 @@ class PackedAssetLibrary extends AssetLibrary { } + #end + }