hedgewars/uPhysFSLayer.pas
author koda
Wed, 12 Jun 2013 01:00:47 +0200
changeset 9202 4d69569baabb
parent 9111 f529fba57ba5
child 9204 1c233176fffd
permissions -rw-r--r--
also physlayer can be comiled as static or shared now
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7959
644b757d20e6 Start work on physfs support in engine
unc0rr
parents:
diff changeset
     1
unit uPhysFSLayer;
8063
06efc1ea6a40 linking phsyfs on osx
koda
parents: 8062
diff changeset
     2
7959
644b757d20e6 Start work on physfs support in engine
unc0rr
parents:
diff changeset
     3
interface
8077
2ea5cde93abc Fix build
unc0rr
parents: 8073
diff changeset
     4
uses SDLh, LuaPas;
7959
644b757d20e6 Start work on physfs support in engine
unc0rr
parents:
diff changeset
     5
8558
e96bf10216ef on windows fpc-linking is the funniest of all
koda
parents: 8540
diff changeset
     6
const PhysfsLibName = {$IFDEF PHYSFS_INTERNAL}'libhw_physfs'{$ELSE}'libphysfs'{$ENDIF};
8540
cf808329bb6f this should hijack the linker name and always pick the bundled physfs when another version is already prsent
koda
parents: 8533
diff changeset
     7
const PhyslayerLibName = 'libphyslayer';
8073
5a289ef40fdb physfs compilation on windows
koda
parents: 8067
diff changeset
     8
8540
cf808329bb6f this should hijack the linker name and always pick the bundled physfs when another version is already prsent
koda
parents: 8533
diff changeset
     9
{$IFNDEF WIN32}
cf808329bb6f this should hijack the linker name and always pick the bundled physfs when another version is already prsent
koda
parents: 8533
diff changeset
    10
    {$linklib physfs}
cf808329bb6f this should hijack the linker name and always pick the bundled physfs when another version is already prsent
koda
parents: 8533
diff changeset
    11
    {$linklib physlayer}
8533
2d7703d6bc22 this should make physfs happy to link on windows too, forcing every function in its proper dll and skipping linklib
koda
parents: 8528
diff changeset
    12
{$ENDIF}
2d7703d6bc22 this should make physfs happy to link on windows too, forcing every function in its proper dll and skipping linklib
koda
parents: 8528
diff changeset
    13
7959
644b757d20e6 Start work on physfs support in engine
unc0rr
parents:
diff changeset
    14
procedure initModule;
644b757d20e6 Start work on physfs support in engine
unc0rr
parents:
diff changeset
    15
procedure freeModule;
644b757d20e6 Start work on physfs support in engine
unc0rr
parents:
diff changeset
    16
8028
dc30104660d3 Engine loads fine with basic config
unc0rr
parents: 8025
diff changeset
    17
type PFSFile = pointer;
dc30104660d3 Engine loads fine with basic config
unc0rr
parents: 8025
diff changeset
    18
8025
07862ab415c8 Get rid of Pathz and UserPathz
unc0rr
parents: 8022
diff changeset
    19
function rwopsOpenRead(fname: shortstring): PSDL_RWops;
07862ab415c8 Get rid of Pathz and UserPathz
unc0rr
parents: 8022
diff changeset
    20
function rwopsOpenWrite(fname: shortstring): PSDL_RWops;
8022
10b3b93c1f56 - Add physfsrwops library
unc0rr
parents: 7963
diff changeset
    21
8028
dc30104660d3 Engine loads fine with basic config
unc0rr
parents: 8025
diff changeset
    22
function pfsOpenRead(fname: shortstring): PFSFile;
dc30104660d3 Engine loads fine with basic config
unc0rr
parents: 8025
diff changeset
    23
function pfsClose(f: PFSFile): boolean;
dc30104660d3 Engine loads fine with basic config
unc0rr
parents: 8025
diff changeset
    24
dc30104660d3 Engine loads fine with basic config
unc0rr
parents: 8025
diff changeset
    25
procedure pfsReadLn(f: PFSFile; var s: shortstring);
8107
ee21b816394f Bring ansistrings back
unc0rr
parents: 8080
diff changeset
    26
