using System;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
namespace Ubisoft.Hotel.Addressables.Core.Editor
{
public class BuildProcessorHelper
{
private static void MoveFile(string srcPath, string dstPath)
{
AddressablesLogger.Channel.LogInformation($"Moving {srcPath} to {dstPath}");
File.Move(srcPath, dstPath);
string metaFilePath = AssetDatabase.GetTextMetaFilePathFromAssetPath(srcPath);
File.Delete(metaFilePath);
}
///
/// Move custom asset pack data from their build location to their App Bundle data location.
///
public static void MoveDataForAppBundleBuild()
{
try
{
AssetDatabase.StartAssetEditing();
if (File.Exists(AssetDeliveryUtility.AssetPackCatalogEditorPath))
{
if (!Directory.Exists(AssetDeliveryUtility.AssetPackCatalogFolderName))
{
_ = Directory.CreateDirectory(AssetDeliveryUtility.AssetPackCatalogFolderName);
}
MoveFile(AssetDeliveryUtility.AssetPackCatalogEditorPath, AssetDeliveryUtility.AssetPackCatalogRuntimePath);
}
Debug.Log($"Exits {AssetDeliveryUtility.BuildProcessorDataPath}: " + File.Exists(AssetDeliveryUtility.BuildProcessorDataPath));
if (File.Exists(AssetDeliveryUtility.BuildProcessorDataPath))
{
string contents = File.ReadAllText(AssetDeliveryUtility.BuildProcessorDataPath);
var data = JsonUtility.FromJson(contents);
foreach (BuildProcessorDataEntry entry in data.Entries)
{
string assetsFolderPath = entry.AssetsSubfolderPath;
Debug.Log($"Exits {entry.BundleBuildPath}: " + File.Exists(entry.BundleBuildPath));
if (File.Exists(entry.BundleBuildPath))
{
MoveFile(entry.BundleBuildPath, assetsFolderPath);
}
}
}
}
catch (Exception e)
{
AddressablesLogger.Channel.LogError($"Exception occurred when moving data for an app bundle build: {e.Message}.");
}
finally
{
AssetDatabase.StopAssetEditing();
}
}
///
/// Move custom asset pack data from their App Bundle data location to to their build location.
///
public static void MoveDataToDefaultLocation()
{
try
{
AssetDatabase.StartAssetEditing();
if (File.Exists(AssetDeliveryUtility.AssetPackCatalogRuntimePath))
{
MoveFile(AssetDeliveryUtility.AssetPackCatalogRuntimePath, AssetDeliveryUtility.AssetPackCatalogEditorPath);
AssetDeliveryUtility.DeleteDirectory(AssetDeliveryUtility.AssetPackCatalogFolderName, true);
}
if (File.Exists(AssetDeliveryUtility.BuildProcessorDataPath))
{
string contents = File.ReadAllText(AssetDeliveryUtility.BuildProcessorDataPath);
var data = JsonUtility.FromJson(contents);
foreach (BuildProcessorDataEntry entry in data.Entries)
{
string assetsFolderPath = entry.AssetsSubfolderPath;
if (File.Exists(assetsFolderPath))
{
MoveFile(assetsFolderPath, entry.BundleBuildPath);
}
}
}
}
catch (Exception e)
{
AddressablesLogger.Channel.LogError($"Exception occurred when moving data for a player build: {e.Message}.");
}
finally
{
AssetDatabase.StopAssetEditing();
}
}
#if UNITY_IOS
public static UnityEditor.iOS.Resource[] GetIosAssetPackResources()
{
List resources = new List();
if (File.Exists(AssetDeliveryUtility.BuildProcessorDataPath))
{
string assetBundleFilePath;
string contents = File.ReadAllText(AssetDeliveryUtility.BuildProcessorDataPath);
var data = JsonUtility.FromJson(contents);
foreach (BuildProcessorDataEntry entry in data.Entries)
{
if (File.Exists(entry.AssetsSubfolderPath))
{
string[] tokens = entry.AssetsSubfolderPath.Split(Path.DirectorySeparatorChar);
if (tokens.Length < 2)
{
AddressablesLogger.Channel.LogWarning($"No asset pack found for path: {entry.AssetsSubfolderPath}");
}
else
{
assetBundleFilePath = Path.GetFullPath(entry.AssetsSubfolderPath);
string assetPackName = tokens[tokens.Length - 2];
Debug.Log($"Adding asset bundle {assetBundleFilePath} to asset pack: {assetPackName}");
resources.Add(
new UnityEditor.iOS.Resource(assetPackName, assetBundleFilePath)
.AddOnDemandResourceTags(assetPackName));
}
}
else
{
AddressablesLogger.Channel.LogWarning($"No asset pack found at {entry.BundleBuildPath}");
}
}
}
return resources.ToArray();
}
#endif
}
}