ContainerList_Chunk.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7. namespace System.Fire.Container {
  8. public partial class ContainerList {
  9. private long ChunkFile_Offset;
  10. private FileStream ChunkFile;
  11. private BinaryReader ChunkFileR;
  12. private BinaryWriter ChunkFileW;
  13. /*public bool ReadChunk(int pos, int ChunkNum, ref byte Group, ref byte Container, ref ushort ChunkIndex) {
  14. if (ChunkNum >= 0 && ChunkNum * 4 < this._chunks.Length - 4) {
  15. Group = this._chunks[pos * 4];
  16. Container = this._chunks[(pos * 4) + 1];
  17. ChunkIndex = (ushort)(this._chunks[(((pos * 4) + 2) * 256)] + this._chunks[(pos * 4) + 2]);
  18. return true;
  19. }
  20. return true;
  21. }
  22. public bool ReadChunk(int pos, int ChunkNum, ref byte Group, ref byte Container, ref ushort ChunkIndex) {
  23. this.ChunkFile.Seek(pos + (ChunkNum * 4), IO.SeekOrigin.Begin);
  24. Group = this.ChunkFileR.ReadByte();
  25. Container = this.ChunkFileR.ReadByte();
  26. ChunkIndex = this.ChunkFileR.ReadUInt16();
  27. return true;
  28. }*/
  29. /// <summary>
  30. /// Returns the length of a container, given by chunkfile-position and chunk number
  31. /// </summary>
  32. /// <param name="pos"></param>
  33. /// <param name="ChunkNum"></param>
  34. /// <returns></returns>
  35. private int GetContainerLenFromChunk(long pos, int ChunkNum) {
  36. this.ChunkFile.Position = (ChunkNum * 4) + pos;
  37. byte Group = this.ChunkFileR.ReadByte();
  38. byte Container = this.ChunkFileR.ReadByte();
  39. return this._container[Group][Container].ChunkSize;
  40. }
  41. private byte[] ReadChunk(long pos, int ChunkNum) {
  42. this.ChunkFile.Position = (ChunkNum * 4) + pos;
  43. byte Group = this.ChunkFileR.ReadByte();
  44. byte Container = this.ChunkFileR.ReadByte();
  45. byte ci1 = this.ChunkFileR.ReadByte();
  46. byte ci2 = this.ChunkFileR.ReadByte();
  47. ushort ChunkIndex = (ushort)((ci1 * 256) + ci2);
  48. //ushort ChunkIndex = this.ChunkFileR.ReadUInt16();
  49. System.Diagnostics.Debug.WriteLine("Read Chunk: " +
  50. Group.ToString().PadLeft(3) + " | " +
  51. Container.ToString().PadLeft(3) + " | " +
  52. ChunkIndex.ToString().PadLeft(5) +
  53. " (" + pos + " | " + ChunkNum + ")"
  54. );
  55. return this._container[Group][Container].GetChunk(ChunkIndex);
  56. }
  57. private long WriteChunk(byte[] b) {
  58. this.ChunkFile.Seek(0, SeekOrigin.End);
  59. long pos = ChunkFile.Position;
  60. System.Diagnostics.Debug.WriteLine("Writ Chunk: " +
  61. " (" + pos + " | " + (b.Length / 4) + ")"
  62. );
  63. this.ChunkFileW.Write(b);
  64. return pos;
  65. }
  66. private void OpenChunkFile(string Path) {
  67. bool bHasFileInfo = false;
  68. if (System.IO.File.Exists(Path)) {
  69. this.ChunkFile = new FileStream(Path, FileMode.Open);
  70. if (this.ChunkFile.Length > 6) {
  71. this.ChunkFileR = new BinaryReader(this.ChunkFile);
  72. this.ChunkFileW = new BinaryWriter(this.ChunkFile);
  73. byte[] Magic = this.ChunkFileR.ReadBytes(4);
  74. if (
  75. Magic[0] == 67 /* C */ &&
  76. Magic[1] == 111 /* o */ &&
  77. Magic[2] == 67 /* C */ &&
  78. Magic[3] == 72 /* H */
  79. ) {
  80. ushort version = this.ChunkFileR.ReadUInt16();
  81. bHasFileInfo = true;
  82. this.ChunkFile_Offset = this.ChunkFile.Position;
  83. }
  84. }
  85. if (bHasFileInfo == false) {
  86. this.ChunkFile.Close();
  87. File.Delete(Path);
  88. }
  89. }
  90. if (!bHasFileInfo) {
  91. this.ChunkFile = new FileStream(Path, FileMode.CreateNew);
  92. this.ChunkFileR = new BinaryReader(this.ChunkFile);
  93. this.ChunkFileW = new BinaryWriter(this.ChunkFile);
  94. this.ChunkFileW.Write(new byte[] { 67, 111, 67, 72 });
  95. this.ChunkFileW.Write(Version);
  96. this.ChunkFile_Offset = this.ChunkFile.Position;
  97. }
  98. }
  99. private void CloseChunkFile() {
  100. this.ChunkFile.Close();
  101. }
  102. }
  103. }