procedure pfsReadLnA(f: PFSFile; var s: ansistring);
8031
fc40b343c45c Script loading via physfs which doesn't work:
unc0rr
parents: 8028
diff changeset
    27
function pfsBlockRead(f: PFSFile; buf: pointer; size: Int64): Int64;
fc40b343c45c Script loading via physfs which doesn't work:
unc0rr
parents: 8028
diff changeset
    28
function pfsEOF(f: PFSFile): boolean;
fc40b343c45c Script loading via physfs which doesn't work:
unc0rr
parents: 8028
diff changeset
    29
fc40b343c45c Script loading via physfs which doesn't work:
unc0rr
parents: 8028
diff changeset
    30
function pfsExists(fname: shortstring): boolean;
8028
dc30104660d3 Engine loads fine with basic config
unc0rr
parents: 8025
diff changeset
    31
8533
2d7703d6bc22 this should make physfs happy to link on windows too, forcing every function in its proper dll and skipping linklib
koda
parents: 8528
diff changeset
    32
function  physfsReader(L: Plua_State; f: PFSFile; sz: Psize_t) : PChar; cdecl; external PhyslayerLibName;
2d7703d6bc22 this should make physfs happy to link on windows too, forcing every function in its proper dll and skipping linklib
koda
parents: 8528
diff changeset
    33
procedure physfsReaderSetBuffer(buf: pointer); cdecl; external PhyslayerLibName;
8978
e6ef8fe314bd suggestion of unc0rr's to fix issue w/ random maps in campaign. load sidecar packages in physfs for lua. should be useful also for lua that does custom layouts
nemo
parents: 8927
diff changeset
    34
procedure hedgewarsMountPackage(filename: PChar); cdecl; external PhyslayerLibName;
8077
2ea5cde93abc Fix build
unc0rr
parents: 8073
diff changeset
    35
7959
644b757d20e6 Start work on physfs support in engine
unc0rr
parents:
diff changeset
    36
implementation
8310
a98c349bc06b minor adjustments to libengine, moc is correctly created as definitions are set before calling it, params are better numbered and we don't subclass qthread but rather use moveToThread()
koda
parents: 8283
diff changeset
    37
uses uUtils, uVariables, sysutils;
7959
644b757d20e6 Start work on physfs support in engine
unc0rr
parents:
diff changeset
    38
8073
5a289ef40fdb physfs compilation on windows
koda
parents: 8067
diff changeset
    39
function PHYSFS_init(argv0: PChar) : LongInt; cdecl; external PhysfsLibName;
5a289ef40fdb physfs compilation on windows
koda
parents: 8067
diff changeset
    40
function PHYSFS_deinit() : LongInt; cdecl; external PhysfsLibName;
8533
2d7703d6bc22 this should make physfs happy to link on windows too, forcing every function in its proper dll and skipping linklib
koda
parents: 8528
diff changeset
    41
function PHYSFSRWOPS_openRead(fname: PChar): PSDL_RWops; cdecl ; external PhyslayerLibName;
2d7703d6bc22 this should make physfs happy to link on windows too, forcing every function in its proper dll and skipping linklib
koda
parents: 8528
diff changeset
    42
function PHYSFSRWOPS_openWrite(fname: PChar): PSDL_RWops; cdecl; external PhyslayerLibName;
7959
644b757d20e6 Start work on physfs support in engine
unc0rr
parents:
diff changeset
    43
8073
5a289ef40fdb physfs compilation on windows
koda
parents: 8067
diff changeset
    44
function PHYSFS_mount(newDir, mountPoint: PChar; appendToPath: LongBool) : LongInt; cdecl; external PhysfsLibName;
5a289ef40fdb physfs compilation on windows
koda
parents: 8067
diff changeset
    45
function PHYSFS_openRead(fname: PChar): PFSFile; cdecl; external PhysfsLibName;
5a289ef40fdb physfs compilation on windows
koda
parents: 8067
diff changeset
    46
function PHYSFS_eof(f: PFSFile): LongBool; cdecl; external PhysfsLibName;
5a289ef40fdb physfs compilation on windows
koda
parents: 8067
diff changeset
    47
