-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathProgram.cs
172 lines (154 loc) · 6.63 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using UnPSARC.Helpers;
namespace UnPSARC
{
internal class Program
{
private static string archiveExtension = ".psarc";
public static bool oodleExist = false;
private static void PrintUsage()
{
Console.WriteLine("Usage:");
Console.WriteLine("Unpack: Unpsarc.exe <psarc path> [destination folder]");
Console.WriteLine(" Pack : Unpsarc.exe <content folder> [archive filename to create]");
Console.WriteLine("\nPress any key to exit");
Console.ReadKey();
}
private static bool CheckForOodle()
{
string currentApplicationPath = System.Reflection.Assembly.GetExecutingAssembly().Location;
string currentApplicationDirectory = Path.GetDirectoryName(currentApplicationPath);
string oodleLocation = currentApplicationDirectory + "\\oo2core_9_win64.dll";
return File.Exists(oodleLocation);
}
static void Main(string[] args)
{
Console.WriteLine("UnPSARC (Archive Tool For PlayStation Archive files 'PSARC')");
Console.WriteLine("By NoobInCoding");
Console.WriteLine("https://github.com/rm-NoobInCoding/UnPSARC");
Console.WriteLine("");
if (args.Length < 1)
{
PrintUsage();
return;
}
oodleExist = CheckForOodle();
bool isFile = File.Exists(args[0]);
bool isDirectory = Directory.Exists(args[0]);
string outputName = null;
if (args.Length > 1)
{
if ((CommendHelper.IsFullPath(args[1]) && CommendHelper.IsValidPath(args[1])) || !CommendHelper.IsFullPath(args[1]))
outputName = args[1];
else
{
Console.WriteLine("Output directory is not a valid path! Make sure your path is in quotation marks.");
PrintUsage();
return;
}
}
if (isFile && !isDirectory)
{
if (Path.GetExtension(args[0]) == archiveExtension)
{
if (outputName == null)
{
string TempDir = Path.GetDirectoryName(args[0]);
if (TempDir == "") TempDir = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
string customOutputDirectory = TempDir + "\\" + Path.GetFileNameWithoutExtension(args[0]) + "_Unpacked";
if (Directory.Exists(customOutputDirectory) == false)
Directory.CreateDirectory(customOutputDirectory);
UnpackArchiveFile(args[0], customOutputDirectory);
}
else
{
UnpackArchiveFile(args[0], outputName);
}
}
}
else if (isDirectory && !isFile)
{
if (outputName == null)
{
Console.WriteLine($"Packing {args[0]} to {Path.GetFileNameWithoutExtension(args[0]) + ".psarc"}");
PackArchiveFile(args[0], "../" + Path.GetFileNameWithoutExtension(args[0]) + ".psarc");
}
else
{
if (!CommendHelper.IsFullPath(outputName))
{
if (outputName.StartsWith("\\")) outputName = outputName.Remove(0, 1);
outputName = Path.Combine(Environment.CurrentDirectory, outputName);
}
Console.WriteLine($"Packing {args[0]} to {outputName}");
PackArchiveFile(args[0], outputName);
}
}
else
{
Console.WriteLine("Input path argument is not a valid file/directory, or does not exist! (Make sure your path is in quotation marks)");
PrintUsage();
}
}
private static void PackArchiveFile(string contentFolderPath, string outputFilename)
{
File.WriteAllText(Path.Combine(contentFolderPath, "Filenames.txt"), MakeFileNameTable(contentFolderPath));
File.WriteAllBytes(Path.Combine(contentFolderPath, "r.exe"), Packer.psarc);
ProcessStartInfo psi = new ProcessStartInfo
{
FileName = Path.Combine(contentFolderPath, "r.exe"),
Arguments = $"create -a --skip-missing-files --inputfile=filenames.txt --output=\"{outputFilename}\" -N -y",
RedirectStandardOutput = true,
RedirectStandardError = true,
WorkingDirectory = contentFolderPath,
UseShellExecute = false,
CreateNoWindow = true,
};
using (var process = new Process { StartInfo = psi })
{
process.OutputDataReceived += (sender, e) =>
{
if (e.Data != null)
{
Console.WriteLine(e.Data);
}
};
process.ErrorDataReceived += (sender, e) =>
{
if (e.Data != null)
{
Console.Error.WriteLine(e.Data);
}
};
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();
File.Delete(Path.Combine(contentFolderPath, "r.exe"));
File.Delete(Path.Combine(contentFolderPath, "Filenames.txt"));
}
}
private static string MakeFileNameTable(string contentFolderPath)
{
List<string> files = new List<string>();
foreach (string fname in Directory.GetFiles(contentFolderPath, "*.*", SearchOption.AllDirectories))
{
if (Path.GetFileName(fname) == "Filenames.txt")
continue;
string _ = fname.Replace(contentFolderPath + "\\", "").Replace("\\", "/");
files.Add(_);
}
return string.Join("\n", files);
}
private static void UnpackArchiveFile(string inputPath, string outputDirectory)
{
Console.WriteLine("Unpacking {0}...", Path.GetFileName(inputPath));
Stream R = File.OpenRead(inputPath);
Archive.Unpack(R, outputDirectory);
R.Close();
}
}
}