hedgewars/uPhysFSLayer.pas
changeset 8058 bcebfc477459
parent 8052 845b5ae03841
child 8062 abbcdf73327a
equal deleted inserted replaced
8054:39f8ea1a441f 8058:bcebfc477459
       
     1 unit uPhysFSLayer;
       
     2 
       
     3 {$LINKLIB ../bin/libphysfs.a}
       
     4 {$LINKLIB ../bin/libphysfsrwops.a}
       
     5 
       
     6 interface
       
     7 uses SDLh;
       
     8 
       
     9 procedure initModule;
       
    10 procedure freeModule;
       
    11 
       
    12 type PFSFile = pointer;
       
    13 
       
    14 function rwopsOpenRead(fname: shortstring): PSDL_RWops;
       
    15 function rwopsOpenWrite(fname: shortstring): PSDL_RWops;
       
    16 
       
    17 function pfsOpenRead(fname: shortstring): PFSFile;
       
    18 function pfsClose(f: PFSFile): boolean;
       
    19 
       
    20 procedure pfsReadLn(f: PFSFile; var s: shortstring);
       
    21 function pfsBlockRead(f: PFSFile; buf: pointer; size: Int64): Int64;
       
    22 function pfsEOF(f: PFSFile): boolean;
       
    23 
       
    24 function pfsExists(fname: shortstring): boolean;
       
    25 
       
    26 implementation
       
    27 uses uUtils, uVariables;
       
    28 
       
    29 function PHYSFS_init(argv0: PChar) : LongInt; cdecl; external;
       
    30 function PHYSFS_deinit() : LongInt; cdecl; external;
       
    31 function PHYSFSRWOPS_openRead(fname: PChar): PSDL_RWops; cdecl; external;
       
    32 function PHYSFSRWOPS_openWrite(fname: PChar): PSDL_RWops; cdecl; external;
       
    33 
       
    34 function PHYSFS_mount(newDir, mountPoint: PChar; appendToPath: LongBool) : LongInt; cdecl; external;
       
    35 function PHYSFS_openRead(fname: PChar): PFSFile; cdecl; external;
       
    36 function PHYSFS_eof(f: PFSFile): LongBool; cdecl; external;
       
    37 function PHYSFS_readBytes(f: PFSFile; buffer: pointer; len: Int64): Int64; cdecl; external;
       
    38 function PHYSFS_close(f: PFSFile): LongBool; cdecl; external;
       
    39 function PHYSFS_exists(fname: PChar): LongBool; cdecl; external;
       
    40 
       
    41 procedure hedgewarsMountPackages(); cdecl; external;
       
    42 
       
    43 function rwopsOpenRead(fname: shortstring): PSDL_RWops;
       
    44 begin
       
    45     exit(PHYSFSRWOPS_openRead(Str2PChar(fname)));
       
    46 end;
       
    47 
       
    48 function rwopsOpenWrite(fname: shortstring): PSDL_RWops;
       
    49 begin
       
    50     exit(PHYSFSRWOPS_openWrite(Str2PChar(fname)));
       
    51 end;
       
    52 
       
    53 function pfsOpenRead(fname: shortstring): PFSFile;
       
    54 begin
       
    55     exit(PHYSFS_openRead(Str2PChar(fname)));
       
    56 end;
       
    57 
       
    58 function pfsEOF(f: PFSFile): boolean;
       
    59 begin
       
    60     exit(PHYSFS_eof(f))
       
    61 end;
       
    62 
       
    63 function pfsClose(f: PFSFile): boolean;
       
    64 begin
       
    65     exit(PHYSFS_close(f))
       
    66 end;
       
    67 
       
    68 function pfsExists(fname: shortstring): boolean;
       
    69 begin
       
    70     exit(PHYSFS_exists(Str2PChar(fname)))
       
    71 end;
       
    72 
       
    73 
       
    74 procedure pfsReadLn(f: PFSFile; var s: shortstring);
       
    75 var c: char;
       
    76 begin
       
    77 s[0]:= #0;
       
    78 
       
    79 while (PHYSFS_readBytes(f, @c, 1) = 1) and (c <> #10) do
       
    80     if (c <> #13) and (s[0] < #255) then
       
    81         begin
       
    82         inc(s[0]);
       
    83         s[byte(s[0])]:= c
       
    84         end
       
    85 end;
       
    86 
       
    87 function pfsBlockRead(f: PFSFile; buf: pointer; size: Int64): Int64;
       
    88 var r: Int64;
       
    89 begin
       
    90     r:= PHYSFS_readBytes(f, buf, size);
       
    91 
       
    92     if r <= 0 then
       
    93         pfsBlockRead:= 0
       
    94     else
       
    95         pfsBlockRead:= r
       
    96 end;
       
    97 
       
    98 procedure initModule;
       
    99 var i: LongInt;
       
   100 begin
       
   101     i:= PHYSFS_init(Str2PChar(ParamStr(0)));
       
   102     AddFileLog('[PhysFS] init: ' + inttostr(i));
       
   103 
       
   104     i:= PHYSFS_mount(Str2PChar(PathPrefix), nil, true);
       
   105     AddFileLog('[PhysFS] mount ' + PathPrefix + ': ' + inttostr(i));
       
   106     i:= PHYSFS_mount(Str2PChar(UserPathPrefix + '/Data'), nil, true);
       
   107     AddFileLog('[PhysFS] mount ' + UserPathPrefix + '/Data: ' + inttostr(i));
       
   108 
       
   109     hedgewarsMountPackages;
       
   110 end;
       
   111 
       
   112 procedure freeModule;
       
   113 begin
       
   114     PHYSFS_deinit;
       
   115 end;
       
   116 
       
   117 end.