Skip to content

Commit

Permalink
Update version & FxCop code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
emoose committed Feb 2, 2020
1 parent f0e4054 commit ea1324f
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 50 deletions.
19 changes: 9 additions & 10 deletions XbRecUnpack/CabFile.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.IO;
using System.Runtime.InteropServices;

// TODO: this doesn't support cabs that use multiple CFFOLDERS (if any do)
// Also only supports LZX compression, there's code to support non-compressed data too but it's untested
Expand Down Expand Up @@ -70,8 +70,7 @@ public class CabFile
CFFOLDER folder;

public List<Tuple<CFFILE, string>> Entries = new List<Tuple<CFFILE, string>>();

BinaryReader reader;
readonly BinaryReader reader;

MemoryStream decompressed;

Expand Down Expand Up @@ -101,7 +100,7 @@ public bool Read()
if (!header.IsValid)
return false;

if((header.flags & 4) == 4)
if ((header.flags & 4) == 4)
{
headerReservedSize = reader.ReadUInt16();
folderReservedSize = reader.ReadByte();
Expand All @@ -111,13 +110,13 @@ public bool Read()
headerReserved = reader.ReadBytes(headerReservedSize);
}

if((header.flags & 1) == 1)
if ((header.flags & 1) == 1)
{
szCabinetPrev = reader.ReadNullTermASCII();
szDiskPrev = reader.ReadNullTermASCII();
}

