home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2006 March / Gamestar_82_2006-03_dvd.iso / DVDStar / Editace / quake4_sdkv10.exe / source / framework / declManager.h < prev    next >
C/C++ Source or Header  |  2005-11-14  |  18KB  |  454 lines

  1. // Copyright (C) 2004 Id Software, Inc.
  2. //
  3.  
  4. #ifndef __DECLMANAGER_H__
  5. #define __DECLMANAGER_H__
  6.  
  7. /*
  8. ===============================================================================
  9.  
  10.     Declaration Manager
  11.  
  12.     All "small text" data types, like materials, sound shaders, fx files,
  13.     entity defs, etc. are managed uniformly, allowing reloading, purging,
  14.     listing, printing, etc. All "large text" data types that never have more
  15.     than one declaration in a given file, like maps, models, AAS files, etc.
  16.     are not handled here.
  17.  
  18.     A decl will never, ever go away once it is created. The manager is
  19.     garranteed to always return the same decl pointer for a decl type/name
  20.     combination. The index of a decl in the per type list also stays the
  21.     same throughout the lifetime of the engine. Although the pointer to
  22.     a decl always stays the same, one should never maintain pointers to
  23.     data inside decls. The data stored in a decl is not garranteed to stay
  24.     the same for more than one engine frame.
  25.  
  26.     The decl indexes of explicitely defined decls are garrenteed to be
  27.     consistent based on the parsed decl files. However, the indexes of
  28.     implicit decls may be different based on the order in which levels
  29.     are loaded.
  30.  
  31.     The decl namespaces are separate for each type. Comments for decls go
  32.     above the text definition to keep them associated with the proper decl.
  33.  
  34.     During decl parsing, errors should never be issued, only warnings
  35.     followed by a call to MakeDefault().
  36.  
  37. ===============================================================================
  38. */
  39.  
  40. typedef enum {
  41.     DECL_TABLE                = 0,
  42.     DECL_MATERIAL,
  43.     DECL_SKIN,
  44.     DECL_SOUND,
  45.     DECL_ENTITYDEF,
  46.     DECL_MODELDEF,
  47. // RAVEN BEGIN
  48. // jscott: added new decls
  49.     DECL_MATERIALTYPE,
  50.     DECL_LIPSYNC,
  51.     DECL_PLAYBACK,
  52.     DECL_EFFECT,
  53. // rjohnson: camera is now contained in a def for frame commands
  54.     DECL_CAMERADEF,
  55. // jscott: don't use these
  56. //    DECL_FX,
  57. //    DECL_PARTICLE,
  58. // RAVEN END
  59.     DECL_AF,
  60.     DECL_PDA,
  61.     DECL_VIDEO,
  62.     DECL_AUDIO,
  63.     DECL_EMAIL,
  64.     DECL_MODELEXPORT,
  65.     DECL_MAPDEF,
  66.  
  67.     // new decl types can be added here
  68.  
  69.     DECL_MAX_TYPES            = 32
  70. } declType_t;
  71.  
  72. typedef enum {
  73.     DS_UNPARSED,
  74.     DS_DEFAULTED,            // set if a parse failed due to an error, or the lack of any source
  75.     DS_PARSED
  76. } declState_t;
  77.  
  78. const int DECL_LEXER_FLAGS    =    LEXFL_NOSTRINGCONCAT |                // multiple strings seperated by whitespaces are not concatenated
  79.                                 LEXFL_NOSTRINGESCAPECHARS |            // no escape characters inside strings
  80.                                 LEXFL_ALLOWPATHNAMES |                // allow path seperators in names
  81.                                 LEXFL_ALLOWMULTICHARLITERALS |        // allow multi character literals
  82.                                 LEXFL_ALLOWBACKSLASHSTRINGCONCAT |    // allow multiple strings seperated by '\' to be concatenated
  83.                                 LEXFL_NOFATALERRORS;                // just set a flag instead of fatal erroring
  84.  
  85.  
  86. class idDeclBase {
  87. public:
  88.     virtual                 ~idDeclBase() {};
  89.     virtual const char *    GetName( void ) const = 0;
  90.     virtual declType_t        GetType( void ) const = 0;
  91.     virtual declState_t        GetState( void ) const = 0;
  92.     virtual bool            IsImplicit( void ) const = 0;
  93.     virtual bool            IsValid( void ) const = 0;
  94.     virtual void            Invalidate( void ) = 0;
  95.     virtual void            Reload( void ) = 0;
  96.     virtual void            EnsureNotPurged( void ) = 0;
  97.     virtual int                Index( void ) const = 0;
  98.     virtual int                GetLineNum( void ) const = 0;
  99.     virtual const char *    GetFileName( void ) const = 0;
  100.     virtual void            GetText( char *text ) const = 0;
  101.     virtual int                GetTextLength( void ) const = 0;
  102. // RAVEN BEGIN
  103.     virtual int                GetCompressedLength( void ) const = 0;
  104. // RAVEN END
  105.     virtual void            SetText( const char *text ) = 0;
  106.     virtual bool            ReplaceSourceFileText( void ) = 0;
  107.     virtual bool            SourceFileChanged( void ) const = 0;
  108.     virtual void            MakeDefault( void ) = 0;
  109.     virtual bool            EverReferenced( void ) const = 0;
  110. // RAVEN BEGIN
  111.     virtual void            SetReferencedThisLevel( void ) = 0;
  112. // RAVEN END
  113.     virtual bool            SetDefaultText( void ) = 0;
  114.     virtual const char *    DefaultDefinition( void ) const = 0;
  115.     virtual bool            Parse( const char *text, const int textLength, bool noCaching ) = 0;
  116.     virtual void            FreeData( void ) = 0;
  117.     virtual size_t            Size( void ) const = 0;
  118.     virtual void            List( void ) const = 0;
  119.     virtual void            Print( void ) const = 0;
  120. // RAVEN BEGIN
  121. // jscott: to prevent a recursive crash
  122.     virtual    bool            RebuildTextSource( void ) { return( false ); }
  123. // scork: Validation call for detailed error-reporting
  124.     virtual bool            Validate( const char *psText, int iLength, idStr &strReportTo ) const = 0;
  125. // RAVEN END
  126. };
  127.  
  128. // RAVEN BEGIN
  129. // jscott: for guides
  130. #define MAX_GUIDE_PARMS                20
  131. #define    MAX_GUIDE_SHADER_SIZE        20480
  132.  
  133. class rvDeclGuide
  134. {
  135. private:
  136.     idStr        mName;
  137.     idStr        mParms[MAX_GUIDE_PARMS];
  138.     idStr        mDefinition;
  139.     int            mNumParms;
  140.  
  141. public:
  142.                 rvDeclGuide( idStr &name );
  143.                 ~rvDeclGuide( void );
  144.  
  145.     const char    *GetName( void ) const { return( mName.c_str() ); }
  146.     int            GetNumParms( void ) const { return( mNumParms ); }
  147.     const char    *GetParm( int index ) const { assert( index < mNumParms ); return( mParms[index].c_str() ); }
  148.  
  149.     void        SetParm( int index, const char *value );
  150.     void        RemoveOuterBracing( void );
  151.     void        Parse( idLexer *src );
  152.     bool        Evaluate( idLexer *src, idStr &definition );
  153. };
  154. // RAVEN END
  155.  
  156. class idDecl {
  157. public:
  158.                             // The constructor should initialize variables such that
  159.                             // an immediate call to FreeData() does no harm.
  160.                             idDecl( void ) { base = NULL; }
  161.     virtual                 ~idDecl( void ) {};
  162.  
  163.                             // Returns the name of the decl.
  164.     const char *            GetName( void ) const { return base->GetName(); }
  165.  
  166.                             // Returns the decl type.
  167.     declType_t                GetType( void ) const { return base->GetType(); }
  168.  
  169.                             // Returns the decl state which is usefull for finding out if a decl defaulted.
  170.     declState_t                GetState( void ) const { return base->GetState(); }
  171.  
  172.                             // Returns true if the decl was defaulted or the text was created with a call to SetDefaultText.
  173.     bool                    IsImplicit( void ) const { return base->IsImplicit(); }
  174.  
  175.                             // The only way non-manager code can have an invalid decl is if the *ByIndex()
  176.                             // call was used with forceParse = false to walk the lists to look at names
  177.                             // without touching the media.
  178.     bool                    IsValid( void ) const { return base->IsValid(); }
  179.  
  180.                             // Sets state back to unparsed.
  181.                             // Used by decl editors to undo any changes to the decl.
  182.     void                    Invalidate( void ) { base->Invalidate(); }
  183.  
  184.                             // if a pointer might possible be stale from a previous level,
  185.                             // call this to have it re-parsed
  186.     void                    EnsureNotPurged( void ) { base->EnsureNotPurged(); }
  187.  
  188.                             // Returns the index in the per-type list.
  189.     int                        Index( void ) const { return base->Index(); }
  190.  
  191.                             // Returns the line number the decl starts.
  192.     int                        GetLineNum( void ) const { return base->GetLineNum(); }
  193.  
  194.                             // Returns the name of the file in which the decl is defined.
  195.     const char *            GetFileName( void ) const { return base->GetFileName(); }
  196.  
  197.                             // Returns the decl text.
  198.     void                    GetText( char *text ) const { base->GetText( text ); }
  199.  
  200.                             // Returns the length of the decl text.
  201.     int                        GetTextLength( void ) const { return base->GetTextLength(); }
  202.  
  203.                             // Returns the compressed length of the decl text.
  204.     int                        GetCompressedLength( void ) const { return( base->GetCompressedLength() ); }
  205.  
  206.                             // Sets new decl text.
  207.     void                    SetText( const char *text ) { base->SetText( text ); }
  208.  
  209.                             // Saves out new text for the decl.
  210.                             // Used by decl editors to replace the decl text in the source file.
  211.     bool                    ReplaceSourceFileText( void ) { return base->ReplaceSourceFileText(); }
  212.  
  213.                             // Returns true if the source file changed since it was loaded and parsed.
  214.     bool                    SourceFileChanged( void ) const { return base->SourceFileChanged(); }
  215.  
  216.                             // Frees data and makes the decl a default.
  217.     void                    MakeDefault( void ) { base->MakeDefault(); }
  218.  
  219.                             // Returns true if the decl was ever referenced.
  220.     bool                    EverReferenced( void ) const { return base->EverReferenced(); }
  221.  
  222. public:
  223.                             // Sets textSource to a default text if necessary.
  224.                             // This may be overridden to provide a default definition based on the
  225.                             // decl name. For instance materials may default to an implicit definition
  226.                             // using a texture with the same name as the decl.
  227.     virtual bool            SetDefaultText( void ) { return base->SetDefaultText(); }
  228.  
  229.                             // Each declaration type must have a default string that it is guaranteed
  230.                             // to parse acceptably. When a decl is not explicitly found, is purged, or
  231.                             // has an error while parsing, MakeDefault() will do a FreeData(), then a
  232.                             // Parse() with DefaultDefinition(). The defaultDefintion should start with
  233.                             // an open brace and end with a close brace.
  234.     virtual const char *    DefaultDefinition( void ) const { return base->DefaultDefinition(); }
  235.  
  236.                             // The manager will have already parsed past the type, name and opening brace.
  237.                             // All necessary media will be touched before return.
  238.                             // The manager will have called FreeData() before issuing a Parse().
  239.                             // The subclass can call MakeDefault() internally at any point if
  240.                             // there are parse errors.
  241.     virtual bool            Parse( const char *text, const int textLength, bool noCaching ) { return base->Parse( text, textLength, noCaching ); }
  242.  
  243.                             // Frees any pointers held by the subclass. This may be called before
  244.                             // any Parse(), so the constructor must have set sane values. The decl will be
  245.                             // invalid after issuing this call, but it will always be immediately followed
  246.                             // by a Parse()
  247.     virtual void            FreeData( void ) { base->FreeData(); }
  248.  
  249.                             // Returns the size of the decl in memory.
  250.     virtual size_t            Size( void ) const { return base->Size(); }
  251.  
  252.                             // If this isn't overridden, it will just print the decl name.
  253.                             // The manager will have printed 7 characters on the line already,
  254.                             // containing the reference state and index number.
  255.     virtual void            List( void ) const { base->List(); }
  256.  
  257.                             // The print function will already have dumped the text source
  258.                             // and common data, subclasses can override this to dump more
  259.                             // explicit data.
  260.     virtual void            Print( void ) const { base->Print(); }
  261.  
  262. // RAVEN BEGIN
  263.                             // Rebuilds the text source of the decl for saving
  264.     virtual    bool            RebuildTextSource( void ) { return( base->RebuildTextSource() ); }
  265.  
  266.                             // Marks this decl as referenced this level
  267.     virtual void            SetReferencedThisLevel( void ) { base->SetReferencedThisLevel(); }
  268.  
  269. // scork: for detailed error reporting
  270.     virtual bool            Validate( const char *psText, int iLength, idStr &strReportTo ) const { return base->Validate( psText, iLength, strReportTo ); }
  271. // RAVEN END
  272.  
  273. public:
  274.     idDeclBase *            base;
  275. };
  276.  
  277.  
  278. template< class type >
  279. ID_INLINE idDecl *idDeclAllocator( void ) {
  280.     return new type;
  281. }
  282.  
  283. // RAVEN BEGIN
  284. // jsinger: added to allow support for serialization/deserialization of binary decls
  285. #ifdef RV_BINARYDECLS
  286. template< class type >
  287. ID_INLINE SerializableBase *idDeclStreamAllocator( SerialInputStream &stream ) {
  288.     type *ptr = new type(stream);
  289.  
  290.     return dynamic_cast<SerializableBase *>(ptr);
  291. }
  292. #endif
  293.  
  294.  
  295. class idMaterial;
  296. class idDeclSkin;
  297. class idSoundShader;
  298.  
  299. // RAVEN BEGIN
  300. // jscott: new decl types
  301. class rvDeclMatType;
  302. class rvDeclLipSync;
  303. class rvDeclPlayback;
  304. class rvDeclEffect;
  305. // RAVEN END
  306.  
  307. class idDeclManager {
  308. public:
  309.     virtual                    ~idDeclManager( void ) {}
  310.  
  311.     virtual void            SetInsideLoad( bool var ) = 0;
  312.     virtual bool            GetInsideLoad( void ) = 0;
  313.     virtual void            Init( void ) = 0;
  314.     virtual void            Shutdown( void ) = 0;
  315.     virtual void            Reload( bool force ) = 0;
  316.  
  317.     virtual void            BeginLevelLoad() = 0;
  318.     virtual void            EndLevelLoad() = 0;
  319.  
  320.                             // Registers a new decl type.
  321. // RAVEN BEGIN
  322. // jsinger: Added to support serialization/deserialization of binary decls
  323. #ifdef RV_BINARYDECLS
  324.     virtual void            RegisterDeclType( const char *typeName, declType_t type, idDecl *(*allocator)( void ), SerializableBase *(*streamAllocator)( SerialInputStream &stream ) ) = 0;
  325. #else
  326.     virtual void            RegisterDeclType( const char *typeName, declType_t type, idDecl *(*allocator)( void ) ) = 0;
  327. #endif
  328. // jsinger: Added to support loading all decls from a single file
  329. #ifdef RV_SINGLE_DECL_FILE
  330.     virtual void            StartLoadingDecls() = 0;
  331.     virtual void            FinishLoadingDecls() = 0;
  332.     virtual void            LoadDeclsFromFile() = 0;
  333.     virtual void            WriteDeclFile() = 0;
  334.     virtual void            FlushDecls() = 0;
  335. #endif
  336. // RAVEN END
  337.  
  338. // RAVEN BEGIN
  339. // jscott: for timing
  340.                             // Registers a new folder with decl files.
  341.     virtual void            RegisterDeclFolderWrapper( const char *folder, const char *extension, declType_t defaultType, bool unique = false, bool norecurse = false ) = 0;
  342. // RAVEN END
  343.  
  344.                             // Returns a checksum for all loaded decl text.
  345.     virtual int                GetChecksum( void ) const = 0;
  346.  
  347.                             // Returns the number of decl types.
  348.     virtual int                GetNumDeclTypes( void ) const = 0;
  349.  
  350.                             // Returns the type name for a decl type.
  351.     virtual const char *    GetDeclNameFromType( declType_t type ) const = 0;
  352.  
  353.                             // Returns the decl type for a type name.
  354.     virtual declType_t        GetDeclTypeFromName( const char *typeName ) const = 0;
  355.  
  356.                             // If makeDefault is true, a default decl of appropriate type will be created
  357.                             // if an explicit one isn't found. If makeDefault is false, NULL will be returned
  358.                             // if the decl wasn't explcitly defined.
  359.     virtual const idDecl *    FindType( declType_t type, const char *name, bool makeDefault = true, bool noCaching = false ) = 0;
  360.  
  361.     virtual const idDecl*    FindDeclWithoutParsing( declType_t type, const char *name, bool makeDefault = true ) = 0;
  362.  
  363.     virtual void            ReloadFile( const char* filename, bool force ) = 0;
  364.  
  365.                             // Returns the number of decls of the given type.
  366.     virtual int                GetNumDecls( declType_t type ) = 0;
  367.  
  368.                             // The complete lists of decls can be walked to populate editor browsers.
  369.                             // If forceParse is set false, you can get the decl to check name / filename / etc.
  370.                             // without causing it to parse the source and load media.
  371.     virtual const idDecl *    DeclByIndex( declType_t type, int index, bool forceParse = true ) = 0;
  372.  
  373.                             // List and print decls.
  374.     virtual void            ListType( const idCmdArgs &args, declType_t type ) = 0;
  375.     virtual void            PrintType( const idCmdArgs &args, declType_t type ) = 0;
  376.  
  377.                             // Creates a new default decl of the given type with the given name in
  378.                             // the given file used by editors to create a new decls.
  379.     virtual idDecl *        CreateNewDecl( declType_t type, const char *name, const char *fileName ) = 0;
  380.  
  381.                             // BSM - Added for the material editors rename capabilities
  382.     virtual bool            RenameDecl( declType_t type, const char* oldName, const char* newName ) = 0;
  383.  
  384.                             // When media files are loaded, a reference line can be printed at a
  385.                             // proper indentation if decl_show is set
  386.     virtual void            MediaPrint( const char *fmt, ... ) id_attribute((format(printf,2,3))) = 0;
  387.  
  388.     virtual void            WritePrecacheCommands( idFile *f ) = 0;
  389.  
  390. // RAVEN BEGIN
  391. // jscott: precache any guide (template) files
  392.     virtual void                    ParseGuides( void ) = 0;
  393.     virtual    void                    ShutdownGuides( void ) = 0;
  394.     virtual bool                    EvaluateGuide( idStr &name, idLexer *src, idStr &definition ) = 0;
  395.     virtual bool                    EvaluateInlineGuide( idStr &name, idStr &definition ) = 0;
  396. // RAVEN END
  397.                                     // Convenience functions for specific types.
  398.     virtual    const idMaterial *        FindMaterial( const char *name, bool makeDefault = true ) = 0;
  399.     virtual const idDeclSkin *        FindSkin( const char *name, bool makeDefault = true ) = 0;
  400.     virtual const idSoundShader *    FindSound( const char *name, bool makeDefault = true ) = 0;
  401. // RAVEN BEGIN
  402. // jscott: for new Raven decls
  403.     virtual const rvDeclMatType *    FindMaterialType( const char *name, bool makeDefault = true ) = 0;
  404.     virtual    const rvDeclLipSync *    FindLipSync( const char *name, bool makeDefault = true ) = 0;
  405.     virtual    const rvDeclPlayback *    FindPlayback( const char *name, bool makeDefault = true ) = 0;
  406.     virtual    const rvDeclEffect *    FindEffect( const char *name, bool makeDefault = true ) = 0;
  407. // RAVEN END
  408.  
  409.     virtual const idMaterial *        MaterialByIndex( int index, bool forceParse = true ) = 0;
  410.     virtual const idDeclSkin *        SkinByIndex( int index, bool forceParse = true ) = 0;
  411.     virtual const idSoundShader *    SoundByIndex( int index, bool forceParse = true ) = 0;
  412. // RAVEN BEGIN
  413. // jscott: for new Raven decls
  414.     virtual const rvDeclMatType *    MaterialTypeByIndex( int index, bool forceParse = true ) = 0;
  415.     virtual const rvDeclLipSync *    LipSyncByIndex( int index, bool forceParse = true ) = 0;
  416.     virtual    const rvDeclPlayback *    PlaybackByIndex( int index, bool forceParse = true ) = 0;
  417.     virtual const rvDeclEffect *    EffectByIndex( int index, bool forceParse = true ) = 0;
  418.  
  419.     virtual void                    StartPlaybackRecord( rvDeclPlayback *playback ) = 0;
  420.     virtual bool                    SetPlaybackData( rvDeclPlayback *playback, int now, int control, class rvDeclPlaybackData *pbd ) = 0;
  421.     virtual bool                    GetPlaybackData( const rvDeclPlayback *playback, int control, int now, int last, class rvDeclPlaybackData *pbd ) = 0;
  422.     virtual bool                    FinishPlayback( rvDeclPlayback *playback ) = 0;
  423.  
  424.     virtual    idStr                    GetNewName( declType_t type, const char *base ) = 0;
  425.     virtual    const char *            GetDeclTypeName( declType_t type ) = 0;
  426.     virtual size_t                    ListDeclSummary( const idCmdArgs &args ) = 0; 
  427.     virtual void                    RemoveDeclFile( const char *file ) = 0;
  428. // scork: Validation call for detailed error-reporting
  429.     virtual bool                    Validate( declType_t type, int iIndex, idStr &strReportTo ) = 0;
  430.     virtual idDecl *                AllocateDecl( declType_t type ) = 0;
  431.  
  432. #if defined(_XENON)
  433. // mwhitlock: Xenon texture streaming
  434.     virtual void                    SetLightMaterialList(idList<idMaterial*>* materialList) = 0;
  435.     virtual void                    SetEntityMaterialList(idList<idMaterial*>* materialList) = 0;
  436.     virtual void                    PurgeType( declType_t type ) = 0;
  437. #endif
  438. // RAVEN END
  439. };
  440.  
  441. extern idDeclManager *        declManager;
  442.  
  443. template< declType_t type >
  444. ID_INLINE void idListDecls_f( const idCmdArgs &args ) {
  445.     declManager->ListType( args, type );
  446. }
  447.  
  448. template< declType_t type >
  449. ID_INLINE void idPrintDecls_f( const idCmdArgs &args ) {
  450.     declManager->PrintType( args, type );
  451. }
  452.  
  453. #endif /* !__DECLMANAGER_H__ */
  454.