Resources - simple-entertainment/simplicity GitHub Wiki
The resources API provides an abstraction layer for interacting with external data e.g. the file system. Resources are in categories instead of folders or other grouping mechanisms. So, to grab a resource, you specify its name and the category it is in:
Resource* badGuyData = Resources::get("bad-guy01.txt", MyCategories::BAD_GUY);
Now that you've got your hands on the resource you can read and write some data!
std::string data = badGuydData->getData(); // Retrieves the contents of the resource.
badGuyData->appendData("You are a very bad man."); // Adds data to the end of the resource.
badGuyData->setData("You are a very good man."); // Overwrites the resource.
Also, you can grab streams for the resource:
std::unique_ptr<istream> inputSteam = badGuyData->getInputStream();
std::unique_ptr<ostream> outputSteam = badGuyData->getOutputStream();
For binary data just specify the resource is binary when retrieving it:
Resource* badGuyBinaryData = Resources::get("bad-guy01.bin", MyCategories::BAD_GUY, true); // <- true
So just how do these files and things magically end up in these strange 'categories' anyway? Well, you put them there:
// Create a data store pointing at the 'bad-guy-files' folder relative to the current working directory.
std::unique_ptr<DataStore> badGuyDataStore(new FileSystemDataStore("bad-guy-files"));
// Bind the data store to the MyCategories::BAD_GUY category.
Resources::setDataStore(move(badGuyDataStore), MyCategories::BAD_GUY);
Also, you can add a default data store that will be used for all categories that don't have a data store explicitly bound to them:
std::unique_ptr<DataStore> someDataStore = // Some data store...
Resources::setDataStore(move(someDataStore), Category::ALL_CATEGORIES);
The two data stores provided with simplicity are:
- ConsoleDataStore - For interacting with the console.
- FileSystemDataStore - For interacting with files in the local file system.
Want to make your own data store? Go for it! Just extend DataStore
.