Просмотр исходного кода

Expands ScriptAsset behavior to operate as a generic type-settable asset

JeffR 2 лет назад
Родитель
Сommit
167f98b767

+ 28 - 3
Engine/source/T3D/assets/ScriptAsset.cpp

@@ -89,6 +89,15 @@ ConsoleSetType(TypeScriptAssetPtr)
 
 //-----------------------------------------------------------------------------
 
+IMPLEMENT_CALLBACK(ScriptAsset, onInitializeAsset, void, (), (),
+   "@brief When the ScriptAsset is initialized(loaded) by the AssetManager.\n\n");
+
+IMPLEMENT_CALLBACK(ScriptAsset, onRefreshAsset, void, (), (),
+   "@brief When the ScriptAsset is refreshed by the AssetManager.\n\n");
+
+IMPLEMENT_CALLBACK(ScriptAsset, onUnloadAsset, void, (), (),
+   "@brief When the ScriptAsset is unloaded by the AssetManager.\n\n");
+
 ScriptAsset::ScriptAsset() : AssetBase(), mIsServerSide(true)
 {
    mScriptFile = StringTable->EmptyString();
@@ -123,6 +132,12 @@ void ScriptAsset::copyTo(SimObject* object)
 
 void ScriptAsset::initializeAsset()
 {
+   if (mpAssetDefinition->mAssetType != StringTable->insert("ScriptAsset"))
+   {
+      //if we've got a custom type, treat it as our namespace, too
+      setClassNamespace(mpAssetDefinition->mAssetType);
+   }
+
    mScriptPath = getOwned() ? expandAssetFilePath(mScriptFile) : mScriptPath;
 
    if (Torque::FS::IsScriptFile(mScriptPath))
@@ -138,7 +153,7 @@ void ScriptAsset::initializeAsset()
          {
             AssetPtr<ScriptAsset> scriptAsset = assetDependenciesItr->value;
 
-            mScriptAssets.push_front(scriptAsset);
+            mScriptAssetDependencies.push_front(scriptAsset);
 
             // Next dependency.
             assetDependenciesItr++;
@@ -147,6 +162,8 @@ void ScriptAsset::initializeAsset()
 
       Con::executeFile(mScriptPath, false, false);
    }
+
+   onInitializeAsset_callback();
 }
 
 void ScriptAsset::onAssetRefresh()
@@ -156,13 +173,20 @@ void ScriptAsset::onAssetRefresh()
    if (Torque::FS::IsScriptFile(mScriptPath))
    {
       //Refresh any dependencies we may have
-      for (U32 i = 0; i < mScriptAssets.size(); i++)
+      for (U32 i = 0; i < mScriptAssetDependencies.size(); i++)
       {
-         mScriptAssets[i]->onAssetRefresh();
+         mScriptAssetDependencies[i]->onAssetRefresh();
       }
 
       Con::executeFile(mScriptPath, false, false);
    }
+
+   onRefreshAsset_callback();
+}
+
+void ScriptAsset::unloadAsset()
+{
+   onUnloadAsset_callback();
 }
 
 void ScriptAsset::setScriptFile(const char* pScriptFile)
@@ -205,3 +229,4 @@ DefineEngineMethod(ScriptAsset, execScript, bool, (), ,
 {
    return object->execScript();
 }
+

+ 6 - 1
Engine/source/T3D/assets/ScriptAsset.h

@@ -52,7 +52,7 @@ class ScriptAsset : public AssetBase
    StringTableEntry        mScriptPath;
    bool                    mIsServerSide;
 
-   Vector<AssetPtr<ScriptAsset>> mScriptAssets;
+   Vector<AssetPtr<ScriptAsset>> mScriptAssetDependencies;
 
 public:
    ScriptAsset();
@@ -72,9 +72,14 @@ public:
 
    bool execScript();
 
+   DECLARE_CALLBACK(void, onInitializeAsset, ());
+   DECLARE_CALLBACK(void, onRefreshAsset, ());
+   DECLARE_CALLBACK(void, onUnloadAsset, ());
+
 protected:
    virtual void            initializeAsset(void);
    virtual void            onAssetRefresh(void);
+   virtual void            unloadAsset(void);
 
    static bool setScriptFile(void *obj, const char *index, const char *data) { static_cast<ScriptAsset*>(obj)->setScriptFile(data); return false; }
    static const char* getScriptFile(void* obj, const char* data) { return static_cast<ScriptAsset*>(obj)->getScriptFile(); }

+ 1 - 0
Engine/source/assets/assetBase.cpp

@@ -50,6 +50,7 @@ StringTableEntry assetCategoryField = StringTable->insert("AssetCategory");
 StringTableEntry assetAutoUnloadField = StringTable->insert("AssetAutoUnload");
 StringTableEntry assetInternalField = StringTable->insert("AssetInternal");
 StringTableEntry assetPrivateField = StringTable->insert("AssetPrivate");
+StringTableEntry assetTypeField = StringTable->insert("AssetType");
 
 //-----------------------------------------------------------------------------
 const String AssetBase::mErrCodeStrings[] =

+ 2 - 0
Engine/source/assets/assetBase.h

@@ -46,6 +46,7 @@ extern StringTableEntry assetCategoryField;
 extern StringTableEntry assetInternalField;
 extern StringTableEntry assetPrivateField;
 extern StringTableEntry assetAutoUnloadField;
+extern StringTableEntry assetTypeField;
 
 //#define ASSET_BASE_ASSETNAME_FIELD         "AssetName"
 //#define ASSET_BASE_ASSETDESCRIPTION_FIELD  "AssetDescription"
@@ -137,6 +138,7 @@ public:
 protected:
    virtual void            initializeAsset(void) {}
    virtual void            onAssetRefresh(void) {}
+   virtual void            unloadAsset(void) {}
 
 protected:
    static bool             setAssetName(void *obj, const char *array, const char *data)           { static_cast<AssetBase*>(obj)->setAssetName(data); return false; }

+ 2 - 0
Engine/source/assets/assetManager.cpp

@@ -2988,6 +2988,8 @@ void AssetManager::unloadAsset( AssetDefinition* pAssetDefinition )
     // Debug Profiling.
     PROFILE_SCOPE(AssetManager_UnloadAsset);
 
+    pAssetDefinition->mpAssetBase->unloadAsset();
+
     // Destroy the asset.
     if(pAssetDefinition->mpAssetBase->isProperlyAdded())
       pAssetDefinition->mpAssetBase->deleteObject();

+ 11 - 0
Engine/source/assets/tamlAssetDeclaredVisitor.h

@@ -129,6 +129,17 @@ public:
                 mAssetDefinition.mAssetInternal = dAtob( pPropertyValue );
                 return true;
             }
+            else if (propertyName == assetTypeField)
+            {
+               if (mAssetDefinition.mAssetType == StringTable->insert("ScriptAsset"))
+               {
+                  //We're gunna special-casehere.
+                  //If it's a ScriptAsset and it defines an AssetType property, we presume
+                  //that's the ScriptAsset's special asset type, so we set it here
+                  mAssetDefinition.mAssetType = StringTable->insert(pPropertyValue);
+                  return true;
+               }
+            }
         }
 
         // Fetch property word count.