physfs_platform_playdate.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*
  2. * Playdate support routines for PhysicsFS.
  3. *
  4. * Please see the file LICENSE.txt in the source's root directory.
  5. *
  6. * This file written by Ryan C. Gordon.
  7. */
  8. #define __PHYSICSFS_INTERNAL__
  9. #include "physfs_platforms.h"
  10. #ifdef PHYSFS_PLATFORM_PLAYDATE
  11. #include "pd_api.h"
  12. #include "physfs_internal.h"
  13. static PlaydateAPI *playdate = NULL;
  14. /* Playdate interpolates a separate write dir on top of a read-only
  15. base dir, so put the "user" dir somewhere deeper in the tree
  16. so they don't overlap, so PhysicsFS can manage this like apps
  17. expect. */
  18. #define WRITABLE_DIRNAME ".$PHYSFSWRITE$"
  19. static void *playdateAllocatorMalloc(PHYSFS_uint64 s);
  20. static void *playdateAllocatorRealloc(void *ptr, PHYSFS_uint64 s);
  21. static void playdateAllocatorFree(void *ptr);
  22. int __PHYSFS_platformInit(const char *argv0)
  23. {
  24. /* as a cheat, we expect argv0 to be a PlaydateAPI* on Playdate. */
  25. playdate = (PlaydateAPI *) argv0;
  26. allocator.Init = NULL;
  27. allocator.Deinit = NULL;
  28. allocator.Malloc = playdateAllocatorMalloc;
  29. allocator.Realloc = playdateAllocatorRealloc;
  30. allocator.Free = playdateAllocatorFree;
  31. return 1; /* ready to go! */
  32. }
  33. void __PHYSFS_platformDeinit(void)
  34. {
  35. playdate = NULL;
  36. }
  37. void __PHYSFS_platformDetectAvailableCDs(PHYSFS_StringCallback cb, void *data)
  38. {
  39. /* obviously no CD-ROM drives on Playdate */
  40. }
  41. char *__PHYSFS_platformCalcBaseDir(const char *argv0)
  42. {
  43. return __PHYSFS_strdup("/");
  44. }
  45. char *__PHYSFS_platformCalcUserDir(void)
  46. {
  47. return __PHYSFS_strdup("/" WRITABLE_DIRNAME "/");
  48. }
  49. char *__PHYSFS_platformCalcPrefDir(const char *org, const char *app)
  50. {
  51. const size_t slen = strlen(org) + strlen(app) + strlen(WRITABLE_DIRNAME) + 5;
  52. char *retval = (char *) allocator.Malloc(slen);
  53. BAIL_IF(!retval, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
  54. snprintf(retval, slen, "/" WRITABLE_DIRNAME "/%s/%s/", org, app);
  55. return retval;
  56. }
  57. typedef struct listfiles_callback_data
  58. {
  59. PHYSFS_EnumerateCallback callback;
  60. const char *origdir;
  61. void *callbackdata;
  62. PHYSFS_EnumerateCallbackResult retval;
  63. } listfiles_callback_data;
  64. static void listfiles_callback(const char *path, void *userdata)
  65. {
  66. listfiles_callback_data *data = (listfiles_callback_data *) userdata;
  67. if (data->retval == PHYSFS_ENUM_OK) {
  68. const char *od = data->origdir;
  69. if (path[0] == '.') { /* ignore "." and ".." */
  70. if ((path[1] == '\0') || ((path[1] == '.') && (path[2] == '\0'))) {
  71. return;
  72. }
  73. }
  74. if ((*od == '\0') || ((od[0] == '/') && (od[1] == '\0'))) {
  75. /* don't list our separated write dir when enumerating. */
  76. if (strcmp(path, WRITABLE_DIRNAME) == 0) {
  77. return;
  78. }
  79. }
  80. data->retval = data->callback(data->callbackdata, data->origdir, path);
  81. }
  82. }
  83. PHYSFS_EnumerateCallbackResult __PHYSFS_platformEnumerate(const char *dirname,
  84. PHYSFS_EnumerateCallback callback,
  85. const char *origdir, void *callbackdata)
  86. {
  87. listfiles_callback_data data;
  88. data.callback = callback;
  89. data.origdir = origdir;
  90. data.callbackdata = callbackdata;
  91. data.retval = PHYSFS_ENUM_OK;
  92. if (playdate->file->listfiles(dirname, listfiles_callback, &data, 1) == -1) {
  93. data.retval = PHYSFS_ENUM_ERROR;
  94. PHYSFS_setErrorCode(PHYSFS_ERR_OS_ERROR);
  95. } else if (data.retval == PHYSFS_ENUM_ERROR) {
  96. PHYSFS_setErrorCode(PHYSFS_ERR_APP_CALLBACK);
  97. }
  98. return data.retval;
  99. }
  100. int __PHYSFS_platformMkDir(const char *filename)
  101. {
  102. BAIL_IF(playdate->file->mkdir(filename) == -1, PHYSFS_ERR_OS_ERROR, 0);
  103. return 1;
  104. }
  105. void *__PHYSFS_platformOpenRead(const char *filename)
  106. {
  107. SDFile *sdf = playdate->file->open(filename, kFileRead|kFileReadData);
  108. BAIL_IF(!sdf, PHYSFS_ERR_OS_ERROR, NULL);
  109. return sdf;
  110. }
  111. void *__PHYSFS_platformOpenWrite(const char *filename)
  112. {
  113. SDFile *sdf = playdate->file->open(filename, kFileWrite);
  114. BAIL_IF(!sdf, PHYSFS_ERR_OS_ERROR, NULL);
  115. return sdf;
  116. }
  117. void *__PHYSFS_platformOpenAppend(const char *filename)
  118. {
  119. SDFile *sdf = playdate->file->open(filename, kFileAppend);
  120. BAIL_IF(!sdf, PHYSFS_ERR_OS_ERROR, NULL);
  121. return sdf;
  122. }
  123. PHYSFS_sint64 __PHYSFS_platformRead(void *opaque, void *buf, PHYSFS_uint64 len)
  124. {
  125. int rc;
  126. BAIL_IF(!__PHYSFS_ui64FitsAddressSpace(len),PHYSFS_ERR_INVALID_ARGUMENT,-1);
  127. rc = playdate->file->read((SDFile *) opaque, buf, (unsigned int) len);
  128. BAIL_IF(rc == -1, PHYSFS_ERR_OS_ERROR, -1);
  129. return (PHYSFS_sint64) rc;
  130. }
  131. PHYSFS_sint64 __PHYSFS_platformWrite(void *opaque, const void *buf,
  132. PHYSFS_uint64 len)
  133. {
  134. int rc;
  135. BAIL_IF(!__PHYSFS_ui64FitsAddressSpace(len),PHYSFS_ERR_INVALID_ARGUMENT,-1);
  136. rc = playdate->file->write((SDFile *) opaque, buf, (unsigned int) len);
  137. BAIL_IF(rc == -1, PHYSFS_ERR_OS_ERROR, -1);
  138. return (PHYSFS_sint64) rc;
  139. }
  140. int __PHYSFS_platformSeek(void *opaque, PHYSFS_uint64 pos)
  141. {
  142. const int ipos = (int) pos;
  143. int rc;
  144. /* hooray for 32-bit filesystem limits! :) */
  145. BAIL_IF(((PHYSFS_uint64) ipos) != pos, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  146. rc = playdate->file->seek((SDFile *) opaque, ipos, SEEK_SET);
  147. BAIL_IF(rc == -1, PHYSFS_ERR_OS_ERROR, 0);
  148. return 1;
  149. }
  150. PHYSFS_sint64 __PHYSFS_platformTell(void *opaque)
  151. {
  152. const int pos = playdate->file->tell((SDFile *) opaque);
  153. BAIL_IF(pos == -1, PHYSFS_ERR_OS_ERROR, -1);
  154. return (PHYSFS_sint64) pos;
  155. }
  156. PHYSFS_sint64 __PHYSFS_platformFileLength(void *opaque)
  157. {
  158. SDFile *sdf = (SDFile *) opaque;
  159. const int pos = playdate->file->tell(sdf);
  160. int retval;
  161. BAIL_IF(pos == -1, PHYSFS_ERR_OS_ERROR, -1);
  162. BAIL_IF(playdate->file->seek(sdf, 0, SEEK_END) == -1, PHYSFS_ERR_OS_ERROR, -1);
  163. retval = playdate->file->tell(sdf);
  164. BAIL_IF(playdate->file->seek(sdf, pos, SEEK_CUR) == -1, PHYSFS_ERR_OS_ERROR, -1);
  165. BAIL_IF(retval == -1, PHYSFS_ERR_OS_ERROR, -1);
  166. return (PHYSFS_sint64) retval;
  167. }
  168. int __PHYSFS_platformFlush(void *opaque)
  169. {
  170. BAIL_IF(playdate->file->flush((SDFile *) opaque) == -1, PHYSFS_ERR_OS_ERROR, 0);
  171. return 1;
  172. }
  173. void __PHYSFS_platformClose(void *opaque)
  174. {
  175. playdate->file->close((SDFile *) opaque); /* ignore errors. You should have flushed! */
  176. }
  177. int __PHYSFS_platformDelete(const char *path)
  178. {
  179. BAIL_IF(playdate->file->unlink(path, 0) == -1, PHYSFS_ERR_OS_ERROR, 0);
  180. return 1;
  181. }
  182. /* Convert to a format PhysicsFS can grok... */
  183. static PHYSFS_sint64 playdateTimeToUnixTime(FileStat *statbuf)
  184. {
  185. return 10; /* !!! FIXME: calculate the time in seconds, adjust for leap years and the Unix epoch. */
  186. }
  187. int __PHYSFS_platformStat(const char *filename, PHYSFS_Stat *st, const int follow)
  188. {
  189. FileStat statbuf;
  190. const int rc = playdate->file->stat(filename, &statbuf);
  191. BAIL_IF(rc == -1, PHYSFS_ERR_OS_ERROR, 0);
  192. st->filesize = (PHYSFS_sint64) statbuf.size;
  193. st->modtime = st->createtime = playdateTimeToUnixTime(&statbuf);
  194. st->accesstime = -1;
  195. st->filetype = statbuf.isdir ? PHYSFS_FILETYPE_DIRECTORY : PHYSFS_FILETYPE_REGULAR;
  196. st->readonly = (strncmp(filename, "/" WRITABLE_DIRNAME, strlen("/" WRITABLE_DIRNAME)) == 0) ? 1 : 0;
  197. return 1;
  198. }
  199. void *__PHYSFS_platformGetThreadID(void)
  200. {
  201. return (void *) (size_t) 0x1; /* !!! FIXME: does Playdate have threads? */
  202. }
  203. void *__PHYSFS_platformCreateMutex(void)
  204. {
  205. return (void *) (size_t) 0x1; /* !!! FIXME: does Playdate have threads? */
  206. }
  207. void __PHYSFS_platformDestroyMutex(void *mutex)
  208. {
  209. }
  210. int __PHYSFS_platformGrabMutex(void *mutex)
  211. {
  212. return 1;
  213. }
  214. void __PHYSFS_platformReleaseMutex(void *mutex)
  215. {
  216. }
  217. #undef realloc
  218. static void *playdateAllocatorMalloc(PHYSFS_uint64 s)
  219. {
  220. BAIL_IF(!__PHYSFS_ui64FitsAddressSpace(s), PHYSFS_ERR_OUT_OF_MEMORY, NULL);
  221. return playdate->system->realloc(NULL, (size_t) s);
  222. }
  223. static void *playdateAllocatorRealloc(void *ptr, PHYSFS_uint64 s)
  224. {
  225. BAIL_IF(!__PHYSFS_ui64FitsAddressSpace(s), PHYSFS_ERR_OUT_OF_MEMORY, NULL);
  226. return playdate->system->realloc(ptr, (size_t) s);
  227. }
  228. static void playdateAllocatorFree(void *ptr)
  229. {
  230. playdate->system->realloc(ptr, 0);
  231. }
  232. #endif /* PHYSFS_PLATFORM_PLAYDATE */
  233. /* end of physfs_platform_playdate.c ... */