This commit is contained in:
Emil Axelsson
2017-12-12 15:21:17 +01:00
parent 1417db5ed7
commit d1305bee6b
5 changed files with 45 additions and 27 deletions

View File

@@ -80,23 +80,28 @@ public:
~AssetLoader();
/**
* Add the asset as a request for the root asset
* The asset is loaded synchronously
* Add the asset as a request of the root asset
*/
std::shared_ptr<Asset> add(const std::string& identifier);
/**
* Remove the asset as a dependency on the root asset
* The asset is unloaded synchronously
* Remove the asset as a request of the root asset
*/
void remove(const std::string& identifier);
/**
* Enable the asset to be reused when the same path is required/requested again
*/
void trackAsset(std::shared_ptr<Asset> asset);
/**
* Disable the asset from being reused when the same path is required/requested again
*/
void untrackAsset(Asset* asset);
/**
* Returns the asset identified by the identifier,
* if the asset is loaded. Otherwise return nullptr.
* Return the asset identified by the identifier,
* if the asset is tracked. Otherwise return nullptr.
*/
std::shared_ptr<Asset> has(const std::string& identifier) const;
@@ -115,20 +120,37 @@ public:
*/
const std::string& assetRootDirectory() const;
/**
* Load an asset
*/
bool loadAsset(std::shared_ptr<Asset> asset);
/**
* Call the onInitialize function specified in the asset file
*/
void callOnInitialize(Asset* asset);
/**
* Call the onDeinitialize function specified in the asset file
*/
void callOnDeinitialize(Asset* asset);
void callOnDependencyInitialize(Asset* asset, Asset* dependant);
/**
* Call the dependency.onInitialize function specified in the asset file
*/
void callOnDependencyInitialize(Asset* dependency, Asset* asset);
void callOnDependencyDeinitialize(Asset* asset, Asset* dependant);
/**
* Call the dependency.onDeinitialize function specified in the asset file
*/
void callOnDependencyDeinitialize(Asset* dependency, Asset* asset);
/**
* Generate the absolute path for an asset specified as `path` relative to `baseDirectory`
*/
std::string generateAssetPath(const std::string& baseDirectory,
const std::string& path) const;
/**
* Add listener to asset state changes
*/
@@ -159,9 +181,6 @@ private:
std::shared_ptr<Asset> request(const std::string& path);
void unrequest(const std::string& path);
/**
* Add the global assets table to the lua stack.
*/
void setUpAssetLuaTable(Asset* asset);
void tearDownAssetLuaTable(Asset* asset);
@@ -193,12 +212,11 @@ private:
friend int assetloader::syncedResource(lua_State* state);
friend int assetloader::exportAsset(lua_State* state);
// Member variables
std::shared_ptr<Asset> _rootAsset;
std::shared_ptr<Asset> _currentAsset;
std::unordered_map<std::string, std::weak_ptr<Asset>> _loadedAssets;
std::unordered_map<std::string, std::weak_ptr<Asset>> _trackedAssets;
SynchronizationWatcher* _synchronizationWatcher;
std::string _assetRootDirectory;
ghoul::lua::LuaState* _luaState;

View File

@@ -79,8 +79,8 @@ private:
std::mutex _pendingInitializationsMutex;
std::vector<std::shared_ptr<Asset>> _pendingInitializations;
std::unique_ptr<AssetLoader> _assetLoader;
std::unique_ptr<SynchronizationWatcher> _synchronizationWatcher;
std::unique_ptr<AssetLoader> _assetLoader;
};
} // namespace openspace

View File

@@ -407,7 +407,7 @@ void Asset::initialize() {
// 5. Call dependency initialization function of the child and this
// if the requested child was initialized before this.
for (auto& child : _requestedAssets) {
if (child->state() == State::Initialized) {
if (child->isInitialized()) {
try {
loader()->callOnDependencyInitialize(child.get(), this);
} catch (const ghoul::lua::LuaRuntimeException& e) {

View File

@@ -93,13 +93,13 @@ AssetLoader::~AssetLoader() {
}
void AssetLoader::trackAsset(std::shared_ptr<Asset> asset) {
_loadedAssets.emplace(asset->id(), asset);
_trackedAssets.emplace(asset->id(), asset);
}
void AssetLoader::untrackAsset(Asset* asset) {
auto it = _loadedAssets.find(asset->id());
if (it != _loadedAssets.end()) {
_loadedAssets.erase(it);
auto it = _trackedAssets.find(asset->id());
if (it != _trackedAssets.end()) {
_trackedAssets.erase(it);
}
}
@@ -252,9 +252,9 @@ std::shared_ptr<Asset> AssetLoader::getAsset(std::string name) {
std::string path = generateAssetPath(directory, name);
// Check if asset is already loaded.
const auto it = _loadedAssets.find(path);
const auto it = _trackedAssets.find(path);
if (it != _loadedAssets.end()) {
if (it != _trackedAssets.end()) {
std::shared_ptr<Asset> a = it->second.lock();
if (a != nullptr) {
return a;
@@ -343,8 +343,8 @@ std::shared_ptr<Asset> AssetLoader::has(const std::string& name) const {
ghoul::filesystem::Directory directory = currentDirectory();
std::string path = generateAssetPath(directory, name);
const auto it = _loadedAssets.find(path);
if (it == _loadedAssets.end()) {
const auto it = _trackedAssets.find(path);
if (it == _trackedAssets.end()) {
return nullptr;
}
return it->second.lock();

View File

@@ -41,8 +41,8 @@ AssetManager::AssetManager(
std::unique_ptr<AssetLoader> loader,
std::unique_ptr<SynchronizationWatcher> syncWatcher
)
: _assetLoader(std::move(loader))
, _synchronizationWatcher(std::move(syncWatcher))
: _synchronizationWatcher(std::move(syncWatcher))
, _assetLoader(std::move(loader))
{}
void AssetManager::initialize() {