Adds function for sceneObjects to report utilized assets

Add reporting of used assets to tsStatics and TerrData
When saving a Scene, it will write static object asset dependencies to it's levelAsset
Shifted level loader logic to pass up the levelAsset rather than full level path
Made it so when level is loading, the levelAsset loads its dependencies. When the level is ended, as part of cleanup, they are unloaded
Shifts defaultEditorLevel to be an actual asset and made the ToolsModule load assets
Fixes the Save As action to correctly save out to the new level asset
Fixed the autoLoadAssets logic to be cleaner and not manually check types
Removed extra, unused DefaultEditorFile file
This commit is contained in:
Areloch 2020-08-19 18:30:42 -05:00
parent f7dbfff42d
commit 40dd926873
21 changed files with 263 additions and 255 deletions

View file

@ -1,4 +1,5 @@
#include "Scene.h"
#include "T3D/assets/LevelAsset.h"
Scene * Scene::smRootScene = nullptr;
Vector<Scene*> Scene::smSceneList;
@ -180,6 +181,93 @@ void Scene::unpackUpdate(NetConnection *conn, BitStream *stream)
}
void Scene::dumpUtilizedAssets()
{
Con::printf("Dumping utilized assets in scene!");
Vector<StringTableEntry> utilizedAssetsList;
for (U32 i = 0; i < mPermanentObjects.size(); i++)
{
mPermanentObjects[i]->getUtilizedAssets(&utilizedAssetsList);
}
for (U32 i = 0; i < mDynamicObjects.size(); i++)
{
mDynamicObjects[i]->getUtilizedAssets(&utilizedAssetsList);
}
for (U32 i = 0; i < utilizedAssetsList.size(); i++)
{
Con::printf("Utilized Asset: %s", utilizedAssetsList[i]);
}
Con::printf("Utilized Asset dump complete!");
}
StringTableEntry Scene::getOriginatingFile()
{
return getFilename();
}
StringTableEntry Scene::getLevelAsset()
{
StringTableEntry levelFile = getFilename();
if (levelFile == StringTable->EmptyString())
return StringTable->EmptyString();
AssetQuery* query = new AssetQuery();
query->registerObject();
S32 foundAssetcount = AssetDatabase.findAssetLooseFile(query, levelFile);
if (foundAssetcount == 0)
return StringTable->EmptyString();
else
return query->mAssetList[0];
}
bool Scene::saveScene(StringTableEntry fileName)
{
//So, we ultimately want to not only save out the level, but also collate all the assets utilized
//by the static objects in the scene so we can have those before we parse the level file itself
//Useful for preloading or stat tracking
//First, save the level file
if (fileName == StringTable->EmptyString())
{
fileName = getOriginatingFile();
}
bool saveSuccess = save(fileName);
if (!saveSuccess)
return false;
//Get the level asset
StringTableEntry levelAsset = getLevelAsset();
if (levelAsset == StringTable->EmptyString())
return saveSuccess;
LevelAsset* levelAssetDef = AssetDatabase.acquireAsset<LevelAsset>(levelAsset);
levelAssetDef->clearAssetDependencyFields("staticObjectAssetDependency");
//Next, lets build out our
Vector<StringTableEntry> utilizedAssetsList;
for (U32 i = 0; i < mPermanentObjects.size(); i++)
{
mPermanentObjects[i]->getUtilizedAssets(&utilizedAssetsList);
}
for (U32 i = 0; i < utilizedAssetsList.size(); i++)
{
levelAssetDef->addAssetDependencyField("staticObjectAssetDependency", utilizedAssetsList[i]);
}
saveSuccess = levelAssetDef->saveAsset();
return saveSuccess;
}
//
Vector<SceneObject*> Scene::getObjectsByClass(String className, bool checkSubscenes)
{
@ -251,3 +339,34 @@ DefineEngineMethod(Scene, getObjectsByClass, String, (String className), (""),
//return object->getObjectsByClass(className);
return "";
}
DefineEngineMethod(Scene, dumpUtilizedAssets, void, (), ,
"Get the root Scene object that is loaded.\n"
"@return The id of the Root Scene. Will be 0 if no root scene is loaded")
{
object->dumpUtilizedAssets();
}
DefineEngineMethod(Scene, getOriginatingFile, const char*, (), ,
"Get the root Scene object that is loaded.\n"
"@return The id of the Root Scene. Will be 0 if no root scene is loaded")
{
return object->getOriginatingFile();
}
DefineEngineMethod(Scene, getLevelAsset, const char*, (), ,
"Get the root Scene object that is loaded.\n"
"@return The id of the Root Scene. Will be 0 if no root scene is loaded")
{
return object->getLevelAsset();
}
DefineEngineMethod(Scene, save, bool, (const char* fileName), (""),
"Save out the object to the given file.\n"
"@param fileName The name of the file to save to."
"@param selectedOnly If true, only objects marked as selected will be saved out.\n"
"@param preAppendString Text which will be preprended directly to the object serialization.\n"
"@param True on success, false on failure.")
{
return object->saveScene(StringTable->insert(fileName));
}

View file

@ -62,6 +62,13 @@ public:
void addDynamicObject(SceneObject* object);
void removeDynamicObject(SceneObject* object);
void dumpUtilizedAssets();
StringTableEntry getOriginatingFile();
StringTableEntry getLevelAsset();
bool saveScene(StringTableEntry fileName);
//
//Networking
U32 packUpdate(NetConnection *conn, U32 mask, BitStream *stream);

View file

@ -330,9 +330,52 @@ void LevelAsset::setNavmeshFile(const char* pNavmeshFile)
refreshAsset();
}
void LevelAsset::loadDependencies()
{
//First, load any material, animation, etc assets we may be referencing in our asset
// Find any asset dependencies.
AssetManager::typeAssetDependsOnHash::Iterator assetDependenciesItr = mpOwningAssetManager->getDependedOnAssets()->find(mpAssetDefinition->mAssetId);
// Does the asset have any dependencies?
if (assetDependenciesItr != mpOwningAssetManager->getDependedOnAssets()->end())
{
// Iterate all dependencies.
while (assetDependenciesItr != mpOwningAssetManager->getDependedOnAssets()->end() && assetDependenciesItr->key == mpAssetDefinition->mAssetId)
{
//Force it to be loaded by acquiring it
StringTableEntry assetId = assetDependenciesItr->value;
mAssetDependencies.push_back(AssetDatabase.acquireAsset<AssetBase>(assetId));
// Next dependency.
assetDependenciesItr++;
}
}
}
void LevelAsset::unloadDependencies()
{
for (U32 i = 0; i < mAssetDependencies.size(); i++)
{
AssetBase* assetDef = mAssetDependencies[i];
AssetDatabase.releaseAsset(assetDef->getAssetId());
}
}
DefineEngineMethod(LevelAsset, getLevelFile, const char*, (),,
"Creates a new script asset using the targetFilePath.\n"
"@return The bool result of calling exec")
{
return object->getLevelPath();
}
DefineEngineMethod(LevelAsset, loadDependencies, void, (), ,
"Initiates the loading of asset dependencies for this level.")
{
return object->loadDependencies();
}
DefineEngineMethod(LevelAsset, unloadDependencies, void, (), ,
"Initiates the unloading of previously loaded asset dependencies for this level.")
{
return object->unloadDependencies();
}

