forked from tier4/AWSIM
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Alptuğ Cırıt <[email protected]>
- Loading branch information
Showing
5 changed files
with
172 additions
and
1 deletion.
There are no files selected for viewing
136 changes: 136 additions & 0 deletions
136
Assets/AWSIM/Scripts/Loader/SimulationCore/BundleManager.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
11
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.
Oops, something went wrong.
9 changes: 9 additions & 0 deletions
9
Assets/AWSIM/Scripts/Loader/SimulationCore/BundleTypeEnums.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
11
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.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters