unit IFPReader;
interface uses //...;
type
TIFPHeader = array[0..3] of Char;
type
TIFPReader = class
private
IFP: array of byte;
IFPFile: file of Byte;
IFPFilePos: integer;
function GetIFPName: string;
function GetIFPSize: Integer;
function GetIFPHeader: TIFPHeader;
function GetIFPAnimListNext: string;
function GetIFPAnimCount: Integer;
public
destructor Destroy; override;
function LoadIFP(Path: string): Boolean;
procedure GetIFPAnimList(HList: HWND);
property IFPName: string read GetIFPName;
property IFPSize: Integer read GetIFPSize;
property IFPHeader: TIFPHeader read GetIFPHeader;
property IFPAnimListNext: string read GetIFPAnimListNext;
property IFPAnimCount: Integer read GetIFPAnimCount;
property FilePos: Integer read IFPFilePos write IFPFilePos;
published
end;
var
GLOBALIFP: TIFPReader;
implementation //...
{ TIFPReader }
// frees the memory
destructor TIFPReader.Destroy;
begin
SetLength(IFP, 0);
inherited;
end;
// copies IFP file to the byte array
function TIFPReader.LoadIFP(Path: string): Boolean;
var
aFileSize: Cardinal;
begin
if FileExists(Path) then
begin
AssignFile(IFPFile, Path);
Reset(IFPFile);
aFileSize := FileSize(IFPFile);
try
SetLength(IFP, aFileSize);
BlockRead(IFPFile, IFP[0], aFileSize);
Result := True;
finally
CloseFile(IFPFile);
end;
end else Result := False;
end;
// reads a name of IFP (string beginning from the 20th byte of IFP)
function TIFPReader.GetIFPName: string;
begin
Result := PChar(@IFP[20]);
end;
// returns total size of IFP (without header)
function TIFPReader.GetIFPSize: Integer;
begin
Result := PLongInt(@IFP[4])^;
end;
// returns 'ANPK'
function TIFPReader.GetIFPHeader: TIFPHeader;
begin
Q_MoveMem(@IFP[0], @Result[0], 4);
end;
// reads name of the next animation
function TIFPReader.GetIFPAnimListNext: string;
var
NameLen: integer;
begin
NameLen := PLongInt(@IFP[FilePos])^;
Inc(IFPFilePos, 4);
System.SetString(Result, PChar(@IFP[FilePos]), NameLen);
if NameLen mod 4 = 0 then
Inc(IFPFilePos, NameLen)
else
Inc(IFPFilePos, (NameLen div 4 + 1) * 4);
Inc(IFPFilePos, PLongInt(@IFP[FilePos + 4])^ + 12); // +DGAN
end;
// returns total number of animations in this IFP
function TIFPReader.GetIFPAnimCount: Integer;
begin
Result := PLongInt(@IFP[16])^;
end;
// fills the listbox with animation names
procedure TIFPReader.GetIFPAnimList;
var
I, Count: integer;
begin
FilePos := 32;
Count := IFPAnimCount - 1;
ListBox_ClearItems(HList);
for I := 0 to Count do
begin
ListBox_AddItem(HList, GetIFPAnimListNext);
end;
end;
end.