-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathProgram.cs
More file actions
124 lines (114 loc) · 4.43 KB
/
Program.cs
File metadata and controls
124 lines (114 loc) · 4.43 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
using System;
using System.IO;
using FragmentUpdater.Patchers;
using Ps2IsoTools.UDF;
using Serilog;
namespace FragmentUpdater
{
class Program
{
static void Main(string[] args)
{
Log.Logger = new LoggerConfiguration()
.WriteTo.Console()
.WriteTo.File(Path.Combine(AppContext.BaseDirectory, "ViPatchLog.txt"), outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {Message}{NewLine}{Exception}")
.MinimumLevel.Information()
.CreateLogger();
Log.Logger.Information("Beginning Vi's Fragment Updater");
#if DEBUG
string inputISO = @"P:\DotHack\Fragment\ViPatch\fragment.iso",
outputISO = @"P:\DotHack\Fragment\ViPatch\fragmentCopy.iso",
foodEPath = @"P:\DotHack\Fragment\ViPatch\food_E.bin";
#else
string inputISO, outputISO, loc = System.AppContext.BaseDirectory, foodEPath;
if (loc == "")
loc = @".\";
loc = Path.GetDirectoryName(loc);
foodEPath = Path.Combine(loc, "food_E.bin");
if (args.Length == 0)
{
inputISO = Path.Combine(loc, "fragment.iso");
outputISO = Path.Combine(loc, "dotHack fragment (EN).iso");
}
else if (args.Length == 1)
{
inputISO = CheckFilePath(args[0]);
outputISO = inputISO;
}
else
{
inputISO = CheckFilePath(args[0]);
outputISO = CheckFilePath(args[1]);
}
#endif
if (inputISO != outputISO)
{
if (File.Exists(inputISO))
CopyFile(inputISO, outputISO);
else
Log.Logger.Error($"Could not find input file \"{inputISO}\".");
}
if (File.Exists(outputISO))
{
Log.Logger.Information($"Writing patches to: {outputISO}");
using (UdfEditor editor = new(outputISO))
{
ViFragmentPatcher.PatchISO(editor);
if (File.Exists(foodEPath))
{
EnglishGruntyFoodPatcher.PatchISO(editor, File.Open(foodEPath, FileMode.Open, FileAccess.ReadWrite));
}
}
Log.Logger.Information("Patch process complete!");
}
else
{
Log.Logger.Error($"Could not find output file \"{outputISO}\".");
}
}
private static string CheckFilePath(string fileName)
{
if (fileName.Contains(@"\") || fileName.Contains(@"/"))
{
if (fileName.Contains(".iso"))
return fileName;
else
return $"{fileName}.iso";
}
else
{
string loc = AppContext.BaseDirectory;
if (loc == "")
loc = @".\";
if (fileName.Contains(".iso"))
return Path.Combine(Path.GetDirectoryName(loc), fileName);
else
return Path.Combine(Path.GetDirectoryName(loc), $"{fileName}.iso");
}
}
private static void CopyFile(string inputFilePath, string outputFilePath)
{
if (File.Exists(outputFilePath))
File.Delete(outputFilePath);
int bufferSize = 1024 * 1024;
int progress = 0;
Log.Logger.Information($"Copying {inputFilePath} to {outputFilePath}");
using (FileStream fileStream = new FileStream(outputFilePath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite))
{
using (FileStream fs = new FileStream(inputFilePath, FileMode.Open, FileAccess.ReadWrite))
{
fileStream.SetLength(fs.Length);
int bytesRead = -1;
byte[] bytes = new byte[bufferSize];
while ((bytesRead = fs.Read(bytes, 0, bufferSize)) > 0)
{
progress += bytesRead;
Console.Write($"\r{progress.ToString("X8")} / {fs.Length.ToString("X8")} bytes copied... ");
fileStream.Write(bytes, 0, bytesRead);
}
}
Console.WriteLine("");
}
}
}
}