DeclEntityDef.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. ===========================================================================
  3. Doom 3 BFG Edition GPL Source Code
  4. Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
  6. Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #include "../idlib/precompiled.h"
  21. #pragma hdrstop
  22. /*
  23. =================
  24. idDeclEntityDef::Size
  25. =================
  26. */
  27. size_t idDeclEntityDef::Size() const {
  28. return sizeof( idDeclEntityDef ) + dict.Allocated();
  29. }
  30. /*
  31. ================
  32. idDeclEntityDef::FreeData
  33. ================
  34. */
  35. void idDeclEntityDef::FreeData() {
  36. dict.Clear();
  37. }
  38. /*
  39. ================
  40. idDeclEntityDef::Parse
  41. ================
  42. */
  43. bool idDeclEntityDef::Parse( const char *text, const int textLength, bool allowBinaryVersion ) {
  44. idLexer src;
  45. idToken token, token2;
  46. src.LoadMemory( text, textLength, GetFileName(), GetLineNum() );
  47. src.SetFlags( DECL_LEXER_FLAGS );
  48. src.SkipUntilString( "{" );
  49. while (1) {
  50. if ( !src.ReadToken( &token ) ) {
  51. break;
  52. }
  53. if ( !token.Icmp( "}" ) ) {
  54. break;
  55. }
  56. if ( token.type != TT_STRING ) {
  57. src.Warning( "Expected quoted string, but found '%s'", token.c_str() );
  58. MakeDefault();
  59. return false;
  60. }
  61. if ( !src.ReadToken( &token2 ) ) {
  62. src.Warning( "Unexpected end of file" );
  63. MakeDefault();
  64. return false;
  65. }
  66. if ( dict.FindKey( token ) ) {
  67. src.Warning( "'%s' already defined", token.c_str() );
  68. }
  69. dict.Set( token, token2 );
  70. }
  71. // we always automatically set a "classname" key to our name
  72. dict.Set( "classname", GetName() );
  73. // "inherit" keys will cause all values from another entityDef to be copied into this one
  74. // if they don't conflict. We can't have circular recursions, because each entityDef will
  75. // never be parsed mroe than once
  76. // find all of the dicts first, because copying inherited values will modify the dict
  77. idList<const idDeclEntityDef *> defList;
  78. while ( 1 ) {
  79. const idKeyValue *kv;
  80. kv = dict.MatchPrefix( "inherit", NULL );
  81. if ( !kv ) {
  82. break;
  83. }
  84. const idDeclEntityDef *copy = static_cast<const idDeclEntityDef *>( declManager->FindType( DECL_ENTITYDEF, kv->GetValue(), false ) );
  85. if ( !copy ) {
  86. src.Warning( "Unknown entityDef '%s' inherited by '%s'", kv->GetValue().c_str(), GetName() );
  87. } else {
  88. defList.Append( copy );
  89. }
  90. // delete this key/value pair
  91. dict.Delete( kv->GetKey() );
  92. }
  93. // now copy over the inherited key / value pairs
  94. for ( int i = 0 ; i < defList.Num() ; i++ ) {
  95. dict.SetDefaults( &defList[ i ]->dict );
  96. }
  97. game->CacheDictionaryMedia( &dict );
  98. return true;
  99. }
  100. /*
  101. ================
  102. idDeclEntityDef::DefaultDefinition
  103. ================
  104. */
  105. const char *idDeclEntityDef::DefaultDefinition() const {
  106. return
  107. "{\n"
  108. "\t" "\"DEFAULTED\"\t\"1\"\n"
  109. "}";
  110. }
  111. /*
  112. ================
  113. idDeclEntityDef::Print
  114. Dumps all key/value pairs, including inherited ones
  115. ================
  116. */
  117. void idDeclEntityDef::Print() {
  118. dict.Print();
  119. }