function PHYSFS_readBytes(f: PFSFile; buffer: pointer; len: Int64): Int64; cdecl; external PhysfsLibName;
5a289ef40fdb physfs compilation on windows
koda
parents: 8067
diff changeset
    48
function PHYSFS_close(f: PFSFile): LongBool; cdecl; external PhysfsLibName;
5a289ef40fdb physfs compilation on windows
koda
parents: 8067
diff changeset
    49
function PHYSFS_exists(fname: PChar): LongBool; cdecl; external PhysfsLibName;
7959
644b757d20e6 Start work on physfs support in engine
unc0rr
parents:
diff changeset
    50
8533
2d7703d6bc22 this should make physfs happy to link on windows too, forcing every function in its proper dll and skipping linklib
koda
parents: 8528
diff changeset
    51
procedure hedgewarsMountPackages(); cdecl; external PhyslayerLibName;
8052
845b5ae03841 Mount .hwt files found in Data folder
unc0rr
parents: 8046
diff changeset
    52
8025
07862ab415c8 Get rid of Pathz and UserPathz
unc0rr
parents: 8022
diff changeset
    53
function rwopsOpenRead(fname: shortstring): PSDL_RWops;
07862ab415c8 Get rid of Pathz and UserPathz
unc0rr
parents: 8022
diff changeset
    54
begin
07862ab415c8 Get rid of Pathz and UserPathz
unc0rr
parents: 8022
diff changeset
    55
    exit(PHYSFSRWOPS_openRead(Str2PChar(fname)));
07862ab415c8 Get rid of Pathz and UserPathz
unc0rr
parents: 8022
diff changeset
    56
end;
07862ab415c8 Get rid of Pathz and UserPathz
unc0rr
parents: 8022
diff changeset
    57
07862ab415c8 Get rid of Pathz and UserPathz
unc0rr
parents: 8022
diff changeset
    58
function rwopsOpenWrite(fname: shortstring): PSDL_RWops;
7959
644b757d20e6 Start work on physfs support in engine
unc0rr
parents:
diff changeset
    59
begin
8025
07862ab415c8 Get rid of Pathz and UserPathz
unc0rr
parents: 8022
diff changeset
    60
    exit(PHYSFSRWOPS_openWrite(Str2PChar(fname)));
07862ab415c8 Get rid of Pathz and UserPathz
unc0rr
parents: 8022
diff changeset
    61
end;
8022
10b3b93c1f56 - Add physfsrwops library
unc0rr
parents: 7963
diff changeset
    62
8028
dc30104660d3 Engine loads fine with basic config
unc0rr
parents: 8025
diff changeset
    63
function pfsOpenRead(fname: shortstring): PFSFile;
dc30104660d3 Engine loads fine with basic config
unc0rr
parents: 8025
diff changeset
    64
begin
dc30104660d3 Engine loads fine with basic config
unc0rr
parents: 8025
diff changeset
    65
    exit(PHYSFS_openRead(Str2PChar(fname)));
dc30104660d3 Engine loads fine with basic config
unc0rr
parents: 8025
diff changeset
    66
end;
dc30104660d3 Engine loads fine with basic config
unc0rr
parents: 8025
diff changeset
    67
dc30104660d3 Engine loads fine with basic config
unc0rr
parents: 8025
diff changeset
    68
function pfsEOF(f: PFSFile): boolean;
dc30104660d3 Engine loads fine with basic config
unc0rr
parents: 8025
diff changeset
    69
begin
dc30104660d3 Engine loads fine with basic config
unc0rr
parents: 8025
diff changeset
    70
    exit(PHYSFS_eof(f))
dc30104660d3 Engine loads fine with basic config
unc0rr
parents: 8025
diff changeset
    71
end;
dc30104660d3 Engine loads fine with basic config
unc0rr
parents: 8025
diff changeset
    72
dc30104660d3 Engine loads fine with basic config
unc0rr
parents: 8025
diff changeset
    73
function pfsClose(f: PFSFile): boolean;
dc30104660d3 Engine loads fine with basic config
unc0rr
parents: 8025
diff changeset
    74
