2
0

moduleDefinition.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "moduleDefinition.h"
  23. #ifndef _MODULE_MANAGER_H
  24. #include "moduleManager.h"
  25. #endif
  26. // Script bindings.
  27. #include "moduleDefinition_ScriptBinding.h"
  28. #ifndef _CONSOLETYPES_H_
  29. #include "console/consoleTypes.h"
  30. #endif
  31. #ifndef _TAML_H_
  32. #include "persistence/taml/taml.h"
  33. #endif
  34. //-----------------------------------------------------------------------------
  35. IMPLEMENT_CONOBJECT( ModuleDefinition );
  36. //-----------------------------------------------------------------------------
  37. ModuleDefinition::ModuleDefinition() :
  38. mModuleId(StringTable->EmptyString()),
  39. mVersionId( 0 ),
  40. mBuildId( 0 ),
  41. mEnabled( true ),
  42. mSynchronized( false ),
  43. mDeprecated( false ),
  44. mCriticalMerge( false ),
  45. mOverrideExistingObjects(false),
  46. mModuleDescription( StringTable->EmptyString() ),
  47. mAuthor(StringTable->EmptyString()),
  48. mModuleGroup(StringTable->EmptyString()),
  49. mModuleType(StringTable->EmptyString()),
  50. mScriptFile(StringTable->EmptyString()),
  51. mCreateFunction(StringTable->EmptyString()),
  52. mDestroyFunction(StringTable->EmptyString()),
  53. mAssetTagsManifest(StringTable->EmptyString()),
  54. mModulePath(StringTable->EmptyString()),
  55. mModuleFile(StringTable->EmptyString()),
  56. mModuleFilePath(StringTable->EmptyString()),
  57. mModuleScriptFilePath(StringTable->EmptyString()),
  58. mSignature(StringTable->EmptyString()),
  59. mLoadCount( 0 ),
  60. mScopeSet( 0 ),
  61. mLocked( false ),
  62. mpModuleManager( NULL )
  63. {
  64. // Set Vector Associations.
  65. VECTOR_SET_ASSOCIATION( mDependencies );
  66. VECTOR_SET_ASSOCIATION( mModuleAssets );
  67. }
  68. //-----------------------------------------------------------------------------
  69. void ModuleDefinition::initPersistFields()
  70. {
  71. // Call parent.
  72. Parent::initPersistFields();
  73. /// Module configuration.
  74. 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)." );
  75. addProtectedField( "VersionId", TypeS32, Offset(mVersionId, ModuleDefinition), &setVersionId, &defaultProtectedGetFn, "The version Id. Breaking changes to a module should use a higher version Id." );
  76. 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." );
  77. 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." );
  78. 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." );
  79. 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." );
  80. 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." );
  81. 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.");
  82. addProtectedField( "Description", TypeString, Offset(mModuleDescription, ModuleDefinition), &setModuleDescription, &defaultProtectedGetFn, &writeModuleDescription, "The description typically used for debugging purposes but can be used for anything." );
  83. addProtectedField( "Author", TypeString, Offset(mAuthor, ModuleDefinition), &setAuthor, &defaultProtectedGetFn, &writeAuthor, "The author of the module." );
  84. addProtectedField( "Group", TypeString, Offset(mModuleGroup, ModuleDefinition), &setModuleGroup, &defaultProtectedGetFn, "The module group used typically when loading modules as a group." );
  85. 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." );
  86. 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." );
  87. addProtectedField( "ScriptFile", TypeString, Offset(mScriptFile, ModuleDefinition), &setScriptFile, &defaultProtectedGetFn, &writeScriptFile, "The name of the script file to compile when loading the module. Optional." );
  88. 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." );
  89. 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." );
  90. 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." );
  91. 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." );
  92. /// Module location (Read-only).
  93. 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." );
  94. 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." );
  95. 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." );
  96. 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." );
  97. /// Misc.
  98. 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." );
  99. }
  100. //-----------------------------------------------------------------------------
  101. bool ModuleDefinition::getDependency( const U32 dependencyIndex, ModuleDependency& dependency ) const
  102. {
  103. // Is dependency index out of bounds?
  104. if ( dependencyIndex >= (U32)mDependencies.size() )
  105. {
  106. // Yes, so warn.
  107. Con::warnf("Could not get module dependency '%d' as it is out of range.", dependencyIndex);
  108. return false;
  109. }
  110. // Fetch module dependency.
  111. dependency = mDependencies[dependencyIndex];
  112. return true;
  113. }
  114. //-----------------------------------------------------------------------------
  115. bool ModuleDefinition::addDependency( const char* pModuleId, const U32 versionId )
  116. {
  117. // Fetch module Id.
  118. StringTableEntry moduleId = StringTable->insert( pModuleId );
  119. // Do we have any existing dependencies?
  120. if ( mDependencies.size() > 0 )
  121. {
  122. // Yes, so is the module Id already a dependency?
  123. for( typeModuleDependencyVector::iterator dependencyItr = mDependencies.begin(); dependencyItr != mDependencies.end(); ++dependencyItr )
  124. {
  125. // Skip if not the same module Id.
  126. if ( dependencyItr->mModuleId != moduleId )
  127. continue;
  128. // Dependency already exists so warn.
  129. Con::warnf("Could not add dependency of module Id '%s' at version Id '%d' as the module Id is already a dependency.", pModuleId, versionId );
  130. return false;
  131. }
  132. }
  133. // Populate module dependency.
  134. ModuleDefinition::ModuleDependency dependency( moduleId, versionId );
  135. // Store dependency.
  136. mDependencies.push_back( dependency );
  137. return true;
  138. }
  139. //-----------------------------------------------------------------------------
  140. bool ModuleDefinition::removeDependency( const char* pModuleId )
  141. {
  142. // Fetch module Id.
  143. StringTableEntry moduleId = StringTable->insert( pModuleId );
  144. // Do we have any existing dependencies?
  145. if ( mDependencies.size() > 0 )
  146. {
  147. // Yes, so is the module Id a dependency?
  148. for( typeModuleDependencyVector::iterator dependencyItr = mDependencies.begin(); dependencyItr != mDependencies.end(); ++dependencyItr )
  149. {
  150. // Skip if not the same module Id.
  151. if ( dependencyItr->mModuleId != moduleId )
  152. continue;
  153. // Remove dependency.
  154. mDependencies.erase( dependencyItr );
  155. return true;
  156. }
  157. }
  158. // No, so warn.
  159. Con::warnf("Could not remove dependency of module Id '%s' as the module Id is not a dependency.", pModuleId );
  160. return false;
  161. }
  162. //-----------------------------------------------------------------------------
  163. bool ModuleDefinition::save( void )
  164. {
  165. // Does the module have a file-path yet?
  166. if (mModuleFilePath == StringTable->EmptyString())
  167. {
  168. // No, so warn.
  169. Con::warnf("Save() - Cannot save module definition '%s' as it does not have a file-path.", mModuleId );
  170. return false;
  171. }
  172. // Save the module file.
  173. Taml taml;
  174. return taml.write( this, mModuleFilePath );
  175. }