public static void injectOneFile(MainWindow.fileReplacement fR, List <byte> newFile, byte[] sourceFile, string mapFilePath, string sourcePath) { if (newFile.Count() > fR.size) { throw new Exception("New file greater than source file, which is " + fR.size + " bytes!"); } else { MemoryStream src = new MemoryStream(sourceFile); BinaryDataReader br = new BinaryDataReader(src); MemoryStream o = new MemoryStream(); BinaryDataWriter bw = new BinaryDataWriter(o); byte[] firstJunk = br.ReadBytes(fR.offset); bw.Write(firstJunk); br.ReadBytes(fR.size); //Make size correct. while (newFile.Count() < fR.size) { newFile.Add(0); } bw.Write(newFile.ToArray()); byte[] lastJunk = br.ReadBytes(sourceFile.Length - (int)br.Position); bw.Write(lastJunk); File.WriteAllBytes(sourcePath, o.ToArray()); } }
public static bool injectAllFiles(MainWindow.fileReplacement[] fR, string mapDirectory, byte[] sourceFile, string sourcePath) { MemoryStream o = new MemoryStream(); BinaryDataWriter bw = new BinaryDataWriter(o); MemoryStream sourceReader = new MemoryStream(sourceFile); BinaryDataReader sR = new BinaryDataReader(sourceReader); bool cancel = false; foreach (MainWindow.fileReplacement f in fR) { byte[] junk = sR.ReadBytes(f.offset - (int)sR.Position); bw.Write(junk); MemoryStream src = new MemoryStream(File.ReadAllBytes(mapDirectory + "/" + f.path)); BinaryDataReader br = new BinaryDataReader(src); List <byte> newFile = br.ReadBytes((int)src.Length).ToList(); if (f.path.EndsWith("fgrp") || f.path.EndsWith("cgrp") || f.path.EndsWith("fwar") || f.path.EndsWith("cwar") || f.path.EndsWith("fwsd") || f.path.EndsWith("cwsd")) { List <MainWindow.fileReplacement> h = new List <MainWindow.fileReplacement>(); string[] map = File.ReadAllLines(Path.GetDirectoryName(mapDirectory + "/" + f.path) + "/" + Path.GetFileNameWithoutExtension(f.path) + f.path.Substring(0, 4) + "/fileMap.txt"); for (int i = 1; i < map.Length; i++) { MainWindow.fileReplacement r = new MainWindow.fileReplacement(); string[] split = map[i].Split(';'); r.offset = int.Parse(split[0]); r.size = int.Parse(split[1].Substring(1)); r.path = split[2].Substring(1); r.name = split[3].Substring(1); h.Add(r); } cancel = injectAllFiles(h.ToArray(), Path.GetDirectoryName(mapDirectory + "/" + f.path) + "/" + Path.GetFileNameWithoutExtension(f.path) + f.path.Substring(0, 4), File.ReadAllBytes(mapDirectory + "/" + f.path), mapDirectory + "/" + f.path); } while (newFile.Count() < f.size) { newFile.Add(0); } bw.Write(newFile.ToArray()); sR.ReadBytes(newFile.Count()); if (newFile.Count() > f.size) { cancel = true; throw new Exception("Filesize over limit! File " + f.path + " is over " + f.size + " bytes!"); } } byte[] endingJunk = sR.ReadBytes(sourceFile.Length - (int)sR.Position); bw.Write(endingJunk); if (!cancel) { File.WriteAllBytes(sourcePath, o.ToArray()); } else { throw new Exception("Source File was NOT overwritten due to errors."); } return(cancel); }