SDL_storage.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. /**
  19. * \file SDL_storage.h
  20. *
  21. * Include file for storage container SDL API functions
  22. */
  23. #ifndef SDL_storage_h_
  24. #define SDL_storage_h_
  25. #include <SDL3/SDL_stdinc.h>
  26. #include <SDL3/SDL_mutex.h>
  27. #include <SDL3/SDL_properties.h>
  28. #include <SDL3/SDL_begin_code.h>
  29. /* Set up for C function definitions, even when using C++ */
  30. #ifdef __cplusplus
  31. extern "C" {
  32. #endif
  33. /* !!! FIXME: Don't let this ship without async R/W support!!! */
  34. typedef struct SDL_StorageInterface
  35. {
  36. int (SDLCALL *close)(void *userdata);
  37. SDL_bool (SDLCALL *ready)(void *userdata);
  38. int (SDLCALL *fileSize)(void *userdata, const char *path, Uint64 *length);
  39. int (SDLCALL *readFile)(void *userdata, const char *path, void *destination, Uint64 length);
  40. int (SDLCALL *writeFile)(void *userdata, const char *path, const void *source, Uint64 length);
  41. Uint64 (SDLCALL *spaceRemaining)(void *userdata);
  42. } SDL_StorageInterface;
  43. typedef struct SDL_Storage SDL_Storage;
  44. /**
  45. * Opens up a read-only container for the application's filesystem.
  46. *
  47. * \param override a path to override the backend's default title root
  48. * \param props a property list that may contain backend-specific information
  49. * \returns a title storage container on success or NULL on failure; call
  50. * SDL_GetError() for more information.
  51. *
  52. * \since This function is available since SDL 3.0.0.
  53. *
  54. * \sa SDL_OpenUserStorage
  55. * \sa SDL_OpenStorage
  56. * \sa SDL_TitleStorageReady
  57. * \sa SDL_CloseStorage
  58. * \sa SDL_StorageReady
  59. * \sa SDL_StorageFileSize
  60. * \sa SDL_StorageReadFile
  61. * \sa SDL_StorageWriteFile
  62. * \sa SDL_StorageSpaceRemaining
  63. */
  64. extern DECLSPEC SDL_Storage *SDLCALL SDL_OpenTitleStorage(const char *override, SDL_PropertiesID props);
  65. /**
  66. * Opens up a container for a user's unique read/write filesystem.
  67. *
  68. * While title storage can generally be kept open throughout runtime, user
  69. * storage should only be opened when the client is ready to read/write files.
  70. * This allows the backend to properly batch R/W operations and flush them
  71. * when the container has been closed; ensuring safe and optimal save I/O.
  72. *
  73. * \param org the name of your organization
  74. * \param app the name of your application
  75. * \param props a property list that may contain backend-specific information
  76. * \returns a user storage container on success or NULL on failure; call
  77. * SDL_GetError() for more information.
  78. *
  79. * \since This function is available since SDL 3.0.0.
  80. *
  81. * \sa SDL_OpenTitleStorage
  82. * \sa SDL_OpenStorage
  83. * \sa SDL_CloseStorage
  84. * \sa SDL_StorageReady
  85. * \sa SDL_StorageFileSize
  86. * \sa SDL_StorageReadFile
  87. * \sa SDL_StorageWriteFile
  88. * \sa SDL_StorageSpaceRemaining
  89. */
  90. extern DECLSPEC SDL_Storage *SDLCALL SDL_OpenUserStorage(const char *org, const char *app, SDL_PropertiesID props);
  91. /**
  92. * Opens up a container using a client-provided storage interface.
  93. *
  94. * Applications do not need to use this function unless they are providing
  95. * their own SDL_Storage implementation. If you just need an SDL_Storage, you
  96. * should use the built-in implementations in SDL, like SDL_OpenTitleStorage()
  97. * or SDL_OpenUserStorage().
  98. *
  99. * \param iface the function table to be used by this container
  100. * \param userdata the pointer that will be passed to the store interface
  101. * \returns a storage container on success or NULL on failure; call
  102. * SDL_GetError() for more information.
  103. *
  104. * \since This function is available since SDL 3.0.0.
  105. *
  106. * \sa SDL_OpenTitleStorage
  107. * \sa SDL_OpenUserStorage
  108. * \sa SDL_CloseStorage
  109. * \sa SDL_StorageReady
  110. * \sa SDL_StorageFileSize
  111. * \sa SDL_StorageReadFile
  112. * \sa SDL_StorageWriteFile
  113. * \sa SDL_StorageSpaceRemaining
  114. */
  115. extern DECLSPEC SDL_Storage *SDLCALL SDL_OpenStorage(const SDL_StorageInterface *iface, void *userdata);
  116. /**
  117. * Closes and frees a storage container.
  118. *
  119. * \param storage a storage container to close
  120. * \returns 0 if the container was freed with no errors, a negative value
  121. * otherwise; call SDL_GetError() for more information. Even if the
  122. * function returns an error, the container data will be freed; the
  123. * error is only for informational purposes.
  124. *
  125. * \since This function is available since SDL 3.0.0.
  126. *
  127. * \sa SDL_OpenTitleStorage
  128. * \sa SDL_OpenUserStorage
  129. * \sa SDL_OpenStorage
  130. * \sa SDL_StorageReady
  131. * \sa SDL_StorageFileSize
  132. * \sa SDL_StorageReadFile
  133. * \sa SDL_StorageWriteFile
  134. * \sa SDL_StorageSpaceRemaining
  135. */
  136. extern DECLSPEC int SDLCALL SDL_CloseStorage(SDL_Storage *storage);
  137. /**
  138. * Checks if the storage container is ready to use.
  139. *
  140. * This function should be called in regular intervals until it returns
  141. * SDL_TRUE - however, it is not recommended to spinwait on this call, as the
  142. * backend may depend on a synchronous message loop.
  143. *
  144. * \param storage a storage container to query
  145. * \returns SDL_TRUE if the container is ready, SDL_FALSE otherwise
  146. *
  147. * \since This function is available since SDL 3.0.0.
  148. *
  149. * \sa SDL_OpenTitleStorage
  150. * \sa SDL_OpenUserStorage
  151. * \sa SDL_OpenStorage
  152. * \sa SDL_CloseStorage
  153. * \sa SDL_StorageFileSize
  154. * \sa SDL_StorageReadFile
  155. * \sa SDL_StorageWriteFile
  156. * \sa SDL_StorageSpaceRemaining
  157. */
  158. extern DECLSPEC SDL_bool SDLCALL SDL_StorageReady(SDL_Storage *storage);
  159. /**
  160. * Query the size of a file within a storage container.
  161. *
  162. * \param storage a storage container to query
  163. * \param path the relative path of the file to query
  164. * \param length a pointer to be filled with the file's length
  165. * \returns 0 if the file could be queried, a negative value otherwise; call
  166. * SDL_GetError() for more information.
  167. *
  168. * \since This function is available since SDL 3.0.0.
  169. *
  170. * \sa SDL_OpenTitleStorage
  171. * \sa SDL_OpenUserStorage
  172. * \sa SDL_OpenStorage
  173. * \sa SDL_CloseStorage
  174. * \sa SDL_StorageReady
  175. * \sa SDL_StorageReadFile
  176. * \sa SDL_StorageWriteFile
  177. * \sa SDL_StorageSpaceRemaining
  178. */
  179. extern DECLSPEC int SDLCALL SDL_StorageFileSize(SDL_Storage *storage, const char *path, Uint64 *length);
  180. /**
  181. * Synchronously read a file from a storage container into a client-provided
  182. * buffer.
  183. *
  184. * \param storage a storage container to read from
  185. * \param path the relative path of the file to read
  186. * \param destination a client-provided buffer to read the file into
  187. * \param length the length of the destination buffer
  188. * \returns 0 if the file was read, a negative value otherwise; call
  189. * SDL_GetError() for more information.
  190. *
  191. * \since This function is available since SDL 3.0.0.
  192. *
  193. * \sa SDL_OpenTitleStorage
  194. * \sa SDL_OpenUserStorage
  195. * \sa SDL_OpenStorage
  196. * \sa SDL_CloseStorage
  197. * \sa SDL_StorageReady
  198. * \sa SDL_StorageFileSize
  199. * \sa SDL_StorageWriteFile
  200. * \sa SDL_StorageSpaceRemaining
  201. */
  202. extern DECLSPEC int SDLCALL SDL_StorageReadFile(SDL_Storage *storage, const char *path, void *destination, Uint64 length);
  203. /**
  204. * Synchronously write a file from client memory into a storage container.
  205. *
  206. * \param storage a storage container to write to
  207. * \param path the relative path of the file to write
  208. * \param source a client-provided buffer to write from
  209. * \param length the length of the source buffer
  210. * \returns 0 if the file was written, a negative value otherwise; call
  211. * SDL_GetError() for more information.
  212. *
  213. * \since This function is available since SDL 3.0.0.
  214. *
  215. * \sa SDL_OpenTitleStorage
  216. * \sa SDL_OpenUserStorage
  217. * \sa SDL_OpenStorage
  218. * \sa SDL_CloseStorage
  219. * \sa SDL_StorageReady
  220. * \sa SDL_StorageFileSize
  221. * \sa SDL_StorageReadFile
  222. * \sa SDL_StorageSpaceRemaining
  223. */
  224. extern DECLSPEC int SDL_StorageWriteFile(SDL_Storage *storage, const char *path, const void *source, Uint64 length);
  225. /**
  226. * Queries the remaining space in a storage container.
  227. *
  228. * \param storage a storage container to query
  229. * \returns the amount of remaining space, in bytes
  230. *
  231. * \since This function is available since SDL 3.0.0.
  232. *
  233. * \sa SDL_OpenTitleStorage
  234. * \sa SDL_OpenUserStorage
  235. * \sa SDL_OpenStorage
  236. * \sa SDL_CloseStorage
  237. * \sa SDL_StorageReady
  238. * \sa SDL_StorageFileSize
  239. * \sa SDL_StorageReadFile
  240. * \sa SDL_StorageReadFileAsync
  241. * \sa SDL_StorageWriteFile
  242. * \sa SDL_StorageWriteFileAsync
  243. */
  244. extern DECLSPEC Uint64 SDLCALL SDL_StorageSpaceRemaining(SDL_Storage *storage);
  245. /* Ends C function definitions when using C++ */
  246. #ifdef __cplusplus
  247. }
  248. #endif
  249. #include <SDL3/SDL_close_code.h>
  250. #endif /* SDL_storage_h_ */