-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathProgram.cs
102 lines (85 loc) · 3.25 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace XCatTool
{
class Program
{
const int BUFFER_SIZE = 1024 * 64;
static void Main(string[] args)
{
new Program(args);
}
public Program(string[] args)
{
if (args.Length != 1)
{
Console.WriteLine("Usage: XCatTool.exe <catfilename>");
return;
}
var inputFile = Path.GetFileNameWithoutExtension(args[0]);
var catFile = String.Concat(inputFile, ".cat");
var datFile = String.Concat(inputFile, ".dat");
if (!File.Exists(catFile) || !File.Exists(datFile))
{
Console.WriteLine("Could not locate the {0} or {1} files", catFile, datFile);
return;
}
Extract(catFile, datFile);
}
private void Extract(string catFile, string datFile)
{
var catFileContents = File.ReadAllLines(catFile);
using (var datFileStream = File.OpenRead(datFile))
{
var buffer = new byte[BUFFER_SIZE];
var totalLines = (float)catFileContents.Length;
var currentLine = 0.0;
Console.WriteLine("Extracting...\t");
Console.CursorVisible = false;
foreach (var line in catFileContents)
{
WriteInPlace("Line {0}/{1}", ++currentLine, totalLines);
var pieces = line.Split(' ').ToList();
if (pieces.Count >= 4)
{
while (pieces.Count > 4)
{
pieces[0] = pieces[0] + pieces[1];
pieces.RemoveAt(1);
}
if (!string.IsNullOrWhiteSpace(Path.GetDirectoryName(pieces[0])) && !Directory.Exists(Path.GetDirectoryName(pieces[0])))
{
Directory.CreateDirectory(Path.GetDirectoryName(pieces[0]));
}
using (var outputFile = File.OpenWrite(pieces[0]))
{
int remBytes = int.Parse(pieces[1]);
while (remBytes > 0)
{
int br = datFileStream.Read(buffer, 0, Math.Min(BUFFER_SIZE, remBytes));
outputFile.Write(buffer, 0, br);
remBytes -= br;
}
}
}
else
{
Console.WriteLine("Invalid line, contents: " + line);
}
}
Console.CursorVisible = true;
Console.WriteLine("Done");
}
}
private void WriteInPlace(string text, params object[] args)
{
var currentX = Console.CursorLeft;
var currentY = Console.CursorTop;
Console.Write(text, args);
Console.SetCursorPosition(currentX, currentY);
}
}
}