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. mModuleDescription( StringTable->EmptyString() ),
  46. mAuthor(StringTable->EmptyString()),
  47. mModuleGroup(StringTable->EmptyString()),
  48. mModuleType(StringTable->EmptyString()),
  49. mScriptFile(StringTable->EmptyString()),
  50. mCreateFunction(StringTable->EmptyString()),
  51. mDestroyFunction(StringTable->EmptyString()),
  52. mAssetTagsManifest(StringTable->EmptyString()),
  53. mModulePath(StringTable->EmptyString()),
  54. mModuleFile(StringTable->EmptyString()),
  55. mModuleFilePath(StringTable->EmptyString()),
  56. mModuleScriptFilePath(StringTable->EmptyString()),
  57. mSignature(StringTable->EmptyString()),
  58. mLoadCount( 0 ),
  59. mLocked( false ),
  60. mScopeSet( 0 ),
  61. mpModuleManager( NULL )
  62. {
  63. // Set Vector Associations.
  64. VECTOR_SET_ASSOCIATION( mDependencies );
  65. VECTOR_SET_ASSOCIATION( mModuleAssets );
  66. }
  67. //-----------------------------------------------------------------------------
  68. void ModuleDefinition::initPersistFields()
  69. {
  70. // Call parent.
  71. Parent::initPersistFields();
  72. addProtectedField("ModuleId", TypeString, Offset(mModuleId, ModuleDefinition), &defaultProtectedSetFn, &defaultProtectedGetFn, "");
  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( "Description", TypeString, Offset(mModuleDescription, ModuleDefinition), &setModuleDescription, &defaultProtectedGetFn, &writeModuleDescription, "The description typically used for debugging purposes but can be used for anything." );
  82. addProtectedField( "Author", TypeString, Offset(mAuthor, ModuleDefinition), &setAuthor, &defaultProtectedGetFn, &writeAuthor, "The author of the module." );
  83. addProtectedField( "Group", TypeString, Offset(mModuleGroup, ModuleDefinition), &setModuleGroup, &defaultProtectedGetFn, "The module group used typically when loading modules as a group." );
  84. 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." );
  85. 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." );
  86. addProtectedField( "ScriptFile", TypeString, Offset(mScriptFile, ModuleDefinition), &setScriptFile, &defaultProtectedGetFn, &writeScriptFile, "The name of the script file to compile when loading the module. Optional." );
  87. 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." );
  88. 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." );
  89. 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." );
  90. 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." );
  91. /// Module location (Read-only).
  92. 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." );
  93. 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." );
  94. 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." );
  95. 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." );
  96. /// Misc.
  97. 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." );
  98. }
  99. //-----------------------------------------------------------------------------
  100. bool ModuleDefinition::getDependency( const U32 dependencyIndex, ModuleDependency& dependency ) const
  101. {
  102. // Is dependency index out of bounds?
  103. if ( dependencyIndex >= (U32)mDependencies.size() )
  104. {
  105. // Yes, so warn.
  106. Con::warnf("Could not get module dependency '%d' as it is out of range.", dependencyIndex);
  107. return false;
  108. }
  109. // Fetch module dependency.
  110. dependency = mDependencies[dependencyIndex];
  111. return true;
  112. }
  113. //-----------------------------------------------------------------------------
  114. bool ModuleDefinition::addDependency( const char* pModuleId, const U32 versionId )
  115. {
  116. // Fetch module Id.
  117. StringTableEntry moduleId = StringTable->insert( pModuleId );
  118. // Do we have any existing dependencies?
  119. if ( mDependencies.size() > 0 )
  120. {
  121. // Yes, so is the module Id already a dependency?
  122. for( typeModuleDependencyVector::iterator dependencyItr = mDependencies.begin(); dependencyItr != mDependencies.end(); ++dependencyItr )
  123. {
  124. // Skip if not the same module Id.
  125. if ( dependencyItr->mModuleId != moduleId )
  126. continue;
  127. // Dependency already exists so warn.
  128. Con::warnf("Could not add dependency of module Id '%s' at version Id '%d' as the module Id is already a dependency.", pModuleId, versionId );
  129. return false;
  130. }
  131. }
  132. // Populate module dependency.
  133. ModuleDefinition::ModuleDependency dependency( moduleId, versionId );
  134. // Store dependency.
  135. mDependencies.push_back( dependency );
  136. return true;
  137. }
  138. //-----------------------------------------------------------------------------
  139. bool ModuleDefinition::removeDependency( const char* pModuleId )
  140. {
  141. // Fetch module Id.
  142. StringTableEntry moduleId = StringTable->insert( pModuleId );
  143. // Do we have any existing dependencies?
  144. if ( mDependencies.size() > 0 )
  145. {
  146. // Yes, so is the module Id a dependency?
  147. for( typeModuleDependencyVector::iterator dependencyItr = mDependencies.begin(); dependencyItr != mDependencies.end(); ++dependencyItr )
  148. {
  149. // Skip if not the same module Id.
  150. if ( dependencyItr->mModuleId != moduleId )
  151. continue;
  152. // Remove dependency.
  153. mDependencies.erase( dependencyItr );
  154. return true;
  155. }
  156. }
  157. // No, so warn.
  158. Con::warnf("Could not remove dependency of module Id '%s' as the module Id is not a dependency.", pModuleId );
  159. return false;
  160. }
  161. //-----------------------------------------------------------------------------
  162. bool ModuleDefinition::save( void )
  163. {
  164. // Does the module have a file-path yet?
  165. if (mModuleFilePath == StringTable->EmptyString())
  166. {
  167. // No, so warn.
  168. Con::warnf("Save() - Cannot save module definition '%s' as it does not have a file-path.", mModuleId );
  169. return false;
  170. }
  171. // Save the module file.
  172. Taml taml;
  173. return taml.write( this, mModuleFilePath );
  174. }