From 5d081ced5f62d547c13c0b0bced4c0819be484d6 Mon Sep 17 00:00:00 2001 From: Joshua Granick Date: Mon, 13 Nov 2017 16:28:50 -0800 Subject: [PATCH] Rough pass at enabling asset pak libraries --- lime/tools/helpers/AssetHelper.hx | 134 ++++++++++++++++++++++++++++++ lime/tools/helpers/FileHelper.hx | 2 +- lime/tools/helpers/PathHelper.hx | 2 + 3 files changed, 137 insertions(+), 1 deletion(-) diff --git a/lime/tools/helpers/AssetHelper.hx b/lime/tools/helpers/AssetHelper.hx index 81c58f763..0c12f3c91 100644 --- a/lime/tools/helpers/AssetHelper.hx +++ b/lime/tools/helpers/AssetHelper.hx @@ -227,6 +227,7 @@ class AssetHelper { } var handlers = new Array (); + var hasPak = false; var type; for (library in project.libraries) { @@ -250,6 +251,10 @@ class AssetHelper { library.type = type; + } else if (type == "pak") { + + hasPak = true; + } } @@ -314,6 +319,12 @@ class AssetHelper { } + if (hasPak) { + + processPakLibraries (project, targetDirectory); + + } + var manifest, asset; for (library in project.libraries) { @@ -366,4 +377,127 @@ class AssetHelper { } + public static function processPakLibraries (project:HXProject, targetDirectory:String = null):Void { + + var asset, cacheAvailable, cacheDirectory, filename; + var output, manifest, position, assetData:Dynamic, input; + var embeddedPak = false; + + for (library in project.libraries) { + + if (library.type == "pak") { + + // TODO: Support library.embed=true by embedding all the assets instead of packing + + cacheAvailable = false; + cacheDirectory = null; + + if (targetDirectory != null) { + + manifest = new AssetManifest (); + + cacheDirectory = targetDirectory + "/obj/libraries/"; + filename = library.name + ".pak"; + + // TODO: Support caching + + PathHelper.mkdir (cacheDirectory); + + if (FileSystem.exists (cacheDirectory + filename)) { + + FileSystem.deleteFile (cacheDirectory + filename); + + } + + output = File.write (cacheDirectory + filename, true); + position = 0; + + try { + + 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 (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); + + } + + position = output.tell (); + trace (position); + assetData.length = position - assetData.position; + + manifest.assets.push (assetData); + + asset.library = library.name; + + // asset.sourcePath = ""; + asset.targetPath = null; + asset.data = null; + + } + + } + + } catch (e:Dynamic) { + + output.close (); + FileSystem.deleteFile (cacheDirectory + filename); + + neko.Lib.rethrow (e); + + } + + output.close (); + + var libraryAsset = new Asset (cacheDirectory + filename, "lib/" + filename, AssetType.BINARY); + libraryAsset.library = library.name; + project.assets.push (libraryAsset); + + var data = new Asset ("", "manifest/" + library.name + ".json", AssetType.MANIFEST); + data.library = library.name; + manifest.libraryType = "lime.utils.AssetPakLibrary"; + manifest.libraryArgs = [ "lib/" + filename ]; + data.data = manifest.serialize (); + data.embed = true; + + project.assets.push (data); + embeddedPak = true; + + } + + if (library.name == DEFAULT_LIBRARY_NAME) { + + library.preload = true; + + } + + } + + } + + if (embeddedPak) { + + project.haxeflags.push ("lime.utils.AssetPakLibrary"); + + } + + } + + } \ No newline at end of file diff --git a/lime/tools/helpers/FileHelper.hx b/lime/tools/helpers/FileHelper.hx index add7554ed..1a76827f8 100644 --- a/lime/tools/helpers/FileHelper.hx +++ b/lime/tools/helpers/FileHelper.hx @@ -255,7 +255,7 @@ class FileHelper { public static function copyIfNewer (source:String, destination:String) { - + //allFiles.push (destination); if (!isNewer (source, destination)) { diff --git a/lime/tools/helpers/PathHelper.hx b/lime/tools/helpers/PathHelper.hx index 7f973c35f..de966c78f 100644 --- a/lime/tools/helpers/PathHelper.hx +++ b/lime/tools/helpers/PathHelper.hx @@ -676,6 +676,8 @@ class PathHelper { public static function standardize (path:String, trailingSlash:Bool = false):String { + if (path == null) return null; + path = StringTools.replace (path, "\\", "/"); path = StringTools.replace (path, "//", "/"); path = StringTools.replace (path, "//", "/");