diff --git a/asset-bundle-converter/Assets/AssetBundleConverter/Config.cs b/asset-bundle-converter/Assets/AssetBundleConverter/Config.cs index 18e3cf1c..0392c96a 100644 --- a/asset-bundle-converter/Assets/AssetBundleConverter/Config.cs +++ b/asset-bundle-converter/Assets/AssetBundleConverter/Config.cs @@ -35,6 +35,9 @@ public static class Config internal static string[] gltfExtensions = { ".glb", ".gltf" }; internal static string[] textureExtensions = { ".jpg", ".png", ".jpeg", ".tga", ".gif", ".bmp", ".psd", ".tiff", ".iff", ".ktx" }; + internal const string LODS_URL = "lods"; + + internal const string CLI_BUCKET_DIRECTORY = "bucketDirectory"; internal const string CLI_BUCKET = "bucket"; diff --git a/asset-bundle-converter/Assets/AssetBundleConverter/LODsConverter/Editor/ExportLODAssetBundles.cs b/asset-bundle-converter/Assets/AssetBundleConverter/LODsConverter/Editor/LODClient.cs similarity index 79% rename from asset-bundle-converter/Assets/AssetBundleConverter/LODsConverter/Editor/ExportLODAssetBundles.cs rename to asset-bundle-converter/Assets/AssetBundleConverter/LODsConverter/Editor/LODClient.cs index 524ba82b..6e61676c 100644 --- a/asset-bundle-converter/Assets/AssetBundleConverter/LODsConverter/Editor/ExportLODAssetBundles.cs +++ b/asset-bundle-converter/Assets/AssetBundleConverter/LODsConverter/Editor/LODClient.cs @@ -3,81 +3,44 @@ using UnityEditor; using System.IO; using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using Amazon; -using Amazon.S3; -using Amazon.S3.Model; using AssetBundleConverter.LODsConverter.Utils; -using Cysharp.Threading.Tasks; -using DCL.ABConverter; -using DCL.Shaders; -using UnityEngine.Experimental.Rendering; -using UnityEngine.Networking; using UnityEngine.Rendering; using Object = UnityEngine.Object; -using Random = UnityEngine.Random; -namespace DCL.ABConverter.LODClient +namespace DCL.ABConverter { - public class ExportLODAssetBundles : MonoBehaviour + public class LODClient : MonoBehaviour { - private static readonly string outputPath = Path.Combine(Application.dataPath, "../AssetBundles/"); + private static readonly string outputPath = Config.ASSET_BUNDLES_PATH_ROOT + Path.DirectorySeparatorChar; private static readonly string tempPath = Path.Combine(Application.dataPath, "temp"); + + [MenuItem("Assets/Export URL LODs")] public static async void ExportURLLODsToAssetBundles() { string[] commandLineArgs = Environment.GetCommandLineArgs(); - - string lodsURL = ""; + string customOutputDirectory = ""; + string lodsURL = ""; if (Utils.ParseOption(commandLineArgs, Config.LODS_URL, 1, out string[] lodsURLArg)) - lodsURL = lodsURLArg[0].ToLower(); + lodsURL = lodsURLArg[0]; if (Utils.ParseOption(commandLineArgs, Config.CLI_SET_CUSTOM_OUTPUT_ROOT_PATH, 1, out string[] outputDirectoryArg)) - customOutputDirectory = outputDirectoryArg[0].ToLower(); + customOutputDirectory = outputDirectoryArg[0] + "/"; else customOutputDirectory = outputPath; - string[] downloadedFiles = await DownloadRawLOD(lodsURL); + Debug.Log("Starting file download"); + var urlFileDownloader = new URLFileDownloader(lodsURL, tempPath); + string[] downloadedFiles = await urlFileDownloader.Download(); + Debug.Log("Finished file download"); + AssetDatabase.SaveAssets(); ExportFilesToAssetBundles(downloadedFiles, customOutputDirectory); + Utils.Exit(); } - private static async Task DownloadRawLOD(string lodsURL) - { - Directory.CreateDirectory(tempPath); - string[] filesToDownload = lodsURL.Split(","); - string[] downloadedPaths = new string[filesToDownload.Length]; - for (int index = 0; index < filesToDownload.Length; index++) - { - string url = filesToDownload[index]; - using (var webRequest = UnityWebRequest.Get(url)) - { - string fileName = Path.GetFileName(url); - // Modify this path according to your needs - string savePath = Path.Combine(tempPath, fileName); - - // Await the completion of the download - await webRequest.SendWebRequest(); - - if (webRequest.result == UnityWebRequest.Result.Success) - { - // Success, save the downloaded file - File.WriteAllBytes(savePath, webRequest.downloadHandler.data); - Debug.Log($"File downloaded and saved to {savePath}"); - downloadedPaths[index] = savePath; - } - else - { - Debug.LogError($"Error downloading {url}: {webRequest.error}"); - return null; - } - } - } - - return downloadedPaths; - } + public static async void ExportS3LODsToAssetBundles() { @@ -99,7 +62,7 @@ public static async void ExportS3LODsToAssetBundles() customOutputDirectory = outputPath; var amazonS3FileProvider = new AmazonS3FileProvider("lods", "LOD/Sources/1707159813915/", tempPath); - await amazonS3FileProvider.DownloadFilesAsync(); + await amazonS3FileProvider.Download(); //string[] downloadedFiles = Array.Empty(); //ExportFilesToAssetBundles(downloadedFiles, customOutputDirectory); @@ -137,9 +100,6 @@ private static void ExportFilesToAssetBundles(string[] filesToExport, string out GC.Collect(); } - // Save assets and refresh the AssetDatabase - AssetDatabase.SaveAssets(); - AssetDatabase.Refresh(); BuildPipeline.BuildAssetBundles(outputPath, BuildAssetBundleOptions.None, EditorUserBuildSettings.activeBuildTarget); Directory.Delete(tempPath, true); @@ -180,12 +140,10 @@ private static void ProcessModel(string fileToProcess, string tempPath) string prefabPath = tempPath + "/" + instantiated + ".prefab"; PrefabUtility.SaveAsPrefabAsset(instantiated, prefabPath); DestroyImmediate(instantiated); - // Save assets and refresh the AssetDatabase - AssetDatabase.SaveAssets(); - AssetDatabase.Refresh(); var prefabImporter = AssetImporter.GetAtPath(PathUtils.GetRelativePathTo(Application.dataPath, prefabPath)); prefabImporter.SetAssetBundleNameAndVariant(fileNameWithoutExtension, ""); + AssetDatabase.Refresh(); } } @@ -244,8 +202,6 @@ private static void SetDCLShaderMaterial(string path, GameObject transform, stri { string materialPath = Path.Combine(tempPath, materialName); AssetDatabase.CreateAsset(duplicatedMaterial, materialPath); - // Save assets and refresh the AssetDatabase - AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); materialsDictionary.Add(materialName, AssetDatabase.LoadAssetAtPath(materialPath)); } diff --git a/asset-bundle-converter/Assets/AssetBundleConverter/LODsConverter/Editor/ExportLODAssetBundles.cs.meta b/asset-bundle-converter/Assets/AssetBundleConverter/LODsConverter/Editor/LODClient.cs.meta similarity index 100% rename from asset-bundle-converter/Assets/AssetBundleConverter/LODsConverter/Editor/ExportLODAssetBundles.cs.meta rename to asset-bundle-converter/Assets/AssetBundleConverter/LODsConverter/Editor/LODClient.cs.meta diff --git a/asset-bundle-converter/Assets/AssetBundleConverter/LODsConverter/Utils/AmazonS3FileProvider.cs b/asset-bundle-converter/Assets/AssetBundleConverter/LODsConverter/Utils/AmazonS3FileProvider.cs index d83ea1d3..11f6e58d 100644 --- a/asset-bundle-converter/Assets/AssetBundleConverter/LODsConverter/Utils/AmazonS3FileProvider.cs +++ b/asset-bundle-converter/Assets/AssetBundleConverter/LODsConverter/Utils/AmazonS3FileProvider.cs @@ -1,93 +1,96 @@ using System; -using System.Collections; -using System.Collections.Generic; using System.IO; -using System.Runtime.CompilerServices; using System.Threading.Tasks; using Amazon; using Amazon.S3; using Amazon.S3.Model; -using UnityEngine; +using JetBrains.Annotations; -public class AmazonS3FileProvider +namespace AssetBundleConverter.LODsConverter.Utils { - private readonly string bucketName = "your-bucket-name"; - private readonly string directoryPath = "your-directory-path/"; // Ensure it ends with a '/' - private readonly string outputPath = "your-output-path"; - private static readonly RegionEndpoint bucketRegion = RegionEndpoint.USEast1; // Update to your bucket's region - private readonly IAmazonS3 s3Client; - - public AmazonS3FileProvider(string bucketName, string bucketDirectory, string outputPath) + public class AmazonS3FileProvider : IFileDownloader { - var config = new AmazonS3Config(); - config.RegionEndpoint = bucketRegion; - config.ServiceURL = "http://localhost:4566"; // Ensure to use the correct endpoint - s3Client = new AmazonS3Client(config); - this.bucketName = bucketName; - directoryPath = bucketDirectory; - this.outputPath = outputPath; - } + private readonly string bucketName = "your-bucket-name"; + private readonly string directoryPath = "your-directory-path/"; // Ensure it ends with a '/' + private readonly string outputPath = "your-output-path"; + private static readonly RegionEndpoint bucketRegion = RegionEndpoint.USEast1; // Update to your bucket's region + private readonly IAmazonS3 s3Client; - public async Task DownloadFilesAsync() - { - try + public AmazonS3FileProvider(string bucketName, string bucketDirectory, string outputPath) { - var request = new ListObjectsV2Request - { - BucketName = bucketName, Prefix = directoryPath - }; + var config = new AmazonS3Config(); + config.RegionEndpoint = bucketRegion; + config.ServiceURL = "http://localhost:4566"; // Ensure to use the correct endpoint + s3Client = new AmazonS3Client(config); + this.bucketName = bucketName; + directoryPath = bucketDirectory; + this.outputPath = outputPath; + } - ListObjectsV2Response response; - do + [CanBeNull] + public async Task Download() + { + try { - response = await s3Client.ListObjectsV2Async(request); - foreach (var entry in response.S3Objects) + var request = new ListObjectsV2Request { - Console.WriteLine($"Downloading {entry.Key}..."); - await DownloadFileAsync(entry.Key); - } + BucketName = bucketName, Prefix = directoryPath + }; - request.ContinuationToken = response.NextContinuationToken; - } while (response.IsTruncated); - } - catch (AmazonS3Exception e) - { - Console.WriteLine($"Error encountered on server. Message:'{e.Message}' when listing objects"); - } - catch (Exception e) - { - Console.WriteLine($"Unknown encountered on server. Message:'{e.Message}' when listing objects"); - } - } + ListObjectsV2Response response; + do + { + response = await s3Client.ListObjectsV2Async(request); + foreach (var entry in response.S3Objects) + { + Console.WriteLine($"Downloading {entry.Key}..."); + await DownloadFileAsync(entry.Key); + } - private async Task DownloadFileAsync(string keyName) - { - string filePath = Path.Combine(outputPath, keyName.Replace("/", "\\")); - // Ensure the directory exists - Directory.CreateDirectory(Path.GetDirectoryName(filePath)); + request.ContinuationToken = response.NextContinuationToken; + } while (response.IsTruncated); + } + catch (AmazonS3Exception e) + { + Console.WriteLine($"Error encountered on server. Message:'{e.Message}' when listing objects"); + } + catch (Exception e) + { + Console.WriteLine($"Unknown encountered on server. Message:'{e.Message}' when listing objects"); + } + + return Array.Empty(); + } - try + private async Task DownloadFileAsync(string keyName) { - var request = new GetObjectRequest + string filePath = Path.Combine(outputPath, keyName.Replace("/", "\\")); + // Ensure the directory exists + Directory.CreateDirectory(Path.GetDirectoryName(filePath)); + + try { - BucketName = bucketName, Key = keyName - }; + var request = new GetObjectRequest + { + BucketName = bucketName, Key = keyName + }; - using (var response = await s3Client.GetObjectAsync(request)) - using (var responseStream = response.ResponseStream) - using (var fileStream = File.Create(filePath)) + using (var response = await s3Client.GetObjectAsync(request)) + using (var responseStream = response.ResponseStream) + using (var fileStream = File.Create(filePath)) + { + await responseStream.CopyToAsync(fileStream); + Console.WriteLine($"{keyName} has been downloaded to {filePath}"); + } + } + catch (AmazonS3Exception e) { - await responseStream.CopyToAsync(fileStream); - Console.WriteLine($"{keyName} has been downloaded to {filePath}"); + Console.WriteLine($"Error encountered on server. Message:'{e.Message}' when downloading an object"); + } + catch (Exception e) + { + Console.WriteLine($"Unknown encountered on server. Message:'{e.Message}' when downloading an object"); } - } - catch (AmazonS3Exception e) - { - Console.WriteLine($"Error encountered on server. Message:'{e.Message}' when downloading an object"); - } - catch (Exception e) - { - Console.WriteLine($"Unknown encountered on server. Message:'{e.Message}' when downloading an object"); } } } \ No newline at end of file diff --git a/asset-bundle-converter/Assets/AssetBundleConverter/LODsConverter/Utils/IFileDownloader.cs b/asset-bundle-converter/Assets/AssetBundleConverter/LODsConverter/Utils/IFileDownloader.cs new file mode 100644 index 00000000..0f54bdff --- /dev/null +++ b/asset-bundle-converter/Assets/AssetBundleConverter/LODsConverter/Utils/IFileDownloader.cs @@ -0,0 +1,9 @@ +using System.Threading.Tasks; + +namespace AssetBundleConverter.LODsConverter.Utils +{ + public interface IFileDownloader + { + Task Download(); + } +} \ No newline at end of file diff --git a/asset-bundle-converter/Assets/AssetBundleConverter/LODsConverter/Utils/IFileDownloader.cs.meta b/asset-bundle-converter/Assets/AssetBundleConverter/LODsConverter/Utils/IFileDownloader.cs.meta new file mode 100644 index 00000000..7eb8e11b --- /dev/null +++ b/asset-bundle-converter/Assets/AssetBundleConverter/LODsConverter/Utils/IFileDownloader.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: feaeaaba66fd4846896f7db6b60f3c2e +timeCreated: 1709895539 \ No newline at end of file diff --git a/windows-test-run-lods.bat b/windows-test-run-lods.bat new file mode 100644 index 00000000..8fbd11d9 --- /dev/null +++ b/windows-test-run-lods.bat @@ -0,0 +1 @@ +"C:\Program Files\Unity\Hub\Editor\2022.3.12f1\Editor\Unity.exe" -projectPath "asset-bundle-converter" -batchmode -executeMethod DCL.ABConverter.LODClient.ExportURLLODsToAssetBundles -lods "https://lods-bucket-ed4300a.s3.amazonaws.com/-17,-21/LOD/Sources/1707776785658/bafkreidnwpjkv3yoxsz6iiqh3fahuec7lfsqtmkyz3yf6dgps454ngldnu_0.fbx;https://lods-bucket-ed4300a.s3.amazonaws.com/-17,-21/LOD/Sources/1707776785658/bafkreidnwpjkv3yoxsz6iiqh3fahuec7lfsqtmkyz3yf6dgps454ngldnu_1.fbx;https://lods-bucket-ed4300a.s3.amazonaws.com/-17,-21/LOD/Sources/1707776785658/bafkreidnwpjkv3yoxsz6iiqh3fahuec7lfsqtmkyz3yf6dgps454ngldnu_2.fbx" -output "C:/Users/juani/Documents/Decentraland/asset-bundle-converter - Copy/AssetBundles" -logFile ./tmp/log.txt \ No newline at end of file