Skip to content

Commit

Permalink
Moved temp path to LODPathHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
dalkia committed Mar 12, 2024
1 parent 7ac7672 commit e7e60c0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
Expand All @@ -46,7 +41,7 @@ public async void ConvertLODs()
var dictionaryStringForMetadata = new Dictionary<string, string>();
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);
Expand All @@ -61,7 +56,7 @@ public async void ConvertLODs()
}
catch (Exception e)
{
Directory.Delete(tempPath, true);
Directory.Delete(lodPathHandler.tempPath, true);
Utils.Exit(1);
}

Expand All @@ -73,7 +68,7 @@ public async void ConvertLODs()
private async Task<string[]> 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();
}
Expand Down Expand Up @@ -198,7 +193,7 @@ public void BuildAssetBundles(BuildTarget target, Dictionary<string, string> 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);

Expand All @@ -209,13 +204,13 @@ public void BuildAssetBundles(BuildTarget target, Dictionary<string, string> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -24,39 +25,47 @@ 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);

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);

Expand All @@ -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();
Expand Down

0 comments on commit e7e60c0

Please sign in to comment.