SLProject  4.3.020
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
SLAssimpIOSystem.h
Go to the documentation of this file.
1 /**
2  * \file SLAssimpIOSystem.h
3  * \brief Adapters that let Assimp read through SLProject's SLIOStream
4  * \date May 2024
5  * \authors Marino von Wattenwyl
6  * \copyright http://opensource.org/licenses/GPL-3.0
7  * \remarks Please use clangformat to format the code. See more code style on
8  * https://github.com/cpvrlab/SLProject4/wiki/SLProject-Coding-Style
9  */
10 
11 #ifndef SLPROJECT_SLASSIMPIOSYSTEM_H
12 #define SLPROJECT_SLASSIMPIOSYSTEM_H
13 
14 #include <assimp/IOStream.hpp>
15 #include <assimp/IOSystem.hpp>
16 #include "SLFileStorage.h"
17 
18 //-----------------------------------------------------------------------------
19 //! Adapts a single SLIOStream to the stream interface Assimp expects
20 /*! Assimp reads model files through its own Assimp::IOStream abstraction.
21 This class forwards each of those calls to an SLIOStream, so that Assimp reads
22 through the same storage layer as the rest of SLProject. That matters on
23 Emscripten in particular, where an asset is not a file on disk but data
24 fetched over the network or held in browser storage. */
25 class SLAssimpIOStream : public Assimp::IOStream
26 {
27 public:
28  //! \param stream Stream to read from; ownership stays with the caller
30 
31  //! Reads pCount records of pSize bytes into pvBuffer, returns records read
32  size_t Read(void* pvBuffer, size_t pSize, size_t pCount) override;
33 
34  //! Writes pCount records of pSize bytes from pvBuffer, returns bytes written
35  size_t Write(const void* pvBuffer, size_t pSize, size_t pCount) override;
36 
37  //! Moves the read position to pOffset relative to pOrigin
38  aiReturn Seek(size_t pOffset, aiOrigin pOrigin) override;
39 
40  //! Returns the current read position in bytes
41  size_t Tell() const override;
42 
43  //! Returns the total size of the stream in bytes
44  size_t FileSize() const override;
45 
46  //! Flushes the wrapped stream
47  void Flush() override;
48 
49  //! Returns the wrapped SLProject stream
50  SLIOStream* stream() { return _stream; }
51 
52 private:
53  SLIOStream* _stream; //!< Wrapped SLProject stream, not owned
54 };
55 //-----------------------------------------------------------------------------
56 //! Assimp file system handler backed by SLFileStorage
57 /*! Installed on the Assimp importer with SetIOHandler in SLAssimpImporter::load,
58 so that every file Assimp opens is resolved through SLFileStorage rather than
59 the C runtime. Paths are looked up with the IOK_model file kind. */
60 class SLAssimpIOSystem : public Assimp::IOSystem
61 {
62 public:
63  //! Returns true if pFile can be opened through SLFileStorage
64  bool Exists(const char* pFile) const override;
65 
66  //! Returns the path separator, always '/' for SLProject paths
67  char getOsSeparator() const override;
68 
69  //! Opens pFile through SLFileStorage and wraps it in an SLAssimpIOStream
70  /*! \param pFile Path of the file to open
71  \param pMode Assimp mode string ("rb", "r", "rt", "wb", "w", "wt").
72  Only the first character is inspected, because an
73  SLIOStream is always binary. */
74  Assimp::IOStream* Open(const char* pFile, const char* pMode) override;
75 
76  //! Closes the stream and deletes the adapter created by Open
77  void Close(Assimp::IOStream* pFile) override;
78 };
79 //-----------------------------------------------------------------------------
80 #endif // SLPROJECT_SLASSIMPIOSYSTEM_H
Adapts a single SLIOStream to the stream interface Assimp expects.
void Flush() override
Flushes the wrapped stream.
aiReturn Seek(size_t pOffset, aiOrigin pOrigin) override
Moves the read position to pOffset relative to pOrigin.
size_t FileSize() const override
Returns the total size of the stream in bytes.
SLAssimpIOStream(SLIOStream *stream)
SLIOStream * stream()
Returns the wrapped SLProject stream.
size_t Read(void *pvBuffer, size_t pSize, size_t pCount) override
Reads pCount records of pSize bytes into pvBuffer, returns records read.
size_t Write(const void *pvBuffer, size_t pSize, size_t pCount) override
Writes pCount records of pSize bytes from pvBuffer, returns bytes written.
SLIOStream * _stream
Wrapped SLProject stream, not owned.
size_t Tell() const override
Returns the current read position in bytes.
Assimp file system handler backed by SLFileStorage.
Assimp::IOStream * Open(const char *pFile, const char *pMode) override
Opens pFile through SLFileStorage and wraps it in an SLAssimpIOStream.
char getOsSeparator() const override
Returns the path separator, always '/' for SLProject paths.
bool Exists(const char *pFile) const override
Returns true if pFile can be opened through SLFileStorage.
void Close(Assimp::IOStream *pFile) override
Closes the stream and deletes the adapter created by Open.
Interface for accessing external data using streams.
Definition: SLFileStorage.h:62