ResourceBuilderController.BuildReport.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. //------------------------------------------------------------
  2. // Game Framework
  3. // Copyright © 2013-2021 loyalsoft. All rights reserved.
  4. // Homepage: http://www.game7000.com/
  5. // Feedback: http://www.game7000.com/
  6. //------------------------------------------------------------
  7. using GameFramework;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.IO;
  11. using System.Text;
  12. using System.Xml;
  13. using UnityEditor;
  14. namespace UnityGameFramework.Editor.ResourceTools
  15. {
  16. public sealed partial class ResourceBuilderController
  17. {
  18. private sealed class BuildReport
  19. {
  20. private const string BuildReportName = "BuildReport.xml";
  21. private const string BuildLogName = "BuildLog.txt";
  22. private string m_BuildReportName = null;
  23. private string m_BuildLogName = null;
  24. private string m_ProductName = null;
  25. private string m_CompanyName = null;
  26. private string m_GameIdentifier = null;
  27. private string m_GameFrameworkVersion = null;
  28. private string m_UnityVersion = null;
  29. private string m_ApplicableGameVersion = null;
  30. private int m_InternalResourceVersion = 0;
  31. private Platform m_Platforms = Platform.Undefined;
  32. private AssetBundleCompressionType m_AssetBundleCompression;
  33. private string m_CompressionHelperTypeName;
  34. private bool m_AdditionalCompressionSelected = false;
  35. private bool m_ForceRebuildAssetBundleSelected = false;
  36. private string m_BuildEventHandlerTypeName;
  37. private string m_OutputDirectory;
  38. private BuildAssetBundleOptions m_BuildAssetBundleOptions = BuildAssetBundleOptions.None;
  39. private StringBuilder m_LogBuilder = null;
  40. private SortedDictionary<string, ResourceData> m_ResourceDatas = null;
  41. public void Initialize(string buildReportPath, string productName, string companyName, string gameIdentifier, string gameFrameworkVersion, string unityVersion, string applicableGameVersion, int internalResourceVersion,
  42. Platform platforms, AssetBundleCompressionType assetBundleCompression, string compressionHelperTypeName, bool additionalCompressionSelected, bool forceRebuildAssetBundleSelected, string buildEventHandlerTypeName, string outputDirectory, BuildAssetBundleOptions buildAssetBundleOptions, SortedDictionary<string, ResourceData> resourceDatas)
  43. {
  44. if (string.IsNullOrEmpty(buildReportPath))
  45. {
  46. throw new GameFrameworkException("Build report path is invalid.");
  47. }
  48. m_BuildReportName = Utility.Path.GetRegularPath(Path.Combine(buildReportPath, BuildReportName));
  49. m_BuildLogName = Utility.Path.GetRegularPath(Path.Combine(buildReportPath, BuildLogName));
  50. m_ProductName = productName;
  51. m_CompanyName = companyName;
  52. m_GameIdentifier = gameIdentifier;
  53. m_GameFrameworkVersion = gameFrameworkVersion;
  54. m_UnityVersion = unityVersion;
  55. m_ApplicableGameVersion = applicableGameVersion;
  56. m_InternalResourceVersion = internalResourceVersion;
  57. m_Platforms = platforms;
  58. m_AssetBundleCompression = assetBundleCompression;
  59. m_CompressionHelperTypeName = compressionHelperTypeName;
  60. m_AdditionalCompressionSelected = additionalCompressionSelected;
  61. m_ForceRebuildAssetBundleSelected = forceRebuildAssetBundleSelected;
  62. m_BuildEventHandlerTypeName = buildEventHandlerTypeName;
  63. m_OutputDirectory = outputDirectory;
  64. m_BuildAssetBundleOptions = buildAssetBundleOptions;
  65. m_LogBuilder = new StringBuilder();
  66. m_ResourceDatas = resourceDatas;
  67. }
  68. public void LogInfo(string format, params object[] args)
  69. {
  70. LogInternal("INFO", format, args);
  71. }
  72. public void LogWarning(string format, params object[] args)
  73. {
  74. LogInternal("WARNING", format, args);
  75. }
  76. public void LogError(string format, params object[] args)
  77. {
  78. LogInternal("ERROR", format, args);
  79. }
  80. public void LogFatal(string format, params object[] args)
  81. {
  82. LogInternal("FATAL", format, args);
  83. }
  84. public void SaveReport()
  85. {
  86. XmlElement xmlElement = null;
  87. XmlAttribute xmlAttribute = null;
  88. XmlDocument xmlDocument = new XmlDocument();
  89. xmlDocument.AppendChild(xmlDocument.CreateXmlDeclaration("1.0", "UTF-8", null));
  90. XmlElement xmlRoot = xmlDocument.CreateElement("UnityGameFramework");
  91. xmlDocument.AppendChild(xmlRoot);
  92. XmlElement xmlBuildReport = xmlDocument.CreateElement("BuildReport");
  93. xmlRoot.AppendChild(xmlBuildReport);
  94. XmlElement xmlSummary = xmlDocument.CreateElement("Summary");
  95. xmlBuildReport.AppendChild(xmlSummary);
  96. xmlElement = xmlDocument.CreateElement("ProductName");
  97. xmlElement.InnerText = m_ProductName;
  98. xmlSummary.AppendChild(xmlElement);
  99. xmlElement = xmlDocument.CreateElement("CompanyName");
  100. xmlElement.InnerText = m_CompanyName;
  101. xmlSummary.AppendChild(xmlElement);
  102. xmlElement = xmlDocument.CreateElement("GameIdentifier");
  103. xmlElement.InnerText = m_GameIdentifier;
  104. xmlSummary.AppendChild(xmlElement);
  105. xmlElement = xmlDocument.CreateElement("GameFrameworkVersion");
  106. xmlElement.InnerText = m_GameFrameworkVersion;
  107. xmlSummary.AppendChild(xmlElement);
  108. xmlElement = xmlDocument.CreateElement("UnityVersion");
  109. xmlElement.InnerText = m_UnityVersion;
  110. xmlSummary.AppendChild(xmlElement);
  111. xmlElement = xmlDocument.CreateElement("ApplicableGameVersion");
  112. xmlElement.InnerText = m_ApplicableGameVersion;
  113. xmlSummary.AppendChild(xmlElement);
  114. xmlElement = xmlDocument.CreateElement("InternalResourceVersion");
  115. xmlElement.InnerText = m_InternalResourceVersion.ToString();
  116. xmlSummary.AppendChild(xmlElement);
  117. xmlElement = xmlDocument.CreateElement("Platforms");
  118. xmlElement.InnerText = m_Platforms.ToString();
  119. xmlSummary.AppendChild(xmlElement);
  120. xmlElement = xmlDocument.CreateElement("AssetBundleCompression");
  121. xmlElement.InnerText = m_AssetBundleCompression.ToString();
  122. xmlSummary.AppendChild(xmlElement);
  123. xmlElement = xmlDocument.CreateElement("CompressionHelperTypeName");
  124. xmlElement.InnerText = m_CompressionHelperTypeName;
  125. xmlSummary.AppendChild(xmlElement);
  126. xmlElement = xmlDocument.CreateElement("AdditionalCompressionSelected");
  127. xmlElement.InnerText = m_AdditionalCompressionSelected.ToString();
  128. xmlSummary.AppendChild(xmlElement);
  129. xmlElement = xmlDocument.CreateElement("ForceRebuildAssetBundleSelected");
  130. xmlElement.InnerText = m_ForceRebuildAssetBundleSelected.ToString();
  131. xmlSummary.AppendChild(xmlElement);
  132. xmlElement = xmlDocument.CreateElement("BuildEventHandlerTypeName");
  133. xmlElement.InnerText = m_BuildEventHandlerTypeName;
  134. xmlSummary.AppendChild(xmlElement);
  135. xmlElement = xmlDocument.CreateElement("OutputDirectory");
  136. xmlElement.InnerText = m_OutputDirectory;
  137. xmlSummary.AppendChild(xmlElement);
  138. xmlElement = xmlDocument.CreateElement("BuildAssetBundleOptions");
  139. xmlElement.InnerText = m_BuildAssetBundleOptions.ToString();
  140. xmlSummary.AppendChild(xmlElement);
  141. XmlElement xmlResources = xmlDocument.CreateElement("Resources");
  142. xmlAttribute = xmlDocument.CreateAttribute("Count");
  143. xmlAttribute.Value = m_ResourceDatas.Count.ToString();
  144. xmlResources.Attributes.SetNamedItem(xmlAttribute);
  145. xmlBuildReport.AppendChild(xmlResources);
  146. foreach (ResourceData resourceData in m_ResourceDatas.Values)
  147. {
  148. XmlElement xmlResource = xmlDocument.CreateElement("Resource");
  149. xmlAttribute = xmlDocument.CreateAttribute("Name");
  150. xmlAttribute.Value = resourceData.Name;
  151. xmlResource.Attributes.SetNamedItem(xmlAttribute);
  152. if (resourceData.Variant != null)
  153. {
  154. xmlAttribute = xmlDocument.CreateAttribute("Variant");
  155. xmlAttribute.Value = resourceData.Variant;
  156. xmlResource.Attributes.SetNamedItem(xmlAttribute);
  157. }
  158. xmlAttribute = xmlDocument.CreateAttribute("Extension");
  159. xmlAttribute.Value = GetExtension(resourceData);
  160. xmlResource.Attributes.SetNamedItem(xmlAttribute);
  161. if (resourceData.FileSystem != null)
  162. {
  163. xmlAttribute = xmlDocument.CreateAttribute("FileSystem");
  164. xmlAttribute.Value = resourceData.FileSystem;
  165. xmlResource.Attributes.SetNamedItem(xmlAttribute);
  166. }
  167. xmlAttribute = xmlDocument.CreateAttribute("LoadType");
  168. xmlAttribute.Value = ((byte)resourceData.LoadType).ToString();
  169. xmlResource.Attributes.SetNamedItem(xmlAttribute);
  170. xmlAttribute = xmlDocument.CreateAttribute("Packed");
  171. xmlAttribute.Value = resourceData.Packed.ToString();
  172. xmlResource.Attributes.SetNamedItem(xmlAttribute);
  173. string[] resourceGroups = resourceData.GetResourceGroups();
  174. if (resourceGroups.Length > 0)
  175. {
  176. xmlAttribute = xmlDocument.CreateAttribute("ResourceGroups");
  177. xmlAttribute.Value = string.Join(",", resourceGroups);
  178. xmlResource.Attributes.SetNamedItem(xmlAttribute);
  179. }
  180. xmlResources.AppendChild(xmlResource);
  181. AssetData[] assetDatas = resourceData.GetAssetDatas();
  182. XmlElement xmlAssets = xmlDocument.CreateElement("Assets");
  183. xmlAttribute = xmlDocument.CreateAttribute("Count");
  184. xmlAttribute.Value = assetDatas.Length.ToString();
  185. xmlAssets.Attributes.SetNamedItem(xmlAttribute);
  186. xmlResource.AppendChild(xmlAssets);
  187. foreach (AssetData assetData in assetDatas)
  188. {
  189. XmlElement xmlAsset = xmlDocument.CreateElement("Asset");
  190. xmlAttribute = xmlDocument.CreateAttribute("Guid");
  191. xmlAttribute.Value = assetData.Guid;
  192. xmlAsset.Attributes.SetNamedItem(xmlAttribute);
  193. xmlAttribute = xmlDocument.CreateAttribute("Name");
  194. xmlAttribute.Value = assetData.Name;
  195. xmlAsset.Attributes.SetNamedItem(xmlAttribute);
  196. xmlAttribute = xmlDocument.CreateAttribute("Length");
  197. xmlAttribute.Value = assetData.Length.ToString();
  198. xmlAsset.Attributes.SetNamedItem(xmlAttribute);
  199. xmlAttribute = xmlDocument.CreateAttribute("HashCode");
  200. xmlAttribute.Value = assetData.HashCode.ToString();
  201. xmlAsset.Attributes.SetNamedItem(xmlAttribute);
  202. xmlAssets.AppendChild(xmlAsset);
  203. string[] dependencyAssetNames = assetData.GetDependencyAssetNames();
  204. if (dependencyAssetNames.Length > 0)
  205. {
  206. XmlElement xmlDependencyAssets = xmlDocument.CreateElement("DependencyAssets");
  207. xmlAttribute = xmlDocument.CreateAttribute("Count");
  208. xmlAttribute.Value = dependencyAssetNames.Length.ToString();
  209. xmlDependencyAssets.Attributes.SetNamedItem(xmlAttribute);
  210. xmlAsset.AppendChild(xmlDependencyAssets);
  211. foreach (string dependencyAssetName in dependencyAssetNames)
  212. {
  213. XmlElement xmlDependencyAsset = xmlDocument.CreateElement("DependencyAsset");
  214. xmlAttribute = xmlDocument.CreateAttribute("Name");
  215. xmlAttribute.Value = dependencyAssetName;
  216. xmlDependencyAsset.Attributes.SetNamedItem(xmlAttribute);
  217. xmlDependencyAssets.AppendChild(xmlDependencyAsset);
  218. }
  219. }
  220. }
  221. XmlElement xmlCodes = xmlDocument.CreateElement("Codes");
  222. xmlResource.AppendChild(xmlCodes);
  223. foreach (ResourceCode resourceCode in resourceData.GetCodes())
  224. {
  225. XmlElement xmlCode = xmlDocument.CreateElement(resourceCode.Platform.ToString());
  226. xmlAttribute = xmlDocument.CreateAttribute("Length");
  227. xmlAttribute.Value = resourceCode.Length.ToString();
  228. xmlCode.Attributes.SetNamedItem(xmlAttribute);
  229. xmlAttribute = xmlDocument.CreateAttribute("HashCode");
  230. xmlAttribute.Value = resourceCode.HashCode.ToString();
  231. xmlCode.Attributes.SetNamedItem(xmlAttribute);
  232. xmlAttribute = xmlDocument.CreateAttribute("CompressedLength");
  233. xmlAttribute.Value = resourceCode.CompressedLength.ToString();
  234. xmlCode.Attributes.SetNamedItem(xmlAttribute);
  235. xmlAttribute = xmlDocument.CreateAttribute("CompressedHashCode");
  236. xmlAttribute.Value = resourceCode.CompressedHashCode.ToString();
  237. xmlCode.Attributes.SetNamedItem(xmlAttribute);
  238. xmlCodes.AppendChild(xmlCode);
  239. }
  240. }
  241. xmlDocument.Save(m_BuildReportName);
  242. File.WriteAllText(m_BuildLogName, m_LogBuilder.ToString());
  243. }
  244. private void LogInternal(string type, string format, object[] args)
  245. {
  246. m_LogBuilder.Append("[");
  247. m_LogBuilder.Append(DateTime.UtcNow.ToLocalTime().ToString("HH:mm:ss.fff"));
  248. m_LogBuilder.Append("][");
  249. m_LogBuilder.Append(type);
  250. m_LogBuilder.Append("] ");
  251. m_LogBuilder.AppendFormat(format, args);
  252. m_LogBuilder.AppendLine();
  253. }
  254. }
  255. }
  256. }