Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

237 lines
8.1 KiB

  1. //====== Copyright � 1996-2004, Valve Corporation, All rights reserved. =====//
  2. //
  3. // Describes the way to compile a MDL file (eventual replacement for qc)
  4. //
  5. //===========================================================================//
  6. #ifndef DMEMAKEFILE_H
  7. #define DMEMAKEFILE_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. #include "datamodel/dmelement.h"
  12. #include "datamodel/dmattribute.h"
  13. #include "datamodel/dmattributevar.h"
  14. #include "datamodel/dmehandle.h"
  15. #include "vstdlib/iprocessutils.h"
  16. //-----------------------------------------------------------------------------
  17. // Forward declarations
  18. //-----------------------------------------------------------------------------
  19. class CDmeMakefile;
  20. //-----------------------------------------------------------------------------
  21. // Returns a list of source types and whether there can be only 1 or not
  22. //-----------------------------------------------------------------------------
  23. struct DmeMakefileType_t
  24. {
  25. const char *m_pTypeName;
  26. const char *m_pHumanReadableName;
  27. bool m_bIsSingleton;
  28. const char *m_pDefaultDirectoryID; // NOTE: Use CDmeMakefile::GetDefaultDirectory, passing this in to crack it.
  29. const char *m_pFileFilter;
  30. const char *m_pFileFilterString;
  31. };
  32. //-----------------------------------------------------------------------------
  33. // Describes an asset source; contains a source name + options
  34. //-----------------------------------------------------------------------------
  35. class CDmeSource : public CDmElement
  36. {
  37. DEFINE_ELEMENT( CDmeSource, CDmElement );
  38. public:
  39. // NOTE: Filenames are stored as relative file names in dmesource
  40. // To resolve them to full paths, use CDmeMakefile::GetSourceFullPath
  41. const char *GetRelativeFileName() const;
  42. void SetRelativeFileName( const char *pFileName );
  43. // If this source can be built by another makefile, return the type of makefile
  44. // NOTE: This can be a base class of a number of makefile types
  45. virtual const char **GetSourceMakefileTypes() { return NULL; }
  46. // Sets/gets the makefile that was used to build this source
  47. // NOTE: This information isn't saved and must be reconstructed each frame
  48. void SetDependentMakefile( CDmeMakefile *pMakeFile );
  49. CDmeMakefile *GetDependentMakefile();
  50. // Call this to open the source file in an editor
  51. void OpenEditor();
  52. private:
  53. // The makefile that built this source
  54. CDmeHandle< CDmeMakefile > m_DependentMakefile;
  55. };
  56. inline const char *CDmeSource::GetRelativeFileName() const
  57. {
  58. return m_Name;
  59. }
  60. inline void CDmeSource::SetRelativeFileName( const char *pName )
  61. {
  62. m_Name = pName;
  63. }
  64. //-----------------------------------------------------------------------------
  65. // Describes an asset: something that is compiled from sources
  66. //-----------------------------------------------------------------------------
  67. class CDmeMakefile : public CDmElement
  68. {
  69. DEFINE_ELEMENT( CDmeMakefile, CDmElement );
  70. public:
  71. // NOTE: Adding or removing sources of the specified type will invalidate the index
  72. // NOTE: This index is the same index used in GetSources()
  73. CDmeSource *AddSource( const char *pSourceType, const char *pFullPath );
  74. template< class T >
  75. T* AddSource( const char *pFullPath );
  76. CDmeSource *SetSingleSource( const char *pSourceType, const char *pFullPath );
  77. template< class T >
  78. T* SetSingleSource( const char *pFullPath );
  79. CDmeSource *FindSource( const char *pSourceType, const char *pFullPath );
  80. void RemoveSource( const char *pSourceType, const char *pFullPath );
  81. void RemoveSource( CDmeSource *pSource );
  82. void RemoveAllSources( const char *pSourceType );
  83. bool HasSourceOfType( const char *pSourceType );
  84. // Gets/sets paths associated with sources
  85. void SetSourceFullPath( CDmeSource *pSource, const char *pFullPath );
  86. void GetSourceFullPath( CDmeSource *pSource, char *pFullPath, int nBufLen );
  87. // Returns the output directory we expect to compile files into
  88. bool GetOutputDirectory( char *pFullPath, int nBufLen );
  89. // Returns the output name (output directory + filename, no extension)
  90. bool GetOutputName( char *pFullPath, int nBufLen );
  91. // Call this to change the file the makefile is stored in
  92. // Will make all sources be relative to this path
  93. bool SetFileName( const char *pFileName );
  94. const char *GetFileName() const;
  95. // Gets a list of all sources of a particular type
  96. void GetSources( const char *pSourceType, CUtlVector< CDmeHandle< CDmeSource > > &sources );
  97. template< class T >
  98. void GetSources( CUtlVector< CDmeHandle<T> > &sources );
  99. // Gets a list of all sources, regardless of type
  100. int GetSourceCount();
  101. CDmeSource *GetSource( int nIndex );
  102. virtual DmeMakefileType_t *GetMakefileType() { return NULL; }
  103. virtual DmeMakefileType_t* GetSourceTypes() { Assert(0); return NULL; }
  104. // FIXME: Should we have output types? Not sure...
  105. virtual void GetOutputs( CUtlVector<CUtlString> &fullPaths ) { Assert(0); }
  106. // Converts the m_pDefaultDirectoryID field of the DmeMakefileType_t to a full path
  107. bool GetDefaultDirectory( const char *pDefaultDirectoryID, char *pFullPath, int nBufLen );
  108. // These methods are used to help traverse a dependency graph.
  109. // They work with information that is not saved.
  110. void SetAssociation( CDmeSource *pSource, CDmeMakefile *pSourceMakefile );
  111. CDmeMakefile *FindDependentMakefile( CDmeSource *pSource );
  112. CDmeSource *FindAssociatedSource( CDmeMakefile *pChildMakefile );
  113. CDmElement *GetOutputElement( bool bCreateIfNecessary = false );
  114. // Performs compilation steps
  115. void PreCompile( );
  116. void PostCompile( );
  117. // Dirty, dirty!
  118. bool IsDirty() const;
  119. void SetDirty( bool bDirty );
  120. protected:
  121. // Make all outputs writeable
  122. void MakeOutputsWriteable( );
  123. // Gets the path of the makefile
  124. void GetMakefilePath( char *pFullPath, int nBufLen );
  125. private:
  126. // Inherited classes should re-implement these methods
  127. virtual CDmElement *CreateOutputElement( ) { return NULL; }
  128. virtual void DestroyOutputElement( CDmElement *pOutput ) { }
  129. virtual IProcess* PerformCompilation() { Assert(0); return NULL; }
  130. virtual const char *GetOutputDirectoryID() { return "makefilegamedir:"; }
  131. private:
  132. // Relative path to full path
  133. void RelativePathToFullPath( const char *pRelativePath, char *pFullPath, int nBufLen );
  134. // Fullpath to relative path
  135. void FullPathToRelativePath( const char *pFullPath, char *pRelativePath, int nBufLen );
  136. // Updates the source names to be relative to a particular path
  137. bool UpdateSourceNames( const char *pOldRootDir, const char *pNewRootDir, bool bApplyChanges );
  138. CDmaElementArray< CDmeSource > m_Sources;
  139. CDmeHandle< CDmElement > m_hOutput;
  140. IProcess *m_hCompileProcess;
  141. bool m_bIsDirty;
  142. };
  143. //-----------------------------------------------------------------------------
  144. // Dirty, dirty!
  145. //-----------------------------------------------------------------------------
  146. inline bool CDmeMakefile::IsDirty() const
  147. {
  148. return m_bIsDirty;
  149. }
  150. inline void CDmeMakefile::SetDirty( bool bDirty )
  151. {
  152. m_bIsDirty = bDirty;
  153. }
  154. //-----------------------------------------------------------------------------
  155. // Gets a list of all sources of a particular type
  156. //-----------------------------------------------------------------------------
  157. template< class T >
  158. void CDmeMakefile::GetSources( CUtlVector< CDmeHandle<T> > &sources )
  159. {
  160. int nCount = m_Sources.Count();
  161. sources.EnsureCapacity( nCount );
  162. for ( int i = 0; i < nCount; ++i )
  163. {
  164. if ( m_Sources[i]->IsA( T::GetStaticTypeSymbol() ) )
  165. {
  166. int j = sources.AddToTail();
  167. sources[j] = CastElement<T>( m_Sources[i] );
  168. }
  169. }
  170. }
  171. //-----------------------------------------------------------------------------
  172. // Template version to add a source
  173. //-----------------------------------------------------------------------------
  174. template< class T >
  175. T* CDmeMakefile::AddSource( const char *pFullPath )
  176. {
  177. return CastElement< T >( AddSource( T::GetStaticTypeSymbol().String(), pFullPath ) );
  178. }
  179. //-----------------------------------------------------------------------------
  180. // Template version to set a single source
  181. //-----------------------------------------------------------------------------
  182. template< class T >
  183. T* CDmeMakefile::SetSingleSource( const char *pFullPath )
  184. {
  185. return CastElement< T >( SetSingleSource( g_pDataModel->GetString( T::GetStaticTypeSymbol() ), pFullPath ) );
  186. }
  187. #endif // DMEMAKEFILE_H