FileSystem.HeaderData.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 System.Runtime.InteropServices;
  8. namespace GameFramework.FileSystem
  9. {
  10. internal sealed partial class FileSystem : IFileSystem
  11. {
  12. /// <summary>
  13. /// 头数据。
  14. /// </summary>
  15. [StructLayout(LayoutKind.Sequential)]
  16. private struct HeaderData
  17. {
  18. private const int HeaderLength = 3;
  19. private const int FileSystemVersion = 0;
  20. private const int EncryptBytesLength = 4;
  21. private static readonly byte[] Header = new byte[HeaderLength] { (byte)'G', (byte)'F', (byte)'F' };
  22. [MarshalAs(UnmanagedType.ByValArray, SizeConst = HeaderLength)]
  23. private readonly byte[] m_Header;
  24. private readonly byte m_Version;
  25. [MarshalAs(UnmanagedType.ByValArray, SizeConst = EncryptBytesLength)]
  26. private readonly byte[] m_EncryptBytes;
  27. private readonly int m_MaxFileCount;
  28. private readonly int m_MaxBlockCount;
  29. private readonly int m_BlockCount;
  30. public HeaderData(int maxFileCount, int maxBlockCount)
  31. : this(FileSystemVersion, new byte[EncryptBytesLength], maxFileCount, maxBlockCount, 0)
  32. {
  33. Utility.Random.GetRandomBytes(m_EncryptBytes);
  34. }
  35. public HeaderData(byte version, byte[] encryptBytes, int maxFileCount, int maxBlockCount, int blockCount)
  36. {
  37. m_Header = Header;
  38. m_Version = version;
  39. m_EncryptBytes = encryptBytes;
  40. m_MaxFileCount = maxFileCount;
  41. m_MaxBlockCount = maxBlockCount;
  42. m_BlockCount = blockCount;
  43. }
  44. public bool IsValid
  45. {
  46. get
  47. {
  48. return m_Header.Length == HeaderLength && m_Header[0] == Header[0] && m_Header[1] == Header[1] && m_Header[2] == Header[2] && m_Version == FileSystemVersion && m_EncryptBytes.Length == EncryptBytesLength
  49. && m_MaxFileCount > 0 && m_MaxBlockCount > 0 && m_MaxFileCount <= m_MaxBlockCount && m_BlockCount > 0 && m_BlockCount <= m_MaxBlockCount;
  50. }
  51. }
  52. public byte Version
  53. {
  54. get
  55. {
  56. return m_Version;
  57. }
  58. }
  59. public int MaxFileCount
  60. {
  61. get
  62. {
  63. return m_MaxFileCount;
  64. }
  65. }
  66. public int MaxBlockCount
  67. {
  68. get
  69. {
  70. return m_MaxBlockCount;
  71. }
  72. }
  73. public int BlockCount
  74. {
  75. get
  76. {
  77. return m_BlockCount;
  78. }
  79. }
  80. public byte[] GetEncryptBytes()
  81. {
  82. return m_EncryptBytes;
  83. }
  84. public HeaderData SetBlockCount(int blockCount)
  85. {
  86. return new HeaderData(m_Version, m_EncryptBytes, m_MaxFileCount, m_MaxBlockCount, blockCount);
  87. }
  88. }
  89. }
  90. }