Torque3D Documentation / _generateds / tamlAssetDeclaredVisitor.h

tamlAssetDeclaredVisitor.h

Engine/source/assets/tamlAssetDeclaredVisitor.h

More...

Classes:

Detailed Description

  1
  2//-----------------------------------------------------------------------------
  3// Copyright (c) 2013 GarageGames, LLC
  4//
  5// Permission is hereby granted, free of charge, to any person obtaining a copy
  6// of this software and associated documentation files (the "Software"), to
  7// deal in the Software without restriction, including without limitation the
  8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9// sell copies of the Software, and to permit persons to whom the Software is
 10// furnished to do so, subject to the following conditions:
 11//
 12// The above copyright notice and this permission notice shall be included in
 13// all copies or substantial portions of the Software.
 14//
 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 21// IN THE SOFTWARE.
 22//-----------------------------------------------------------------------------
 23
 24#ifndef _TAML_ASSET_DECLARED_VISITOR_H_
 25#define _TAML_ASSET_DECLARED_VISITOR_H_
 26
 27#ifndef _TAML_VISITOR_H_
 28#include "persistence/taml/tamlVisitor.h"
 29#endif
 30
 31#ifndef _TAML_PARSER_H_
 32#include "persistence\/taml/tamlParser.h"
 33#endif
 34
 35#ifndef _ASSET_FIELD_TYPES_H_
 36#include "assets/assetFieldTypes.h"
 37#endif
 38
 39#ifndef _ASSET_DEFINITION_H_
 40#include "assetDefinition.h"
 41#endif
 42
 43#ifndef _ASSET_BASE_H_
 44#include "assetBase.h"
 45#endif
 46
 47// Debug Profiling.
 48#include "platform/profiler.h"
 49
 50//-----------------------------------------------------------------------------
 51
 52class TamlAssetDeclaredVisitor : public TamlVisitor
 53{
 54public:
 55    typedef StringTableEntry typeAssetId;
 56    typedef Vector<typeAssetId> typeAssetIdVector;
 57    typedef Vector<StringTableEntry> typeLooseFileVector;
 58
 59private:
 60    AssetDefinition         mAssetDefinition;
 61    typeAssetIdVector       mAssetDependencies;
 62    typeLooseFileVector     mAssetLooseFiles;
 63
 64public:
 65    TamlAssetDeclaredVisitor() { mAssetDefinition.reset(); }
 66    virtual ~TamlAssetDeclaredVisitor() {}
 67
 68
 69    inline AssetDefinition& getAssetDefinition( void ) { return mAssetDefinition; }
 70    inline typeAssetIdVector& getAssetDependencies( void ) { return mAssetDependencies; }
 71    inline typeLooseFileVector& getAssetLooseFiles( void ) { return mAssetLooseFiles; }
 72
 73    void clear( void ) { mAssetDefinition.reset(); mAssetDependencies.clear(); mAssetLooseFiles.clear(); }
 74
 75    virtual bool wantsPropertyChanges( void ) { return false; }
 76    virtual bool wantsRootOnly( void ) { return false; }
 77
 78    virtual bool visit( const TamlParser& parser, TamlVisitor::PropertyState& propertyState )
 79    {    
 80        // Debug Profiling.
 81        PROFILE_SCOPE(TamlAssetDeclaredVisitor_Visit);
 82
 83        // Fetch property name and value.
 84        StringTableEntry propertyName = propertyState.getPropertyName();
 85        const char* pPropertyValue = propertyState.getPropertyValue();
 86
 87        // Is this the root object?
 88        if ( propertyState.isRootObject() )
 89        {
 90            // Yes, so is the asset type set yet?
 91            if ( mAssetDefinition.mAssetType == StringTable->EmptyString() )
 92            {
 93                // No, set set asset type and base file-path.
 94                mAssetDefinition.mAssetType = propertyState.getObjectName();
 95                mAssetDefinition.mAssetBaseFilePath = parser.getParsingFilename();
 96            }
 97
 98            // Asset name?
 99            if ( propertyName == assetNameField )
100            {
101                // Yes, so assign it.
102                mAssetDefinition.mAssetName = StringTable->insert( pPropertyValue );
103                return true;
104            }
105            // Asset description?
106            else if ( propertyName == assetDescriptionField )
107            {
108                // Yes, so assign it.
109                mAssetDefinition.mAssetDescription = StringTable->insert( pPropertyValue );
110                return true;
111            }
112            // Asset description?
113            else if ( propertyName == assetCategoryField )
114            {
115                // Yes, so assign it.
116                mAssetDefinition.mAssetCategory = StringTable->insert( pPropertyValue );
117                return true;
118            }
119            // Asset auto-unload?
120            else if ( propertyName == assetAutoUnloadField )
121            {
122                // Yes, so assign it.
123                mAssetDefinition.mAssetAutoUnload = dAtob( pPropertyValue );
124                return true;
125            }
126            // Asset internal?
127            else if ( propertyName == assetInternalField )
128            {
129                // Yes, so assign it.
130                mAssetDefinition.mAssetInternal = dAtob( pPropertyValue );
131                return true;
132            }
133        }
134
135        // Fetch property word count.
136        const U32 propertyWordCount = StringUnit::getUnitCount( pPropertyValue, ASSET_ASSIGNMENT_TOKEN );
137
138        // Finish if there's not two words.
139        if ( propertyWordCount != 2 )
140            return true;
141
142        // Fetch the asset signature.
143        StringTableEntry assetSignature = StringTable->insert( StringUnit::getUnit( pPropertyValue, 0, ASSET_ASSIGNMENT_TOKEN ) );
144
145        // Is this an asset Id signature?
146        if ( assetSignature == assetLooseIdSignature )
147        {
148            // Yes, so get asset Id.
149            typeAssetId assetId = StringTable->insert( StringUnit::getUnit( pPropertyValue, 1, ASSET_ASSIGNMENT_TOKEN ) );
150
151            // Finish if the dependency is itself!
152            if ( mAssetDefinition.mAssetId == assetId )
153                return true;
154
155            // Iterate existing dependencies.
156            for( typeAssetIdVector::iterator dependencyItr = mAssetDependencies.begin(); dependencyItr != mAssetDependencies.end(); ++dependencyItr )
157            {
158                // Finish if asset Id is already a dependency.
159                if ( *dependencyItr == assetId )
160                    return true;
161            }
162
163            // Insert asset reference.
164            mAssetDependencies.push_back( assetId );
165        }
166        // Is this a loose-file signature?
167        else if ( assetSignature == assetLooseFileSignature )
168        {
169            // Yes, so get loose-file reference.
170            const char* pAssetLooseFile = StringUnit::getUnit( pPropertyValue, 1, ASSET_ASSIGNMENT_TOKEN );
171
172            // Fetch asset path only.
173            char assetBasePathBuffer[1024];
174            dSprintf( assetBasePathBuffer, sizeof(assetBasePathBuffer), "%s", mAssetDefinition.mAssetBaseFilePath );
175            char* pFinalSlash = dStrrchr( assetBasePathBuffer, '/' );
176            if ( pFinalSlash != NULL ) *pFinalSlash = 0;
177
178            // Expand the path in the usual way.
179            char assetFilePathBuffer[1024];
180            Con::expandPath( assetFilePathBuffer, sizeof(assetFilePathBuffer), pAssetLooseFile, assetBasePathBuffer );
181
182            // Insert asset loose-file.
183            mAssetLooseFiles.push_back( StringTable->insert( assetFilePathBuffer ) );
184        }
185
186        return true;
187    }
188};
189
190#endif // _TAML_ASSET_DECLARED_VISITOR_H_
191