moduleDefinition.h 20 KB

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