Code: Select all
private void InsertBytesInMap(byte[] bytesToInsert, int insertAt)
{
Benchmark.Begin();
string MapFolder = sourceMap.FilePath.Remove(sourceMap.FilePath.LastIndexOf('\\')+1);
FileStream temp = new FileStream(MapFolder + "BIN-01.bin", FileMode.Create, FileAccess.ReadWrite);
sourceMap.Mark();
sourceMap.Position = 0;
Status("Writing MapStream To BIN File...");
temp.Write(sourceMap.ReadBytes(insertAt), 0, insertAt);
Status("Saving...");
temp.Flush();
Status("Closing BIN File...");
temp.Close();
Status("Writing MapStream To BIN File...");
temp = new FileStream(MapFolder + "BIN-02.bin", FileMode.Create, FileAccess.ReadWrite);
temp.Write(sourceMap.ReadBytes((int)(sourceMap.Length - insertAt)), 0, (int)(sourceMap.Length - insertAt));
Status("Saving...");
temp.Flush();
Status("Closing BIN File...");
temp.Close();
Status("Closing MapStream...");
sourceMap.Close();
Status("Creating New Map File...");
FileStream newMap = new FileStream(sourceMap.FilePath, FileMode.Create, FileAccess.ReadWrite);
BinaryWriter newMapWriter = new BinaryWriter(newMap);
Status("Opening BIN File...");
temp = new FileStream(MapFolder + "BIN-01.bin", FileMode.Open, FileAccess.Read);
BinaryReader tempReader = new BinaryReader(temp);
Status("Writing Data To New Map File From BIN File...");
newMapWriter.Write(tempReader.ReadBytes((int)temp.Length));
Status("Saving...");
newMapWriter.Flush();
temp.Flush();
Status("Closing BIN File...");
temp.Close();
Status("Deleting BIN File...");
File.Delete(temp.Name);
Status("Writing New Data Bytes To New Map File...");
newMapWriter.Write(bytesToInsert);
Status("Saving...");
newMapWriter.Flush();
Status("Opening BIN File...");
temp = new FileStream(MapFolder + "BIN-02.bin", FileMode.Open, FileAccess.Read);
tempReader = new BinaryReader(temp);
Status("Writing Data To New Map File From BIN File...");
newMapWriter.Write(tempReader.ReadBytes((int)temp.Length));
Status("Saving...");
newMapWriter.Flush();
Status("Closing New Map File...");
newMapWriter.Close();
newMapWriter = null;
Status("Saving...");
temp.Flush();
Status("Closing BIN File...");
temp.Close();
Status("Deleting BIN File...");
File.Delete(temp.Name);
temp = null;
sourceMap.Close();
Benchmark.End();
}