hedgewars/uPhysFSLayer.pas
changeset 9994 8455993a7a1b
parent 9993 76ad55807c24
child 9996 d93fb04619ee
equal deleted inserted replaced
9993:76ad55807c24 9994:8455993a7a1b
     1 unit uPhysFSLayer;
     1 unit uPhysFSLayer;
     2 
     2 
     3 interface
     3 interface
     4 uses SDLh, LuaPas;
     4 uses SDLh, LuaPas;
       
     5 
       
     6 {$INCLUDE "config.inc"}
     5 
     7 
     6 const PhysfsLibName = {$IFDEF PHYSFS_INTERNAL}'libhwphysfs'{$ELSE}'libphysfs'{$ENDIF};
     8 const PhysfsLibName = {$IFDEF PHYSFS_INTERNAL}'libhwphysfs'{$ELSE}'libphysfs'{$ENDIF};
     7 const PhyslayerLibName = 'libphyslayer';
     9 const PhyslayerLibName = 'libphyslayer';
     8 
    10 
     9 {$IFNDEF WIN32}
    11 {$IFNDEF WIN32}
    29 
    31 
    30 function pfsExists(fname: shortstring): boolean;
    32 function pfsExists(fname: shortstring): boolean;
    31 
    33 
    32 function  physfsReader(L: Plua_State; f: PFSFile; sz: Psize_t) : PChar; cdecl; external PhyslayerLibName;
    34 function  physfsReader(L: Plua_State; f: PFSFile; sz: Psize_t) : PChar; cdecl; external PhyslayerLibName;
    33 procedure physfsReaderSetBuffer(buf: pointer); cdecl; external PhyslayerLibName;
    35 procedure physfsReaderSetBuffer(buf: pointer); cdecl; external PhyslayerLibName;
    34 procedure pfsPermitSymbolicLinks(allow: boolean);
       
    35 procedure hedgewarsMountPackage(filename: PChar); cdecl; external PhyslayerLibName;
    36 procedure hedgewarsMountPackage(filename: PChar); cdecl; external PhyslayerLibName;
    36 
    37 
    37 implementation
    38 implementation
    38 uses uUtils, uVariables, sysutils;
    39 uses uUtils, uVariables, sysutils;
    39 
    40 
    40 function PHYSFS_init(argv0: PChar) : LongInt; cdecl; external PhysfsLibName;
    41 function PHYSFS_init(argv0: PChar) : LongInt; cdecl; external PhysfsLibName;
    41 function PHYSFS_deinit() : LongInt; cdecl; external PhysfsLibName;
    42 function PHYSFS_deinit() : LongInt; cdecl; external PhysfsLibName;
    42 function PHYSFSRWOPS_openRead(fname: PChar): PSDL_RWops; cdecl ; external PhyslayerLibName;
    43 function PHYSFSRWOPS_openRead(fname: PChar): PSDL_RWops; cdecl ; external PhyslayerLibName;
    43 function PHYSFSRWOPS_openWrite(fname: PChar): PSDL_RWops; cdecl; external PhyslayerLibName;
    44 function PHYSFSRWOPS_openWrite(fname: PChar): PSDL_RWops; cdecl; external PhyslayerLibName;
    44 
       
    45 procedure PHYSFS_permitSymbolicLinks(allow: boolean); cdecl; external PhysfsLibName;
       
    46 
    45 
    47 function PHYSFS_mount(newDir, mountPoint: PChar; appendToPath: LongBool) : LongBool; cdecl; external PhysfsLibName;
    46 function PHYSFS_mount(newDir, mountPoint: PChar; appendToPath: LongBool) : LongBool; cdecl; external PhysfsLibName;
    48 function PHYSFS_openRead(fname: PChar): PFSFile; cdecl; external PhysfsLibName;
    47 function PHYSFS_openRead(fname: PChar): PFSFile; cdecl; external PhysfsLibName;
    49 function PHYSFS_eof(f: PFSFile): LongBool; cdecl; external PhysfsLibName;
    48 function PHYSFS_eof(f: PFSFile): LongBool; cdecl; external PhysfsLibName;
    50 function PHYSFS_readBytes(f: PFSFile; buffer: pointer; len: Int64): Int64; cdecl; external PhysfsLibName;
    49 function PHYSFS_readBytes(f: PFSFile; buffer: pointer; len: Int64): Int64; cdecl; external PhysfsLibName;
   129         pfsBlockRead:= 0
   128         pfsBlockRead:= 0
   130     else
   129     else
   131         pfsBlockRead:= r
   130         pfsBlockRead:= r
   132 end;
   131 end;
   133 
   132 
   134 procedure pfsPermitSymbolicLinks(allow: boolean);
       
   135 begin
       
   136     PHYSFS_permitSymbolicLinks(allow);
       
   137 end;
       
   138 
       
   139 procedure pfsMount(path: AnsiString; mountpoint: PChar);
   133 procedure pfsMount(path: AnsiString; mountpoint: PChar);
   140 begin
   134 begin
   141     if PHYSFS_mount(Str2PChar(path), mountpoint, false) then
   135     if PHYSFS_mount(Str2PChar(path), mountpoint, false) then
   142         AddFileLog('[PhysFS] mount ' + path + ': ok')
   136         AddFileLog('[PhysFS] mount ' + path + ' at ' + mountpoint + ' : ok')
   143     else
   137     else
   144         AddFileLog('[PhysFS] mount ' + path + ': FAILED ("' + PHYSFS_getLastError() + '")');
   138         AddFileLog('[PhysFS] mount ' + path + ' at ' + mountpoint + ' : FAILED ("' + PHYSFS_getLastError() + '")');
   145 end;
   139 end;
   146 
   140 
   147 procedure pfsMountAtRoot(path: AnsiString);
   141 procedure pfsMountAtRoot(path: AnsiString);
   148 begin
   142 begin
   149     pfsMount(path, nil);
   143     pfsMount(path, '/');
   150 end;
   144 end;
   151 
   145 
   152 procedure initModule;
   146 procedure initModule;
   153 var i: LongInt;
   147 var i: LongInt;
   154     cPhysfsId: shortstring;
   148     cPhysfsId: shortstring;
       
   149     fp: AnsiString;
   155 begin
   150 begin
   156 {$IFDEF HWLIBRARY}
   151 {$IFDEF HWLIBRARY}
   157     //TODO: http://icculus.org/pipermail/physfs/2011-August/001006.html
   152     //TODO: http://icculus.org/pipermail/physfs/2011-August/001006.html
   158     cPhysfsId:= GetCurrentDir() + {$IFDEF DARWIN}{$IFNDEF IPHONEOS}'/Hedgewars.app/Contents/MacOS/' + {$ENDIF}{$ENDIF} ' hedgewars';
   153     cPhysfsId:= GetCurrentDir() + {$IFDEF DARWIN}{$IFNDEF IPHONEOS}'/Hedgewars.app/Contents/MacOS/' + {$ENDIF}{$ENDIF} ' hedgewars';
   159 {$ELSE}
   154 {$ELSE}
   161 {$ENDIF}
   156 {$ENDIF}
   162 
   157 
   163     i:= PHYSFS_init(Str2PChar(cPhysfsId));
   158     i:= PHYSFS_init(Str2PChar(cPhysfsId));
   164     AddFileLog('[PhysFS] init: ' + inttostr(i));
   159     AddFileLog('[PhysFS] init: ' + inttostr(i));
   165 
   160 
       
   161     // mount system fonts paths first
       
   162     fp := cFontsPaths;
       
   163 
       
   164     // let's remove paths from fp and mount them until nothing is left
       
   165     while length(fp) > 0 do
       
   166         begin
       
   167         AddFileLog('lol');
       
   168         // search for ;
       
   169         i := pos(';', fp);
       
   170         // if there is no ; left, read to end
       
   171         if i < 1 then
       
   172             i := length(fp) + 1;
       
   173         // don't mount empty path
       
   174         if i > 2 then
       
   175             pfsMount(copy(fp, 1, i-1), '/Fonts');
       
   176         // remove mounted path from fp
       
   177         fp := copy(fp, i+1, length(fp)-i);
       
   178         end;
       
   179 
   166     pfsMountAtRoot(PathPrefix);
   180     pfsMountAtRoot(PathPrefix);
   167     pfsMountAtRoot(UserPathPrefix + '/Data');
   181     pfsMountAtRoot(UserPathPrefix + '/Data');
   168 
   182 
   169     hedgewarsMountPackages;
   183     hedgewarsMountPackages;
   170 
   184 
   174     if cTestLua then
   188     if cTestLua then
   175         begin
   189         begin
   176             pfsMountAtRoot(ExtractFileDir(cScriptName));
   190             pfsMountAtRoot(ExtractFileDir(cScriptName));
   177             cScriptName := ExtractFileName(cScriptName);
   191             cScriptName := ExtractFileName(cScriptName);
   178         end;
   192         end;
   179 
       
   180     // mounts fonts last - don't allow overwriting them as we allow symlinks there
       
   181     pfsMount(PathPrefix + '/Fonts', '/Fonts');
       
   182 end;
   193 end;
   183 
   194 
   184 procedure freeModule;
   195 procedure freeModule;
   185 begin
   196 begin
   186     PHYSFS_deinit;
   197     PHYSFS_deinit;