static void Main(string[] args) { if (args.Length == 0 || args.Length > 2) { Usage(); System.Environment.Exit(1); } string infile = args[0]; if (!File.Exists(infile)) { Console.WriteLine("Unable to locate file '{0}'", infile); System.Environment.Exit(1); } string outdir = args.Length > 1 ? args[1] : Path.Combine(System.Environment.CurrentDirectory, Path.GetFileNameWithoutExtension(infile)); BSAArchive archive = new BSAArchive(infile); foreach (var name in archive.FileNames) { byte[] data = archive.GetFile(name); string fileName = Path.Combine(outdir, name); string pathName = Path.GetDirectoryName(fileName); try { Console.WriteLine(fileName); Directory.CreateDirectory(pathName); using (var file = File.Create(fileName, 4096)) { file.Write(data, 0, data.Length); } } catch (System.Exception ex) { Console.WriteLine("Unhandled exception while writing '{0}'", name); } } }
public void Read(string skyrimDir, string fileName, LogFile logFile) { MemoryStream memoryStream = new MemoryStream(); if (File.Exists(skyrimDir + fileName)) { try { FileStream fileStream = (FileStream)null; while (fileStream == null) { fileStream = Utils.GetFileStream(new FileInfo(skyrimDir + fileName), logFile); } BinaryReader binaryReader = new BinaryReader((Stream)fileStream); long length = binaryReader.BaseStream.Length; memoryStream.Write(binaryReader.ReadBytes((int)length), 0, (int)length); binaryReader.Close(); //logFile.WriteLog(" read " + fileName + " " + length); } catch (Exception ex) { logFile.WriteLog("Error reading " + fileName + " " + ex.Message); logFile.Close(); System.Environment.Exit(500); } } else if (BSAArchive.FileExists(fileName)) { try { //logFile.Console.WriteLine(" reading " + fileName + " from BSA"); byte[] newfile = BSAArchive.GetFile(fileName); int length = newfile.Length; memoryStream.Write(newfile, 0, length); //logFile.WriteLog(" read " + fileName + " " + length + " from BSA"); } catch (Exception ex) { logFile.WriteLog("Error reading " + fileName + " from BSA " + ex.Message); logFile.Close(); System.Environment.Exit(501); } } else { logFile.WriteLog(fileName + " not found"); logFile.Close(); System.Environment.Exit(404); } memoryStream.Position = 0L; //Console.WriteLine("Reading " + fileName); BinaryReader reader = new BinaryReader((Stream)memoryStream); this.header.Read(reader); for (int index = 0; (long)index < (long)this.header.GetNumBlocks(); ++index) { if (NiFile.classTypes.ContainsKey(this.header.GetBlockTypeAtIndex(index))) { NiObject niObject = (NiObject)Activator.CreateInstance(NiFile.classTypes[this.header.GetBlockTypeAtIndex(index)]); niObject.Read(this.header, reader); this.blocks.Add(niObject); } else { uint blockSizeAtIndex = this.header.GetBlockSizeAtIndex(index); reader.ReadBytes((int)blockSizeAtIndex); this.blocks.Add((NiObject)null); } } reader.Close(); //Console.WriteLine("done " + fileName); }
public void Read(string gameDir, string fileName, LogFile logFile) { MemoryStream memoryStream = new MemoryStream(); try { if (File.Exists(gameDir + fileName)) { try { FileStream fileStream = (FileStream)null; while (fileStream == null) { fileStream = Utils.GetFileStream(new FileInfo(gameDir + fileName), logFile); } BinaryReader binaryReader = new BinaryReader((Stream)fileStream); long length = binaryReader.BaseStream.Length; memoryStream.Write(binaryReader.ReadBytes((int)length), 0, (int)length); binaryReader.Close(); } catch (Exception ex) { logFile.WriteLog("Error reading " + fileName + " " + ex.Message); logFile.Close(); System.Environment.Exit(500); } } else if (BSAArchive.FileExists(fileName)) { try { byte[] newfile = BSAArchive.GetFile(fileName); int length = newfile.Length; memoryStream.Write(newfile, 0, length); } catch (Exception ex) { logFile.WriteLog("Error reading " + fileName + " from BSA/BA2 " + ex.Message); logFile.Close(); System.Environment.Exit(501); } } else { logFile.WriteLog(fileName + " not found!"); return; //logFile.Close(); //System.Environment.Exit(404); } } catch (Exception ex) { logFile.WriteLog("Error accessing " + gameDir + fileName + " " + ex.Message); logFile.WriteLog("In case Mod Organizer is used, set output path outside of game and MO virtual file system directory"); logFile.Close(); System.Environment.Exit(502); } memoryStream.Position = 0L; BinaryReader reader = new BinaryReader((Stream)memoryStream); try { this.headerString = ""; byte[] data = reader.ReadBytes(4); for (int index = 0; index < 4; index++) { this.headerString += (object)(char)data[index]; } this.version = reader.ReadUInt32(); this.textureClampMode = reader.ReadUInt32(); this.uvOffset = Utils.ReadUVCoord(reader); this.uvScale = Utils.ReadUVCoord(reader); this.alpha = reader.ReadSingle(); this.alphamode0 = reader.ReadByte(); this.alphamode1 = reader.ReadUInt32(); this.alphamode2 = reader.ReadUInt32(); this.alphaThreshold = reader.ReadByte(); this.alphaFlag = reader.ReadByte(); this.zBufferWrite = reader.ReadByte(); this.zBufferTest = reader.ReadByte(); this.reflections = reader.ReadByte(); this.wetreflections = reader.ReadByte(); this.decal = reader.ReadByte(); this.doubleSided = reader.ReadByte(); this.decalnofade = reader.ReadByte(); this.noocclude = reader.ReadByte(); this.refraction = reader.ReadByte(); this.refractionfalloff = reader.ReadByte(); this.refractionpower = reader.ReadSingle(); this.envmap = reader.ReadByte(); this.envmapscale = reader.ReadSingle(); this.graytocolor = reader.ReadByte(); for (int index = 0; index < 9; index++) { this.textures[index] = Utils.ReadSizedString(reader).ToLower(CultureInfo.InvariantCulture).Replace("/", "\\").Replace("\0", string.Empty); } this.enablealpha = reader.ReadByte(); this.rimlighting = reader.ReadByte(); this.rimPower = reader.ReadSingle(); this.backlightPower = reader.ReadSingle(); // do no care what comes after this } catch (Exception ex) { logFile.WriteLog("Error BGSM " + fileName + " " + ex.Message); logFile.Close(); System.Environment.Exit(501); } reader.Close(); }
public void Read(string gameDir, string fileName, LogFile logFile) { MemoryStream memoryStream = new MemoryStream(); try { if (File.Exists(gameDir + fileName)) { try { FileStream fileStream = (FileStream)null; while (fileStream == null) { fileStream = Utils.GetFileStream(new FileInfo(gameDir + fileName), logFile); } BinaryReader binaryReader = new BinaryReader((Stream)fileStream); long length = binaryReader.BaseStream.Length; memoryStream.Write(binaryReader.ReadBytes((int)length), 0, (int)length); binaryReader.Close(); //logFile.WriteLog(" read " + fileName + " " + length); } catch (Exception ex) { logFile.WriteLog("Error reading " + fileName + " " + ex.Message); logFile.Close(); System.Environment.Exit(500); } } else if (BSAArchive.FileExists(fileName)) { try { byte[] newfile = BSAArchive.GetFile(fileName); int length = newfile.Length; memoryStream.Write(newfile, 0, length); } catch (Exception ex) { logFile.WriteLog("Error reading " + fileName + " from BSA/BA2 " + ex.Message + ex.Source); if (ex.Source.ToLower().Contains("mscorlib")) { logFile.WriteLog("Install Visual C++ Redistributable for Visual Studio 2015 from https://www.microsoft.com/en-us/download/details.aspx?id=48145"); } logFile.Close(); System.Environment.Exit(501); } } else { logFile.WriteLog(fileName + " not found"); logFile.Close(); System.Environment.Exit(404); } } catch (Exception ex) { logFile.WriteLog("Error accessing " + gameDir + fileName + " " + ex.Message); logFile.WriteLog("In case Mod Organizer is used, set output path outside of game and MO virtual file system directory"); logFile.Close(); System.Environment.Exit(502); } memoryStream.Position = 0L; BinaryReader reader = new BinaryReader((Stream)memoryStream); string error = "Read error " + fileName; try { this.header.Read(reader); var stream = reader.BaseStream; for (int index = 0; (long)index < (long)this.header.GetNumBlocks(); ++index) { //Console.WriteLine("Reading block " + index + " of " + this.header.GetNumBlocks() + " = " + stream.Position + " = " + this.header.GetBlockTypeAtIndex(index)); if (NiFile.classTypes.ContainsKey(this.header.GetBlockTypeAtIndex(index))) { NiObject niObject = (NiObject)Activator.CreateInstance(NiFile.classTypes[this.header.GetBlockTypeAtIndex(index)]); niObject.Read(this.header, reader); this.blocks.Add(niObject); this.rawBlocks.Add(new byte[0]); this.blockType.Add(this.header.GetBlockTypeAtIndex(index)); this.blockSize.Add(this.header.GetBlockSizeAtIndex(index)); } else { error = "Unsupported block " + index + " " + this.header.GetBlockTypeAtIndex(index) + " in " + gameDir + fileName; //logFile.WriteLog("Unsupported block " + index + " " + this.header.GetBlockTypeAtIndex(index) + " in " + gameDir + fileName); uint blockSizeAtIndex = this.header.GetBlockSizeAtIndex(index); this.blocks.Add((NiObject)null); this.rawBlocks.Add(reader.ReadBytes((int)blockSizeAtIndex)); this.blockType.Add(this.header.GetBlockTypeAtIndex(index)); this.blockSize.Add(this.header.GetBlockSizeAtIndex(index)); } } } catch { logFile.WriteLog(error); //logFile.Close(); //System.Environment.Exit(501); } reader.Close(); }