ResourcePackBuilderController.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  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 GameFramework.Resource;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.IO;
  12. using System.Xml;
  13. using UnityEditor;
  14. using UnityEngine;
  15. using UnityGameFramework.Runtime;
  16. namespace UnityGameFramework.Editor.ResourceTools
  17. {
  18. public sealed class ResourcePackBuilderController
  19. {
  20. private const string DefaultResourcePackName = "GameFrameworkResourcePack";
  21. private const string DefaultExtension = "dat";
  22. private const string NoneOptionName = "<None>";
  23. private static readonly string[] EmptyStringArray = new string[0];
  24. private static readonly UpdatableVersionList.Resource[] EmptyResourceArray = new UpdatableVersionList.Resource[0];
  25. private readonly string m_ConfigurationPath;
  26. private readonly List<string> m_CompressionHelperTypeNames;
  27. private readonly UpdatableVersionListSerializer m_UpdatableVersionListSerializer;
  28. private readonly ResourcePackVersionListSerializer m_ResourcePackVersionListSerializer;
  29. public ResourcePackBuilderController()
  30. {
  31. m_ConfigurationPath = Type.GetConfigurationPath<ResourceBuilderConfigPathAttribute>() ?? Utility.Path.GetRegularPath(Path.Combine(Application.dataPath, "GameFramework/Configs/ResourceBuilder.xml"));
  32. m_UpdatableVersionListSerializer = new UpdatableVersionListSerializer();
  33. m_UpdatableVersionListSerializer.RegisterDeserializeCallback(0, BuiltinVersionListSerializer.UpdatableVersionListDeserializeCallback_V0);
  34. m_UpdatableVersionListSerializer.RegisterDeserializeCallback(1, BuiltinVersionListSerializer.UpdatableVersionListDeserializeCallback_V1);
  35. m_UpdatableVersionListSerializer.RegisterDeserializeCallback(2, BuiltinVersionListSerializer.UpdatableVersionListDeserializeCallback_V2);
  36. m_ResourcePackVersionListSerializer = new ResourcePackVersionListSerializer();
  37. m_ResourcePackVersionListSerializer.RegisterSerializeCallback(0, BuiltinVersionListSerializer.ResourcePackVersionListSerializeCallback_V0);
  38. m_CompressionHelperTypeNames = new List<string>
  39. {
  40. NoneOptionName
  41. };
  42. m_CompressionHelperTypeNames.AddRange(Type.GetRuntimeOrEditorTypeNames(typeof(Utility.Compression.ICompressionHelper)));
  43. Platform = Platform.Windows;
  44. CompressionHelperTypeName = string.Empty;
  45. }
  46. public string ProductName
  47. {
  48. get
  49. {
  50. return PlayerSettings.productName;
  51. }
  52. }
  53. public string CompanyName
  54. {
  55. get
  56. {
  57. return PlayerSettings.companyName;
  58. }
  59. }
  60. public string GameIdentifier
  61. {
  62. get
  63. {
  64. #if UNITY_5_6_OR_NEWER
  65. return PlayerSettings.applicationIdentifier;
  66. #else
  67. return PlayerSettings.bundleIdentifier;
  68. #endif
  69. }
  70. }
  71. public string GameFrameworkVersion
  72. {
  73. get
  74. {
  75. return GameFramework.Version.GameFrameworkVersion;
  76. }
  77. }
  78. public string UnityVersion
  79. {
  80. get
  81. {
  82. return Application.unityVersion;
  83. }
  84. }
  85. public string ApplicableGameVersion
  86. {
  87. get
  88. {
  89. return Application.version;
  90. }
  91. }
  92. public string WorkingDirectory
  93. {
  94. get;
  95. set;
  96. }
  97. public Platform Platform
  98. {
  99. get;
  100. set;
  101. }
  102. public string CompressionHelperTypeName
  103. {
  104. get;
  105. set;
  106. }
  107. public bool BackupDiff
  108. {
  109. get;
  110. set;
  111. }
  112. public bool BackupVersion
  113. {
  114. get;
  115. set;
  116. }
  117. public int LengthLimit
  118. {
  119. get;
  120. set;
  121. }
  122. public bool IsValidWorkingDirectory
  123. {
  124. get
  125. {
  126. if (string.IsNullOrEmpty(WorkingDirectory))
  127. {
  128. return false;
  129. }
  130. if (!Directory.Exists(WorkingDirectory))
  131. {
  132. return false;
  133. }
  134. return true;
  135. }
  136. }
  137. public string SourcePath
  138. {
  139. get
  140. {
  141. if (!IsValidWorkingDirectory)
  142. {
  143. return string.Empty;
  144. }
  145. return Utility.Path.GetRegularPath(new DirectoryInfo(Utility.Text.Format("{0}/Full/", WorkingDirectory)).FullName);
  146. }
  147. }
  148. public string SourcePathForDisplay
  149. {
  150. get
  151. {
  152. if (!IsValidWorkingDirectory)
  153. {
  154. return string.Empty;
  155. }
  156. return Utility.Path.GetRegularPath(new DirectoryInfo(Utility.Text.Format("{0}/Full/*/{1}/", WorkingDirectory, Platform.ToString())).FullName);
  157. }
  158. }
  159. public string OutputPath
  160. {
  161. get
  162. {
  163. if (!IsValidWorkingDirectory)
  164. {
  165. return string.Empty;
  166. }
  167. return Utility.Path.GetRegularPath(new DirectoryInfo(Utility.Text.Format("{0}/ResourcePack/{1}/", WorkingDirectory, Platform.ToString())).FullName);
  168. }
  169. }
  170. public event GameFrameworkAction<int> OnBuildResourcePacksStarted = null;
  171. public event GameFrameworkAction<int, int> OnBuildResourcePacksCompleted = null;
  172. public event GameFrameworkAction<int, int, string, string> OnBuildResourcePackSuccess = null;
  173. public event GameFrameworkAction<int, int, string, string> OnBuildResourcePackFailure = null;
  174. public bool Load()
  175. {
  176. if (!File.Exists(m_ConfigurationPath))
  177. {
  178. return false;
  179. }
  180. try
  181. {
  182. XmlDocument xmlDocument = new XmlDocument();
  183. xmlDocument.Load(m_ConfigurationPath);
  184. XmlNode xmlRoot = xmlDocument.SelectSingleNode("UnityGameFramework");
  185. XmlNode xmlEditor = xmlRoot.SelectSingleNode("ResourceBuilder");
  186. XmlNode xmlSettings = xmlEditor.SelectSingleNode("Settings");
  187. XmlNodeList xmlNodeList = null;
  188. XmlNode xmlNode = null;
  189. xmlNodeList = xmlSettings.ChildNodes;
  190. for (int i = 0; i < xmlNodeList.Count; i++)
  191. {
  192. xmlNode = xmlNodeList.Item(i);
  193. switch (xmlNode.Name)
  194. {
  195. case "CompressionHelperTypeName":
  196. CompressionHelperTypeName = xmlNode.InnerText;
  197. break;
  198. case "OutputDirectory":
  199. WorkingDirectory = xmlNode.InnerText;
  200. break;
  201. }
  202. }
  203. }
  204. catch
  205. {
  206. return false;
  207. }
  208. return true;
  209. }
  210. public string[] GetCompressionHelperTypeNames()
  211. {
  212. return m_CompressionHelperTypeNames.ToArray();
  213. }
  214. public string[] GetVersionNames()
  215. {
  216. if (Platform == Platform.Undefined || !IsValidWorkingDirectory)
  217. {
  218. return EmptyStringArray;
  219. }
  220. string platformName = Platform.ToString();
  221. DirectoryInfo sourceDirectoryInfo = new DirectoryInfo(SourcePath);
  222. if (!sourceDirectoryInfo.Exists)
  223. {
  224. return EmptyStringArray;
  225. }
  226. List<string> versionNames = new List<string>();
  227. foreach (DirectoryInfo directoryInfo in sourceDirectoryInfo.GetDirectories())
  228. {
  229. string[] splitedVersionNames = directoryInfo.Name.Split('_');
  230. if (splitedVersionNames.Length < 2)
  231. {
  232. continue;
  233. }
  234. bool invalid = false;
  235. int value = 0;
  236. for (int i = 0; i < splitedVersionNames.Length; i++)
  237. {
  238. if (!int.TryParse(splitedVersionNames[i], out value))
  239. {
  240. invalid = true;
  241. break;
  242. }
  243. }
  244. if (invalid)
  245. {
  246. continue;
  247. }
  248. DirectoryInfo platformDirectoryInfo = new DirectoryInfo(Path.Combine(directoryInfo.FullName, platformName));
  249. if (!platformDirectoryInfo.Exists)
  250. {
  251. continue;
  252. }
  253. FileInfo[] versionListFiles = platformDirectoryInfo.GetFiles("GameFrameworkVersion.*.dat", SearchOption.TopDirectoryOnly);
  254. if (versionListFiles.Length != 1)
  255. {
  256. continue;
  257. }
  258. versionNames.Add(directoryInfo.Name);
  259. }
  260. versionNames.Sort((x, y) =>
  261. {
  262. return int.Parse(x.Substring(x.LastIndexOf('_') + 1)).CompareTo(int.Parse(y.Substring(y.LastIndexOf('_') + 1)));
  263. });
  264. return versionNames.ToArray();
  265. }
  266. public bool RefreshCompressionHelper()
  267. {
  268. bool retVal = false;
  269. if (!string.IsNullOrEmpty(CompressionHelperTypeName) && m_CompressionHelperTypeNames.Contains(CompressionHelperTypeName))
  270. {
  271. System.Type compressionHelperType = Utility.Assembly.GetType(CompressionHelperTypeName);
  272. if (compressionHelperType != null)
  273. {
  274. Utility.Compression.ICompressionHelper compressionHelper = (Utility.Compression.ICompressionHelper)Activator.CreateInstance(compressionHelperType);
  275. if (compressionHelper != null)
  276. {
  277. Utility.Compression.SetCompressionHelper(compressionHelper);
  278. return true;
  279. }
  280. }
  281. }
  282. else
  283. {
  284. retVal = true;
  285. }
  286. CompressionHelperTypeName = string.Empty;
  287. Utility.Compression.SetCompressionHelper(null);
  288. return retVal;
  289. }
  290. public void BuildResourcePacks(string[] sourceVersions, string targetVersion)
  291. {
  292. int count = sourceVersions.Length;
  293. if (OnBuildResourcePacksStarted != null)
  294. {
  295. OnBuildResourcePacksStarted(count);
  296. }
  297. int successCount = 0;
  298. for (int i = 0; i < count; i++)
  299. {
  300. if (BuildResourcePack(sourceVersions[i], targetVersion))
  301. {
  302. successCount++;
  303. if (OnBuildResourcePackSuccess != null)
  304. {
  305. OnBuildResourcePackSuccess(i, count, sourceVersions[i], targetVersion);
  306. }
  307. }
  308. else
  309. {
  310. if (OnBuildResourcePackFailure != null)
  311. {
  312. OnBuildResourcePackFailure(i, count, sourceVersions[i], targetVersion);
  313. }
  314. }
  315. }
  316. if (OnBuildResourcePacksCompleted != null)
  317. {
  318. OnBuildResourcePacksCompleted(successCount, count);
  319. }
  320. }
  321. public bool BuildResourcePack(string sourceVersion, string targetVersion)
  322. {
  323. try
  324. {
  325. if (!Directory.Exists(OutputPath))
  326. {
  327. Directory.CreateDirectory(OutputPath);
  328. }
  329. string defaultBackupDiffPath = Path.Combine(OutputPath, DefaultResourcePackName);
  330. string defaultResourcePackName = Utility.Text.Format("{0}.{1}", defaultBackupDiffPath, DefaultExtension);
  331. if (File.Exists(defaultResourcePackName))
  332. {
  333. File.Delete(defaultResourcePackName);
  334. }
  335. if (BackupDiff)
  336. {
  337. if (Directory.Exists(defaultBackupDiffPath))
  338. {
  339. Directory.Delete(defaultBackupDiffPath, true);
  340. }
  341. Directory.CreateDirectory(defaultBackupDiffPath);
  342. }
  343. UpdatableVersionList sourceUpdatableVersionList = default(UpdatableVersionList);
  344. if (sourceVersion != null)
  345. {
  346. DirectoryInfo sourceDirectoryInfo = new DirectoryInfo(Path.Combine(Path.Combine(SourcePath, sourceVersion), Platform.ToString()));
  347. FileInfo[] sourceVersionListFiles = sourceDirectoryInfo.GetFiles("GameFrameworkVersion.*.dat", SearchOption.TopDirectoryOnly);
  348. byte[] sourceVersionListBytes = File.ReadAllBytes(sourceVersionListFiles[0].FullName);
  349. sourceVersionListBytes = Utility.Compression.Decompress(sourceVersionListBytes);
  350. using (Stream stream = new MemoryStream(sourceVersionListBytes))
  351. {
  352. sourceUpdatableVersionList = m_UpdatableVersionListSerializer.Deserialize(stream);
  353. }
  354. }
  355. UpdatableVersionList targetUpdatableVersionList = default(UpdatableVersionList);
  356. DirectoryInfo targetDirectoryInfo = new DirectoryInfo(Path.Combine(Path.Combine(SourcePath, targetVersion), Platform.ToString()));
  357. FileInfo[] targetVersionListFiles = targetDirectoryInfo.GetFiles("GameFrameworkVersion.*.dat", SearchOption.TopDirectoryOnly);
  358. byte[] targetVersionListBytes = File.ReadAllBytes(targetVersionListFiles[0].FullName);
  359. targetVersionListBytes = Utility.Compression.Decompress(targetVersionListBytes);
  360. using (Stream stream = new MemoryStream(targetVersionListBytes))
  361. {
  362. targetUpdatableVersionList = m_UpdatableVersionListSerializer.Deserialize(stream);
  363. }
  364. List<ResourcePackVersionList.Resource> resources = new List<ResourcePackVersionList.Resource>();
  365. UpdatableVersionList.Resource[] sourceResources = sourceUpdatableVersionList.IsValid ? sourceUpdatableVersionList.GetResources() : EmptyResourceArray;
  366. UpdatableVersionList.Resource[] targetResources = targetUpdatableVersionList.GetResources();
  367. long offset = 0L;
  368. foreach (UpdatableVersionList.Resource targetResource in targetResources)
  369. {
  370. bool ready = false;
  371. foreach (UpdatableVersionList.Resource sourceResource in sourceResources)
  372. {
  373. if (sourceResource.Name != targetResource.Name || sourceResource.Variant != targetResource.Variant || sourceResource.Extension != targetResource.Extension)
  374. {
  375. continue;
  376. }
  377. if (sourceResource.LoadType == targetResource.LoadType && sourceResource.Length == targetResource.Length && sourceResource.HashCode == targetResource.HashCode)
  378. {
  379. ready = true;
  380. }
  381. break;
  382. }
  383. if (!ready)
  384. {
  385. resources.Add(new ResourcePackVersionList.Resource(targetResource.Name, targetResource.Variant, targetResource.Extension, targetResource.LoadType, offset, targetResource.Length, targetResource.HashCode, targetResource.CompressedLength, targetResource.CompressedHashCode));
  386. offset += targetResource.CompressedLength;
  387. }
  388. }
  389. ResourcePackVersionList.Resource[] resourceArray = resources.ToArray();
  390. using (FileStream fileStream = new FileStream(defaultResourcePackName, FileMode.Create, FileAccess.Write))
  391. {
  392. if (!m_ResourcePackVersionListSerializer.Serialize(fileStream, new ResourcePackVersionList(0, 0L, 0, resourceArray)))
  393. {
  394. return false;
  395. }
  396. }
  397. int position = 0;
  398. int hashCode = 0;
  399. string targetDirectoryPath = targetDirectoryInfo.FullName;
  400. using (FileStream fileStream = new FileStream(defaultResourcePackName, FileMode.Open, FileAccess.ReadWrite))
  401. {
  402. position = (int)fileStream.Length;
  403. fileStream.Position = position;
  404. foreach (ResourcePackVersionList.Resource resource in resourceArray)
  405. {
  406. string resourceName = Path.Combine(targetDirectoryPath, GetResourceFullName(resource.Name, resource.Variant, resource.HashCode));
  407. if (!File.Exists(resourceName))
  408. {
  409. return false;
  410. }
  411. byte[] resourceBytes = File.ReadAllBytes(resourceName);
  412. fileStream.Write(resourceBytes, 0, resourceBytes.Length);
  413. if (BackupDiff)
  414. {
  415. string backupDiffName = Path.Combine(defaultBackupDiffPath, GetResourceFullName(resource.Name, resource.Variant, resource.HashCode));
  416. string directoryName = Path.GetDirectoryName(backupDiffName);
  417. if (!Directory.Exists(directoryName))
  418. {
  419. Directory.CreateDirectory(directoryName);
  420. }
  421. File.WriteAllBytes(backupDiffName, resourceBytes);
  422. }
  423. }
  424. if (fileStream.Position - position != offset)
  425. {
  426. return false;
  427. }
  428. fileStream.Position = position;
  429. hashCode = Utility.Verifier.GetCrc32(fileStream);
  430. fileStream.Position = 0L;
  431. if (!m_ResourcePackVersionListSerializer.Serialize(fileStream, new ResourcePackVersionList(position, offset, hashCode, resourceArray)))
  432. {
  433. return false;
  434. }
  435. }
  436. string backupDiffPath = Path.Combine(OutputPath, Utility.Text.Format("{0}-{1}-{2}", DefaultResourcePackName, sourceVersion ?? GetNoneVersion(targetVersion), targetVersion));
  437. string resourcePackName = Utility.Text.Format("{0}.{1:x8}.{2}", backupDiffPath, hashCode, DefaultExtension);
  438. if (File.Exists(resourcePackName))
  439. {
  440. File.Delete(resourcePackName);
  441. }
  442. File.Move(defaultResourcePackName, resourcePackName);
  443. if (BackupDiff)
  444. {
  445. if (BackupVersion)
  446. {
  447. File.Copy(targetVersionListFiles[0].FullName, Path.Combine(defaultBackupDiffPath, Path.GetFileName(targetVersionListFiles[0].FullName)));
  448. }
  449. if (Directory.Exists(backupDiffPath))
  450. {
  451. Directory.Delete(backupDiffPath, true);
  452. }
  453. Directory.Move(defaultBackupDiffPath, backupDiffPath);
  454. }
  455. return true;
  456. }
  457. catch
  458. {
  459. return false;
  460. }
  461. }
  462. private string GetNoneVersion(string targetVersion)
  463. {
  464. string[] splitedVersionNames = targetVersion.Split('_');
  465. for (int i = 0; i < splitedVersionNames.Length; i++)
  466. {
  467. splitedVersionNames[i] = "0";
  468. }
  469. return string.Join("_", splitedVersionNames);
  470. }
  471. private string GetResourceFullName(string name, string variant, int hashCode)
  472. {
  473. return !string.IsNullOrEmpty(variant) ? Utility.Text.Format("{0}.{1}.{2:x8}.{3}", name, variant, hashCode, DefaultExtension) : Utility.Text.Format("{0}.{1:x8}.{2}", name, hashCode, DefaultExtension);
  474. }
  475. }
  476. }