using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace System.Fire.Container {
public partial class ContainerList {
private long ChunkFile_Offset;
private FileStream ChunkFile;
private BinaryReader ChunkFileR;
private BinaryWriter ChunkFileW;
/*public bool ReadChunk(int pos, int ChunkNum, ref byte Group, ref byte Container, ref ushort ChunkIndex) {
if (ChunkNum >= 0 && ChunkNum * 4 < this._chunks.Length - 4) {
Group = this._chunks[pos * 4];
Container = this._chunks[(pos * 4) + 1];
ChunkIndex = (ushort)(this._chunks[(((pos * 4) + 2) * 256)] + this._chunks[(pos * 4) + 2]);
return true;
}
return true;
}
public bool ReadChunk(int pos, int ChunkNum, ref byte Group, ref byte Container, ref ushort ChunkIndex) {
this.ChunkFile.Seek(pos + (ChunkNum * 4), IO.SeekOrigin.Begin);
Group = this.ChunkFileR.ReadByte();
Container = this.ChunkFileR.ReadByte();
ChunkIndex = this.ChunkFileR.ReadUInt16();
return true;
}*/
///
/// Returns the length of a container, given by chunkfile-position and chunk number
///
///
///
///
private int GetContainerLenFromChunk(long pos, int ChunkNum) {
this.ChunkFile.Position = (ChunkNum * 4) + pos;
byte Group = this.ChunkFileR.ReadByte();
byte Container = this.ChunkFileR.ReadByte();
return this._container[Group][Container].ChunkSize;
}
private byte[] ReadChunk(long pos, int ChunkNum) {
this.ChunkFile.Position = (ChunkNum * 4) + pos;
byte Group = this.ChunkFileR.ReadByte();
byte Container = this.ChunkFileR.ReadByte();
byte ci1 = this.ChunkFileR.ReadByte();
byte ci2 = this.ChunkFileR.ReadByte();
ushort ChunkIndex = (ushort)((ci1 * 256) + ci2);
//ushort ChunkIndex = this.ChunkFileR.ReadUInt16();
System.Diagnostics.Debug.WriteLine("Read Chunk: " +
Group.ToString().PadLeft(3) + " | " +
Container.ToString().PadLeft(3) + " | " +
ChunkIndex.ToString().PadLeft(5) +
" (" + pos + " | " + ChunkNum + ")"
);
return this._container[Group][Container].GetChunk(ChunkIndex);
}
private long WriteChunk(byte[] b) {
this.ChunkFile.Seek(0, SeekOrigin.End);
long pos = ChunkFile.Position;
System.Diagnostics.Debug.WriteLine("Writ Chunk: " +
" (" + pos + " | " + (b.Length / 4) + ")"
);
this.ChunkFileW.Write(b);
return pos;
}
private void OpenChunkFile(string Path) {
bool bHasFileInfo = false;
if (System.IO.File.Exists(Path)) {
this.ChunkFile = new FileStream(Path, FileMode.Open);
if (this.ChunkFile.Length > 6) {
this.ChunkFileR = new BinaryReader(this.ChunkFile);
this.ChunkFileW = new BinaryWriter(this.ChunkFile);
byte[] Magic = this.ChunkFileR.ReadBytes(4);
if (
Magic[0] == 67 /* C */ &&
Magic[1] == 111 /* o */ &&
Magic[2] == 67 /* C */ &&
Magic[3] == 72 /* H */
) {
ushort version = this.ChunkFileR.ReadUInt16();
bHasFileInfo = true;
this.ChunkFile_Offset = this.ChunkFile.Position;
}
}
if (bHasFileInfo == false) {
this.ChunkFile.Close();
File.Delete(Path);
}
}
if (!bHasFileInfo) {
this.ChunkFile = new FileStream(Path, FileMode.CreateNew);
this.ChunkFileR = new BinaryReader(this.ChunkFile);
this.ChunkFileW = new BinaryWriter(this.ChunkFile);
this.ChunkFileW.Write(new byte[] { 67, 111, 67, 72 });
this.ChunkFileW.Write(Version);
this.ChunkFile_Offset = this.ChunkFile.Position;
}
}
private void CloseChunkFile() {
this.ChunkFile.Close();
}
}
}