View file

@ -67,6 +67,8 @@ class LevelAsset : public AssetBase
StringTableEntry mGamemodeName;
Vector<AssetBase*> mAssetDependencies;
public:
LevelAsset();
virtual ~LevelAsset();
@ -78,6 +80,9 @@ public:
/// Declare Console Object.
DECLARE_CONOBJECT(LevelAsset);
void loadDependencies();
void unloadDependencies();
void setLevelFile(const char* pImageFile);
inline StringTableEntry getLevelFile(void) const { return mLevelFile; };
void setPostFXPresetFile(const char* pPostFXPresetFile);

View file

@ -221,7 +221,6 @@ bool TerrainAsset::loadTerrain()
//Force the asset to become initialized if it hasn't been already
AssetPtr<TerrainMaterialAsset> matAsset = assetId;
mTerrMaterialAssets.push_front(matAsset);
}

View file

@ -1691,6 +1691,12 @@ void TSStatic::updateMaterials()
mShapeInstance->initMaterialList();
}
void TSStatic::getUtilizedAssets(Vector<StringTableEntry>* usedAssetsList)
{
if(!mShapeAsset.isNull())
usedAssetsList->push_back_unique(mShapeAssetId);
}
//------------------------------------------------------------------------
//These functions are duplicated in tsStatic and shapeBase.
//They each function a little differently; but achieve the same purpose of gathering

