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 
36 
38 
40 
41 namespace Engines {
42 
43 namespace DragonAge {
44 
45 using namespace ::Aurora::GFF4FieldNamesEnum;
46 
47 static const char * const kScriptNames[] = {
48  "EventScript",
49  "Script",
50  "ScriptResRefID"
51 };
52 
53 static const GFF4FieldNames kScriptFields[] = {
56 };
57 
59 }
60 
62 }
63 
65  return _script;
66 }
67 
69  return !_script.empty();
70 }
71 
72 void ScriptContainer::enableEvent(EventType event, bool enabled) {
73  _eventEnabled[event] = enabled;
74 }
75 
76 void ScriptContainer::enableEvents(bool enabled) {
78 
79  for (size_t i = (int) kEventTypeSpellCastAt; i < kEventTypePartyPickerInit; i++)
80  _eventEnabled[(EventType) i] = enabled;
81 
83  _eventEnabled[(EventType) i] = enabled;
84 }
85 
87  _script.clear();
88 }
89 
91  clearScript();
92 
93  for (size_t i = 0; i < ARRAYSIZE(kScriptNames); i++)
95 }
96 
98  clearScript();
99 
100  for (size_t i = 0; i < ARRAYSIZE(kScriptFields); i++)
102 }
103 
106  const Aurora::NWScript::ObjectReference triggerer) {
107 
108  Event e(event, triggerer, owner);
109 
110  return runScript(e);
111 }
112 
114  std::map<EventType, bool>::const_iterator enabled = _eventEnabled.find(event.getType());
115  if ((enabled == _eventEnabled.end()) != !enabled->second)
116  return true;
117 
118  return runScript(_script, event);
119 }
120 
123  const Aurora::NWScript::ObjectReference triggerer) {
124 
125  return runScript(script, event, Aurora::NWScript::NCSFile::getEmptyState(), owner, triggerer);
126 }
127 
129  const Aurora::NWScript::ScriptState &state,
131  const Aurora::NWScript::ObjectReference triggerer) {
132 
133 
134  Event e(event, triggerer, owner);
135 
136  return runScript(script, e, state);
137 }
138 
139 bool ScriptContainer::runScript(const Common::UString &script, Event &event) {
140  return runScript(script, event, Aurora::NWScript::NCSFile::getEmptyState());
141 }
142 
144  const Aurora::NWScript::ScriptState &state) {
145 
146  if (script.empty())
147  return true;
148 
149  try {
150  Aurora::NWScript::NCSFile ncs(script);
151 
152  ncs.getEnvironment().setVariable("Event", static_cast<Aurora::NWScript::EngineType *>(&event));
153 
154  const Aurora::NWScript::Variable &retVal = ncs.run(state, event.getTarget(), event.getCreator());
155  if (retVal.getType() == Aurora::NWScript::kTypeInt)
156  return retVal.getInt() != 0;
157  if (retVal.getType() == Aurora::NWScript::kTypeFloat)
158  return retVal.getFloat() != 0.0f;
159 
160  return true;
161 
162  } catch (...) {
163  Common::exceptionDispatcherWarning("Failed running script \"%s\"", script.c_str());
164  return false;
165  }
166 
167  return true;
168 }
169 
170 } // End of namespace DragonAge
171 
172 } // 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
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.
void enableEvent(EventType event, bool enabled)
Enable/Disable the handling of a specific event.
Definition: container.cpp:72
#define ARRAYSIZE(x)
Macro which determines the number of entries in a fixed size array.
Definition: util.h:131
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
const Aurora::NWScript::ObjectReference & getTarget() const
Definition: event.cpp:61
std::map< EventType, bool > _eventEnabled
Definition: container.h:92
Handling version V4.0/V4.1 of BioWare&#39;s GFFs (generic file format).
Basic exceptions to throw.
static const char *const kScriptNames[]
Definition: container.cpp:47
const char * c_str() const
Return the (utf8 encoded) string data.
Definition: ustring.cpp:249
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.
A scripting event.
bool empty() const
Is the string empty?
Definition: ustring.cpp:245
EventType getType() const
Definition: event.cpp:45
static ScriptState getEmptyState()
Definition: ncsfile.cpp:306
Handling BioWare&#39;s NWN Compiled Scripts.
Reference to an NWScript object.
An object containing scripts.
A struct within a GFF3.
Definition: gff3file.h:164
static const GFF4FieldNames kScriptFields[]
Definition: container.cpp:53
Common::UString getString(const Common::UString &field, const Common::UString &def="") const
Definition: gff3file.cpp:527
void readScript(const Aurora::GFF3Struct &gff)
Definition: container.cpp:90
void setVariable(const Common::UString &var, const Variable &value)
const Common::UString & getScript() const
Definition: container.cpp:64
NWScript types.
void clear()
Clear the string&#39;s contents.
Definition: ustring.cpp:236
bool runScript(EventType event, const Aurora::NWScript::ObjectReference owner=Aurora::NWScript::ObjectReference(), const Aurora::NWScript::ObjectReference triggerer=Aurora::NWScript::ObjectReference())
Definition: container.cpp:104
void enableEvents(bool enabled)
Enable/Disable the handling of all events.
Definition: container.cpp:76
EventType
Types for script events.
Definition: types.h:110