Start adding support for removing assets

This commit is contained in:
Emil Axelsson
2017-12-08 16:02:45 +01:00
parent 137c4cd16e
commit f3dfade201
5 changed files with 46 additions and 25 deletions

View File

@@ -120,8 +120,6 @@ public:
std::string resolveLocalResource(std::string resourceName);
private:
void handleRequests();
std::atomic<State> _state;
AssetLoader* _loader;
std::vector<std::shared_ptr<ResourceSynchronization>> _synchronizations;

View File

@@ -141,7 +141,7 @@ private:
std::shared_ptr<Asset> loadAsset(std::string path);
std::shared_ptr<Asset> getAsset(std::string path);
ghoul::filesystem::Directory currentDirectory();
ghoul::filesystem::Directory currentDirectory() const;
void pushAsset(std::shared_ptr<Asset> asset);
void popAsset();

View File

@@ -65,14 +65,6 @@ std::string Asset::resolveLocalResource(std::string resourceName) {
resourceName;
}
void Asset::handleRequests() {
State currentState = state();
if (currentState == State::Initialized) {
// ...
}
}
Asset::State Asset::state() const {
return _state;
}
@@ -361,7 +353,7 @@ void Asset::initialize() {
// 6. Ask requested children to initialize if they are not already initialized.
// Initialization may not happen immediately.
for (auto& child : _requestedAssets) {
child->handleRequests();
//child->handleRequests();
}
// 7. Call dependency initialization function of this and the parent
@@ -474,22 +466,34 @@ void Asset::request(std::shared_ptr<Asset> child) {
_requestedAssets.push_back(child);
child->_requestingAssets.push_back(shared_from_this());
child->handleRequests();
// TODO: update real state!
}
void Asset::unrequest(std::shared_ptr<Asset> child) {
auto it = std::find(_requestedAssets.begin(),
auto childIt = std::find(
_requestedAssets.begin(),
_requestedAssets.end(),
child);
if (it != _requestedAssets.end()) {
// Do nothing if the request already exists.
auto parentIt = std::find_if(
child->_requestingAssets.begin(),
child->_requestingAssets.end(),
[this](std::weak_ptr<Asset> a) {
return a.lock().get() == this;
}
);
if (childIt == _requestedAssets.end() ||
parentIt == child->_requestingAssets.end())
{
// Do nothing if the request node not exist.
return;
}
_requestedAssets.push_back(child);
child->_requestingAssets.push_back(shared_from_this());
child->handleRequests();
_requestedAssets.erase(childIt);
child->_requestingAssets.erase(parentIt);
// TODO: update real state!
}
bool Asset::requests(const Asset* asset) const {

View File

@@ -202,7 +202,22 @@ std::shared_ptr<Asset> AssetLoader::request(const std::string& name) {
return nullptr;
}
ghoul::filesystem::Directory AssetLoader::currentDirectory() {
void AssetLoader::unrequest(const std::string& name) {
std::shared_ptr<Asset> asset = has(name);
if (asset) {
try {
std::shared_ptr<Asset> parent = _assetStack.back();
parent->unrequest(asset);
}
catch (ghoul::RuntimeError& e) {
LERROR("Failed to unload " << name << ". " << e.component << " :" << e.message);
}
}
}
ghoul::filesystem::Directory AssetLoader::currentDirectory() const {
if (_assetStack.back()->hasAssetFile()) {
return _assetStack.back()->assetDirectory();
} else {
@@ -216,8 +231,9 @@ std::shared_ptr<Asset> AssetLoader::add(const std::string& identifier) {
}
void AssetLoader::remove(const std::string & identifier) {
void AssetLoader::remove(const std::string& identifier) {
ghoul_assert(_assetStack.size() == 1, "Can only unload an asset from the root asset");
unrequest(identifier);
// TODO: Implement this
//_rootAsset->removeDependency(id);
}
@@ -228,8 +244,11 @@ void AssetLoader::remove(const Asset* asset) {
//_rootAsset->removeDependency(id);
}
std::shared_ptr<Asset> AssetLoader::has(const std::string& identifier) const {
const auto it = _loadedAssets.find(identifier);
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()) {
return nullptr;
}

View File

@@ -75,7 +75,7 @@ bool AssetManager::update() {
for (const auto& c : _pendingStateChangeCommands) {
const std::string& path = c.first;
const bool add = c.second;
if (add && !_assetLoader->has(path)) {
if (add) {
std::shared_ptr<Asset> asset = tryAddAsset(path);
}
}
@@ -83,7 +83,7 @@ bool AssetManager::update() {
// Remove assets
for (const auto& c : _pendingStateChangeCommands) {
const std::string& path = c.first;
const bool remove = c.second;
const bool remove = !c.second;
if (remove && _assetLoader->has(path)) {
tryRemoveAsset(path);
}