You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
71 lines
1.5 KiB
71 lines
1.5 KiB
|
4 years ago
|
#include "utils/sdl_rwops_file_wrapper.hpp"
|
||
|
5 years ago
|
|
||
|
4 years ago
|
#ifdef DEVILUTIONX_SDL_RWOPS_FILE_WRAPPER_AVAILABLE
|
||
|
5 years ago
|
|
||
|
|
#include "utils/log.hpp"
|
||
|
|
|
||
|
|
namespace devilution {
|
||
|
|
extern "C" {
|
||
|
|
|
||
|
4 years ago
|
#ifdef DEVILUTIONX_SDL_RWOPS_FILE_WRAPPER_IMPL_FOPENCOOKIE
|
||
|
5 years ago
|
|
||
|
4 years ago
|
ssize_t SDL_RWops_CookieRead(void *cookie, char *buf, size_t nbytes)
|
||
|
5 years ago
|
{
|
||
|
4 years ago
|
size_t numRead = SDL_RWread(static_cast<SDL_RWops *>(cookie), buf, nbytes, 1);
|
||
|
|
if (numRead == 0) {
|
||
|
|
Log("SDL_RWread error: {} ERROR CODE {}", SDL_GetError(), numRead);
|
||
|
5 years ago
|
}
|
||
|
4 years ago
|
return numRead * nbytes;
|
||
|
5 years ago
|
}
|
||
|
|
|
||
|
4 years ago
|
int SDL_RWops_CookieSeek(void *cookie, off64_t *pos, int whence)
|
||
|
5 years ago
|
{
|
||
|
|
int swhence;
|
||
|
|
switch (whence) {
|
||
|
|
case SEEK_SET:
|
||
|
4 years ago
|
swhence = RW_SEEK_SET;
|
||
|
5 years ago
|
break;
|
||
|
|
case SEEK_CUR:
|
||
|
4 years ago
|
swhence = RW_SEEK_CUR;
|
||
|
5 years ago
|
break;
|
||
|
|
case SEEK_END:
|
||
|
4 years ago
|
swhence = RW_SEEK_END;
|
||
|
5 years ago
|
break;
|
||
|
|
default:
|
||
|
|
return -1;
|
||
|
|
}
|
||
|
4 years ago
|
const Sint64 spos = SDL_RWseek(static_cast<SDL_RWops *>(cookie), *pos, swhence);
|
||
|
|
if (spos < 0) {
|
||
|
4 years ago
|
Log("SDL_RWops_RwSeek error: {}", SDL_GetError());
|
||
|
5 years ago
|
return -1;
|
||
|
|
}
|
||
|
|
*pos = static_cast<off64_t>(spos);
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
|
||
|
4 years ago
|
int SDL_RWops_CookieClose(void *cookie)
|
||
|
5 years ago
|
{
|
||
|
4 years ago
|
return SDL_RWclose(static_cast<SDL_RWops *>(cookie));
|
||
|
5 years ago
|
}
|
||
|
|
|
||
|
|
} // extern "C"
|
||
|
|
#endif
|
||
|
|
|
||
|
4 years ago
|
FILE *FILE_FromSDL_RWops(SDL_RWops *handle)
|
||
|
5 years ago
|
{
|
||
|
4 years ago
|
#ifdef DEVILUTIONX_SDL_RWOPS_FILE_WRAPPER_IMPL_FOPENCOOKIE
|
||
|
5 years ago
|
cookie_io_functions_t ioFns;
|
||
|
|
std::memset(&ioFns, 0, sizeof(ioFns));
|
||
|
4 years ago
|
ioFns.read = &SDL_RWops_CookieRead;
|
||
|
|
ioFns.seek = &SDL_RWops_CookieSeek;
|
||
|
|
ioFns.close = &SDL_RWops_CookieClose;
|
||
|
5 years ago
|
return fopencookie(handle, "rb", ioFns);
|
||
|
|
#else
|
||
|
|
#error "unimplemented"
|
||
|
|
#endif
|
||
|
|
}
|
||
|
|
|
||
|
|
} // namespace devilution
|
||
|
|
|
||
|
|
#endif
|