xoreos  0.0.5
ncsfile.h
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 #ifndef AURORA_NWSCRIPT_NCSFILE_H
26 #define AURORA_NWSCRIPT_NCSFILE_H
27 
28 #include <vector>
29 #include <stack>
30 
31 #include "src/common/types.h"
32 #include "src/common/scopedptr.h"
33 
34 #include "src/aurora/types.h"
35 #include "src/aurora/aurorafile.h"
36 
41 
42 namespace Common {
43  class UString;
44  class SeekableReadStream;
45 }
46 
47 namespace Aurora {
48 
49 namespace NWScript {
50 
51 class NCSStack : public std::vector<Variable> {
52 public:
53  NCSStack();
54  ~NCSStack();
55 
56  void reset();
57 
58  bool empty() const;
59 
60  Variable &top();
61  Variable pop();
62  void push(const Variable &obj);
63 
64  Variable &getRelSP(int32 pos);
65  void setRelSP(int32 pos, const Variable &obj);
66 
67  Variable &getRelBP(int32 pos);
68  void setRelBP(int32 pos, const Variable &obj);
69 
71  void setStackPtr(int32 pos);
72 
73  int32 getBasePtr();
74  void setBasePtr(int32 pos);
75 
76  void print() const;
77 
78 private:
81 };
82 
83 #define DECLARE_OPCODE(x) void x(InstructionType type)
84 
86 class NCSFile : public AuroraFile {
87 public:
89  NCSFile(const Common::UString &ncs);
90  ~NCSFile();
91 
92  const Common::UString &getName() const;
93 
103  void setEnvironment(const VariableContainer &env);
104 
106  const Variable &run(Object *owner = 0, Object *triggerer = 0);
107  const Variable &run(const ObjectReference owner = ObjectReference(),
108  const ObjectReference triggerer = ObjectReference());
109 
111  const Variable &run(const ScriptState &state, Object *owner = 0, Object *triggerer = 0);
112  const Variable &run(const ScriptState &state,
113  const ObjectReference owner = ObjectReference(),
114  const ObjectReference triggerer = ObjectReference());
115 
116  static ScriptState getEmptyState();
117 
118 private:
120  // Unary
128  kInstTypeEngineType0 = 16, // NWN: effect DA: event
129  kInstTypeEngineType1 = 17, // NWN: event DA: location
130  kInstTypeEngineType2 = 18, // NWN: location DA: command
131  kInstTypeEngineType3 = 19, // NWN: talent DA: effect
132  kInstTypeEngineType4 = 20, // NWN: itemproperty DA: itemproperty
133  kInstTypeEngineType5 = 21, // Witcher: mod DA: player
134 
135  // Arrays
147 
148  // Binary
165  };
166 
168 
171 
173 
176 
178 
179  std::stack<uint32> _returnOffsets;
180 
182 
183  typedef void (NCSFile::*OpcodeProc)(InstructionType type);
184  struct Opcode {
186  const char *desc;
187  };
188  const Opcode *_opcodes;
190  void setupOpcodes();
191 
192  void load();
193 
195  void reset();
196 
197  const Variable &execute(const ObjectReference owner = ObjectReference(),
198  const ObjectReference triggerer = ObjectReference());
199 
201  bool executeStep();
202 
203  void decompile(); // TODO
204 
205  void callEngine(Aurora::NWScript::FunctionContext &ctx, uint32 function, uint8 argCount);
206 
207  // Opcode declarations
257 };
258 
259 #undef DECLARE_OPCODE
260 
261 } // End of namespace NWScript
262 
263 } // End of namespace Aurora
264 
265 #endif // AURORA_NWSCRIPT_NCSFILE_H
VariableContainer & getEnvironment()
Return the script&#39;s environment variables.
Definition: ncsfile.cpp:298
void o_retn(InstructionType type)
RETN: return from a subroutine call.
Definition: ncsfile.cpp:1375
ObjectReference _triggerer
Definition: ncsfile.h:175
void o_ushright(InstructionType type)
USHRIGHT: shift the top-most stack element to the right (>>).
Definition: ncsfile.cpp:879
NWScript variable.
void o_writearray(InstructionType type)
WRITEARRAY: write the value of an array element on the stack.
Definition: ncsfile.cpp:1506
void o_leq(InstructionType type)
LEQ: compare the top-most stack elements, less-or-equal (<=).
Definition: ncsfile.cpp:824
Definition: 2dafile.h:39
void reset()
Reset the script for another execution.
Definition: ncsfile.cpp:338
void o_destruct(InstructionType type)
DESTRUCT: remove elements from the stack.
Definition: ncsfile.cpp:1389
A class holding an UTF-8 string.
Definition: ustring.h:48
void o_getref(InstructionType type)
GETREF: push the reference to a stack element onto the stack.
Definition: ncsfile.cpp:1573
void o_incsp(InstructionType type)
INCSP: increment the value of a stack element (++).
Definition: ncsfile.cpp:980
void o_decsp(InstructionType type)
DECSP: decrement the value of a stack element (–).
Definition: ncsfile.cpp:970
uint8_t uint8
Definition: types.h:200
void setBasePtr(int32 pos)
Definition: ncsfile.cpp:167
Variable & getRelSP(int32 pos)
Definition: ncsfile.cpp:105
void o_movsp(InstructionType type)
MOVSP: pop elements off the stack.
Definition: ncsfile.cpp:934
void o_comp(InstructionType type)
COMP: calculate the 1-complement of the top-most stack element (~).
Definition: ncsfile.cpp:926
bool executeStep()
Execute one script step.
Definition: ncsfile.cpp:404
#define DECLARE_OPCODE(x)
Definition: ncsfile.h:83
ObjectReference _owner
Definition: ncsfile.h:174
void o_lt(InstructionType type)
LT: compare the top-most stack elements, less (<).
Definition: ncsfile.cpp:800
void o_rsadd(InstructionType type)
RSADD: push an empty variable onto the stack.
Definition: ncsfile.cpp:440
An NCS, BioWare&#39;s NWN Compile Script.
Definition: ncsfile.h:86
void o_jsr(InstructionType type)
JSR: call a subroutine.
Definition: ncsfile.cpp:1362
A simple scoped smart pointer template.
const Variable & execute(const ObjectReference owner=ObjectReference(), const ObjectReference triggerer=ObjectReference())
Definition: ncsfile.cpp:384
void o_excor(InstructionType type)
EXCOR: perform a bit-wise exclusive OR (^).
Definition: ncsfile.cpp:674
void o_cptopbp(InstructionType type)
CPTOPBP: push a copy of a base-pointer stack element on top of the stack.
Definition: ncsfile.cpp:1447
Variable & getRelBP(int32 pos)
Definition: ncsfile.cpp:127
Common::ScopedPtr< Common::SeekableReadStream > _script
Definition: ncsfile.h:170
const Variable & run(Object *owner=0, Object *triggerer=0)
Run the current script, from start to finish.
Definition: ncsfile.cpp:350
void o_savebp(InstructionType type)
SAVEBP: set the value of the base-pointer.
Definition: ncsfile.cpp:1024
void o_restorebp(InstructionType type)
RESTOREBP: restore the value of the base-pointer to a prior value.
Definition: ncsfile.cpp:1036
void o_storestate(InstructionType type)
STORESTATE: create a functor of a subroutine with the current stack.
Definition: ncsfile.cpp:1470
Base for BioWare&#39;s Aurora engine files.
void o_eq(InstructionType type)
EQ: compare the top-most stack elements for equality (==).
Definition: ncsfile.cpp:694
void o_add(InstructionType type)
ADD: add the top-most stack elements (+).
Definition: ncsfile.cpp:1088
void o_jnz(InstructionType type)
JNZ: jump conditionally if the top-most stack element is not 0.
Definition: ncsfile.cpp:990
void o_neq(InstructionType type)
NEQ: compare the top-most stack elements for inequality (!=).
Definition: ncsfile.cpp:723
Common::UString _name
Definition: ncsfile.h:167
void o_sub(InstructionType type)
SUB: subtract the top-most stack elements (-).
Definition: ncsfile.cpp:1150
Low-level type definitions to handle fixed width types portably.
void o_storestateall(InstructionType type)
STORESTATEALL: unused, obsolete opcode.
Definition: ncsfile.cpp:1353
void o_readarray(InstructionType type)
READARRAY: push the value of an array element onto of the stack.
Definition: ncsfile.cpp:1541
void(NCSFile::* OpcodeProc)(InstructionType type)
Definition: ncsfile.h:183
void o_const(InstructionType type)
CONST: push a constant (predetermined value) variable onto the stack.
Definition: ncsfile.cpp:482
std::stack< uint32 > _returnOffsets
Definition: ncsfile.h:179
void setStackPtr(int32 pos)
Definition: ncsfile.cpp:153
void setRelSP(int32 pos, const Variable &obj)
Definition: ncsfile.cpp:116
void o_logor(InstructionType type)
LOGOR: perform a logical boolean OR (||).
Definition: ncsfile.cpp:654
void o_mul(InstructionType type)
MUL: multiply the top-most stack elements (*).
Definition: ncsfile.cpp:1204
void o_getrefarray(InstructionType type)
GETREFARRAY: push the reference to an array element onto the stack.
Definition: ncsfile.cpp:1604
An NWScript variable container.
void o_neg(InstructionType type)
NEQ: negate the top-most stack element (unary -).
Definition: ncsfile.cpp:910
static ScriptState getEmptyState()
Definition: ncsfile.cpp:306
void o_cpdownsp(InstructionType type)
CPDOWNSP: copy a value into an existing stack element.
Definition: ncsfile.cpp:1049
void callEngine(Aurora::NWScript::FunctionContext &ctx, uint32 function, uint8 argCount)
Helper function for o_action(), doing the actual engine function calling.
Definition: ncsfile.cpp:529
Basic type definitions to handle files used in BioWare&#39;s Aurora engine.
void o_div(InstructionType type)
DIV: divide the top-most stack elements (/).
Definition: ncsfile.cpp:1268
void push(const Variable &obj)
Definition: ncsfile.cpp:93
void o_action(InstructionType type)
ACTION: call a game-specific engine function.
Definition: ncsfile.cpp:625
Reference to an NWScript object.
void o_nop(InstructionType type)
NOP: no operation.
Definition: ncsfile.cpp:1044
void o_cptopsp(InstructionType type)
CPTOPSP: push a copy of a stack element on top of the stack.
Definition: ncsfile.cpp:1070
void o_logand(InstructionType type)
LOGAND: perform a logical boolean AND (&&).
Definition: ncsfile.cpp:644
void o_mod(InstructionType type)
MOD: calculate the remainder (modulo) of an integer division (%).
Definition: ncsfile.cpp:894
uint32_t uint32
Definition: types.h:204
NCSFile(Common::SeekableReadStream *ncs)
Definition: ncsfile.cpp:277
Base class for most files found in games using BioWare&#39;s Aurora engine.
Definition: aurorafile.h:52
void o_jz(InstructionType type)
JZ: jump conditionally if the top-most stack element is 0.
Definition: ncsfile.cpp:951
void o_cpdownbp(InstructionType type)
CPDOWNBP: copy a value into an existing base-pointer stack element.
Definition: ncsfile.cpp:1423
void o_not(InstructionType type)
NOT: boolean-negate the top-most stack element (!).
Definition: ncsfile.cpp:962
void setRelBP(int32 pos, const Variable &obj)
Definition: ncsfile.cpp:138
void o_incbp(InstructionType type)
INCBP: increment the value of a base-pointer stack element (++).
Definition: ncsfile.cpp:1011
const Common::UString & getName() const
Definition: ncsfile.cpp:294
VariableContainer _env
Definition: ncsfile.h:177
void o_gt(InstructionType type)
GT: compare the top-most stack elements, greater (>).
Definition: ncsfile.cpp:776
void o_shright(InstructionType type)
SHRIGHT: signed-shift the top-most stack element to the right (>>>).
Definition: ncsfile.cpp:858
void o_decbp(InstructionType type)
DECBP: decrement the value of a base-pointer stack element (–).
Definition: ncsfile.cpp:1001
void o_booland(InstructionType type)
BOOLAND: perform a bit-wise AND (&).
Definition: ncsfile.cpp:684
void o_jmp(InstructionType type)
JMP: jump directly to a different script offset.
Definition: ncsfile.cpp:942
NWScript types.
const Opcode * _opcodes
Definition: ncsfile.h:188
void o_incor(InstructionType type)
INCOR: perform a bit-wise inclusive OR (|).
Definition: ncsfile.cpp:664
Interface for a seekable & readable data stream.
Definition: readstream.h:265
void o_geq(InstructionType type)
GEQ: compare the top-most stack elements, greater-or-equal (>=).
Definition: ncsfile.cpp:752
void o_shleft(InstructionType type)
SHLEFT: shift the top-most stack element to the left (<<).
Definition: ncsfile.cpp:848
void setEnvironment(const VariableContainer &env)
Overwrite the environment.
Definition: ncsfile.cpp:302
int32_t int32
Definition: types.h:203