hedgewars/uPhysFSLayer.pas
changeset 9993 76ad55807c24
parent 9988 317d46a2afd2
child 9994 8455993a7a1b
equal deleted inserted replaced
9992:1773ef634b78 9993:76ad55807c24
    29 
    29 
    30 function pfsExists(fname: shortstring): boolean;
    30 function pfsExists(fname: shortstring): boolean;
    31 
    31 
    32 function  physfsReader(L: Plua_State; f: PFSFile; sz: Psize_t) : PChar; cdecl; external PhyslayerLibName;
    32 function  physfsReader(L: Plua_State; f: PFSFile; sz: Psize_t) : PChar; cdecl; external PhyslayerLibName;
    33 procedure physfsReaderSetBuffer(buf: pointer); cdecl; external PhyslayerLibName;
    33 procedure physfsReaderSetBuffer(buf: pointer); cdecl; external PhyslayerLibName;
       
    34 procedure pfsPermitSymbolicLinks(allow: boolean);
    34 procedure hedgewarsMountPackage(filename: PChar); cdecl; external PhyslayerLibName;
    35 procedure hedgewarsMountPackage(filename: PChar); cdecl; external PhyslayerLibName;
    35 
    36 
    36 implementation
    37 implementation
    37 uses uUtils, uVariables, sysutils;
    38 uses uUtils, uVariables, sysutils;
    38 
    39 
    39 function PHYSFS_init(argv0: PChar) : LongInt; cdecl; external PhysfsLibName;
    40 function PHYSFS_init(argv0: PChar) : LongInt; cdecl; external PhysfsLibName;
    40 function PHYSFS_deinit() : LongInt; cdecl; external PhysfsLibName;
    41 function PHYSFS_deinit() : LongInt; cdecl; external PhysfsLibName;
    41 function PHYSFSRWOPS_openRead(fname: PChar): PSDL_RWops; cdecl ; external PhyslayerLibName;
    42 function PHYSFSRWOPS_openRead(fname: PChar): PSDL_RWops; cdecl ; external PhyslayerLibName;
    42 function PHYSFSRWOPS_openWrite(fname: PChar): PSDL_RWops; cdecl; external PhyslayerLibName;
    43 function PHYSFSRWOPS_openWrite(fname: PChar): PSDL_RWops; cdecl; external PhyslayerLibName;
    43 
    44 
    44 function PHYSFS_mount(newDir, mountPoint: PChar; appendToPath: LongBool) : LongInt; cdecl; external PhysfsLibName;
    45 procedure PHYSFS_permitSymbolicLinks(allow: boolean); cdecl; external PhysfsLibName;
       
    46 
       
    47 function PHYSFS_mount(newDir, mountPoint: PChar; appendToPath: LongBool) : LongBool; cdecl; external PhysfsLibName;
    45 function PHYSFS_openRead(fname: PChar): PFSFile; cdecl; external PhysfsLibName;
    48 function PHYSFS_openRead(fname: PChar): PFSFile; cdecl; external PhysfsLibName;
    46 function PHYSFS_eof(f: PFSFile): LongBool; cdecl; external PhysfsLibName;
    49 function PHYSFS_eof(f: PFSFile): LongBool; cdecl; external PhysfsLibName;
    47 function PHYSFS_readBytes(f: PFSFile; buffer: pointer; len: Int64): Int64; cdecl; external PhysfsLibName;
    50 function PHYSFS_readBytes(f: PFSFile; buffer: pointer; len: Int64): Int64; cdecl; external PhysfsLibName;
    48 function PHYSFS_close(f: PFSFile): LongBool; cdecl; external PhysfsLibName;
    51 function PHYSFS_close(f: PFSFile): LongBool; cdecl; external PhysfsLibName;
    49 function PHYSFS_exists(fname: PChar): LongBool; cdecl; external PhysfsLibName;
    52 function PHYSFS_exists(fname: PChar): LongBool; cdecl; external PhysfsLibName;
       
    53 function PHYSFS_getLastError(): PChar; cdecl; external PhysfsLibName;
    50 
    54 
    51 procedure hedgewarsMountPackages(); cdecl; external PhyslayerLibName;
    55 procedure hedgewarsMountPackages(); cdecl; external PhyslayerLibName;
    52 
    56 
    53 function rwopsOpenRead(fname: shortstring): PSDL_RWops;
    57 function rwopsOpenRead(fname: shortstring): PSDL_RWops;
    54 begin
    58 begin
   125         pfsBlockRead:= 0
   129         pfsBlockRead:= 0
   126     else
   130     else
   127         pfsBlockRead:= r
   131         pfsBlockRead:= r
   128 end;
   132 end;
   129 
   133 
       
   134 procedure pfsPermitSymbolicLinks(allow: boolean);
       
   135 begin
       
   136     PHYSFS_permitSymbolicLinks(allow);
       
   137 end;
       
   138 
       
   139 procedure pfsMount(path: AnsiString; mountpoint: PChar);
       
   140 begin
       
   141     if PHYSFS_mount(Str2PChar(path), mountpoint, false) then
       
   142         AddFileLog('[PhysFS] mount ' + path + ': ok')
       
   143     else
       
   144         AddFileLog('[PhysFS] mount ' + path + ': FAILED ("' + PHYSFS_getLastError() + '")');
       
   145 end;
       
   146 
       
   147 procedure pfsMountAtRoot(path: AnsiString);
       
   148 begin
       
   149     pfsMount(path, nil);
       
   150 end;
       
   151 
   130 procedure initModule;
   152 procedure initModule;
   131 var i: LongInt;
   153 var i: LongInt;
   132     cPhysfsId: shortstring;
   154     cPhysfsId: shortstring;
   133 begin
   155 begin
   134 {$IFDEF HWLIBRARY}
   156 {$IFDEF HWLIBRARY}
   139 {$ENDIF}
   161 {$ENDIF}
   140 
   162 
   141     i:= PHYSFS_init(Str2PChar(cPhysfsId));
   163     i:= PHYSFS_init(Str2PChar(cPhysfsId));
   142     AddFileLog('[PhysFS] init: ' + inttostr(i));
   164     AddFileLog('[PhysFS] init: ' + inttostr(i));
   143 
   165 
   144     i:= PHYSFS_mount(Str2PChar(PathPrefix), nil, false);
   166     pfsMountAtRoot(PathPrefix);
   145     AddFileLog('[PhysFS] mount ' + PathPrefix + ': ' + inttostr(i));
   167     pfsMountAtRoot(UserPathPrefix + '/Data');
   146     i:= PHYSFS_mount(Str2PChar(UserPathPrefix + '/Data'), nil, false);
       
   147     AddFileLog('[PhysFS] mount ' + UserPathPrefix + '/Data: ' + inttostr(i));
       
   148 
   168 
   149     hedgewarsMountPackages;
   169     hedgewarsMountPackages;
   150 
   170 
   151     i:= PHYSFS_mount(Str2PChar(UserPathPrefix), nil, false);
       
   152     // need access to teams and frontend configs (for bindings)
   171     // need access to teams and frontend configs (for bindings)
   153     AddFileLog('[PhysFS] mount ' + UserPathPrefix + ': ' + inttostr(i));
   172     pfsMountAtRoot(UserPathPrefix);
   154 
   173 
   155     if cTestLua then
   174     if cTestLua then
   156         begin
   175         begin
   157             i:= PHYSFS_mount(Str2PChar(ExtractFileDir(cScriptName)), nil, false);
   176             pfsMountAtRoot(ExtractFileDir(cScriptName));
   158             AddFileLog('[PhysFS] mount ' + ExtractFileDir(cScriptName) + ': ' + inttostr(i));
       
   159             cScriptName := ExtractFileName(cScriptName);
   177             cScriptName := ExtractFileName(cScriptName);
   160         end;
   178         end;
       
   179 
       
   180     // mounts fonts last - don't allow overwriting them as we allow symlinks there
       
   181     pfsMount(PathPrefix + '/Fonts', '/Fonts');
   161 end;
   182 end;
   162 
   183 
   163 procedure freeModule;
   184 procedure freeModule;
   164 begin
   185 begin
   165     PHYSFS_deinit;
   186     PHYSFS_deinit;