BuiltinVersionListSerializer.LocalVersionListSerializeCallback.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. /// <summary>
  20. /// 序列化本地版本资源列表(版本 0)回调函数。
  21. /// </summary>
  22. /// <param name="stream">目标流。</param>
  23. /// <param name="versionList">要序列化的本地版本资源列表(版本 0)。</param>
  24. /// <returns>是否序列化本地版本资源列表(版本 0)成功。</returns>
  25. public static bool LocalVersionListSerializeCallback_V0(Stream stream, LocalVersionList versionList)
  26. {
  27. if (!versionList.IsValid)
  28. {
  29. return false;
  30. }
  31. Utility.Random.GetRandomBytes(s_CachedHashBytes);
  32. using (BinaryWriter binaryWriter = new BinaryWriter(stream, Encoding.UTF8))
  33. {
  34. binaryWriter.Write(s_CachedHashBytes);
  35. LocalVersionList.Resource[] resources = versionList.GetResources();
  36. binaryWriter.Write(resources.Length);
  37. foreach (LocalVersionList.Resource resource in resources)
  38. {
  39. binaryWriter.WriteEncryptedString(resource.Name, s_CachedHashBytes);
  40. binaryWriter.WriteEncryptedString(resource.Variant, s_CachedHashBytes);
  41. binaryWriter.Write(resource.LoadType);
  42. binaryWriter.Write(resource.Length);
  43. binaryWriter.Write(resource.HashCode);
  44. }
  45. }
  46. Array.Clear(s_CachedHashBytes, 0, CachedHashBytesLength);
  47. return true;
  48. }
  49. /// <summary>
  50. /// 序列化本地版本资源列表(版本 1)回调函数。
  51. /// </summary>
  52. /// <param name="stream">目标流。</param>
  53. /// <param name="versionList">要序列化的本地版本资源列表(版本 1)。</param>
  54. /// <returns>是否序列化本地版本资源列表(版本 1)成功。</returns>
  55. public static bool LocalVersionListSerializeCallback_V1(Stream stream, LocalVersionList versionList)
  56. {
  57. if (!versionList.IsValid)
  58. {
  59. return false;
  60. }
  61. Utility.Random.GetRandomBytes(s_CachedHashBytes);
  62. using (BinaryWriter binaryWriter = new BinaryWriter(stream, Encoding.UTF8))
  63. {
  64. binaryWriter.Write(s_CachedHashBytes);
  65. LocalVersionList.Resource[] resources = versionList.GetResources();
  66. binaryWriter.Write7BitEncodedInt32(resources.Length);
  67. foreach (LocalVersionList.Resource resource in resources)
  68. {
  69. binaryWriter.WriteEncryptedString(resource.Name, s_CachedHashBytes);
  70. binaryWriter.WriteEncryptedString(resource.Variant, s_CachedHashBytes);
  71. binaryWriter.WriteEncryptedString(resource.Extension != DefaultExtension ? resource.Extension : null, s_CachedHashBytes);
  72. binaryWriter.Write(resource.LoadType);
  73. binaryWriter.Write7BitEncodedInt32(resource.Length);
  74. binaryWriter.Write(resource.HashCode);
  75. }
  76. }
  77. Array.Clear(s_CachedHashBytes, 0, CachedHashBytesLength);
  78. return true;
  79. }
  80. /// <summary>
  81. /// 序列化本地版本资源列表(版本 2)回调函数。
  82. /// </summary>
  83. /// <param name="stream">目标流。</param>
  84. /// <param name="versionList">要序列化的本地版本资源列表(版本 2)。</param>
  85. /// <returns>是否序列化本地版本资源列表(版本 2)成功。</returns>
  86. public static bool LocalVersionListSerializeCallback_V2(Stream stream, LocalVersionList versionList)
  87. {
  88. if (!versionList.IsValid)
  89. {
  90. return false;
  91. }
  92. Utility.Random.GetRandomBytes(s_CachedHashBytes);
  93. using (BinaryWriter binaryWriter = new BinaryWriter(stream, Encoding.UTF8))
  94. {
  95. binaryWriter.Write(s_CachedHashBytes);
  96. LocalVersionList.Resource[] resources = versionList.GetResources();
  97. binaryWriter.Write7BitEncodedInt32(resources.Length);
  98. foreach (LocalVersionList.Resource resource in resources)
  99. {
  100. binaryWriter.WriteEncryptedString(resource.Name, s_CachedHashBytes);
  101. binaryWriter.WriteEncryptedString(resource.Variant, s_CachedHashBytes);
  102. binaryWriter.WriteEncryptedString(resource.Extension != DefaultExtension ? resource.Extension : null, s_CachedHashBytes);
  103. binaryWriter.Write(resource.LoadType);
  104. binaryWriter.Write7BitEncodedInt32(resource.Length);
  105. binaryWriter.Write(resource.HashCode);
  106. }
  107. LocalVersionList.FileSystem[] fileSystems = versionList.GetFileSystems();
  108. binaryWriter.Write7BitEncodedInt32(fileSystems.Length);
  109. foreach (LocalVersionList.FileSystem fileSystem in fileSystems)
  110. {
  111. binaryWriter.WriteEncryptedString(fileSystem.Name, s_CachedHashBytes);
  112. int[] resourceIndexes = fileSystem.GetResourceIndexes();
  113. binaryWriter.Write7BitEncodedInt32(resourceIndexes.Length);
  114. foreach (int resourceIndex in resourceIndexes)
  115. {
  116. binaryWriter.Write7BitEncodedInt32(resourceIndex);
  117. }
  118. }
  119. }
  120. Array.Clear(s_CachedHashBytes, 0, CachedHashBytesLength);
  121. return true;
  122. }
  123. }
  124. }