1+ using OldBit . Spectron . Files . IO ;
2+ using OldBit . Spectron . Files . Szx ;
3+
4+ namespace OldBit . Spectron . Files . Rzx . Blocks ;
5+
6+ /// <summary>
7+ /// Actual input recording data.
8+ /// </summary>
9+ public class RecordingBlock
10+ {
11+ /// <summary>
12+ /// Number of frames in the block.
13+ /// </summary>
14+ public DWord FrameCount { get ; private init ; }
15+
16+ /// <summary>
17+ /// Reserved.
18+ /// </summary>
19+ public byte Reserved { get ; private set ; }
20+
21+ /// <summary>
22+ /// T-STATES counter at the beginning.
23+ /// </summary>
24+ public DWord TStatesCounter { get ; private set ; }
25+
26+ /// <summary>
27+ /// Flags (b0: Protected (frames are encrypted with x-key), b1: Compressed data.)
28+ /// </summary>
29+ public DWord Flags { get ; private set ; }
30+
31+ public List < RecordingFrame > Frames { get ; } = [ ] ;
32+
33+ internal static RecordingBlock Read ( ByteStreamReader reader , DWord blockLength )
34+ {
35+ var block = new RecordingBlock
36+ {
37+ FrameCount = reader . ReadDWord ( ) ,
38+ Reserved = reader . ReadByte ( ) ,
39+ TStatesCounter = reader . ReadDWord ( ) ,
40+ Flags = reader . ReadDWord ( )
41+ } ;
42+
43+ var isProtected = ( block . Flags & 0x01 ) == 0x01 ;
44+ if ( isProtected )
45+ {
46+ throw new NotSupportedException ( "Protected recording blocks are not supported." ) ;
47+ }
48+
49+ var data = reader . ReadBytes ( ( int ) ( blockLength - 18 ) ) ;
50+
51+ var isCompressed = ( block . Flags & 0x02 ) == 0x02 ;
52+ if ( isCompressed )
53+ {
54+ data = ZLibHelper . Decompress ( data ) ;
55+ }
56+
57+ ReadFrames ( block , data ) ;
58+
59+ return block ;
60+ }
61+
62+ private static void ReadFrames ( RecordingBlock block , byte [ ] data )
63+ {
64+ var memoryStream = new MemoryStream ( data ) ;
65+ var reader = new ByteStreamReader ( memoryStream ) ;
66+
67+ for ( var i = 0 ; i < block . FrameCount ; i ++ )
68+ {
69+ var frame = RecordingFrame . Read ( reader ) ;
70+
71+ if ( frame . InCounter == 65535 )
72+ {
73+ // Repeated frame, copy the values from the previous frame
74+ frame . Values = block . Frames . LastOrDefault ( ) ? . Values ?? [ ] ;
75+ }
76+
77+ block . Frames . Add ( frame ) ;
78+ }
79+ }
80+ }
0 commit comments