moduleDefinition.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. #ifndef _MODULE_DEFINITION_H
  23. #define _MODULE_DEFINITION_H
  24. #ifndef _ASSET_DEFINITION_H_
  25. #include "assets/assetDefinition.h"
  26. #endif
  27. #ifndef _SIMSET_H_
  28. #include "console/simSet.h"
  29. #endif
  30. #ifndef _SIMBASE_H_
  31. #include "console/simBase.h"
  32. #endif
  33. #ifndef _TVECTOR_H_
  34. #include "core/util/tVector.h"
  35. #endif
  36. #ifndef _STRINGUNIT_H_
  37. #include "core/strings/stringUnit.h"
  38. #endif
  39. //-----------------------------------------------------------------------------
  40. class ModuleManager;
  41. //-----------------------------------------------------------------------------
  42. /// @ingroup moduleGroup
  43. /// @see moduleGroup
  44. class ModuleDefinition : public SimSet
  45. {
  46. friend class ModuleManager;
  47. private:
  48. typedef SimSet Parent;
  49. public:
  50. /// Module dependency.
  51. struct ModuleDependency
  52. {
  53. ModuleDependency() :
  54. mModuleId( StringTable->EmptyString() ),
  55. mVersionId( 0 )
  56. {
  57. }
  58. ModuleDependency( StringTableEntry moduleId, const U32 versionId ) :
  59. mModuleId( moduleId ),
  60. mVersionId( versionId )
  61. {
  62. }
  63. StringTableEntry mModuleId;
  64. U32 mVersionId;
  65. };
  66. typedef Vector<ModuleDependency> typeModuleDependencyVector;
  67. typedef Vector<AssetDefinition*> typeModuleAssetsVector;
  68. private:
  69. /// Module definition.
  70. StringTableEntry mModuleId;
  71. U32 mVersionId;
  72. U32 mBuildId;
  73. bool mEnabled;
  74. bool mSynchronized;
  75. bool mDeprecated;
  76. bool mCriticalMerge;
  77. bool mOverrideExistingObjects;
  78. StringTableEntry mModuleDescription;
  79. StringTableEntry mAuthor;;
  80. StringTableEntry mModuleGroup;
  81. StringTableEntry mModuleType;
  82. typeModuleDependencyVector mDependencies;
  83. StringTableEntry mScriptFile;
  84. StringTableEntry mCreateFunction;
  85. StringTableEntry mDestroyFunction;
  86. /// Modules assets.
  87. StringTableEntry mAssetTagsManifest;
  88. typeModuleAssetsVector mModuleAssets;
  89. /// Module location.
  90. StringTableEntry mModulePath;
  91. StringTableEntry mModuleFile;
  92. StringTableEntry mModuleFilePath;
  93. StringTableEntry mModuleScriptFilePath;
  94. /// Miscellaneous.
  95. StringTableEntry mSignature;
  96. S32 mLoadCount;
  97. SimObjectId mScopeSet;
  98. bool mLocked;
  99. ModuleManager* mpModuleManager;
  100. F32 mPriority;
  101. private:
  102. inline bool checkUnlocked( void ) const { if ( mLocked ) { Con::warnf("Ignoring changes for locked module definition."); } return !mLocked; }
  103. inline void setModuleManager( ModuleManager* pModuleManager ) { mpModuleManager = pModuleManager; }
  104. public:
  105. ModuleDefinition();
  106. virtual ~ModuleDefinition() {}
  107. /// Engine.
  108. static void initPersistFields();
  109. /// Module definition.
  110. inline void setModuleId( const char* pModuleId ) { if ( checkUnlocked() ) { mModuleId = StringTable->insert(pModuleId); } }
  111. inline StringTableEntry getModuleId( void ) const { return mModuleId; }
  112. inline void setVersionId( const U32 versionId ) { if ( checkUnlocked() ) { mVersionId = versionId; } }
  113. inline U32 getVersionId( void ) const { return mVersionId; }
  114. inline void setBuildId( const U32 buildId ) { if ( checkUnlocked() ) { mBuildId = buildId; } }
  115. inline U32 getBuildId( void ) const { return mBuildId; }
  116. inline void setEnabled( const bool enabled ) { if ( checkUnlocked() ) { mEnabled = enabled; } }
  117. inline bool getEnabled( void ) const { return mEnabled; }
  118. inline void setSynchronized( const bool synchronized ) { if ( checkUnlocked() ) { mSynchronized = synchronized; } }
  119. inline bool getSynchronized( void ) const { return mSynchronized; }
  120. inline void setDeprecated( const bool deprecated ) { if ( checkUnlocked() ) { mDeprecated = deprecated; } }
  121. inline bool getDeprecated( void ) const { return mDeprecated; }
  122. inline void setCriticalMerge( const bool mergeCritical ) { if ( checkUnlocked() ) { mCriticalMerge = mergeCritical; } }
  123. inline bool getCriticalMerge( void ) const { return mCriticalMerge; }
  124. inline void setOverrideExistingObjects(const bool overrideExistingObj) { if (checkUnlocked()) { mOverrideExistingObjects = overrideExistingObj; } }
  125. inline bool getOverrideExistingObjects(void) const { return mOverrideExistingObjects; }
  126. inline void setModuleDescription( const char* pModuleDescription ) { if ( checkUnlocked() ) { mModuleDescription = StringTable->insert(pModuleDescription); } }
  127. inline StringTableEntry getModuleDescription( void ) const { return mModuleDescription; }
  128. inline void setAuthor( const char* pAuthor ) { if ( checkUnlocked() ) { mAuthor = StringTable->insert(pAuthor); } }
  129. inline StringTableEntry getAuthor( void ) const { return mAuthor; }
  130. inline void setModuleGroup( const char* pModuleGroup ) { if ( checkUnlocked() ) { mModuleGroup = StringTable->insert(pModuleGroup); } }
  131. inline StringTableEntry getModuleGroup( void ) const { return mModuleGroup; }
  132. inline void setModuleType( const char* pModuleType ) { if ( checkUnlocked() ) { mModuleType = StringTable->insert(pModuleType); } }
  133. inline StringTableEntry getModuleType( void ) const { return mModuleType; }
  134. inline void setDependencies( const typeModuleDependencyVector& dependencies ) { if ( checkUnlocked() ) { mDependencies.clear(); mDependencies.merge(dependencies); } }
  135. inline const typeModuleDependencyVector& getDependencies( void ) const { return mDependencies; }
  136. inline void setScriptFile( const char* pScriptFile ) { if ( checkUnlocked() ) { mScriptFile = StringTable->insert(pScriptFile); } }
  137. inline StringTableEntry getScriptFile( void ) const { return mScriptFile; }
  138. inline void setCreateFunction( const char* pCreateFunction ) { if ( checkUnlocked() ) { mCreateFunction = StringTable->insert(pCreateFunction); } }
  139. inline StringTableEntry getCreateFunction( void ) const { return mCreateFunction; }
  140. inline void setDestroyFunction( const char* pDestroyFunction ) { if ( checkUnlocked() ) { mDestroyFunction = StringTable->insert(pDestroyFunction); } }
  141. inline StringTableEntry getDestroyFunction( void ) const { return mDestroyFunction; }
  142. inline SimObjectId getScopeSet( void ) const { return mScopeSet; }
  143. /// Module assets.
  144. inline void setAssetTagsManifest( const char* pTagsAssetManifest ) { if ( checkUnlocked() ) { mAssetTagsManifest = StringTable->insert(pTagsAssetManifest); } }
  145. inline StringTableEntry getAssetTagsManifest( void ) const { return mAssetTagsManifest; }
  146. inline typeModuleAssetsVector& getModuleAssets( void ) { return mModuleAssets; }
  147. void addDeclaredAsset(AssetDefinition* asset) { mModuleAssets.push_back(asset); }
  148. /// Module location.
  149. inline void setModulePath( const char* pModulePath ) { if ( checkUnlocked() ) { mModulePath = StringTable->insert(pModulePath); } }
  150. inline StringTableEntry getModulePath( void ) const { return mModulePath; }
  151. inline void setModuleFile( const char* pModuleDefinitionFile ) { if ( checkUnlocked() ) { mModuleFile = StringTable->insert(pModuleDefinitionFile); } }
  152. inline StringTableEntry getModuleFile( void ) const { return mModuleFile; }
  153. inline void setModuleFilePath( const char* pModuleDefinitionFilePath ) { if ( checkUnlocked() ) { mModuleFilePath = StringTable->insert(pModuleDefinitionFilePath); } }
  154. inline StringTableEntry getModuleFilePath( void ) const { return mModuleFilePath; }
  155. inline void setModuleScriptFilePath( const char* pModuleScriptFilePath ) { if ( checkUnlocked() ) { mModuleScriptFilePath = StringTable->insert(pModuleScriptFilePath); } }
  156. inline StringTableEntry getModuleScriptFilePath( void ) const { return mModuleScriptFilePath; }
  157. /// Specialized dependency control.
  158. inline U32 getDependencyCount( void ) const { return mDependencies.size(); }
  159. bool getDependency( const U32 dependencyIndex, ModuleDependency& dependency ) const;
  160. bool addDependency( const char* pModuleId, const U32 versionId );
  161. bool removeDependency( const char* pModuleId );
  162. /// Miscellaneous.
  163. inline void setSignature( const char* pSignature ) { if ( checkUnlocked() ) { mSignature = StringTable->insert(pSignature); } }
  164. inline StringTableEntry getSignature( void ) const { return mSignature; }
  165. inline void increaseLoadCount( void ) { ++mLoadCount; }
  166. inline void reduceLoadCount( void ) { --mLoadCount; }
  167. inline S32 getLoadCount( void ) const { return mLoadCount; }
  168. inline void setModuleLocked( const bool status ) { mLocked = status; }
  169. inline bool getModuleLocked( void ) const { return mLocked; }
  170. inline ModuleManager* getModuleManager( void ) const { return mpModuleManager; }
  171. inline void setPriority(const F32 pPriority) { if (checkUnlocked()) { mPriority = pPriority; } }
  172. inline F32 getPriority(void) const { return mPriority; }
  173. using Parent::save;
  174. bool save( void );
  175. /// Declare Console Object.
  176. DECLARE_CONOBJECT( ModuleDefinition );
  177. protected:
  178. static bool setModuleId(void* obj, const char* index, const char* data) { static_cast<ModuleDefinition*>(obj)->setModuleId( data ); return false; }
  179. static bool setVersionId(void* obj, const char* index, const char* data) { static_cast<ModuleDefinition*>(obj)->setVersionId((U32)dAtoi(data)); return false; }
  180. static bool setBuildId(void* obj, const char* index, const char* data) { static_cast<ModuleDefinition*>(obj)->setBuildId((U32)dAtoi(data)); return false; }
  181. static bool writeBuildId( void* obj, StringTableEntry pFieldName ) { return static_cast<ModuleDefinition*>(obj)->getBuildId() != 0; }
  182. static bool setEnabled(void* obj, const char* index, const char* data) { static_cast<ModuleDefinition*>(obj)->setEnabled(dAtob(data)); return false; }
  183. static bool writeEnabled( void* obj, StringTableEntry pFieldName ) { return static_cast<ModuleDefinition*>(obj)->getEnabled() == false; }
  184. static bool setSynchronized(void* obj, const char* index, const char* data) { static_cast<ModuleDefinition*>(obj)->setSynchronized(dAtob(data)); return false; }
  185. static bool writeSynchronized( void* obj, StringTableEntry pFieldName ) { return static_cast<ModuleDefinition*>(obj)->getSynchronized() == true; }
  186. static bool setDeprecated(void* obj, const char* index, const char* data) { static_cast<ModuleDefinition*>(obj)->setDeprecated(dAtob(data)); return false; }
  187. static bool writeDeprecated( void* obj, StringTableEntry pFieldName ) { return static_cast<ModuleDefinition*>(obj)->getDeprecated() == true; }
  188. static bool writeCriticalMerge( void* obj, StringTableEntry pFieldName ){ return static_cast<ModuleDefinition*>(obj)->getCriticalMerge() == true; }
  189. static bool setOverrideExistingObjects(void* obj, const char* index, const char* data) { static_cast<ModuleDefinition*>(obj)->setOverrideExistingObjects(dAtob(data)); return false; }
  190. static bool writeOverrideExistingObjects(void* obj, StringTableEntry pFieldName) { return static_cast<ModuleDefinition*>(obj)->getOverrideExistingObjects() == true; }
  191. static bool setModuleDescription(void* obj, const char* index, const char* data) { static_cast<ModuleDefinition*>(obj)->setModuleDescription(data); return false; }
  192. static bool writeModuleDescription( void* obj, StringTableEntry pFieldName ) { return static_cast<ModuleDefinition*>(obj)->getModuleDescription() != StringTable->EmptyString(); }
  193. static bool setAuthor(void* obj, const char* index, const char* data) { static_cast<ModuleDefinition*>(obj)->setAuthor(data); return false; }
  194. static bool writeAuthor(void* obj, StringTableEntry pFieldName) { return static_cast<ModuleDefinition*>(obj)->getAuthor() != StringTable->EmptyString(); }
  195. static bool setModuleGroup(void* obj, const char* index, const char* data) { static_cast<ModuleDefinition*>(obj)->setModuleGroup(data); return false; }
  196. static bool setModuleType(void* obj, const char* index, const char* data) { static_cast<ModuleDefinition*>(obj)->setModuleType(data); return false; }
  197. static bool writeModuleType(void* obj, StringTableEntry pFieldName) { return static_cast<ModuleDefinition*>(obj)->getModuleType() != StringTable->EmptyString(); }
  198. static bool setScriptFile(void* obj, const char* index, const char* data) { static_cast<ModuleDefinition*>(obj)->setScriptFile(data); return false; }
  199. static bool writeScriptFile(void* obj, StringTableEntry pFieldName) { return static_cast<ModuleDefinition*>(obj)->getScriptFile() != StringTable->EmptyString(); }
  200. static bool setCreateFunction(void* obj, const char* index, const char* data) { static_cast<ModuleDefinition*>(obj)->setCreateFunction(data); return false; }
  201. static bool writeCreateFunction(void* obj, StringTableEntry pFieldName) { return static_cast<ModuleDefinition*>(obj)->getCreateFunction() != StringTable->EmptyString(); }
  202. static bool setDestroyFunction(void* obj, const char* index, const char* data) { static_cast<ModuleDefinition*>(obj)->setDestroyFunction(data); return false; }
  203. static bool writeDestroyFunction(void* obj, StringTableEntry pFieldName) { return static_cast<ModuleDefinition*>(obj)->getDestroyFunction() != StringTable->EmptyString(); }
  204. /// Asset manifest.
  205. static bool setAssetTagsManifest(void* obj, const char* index, const char* data) { static_cast<ModuleDefinition*>(obj)->setAssetTagsManifest(data); return false; }
  206. static bool writeAssetTagsManifest(void* obj, StringTableEntry pFieldName) { return static_cast<ModuleDefinition*>(obj)->getAssetTagsManifest() != StringTable->EmptyString(); }
  207. static const char* getScopeSet(void* obj, const char* data) { return Con::getIntArg(static_cast<ModuleDefinition*>(obj)->getScopeSet()); }
  208. static bool setDependencies(void* obj, const char* index, const char* data)
  209. {
  210. // Fetch module dependencies.
  211. ModuleDefinition::typeModuleDependencyVector moduleDependencies;
  212. // Fetch dependency value.
  213. const char* pDependencyValue = data;
  214. char slotUnit[256];
  215. char slotName[256];
  216. char slotValue[256];
  217. // Fetch definition word count.
  218. const U32 dependencyWordCount = StringUnit::getUnitCount( pDependencyValue, "," );
  219. // Do we have any dependencies specified?
  220. if ( dependencyWordCount > 0 )
  221. {
  222. // Yes, so iterate dependencies.
  223. for ( U32 dependencyIndex = 0; dependencyIndex < dependencyWordCount; ++dependencyIndex )
  224. {
  225. // Fetch slot.
  226. dStrcpy( slotUnit, StringUnit::getUnit( pDependencyValue, dependencyIndex, "," ), 256 );
  227. // Fetch slot name and value.
  228. dStrcpy( slotName, StringUnit::getUnit( slotUnit, 0, "=" ), 256 );
  229. dStrcpy( slotValue, StringUnit::getUnit( slotUnit, 1, "=" ), 256 );
  230. // Fetch module Id.
  231. StringTableEntry moduleId = StringTable->insert( slotName );
  232. // Fetch version Id.
  233. const U32 versionId = slotValue[0] == '*' ? 0 : dAtoi(slotValue);
  234. // Populate module dependency.
  235. ModuleDefinition::ModuleDependency dependency( moduleId, versionId );
  236. // Store dependency.
  237. moduleDependencies.push_back( dependency );
  238. }
  239. }
  240. // Set dependencies.
  241. static_cast<ModuleDefinition*>(obj)->setDependencies( moduleDependencies );
  242. return false;
  243. }
  244. static const char* getDependencies(void* obj, const char* data)
  245. {
  246. // Fetch module dependencies.
  247. const ModuleDefinition::typeModuleDependencyVector& moduleDependencies = static_cast<ModuleDefinition*>(obj)->getDependencies();
  248. // Finish if no dependencies.
  249. if ( moduleDependencies.size() == 0 )
  250. return StringTable->EmptyString();
  251. // Get a return buffer.
  252. const S32 bufferSize = 1024;
  253. char* pReturnBuffer = Con::getReturnBuffer(bufferSize);
  254. pReturnBuffer[0] = '\0';
  255. // Set buffer limits.
  256. char* pValueBuffer = pReturnBuffer;
  257. S32 bufferLeft = bufferSize;
  258. U32 used;
  259. // Iterate module dependencies.
  260. for ( ModuleDefinition::typeModuleDependencyVector::const_iterator dependencyItr = moduleDependencies.begin(); dependencyItr < moduleDependencies.end(); ++dependencyItr )
  261. {
  262. // Fetch module dependency.
  263. const ModuleDefinition::ModuleDependency* pDependency = dependencyItr;
  264. // Fetch version Id.
  265. const char* pVersionId = pDependency->mVersionId == 0 ? "*" : avar("%d", pDependency->mVersionId );
  266. if ( dependencyItr == moduleDependencies.begin() )
  267. {
  268. // Write out a field/value pair
  269. used = dSprintf( pValueBuffer, bufferLeft, "%s=%s", pDependency->mModuleId, pVersionId );
  270. pValueBuffer += used;
  271. bufferLeft -= used;
  272. }
  273. else
  274. {
  275. // Write out a field/value pair
  276. used = dSprintf( pValueBuffer, bufferLeft, ",%s=%s", pDependency->mModuleId, pVersionId );
  277. pValueBuffer += used;
  278. bufferLeft -= used;
  279. }
  280. // Sanity.
  281. AssertFatal( bufferLeft > 0, "Cannot format module dependencies as we ran out of buffer." );
  282. }
  283. return pReturnBuffer;
  284. }
  285. static bool writeDependencies( void* obj, StringTableEntry pFieldName ) { return static_cast<ModuleDefinition*>(obj)->getDependencies().size() > 0; }
  286. static const char* getSignature(void* obj, const char* data) { return static_cast<ModuleDefinition*>(obj)->getSignature(); }
  287. static bool setPriority(void* obj, const char* index, const char* data) { static_cast<ModuleDefinition*>(obj)->setPriority((F32)dAtof(data)); return false; }
  288. };
  289. #endif // _MODULE_DEFINITION_H