RtB/src/AssetManager.cpp

71 lines
2.1 KiB
C++

/* ===============================================================
AssetManager
----------------
TODO: On load, the APP AssetCache is loaded. The location of this Cache varies between platforms:
* Android - assets archive, accessed via apk*
* iOS/OSX - app Resources dir
* Linux - global install directory? dunno
* Windows - install dir?
Following this, the USER AssetCache is loaded, and are:
* Android - <Application data folder>/assets
* iOS - <app Documents?>/assets
* OSX - ~/Library/Application Support/RtB/assets
* Linux - ~/.RtB/assets
================================================================ */
#include "AssetManager.hpp"
#include "Log.hpp"
#include "fio.hpp"
#include "checksum.hpp"
#include <stdexcept> // for std::out_of_range
AssetManager::AssetManager() {
null_asset.filename = "NULL";
}
AssetManager::~AssetManager() {
AssetCache *cache;
int i;
for (i = caches.size()-1; i >= 0; i--) {
try {
cache = caches.at(i);
} catch (const std::out_of_range& oor) {
LOG(LOG_ERROR) << FUNC_NAME << ": cache index out of range: " << oor.what();
continue;
}
// FIXME: should we even save the caches from here?
// save the cache to its proper CACHE file
char cachepath[1024];
#ifndef _WIN32
sprintf(cachepath, "%s/.CACHE", cache->directory.c_str());
#else
sprintf_s(cachepath, 1024, "%s/.CACHE", cache->directory.c_str());
#endif
cache->toFile(cachepath);
// annd delete!
delete cache;
}
}
/* ======== Asset Loading ======== */
Asset* AssetManager::loadFile(const char *filename) {
Asset *asset = NULL;
AssetCache *cache = NULL;
int i;
for (i = caches.size()-1; i >= 0; i--) {
try {
cache = caches.at(i);
} catch (const std::out_of_range& oor) {
LOG(LOG_ERROR) << FUNC_NAME << ": cache index out of range: " << oor.what();
continue;
}
if ((asset = cache->loadAsset(filename)) != NULL) return asset;
}
// if we got to here, we failed to find an asset
asset = &null_asset;
return asset;
}
/* ======== Asset Cache ======== */
int AssetManager::addCache(AssetCache *cache) {
caches.push_back(cache);
return 0;
}