moduleDefinition.cpp
Engine/source/module/moduleDefinition.cpp
Public Functions
Detailed Description
Public Functions
IMPLEMENT_CONOBJECT(ModuleDefinition )
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#include "moduleDefinition.h" 25 26#ifndef _MODULE_MANAGER_H 27#include "moduleManager.h" 28#endif 29 30// Script bindings. 31#include "moduleDefinition_ScriptBinding.h" 32 33#ifndef _CONSOLETYPES_H_ 34#include "console/consoleTypes.h" 35#endif 36 37#ifndef _TAML_H_ 38#include "persistence/taml/taml.h" 39#endif 40 41//----------------------------------------------------------------------------- 42 43IMPLEMENT_CONOBJECT( ModuleDefinition ); 44 45//----------------------------------------------------------------------------- 46 47ModuleDefinition::ModuleDefinition() : 48mModuleId(StringTable->EmptyString()), 49 mVersionId( 0 ), 50 mBuildId( 0 ), 51 mEnabled( true ), 52 mSynchronized( false ), 53 mDeprecated( false ), 54 mCriticalMerge( false ), 55 mOverrideExistingObjects(false), 56 mModuleDescription( StringTable->EmptyString() ), 57 mAuthor(StringTable->EmptyString()), 58 mModuleGroup(StringTable->EmptyString()), 59 mModuleType(StringTable->EmptyString()), 60 mScriptFile(StringTable->EmptyString()), 61 mCreateFunction(StringTable->EmptyString()), 62 mDestroyFunction(StringTable->EmptyString()), 63 mAssetTagsManifest(StringTable->EmptyString()), 64 mModulePath(StringTable->EmptyString()), 65 mModuleFile(StringTable->EmptyString()), 66 mModuleFilePath(StringTable->EmptyString()), 67 mModuleScriptFilePath(StringTable->EmptyString()), 68 mSignature(StringTable->EmptyString()), 69 mLoadCount( 0 ), 70 mScopeSet( 0 ), 71 mLocked( false ), 72 mpModuleManager( NULL ) 73{ 74 // Set Vector Associations. 75 VECTOR_SET_ASSOCIATION( mDependencies ); 76 VECTOR_SET_ASSOCIATION( mModuleAssets ); 77} 78 79//----------------------------------------------------------------------------- 80 81void ModuleDefinition::initPersistFields() 82{ 83 // Call parent. 84 Parent::initPersistFields(); 85 86 /// Module configuration. 87 addProtectedField( "ModuleId", TypeString, Offset(mModuleId, ModuleDefinition), &setModuleId, &defaultProtectedGetFn, "A unique string Id for the module. It can contain any characters except a comma or semi-colon (the asset scope character)." ); 88 addProtectedField( "VersionId", TypeS32, Offset(mVersionId, ModuleDefinition), &setVersionId, &defaultProtectedGetFn, "The version Id. Breaking changes to a module should use a higher version Id." ); 89 addProtectedField( "BuildId", TypeS32, Offset(mBuildId, ModuleDefinition), &setBuildId, &defaultProtectedGetFn, &writeBuildId, "The build Id. Non-breaking changes to a module should use a higher build Id. Optional: If not specified then the build Id will be zero." ); 90 addProtectedField( "enabled", TypeBool, Offset(mEnabled, ModuleDefinition), &setEnabled, &defaultProtectedGetFn, &writeEnabled, "Whether the module is enabled or not. When disabled, it is effectively ignored. Optional: If not specified then the module is enabled." ); 91 addProtectedField( "Synchronized", TypeBool, Offset(mSynchronized, ModuleDefinition), &setSynchronized, &defaultProtectedGetFn, &writeSynchronized, "Whether the module should be synchronized or not. Optional: If not specified then the module is not synchronized." ); 92 addProtectedField( "Deprecated", TypeBool, Offset(mDeprecated, ModuleDefinition), &setDeprecated, &defaultProtectedGetFn, &writeDeprecated, "Whether the module is deprecated or not. Optional: If not specified then the module is not deprecated." ); 93 addProtectedField( "CriticalMerge", TypeBool, Offset(mCriticalMerge, ModuleDefinition), &setDeprecated, &defaultProtectedGetFn, &writeCriticalMerge, "Whether the merging of a module prior to a restart is critical or not. Optional: If not specified then the module is not merge critical." ); 94 addProtectedField( "OverrideExistingObjects", TypeBool, Offset(mOverrideExistingObjects, ModuleDefinition), &setOverrideExistingObjects, &defaultProtectedGetFn, &writeOverrideExistingObjects, "Controls if when this module is loaded and the create function is executed, it will replace existing objects that share names or not."); 95 addProtectedField( "Description", TypeString, Offset(mModuleDescription, ModuleDefinition), &setModuleDescription, &defaultProtectedGetFn, &writeModuleDescription, "The description typically used for debugging purposes but can be used for anything." ); 96 addProtectedField( "Author", TypeString, Offset(mAuthor, ModuleDefinition), &setAuthor, &defaultProtectedGetFn, &writeAuthor, "The author of the module." ); 97 addProtectedField( "Group", TypeString, Offset(mModuleGroup, ModuleDefinition), &setModuleGroup, &defaultProtectedGetFn, "The module group used typically when loading modules as a group." ); 98 addProtectedField( "Type", TypeString, Offset(mModuleType, ModuleDefinition), &setModuleType, &defaultProtectedGetFn, &writeModuleType, "The module type typically used to distinguish modules during module enumeration. Optional: If not specified then the type is empty although this can still be used as a pseudo 'global' type for instance." ); 99 addProtectedField( "Dependencies", TypeString, Offset(mDependencies, ModuleDefinition), &setDependencies, &getDependencies, &writeDependencies, "A comma-separated list of module Ids/VersionIds (<ModuleId>=<VersionId>,<ModuleId>=<VersionId>,etc) which this module depends upon. Optional: If not specified then no dependencies are assumed." ); 100 addProtectedField( "ScriptFile", TypeString, Offset(mScriptFile, ModuleDefinition), &setScriptFile, &defaultProtectedGetFn, &writeScriptFile, "The name of the script file to compile when loading the module. Optional." ); 101 addProtectedField( "CreateFunction", TypeString, Offset(mCreateFunction, ModuleDefinition), &setCreateFunction, &defaultProtectedGetFn, &writeCreateFunction, "The name of the function used to create the module. Optional: If not specified then no create function is called." ); 102 addProtectedField( "DestroyFunction", TypeString, Offset(mDestroyFunction, ModuleDefinition), &setDestroyFunction, &defaultProtectedGetFn, &writeDestroyFunction, "The name of the function used to destroy the module. Optional: If not specified then no destroy function is called." ); 103 addProtectedField( "AssetTagsManifest", TypeString, Offset(mAssetTagsManifest, ModuleDefinition), &setAssetTagsManifest, &defaultProtectedGetFn, &writeAssetTagsManifest, "The name of tags asset manifest file if this module contains asset tags. Optional: If not specified then no asset tags will be found for this module. Currently, only a single asset tag manifest should exist." ); 104 addProtectedField( "ScopeSet", TypeS32, Offset( mScopeSet, ModuleDefinition ), &defaultProtectedNotSetFn, &getScopeSet, &defaultProtectedNotWriteFn, "The scope set used to control the lifetime scope of objects that the module uses. Objects added to this set are destroyed automatically when the module is unloaded." ); 105 106 /// Module location (Read-only). 107 addProtectedField( "ModulePath", TypeString, Offset(mModulePath, ModuleDefinition), &defaultProtectedNotSetFn, &defaultProtectedGetFn, &defaultProtectedNotWriteFn, "The path of the module. This is read-only and is available only after the module has been registered by a module manager." ); 108 addProtectedField( "ModuleFile", TypeString, Offset(mModuleFile, ModuleDefinition), &defaultProtectedNotSetFn, &defaultProtectedGetFn, &defaultProtectedNotWriteFn, "The file of the module. This is read-only and is available only after the module has been registered by a module manager." ); 109 addProtectedField( "ModuleFilePath", TypeString, Offset(mModuleFilePath, ModuleDefinition), &defaultProtectedNotSetFn, &defaultProtectedGetFn, &defaultProtectedNotWriteFn, "The file-path of the module definition. This is read-only and is available only after the module has been registered by a module manager." ); 110 addProtectedField( "ModuleScriptFilePath", TypeString, Offset(mModuleScriptFilePath, ModuleDefinition), &defaultProtectedNotSetFn, &defaultProtectedGetFn, &defaultProtectedNotWriteFn, "The file-path of the script-file referenced in the module definition. This is read-only and is available only after the module has been registered by a module manager." ); 111 112 /// Misc. 113 addProtectedField( "Signature", TypeString, 0, &defaultProtectedNotSetFn, &getSignature, &defaultProtectedNotWriteFn, "A unique signature of the module definition based upon its Id, version and build. This is read-only and is available only after the module has been registered by a module manager." ); 114} 115 116//----------------------------------------------------------------------------- 117 118bool ModuleDefinition::getDependency( const U32 dependencyIndex, ModuleDependency& dependency ) const 119{ 120 // Is dependency index out of bounds? 121 if ( dependencyIndex >= (U32)mDependencies.size() ) 122 { 123 // Yes, so warn. 124 Con::warnf("Could not get module dependency '%d' as it is out of range.", dependencyIndex); 125 return false; 126 } 127 128 // Fetch module dependency. 129 dependency = mDependencies[dependencyIndex]; 130 131 return true; 132} 133 134//----------------------------------------------------------------------------- 135 136bool ModuleDefinition::addDependency( const char* pModuleId, const U32 versionId ) 137{ 138 // Fetch module Id. 139 StringTableEntry moduleId = StringTable->insert( pModuleId ); 140 141 // Do we have any existing dependencies? 142 if ( mDependencies.size() > 0 ) 143 { 144 // Yes, so is the module Id already a dependency? 145 for( typeModuleDependencyVector::iterator dependencyItr = mDependencies.begin(); dependencyItr != mDependencies.end(); ++dependencyItr ) 146 { 147 // Skip if not the same module Id. 148 if ( dependencyItr->mModuleId != moduleId ) 149 continue; 150 151 // Dependency already exists so warn. 152 Con::warnf("Could not add dependency of module Id '%s' at version Id '%d' as the module Id is already a dependency.", pModuleId, versionId ); 153 return false; 154 } 155 } 156 157 // Populate module dependency. 158 ModuleDefinition::ModuleDependency dependency( moduleId, versionId ); 159 160 // Store dependency. 161 mDependencies.push_back( dependency ); 162 163 return true; 164} 165 166//----------------------------------------------------------------------------- 167 168bool ModuleDefinition::removeDependency( const char* pModuleId ) 169{ 170 // Fetch module Id. 171 StringTableEntry moduleId = StringTable->insert( pModuleId ); 172 173 // Do we have any existing dependencies? 174 if ( mDependencies.size() > 0 ) 175 { 176 // Yes, so is the module Id a dependency? 177 for( typeModuleDependencyVector::iterator dependencyItr = mDependencies.begin(); dependencyItr != mDependencies.end(); ++dependencyItr ) 178 { 179 // Skip if not the same module Id. 180 if ( dependencyItr->mModuleId != moduleId ) 181 continue; 182 183 // Remove dependency. 184 mDependencies.erase( dependencyItr ); 185 186 return true; 187 } 188 } 189 190 // No, so warn. 191 Con::warnf("Could not remove dependency of module Id '%s' as the module Id is not a dependency.", pModuleId ); 192 return false; 193} 194 195//----------------------------------------------------------------------------- 196 197bool ModuleDefinition::save( void ) 198{ 199 // Does the module have a file-path yet? 200 if (mModuleFilePath == StringTable->EmptyString()) 201 { 202 // No, so warn. 203 Con::warnf("Save() - Cannot save module definition '%s' as it does not have a file-path.", mModuleId ); 204 return false; 205 } 206 207 // Save the module file. 208 Taml taml; 209 return taml.write( this, mModuleFilePath ); 210} 211