Compile fix in asset and assetloader

This commit is contained in:
Emil Axelsson
2017-09-15 11:13:46 +02:00
parent af8bec1441
commit bf45937619
7 changed files with 97 additions and 74 deletions

View File

@@ -39,12 +39,13 @@ class AssetLoader;
class Asset : public properties::PropertyOwner {
public:
struct Optional : public properties::PropertyOwner {
class Optional : public properties::PropertyOwner {
public:
Optional(Asset* asset, Asset* owner, bool enabled = false);
private:
properties::BoolProperty _enabled;
Asset* asset;
Asset* _asset;
Asset* _owner;
};
enum class ReadyState : unsigned int {
@@ -64,29 +65,30 @@ public:
*/
Asset(AssetLoader* loader, ghoul::filesystem::Directory baseDirectory, std::string assetPath);
std::string id();
std::string assetFilePath();
std::string assetName();
std::string assetDirectory();
AssetLoader* loader();
std::string syncDirectory();
ReadyState readyState();
std::string id() const;
std::string assetFilePath() const;
std::string assetName() const;
std::string assetDirectory() const;
AssetLoader* loader() const;
std::string syncDirectory() const;
ReadyState readyState() const;
bool isInitReady() const;
void initialize();
void deinitialize();
bool hasDependency(Asset* asset);
bool hasDependency(const Asset* asset) const;
void addDependency(Asset* asset);
void removeDependency(Asset* asset);
void removeDependency(const std::string& assetId);
bool hasDependants();
bool hasInitializedDependants();
bool hasDependants() const;
bool hasInitializedDependants() const;
bool hasOptional(Asset* asset);
bool setOptionalEnabled(Asset* asset, bool enabled);
bool addOptional(Asset* asset, bool enabled);
bool removeOptional(Asset* asset);
bool hasOptional(Asset* asset) const;
bool optionalIsEnabled(Asset* asset) const;
void setOptionalEnabled(Asset* asset, bool enabled);
void addOptional(Asset* asset, bool enabled);
void removeOptional(Asset* asset);
void dependantDidInitialize(Asset* dependant);
void dependantWillDeinitialize(Asset* dependant);
@@ -96,10 +98,14 @@ public:
bool shouldSynchronize();
bool shouldInitialize();
private:
std::string resolveLocalResource(std::string resourceName);
std::string resolveSyncedResource(std::string resourceName);
static std::string generateAssetId(std::string directory, std::string name);
private:
ReadyState _readyState;
AssetLoader* _loader;
@@ -109,6 +115,9 @@ private:
// Asbolute path to directory with the .asset file
std::string _assetDirectory;
// Asset id
std::string _id;
// Asset dependencies
std::vector<Asset*> _dependencies;

View File

@@ -92,7 +92,7 @@ public:
/**
* Return the root asset
*/
Asset* rootAsset();
Asset* rootAsset() const;
/**
* Return the sync root directory
@@ -126,14 +126,18 @@ private:
ResourceSynchronizer* _resourceSynchronizer;
std::string _syncRootDirectory;
friend int assetloader::importDependency(lua_State* state);
friend int assetloader::importOptional(lua_State* state);
int importAssetLua(
std::string assetName,
bool togglableInitializationRequirement = false,
bool toggleOn = true);
int importDependencyLua(std::string assetName);
int importOptionalLua(std::string assetName, bool enabled);
int resolveLocalResourceLua(Asset* asset);
int resolveSyncedResourceLua(Asset* asset);
int createLuaTableEntries(const Asset* importer, const Asset* importedAsset);
ghoul::lua::LuaState* _luaState;
friend int assetloader::importDependency(lua_State* state);
friend int assetloader::importOptional(lua_State* state);
friend int assetloader::resolveLocalResource(lua_State* state);
friend int assetloader::resolveSyncedResource(lua_State* state);
};

View File

@@ -1,4 +1,4 @@
/*****************************************************************************************
/*****************************************************************************************
* *
* OpenSpace *
* *
@@ -37,6 +37,7 @@
#include <openspace/rendering/screenspacerenderable.h>
#include <openspace/scene/scene.h>
#include <openspace/scene/scenegraphnode.h>
#include <openspace/scene/asset.h>
#include <openspace/scene/assetloader.h>
#include <ghoul/logging/logmanager.h>

View File

@@ -45,12 +45,13 @@
#include <openspace/scripting/scriptscheduler.h>
#include <openspace/scripting/scriptengine.h>
#include <openspace/scene/asset.h>
#include <openspace/scene/assetloader.h>
#include <openspace/scene/scene.h>
#include <openspace/scene/rotation.h>
#include <openspace/scene/scale.h>
#include <openspace/scene/translation.h>
#include <openspace/scene/sceneloader.h>
#include <openspace/scene/assetloader.h>
#include <openspace/util/resourcesynchronization.h>
#include <openspace/util/factorymanager.h>
@@ -145,7 +146,7 @@ OpenSpaceEngine::OpenSpaceEngine(std::string programName,
, _networkEngine(new NetworkEngine)
, _parallelConnection(new ParallelConnection)
, _renderEngine(new RenderEngine)
, _resourceSynchronizer(new ResourceSynchronizer(8))
, _resourceSynchronizer(new ResourceSynchronizer)
, _settingsEngine(new SettingsEngine)
, _syncEngine(std::make_unique<SyncEngine>(4096))
, _timeManager(new TimeManager)

View File

@@ -39,6 +39,8 @@ namespace {
}
return false;
};
const char* AssetFileSuffix = "asset";
}
namespace openspace {
@@ -48,7 +50,7 @@ std::string Asset::resolveLocalResource(std::string resourceName) {
return currentAssetDirectory + ghoul::filesystem::FileSystem::PathSeparator + resourceName;
}
std::string Asset::syncDirectory() {
std::string Asset::syncDirectory() const {
std::string currentAssetDirectory = assetDirectory();
std::string rootAssetDirectory = loader()->rootAsset()->assetDirectory();
std::string relativePath = FileSys.relativePath(currentAssetDirectory, rootAssetDirectory);
@@ -60,7 +62,7 @@ std::string Asset::syncDirectory() {
assetName();
}
Asset::ReadyState Asset::readyState() {
Asset::ReadyState Asset::readyState() const {
return _readyState;
}
@@ -138,7 +140,9 @@ Asset::Asset(AssetLoader* loader, ghoul::filesystem::Directory directory)
, _assetDirectory(directory)
, _loader(loader)
, _readyState(Asset::ReadyState::Loaded)
{}
{
_id = generateAssetId(directory, "");
}
Asset::Asset(AssetLoader* loader, ghoul::filesystem::Directory baseDirectory, std::string assetPath)
: PropertyOwner({ assetPath, assetPath })
@@ -167,36 +171,41 @@ Asset::Asset(AssetLoader* loader, ghoul::filesystem::Directory baseDirectory, st
_assetDirectory = assetFile.directoryName();
_assetName = assetFile.baseName();
}
_id = generateAssetId(_assetDirectory, _assetName);
}
std::string Asset::assetFilePath() {
ghoul::filesystem::File dir(_assetDirectory);
std::string Asset::assetFilePath() const {
//ghoul::filesystem::File dir(_assetDirectory);
return _assetDirectory + ghoul::filesystem::FileSystem::PathSeparator + _assetName + "." + AssetFileSuffix;
}
std::string Asset::assetName() {
std::string Asset::generateAssetId(std::string directory, std::string name) {
return directory + ghoul::filesystem::FileSystem::PathSeparator + name;
}
std::string Asset::assetName() const {
return _assetName;
}
std::string Asset::assetDirectory() {
std::string Asset::assetDirectory() const {
return _assetDirectory;
}
std::string Asset::id() {
return assetFilePath();
std::string Asset::id() const {
return _id;
}
AssetLoader* Asset::loader() {
AssetLoader* Asset::loader() const {
return _loader;
}
bool Asset::hasDependency(Asset* asset) {
auto it = std::find(_dependencies.begin(), _dependencies.end(), dependency);
bool Asset::hasDependency(const Asset* asset) const {
const auto it = std::find(_dependencies.begin(), _dependencies.end(), asset);
return it != _dependencies.end();
}
void Asset::addDependency(Asset* dependency) {
if (readyState == Initialized) {
if (_readyState == Asset::ReadyState::Initialized) {
// TODO: Throw: cannot add dep while asset is initialized.
return;
}
@@ -230,11 +239,11 @@ void Asset::removeDependency(Asset* dependency) {
}
void Asset::removeDependency(const std::string& assetId) {
auto dep = std::find_if(_dependencies.begin(), _dependencies.end(), [&assetId](const Asset& d) {
return d.id() == assetId;
auto dep = std::find_if(_dependencies.begin(), _dependencies.end(), [&assetId](const Asset* d) {
return d->id() == assetId;
});
if (dep != _dependencies.end()) {
removeDependency(dep);
removeDependency(*dep);
} else {
LERROR("No such dependency '" << assetId << "'");
}
@@ -248,9 +257,9 @@ void Asset::dependantWillDeinitialize(Asset* dependant) {
loader()->callOnDependantDeinitialize(this, dependant);
}
bool Asset::hasDependants() {
bool Asset::hasDependants() const {
bool foundDep = false;
for (auto& dependant : _dependants) {
for (const auto& dependant : _dependants) {
if (dependant->hasDependency(this)) {
foundDep = true;
}
@@ -258,10 +267,10 @@ bool Asset::hasDependants() {
return foundDep;
}
bool Asset::hasInitializedDependants() {
bool Asset::hasInitializedDependants() const {
bool foundInitializedDep = false;
for (auto& dependant : _dependants) {
if (dependant->isInitialized() && dependant->hasDependency(this)) {
for (const auto& dependant : _dependants) {
if (dependant->readyState() == Asset::ReadyState::Initialized && dependant->hasDependency(this)) {
foundInitializedDep = true;
}
}
@@ -270,7 +279,7 @@ bool Asset::hasInitializedDependants() {
// Dependency toggle
Asset::Optional::Optional(Asset* asset, Asset* owner, bool enabled)
: PropertyOwner({ dependency->name(), dependency->name() })
: PropertyOwner({ asset->name(), asset->name() })
, _enabled({ "enabled", "Enabled", "Enable optional" }, enabled)
, _asset(asset)
, _owner(owner)
@@ -279,7 +288,7 @@ Asset::Optional::Optional(Asset* asset, Asset* owner, bool enabled)
addPropertySubOwner(asset);
_enabled.onChange([this]() {
_owner->setOptionalEnabled(
asset,
_asset,
_enabled
);
});

View File

@@ -37,9 +37,7 @@
#include "assetloader_lua.inl"
namespace {
const char* AssetFileSuffix = "asset";
const char* AssetGlobalVariableName = "asset";
const char* DataFileSuffix = "data";
const char* SyncTableName = "sync";
const char* ImportFunctionName = "import";
@@ -148,7 +146,9 @@ Asset* AssetLoader::loadAsset(std::string name) {
ghoul::filesystem::Directory directory = currentDirectory();
std::unique_ptr<Asset> asset = std::make_unique<Asset>(this, directory, name);
pushAsset(asset);
Asset* rawAsset = asset.get();
pushAsset(rawAsset);
ghoul::OnScopeExit e([this]() {
popAsset();
});
@@ -170,15 +170,14 @@ Asset* AssetLoader::loadAsset(std::string name) {
return nullptr;
}
Asset* rawAsset = asset.get();
_importedAssets.emplace(id, std::move(asset));
_importedAssets.emplace(asset->id(), std::move(asset));
return rawAsset;
}
Asset* AssetLoader::getAsset(std::string name) {
ghoul::filesystem::Directory directory = currentDirectory();
std::string assetId = generateAssetId(directory, name);
std::string assetId = Asset::generateAssetId(directory, name);
// Check if asset is already loaded.
const auto it = _importedAssets.find(assetId);
@@ -187,14 +186,14 @@ Asset* AssetLoader::getAsset(std::string name) {
return loaded ? it->second.get() : loadAsset(name);
}
AssetLoader::Asset* AssetLoader::importDependency(const std::string& name) {
Asset* AssetLoader::importDependency(const std::string& name) {
Asset* asset = getAsset(name);
Asset* dependant = _assetStack.back();
dependant->addDependency(asset);
return asset;
}
AssetLoader::Asset* AssetLoader::importOptional(const std::string& name, bool enabled) {
Asset* AssetLoader::importOptional(const std::string& name, bool enabled) {
Asset* asset = getAsset(name);
Asset* owner = _assetStack.back();
owner->addOptional(asset, enabled);
@@ -232,7 +231,7 @@ ghoul::lua::LuaState* AssetLoader::luaState() {
return _luaState;
}
AssetLoader::Asset* AssetLoader::rootAsset() {
Asset* AssetLoader::rootAsset() const {
return _rootAsset.get();
}
@@ -249,7 +248,7 @@ void AssetLoader::callOnInitialize(Asset * asset) {
lua_getfield(*_luaState, -1, OnInitializeFunctionName);
const int status = lua_pcall(*_luaState, 0, 0, 0);
if (status != LUA_OK) {
throw ghoul::lua::LuaExecutionException(lua_tostring(*state, -1));
throw ghoul::lua::LuaExecutionException(lua_tostring(*_luaState, -1));
}
}
@@ -262,7 +261,7 @@ void AssetLoader::callOnDeinitialize(Asset* asset) {
lua_getfield(*_luaState, -1, OnDeinitializeFunctionName);
const int status = lua_pcall(*_luaState, 0, 0, 0);
if (status != LUA_OK) {
throw ghoul::lua::LuaExecutionException(lua_tostring(*state, -1));
throw ghoul::lua::LuaExecutionException(lua_tostring(*_luaState, -1));
}
}
@@ -277,7 +276,7 @@ void AssetLoader::callOnDependantInitialize(Asset* asset, Asset* dependant) {
lua_getfield(*_luaState, -1, OnInitializeFunctionName);
const int status = lua_pcall(*_luaState, 0, 0, 0);
if (status != LUA_OK) {
throw ghoul::lua::LuaLoadingException(lua_tostring(*state, -1));
throw ghoul::lua::LuaLoadingException(lua_tostring(*_luaState, -1));
}
}
@@ -293,41 +292,41 @@ void AssetLoader::callOnDependantDeinitialize(Asset* asset, Asset* dependant) {
lua_getfield(*_luaState, -1, OnDeinitializeFunctionName);
const int status = lua_pcall(*_luaState, 0, 0, 0);
if (status != LUA_OK) {
throw ghoul::lua::LuaLoadingException(lua_tostring(*state, -1));
throw ghoul::lua::LuaLoadingException(lua_tostring(*_luaState, -1));
}
}
int AssetLoader::resolveLocalResourceLua(Asset* asset) {
int nArguments = lua_gettop(*state);
int nArguments = lua_gettop(*_luaState);
if (nArguments != 1) {
return luaL_error(*state, "Expected %i arguments, got %i", 1, nArguments);
return luaL_error(*_luaState, "Expected %i arguments, got %i", 1, nArguments);
}
std::string resourceName = luaL_checkstring(*_luaState, -1);
std::string resolved = asset->resolveLocalResource(resourceName);
lua_pushstring(*state, resolved.c_str());
lua_pushstring(*_luaState, resolved.c_str());
return 1;
}
int AssetLoader::resolveSyncedResourceLua(Asset* asset) {
int nArguments = lua_gettop(*state);
int nArguments = lua_gettop(*_luaState);
if (nArguments != 1) {
return luaL_error(*state, "Expected %i arguments, got %i", 1, nArguments);
return luaL_error(*_luaState, "Expected %i arguments, got %i", 1, nArguments);
}
std::string resourceName = luaL_checkstring(*_luaState, -1);
std::string resolved = asset->resolveSyncedResource(resourceName);
lua_pushstring(*state, resolved.c_str());
lua_pushstring(*_luaState, resolved.c_str());
return 1;
}
void AssetLoader::pushAsset(Asset* asset) {
_assetStack.push_back(asset);
if (!asset->hasLuaTable()) {
if (asset == _rootAsset.get()) {
return;
}
@@ -357,7 +356,7 @@ void AssetLoader::pushAsset(Asset* asset) {
// Register import-optional function
lua_pushlightuserdata(*_luaState, this);
lua_pushcclosure(*_luaState, &assetloader::importOptional, 1);
lua_setfield(*_luaState, assetTableIndex, ImportToggleFunctionName);
lua_setfield(*_luaState, assetTableIndex, ImportOptionalFunctionName);
// Register default onDeinitialize function
lua_pushcfunction(*_luaState, &assetloader::noOperation);
@@ -399,7 +398,7 @@ void AssetLoader::updateLuaGlobals() {
int AssetLoader::importDependencyLua(std::string assetName) {
Asset* importer = _assetStack.back();
Asset* importedAsset = importDependency(assetName, toggleEnabled);
Asset* importedAsset = importDependency(assetName);
if (!importedAsset) {
return luaL_error(*_luaState, "Asset '%s' not found", assetName.c_str());
}
@@ -418,7 +417,7 @@ int AssetLoader::importOptionalLua(std::string assetName, bool enabled) {
void AssetLoader::createAssetLuaTableEntries(Asset* importer, Asset* importedAsset) {
int AssetLoader::createLuaTableEntries(const Asset* importer, const Asset* importedAsset) {
const std::string importerId = importer->id();
const std::string importedAssetId = importedAsset->id();

View File

@@ -88,7 +88,7 @@ void SceneLoader::loadScene(Scene* scene, const std::string& path) {
for (const std::string& key : keys) {
std::string assetName = assetDictionary.value<std::string>(key);
_assetLoader->loadAsset(assetName);
_assetLoader->importAsset(assetName);
}
// Sync all resources from assets that are dependencies of root asset.