author | alfadur |
Sun, 14 Oct 2018 22:55:07 +0300 | |
changeset 13912 | d178a834f1f4 |
parent 13538 | f8c0a62fa3ac |
child 13913 | f8b5708835de |
permissions | -rw-r--r-- |
7768 | 1 |
/* |
2 |
* This code provides a glue layer between PhysicsFS and Simple Directmedia |
|
3 |
* Layer's (SDL) RWops i/o abstraction. |
|
4 |
* |
|
5 |
* License: this code is public domain. I make no warranty that it is useful, |
|
6 |
* correct, harmless, or environmentally safe. |
|
7 |
* |
|
8 |
* This particular file may be used however you like, including copying it |
|
9 |
* verbatim into a closed-source project, exploiting it commercially, and |
|
10 |
* removing any trace of my name from the source (although I hope you won't |
|
11 |
* do that). I welcome enhancements and corrections to this file, but I do |
|
12 |
* not require you to send me patches if you make changes. This code has |
|
13 |
* NO WARRANTY. |
|
14 |
* |
|
15 |
* Unless otherwise stated, the rest of PhysicsFS falls under the zlib license. |
|
16 |
* Please see LICENSE.txt in the root of the source tree. |
|
17 |
* |
|
18 |
* SDL 1.2 falls under the LGPL license. SDL 1.3+ is zlib, like PhysicsFS. |
|
19 |
* You can get SDL at http://www.libsdl.org/ |
|
20 |
* |
|
21 |
* This file was written by Ryan C. Gordon. (icculus@icculus.org). |
|
22 |
*/ |
|
23 |
||
24 |
#include <stdio.h> /* used for SEEK_SET, SEEK_CUR, SEEK_END ... */ |
|
25 |
#include "physfsrwops.h" |
|
26 |
||
13912
d178a834f1f4
set proper sdl calling convention for vcpkg builds
alfadur
parents:
13538
diff
changeset
|
27 |
#ifdef WIN32_VCPKG |
d178a834f1f4
set proper sdl calling convention for vcpkg builds
alfadur
parents:
13538
diff
changeset
|
28 |
#define SDLCALL __cdecl |
d178a834f1f4
set proper sdl calling convention for vcpkg builds
alfadur
parents:
13538
diff
changeset
|
29 |
#endif |
d178a834f1f4
set proper sdl calling convention for vcpkg builds
alfadur
parents:
13538
diff
changeset
|
30 |
|
7768 | 31 |
/* SDL's RWOPS interface changed a little in SDL 1.3... */ |
32 |
#if defined(SDL_VERSION_ATLEAST) |
|
33 |
#if SDL_VERSION_ATLEAST(1, 3, 0) |
|
34 |
#define TARGET_SDL13 1 |
|
35 |
#endif |
|
36 |
#endif |
|
37 |
||
38 |
#if TARGET_SDL13 |
|
9378
2be457289e60
update physlayer and sdl bindings to the new rwops interface
koda
parents:
9309
diff
changeset
|
39 |
static SDLCALL Sint64 physfsrwops_size(struct SDL_RWops *rw) |
2be457289e60
update physlayer and sdl bindings to the new rwops interface
koda
parents:
9309
diff
changeset
|
40 |
{ |
2be457289e60
update physlayer and sdl bindings to the new rwops interface
koda
parents:
9309
diff
changeset
|
41 |
PHYSFS_File *handle = (PHYSFS_File *) rw->hidden.unknown.data1; |
2be457289e60
update physlayer and sdl bindings to the new rwops interface
koda
parents:
9309
diff
changeset
|
42 |
return PHYSFS_fileLength(handle); |
2be457289e60
update physlayer and sdl bindings to the new rwops interface
koda
parents:
9309
diff
changeset
|
43 |
} |
2be457289e60
update physlayer and sdl bindings to the new rwops interface
koda
parents:
9309
diff
changeset
|
44 |
#endif |
2be457289e60
update physlayer and sdl bindings to the new rwops interface
koda
parents:
9309
diff
changeset
|
45 |
|
2be457289e60
update physlayer and sdl bindings to the new rwops interface
koda
parents:
9309
diff
changeset
|
46 |
#if TARGET_SDL13 |
9309
7e8f91634f80
engine compiles for ios again, but SDL bindings are outdated. Fix some warnings
koda
parents:
8524
diff
changeset
|
47 |
static SDLCALL Sint64 physfsrwops_seek(struct SDL_RWops *rw, Sint64 offset, int whence) |
7768 | 48 |
#else |
49 |
static int physfsrwops_seek(SDL_RWops *rw, int offset, int whence) |
|
50 |
#endif |
|
51 |
{ |
|
52 |
PHYSFS_File *handle = (PHYSFS_File *) rw->hidden.unknown.data1; |
|
53 |
PHYSFS_sint64 pos = 0; |
|
54 |
||
55 |
if (whence == SEEK_SET) |
|
56 |
pos = (PHYSFS_sint64) offset; |
|
57 |
else if (whence == SEEK_CUR) |
|
58 |
{ |
|
59 |
const PHYSFS_sint64 current = PHYSFS_tell(handle); |
|
60 |
if (current == -1) |
|
61 |
{ |
|
62 |
SDL_SetError("Can't find position in file: %s", |
|
13538
f8c0a62fa3ac
Fix deprecation warnings in libphyslayer
Wuzzy <Wuzzy2@mail.ru>
parents:
11656
diff
changeset
|
63 |
PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode())); |
7768 | 64 |
return -1; |
65 |
} /* if */ |
|
66 |
||
67 |
if (offset == 0) /* this is a "tell" call. We're done. */ |
|
68 |
{ |
|
69 |
#if TARGET_SDL13 |
|
70 |
return (long) current; |
|
71 |
#else |
|
72 |
return (int) current; |
|
73 |
#endif |
|
74 |
} /* if */ |
|
75 |
||
76 |
pos = current + ((PHYSFS_sint64) offset); |
|
77 |
} /* else if */ |
|
78 |
||
79 |
else if (whence == SEEK_END) |
|
80 |
{ |
|
81 |
const PHYSFS_sint64 len = PHYSFS_fileLength(handle); |
|
82 |
if (len == -1) |
|
83 |
{ |
|
13538
f8c0a62fa3ac
Fix deprecation warnings in libphyslayer
Wuzzy <Wuzzy2@mail.ru>
parents:
11656
diff
changeset
|
84 |
SDL_SetError("Can't find end of file: %s", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode())); |
7768 | 85 |
return -1; |
86 |
} /* if */ |
|
87 |
||
88 |
pos = len + ((PHYSFS_sint64) offset); |
|
89 |
} /* else if */ |
|
90 |
||
91 |
else |
|
92 |
{ |
|
93 |
SDL_SetError("Invalid 'whence' parameter."); |
|
94 |
return -1; |
|
95 |
} /* else */ |
|
96 |
||
97 |
if ( pos < 0 ) |
|
98 |
{ |
|
99 |
SDL_SetError("Attempt to seek past start of file."); |
|
100 |
return -1; |
|
101 |
} /* if */ |
|
10017 | 102 |
|
7768 | 103 |
if (!PHYSFS_seek(handle, (PHYSFS_uint64) pos)) |
104 |
{ |
|
13538
f8c0a62fa3ac
Fix deprecation warnings in libphyslayer
Wuzzy <Wuzzy2@mail.ru>
parents:
11656
diff
changeset
|
105 |
SDL_SetError("PhysicsFS error: %s", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode())); |
7768 | 106 |
return -1; |
107 |
} /* if */ |
|
108 |
||
109 |
#if TARGET_SDL13 |
|
110 |
return (long) pos; |
|
111 |
#else |
|
112 |
return (int) pos; |
|
113 |
#endif |
|
114 |
} /* physfsrwops_seek */ |
|
115 |
||
116 |
||
117 |
#if TARGET_SDL13 |
|
118 |
static size_t SDLCALL physfsrwops_read(struct SDL_RWops *rw, void *ptr, |
|
119 |
size_t size, size_t maxnum) |
|
120 |
#else |
|
121 |
static int physfsrwops_read(SDL_RWops *rw, void *ptr, int size, int maxnum) |
|
122 |
#endif |
|
123 |
{ |
|
124 |
PHYSFS_File *handle = (PHYSFS_File *) rw->hidden.unknown.data1; |
|
125 |
const PHYSFS_uint64 readlen = (PHYSFS_uint64) (maxnum * size); |
|
126 |
const PHYSFS_sint64 rc = PHYSFS_readBytes(handle, ptr, readlen); |
|
127 |
if (rc != ((PHYSFS_sint64) readlen)) |
|
128 |
{ |
|
129 |
if (!PHYSFS_eof(handle)) /* not EOF? Must be an error. */ |
|
13538
f8c0a62fa3ac
Fix deprecation warnings in libphyslayer
Wuzzy <Wuzzy2@mail.ru>
parents:
11656
diff
changeset
|
130 |
SDL_SetError("PhysicsFS error: %s", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode())); |
7768 | 131 |
} /* if */ |
132 |
||
133 |
#if TARGET_SDL13 |
|
134 |
return (size_t) rc; |
|
135 |
#else |
|
136 |
return (int) rc; |
|
137 |
#endif |
|
138 |
} /* physfsrwops_read */ |
|
139 |
||
140 |
||
141 |
#if TARGET_SDL13 |
|
142 |
static size_t SDLCALL physfsrwops_write(struct SDL_RWops *rw, const void *ptr, |
|
143 |
size_t size, size_t num) |
|
144 |
#else |
|
145 |
static int physfsrwops_write(SDL_RWops *rw, const void *ptr, int size, int num) |
|
146 |
#endif |
|
147 |
{ |
|
148 |
PHYSFS_File *handle = (PHYSFS_File *) rw->hidden.unknown.data1; |
|
149 |
const PHYSFS_uint64 writelen = (PHYSFS_uint64) (num * size); |
|
150 |
const PHYSFS_sint64 rc = PHYSFS_writeBytes(handle, ptr, writelen); |
|
151 |
if (rc != ((PHYSFS_sint64) writelen)) |
|
13538
f8c0a62fa3ac
Fix deprecation warnings in libphyslayer
Wuzzy <Wuzzy2@mail.ru>
parents:
11656
diff
changeset
|
152 |
SDL_SetError("PhysicsFS error: %s", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode())); |
7768 | 153 |
|
154 |
#if TARGET_SDL13 |
|
155 |
return (size_t) rc; |
|
156 |
#else |
|
157 |
return (int) rc; |
|
158 |
#endif |
|
159 |
} /* physfsrwops_write */ |
|
160 |
||
161 |
||
162 |
static int physfsrwops_close(SDL_RWops *rw) |
|
163 |
{ |
|
164 |
PHYSFS_File *handle = (PHYSFS_File *) rw->hidden.unknown.data1; |
|
165 |
if (!PHYSFS_close(handle)) |
|
166 |
{ |
|
13538
f8c0a62fa3ac
Fix deprecation warnings in libphyslayer
Wuzzy <Wuzzy2@mail.ru>
parents:
11656
diff
changeset
|
167 |
SDL_SetError("PhysicsFS error: %s", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode())); |
7768 | 168 |
return -1; |
169 |
} /* if */ |
|
170 |
||
171 |
SDL_FreeRW(rw); |
|
172 |
return 0; |
|
173 |
} /* physfsrwops_close */ |
|
174 |
||
175 |
||
176 |
static SDL_RWops *create_rwops(PHYSFS_File *handle) |
|
177 |
{ |
|
178 |
SDL_RWops *retval = NULL; |
|
179 |
||
180 |
if (handle == NULL) |
|
13538
f8c0a62fa3ac
Fix deprecation warnings in libphyslayer
Wuzzy <Wuzzy2@mail.ru>
parents:
11656
diff
changeset
|
181 |
SDL_SetError("PhysicsFS error: %s", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode())); |
7768 | 182 |
else |
183 |
{ |
|
184 |
retval = SDL_AllocRW(); |
|
185 |
if (retval != NULL) |
|
186 |
{ |
|
11656 | 187 |
#if TARGET_SDL13 && !defined(EMSCRIPTEN) |
9378
2be457289e60
update physlayer and sdl bindings to the new rwops interface
koda
parents:
9309
diff
changeset
|
188 |
retval->size = physfsrwops_size; |
2be457289e60
update physlayer and sdl bindings to the new rwops interface
koda
parents:
9309
diff
changeset
|
189 |
#endif |
7768 | 190 |
retval->seek = physfsrwops_seek; |
191 |
retval->read = physfsrwops_read; |
|
192 |
retval->write = physfsrwops_write; |
|
193 |
retval->close = physfsrwops_close; |
|
194 |
retval->hidden.unknown.data1 = handle; |
|
195 |
} /* if */ |
|
196 |
} /* else */ |
|
197 |
||
198 |
return retval; |
|
199 |
} /* create_rwops */ |
|
200 |
||
201 |
||
202 |
SDL_RWops *PHYSFSRWOPS_makeRWops(PHYSFS_File *handle) |
|
203 |
{ |
|
204 |
SDL_RWops *retval = NULL; |
|
205 |
if (handle == NULL) |
|
206 |
SDL_SetError("NULL pointer passed to PHYSFSRWOPS_makeRWops()."); |
|
207 |
else |
|
208 |
retval = create_rwops(handle); |
|
209 |
||
210 |
return retval; |
|
211 |
} /* PHYSFSRWOPS_makeRWops */ |
|
212 |
||
213 |
||
214 |
SDL_RWops *PHYSFSRWOPS_openRead(const char *fname) |
|
215 |
{ |
|
216 |
return create_rwops(PHYSFS_openRead(fname)); |
|
217 |
} /* PHYSFSRWOPS_openRead */ |
|
218 |
||
219 |
||
220 |
SDL_RWops *PHYSFSRWOPS_openWrite(const char *fname) |
|
221 |
{ |
|
222 |
return create_rwops(PHYSFS_openWrite(fname)); |
|
223 |
} /* PHYSFSRWOPS_openWrite */ |
|
224 |
||
225 |
||
226 |
SDL_RWops *PHYSFSRWOPS_openAppend(const char *fname) |
|
227 |
{ |
|
228 |
return create_rwops(PHYSFS_openAppend(fname)); |
|
229 |
} /* PHYSFSRWOPS_openAppend */ |
|
230 |
||
231 |
||
232 |
/* end of physfsrwops.c ... */ |
|
233 |