Skip to content

Commit

Permalink
AURORA: Use ScopedPtr in NCSFile
Browse files Browse the repository at this point in the history
  • Loading branch information
DrMcCoy committed Oct 26, 2016
1 parent cf7d245 commit deeeb62
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 129 deletions.
180 changes: 52 additions & 128 deletions src/aurora/nwscript/ncsfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,28 +274,21 @@ void NCSFile::setupOpcodes() {

#undef OPCODE

NCSFile::NCSFile(Common::SeekableReadStream *ncs) : _owner(0), _triggerer(0) {
_script = ncs;
NCSFile::NCSFile(Common::SeekableReadStream *ncs) : _script(ncs), _owner(0), _triggerer(0) {
assert(_script);

try {
load();
} catch (...) {
delete _script;
}
load();
}

NCSFile::NCSFile(const Common::UString &ncs) : _name(ncs), _script(0),
_owner(0), _triggerer(0) {

_script = ResMan.getResource(ncs, kFileTypeNCS);
NCSFile::NCSFile(const Common::UString &ncs) : _name(ncs), _owner(0), _triggerer(0) {
_script.reset(ResMan.getResource(ncs, kFileTypeNCS));
if (!_script)
throw Common::Exception("No such NCS \"%s\"", ncs.c_str());

load();
}

NCSFile::~NCSFile() {
delete _script;
}

const Common::UString &NCSFile::getName() const {
Expand Down Expand Up @@ -415,11 +408,7 @@ bool NCSFile::executeStep() {

debugC(kDebugScripts, 1, "NWScript opcode %s [0x%02X]", _opcodes[opcode].desc, opcode);

try {
(this->*(_opcodes[opcode].proc))((InstructionType)type);
} catch (Common::Exception &UNUSED(e)) {
throw;
}
(this->*(_opcodes[opcode].proc))((InstructionType)type);

_stack.print();
debugC(kDebugScripts, 2, "[RETURN: %d]",
Expand Down Expand Up @@ -648,69 +637,49 @@ void NCSFile::o_logand(InstructionType type) {
if (type != kInstTypeIntInt)
throw Common::Exception("NCSFile::o_logand(): Illegal type %d", type);

try {
int32 arg1 = _stack.pop().getInt();
int32 arg2 = _stack.pop().getInt();
_stack.push(arg1 && arg2);
} catch (Common::Exception &UNUSED(e)) {
throw;
}
int32 arg1 = _stack.pop().getInt();
int32 arg2 = _stack.pop().getInt();
_stack.push(arg1 && arg2);
}

/** LOGOR: perform a logical boolean OR (||). */
void NCSFile::o_logor(InstructionType type) {
if (type != kInstTypeIntInt)
throw Common::Exception("NCSFile::o_logor(): Illegal type %d", type);

try {
int32 arg1 = _stack.pop().getInt();
int32 arg2 = _stack.pop().getInt();
_stack.push(arg1 || arg2);
} catch (Common::Exception &UNUSED(e)) {
throw;
}
int32 arg1 = _stack.pop().getInt();
int32 arg2 = _stack.pop().getInt();
_stack.push(arg1 || arg2);
}

/** INCOR: perform a bit-wise inclusive OR (|). */
void NCSFile::o_incor(InstructionType type) {
if (type != kInstTypeIntInt)
throw Common::Exception("NCSFile::o_incor(): Illegal type %d", type);

try {
int32 arg1 = _stack.pop().getInt();
int32 arg2 = _stack.pop().getInt();
_stack.push(arg1 | arg2);
} catch (Common::Exception &UNUSED(e)) {
throw;
}
int32 arg1 = _stack.pop().getInt();
int32 arg2 = _stack.pop().getInt();
_stack.push(arg1 | arg2);
}

/** EXCOR: perform a bit-wise exclusive OR (^). */
void NCSFile::o_excor(InstructionType type) {
if (type != kInstTypeIntInt)
throw Common::Exception("NCSFile::o_excor(): Illegal type %d", type);

try {
int32 arg1 = _stack.pop().getInt();
int32 arg2 = _stack.pop().getInt();
_stack.push(arg1 ^ arg2);
} catch (Common::Exception &UNUSED(e)) {
throw;
}
int32 arg1 = _stack.pop().getInt();
int32 arg2 = _stack.pop().getInt();
_stack.push(arg1 ^ arg2);
}

/** BOOLAND: perform a bit-wise AND (&). */
void NCSFile::o_booland(InstructionType type) {
if (type != kInstTypeIntInt)
throw Common::Exception("NCSFile::o_booland(): Illegal type %d", type);

try {
int32 arg1 = _stack.pop().getInt();
int32 arg2 = _stack.pop().getInt();
_stack.push(arg1 & arg2);
} catch (Common::Exception &UNUSED(e)) {
throw;
}
int32 arg1 = _stack.pop().getInt();
int32 arg2 = _stack.pop().getInt();
_stack.push(arg1 & arg2);
}

/** EQ: compare the top-most stack elements for equality (==). */
Expand Down Expand Up @@ -775,22 +744,18 @@ void NCSFile::o_neq(InstructionType type) {
void NCSFile::o_geq(InstructionType type) {
switch (type) {
case kInstTypeIntInt:
try {
{
int32 arg1 = _stack.pop().getInt();
int32 arg2 = _stack.pop().getInt();
_stack.push(arg2 >= arg1);
} catch (Common::Exception &UNUSED(e)) {
throw;
}
break;

case kInstTypeFloatFloat:
try {
{
float arg1 = _stack.pop().getFloat();
float arg2 = _stack.pop().getFloat();
_stack.push(arg2 >= arg1);
} catch (Common::Exception &UNUSED(e)) {
throw;
}
break;

Expand All @@ -803,22 +768,18 @@ void NCSFile::o_geq(InstructionType type) {
void NCSFile::o_gt(InstructionType type) {
switch (type) {
case kInstTypeIntInt:
try {
{
int32 arg1 = _stack.pop().getInt();
int32 arg2 = _stack.pop().getInt();
_stack.push(arg2 > arg1);
} catch (Common::Exception &UNUSED(e)) {
throw;
}
break;

case kInstTypeFloatFloat:
try {
{
float arg1 = _stack.pop().getFloat();
float arg2 = _stack.pop().getFloat();
_stack.push(arg2 > arg1);
} catch (Common::Exception &UNUSED(e)) {
throw;
}
break;

Expand All @@ -831,22 +792,18 @@ void NCSFile::o_gt(InstructionType type) {
void NCSFile::o_lt(InstructionType type) {
switch (type) {
case kInstTypeIntInt:
try {
{
int32 arg1 = _stack.pop().getInt();
int32 arg2 = _stack.pop().getInt();
_stack.push(arg2 < arg1);
} catch (Common::Exception &UNUSED(e)) {
throw;
}
break;

case kInstTypeFloatFloat:
try {
{
float arg1 = _stack.pop().getFloat();
float arg2 = _stack.pop().getFloat();
_stack.push(arg2 < arg1);
} catch (Common::Exception &UNUSED(e)) {
throw;
}
break;

Expand All @@ -859,22 +816,18 @@ void NCSFile::o_lt(InstructionType type) {
void NCSFile::o_leq(InstructionType type) {
switch (type) {
case kInstTypeIntInt:
try {
{
int32 arg1 = _stack.pop().getInt();
int32 arg2 = _stack.pop().getInt();
_stack.push(arg2 <= arg1);
} catch (Common::Exception &UNUSED(e)) {
throw;
}
break;

case kInstTypeFloatFloat:
try {
{
float arg1 = _stack.pop().getFloat();
float arg2 = _stack.pop().getFloat();
_stack.push(arg2 <= arg1);
} catch (Common::Exception &UNUSED(e)) {
throw;
}
break;

Expand All @@ -888,13 +841,9 @@ void NCSFile::o_shleft(InstructionType type) {
if (type != kInstTypeIntInt)
throw Common::Exception("NCSFile::o_shleft(): Illegal type %d", type);

try {
int32 arg1 = _stack.pop().getInt();
int32 arg2 = _stack.pop().getInt();
_stack.push(arg2 << arg1);
} catch (Common::Exception &UNUSED(e)) {
throw;
}
int32 arg1 = _stack.pop().getInt();
int32 arg2 = _stack.pop().getInt();
_stack.push(arg2 << arg1);
}

/** SHRIGHT: signed-shift the top-most stack element to the right (>>>). */
Expand All @@ -908,19 +857,14 @@ void NCSFile::o_shright(InstructionType type) {
if (type != kInstTypeIntInt)
throw Common::Exception("NCSFile::o_shright(): Illegal type %d", type);

try {
int32 arg1 = _stack.pop().getInt();
int32 arg2 = _stack.pop().getInt();

if (arg2 < 0) {
arg2 = -arg2;
_stack.push(-(arg2 >> arg1));
} else
_stack.push(arg2 >> arg1);
int32 arg1 = _stack.pop().getInt();
int32 arg2 = _stack.pop().getInt();

} catch (Common::Exception &UNUSED(e)) {
throw;
}
if (arg2 < 0) {
arg2 = -arg2;
_stack.push(-(arg2 >> arg1));
} else
_stack.push(arg2 >> arg1);
}

/** USHRIGHT: shift the top-most stack element to the right (>>). */
Expand All @@ -933,52 +877,36 @@ void NCSFile::o_ushright(InstructionType type) {
if (type != kInstTypeIntInt)
throw Common::Exception("NCSFile::o_ushright(): Illegal type %d", type);

try {
int32 arg1 = _stack.pop().getInt();
int32 arg2 = _stack.pop().getInt();
_stack.push(arg2 >> arg1);
} catch (Common::Exception &UNUSED(e)) {
throw;
}
int32 arg1 = _stack.pop().getInt();
int32 arg2 = _stack.pop().getInt();
_stack.push(arg2 >> arg1);
}

/** MOD: calculate the remainder (modulo) of an integer division (%). */
void NCSFile::o_mod(InstructionType type) {
if (type != kInstTypeIntInt)
throw Common::Exception("NCSFile::o_mod(): Illegal type %d", type);

try {
int32 arg1 = _stack.pop().getInt();
int32 arg2 = _stack.pop().getInt();
int32 arg1 = _stack.pop().getInt();
int32 arg2 = _stack.pop().getInt();

if (arg1 == 0)
throw Common::Exception("NCSFile::o_mod(): Modulus by zero");
else if (arg1 < 0 || arg2 < 0)
throw Common::Exception("NCSFile::o_mod(): Modulus by negative number (%d %% %d)", arg2, arg1);
if (arg1 == 0)
throw Common::Exception("NCSFile::o_mod(): Modulus by zero");
else if (arg1 < 0 || arg2 < 0)
throw Common::Exception("NCSFile::o_mod(): Modulus by negative number (%d %% %d)", arg2, arg1);

_stack.push(arg2 % arg1);
} catch (Common::Exception &UNUSED(e)) {
throw;
}
_stack.push(arg2 % arg1);
}

/** NEQ: negate the top-most stack element (unary -). */
void NCSFile::o_neg(InstructionType type) {
switch (type) {
case kInstTypeInt:
try {
_stack.push(-_stack.pop().getInt());
} catch (Common::Exception &UNUSED(e)) {
throw;
}
_stack.push(-_stack.pop().getInt());
break;

case kInstTypeFloat:
try {
_stack.push(-_stack.pop().getFloat());
} catch (Common::Exception &UNUSED(e)) {
throw;
}
_stack.push(-_stack.pop().getFloat());
break;

default:
Expand All @@ -991,11 +919,7 @@ void NCSFile::o_comp(InstructionType type) {
if (type != kInstTypeInt)
throw Common::Exception("NCSFile::o_comp(): Illegal type %d", type);

try {
_stack.push(~_stack.pop().getInt());
} catch (Common::Exception &UNUSED(e)) {
throw;
}
_stack.push(~_stack.pop().getInt());
}

/** MOVSP: pop elements off the stack. */
Expand Down
3 changes: 2 additions & 1 deletion src/aurora/nwscript/ncsfile.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <stack>

#include "src/common/types.h"
#include "src/common/scopedptr.h"

#include "src/aurora/types.h"
#include "src/aurora/aurorafile.h"
Expand Down Expand Up @@ -160,7 +161,7 @@ class NCSFile : public AuroraFile {
Common::UString _name;

NCSStack _stack;
Common::SeekableReadStream *_script;
Common::ScopedPtr<Common::SeekableReadStream> _script;

Variable _return;

Expand Down

0 comments on commit deeeb62

Please sign in to comment.