… | |
… | |
28 | |
28 | |
29 | #include <global.h> |
29 | #include <global.h> |
30 | #include <loader.h> |
30 | #include <loader.h> |
31 | #include <sproto.h> |
31 | #include <sproto.h> |
32 | |
32 | |
33 | // resource loader pure base class |
33 | ///////////////////////////////////////////////////////////////////////////// |
34 | struct loader_base |
|
|
35 | { |
|
|
36 | const char *filename; |
|
|
37 | virtual const char *type () const = 0; |
|
|
38 | |
34 | |
39 | virtual archetype *get_arch (const char *name); |
35 | loader_base::~loader_base () |
40 | virtual void put_arch (archetype *arch); |
|
|
41 | |
|
|
42 | virtual object *get_object (const char *name); |
|
|
43 | virtual void put_object (object *op); |
|
|
44 | |
|
|
45 | virtual player *get_player (); |
|
|
46 | virtual void put_player (player *pl); |
|
|
47 | |
|
|
48 | virtual region *get_region (const char *name); |
|
|
49 | virtual void put_region (region *region); |
|
|
50 | |
|
|
51 | virtual facetile *get_face (const char *name); |
|
|
52 | virtual void put_face (facetile *face); |
|
|
53 | |
|
|
54 | virtual treasurelist *get_treasure (const char *name, bool one = false); |
|
|
55 | virtual void put_treasure (treasurelist *treasure); |
|
|
56 | |
|
|
57 | virtual animation *get_animation (const char *name); |
|
|
58 | virtual void put_animation (animation *anim); |
|
|
59 | }; |
|
|
60 | |
|
|
61 | // pure base class for default archetype loader |
|
|
62 | struct loader_arch : virtual loader_base { |
|
|
63 | archetype *get_arch (const char *name); |
|
|
64 | void put_arch (archetype *arch); |
|
|
65 | }; |
|
|
66 | |
|
|
67 | // pure base class for default object loader |
|
|
68 | struct loader_object : virtual loader_base { |
|
|
69 | object *get_object (const char *name); |
|
|
70 | void put_object (object *op); |
|
|
71 | }; |
|
|
72 | |
|
|
73 | // pure base class for default player loader |
|
|
74 | struct loader_player : virtual loader_base { |
|
|
75 | virtual player *get_player (); |
|
|
76 | virtual void put_player (player *pl); |
|
|
77 | }; |
|
|
78 | |
|
|
79 | // pure base class for default region loader |
|
|
80 | struct loader_region : virtual loader_base { |
|
|
81 | region *get_region (); |
|
|
82 | void put_region (region *region); |
|
|
83 | }; |
|
|
84 | |
|
|
85 | // pure base class for default face loader |
|
|
86 | struct loader_face : virtual loader_base { |
|
|
87 | facetile *get_face (const char *name); |
|
|
88 | void put_face (facetile *face); |
|
|
89 | }; |
|
|
90 | |
|
|
91 | // pure base class for default treasure loader |
|
|
92 | struct loader_treasure : virtual loader_base { |
|
|
93 | treasurelist *get_treasure (const char *name, bool one = false); |
|
|
94 | void put_treasure (treasurelist *treasure); |
|
|
95 | }; |
|
|
96 | |
|
|
97 | // pure base class for default animation loader |
|
|
98 | struct loader_animation : virtual loader_base { |
|
|
99 | animation *get_animation (const char *name); |
|
|
100 | void put_animation (animation *anim); |
|
|
101 | }; |
|
|
102 | |
|
|
103 | // future generic resource loader |
|
|
104 | // handles generic stuff valid in most files, such as |
|
|
105 | // animations, treasures, faces and so on |
|
|
106 | struct loader_generic |
|
|
107 | : virtual loader_base, |
|
|
108 | loader_object, loader_player, |
|
|
109 | loader_region, loader_face, |
|
|
110 | loader_treasure, loader_animation |
|
|
111 | { |
36 | { |
112 | const char *type () const = 0; |
37 | } |
113 | }; |
|
|
114 | |
38 | |
115 | // the base class warns about and skips everything |
39 | // the base class warns about and skips everything |
116 | archetype * |
40 | archetype * |
117 | loader_base::get_arch (const char *name) |
41 | loader_base::get_arch (const char *name) |
118 | { |
42 | { |
119 | LOG (llevError, "%s: found archetype definition '%s', which is not allowed in files of type %s.\n", |
43 | LOG (llevError, "%s: found archetype definition '%s', which is not allowed in files of this type.\n", |
120 | filename, name, type ()); |
44 | filename, name); |
121 | |
45 | |
122 | return new archetype; |
46 | return new archetype; |
123 | } |
47 | } |
124 | |
48 | |
125 | object * |
49 | object * |
126 | loader_base::get_object (const char *name) |
50 | loader_base::get_object (const char *name) |
127 | { |
51 | { |
128 | LOG (llevError, "%s: found object definition '%s', which is not allowed in files of type %s.\n", |
52 | LOG (llevError, "%s: found object definition '%s', which is not allowed in files of this type.\n", |
129 | filename, name, type ()); |
53 | filename, name); |
130 | |
54 | |
131 | return object::create (); |
55 | return object::create (); |
132 | } |
56 | } |
133 | |
57 | |
134 | player * |
58 | player * |
135 | loader_base::get_player () |
59 | loader_base::get_player () |
136 | { |
60 | { |
137 | LOG (llevError, "%s: found player definition, which is not allowed in files of type %s.\n", |
61 | LOG (llevError, "%s: found player definition, which is not allowed in files of this type.\n", |
138 | filename, type ()); |
62 | filename); |
139 | |
63 | |
140 | return player::create (); |
64 | return player::create (); |
141 | } |
65 | } |
142 | |
66 | |
143 | region * |
67 | region * |
144 | loader_base::get_region (const char *name) |
68 | loader_base::get_region (const char *name) |
145 | { |
69 | { |
146 | LOG (llevError, "%s: found region definition '%s', which is not allowed in files of type %s.\n", |
70 | LOG (llevError, "%s: found region definition '%s', which is not allowed in files of this type.\n", |
147 | filename, name, type ()); |
71 | filename, name); |
148 | |
72 | |
149 | return new region; |
73 | return new region; |
150 | } |
74 | } |
151 | |
75 | |
152 | facetile * |
76 | facetile * |
153 | loader_base::get_face (const char *name) |
77 | loader_base::get_face (const char *name) |
154 | { |
78 | { |
155 | LOG (llevError, "%s: found face definition '%s', which is not allowed in files of type %s.\n", |
79 | LOG (llevError, "%s: found face definition '%s', which is not allowed in files of this type.\n", |
156 | filename, name, type ()); |
80 | filename, name); |
157 | |
81 | |
158 | return new facetile; |
82 | return new facetile; |
159 | } |
83 | } |
160 | |
84 | |
161 | treasurelist * |
85 | treasurelist * |
162 | loader_base::get_treasure (const char *name, bool one) |
86 | loader_base::get_treasure (const char *name, bool one) |
163 | { |
87 | { |
164 | LOG (llevError, "%s: found treasure definition '%s', which is not allowed in files of type %s.\n", |
88 | LOG (llevError, "%s: found treasure definition '%s', which is not allowed in files of this type.\n", |
165 | filename, name, type ()); |
89 | filename, name); |
166 | |
90 | |
167 | return new treasurelist;//D |
91 | return new treasurelist;//D |
168 | } |
92 | } |
169 | |
93 | |
170 | animation * |
94 | animation * |
171 | loader_base::get_animation (const char *name) |
95 | loader_base::get_animation (const char *name) |
172 | { |
96 | { |
173 | LOG (llevError, "%s: found animation definition '%s', which is not allowed in files of type %s.\n", |
97 | LOG (llevError, "%s: found animation definition '%s', which is not allowed in files of this type.\n", |
174 | filename, name, type ()); |
98 | filename, name); |
175 | |
99 | |
176 | return new animation; |
100 | return new animation; |
177 | } |
101 | } |
178 | |
102 | |
179 | void |
103 | void |
… | |
… | |
215 | void |
139 | void |
216 | loader_base::put_animation (animation *anim) |
140 | loader_base::put_animation (animation *anim) |
217 | { |
141 | { |
218 | delete anim; |
142 | delete anim; |
219 | } |
143 | } |
|
|
144 | |
|
|
145 | ///////////////////////////////////////////////////////////////////////////// |
|
|
146 | |
|
|
147 | bool loader_base::parse (object_thawer &thawer) |
|
|
148 | { |
|
|
149 | for (;;) |
|
|
150 | { |
|
|
151 | keyword kw = thawer.get_kv (); |
|
|
152 | |
|
|
153 | switch (kw) |
|
|
154 | { |
|
|
155 | case KW_region: |
|
|
156 | { |
|
|
157 | region *rgn = get_region (thawer.get_str ()); |
|
|
158 | |
|
|
159 | if (!parse_region (thawer, rgn)) |
|
|
160 | { |
|
|
161 | delete rgn; |
|
|
162 | return false; |
|
|
163 | } |
|
|
164 | |
|
|
165 | put_region (rgn); |
|
|
166 | } |
|
|
167 | break; |
|
|
168 | |
|
|
169 | case KW_EOF: |
|
|
170 | return true; |
|
|
171 | |
|
|
172 | default: |
|
|
173 | if (!thawer.parse_error (kw, "resource file")) |
|
|
174 | return false; |
|
|
175 | } |
|
|
176 | } |
|
|
177 | } |
|
|
178 | |
|
|
179 | bool loader_base::load (const char *filename) |
|
|
180 | { |
|
|
181 | this->filename = filename; |
|
|
182 | |
|
|
183 | object_thawer fp (filename); |
|
|
184 | |
|
|
185 | if (!fp) |
|
|
186 | return false; |
|
|
187 | |
|
|
188 | return parse (fp); |
|
|
189 | } |
|
|
190 | |
|
|
191 | ///////////////////////////////////////////////////////////////////////////// |
220 | |
192 | |
221 | /* Maps the MOVE_* values to names */ |
193 | /* Maps the MOVE_* values to names */ |
222 | static const char *const move_name[] = { "walk", "fly_low", "fly_high", "swim", "boat", NULL }; |
194 | static const char *const move_name[] = { "walk", "fly_low", "fly_high", "swim", "boat", NULL }; |
223 | |
195 | |
224 | /* This table is only necessary to convert objects that existed before the |
196 | /* This table is only necessary to convert objects that existed before the |
… | |
… | |
1222 | set_ob_key_value (op, thawer.last_keyword, thawer.last_value, true); |
1194 | set_ob_key_value (op, thawer.last_keyword, thawer.last_value, true); |
1223 | //fprintf (stderr, "addkv(%s,%s)\n", thawer.last_keyword, thawer.last_value);//D |
1195 | //fprintf (stderr, "addkv(%s,%s)\n", thawer.last_keyword, thawer.last_value);//D |
1224 | break; |
1196 | break; |
1225 | |
1197 | |
1226 | default: |
1198 | default: |
1227 | LOG (llevError, "UNSUPPORTED KEYWORD IN MAP: \"%s\", bug in normaliser. skipping.\n", keyword_str[kw]); |
1199 | if (!thawer.parse_error (kw, "object", op->name)) |
|
|
1200 | return false; |
1228 | break; |
1201 | break; |
1229 | } |
1202 | } |
1230 | } |
1203 | } |
1231 | } |
1204 | } |
1232 | |
1205 | |