BuiltinVersionListSerializer.UpdatableVersionListSerializeCallback.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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.IO;
  11. using System.Text;
  12. namespace UnityGameFramework.Runtime
  13. {
  14. /// <summary>
  15. /// 内置版本资源列表序列化器。
  16. /// </summary>
  17. public static partial class BuiltinVersionListSerializer
  18. {
  19. #if UNITY_EDITOR
  20. /// <summary>
  21. /// 序列化可更新模式版本资源列表(版本 0)回调函数。
  22. /// </summary>
  23. /// <param name="stream">目标流。</param>
  24. /// <param name="versionList">要序列化的可更新模式版本资源列表(版本 0)。</param>
  25. /// <returns>是否序列化可更新模式版本资源列表(版本 0)成功。</returns>
  26. public static bool UpdatableVersionListSerializeCallback_V0(Stream stream, UpdatableVersionList versionList)
  27. {
  28. if (!versionList.IsValid)
  29. {
  30. return false;
  31. }
  32. Utility.Random.GetRandomBytes(s_CachedHashBytes);
  33. using (BinaryWriter binaryWriter = new BinaryWriter(stream, Encoding.UTF8))
  34. {
  35. binaryWriter.Write(s_CachedHashBytes);
  36. binaryWriter.WriteEncryptedString(versionList.ApplicableGameVersion, s_CachedHashBytes);
  37. binaryWriter.Write(versionList.InternalResourceVersion);
  38. UpdatableVersionList.Asset[] assets = versionList.GetAssets();
  39. binaryWriter.Write(assets.Length);
  40. UpdatableVersionList.Resource[] resources = versionList.GetResources();
  41. binaryWriter.Write(resources.Length);
  42. foreach (UpdatableVersionList.Resource resource in resources)
  43. {
  44. binaryWriter.WriteEncryptedString(resource.Name, s_CachedHashBytes);
  45. binaryWriter.WriteEncryptedString(resource.Variant, s_CachedHashBytes);
  46. binaryWriter.Write(resource.LoadType);
  47. binaryWriter.Write(resource.Length);
  48. binaryWriter.Write(resource.HashCode);
  49. binaryWriter.Write(resource.CompressedLength);
  50. binaryWriter.Write(resource.CompressedHashCode);
  51. int[] assetIndexes = resource.GetAssetIndexes();
  52. binaryWriter.Write(assetIndexes.Length);
  53. byte[] hashBytes = new byte[CachedHashBytesLength];
  54. foreach (int assetIndex in assetIndexes)
  55. {
  56. Utility.Converter.GetBytes(resource.HashCode, hashBytes);
  57. UpdatableVersionList.Asset asset = assets[assetIndex];
  58. binaryWriter.WriteEncryptedString(asset.Name, hashBytes);
  59. int[] dependencyAssetIndexes = asset.GetDependencyAssetIndexes();
  60. binaryWriter.Write(dependencyAssetIndexes.Length);
  61. foreach (int dependencyAssetIndex in dependencyAssetIndexes)
  62. {
  63. binaryWriter.WriteEncryptedString(assets[dependencyAssetIndex].Name, hashBytes);
  64. }
  65. }
  66. }
  67. UpdatableVersionList.ResourceGroup[] resourceGroups = versionList.GetResourceGroups();
  68. binaryWriter.Write(resourceGroups.Length);
  69. foreach (UpdatableVersionList.ResourceGroup resourceGroup in resourceGroups)
  70. {
  71. binaryWriter.WriteEncryptedString(resourceGroup.Name, s_CachedHashBytes);
  72. int[] resourceIndexes = resourceGroup.GetResourceIndexes();
  73. binaryWriter.Write(resourceIndexes.Length);
  74. foreach (ushort resourceIndex in resourceIndexes)
  75. {
  76. binaryWriter.Write(resourceIndex);
  77. }
  78. }
  79. }
  80. Array.Clear(s_CachedHashBytes, 0, CachedHashBytesLength);
  81. return true;
  82. }
  83. /// <summary>
  84. /// 序列化可更新模式版本资源列表(版本 1)回调函数。
  85. /// </summary>
  86. /// <param name="stream">目标流。</param>
  87. /// <param name="versionList">要序列化的可更新模式版本资源列表(版本 1)。</param>
  88. /// <returns>是否序列化可更新模式版本资源列表(版本 1)成功。</returns>
  89. public static bool UpdatableVersionListSerializeCallback_V1(Stream stream, UpdatableVersionList versionList)
  90. {
  91. if (!versionList.IsValid)
  92. {
  93. return false;
  94. }
  95. Utility.Random.GetRandomBytes(s_CachedHashBytes);
  96. using (BinaryWriter binaryWriter = new BinaryWriter(stream, Encoding.UTF8))
  97. {
  98. binaryWriter.Write(s_CachedHashBytes);
  99. binaryWriter.WriteEncryptedString(versionList.ApplicableGameVersion, s_CachedHashBytes);
  100. binaryWriter.Write7BitEncodedInt32(versionList.InternalResourceVersion);
  101. UpdatableVersionList.Asset[] assets = versionList.GetAssets();
  102. binaryWriter.Write7BitEncodedInt32(assets.Length);
  103. foreach (UpdatableVersionList.Asset asset in assets)
  104. {
  105. binaryWriter.WriteEncryptedString(asset.Name, s_CachedHashBytes);
  106. int[] dependencyAssetIndexes = asset.GetDependencyAssetIndexes();
  107. binaryWriter.Write7BitEncodedInt32(dependencyAssetIndexes.Length);
  108. foreach (int dependencyAssetIndex in dependencyAssetIndexes)
  109. {
  110. binaryWriter.Write7BitEncodedInt32(dependencyAssetIndex);
  111. }
  112. }
  113. UpdatableVersionList.Resource[] resources = versionList.GetResources();
  114. binaryWriter.Write7BitEncodedInt32(resources.Length);
  115. foreach (UpdatableVersionList.Resource resource in resources)
  116. {
  117. binaryWriter.WriteEncryptedString(resource.Name, s_CachedHashBytes);
  118. binaryWriter.WriteEncryptedString(resource.Variant, s_CachedHashBytes);
  119. binaryWriter.WriteEncryptedString(resource.Extension != DefaultExtension ? resource.Extension : null, s_CachedHashBytes);
  120. binaryWriter.Write(resource.LoadType);
  121. binaryWriter.Write7BitEncodedInt32(resource.Length);
  122. binaryWriter.Write(resource.HashCode);
  123. binaryWriter.Write7BitEncodedInt32(resource.CompressedLength);
  124. binaryWriter.Write(resource.CompressedHashCode);
  125. int[] assetIndexes = resource.GetAssetIndexes();
  126. binaryWriter.Write7BitEncodedInt32(assetIndexes.Length);
  127. foreach (int assetIndex in assetIndexes)
  128. {
  129. binaryWriter.Write7BitEncodedInt32(assetIndex);
  130. }
  131. }
  132. UpdatableVersionList.ResourceGroup[] resourceGroups = versionList.GetResourceGroups();
  133. binaryWriter.Write7BitEncodedInt32(resourceGroups.Length);
  134. foreach (UpdatableVersionList.ResourceGroup resourceGroup in resourceGroups)
  135. {
  136. binaryWriter.WriteEncryptedString(resourceGroup.Name, s_CachedHashBytes);
  137. int[] resourceIndexes = resourceGroup.GetResourceIndexes();
  138. binaryWriter.Write7BitEncodedInt32(resourceIndexes.Length);
  139. foreach (int resourceIndex in resourceIndexes)
  140. {
  141. binaryWriter.Write7BitEncodedInt32(resourceIndex);
  142. }
  143. }
  144. }
  145. Array.Clear(s_CachedHashBytes, 0, CachedHashBytesLength);
  146. return true;
  147. }
  148. /// <summary>
  149. /// 序列化可更新模式版本资源列表(版本 2)回调函数。
  150. /// </summary>
  151. /// <param name="stream">目标流。</param>
  152. /// <param name="versionList">要序列化的可更新模式版本资源列表(版本 2)。</param>
  153. /// <returns>是否序列化可更新模式版本资源列表(版本 2)成功。</returns>
  154. public static bool UpdatableVersionListSerializeCallback_V2(Stream stream, UpdatableVersionList versionList)
  155. {
  156. if (!versionList.IsValid)
  157. {
  158. return false;
  159. }
  160. Utility.Random.GetRandomBytes(s_CachedHashBytes);
  161. using (BinaryWriter binaryWriter = new BinaryWriter(stream, Encoding.UTF8))
  162. {
  163. binaryWriter.Write(s_CachedHashBytes);
  164. binaryWriter.WriteEncryptedString(versionList.ApplicableGameVersion, s_CachedHashBytes);
  165. binaryWriter.Write7BitEncodedInt32(versionList.InternalResourceVersion);
  166. UpdatableVersionList.Asset[] assets = versionList.GetAssets();
  167. binaryWriter.Write7BitEncodedInt32(assets.Length);
  168. foreach (UpdatableVersionList.Asset asset in assets)
  169. {
  170. binaryWriter.WriteEncryptedString(asset.Name, s_CachedHashBytes);
  171. int[] dependencyAssetIndexes = asset.GetDependencyAssetIndexes();
  172. binaryWriter.Write7BitEncodedInt32(dependencyAssetIndexes.Length);
  173. foreach (int dependencyAssetIndex in dependencyAssetIndexes)
  174. {
  175. binaryWriter.Write7BitEncodedInt32(dependencyAssetIndex);
  176. }
  177. }
  178. UpdatableVersionList.Resource[] resources = versionList.GetResources();
  179. binaryWriter.Write7BitEncodedInt32(resources.Length);
  180. foreach (UpdatableVersionList.Resource resource in resources)
  181. {
  182. binaryWriter.WriteEncryptedString(resource.Name, s_CachedHashBytes);
  183. binaryWriter.WriteEncryptedString(resource.Variant, s_CachedHashBytes);
  184. binaryWriter.WriteEncryptedString(resource.Extension != DefaultExtension ? resource.Extension : null, s_CachedHashBytes);
  185. binaryWriter.Write(resource.LoadType);
  186. binaryWriter.Write7BitEncodedInt32(resource.Length);
  187. binaryWriter.Write(resource.HashCode);
  188. binaryWriter.Write7BitEncodedInt32(resource.CompressedLength);
  189. binaryWriter.Write(resource.CompressedHashCode);
  190. int[] assetIndexes = resource.GetAssetIndexes();
  191. binaryWriter.Write7BitEncodedInt32(assetIndexes.Length);
  192. foreach (int assetIndex in assetIndexes)
  193. {
  194. binaryWriter.Write7BitEncodedInt32(assetIndex);
  195. }
  196. }
  197. UpdatableVersionList.FileSystem[] fileSystems = versionList.GetFileSystems();
  198. binaryWriter.Write7BitEncodedInt32(fileSystems.Length);
  199. foreach (UpdatableVersionList.FileSystem fileSystem in fileSystems)
  200. {
  201. binaryWriter.WriteEncryptedString(fileSystem.Name, s_CachedHashBytes);
  202. int[] resourceIndexes = fileSystem.GetResourceIndexes();
  203. binaryWriter.Write7BitEncodedInt32(resourceIndexes.Length);
  204. foreach (int resourceIndex in resourceIndexes)
  205. {
  206. binaryWriter.Write7BitEncodedInt32(resourceIndex);
  207. }
  208. }
  209. UpdatableVersionList.ResourceGroup[] resourceGroups = versionList.GetResourceGroups();
  210. binaryWriter.Write7BitEncodedInt32(resourceGroups.Length);
  211. foreach (UpdatableVersionList.ResourceGroup resourceGroup in resourceGroups)
  212. {
  213. binaryWriter.WriteEncryptedString(resourceGroup.Name, s_CachedHashBytes);
  214. int[] resourceIndexes = resourceGroup.GetResourceIndexes();
  215. binaryWriter.Write7BitEncodedInt32(resourceIndexes.Length);
  216. foreach (int resourceIndex in resourceIndexes)
  217. {
  218. binaryWriter.Write7BitEncodedInt32(resourceIndex);
  219. }
  220. }
  221. }
  222. Array.Clear(s_CachedHashBytes, 0, CachedHashBytesLength);
  223. return true;
  224. }
  225. #endif
  226. }
  227. }