This repository has been archived by the owner on Aug 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathlod.pas
188 lines (142 loc) · 4.45 KB
/
lod.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
{ This file is a part of Map editor for VCMI project
Copyright (C) 2013-2017 Alexander Shishkin alexvins@users.sourceforge.net
This source is free software; you can redistribute it and/or modify it under the terms of the GNU General Public
License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later
version.
This code is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
details.
A copy of the GNU General Public License is available on the World Wide Web at
<http://www.gnu.org/copyleft/gpl.html>. You can also obtain it by writing to the Free Software Foundation, Inc., 59
Temple Place - Suite 330, Boston, MA 02111-1307, USA.
}
unit lod;
{$I compilersetup.inc}
interface
uses
SysUtils, Classes, fgl, filesystem_base, zlib_stream, LazUTF8Classes;
//Format Specifications
//
//char {4} - Header ("LOD\0")
//byte {4} - "file use flag", i.e. used as 'base' or 'extension' resource file. 200 if base resource file, 500 if extension, little endian values.
//uint32 {4} - Number Of Files
//byte {80} - Unknown
//
//// for each file
//
// char {16} - Filename (null)
// uint32 {4} - File Offset
// uint32 {4} - Uncompressed file size
// uint32 {4} - File type(?)
// uint32 {4} - File Length
//
//
//byte {X} - File Data
//
type
TLod = class;
TLodItem = packed record
Filename: array [0..16 - 1] of AnsiChar;
FileOffset: Int32;
UncompressedFileSize: Int32;
FileType: Int32;
FileLength: Int32;
end;
//todo: add match filter flag to not to store filtered items
TOnItemFound = procedure (Alod: TLod; constref AItem: TLodItem) of object;
{ TLod }
TLod = class
private
FFileStream: TFileStreamUTF8;
//FBuffer: TZBuffer;
public
constructor Create(AFullPath: string);
destructor Destroy; override;
procedure Scan(ACallback: TOnItemFound);
procedure LoadResource(AResource: IResource; constref AItem:TLodItem);
end;
TLodList = specialize TFPGObjectList<TLod>;
implementation
uses
editor_utils;
const
LOD_MAGIC: packed array[1..4] of char = ('L','O','D',#0);
LOD_HEADER_SIZE = 4+4+4+80;
var
GlobalZBuffer: TZBuffer;
GlobalMemBuffer: TMemoryStream;
{ TLod }
constructor TLod.Create(AFullPath: string);
begin
FFileStream := TFileStreamUTF8.Create(AFullPath, fmOpenRead+fmShareDenyWrite);
//FBuffer := TZBuffer.Create;
end;
destructor TLod.Destroy;
begin
//FBuffer.Free;
FFileStream.Free;
inherited Destroy;
end;
procedure TLod.LoadResource(AResource: IResource; constref AItem: TLodItem);
var
zstm: TZlibInputStream;
fname: AnsiString;
begin
//todo: multithreaded LOD decompression
fname:=AItem.Filename;
GlobalMemBuffer.SetSize(AItem.UncompressedFileSize);
zstm := nil;
try
FFileStream.Seek(AItem.FileOffset,soBeginning);
if AItem.FileLength <> 0 then
begin
zstm := TZlibInputStream.Create(GlobalZBuffer, FFileStream, AItem.UncompressedFileSize);
zstm.Read(GlobalMemBuffer.Memory^, AItem.UncompressedFileSize);
end
else begin
FFileStream.Read(GlobalMemBuffer.Memory^, AItem.UncompressedFileSize);
end;
GlobalMemBuffer.Seek(0, soBeginning);
AResource.LoadFromStream(fname, GlobalMemBuffer);
finally
if Assigned(zstm) then
begin
zstm.Free;
end;
end;
end;
procedure TLod.Scan(ACallback: TOnItemFound);
var
nomber_of_files: UInt32;
item: TLodItem;
i: Integer;
magic: packed array[1..4] of char;
begin
if FFileStream.Size < LOD_HEADER_SIZE then
begin
Exit; //too small, may be empty, ignore silently
end;
FFileStream.Seek(0,soBeginning);
FFileStream.Read(magic{%H-},SizeOf(magic));
if magic <> LOD_MAGIC then
raise Exception.Create('Wrong LOD archive');
FFileStream.Seek(8,soBeginning);
nomber_of_files := FFileStream.ReadDWord;
FFileStream.Seek(LOD_HEADER_SIZE,soBeginning);
for i := 0 to nomber_of_files - 1 do
begin
FFileStream.Read(item{%H-},SizeOf(item));
LeToNInPlase(item.FileLength);
LeToNInPlase(item.FileOffset);
LeToNInPlase(item.FileType);
LeToNInPlase(item.UncompressedFileSize);
ACallback(self,item);
end;
end;
initialization
GlobalZBuffer := TZBuffer.Create;
GlobalMemBuffer := TMemoryStream.Create;
finalization
FreeAndNil(GlobalZBuffer);
FreeAndNil(GlobalMemBuffer);
end.