begin
dc30104660d3 Engine loads fine with basic config
unc0rr
parents: 8025
diff changeset
    75
    exit(PHYSFS_close(f))
dc30104660d3 Engine loads fine with basic config
unc0rr
parents: 8025
diff changeset
    76
end;
dc30104660d3 Engine loads fine with basic config
unc0rr
parents: 8025
diff changeset
    77
8031
fc40b343c45c Script loading via physfs which doesn't work:
unc0rr
parents: 8028
diff changeset
    78
function pfsExists(fname: shortstring): boolean;
fc40b343c45c Script loading via physfs which doesn't work:
unc0rr
parents: 8028
diff changeset
    79
begin
fc40b343c45c Script loading via physfs which doesn't work:
unc0rr
parents: 8028
diff changeset
    80
    exit(PHYSFS_exists(Str2PChar(fname)))
fc40b343c45c Script loading via physfs which doesn't work:
unc0rr
parents: 8028
diff changeset
    81
end;
fc40b343c45c Script loading via physfs which doesn't work:
unc0rr
parents: 8028
diff changeset
    82
8028
dc30104660d3 Engine loads fine with basic config
unc0rr
parents: 8025
diff changeset
    83
dc30104660d3 Engine loads fine with basic config
unc0rr
parents: 8025
diff changeset
    84
procedure pfsReadLn(f: PFSFile; var s: shortstring);
dc30104660d3 Engine loads fine with basic config
unc0rr
parents: 8025
diff changeset
    85
var c: char;
dc30104660d3 Engine loads fine with basic config
unc0rr
parents: 8025
diff changeset
    86
begin
dc30104660d3 Engine loads fine with basic config
unc0rr
parents: 8025
diff changeset
    87
s[0]:= #0;
dc30104660d3 Engine loads fine with basic config
unc0rr
parents: 8025
diff changeset
    88
8034
fc032c0f7b23 Implement reader in C ffs
unc0rr
parents: 8031
diff changeset
    89
