Skip to content

Commit

Permalink
Updated to use tool via batchmode
Browse files Browse the repository at this point in the history
  • Loading branch information
dalkia committed Mar 8, 2024
1 parent 81b0783 commit 4a6305d
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 130 deletions.
3 changes: 3 additions & 0 deletions asset-bundle-converter/Assets/AssetBundleConverter/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<string[]> 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()
{
Expand All @@ -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<string>();
//ExportFilesToAssetBundles(downloadedFiles, customOutputDirectory);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
}
}

Expand Down Expand Up @@ -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<Material>(materialPath));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<string[]> 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<string>();
}

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");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Threading.Tasks;

namespace AssetBundleConverter.LODsConverter.Utils
{
public interface IFileDownloader
{
Task<string[]> Download();
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions windows-test-run-lods.bat
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 4a6305d

Please sign in to comment.