if((header.flags & 2) == 2)
if ((header.flags & 2) == 2)
{
szCabinetNext = reader.ReadNullTermASCII();
szDiskNext = reader.ReadNullTermASCII();
Expand All @@ -126,7 +125,7 @@ public bool Read()
folder = reader.ReadStruct<CFFOLDER>();

reader.BaseStream.Position = headerPos + header.coffFiles;
for(int i = 0; i < header.cFiles; i++)
for (int i = 0; i < header.cFiles; i++)
{
var file = reader.ReadStruct<CFFILE>();
var name = reader.ReadNullTermASCII();
Expand All @@ -140,7 +139,7 @@ public bool Read()

bool DecompressFolder()
{
if(decompressed != null)
if (decompressed != null)
decompressed.Close();

decompressed = new MemoryStream();
Expand Down Expand Up @@ -174,7 +173,7 @@ bool DecompressFolder()
}
return true;
}
else if(compType == 0)
else if (compType == 0)
{
reader.BaseStream.Position = headerPos + folder.coffCabStart;
for (int i = 0; i < folder.cCFData; i++)
Expand Down Expand Up @@ -220,7 +219,7 @@ public bool GetEntry(string fileName, ref CFFILE entry, bool caseSensitive = tru

public Stream OpenFile(CFFILE entry)
{
if(decompressed == null)
if (decompressed == null)
if (!DecompressFolder())
return null;

Expand Down
24 changes: 12 additions & 12 deletions XbRecUnpack/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
* by emoose
*/

using DiscUtils.Iso9660;
using DiscUtils.Udf;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using DiscUtils.Iso9660;
using DiscUtils.Udf;

namespace XbRecUnpack
{
Expand All @@ -17,9 +17,9 @@ class Program
static string outputPath;

// Versions & Mobos supported by the input recovery
static List<int> Versions = new List<int>();
static Dictionary<string, List<int>> Motherboards = new Dictionary<string, List<int>>();
static List<string> XboxOGMotherboards = new List<string>();
static readonly List<int> Versions = new List<int>();
static readonly Dictionary<string, List<int>> Motherboards = new Dictionary<string, List<int>>();
static readonly List<string> XboxOGMotherboards = new List<string>();

[STAThread]
static void Main(string[] args)
Expand All @@ -28,7 +28,7 @@ static void Main(string[] args)
bool printRomInfo = false;

Console.WriteLine("XbRecUnpack - tool for extracting Xbox/Xbox360 SDKs & recoveries");
Console.WriteLine("v2.3456 by emoose");
Console.WriteLine("v2.34567 by emoose");
Console.WriteLine();
{
var maxPathSize = Util.GetMaxPathSize();
Expand All @@ -49,7 +49,7 @@ static void Main(string[] args)
extractFiles = false;
pathIdx++; // use next arg as filepath
}
else if(args[0].ToLower() == "-r")
else if (args[0].ToLower() == "-r")
{
printRomInfo = true;
pathIdx++;
Expand Down Expand Up @@ -85,7 +85,7 @@ static void Main(string[] args)
}

bool result = false;
if(Path.GetExtension(filePath).ToLower() == ".exe")
if (Path.GetExtension(filePath).ToLower() == ".exe")
result = ProcessRecoveryEXE(filePath, outputPath, extractFiles);
else if (Path.GetExtension(filePath).ToLower() == ".iso")
result = ProcessRecoveryISO(File.OpenRead(filePath), outputPath, extractFiles);
Expand Down Expand Up @@ -136,7 +136,7 @@ static void Main(string[] args)

List<string> moboKeys = Motherboards.Keys.ToList();
moboKeys.Sort();
foreach(var mobo in moboKeys)
foreach (var mobo in moboKeys)
{
var versions = Motherboards[mobo];
versions.Sort();
Expand Down Expand Up @@ -232,7 +232,7 @@ static bool ProcessRecoveryEXE(string exePath, string outputPath, bool extractFi
if (!recovery.Read())
return false;

if(extractFiles)
if (extractFiles)
Console.WriteLine($"Extracting to {outputPath}...");

return recovery.Extract(outputPath, !extractFiles);
Expand All @@ -246,7 +246,7 @@ static bool ProcessRecoveryZIP(string zipPath, string outputPath, bool extractFi
using (var archive = new ZipArchive(fileStream))
{
ZipArchiveEntry isoEntry = null;
foreach(var entry in archive.Entries)
foreach (var entry in archive.Entries)
{
if (Path.GetExtension(entry.Name).ToLower() == ".iso")
isoEntry = entry;
Expand Down Expand Up @@ -285,7 +285,7 @@ static bool ProcessRecoveryGDF(Stream isoStream, string outputPath, bool extract
using (var reader = new BinaryReader(gdf.OpenFile(entry)))
{
Stream dataStream = null;
if(extractFiles)
if (extractFiles)
{
if (!gdf.GetEntry("recdata.bin", ref entry, false))
{
Expand Down
4 changes: 2 additions & 2 deletions XbRecUnpack/RecoveryControlFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class RecoveryControlEntry
{
public const int LZXDecompressedBlockSize = 0x8000;

private RecoveryControlFile _baseFile; // ControlFile instance so we can retrieve version/device arrays
private readonly RecoveryControlFile _baseFile; // ControlFile instance so we can retrieve version/device arrays
public long DataOffset; // DataOffset isn't stored inside entry, so we have to track this ourselves

// All following fields are stored in the entry itself, in little-endian format
Expand Down Expand Up @@ -139,7 +139,7 @@ public void Extract(Stream dataStream, Stream outputStream)
dataStream.Read(input, 0, blockSize);
size = lzx.Decompress(ref input, blockSize, ref output, LZXDecompressedBlockSize);
outputStream.Write(output, 0, LZXDecompressedBlockSize);
if(size != LZXDecompressedBlockSize)
if (size != LZXDecompressedBlockSize)
Console.WriteLine($"LZX warning: returned 0x{size:X} bytes, expected 0x{LZXDecompressedBlockSize:X}!");

}
Expand Down
20 changes: 10 additions & 10 deletions XbRecUnpack/RemoteRecovery.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Text;

namespace XbRecUnpack
{
Expand All @@ -26,7 +26,7 @@ public string VariantAndLang
}
class RemoteRecovery
{
BinaryReader reader;
readonly BinaryReader reader;
List<ManifestEntry> Entries;
List<long> cabHeaderPos;

Expand Down Expand Up @@ -71,7 +71,7 @@ public bool Read()

while (position + 8 < length)
{
view = (view << 8) | reader.ReadByte() ; // shift-in next byte
view = (view << 8) | reader.ReadByte(); // shift-in next byte
position++;
viewed++;
if (view == pattern && viewed >= 8) // make sure we already got at least 4 bytes
Expand Down Expand Up @@ -124,15 +124,15 @@ public bool Read()
break;
}

if(csv == null)
if (csv == null)
{
Console.WriteLine("Error: failed to read manifest.csv from meta-cab section!");
return false;
}

Variants = new List<string>();
Entries = new List<ManifestEntry>();
foreach(var line in csv)
foreach (var line in csv)
{
if (string.IsNullOrEmpty(line))
continue;
Expand Down Expand Up @@ -169,7 +169,7 @@ public bool Read()
Console.WriteLine($"EXE contents:");
Console.WriteLine($"{Entries.Count} file{(Entries.Count == 1 ? "" : "s")}");
Console.WriteLine($"{Variants.Count} variant{(Variants.Count == 1 ? "" : "s")}:");
foreach(var variant in Variants)
foreach (var variant in Variants)
{
int numFiles = 0;
foreach (var entry in Entries)
Expand Down Expand Up @@ -200,7 +200,7 @@ public bool Extract(string destDirPath, bool listOnly = false)

int cabIndex = 0;
int totalIndex = 1;
foreach(var entry in Entries)
foreach (var entry in Entries)
{
var variantPath = Path.Combine(entry.VariantAndLang, entry.FilePath);
var entryPath = Path.Combine(destDirPath, variantPath);
Expand Down Expand Up @@ -231,7 +231,7 @@ public bool Extract(string destDirPath, bool listOnly = false)

var cfEntry = mainCab.Entries[cabIndex];

if(cfEntry.Item2.ToLower() != entry.FilePath.ToLower())
if (cfEntry.Item2.ToLower() != entry.FilePath.ToLower())
{
Console.WriteLine("Warning: mismatch between manifest entry and cab entry!");
}
Expand Down Expand Up @@ -259,7 +259,7 @@ public bool Extract(string destDirPath, bool listOnly = false)
byte[] buffer = new byte[32768];
while (sizeRemain > 0)
{
int read = (int)Math.Min((long)buffer.Length, sizeRemain);
int read = (int)Math.Min(buffer.Length, sizeRemain);
srcStream.Read(buffer, 0, read);
destStream.Write(buffer, 0, read);
sizeRemain -= read;
Expand All @@ -271,7 +271,7 @@ public bool Extract(string destDirPath, bool listOnly = false)

cabIndex++;
}
else if(entry.Action == "copy")
else if (entry.Action == "copy")
{
variantPath = Path.Combine(entry.Variant, entry.CopyDestPath);
string destPath = "";
Expand Down
8 changes: 4 additions & 4 deletions XbRecUnpack/Util.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Runtime.InteropServices;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;

namespace XbRecUnpack
{
Expand All @@ -14,7 +14,7 @@ public static class Util
public static string ReadMSString(this BinaryReader reader)
{
ushort length = reader.ReadUInt16();
byte[] str = reader.ReadBytes((int)length);
byte[] str = reader.ReadBytes(length);

if ((reader.BaseStream.Position & 1) == 1)
reader.BaseStream.Position++; // pad to next power of 2
Expand All @@ -39,7 +39,7 @@ public static string ReadNullTermASCII(this BinaryReader reader)
var list = new List<byte>();

var byt = reader.ReadByte();
while(byt != 0)
while (byt != 0)
{
list.Add(byt);
byt = reader.ReadByte();
Expand Down
19 changes: 9 additions & 10 deletions XbRecUnpack/XboxGDFImage.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;

// NOTE: Only tested with Xbox OG recovery ISOs, games & X360 ISOs likely won't work well with it
namespace XbRecUnpack
Expand Down Expand Up @@ -45,8 +45,7 @@ public class XboxGDFImage
const int kSectorSize = 0x800;

public GdfHeader VolumeDescriptor;

BinaryReader reader;
readonly BinaryReader reader;

public List<Tuple<GdfEntry, string>> Entries = new List<Tuple<GdfEntry, string>>();

Expand All @@ -57,14 +56,14 @@ public XboxGDFImage(Stream imageStream)

static long SectorToOffset(long sector)
{
return sector * (long)kSectorSize;
return sector * kSectorSize;
}

public bool Read()
{
reader.BaseStream.Position = 0x8000;
VolumeDescriptor = reader.ReadStruct<GdfHeader>();
if(!VolumeDescriptor.IsValid)
if (!VolumeDescriptor.IsValid)
{
reader.BaseStream.Position = 0x10000;
VolumeDescriptor = reader.ReadStruct<GdfHeader>();
Expand Down Expand Up @@ -93,9 +92,9 @@ public bool Read()

public bool GetEntry(string fileName, ref GdfEntry entry, bool caseSensitive = true)
{
foreach(var pair in Entries)
foreach (var pair in Entries)
{
if(caseSensitive)
if (caseSensitive)
{
if (fileName == pair.Item2)
{
Expand Down Expand Up @@ -124,9 +123,9 @@ public Stream OpenFile(GdfEntry entry)

public class WindowedStream : Stream
{
private Stream source;
private readonly Stream source;

private long sourceOffset;
private readonly long sourceOffset;

private long length = -1;

Expand Down
4 changes: 2 additions & 2 deletions XbRecUnpack/XboxRom.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class XboxRomHeader
public int MotherboardTypeInt;
public int MotherboardTypeRevision;

static string[] kMotherboardTypes = {
static readonly string[] kMotherboardTypes = {
"none/unk",
"xenon", // -w
"zephyr", // -z
Expand Down Expand Up @@ -100,7 +100,7 @@ public void Read(BinaryReader reader)
NextHeader = reader.ReadStruct<BLDR>();
NextHeader.EndianSwap();

if(FlashHeader.dwSmcBootAddr != 0)
if (FlashHeader.dwSmcBootAddr != 0)
{
reader.BaseStream.Position = headerPos + FlashHeader.dwSmcBootAddr;
SmcData = reader.ReadBytes((int)FlashHeader.dwSmcBootSize);
Expand Down

0 comments on commit ea1324f

Please sign in to comment.