From 31af41e69c15da96c8eae8490e1c01aac0a5168d Mon Sep 17 00:00:00 2001 From: Joseph Cloutier Date: Sun, 31 Aug 2025 22:07:10 -0400 Subject: [PATCH] Unify asset copying code, and standardize asset embedding. Having eight separate implementations made it harder to maintain. While an asset embedding bug got fixed on desktop targets, it got overlooked on others. This lead to assets being included twice on those other targets: once embedded, and once normally. (#1898) Now, that behavior is controlled from one place, and the bug is fixed for eight targets at once. This also standardizes the case of `asset.embed == true && asset.type == TEMPLATE`. Previously, some targets would prioritze `embed` over `type`, embedding the template into the app without even processing it as a template. Now, it will always prioritize `type`, processing templates as templates and never trying to embed them. Three targets are left out since they have more complex asset embedding behavior. I haven't checked if the bug is present on any of those. --- src/lime/tools/PlatformTarget.hx | 44 +++++++++++++++++++++++ tools/platforms/AndroidPlatform.hx | 49 +++----------------------- tools/platforms/IOSPlatform.hx | 38 ++------------------ tools/platforms/LinuxPlatform.hx | 30 +--------------- tools/platforms/MacPlatform.hx | 28 +-------------- tools/platforms/TVOSPlatform.hx | 36 +------------------ tools/platforms/TizenPlatform.hx | 38 ++------------------ tools/platforms/WebAssemblyPlatform.hx | 37 +------------------ tools/platforms/WindowsPlatform.hx | 30 +--------------- 9 files changed, 58 insertions(+), 272 deletions(-) diff --git a/src/lime/tools/PlatformTarget.hx b/src/lime/tools/PlatformTarget.hx index fe4ea957d..617d94cfd 100644 --- a/src/lime/tools/PlatformTarget.hx +++ b/src/lime/tools/PlatformTarget.hx @@ -151,6 +151,8 @@ class PlatformTarget } } + // Command implementations + @ignore public function build():Void {} @ignore public function clean():Void {} @@ -173,6 +175,48 @@ class PlatformTarget @ignore public function watch():Void {} + // Common functionality used by subclasses + + private function copyProjectAssets(outputDirectory:String, assetDirectory:String = null) + { + if (assetDirectory == null) + { + assetDirectory = outputDirectory; + } + else if (!StringTools.startsWith(assetDirectory, targetDirectory)) + { + assetDirectory = Path.combine(outputDirectory, assetDirectory); + } + + var embedDirectory = Path.combine(targetDirectory, "obj/tmp"); + + for (asset in project.assets) + { + if (asset.type == AssetType.TEMPLATE) + { + var path = Path.combine(outputDirectory, asset.targetPath); + System.mkdir(Path.directory(path)); + AssetHelper.copyAsset(asset, path, project.templateContext); + } + else if (asset.embed == true) + { + if (asset.sourcePath == "") + { + var path = Path.combine(embedDirectory, asset.targetPath); + System.mkdir(Path.directory(path)); + AssetHelper.copyAsset(asset, path); + asset.sourcePath = path; + } + } + else + { + var path = Path.combine(assetDirectory, asset.targetPath); + System.mkdir(Path.directory(path)); + AssetHelper.copyAssetIfNewer(asset, path); + } + } + } + // Functions to track and delete stale files /** diff --git a/tools/platforms/AndroidPlatform.hx b/tools/platforms/AndroidPlatform.hx index 16d8859ac..9fb12587a 100644 --- a/tools/platforms/AndroidPlatform.hx +++ b/tools/platforms/AndroidPlatform.hx @@ -406,17 +406,6 @@ class AndroidPlatform extends PlatformTarget // project = project.clone (); - for (asset in project.assets) - { - if (asset.embed && asset.sourcePath == "") - { - var path = Path.combine(targetDirectory + "/obj/tmp", asset.targetPath); - System.mkdir(Path.directory(path)); - AssetHelper.copyAsset(asset, path); - asset.sourcePath = path; - } - } - // initialize (project); var destination = targetDirectory + "/bin"; @@ -427,36 +416,6 @@ class AndroidPlatform extends PlatformTarget System.mkdir(sourceSet + "/res/drawable-hdpi/"); System.mkdir(sourceSet + "/res/drawable-xhdpi/"); - for (asset in project.assets) - { - if (asset.type != AssetType.TEMPLATE) - { - var targetPath = ""; - - switch (asset.type) - { - default: - // case SOUND, MUSIC: - - // var extension = Path.extension (asset.sourcePath); - // asset.flatName += ((extension != "") ? "." + extension : ""); - - // asset.resourceName = asset.flatName; - targetPath = Path.combine(sourceSet + "/assets/", asset.resourceName); - - // asset.resourceName = asset.id; - // targetPath = sourceSet + "/res/raw/" + asset.flatName + "." + Path.extension (asset.targetPath); - - // default: - - // asset.resourceName = asset.flatName; - // targetPath = sourceSet + "/assets/" + asset.resourceName; - } - - AssetHelper.copyAssetIfNewer(asset, targetPath); - } - } - if (project.targetFlags.exists("xml")) { project.haxeflags.push("-xml " + targetDirectory + "/types.xml"); @@ -663,13 +622,13 @@ class AndroidPlatform extends PlatformTarget for (asset in project.assets) { - if (asset.type == AssetType.TEMPLATE) + if (asset.type != AssetType.TEMPLATE) { - var targetPath = Path.combine(destination, asset.targetPath); - System.mkdir(Path.directory(targetPath)); - AssetHelper.copyAsset(asset, targetPath, context); + asset.targetPath = asset.resourceName; } } + + copyProjectAssets(destination, sourceSet + "/assets/"); } public override function watch():Void diff --git a/tools/platforms/IOSPlatform.hx b/tools/platforms/IOSPlatform.hx index bf9ced537..f9679a8ca 100644 --- a/tools/platforms/IOSPlatform.hx +++ b/tools/platforms/IOSPlatform.hx @@ -169,7 +169,7 @@ class IOSPlatform extends PlatformTarget { project.haxeflags.push("-xml " + targetDirectory + "/types.xml"); } - + if (project.targetFlags.exists("json")) { project.haxeflags.push("--json " + targetDirectory + "/types.json"); @@ -524,17 +524,6 @@ class IOSPlatform extends PlatformTarget // project = project.clone (); - for (asset in project.assets) - { - if (asset.embed && asset.sourcePath == "") - { - var path = Path.combine(targetDirectory + "/" + project.app.file + "/obj/tmp", asset.targetPath); - System.mkdir(Path.directory(path)); - AssetHelper.copyAsset(asset, path); - asset.sourcePath = path; - } - } - // var manifest = new Asset (); // manifest.id = "__manifest__"; // manifest.data = AssetHelper.createManifest (project).serialize (); @@ -857,30 +846,7 @@ class IOSPlatform extends PlatformTarget } } - System.mkdir(projectDirectory + "/assets"); - - for (asset in project.assets) - { - if (asset.type != AssetType.TEMPLATE) - { - var targetPath = Path.combine(projectDirectory + "/assets/", asset.resourceName); - - // var sourceAssetPath:String = projectDirectory + "haxe/" + asset.sourcePath; - - System.mkdir(Path.directory(targetPath)); - AssetHelper.copyAssetIfNewer(asset, targetPath); - - // System.mkdir (Path.directory (sourceAssetPath)); - // System.linkFile (flatAssetPath, sourceAssetPath, true, true); - } - else - { - var targetPath = Path.combine(projectDirectory, asset.targetPath); - - System.mkdir(Path.directory(targetPath)); - AssetHelper.copyAsset(asset, targetPath, context); - } - } + copyProjectAssets(projectDirectory, "assets"); if (project.targetFlags.exists("xcode") && System.hostPlatform == MAC && command == "update") { diff --git a/tools/platforms/LinuxPlatform.hx b/tools/platforms/LinuxPlatform.hx index 26fc42ba5..0399b1b1a 100644 --- a/tools/platforms/LinuxPlatform.hx +++ b/tools/platforms/LinuxPlatform.hx @@ -553,17 +553,6 @@ class LinuxPlatform extends PlatformTarget // project = project.clone (); // initialize (project); - for (asset in project.assets) - { - if (asset.embed && asset.sourcePath == "") - { - var path = Path.combine(targetDirectory + "/obj/tmp", asset.targetPath); - System.mkdir(Path.directory(path)); - AssetHelper.copyAsset(asset, path); - asset.sourcePath = path; - } - } - if (project.targetFlags.exists("xml")) { project.haxeflags.push("-xml " + targetDirectory + "/types.xml"); @@ -606,24 +595,7 @@ class LinuxPlatform extends PlatformTarget } // context.HAS_ICON = IconHelper.createIcon (project.icons, 256, 256, Path.combine (applicationDirectory, "icon.png")); - for (asset in project.assets) - { - var path = Path.combine(applicationDirectory, asset.targetPath); - - if (asset.embed != true) - { - if (asset.type != AssetType.TEMPLATE) - { - System.mkdir(Path.directory(path)); - AssetHelper.copyAssetIfNewer(asset, path); - } - else - { - System.mkdir(Path.directory(path)); - AssetHelper.copyAsset(asset, path, context); - } - } - } + copyProjectAssets(applicationDirectory); } public override function watch():Void diff --git a/tools/platforms/MacPlatform.hx b/tools/platforms/MacPlatform.hx index 6857fbf52..16a4a1c02 100644 --- a/tools/platforms/MacPlatform.hx +++ b/tools/platforms/MacPlatform.hx @@ -525,17 +525,6 @@ class MacPlatform extends PlatformTarget project.haxeflags.push("--json " + targetDirectory + "/types.json"); } - for (asset in project.assets) - { - if (asset.embed && asset.sourcePath == "") - { - var path = Path.combine(targetDirectory + "/obj/tmp", asset.targetPath); - System.mkdir(Path.directory(path)); - AssetHelper.copyAsset(asset, path); - asset.sourcePath = path; - } - } - var context = generateContext(); context.OUTPUT_DIR = targetDirectory; @@ -584,22 +573,7 @@ class MacPlatform extends PlatformTarget context.HAS_ICON = IconHelper.createMacIcon(icons, Path.combine(contentDirectory, "icon.icns")); - for (asset in project.assets) - { - if (asset.embed != true) - { - if (asset.type != AssetType.TEMPLATE) - { - System.mkdir(Path.directory(Path.combine(contentDirectory, asset.targetPath))); - AssetHelper.copyAssetIfNewer(asset, Path.combine(contentDirectory, asset.targetPath)); - } - else - { - System.mkdir(Path.directory(Path.combine(targetDirectory, asset.targetPath))); - AssetHelper.copyAsset(asset, Path.combine(targetDirectory, asset.targetPath), context); - } - } - } + copyProjectAssets(targetDirectory, contentDirectory); } public override function watch():Void diff --git a/tools/platforms/TVOSPlatform.hx b/tools/platforms/TVOSPlatform.hx index c8b4a25c2..c814df601 100644 --- a/tools/platforms/TVOSPlatform.hx +++ b/tools/platforms/TVOSPlatform.hx @@ -434,17 +434,6 @@ class TVOSPlatform extends PlatformTarget // project = project.clone (); - for (asset in project.assets) - { - if (asset.embed && asset.sourcePath == "") - { - var path = Path.combine(targetDirectory + "/" + project.app.file + "/obj/tmp", asset.targetPath); - System.mkdir(Path.directory(path)); - AssetHelper.copyAsset(asset, path); - asset.sourcePath = path; - } - } - // var manifest = new Asset (); // manifest.id = "__manifest__"; // manifest.data = AssetHelper.createManifest (project).serialize (); @@ -628,30 +617,7 @@ class TVOSPlatform extends PlatformTarget } } - System.mkdir(projectDirectory + "/assets"); - - for (asset in project.assets) - { - if (asset.type != AssetType.TEMPLATE) - { - var targetPath = Path.combine(projectDirectory + "/assets/", asset.resourceName); - - // var sourceAssetPath:String = projectDirectory + "haxe/" + asset.sourcePath; - - System.mkdir(Path.directory(targetPath)); - AssetHelper.copyAssetIfNewer(asset, targetPath); - - // System.mkdir (Path.directory (sourceAssetPath)); - // System.linkFile (flatAssetPath, sourceAssetPath, true, true); - } - else - { - var targetPath = Path.combine(projectDirectory, asset.targetPath); - - System.mkdir(Path.directory(targetPath)); - AssetHelper.copyAsset(asset, targetPath, context); - } - } + copyProjectAssets(projectDirectory, "assets"); if (project.targetFlags.exists("xcode") && System.hostPlatform == MAC && command == "update") { diff --git a/tools/platforms/TizenPlatform.hx b/tools/platforms/TizenPlatform.hx index 49bc7e948..8d1e1c6db 100644 --- a/tools/platforms/TizenPlatform.hx +++ b/tools/platforms/TizenPlatform.hx @@ -203,17 +203,6 @@ class TizenPlatform extends PlatformTarget // project = project.clone (); - for (asset in project.assets) - { - if (asset.embed && asset.sourcePath == "") - { - var path = Path.combine(targetDirectory + "/obj/tmp", asset.targetPath); - System.mkdir(Path.directory(path)); - AssetHelper.copyAsset(asset, path); - asset.sourcePath = path; - } - } - var destination = targetDirectory + "/bin/"; System.mkdir(destination); @@ -256,30 +245,9 @@ class TizenPlatform extends PlatformTarget ProjectHelper.recursiveSmartCopyTemplate(project, "haxe", targetDirectory + "/haxe", context); ProjectHelper.recursiveSmartCopyTemplate(project, "tizen/hxml", targetDirectory + "/haxe", context); - for (asset in project.assets) - { - var path = Path.combine(destination + "res/", asset.targetPath); - - System.mkdir(Path.directory(path)); - - if (asset.type != AssetType.TEMPLATE) - { - if (asset.targetPath == "/appinfo.json") - { - AssetHelper.copyAsset(asset, path, context); - } - else - { - // going to root directory now, but should it be a forced "assets" folder later? - - AssetHelper.copyAssetIfNewer(asset, path); - } - } - else - { - AssetHelper.copyAsset(asset, path, context); - } - } + // going to root directory now, but should it be a forced "assets" folder later? + copyProjectAssets(destination + "res/", ""); + // copyProjectAssets(destination + "res/", "assets"); } @ignore public override function install():Void {} diff --git a/tools/platforms/WebAssemblyPlatform.hx b/tools/platforms/WebAssemblyPlatform.hx index 46aaf5ae2..af723aaae 100644 --- a/tools/platforms/WebAssemblyPlatform.hx +++ b/tools/platforms/WebAssemblyPlatform.hx @@ -396,17 +396,6 @@ class WebAssemblyPlatform extends PlatformTarget // project = project.clone (); - for (asset in project.assets) - { - if (asset.embed && asset.sourcePath == "") - { - var path = Path.combine(targetDirectory + "/obj/tmp", asset.targetPath); - System.mkdir(Path.directory(path)); - AssetHelper.copyAsset(asset, path); - asset.sourcePath = path; - } - } - // for (asset in project.assets) // { // asset.resourceName = "assets/" + asset.resourceName; @@ -481,37 +470,13 @@ class WebAssemblyPlatform extends PlatformTarget } } - for (asset in project.assets) - { - var path = Path.combine(targetDirectory + "/obj/assets", asset.targetPath); - - if (asset.type != AssetType.TEMPLATE) - { - // if (asset.type != AssetType.FONT) { - - System.mkdir(Path.directory(path)); - AssetHelper.copyAssetIfNewer(asset, path); - - // } - } - } - ProjectHelper.recursiveSmartCopyTemplate(project, "webassembly/template", destination, context); ProjectHelper.recursiveSmartCopyTemplate(project, "haxe", targetDirectory + "/haxe", context); ProjectHelper.recursiveSmartCopyTemplate(project, "webassembly/hxml", targetDirectory + "/haxe", context); // ProjectHelper.recursiveSmartCopyTemplate(project, "webassembly/cpp", targetDirectory + "/obj", context); ProjectHelper.recursiveSmartCopyTemplate(project, "cpp/static", targetDirectory + "/obj", context); - for (asset in project.assets) - { - var path = Path.combine(destination, asset.targetPath); - - if (asset.type == AssetType.TEMPLATE) - { - System.mkdir(Path.directory(path)); - AssetHelper.copyAsset(asset, path, context); - } - } + copyProjectAssets(destination, targetDirectory + "/obj/assets"); } @ignore public override function install():Void {} diff --git a/tools/platforms/WindowsPlatform.hx b/tools/platforms/WindowsPlatform.hx index e474b3c00..f2bde6534 100644 --- a/tools/platforms/WindowsPlatform.hx +++ b/tools/platforms/WindowsPlatform.hx @@ -962,17 +962,6 @@ class WindowsPlatform extends PlatformTarget project.haxeflags.push("--json " + targetDirectory + "/types.json"); } - for (asset in project.assets) - { - if (asset.embed && asset.sourcePath == "") - { - var path = Path.combine(targetDirectory + "/obj/tmp", asset.targetPath); - System.mkdir(Path.directory(path)); - AssetHelper.copyAsset(asset, path); - asset.sourcePath = path; - } - } - var context = generateContext(); context.OUTPUT_DIR = targetDirectory; @@ -1040,24 +1029,7 @@ class WindowsPlatform extends PlatformTarget }*/ - for (asset in project.assets) - { - if (asset.embed != true) - { - var path = Path.combine(applicationDirectory, asset.targetPath); - - if (asset.type != AssetType.TEMPLATE) - { - System.mkdir(Path.directory(path)); - AssetHelper.copyAssetIfNewer(asset, path); - } - else - { - System.mkdir(Path.directory(path)); - AssetHelper.copyAsset(asset, path, context); - } - } - } + copyProjectAssets(applicationDirectory); } private function updateUWP():Void