moduleDefinition.h 20 KB

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