root / Client / UResourceManager.pas @ 119:66352054ce4d
History | View | Annotate | Download (2.6 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 2009 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
|
| 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 | FreeAndNil(FFileStream); |
| 73 | 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 | FResourceStream.Free; |
| 85 | FResourceStream := TMemoryStream.Create; |
| 86 | FFileStream.Read(size, SizeOf(Cardinal)); |
| 87 | FResourceStream.CopyFrom(FFileStream, size); |
| 88 | FCurrentResource := AIndex; |
| 89 | end;
|
| 90 | FResourceStream.Position := 0;
|
| 91 | Result := FResourceStream; |
| 92 | end;
|
| 93 | |
| 94 | initialization
|
| 95 | begin
|
| 96 | ResourceManager := TResourceManager.Create(ChangeFileExt(ParamStr(0), '.dat')); |
| 97 | end;
|
| 98 | |
| 99 | finalization
|
| 100 | begin
|
| 101 | if ResourceManager <> nil then FreeAndNil(ResourceManager); |
| 102 | end;
|
| 103 | |
| 104 | end.
|
| 105 |