Skip to content

Commit 9f656a7

Browse files
committed
Switch to Squid-Box.SevenZipSharp to get extraction notifications
Using ExtractFiles so you don't have to skip over preceding files Fix overflow bug when extracting from PBP > 2Gb (Riven)
1 parent 8ca11d8 commit 9f656a7

File tree

12 files changed

+295
-243
lines changed

12 files changed

+295
-243
lines changed

PSXPackager/ConsoleNotifications.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public void Notify(PopstationEventEnum @event, object value)
2828
_total = Convert.ToInt64(value);
2929
break;
3030
case PopstationEventEnum.ConvertSize:
31+
case PopstationEventEnum.ExtractSize:
3132
case PopstationEventEnum.WriteSize:
3233
_total = Convert.ToInt64(value);
3334
break;
@@ -47,10 +48,15 @@ public void Notify(PopstationEventEnum @event, object value)
4748
_cursorYPos = Console.CursorTop;
4849
Console.CursorVisible = false;
4950
break;
50-
51+
case PopstationEventEnum.DecompressStart:
52+
Console.Write($"Decompressing file {value} - ");
53+
_cursorYPos = Console.CursorTop;
54+
Console.CursorVisible = false;
55+
break;
5156
case PopstationEventEnum.ConvertComplete:
5257
case PopstationEventEnum.ExtractComplete:
5358
case PopstationEventEnum.WriteComplete:
59+
case PopstationEventEnum.DecompressComplete:
5460
_charsToDelete = 0;
5561
Console.CursorVisible = true;
5662
Console.WriteLine();
@@ -66,6 +72,14 @@ public void Notify(PopstationEventEnum @event, object value)
6672
_lastTicks = DateTime.Now.Ticks;
6773
}
6874
break;
75+
case PopstationEventEnum.DecompressProgress:
76+
//Console.SetCursorPosition(0, _cursorYPos);
77+
if (DateTime.Now.Ticks - _lastTicks > 100000)
78+
{
79+
Overwrite($"{value}%");
80+
_lastTicks = DateTime.Now.Ticks;
81+
}
82+
break;
6983
}
7084
}
7185

PSXPackager/PSXPackager.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212

1313

1414
<ItemGroup>
15+
<PackageReference Include="7z.Libs" Version="19.0.2" />
1516
<PackageReference Include="CommandLineParser" Version="2.8.0" />
16-
<PackageReference Include="SevenZipExtractor" Version="1.0.15" />
17+
<PackageReference Include="Squid-Box.SevenZipSharp" Version="1.3.283" />
1718
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
1819
</ItemGroup>
1920

PSXPackager/Processing.cs

Lines changed: 88 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
using Popstation;
99
using Popstation.Cue;
1010
using Popstation.M3u;
11-
using SevenZipExtractor;
11+
using SevenZip;
1212

1313
namespace PSXPackager
1414
{
@@ -104,6 +104,20 @@ public bool ProcessFile(
104104
var files = new List<string>();
105105
var tocs = new List<string>();
106106
var m3UFile = M3uFileReader.Read(file);
107+
108+
if (m3UFile.FileEntries.Count == 0)
109+
{
110+
_notifications?.Notify(PopstationEventEnum.Info, $"Invalid number of entries, found {m3UFile.FileEntries.Count}");
111+
return false;
112+
}
113+
else if (m3UFile.FileEntries.Count > 5)
114+
{
115+
_notifications?.Notify(PopstationEventEnum.Info, $"Invalid number of entries, found {m3UFile.FileEntries.Count}, max is 5");
116+
return false;
117+
}
118+
119+
_notifications?.Notify(PopstationEventEnum.Info, $"Found {m3UFile.FileEntries.Count} entries");
120+
107121
foreach (var fileEntry in m3UFile.FileEntries)
108122
{
109123
if (FileExtensionHelper.IsCue(fileEntry))
@@ -112,10 +126,16 @@ public bool ProcessFile(
112126
files.Add(outfile);
113127
tocs.Add(srcToc);
114128
}
115-
else
129+
else if (FileExtensionHelper.IsImageFile(fileEntry))
116130
{
117131
files.Add(Path.Combine(filePath, fileEntry));
118132
}
133+
else
134+
{
135+
_notifications?.Notify(PopstationEventEnum.Info, $"Unsupported playlist entry '{fileEntry}'");
136+
_notifications?.Notify(PopstationEventEnum.Info, "Only the following are supported: .cue .img .bin .iso");
137+
return false;
138+
}
119139
}
120140
result = ConvertIsos(files.ToArray(), tocs.ToArray(), outPath, compressionLevel, checkIfFileExists, cancellationToken);
121141
}
@@ -149,7 +169,7 @@ public bool ProcessFile(
149169
catch (Exception ex)
150170
{
151171
Console.WriteLine(ex.Message);
152-
throw;
172+
//throw;
153173
}
154174
finally
155175
{
@@ -239,7 +259,7 @@ static MergedBin MergeBins(string file, CueFile cueFilex, string tempPath)
239259
var newIndex = new CueIndex
240260
{
241261
Number = idx.Number,
242-
Position = idx.Position + Helper.PositionFromFrames(currentFrame)
262+
Position = idx.Position + TOCHelper.PositionFromFrames(currentFrame)
243263
};
244264
indexes.Add(newIndex);
245265
}
@@ -275,42 +295,67 @@ static string Shorten(ulong size)
275295
}
276296
}
277297

