using System;
using System.Collections.Generic;
using UnityEditor.AddressableAssets.Build.BuildPipelineTasks;
using UnityEditor.AddressableAssets.Settings;
using UnityEditor.AddressableAssets.Settings.GroupSchemas;
using UnityEditor.Build.Pipeline;
using UnityEditor.Build.Pipeline.Interfaces;
using UnityEditor.Build.Pipeline.Tasks;
using UnityEditor.SceneManagement;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.AddressableAssets.Initialization;
using UnityEngine.AddressableAssets.ResourceLocators;
using UnityEngine.AddressableAssets.ResourceProviders;
using UnityEngine.ResourceManagement.ResourceProviders;
using UnityEngine.ResourceManagement.ResourceProviders.Simulation;
using UnityEngine.ResourceManagement.Util;
using System.IO;
using System.Linq;
using System.Reflection;
using UnityEditor.Experimental;
namespace UnityEditor.AddressableAssets.Build.DataBuilders
{
///
/// Build script for creating virtual asset bundle dat for running in the editor.
///
[CreateAssetMenu(fileName = "BuildScriptVirtual.asset", menuName = "Addressables/Content Builders/Simulate Groups (advanced)")]
public class BuildScriptVirtualMode : BuildScriptBase
{
///
public override string Name
{
get { return "Simulate Groups (advanced)"; }
}
///
public override bool CanBuildData()
{
return typeof(T).IsAssignableFrom(typeof(AddressablesPlayModeBuildResult));
}
///
public override void ClearCachedData()
{
DeleteFile(string.Format(m_PathFormat, "", "catalog"));
DeleteFile(string.Format(m_PathFormat, "", "settings"));
}
///
public override bool IsDataBuilt()
{
var catalogPath = string.Format(m_PathFormat, "", "catalog");
var settingsPath = string.Format(m_PathFormat, "", "settings");
return File.Exists(catalogPath) &&
File.Exists(settingsPath);
}
private string m_pathFormatStore;
private string m_PathFormat
{
get
{
if (string.IsNullOrEmpty(m_pathFormatStore))
m_pathFormatStore = "{0}" + Addressables.LibraryPath + "{1}_BuildScriptVirtualMode.json";
return m_pathFormatStore;
}
set { m_pathFormatStore = value; }
}
List m_ResourceProviderData;
List m_AllBundleInputDefinitions;
Dictionary m_CreatedProviderIds;
///
protected override TResult BuildDataImplementation(AddressablesDataBuilderInput builderInput)
{
TResult result = default(TResult);
var timer = new System.Diagnostics.Stopwatch();
timer.Start();
var aaSettings = builderInput.AddressableSettings;
m_PathFormat = builderInput.PathFormat;
//gather entries
var aaContext = new AddressableAssetsBuildContext
{
Settings = aaSettings,
runtimeData = new ResourceManagerRuntimeData(),
bundleToAssetGroup = new Dictionary(),
locations = new List(),
providerTypes = new HashSet(),
assetEntries = new List()
};
m_AllBundleInputDefinitions = new List();
aaContext.runtimeData.BuildTarget = builderInput.Target.ToString();
aaContext.runtimeData.ProfileEvents = ProjectConfigData.PostProfilerEvents;
aaContext.runtimeData.LogResourceManagerExceptions = aaSettings.buildSettings.LogResourceManagerExceptions;
aaContext.runtimeData.ProfileEvents = ProjectConfigData.PostProfilerEvents;
aaContext.runtimeData.MaxConcurrentWebRequests = aaSettings.MaxConcurrentWebRequests;
aaContext.runtimeData.CatalogRequestsTimeout = aaSettings.CatalogRequestsTimeout;
aaContext.runtimeData.CatalogLocations.Add(new ResourceLocationData(
new[] { ResourceManagerRuntimeData.kCatalogAddress },
string.Format(m_PathFormat, "file://{UnityEngine.Application.dataPath}/../", "catalog"),
typeof(ContentCatalogProvider), typeof(ContentCatalogData)));
aaContext.runtimeData.AddressablesVersion = PackageManager.PackageInfo.FindForAssembly(typeof(Addressables).Assembly)?.version;
m_CreatedProviderIds = new Dictionary();
m_ResourceProviderData = new List();
var errorString = ProcessAllGroups(aaContext);
if (!string.IsNullOrEmpty(errorString))
result = AddressableAssetBuildResult.CreateResult(null, 0, errorString);
if (result == null)
{
result = DoBuild(builderInput, aaSettings, aaContext);
}
if (result != null)
result.Duration = timer.Elapsed.TotalSeconds;
return result;
}
TResult DoBuild(AddressablesDataBuilderInput builderInput, AddressableAssetSettings aaSettings, AddressableAssetsBuildContext aaContext) where TResult : IDataBuilderResult
{
if (m_AllBundleInputDefinitions.Count > 0)
{
if (!BuildUtility.CheckModifiedScenesAndAskToSave())
return AddressableAssetBuildResult.CreateResult(null, 0, "Unsaved scenes");
var buildTarget = builderInput.Target;
var buildTargetGroup = builderInput.TargetGroup;
var buildParams = new AddressableAssetsBundleBuildParameters(aaSettings, aaContext.bundleToAssetGroup, buildTarget, buildTargetGroup, aaSettings.buildSettings.bundleBuildPath);
var builtinShaderBundleName = aaSettings.DefaultGroup.Name.ToLower().Replace(" ", "").Replace('\\', '/').Replace("//", "/") + "_unitybuiltinshaders.bundle";
var buildTasks = RuntimeDataBuildTasks(aaSettings.buildSettings.compileScriptsInVirtualMode, builtinShaderBundleName);
ExtractDataTask extractData = new ExtractDataTask();
buildTasks.Add(extractData);
string aaPath = aaSettings.AssetPath;
IBundleBuildResults results;
var exitCode = ContentPipeline.BuildAssetBundles(buildParams, new BundleBuildContent(m_AllBundleInputDefinitions), out results, buildTasks, aaContext);
if (exitCode < ReturnCode.Success)
return AddressableAssetBuildResult.CreateResult(null, 0, "SBP Error" + exitCode);
if (aaSettings == null && !string.IsNullOrEmpty(aaPath))
aaSettings = AssetDatabase.LoadAssetAtPath(aaPath);
}
var bundledAssets = new Dictionary