source: trunk/Packages/CoolStreaming/USubStream.pas

Last change on this file was 2, checked in by chronos, 9 years ago
  • Added: TPakFile class for extraction PAK files from original game.
File size: 2.1 KB
Line 
1unit USubStream;
2
3{$mode delphi}
4
5interface
6
7uses
8 Classes, SysUtils;
9
10type
11
12 { TSubStream }
13
14 TSubStream = class(TStream)
15 private
16 FSource: TStream;
17 FSourcePosition: Int64;
18 FSize, FPosition: Longint;
19 public
20 function GetSize: Int64; override;
21 procedure SetSize(NewSize: Longint); override;
22 function Seek(Offset: Longint; Origin: Word): Longint; override;
23 function Read(var Buffer; Count: Longint): Longint; override;
24 function Write(const Buffer; Count: Longint): Longint; override;
25 procedure Assign(ASource: TObject);
26 property Source: TStream read FSource write FSource;
27 property SourcePosition: Int64 read FSourcePosition write FSourcePosition;
28 end;
29
30implementation
31
32{ TSubStream }
33
34function TSubStream.GetSize:Int64;
35begin
36 Result := FSize;
37end;
38
39procedure TSubStream.SetSize(NewSize:Longint);
40begin
41 FSize := NewSize;
42 if FPosition > FSize then
43 FPosition := FSize;
44end;
45
46function TSubStream.Seek(Offset:Longint;Origin:Word):Longint;
47begin
48 case Origin of
49 soFromBeginning: FPosition := Offset;
50 soFromEnd: FPosition := FSize + Offset;
51 soFromCurrent: FPosition := FPosition + Offset;
52 end;
53 Result := FPosition;
54end;
55
56function TSubStream.Read(var Buffer; Count:Longint): Longint;
57var
58 TempPos: Int64;
59begin
60 TempPos := FSource.Position;
61 FSource.Position := FSourcePosition + Position;
62 Result := FSource.Read(Buffer, Count);
63 FSource.Position := TempPos;
64 Inc(FPosition, Result);
65end;
66
67function TSubStream.Write(const Buffer;Count:Longint):Longint;
68var
69 TempPos: Int64;
70begin
71 TempPos := FSource.Position;
72 FSource.Position := FSourcePosition + Position;
73 Result := Source.Write(Buffer, Count);
74 FSource.Position := TempPos;
75 Inc(FPosition, Result);
76end;
77
78procedure TSubStream.Assign(ASource: TObject);
79begin
80 if ASource is TSubStream then begin
81 FSource := TSubStream(ASource).FSource;
82 FSourcePosition := TSubStream(ASource).FSourcePosition;
83 FSize := TSubStream(ASource).FSize;
84 FPosition := TSubStream(ASource).FPosition;
85 end;
86end;
87
88end.
89
Note: See TracBrowser for help on using the repository browser.