278-
static List<string> Unpack(string file, string tempPath, CancellationToken cancellationToken)
298+
List<string> Unpack(string file, string tempPath, CancellationToken cancellationToken)
279299
{
280-
var files = new List<string>();
300+
List<string> files;
281301

282-
using (ArchiveFile archiveFile = new ArchiveFile(file))
302+
using (var archiveFile = new SevenZipExtractor(file))
283303
{
304+
var archiveFiles = archiveFile.ArchiveFileData.Select(x => x.FileName).ToList();
305+
306+
archiveFile.FileExtractionStarted += (sender, args) => _notifications.Notify(PopstationEventEnum.DecompressStart, args.FileInfo.FileName);
307+
archiveFile.Extracting += ArchiveFileOnExtracting;
308+
archiveFile.FileExtractionFinished += (sender, args) => _notifications.Notify(PopstationEventEnum.DecompressComplete, null);
309+
//archiveFile.BeginExtractFiles(ExtractFileCallback);
284310
//var unpackTasks = new List<Task>();
285-
foreach (Entry entry in archiveFile.Entries)
286-
{
287-
if (FileExtensionHelper.IsImageFile(entry.FileName) || FileExtensionHelper.IsCue(entry.FileName))
288-
{
289-
Console.WriteLine($"Extracting {entry.FileName} ({Shorten(entry.Size)})");
290-
var path = Path.Combine(tempPath, entry.FileName);
291-
// extract to file
292-
files.Add(path);
293-
entry.Extract(path, false);
294-
295-
//unpackTasks.Add(Task.Run(() =>
296-
//{
297-
// entry.Extract(path, false);
298-
// files.Add(path);
299-
//}, cancellationToken));
300-
}
301-
if (cancellationToken.IsCancellationRequested)
302-
{
303-
return files;
304-
}
305-
}
311+
archiveFile.ExtractFiles(tempPath, archiveFiles.ToArray());
312+
313+
files = archiveFiles.Select(x => Path.Combine(tempPath, x)).ToList();
314+
315+
//foreach (var entry in archiveFile.ArchiveFileData)
316+
//{
317+
// if (FileExtensionHelper.IsImageFile(entry.FileName) || FileExtensionHelper.IsCue(entry.FileName))
318+
// {
319+
// //Console.WriteLine($"Extracting {entry.FileName} ({Shorten(entry.Size)})");
320+
// var path = Path.Combine(tempPath, entry.FileName);
321+
// // extract to file
322+
// files.Add(path);
323+
324+
// _notifications.Notify(PopstationEventEnum.DecompressStart, entry.FileName);
325+
// _notifications.Notify(PopstationEventEnum.DecompressComplete, null);
326+
327+
// using (var stream = new FileStream(path, FileMode.Create, FileAccess.Write))
328+
// {
329+
// _notifications.Notify(PopstationEventEnum.DecompressStart, entry.FileName);
330+
// archiveFile.ExtractFile(entry.FileName, stream);
331+
// _notifications.Notify(PopstationEventEnum.DecompressComplete, null);
332+
// }
333+
334+
// }
335+
// if (cancellationToken.IsCancellationRequested)
336+
// {
337+
// return files;
338+
// }
339+
//}
306340

307341
//Task.WaitAll(unpackTasks.ToArray());
342+
archiveFile.Extracting -= ArchiveFileOnExtracting;
308343

309344
}
310345

311346
return files;
312347
}
313348