View file

@ -286,6 +286,8 @@ public:
bool isAnimated() { return mPlayAmbient; }
virtual void getUtilizedAssets(Vector<StringTableEntry>* usedAssetsList);
private:
virtual void onStaticModified(const char* slotName, const char* newValue = NULL);
protected:

View file

@ -253,30 +253,22 @@ bool AssetManager::loadModuleAutoLoadAssets(ModuleDefinition* pModuleDefinition)
if (assetDef->mAssetType == pAutoloadAssets->getAssetType())
{
//TODO: this is stupid and ugly, need to properly automagically parse the class for registration
AssetBase* assetBase = nullptr;
String assetPath = assetDef->mAssetBaseFilePath;
assetPath.replace("//", "/");
if (assetDef->mAssetType == StringTable->insert("GUIAsset"))
{
assetBase = mTaml.read<GUIAsset>(assetDef->mAssetBaseFilePath);
}
else if (assetDef->mAssetType == StringTable->insert("ScriptAsset"))
{
assetBase = mTaml.read<ScriptAsset>(assetDef->mAssetBaseFilePath);
}
else if (assetDef->mAssetType == StringTable->insert("MaterialAsset"))
{
assetBase = mTaml.read<MaterialAsset>(assetDef->mAssetBaseFilePath);
}
else if (assetDef->mAssetType == StringTable->insert("GameObjectAsset"))
{
assetBase = mTaml.read<GameObjectAsset>(assetDef->mAssetBaseFilePath);
}
String autoLoadPath = pModuleDefinition->getModulePath();
autoLoadPath += "/";
autoLoadPath += pAutoloadAssets->getPath();
//load the asset now if valid
if (assetBase)
if (pAutoloadAssets->getPath() == StringTable->EmptyString() || assetPath.startsWith(autoLoadPath.c_str()))
{
assetBase->setOwned(this, assetDef);
AssetBase* assetBase = dynamic_cast<AssetBase*>(mTaml.read(assetDef->mAssetBaseFilePath));
//load the asset now if valid
if (assetBase)
{
assetBase->setOwned(this, assetDef);
}
}
}
}

View file

@ -942,6 +942,8 @@ class SceneObject : public NetObject, private SceneContainer::Link, public Proce
/// notification that a direct child object has been detached
virtual void onLostChild(SceneObject *subObject);
// PATHSHAPE END
virtual void getUtilizedAssets(Vector<StringTableEntry>* usedAssetsList) {}
};
#endif // _SCENEOBJECT_H_

View file

@ -1420,6 +1420,11 @@ void TerrainBlock::getMinMaxHeight( F32 *minHeight, F32 *maxHeight ) const
*maxHeight = fixedToFloat( sq->maxHeight );
}
void TerrainBlock::getUtilizedAssets(Vector<StringTableEntry>* usedAssetsList)
{
if (!mTerrainAsset.isNull())
usedAssetsList->push_back_unique(mTerrainAssetId);
}
//-----------------------------------------------------------------------------
// Console Methods
//-----------------------------------------------------------------------------

View file

@ -474,6 +474,8 @@ public:
U32 packUpdate (NetConnection *conn, U32 mask, BitStream *stream);
void unpackUpdate(NetConnection *conn, BitStream *stream);
void inspectPostApply();
virtual void getUtilizedAssets(Vector<StringTableEntry>* usedAssetsList);
protected:
bool mIgnoreZodiacs;