source: trunk/UEngFile.pas

Last change on this file was 3, checked in by chronos, 8 years ago
  • Added: Classes for decoding MAP, CPS, SHP and ENG file formats.
File size: 1.9 KB
Line 
1unit UEngFile;
2
3{$mode delphi}
4
5interface
6
7uses
8 Classes, SysUtils;
9
10type
11
12 { TEngFile }
13
14 TEngFile = class
15 private
16 function DecodeString(Data: array of Byte): string;
17 public
18 FileName: string;
19 Lines: TStringList;
20 procedure LoadFromFile(FileName: string);
21 constructor Create;
22 destructor Destroy; override;
23 end;
24
25implementation
26
27const
28 Dictionary: string = ' etainosrlhcdupmtasio wb rnsdalmh ieorasnrtlc synstcloer dtgesionr ufmsw tep.icae oiadur laeiyodeia otruetoakhlr eiu,.oansrctlaileoiratpeaoip bm';
29
30{ TEngFile }
31
32function TEngFile.DecodeString(Data: array of Byte): string;
33var
34 I: Integer;
35begin
36 Result := '';
37 for I := 0 to Length(Data) - 1 do begin
38 if (Data[I] and $80) > 0 then begin
39 Result := Result + Dictionary[(Data[I] and $7f) shr 3 + 1];
40 Result := Result + Dictionary[(Data[I] and $7f) + 16 + 1];
41 end else
42 if Data[I] <> 0 then
43 Result := Result + Chr(Data[I]);
44 end;
45end;
46
47procedure TEngFile.LoadFromFile(FileName: string);
48var
49 F: TFileStream;
50 Buffer: array of Byte;
51 I: Integer;
52 Addr: array of Word;
53begin
54 Lines.Clear;
55 F := TFileStream.Create(FileName, fmOpenRead);
56{ // Read string address array
57 SetLength(Addr, 1);
58 Addr[0] := F.ReadWord;
59 while F.Position < Addr[0] do begin
60 SetLength(Addr, Length(Addr) + 1);
61 Addr[Length(Addr) - 1] := F.ReadWord;
62 end;
63
64 // Read and decode strings
65 for I := 0 to Length(Addr) - 1 do begin
66 F.Position := Addr[I];
67 if I < Length(Addr) - 1 then SetLength(Buffer, Addr[I + 1] - Addr[I])
68 else SetLength(Buffer, F.Size - Addr[I]);
69 F.Read(Buffer[0], Length(Buffer));
70 Lines.Add(DecodeString(Buffer));
71 end;
72 }
73 SetLength(Buffer, F.Size);
74 F.Read(Buffer[0], F.Size);
75 Lines.Add(DecodeString(Buffer));
76 F.Free;
77end;
78
79constructor TEngFile.Create;
80begin
81 Lines := TStringList.Create;
82end;
83
84destructor TEngFile.Destroy;
85begin
86 FreeAndNil(Lines);
87 inherited Destroy;
88end;
89
90end.
91
Note: See TracBrowser for help on using the repository browser.