From e7e60c0262f3062edb36326e61b3894671dde8c3 Mon Sep 17 00:00:00 2001 From: Juan Ignacio Molteni Date: Tue, 12 Mar 2024 10:11:19 -0300 Subject: [PATCH] Moved temp path to LODPathHandler --- .../LODsConverter/Scripts/LODConversion.cs | 21 +++++------- .../LODsConverter/Utils/LODPathHandler.cs | 32 ++++++++++++------- 2 files changed, 28 insertions(+), 25 deletions(-) diff --git a/asset-bundle-converter/Assets/AssetBundleConverter/LODsConverter/Scripts/LODConversion.cs b/asset-bundle-converter/Assets/AssetBundleConverter/LODsConverter/Scripts/LODConversion.cs index cf3b8e34..28f75d10 100644 --- a/asset-bundle-converter/Assets/AssetBundleConverter/LODsConverter/Scripts/LODConversion.cs +++ b/asset-bundle-converter/Assets/AssetBundleConverter/LODsConverter/Scripts/LODConversion.cs @@ -16,8 +16,7 @@ public class LODConversion { - private readonly string outputPath; - private readonly string tempPath; + private readonly LODPathHandler lodPathHandler; private readonly string[] urlsToConvert; //TODO: CLEAN THIS UP HERE AND IN THE ASSET BUNDLE BUILDER. THIS IS NOT USED IN ALPHA @@ -26,17 +25,13 @@ public class LODConversion public LODConversion(string customOutputPath, string[] urlsToConvert) { this.urlsToConvert = urlsToConvert; - tempPath = LODConstants.DEFAULT_TEMP_PATH; - outputPath = !string.IsNullOrEmpty(customOutputPath) ? customOutputPath : LODConstants.DEFAULT_OUTPUT_PATH; + lodPathHandler = new LODPathHandler(customOutputPath); } public async void ConvertLODs() { PlatformUtils.currentTarget = EditorUserBuildSettings.activeBuildTarget; IAssetDatabase assetDatabase = new UnityEditorWrappers.AssetDatabase(); - - Directory.CreateDirectory(outputPath); - Directory.CreateDirectory(tempPath); string[] downloadedFilePaths = await DownloadFiles(); AssetDatabase.SaveAssets(); @@ -46,7 +41,7 @@ public async void ConvertLODs() var dictionaryStringForMetadata = new Dictionary(); foreach (string downloadedFilePath in downloadedFilePaths) { - var lodPathHandler = new LODPathHandler(tempPath, outputPath, downloadedFilePath); + lodPathHandler.SetCurrentFile(downloadedFilePath); if (File.Exists(lodPathHandler.assetBundlePath)) continue; dictionaryStringForMetadata.Add(lodPathHandler.fileNameWithoutExtension, lodPathHandler.fileNameWithoutExtension); @@ -61,7 +56,7 @@ public async void ConvertLODs() } catch (Exception e) { - Directory.Delete(tempPath, true); + Directory.Delete(lodPathHandler.tempPath, true); Utils.Exit(1); } @@ -73,7 +68,7 @@ public async void ConvertLODs() private async Task DownloadFiles() { Debug.Log("Starting file download"); - var urlFileDownloader = new URLFileDownloader(urlsToConvert, tempPath); + var urlFileDownloader = new URLFileDownloader(urlsToConvert, lodPathHandler.tempPath); Debug.Log("Finished file download"); return await urlFileDownloader.Download(); } @@ -198,7 +193,7 @@ public void BuildAssetBundles(BuildTarget target, Dictionary dic IBuildPipeline buildPipeline = new ScriptableBuildPipeline(); // 1. Convert flagged folders to asset bundles only to automatically get dependencies for the metadata - var manifest = buildPipeline.BuildAssetBundles(outputPath, + var manifest = buildPipeline.BuildAssetBundles(lodPathHandler.outputPath, BuildAssetBundleOptions.UncompressedAssetBundle | BuildAssetBundleOptions.ForceRebuildAssetBundle | BuildAssetBundleOptions.AssetBundleStripUnityVersion, target); @@ -209,13 +204,13 @@ public void BuildAssetBundles(BuildTarget target, Dictionary dic } // 2. Create metadata (dependencies, version, timestamp) and store in the target folders to be converted again later with the metadata inside - AssetBundleMetadataBuilder.Generate(new SystemWrappers.File(), tempPath, dictionaryForMetadataBuilder, manifest, VERSION); + AssetBundleMetadataBuilder.Generate(new SystemWrappers.File(), lodPathHandler.tempPath, dictionaryForMetadataBuilder, manifest, VERSION); AssetDatabase.Refresh(ImportAssetOptions.ForceSynchronousImport | ImportAssetOptions.ForceUpdate); AssetDatabase.SaveAssets(); // 3. Convert flagged folders to asset bundles again but this time they have the metadata file inside - buildPipeline.BuildAssetBundles(outputPath, + buildPipeline.BuildAssetBundles(lodPathHandler.outputPath, BuildAssetBundleOptions.UncompressedAssetBundle | BuildAssetBundleOptions.ForceRebuildAssetBundle | BuildAssetBundleOptions.AssetBundleStripUnityVersion, target); } diff --git a/asset-bundle-converter/Assets/AssetBundleConverter/LODsConverter/Utils/LODPathHandler.cs b/asset-bundle-converter/Assets/AssetBundleConverter/LODsConverter/Utils/LODPathHandler.cs index c07923dd..70ee0937 100644 --- a/asset-bundle-converter/Assets/AssetBundleConverter/LODsConverter/Utils/LODPathHandler.cs +++ b/asset-bundle-converter/Assets/AssetBundleConverter/LODsConverter/Utils/LODPathHandler.cs @@ -9,11 +9,12 @@ namespace AssetBundleConverter.LODsConverter.Utils public class LODPathHandler { public string tempPath; + public string outputPath; - public string filePath; public string fileDirectoryRelativeToDataPath; public string filePathRelativeToDataPath; + public string filePath; public string fileName; public string fileNameWithoutExtension; @@ -24,17 +25,24 @@ public class LODPathHandler public string prefabPathRelativeToDataPath; + public LODPathHandler(string customOutputPath) + { + outputPath = !string.IsNullOrEmpty(customOutputPath) ? customOutputPath : LODConstants.DEFAULT_OUTPUT_PATH; + tempPath = LODConstants.DEFAULT_TEMP_PATH; - public LODPathHandler(string tempPath, string outputPath, string filePath) + Directory.CreateDirectory(outputPath); + Directory.CreateDirectory(tempPath); + } + + public void SetCurrentFile(string downloadedFilePath) { - this.tempPath = tempPath; - this.filePath = filePath; + filePath = downloadedFilePath; fileName = Path.GetFileName(filePath); fileNameWithoutExtension = Path.GetFileNameWithoutExtension(filePath); assetBundleFileName = fileNameWithoutExtension + PlatformUtils.GetPlatform(); assetBundlePath = Path.Combine(outputPath, assetBundleFileName); } - + public void MoveFileToMatchingFolder() { string fileDirectory = Path.GetDirectoryName(filePath); @@ -42,21 +50,22 @@ public void MoveFileToMatchingFolder() if (fileDirectory.EndsWith(fileNameWithoutExtension)) { Console.WriteLine("The file is already in the correct folder."); - filePathRelativeToDataPath = PathUtils.GetRelativePathTo(Application.dataPath, filePath); + UpdatePaths(filePath); return; } string targetFolderPath = Path.Combine(fileDirectory, fileNameWithoutExtension); Directory.CreateDirectory(targetFolderPath); - - // Create a new path for the file in the new folder string newFilePath = Path.Combine(targetFolderPath, fileName); - - // Move the file to the new folder File.Move(filePath, newFilePath); + UpdatePaths(newFilePath); + } + + private void UpdatePaths(string newFilePath) + { filePath = newFilePath; - fileDirectory = Path.GetDirectoryName(filePath); + string fileDirectory = Path.GetDirectoryName(filePath); filePathRelativeToDataPath = PathUtils.GetRelativePathTo(Application.dataPath, filePath); fileDirectoryRelativeToDataPath = PathUtils.GetRelativePathTo(Application.dataPath, fileDirectory); @@ -65,7 +74,6 @@ public void MoveFileToMatchingFolder() materialsPathRelativeToDataPath = PathUtils.GetRelativePathTo(Application.dataPath, materialsPath); prefabPathRelativeToDataPath = PathUtils.GetRelativePathTo(Application.dataPath, fileDirectory + "/" + fileNameWithoutExtension + ".prefab"); - // Save assets and refresh the AssetDatabase AssetDatabase.SaveAssets(); AssetDatabase.Refresh();