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