12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568 |
- //------------------------------------------------------------
- // Game Framework
- // Copyright © 2013-2021 Jiang Yin. All rights reserved.
- // Homepage: https://gameframework.cn/
- // Feedback: mailto:ellan@gameframework.cn
- //------------------------------------------------------------
- using GameFramework;
- using GameFramework.FileSystem;
- using GameFramework.Resource;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Xml;
- using UnityEditor;
- using UnityEngine;
- using UnityGameFramework.Runtime;
- namespace UnityGameFramework.Editor.ResourceTools
- {
- public sealed partial class ResourceBuilderController
- {
- private const string RemoteVersionListFileName = "GameFrameworkVersion.dat";
- private const string LocalVersionListFileName = "GameFrameworkList.dat";
- private const string DefaultExtension = "dat";
- private const string NoneOptionName = "<None>";
- private static readonly int AssetsStringLength = "Assets".Length;
- private readonly string m_ConfigurationPath;
- private readonly ResourceCollection m_ResourceCollection;
- private readonly ResourceAnalyzerController m_ResourceAnalyzerController;
- private readonly SortedDictionary<string, ResourceData> m_ResourceDatas;
- private readonly Dictionary<string, IFileSystem> m_OutputPackageFileSystems;
- private readonly Dictionary<string, IFileSystem> m_OutputPackedFileSystems;
- private readonly BuildReport m_BuildReport;
- private readonly List<string> m_CompressionHelperTypeNames;
- private readonly List<string> m_BuildEventHandlerTypeNames;
- private IBuildEventHandler m_BuildEventHandler;
- private IFileSystemManager m_FileSystemManager;
- public ResourceBuilderController()
- {
- m_ConfigurationPath = Type.GetConfigurationPath<ResourceBuilderConfigPathAttribute>() ?? Utility.Path.GetRegularPath(Path.Combine(Application.dataPath, "GameFramework/Configs/ResourceBuilder.xml"));
- m_ResourceCollection = new ResourceCollection();
- m_ResourceCollection.OnLoadingResource += delegate (int index, int count)
- {
- if (OnLoadingResource != null)
- {
- OnLoadingResource(index, count);
- }
- };
- m_ResourceCollection.OnLoadingAsset += delegate (int index, int count)
- {
- if (OnLoadingAsset != null)
- {
- OnLoadingAsset(index, count);
- }
- };
- m_ResourceCollection.OnLoadCompleted += delegate ()
- {
- if (OnLoadCompleted != null)
- {
- OnLoadCompleted();
- }
- };
- m_ResourceAnalyzerController = new ResourceAnalyzerController(m_ResourceCollection);
- m_ResourceAnalyzerController.OnAnalyzingAsset += delegate (int index, int count)
- {
- if (OnAnalyzingAsset != null)
- {
- OnAnalyzingAsset(index, count);
- }
- };
- m_ResourceAnalyzerController.OnAnalyzeCompleted += delegate ()
- {
- if (OnAnalyzeCompleted != null)
- {
- OnAnalyzeCompleted();
- }
- };
- m_ResourceDatas = new SortedDictionary<string, ResourceData>(StringComparer.Ordinal);
- m_OutputPackageFileSystems = new Dictionary<string, IFileSystem>(StringComparer.Ordinal);
- m_OutputPackedFileSystems = new Dictionary<string, IFileSystem>(StringComparer.Ordinal);
- m_BuildReport = new BuildReport();
- m_CompressionHelperTypeNames = new List<string>
- {
- NoneOptionName
- };
- m_BuildEventHandlerTypeNames = new List<string>
- {
- NoneOptionName
- };
- m_CompressionHelperTypeNames.AddRange(Type.GetRuntimeOrEditorTypeNames(typeof(Utility.Compression.ICompressionHelper)));
- m_BuildEventHandlerTypeNames.AddRange(Type.GetRuntimeOrEditorTypeNames(typeof(IBuildEventHandler)));
- m_BuildEventHandler = null;
- m_FileSystemManager = null;
- Platforms = Platform.Undefined;
- AssetBundleCompression = AssetBundleCompressionType.LZ4;
- CompressionHelperTypeName = string.Empty;
- AdditionalCompressionSelected = false;
- ForceRebuildAssetBundleSelected = false;
- BuildEventHandlerTypeName = string.Empty;
- OutputDirectory = string.Empty;
- OutputPackageSelected = OutputFullSelected = OutputPackedSelected = true;
- }
- public string ProductName
- {
- get
- {
- return PlayerSettings.productName;
- }
- }
- public string CompanyName
- {
- get
- {
- return PlayerSettings.companyName;
- }
- }
- public string GameIdentifier
- {
- get
- {
- #if UNITY_5_6_OR_NEWER
- return PlayerSettings.applicationIdentifier;
- #else
- return PlayerSettings.bundleIdentifier;
- #endif
- }
- }
- public string GameFrameworkVersion
- {
- get
- {
- return GameFramework.Version.GameFrameworkVersion;
- }
- }
- public string UnityVersion
- {
- get
- {
- return Application.unityVersion;
- }
- }
- public string ApplicableGameVersion
- {
- get
- {
- return Application.version;
- }
- }
- public int InternalResourceVersion
- {
- get;
- set;
- }
- public Platform Platforms
- {
- get;
- set;
- }
- public AssetBundleCompressionType AssetBundleCompression
- {
- get;
- set;
- }
- public string CompressionHelperTypeName
- {
- get;
- set;
- }
- public bool AdditionalCompressionSelected
- {
- get;
- set;
- }
- public bool ForceRebuildAssetBundleSelected
- {
- get;
- set;
- }
- public string BuildEventHandlerTypeName
- {
- get;
- set;
- }
- public string OutputDirectory
- {
- get;
- set;
- }
- public bool OutputPackageSelected
- {
- get;
- set;
- }
- public bool OutputFullSelected
- {
- get;
- set;
- }
- public bool OutputPackedSelected
- {
- get;
- set;
- }
- public bool IsValidOutputDirectory
- {
- get
- {
- if (string.IsNullOrEmpty(OutputDirectory))
- {
- return false;
- }
- if (!Directory.Exists(OutputDirectory))
- {
- return false;
- }
- return true;
- }
- }
- public string WorkingPath
- {
- get
- {
- if (!IsValidOutputDirectory)
- {
- return string.Empty;
- }
- return Utility.Path.GetRegularPath(new DirectoryInfo(Utility.Text.Format("{0}/Working/", OutputDirectory)).FullName);
- }
- }
- public string OutputPackagePath
- {
- get
- {
- if (!IsValidOutputDirectory)
- {
- return string.Empty;
- }
- return Utility.Path.GetRegularPath(new DirectoryInfo(Utility.Text.Format("{0}/Package/{1}_{2}/", OutputDirectory, ApplicableGameVersion.Replace('.', '_'), InternalResourceVersion.ToString())).FullName);
- }
- }
- public string OutputFullPath
- {
- get
- {
- if (!IsValidOutputDirectory)
- {
- return string.Empty;
- }
- return Utility.Path.GetRegularPath(new DirectoryInfo(Utility.Text.Format("{0}/Full/{1}_{2}/", OutputDirectory, ApplicableGameVersion.Replace('.', '_'), InternalResourceVersion.ToString())).FullName);
- }
- }
- public string OutputPackedPath
- {
- get
- {
- if (!IsValidOutputDirectory)
- {
- return string.Empty;
- }
- return Utility.Path.GetRegularPath(new DirectoryInfo(Utility.Text.Format("{0}/Packed/{1}_{2}/", OutputDirectory, ApplicableGameVersion.Replace('.', '_'), InternalResourceVersion.ToString())).FullName);
- }
- }
- public string BuildReportPath
- {
- get
- {
- if (!IsValidOutputDirectory)
- {
- return string.Empty;
- }
- return Utility.Path.GetRegularPath(new DirectoryInfo(Utility.Text.Format("{0}/BuildReport/{1}_{2}/", OutputDirectory, ApplicableGameVersion.Replace('.', '_'), InternalResourceVersion.ToString())).FullName);
- }
- }
- public event GameFrameworkAction<int, int> OnLoadingResource = null;
- public event GameFrameworkAction<int, int> OnLoadingAsset = null;
- public event GameFrameworkAction OnLoadCompleted = null;
- public event GameFrameworkAction<int, int> OnAnalyzingAsset = null;
- public event GameFrameworkAction OnAnalyzeCompleted = null;
- public event GameFrameworkFunc<string, float, bool> ProcessingAssetBundle = null;
- public event GameFrameworkFunc<string, float, bool> ProcessingBinary = null;
- public event GameFrameworkAction<Platform> ProcessResourceComplete = null;
- public event GameFrameworkAction<string> BuildResourceError = null;
- public bool Load()
- {
- if (!File.Exists(m_ConfigurationPath))
- {
- return false;
- }
- try
- {
- XmlDocument xmlDocument = new XmlDocument();
- xmlDocument.Load(m_ConfigurationPath);
- XmlNode xmlRoot = xmlDocument.SelectSingleNode("UnityGameFramework");
- XmlNode xmlEditor = xmlRoot.SelectSingleNode("ResourceBuilder");
- XmlNode xmlSettings = xmlEditor.SelectSingleNode("Settings");
- XmlNodeList xmlNodeList = null;
- XmlNode xmlNode = null;
- xmlNodeList = xmlSettings.ChildNodes;
- for (int i = 0; i < xmlNodeList.Count; i++)
- {
- xmlNode = xmlNodeList.Item(i);
- switch (xmlNode.Name)
- {
- case "InternalResourceVersion":
- InternalResourceVersion = int.Parse(xmlNode.InnerText) + 1;
- break;
- case "Platforms":
- Platforms = (Platform)int.Parse(xmlNode.InnerText);
- break;
- case "AssetBundleCompression":
- AssetBundleCompression = (AssetBundleCompressionType)byte.Parse(xmlNode.InnerText);
- break;
- case "CompressionHelperTypeName":
- CompressionHelperTypeName = xmlNode.InnerText;
- break;
- case "AdditionalCompressionSelected":
- AdditionalCompressionSelected = bool.Parse(xmlNode.InnerText);
- break;
- case "ForceRebuildAssetBundleSelected":
- ForceRebuildAssetBundleSelected = bool.Parse(xmlNode.InnerText);
- break;
- case "BuildEventHandlerTypeName":
- BuildEventHandlerTypeName = xmlNode.InnerText;
- break;
- case "OutputDirectory":
- OutputDirectory = xmlNode.InnerText;
- break;
- case "OutputPackageSelected":
- OutputPackageSelected = bool.Parse(xmlNode.InnerText);
- break;
- case "OutputFullSelected":
- OutputFullSelected = bool.Parse(xmlNode.InnerText);
- break;
- case "OutputPackedSelected":
- OutputPackedSelected = bool.Parse(xmlNode.InnerText);
- break;
- }
- }
- }
- catch
- {
- File.Delete(m_ConfigurationPath);
- return false;
- }
- return true;
- }
- public bool Save()
- {
- try
- {
- XmlDocument xmlDocument = new XmlDocument();
- xmlDocument.AppendChild(xmlDocument.CreateXmlDeclaration("1.0", "UTF-8", null));
- XmlElement xmlRoot = xmlDocument.CreateElement("UnityGameFramework");
- xmlDocument.AppendChild(xmlRoot);
- XmlElement xmlBuilder = xmlDocument.CreateElement("ResourceBuilder");
- xmlRoot.AppendChild(xmlBuilder);
- XmlElement xmlSettings = xmlDocument.CreateElement("Settings");
- xmlBuilder.AppendChild(xmlSettings);
- XmlElement xmlElement = null;
- xmlElement = xmlDocument.CreateElement("InternalResourceVersion");
- xmlElement.InnerText = InternalResourceVersion.ToString();
- xmlSettings.AppendChild(xmlElement);
- xmlElement = xmlDocument.CreateElement("Platforms");
- xmlElement.InnerText = ((int)Platforms).ToString();
- xmlSettings.AppendChild(xmlElement);
- xmlElement = xmlDocument.CreateElement("AssetBundleCompression");
- xmlElement.InnerText = ((byte)AssetBundleCompression).ToString();
- xmlSettings.AppendChild(xmlElement);
- xmlElement = xmlDocument.CreateElement("CompressionHelperTypeName");
- xmlElement.InnerText = CompressionHelperTypeName;
- xmlSettings.AppendChild(xmlElement);
- xmlElement = xmlDocument.CreateElement("AdditionalCompressionSelected");
- xmlElement.InnerText = AdditionalCompressionSelected.ToString();
- xmlSettings.AppendChild(xmlElement);
- xmlElement = xmlDocument.CreateElement("ForceRebuildAssetBundleSelected");
- xmlElement.InnerText = ForceRebuildAssetBundleSelected.ToString();
- xmlSettings.AppendChild(xmlElement);
- xmlElement = xmlDocument.CreateElement("BuildEventHandlerTypeName");
- xmlElement.InnerText = BuildEventHandlerTypeName;
- xmlSettings.AppendChild(xmlElement);
- xmlElement = xmlDocument.CreateElement("OutputDirectory");
- xmlElement.InnerText = OutputDirectory;
- xmlSettings.AppendChild(xmlElement);
- xmlElement = xmlDocument.CreateElement("OutputPackageSelected");
- xmlElement.InnerText = OutputPackageSelected.ToString();
- xmlSettings.AppendChild(xmlElement);
- xmlElement = xmlDocument.CreateElement("OutputFullSelected");
- xmlElement.InnerText = OutputFullSelected.ToString();
- xmlSettings.AppendChild(xmlElement);
- xmlElement = xmlDocument.CreateElement("OutputPackedSelected");
- xmlElement.InnerText = OutputPackedSelected.ToString();
- xmlSettings.AppendChild(xmlElement);
- string configurationDirectoryName = Path.GetDirectoryName(m_ConfigurationPath);
- if (!Directory.Exists(configurationDirectoryName))
- {
- Directory.CreateDirectory(configurationDirectoryName);
- }
- xmlDocument.Save(m_ConfigurationPath);
- AssetDatabase.Refresh();
- return true;
- }
- catch
- {
- if (File.Exists(m_ConfigurationPath))
- {
- File.Delete(m_ConfigurationPath);
- }
- return false;
- }
- }
- public string[] GetCompressionHelperTypeNames()
- {
- return m_CompressionHelperTypeNames.ToArray();
- }
- public string[] GetBuildEventHandlerTypeNames()
- {
- return m_BuildEventHandlerTypeNames.ToArray();
- }
- public bool IsPlatformSelected(Platform platform)
- {
- return (Platforms & platform) != 0;
- }
- public void SelectPlatform(Platform platform, bool selected)
- {
- if (selected)
- {
- Platforms |= platform;
- }
- else
- {
- Platforms &= ~platform;
- }
- }
- public bool RefreshCompressionHelper()
- {
- bool retVal = false;
- if (!string.IsNullOrEmpty(CompressionHelperTypeName) && m_CompressionHelperTypeNames.Contains(CompressionHelperTypeName))
- {
- System.Type compressionHelperType = Utility.Assembly.GetType(CompressionHelperTypeName);
- if (compressionHelperType != null)
- {
- Utility.Compression.ICompressionHelper compressionHelper = (Utility.Compression.ICompressionHelper)Activator.CreateInstance(compressionHelperType);
- if (compressionHelper != null)
- {
- Utility.Compression.SetCompressionHelper(compressionHelper);
- return true;
- }
- }
- }
- else
- {
- retVal = true;
- }
- CompressionHelperTypeName = string.Empty;
- Utility.Compression.SetCompressionHelper(null);
- return retVal;
- }
- public bool RefreshBuildEventHandler()
- {
- bool retVal = false;
- if (!string.IsNullOrEmpty(BuildEventHandlerTypeName) && m_BuildEventHandlerTypeNames.Contains(BuildEventHandlerTypeName))
- {
- System.Type buildEventHandlerType = Utility.Assembly.GetType(BuildEventHandlerTypeName);
- if (buildEventHandlerType != null)
- {
- IBuildEventHandler buildEventHandler = (IBuildEventHandler)Activator.CreateInstance(buildEventHandlerType);
- if (buildEventHandler != null)
- {
- m_BuildEventHandler = buildEventHandler;
- return true;
- }
- }
- }
- else
- {
- retVal = true;
- }
- BuildEventHandlerTypeName = string.Empty;
- m_BuildEventHandler = null;
- return retVal;
- }
- public bool BuildResources()
- {
- if (!IsValidOutputDirectory)
- {
- return false;
- }
- if (Directory.Exists(OutputPackagePath))
- {
- Directory.Delete(OutputPackagePath, true);
- }
- Directory.CreateDirectory(OutputPackagePath);
- if (Directory.Exists(OutputFullPath))
- {
- Directory.Delete(OutputFullPath, true);
- }
- Directory.CreateDirectory(OutputFullPath);
- if (Directory.Exists(OutputPackedPath))
- {
- Directory.Delete(OutputPackedPath, true);
- }
- Directory.CreateDirectory(OutputPackedPath);
- if (Directory.Exists(BuildReportPath))
- {
- Directory.Delete(BuildReportPath, true);
- }
- Directory.CreateDirectory(BuildReportPath);
- BuildAssetBundleOptions buildAssetBundleOptions = GetBuildAssetBundleOptions();
- m_BuildReport.Initialize(BuildReportPath, ProductName, CompanyName, GameIdentifier, GameFrameworkVersion, UnityVersion, ApplicableGameVersion, InternalResourceVersion,
- Platforms, AssetBundleCompression, CompressionHelperTypeName, AdditionalCompressionSelected, ForceRebuildAssetBundleSelected, BuildEventHandlerTypeName, OutputDirectory, buildAssetBundleOptions, m_ResourceDatas);
- try
- {
- m_BuildReport.LogInfo("Build Start Time: {0}", DateTime.UtcNow.ToLocalTime().ToString("yyyy-MM-dd HH:mm:ss.fff"));
- if (m_BuildEventHandler != null)
- {
- m_BuildReport.LogInfo("Execute build event handler 'OnPreprocessAllPlatforms'...");
- m_BuildEventHandler.OnPreprocessAllPlatforms(ProductName, CompanyName, GameIdentifier, GameFrameworkVersion, UnityVersion, ApplicableGameVersion, InternalResourceVersion,
- Platforms, AssetBundleCompression, CompressionHelperTypeName, AdditionalCompressionSelected, ForceRebuildAssetBundleSelected, BuildEventHandlerTypeName, OutputDirectory, buildAssetBundleOptions,
- WorkingPath, OutputPackageSelected, OutputPackagePath, OutputFullSelected, OutputFullPath, OutputPackedSelected, OutputPackedPath, BuildReportPath);
- }
- m_BuildReport.LogInfo("Start prepare resource collection...");
- if (!m_ResourceCollection.Load())
- {
- m_BuildReport.LogError("Can not parse 'ResourceCollection.xml', please use 'Resource Editor' tool first.");
- if (m_BuildEventHandler != null)
- {
- m_BuildReport.LogInfo("Execute build event handler 'OnPostprocessAllPlatforms'...");
- m_BuildEventHandler.OnPostprocessAllPlatforms(ProductName, CompanyName, GameIdentifier, GameFrameworkVersion, UnityVersion, ApplicableGameVersion, InternalResourceVersion,
- Platforms, AssetBundleCompression, CompressionHelperTypeName, AdditionalCompressionSelected, ForceRebuildAssetBundleSelected, BuildEventHandlerTypeName, OutputDirectory, buildAssetBundleOptions,
- WorkingPath, OutputPackageSelected, OutputPackagePath, OutputFullSelected, OutputFullPath, OutputPackedSelected, OutputPackedPath, BuildReportPath);
- }
- m_BuildReport.SaveReport();
- return false;
- }
- if (Platforms == Platform.Undefined)
- {
- m_BuildReport.LogError("Platform undefined.");
- if (m_BuildEventHandler != null)
- {
- m_BuildReport.LogInfo("Execute build event handler 'OnPostprocessAllPlatforms'...");
- m_BuildEventHandler.OnPostprocessAllPlatforms(ProductName, CompanyName, GameIdentifier, GameFrameworkVersion, UnityVersion, ApplicableGameVersion, InternalResourceVersion,
- Platforms, AssetBundleCompression, CompressionHelperTypeName, AdditionalCompressionSelected, ForceRebuildAssetBundleSelected, BuildEventHandlerTypeName, OutputDirectory, buildAssetBundleOptions,
- WorkingPath, OutputPackageSelected, OutputPackagePath, OutputFullSelected, OutputFullPath, OutputPackedSelected, OutputPackedPath, BuildReportPath);
- }
- m_BuildReport.SaveReport();
- return false;
- }
- m_BuildReport.LogInfo("Prepare resource collection complete.");
- m_BuildReport.LogInfo("Start analyze assets dependency...");
- m_ResourceAnalyzerController.Analyze();
- m_BuildReport.LogInfo("Analyze assets dependency complete.");
- m_BuildReport.LogInfo("Start prepare build data...");
- AssetBundleBuild[] assetBundleBuildDatas = null;
- ResourceData[] assetBundleResourceDatas = null;
- ResourceData[] binaryResourceDatas = null;
- if (!PrepareBuildData(out assetBundleBuildDatas, out assetBundleResourceDatas, out binaryResourceDatas))
- {
- m_BuildReport.LogError("Prepare resource build data failure.");
- if (m_BuildEventHandler != null)
- {
- m_BuildReport.LogInfo("Execute build event handler 'OnPostprocessAllPlatforms'...");
- m_BuildEventHandler.OnPostprocessAllPlatforms(ProductName, CompanyName, GameIdentifier, GameFrameworkVersion, UnityVersion, ApplicableGameVersion, InternalResourceVersion,
- Platforms, AssetBundleCompression, CompressionHelperTypeName, AdditionalCompressionSelected, ForceRebuildAssetBundleSelected, BuildEventHandlerTypeName, OutputDirectory, buildAssetBundleOptions,
- WorkingPath, OutputPackageSelected, OutputPackagePath, OutputFullSelected, OutputFullPath, OutputPackedSelected, OutputPackedPath, BuildReportPath);
- }
- m_BuildReport.SaveReport();
- return false;
- }
- m_BuildReport.LogInfo("Prepare resource build data complete.");
- m_BuildReport.LogInfo("Start build resources for selected platforms...");
- bool watchResult = m_BuildEventHandler == null || !m_BuildEventHandler.ContinueOnFailure;
- bool isSuccess = false;
- isSuccess = BuildResources(Platform.Windows, assetBundleBuildDatas, buildAssetBundleOptions, assetBundleResourceDatas, binaryResourceDatas);
- if (!watchResult || isSuccess)
- {
- isSuccess = BuildResources(Platform.Windows64, assetBundleBuildDatas, buildAssetBundleOptions, assetBundleResourceDatas, binaryResourceDatas);
- }
- if (!watchResult || isSuccess)
- {
- isSuccess = BuildResources(Platform.MacOS, assetBundleBuildDatas, buildAssetBundleOptions, assetBundleResourceDatas, binaryResourceDatas);
- }
- if (!watchResult || isSuccess)
- {
- isSuccess = BuildResources(Platform.Linux, assetBundleBuildDatas, buildAssetBundleOptions, assetBundleResourceDatas, binaryResourceDatas);
- }
- if (!watchResult || isSuccess)
- {
- isSuccess = BuildResources(Platform.IOS, assetBundleBuildDatas, buildAssetBundleOptions, assetBundleResourceDatas, binaryResourceDatas);
- }
- if (!watchResult || isSuccess)
- {
- isSuccess = BuildResources(Platform.Android, assetBundleBuildDatas, buildAssetBundleOptions, assetBundleResourceDatas, binaryResourceDatas);
- }
- if (!watchResult || isSuccess)
- {
- isSuccess = BuildResources(Platform.WindowsStore, assetBundleBuildDatas, buildAssetBundleOptions, assetBundleResourceDatas, binaryResourceDatas);
- }
- if (!watchResult || isSuccess)
- {
- isSuccess = BuildResources(Platform.WebGL, assetBundleBuildDatas, buildAssetBundleOptions, assetBundleResourceDatas, binaryResourceDatas);
- }
- if (m_BuildEventHandler != null)
- {
- m_BuildReport.LogInfo("Execute build event handler 'OnPostprocessAllPlatforms'...");
- m_BuildEventHandler.OnPostprocessAllPlatforms(ProductName, CompanyName, GameIdentifier, GameFrameworkVersion, UnityVersion, ApplicableGameVersion, InternalResourceVersion,
- Platforms, AssetBundleCompression, CompressionHelperTypeName, AdditionalCompressionSelected, ForceRebuildAssetBundleSelected, BuildEventHandlerTypeName, OutputDirectory, buildAssetBundleOptions,
- WorkingPath, OutputPackageSelected, OutputPackagePath, OutputFullSelected, OutputFullPath, OutputPackedSelected, OutputPackedPath, BuildReportPath);
- }
- m_BuildReport.LogInfo("Build resources for selected platforms complete.");
- m_BuildReport.SaveReport();
- return true;
- }
- catch (Exception exception)
- {
- string errorMessage = exception.ToString();
- m_BuildReport.LogFatal(errorMessage);
- m_BuildReport.SaveReport();
- if (BuildResourceError != null)
- {
- BuildResourceError(errorMessage);
- }
- return false;
- }
- finally
- {
- m_OutputPackageFileSystems.Clear();
- m_OutputPackedFileSystems.Clear();
- if (m_FileSystemManager != null)
- {
- GameFrameworkEntry.Shutdown();
- m_FileSystemManager = null;
- }
- }
- }
- private bool BuildResources(Platform platform, AssetBundleBuild[] assetBundleBuildDatas, BuildAssetBundleOptions buildAssetBundleOptions, ResourceData[] assetBundleResourceDatas, ResourceData[] binaryResourceDatas)
- {
- if (!IsPlatformSelected(platform))
- {
- return true;
- }
- string platformName = platform.ToString();
- m_BuildReport.LogInfo("Start build resources for '{0}'...", platformName);
- string workingPath = Utility.Text.Format("{0}{1}/", WorkingPath, platformName);
- m_BuildReport.LogInfo("Working path is '{0}'.", workingPath);
- string outputPackagePath = Utility.Text.Format("{0}{1}/", OutputPackagePath, platformName);
- if (OutputPackageSelected)
- {
- Directory.CreateDirectory(outputPackagePath);
- m_BuildReport.LogInfo("Output package is selected, path is '{0}'.", outputPackagePath);
- }
- else
- {
- m_BuildReport.LogInfo("Output package is not selected.");
- }
- string outputFullPath = Utility.Text.Format("{0}{1}/", OutputFullPath, platformName);
- if (OutputFullSelected)
- {
- Directory.CreateDirectory(outputFullPath);
- m_BuildReport.LogInfo("Output full is selected, path is '{0}'.", outputFullPath);
- }
- else
- {
- m_BuildReport.LogInfo("Output full is not selected.");
- }
- string outputPackedPath = Utility.Text.Format("{0}{1}/", OutputPackedPath, platformName);
- if (OutputPackedSelected)
- {
- Directory.CreateDirectory(outputPackedPath);
- m_BuildReport.LogInfo("Output packed is selected, path is '{0}'.", outputPackedPath);
- }
- else
- {
- m_BuildReport.LogInfo("Output packed is not selected.");
- }
- // Clean working path
- List<string> validNames = new List<string>();
- foreach (ResourceData assetBundleResourceData in assetBundleResourceDatas)
- {
- validNames.Add(GetResourceFullName(assetBundleResourceData.Name, assetBundleResourceData.Variant).ToLowerInvariant());
- }
- if (Directory.Exists(workingPath))
- {
- Uri workingUri = new Uri(workingPath, UriKind.Absolute);
- string[] fileNames = Directory.GetFiles(workingPath, "*", SearchOption.AllDirectories);
- foreach (string fileName in fileNames)
- {
- if (fileName.EndsWith(".manifest", StringComparison.Ordinal))
- {
- continue;
- }
- string relativeName = workingUri.MakeRelativeUri(new Uri(fileName, UriKind.Absolute)).ToString();
- if (!validNames.Contains(relativeName))
- {
- File.Delete(fileName);
- }
- }
- string[] manifestNames = Directory.GetFiles(workingPath, "*.manifest", SearchOption.AllDirectories);
- foreach (string manifestName in manifestNames)
- {
- if (!File.Exists(manifestName.Substring(0, manifestName.LastIndexOf('.'))))
- {
- File.Delete(manifestName);
- }
- }
- Utility.Path.RemoveEmptyDirectory(workingPath);
- }
- if (!Directory.Exists(workingPath))
- {
- Directory.CreateDirectory(workingPath);
- }
- if (m_BuildEventHandler != null)
- {
- m_BuildReport.LogInfo("Execute build event handler 'OnPreprocessPlatform' for '{0}'...", platformName);
- m_BuildEventHandler.OnPreprocessPlatform(platform, workingPath, OutputPackageSelected, outputPackagePath, OutputFullSelected, outputFullPath, OutputPackedSelected, outputPackedPath);
- }
- // Build AssetBundles
- m_BuildReport.LogInfo("Unity start build asset bundles for '{0}'...", platformName);
- AssetBundleManifest assetBundleManifest = BuildPipeline.BuildAssetBundles(workingPath, assetBundleBuildDatas, buildAssetBundleOptions, GetBuildTarget(platform));
- if (assetBundleManifest == null)
- {
- m_BuildReport.LogError("Build asset bundles for '{0}' failure.", platformName);
- if (m_BuildEventHandler != null)
- {
- m_BuildReport.LogInfo("Execute build event handler 'OnPostprocessPlatform' for '{0}'...", platformName);
- m_BuildEventHandler.OnPostprocessPlatform(platform, workingPath, OutputPackageSelected, outputPackagePath, OutputFullSelected, outputFullPath, OutputPackedSelected, outputPackedPath, false);
- }
- return false;
- }
- if (m_BuildEventHandler != null)
- {
- m_BuildReport.LogInfo("Execute build event handler 'OnBuildAssetBundlesComplete' for '{0}'...", platformName);
- m_BuildEventHandler.OnBuildAssetBundlesComplete(platform, workingPath, OutputPackageSelected, outputPackagePath, OutputFullSelected, outputFullPath, OutputPackedSelected, outputPackedPath, assetBundleManifest);
- }
- m_BuildReport.LogInfo("Unity build asset bundles for '{0}' complete.", platformName);
- // Create FileSystems
- m_BuildReport.LogInfo("Start create file system for '{0}'...", platformName);
- if (OutputPackageSelected)
- {
- CreateFileSystems(m_ResourceDatas.Values, outputPackagePath, m_OutputPackageFileSystems);
- }
- if (OutputPackedSelected)
- {
- CreateFileSystems(GetPackedResourceDatas(), outputPackedPath, m_OutputPackedFileSystems);
- }
- m_BuildReport.LogInfo("Create file system for '{0}' complete.", platformName);
- // Process AssetBundles
- for (int i = 0; i < assetBundleResourceDatas.Length; i++)
- {
- string fullName = GetResourceFullName(assetBundleResourceDatas[i].Name, assetBundleResourceDatas[i].Variant);
- if (ProcessingAssetBundle != null)
- {
- if (ProcessingAssetBundle(fullName, (float)(i + 1) / assetBundleResourceDatas.Length))
- {
- m_BuildReport.LogWarning("The build has been canceled by user.");
- if (m_BuildEventHandler != null)
- {
- m_BuildReport.LogInfo("Execute build event handler 'OnPostprocessPlatform' for '{0}'...", platformName);
- m_BuildEventHandler.OnPostprocessPlatform(platform, workingPath, OutputPackageSelected, outputPackagePath, OutputFullSelected, outputFullPath, OutputPackedSelected, outputPackedPath, false);
- }
- return false;
- }
- }
- m_BuildReport.LogInfo("Start process asset bundle '{0}' for '{1}'...", fullName, platformName);
- if (!ProcessAssetBundle(platform, workingPath, outputPackagePath, outputFullPath, outputPackedPath, AdditionalCompressionSelected, assetBundleResourceDatas[i].Name, assetBundleResourceDatas[i].Variant, assetBundleResourceDatas[i].FileSystem))
- {
- return false;
- }
- m_BuildReport.LogInfo("Process asset bundle '{0}' for '{1}' complete.", fullName, platformName);
- }
- // Process Binaries
- for (int i = 0; i < binaryResourceDatas.Length; i++)
- {
- string fullName = GetResourceFullName(binaryResourceDatas[i].Name, binaryResourceDatas[i].Variant);
- if (ProcessingBinary != null)
- {
- if (ProcessingBinary(fullName, (float)(i + 1) / binaryResourceDatas.Length))
- {
- m_BuildReport.LogWarning("The build has been canceled by user.");
- if (m_BuildEventHandler != null)
- {
- m_BuildReport.LogInfo("Execute build event handler 'OnPostprocessPlatform' for '{0}'...", platformName);
- m_BuildEventHandler.OnPostprocessPlatform(platform, workingPath, OutputPackageSelected, outputPackagePath, OutputFullSelected, outputFullPath, OutputPackedSelected, outputPackedPath, false);
- }
- return false;
- }
- }
- m_BuildReport.LogInfo("Start process binary '{0}' for '{1}'...", fullName, platformName);
- if (!ProcessBinary(platform, workingPath, outputPackagePath, outputFullPath, outputPackedPath, AdditionalCompressionSelected, binaryResourceDatas[i].Name, binaryResourceDatas[i].Variant, binaryResourceDatas[i].FileSystem))
- {
- return false;
- }
- m_BuildReport.LogInfo("Process binary '{0}' for '{1}' complete.", fullName, platformName);
- }
- if (OutputPackageSelected)
- {
- ProcessPackageVersionList(outputPackagePath, platform);
- m_BuildReport.LogInfo("Process package version list for '{0}' complete.", platformName);
- }
- if (OutputFullSelected)
- {
- VersionListData versionListData = ProcessUpdatableVersionList(outputFullPath, platform);
- m_BuildReport.LogInfo("Process updatable version list for '{0}' complete, updatable version list path is '{1}', length is '{2}', hash code is '{3}[0x{3:X8}]', compressed length is '{4}', compressed hash code is '{5}[0x{5:X8}]'.", platformName, versionListData.Path, versionListData.Length.ToString(), versionListData.HashCode, versionListData.CompressedLength.ToString(), versionListData.CompressedHashCode);
- if (m_BuildEventHandler != null)
- {
- m_BuildReport.LogInfo("Execute build event handler 'OnOutputUpdatableVersionListData' for '{0}'...", platformName);
- m_BuildEventHandler.OnOutputUpdatableVersionListData(platform, versionListData.Path, versionListData.Length, versionListData.HashCode, versionListData.CompressedLength, versionListData.CompressedHashCode);
- }
- }
- if (OutputPackedSelected)
- {
- ProcessReadOnlyVersionList(outputPackedPath, platform);
- m_BuildReport.LogInfo("Process read-only version list for '{0}' complete.", platformName);
- }
- if (m_BuildEventHandler != null)
- {
- m_BuildReport.LogInfo("Execute build event handler 'OnPostprocessPlatform' for '{0}'...", platformName);
- m_BuildEventHandler.OnPostprocessPlatform(platform, workingPath, OutputPackageSelected, outputPackagePath, OutputFullSelected, outputFullPath, OutputPackedSelected, outputPackedPath, true);
- }
- if (ProcessResourceComplete != null)
- {
- ProcessResourceComplete(platform);
- }
- m_BuildReport.LogInfo("Build resources for '{0}' success.", platformName);
- return true;
- }
- private bool ProcessAssetBundle(Platform platform, string workingPath, string outputPackagePath, string outputFullPath, string outputPackedPath, bool additionalCompressionSelected, string name, string variant, string fileSystem)
- {
- string fullName = GetResourceFullName(name, variant);
- ResourceData resourceData = m_ResourceDatas[fullName];
- string workingName = Utility.Path.GetRegularPath(Path.Combine(workingPath, fullName.ToLowerInvariant()));
- byte[] bytes = File.ReadAllBytes(workingName);
- int length = bytes.Length;
- int hashCode = Utility.Verifier.GetCrc32(bytes);
- int compressedLength = length;
- int compressedHashCode = hashCode;
- byte[] hashBytes = Utility.Converter.GetBytes(hashCode);
- if (resourceData.LoadType == LoadType.LoadFromMemoryAndQuickDecrypt)
- {
- bytes = Utility.Encryption.GetQuickXorBytes(bytes, hashBytes);
- }
- else if (resourceData.LoadType == LoadType.LoadFromMemoryAndDecrypt)
- {
- bytes = Utility.Encryption.GetXorBytes(bytes, hashBytes);
- }
- return ProcessOutput(platform, outputPackagePath, outputFullPath, outputPackedPath, additionalCompressionSelected, name, variant, fileSystem, resourceData, bytes, length, hashCode, compressedLength, compressedHashCode);
- }
- private bool ProcessBinary(Platform platform, string workingPath, string outputPackagePath, string outputFullPath, string outputPackedPath, bool additionalCompressionSelected, string name, string variant, string fileSystem)
- {
- string fullName = GetResourceFullName(name, variant);
- ResourceData resourceData = m_ResourceDatas[fullName];
- string assetName = resourceData.GetAssetNames()[0];
- string assetPath = Utility.Path.GetRegularPath(Application.dataPath.Substring(0, Application.dataPath.Length - AssetsStringLength) + assetName);
- byte[] bytes = File.ReadAllBytes(assetPath);
- int length = bytes.Length;
- int hashCode = Utility.Verifier.GetCrc32(bytes);
- int compressedLength = length;
- int compressedHashCode = hashCode;
- byte[] hashBytes = Utility.Converter.GetBytes(hashCode);
- if (resourceData.LoadType == LoadType.LoadFromBinaryAndQuickDecrypt)
- {
- bytes = Utility.Encryption.GetQuickXorBytes(bytes, hashBytes);
- }
- else if (resourceData.LoadType == LoadType.LoadFromBinaryAndDecrypt)
- {
- bytes = Utility.Encryption.GetXorBytes(bytes, hashBytes);
- }
- return ProcessOutput(platform, outputPackagePath, outputFullPath, outputPackedPath, additionalCompressionSelected, name, variant, fileSystem, resourceData, bytes, length, hashCode, compressedLength, compressedHashCode);
- }
- private void ProcessPackageVersionList(string outputPackagePath, Platform platform)
- {
- Asset[] originalAssets = m_ResourceCollection.GetAssets();
- PackageVersionList.Asset[] assets = new PackageVersionList.Asset[originalAssets.Length];
- for (int i = 0; i < assets.Length; i++)
- {
- Asset originalAsset = originalAssets[i];
- assets[i] = new PackageVersionList.Asset(originalAsset.Name, GetDependencyAssetIndexes(originalAsset.Name));
- }
- SortedDictionary<string, ResourceData>.ValueCollection resourceDatas = m_ResourceDatas.Values;
- int index = 0;
- PackageVersionList.Resource[] resources = new PackageVersionList.Resource[m_ResourceCollection.ResourceCount];
- foreach (ResourceData resourceData in resourceDatas)
- {
- ResourceCode resourceCode = resourceData.GetCode(platform);
- resources[index++] = new PackageVersionList.Resource(resourceData.Name, resourceData.Variant, GetExtension(resourceData), (byte)resourceData.LoadType, resourceCode.Length, resourceCode.HashCode, GetAssetIndexes(resourceData));
- }
- string[] fileSystemNames = GetFileSystemNames(resourceDatas);
- PackageVersionList.FileSystem[] fileSystems = new PackageVersionList.FileSystem[fileSystemNames.Length];
- for (int i = 0; i < fileSystems.Length; i++)
- {
- fileSystems[i] = new PackageVersionList.FileSystem(fileSystemNames[i], GetResourceIndexesFromFileSystem(resourceDatas, fileSystemNames[i]));
- }
- string[] resourceGroupNames = GetResourceGroupNames(resourceDatas);
- PackageVersionList.ResourceGroup[] resourceGroups = new PackageVersionList.ResourceGroup[resourceGroupNames.Length];
- for (int i = 0; i < resourceGroups.Length; i++)
- {
- resourceGroups[i] = new PackageVersionList.ResourceGroup(resourceGroupNames[i], GetResourceIndexesFromResourceGroup(resourceDatas, resourceGroupNames[i]));
- }
- PackageVersionList versionList = new PackageVersionList(ApplicableGameVersion, InternalResourceVersion, assets, resources, fileSystems, resourceGroups);
- PackageVersionListSerializer serializer = new PackageVersionListSerializer();
- serializer.RegisterSerializeCallback(0, BuiltinVersionListSerializer.PackageVersionListSerializeCallback_V0);
- serializer.RegisterSerializeCallback(1, BuiltinVersionListSerializer.PackageVersionListSerializeCallback_V1);
- serializer.RegisterSerializeCallback(2, BuiltinVersionListSerializer.PackageVersionListSerializeCallback_V2);
- string packageVersionListPath = Utility.Path.GetRegularPath(Path.Combine(outputPackagePath, RemoteVersionListFileName));
- using (FileStream fileStream = new FileStream(packageVersionListPath, FileMode.Create, FileAccess.Write))
- {
- if (!serializer.Serialize(fileStream, versionList))
- {
- throw new GameFrameworkException("Serialize package version list failure.");
- }
- }
- }
- private VersionListData ProcessUpdatableVersionList(string outputFullPath, Platform platform)
- {
- Asset[] originalAssets = m_ResourceCollection.GetAssets();
- UpdatableVersionList.Asset[] assets = new UpdatableVersionList.Asset[originalAssets.Length];
- for (int i = 0; i < assets.Length; i++)
- {
- Asset originalAsset = originalAssets[i];
- assets[i] = new UpdatableVersionList.Asset(originalAsset.Name, GetDependencyAssetIndexes(originalAsset.Name));
- }
- SortedDictionary<string, ResourceData>.ValueCollection resourceDatas = m_ResourceDatas.Values;
- int index = 0;
- UpdatableVersionList.Resource[] resources = new UpdatableVersionList.Resource[m_ResourceCollection.ResourceCount];
- foreach (ResourceData resourceData in resourceDatas)
- {
- ResourceCode resourceCode = resourceData.GetCode(platform);
- resources[index++] = new UpdatableVersionList.Resource(resourceData.Name, resourceData.Variant, GetExtension(resourceData), (byte)resourceData.LoadType, resourceCode.Length, resourceCode.HashCode, resourceCode.CompressedLength, resourceCode.CompressedHashCode, GetAssetIndexes(resourceData));
- }
- string[] fileSystemNames = GetFileSystemNames(resourceDatas);
- UpdatableVersionList.FileSystem[] fileSystems = new UpdatableVersionList.FileSystem[fileSystemNames.Length];
- for (int i = 0; i < fileSystems.Length; i++)
- {
- fileSystems[i] = new UpdatableVersionList.FileSystem(fileSystemNames[i], GetResourceIndexesFromFileSystem(resourceDatas, fileSystemNames[i]));
- }
- string[] resourceGroupNames = GetResourceGroupNames(resourceDatas);
- UpdatableVersionList.ResourceGroup[] resourceGroups = new UpdatableVersionList.ResourceGroup[resourceGroupNames.Length];
- for (int i = 0; i < resourceGroups.Length; i++)
- {
- resourceGroups[i] = new UpdatableVersionList.ResourceGroup(resourceGroupNames[i], GetResourceIndexesFromResourceGroup(resourceDatas, resourceGroupNames[i]));
- }
- UpdatableVersionList versionList = new UpdatableVersionList(ApplicableGameVersion, InternalResourceVersion, assets, resources, fileSystems, resourceGroups);
- UpdatableVersionListSerializer serializer = new UpdatableVersionListSerializer();
- serializer.RegisterSerializeCallback(0, BuiltinVersionListSerializer.UpdatableVersionListSerializeCallback_V0);
- serializer.RegisterSerializeCallback(1, BuiltinVersionListSerializer.UpdatableVersionListSerializeCallback_V1);
- serializer.RegisterSerializeCallback(2, BuiltinVersionListSerializer.UpdatableVersionListSerializeCallback_V2);
- string updatableVersionListPath = Utility.Path.GetRegularPath(Path.Combine(outputFullPath, RemoteVersionListFileName));
- using (FileStream fileStream = new FileStream(updatableVersionListPath, FileMode.Create, FileAccess.Write))
- {
- if (!serializer.Serialize(fileStream, versionList))
- {
- throw new GameFrameworkException("Serialize updatable version list failure.");
- }
- }
- byte[] bytes = File.ReadAllBytes(updatableVersionListPath);
- int length = bytes.Length;
- int hashCode = Utility.Verifier.GetCrc32(bytes);
- bytes = Utility.Compression.Compress(bytes);
- int compressedLength = bytes.Length;
- File.WriteAllBytes(updatableVersionListPath, bytes);
- int compressedHashCode = Utility.Verifier.GetCrc32(bytes);
- int dotPosition = RemoteVersionListFileName.LastIndexOf('.');
- string versionListFullNameWithCrc32 = Utility.Text.Format("{0}.{2:x8}.{1}", RemoteVersionListFileName.Substring(0, dotPosition), RemoteVersionListFileName.Substring(dotPosition + 1), hashCode);
- string updatableVersionListPathWithCrc32 = Utility.Path.GetRegularPath(Path.Combine(outputFullPath, versionListFullNameWithCrc32));
- File.Move(updatableVersionListPath, updatableVersionListPathWithCrc32);
- return new VersionListData(updatableVersionListPathWithCrc32, length, hashCode, compressedLength, compressedHashCode);
- }
- private void ProcessReadOnlyVersionList(string outputPackedPath, Platform platform)
- {
- ResourceData[] packedResourceDatas = GetPackedResourceDatas();
- LocalVersionList.Resource[] resources = new LocalVersionList.Resource[packedResourceDatas.Length];
- for (int i = 0; i < resources.Length; i++)
- {
- ResourceData resourceData = packedResourceDatas[i];
- ResourceCode resourceCode = resourceData.GetCode(platform);
- resources[i] = new LocalVersionList.Resource(resourceData.Name, resourceData.Variant, GetExtension(resourceData), (byte)resourceData.LoadType, resourceCode.Length, resourceCode.HashCode);
- }
- string[] packedFileSystemNames = GetFileSystemNames(packedResourceDatas);
- LocalVersionList.FileSystem[] fileSystems = new LocalVersionList.FileSystem[packedFileSystemNames.Length];
- for (int i = 0; i < fileSystems.Length; i++)
- {
- fileSystems[i] = new LocalVersionList.FileSystem(packedFileSystemNames[i], GetResourceIndexesFromFileSystem(packedResourceDatas, packedFileSystemNames[i]));
- }
- LocalVersionList versionList = new LocalVersionList(resources, fileSystems);
- ReadOnlyVersionListSerializer serializer = new ReadOnlyVersionListSerializer();
- serializer.RegisterSerializeCallback(0, BuiltinVersionListSerializer.LocalVersionListSerializeCallback_V0);
- serializer.RegisterSerializeCallback(1, BuiltinVersionListSerializer.LocalVersionListSerializeCallback_V1);
- serializer.RegisterSerializeCallback(2, BuiltinVersionListSerializer.LocalVersionListSerializeCallback_V2);
- string readOnlyVersionListPath = Utility.Path.GetRegularPath(Path.Combine(outputPackedPath, LocalVersionListFileName));
- using (FileStream fileStream = new FileStream(readOnlyVersionListPath, FileMode.Create, FileAccess.Write))
- {
- if (!serializer.Serialize(fileStream, versionList))
- {
- throw new GameFrameworkException("Serialize read-only version list failure.");
- }
- }
- }
- private int[] GetDependencyAssetIndexes(string assetName)
- {
- List<int> dependencyAssetIndexes = new List<int>();
- Asset[] assets = m_ResourceCollection.GetAssets();
- DependencyData dependencyData = m_ResourceAnalyzerController.GetDependencyData(assetName);
- foreach (Asset dependencyAsset in dependencyData.GetDependencyAssets())
- {
- for (int i = 0; i < assets.Length; i++)
- {
- if (assets[i] == dependencyAsset)
- {
- dependencyAssetIndexes.Add(i);
- break;
- }
- }
- }
- dependencyAssetIndexes.Sort();
- return dependencyAssetIndexes.ToArray();
- }
- private int[] GetAssetIndexes(ResourceData resourceData)
- {
- Asset[] assets = m_ResourceCollection.GetAssets();
- string[] assetGuids = resourceData.GetAssetGuids();
- int[] assetIndexes = new int[assetGuids.Length];
- for (int i = 0; i < assetGuids.Length; i++)
- {
- assetIndexes[i] = Array.BinarySearch(assets, m_ResourceCollection.GetAsset(assetGuids[i]));
- if (assetIndexes[i] < 0)
- {
- throw new GameFrameworkException("Asset is invalid.");
- }
- }
- return assetIndexes;
- }
- private ResourceData[] GetPackedResourceDatas()
- {
- List<ResourceData> packedResourceDatas = new List<ResourceData>();
- foreach (ResourceData resourceData in m_ResourceDatas.Values)
- {
- if (!resourceData.Packed)
- {
- continue;
- }
- packedResourceDatas.Add(resourceData);
- }
- return packedResourceDatas.ToArray();
- }
- private string[] GetFileSystemNames(IEnumerable<ResourceData> resourceDatas)
- {
- HashSet<string> fileSystemNames = new HashSet<string>();
- foreach (ResourceData resourceData in resourceDatas)
- {
- if (resourceData.FileSystem == null)
- {
- continue;
- }
- fileSystemNames.Add(resourceData.FileSystem);
- }
- return fileSystemNames.OrderBy(x => x).ToArray();
- }
- private int[] GetResourceIndexesFromFileSystem(IEnumerable<ResourceData> resourceDatas, string fileSystemName)
- {
- int index = 0;
- List<int> resourceIndexes = new List<int>();
- foreach (ResourceData resourceData in resourceDatas)
- {
- if (resourceData.FileSystem == fileSystemName)
- {
- resourceIndexes.Add(index);
- }
- index++;
- }
- resourceIndexes.Sort();
- return resourceIndexes.ToArray();
- }
- private string[] GetResourceGroupNames(IEnumerable<ResourceData> resourceDatas)
- {
- HashSet<string> resourceGroupNames = new HashSet<string>();
- foreach (ResourceData resourceData in resourceDatas)
- {
- foreach (string resourceGroup in resourceData.GetResourceGroups())
- {
- resourceGroupNames.Add(resourceGroup);
- }
- }
- return resourceGroupNames.OrderBy(x => x).ToArray();
- }
- private int[] GetResourceIndexesFromResourceGroup(IEnumerable<ResourceData> resourceDatas, string resourceGroupName)
- {
- int index = 0;
- List<int> resourceIndexes = new List<int>();
- foreach (ResourceData resourceData in resourceDatas)
- {
- foreach (string resourceGroup in resourceData.GetResourceGroups())
- {
- if (resourceGroup == resourceGroupName)
- {
- resourceIndexes.Add(index);
- break;
- }
- }
- index++;
- }
- resourceIndexes.Sort();
- return resourceIndexes.ToArray();
- }
- private void CreateFileSystems(IEnumerable<ResourceData> resourceDatas, string outputPath, Dictionary<string, IFileSystem> outputFileSystem)
- {
- outputFileSystem.Clear();
- string[] fileSystemNames = GetFileSystemNames(resourceDatas);
- if (fileSystemNames.Length > 0 && m_FileSystemManager == null)
- {
- m_FileSystemManager = GameFrameworkEntry.GetModule<IFileSystemManager>();
- m_FileSystemManager.SetFileSystemHelper(new FileSystemHelper());
- }
- foreach (string fileSystemName in fileSystemNames)
- {
- int fileCount = GetResourceIndexesFromFileSystem(resourceDatas, fileSystemName).Length;
- string fullPath = Utility.Path.GetRegularPath(Path.Combine(outputPath, Utility.Text.Format("{0}.{1}", fileSystemName, DefaultExtension)));
- string directory = Path.GetDirectoryName(fullPath);
- if (!Directory.Exists(directory))
- {
- Directory.CreateDirectory(directory);
- }
- IFileSystem fileSystem = m_FileSystemManager.CreateFileSystem(fullPath, FileSystemAccess.Write, fileCount, fileCount);
- outputFileSystem.Add(fileSystemName, fileSystem);
- }
- }
- private bool ProcessOutput(Platform platform, string outputPackagePath, string outputFullPath, string outputPackedPath, bool additionalCompressionSelected, string name, string variant, string fileSystem, ResourceData resourceData, byte[] bytes, int length, int hashCode, int compressedLength, int compressedHashCode)
- {
- string fullNameWithExtension = Utility.Text.Format("{0}.{1}", GetResourceFullName(name, variant), GetExtension(resourceData));
- if (OutputPackageSelected)
- {
- if (string.IsNullOrEmpty(fileSystem))
- {
- string packagePath = Utility.Path.GetRegularPath(Path.Combine(outputPackagePath, fullNameWithExtension));
- string packageDirectoryName = Path.GetDirectoryName(packagePath);
- if (!Directory.Exists(packageDirectoryName))
- {
- Directory.CreateDirectory(packageDirectoryName);
- }
- File.WriteAllBytes(packagePath, bytes);
- }
- else
- {
- if (!m_OutputPackageFileSystems[fileSystem].WriteFile(fullNameWithExtension, bytes))
- {
- return false;
- }
- }
- }
- if (OutputPackedSelected && resourceData.Packed)
- {
- if (string.IsNullOrEmpty(fileSystem))
- {
- string packedPath = Utility.Path.GetRegularPath(Path.Combine(outputPackedPath, fullNameWithExtension));
- string packedDirectoryName = Path.GetDirectoryName(packedPath);
- if (!Directory.Exists(packedDirectoryName))
- {
- Directory.CreateDirectory(packedDirectoryName);
- }
- File.WriteAllBytes(packedPath, bytes);
- }
- else
- {
- if (!m_OutputPackedFileSystems[fileSystem].WriteFile(fullNameWithExtension, bytes))
- {
- return false;
- }
- }
- }
- if (OutputFullSelected)
- {
- string fullNameWithCrc32AndExtension = variant != null ? Utility.Text.Format("{0}.{1}.{2:x8}.{3}", name, variant, hashCode, DefaultExtension) : Utility.Text.Format("{0}.{1:x8}.{2}", name, hashCode, DefaultExtension);
- string fullPath = Utility.Path.GetRegularPath(Path.Combine(outputFullPath, fullNameWithCrc32AndExtension));
- string fullDirectoryName = Path.GetDirectoryName(fullPath);
- if (!Directory.Exists(fullDirectoryName))
- {
- Directory.CreateDirectory(fullDirectoryName);
- }
- if (additionalCompressionSelected)
- {
- byte[] compressedBytes = Utility.Compression.Compress(bytes);
- compressedLength = compressedBytes.Length;
- compressedHashCode = Utility.Verifier.GetCrc32(compressedBytes);
- File.WriteAllBytes(fullPath, compressedBytes);
- }
- else
- {
- File.WriteAllBytes(fullPath, bytes);
- }
- }
- resourceData.AddCode(platform, length, hashCode, compressedLength, compressedHashCode);
- return true;
- }
- private BuildAssetBundleOptions GetBuildAssetBundleOptions()
- {
- BuildAssetBundleOptions buildOptions = BuildAssetBundleOptions.DeterministicAssetBundle;
- if (ForceRebuildAssetBundleSelected)
- {
- buildOptions |= BuildAssetBundleOptions.ForceRebuildAssetBundle;
- }
- if (AssetBundleCompression == AssetBundleCompressionType.Uncompressed)
- {
- buildOptions |= BuildAssetBundleOptions.UncompressedAssetBundle;
- }
- else if (AssetBundleCompression == AssetBundleCompressionType.LZ4)
- {
- buildOptions |= BuildAssetBundleOptions.ChunkBasedCompression;
- }
- return buildOptions;
- }
- private bool PrepareBuildData(out AssetBundleBuild[] assetBundleBuildDatas, out ResourceData[] assetBundleResourceDatas, out ResourceData[] binaryResourceDatas)
- {
- assetBundleBuildDatas = null;
- assetBundleResourceDatas = null;
- binaryResourceDatas = null;
- m_ResourceDatas.Clear();
- Resource[] resources = m_ResourceCollection.GetResources();
- foreach (Resource resource in resources)
- {
- m_ResourceDatas.Add(resource.FullName, new ResourceData(resource.Name, resource.Variant, resource.FileSystem, resource.LoadType, resource.Packed, resource.GetResourceGroups()));
- }
- Asset[] assets = m_ResourceCollection.GetAssets();
- foreach (Asset asset in assets)
- {
- string assetName = asset.Name;
- if (string.IsNullOrEmpty(assetName))
- {
- m_BuildReport.LogError("Can not find asset by guid '{0}'.", asset.Guid);
- return false;
- }
- string assetFileFullName = Application.dataPath.Substring(0, Application.dataPath.Length - AssetsStringLength) + assetName;
- if (!File.Exists(assetFileFullName))
- {
- m_BuildReport.LogError("Can not find asset '{0}'.", assetFileFullName);
- return false;
- }
- byte[] assetBytes = File.ReadAllBytes(assetFileFullName);
- int assetHashCode = Utility.Verifier.GetCrc32(assetBytes);
- List<string> dependencyAssetNames = new List<string>();
- DependencyData dependencyData = m_ResourceAnalyzerController.GetDependencyData(assetName);
- Asset[] dependencyAssets = dependencyData.GetDependencyAssets();
- foreach (Asset dependencyAsset in dependencyAssets)
- {
- dependencyAssetNames.Add(dependencyAsset.Name);
- }
- dependencyAssetNames.Sort();
- m_ResourceDatas[asset.Resource.FullName].AddAssetData(asset.Guid, assetName, assetBytes.Length, assetHashCode, dependencyAssetNames.ToArray());
- }
- List<AssetBundleBuild> assetBundleBuildDataList = new List<AssetBundleBuild>();
- List<ResourceData> assetBundleResourceDataList = new List<ResourceData>();
- List<ResourceData> binaryResourceDataList = new List<ResourceData>();
- foreach (ResourceData resourceData in m_ResourceDatas.Values)
- {
- if (resourceData.AssetCount <= 0)
- {
- m_BuildReport.LogError("Resource '{0}' has no asset.", GetResourceFullName(resourceData.Name, resourceData.Variant));
- return false;
- }
- if (resourceData.IsLoadFromBinary)
- {
- binaryResourceDataList.Add(resourceData);
- }
- else
- {
- assetBundleResourceDataList.Add(resourceData);
- AssetBundleBuild build = new AssetBundleBuild();
- build.assetBundleName = resourceData.Name;
- build.assetBundleVariant = resourceData.Variant;
- build.assetNames = resourceData.GetAssetNames();
- assetBundleBuildDataList.Add(build);
- }
- }
- assetBundleBuildDatas = assetBundleBuildDataList.ToArray();
- assetBundleResourceDatas = assetBundleResourceDataList.ToArray();
- binaryResourceDatas = binaryResourceDataList.ToArray();
- return true;
- }
- private static string GetResourceFullName(string name, string variant)
- {
- return !string.IsNullOrEmpty(variant) ? Utility.Text.Format("{0}.{1}", name, variant) : name;
- }
- private static BuildTarget GetBuildTarget(Platform platform)
- {
- switch (platform)
- {
- case Platform.Windows:
- return BuildTarget.StandaloneWindows;
- case Platform.Windows64:
- return BuildTarget.StandaloneWindows64;
- case Platform.MacOS:
- #if UNITY_2017_3_OR_NEWER
- return BuildTarget.StandaloneOSX;
- #else
- return BuildTarget.StandaloneOSXUniversal;
- #endif
- case Platform.Linux:
- return BuildTarget.StandaloneLinux64;
- case Platform.IOS:
- return BuildTarget.iOS;
- case Platform.Android:
- return BuildTarget.Android;
- case Platform.WindowsStore:
- return BuildTarget.WSAPlayer;
- case Platform.WebGL:
- return BuildTarget.WebGL;
- default:
- throw new GameFrameworkException("Platform is invalid.");
- }
- }
- private static string GetExtension(ResourceData data)
- {
- if (data.IsLoadFromBinary)
- {
- string assetName = data.GetAssetNames()[0];
- int position = assetName.LastIndexOf('.');
- if (position >= 0)
- {
- return assetName.Substring(position + 1);
- }
- }
- return DefaultExtension;
- }
- }
- }
|