moduleDefinition.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. /// Module location.
  147. inline void setModulePath( const char* pModulePath ) { if ( checkUnlocked() ) { mModulePath = StringTable->insert(pModulePath); } }
  148. inline StringTableEntry getModulePath( void ) const { return mModulePath; }
  149. inline void setModuleFile( const char* pModuleDefinitionFile ) { if ( checkUnlocked() ) { mModuleFile = StringTable->insert(pModuleDefinitionFile); } }
  150. inline StringTableEntry getModuleFile( void ) const { return mModuleFile; }
  151. inline void setModuleFilePath( const char* pModuleDefinitionFilePath ) { if ( checkUnlocked() ) { mModuleFilePath = StringTable->insert(pModuleDefinitionFilePath); } }
  152. inline StringTableEntry getModuleFilePath( void ) const { return mModuleFilePath; }
  153. inline void setModuleScriptFilePath( const char* pModuleScriptFilePath ) { if ( checkUnlocked() ) { mModuleScriptFilePath = StringTable->insert(pModuleScriptFilePath); } }
  154. inline StringTableEntry getModuleScriptFilePath( void ) const { return mModuleScriptFilePath; }
  155. /// Specialized dependency control.
  156. inline U32 getDependencyCount( void ) const { return mDependencies.size(); }
  157. bool getDependency( const U32 dependencyIndex, ModuleDependency& dependency ) const;
  158. bool addDependency( const char* pModuleId, const U32 versionId );
  159. bool removeDependency( const char* pModuleId );
  160. /// Miscellaneous.
  161. inline void setSignature( const char* pSignature ) { if ( checkUnlocked() ) { mSignature = StringTable->insert(pSignature); } }
  162. inline StringTableEntry getSignature( void ) const { return mSignature; }
  163. inline void increaseLoadCount( void ) { ++mLoadCount; }
  164. inline void reduceLoadCount( void ) { --mLoadCount; }
  165. inline S32 getLoadCount( void ) const { return mLoadCount; }
  166. inline void setLocked( const bool status ) { mLocked = status; }
  167. inline bool getLocked( void ) const { return mLocked; }
  168. inline ModuleManager* getModuleManager( void ) const { return mpModuleManager; }
  169. bool save( void );
  170. /// Declare Console Object.
  171. DECLARE_CONOBJECT( ModuleDefinition );
  172. protected:
  173. static bool setModuleId(void* obj, const char* index, const char* data) { static_cast<ModuleDefinition*>(obj)->setModuleId( data ); return false; }
  174. static bool setVersionId(void* obj, const char* index, const char* data) { static_cast<ModuleDefinition*>(obj)->setVersionId((U32)dAtoi(data)); return false; }
  175. static bool setBuildId(void* obj, const char* index, const char* data) { static_cast<ModuleDefinition*>(obj)->setBuildId((U32)dAtoi(data)); return false; }
  176. static bool writeBuildId( void* obj, StringTableEntry pFieldName ) { return static_cast<ModuleDefinition*>(obj)->getBuildId() != 0; }
  177. static bool setEnabled(void* obj, const char* index, const char* data) { static_cast<ModuleDefinition*>(obj)->setEnabled(dAtob(data)); return false; }
  178. static bool writeEnabled( void* obj, StringTableEntry pFieldName ) { return static_cast<ModuleDefinition*>(obj)->getEnabled() == false; }
  179. static bool setSynchronized(void* obj, const char* index, const char* data) { static_cast<ModuleDefinition*>(obj)->setSynchronized(dAtob(data)); return false; }
  180. static bool writeSynchronized( void* obj, StringTableEntry pFieldName ) { return static_cast<ModuleDefinition*>(obj)->getSynchronized() == true; }
  181. static bool setDeprecated(void* obj, const char* index, const char* data) { static_cast<ModuleDefinition*>(obj)->setDeprecated(dAtob(data)); return false; }
  182. static bool writeDeprecated( void* obj, StringTableEntry pFieldName ) { return static_cast<ModuleDefinition*>(obj)->getDeprecated() == true; }
  183. static bool writeCriticalMerge( void* obj, StringTableEntry pFieldName ){ return static_cast<ModuleDefinition*>(obj)->getCriticalMerge() == true; }
  184. static bool setOverrideExistingObjects(void* obj, const char* index, const char* data) { static_cast<ModuleDefinition*>(obj)->setOverrideExistingObjects(dAtob(data)); return false; }
  185. static bool writeOverrideExistingObjects(void* obj, StringTableEntry pFieldName) { return static_cast<ModuleDefinition*>(obj)->getOverrideExistingObjects() == true; }
  186. static bool setModuleDescription(void* obj, const char* index, const char* data) { static_cast<ModuleDefinition*>(obj)->setModuleDescription(data); return false; }
  187. static bool writeModuleDescription( void* obj, StringTableEntry pFieldName ) { return static_cast<ModuleDefinition*>(obj)->getModuleDescription() != StringTable->EmptyString(); }
  188. static bool setAuthor(void* obj, const char* index, const char* data) { static_cast<ModuleDefinition*>(obj)->setAuthor(data); return false; }
  189. static bool writeAuthor(void* obj, StringTableEntry pFieldName) { return static_cast<ModuleDefinition*>(obj)->getAuthor() != StringTable->EmptyString(); }
  190. static bool setModuleGroup(void* obj, const char* index, const char* data) { static_cast<ModuleDefinition*>(obj)->setModuleGroup(data); return false; }
  191. static bool setModuleType(void* obj, const char* index, const char* data) { static_cast<ModuleDefinition*>(obj)->setModuleType(data); return false; }
  192. static bool writeModuleType(void* obj, StringTableEntry pFieldName) { return static_cast<ModuleDefinition*>(obj)->getModuleType() != StringTable->EmptyString(); }
  193. static bool setScriptFile(void* obj, const char* index, const char* data) { static_cast<ModuleDefinition*>(obj)->setScriptFile(data); return false; }
  194. static bool writeScriptFile(void* obj, StringTableEntry pFieldName) { return static_cast<ModuleDefinition*>(obj)->getScriptFile() != StringTable->EmptyString(); }
  195. static bool setCreateFunction(void* obj, const char* index, const char* data) { static_cast<ModuleDefinition*>(obj)->setCreateFunction(data); return false; }
  196. static bool writeCreateFunction(void* obj, StringTableEntry pFieldName) { return static_cast<ModuleDefinition*>(obj)->getCreateFunction() != StringTable->EmptyString(); }
  197. static bool setDestroyFunction(void* obj, const char* index, const char* data) { static_cast<ModuleDefinition*>(obj)->setDestroyFunction(data); return false; }
  198. static bool writeDestroyFunction(void* obj, StringTableEntry pFieldName) { return static_cast<ModuleDefinition*>(obj)->getDestroyFunction() != StringTable->EmptyString(); }
  199. /// Asset manifest.
  200. static bool setAssetTagsManifest(void* obj, const char* index, const char* data) { static_cast<ModuleDefinition*>(obj)->setAssetTagsManifest(data); return false; }
  201. static bool writeAssetTagsManifest(void* obj, StringTableEntry pFieldName) { return static_cast<ModuleDefinition*>(obj)->getAssetTagsManifest() != StringTable->EmptyString(); }
  202. static const char* getScopeSet(void* obj, const char* data) { return Con::getIntArg(static_cast<ModuleDefinition*>(obj)->getScopeSet()); }
  203. static bool setDependencies(void* obj, const char* index, const char* data)
  204. {
  205. // Fetch module dependencies.
  206. ModuleDefinition::typeModuleDependencyVector moduleDependencies;
  207. // Fetch dependency value.
  208. const char* pDependencyValue = data;
  209. char slotUnit[256];
  210. char slotName[256];
  211. char slotValue[256];
  212. // Fetch definition word count.
  213. const U32 dependencyWordCount = StringUnit::getUnitCount( pDependencyValue, "," );
  214. // Do we have any dependencies specified?
  215. if ( dependencyWordCount > 0 )
  216. {
  217. // Yes, so iterate dependencies.
  218. for ( U32 dependencyIndex = 0; dependencyIndex < dependencyWordCount; ++dependencyIndex )
  219. {
  220. // Fetch slot.
  221. dStrcpy( slotUnit, StringUnit::getUnit( pDependencyValue, dependencyIndex, "," ) );
  222. // Fetch slot name and value.
  223. dStrcpy( slotName, StringUnit::getUnit( slotUnit, 0, "=" ) );
  224. dStrcpy( slotValue, StringUnit::getUnit( slotUnit, 1, "=" ) );
  225. // Fetch module Id.
  226. StringTableEntry moduleId = StringTable->insert( slotName );
  227. // Fetch version Id.
  228. const U32 versionId = slotValue[0] == '*' ? 0 : dAtoi(slotValue);
  229. // Populate module dependency.
  230. ModuleDefinition::ModuleDependency dependency( moduleId, versionId );
  231. // Store dependency.
  232. moduleDependencies.push_back( dependency );
  233. }
  234. }
  235. // Set dependencies.
  236. static_cast<ModuleDefinition*>(obj)->setDependencies( moduleDependencies );
  237. return false;
  238. }
  239. static const char* getDependencies(void* obj, const char* data)
  240. {
  241. // Fetch module dependencies.
  242. const ModuleDefinition::typeModuleDependencyVector& moduleDependencies = static_cast<ModuleDefinition*>(obj)->getDependencies();
  243. // Finish if no dependencies.
  244. if ( moduleDependencies.size() == 0 )
  245. return StringTable->EmptyString();
  246. // Get a return buffer.
  247. const S32 bufferSize = 1024;
  248. char* pReturnBuffer = Con::getReturnBuffer(bufferSize);
  249. pReturnBuffer[0] = '\0';
  250. // Set buffer limits.
  251. char* pValueBuffer = pReturnBuffer;
  252. S32 bufferLeft = bufferSize;
  253. U32 used;
  254. // Iterate module dependencies.
  255. for ( ModuleDefinition::typeModuleDependencyVector::const_iterator dependencyItr = moduleDependencies.begin(); dependencyItr < moduleDependencies.end(); ++dependencyItr )
  256. {
  257. // Fetch module dependency.
  258. const ModuleDefinition::ModuleDependency* pDependency = dependencyItr;
  259. // Fetch version Id.
  260. const char* pVersionId = pDependency->mVersionId == 0 ? "*" : avar("%d", pDependency->mVersionId );
  261. if ( dependencyItr == moduleDependencies.begin() )
  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. else
  269. {
  270. // Write out a field/value pair
  271. used = dSprintf( pValueBuffer, bufferLeft, ",%s=%s", pDependency->mModuleId, pVersionId );
  272. pValueBuffer += used;
  273. bufferLeft -= used;
  274. }
  275. // Sanity.
  276. AssertFatal( bufferLeft > 0, "Cannot format module dependencies as we ran out of buffer." );
  277. }
  278. return pReturnBuffer;
  279. }
  280. static bool writeDependencies( void* obj, StringTableEntry pFieldName ) { return static_cast<ModuleDefinition*>(obj)->getDependencies().size() > 0; }
  281. static const char* getSignature(void* obj, const char* data) { return static_cast<ModuleDefinition*>(obj)->getSignature(); }
  282. };
  283. #endif // _MODULE_DEFINITION_H