7959
|
1 |
unit uPhysFSLayer;
|
8063
|
2 |
|
7959
|
3 |
interface
|
8077
|
4 |
uses SDLh, LuaPas;
|
7959
|
5 |
|
8073
|
6 |
{$IFDEF ANDROID}
|
|
7 |
{$linklib physfs}
|
|
8 |
{$ELSE}
|
|
9 |
{$IFDEF DARWIN}
|
|
10 |
{$LINKFRAMEWORK IOKit}
|
|
11 |
{$ENDIF}
|
|
12 |
{$ENDIF}
|
|
13 |
|
|
14 |
const
|
|
15 |
{$IFDEF WIN32}
|
|
16 |
PhysfsLibName = 'libphysfs';
|
|
17 |
{$ELSE}
|
|
18 |
{$IFDEF DARWIN}
|
|
19 |
PhysfsLibName = 'physfs';
|
|
20 |
{$ELSE}
|
8077
|
21 |
PhysfsLibName = 'physfs';
|
8073
|
22 |
{$ENDIF}
|
|
23 |
{$ENDIF}
|
|
24 |
|
7959
|
25 |
procedure initModule;
|
|
26 |
procedure freeModule;
|
|
27 |
|
8028
|
28 |
type PFSFile = pointer;
|
|
29 |
|
8025
|
30 |
function rwopsOpenRead(fname: shortstring): PSDL_RWops;
|
|
31 |
function rwopsOpenWrite(fname: shortstring): PSDL_RWops;
|
8022
|
32 |
|
8028
|
33 |
function pfsOpenRead(fname: shortstring): PFSFile;
|
|
34 |
function pfsClose(f: PFSFile): boolean;
|
|
35 |
|
|
36 |
procedure pfsReadLn(f: PFSFile; var s: shortstring);
|
8031
|
37 |
function pfsBlockRead(f: PFSFile; buf: pointer; size: Int64): Int64;
|
|
38 |
function pfsEOF(f: PFSFile): boolean;
|
|
39 |
|
|
40 |
function pfsExists(fname: shortstring): boolean;
|
8028
|
41 |
|
8077
|
42 |
function physfsReader(L: Plua_State; f: PFSFile; sz: Psize_t) : PChar; cdecl; external PhysfsLibName;
|
|
43 |
procedure physfsReaderSetBuffer(buf: pointer); cdecl; external PhysfsLibName;
|
|
44 |
|
7959
|
45 |
implementation
|
8022
|
46 |
uses uUtils, uVariables;
|
7959
|
47 |
|
8073
|
48 |
function PHYSFS_init(argv0: PChar) : LongInt; cdecl; external PhysfsLibName;
|
|
49 |
function PHYSFS_deinit() : LongInt; cdecl; external PhysfsLibName;
|
|
50 |
function PHYSFSRWOPS_openRead(fname: PChar): PSDL_RWops; cdecl ; external PhysfsLibName;
|
|
51 |
function PHYSFSRWOPS_openWrite(fname: PChar): PSDL_RWops; cdecl; external PhysfsLibName;
|
7959
|
52 |
|
8073
|
53 |
function PHYSFS_mount(newDir, mountPoint: PChar; appendToPath: LongBool) : LongInt; cdecl; external PhysfsLibName;
|
|
54 |
function PHYSFS_openRead(fname: PChar): PFSFile; cdecl; external PhysfsLibName;
|
|
55 |
function PHYSFS_eof(f: PFSFile): LongBool; cdecl; external PhysfsLibName;
|
|
56 |
function PHYSFS_readBytes(f: PFSFile; buffer: pointer; len: Int64): Int64; cdecl; external PhysfsLibName;
|
|
57 |
function PHYSFS_close(f: PFSFile): LongBool; cdecl; external PhysfsLibName;
|
|
58 |
function PHYSFS_exists(fname: PChar): LongBool; cdecl; external PhysfsLibName;
|
7959
|
59 |
|
8073
|
60 |
procedure hedgewarsMountPackages(); cdecl; external PhysfsLibName;
|
8052
|
61 |
|
8025
|
62 |
function rwopsOpenRead(fname: shortstring): PSDL_RWops;
|
|
63 |
begin
|
|
64 |
exit(PHYSFSRWOPS_openRead(Str2PChar(fname)));
|
|
65 |
end;
|
|
66 |
|
|
67 |
function rwopsOpenWrite(fname: shortstring): PSDL_RWops;
|
7959
|
68 |
begin
|
8025
|
69 |
exit(PHYSFSRWOPS_openWrite(Str2PChar(fname)));
|
|
70 |
end;
|
8022
|
71 |
|
8028
|
72 |
function pfsOpenRead(fname: shortstring): PFSFile;
|
|
73 |
begin
|
|
74 |
exit(PHYSFS_openRead(Str2PChar(fname)));
|
|
75 |
end;
|
|
76 |
|
|
77 |
function pfsEOF(f: PFSFile): boolean;
|
|
78 |
begin
|
|
79 |
exit(PHYSFS_eof(f))
|
|
80 |
end;
|
|
81 |
|
|
82 |
function pfsClose(f: PFSFile): boolean;
|
|
83 |
begin
|
|
84 |
exit(PHYSFS_close(f))
|
|
85 |
end;
|
|
86 |
|
8031
|
87 |
function pfsExists(fname: shortstring): boolean;
|
|
88 |
begin
|
|
89 |
exit(PHYSFS_exists(Str2PChar(fname)))
|
|
90 |
end;
|
|
91 |
|
8028
|
92 |
|
|
93 |
procedure pfsReadLn(f: PFSFile; var s: shortstring);
|
|
94 |
var c: char;
|
|
95 |
begin
|
|
96 |
s[0]:= #0;
|
|
97 |
|
8034
|
98 |
while (PHYSFS_readBytes(f, @c, 1) = 1) and (c <> #10) do
|
8028
|
99 |
if (c <> #13) and (s[0] < #255) then
|
|
100 |
begin
|
|
101 |
inc(s[0]);
|
|
102 |
s[byte(s[0])]:= c
|
|
103 |
end
|
|
104 |
end;
|
|
105 |
|
8031
|
106 |
function pfsBlockRead(f: PFSFile; buf: pointer; size: Int64): Int64;
|
|
107 |
var r: Int64;
|
|
108 |
begin
|
8034
|
109 |
r:= PHYSFS_readBytes(f, buf, size);
|
8031
|
110 |
|
|
111 |
if r <= 0 then
|
|
112 |
pfsBlockRead:= 0
|
|
113 |
else
|
|
114 |
pfsBlockRead:= r
|
|
115 |
end;
|
|
116 |
|
8025
|
117 |
procedure initModule;
|
|
118 |
var i: LongInt;
|
|
119 |
begin
|
|
120 |
i:= PHYSFS_init(Str2PChar(ParamStr(0)));
|
|
121 |
AddFileLog('[PhysFS] init: ' + inttostr(i));
|
|
122 |
|
|
123 |
i:= PHYSFS_mount(Str2PChar(PathPrefix), nil, true);
|
|
124 |
AddFileLog('[PhysFS] mount ' + PathPrefix + ': ' + inttostr(i));
|
8031
|
125 |
i:= PHYSFS_mount(Str2PChar(UserPathPrefix + '/Data'), nil, true);
|
8046
|
126 |
AddFileLog('[PhysFS] mount ' + UserPathPrefix + '/Data: ' + inttostr(i));
|
8052
|
127 |
|
|
128 |
hedgewarsMountPackages;
|
7959
|
129 |
end;
|
|
130 |
|
|
131 |
procedure freeModule;
|
|
132 |
begin
|
|
133 |
PHYSFS_deinit;
|
|
134 |
end;
|
|
135 |
|
|
136 |
end.
|