Skip to content

Commit dc5ffbd

Browse files
author
David Khristepher Santos
committed
Experimental Stream class to expose PbpDiscEntry as a Stream
1 parent 9c0527b commit dc5ffbd

File tree

3 files changed

+127
-0
lines changed

3 files changed

+127
-0
lines changed

PSXPackagerGUI/Pages/SinglePage.xaml.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,6 +1127,23 @@ private void SaveImage_OnClick(object sender, RoutedEventArgs e)
11271127
Model.IsBusy = true;
11281128
_lastvalue = 0;
11291129
disc.ProgressEvent = ProgressEvent;
1130+
1131+
//var sourceStream = disc.GetDiscStream();
1132+
//var buffer = new byte[4096];
1133+
//int bytesRead = 0;
1134+
//int totalRead = 0;
1135+
1136+
//while ((bytesRead = sourceStream.Read(buffer, 0, 4096)) > 0)
1137+
//{
1138+
// totalRead += bytesRead;
1139+
// output.Write(buffer, 0, bytesRead);
1140+
// Dispatcher.Invoke(() =>
1141+
// {
1142+
// Model.Status = $"{totalRead}";
1143+
// });
1144+
//}
1145+
//output.Flush();
1146+
11301147
disc.CopyTo(output, _cancellationTokenSource.Token);
11311148

11321149
if (!_cancellationTokenSource.IsCancellationRequested)

Popstation/Pbp/PbpDiscEntry.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,11 @@ private uint GetIsoSize()
223223
return (uint)((out_buffer[104] + (out_buffer[105] << 8) + (out_buffer[106] << 16) + (out_buffer[107] << 24)) * ISO_BLOCK_SIZE);
224224
}
225225

226+
public PbpDiscStream GetDiscStream()
227+
{
228+
return new PbpDiscStream(this);
229+
}
230+
226231
public void CopyTo(Stream destination, CancellationToken cancellationToken)
227232
{
228233
uint totSize = 0;

Popstation/Pbp/PbpDiscStream.cs

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
using System;
2+
using System.IO;
3+
4+
namespace Popstation.Pbp;
5+
6+
public class PbpDiscStream : Stream
7+
{
8+
private readonly PbpDiscEntry _reader;
9+
10+
private readonly byte[] _buffer =
11+
new byte[16 * PbpReader.ISO_BLOCK_SIZE];
12+
13+
private int _bufPos;
14+
private int _bufLen;
15+
16+
private long _position;
17+
private int _blockIndex;
18+
19+
public PbpDiscStream(PbpDiscEntry reader)
20+
{
21+
_reader = reader;
22+
}
23+
24+
public override int Read(byte[] buffer, int offset, int count)
25+
{
26+
if (_position >= _reader.IsoSize)
27+
return 0; // EOF
28+
29+
int totalRead = 0;
30+
31+
while (count > 0)
32+
{
33+
// If buffer empty, refill
34+
if (_bufPos >= _bufLen)
35+
{
36+
_bufLen = (int)_reader.ReadBlock(_blockIndex++, _buffer);
37+
_bufPos = 0;
38+
39+
if (_bufLen == 0)
40+
break; // EOF
41+
}
42+
43+
int available = _bufLen - _bufPos;
44+
int toCopy = Math.Min(available, count);
45+
46+
if (_position + toCopy > _reader.IsoSize)
47+
{
48+
toCopy = (int)(_reader.IsoSize - _position);
49+
}
50+
51+
Array.Copy(_buffer, _bufPos, buffer, offset, toCopy);
52+
53+
_bufPos += toCopy;
54+
offset += toCopy;
55+
count -= toCopy;
56+
57+
totalRead += toCopy;
58+
_position += toCopy;
59+
60+
if (_position >= _reader.IsoSize)
61+
break;
62+
}
63+
64+
return totalRead;
65+
}
66+
67+
public override long Seek(long offset, SeekOrigin origin)
68+
{
69+
throw new NotSupportedException();
70+
//long newPos = origin switch
71+
//{
72+
// SeekOrigin.Begin => offset,
73+
// SeekOrigin.Current => _position + offset,
74+
// SeekOrigin.End => _reader.IsoSize + offset,
75+
// _ => throw new ArgumentOutOfRangeException()
76+
//};
77+
78+
//if (newPos < 0 || newPos > _reader.IsoSize)
79+
// throw new IOException("Seek out of range");
80+
81+
//_position = newPos;
82+
83+
//// Reset buffer and compute block index
84+
//_blockIndex = (int)(_position / _buffer.Length);
85+
//_bufPos = _bufLen = 0;
86+
87+
//return _position;
88+
}
89+
90+
public override bool CanRead => true;
91+
public override bool CanSeek => true;
92+
public override bool CanWrite => false;
93+
94+
public override long Length => _reader.IsoSize;
95+
96+
public override long Position
97+
{
98+
get => _position;
99+
set => Seek(value, SeekOrigin.Begin);
100+
}
101+
102+
public override void Flush() { }
103+
public override void SetLength(long value) => throw new NotSupportedException();
104+
public override void Write(byte[] buffer, int offset, int count) => throw new NotSupportedException();
105+
}

0 commit comments

Comments
 (0)