Asset loading and synchronization

This commit is contained in:
Emil Axelsson
2017-11-16 19:29:45 +01:00
parent 7c80384d12
commit 436062381e
9 changed files with 339 additions and 304 deletions
+31 -13
View File
@@ -33,7 +33,7 @@
#include <vector>
#include <memory>
#include <set>
#include <unordered_set>
#include <iterator>
namespace openspace {
@@ -76,28 +76,40 @@ public:
void setState(State state);
void addSynchronization(std::shared_ptr<ResourceSynchronization> synchronization);
std::vector<std::shared_ptr<ResourceSynchronization>> synchronizations();
std::vector<std::shared_ptr<Asset>> allAssets();
// Sync
bool startSynchronizations();
bool cancelSynchronizations();
bool restartSynchronizations();
float synchronizationProgress();
// Init
bool isInitReady() const;
void initialize();
void deinitialize();
bool hasDependency(const Asset* asset) const;
void addDependency(std::shared_ptr<Asset> asset);
void removeDependency(Asset* asset);
void removeDependency(const std::string& assetId);
std::vector<std::shared_ptr<Asset>> dependencies();
// Dependency graph
bool requires(const Asset* asset) const;
void require(std::shared_ptr<Asset> asset);
bool hasDependants() const;
bool hasInitializedDependants() const;
bool requests(const Asset* child) const;
void request(std::shared_ptr<Asset> child);
void unrequest(std::shared_ptr<Asset> child);
std::vector<std::shared_ptr<Asset>> requiredSubTreeAssets();
std::vector<std::shared_ptr<Asset>> subTreeAssets();
std::vector<std::shared_ptr<Asset>> requestedAssets();
std::vector<std::shared_ptr<Asset>> requiredAssets();
std::vector<std::shared_ptr<Asset>> childAssets();
bool isRequired() const;
bool isRequested() const;
std::string resolveLocalResource(std::string resourceName);
private:
void handleRequests();
void startSync(ResourceSynchronization& rs);
void cancelSync(ResourceSynchronization& rs);
@@ -112,11 +124,17 @@ private:
// Absolute path to asset file
std::string _assetPath;
// Dependencies
std::vector<std::shared_ptr<Asset>> _dependencies;
// Required assets
std::vector<std::shared_ptr<Asset>> _requiredAssets;
// Assets that refers to this asset as an dependency
std::vector<std::weak_ptr<Asset>> _dependants;
// Assets that refers to this asset as a required asset
std::vector<std::weak_ptr<Asset>> _requiringAssets;
// Requested assets
std::vector<std::shared_ptr<Asset>> _requestedAssets;
// Assets that refers to this asset as a requested asset
std::vector<std::weak_ptr<Asset>> _requestingAssets;
};
} // namespace openspace
+17 -23
View File
@@ -71,43 +71,34 @@ public:
~AssetLoader();
/**
* Load an asset:
* Add the asset as a dependency on the root asset
* Add the asset as a request for the root asset
* The asset is loaded synchronously
*/
std::shared_ptr<Asset> loadAsset(const std::string& identifier);
std::shared_ptr<Asset> add(const std::string& identifier);
/**
* Unload an asset:
* Remove the asset as a dependency on the root asset
* The asset is unloaded synchronously
*/
void unloadAsset(const std::string& identifier);
void remove(const std::string& identifier);
/**
* Unload an asset:
* Remove the asset as a dependency on the root asset
* The asset is unloaded synchronously
*/
void unloadAsset(const Asset* asset);
void remove(const Asset* asset);
/**
* Return true if the specified asset is loaded
*/
bool hasLoadedAsset(const std::string& identifier);
/**
* Returns the asset identified by the identifier,
* if the asset is loaded. Otherwise return nullptr.
*/
std::shared_ptr<Asset> loadedAsset(const std::string& identifier);
* Returns the asset identified by the identifier,
* if the asset is loaded. Otherwise return nullptr.
*/
std::shared_ptr<Asset> has(const std::string& identifier);
/**
* Return all assets loaded using the loadAsset method.
* Non-recursive (does not include imports of the loaded assets)
*/
std::vector<std::shared_ptr<Asset>> loadedAssets();
//std::vector<std::shared_ptr<Asset>> loadedAssets();
/**
* Return the lua state
@@ -128,15 +119,18 @@ public:
void callOnDeinitialize(Asset* asset);
void callOnDependantInitialize(Asset* asset, Asset* dependant);
void callOnDependencyInitialize(Asset* asset, Asset* dependant);
void callOnDependantDeinitialize(Asset* asset, Asset* dependant);
void callOnDependencyDeinitialize(Asset* asset, Asset* dependant);
std::string generateAssetPath(const std::string& baseDirectory, const std::string& path) const;
private:
std::shared_ptr<Asset> importDependency(const std::string& identifier);
std::shared_ptr<Asset> importAsset(std::string path);
std::shared_ptr<Asset> require(const std::string& identifier);
std::shared_ptr<Asset> request(const std::string& path);
void unrequest(const std::string& path);
std::shared_ptr<Asset> loadAsset(std::string path);
std::shared_ptr<Asset> getAsset(std::string path);
ghoul::filesystem::Directory currentDirectory();
@@ -166,7 +160,7 @@ private:
friend int assetloader::exportAsset(lua_State* state);
std::shared_ptr<Asset> _rootAsset;
std::map<std::string, std::shared_ptr<Asset>> _importedAssets;
std::map<std::string, std::shared_ptr<Asset>> _loadedAssets;
std::vector<std::shared_ptr<Asset>> _assetStack;
std::string _assetRootDirectory;
+15 -15
View File
@@ -57,34 +57,34 @@ public:
std::unique_ptr<AssetLoader> loader
);
struct ManagedAsset {
std::string path;
std::shared_ptr<Asset> asset;
};
void initialize();
void deinitialize();
void add(const std::string& path);
void remove(const std::string& path);
void removeAll();
std::shared_ptr<Asset> rootAsset();
bool update();
void setTargetAssetState(const std::string& path, Asset::State targetState);
void setTargetAssetState(Asset* asset, Asset::State targetState);
Asset::State currentAssetState(Asset* asset);
Asset::State currentAssetState(const std::string& path);
void clearAllTargetAssets();
std::vector<std::shared_ptr<Asset>> loadedAssets();
//void setTargetAssetState(const std::string& path, Asset::State targetState);
//void setTargetAssetState(Asset* asset, Asset::State targetState);
//Asset::State currentAssetState(Asset* asset);
//Asset::State currentAssetState(const std::string& path);
//void clearAllTargetAssets();
//std::vector<std::shared_ptr<Asset>> loadedAssets();
scripting::LuaLibrary luaLibrary();
bool isDone();
private:
std::shared_ptr<Asset> tryLoadAsset(const std::string& path);
std::shared_ptr<Asset> tryAddAsset(const std::string& path);
bool tryRemoveAsset(const std::string& path);
void unloadAsset(Asset* asset);
bool tryInitializeAsset(Asset& asset);
bool tryDeinitializeAsset(Asset& asset);
void startSynchronization(Asset& asset);
void cancelSynchronization(Asset& asset);
std::vector<ManagedAsset> _managedAssets;
std::unordered_map<std::string, Asset::State> _pendingStateChangeCommands;
std::unordered_map<std::string, bool> _pendingStateChangeCommands;
//std::unordered_map<Asset*, AssetState> _stateChangesInProgress;
std::unordered_set<Asset*> _pendingInitializations;