1 | #include <string> |
---|
2 | #include <list> |
---|
3 | |
---|
4 | |
---|
5 | #include "script_manager.h" |
---|
6 | #include "lunar.h" |
---|
7 | |
---|
8 | #include "class_list.h" |
---|
9 | |
---|
10 | #include "script.h" |
---|
11 | #include "script_trigger.h" |
---|
12 | #include "luaincl.h" |
---|
13 | #include "loading/load_param.h" |
---|
14 | #include "parser/tinyxml/tinyxml.h" |
---|
15 | |
---|
16 | |
---|
17 | |
---|
18 | ScriptManager::ScriptManager() |
---|
19 | { |
---|
20 | this->init(); |
---|
21 | } |
---|
22 | |
---|
23 | ScriptManager::~ScriptManager() |
---|
24 | { |
---|
25 | // delete all scripts since they are useless without the manager |
---|
26 | if(getScripts()) |
---|
27 | for(std::list<BaseObject*>::const_iterator it = scripts->begin(); it != scripts->end(); it++ ) |
---|
28 | { |
---|
29 | ClassList::removeFromClassList( (*it) ); |
---|
30 | } |
---|
31 | |
---|
32 | } |
---|
33 | |
---|
34 | |
---|
35 | void ScriptManager::loadParams(const TiXmlElement* root) |
---|
36 | { |
---|
37 | this->reset(); |
---|
38 | //BaseObject::loadParams(root); |
---|
39 | { |
---|
40 | LoadParamXML(root, "Scripts", this, ScriptManager, createScripts); |
---|
41 | |
---|
42 | LoadParamXML(root, "ScriptTriggers", this, ScriptManager, createTriggers); |
---|
43 | } // make shure that the loading process is finished |
---|
44 | |
---|
45 | // fill the scripts and triggers (doing that on runtime is very slow!) |
---|
46 | getTriggers(); |
---|
47 | getScripts(); |
---|
48 | |
---|
49 | |
---|
50 | } |
---|
51 | |
---|
52 | void ScriptManager::reset() |
---|
53 | { |
---|
54 | //Delete all scripts as they aren't deleted automatically |
---|
55 | if(getScripts()) |
---|
56 | for(std::list<BaseObject*>::const_iterator it = scripts->begin(); it != scripts->end(); it++ ) |
---|
57 | { |
---|
58 | ClassList::removeFromClassList( (*it) ); |
---|
59 | } |
---|
60 | |
---|
61 | this->scripts = NULL; |
---|
62 | this->triggers = NULL; |
---|
63 | } |
---|
64 | |
---|
65 | void ScriptManager::init() |
---|
66 | { |
---|
67 | //this->setClassID(""); |
---|
68 | this->setName("ScriptManager"); |
---|
69 | |
---|
70 | this->scripts = NULL; |
---|
71 | this->triggers = NULL; |
---|
72 | } |
---|
73 | |
---|
74 | |
---|
75 | void ScriptManager::createScripts(const TiXmlElement* scripts) |
---|
76 | { |
---|
77 | |
---|
78 | LOAD_PARAM_START_CYCLE(scripts, object); |
---|
79 | { |
---|
80 | new Script(object); |
---|
81 | } |
---|
82 | LOAD_PARAM_END_CYCLE(object); |
---|
83 | |
---|
84 | } |
---|
85 | |
---|
86 | void ScriptManager::createTriggers(const TiXmlElement* triggers) |
---|
87 | { |
---|
88 | LOAD_PARAM_START_CYCLE(triggers, object); |
---|
89 | { |
---|
90 | new ScriptTrigger(object); |
---|
91 | } |
---|
92 | LOAD_PARAM_END_CYCLE(object); |
---|
93 | |
---|
94 | } |
---|
95 | |
---|
96 | |
---|
97 | Script* ScriptManager::getScriptByFile(const std::string& file) |
---|
98 | { |
---|
99 | if(getScripts()) |
---|
100 | for(std::list<BaseObject*>::const_iterator it = scripts->begin(); it != scripts->end(); it++ ) |
---|
101 | { |
---|
102 | if( (dynamic_cast<Script*>(*it))->getFileName().compare(file) == 0) |
---|
103 | { |
---|
104 | return dynamic_cast<Script*>(*it); |
---|
105 | } |
---|
106 | } |
---|
107 | |
---|
108 | return NULL; |
---|
109 | |
---|
110 | } |
---|
111 | |
---|
112 | |
---|
113 | bool ScriptManager::getScripts() |
---|
114 | { |
---|
115 | return (this->scripts != NULL || (this->scripts = ClassList::getList(CL_SCRIPT)) != NULL); |
---|
116 | } |
---|
117 | |
---|
118 | bool ScriptManager::getTriggers() |
---|
119 | { |
---|
120 | return (this->triggers != NULL || (this->triggers = ClassList::getList(CL_SCRIPT_TRIGGER)) != NULL); |
---|
121 | } |
---|
122 | |
---|
123 | |
---|
124 | |
---|