moduleDefinition.h 21 KB

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