-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathecho_loader.cpp
1334 lines (1249 loc) · 34.3 KB
/
echo_loader.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// echo_loader.cpp
/*
This file is part of L-Echo.
L-Echo is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
L-Echo is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with L-Echo. If not, see <http://www.gnu.org/licenses/>.
*/
/// Standard libraries
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>
#include <typeinfo>
#include <vector>
#include <map>
/// Various L-Echo libraries
#include "echo_debug.h"
#include "echo_error.h"
#include "echo_stage.h"
#include "echo_platform.h"
#include "echo_xml.h"
#include "filter.h"
#include "trigger.h"
/// Various grids
#include "launcher.h"
#include "freeform_grid.h"
#include "t_grid.h"
#include "escgrid.h"
#include "hole.h"
#include "grid.h"
#include "stair.h"
/// If we want to debug the loading...
#ifdef LOAD_DEBUG
/// Forward the printing to ECHO_PRINT
#define LD_PRINT(...) ECHO_PRINT(__VA_ARGS__)
#else
/// Or just swallow it
#define LD_PRINT(...)
#endif
/** @brief Holds a grid and calls a given function function of the grid.\n
* Used for dependencies.
*/
class functor
{
public:
/// Grid object
grid* obj;
/// Function to be called; has to take a grid pointer as arg
void (grid::*funcp)(grid*);
protected:
/// Default constructor; everything NULL; used by subclasses
functor()
{
obj = NULL;
funcp = NULL;
}
public:
/// Deconstructor; does nothing
virtual ~functor(){}
/// Constructor, initializes both attributes
functor(grid* my_obj, void (grid::*my_funcp)(grid*))
{
obj = my_obj;
funcp = my_funcp;
}
/// Calls the function with the pointer as argument
virtual void call(grid* ptr) const
{
(obj->*funcp)(ptr);
}
};
/** @brief Holds a t_grid and calls a given function function of the grid.\n
* Used for dependencies.
*/
class t_functor : public functor
{
public:
/// The t_grid object whose function is called
t_grid* t_obj;
/// The function to be called
void (t_grid::*t_funcp)(grid*);
/// Deconstructor; does nothing
virtual ~t_functor(){}
/// Constructor, initializes both attributes of this class; doesn't initialize functor stuff
t_functor(t_grid* my_obj, void (t_grid::*my_funcp)(grid*))
{
t_obj = my_obj;
t_funcp = my_funcp;
}
/// Calls the function with the pointer as argument
virtual void call(grid* ptr) const
{
(t_obj->*t_funcp)(ptr);
}
};
/** @brief Holds a filter and calls a given function function of the grid.\n
* Used for dependencies.
*/
class filter_functor : public functor
{
public:
/// The filter object whose function is called
filter* f_obj;
/// The function to be called
void (filter::*f_funcp)(grid*);
/// Deconstructor; does nothing
virtual ~filter_functor(){}
/// Constructor, initializes both attributes of this class; doesn't initialize functor stuff
filter_functor(filter* my_obj, void (filter::*my_funcp)(grid*))
{
f_obj = my_obj;
f_funcp = my_funcp;
}
/// Calls the function with the pointer as argument
virtual void call(grid* ptr) const
{
(f_obj->*f_funcp)(ptr);
}
};
/** @brief Holds a trigger and calls a given function function of the grid.\n
* Used for dependencies.
*/
class trigger_functor : public functor
{
public:
/// The trigger object whose function is called
trigger* t_obj;
/// The function to be called
void (trigger::*t_funcp)(grid*);
/// Deconstructor; does nothing
virtual ~trigger_functor(){}
/// Constructor, initializes both attributes of this class; doesn't initialize functor stuff
trigger_functor(trigger* my_obj, void (trigger::*my_funcp)(grid*))
{
t_obj = my_obj;
t_funcp = my_funcp;
}
/// Calls the function with the pointer as argument
virtual void call(grid* ptr) const
{
(t_obj->*t_funcp)(ptr);
}
};
/// A list of functors.
typedef std::vector<functor*> FUNCTOR_VEC;
/// Map of grid id strings to a list of functors to call with the grid.
typedef std::map<std::string, FUNCTOR_VEC*> DEPENDENCY_MAP;
/// A map of y-coordinates to a list of grids on that level
typedef std::map<float, GRID_PTR_SET*> LEVEL_MAP;
#ifdef ECHO_NDS
/// The function for parsing a grid from an element. This NDS version has extra maps for polyID assigning
static grid* parse_grid(echo_xml_element* txe, stage* st, DEPENDENCY_MAP* map, escgrid* escroot
, LEVEL_MAP* nonffgrids, LEVEL_MAP* ffgrids);
#else
/// The function for parsing a grid from an element
static grid* parse_grid(echo_xml_element* txe, stage* st, DEPENDENCY_MAP* map, escgrid* escroot);
#endif
/// Convenience function for map_add_pos below; gets the right grid list in the map
static GRID_PTR_SET* map_get_level(LEVEL_MAP* levels, vector3f* pos)
{
LEVEL_MAP::iterator it = levels->find(pos->y);
if(it == levels->end())
return(NULL);
return(it->second);
}
/// Add the grid to the right level with its position
void map_add_pos(LEVEL_MAP* levels, vector3f* pos, grid* g)
{
GRID_PTR_SET* set = map_get_level(levels, pos);
if(set)
{
set->insert(g);
}
else
{
set = new GRID_PTR_SET();
set->insert(g);
levels->insert(LEVEL_MAP::value_type(pos->y, set));
}
}
/// Deletes all the functors in the list, and the list itself
void delete_functors(FUNCTOR_VEC* vec)
{
FUNCTOR_VEC::iterator it = vec->begin();
while(it != vec->end())
{
functor* func = *it;
if(func != NULL)
delete func;
it++;
}
delete vec;
}
/// Deletes all the functor lists in the map, and the map itself
void delete_dependencies(DEPENDENCY_MAP* map)
{
DEPENDENCY_MAP::iterator it = map->begin();
while(it != map->end())
{
FUNCTOR_VEC* vec = it->second;
if(vec != NULL)
delete_functors(vec);
it++;
}
delete map;
}
/** Load the stage from the file name
* @param file_name File to load the stage from.
*/
stage* load_stage(char* file_name)
{
/// Prepare to load the file
echo_xml** doc = new(echo_xml*);
/// Load the file
if(echo_xml_load_file(doc, file_name) == WIN)
{
/// -----------------------------------------------------------prepare stuff
DEPENDENCY_MAP* map = new DEPENDENCY_MAP();
stage* ret = new stage();
#ifdef ECHO_NDS
/// NDS versions need to assign polyID, so non-freeform_grids and freeform_grids are separated into different LEVEL_MAPs
LEVEL_MAP* nonffgrids = new LEVEL_MAP();
LEVEL_MAP* ffgrids = new LEVEL_MAP();
#endif
/// -----------------------------------------------------------get the root of the document
echo_xml_element** root = new(echo_xml_element*);
if(echo_xml_get_root(*doc, root) == FAIL)
{
lderr("cannot find root element!");
delete root;
echo_xml_delete_file(*doc);
delete doc;
delete_dependencies(map);
delete ret;
#ifdef ECHO_NDS
delete nonffgrids;
delete ffgrids;
#endif
return(NULL);
}
/// -----------------------------------------------------------parse all grids
echo_xml_node** child = new(echo_xml_node*);
if(echo_xml_get_first_child(*root, child) == WIN)
{
echo_xml_element** e = new(echo_xml_element*);
echo_xml_type type = ECHO_XML_TYPE_NULL;
/// For each element...
do
{
/// Get the node type (needs to be element or comment)
if(echo_xml_get_node_type(*child, &type) == WIN)
{
if(type == ECHO_XML_TYPE_ELEMENT)
{
/// If either converting or parsing fails, fail the loader...
#ifdef ECHO_NDS
if(echo_xml_to_element(*child, e) != WIN || parse_grid(*e, ret, map, NULL, nonffgrids, ffgrids) == NULL)
#else
if(echo_xml_to_element(*child, e) != WIN || parse_grid(*e, ret, map, NULL) == NULL)
#endif
{
lderr("parse not successful!");
delete (*root);
delete root;
delete child;
delete e;
echo_xml_delete_file(*doc);
delete doc;
delete_dependencies(map);
delete ret;
#ifdef ECHO_NDS
delete nonffgrids;
delete ffgrids;
#endif
return(NULL);
}
}
else if(type != ECHO_XML_TYPE_COMMENT)
{
lderr("unknown node type!");
delete (*root);
delete root;
delete child;
delete e;
echo_xml_delete_file(*doc);
delete doc;
delete_dependencies(map);
delete ret;
#ifdef ECHO_NDS
delete nonffgrids;
delete ffgrids;
#endif
return(NULL);
}
}
else
{
lderr("couldn't get node type!\n");
delete (*root);
delete root;
delete child;
delete e;
echo_xml_delete_file(*doc);
delete doc;
delete_dependencies(map);
delete ret;
#ifdef ECHO_NDS
delete nonffgrids;
delete ffgrids;
#endif
return(NULL);
}
}
while(echo_xml_next_sibling(*child, child));
delete e;
}
delete child;
/// -----------------------------------------------------------get starting point string
char** start = new(char*);
if(echo_xml_get_attribute(*root, "start", start) == FAIL)
{
lderr("no starting point specified!\n");
delete start;
echo_xml_delete_file(*doc);
delete doc;
delete_dependencies(map);
delete ret;
#ifdef ECHO_NDS
delete nonffgrids;
delete ffgrids;
#endif
return(NULL);
}
else
LD_PRINT("start: %s\n", start);
/// -----------------------------------------------------------get starting grid, set stage starting point as that
grid* start_grid = ret->get(*start);
if(start_grid == NULL)
{
lderr("start grid not found...\n");
delete start;
echo_xml_delete_file(*doc);
delete doc;
delete_dependencies(map);
delete ret;
#ifdef ECHO_NDS
delete nonffgrids;
delete ffgrids;
#endif
return(NULL);
}
ret->set_start(start_grid);
delete start;
/// -----------------------------------------------------------get/set name
char** name = new(char*);
if(echo_xml_get_attribute(*root, "name", name) == FAIL)
{
lderr("name of stage not specified!\n");
delete name;
echo_xml_delete_file(*doc);
delete doc;
delete_dependencies(map);
delete ret;
#ifdef ECHO_NDS
delete nonffgrids;
delete ffgrids;
#endif
return(NULL);
}
ret->set_name(new std::string(*name));
delete name;
/// -----------------------------------------------------------get num goals
int num_goals = 0;
if(echo_xml_get_int_attribute(*root, "goals", &num_goals) == FAIL)
{
lderr("cannot find number of goals!\n");
echo_xml_delete_file(*doc);
delete doc;
delete_dependencies(map);
delete ret;
#ifdef ECHO_NDS
delete nonffgrids;
delete ffgrids;
#endif
return(NULL);
}
ret->set_num_goals(num_goals);
/// -----------------------------------------------------------delete docs and dependencies
if(!map->empty())
ldwarn("dependencies not satisfied...\n");
delete_dependencies(map);
echo_xml_delete_file(*doc);
delete doc;
delete (*root);
delete root;
/// -----------------------------------------------------------hand out the polyIDs
#ifdef ECHO_NDS
#define GRID_POLYID_START 19
unsigned int polyID = GRID_POLYID_START;
LEVEL_MAP::iterator it = nonffgrids->begin(), end = nonffgrids->end();
GRID_PTR_SET::iterator git, gend;
while(it != end)
{
git = it->second->begin();
gend = it->second->end();
while(git != gend)
{
(*git)->set_polyID(polyID);
git++;
}
ECHO_PRINT("polyID: %i (height: %f)\n", polyID, it->first);
polyID++;
if(polyID >= 64)
polyID = GRID_POLYID_START; /// Cycle through?
it++;
}
it = ffgrids->begin();
end = ffgrids->end();
while(it != end) /// NOTE: NOT COMPLETE! ffgrids with the same or opposing normals can have the same polyID!
{
git = it->second->begin();
gend = it->second->end();
while(git != gend)
{
(*git)->set_polyID(polyID);
git++;
}
ECHO_PRINT("polyID: %i (height: %f)\n", polyID, it->first);
polyID++;
if(polyID >= 64)
polyID = GRID_POLYID_START; /// Cycle through?
it++;
}
delete nonffgrids;
delete ffgrids;
#endif
return(ret);
}
/// Failing to open the file...
else
{
lderr("cannot open file! (might not be correct xml file): ", file_name);
echo_xml_delete_file(*doc);
return(NULL);
}
return(NULL);
}
/// Get the list of dependencies associated with a grid id
static FUNCTOR_VEC* dep_set(DEPENDENCY_MAP* map, char* id)
{
std::string* s1 = new std::string(id);
DEPENDENCY_MAP::iterator it = map->find(*s1);
delete s1;
return(it == map->end() ? NULL : it->second);
}
/// Add a dependency with functor "f" for grid with id "id" onto dependency map "map"
static void add(DEPENDENCY_MAP* map, char* id, functor* f)
{
FUNCTOR_VEC* set = dep_set(map, id);
if(set)
{
LD_PRINT("dep set for %s found, adding\n", id);
set->push_back(f);
}
else
{
LD_PRINT("dep set for %s NOT found, adding\n", id);
set = new FUNCTOR_VEC();
set->push_back(f);
map->insert(DEPENDENCY_MAP::value_type(id, set));
}
}
/// Add a dependency of grid "obj", function "funcp" for grid with id "id"
static void add(DEPENDENCY_MAP* map, char* id, t_grid* obj, void (t_grid::*funcp)(grid*))
{
#ifdef STRICT_MEM
t_functor* f = new t_functor(obj, funcp);
add(map, id, f);
#else
add(map, id, new t_functor(obj, funcp));
#endif
}
/// Add a dependency of grid "obj", function "funcp" for grid with id "id"
static void add(DEPENDENCY_MAP* map, char* id, grid* obj, void (grid::*funcp)(grid*))
{
#ifdef STRICT_MEM
functor* f = new functor(obj, funcp);
add(map, id, f);
#else
add(map, id, new t_functor(obj, funcp));
#endif
}
/// Add a dependency of filter "obj", function "funcp" for grid with id "id"
static void add(DEPENDENCY_MAP* map, char* id, filter* obj, void (filter::*funcp)(grid*))
{
#ifdef STRICT_MEM
filter_functor* f = new filter_functor(obj, funcp);
add(map, id, f);
#else
add(map, id, new filter_functor(obj, funcp));
#endif
}
/// Add a dependency of trigger "obj", function "funcp" for grid with id "id"
static void add(DEPENDENCY_MAP* map, char* id, trigger* obj, void (trigger::*funcp)(grid*))
{
#ifdef STRICT_MEM
trigger_functor* f = new trigger_functor(obj, funcp);
add(map, id, f);
#else
add(map, id, new trigger_functor(obj, funcp));
#endif
}
/// Get the vector described by the attributes of the element "txe"
static int get_vec(echo_xml_element* txe, vector3f* vec, stage* st = NULL)
{
const int result = echo_xml_get_float_attribute(txe, "x", &vec->x) == WIN
&& echo_xml_get_float_attribute(txe, "y", &vec->y) == WIN
&& echo_xml_get_float_attribute(txe, "z", &vec->z) == WIN;
if(st)
st->set_farthest(vec->length());
return(result);
}
/// Get the angle described by the attributes of the element "txe"
static int get_angle(echo_xml_element* txe, vector3f* vec)
{
return(echo_xml_get_float_attribute(txe, "x", &vec->x) == WIN
&& echo_xml_get_float_attribute(txe, "y", &vec->y) == WIN);
}
/// Get the esc from element "txe" and add it to "escroot"
#ifdef ECHO_NDS
static int add_esc(echo_xml_element* child, stage* st, DEPENDENCY_MAP* map, escgrid* escroot, escgrid* egrid
, LEVEL_MAP* nonffgrids, LEVEL_MAP* ffgrids)
#else
static int add_esc(echo_xml_element* child, stage* st, DEPENDENCY_MAP* map, escgrid* escroot, escgrid* egrid)
#endif
{
char** type = new(char*);
if(echo_xml_get_tagname(child, type) == WIN)
{
if(!strcmp(*type, "angle"))
{
vector3f* each_angle = new vector3f();
if(get_angle(child, each_angle) == WIN)
{
echo_xml_node** first = new(echo_xml_node*);
if(echo_xml_get_first_child(child, first) == WIN)
{
echo_xml_element** e = new(echo_xml_element*);
if(echo_xml_to_element(*first, e) == WIN)
{
#ifdef ECHO_NDS
grid* g = parse_grid(*e, st, map, escroot ? escroot : egrid, nonffgrids, ffgrids);
#else
grid* g = parse_grid(*e, st, map, escroot ? escroot : egrid);
#endif
if(g != NULL)
{
egrid->add(each_angle, g);
delete *first;
delete first;
delete e;
delete type;
return(WIN);
}
else
lderr("parse grid error in add_esc!");
}
else
lderr("couldn't convert to element error in add_esc!");
delete e;
}
else
lderr("no esc in angle!");
delete *first;
delete first;
}
else
lderr("couldn't get angle in add_esc!");
delete each_angle;
}
else if(!strcmp(*type, "range"))
{
vector3f* v1 = new vector3f();
if(echo_xml_get_float_attribute(child, "x_min", &(v1->x)) == WIN
&& echo_xml_get_float_attribute(child, "y_min", &(v1->y)) == WIN)
{
vector3f* v2 = new vector3f();
if(echo_xml_get_float_attribute(child, "x_max", &(v2->x)) == WIN
&& echo_xml_get_float_attribute(child, "y_max", &(v2->y)) == WIN)
{
echo_xml_node** first = new(echo_xml_node*);
if(echo_xml_get_first_child(child, first) == WIN)
{
echo_xml_element** e = new(echo_xml_element*);
if(echo_xml_to_element(*first, e) == WIN)
{
#ifdef ECHO_NDS
grid* g = parse_grid(*e, st, map, escroot ? escroot : egrid, nonffgrids, ffgrids);
#else
grid* g = parse_grid(*e, st, map, escroot ? escroot : egrid);
#endif
if(g != NULL)
{
angle_range* range = new angle_range(v1, v2);
//ECHO_PRINT("range added\n");
egrid->add(range, g);
delete type;
delete e;
delete first;
return(WIN);
}
else
lderr("parse grid error in add_esc!");
}
else
lderr("couldn't convert to element error in add_esc!");
delete e;
}
else
lderr("no esc in angle range!");
delete first;
}
else
lderr("couldn't get max for angle range!");
delete v2;
}
else
lderr("couldn't get min for angle range!");
delete v1;
}
else
lderr("child of escgrid, hole or launcher is not an angle or range!");
}
delete type;
return(FAIL);
}
/// Add all the escs beginning with element "txe" to escgrid "escroot"
#ifdef ECHO_NDS
static int add_escs(echo_xml_element* txe, stage* st, DEPENDENCY_MAP* map, escgrid* escroot, escgrid* grid
, LEVEL_MAP* nonffgrids, LEVEL_MAP* ffgrids)
#else
static int add_escs(echo_xml_element* txe, stage* st, DEPENDENCY_MAP* map, escgrid* escroot, escgrid* grid)
#endif
{
echo_xml_node** first = new(echo_xml_node*);
ECHO_PRINT("first: %p\n", first);
if(echo_xml_get_first_child(txe, first) == WIN)
{
echo_xml_node* first_p = *first;
ECHO_PRINT("*first: %p\n", first_p);
echo_xml_type type = ECHO_XML_TYPE_NULL;
char** tag = new(char*);
echo_xml_element** e = new(echo_xml_element*);
do
{
if(echo_xml_get_node_type(*first, &type) == WIN)
{
if(type == ECHO_XML_TYPE_ELEMENT)
{
if(echo_xml_to_element(*first, e) == WIN)
{
if(echo_xml_get_tagname(*e, tag) == WIN)
{
/// Don't look for triggers
if(strcmp(*tag, "triggers"))
#ifdef ECHO_NDS
add_esc(*e, st, map, escroot, grid, nonffgrids, ffgrids);
#else
add_esc(*e, st, map, escroot, grid);
#endif
}
else
{
lderr("couldn't get tag name in escgrid!");
delete first;
delete e;
delete tag;
return(FAIL);
}
}
else
{
lderr("couldn't convert to element in escgrid!");
delete first;
delete e;
delete tag;
return(FAIL);
}
}
else if(type != ECHO_XML_TYPE_COMMENT && type != ECHO_XML_TYPE_NULL)
{
lderr("unknown node in escgrid!");
delete first;
delete e;
delete tag;
return(FAIL);
}
}
else
{
lderr("couldn't get node type in escgrid!");
delete first;
delete e;
delete tag;
return(FAIL);
}
}
while(echo_xml_next_sibling(*first, first) == WIN);
delete tag;
delete e;
}
delete first;
return(WIN);
}
/// Get the particular attribute with key "attr" from "txe"; print error messages "errmsg1" and "errmsg2" if not successful
static char* get_attribute(echo_xml_element* txe, const char* attr, const char* errmsg1, const char* errmsg2)
{
char** retp = new(char*);
if(echo_xml_get_attribute(txe, attr, retp) == FAIL)
{
lderr(errmsg1, errmsg2);
return(NULL);
}
char* ret = *retp;
delete retp;
return(ret);
}
/// Get the particular attribute with key "attr" from "txe"; print error messages "errmsg" if not successful
static char* get_attribute(echo_xml_element* txe, const char* attr, const char* errmsg)
{
char** retp = new(char*);
if(echo_xml_get_attribute(txe, attr, retp) == FAIL)
{
lderr(errmsg);
return(NULL);
}
char* ret = *retp;
delete retp;
return(ret);
}
/// Get the filter from element "txe"
static filter* get_filter(echo_xml_element* txe, stage* st, DEPENDENCY_MAP* map, char* type = NULL)
{
if(type == NULL)
{
if(echo_xml_get_tagname(txe, &type) == FAIL)
{
lderr("type unknown for filter...");
return(NULL);
}
}
ECHO_PRINT("type of filter:%s\n", type);
if(!strcmp(type, "goal"))
{
char* name = get_attribute(txe, "id", "no id for goal filter");
if(name != NULL)
{
filter* ret = new filter();
grid* g = st->get(name);
if(g)
ret->set_target(g);
else
{
LD_PRINT("filter target (%s) is null, adding to dep map: ", name);
add(map, name, ret, &filter::set_target);
}
return(ret);
}
}
else if(!strcmp(type, "not"))
{
echo_xml_node** first = new(echo_xml_node*);
if(echo_xml_get_first_child(txe, first) == WIN)
{
echo_xml_type type = ECHO_XML_TYPE_NULL;
while(echo_xml_get_node_type(*first, &type) == WIN
&& type != ECHO_XML_TYPE_ELEMENT
&& echo_xml_next_sibling(*first, first) == WIN);
if(*first != NULL)
{
echo_xml_element** e = new(echo_xml_element*);
if(echo_xml_to_element(*first, e) == WIN)
{
not_filter* ret = new not_filter(get_filter(*e, st, map));
delete e;
delete first;
return(ret);
}
else
lderr("couldn't convert to element in \"not\" filter!");
delete e;
}
else
lderr("no filter element in \"not\" filter!");
}
else
lderr("\"not\" filter has no item inside");
delete first;
}
else if(!strcmp(type, "or") || !strcmp(type, "and"))
{
multi_filter* ret = (!strcmp(type, "or")) ?
dynamic_cast<multi_filter*>(new or_filter()) :
dynamic_cast<multi_filter*>(new and_filter());
int error = false;
echo_xml_node** first = new(echo_xml_node*);
if(echo_xml_get_first_child(txe, first) == WIN)
{
echo_xml_type type = ECHO_XML_TYPE_NULL;
echo_xml_element** e = new(echo_xml_element*);
do
{
if(echo_xml_get_node_type(*first, &type) == WIN)
{
if(type == ECHO_XML_TYPE_ELEMENT)
{
if(echo_xml_to_element(*first, e) == WIN)
{
ret->add_filter(get_filter(*e, st, map));
}
else
{
error = true;
lderr("couldn't convert to element in \"or\" filter\n");
}
}
else if(type != ECHO_XML_TYPE_COMMENT)
{
error = true;
lderr("unknown node in \"or\" filter\n");
}
}
else
{
error = true;
lderr("couldn't get node type for \"or\" filter\n");
}
}
while(echo_xml_next_sibling(*first, first) == WIN && error == false);
delete e;
}
delete first;
if(error == true) delete ret;
else return(ret);
}
else
lderr("filter type unknown\n");
return(NULL);
}
/// Get the trigger from element "txe"
static trigger* get_trigger(echo_xml_element* txe, stage* st, DEPENDENCY_MAP* map)
{
/// Filter is optional; function does not fail if there is no filter
filter* f = NULL;
echo_xml_node** first = new(echo_xml_node*);
if(echo_xml_get_first_child(txe, first) == WIN)
{
/// @todo Account for comments in trigger section
echo_xml_element** e = new(echo_xml_element*);
if(echo_xml_to_element(*first, e) == WIN)
{
f = get_filter(*e, st, map, "and");
if(f == NULL)
{
delete e;
delete first;
return(NULL);
}
}
delete e;
}
delete *first;
delete first;
char* name = get_attribute(txe, "id", "no id for trigger");
if(name != NULL)
{
grid* g = st->get(name);
if(g)
{
trigger* ret = new trigger(f, g);
return(ret);
}
else
{
trigger* ret = new trigger(f);
LD_PRINT("trigger target (%s) is null, adding to dep map\n", name);
add(map, name, ret, &trigger::set_target);
return(ret);
}
}
return(NULL);
}
/// Add all the triggers from the first child of element "txe" to the grid "g"
static int add_triggers(echo_xml_element* txe, stage* st, DEPENDENCY_MAP* map, grid* g)
{
echo_xml_node** first = new(echo_xml_node*);
int error = false;
if(echo_xml_get_first_child(txe, first) == WIN)
{
//std::cout << "add_triggers: " << *txe << std::endl;
echo_xml_type type = ECHO_XML_TYPE_NULL;
echo_xml_element** e = new(echo_xml_element*);
do
{
//std::cout << "add_triggers: child: " << (*first)->name() << "; " << **first << std::endl;
if(echo_xml_get_node_type(*first, &type) == WIN)
{
if(type == ECHO_XML_TYPE_ELEMENT)
{
if(echo_xml_to_element(*first, e) == WIN)
{
g->add_trigger(get_trigger(*e, st, map));
}
else
{
error = true;
lderr("couldn't convert to element in trigger\n");
}
}
else if(type != ECHO_XML_TYPE_COMMENT)
{
error = true;
lderr("unknown node in trigger\n");
}
}
else
{