Skip to content

Commit

Permalink
update importing bundles
Browse files Browse the repository at this point in the history
Signed-off-by: Alptuğ Cırıt <[email protected]>
  • Loading branch information
mozhoku committed Oct 1, 2024
1 parent d54f82f commit 6a595a3
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 1 deletion.
136 changes: 136 additions & 0 deletions Assets/AWSIM/Scripts/Loader/SimulationCore/BundleManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
using UnityEngine;
using System.IO;
using System.Collections.Generic;

namespace AWSIM.Scripts.Loader.SimulationCore
{
public static class BundleManager
{
// Dictionary to store bundle name and its internal path
private static Dictionary<string, string> bundlePathDictionary = new();

// Define locations for different bundle types
private static readonly string vehicleFolder = Path.Combine(Application.persistentDataPath, "Bundles/Vehicles");

private static readonly string environmentFolder =
Path.Combine(Application.persistentDataPath, "Bundles/Environments");

private static readonly string unknownFolder = Path.Combine(Application.persistentDataPath, "Bundles/Sensors");

/// <summary>
/// Imports the bundle to the target directory based on the type specified in PrefabInfo.
/// </summary>
public static bool ImportBundle(string bundlePath)
{
var bundle = AssetBundle.LoadFromFile(bundlePath);
if (bundle == null)
{
Debug.LogWarning($"Failed to load AssetBundle from path: {bundlePath}");
return false;
}

// Load the PrefabInfo to determine bundle type
var prefabInfo = bundle.LoadAsset<PrefabInfo>("PrefabInfo");
if (prefabInfo == null)
{
Debug.LogWarning("PrefabInfo not found in AssetBundle!");
bundle.Unload(true);
return false;
}

// Determine the target directory based on the bundle type
string targetDirectory = GetTargetDirectory(prefabInfo.bundleType);

if (string.IsNullOrEmpty(targetDirectory))
{
Debug.LogWarning("No valid target directory found for this bundle type!");
bundle.Unload(true);
return false;
}

// Move the bundle to the correct directory
string destinationPath = MoveBundle(bundlePath, targetDirectory);

if (!string.IsNullOrEmpty(destinationPath))
{
// Add the bundle name and internal path to the dictionary
bundlePathDictionary[prefabInfo.prefabName] = destinationPath;
}

bundle.Unload(true);
// Caching.Clear();
return true;
}

/// <summary>
/// Loads a prefab from the previously imported bundle.
/// </summary>
public static GameObject LoadPrefab(string prefabName)
{
if (!bundlePathDictionary.TryGetValue(prefabName, out string bundlePath))
{
Debug.LogWarning($"Bundle for prefab '{prefabName}' not found in the dictionary.");
return null;
}

// Load the bundle and retrieve the prefab
var bundle = AssetBundle.LoadFromFile(bundlePath);
if (bundle == null)
{
Debug.LogWarning($"Failed to load AssetBundle from path: {bundlePath}");
return null;
}

// Load the prefab
var prefab = bundle.LoadAsset<GameObject>(prefabName);
bundle.Unload(false);
return prefab;
}

/// <summary>
/// Returns the target directory based on the bundle type.
/// </summary>
private static string GetTargetDirectory(BundleType bundleType)
{
return bundleType switch
{
BundleType.Vehicle => vehicleFolder,
BundleType.Environment => environmentFolder,
BundleType.Unknown => unknownFolder,
_ => null
};
}

/// <summary>
/// Moves the bundle file to the target directory and returns the destination path.
/// </summary>
private static string MoveBundle(string bundlePath, string targetDirectory)
{
if (!Directory.Exists(targetDirectory))
{
Directory.CreateDirectory(targetDirectory);
}

string bundleFileName = Path.GetFileName(bundlePath);
string destinationPath = Path.Combine(targetDirectory, bundleFileName);

if (File.Exists(destinationPath))
{
Debug.LogWarning($"Bundle already exists at {destinationPath}, it will be overwritten.");
}

File.Copy(bundlePath, destinationPath, true); // Copy and overwrite if necessary
Debug.Log($"Bundle successfully moved to {destinationPath}");

return destinationPath;
}

/// <summary>
/// Gets the path of the imported bundle by its name.
/// </summary>
public static string GetBundlePath(string prefabName)
{
return bundlePathDictionary.GetValueOrDefault(prefabName);
}
}
}
11 changes: 11 additions & 0 deletions Assets/AWSIM/Scripts/Loader/SimulationCore/BundleManager.cs.meta

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

9 changes: 9 additions & 0 deletions Assets/AWSIM/Scripts/Loader/SimulationCore/BundleTypeEnums.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace AWSIM.Scripts.Loader.SimulationCore
{
public enum BundleType
{
Vehicle = 0,
Environment = 1,
Unknown = -1
}
}
11 changes: 11 additions & 0 deletions Assets/AWSIM/Scripts/Loader/SimulationCore/BundleTypeEnums.cs.meta

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

6 changes: 5 additions & 1 deletion Assets/AWSIM/Scripts/Loader/SimulationCore/PrefabInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
namespace AWSIM.Scripts.Loader.SimulationCore
{
/// <summary>
/// Used for tracking prefab name when building assetBundles.
/// Used for tracking metadata when building and loading assetBundles.
/// </summary>
[CreateAssetMenu(fileName = "PrefabInfo", menuName = "AWSIM/Prefab Info", order = 1)]
public class PrefabInfo : ScriptableObject
{
public string prefabName;
public BundleType bundleType;
public string version;
public string creator;
public string description;
}
}

0 comments on commit 6a595a3

Please sign in to comment.