2
0

moduleDefinition.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. docsURL;
  72. // Call parent.
  73. Parent::initPersistFields();
  74. /// Module configuration.
  75. 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)." );
  76. addProtectedField( "VersionId", TypeS32, Offset(mVersionId, ModuleDefinition), &setVersionId, &defaultProtectedGetFn, "The version Id. Breaking changes to a module should use a higher version Id." );
  77. 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." );
  78. 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." );
  79. 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." );
  80. 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." );
  81. 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." );
  82. 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.");
  83. addProtectedField( "Description", TypeString, Offset(mModuleDescription, ModuleDefinition), &setModuleDescription, &defaultProtectedGetFn, &writeModuleDescription, "The description typically used for debugging purposes but can be used for anything." );
  84. addProtectedField( "Author", TypeString, Offset(mAuthor, ModuleDefinition), &setAuthor, &defaultProtectedGetFn, &writeAuthor, "The author of the module." );
  85. addProtectedField( "Group", TypeString, Offset(mModuleGroup, ModuleDefinition), &setModuleGroup, &defaultProtectedGetFn, "The module group used typically when loading modules as a group." );
  86. 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." );
  87. 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." );
  88. addProtectedField( "ScriptFile", TypeString, Offset(mScriptFile, ModuleDefinition), &setScriptFile, &defaultProtectedGetFn, &writeScriptFile, "The name of the script file to compile when loading the module. Optional." );
  89. 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." );
  90. 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." );
  91. 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." );
  92. 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." );
  93. /// Module location (Read-only).
  94. 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." );
  95. 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." );
  96. 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." );
  97. 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." );
  98. /// Misc.
  99. 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." );
  100. }
  101. //-----------------------------------------------------------------------------
  102. bool ModuleDefinition::getDependency( const U32 dependencyIndex, ModuleDependency& dependency ) const
  103. {
  104. // Is dependency index out of bounds?
  105. if ( dependencyIndex >= (U32)mDependencies.size() )
  106. {
  107. // Yes, so warn.
  108. Con::warnf("Could not get module dependency '%d' as it is out of range.", dependencyIndex);
  109. return false;
  110. }
  111. // Fetch module dependency.
  112. dependency = mDependencies[dependencyIndex];
  113. return true;
  114. }
  115. //-----------------------------------------------------------------------------
  116. bool ModuleDefinition::addDependency( const char* pModuleId, const U32 versionId )
  117. {
  118. // Fetch module Id.
  119. StringTableEntry moduleId = StringTable->insert( pModuleId );
  120. // Do we have any existing dependencies?
  121. if ( mDependencies.size() > 0 )
  122. {
  123. // Yes, so is the module Id already a dependency?
  124. for( typeModuleDependencyVector::iterator dependencyItr = mDependencies.begin(); dependencyItr != mDependencies.end(); ++dependencyItr )
  125. {
  126. // Skip if not the same module Id.
  127. if ( dependencyItr->mModuleId != moduleId )
  128. continue;
  129. // Dependency already exists so warn.
  130. Con::warnf("Could not add dependency of module Id '%s' at version Id '%d' as the module Id is already a dependency.", pModuleId, versionId );
  131. return false;
  132. }
  133. }
  134. // Populate module dependency.
  135. ModuleDefinition::ModuleDependency dependency( moduleId, versionId );
  136. // Store dependency.
  137. mDependencies.push_back( dependency );
  138. return true;
  139. }
  140. //-----------------------------------------------------------------------------
  141. bool ModuleDefinition::removeDependency( const char* pModuleId )
  142. {
  143. // Fetch module Id.
  144. StringTableEntry moduleId = StringTable->insert( pModuleId );
  145. // Do we have any existing dependencies?
  146. if ( mDependencies.size() > 0 )
  147. {
  148. // Yes, so is the module Id a dependency?
  149. for( typeModuleDependencyVector::iterator dependencyItr = mDependencies.begin(); dependencyItr != mDependencies.end(); ++dependencyItr )
  150. {
  151. // Skip if not the same module Id.
  152. if ( dependencyItr->mModuleId != moduleId )
  153. continue;
  154. // Remove dependency.
  155. mDependencies.erase( dependencyItr );
  156. return true;
  157. }
  158. }
  159. // No, so warn.
  160. Con::warnf("Could not remove dependency of module Id '%s' as the module Id is not a dependency.", pModuleId );
  161. return false;
  162. }
  163. //-----------------------------------------------------------------------------
  164. bool ModuleDefinition::save( void )
  165. {
  166. // Does the module have a file-path yet?
  167. if (mModuleFilePath == StringTable->EmptyString())
  168. {
  169. // No, so warn.
  170. Con::warnf("Save() - Cannot save module definition '%s' as it does not have a file-path.", mModuleId );
  171. return false;
  172. }
  173. // Save the module file.
  174. Taml taml;
  175. return taml.write( this, mModuleFilePath );
  176. }