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