Statistics
| Branch: | Tag: | Revision:

root / Client / UResourceManager.pas @ 0:95bd93c28625

History | View | Annotate | Download (2.7 kB)

1
(*
2
 * CDDL HEADER START
3
 *
4
 * The contents of this file are subject to the terms of the
5
 * Common Development and Distribution License, Version 1.0 only
6
 * (the "License").  You may not use this file except in compliance
7
 * with the License.
8
 *
9
 * You can obtain a copy of the license at
10
 * http://www.opensource.org/licenses/cddl1.php.
11
 * See the License for the specific language governing permissions
12
 * and limitations under the License.
13
 *
14
 * When distributing Covered Code, include this CDDL HEADER in each
15
 * file and include the License file at
16
 * http://www.opensource.org/licenses/cddl1.php.  If applicable,
17
 * add the following below this CDDL HEADER, with the fields enclosed
18
 * by brackets "[]" replaced with your own identifying * information:
19
 *      Portions Copyright [yyyy] [name of copyright owner]
20
 *
21
 * CDDL HEADER END
22
 *
23
 *
24
 *      Portions Copyright 2007 Andreas Schneider
25
 *)
26
unit UResourceManager;
27
28
{$mode objfpc}{$H+}
29
30
interface
31
32
uses
33
  Classes, SysUtils;
34
  
35
type
36
37
  { TResourceManager }
38
39
  TResourceManager = class(TObject)
40
    constructor Create(AFileName: string);
41
    destructor Destroy; override;
42
  protected
43
    FFileStream: TFileStream;
44
    FCount: Integer;
45
    FLookupTable: array of Cardinal;
46
    FCurrentResource: Integer;
47
    FResourceStream: TMemoryStream;
48
  public
49
    function GetResource(AIndex: Integer): TStream;
50
  end;
51
  
52
var
53
  ResourceManager: TResourceManager;
54
55
implementation
56
57
{ TResourceManager }
58
59
constructor TResourceManager.Create(AFileName: string);
60
begin
61
  inherited Create;
62
  FFileStream := TFileStream.Create(AFileName, fmOpenRead or fmShareDenyWrite);
63
  FFileStream.Position := 0;
64
  FFileStream.Read(FCount, SizeOf(Integer));
65
  SetLength(FLookupTable, FCount);
66
  FFileStream.Read(FLookupTable[0], FCount * SizeOf(Cardinal));
67
  FCurrentResource := -1;
68
end;
69
70
destructor TResourceManager.Destroy;
71
begin
72
  if FFileStream <> nil then FreeAndNil(FFileStream);
73
  if FResourceStream <> nil then FreeAndNil(FResourceStream);
74
  inherited Destroy;
75
end;
76
77
function TResourceManager.GetResource(AIndex: Integer): TStream;
78
var
79
  size: Cardinal;
80
begin
81
  if AIndex <> FCurrentResource then
82
  begin
83
    FFileStream.Position := FLookupTable[AIndex];
84
    if FResourceStream <> nil then
85
      FResourceStream.Free;
86
    FResourceStream := TMemoryStream.Create;
87
    FFileStream.Read(size, SizeOf(Cardinal));
88
    FResourceStream.CopyFrom(FFileStream, size);
89
    FCurrentResource := AIndex;
90
  end;
91
  FResourceStream.Position := 0;
92
  Result := FResourceStream;
93
end;
94
95
initialization
96
begin
97
  ResourceManager := TResourceManager.Create(ChangeFileExt(ParamStr(0), '.dat'));
98
end;
99
100
finalization
101
begin
102
  if ResourceManager <> nil then FreeAndNil(ResourceManager);
103
end;
104
105
end.
106