349+
//private void ExtractFileCallback(ExtractFileCallbackArgs extractfilecallbackargs)
350+
//{
351+
// extractfilecallbackargs.CancelExtraction
352+
//}
353+
354+
private void ArchiveFileOnExtracting(object sender, ProgressEventArgs e)
355+
{
356+
_notifications.Notify(PopstationEventEnum.DecompressProgress, e.PercentDone);
357+
}
358+
314359
private const int RING_BUFFER_SIZE = 14;
315360

316361
string[] gamecodes =
@@ -529,9 +574,11 @@ private bool ConvertIsos(
529574
});
530575
}
531576

532-
var popstation = new Popstation.Popstation();
533-
popstation.ActionIfFileExists = _notifications.ActionIfFileExists;
534-
popstation.OnEvent = _notifications.Notify;
577+
var popstation = new Popstation.Popstation
578+
{
579+
ActionIfFileExists = _notifications.ActionIfFileExists,
580+
OnEvent = _notifications.Notify
581+
};
535582

536583
return popstation.Convert(info, cancellationToken);
537584
}
@@ -577,9 +624,11 @@ private bool ConvertIso(
577624
CheckIfFileExists = checkIfFileExists
578625
};
579626

580-
var popstation = new Popstation.Popstation();
581-
popstation.ActionIfFileExists = _notifications.ActionIfFileExists;
582-
popstation.OnEvent = _notifications.Notify;
627+
var popstation = new Popstation.Popstation
628+
{
629+
ActionIfFileExists = _notifications.ActionIfFileExists,
630+
OnEvent = _notifications.Notify
631+
};
583632

584633
return popstation.Convert(info, cancellationToken);
585634
}
@@ -603,9 +652,11 @@ private void ExtractPbp(
603652
CheckIfFileExists = checkIfFileExists
604653
};
605654

606-
var popstation = new Popstation.Popstation();
607-
popstation.ActionIfFileExists = _notifications.ActionIfFileExists;
608-
popstation.OnEvent = _notifications.Notify;
655+
var popstation = new Popstation.Popstation
656+
{
657+
ActionIfFileExists = _notifications.ActionIfFileExists,
658+
OnEvent = _notifications.Notify
659+
};
609660

610661
popstation.Extract(info, cancellationToken);
611662
}

PSXPackager/Program.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ static void CancelEventHandler(object sender, ConsoleCancelEventArgs args)
2525

2626
static void Main(string[] args)
2727
{
28+
SevenZip.SevenZipBase.SetLibraryPath("x64/7z.dll");
29+
2830
_cancellationTokenSource = new CancellationTokenSource();
2931

3032
Console.CancelKeyPress += CancelEventHandler;
@@ -49,7 +51,7 @@ static void Main(string[] args)
4951

5052
if (!string.IsNullOrEmpty(o.Discs))
5153
{
52-
if (!Regex.IsMatch(o.Discs, "\\d(,\\d)+"))
54+
if (!Regex.IsMatch(o.Discs, "\\d(,\\d)*"))
5355
{
5456
Console.WriteLine($"Invalid discs specification, please enter a comma separated list of values from 1-5");
5557
return;

PSXPackager/Properties/launchSettings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"profiles": {
33
"PSXPackager": {
44
"commandName": "Project",
5-
"commandLineArgs": "-i \"C:\\roms\\Legend of Dragoon, The.PBP\""
5+
"commandLineArgs": "-i \"C:\\roms\\Riven - The Sequel to Myst.PBP\" -o \"C:\\roms\" -d 5"
66
}
77
}
88
}

Popstation/Iso/IsoIndexLite.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// Struct to store an ISO index
44
public class IsoIndexLite
55
{
6-
public int Offset { get; set; }
6+
public uint Offset { get; set; }
77
public int Length { get; set; }
88
}
99
}

0 commit comments

Comments
 (0)