while (PHYSFS_readBytes(f, @c, 1) = 1) and (c <> #10) do
8028
dc30104660d3 Engine loads fine with basic config
unc0rr
parents: 8025
diff changeset
    90
    if (c <> #13) and (s[0] < #255) then
dc30104660d3 Engine loads fine with basic config
unc0rr
parents: 8025
diff changeset
    91
        begin
dc30104660d3 Engine loads fine with basic config
unc0rr
parents: 8025
diff changeset
    92
        inc(s[0]);
dc30104660d3 Engine loads fine with basic config
unc0rr
parents: 8025
diff changeset
    93
        s[byte(s[0])]:= c
dc30104660d3 Engine loads fine with basic config
unc0rr
parents: 8025
diff changeset
    94
        end
dc30104660d3 Engine loads fine with basic config
unc0rr
parents: 8025
diff changeset
    95
end;
dc30104660d3 Engine loads fine with basic config
unc0rr
parents: 8025
diff changeset
    96
8107
ee21b816394f Bring ansistrings back
unc0rr
parents: 8080
diff changeset
    97
procedure pfsReadLnA(f: PFSFile; var s: ansistring);
ee21b816394f Bring ansistrings back
unc0rr
parents: 8080
diff changeset
    98
var c: char;
ee21b816394f Bring ansistrings back
unc0rr
parents: 8080
diff changeset
    99
    b: shortstring;
ee21b816394f Bring ansistrings back
unc0rr
parents: 8080
diff changeset
   100
begin
ee21b816394f Bring ansistrings back
unc0rr
parents: 8080
diff changeset
   101
s:= '';
ee21b816394f Bring ansistrings back
unc0rr
parents: 8080
diff changeset
   102
b[0]:= #0;
ee21b816394f Bring ansistrings back
unc0rr
parents: 8080
diff changeset
   103
ee21b816394f Bring ansistrings back
unc0rr
parents: 8080
diff changeset
   104
while (PHYSFS_readBytes(f, @c, 1) = 1) and (c <> #10) do
ee21b816394f Bring ansistrings back
unc0rr
parents: 8080
diff changeset
   105
    if (c <> #13) then
ee21b816394f Bring ansistrings back
unc0rr
parents: 8080
diff changeset
   106
        begin
ee21b816394f Bring ansistrings back
unc0rr
parents: 8080
diff changeset
   107
        inc(b[0]);
ee21b816394f Bring ansistrings back
unc0rr
parents: 8080
diff changeset
   108
        b[byte(b[0])]:= c;
ee21b816394f Bring ansistrings back
unc0rr
parents: 8080
diff changeset
   109
        if b[0] = #255 then
ee21b816394f Bring ansistrings back
unc0rr
parents: 8080
diff changeset
   110
            begin
ee21b816394f Bring ansistrings back
unc0rr
parents: 8080
diff changeset
   111
            s:= s + b;
ee21b816394f Bring ansistrings back
unc0rr
parents: 8080
diff changeset
   112
            b[0]:= #0
ee21b816394f Bring ansistrings back
unc0rr
parents: 8080
diff changeset
   113
            end
ee21b816394f Bring ansistrings back
unc0rr
parents: 8080
diff changeset
   114
        end;
ee21b816394f Bring ansistrings back
unc0rr
parents: 8080
diff changeset
   115
        
ee21b816394f Bring ansistrings back
unc0rr
parents: 8080
diff changeset
   116
s:= s + b
ee21b816394f Bring ansistrings back
unc0rr
parents: 8080
diff changeset
   117
end;
ee21b816394f Bring ansistrings back
unc0rr
parents: 8080
diff changeset
   118
8031
fc40b343c45c Script loading via physfs which doesn't work:
unc0rr
parents: 8028
diff changeset
   119
function pfsBlockRead(f: PFSFile; buf: pointer; size: Int64): Int64;
fc40b343c45c Script loading via physfs which doesn't work:
unc0rr
parents: 8028
diff changeset
   120
var r: Int64;
fc40b343c45c Script loading via physfs which doesn't work:
unc0rr
parents: 8028
diff changeset
   121
begin
8034
fc032c0f7b23 Implement reader in C ffs
unc0rr
parents: 8031
diff changeset
   122
    r:= PHYSFS_readBytes(f, buf, size);
8031
fc40b343c45c Script loading via physfs which doesn't work:
unc0rr
parents: 8028
diff changeset
   123
fc40b343c45c Script loading via physfs which doesn't work:
unc0rr
parents: 8028
diff changeset
   124
    if r <= 0 then
fc40b343c45c Script loading via physfs which doesn't work:
unc0rr
parents: 8028
diff changeset
   125
        pfsBlockRead:= 0
fc40b343c45c Script loading via physfs which doesn't work:
unc0rr
parents: 8028
diff changeset
   126
    else
fc40b343c45c Script loading via physfs which doesn't work:
unc0rr
parents: 8028
diff changeset
   127
        pfsBlockRead:= r
fc40b343c45c Script loading via physfs which doesn't work:
unc0rr
parents: 8028
diff changeset
   128
end;
fc40b343c45c Script loading via physfs which doesn't work:
unc0rr
parents: 8028
diff changeset
   129
8025
07862ab415c8 Get rid of Pathz and UserPathz
unc0rr
parents: 8022
diff changeset
   130
procedure initModule;
07862ab415c8 Get rid of Pathz and UserPathz
unc0rr
parents: 8022
diff changeset
   131
var i: LongInt;
8310
a98c349bc06b minor adjustments to libengine, moc is correctly created as definitions are set before calling it, params are better numbered and we don't subclass qthread but rather use moveToThread()
koda
parents: 8283
diff changeset
   132
    cPhysfsId: shortstring;
8025
07862ab415c8 Get rid of Pathz and UserPathz
unc0rr
parents: 8022
diff changeset
   133
begin
8310
a98c349bc06b minor adjustments to libengine, moc is correctly created as definitions are set before calling it, params are better numbered and we don't subclass qthread but rather use moveToThread()
koda
parents: 8283
diff changeset
   134
{$IFDEF HWLIBRARY}
a98c349bc06b minor adjustments to libengine, moc is correctly created as definitions are set before calling it, params are better numbered and we don't subclass qthread but rather use moveToThread()
koda
parents: 8283
diff changeset
   135
    //TODO: http://icculus.org/pipermail/physfs/2011-August/001006.html
a98c349bc06b minor adjustments to libengine, moc is correctly created as definitions are set before calling it, params are better numbered and we don't subclass qthread but rather use moveToThread()
koda
parents: 8283
diff changeset
   136
    cPhysfsId:= GetCurrentDir() + {$IFDEF DARWIN}'/Hedgewars.app/Contents/MacOS/' + {$ENDIF} ' hedgewars';
a98c349bc06b minor adjustments to libengine, moc is correctly created as definitions are set before calling it, params are better numbered and we don't subclass qthread but rather use moveToThread()
koda
parents: 8283
diff changeset
   137
{$ELSE}
a98c349bc06b minor adjustments to libengine, moc is correctly created as definitions are set before calling it, params are better numbered and we don't subclass qthread but rather use moveToThread()
koda
parents: 8283
diff changeset
   138
    cPhysfsId:= ParamStr(0);
a98c349bc06b minor adjustments to libengine, moc is correctly created as definitions are set before calling it, params are better numbered and we don't subclass qthread but rather use moveToThread()
koda
parents: 8283
diff changeset
   139
{$ENDIF}
a98c349bc06b minor adjustments to libengine, moc is correctly created as definitions are set before calling it, params are better numbered and we don't subclass qthread but rather use moveToThread()
koda
parents: 8283
diff changeset
   140
a98c349bc06b minor adjustments to libengine, moc is correctly created as definitions are set before calling it, params are better numbered and we don't subclass qthread but rather use moveToThread()
koda
parents: 8283
diff changeset
   141
    i:= PHYSFS_init(Str2PChar(cPhysfsId));
8025
07862ab415c8 Get rid of Pathz and UserPathz
unc0rr
parents: 8022
diff changeset
   142
    AddFileLog('[PhysFS] init: ' + inttostr(i));
07862ab415c8 Get rid of Pathz and UserPathz
unc0rr
parents: 8022
diff changeset
   143
8714
ab201a62d115 Prepend, not append
unc0rr
parents: 8558
diff changeset
   144
    i:= PHYSFS_mount(Str2PChar(PathPrefix), nil, false);
8025
07862ab415c8 Get rid of Pathz and UserPathz
unc0rr
parents: 8022
diff changeset
   145
    AddFileLog('[PhysFS] mount ' + PathPrefix + ': ' + inttostr(i));
8714
ab201a62d115 Prepend, not append
unc0rr
parents: 8558
diff changeset
   146
    i:= PHYSFS_mount(Str2PChar(UserPathPrefix + '/Data'), nil, false);
8046
4d3415927d2c Use (undocumented) Mix_LoadMUS_RW to load music
unc0rr
parents: 8034
diff changeset
   147
    AddFileLog('[PhysFS] mount ' + UserPathPrefix + '/Data: ' + inttostr(i));
8052
845b5ae03841 Mount .hwt files found in Data folder
unc0rr
parents: 8046
diff changeset
   148
845b5ae03841 Mount .hwt files found in Data folder
unc0rr
parents: 8046
diff changeset
   149
    hedgewarsMountPackages;
7959
644b757d20e6 Start work on physfs support in engine
unc0rr
parents:
diff changeset
   150
end;
644b757d20e6 Start work on physfs support in engine
unc0rr
parents:
diff changeset
   151
644b757d20e6 Start work on physfs support in engine
unc0rr
parents:
diff changeset
   152
procedure freeModule;
644b757d20e6 Start work on physfs support in engine
unc0rr
parents:
diff changeset
   153
begin
644b757d20e6 Start work on physfs support in engine
unc0rr
parents:
diff changeset
   154
    PHYSFS_deinit;
644b757d20e6 Start work on physfs support in engine
unc0rr
parents:
diff changeset
   155
end;
644b757d20e6 Start work on physfs support in engine
unc0rr
parents:
diff changeset
   156
644b757d20e6 Start work on physfs support in engine
unc0rr
parents:
diff changeset
   157
end.