xoreos  0.0.5
container.cpp
Go to the documentation of this file.
1 /* xoreos - A reimplementation of BioWare's Aurora engine
2  *
3  * xoreos is the legal property of its developers, whose names
4  * can be found in the AUTHORS file distributed with this source
5  * distribution.
6  *
7  * xoreos is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * xoreos is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with xoreos. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
25 #include "src/common/util.h"
26 #include "src/common/error.h"
27 
28 #include "src/aurora/gff3file.h"
29 #include "src/aurora/gff4file.h"
30 #include "src/aurora/gff4fields.h"
31 
35 
37 
39 
40 namespace Engines {
41 
42 namespace DragonAge2 {
43 
44 using namespace ::Aurora::GFF4FieldNamesEnum;
45 
46 static const char * const kScriptNames[] = {
47  "EventScript",
48  "Script",
49  "ScriptResRefID"
50 };
51 
52 static const GFF4FieldNames kScriptFields[] = {
55 };
56 
58 }
59 
61 }
62 
64  return _script;
65 }
66 
68  return !_script.empty();
69 }
70 
71 void ScriptContainer::enableEvent(EventType event, bool enabled) {
72  _eventEnabled[event] = enabled;
73 }
74 
75 void ScriptContainer::enableEvents(bool enabled) {
77 
78  for (size_t i = (int) kEventTypeSpellCastAt; i < kEventTypeAOETargetingGUIOpened; i++)
79  _eventEnabled[(EventType) i] = enabled;
80 }
81 
83  _script.clear();
84 }
85 
87  clearScript();
88 
89  for (size_t i = 0; i < ARRAYSIZE(kScriptNames); i++)
91 }
92 
94  clearScript();
95 
96  for (size_t i = 0; i < ARRAYSIZE(kScriptFields); i++)
98 }
99 
102  const Aurora::NWScript::ObjectReference triggerer) {
103 
104  Event e(event, triggerer, owner);
105 
106  return runScript(e);
107 }
108 
110  std::map<EventType, bool>::const_iterator enabled = _eventEnabled.find(event.getType());
111  if ((enabled == _eventEnabled.end()) != !enabled->second)
112  return true;
113 
114  return runScript(_script, event);
115 }
116 
119  const Aurora::NWScript::ObjectReference triggerer) {
120 
121  return runScript(script, event, Aurora::NWScript::NCSFile::getEmptyState(), owner, triggerer);
122 }
123 
125  const Aurora::NWScript::ScriptState &state,
127  const Aurora::NWScript::ObjectReference triggerer) {
128 
129 
130  Event e(event, triggerer, owner);
131 
132  return runScript(script, e, state);
133 }
134 
135 bool ScriptContainer::runScript(const Common::UString &script, Event &event) {
136  return runScript(script, event, Aurora::NWScript::NCSFile::getEmptyState());
137 }
138 
140  const Aurora::NWScript::ScriptState &state) {
141 
142  if (script.empty())
143  return true;
144 
145  try {
146  Aurora::NWScript::NCSFile ncs(script);
147 
148  ncs.getEnvironment().setVariable("Event", static_cast<Aurora::NWScript::EngineType *>(&event));
149 
150  const Aurora::NWScript::Variable &retVal = ncs.run(state, event.getTarget(), event.getCreator());
151  if (retVal.getType() == Aurora::NWScript::kTypeInt)
152  return retVal.getInt() != 0;
153  if (retVal.getType() == Aurora::NWScript::kTypeFloat)
154  return retVal.getFloat() != 0.0f;
155 
156  return true;
157 
158  } catch (...) {
159  Common::exceptionDispatcherWarning("Failed running script \"%s\"", script.c_str());
160  return false;
161  }
162 
163  return true;
164 }
165 
166 } // End of namespace DragonAge2
167 
168 } // End of namespace Engines
Handling version V3.2/V3.3 of BioWare&#39;s GFFs (generic file format).
VariableContainer & getEnvironment()
Return the script&#39;s environment variables.
Definition: ncsfile.cpp:298
const Common::UString & getScript() const
Definition: container.cpp:63
NWScript variable.
Common::UString getString(uint32 field, Common::Encoding encoding, const Common::UString &def="") const
Return a field string, read from the given encoding.
Definition: gff4file.cpp:949
A class holding an UTF-8 string.
Definition: ustring.h:48
All currently known fields in V4.0/V4.1 GFFs.
An object containing scripts.
void enableEvents(bool enabled)
Enable/Disable the handling of all events.
Definition: container.cpp:75
#define ARRAYSIZE(x)
Macro which determines the number of entries in a fixed size array.
Definition: util.h:131
bool runScript(EventType event, const Aurora::NWScript::ObjectReference owner=Aurora::NWScript::ObjectReference(), const Aurora::NWScript::ObjectReference triggerer=Aurora::NWScript::ObjectReference())
Definition: container.cpp:100
An NCS, BioWare&#39;s NWN Compile Script.
Definition: ncsfile.h:86
void exceptionDispatcherWarning(const char *s,...)
Exception dispatcher that prints the exception as a warning, and adds another reason on top...
Definition: error.cpp:158
Handling version V4.0/V4.1 of BioWare&#39;s GFFs (generic file format).
static const GFF4FieldNames kScriptFields[]
Definition: container.cpp:52
Basic exceptions to throw.
const char * c_str() const
Return the (utf8 encoded) string data.
Definition: ustring.cpp:249
void enableEvent(EventType event, bool enabled)
Enable/Disable the handling of a specific event.
Definition: container.cpp:71
const Variable & run(Object *owner=0, Object *triggerer=0)
Run the current script, from start to finish.
Definition: ncsfile.cpp:350
Utility templates and functions.
EventType getType() const
Definition: event.cpp:46
bool empty() const
Is the string empty?
Definition: ustring.cpp:245
std::map< EventType, bool > _eventEnabled
Definition: container.h:92
static ScriptState getEmptyState()
Definition: ncsfile.cpp:306
Handling BioWare&#39;s NWN Compiled Scripts.
A struct within a GFF3.
Definition: gff3file.h:164
A scripting event.
Common::UString getString(const Common::UString &field, const Common::UString &def="") const
Definition: gff3file.cpp:527
static const char *const kScriptNames[]
Definition: container.cpp:46
void setVariable(const Common::UString &var, const Variable &value)
NWScript types.
void clear()
Clear the string&#39;s contents.
Definition: ustring.cpp:236
EventType
Types for script events.
Definition: types.h:114
const Aurora::NWScript::ObjectReference & getTarget() const
Definition: event.cpp:62
void readScript(const Aurora::GFF3Struct &gff)
Definition: container.cpp:86