From 113961fd2a2203434b03766d722a0f8c0854bfd0 Mon Sep 17 00:00:00 2001 From: Einar Johan Trøan Sømåen Date: Thu, 19 Jul 2012 19:29:15 +0200 Subject: WINTERMUTE: Change all folder-names to lowercase. --- engines/wintermute/base/scriptables/SXArray.cpp | 238 ++++ engines/wintermute/base/scriptables/SXArray.h | 54 + engines/wintermute/base/scriptables/SXDate.cpp | 297 ++++ engines/wintermute/base/scriptables/SXDate.h | 55 + engines/wintermute/base/scriptables/SXFile.cpp | 779 +++++++++++ engines/wintermute/base/scriptables/SXFile.h | 66 + engines/wintermute/base/scriptables/SXMath.cpp | 295 ++++ engines/wintermute/base/scriptables/SXMath.h | 53 + .../wintermute/base/scriptables/SXMemBuffer.cpp | 508 +++++++ engines/wintermute/base/scriptables/SXMemBuffer.h | 59 + engines/wintermute/base/scriptables/SXString.cpp | 404 ++++++ engines/wintermute/base/scriptables/SXString.h | 58 + engines/wintermute/base/scriptables/ScEngine.cpp | 712 ++++++++++ engines/wintermute/base/scriptables/ScEngine.h | 147 ++ engines/wintermute/base/scriptables/ScScript.cpp | 1461 ++++++++++++++++++++ engines/wintermute/base/scriptables/ScScript.h | 183 +++ engines/wintermute/base/scriptables/ScStack.cpp | 226 +++ engines/wintermute/base/scriptables/ScStack.h | 66 + engines/wintermute/base/scriptables/ScValue.cpp | 1054 ++++++++++++++ engines/wintermute/base/scriptables/ScValue.h | 141 ++ engines/wintermute/base/scriptables/SxObject.cpp | 67 + engines/wintermute/base/scriptables/SxObject.h | 47 + 22 files changed, 6970 insertions(+) create mode 100644 engines/wintermute/base/scriptables/SXArray.cpp create mode 100644 engines/wintermute/base/scriptables/SXArray.h create mode 100644 engines/wintermute/base/scriptables/SXDate.cpp create mode 100644 engines/wintermute/base/scriptables/SXDate.h create mode 100644 engines/wintermute/base/scriptables/SXFile.cpp create mode 100644 engines/wintermute/base/scriptables/SXFile.h create mode 100644 engines/wintermute/base/scriptables/SXMath.cpp create mode 100644 engines/wintermute/base/scriptables/SXMath.h create mode 100644 engines/wintermute/base/scriptables/SXMemBuffer.cpp create mode 100644 engines/wintermute/base/scriptables/SXMemBuffer.h create mode 100644 engines/wintermute/base/scriptables/SXString.cpp create mode 100644 engines/wintermute/base/scriptables/SXString.h create mode 100644 engines/wintermute/base/scriptables/ScEngine.cpp create mode 100644 engines/wintermute/base/scriptables/ScEngine.h create mode 100644 engines/wintermute/base/scriptables/ScScript.cpp create mode 100644 engines/wintermute/base/scriptables/ScScript.h create mode 100644 engines/wintermute/base/scriptables/ScStack.cpp create mode 100644 engines/wintermute/base/scriptables/ScStack.h create mode 100644 engines/wintermute/base/scriptables/ScValue.cpp create mode 100644 engines/wintermute/base/scriptables/ScValue.h create mode 100644 engines/wintermute/base/scriptables/SxObject.cpp create mode 100644 engines/wintermute/base/scriptables/SxObject.h (limited to 'engines/wintermute/base/scriptables') diff --git a/engines/wintermute/base/scriptables/SXArray.cpp b/engines/wintermute/base/scriptables/SXArray.cpp new file mode 100644 index 0000000000..425118a3e7 --- /dev/null +++ b/engines/wintermute/base/scriptables/SXArray.cpp @@ -0,0 +1,238 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +/* + * This file is based on WME Lite. + * http://dead-code.org/redir.php?target=wmelite + * Copyright (c) 2011 Jan Nedoma + */ + +#include "engines/wintermute/persistent.h" +#include "engines/wintermute/base/scriptables/ScValue.h" +#include "engines/wintermute/base/scriptables/ScStack.h" +#include "engines/wintermute/system/SysInstance.h" +#include "engines/wintermute/base/scriptables/SXArray.h" + +namespace WinterMute { + +IMPLEMENT_PERSISTENT(CSXArray, false) + +CBScriptable *makeSXArray(CBGame *inGame, CScStack *stack) { + return new CSXArray(inGame, stack); +} + +////////////////////////////////////////////////////////////////////////// +CSXArray::CSXArray(CBGame *inGame, CScStack *stack): CBScriptable(inGame) { + _length = 0; + _values = new CScValue(_gameRef); + + int numParams = stack->pop()->getInt(0); + + if (numParams == 1) _length = stack->pop()->getInt(0); + else if (numParams > 1) { + _length = numParams; + char paramName[20]; + for (int i = 0; i < numParams; i++) { + sprintf(paramName, "%d", i); + _values->setProp(paramName, stack->pop()); + } + } +} + +////////////////////////////////////////////////////////////////////////// +CSXArray::CSXArray(CBGame *inGame): CBScriptable(inGame) { + _length = 0; + _values = new CScValue(_gameRef); +} + + +////////////////////////////////////////////////////////////////////////// +CSXArray::~CSXArray() { + delete _values; + _values = NULL; +} + + +////////////////////////////////////////////////////////////////////////// +const char *CSXArray::scToString() { + static char dummy[32768]; // TODO: Get rid of static. + strcpy(dummy, ""); + char propName[20]; + for (int i = 0; i < _length; i++) { + sprintf(propName, "%d", i); + CScValue *val = _values->getProp(propName); + if (val) { + if (strlen(dummy) + strlen(val->getString()) < 32768) { + strcat(dummy, val->getString()); + } + } + + if (i < _length - 1 && strlen(dummy) + 1 < 32768) strcat(dummy, ","); + } + return dummy; +} + + +////////////////////////////////////////////////////////////////////////// +bool CSXArray::scCallMethod(CScScript *script, CScStack *stack, CScStack *thisStack, const char *name) { + ////////////////////////////////////////////////////////////////////////// + // Push + ////////////////////////////////////////////////////////////////////////// + if (strcmp(name, "Push") == 0) { + int numParams = stack->pop()->getInt(0); + char paramName[20]; + + for (int i = 0; i < numParams; i++) { + _length++; + sprintf(paramName, "%d", _length - 1); + _values->setProp(paramName, stack->pop(), true); + } + stack->pushInt(_length); + + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // Pop + ////////////////////////////////////////////////////////////////////////// + if (strcmp(name, "Pop") == 0) { + + stack->correctParams(0); + + if (_length > 0) { + char paramName[20]; + sprintf(paramName, "%d", _length - 1); + stack->push(_values->getProp(paramName)); + _values->deleteProp(paramName); + _length--; + } else stack->pushNULL(); + + return STATUS_OK; + } + + else return STATUS_FAILED; +} + + +////////////////////////////////////////////////////////////////////////// +CScValue *CSXArray::scGetProperty(const char *name) { + _scValue->setNULL(); + + ////////////////////////////////////////////////////////////////////////// + // Type + ////////////////////////////////////////////////////////////////////////// + if (strcmp(name, "Type") == 0) { + _scValue->setString("array"); + return _scValue; + } + + ////////////////////////////////////////////////////////////////////////// + // Length + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "Length") == 0) { + _scValue->setInt(_length); + return _scValue; + } + + ////////////////////////////////////////////////////////////////////////// + // [number] + ////////////////////////////////////////////////////////////////////////// + else { + char ParamName[20]; + if (validNumber(name, ParamName)) { + return _values->getProp(ParamName); + } else return _scValue; + } +} + + +////////////////////////////////////////////////////////////////////////// +bool CSXArray::scSetProperty(const char *name, CScValue *value) { + ////////////////////////////////////////////////////////////////////////// + // Length + ////////////////////////////////////////////////////////////////////////// + if (strcmp(name, "Length") == 0) { + int OrigLength = _length; + _length = MAX(value->getInt(0), 0); + + char PropName[20]; + if (_length < OrigLength) { + for (int i = _length; i < OrigLength; i++) { + sprintf(PropName, "%d", i); + _values->deleteProp(PropName); + } + } + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // [number] + ////////////////////////////////////////////////////////////////////////// + else { + char paramName[20]; + if (validNumber(name, paramName)) { + int Index = atoi(paramName); + if (Index >= _length) _length = Index + 1; + return _values->setProp(paramName, value); + } else return STATUS_FAILED; + } +} + + +////////////////////////////////////////////////////////////////////////// +bool CSXArray::persist(CBPersistMgr *persistMgr) { + CBScriptable::persist(persistMgr); + + persistMgr->transfer(TMEMBER(_length)); + persistMgr->transfer(TMEMBER(_values)); + + return STATUS_OK; +} + + +////////////////////////////////////////////////////////////////////////// +bool CSXArray::validNumber(const char *origStr, char *outStr) { + bool isNumber = true; + for (uint32 i = 0; i < strlen(origStr); i++) { + if (!(origStr[i] >= '0' && origStr[i] <= '9')) { + isNumber = false; + break; + } + } + + if (isNumber) { + int index = atoi(origStr); + sprintf(outStr, "%d", index); + return true; + } else return false; +} + +////////////////////////////////////////////////////////////////////////// +bool CSXArray::push(CScValue *val) { + char paramName[20]; + _length++; + sprintf(paramName, "%d", _length - 1); + _values->setProp(paramName, val, true); + return STATUS_OK; +} + +} // end of namespace WinterMute diff --git a/engines/wintermute/base/scriptables/SXArray.h b/engines/wintermute/base/scriptables/SXArray.h new file mode 100644 index 0000000000..0f46bd546e --- /dev/null +++ b/engines/wintermute/base/scriptables/SXArray.h @@ -0,0 +1,54 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +/* + * This file is based on WME Lite. + * http://dead-code.org/redir.php?target=wmelite + * Copyright (c) 2011 Jan Nedoma + */ + +#ifndef WINTERMUTE_SXARRAY_H +#define WINTERMUTE_SXARRAY_H + +#include "engines/wintermute/base/BScriptable.h" + +namespace WinterMute { + +class CSXArray : public CBScriptable { +public: + bool push(CScValue *Val); + bool validNumber(const char *origStr, char *outStr); + DECLARE_PERSISTENT(CSXArray, CBScriptable) + CSXArray(CBGame *inGame, CScStack *stack); + CSXArray(CBGame *inGame); + virtual ~CSXArray(); + CScValue *scGetProperty(const char *name); + bool scSetProperty(const char *name, CScValue *value); + bool scCallMethod(CScScript *script, CScStack *stack, CScStack *thisStack, const char *name); + const char *scToString(); + int _length; + CScValue *_values; +}; + +} // end of namespace WinterMute + +#endif diff --git a/engines/wintermute/base/scriptables/SXDate.cpp b/engines/wintermute/base/scriptables/SXDate.cpp new file mode 100644 index 0000000000..cd705cc9d4 --- /dev/null +++ b/engines/wintermute/base/scriptables/SXDate.cpp @@ -0,0 +1,297 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +/* + * This file is based on WME Lite. + * http://dead-code.org/redir.php?target=wmelite + * Copyright (c) 2011 Jan Nedoma + */ + +#include "engines/wintermute/base/scriptables/ScStack.h" +#include "engines/wintermute/base/scriptables/ScValue.h" +#include "engines/wintermute/base/scriptables/SXDate.h" + +namespace WinterMute { + +IMPLEMENT_PERSISTENT(CSXDate, false) + +CBScriptable *makeSXDate(CBGame *inGame, CScStack *stack) { + return new CSXDate(inGame, stack); +} + +////////////////////////////////////////////////////////////////////////// +CSXDate::CSXDate(CBGame *inGame, CScStack *stack): CBScriptable(inGame) { + stack->correctParams(6); + + memset(&_tm, 0, sizeof(_tm)); + + CScValue *valYear = stack->pop(); + _tm.tm_year = valYear->getInt() - 1900; + _tm.tm_mon = stack->pop()->getInt() - 1; + _tm.tm_mday = stack->pop()->getInt(); + _tm.tm_hour = stack->pop()->getInt(); + _tm.tm_min = stack->pop()->getInt(); + _tm.tm_sec = stack->pop()->getInt(); + + if (valYear->isNULL()) { + g_system->getTimeAndDate(_tm); + } +} + + +////////////////////////////////////////////////////////////////////////// +CSXDate::~CSXDate() { + +} + +////////////////////////////////////////////////////////////////////////// +const char *CSXDate::scToString() { + // TODO: Make this more stringy, and less ISO 8601-like + _strRep.format("%04d-%02d-%02d - %02d:%02d:%02d", _tm.tm_year, _tm.tm_mon, _tm.tm_mday, _tm.tm_hour, _tm.tm_min, _tm.tm_sec); + return _strRep.c_str(); +#if 0 + return asctime(&_tm); +#endif +} + + +////////////////////////////////////////////////////////////////////////// +bool CSXDate::scCallMethod(CScScript *script, CScStack *stack, CScStack *thisStack, const char *name) { + ////////////////////////////////////////////////////////////////////////// + // GetYear + ////////////////////////////////////////////////////////////////////////// + if (strcmp(name, "GetYear") == 0) { + stack->correctParams(0); + stack->pushInt(_tm.tm_year + 1900); + return STATUS_OK; + } + ////////////////////////////////////////////////////////////////////////// + // GetMonth + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "GetMonth") == 0) { + stack->correctParams(0); + stack->pushInt(_tm.tm_mon + 1); + return STATUS_OK; + } + ////////////////////////////////////////////////////////////////////////// + // GetDate + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "GetDate") == 0) { + stack->correctParams(0); + stack->pushInt(_tm.tm_mday); + return STATUS_OK; + } + ////////////////////////////////////////////////////////////////////////// + // GetHours + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "GetHours") == 0) { + stack->correctParams(0); + stack->pushInt(_tm.tm_hour); + return STATUS_OK; + } + ////////////////////////////////////////////////////////////////////////// + // GetMinutes + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "GetMinutes") == 0) { + stack->correctParams(0); + stack->pushInt(_tm.tm_min); + return STATUS_OK; + } + ////////////////////////////////////////////////////////////////////////// + // GetSeconds + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "GetSeconds") == 0) { + stack->correctParams(0); + stack->pushInt(_tm.tm_sec); + return STATUS_OK; + } + ////////////////////////////////////////////////////////////////////////// + // GetWeekday + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "GetWeekday") == 0) { + stack->correctParams(0); + warning("GetWeekday returns a wrong value on purpose"); + stack->pushInt(_tm.tm_mday % 7); + return STATUS_OK; + } + + + ////////////////////////////////////////////////////////////////////////// + // SetYear + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "SetYear") == 0) { + stack->correctParams(1); + _tm.tm_year = stack->pop()->getInt() - 1900; + stack->pushNULL(); + return STATUS_OK; + } + ////////////////////////////////////////////////////////////////////////// + // SetMonth + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "SetMonth") == 0) { + stack->correctParams(1); + _tm.tm_mon = stack->pop()->getInt() - 1; + stack->pushNULL(); + return STATUS_OK; + } + ////////////////////////////////////////////////////////////////////////// + // SetDate + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "SetDate") == 0) { + stack->correctParams(1); + _tm.tm_mday = stack->pop()->getInt(); + stack->pushNULL(); + return STATUS_OK; + } + ////////////////////////////////////////////////////////////////////////// + // SetHours + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "SetHours") == 0) { + stack->correctParams(1); + _tm.tm_hour = stack->pop()->getInt(); + stack->pushNULL(); + return STATUS_OK; + } + ////////////////////////////////////////////////////////////////////////// + // SetMinutes + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "SetMinutes") == 0) { + stack->correctParams(1); + _tm.tm_min = stack->pop()->getInt(); + stack->pushNULL(); + return STATUS_OK; + } + ////////////////////////////////////////////////////////////////////////// + // SetSeconds + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "SetSeconds") == 0) { + stack->correctParams(1); + _tm.tm_sec = stack->pop()->getInt(); + stack->pushNULL(); + return STATUS_OK; + } + + + ////////////////////////////////////////////////////////////////////////// + // SetCurrentTime + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "SetCurrentTime") == 0) { + stack->correctParams(0); + g_system->getTimeAndDate(_tm); + stack->pushNULL(); + return STATUS_OK; + } + + else + return STATUS_FAILED; +} + + +////////////////////////////////////////////////////////////////////////// +CScValue *CSXDate::scGetProperty(const char *name) { + _scValue->setNULL(); + + ////////////////////////////////////////////////////////////////////////// + // Type + ////////////////////////////////////////////////////////////////////////// + if (strcmp(name, "Type") == 0) { + _scValue->setString("date"); + return _scValue; + } + + else return _scValue; +} + + +////////////////////////////////////////////////////////////////////////// +bool CSXDate::scSetProperty(const char *name, CScValue *value) { + /* + ////////////////////////////////////////////////////////////////////////// + // Name + ////////////////////////////////////////////////////////////////////////// + if(strcmp(name, "Name")==0){ + setName(value->getString()); + return STATUS_OK; + } + + else*/ return STATUS_FAILED; +} + + +////////////////////////////////////////////////////////////////////////// +bool CSXDate::persist(CBPersistMgr *persistMgr) { + + CBScriptable::persist(persistMgr); + persistMgr->transfer(TMEMBER(_tm.tm_year)); + persistMgr->transfer(TMEMBER(_tm.tm_mon)); + persistMgr->transfer(TMEMBER(_tm.tm_mday)); + persistMgr->transfer(TMEMBER(_tm.tm_hour)); + persistMgr->transfer(TMEMBER(_tm.tm_min)); + persistMgr->transfer(TMEMBER(_tm.tm_sec)); + return STATUS_OK; +} + +////////////////////////////////////////////////////////////////////////// +int CSXDate::scCompare(CBScriptable *Value) { + TimeDate time1 = _tm; + TimeDate time2 = ((CSXDate *)Value)->_tm; + + if (time1.tm_year < time2.tm_year) { + return -1; + } else if (time1.tm_year == time2.tm_year) { + if (time1.tm_mon < time2.tm_mon) { + return -1; + } else if (time1.tm_mon == time2.tm_mon) { + if (time1.tm_mday < time2.tm_mday) { + return -1; + } else if (time1.tm_mday == time2.tm_mday) { + if (time1.tm_hour < time2.tm_hour) { + return -1; + } else if (time1.tm_hour == time2.tm_hour) { + if (time1.tm_min < time2.tm_min) { + return -1; + } else if (time1.tm_min == time2.tm_min) { + if (time1.tm_sec < time2.tm_sec) { + return -1; + } else if (time1.tm_sec == time2.tm_sec) { + return 0; // Equal + } else { + return 1; // Sec + } + } else { + return 1; // Minute + } + } else { + return 1; // Hour + } + } else { + return 1; // Day + } + } else { + return 1; // Month + } + } else { + return 1; // Year + } +} + +} // end of namespace WinterMute diff --git a/engines/wintermute/base/scriptables/SXDate.h b/engines/wintermute/base/scriptables/SXDate.h new file mode 100644 index 0000000000..df0641983f --- /dev/null +++ b/engines/wintermute/base/scriptables/SXDate.h @@ -0,0 +1,55 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +/* + * This file is based on WME Lite. + * http://dead-code.org/redir.php?target=wmelite + * Copyright (c) 2011 Jan Nedoma + */ + +#ifndef WINTERMUTE_SXDATE_H +#define WINTERMUTE_SXDATE_H + +#include "common/system.h" +#include "engines/wintermute/base/BScriptable.h" + +namespace WinterMute { + +class CSXDate : public CBScriptable { +public: + int scCompare(CBScriptable *Value); + DECLARE_PERSISTENT(CSXDate, CBScriptable) + CSXDate(CBGame *inGame, CScStack *Stack); + virtual ~CSXDate(); + CScValue *scGetProperty(const char *name); + bool scSetProperty(const char *name, CScValue *value); + bool scCallMethod(CScScript *script, CScStack *stack, CScStack *thisStack, const char *name); + const char *scToString(); + char *_string; + TimeDate _tm; +private: + Common::String _strRep; +}; + +} // end of namespace WinterMute + +#endif diff --git a/engines/wintermute/base/scriptables/SXFile.cpp b/engines/wintermute/base/scriptables/SXFile.cpp new file mode 100644 index 0000000000..b2a6d24677 --- /dev/null +++ b/engines/wintermute/base/scriptables/SXFile.cpp @@ -0,0 +1,779 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +/* + * This file is based on WME Lite. + * http://dead-code.org/redir.php?target=wmelite + * Copyright (c) 2011 Jan Nedoma + */ + +#include "engines/wintermute/system/SysClassRegistry.h" +#include "engines/wintermute/system/SysClass.h" +#include "engines/wintermute/base/scriptables/ScStack.h" +#include "engines/wintermute/base/scriptables/ScValue.h" +#include "engines/wintermute/base/scriptables/ScScript.h" +#include "engines/wintermute/utils/utils.h" +#include "engines/wintermute/base/BGame.h" +#include "engines/wintermute/base/file/BFile.h" +#include "engines/wintermute/base/BFileManager.h" +#include "engines/wintermute/PlatformSDL.h" +#include "engines/wintermute/base/scriptables/SXFile.h" + +// Note: This code is completely untested, as I have yet to find a game that uses SXFile. + +namespace WinterMute { + +IMPLEMENT_PERSISTENT(CSXFile, false) + +CBScriptable *makeSXFile(CBGame *inGame, CScStack *stack) { + return new CSXFile(inGame, stack); +} + +////////////////////////////////////////////////////////////////////////// +CSXFile::CSXFile(CBGame *inGame, CScStack *stack): CBScriptable(inGame) { + stack->correctParams(1); + CScValue *Val = stack->pop(); + + _filename = NULL; + if (!Val->isNULL()) CBUtils::setString(&_filename, Val->getString()); + + _readFile = NULL; + _writeFile = NULL; + + _mode = 0; + _textMode = false; +} + + +////////////////////////////////////////////////////////////////////////// +CSXFile::~CSXFile() { + cleanup(); +} + +////////////////////////////////////////////////////////////////////////// +void CSXFile::cleanup() { + delete[] _filename; + _filename = NULL; + close(); +} + + +////////////////////////////////////////////////////////////////////////// +void CSXFile::close() { + if (_readFile) { + _gameRef->_fileManager->closeFile(_readFile); + _readFile = NULL; + } + if (_writeFile) { + _writeFile->finalize(); + delete _writeFile; + _writeFile = NULL; + } + _mode = 0; + _textMode = false; +} + +////////////////////////////////////////////////////////////////////////// +const char *CSXFile::scToString() { + if (_filename) return _filename; + else return "[file object]"; +} + +#define FILE_BUFFER_SIZE 32768 +////////////////////////////////////////////////////////////////////////// +bool CSXFile::scCallMethod(CScScript *script, CScStack *stack, CScStack *thisStack, const char *name) { + ////////////////////////////////////////////////////////////////////////// + // SetFilename + ////////////////////////////////////////////////////////////////////////// + if (strcmp(name, "SetFilename") == 0) { + stack->correctParams(1); + const char *filename = stack->pop()->getString(); + cleanup(); + CBUtils::setString(&_filename, filename); + stack->pushNULL(); + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // OpenAsText / OpenAsBinary + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "OpenAsText") == 0 || strcmp(name, "OpenAsBinary") == 0) { + stack->correctParams(1); + close(); + _mode = stack->pop()->getInt(1); + if (_mode < 1 || _mode > 3) { + script->runtimeError("File.%s: invalid access mode. Setting read mode.", name); + _mode = 1; + } + if (_mode == 1) { + _readFile = _gameRef->_fileManager->openFile(_filename); + if (!_readFile) { + //script->runtimeError("File.%s: Error opening file '%s' for reading.", Name, _filename); + close(); + } else _textMode = strcmp(name, "OpenAsText") == 0; + } else { + if (strcmp(name, "OpenAsText") == 0) { + if (_mode == 2) _writeFile = openForWrite(_filename, false); + else _writeFile = openForAppend(_filename, false); + } else { + if (_mode == 2) _writeFile = openForWrite(_filename, true); + else _writeFile = openForAppend(_filename, true); + } + + if (!_writeFile) { + //script->runtimeError("File.%s: Error opening file '%s' for writing.", Name, _filename); + close(); + } else _textMode = strcmp(name, "OpenAsText") == 0; + } + + if (_readFile || _writeFile) stack->pushBool(true); + else stack->pushBool(false); + + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // Close + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "Close") == 0) { + stack->correctParams(0); + close(); + stack->pushNULL(); + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // SetPosition + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "SetPosition") == 0) { + stack->correctParams(1); + if (_mode == 0) { + script->runtimeError("File.%s: File is not open", name); + stack->pushBool(false); + } else { + int Pos = stack->pop()->getInt(); + stack->pushBool(setPos(Pos)); + } + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // Delete + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "Delete") == 0) { + stack->correctParams(0); + close(); + stack->pushBool(CBPlatform::deleteFile(_filename) != false); + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // Copy + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "Copy") == 0) { + stack->correctParams(2); + const char *Dest = stack->pop()->getString(); + bool Overwrite = stack->pop()->getBool(true); + + close(); + stack->pushBool(CBPlatform::copyFile(_filename, Dest, !Overwrite) != false); + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // ReadLine + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "ReadLine") == 0) { + stack->correctParams(0); + if (!_textMode || !_readFile) { + script->runtimeError("File.%s: File must be open in text mode.", name); + stack->pushNULL(); + return STATUS_OK; + } + uint32 bufSize = FILE_BUFFER_SIZE; + byte *buf = (byte *)malloc(bufSize); + uint32 counter = 0; + byte b; + bool foundNewLine = false; + bool ret = STATUS_FAILED; + do { + ret = _readFile->read(&b, 1); + if (ret != 1) break; + + if (counter > bufSize) { + buf = (byte *)realloc(buf, bufSize + FILE_BUFFER_SIZE); + bufSize += FILE_BUFFER_SIZE; + } + if (b == '\n') { + buf[counter] = '\0'; + foundNewLine = true; + break; + } else if (b == 0x0D) continue; + else { + buf[counter] = b; + counter++; + } + } while (DID_SUCCEED(ret)); + + if (counter > bufSize) { + buf = (byte *)realloc(buf, bufSize + FILE_BUFFER_SIZE); + bufSize += FILE_BUFFER_SIZE; + } + buf[counter] = '\0'; + + if (!foundNewLine && counter == 0) stack->pushNULL(); + else stack->pushString((char *)buf); + + free(buf); + + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // ReadText + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "ReadText") == 0) { + stack->correctParams(1); + int textLen = stack->pop()->getInt(); + + if (!_textMode || !_readFile) { + script->runtimeError("File.%s: File must be open in text mode.", name); + stack->pushNULL(); + return STATUS_OK; + } + uint32 bufSize = FILE_BUFFER_SIZE; + byte *buf = (byte *)malloc(bufSize); + uint32 counter = 0; + byte b; + + bool ret = STATUS_FAILED; + while (counter < (uint32)textLen) { + ret = _readFile->read(&b, 1); + if (ret != 1) break; + + if (counter > bufSize) { + buf = (byte *)realloc(buf, bufSize + FILE_BUFFER_SIZE); + bufSize += FILE_BUFFER_SIZE; + } + if (b == 0x0D) continue; + else { + buf[counter] = b; + counter++; + } + } + + if (counter > bufSize) { + buf = (byte *)realloc(buf, bufSize + FILE_BUFFER_SIZE); + bufSize += FILE_BUFFER_SIZE; + } + buf[counter] = '\0'; + + if (textLen > 0 && counter == 0) stack->pushNULL(); + else stack->pushString((char *)buf); + + free(buf); + + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // WriteLine / WriteText + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "WriteLine") == 0 || strcmp(name, "WriteText") == 0) { + stack->correctParams(1); + const char *line = stack->pop()->getString(); + if (!_textMode || !_writeFile) { + script->runtimeError("File.%s: File must be open for writing in text mode.", name); + stack->pushBool(false); + return STATUS_OK; + } + Common::String writeLine; + if (strcmp(name, "WriteLine") == 0) { + writeLine = Common::String::format("%s\n", line); + } else { + writeLine = Common::String::format("%s", line); + } + _writeFile->writeString(writeLine); + _writeFile->writeByte(0); + stack->pushBool(true); + + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + ////////////////////////////////////////////////////////////////////////// + // ReadBool + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "ReadBool") == 0) { + stack->correctParams(0); + if (_textMode || !_readFile) { + script->runtimeError("File.%s: File must be open for reading in binary mode.", name); + stack->pushNULL(); + return STATUS_OK; + } + bool val; + if (_readFile->read(&val, sizeof(bool)) == sizeof(bool)) stack->pushBool(val); + else stack->pushNULL(); + + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // ReadByte + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "ReadByte") == 0) { + stack->correctParams(0); + if (_textMode || !_readFile) { + script->runtimeError("File.%s: File must be open for reading in binary mode.", name); + stack->pushNULL(); + return STATUS_OK; + } + byte val = _readFile->readByte(); + if (!_readFile->err()) { + stack->pushInt(val); + } else { + stack->pushNULL(); + } + + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // ReadShort + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "ReadShort") == 0) { + stack->correctParams(0); + if (_textMode || !_readFile) { + script->runtimeError("File.%s: File must be open for reading in binary mode.", name); + stack->pushNULL(); + return STATUS_OK; + } + int16 val = _readFile->readSint16LE(); + if (!_readFile->err()) { + stack->pushInt(65536 + val); + } else { + stack->pushNULL(); + } + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // ReadInt / ReadLong + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "ReadInt") == 0 || strcmp(name, "ReadLong") == 0) { + stack->correctParams(0); + if (_textMode || !_readFile) { + script->runtimeError("File.%s: File must be open for reading in binary mode.", name); + stack->pushNULL(); + return STATUS_OK; + } + int32 val = _readFile->readSint32LE(); + if (!_readFile->err()) { + stack->pushInt(val); + } else { + stack->pushNULL(); + } + + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // ReadFloat + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "ReadFloat") == 0) { + stack->correctParams(0); + if (_textMode || !_readFile) { + script->runtimeError("File.%s: File must be open for reading in binary mode.", name); + stack->pushNULL(); + return STATUS_OK; + } + float val; + (*(uint32*)&val) = _readFile->readUint32LE(); + if (!_readFile->err()) { + stack->pushFloat(val); + } else { + stack->pushNULL(); + } + + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // ReadDouble + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "ReadDouble") == 0) { // TODO: Solve reading a 8 byte double. + error("SXFile::ReadDouble - Not endian safe yet"); + stack->correctParams(0); + if (_textMode || !_readFile) { + script->runtimeError("File.%s: File must be open for reading in binary mode.", name); + stack->pushNULL(); + return STATUS_OK; + } + double val; + if (_readFile->read(&val, sizeof(double)) == sizeof(double)) stack->pushFloat(val); + else stack->pushNULL(); + + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // ReadString + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "ReadString") == 0) { + stack->correctParams(0); + if (_textMode || !_readFile) { + script->runtimeError("File.%s: File must be open for reading in binary mode.", name); + stack->pushNULL(); + return STATUS_OK; + } + uint32 size = _readFile->readUint32LE(); + if (!_readFile->err()) { + byte *str = new byte[size + 1]; + if (str) { + if (_readFile->read(str, size) == size) { + str[size] = '\0'; + stack->pushString((char *)str); + } + delete [] str; + } else stack->pushNULL(); + } else stack->pushNULL(); + + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // WriteBool + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "WriteBool") == 0) { + stack->correctParams(1); + bool val = stack->pop()->getBool(); + + if (_textMode || !_writeFile) { + script->runtimeError("File.%s: File must be open for writing in binary mode.", name); + stack->pushBool(false); + return STATUS_OK; + } + _writeFile->writeByte(val); + stack->pushBool(true); + + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // WriteByte + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "WriteByte") == 0) { + stack->correctParams(1); + byte val = stack->pop()->getInt(); + + if (_textMode || !_writeFile) { + script->runtimeError("File.%s: File must be open for writing in binary mode.", name); + stack->pushBool(false); + return STATUS_OK; + } + _writeFile->writeByte(val); + stack->pushBool(true); + + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // WriteShort + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "WriteShort") == 0) { + stack->correctParams(1); + int16 val = stack->pop()->getInt(); + + if (_textMode || !_writeFile) { + script->runtimeError("File.%s: File must be open for writing in binary mode.", name); + stack->pushBool(false); + return STATUS_OK; + } + _writeFile->writeSint16LE(val); + stack->pushBool(true); + + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // WriteInt / WriteLong + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "WriteInt") == 0 || strcmp(name, "WriteLong") == 0) { + stack->correctParams(1); + int32 val = stack->pop()->getInt(); + + if (_textMode || !_writeFile) { + script->runtimeError("File.%s: File must be open for writing in binary mode.", name); + stack->pushBool(false); + return STATUS_OK; + } + _writeFile->writeSint32LE(val); + stack->pushBool(true); + + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // WriteFloat + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "WriteFloat") == 0) { + stack->correctParams(1); + float val = stack->pop()->getFloat(); + + if (_textMode || !_writeFile) { + script->runtimeError("File.%s: File must be open for writing in binary mode.", name); + stack->pushBool(false); + return STATUS_OK; + } + uint32 *ptr = (uint32*)&val; + _writeFile->writeUint32LE(*ptr); + stack->pushBool(true); + + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // WriteDouble + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "WriteDouble") == 0) { + error("SXFile::WriteDouble - Not endian safe yet"); + stack->correctParams(1); + double val = stack->pop()->getFloat(); + + if (_textMode || !_writeFile) { + script->runtimeError("File.%s: File must be open for writing in binary mode.", name); + stack->pushBool(false); + return STATUS_OK; + } + //fwrite(&val, sizeof(val), 1, (FILE *)_writeFile); + stack->pushBool(true); + + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // WriteString + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "WriteString") == 0) { + stack->correctParams(1); + const char *val = stack->pop()->getString(); + + if (_textMode || !_writeFile) { + script->runtimeError("File.%s: File must be open for writing in binary mode.", name); + stack->pushBool(false); + return STATUS_OK; + } + + uint32 size = strlen(val); + _writeFile->writeUint32LE(size); + _writeFile->writeString(val); + + stack->pushBool(true); + + return STATUS_OK; + } + + + else return CBScriptable::scCallMethod(script, stack, thisStack, name); +} + + +////////////////////////////////////////////////////////////////////////// +CScValue *CSXFile::scGetProperty(const char *name) { + _scValue->setNULL(); + + ////////////////////////////////////////////////////////////////////////// + // Type (RO) + ////////////////////////////////////////////////////////////////////////// + if (strcmp(name, "Type") == 0) { + _scValue->setString("file"); + return _scValue; + } + + ////////////////////////////////////////////////////////////////////////// + // Filename (RO) + ////////////////////////////////////////////////////////////////////////// + if (strcmp(name, "Filename") == 0) { + _scValue->setString(_filename); + return _scValue; + } + + ////////////////////////////////////////////////////////////////////////// + // Position (RO) + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "Position") == 0) { + _scValue->setInt(getPos()); + return _scValue; + } + + ////////////////////////////////////////////////////////////////////////// + // Length (RO) + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "Length") == 0) { + _scValue->setInt(getLength()); + return _scValue; + } + + ////////////////////////////////////////////////////////////////////////// + // TextMode (RO) + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "TextMode") == 0) { + _scValue->setBool(_textMode); + return _scValue; + } + + ////////////////////////////////////////////////////////////////////////// + // AccessMode (RO) + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "AccessMode") == 0) { + _scValue->setInt(_mode); + return _scValue; + } + + else return CBScriptable::scGetProperty(name); +} + + +////////////////////////////////////////////////////////////////////////// +bool CSXFile::scSetProperty(const char *name, CScValue *value) { + /* + ////////////////////////////////////////////////////////////////////////// + // Length + ////////////////////////////////////////////////////////////////////////// + if(strcmp(name, "Length")==0){ + int OrigLength = _length; + _length = max(value->getInt(0), 0); + + char PropName[20]; + if(_length < OrigLength){ + for(int i=_length; iDeleteProp(PropName); + } + } + return STATUS_OK; + } + else*/ return CBScriptable::scSetProperty(name, value); +} + +////////////////////////////////////////////////////////////////////////// +uint32 CSXFile::getPos() { + if (_mode == 1 && _readFile) + return _readFile->pos(); + else if ((_mode == 2 || _mode == 3) && _writeFile) { + error("SXFile - getPos for WriteFile not supported"); + return 0; +// return ftell((FILE *)_writeFile); + } else { + return 0; + } +} + +////////////////////////////////////////////////////////////////////////// +bool CSXFile::setPos(uint32 pos, int whence) { + if (_mode == 1 && _readFile) + return _readFile->seek(pos, whence); + else if ((_mode == 2 || _mode == 3) && _writeFile) { + error("CSXFile - seeking in WriteFile not supported"); + return false; +// return fseek((FILE *)_writeFile, pos, (int)origin) == 0; + } + else return false; +} + +////////////////////////////////////////////////////////////////////////// +uint32 CSXFile::getLength() { + if (_mode == 1 && _readFile) + return _readFile->size(); + else if ((_mode == 2 || _mode == 3) && _writeFile) { + error("CSXFile - reading length for WriteFile not supported"); + return 0; +/* + uint32 currentPos = ftell((FILE *)_writeFile); + fseek((FILE *)_writeFile, 0, SEEK_END); + int ret = ftell((FILE *)_writeFile); + fseek((FILE *)_writeFile, CurrentPos, SEEK_SET); + return Ret;*/ + } else return 0; +} + +////////////////////////////////////////////////////////////////////////// +bool CSXFile::persist(CBPersistMgr *persistMgr) { + + CBScriptable::persist(persistMgr); + + persistMgr->transfer(TMEMBER(_filename)); + persistMgr->transfer(TMEMBER(_mode)); + persistMgr->transfer(TMEMBER(_textMode)); + + uint32 pos = 0; + if (persistMgr->_saving) { + pos = getPos(); + persistMgr->transfer(TMEMBER(pos)); + } else { + persistMgr->transfer(TMEMBER(pos)); + + // try to re-open file if needed + _writeFile = NULL; + _readFile = NULL; + + if (_mode != 0) { + // open for reading + if (_mode == 1) { + _readFile = _gameRef->_fileManager->openFile(_filename); + if (!_readFile) + close(); + } + // open for writing / appending + else { + if (_textMode) { + if (_mode == 2) + _writeFile = openForWrite(_filename, false); + else + _writeFile = openForAppend(_filename, false); + } else { + if (_mode == 2) + _writeFile = openForWrite(_filename, true); + else + _writeFile = openForAppend(_filename, true); + } + if (_writeFile) + close(); + } + setPos(pos); + } + } + + return STATUS_OK; +} + +// Should replace fopen(..., "wb+") and fopen(..., "w+") +Common::WriteStream *CSXFile::openForWrite(const Common::String &filename, bool binary) { + error("SXFile::openForWrite - WriteFiles not supported"); +} + +// Should replace fopen(..., "ab+") and fopen(..., "a+") +Common::WriteStream *CSXFile::openForAppend(const Common::String &filename, bool binary) { + error("SXFile::openForAppend - WriteFiles not supported"); +} + +} // end of namespace WinterMute diff --git a/engines/wintermute/base/scriptables/SXFile.h b/engines/wintermute/base/scriptables/SXFile.h new file mode 100644 index 0000000000..709d1f4378 --- /dev/null +++ b/engines/wintermute/base/scriptables/SXFile.h @@ -0,0 +1,66 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +/* + * This file is based on WME Lite. + * http://dead-code.org/redir.php?target=wmelite + * Copyright (c) 2011 Jan Nedoma + */ + +#ifndef WINTERMUTES_SXFILE_H +#define WINTERMUTES_SXFILE_H + + +#include "engines/wintermute/base/BScriptable.h" +#include "common/stream.h" + +namespace WinterMute { + +class CBFile; + +class CSXFile : public CBScriptable { +public: + DECLARE_PERSISTENT(CSXFile, CBScriptable) + CScValue *scGetProperty(const char *name); + bool scSetProperty(const char *name, CScValue *value); + bool scCallMethod(CScScript *script, CScStack *stack, CScStack *thisStack, const char *name); + const char *scToString(); + CSXFile(CBGame *inGame, CScStack *Stack); + virtual ~CSXFile(); +private: + Common::SeekableReadStream *_readFile; + Common::WriteStream *_writeFile; + int _mode; // 0..none, 1..read, 2..write, 3..append + bool _textMode; + void close(); + void cleanup(); + uint32 getPos(); + uint32 getLength(); + bool setPos(uint32 Pos, int whence = SEEK_SET); + char *_filename; + Common::WriteStream *openForWrite(const Common::String &filename, bool binary); + Common::WriteStream *openForAppend(const Common::String &filename, bool binary); +}; + +} // end of namespace WinterMute + +#endif diff --git a/engines/wintermute/base/scriptables/SXMath.cpp b/engines/wintermute/base/scriptables/SXMath.cpp new file mode 100644 index 0000000000..fb2838ee94 --- /dev/null +++ b/engines/wintermute/base/scriptables/SXMath.cpp @@ -0,0 +1,295 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +/* + * This file is based on WME Lite. + * http://dead-code.org/redir.php?target=wmelite + * Copyright (c) 2011 Jan Nedoma + */ + +#include "engines/wintermute/base/scriptables/SXMath.h" +#include "engines/wintermute/base/scriptables/ScStack.h" +#include "engines/wintermute/base/scriptables/ScValue.h" +#include "engines/wintermute/persistent.h" +#include "common/math.h" +#include + +namespace WinterMute { + +////////////////////////////////////////////////////////////////////// +// Construction/Destruction +////////////////////////////////////////////////////////////////////// + + +IMPLEMENT_PERSISTENT(CSXMath, true) + +CBScriptable *makeSXMath(CBGame *inGame) { + return new CSXMath(inGame); +} + +////////////////////////////////////////////////////////////////////////// +CSXMath::CSXMath(CBGame *inGame): CBScriptable(inGame) { + +} + + +////////////////////////////////////////////////////////////////////////// +CSXMath::~CSXMath() { + +} + + +////////////////////////////////////////////////////////////////////////// +bool CSXMath::scCallMethod(CScScript *script, CScStack *stack, CScStack *thisStack, const char *name) { + ////////////////////////////////////////////////////////////////////////// + // Abs + ////////////////////////////////////////////////////////////////////////// + if (strcmp(name, "Abs") == 0) { + stack->correctParams(1); + stack->pushFloat(fabs(stack->pop()->getFloat())); + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // Acos + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "Acos") == 0) { + stack->correctParams(1); + stack->pushFloat(acos(stack->pop()->getFloat())); + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // Asin + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "Asin") == 0) { + stack->correctParams(1); + stack->pushFloat(asin(stack->pop()->getFloat())); + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // Atan + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "Atan") == 0) { + stack->correctParams(1); + stack->pushFloat(atan(stack->pop()->getFloat())); + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // Atan2 + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "Atan2") == 0) { + stack->correctParams(2); + double y = stack->pop()->getFloat(); + double x = stack->pop()->getFloat(); + stack->pushFloat(atan2(y, x)); + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // Ceil + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "Ceil") == 0) { + stack->correctParams(1); + stack->pushFloat(ceil(stack->pop()->getFloat())); + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // Cos + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "Cos") == 0) { + stack->correctParams(1); + stack->pushFloat(cos(degreeToRadian(stack->pop()->getFloat()))); + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // Cosh + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "Cosh") == 0) { + stack->correctParams(1); + stack->pushFloat(cosh(degreeToRadian(stack->pop()->getFloat()))); + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // Exp + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "Exp") == 0) { + stack->correctParams(1); + stack->pushFloat(exp(stack->pop()->getFloat())); + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // Floor + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "Floor") == 0) { + stack->correctParams(1); + stack->pushFloat(floor(stack->pop()->getFloat())); + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // Log + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "Log") == 0) { + stack->correctParams(1); + stack->pushFloat(log(stack->pop()->getFloat())); + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // Log10 + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "Log10") == 0) { + stack->correctParams(1); + stack->pushFloat(log10(stack->pop()->getFloat())); + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // Pow + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "Pow") == 0) { + stack->correctParams(2); + double x = stack->pop()->getFloat(); + double y = stack->pop()->getFloat(); + + stack->pushFloat(pow(x, y)); + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // Sin + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "Sin") == 0) { + stack->correctParams(1); + stack->pushFloat(sin(degreeToRadian(stack->pop()->getFloat()))); + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // Sinh + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "Sinh") == 0) { + stack->correctParams(1); + stack->pushFloat(sinh(degreeToRadian(stack->pop()->getFloat()))); + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // Tan + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "Tan") == 0) { + stack->correctParams(1); + stack->pushFloat(tan(degreeToRadian(stack->pop()->getFloat()))); + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // Tanh + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "Tanh") == 0) { + stack->correctParams(1); + stack->pushFloat(tanh(degreeToRadian(stack->pop()->getFloat()))); + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // Sqrt + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "Sqrt") == 0) { + stack->correctParams(1); + stack->pushFloat(sqrt(stack->pop()->getFloat())); + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // DegToRad + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "DegToRad") == 0) { + stack->correctParams(1); + stack->pushFloat(degreeToRadian(stack->pop()->getFloat())); + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // RadToDeg + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "RadToDeg") == 0) { + stack->correctParams(1); + stack->pushFloat(radianToDegree(stack->pop()->getFloat())); + return STATUS_OK; + } + + else return STATUS_FAILED; +} + + +////////////////////////////////////////////////////////////////////////// +CScValue *CSXMath::scGetProperty(const char *name) { + _scValue->setNULL(); + + ////////////////////////////////////////////////////////////////////////// + // Type + ////////////////////////////////////////////////////////////////////////// + if (strcmp(name, "Type") == 0) { + _scValue->setString("math"); + return _scValue; + } + + ////////////////////////////////////////////////////////////////////////// + // PI + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "PI") == 0) { + _scValue->setFloat(M_PI); + return _scValue; + } + + else return _scValue; +} + + +////////////////////////////////////////////////////////////////////////// +double CSXMath::degreeToRadian(double value) { + return value * (M_PI / 180.0f); +} + + +////////////////////////////////////////////////////////////////////////// +double CSXMath::radianToDegree(double value) { + return value * (180.0f / M_PI); +} + + +////////////////////////////////////////////////////////////////////////// +bool CSXMath::persist(CBPersistMgr *persistMgr) { + + CBScriptable::persist(persistMgr); + return STATUS_OK; +} + +} // end of namespace WinterMute diff --git a/engines/wintermute/base/scriptables/SXMath.h b/engines/wintermute/base/scriptables/SXMath.h new file mode 100644 index 0000000000..4389de611f --- /dev/null +++ b/engines/wintermute/base/scriptables/SXMath.h @@ -0,0 +1,53 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +/* + * This file is based on WME Lite. + * http://dead-code.org/redir.php?target=wmelite + * Copyright (c) 2011 Jan Nedoma + */ + +#ifndef WINTERMUTE_SXMATH_H +#define WINTERMUTE_SXMATH_H + + +#include "engines/wintermute/base/BScriptable.h" + +namespace WinterMute { + +class CSXMath : public CBScriptable { +public: + DECLARE_PERSISTENT(CSXMath, CBScriptable) + CSXMath(CBGame *inGame); + virtual ~CSXMath(); + virtual CScValue *scGetProperty(const char *name); + virtual bool scCallMethod(CScScript *script, CScStack *stack, CScStack *thisStack, const char *name); + +private: + double degreeToRadian(double value); + double radianToDegree(double value); + +}; + +} // end of namespace WinterMute + +#endif diff --git a/engines/wintermute/base/scriptables/SXMemBuffer.cpp b/engines/wintermute/base/scriptables/SXMemBuffer.cpp new file mode 100644 index 0000000000..9ac98ab11d --- /dev/null +++ b/engines/wintermute/base/scriptables/SXMemBuffer.cpp @@ -0,0 +1,508 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +/* + * This file is based on WME Lite. + * http://dead-code.org/redir.php?target=wmelite + * Copyright (c) 2011 Jan Nedoma + */ + +#include "engines/wintermute/base/BScriptable.h" +#include "engines/wintermute/base/scriptables/ScStack.h" +#include "engines/wintermute/base/scriptables/ScScript.h" +#include "engines/wintermute/base/scriptables/ScValue.h" +#include "engines/wintermute/base/scriptables/SXMemBuffer.h" +#include "common/file.h" + +namespace WinterMute { + +IMPLEMENT_PERSISTENT(CSXMemBuffer, false) + +CBScriptable *makeSXMemBuffer(CBGame *inGame, CScStack *stack) { + return new CSXMemBuffer(inGame, stack); +} + +////////////////////////////////////////////////////////////////////////// +CSXMemBuffer::CSXMemBuffer(CBGame *inGame, CScStack *stack): CBScriptable(inGame) { + stack->correctParams(1); + _buffer = NULL; + _size = 0; + + int NewSize = stack->pop()->getInt(); + resize(MAX(0, NewSize)); +} + +////////////////////////////////////////////////////////////////////////// +CSXMemBuffer::CSXMemBuffer(CBGame *inGame, void *Buffer): CBScriptable(inGame) { + _size = 0; + _buffer = Buffer; +} + + +////////////////////////////////////////////////////////////////////////// +CSXMemBuffer::~CSXMemBuffer() { + cleanup(); +} + +////////////////////////////////////////////////////////////////////////// +void *CSXMemBuffer::scToMemBuffer() { + return _buffer; +} + +////////////////////////////////////////////////////////////////////////// +void CSXMemBuffer::cleanup() { + if (_size) free(_buffer); + _buffer = NULL; + _size = 0; +} + +////////////////////////////////////////////////////////////////////////// +bool CSXMemBuffer::resize(int newSize) { + int oldSize = _size; + + if (_size == 0) { + _buffer = malloc(newSize); + if (_buffer) _size = newSize; + } else { + void *newBuf = realloc(_buffer, newSize); + if (!newBuf) { + if (newSize == 0) { + _buffer = newBuf; + _size = newSize; + } else return STATUS_FAILED; + } else { + _buffer = newBuf; + _size = newSize; + } + } + + if (_buffer && _size > oldSize) { + memset((byte *)_buffer + oldSize, 0, _size - oldSize); + } + return STATUS_OK; +} + +////////////////////////////////////////////////////////////////////////// +bool CSXMemBuffer::checkBounds(CScScript *script, int start, int length) { + if (_buffer == NULL) { + script->runtimeError("Cannot use Set/Get methods on an uninitialized memory buffer"); + return false; + } + if (_size == 0) + return true; + + if (start < 0 || length == 0 || start + length > _size) { + script->runtimeError("Set/Get method call is out of bounds"); + return false; + } else + return true; +} + +////////////////////////////////////////////////////////////////////////// +const char *CSXMemBuffer::scToString() { + return "[membuffer object]"; +} + + +////////////////////////////////////////////////////////////////////////// +bool CSXMemBuffer::scCallMethod(CScScript *script, CScStack *stack, CScStack *thisStack, const char *name) { + ////////////////////////////////////////////////////////////////////////// + // SetSize + ////////////////////////////////////////////////////////////////////////// + if (strcmp(name, "SetSize") == 0) { + stack->correctParams(1); + int newSize = stack->pop()->getInt(); + newSize = MAX(0, newSize); + if (DID_SUCCEED(resize(newSize))) + stack->pushBool(true); + else + stack->pushBool(false); + + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // GetBool + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "GetBool") == 0) { + stack->correctParams(1); + int start = stack->pop()->getInt(); + if (!checkBounds(script, start, sizeof(bool))) + stack->pushNULL(); + else + stack->pushBool(*(bool *)((byte *)_buffer + start)); + + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // GetByte + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "GetByte") == 0) { + stack->correctParams(1); + int start = stack->pop()->getInt(); + if (!checkBounds(script, start, sizeof(byte))) + stack->pushNULL(); + else + stack->pushInt(*(byte *)((byte *)_buffer + start)); + + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // GetShort + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "GetShort") == 0) { + stack->correctParams(1); + int Start = stack->pop()->getInt(); + if (!checkBounds(script, Start, sizeof(short))) + stack->pushNULL(); + else + stack->pushInt(65536 + * (short *)((byte *)_buffer + Start)); + + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // GetInt / GetLong + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "GetInt") == 0 || strcmp(name, "GetLong") == 0) { + stack->correctParams(1); + int start = stack->pop()->getInt(); + if (!checkBounds(script, start, sizeof(int))) + stack->pushNULL(); + else + stack->pushInt(*(int *)((byte *)_buffer + start)); + + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // GetFloat + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "GetFloat") == 0) { + stack->correctParams(1); + int start = stack->pop()->getInt(); + if (!checkBounds(script, start, sizeof(float))) + stack->pushNULL(); + else + stack->pushFloat(*(float *)((byte *)_buffer + start)); + + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // GetDouble + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "GetDouble") == 0) { + stack->correctParams(1); + int start = stack->pop()->getInt(); + if (!checkBounds(script, start, sizeof(double))) + stack->pushNULL(); + else + stack->pushFloat(*(double *)((byte *)_buffer + start)); + + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // GetString + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "GetString") == 0) { + stack->correctParams(2); + int start = stack->pop()->getInt(); + int length = stack->pop()->getInt(); + + // find end of string + if (length == 0 && start >= 0 && start < _size) { + for (int i = start; i < _size; i++) { + if (((char *)_buffer)[i] == '\0') { + length = i - start; + break; + } + } + } + + if (!checkBounds(script, start, length)) + stack->pushNULL(); + else { + char *str = new char[length + 1]; + strncpy(str, (const char *)_buffer + start, length); + str[length] = '\0'; + stack->pushString(str); + delete [] str; + } + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // GetPointer + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "GetPointer") == 0) { + stack->correctParams(1); + int start = stack->pop()->getInt(); + if (!checkBounds(script, start, sizeof(void *))) + stack->pushNULL(); + else { + void *pointer = *(void **)((byte *)_buffer + start); + CSXMemBuffer *buf = new CSXMemBuffer(_gameRef, pointer); + stack->pushNative(buf, false); + } + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // SetBool + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "SetBool") == 0) { + stack->correctParams(2); + int start = stack->pop()->getInt(); + bool val = stack->pop()->getBool(); + + if (!checkBounds(script, start, sizeof(bool))) + stack->pushBool(false); + else { + *(bool *)((byte *)_buffer + start) = val; + stack->pushBool(true); + } + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // SetByte + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "SetByte") == 0) { + stack->correctParams(2); + int start = stack->pop()->getInt(); + byte val = (byte)stack->pop()->getInt(); + + if (!checkBounds(script, start, sizeof(byte))) + stack->pushBool(false); + else { + *(byte *)((byte *)_buffer + start) = val; + stack->pushBool(true); + } + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // SetShort + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "SetShort") == 0) { + stack->correctParams(2); + int start = stack->pop()->getInt(); + short val = (short)stack->pop()->getInt(); + + if (!checkBounds(script, start, sizeof(short))) + stack->pushBool(false); + else { + *(short *)((byte *)_buffer + start) = val; + stack->pushBool(true); + } + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // SetInt / SetLong + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "SetInt") == 0 || strcmp(name, "SetLong") == 0) { + stack->correctParams(2); + int start = stack->pop()->getInt(); + int val = stack->pop()->getInt(); + + if (!checkBounds(script, start, sizeof(int))) + stack->pushBool(false); + else { + *(int *)((byte *)_buffer + start) = val; + stack->pushBool(true); + } + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // SetFloat + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "SetFloat") == 0) { + stack->correctParams(2); + int start = stack->pop()->getInt(); + float val = (float)stack->pop()->getFloat(); + + if (!checkBounds(script, start, sizeof(float))) + stack->pushBool(false); + else { + *(float *)((byte *)_buffer + start) = val; + stack->pushBool(true); + } + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // SetDouble + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "SetDouble") == 0) { + stack->correctParams(2); + int start = stack->pop()->getInt(); + double val = stack->pop()->getFloat(); + + if (!checkBounds(script, start, sizeof(double))) + stack->pushBool(false); + else { + *(double *)((byte *)_buffer + start) = val; + stack->pushBool(true); + } + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // SetString + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "SetString") == 0) { + stack->correctParams(2); + int start = stack->pop()->getInt(); + const char *val = stack->pop()->getString(); + + if (!checkBounds(script, start, strlen(val) + 1)) + stack->pushBool(false); + else { + memcpy((byte *)_buffer + start, val, strlen(val) + 1); + stack->pushBool(true); + } + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // SetPointer + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "SetPointer") == 0) { + stack->correctParams(2); + int start = stack->pop()->getInt(); + /* CScValue *Val = */ stack->pop(); + + if (!checkBounds(script, start, sizeof(void *))) + stack->pushBool(false); + else { + /* + int Pointer = (int)Val->getMemBuffer(); + memcpy((byte *)_buffer+Start, &Pointer, sizeof(void*)); + stack->pushBool(true); + */ + // TODO fix + stack->pushBool(false); + + } + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // DEBUG_Dump + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "DEBUG_Dump") == 0) { + stack->correctParams(0); + if (_buffer && _size) { + warning("SXMemBuffer::ScCallMethod - DEBUG_Dump"); + Common::DumpFile f; + f.open("buffer.bin"); + f.write(_buffer, _size); + f.close(); + } + stack->pushNULL(); + return STATUS_OK; + } + + else return STATUS_FAILED; +} + + +////////////////////////////////////////////////////////////////////////// +CScValue *CSXMemBuffer::scGetProperty(const char *name) { + _scValue->setNULL(); + + ////////////////////////////////////////////////////////////////////////// + // Type (RO) + ////////////////////////////////////////////////////////////////////////// + if (strcmp(name, "Type") == 0) { + _scValue->setString("membuffer"); + return _scValue; + } + + ////////////////////////////////////////////////////////////////////////// + // Size (RO) + ////////////////////////////////////////////////////////////////////////// + if (strcmp(name, "Size") == 0) { + _scValue->setInt(_size); + return _scValue; + } + + else return CBScriptable::scGetProperty(name); +} + + +////////////////////////////////////////////////////////////////////////// +bool CSXMemBuffer::scSetProperty(const char *name, CScValue *value) { + /* + ////////////////////////////////////////////////////////////////////////// + // Length + ////////////////////////////////////////////////////////////////////////// + if(strcmp(name, "Length")==0){ + int OrigLength = _length; + _length = max(value->getInt(0), 0); + + char PropName[20]; + if(_length < OrigLength){ + for(int i=_length; iDeleteProp(PropName); + } + } + return STATUS_OK; + } + else*/ return CBScriptable::scSetProperty(name, value); +} + + +////////////////////////////////////////////////////////////////////////// +bool CSXMemBuffer::persist(CBPersistMgr *persistMgr) { + + CBScriptable::persist(persistMgr); + + persistMgr->transfer(TMEMBER(_size)); + + if (persistMgr->_saving) { + if (_size > 0) persistMgr->putBytes((byte *)_buffer, _size); + } else { + if (_size > 0) { + _buffer = malloc(_size); + persistMgr->getBytes((byte *)_buffer, _size); + } else _buffer = NULL; + } + + return STATUS_OK; +} + + +////////////////////////////////////////////////////////////////////////// +int CSXMemBuffer::scCompare(CBScriptable *val) { + if (_buffer == val->scToMemBuffer()) return 0; + else return 1; +} + +} // end of namespace WinterMute diff --git a/engines/wintermute/base/scriptables/SXMemBuffer.h b/engines/wintermute/base/scriptables/SXMemBuffer.h new file mode 100644 index 0000000000..09831bf464 --- /dev/null +++ b/engines/wintermute/base/scriptables/SXMemBuffer.h @@ -0,0 +1,59 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +/* + * This file is based on WME Lite. + * http://dead-code.org/redir.php?target=wmelite + * Copyright (c) 2011 Jan Nedoma + */ + +#ifndef WINTERMUTE_SXMEMBUFFER_H +#define WINTERMUTE_SXMEMBUFFER_H + + +#include "engines/wintermute/base/BScriptable.h" + +namespace WinterMute { + +class CSXMemBuffer : public CBScriptable { +public: + virtual int scCompare(CBScriptable *Val); + DECLARE_PERSISTENT(CSXMemBuffer, CBScriptable) + CScValue *scGetProperty(const char *name); + bool scSetProperty(const char *name, CScValue *value); + bool scCallMethod(CScScript *script, CScStack *stack, CScStack *thisStack, const char *name); + const char *scToString(); + CSXMemBuffer(CBGame *inGame, CScStack *stack); + CSXMemBuffer(CBGame *inGame, void *buffer); + virtual ~CSXMemBuffer(); + virtual void *scToMemBuffer(); + int _size; +private: + bool resize(int newSize); + void *_buffer; + void cleanup(); + bool checkBounds(CScScript *script, int start, int length); +}; + +} // end of namespace WinterMute + +#endif diff --git a/engines/wintermute/base/scriptables/SXString.cpp b/engines/wintermute/base/scriptables/SXString.cpp new file mode 100644 index 0000000000..ed3d243cb0 --- /dev/null +++ b/engines/wintermute/base/scriptables/SXString.cpp @@ -0,0 +1,404 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +/* + * This file is based on WME Lite. + * http://dead-code.org/redir.php?target=wmelite + * Copyright (c) 2011 Jan Nedoma + */ + +#include "engines/wintermute/base/BGame.h" +#include "engines/wintermute/base/scriptables/ScStack.h" +#include "engines/wintermute/base/scriptables/ScValue.h" +#include "engines/wintermute/utils/utils.h" +#include "engines/wintermute/base/scriptables/SXString.h" +#include "engines/wintermute/base/scriptables/SXArray.h" +#include "engines/wintermute/utils/StringUtil.h" +#include "common/tokenizer.h" + +namespace WinterMute { + +IMPLEMENT_PERSISTENT(CSXString, false) + +CBScriptable *makeSXString(CBGame *inGame, CScStack *stack) { + return new CSXString(inGame, stack); +} + +////////////////////////////////////////////////////////////////////////// +CSXString::CSXString(CBGame *inGame, CScStack *stack): CBScriptable(inGame) { + _string = NULL; + _capacity = 0; + + stack->correctParams(1); + CScValue *val = stack->pop(); + + if (val->isInt()) { + _capacity = MAX(0, val->getInt()); + if (_capacity > 0) { + _string = new char[_capacity]; + memset(_string, 0, _capacity); + } + } else { + setStringVal(val->getString()); + } + + if (_capacity == 0) setStringVal(""); +} + + +////////////////////////////////////////////////////////////////////////// +CSXString::~CSXString() { + if (_string) delete [] _string; +} + + +////////////////////////////////////////////////////////////////////////// +void CSXString::setStringVal(const char *val) { + int len = strlen(val); + if (len >= _capacity) { + _capacity = len + 1; + delete[] _string; + _string = NULL; + _string = new char[_capacity]; + memset(_string, 0, _capacity); + } + strcpy(_string, val); +} + + +////////////////////////////////////////////////////////////////////////// +const char *CSXString::scToString() { + if (_string) return _string; + else return "[null string]"; +} + + +////////////////////////////////////////////////////////////////////////// +void CSXString::scSetString(const char *val) { + setStringVal(val); +} + + +////////////////////////////////////////////////////////////////////////// +bool CSXString::scCallMethod(CScScript *script, CScStack *stack, CScStack *thisStack, const char *name) { + ////////////////////////////////////////////////////////////////////////// + // Substring + ////////////////////////////////////////////////////////////////////////// + if (strcmp(name, "Substring") == 0) { + stack->correctParams(2); + int start = stack->pop()->getInt(); + int end = stack->pop()->getInt(); + + if (end < start) CBUtils::swap(&start, &end); + + //try { + WideString str; + if (_gameRef->_textEncoding == TEXT_UTF8) + str = StringUtil::utf8ToWide(_string); + else + str = StringUtil::ansiToWide(_string); + + //WideString subStr = str.substr(start, end - start + 1); + WideString subStr(str.c_str() + start, end - start + 1); + + if (_gameRef->_textEncoding == TEXT_UTF8) + stack->pushString(StringUtil::wideToUtf8(subStr).c_str()); + else + stack->pushString(StringUtil::wideToAnsi(subStr).c_str()); + // } catch (std::exception &) { + // stack->pushNULL(); + // } + + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // Substr + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "Substr") == 0) { + stack->correctParams(2); + int start = stack->pop()->getInt(); + + CScValue *val = stack->pop(); + int len = val->getInt(); + + if (!val->isNULL() && len <= 0) { + stack->pushString(""); + return STATUS_OK; + } + + if (val->isNULL()) len = strlen(_string) - start; + +// try { + WideString str; + if (_gameRef->_textEncoding == TEXT_UTF8) + str = StringUtil::utf8ToWide(_string); + else + str = StringUtil::ansiToWide(_string); + +// WideString subStr = str.substr(start, len); + WideString subStr(str.c_str() + start, len); + + if (_gameRef->_textEncoding == TEXT_UTF8) + stack->pushString(StringUtil::wideToUtf8(subStr).c_str()); + else + stack->pushString(StringUtil::wideToAnsi(subStr).c_str()); +// } catch (std::exception &) { +// stack->pushNULL(); +// } + + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // ToUpperCase + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "ToUpperCase") == 0) { + stack->correctParams(0); + + WideString str; + if (_gameRef->_textEncoding == TEXT_UTF8) + str = StringUtil::utf8ToWide(_string); + else + str = StringUtil::ansiToWide(_string); + + str.toUppercase(); + + if (_gameRef->_textEncoding == TEXT_UTF8) + stack->pushString(StringUtil::wideToUtf8(str).c_str()); + else + stack->pushString(StringUtil::wideToAnsi(str).c_str()); + + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // ToLowerCase + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "ToLowerCase") == 0) { + stack->correctParams(0); + + WideString str; + if (_gameRef->_textEncoding == TEXT_UTF8) + str = StringUtil::utf8ToWide(_string); + else + str = StringUtil::ansiToWide(_string); + + str.toLowercase(); + + if (_gameRef->_textEncoding == TEXT_UTF8) + stack->pushString(StringUtil::wideToUtf8(str).c_str()); + else + stack->pushString(StringUtil::wideToAnsi(str).c_str()); + + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // IndexOf + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "IndexOf") == 0) { + stack->correctParams(2); + + const char *strToFind = stack->pop()->getString(); + int index = stack->pop()->getInt(); + + WideString str; + if (_gameRef->_textEncoding == TEXT_UTF8) + str = StringUtil::utf8ToWide(_string); + else + str = StringUtil::ansiToWide(_string); + + WideString toFind; + if (_gameRef->_textEncoding == TEXT_UTF8) + toFind = StringUtil::utf8ToWide(strToFind); + else + toFind = StringUtil::ansiToWide(strToFind); + + int indexOf = StringUtil::indexOf(str, toFind, index); + stack->pushInt(indexOf); + + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // Split + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "Split") == 0) { + stack->correctParams(1); + CScValue *val = stack->pop(); + char separators[MAX_PATH_LENGTH] = ","; + if (!val->isNULL()) strcpy(separators, val->getString()); + + CSXArray *array = new CSXArray(_gameRef); + if (!array) { + stack->pushNULL(); + return STATUS_OK; + } + + + WideString str; + if (_gameRef->_textEncoding == TEXT_UTF8) + str = StringUtil::utf8ToWide(_string); + else + str = StringUtil::ansiToWide(_string); + + WideString delims; + if (_gameRef->_textEncoding == TEXT_UTF8) + delims = StringUtil::utf8ToWide(separators); + else + delims = StringUtil::ansiToWide(separators); + + Common::Array parts; + + + + Common::StringTokenizer tokenizer(str, delims); + while (!tokenizer.empty()) { + Common::String str2 = tokenizer.nextToken(); + parts.push_back(str2); + } + // TODO: Clean this up + /*do { + pos = StringUtil::IndexOf(Common::String(str.c_str() + start), delims, start); + //pos = str.find_first_of(delims, start); + if (pos == start) { + start = pos + 1; + } else if (pos == str.size()) { + parts.push_back(Common::String(str.c_str() + start)); + break; + } else { + parts.push_back(Common::String(str.c_str() + start, pos - start)); + start = pos + 1; + } + //start = str.find_first_not_of(delims, start); + start = StringUtil::LastIndexOf(Common::String(str.c_str() + start), delims, start) + 1; + + } while (pos != str.size());*/ + + for (Common::Array::iterator it = parts.begin(); it != parts.end(); ++it) { + WideString &part = (*it); + + if (_gameRef->_textEncoding == TEXT_UTF8) + val = new CScValue(_gameRef, StringUtil::wideToUtf8(part).c_str()); + else + val = new CScValue(_gameRef, StringUtil::wideToAnsi(part).c_str()); + + array->push(val); + delete val; + val = NULL; + } + + stack->pushNative(array, false); + return STATUS_OK; + } + + else return STATUS_FAILED; +} + + +////////////////////////////////////////////////////////////////////////// +CScValue *CSXString::scGetProperty(const char *name) { + _scValue->setNULL(); + + ////////////////////////////////////////////////////////////////////////// + // Type (RO) + ////////////////////////////////////////////////////////////////////////// + if (strcmp(name, "Type") == 0) { + _scValue->setString("string"); + return _scValue; + } + ////////////////////////////////////////////////////////////////////////// + // Length (RO) + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "Length") == 0) { + if (_gameRef->_textEncoding == TEXT_UTF8) { + WideString wstr = StringUtil::utf8ToWide(_string); + _scValue->setInt(wstr.size()); + } else + _scValue->setInt(strlen(_string)); + + return _scValue; + } + ////////////////////////////////////////////////////////////////////////// + // Capacity + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "Capacity") == 0) { + _scValue->setInt(_capacity); + return _scValue; + } + + else return _scValue; +} + + +////////////////////////////////////////////////////////////////////////// +bool CSXString::scSetProperty(const char *name, CScValue *value) { + ////////////////////////////////////////////////////////////////////////// + // Capacity + ////////////////////////////////////////////////////////////////////////// + if (strcmp(name, "Capacity") == 0) { + int32 newCap = (uint32)value->getInt(); + if (newCap < (int32)(strlen(_string) + 1)) _gameRef->LOG(0, "Warning: cannot lower string capacity"); + else if (newCap != _capacity) { + char *newStr = new char[newCap]; + if (newStr) { + memset(newStr, 0, newCap); + strcpy(newStr, _string); + delete[] _string; + _string = newStr; + _capacity = newCap; + } + } + return STATUS_OK; + } + + else return STATUS_FAILED; +} + + +////////////////////////////////////////////////////////////////////////// +bool CSXString::persist(CBPersistMgr *persistMgr) { + + CBScriptable::persist(persistMgr); + + persistMgr->transfer(TMEMBER(_capacity)); + + if (persistMgr->_saving) { + if (_capacity > 0) persistMgr->putBytes((byte *)_string, _capacity); + } else { + if (_capacity > 0) { + _string = new char[_capacity]; + persistMgr->getBytes((byte *)_string, _capacity); + } else _string = NULL; + } + + return STATUS_OK; +} + + +////////////////////////////////////////////////////////////////////////// +int CSXString::scCompare(CBScriptable *val) { + return strcmp(_string, ((CSXString *)val)->_string); +} + +} // end of namespace WinterMute diff --git a/engines/wintermute/base/scriptables/SXString.h b/engines/wintermute/base/scriptables/SXString.h new file mode 100644 index 0000000000..348595ad29 --- /dev/null +++ b/engines/wintermute/base/scriptables/SXString.h @@ -0,0 +1,58 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +/* + * This file is based on WME Lite. + * http://dead-code.org/redir.php?target=wmelite + * Copyright (c) 2011 Jan Nedoma + */ + +#ifndef WINTERMUTE_SXSTRING_H +#define WINTERMUTE_SXSTRING_H + + +#include "engines/wintermute/base/BScriptable.h" + +namespace WinterMute { + +class CSXString : public CBScriptable { +public: + virtual int scCompare(CBScriptable *Val); + DECLARE_PERSISTENT(CSXString, CBScriptable) + CScValue *scGetProperty(const char *name); + bool scSetProperty(const char *name, CScValue *value); + bool scCallMethod(CScScript *script, CScStack *stack, CScStack *thisStack, const char *name); + void scSetString(const char *val); + const char *scToString(); + void setStringVal(const char *val); + + CSXString(CBGame *inGame, CScStack *Stack); + virtual ~CSXString(); + +private: + char *_string; + int _capacity; +}; + +} // end of namespace WinterMute + +#endif diff --git a/engines/wintermute/base/scriptables/ScEngine.cpp b/engines/wintermute/base/scriptables/ScEngine.cpp new file mode 100644 index 0000000000..db79a7d0e9 --- /dev/null +++ b/engines/wintermute/base/scriptables/ScEngine.cpp @@ -0,0 +1,712 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +/* + * This file is based on WME Lite. + * http://dead-code.org/redir.php?target=wmelite + * Copyright (c) 2011 Jan Nedoma + */ + +#include "engines/wintermute/dcgf.h" +#include "engines/wintermute/base/scriptables/ScEngine.h" +#include "engines/wintermute/utils/StringUtil.h" +#include "engines/wintermute/base/scriptables/ScValue.h" +#include "engines/wintermute/base/scriptables/ScScript.h" +#include "engines/wintermute/base/scriptables/ScStack.h" +#include "engines/wintermute/base/scriptables/SXMath.h" +#include "engines/wintermute/base/BRegistry.h" +#include "engines/wintermute/base/BGame.h" +#include "engines/wintermute/base/BSound.h" +#include "engines/wintermute/base/BFileManager.h" + + +namespace WinterMute { + +IMPLEMENT_PERSISTENT(CScEngine, true) + +#define COMPILER_DLL "dcscomp.dll" +////////////////////////////////////////////////////////////////////////// +CScEngine::CScEngine(CBGame *inGame): CBBase(inGame) { + _gameRef->LOG(0, "Initializing scripting engine..."); + + if (_compilerAvailable) _gameRef->LOG(0, " Script compiler bound successfuly"); + else _gameRef->LOG(0, " Script compiler is NOT available"); + + _globals = new CScValue(_gameRef); + + + // register 'Game' as global variable + if (!_globals->propExists("Game")) { + CScValue val(_gameRef); + val.setNative(_gameRef, true); + _globals->setProp("Game", &val); + } + + // register 'Math' as global variable + if (!_globals->propExists("Math")) { + CScValue val(_gameRef); + val.setNative(_gameRef->_mathClass, true); + _globals->setProp("Math", &val); + } + + // prepare script cache + for (int i = 0; i < MAX_CACHED_SCRIPTS; i++) _cachedScripts[i] = NULL; + + _currentScript = NULL; + + _isProfiling = false; + _profilingStartTime = 0; + + //EnableProfiling(); +} + + +////////////////////////////////////////////////////////////////////////// +CScEngine::~CScEngine() { + _gameRef->LOG(0, "Shutting down scripting engine"); + saveBreakpoints(); + + disableProfiling(); + + cleanup(); + + for (int i = 0; i < _breakpoints.getSize(); i++) { + delete _breakpoints[i]; + _breakpoints[i] = NULL; + } + _breakpoints.removeAll(); +} + + +////////////////////////////////////////////////////////////////////////// +bool CScEngine::cleanup() { + for (int i = 0; i < _scripts.getSize(); i++) { + if (!_scripts[i]->_thread && _scripts[i]->_owner) _scripts[i]->_owner->removeScript(_scripts[i]); + delete _scripts[i]; + _scripts.removeAt(i); + i--; + } + + _scripts.removeAll(); + + delete _globals; + _globals = NULL; + + emptyScriptCache(); + + _currentScript = NULL; // ref only + + return STATUS_OK; +} + + +////////////////////////////////////////////////////////////////////////// +byte *CScEngine::loadFile(void *data, char *filename, uint32 *size) { + CBGame *gameRef = (CBGame *)data; + return gameRef->_fileManager->readWholeFile(filename, size); +} + + +////////////////////////////////////////////////////////////////////////// +void CScEngine::closeFile(void *data, byte *buffer) { + delete [] buffer; +} + + +////////////////////////////////////////////////////////////////////////// +void CScEngine::parseElement(void *data, int line, int type, void *elementData) { +} + + +////////////////////////////////////////////////////////////////////////// +CScScript *CScEngine::runScript(const char *filename, CBScriptHolder *owner) { + byte *compBuffer; + uint32 compSize; + + // get script from cache + compBuffer = getCompiledScript(filename, &compSize); + if (!compBuffer) return NULL; + + // add new script + CScScript *script = new CScScript(_gameRef, this); + bool ret = script->create(filename, compBuffer, compSize, owner); + if (DID_FAIL(ret)) { + _gameRef->LOG(ret, "Error running script '%s'...", filename); + delete script; + return NULL; + } else { + // publish the "self" pseudo-variable + CScValue val(_gameRef); + if (owner)val.setNative(owner, true); + else val.setNULL(); + + script->_globals->setProp("self", &val); + script->_globals->setProp("this", &val); + + _scripts.add(script); + _gameRef->getDebugMgr()->onScriptInit(script); + + return script; + } +} + + +////////////////////////////////////////////////////////////////////////// +byte *CScEngine::getCompiledScript(const char *filename, uint32 *outSize, bool ignoreCache) { + // is script in cache? + if (!ignoreCache) { + for (int i = 0; i < MAX_CACHED_SCRIPTS; i++) { + if (_cachedScripts[i] && scumm_stricmp(_cachedScripts[i]->_filename.c_str(), filename) == 0) { + _cachedScripts[i]->_timestamp = CBPlatform::getTime(); + *outSize = _cachedScripts[i]->_size; + return _cachedScripts[i]->_buffer; + } + } + } + + // nope, load it + byte *compBuffer; + uint32 compSize; + + uint32 size; + + byte *buffer = _gameRef->_fileManager->readWholeFile(filename, &size); + if (!buffer) { + _gameRef->LOG(0, "CScEngine::GetCompiledScript - error opening script '%s'", filename); + return NULL; + } + + // needs to be compiled? + if (FROM_LE_32(*(uint32 *)buffer) == SCRIPT_MAGIC) { + compBuffer = buffer; + compSize = size; + } else { + if (!_compilerAvailable) { + _gameRef->LOG(0, "CScEngine::GetCompiledScript - script '%s' needs to be compiled but compiler is not available", filename); + delete [] buffer; + return NULL; + } + // This code will never be called, since _compilerAvailable is const false. + // It's only here in the event someone would want to reinclude the compiler. + error("Script needs compilation, ScummVM does not contain a WME compiler"); + } + + byte *ret = NULL; + + // add script to cache + CScCachedScript *cachedScript = new CScCachedScript(filename, compBuffer, compSize); + if (cachedScript) { + int index = 0; + uint32 MinTime = CBPlatform::getTime(); + for (int i = 0; i < MAX_CACHED_SCRIPTS; i++) { + if (_cachedScripts[i] == NULL) { + index = i; + break; + } else if (_cachedScripts[i]->_timestamp <= MinTime) { + MinTime = _cachedScripts[i]->_timestamp; + index = i; + } + } + + if (_cachedScripts[index] != NULL) delete _cachedScripts[index]; + _cachedScripts[index] = cachedScript; + + ret = cachedScript->_buffer; + *outSize = cachedScript->_size; + } + + + // cleanup + delete [] buffer; + + return ret; +} + + + +////////////////////////////////////////////////////////////////////////// +bool CScEngine::tick() { + if (_scripts.getSize() == 0) + return STATUS_OK; + + + // resolve waiting scripts + for (int i = 0; i < _scripts.getSize(); i++) { + + switch (_scripts[i]->_state) { + case SCRIPT_WAITING: { + /* + bool obj_found=false; + for(int j=0; j<_gameRef->_regObjects.getSize(); j++) + { + if(_gameRef->_regObjects[j] == _scripts[i]->_waitObject) + { + if(_gameRef->_regObjects[j]->IsReady()) _scripts[i]->Run(); + obj_found = true; + break; + } + } + if(!obj_found) _scripts[i]->finish(); // _waitObject no longer exists + */ + if (_gameRef->validObject(_scripts[i]->_waitObject)) { + if (_scripts[i]->_waitObject->isReady()) _scripts[i]->run(); + } else _scripts[i]->finish(); + break; + } + + case SCRIPT_SLEEPING: { + if (_scripts[i]->_waitFrozen) { + if (_scripts[i]->_waitTime <= CBPlatform::getTime()) _scripts[i]->run(); + } else { + if (_scripts[i]->_waitTime <= _gameRef->_timer) _scripts[i]->run(); + } + break; + } + + case SCRIPT_WAITING_SCRIPT: { + if (!isValidScript(_scripts[i]->_waitScript) || _scripts[i]->_waitScript->_state == SCRIPT_ERROR) { + // fake return value + _scripts[i]->_stack->pushNULL(); + _scripts[i]->_waitScript = NULL; + _scripts[i]->run(); + } else { + if (_scripts[i]->_waitScript->_state == SCRIPT_THREAD_FINISHED) { + // copy return value + _scripts[i]->_stack->push(_scripts[i]->_waitScript->_stack->pop()); + _scripts[i]->run(); + _scripts[i]->_waitScript->finish(); + _scripts[i]->_waitScript = NULL; + } + } + break; + } + default: + //warning("CScEngine::Tick - Unhandled enum"); + break; + } // switch + } // for each script + + + // execute scripts + for (int i = 0; i < _scripts.getSize(); i++) { + + // skip paused scripts + if (_scripts[i]->_state == SCRIPT_PAUSED) continue; + + // time sliced script + if (_scripts[i]->_timeSlice > 0) { + uint32 StartTime = CBPlatform::getTime(); + while (_scripts[i]->_state == SCRIPT_RUNNING && CBPlatform::getTime() - StartTime < _scripts[i]->_timeSlice) { + _currentScript = _scripts[i]; + _scripts[i]->executeInstruction(); + } + if (_isProfiling && _scripts[i]->_filename) addScriptTime(_scripts[i]->_filename, CBPlatform::getTime() - StartTime); + } + + // normal script + else { + uint32 startTime = 0; + bool isProfiling = _isProfiling; + if (isProfiling) startTime = CBPlatform::getTime(); + + while (_scripts[i]->_state == SCRIPT_RUNNING) { + _currentScript = _scripts[i]; + _scripts[i]->executeInstruction(); + } + if (isProfiling && _scripts[i]->_filename) addScriptTime(_scripts[i]->_filename, CBPlatform::getTime() - startTime); + } + _currentScript = NULL; + } + + removeFinishedScripts(); + + return STATUS_OK; +} + + +////////////////////////////////////////////////////////////////////////// +bool CScEngine::tickUnbreakable() { + // execute unbreakable scripts + for (int i = 0; i < _scripts.getSize(); i++) { + if (!_scripts[i]->_unbreakable) continue; + + while (_scripts[i]->_state == SCRIPT_RUNNING) { + _currentScript = _scripts[i]; + _scripts[i]->executeInstruction(); + } + _scripts[i]->finish(); + _currentScript = NULL; + } + removeFinishedScripts(); + + return STATUS_OK; +} + + +////////////////////////////////////////////////////////////////////////// +bool CScEngine::removeFinishedScripts() { + // remove finished scripts + for (int i = 0; i < _scripts.getSize(); i++) { + if (_scripts[i]->_state == SCRIPT_FINISHED || _scripts[i]->_state == SCRIPT_ERROR) { + if (!_scripts[i]->_thread && _scripts[i]->_owner) _scripts[i]->_owner->removeScript(_scripts[i]); + _gameRef->getDebugMgr()->onScriptShutdown(_scripts[i]); + delete _scripts[i]; + _scripts.removeAt(i); + i--; + } + } + return STATUS_OK; +} + + +////////////////////////////////////////////////////////////////////////// +int CScEngine::getNumScripts(int *running, int *waiting, int *persistent) { + int numRunning = 0, numWaiting = 0, numPersistent = 0, numTotal = 0; + + for (int i = 0; i < _scripts.getSize(); i++) { + if (_scripts[i]->_state == SCRIPT_FINISHED) continue; + switch (_scripts[i]->_state) { + case SCRIPT_RUNNING: + case SCRIPT_SLEEPING: + case SCRIPT_PAUSED: + numRunning++; + break; + case SCRIPT_WAITING: + numWaiting++; + break; + case SCRIPT_PERSISTENT: + numPersistent++; + break; + default: + warning("CScEngine::GetNumScripts - unhandled enum"); + break; + } + numTotal++; + } + if (running) *running = numRunning; + if (waiting) *waiting = numWaiting; + if (persistent) *persistent = numPersistent; + + return numTotal; +} + + +////////////////////////////////////////////////////////////////////////// +bool CScEngine::emptyScriptCache() { + for (int i = 0; i < MAX_CACHED_SCRIPTS; i++) { + if (_cachedScripts[i]) { + delete _cachedScripts[i]; + _cachedScripts[i] = NULL; + } + } + return STATUS_OK; +} + + +////////////////////////////////////////////////////////////////////////// +bool CScEngine::resetObject(CBObject *Object) { + // terminate all scripts waiting for this object + for (int i = 0; i < _scripts.getSize(); i++) { + if (_scripts[i]->_state == SCRIPT_WAITING && _scripts[i]->_waitObject == Object) { + if (!_gameRef->_compatKillMethodThreads) resetScript(_scripts[i]); + + bool IsThread = _scripts[i]->_methodThread || _scripts[i]->_thread; + _scripts[i]->finish(!IsThread); // 1.9b1 - top-level script kills its threads as well + } + } + return STATUS_OK; +} + +////////////////////////////////////////////////////////////////////////// +bool CScEngine::resetScript(CScScript *script) { + // terminate all scripts waiting for this script + for (int i = 0; i < _scripts.getSize(); i++) { + if (_scripts[i]->_state == SCRIPT_WAITING_SCRIPT && _scripts[i]->_waitScript == script) { + _scripts[i]->finish(); + } + } + return STATUS_OK; +} + +////////////////////////////////////////////////////////////////////////// +bool CScEngine::persist(CBPersistMgr *persistMgr) { + if (!persistMgr->_saving) cleanup(); + + persistMgr->transfer(TMEMBER(_gameRef)); + persistMgr->transfer(TMEMBER(_currentScript)); + persistMgr->transfer(TMEMBER(_globals)); + _scripts.persist(persistMgr); + + return STATUS_OK; +} + + +////////////////////////////////////////////////////////////////////////// +void CScEngine::editorCleanup() { + for (int i = 0; i < _scripts.getSize(); i++) { + if (_scripts[i]->_owner == NULL && (_scripts[i]->_state == SCRIPT_FINISHED || _scripts[i]->_state == SCRIPT_ERROR)) { + delete _scripts[i]; + _scripts.removeAt(i); + i--; + } + } +} + + +////////////////////////////////////////////////////////////////////////// +bool CScEngine::pauseAll() { + for (int i = 0; i < _scripts.getSize(); i++) { + if (_scripts[i] != _currentScript) _scripts[i]->pause(); + } + + return STATUS_OK; +} + + +////////////////////////////////////////////////////////////////////////// +bool CScEngine::resumeAll() { + for (int i = 0; i < _scripts.getSize(); i++) + _scripts[i]->resume(); + + return STATUS_OK; +} + + +////////////////////////////////////////////////////////////////////////// +bool CScEngine::isValidScript(CScScript *script) { + for (int i = 0; i < _scripts.getSize(); i++) { + if (_scripts[i] == script) return true; + } + return false; +} + +////////////////////////////////////////////////////////////////////////// +bool CScEngine::clearGlobals(bool includingNatives) { + _globals->CleanProps(includingNatives); + return STATUS_OK; +} + +////////////////////////////////////////////////////////////////////////// +bool CScEngine::dbgSendScripts(IWmeDebugClient *client) { + // send global variables + _globals->dbgSendVariables(client, WME_DBGVAR_GLOBAL, NULL, 0); + + // process normal scripts first + for (int i = 0; i < _scripts.getSize(); i++) { + if (_scripts[i]->_thread || _scripts[i]->_methodThread) continue; + _scripts[i]->dbgSendScript(client); + } + + // and threads later + for (int i = 0; i < _scripts.getSize(); i++) { + if (_scripts[i]->_thread || _scripts[i]->_methodThread) + _scripts[i]->dbgSendScript(client); + } + + return STATUS_OK; +} + +////////////////////////////////////////////////////////////////////////// +bool CScEngine::addBreakpoint(const char *scriptFilename, int line) { + if (!_gameRef->getDebugMgr()->_enabled) return STATUS_OK; + + CScBreakpoint *bp = NULL; + for (int i = 0; i < _breakpoints.getSize(); i++) { + if (scumm_stricmp(_breakpoints[i]->_filename.c_str(), scriptFilename) == 0) { + bp = _breakpoints[i]; + break; + } + } + if (bp == NULL) { + bp = new CScBreakpoint(scriptFilename); + _breakpoints.add(bp); + } + + for (int i = 0; i < bp->_lines.getSize(); i++) { + if (bp->_lines[i] == line) return STATUS_OK; + } + bp->_lines.add(line); + + // refresh changes + refreshScriptBreakpoints(); + + return STATUS_OK; +} + +////////////////////////////////////////////////////////////////////////// +bool CScEngine::removeBreakpoint(const char *scriptFilename, int line) { + if (!_gameRef->getDebugMgr()->_enabled) return STATUS_OK; + + for (int i = 0; i < _breakpoints.getSize(); i++) { + if (scumm_stricmp(_breakpoints[i]->_filename.c_str(), scriptFilename) == 0) { + for (int j = 0; j < _breakpoints[i]->_lines.getSize(); j++) { + if (_breakpoints[i]->_lines[j] == line) { + _breakpoints[i]->_lines.removeAt(j); + if (_breakpoints[i]->_lines.getSize() == 0) { + delete _breakpoints[i]; + _breakpoints.removeAt(i); + } + // refresh changes + refreshScriptBreakpoints(); + + return STATUS_OK; + } + } + break; + } + } + return STATUS_FAILED; +} + +////////////////////////////////////////////////////////////////////////// +bool CScEngine::refreshScriptBreakpoints() { + if (!_gameRef->getDebugMgr()->_enabled) return STATUS_OK; + + for (int i = 0; i < _scripts.getSize(); i++) { + refreshScriptBreakpoints(_scripts[i]); + } + return STATUS_OK; +} + +////////////////////////////////////////////////////////////////////////// +bool CScEngine::refreshScriptBreakpoints(CScScript *script) { + if (!_gameRef->getDebugMgr()->_enabled) return STATUS_OK; + + if (!script || !script->_filename) return STATUS_FAILED; + + for (int i = 0; i < _breakpoints.getSize(); i++) { + if (scumm_stricmp(_breakpoints[i]->_filename.c_str(), script->_filename) == 0) { + script->_breakpoints.copy(_breakpoints[i]->_lines); + return STATUS_OK; + } + } + if (script->_breakpoints.getSize() > 0) script->_breakpoints.removeAll(); + + return STATUS_OK; +} + +////////////////////////////////////////////////////////////////////////// +bool CScEngine::saveBreakpoints() { + if (!_gameRef->getDebugMgr()->_enabled) return STATUS_OK; + + + char text[512]; + char key[100]; + + int count = 0; + for (int i = 0; i < _breakpoints.getSize(); i++) { + for (int j = 0; j < _breakpoints[i]->_lines.getSize(); j++) { + count++; + sprintf(key, "Breakpoint%d", count); + sprintf(text, "%s:%d", _breakpoints[i]->_filename.c_str(), _breakpoints[i]->_lines[j]); + + _gameRef->_registry->writeString("Debug", key, text); + } + } + _gameRef->_registry->writeInt("Debug", "NumBreakpoints", count); + + return STATUS_OK; +} + +////////////////////////////////////////////////////////////////////////// +bool CScEngine::loadBreakpoints() { + if (!_gameRef->getDebugMgr()->_enabled) return STATUS_OK; + + char key[100]; + + int count = _gameRef->_registry->readInt("Debug", "NumBreakpoints", 0); + for (int i = 1; i <= count; i++) { + /* uint32 BufSize = 512; */ + sprintf(key, "Breakpoint%d", i); + AnsiString breakpoint = _gameRef->_registry->readString("Debug", key, ""); + + char *path = CBUtils::strEntry(0, breakpoint.c_str(), ':'); + char *line = CBUtils::strEntry(1, breakpoint.c_str(), ':'); + + if (path != NULL && line != NULL) addBreakpoint(path, atoi(line)); + delete[] path; + delete[] line; + path = NULL; + line = NULL; + } + + return STATUS_OK; +} + + +////////////////////////////////////////////////////////////////////////// +void CScEngine::addScriptTime(const char *filename, uint32 time) { + if (!_isProfiling) return; + + AnsiString fileName = filename; + fileName.toLowercase(); + _scriptTimes[fileName] += time; +} + + +////////////////////////////////////////////////////////////////////////// +void CScEngine::enableProfiling() { + if (_isProfiling) return; + + // destroy old data, if any + _scriptTimes.clear(); + + _profilingStartTime = CBPlatform::getTime(); + _isProfiling = true; +} + + +////////////////////////////////////////////////////////////////////////// +void CScEngine::disableProfiling() { + if (!_isProfiling) return; + + dumpStats(); + _isProfiling = false; +} + + +////////////////////////////////////////////////////////////////////////// +void CScEngine::dumpStats() { + error("DumpStats not ported to ScummVM yet"); + /* uint32 totalTime = CBPlatform::getTime() - _profilingStartTime; + + typedef std::vector > TimeVector; + TimeVector times; + + ScriptTimes::iterator it; + for (it = _scriptTimes.begin(); it != _scriptTimes.end(); it++) { + times.push_back(std::pair (it->_value, it->_key)); + } + std::sort(times.begin(), times.end()); + + + TimeVector::reverse_iterator tit; + + _gameRef->LOG(0, "***** Script profiling information: *****"); + _gameRef->LOG(0, " %-40s %fs", "Total execution time", (float)totalTime / 1000); + + for (tit = times.rbegin(); tit != times.rend(); tit++) { + _gameRef->LOG(0, " %-40s %fs (%f%%)", tit->second.c_str(), (float)tit->first / 1000, (float)tit->first / (float)totalTime * 100); + }*/ +} + +} // end of namespace WinterMute diff --git a/engines/wintermute/base/scriptables/ScEngine.h b/engines/wintermute/base/scriptables/ScEngine.h new file mode 100644 index 0000000000..df327d800c --- /dev/null +++ b/engines/wintermute/base/scriptables/ScEngine.h @@ -0,0 +1,147 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +/* + * This file is based on WME Lite. + * http://dead-code.org/redir.php?target=wmelite + * Copyright (c) 2011 Jan Nedoma + */ + +#ifndef WINTERMUTE_SCENGINE_H +#define WINTERMUTE_SCENGINE_H + +#include "engines/wintermute/persistent.h" +#include "engines/wintermute/coll_templ.h" +#include "engines/wintermute/base/BBase.h" +#include "engines/wintermute/wme_debugger.h" +#include "engines/wintermute/utils/utils.h" +#include "engines/wintermute/PlatformSDL.h" + +namespace WinterMute { + +#define MAX_CACHED_SCRIPTS 20 +class CScScript; +class CScValue; +class CBObject; +class CBScriptHolder; +class CScEngine : public CBBase { +public: + class CScCachedScript { + public: + CScCachedScript(const char *filename, byte *buffer, uint32 size) { + _timestamp = CBPlatform::getTime(); + _buffer = new byte[size]; + if (_buffer) memcpy(_buffer, buffer, size); + _size = size; + _filename = filename; + }; + + ~CScCachedScript() { + if (_buffer) delete [] _buffer; + }; + + uint32 _timestamp; + byte *_buffer; + uint32 _size; + Common::String _filename; + }; + + class CScBreakpoint { + public: + CScBreakpoint(const char *filename) { + _filename = filename; + } + + ~CScBreakpoint() { + _lines.removeAll(); + } + + Common::String _filename; + CBArray _lines; + }; + + + + +public: + bool dbgSendScripts(IWmeDebugClient *client); + + CBArray _breakpoints; + bool addBreakpoint(const char *scriptFilename, int line); + bool removeBreakpoint(const char *scriptFilename, int line); + bool refreshScriptBreakpoints(); + bool refreshScriptBreakpoints(CScScript *script); + bool saveBreakpoints(); + bool loadBreakpoints(); + + bool clearGlobals(bool includingNatives = false); + bool tickUnbreakable(); + bool removeFinishedScripts(); + bool isValidScript(CScScript *script); + + CScScript *_currentScript; + bool resumeAll(); + bool pauseAll(); + void editorCleanup(); + bool resetObject(CBObject *Object); + bool resetScript(CScScript *script); + bool emptyScriptCache(); + byte *getCompiledScript(const char *filename, uint32 *outSize, bool ignoreCache = false); + DECLARE_PERSISTENT(CScEngine, CBBase) + bool cleanup(); + int getNumScripts(int *running = NULL, int *waiting = NULL, int *persistent = NULL); + bool tick(); + CScValue *_globals; + CScScript *runScript(const char *filename, CBScriptHolder *owner = NULL); + static const bool _compilerAvailable = false; + + CScEngine(CBGame *inGame); + virtual ~CScEngine(); + static byte *loadFile(void *data, char *filename, uint32 *size); + static void closeFile(void *data, byte *buffer); + static void parseElement(void *data, int line, int type, void *elementData); + + CBArray _scripts; + + void enableProfiling(); + void disableProfiling(); + bool getIsProfiling() { + return _isProfiling; + } + + void addScriptTime(const char *filename, uint32 Time); + void dumpStats(); + +private: + + CScCachedScript *_cachedScripts[MAX_CACHED_SCRIPTS]; + bool _isProfiling; + uint32 _profilingStartTime; + + typedef Common::HashMap ScriptTimes; + ScriptTimes _scriptTimes; + +}; + +} // end of namespace WinterMute + +#endif diff --git a/engines/wintermute/base/scriptables/ScScript.cpp b/engines/wintermute/base/scriptables/ScScript.cpp new file mode 100644 index 0000000000..0b5b3c24bf --- /dev/null +++ b/engines/wintermute/base/scriptables/ScScript.cpp @@ -0,0 +1,1461 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +/* + * This file is based on WME Lite. + * http://dead-code.org/redir.php?target=wmelite + * Copyright (c) 2011 Jan Nedoma + */ + +#include "engines/wintermute/dcgf.h" +#include "engines/wintermute/base/scriptables/ScValue.h" +#include "engines/wintermute/base/scriptables/ScScript.h" +#include "engines/wintermute/base/BGame.h" +#include "engines/wintermute/base/scriptables/ScEngine.h" +#include "engines/wintermute/base/scriptables/ScStack.h" +#include "common/memstream.h" + +namespace WinterMute { + +IMPLEMENT_PERSISTENT(CScScript, false) + +////////////////////////////////////////////////////////////////////////// +CScScript::CScScript(CBGame *inGame, CScEngine *Engine): CBBase(inGame) { + _buffer = NULL; + _bufferSize = _iP = 0; + _scriptStream = NULL; + _filename = NULL; + _currentLine = 0; + + _symbols = NULL; + _numSymbols = 0; + + _engine = Engine; + + _globals = NULL; + + _scopeStack = NULL; + _callStack = NULL; + _thisStack = NULL; + _stack = NULL; + + _operand = NULL; + _reg1 = NULL; + + _functions = NULL; + _numFunctions = 0; + + _methods = NULL; + _numMethods = 0; + + _events = NULL; + _numEvents = 0; + + _externals = NULL; + _numExternals = 0; + + _state = SCRIPT_FINISHED; + _origState = SCRIPT_FINISHED; + + _waitObject = NULL; + _waitTime = 0; + _waitFrozen = false; + _waitScript = NULL; + + _timeSlice = 0; + + _thread = false; + _methodThread = false; + _threadEvent = NULL; + + _freezable = true; + _owner = NULL; + + _unbreakable = false; + _parentScript = NULL; + + _tracingMode = false; +} + + +////////////////////////////////////////////////////////////////////////// +CScScript::~CScScript() { + cleanup(); +} + +void CScScript::readHeader() { + uint32 oldPos = _scriptStream->pos(); + _scriptStream->seek(0); + _header.magic = _scriptStream->readUint32LE(); + _header.version = _scriptStream->readUint32LE(); + _header.code_start = _scriptStream->readUint32LE(); + _header.func_table = _scriptStream->readUint32LE(); + _header.symbol_table = _scriptStream->readUint32LE(); + _header.event_table = _scriptStream->readUint32LE(); + _header.externals_table = _scriptStream->readUint32LE(); + _header.method_table = _scriptStream->readUint32LE(); + _scriptStream->seek(oldPos); +} + + +////////////////////////////////////////////////////////////////////////// +bool CScScript::initScript() { + if (!_scriptStream) { + _scriptStream = new Common::MemoryReadStream(_buffer, _bufferSize); + } + readHeader(); + + if (_header.magic != SCRIPT_MAGIC) { + _gameRef->LOG(0, "File '%s' is not a valid compiled script", _filename); + cleanup(); + return STATUS_FAILED; + } + + if (_header.version > SCRIPT_VERSION) { + _gameRef->LOG(0, "Script '%s' has a wrong version %d.%d (expected %d.%d)", _filename, _header.version / 256, _header.version % 256, SCRIPT_VERSION / 256, SCRIPT_VERSION % 256); + cleanup(); + return STATUS_FAILED; + } + + initTables(); + + // init stacks + _scopeStack = new CScStack(_gameRef); + _callStack = new CScStack(_gameRef); + _thisStack = new CScStack(_gameRef); + _stack = new CScStack(_gameRef); + + _operand = new CScValue(_gameRef); + _reg1 = new CScValue(_gameRef); + + + // skip to the beginning + _iP = _header.code_start; + _scriptStream->seek(_iP); + _currentLine = 0; + + // init breakpoints + _engine->refreshScriptBreakpoints(this); + + + // ready to rumble... + _state = SCRIPT_RUNNING; + + return STATUS_OK; +} + + +////////////////////////////////////////////////////////////////////////// +bool CScScript::initTables() { + uint32 OrigIP = _iP; + + readHeader(); + // load symbol table + _iP = _header.symbol_table; + + _numSymbols = getDWORD(); + _symbols = new char*[_numSymbols]; + for (uint32 i = 0; i < _numSymbols; i++) { + uint32 index = getDWORD(); + _symbols[index] = getString(); + } + + // load functions table + _iP = _header.func_table; + + _numFunctions = getDWORD(); + _functions = new TFunctionPos[_numFunctions]; + for (uint32 i = 0; i < _numFunctions; i++) { + _functions[i].pos = getDWORD(); + _functions[i].name = getString(); + } + + + // load events table + _iP = _header.event_table; + + _numEvents = getDWORD(); + _events = new TEventPos[_numEvents]; + for (uint32 i = 0; i < _numEvents; i++) { + _events[i].pos = getDWORD(); + _events[i].name = getString(); + } + + + // load externals + if (_header.version >= 0x0101) { + _iP = _header.externals_table; + + _numExternals = getDWORD(); + _externals = new TExternalFunction[_numExternals]; + for (uint32 i = 0; i < _numExternals; i++) { + _externals[i].dll_name = getString(); + _externals[i].name = getString(); + _externals[i].call_type = (TCallType)getDWORD(); + _externals[i].returns = (TExternalType)getDWORD(); + _externals[i].nu_params = getDWORD(); + if (_externals[i].nu_params > 0) { + _externals[i].params = new TExternalType[_externals[i].nu_params]; + for (int j = 0; j < _externals[i].nu_params; j++) { + _externals[i].params[j] = (TExternalType)getDWORD(); + } + } + } + } + + // load method table + _iP = _header.method_table; + + _numMethods = getDWORD(); + _methods = new TMethodPos[_numMethods]; + for (uint32 i = 0; i < _numMethods; i++) { + _methods[i].pos = getDWORD(); + _methods[i].name = getString(); + } + + + _iP = OrigIP; + + return STATUS_OK; +} + + +////////////////////////////////////////////////////////////////////////// +bool CScScript::create(const char *filename, byte *buffer, uint32 size, CBScriptHolder *owner) { + cleanup(); + + _thread = false; + _methodThread = false; + + delete[] _threadEvent; + _threadEvent = NULL; + + _filename = new char[strlen(filename) + 1]; + if (_filename) strcpy(_filename, filename); + + _buffer = new byte [size]; + if (!_buffer) return STATUS_FAILED; + + memcpy(_buffer, buffer, size); + + _bufferSize = size; + + bool res = initScript(); + if (DID_FAIL(res)) return res; + + // establish global variables table + _globals = new CScValue(_gameRef); + + _owner = owner; + + return STATUS_OK; +} + + +////////////////////////////////////////////////////////////////////////// +bool CScScript::createThread(CScScript *original, uint32 initIP, const char *eventName) { + cleanup(); + + _thread = true; + _methodThread = false; + _threadEvent = new char[strlen(eventName) + 1]; + if (_threadEvent) strcpy(_threadEvent, eventName); + + // copy filename + _filename = new char[strlen(original->_filename) + 1]; + if (_filename) strcpy(_filename, original->_filename); + + // copy buffer + _buffer = new byte [original->_bufferSize]; + if (!_buffer) return STATUS_FAILED; + + memcpy(_buffer, original->_buffer, original->_bufferSize); + _bufferSize = original->_bufferSize; + + // initialize + bool res = initScript(); + if (DID_FAIL(res)) return res; + + // copy globals + _globals = original->_globals; + + // skip to the beginning of the event + _iP = initIP; + _scriptStream->seek(_iP); + + _timeSlice = original->_timeSlice; + _freezable = original->_freezable; + _owner = original->_owner; + + _engine = original->_engine; + _parentScript = original; + + return STATUS_OK; +} + + + + +////////////////////////////////////////////////////////////////////////// +bool CScScript::createMethodThread(CScScript *original, const char *methodName) { + uint32 ip = original->getMethodPos(methodName); + if (ip == 0) return STATUS_FAILED; + + cleanup(); + + _thread = true; + _methodThread = true; + _threadEvent = new char[strlen(methodName) + 1]; + if (_threadEvent) strcpy(_threadEvent, methodName); + + // copy filename + _filename = new char[strlen(original->_filename) + 1]; + if (_filename) strcpy(_filename, original->_filename); + + // copy buffer + _buffer = new byte [original->_bufferSize]; + if (!_buffer) return STATUS_FAILED; + + memcpy(_buffer, original->_buffer, original->_bufferSize); + _bufferSize = original->_bufferSize; + + // initialize + bool res = initScript(); + if (DID_FAIL(res)) return res; + + // copy globals + _globals = original->_globals; + + // skip to the beginning of the event + _iP = ip; + + _timeSlice = original->_timeSlice; + _freezable = original->_freezable; + _owner = original->_owner; + + _engine = original->_engine; + _parentScript = original; + + return STATUS_OK; +} + + +////////////////////////////////////////////////////////////////////////// +void CScScript::cleanup() { + if (_buffer) delete [] _buffer; + _buffer = NULL; + + if (_filename) delete [] _filename; + _filename = NULL; + + if (_symbols) delete [] _symbols; + _symbols = NULL; + _numSymbols = 0; + + if (_globals && !_thread) delete _globals; + _globals = NULL; + + delete _scopeStack; + _scopeStack = NULL; + + delete _callStack; + _callStack = NULL; + + delete _thisStack; + _thisStack = NULL; + + delete _stack; + _stack = NULL; + + if (_functions) delete [] _functions; + _functions = NULL; + _numFunctions = 0; + + if (_methods) delete [] _methods; + _methods = NULL; + _numMethods = 0; + + if (_events) delete [] _events; + _events = NULL; + _numEvents = 0; + + + if (_externals) { + for (uint32 i = 0; i < _numExternals; i++) { + if (_externals[i].nu_params > 0) delete [] _externals[i].params; + } + delete [] _externals; + } + _externals = NULL; + _numExternals = 0; + + delete _operand; + delete _reg1; + _operand = NULL; + _reg1 = NULL; + + delete[] _threadEvent; + _threadEvent = NULL; + + _state = SCRIPT_FINISHED; + + _waitObject = NULL; + _waitTime = 0; + _waitFrozen = false; + _waitScript = NULL; + + _parentScript = NULL; // ref only + + delete _scriptStream; +} + + +////////////////////////////////////////////////////////////////////////// +uint32 CScScript::getDWORD() { + _scriptStream->seek((int32)_iP); + uint32 ret = _scriptStream->readUint32LE(); + _iP += sizeof(uint32); +// assert(oldRet == ret); + return ret; +} + +////////////////////////////////////////////////////////////////////////// +double CScScript::getFloat() { + _scriptStream->seek((int32)_iP); + byte buffer[8]; + _scriptStream->read(buffer, 8); + +#ifdef SCUMM_BIG_ENDIAN + // TODO: For lack of a READ_LE_UINT64 + SWAP(buffer[0], buffer[7]); + SWAP(buffer[1], buffer[6]); + SWAP(buffer[2], buffer[5]); + SWAP(buffer[3], buffer[4]); +#endif + + double ret = *(double *)(buffer); + _iP += 8; // Hardcode the double-size used originally. + return ret; +} + + +////////////////////////////////////////////////////////////////////////// +char *CScScript::getString() { + char *ret = (char *)(_buffer + _iP); + while (*(char *)(_buffer + _iP) != '\0') _iP++; + _iP++; // string terminator + _scriptStream->seek(_iP); + + return ret; +} + + +////////////////////////////////////////////////////////////////////////// +bool CScScript::executeInstruction() { + bool ret = STATUS_OK; + + uint32 dw; + const char *str = NULL; + + //CScValue* op = new CScValue(_gameRef); + _operand->cleanup(); + + CScValue *op1; + CScValue *op2; + + uint32 inst = getDWORD(); + switch (inst) { + + case II_DEF_VAR: + _operand->setNULL(); + dw = getDWORD(); + if (_scopeStack->_sP < 0) { + _globals->setProp(_symbols[dw], _operand); + if (_gameRef->getDebugMgr()->_enabled) + _gameRef->getDebugMgr()->onVariableInit(WME_DBGVAR_SCRIPT, this, NULL, _globals->getProp(_symbols[dw]), _symbols[dw]); + } else { + _scopeStack->getTop()->setProp(_symbols[dw], _operand); + if (_gameRef->getDebugMgr()->_enabled) + _gameRef->getDebugMgr()->onVariableInit(WME_DBGVAR_SCOPE, this, _scopeStack->getTop(), _scopeStack->getTop()->getProp(_symbols[dw]), _symbols[dw]); + } + + break; + + case II_DEF_GLOB_VAR: + case II_DEF_CONST_VAR: { + dw = getDWORD(); + /* char *Temp = _symbols[dw]; // TODO delete */ + // only create global var if it doesn't exist + if (!_engine->_globals->propExists(_symbols[dw])) { + _operand->setNULL(); + _engine->_globals->setProp(_symbols[dw], _operand, false, inst == II_DEF_CONST_VAR); + + if (_gameRef->getDebugMgr()->_enabled) + _gameRef->getDebugMgr()->onVariableInit(WME_DBGVAR_GLOBAL, this, NULL, _engine->_globals->getProp(_symbols[dw]), _symbols[dw]); + } + break; + } + + case II_RET: + if (_scopeStack->_sP >= 0 && _callStack->_sP >= 0) { + _gameRef->getDebugMgr()->onScriptShutdownScope(this, _scopeStack->getTop()); + + _scopeStack->pop(); + _iP = (uint32)_callStack->pop()->getInt(); + + if (_scopeStack->_sP < 0) _gameRef->getDebugMgr()->onScriptChangeScope(this, NULL); + else _gameRef->getDebugMgr()->onScriptChangeScope(this, _scopeStack->getTop()); + } else { + if (_thread) { + _state = SCRIPT_THREAD_FINISHED; + } else { + if (_numEvents == 0 && _numMethods == 0) _state = SCRIPT_FINISHED; + else _state = SCRIPT_PERSISTENT; + } + } + + break; + + case II_RET_EVENT: + _state = SCRIPT_FINISHED; + break; + + + case II_CALL: + dw = getDWORD(); + + _operand->setInt(_iP); + _callStack->push(_operand); + + _iP = dw; + + break; + + case II_CALL_BY_EXP: { + // push var + // push string + str = _stack->pop()->getString(); + char *MethodName = new char[strlen(str) + 1]; + strcpy(MethodName, str); + + CScValue *var = _stack->pop(); + if (var->_type == VAL_VARIABLE_REF) var = var->_valRef; + + bool res = STATUS_FAILED; + bool TriedNative = false; + + // we are already calling this method, try native + if (_thread && _methodThread && strcmp(MethodName, _threadEvent) == 0 && var->_type == VAL_NATIVE && _owner == var->getNative()) { + TriedNative = true; + res = var->_valNative->scCallMethod(this, _stack, _thisStack, MethodName); + } + + if (DID_FAIL(res)) { + if (var->isNative() && var->getNative()->canHandleMethod(MethodName)) { + if (!_unbreakable) { + _waitScript = var->getNative()->invokeMethodThread(MethodName); + if (!_waitScript) { + _stack->correctParams(0); + runtimeError("Error invoking method '%s'.", MethodName); + _stack->pushNULL(); + } else { + _state = SCRIPT_WAITING_SCRIPT; + _waitScript->copyParameters(_stack); + } + } else { + // can call methods in unbreakable mode + _stack->correctParams(0); + runtimeError("Cannot call method '%s'. Ignored.", MethodName); + _stack->pushNULL(); + } + delete [] MethodName; + break; + } + /* + CScValue* val = var->getProp(MethodName); + if(val){ + dw = GetFuncPos(val->getString()); + if(dw==0){ + TExternalFunction* f = GetExternal(val->getString()); + if(f){ + ExternalCall(_stack, _thisStack, f); + } + else{ + // not an internal nor external, try for native function + _gameRef->ExternalCall(this, _stack, _thisStack, val->getString()); + } + } + else{ + _operand->setInt(_iP); + _callStack->Push(_operand); + _iP = dw; + } + } + */ + else { + res = STATUS_FAILED; + if (var->_type == VAL_NATIVE && !TriedNative) res = var->_valNative->scCallMethod(this, _stack, _thisStack, MethodName); + + if (DID_FAIL(res)) { + _stack->correctParams(0); + runtimeError("Call to undefined method '%s'. Ignored.", MethodName); + _stack->pushNULL(); + } + } + } + delete [] MethodName; + } + break; + + case II_EXTERNAL_CALL: { + uint32 SymbolIndex = getDWORD(); + + TExternalFunction *f = getExternal(_symbols[SymbolIndex]); + if (f) { + externalCall(_stack, _thisStack, f); + } else _gameRef->ExternalCall(this, _stack, _thisStack, _symbols[SymbolIndex]); + + break; + } + case II_SCOPE: + _operand->setNULL(); + _scopeStack->push(_operand); + + if (_scopeStack->_sP < 0) _gameRef->getDebugMgr()->onScriptChangeScope(this, NULL); + else _gameRef->getDebugMgr()->onScriptChangeScope(this, _scopeStack->getTop()); + + break; + + case II_CORRECT_STACK: + dw = getDWORD(); // params expected + _stack->correctParams(dw); + break; + + case II_CREATE_OBJECT: + _operand->setObject(); + _stack->push(_operand); + break; + + case II_POP_EMPTY: + _stack->pop(); + break; + + case II_PUSH_VAR: { + CScValue *var = getVar(_symbols[getDWORD()]); + if (false && /*var->_type==VAL_OBJECT ||*/ var->_type == VAL_NATIVE) { + _operand->setReference(var); + _stack->push(_operand); + } else _stack->push(var); + break; + } + + case II_PUSH_VAR_REF: { + CScValue *var = getVar(_symbols[getDWORD()]); + _operand->setReference(var); + _stack->push(_operand); + break; + } + + case II_POP_VAR: { + char *VarName = _symbols[getDWORD()]; + CScValue *var = getVar(VarName); + if (var) { + CScValue *val = _stack->pop(); + if (!val) { + runtimeError("Script stack corruption detected. Please report this script at WME bug reports forum."); + var->setNULL(); + } else { + if (val->getType() == VAL_VARIABLE_REF) val = val->_valRef; + if (val->_type == VAL_NATIVE) var->setValue(val); + else { + var->copy(val); + } + } + + if (_gameRef->getDebugMgr()->_enabled) + _gameRef->getDebugMgr()->onVariableChangeValue(var, val); + } + + break; + } + + case II_PUSH_VAR_THIS: + _stack->push(_thisStack->getTop()); + break; + + case II_PUSH_INT: + _stack->pushInt((int)getDWORD()); + break; + + case II_PUSH_FLOAT: + _stack->pushFloat(getFloat()); + break; + + + case II_PUSH_BOOL: + _stack->pushBool(getDWORD() != 0); + + break; + + case II_PUSH_STRING: + _stack->pushString(getString()); + break; + + case II_PUSH_NULL: + _stack->pushNULL(); + break; + + case II_PUSH_THIS_FROM_STACK: + _operand->setReference(_stack->getTop()); + _thisStack->push(_operand); + break; + + case II_PUSH_THIS: + _operand->setReference(getVar(_symbols[getDWORD()])); + _thisStack->push(_operand); + break; + + case II_POP_THIS: + _thisStack->pop(); + break; + + case II_PUSH_BY_EXP: { + str = _stack->pop()->getString(); + CScValue *val = _stack->pop()->getProp(str); + if (val) _stack->push(val); + else _stack->pushNULL(); + + break; + } + + case II_POP_BY_EXP: { + str = _stack->pop()->getString(); + CScValue *var = _stack->pop(); + CScValue *val = _stack->pop(); + + if (val == NULL) { + runtimeError("Script stack corruption detected. Please report this script at WME bug reports forum."); + var->setNULL(); + } else var->setProp(str, val); + + if (_gameRef->getDebugMgr()->_enabled) + _gameRef->getDebugMgr()->onVariableChangeValue(var, NULL); + + break; + } + + case II_PUSH_REG1: + _stack->push(_reg1); + break; + + case II_POP_REG1: + _reg1->copy(_stack->pop()); + break; + + case II_JMP: + _iP = getDWORD(); + break; + + case II_JMP_FALSE: { + dw = getDWORD(); + //if(!_stack->pop()->getBool()) _iP = dw; + CScValue *val = _stack->pop(); + if (!val) { + runtimeError("Script corruption detected. Did you use '=' instead of '==' for comparison?"); + } else { + if (!val->getBool()) _iP = dw; + } + break; + } + + case II_ADD: + op2 = _stack->pop(); + op1 = _stack->pop(); + + if (op1->isNULL() || op2->isNULL()) + _operand->setNULL(); + else if (op1->getType() == VAL_STRING || op2->getType() == VAL_STRING) { + char *tempStr = new char [strlen(op1->getString()) + strlen(op2->getString()) + 1]; + strcpy(tempStr, op1->getString()); + strcat(tempStr, op2->getString()); + _operand->setString(tempStr); + delete [] tempStr; + } else if (op1->getType() == VAL_INT && op2->getType() == VAL_INT) + _operand->setInt(op1->getInt() + op2->getInt()); + else _operand->setFloat(op1->getFloat() + op2->getFloat()); + + _stack->push(_operand); + + break; + + case II_SUB: + op2 = _stack->pop(); + op1 = _stack->pop(); + + if (op1->isNULL() || op2->isNULL()) + _operand->setNULL(); + else if (op1->getType() == VAL_INT && op2->getType() == VAL_INT) + _operand->setInt(op1->getInt() - op2->getInt()); + else _operand->setFloat(op1->getFloat() - op2->getFloat()); + + _stack->push(_operand); + + break; + + case II_MUL: + op2 = _stack->pop(); + op1 = _stack->pop(); + + if (op1->isNULL() || op2->isNULL()) _operand->setNULL(); + else if (op1->getType() == VAL_INT && op2->getType() == VAL_INT) + _operand->setInt(op1->getInt() * op2->getInt()); + else _operand->setFloat(op1->getFloat() * op2->getFloat()); + + _stack->push(_operand); + + break; + + case II_DIV: + op2 = _stack->pop(); + op1 = _stack->pop(); + + if (op2->getFloat() == 0.0f) + runtimeError("Division by zero."); + + if (op1->isNULL() || op2->isNULL() || op2->getFloat() == 0.0f) _operand->setNULL(); + else _operand->setFloat(op1->getFloat() / op2->getFloat()); + + _stack->push(_operand); + + break; + + case II_MODULO: + op2 = _stack->pop(); + op1 = _stack->pop(); + + if (op2->getInt() == 0) + runtimeError("Division by zero."); + + if (op1->isNULL() || op2->isNULL() || op2->getInt() == 0) + _operand->setNULL(); + else _operand->setInt(op1->getInt() % op2->getInt()); + + _stack->push(_operand); + + break; + + case II_NOT: + op1 = _stack->pop(); + //if(op1->isNULL()) _operand->setNULL(); + if (op1->isNULL()) _operand->setBool(true); + else _operand->setBool(!op1->getBool()); + _stack->push(_operand); + + break; + + case II_AND: + op2 = _stack->pop(); + op1 = _stack->pop(); + if (op1 == NULL || op2 == NULL) { + runtimeError("Script corruption detected. Did you use '=' instead of '==' for comparison?"); + _operand->setBool(false); + } else { + _operand->setBool(op1->getBool() && op2->getBool()); + } + _stack->push(_operand); + break; + + case II_OR: + op2 = _stack->pop(); + op1 = _stack->pop(); + if (op1 == NULL || op2 == NULL) { + runtimeError("Script corruption detected. Did you use '=' instead of '==' for comparison?"); + _operand->setBool(false); + } else { + _operand->setBool(op1->getBool() || op2->getBool()); + } + _stack->push(_operand); + break; + + case II_CMP_EQ: + op2 = _stack->pop(); + op1 = _stack->pop(); + + /* + if((op1->isNULL() && !op2->isNULL()) || (!op1->isNULL() && op2->isNULL())) _operand->setBool(false); + else if(op1->isNative() && op2->isNative()){ + _operand->setBool(op1->getNative() == op2->getNative()); + } + else if(op1->getType()==VAL_STRING || op2->getType()==VAL_STRING){ + _operand->setBool(scumm_stricmp(op1->getString(), op2->getString())==0); + } + else if(op1->getType()==VAL_FLOAT && op2->getType()==VAL_FLOAT){ + _operand->setBool(op1->getFloat() == op2->getFloat()); + } + else{ + _operand->setBool(op1->getInt() == op2->getInt()); + } + */ + + _operand->setBool(CScValue::compare(op1, op2) == 0); + _stack->push(_operand); + break; + + case II_CMP_NE: + op2 = _stack->pop(); + op1 = _stack->pop(); + + /* + if((op1->isNULL() && !op2->isNULL()) || (!op1->isNULL() && op2->isNULL())) _operand->setBool(true); + else if(op1->isNative() && op2->isNative()){ + _operand->setBool(op1->getNative() != op2->getNative()); + } + else if(op1->getType()==VAL_STRING || op2->getType()==VAL_STRING){ + _operand->setBool(scumm_stricmp(op1->getString(), op2->getString())!=0); + } + else if(op1->getType()==VAL_FLOAT && op2->getType()==VAL_FLOAT){ + _operand->setBool(op1->getFloat() != op2->getFloat()); + } + else{ + _operand->setBool(op1->getInt() != op2->getInt()); + } + */ + + _operand->setBool(CScValue::compare(op1, op2) != 0); + _stack->push(_operand); + break; + + case II_CMP_L: + op2 = _stack->pop(); + op1 = _stack->pop(); + + /* + if(op1->getType()==VAL_FLOAT && op2->getType()==VAL_FLOAT){ + _operand->setBool(op1->getFloat() < op2->getFloat()); + } + else _operand->setBool(op1->getInt() < op2->getInt()); + */ + + _operand->setBool(CScValue::compare(op1, op2) < 0); + _stack->push(_operand); + break; + + case II_CMP_G: + op2 = _stack->pop(); + op1 = _stack->pop(); + + /* + if(op1->getType()==VAL_FLOAT && op2->getType()==VAL_FLOAT){ + _operand->setBool(op1->getFloat() > op2->getFloat()); + } + else _operand->setBool(op1->getInt() > op2->getInt()); + */ + + _operand->setBool(CScValue::compare(op1, op2) > 0); + _stack->push(_operand); + break; + + case II_CMP_LE: + op2 = _stack->pop(); + op1 = _stack->pop(); + + /* + if(op1->getType()==VAL_FLOAT && op2->getType()==VAL_FLOAT){ + _operand->setBool(op1->getFloat() <= op2->getFloat()); + } + else _operand->setBool(op1->getInt() <= op2->getInt()); + */ + + _operand->setBool(CScValue::compare(op1, op2) <= 0); + _stack->push(_operand); + break; + + case II_CMP_GE: + op2 = _stack->pop(); + op1 = _stack->pop(); + + /* + if(op1->getType()==VAL_FLOAT && op2->getType()==VAL_FLOAT){ + _operand->setBool(op1->getFloat() >= op2->getFloat()); + } + else _operand->setBool(op1->getInt() >= op2->getInt()); + */ + + _operand->setBool(CScValue::compare(op1, op2) >= 0); + _stack->push(_operand); + break; + + case II_CMP_STRICT_EQ: + op2 = _stack->pop(); + op1 = _stack->pop(); + + //_operand->setBool(op1->getType()==op2->getType() && op1->getFloat()==op2->getFloat()); + _operand->setBool(CScValue::compareStrict(op1, op2) == 0); + _stack->push(_operand); + + break; + + case II_CMP_STRICT_NE: + op2 = _stack->pop(); + op1 = _stack->pop(); + + //_operand->setBool(op1->getType()!=op2->getType() || op1->getFloat()!=op2->getFloat()); + _operand->setBool(CScValue::compareStrict(op1, op2) != 0); + _stack->push(_operand); + break; + + case II_DBG_LINE: { + int newLine = getDWORD(); + if (newLine != _currentLine) { + _currentLine = newLine; + if (_gameRef->getDebugMgr()->_enabled) { + _gameRef->getDebugMgr()->onScriptChangeLine(this, _currentLine); + for (int i = 0; i < _breakpoints.getSize(); i++) { + if (_breakpoints[i] == _currentLine) { + _gameRef->getDebugMgr()->onScriptHitBreakpoint(this); + sleep(0); + break; + } + } + if (_tracingMode) { + _gameRef->getDebugMgr()->onScriptHitBreakpoint(this); + sleep(0); + break; + } + } + } + break; + + } + default: + _gameRef->LOG(0, "Fatal: Invalid instruction %d ('%s', line %d, IP:0x%x)\n", inst, _filename, _currentLine, _iP - sizeof(uint32)); + _state = SCRIPT_FINISHED; + ret = STATUS_FAILED; + } // switch(instruction) + + //delete op; + + return ret; +} + + +////////////////////////////////////////////////////////////////////////// +uint32 CScScript::getFuncPos(const char *name) { + for (uint32 i = 0; i < _numFunctions; i++) { + if (strcmp(name, _functions[i].name) == 0) + return _functions[i].pos; + } + return 0; +} + + +////////////////////////////////////////////////////////////////////////// +uint32 CScScript::getMethodPos(const char *name) { + for (uint32 i = 0; i < _numMethods; i++) { + if (strcmp(name, _methods[i].name) == 0) + return _methods[i].pos; + } + return 0; +} + + +////////////////////////////////////////////////////////////////////////// +CScValue *CScScript::getVar(char *name) { + CScValue *ret = NULL; + + // scope locals + if (_scopeStack->_sP >= 0) { + if (_scopeStack->getTop()->propExists(name)) + ret = _scopeStack->getTop()->getProp(name); + } + + // script globals + if (ret == NULL) { + if (_globals->propExists(name)) + ret = _globals->getProp(name); + } + + // engine globals + if (ret == NULL) { + if (_engine->_globals->propExists(name)) + ret = _engine->_globals->getProp(name); + } + + if (ret == NULL) { + //RuntimeError("Variable '%s' is inaccessible in the current block. Consider changing the script.", name); + _gameRef->LOG(0, "Warning: variable '%s' is inaccessible in the current block. Consider changing the script (script:%s, line:%d)", name, _filename, _currentLine); + CScValue *val = new CScValue(_gameRef); + CScValue *scope = _scopeStack->getTop(); + if (scope) { + scope->setProp(name, val); + ret = _scopeStack->getTop()->getProp(name); + } else { + _globals->setProp(name, val); + ret = _globals->getProp(name); + } + delete val; + } + + return ret; +} + + +////////////////////////////////////////////////////////////////////////// +bool CScScript::waitFor(CBObject *object) { + if (_unbreakable) { + runtimeError("Script cannot be interrupted."); + return STATUS_OK; + } + + _state = SCRIPT_WAITING; + _waitObject = object; + return STATUS_OK; +} + + +////////////////////////////////////////////////////////////////////////// +bool CScScript::waitForExclusive(CBObject *object) { + _engine->resetObject(object); + return waitFor(object); +} + + +////////////////////////////////////////////////////////////////////////// +bool CScScript::sleep(uint32 duration) { + if (_unbreakable) { + runtimeError("Script cannot be interrupted."); + return STATUS_OK; + } + + _state = SCRIPT_SLEEPING; + if (_gameRef->_state == GAME_FROZEN) { + _waitTime = CBPlatform::getTime() + duration; + _waitFrozen = true; + } else { + _waitTime = _gameRef->_timer + duration; + _waitFrozen = false; + } + return STATUS_OK; +} + + +////////////////////////////////////////////////////////////////////////// +bool CScScript::finish(bool includingThreads) { + if (_state != SCRIPT_FINISHED && includingThreads) { + _state = SCRIPT_FINISHED; + finishThreads(); + } else _state = SCRIPT_FINISHED; + + + return STATUS_OK; +} + + +////////////////////////////////////////////////////////////////////////// +bool CScScript::run() { + _state = SCRIPT_RUNNING; + return STATUS_OK; +} + + +////////////////////////////////////////////////////////////////////// +void CScScript::runtimeError(const char *fmt, ...) { + char buff[256]; + va_list va; + + va_start(va, fmt); + vsprintf(buff, fmt, va); + va_end(va); + + _gameRef->LOG(0, "Runtime error. Script '%s', line %d", _filename, _currentLine); + _gameRef->LOG(0, " %s", buff); + + if (!_gameRef->_suppressScriptErrors) + _gameRef->quickMessage("Script runtime error. View log for details."); +} + + +////////////////////////////////////////////////////////////////////////// +bool CScScript::persist(CBPersistMgr *persistMgr) { + + persistMgr->transfer(TMEMBER(_gameRef)); + + // buffer + if (persistMgr->_saving) { + if (_state != SCRIPT_PERSISTENT && _state != SCRIPT_FINISHED && _state != SCRIPT_THREAD_FINISHED) { + persistMgr->transfer(TMEMBER(_bufferSize)); + persistMgr->putBytes(_buffer, _bufferSize); + } else { + // don't save idle/finished scripts + int bufferSize = 0; + persistMgr->transfer(TMEMBER(bufferSize)); + } + } else { + persistMgr->transfer(TMEMBER(_bufferSize)); + if (_bufferSize > 0) { + _buffer = new byte[_bufferSize]; + persistMgr->getBytes(_buffer, _bufferSize); + _scriptStream = new Common::MemoryReadStream(_buffer, _bufferSize); + initTables(); + } else { + _buffer = NULL; + _scriptStream = NULL; + } + } + + persistMgr->transfer(TMEMBER(_callStack)); + persistMgr->transfer(TMEMBER(_currentLine)); + persistMgr->transfer(TMEMBER(_engine)); + persistMgr->transfer(TMEMBER(_filename)); + persistMgr->transfer(TMEMBER(_freezable)); + persistMgr->transfer(TMEMBER(_globals)); + persistMgr->transfer(TMEMBER(_iP)); + persistMgr->transfer(TMEMBER(_scopeStack)); + persistMgr->transfer(TMEMBER(_stack)); + persistMgr->transfer(TMEMBER_INT(_state)); + persistMgr->transfer(TMEMBER(_operand)); + persistMgr->transfer(TMEMBER_INT(_origState)); + persistMgr->transfer(TMEMBER(_owner)); + persistMgr->transfer(TMEMBER(_reg1)); + persistMgr->transfer(TMEMBER(_thread)); + persistMgr->transfer(TMEMBER(_threadEvent)); + persistMgr->transfer(TMEMBER(_thisStack)); + persistMgr->transfer(TMEMBER(_timeSlice)); + persistMgr->transfer(TMEMBER(_waitObject)); + persistMgr->transfer(TMEMBER(_waitScript)); + persistMgr->transfer(TMEMBER(_waitTime)); + persistMgr->transfer(TMEMBER(_waitFrozen)); + + persistMgr->transfer(TMEMBER(_methodThread)); + persistMgr->transfer(TMEMBER(_methodThread)); + persistMgr->transfer(TMEMBER(_unbreakable)); + persistMgr->transfer(TMEMBER(_parentScript)); + + if (!persistMgr->_saving) _tracingMode = false; + + return STATUS_OK; +} + + +////////////////////////////////////////////////////////////////////////// +CScScript *CScScript::invokeEventHandler(const char *eventName, bool unbreakable) { + //if(_state!=SCRIPT_PERSISTENT) return NULL; + + uint32 pos = getEventPos(eventName); + if (!pos) return NULL; + + CScScript *thread = new CScScript(_gameRef, _engine); + if (thread) { + bool ret = thread->createThread(this, pos, eventName); + if (DID_SUCCEED(ret)) { + thread->_unbreakable = unbreakable; + _engine->_scripts.add(thread); + _gameRef->getDebugMgr()->onScriptEventThreadInit(thread, this, eventName); + return thread; + } else { + delete thread; + return NULL; + } + } else return NULL; + +} + + +////////////////////////////////////////////////////////////////////////// +uint32 CScScript::getEventPos(const char *name) { + for (int i = _numEvents - 1; i >= 0; i--) { + if (scumm_stricmp(name, _events[i].name) == 0) return _events[i].pos; + } + return 0; +} + + +////////////////////////////////////////////////////////////////////////// +bool CScScript::canHandleEvent(const char *eventName) { + return getEventPos(eventName) != 0; +} + + +////////////////////////////////////////////////////////////////////////// +bool CScScript::canHandleMethod(const char *methodName) { + return getMethodPos(methodName) != 0; +} + + +////////////////////////////////////////////////////////////////////////// +bool CScScript::pause() { + if (_state == SCRIPT_PAUSED) { + _gameRef->LOG(0, "Attempting to pause a paused script ('%s', line %d)", _filename, _currentLine); + return STATUS_FAILED; + } + + if (!_freezable || _state == SCRIPT_PERSISTENT) return STATUS_OK; + + _origState = _state; + _state = SCRIPT_PAUSED; + + return STATUS_OK; +} + + +////////////////////////////////////////////////////////////////////////// +bool CScScript::resume() { + if (_state != SCRIPT_PAUSED) return STATUS_OK; + + _state = _origState; + return STATUS_OK; +} + + +////////////////////////////////////////////////////////////////////////// +CScScript::TExternalFunction *CScScript::getExternal(char *name) { + for (uint32 i = 0; i < _numExternals; i++) { + if (strcmp(name, _externals[i].name) == 0) + return &_externals[i]; + } + return NULL; +} + + +////////////////////////////////////////////////////////////////////////// +bool CScScript::externalCall(CScStack *stack, CScStack *thisStack, CScScript::TExternalFunction *function) { + + _gameRef->LOG(0, "External functions are not supported on this platform."); + stack->correctParams(0); + stack->pushNULL(); + return STATUS_FAILED; +} + + +////////////////////////////////////////////////////////////////////////// +bool CScScript::copyParameters(CScStack *stack) { + int i; + int NumParams = stack->pop()->getInt(); + for (i = NumParams - 1; i >= 0; i--) { + _stack->push(stack->getAt(i)); + } + _stack->pushInt(NumParams); + + for (i = 0; i < NumParams; i++) stack->pop(); + + return STATUS_OK; +} + + +////////////////////////////////////////////////////////////////////////// +bool CScScript::finishThreads() { + for (int i = 0; i < _engine->_scripts.getSize(); i++) { + CScScript *scr = _engine->_scripts[i]; + if (scr->_thread && scr->_state != SCRIPT_FINISHED && scr->_owner == _owner && scumm_stricmp(scr->_filename, _filename) == 0) + scr->finish(true); + } + return STATUS_OK; +} + + +////////////////////////////////////////////////////////////////////////// +// IWmeDebugScript interface implementation +int CScScript::dbgGetLine() { + return _currentLine; +} + +////////////////////////////////////////////////////////////////////////// +const char *CScScript::dbgGetFilename() { + return _filename; +} + + +////////////////////////////////////////////////////////////////////////// +bool CScScript::dbgSendScript(IWmeDebugClient *client) { + if (_methodThread) client->onScriptMethodThreadInit(this, _parentScript, _threadEvent); + else if (_thread) client->onScriptEventThreadInit(this, _parentScript, _threadEvent); + else client->onScriptInit(this); + + return dbgSendVariables(client); + return STATUS_OK; +} + +////////////////////////////////////////////////////////////////////////// +bool CScScript::dbgSendVariables(IWmeDebugClient *client) { + // send script globals + _globals->dbgSendVariables(client, WME_DBGVAR_SCRIPT, this, 0); + + // send scope variables + if (_scopeStack->_sP >= 0) { + for (int i = 0; i <= _scopeStack->_sP; i++) { + // CScValue *Scope = _scopeStack->GetAt(i); + //Scope->DbgSendVariables(Client, WME_DBGVAR_SCOPE, this, (unsigned int)Scope); + } + } + return STATUS_OK; +} + + +////////////////////////////////////////////////////////////////////////// +TScriptState CScScript::dbgGetState() { + return _state; +} + +////////////////////////////////////////////////////////////////////////// +int CScScript::dbgGetNumBreakpoints() { + return _breakpoints.getSize(); +} + +////////////////////////////////////////////////////////////////////////// +int CScScript::dbgGetBreakpoint(int index) { + if (index >= 0 && index < _breakpoints.getSize()) return _breakpoints[index]; + else return -1; +} + +////////////////////////////////////////////////////////////////////////// +bool CScScript::dbgSetTracingMode(bool isTracing) { + _tracingMode = isTracing; + return true; +} + +////////////////////////////////////////////////////////////////////////// +bool CScScript::dbgGetTracingMode() { + return _tracingMode; +} + + +////////////////////////////////////////////////////////////////////////// +void CScScript::afterLoad() { + if (_buffer == NULL) { + byte *buffer = _engine->getCompiledScript(_filename, &_bufferSize); + if (!buffer) { + _gameRef->LOG(0, "Error reinitializing script '%s' after load. Script will be terminated.", _filename); + _state = SCRIPT_ERROR; + return; + } + + _buffer = new byte [_bufferSize]; + memcpy(_buffer, buffer, _bufferSize); + + delete _scriptStream; + _scriptStream = new Common::MemoryReadStream(_buffer, _bufferSize); + + initTables(); + } +} + +} // end of namespace WinterMute diff --git a/engines/wintermute/base/scriptables/ScScript.h b/engines/wintermute/base/scriptables/ScScript.h new file mode 100644 index 0000000000..c031f8186f --- /dev/null +++ b/engines/wintermute/base/scriptables/ScScript.h @@ -0,0 +1,183 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +/* + * This file is based on WME Lite. + * http://dead-code.org/redir.php?target=wmelite + * Copyright (c) 2011 Jan Nedoma + */ + +#ifndef WINTERMUTE_SCSCRIPT_H +#define WINTERMUTE_SCSCRIPT_H + + +#include "engines/wintermute/base/BBase.h" +#include "engines/wintermute/dcscript.h" // Added by ClassView +#include "engines/wintermute/coll_templ.h" + +#include "engines/wintermute/wme_debugger.h" + +namespace WinterMute { +class CBScriptHolder; +class CBObject; +class CScEngine; +class CScStack; +class CScScript : public CBBase, public IWmeDebugScript { +public: + bool dbgSendScript(IWmeDebugClient *client); + bool dbgSendVariables(IWmeDebugClient *client); + + CBArray _breakpoints; + bool _tracingMode; + + CScScript *_parentScript; + bool _unbreakable; + bool finishThreads(); + bool copyParameters(CScStack *stack); + + void afterLoad(); + + CScValue *_operand; + CScValue *_reg1; + bool _freezable; + bool resume(); + bool pause(); + bool canHandleEvent(const char *eventName); + bool canHandleMethod(const char *methodName); + bool createThread(CScScript *original, uint32 initIP, const char *eventName); + bool createMethodThread(CScScript *original, const char *methodName); + CScScript *invokeEventHandler(const char *eventName, bool unbreakable = false); + uint32 _timeSlice; + DECLARE_PERSISTENT(CScScript, CBBase) + void runtimeError(const char *fmt, ...); + bool run(); + bool finish(bool includingThreads = false); + bool sleep(uint32 duration); + bool waitForExclusive(CBObject *object); + bool waitFor(CBObject *object); + uint32 _waitTime; + bool _waitFrozen; + CBObject *_waitObject; + CScScript *_waitScript; + TScriptState _state; + TScriptState _origState; + CScValue *getVar(char *name); + uint32 getFuncPos(const char *name); + uint32 getEventPos(const char *name); + uint32 getMethodPos(const char *name); + typedef struct { + uint32 magic; + uint32 version; + uint32 code_start; + uint32 func_table; + uint32 symbol_table; + uint32 event_table; + uint32 externals_table; + uint32 method_table; + } TScriptHeader; + + TScriptHeader _header; + + typedef struct { + char *name; + uint32 pos; + } TFunctionPos; + + typedef struct { + char *name; + uint32 pos; + } TMethodPos; + + typedef struct { + char *name; + uint32 pos; + } TEventPos; + + typedef struct { + char *name; + char *dll_name; + TCallType call_type; + TExternalType returns; + int nu_params; + TExternalType *params; + } TExternalFunction; + + + CScStack *_callStack; + CScStack *_thisStack; + CScStack *_scopeStack; + CScStack *_stack; + CScValue *_globals; + CScEngine *_engine; + int _currentLine; + bool executeInstruction(); + char *getString(); + uint32 getDWORD(); + double getFloat(); + void cleanup(); + bool create(const char *filename, byte *buffer, uint32 size, CBScriptHolder *owner); + uint32 _iP; +private: + void readHeader(); + uint32 _bufferSize; + byte *_buffer; +public: + Common::SeekableReadStream *_scriptStream; + CScScript(CBGame *inGame, CScEngine *Engine); + virtual ~CScScript(); + char *_filename; + char **_symbols; + uint32 _numSymbols; + TFunctionPos *_functions; + TMethodPos *_methods; + TEventPos *_events; + uint32 _numExternals; + TExternalFunction *_externals; + uint32 _numFunctions; + uint32 _numMethods; + uint32 _numEvents; + bool _thread; + bool _methodThread; + char *_threadEvent; + CBScriptHolder *_owner; + CScScript::TExternalFunction *getExternal(char *name); + bool externalCall(CScStack *stack, CScStack *thisStack, CScScript::TExternalFunction *function); +private: + bool initScript(); + bool initTables(); + + +// IWmeDebugScript interface implementation +public: + virtual int dbgGetLine(); + virtual const char *dbgGetFilename(); + virtual TScriptState dbgGetState(); + virtual int dbgGetNumBreakpoints(); + virtual int dbgGetBreakpoint(int Index); + + virtual bool dbgSetTracingMode(bool IsTracing); + virtual bool dbgGetTracingMode(); +}; + +} // end of namespace WinterMute + +#endif diff --git a/engines/wintermute/base/scriptables/ScStack.cpp b/engines/wintermute/base/scriptables/ScStack.cpp new file mode 100644 index 0000000000..252cd21dda --- /dev/null +++ b/engines/wintermute/base/scriptables/ScStack.cpp @@ -0,0 +1,226 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +/* + * This file is based on WME Lite. + * http://dead-code.org/redir.php?target=wmelite + * Copyright (c) 2011 Jan Nedoma + */ + +#include "engines/wintermute/base/scriptables/ScStack.h" +#include "engines/wintermute/base/scriptables/ScValue.h" +#include "engines/wintermute/base/BGame.h" + +namespace WinterMute { + +IMPLEMENT_PERSISTENT(CScStack, false) + +////////////////////////////////////////////////////////////////////////// +CScStack::CScStack(CBGame *inGame): CBBase(inGame) { + _sP = -1; +} + + +////////////////////////////////////////////////////////////////////////// +CScStack::~CScStack() { + +#if _DEBUG + //_gameRef->LOG(0, "STAT: Stack size: %d, SP=%d", _values.getSize(), _sP); +#endif + + for (int i = 0; i < _values.getSize(); i++) { + delete _values[i]; + } + _values.removeAll(); +} + + +////////////////////////////////////////////////////////////////////////// +CScValue *CScStack::pop() { + if (_sP < 0) { + _gameRef->LOG(0, "Fatal: Stack underflow"); + return NULL; + } + + return _values[_sP--]; +} + + +////////////////////////////////////////////////////////////////////////// +void CScStack::push(CScValue *val) { + _sP++; + + if (_sP < _values.getSize()) { + _values[_sP]->cleanup(); + _values[_sP]->copy(val); + } else { + CScValue *copyVal = new CScValue(_gameRef); + copyVal->copy(val); + _values.add(copyVal); + } +} + + +////////////////////////////////////////////////////////////////////////// +CScValue *CScStack::getPushValue() { + _sP++; + + if (_sP >= _values.getSize()) { + CScValue *val = new CScValue(_gameRef); + _values.add(val); + } + _values[_sP]->cleanup(); + return _values[_sP]; +} + + + +////////////////////////////////////////////////////////////////////////// +CScValue *CScStack::getTop() { + if (_sP < 0 || _sP >= _values.getSize()) return NULL; + else return _values[_sP]; +} + + +////////////////////////////////////////////////////////////////////////// +CScValue *CScStack::getAt(int index) { + index = _sP - index; + if (index < 0 || index >= _values.getSize()) return NULL; + else return _values[index]; +} + + +////////////////////////////////////////////////////////////////////////// +void CScStack::correctParams(uint32 expectedParams) { + uint32 nuParams = (uint32)pop()->getInt(); + + if (expectedParams < nuParams) { // too many params + while (expectedParams < nuParams) { + //Pop(); + delete _values[_sP - expectedParams]; + _values.removeAt(_sP - expectedParams); + nuParams--; + _sP--; + } + } else if (expectedParams > nuParams) { // need more params + while (expectedParams > nuParams) { + //Push(null_val); + CScValue *nullVal = new CScValue(_gameRef); + nullVal->setNULL(); + _values.insertAt(_sP - nuParams + 1, nullVal); + nuParams++; + _sP++; + + if (_values.getSize() > _sP + 1) { + delete _values[_values.getSize() - 1]; + _values.removeAt(_values.getSize() - 1); + } + } + } +} + + +////////////////////////////////////////////////////////////////////////// +void CScStack::pushNULL() { + /* + CScValue* val = new CScValue(_gameRef); + val->setNULL(); + Push(val); + delete val; + */ + getPushValue()->setNULL(); +} + + +////////////////////////////////////////////////////////////////////////// +void CScStack::pushInt(int val) { + /* + CScValue* val = new CScValue(_gameRef); + val->setInt(Val); + Push(val); + delete val; + */ + getPushValue()->setInt(val); +} + + +////////////////////////////////////////////////////////////////////////// +void CScStack::pushFloat(double val) { + /* + CScValue* val = new CScValue(_gameRef); + val->setFloat(Val); + Push(val); + delete val; + */ + getPushValue()->setFloat(val); +} + + +////////////////////////////////////////////////////////////////////////// +void CScStack::pushBool(bool val) { + /* + CScValue* val = new CScValue(_gameRef); + val->setBool(Val); + Push(val); + delete val; + */ + getPushValue()->setBool(val); +} + + +////////////////////////////////////////////////////////////////////////// +void CScStack::pushString(const char *val) { + /* + CScValue* val = new CScValue(_gameRef); + val->setString(Val); + Push(val); + delete val; + */ + getPushValue()->setString(val); +} + + +////////////////////////////////////////////////////////////////////////// +void CScStack::pushNative(CBScriptable *val, bool persistent) { + /* + CScValue* val = new CScValue(_gameRef); + val->setNative(Val, Persistent); + Push(val); + delete val; + */ + + getPushValue()->setNative(val, persistent); +} + + +////////////////////////////////////////////////////////////////////////// +bool CScStack::persist(CBPersistMgr *persistMgr) { + + persistMgr->transfer(TMEMBER(_gameRef)); + + persistMgr->transfer(TMEMBER(_sP)); + _values.persist(persistMgr); + + return STATUS_OK; +} + +} // end of namespace WinterMute diff --git a/engines/wintermute/base/scriptables/ScStack.h b/engines/wintermute/base/scriptables/ScStack.h new file mode 100644 index 0000000000..22dae63060 --- /dev/null +++ b/engines/wintermute/base/scriptables/ScStack.h @@ -0,0 +1,66 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +/* + * This file is based on WME Lite. + * http://dead-code.org/redir.php?target=wmelite + * Copyright (c) 2011 Jan Nedoma + */ + +#ifndef WINTERMUTE_SCSTACK_H +#define WINTERMUTE_SCSTACK_H + + +#include "engines/wintermute/base/BBase.h" +#include "engines/wintermute/coll_templ.h" +#include "engines/wintermute/persistent.h" + +namespace WinterMute { + +class CScValue; +class CBScriptable; + +class CScStack : public CBBase { +public: + CScValue *getAt(int Index); + CScValue *getPushValue(); + DECLARE_PERSISTENT(CScStack, CBBase) + void pushNative(CBScriptable *val, bool persistent); + void pushString(const char *val); + void pushBool(bool val); + void pushInt(int val); + void pushFloat(double val); + void pushNULL(); + void correctParams(uint32 expectedParams); + CScValue *getTop(); + void push(CScValue *val); + CScValue *pop(); + CScStack(CBGame *inGame); + virtual ~CScStack(); + CBArray _values; + int _sP; + +}; + +} // end of namespace WinterMute + +#endif diff --git a/engines/wintermute/base/scriptables/ScValue.cpp b/engines/wintermute/base/scriptables/ScValue.cpp new file mode 100644 index 0000000000..e9d5645682 --- /dev/null +++ b/engines/wintermute/base/scriptables/ScValue.cpp @@ -0,0 +1,1054 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +/* + * This file is based on WME Lite. + * http://dead-code.org/redir.php?target=wmelite + * Copyright (c) 2011 Jan Nedoma + */ + +#include "engines/wintermute/PlatformSDL.h" +#include "engines/wintermute/base/BDynBuffer.h" +#include "engines/wintermute/base/BGame.h" +#include "engines/wintermute/base/scriptables/ScValue.h" +#include "engines/wintermute/base/scriptables/ScScript.h" +#include "engines/wintermute/utils/StringUtil.h" +#include "engines/wintermute/base/BScriptable.h" + +namespace WinterMute { + +////////////////////////////////////////////////////////////////////// +// Construction/Destruction +////////////////////////////////////////////////////////////////////// + +IMPLEMENT_PERSISTENT(CScValue, false) + +////////////////////////////////////////////////////////////////////////// +CScValue::CScValue(CBGame *inGame): CBBase(inGame) { + _type = VAL_NULL; + + _valBool = false; + _valInt = 0; + _valFloat = 0.0f; + _valNative = NULL; + _valString = NULL; + _valRef = NULL; + _persistent = false; + _isConstVar = false; +} + + +////////////////////////////////////////////////////////////////////////// +CScValue::CScValue(CBGame *inGame, bool val): CBBase(inGame) { + _type = VAL_BOOL; + _valBool = val; + + _valInt = 0; + _valFloat = 0.0f; + _valNative = NULL; + _valString = NULL; + _valRef = NULL; + _persistent = false; + _isConstVar = false; +} + + +////////////////////////////////////////////////////////////////////////// +CScValue::CScValue(CBGame *inGame, int val): CBBase(inGame) { + _type = VAL_INT; + _valInt = val; + + _valFloat = 0.0f; + _valBool = false; + _valNative = NULL; + _valString = NULL; + _valRef = NULL; + _persistent = false; + _isConstVar = false; +} + + +////////////////////////////////////////////////////////////////////////// +CScValue::CScValue(CBGame *inGame, double val): CBBase(inGame) { + _type = VAL_FLOAT; + _valFloat = val; + + _valInt = 0; + _valBool = false; + _valNative = NULL; + _valString = NULL; + _valRef = NULL; + _persistent = false; + _isConstVar = false; +} + + +////////////////////////////////////////////////////////////////////////// +CScValue::CScValue(CBGame *inGame, const char *val): CBBase(inGame) { + _type = VAL_STRING; + _valString = NULL; + setStringVal(val); + + _valBool = false; + _valInt = 0; + _valFloat = 0.0f; + _valNative = NULL; + _valRef = NULL; + _persistent = false; + _isConstVar = false; +} + + +////////////////////////////////////////////////////////////////////////// +void CScValue::cleanup(bool ignoreNatives) { + deleteProps(); + + if (_valString) delete [] _valString; + + if (!ignoreNatives) { + if (_valNative && !_persistent) { + _valNative->_refCount--; + if (_valNative->_refCount <= 0) { + delete _valNative; + _valNative = NULL; + } + } + } + + + _type = VAL_NULL; + + _valBool = false; + _valInt = 0; + _valFloat = 0.0f; + _valNative = NULL; + _valString = NULL; + _valRef = NULL; + _persistent = false; + _isConstVar = false; +} + + + +////////////////////////////////////////////////////////////////////////// +CScValue::~CScValue() { + cleanup(); +} + + +////////////////////////////////////////////////////////////////////////// +CScValue *CScValue::getProp(const char *name) { + if (_type == VAL_VARIABLE_REF) return _valRef->getProp(name); + + if (_type == VAL_STRING && strcmp(name, "Length") == 0) { + _gameRef->_scValue->_type = VAL_INT; + +#if 0 // TODO: Remove FreeType-dependency + if (_gameRef->_textEncoding == TEXT_ANSI) { +#else + if (true) { +#endif + _gameRef->_scValue->setInt(strlen(_valString)); + } else { + WideString wstr = StringUtil::utf8ToWide(_valString); + _gameRef->_scValue->setInt(wstr.size()); + } + + return _gameRef->_scValue; + } + + CScValue *ret = NULL; + + if (_type == VAL_NATIVE && _valNative) ret = _valNative->scGetProperty(name); + + if (ret == NULL) { + _valIter = _valObject.find(name); + if (_valIter != _valObject.end()) ret = _valIter->_value; + } + return ret; +} + +////////////////////////////////////////////////////////////////////////// +bool CScValue::deleteProp(const char *name) { + if (_type == VAL_VARIABLE_REF) return _valRef->deleteProp(name); + + _valIter = _valObject.find(name); + if (_valIter != _valObject.end()) { + delete _valIter->_value; + _valIter->_value = NULL; + } + + return STATUS_OK; +} + + + +////////////////////////////////////////////////////////////////////////// +bool CScValue::setProp(const char *name, CScValue *val, bool copyWhole, bool setAsConst) { + if (_type == VAL_VARIABLE_REF) + return _valRef->setProp(name, val); + + bool ret = STATUS_FAILED; + if (_type == VAL_NATIVE && _valNative) { + ret = _valNative->scSetProperty(name, val); + } + + if (DID_FAIL(ret)) { + CScValue *newVal = NULL; + + _valIter = _valObject.find(name); + if (_valIter != _valObject.end()) { + newVal = _valIter->_value; + } + if (!newVal) + newVal = new CScValue(_gameRef); + else newVal->cleanup(); + + newVal->copy(val, copyWhole); + newVal->_isConstVar = setAsConst; + _valObject[name] = newVal; + + if (_type != VAL_NATIVE) _type = VAL_OBJECT; + + /* + _valIter = _valObject.find(Name); + if (_valIter != _valObject.end()){ + delete _valIter->_value; + _valIter->_value = NULL; + } + CScValue* val = new CScValue(_gameRef); + val->Copy(Val, CopyWhole); + val->_isConstVar = SetAsConst; + _valObject[Name] = val; + + if(_type!=VAL_NATIVE) _type = VAL_OBJECT; + */ + } + + return STATUS_OK; +} + + +////////////////////////////////////////////////////////////////////////// +bool CScValue::propExists(const char *name) { + if (_type == VAL_VARIABLE_REF) + return _valRef->propExists(name); + _valIter = _valObject.find(name); + + return (_valIter != _valObject.end()); +} + + +////////////////////////////////////////////////////////////////////////// +void CScValue::deleteProps() { + _valIter = _valObject.begin(); + while (_valIter != _valObject.end()) { + delete(CScValue *)_valIter->_value; + _valIter++; + } + _valObject.clear(); +} + + +////////////////////////////////////////////////////////////////////////// +void CScValue::CleanProps(bool includingNatives) { + _valIter = _valObject.begin(); + while (_valIter != _valObject.end()) { + if (!_valIter->_value->_isConstVar && (!_valIter->_value->isNative() || includingNatives)) _valIter->_value->setNULL(); + _valIter++; + } +} + +////////////////////////////////////////////////////////////////////////// +bool CScValue::isNULL() { + if (_type == VAL_VARIABLE_REF) + return _valRef->isNULL(); + + return (_type == VAL_NULL); +} + + +////////////////////////////////////////////////////////////////////////// +bool CScValue::isNative() { + if (_type == VAL_VARIABLE_REF) + return _valRef->isNative(); + + return (_type == VAL_NATIVE); +} + + +////////////////////////////////////////////////////////////////////////// +bool CScValue::isString() { + if (_type == VAL_VARIABLE_REF) + return _valRef->isString(); + + return (_type == VAL_STRING); +} + + +////////////////////////////////////////////////////////////////////////// +bool CScValue::isFloat() { + if (_type == VAL_VARIABLE_REF) + return _valRef->isFloat(); + + return (_type == VAL_FLOAT); +} + + +////////////////////////////////////////////////////////////////////////// +bool CScValue::isInt() { + if (_type == VAL_VARIABLE_REF) + return _valRef->isInt(); + + return (_type == VAL_INT); +} + + +////////////////////////////////////////////////////////////////////////// +bool CScValue::isBool() { + if (_type == VAL_VARIABLE_REF) + return _valRef->isBool(); + + return (_type == VAL_BOOL); +} + + +////////////////////////////////////////////////////////////////////////// +bool CScValue::isObject() { + if (_type == VAL_VARIABLE_REF) + return _valRef->isObject(); + + return (_type == VAL_OBJECT); +} + + +////////////////////////////////////////////////////////////////////////// +TValType CScValue::getTypeTolerant() { + if (_type == VAL_VARIABLE_REF) + return _valRef->getType(); + + return _type; +} + + +////////////////////////////////////////////////////////////////////////// +void CScValue::setBool(bool val) { + if (_type == VAL_VARIABLE_REF) { + _valRef->setBool(val); + return; + } + + if (_type == VAL_NATIVE) { + _valNative->scSetBool(val); + return; + } + + _valBool = val; + _type = VAL_BOOL; +} + + +////////////////////////////////////////////////////////////////////////// +void CScValue::setInt(int val) { + if (_type == VAL_VARIABLE_REF) { + _valRef->setInt(val); + return; + } + + if (_type == VAL_NATIVE) { + _valNative->scSetInt(val); + return; + } + + _valInt = val; + _type = VAL_INT; +} + + +////////////////////////////////////////////////////////////////////////// +void CScValue::setFloat(double val) { + if (_type == VAL_VARIABLE_REF) { + _valRef->setFloat(val); + return; + } + + if (_type == VAL_NATIVE) { + _valNative->scSetFloat(val); + return; + } + + _valFloat = val; + _type = VAL_FLOAT; +} + + +////////////////////////////////////////////////////////////////////////// +void CScValue::setString(const char *val) { + if (_type == VAL_VARIABLE_REF) { + _valRef->setString(val); + return; + } + + if (_type == VAL_NATIVE) { + _valNative->scSetString(val); + return; + } + + setStringVal(val); + if (_valString) _type = VAL_STRING; + else _type = VAL_NULL; +} + +void CScValue::setString(const Common::String &val) { + setString(val.c_str()); +} + +////////////////////////////////////////////////////////////////////////// +void CScValue::setStringVal(const char *val) { + if (_valString) { + delete [] _valString; + _valString = NULL; + } + + if (val == NULL) { + _valString = NULL; + return; + } + + _valString = new char [strlen(val) + 1]; + if (_valString) { + strcpy(_valString, val); + } +} + + +////////////////////////////////////////////////////////////////////////// +void CScValue::setNULL() { + if (_type == VAL_VARIABLE_REF) { + _valRef->setNULL(); + return; + } + + if (_valNative && !_persistent) { + _valNative->_refCount--; + if (_valNative->_refCount <= 0) delete _valNative; + } + _valNative = NULL; + deleteProps(); + + _type = VAL_NULL; +} + + +////////////////////////////////////////////////////////////////////////// +void CScValue::setNative(CBScriptable *val, bool persistent) { + if (_type == VAL_VARIABLE_REF) { + _valRef->setNative(val, persistent); + return; + } + + if (val == NULL) { + setNULL(); + } else { + if (_valNative && !_persistent) { + _valNative->_refCount--; + if (_valNative->_refCount <= 0) { + if (_valNative != val) delete _valNative; + _valNative = NULL; + } + } + + _type = VAL_NATIVE; + _persistent = persistent; + + _valNative = val; + if (_valNative && !_persistent) _valNative->_refCount++; + } +} + + +////////////////////////////////////////////////////////////////////////// +void CScValue::setObject() { + if (_type == VAL_VARIABLE_REF) { + _valRef->setObject(); + return; + } + + deleteProps(); + _type = VAL_OBJECT; +} + + +////////////////////////////////////////////////////////////////////////// +void CScValue::setReference(CScValue *val) { + _valRef = val; + _type = VAL_VARIABLE_REF; +} + + +////////////////////////////////////////////////////////////////////////// +bool CScValue::getBool(bool defaultVal) { + if (_type == VAL_VARIABLE_REF) + return _valRef->getBool(); + + switch (_type) { + case VAL_BOOL: + return _valBool; + + case VAL_NATIVE: + return _valNative->scToBool(); + + case VAL_INT: + return (_valInt != 0); + + case VAL_FLOAT: + return (_valFloat != 0.0f); + + case VAL_STRING: + return (scumm_stricmp(_valString, "1") == 0 || scumm_stricmp(_valString, "yes") == 0 || scumm_stricmp(_valString, "true") == 0); + + default: + return defaultVal; + } +} + + +////////////////////////////////////////////////////////////////////////// +int CScValue::getInt(int defaultVal) { + if (_type == VAL_VARIABLE_REF) return _valRef->getInt(); + + switch (_type) { + case VAL_BOOL: + return _valBool ? 1 : 0; + + case VAL_NATIVE: + return _valNative->scToInt(); + + case VAL_INT: + return _valInt; + + case VAL_FLOAT: + return (int)_valFloat; + + case VAL_STRING: + return atoi(_valString); + + default: + return defaultVal; + } +} + + +////////////////////////////////////////////////////////////////////////// +double CScValue::getFloat(double defaultVal) { + if (_type == VAL_VARIABLE_REF) + return _valRef->getFloat(); + + switch (_type) { + case VAL_BOOL: + return _valBool ? 1.0f : 0.0f; + + case VAL_NATIVE: + return _valNative->scToFloat(); + + case VAL_INT: + return (double)_valInt; + + case VAL_FLOAT: + return _valFloat; + + case VAL_STRING: + return atof(_valString); + + default: + return defaultVal; + } +} + +////////////////////////////////////////////////////////////////////////// +void *CScValue::getMemBuffer() { + if (_type == VAL_VARIABLE_REF) + return _valRef->getMemBuffer(); + + if (_type == VAL_NATIVE) + return _valNative->scToMemBuffer(); + else return (void *)NULL; +} + + +////////////////////////////////////////////////////////////////////////// +const char *CScValue::getString() { + if (_type == VAL_VARIABLE_REF) + return _valRef->getString(); + + switch (_type) { + case VAL_OBJECT: + setStringVal("[object]"); + break; + + case VAL_NULL: + setStringVal("[null]"); + break; + + case VAL_NATIVE: { + const char *strVal = _valNative->scToString(); + setStringVal(strVal); + return strVal; + break; + } + + case VAL_BOOL: + setStringVal(_valBool ? "yes" : "no"); + break; + + case VAL_INT: { + char dummy[50]; + sprintf(dummy, "%d", _valInt); + setStringVal(dummy); + break; + } + + case VAL_FLOAT: { + char dummy[50]; + sprintf(dummy, "%f", _valFloat); + setStringVal(dummy); + break; + } + + case VAL_STRING: + break; + + default: + setStringVal(""); + } + + return _valString; +} + + +////////////////////////////////////////////////////////////////////////// +CBScriptable *CScValue::getNative() { + if (_type == VAL_VARIABLE_REF) + return _valRef->getNative(); + + if (_type == VAL_NATIVE) return _valNative; + else return NULL; +} + + +////////////////////////////////////////////////////////////////////////// +TValType CScValue::getType() { + return _type; +} + + +////////////////////////////////////////////////////////////////////////// +void CScValue::copy(CScValue *orig, bool copyWhole) { + _gameRef = orig->_gameRef; + + if (_valNative && !_persistent) { + _valNative->_refCount--; + if (_valNative->_refCount <= 0) { + if (_valNative != orig->_valNative) delete _valNative; + _valNative = NULL; + } + } + + if (orig->_type == VAL_VARIABLE_REF && orig->_valRef && copyWhole) orig = orig->_valRef; + + cleanup(true); + + _type = orig->_type; + _valBool = orig->_valBool; + _valInt = orig->_valInt; + _valFloat = orig->_valFloat; + setStringVal(orig->_valString); + + _valRef = orig->_valRef; + _persistent = orig->_persistent; + + _valNative = orig->_valNative; + if (_valNative && !_persistent) _valNative->_refCount++; +//!!!! ref->native++ + + // copy properties + if (orig->_type == VAL_OBJECT && orig->_valObject.size() > 0) { + orig->_valIter = orig->_valObject.begin(); + while (orig->_valIter != orig->_valObject.end()) { + _valObject[orig->_valIter->_key] = new CScValue(_gameRef); + _valObject[orig->_valIter->_key]->copy(orig->_valIter->_value); + orig->_valIter++; + } + } else _valObject.clear(); +} + + +////////////////////////////////////////////////////////////////////////// +void CScValue::setValue(CScValue *val) { + if (val->_type == VAL_VARIABLE_REF) { + setValue(val->_valRef); + return; + } + + // if being assigned a simple type, preserve native state + if (_type == VAL_NATIVE && (val->_type == VAL_INT || val->_type == VAL_STRING || val->_type == VAL_BOOL)) { + switch (val->_type) { + case VAL_INT: + _valNative->scSetInt(val->getInt()); + break; + case VAL_FLOAT: + _valNative->scSetFloat(val->getFloat()); + break; + case VAL_BOOL: + _valNative->scSetBool(val->getBool()); + break; + case VAL_STRING: + _valNative->scSetString(val->getString()); + break; + default: + warning("CScValue::setValue - unhandled enum"); + break; + } + } + // otherwise just copy everything + else copy(val); +} + + +////////////////////////////////////////////////////////////////////////// +bool CScValue::persist(CBPersistMgr *persistMgr) { + persistMgr->transfer(TMEMBER(_gameRef)); + + persistMgr->transfer(TMEMBER(_persistent)); + persistMgr->transfer(TMEMBER(_isConstVar)); + persistMgr->transfer(TMEMBER_INT(_type)); + persistMgr->transfer(TMEMBER(_valBool)); + persistMgr->transfer(TMEMBER(_valFloat)); + persistMgr->transfer(TMEMBER(_valInt)); + persistMgr->transfer(TMEMBER(_valNative)); + + int size; + const char *str; + if (persistMgr->_saving) { + size = _valObject.size(); + persistMgr->transfer("", &size); + _valIter = _valObject.begin(); + while (_valIter != _valObject.end()) { + str = _valIter->_key.c_str(); + persistMgr->transfer("", &str); + persistMgr->transfer("", &_valIter->_value); + + _valIter++; + } + } else { + CScValue *val; + persistMgr->transfer("", &size); + for (int i = 0; i < size; i++) { + persistMgr->transfer("", &str); + persistMgr->transfer("", &val); + + _valObject[str] = val; + delete [] str; + } + } + + persistMgr->transfer(TMEMBER(_valRef)); + persistMgr->transfer(TMEMBER(_valString)); + + /* + FILE* f = fopen("c:\\val.log", "a+"); + switch(_type) + { + case VAL_STRING: + fprintf(f, "str %s\n", _valString); + break; + + case VAL_INT: + fprintf(f, "int %d\n", _valInt); + break; + + case VAL_BOOL: + fprintf(f, "bool %d\n", _valBool); + break; + + case VAL_NULL: + fprintf(f, "null\n"); + break; + + case VAL_NATIVE: + fprintf(f, "native\n"); + break; + + case VAL_VARIABLE_REF: + fprintf(f, "ref\n"); + break; + + case VAL_OBJECT: + fprintf(f, "obj\n"); + break; + + case VAL_FLOAT: + fprintf(f, "float\n"); + break; + + } + fclose(f); + */ + + return STATUS_OK; +} + + +////////////////////////////////////////////////////////////////////////// +bool CScValue::saveAsText(CBDynBuffer *buffer, int indent) { + _valIter = _valObject.begin(); + while (_valIter != _valObject.end()) { + buffer->putTextIndent(indent, "PROPERTY {\n"); + buffer->putTextIndent(indent + 2, "NAME=\"%s\"\n", _valIter->_key.c_str()); + buffer->putTextIndent(indent + 2, "VALUE=\"%s\"\n", _valIter->_value->getString()); + buffer->putTextIndent(indent, "}\n\n"); + + _valIter++; + } + return STATUS_OK; +} + + +////////////////////////////////////////////////////////////////////////// +// -1 ... left is less, 0 ... equals, 1 ... left is greater +int CScValue::compare(CScValue *val1, CScValue *val2) { + // both natives? + if (val1->isNative() && val2->isNative()) { + // same class? + if (strcmp(val1->getNative()->getClassName(), val2->getNative()->getClassName()) == 0) { + return val1->getNative()->scCompare(val2->getNative()); + } else return strcmp(val1->getString(), val2->getString()); + } + + // both objects? + if (val1->isObject() && val2->isObject()) return -1; + + + // null states + if (val1->isNULL() && !val2->isNULL()) return -1; + else if (!val1->isNULL() && val2->isNULL()) return 1; + else if (val1->isNULL() && val2->isNULL()) return 0; + + // one of them is string? convert both to string + if (val1->isString() || val2->isString()) return strcmp(val1->getString(), val2->getString()); + + // one of them is float? + if (val1->isFloat() || val2->isFloat()) { + if (val1->getFloat() < val2->getFloat()) return -1; + else if (val1->getFloat() > val2->getFloat()) return 1; + else return 0; + } + + // otherwise compare as int's + if (val1->getInt() < val2->getInt()) return -1; + else if (val1->getInt() > val2->getInt()) return 1; + else return 0; +} + + +////////////////////////////////////////////////////////////////////////// +int CScValue::compareStrict(CScValue *val1, CScValue *val2) { + if (val1->getTypeTolerant() != val2->getTypeTolerant()) return -1; + else return CScValue::compare(val1, val2); +} + + +////////////////////////////////////////////////////////////////////////// +bool CScValue::dbgSendVariables(IWmeDebugClient *client, EWmeDebuggerVariableType type, CScScript *script, unsigned int scopeID) { + _valIter = _valObject.begin(); + while (_valIter != _valObject.end()) { + client->onVariableInit(type, script, scopeID, _valIter->_value, _valIter->_key.c_str()); + _valIter++; + } + return STATUS_OK; +} + + +////////////////////////////////////////////////////////////////////////// +bool CScValue::setProperty(const char *propName, int value) { + CScValue *val = new CScValue(_gameRef, value); + bool ret = DID_SUCCEED(setProp(propName, val)); + delete val; + return ret; +} + +////////////////////////////////////////////////////////////////////////// +bool CScValue::setProperty(const char *propName, const char *value) { + CScValue *val = new CScValue(_gameRef, value); + bool ret = DID_SUCCEED(setProp(propName, val)); + delete val; + return ret; +} + +////////////////////////////////////////////////////////////////////////// +bool CScValue::setProperty(const char *propName, double value) { + CScValue *val = new CScValue(_gameRef, value); + bool ret = DID_SUCCEED(setProp(propName, val)); + delete val; + return ret; +} + + +////////////////////////////////////////////////////////////////////////// +bool CScValue::setProperty(const char *propName, bool value) { + CScValue *val = new CScValue(_gameRef, value); + bool ret = DID_SUCCEED(setProp(propName, val)); + delete val; + return ret; +} + + +////////////////////////////////////////////////////////////////////////// +bool CScValue::setProperty(const char *propName) { + CScValue *val = new CScValue(_gameRef); + bool ret = DID_SUCCEED(setProp(propName, val)); + delete val; + return ret; +} + + +////////////////////////////////////////////////////////////////////////// +// IWmeDebugProp +////////////////////////////////////////////////////////////////////////// +EWmeDebuggerPropType CScValue::dbgGetType() { + switch (getType()) { + case VAL_NULL: + return WME_DBGPROP_NULL; + case VAL_STRING: + return WME_DBGPROP_STRING; + case VAL_INT: + return WME_DBGPROP_INT; + case VAL_BOOL: + return WME_DBGPROP_BOOL; + case VAL_FLOAT: + return WME_DBGPROP_FLOAT; + case VAL_OBJECT: + return WME_DBGPROP_OBJECT; + case VAL_NATIVE: + return WME_DBGPROP_NATIVE; + default: + return WME_DBGPROP_UNKNOWN; + } +} + +////////////////////////////////////////////////////////////////////////// +int CScValue::dbgGetValInt() { + return getInt(); +} + +////////////////////////////////////////////////////////////////////////// +double CScValue::dbgGetValFloat() { + return getFloat(); +} + +////////////////////////////////////////////////////////////////////////// +bool CScValue::dbgGetValBool() { + return getBool(); +} + +////////////////////////////////////////////////////////////////////////// +const char *CScValue::dbgGetValString() { + return getString(); +} + +////////////////////////////////////////////////////////////////////////// +IWmeDebugObject *CScValue::dbgGetValNative() { + return getNative(); +} + +////////////////////////////////////////////////////////////////////////// +bool CScValue::dbgSetVal(int value) { + setInt(value); + return true; +} + +////////////////////////////////////////////////////////////////////////// +bool CScValue::dbgSetVal(double value) { + setFloat(value); + return true; +} + +////////////////////////////////////////////////////////////////////////// +bool CScValue::dbgSetVal(bool value) { + setBool(value); + return true; +} + +////////////////////////////////////////////////////////////////////////// +bool CScValue::dbgSetVal(const char *value) { + setString(value); + return true; +} + +////////////////////////////////////////////////////////////////////////// +bool CScValue::dbgSetVal() { + setNULL(); + return true; +} + + +////////////////////////////////////////////////////////////////////////// +int CScValue::dbgGetNumProperties() { + if (_valNative && _valNative->_scProp) + return _valNative->_scProp->dbgGetNumProperties(); + else return _valObject.size(); +} + +////////////////////////////////////////////////////////////////////////// +bool CScValue::dbgGetProperty(int index, const char **name, IWmeDebugProp **value) { + if (_valNative && _valNative->_scProp) + return _valNative->_scProp->dbgGetProperty(index, name, value); + else { + int count = 0; + _valIter = _valObject.begin(); + while (_valIter != _valObject.end()) { + if (count == index) { + *name = _valIter->_key.c_str(); + *value = _valIter->_value; + return true; + } + _valIter++; + count++; + } + return false; + } +} + +////////////////////////////////////////////////////////////////////////// +bool CScValue::dbgGetDescription(char *buf, int bufSize) { + if (_type == VAL_VARIABLE_REF) + return _valRef->dbgGetDescription(buf, bufSize); + + if (_type == VAL_NATIVE) { + _valNative->scDebuggerDesc(buf, bufSize); + } else { + strncpy(buf, getString(), bufSize); + } + return true; +} + +} // end of namespace WinterMute diff --git a/engines/wintermute/base/scriptables/ScValue.h b/engines/wintermute/base/scriptables/ScValue.h new file mode 100644 index 0000000000..c66a60c22a --- /dev/null +++ b/engines/wintermute/base/scriptables/ScValue.h @@ -0,0 +1,141 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +/* + * This file is based on WME Lite. + * http://dead-code.org/redir.php?target=wmelite + * Copyright (c) 2011 Jan Nedoma + */ + +#ifndef WINTERMUTE_SCVALUE_H +#define WINTERMUTE_SCVALUE_H + + +#include "engines/wintermute/base/BBase.h" +#include "engines/wintermute/persistent.h" +#include "engines/wintermute/dcscript.h" // Added by ClassView +#include "engines/wintermute/wme_debugger.h" +#include "common/str.h" + +namespace WinterMute { + +class CScScript; +class CBScriptable; + +class CScValue : public CBBase, public IWmeDebugProp { +public: + bool dbgSendVariables(IWmeDebugClient *client, EWmeDebuggerVariableType type, CScScript *script, unsigned int scopeID); + + static int compare(CScValue *val1, CScValue *val2); + static int compareStrict(CScValue *val1, CScValue *val2); + TValType getTypeTolerant(); + void cleanup(bool ignoreNatives = false); + DECLARE_PERSISTENT(CScValue, CBBase) + + bool _isConstVar; + bool saveAsText(CBDynBuffer *buffer, int indent); + void setValue(CScValue *val); + bool _persistent; + bool propExists(const char *name); + void copy(CScValue *orig, bool copyWhole = false); + void setStringVal(const char *val); + TValType getType(); + bool getBool(bool defaultVal = false); + int getInt(int defaultVal = 0); + double getFloat(double defaultVal = 0.0f); + const char *getString(); + void *getMemBuffer(); + CBScriptable *getNative(); + bool deleteProp(const char *name); + void deleteProps(); + void CleanProps(bool includingNatives); + void setBool(bool val); + void setInt(int val); + void setFloat(double val); + void setString(const char *val); + void setString(const Common::String &val); + void setNULL(); + void setNative(CBScriptable *val, bool persistent = false); + void setObject(); + void setReference(CScValue *val); + bool isNULL(); + bool isNative(); + bool isString(); + bool isBool(); + bool isFloat(); + bool isInt(); + bool isObject(); + bool setProp(const char *name, CScValue *val, bool copyWhole = false, bool setAsConst = false); + CScValue *getProp(const char *name); + CBScriptable *_valNative; + CScValue *_valRef; +protected: + bool _valBool; + int _valInt; + double _valFloat; + char *_valString; +public: + TValType _type; + CScValue(CBGame *inGame); + CScValue(CBGame *inGame, bool Val); + CScValue(CBGame *inGame, int Val); + CScValue(CBGame *inGame, double Val); + CScValue(CBGame *inGame, const char *Val); + virtual ~CScValue(); + Common::HashMap _valObject; + Common::HashMap::iterator _valIter; + + bool setProperty(const char *propName, int value); + bool setProperty(const char *propName, const char *value); + bool setProperty(const char *propName, double value); + bool setProperty(const char *propName, bool value); + bool setProperty(const char *propName); + + +// IWmeDebugProp interface implementation +public: + virtual EWmeDebuggerPropType dbgGetType(); + + // getters + virtual int dbgGetValInt(); + virtual double dbgGetValFloat(); + virtual bool dbgGetValBool(); + virtual const char *dbgGetValString(); + virtual IWmeDebugObject *dbgGetValNative(); + + // setters + virtual bool dbgSetVal(int value); + virtual bool dbgSetVal(double value); + virtual bool dbgSetVal(bool value); + virtual bool dbgSetVal(const char *value); + virtual bool dbgSetVal(); + + // properties + virtual int dbgGetNumProperties(); + virtual bool dbgGetProperty(int index, const char **mame, IWmeDebugProp **value); + + virtual bool dbgGetDescription(char *buf, int bufSize); +}; + +} // end of namespace WinterMute + +#endif diff --git a/engines/wintermute/base/scriptables/SxObject.cpp b/engines/wintermute/base/scriptables/SxObject.cpp new file mode 100644 index 0000000000..ba961ed2ae --- /dev/null +++ b/engines/wintermute/base/scriptables/SxObject.cpp @@ -0,0 +1,67 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +/* + * This file is based on WME Lite. + * http://dead-code.org/redir.php?target=wmelite + * Copyright (c) 2011 Jan Nedoma + */ + +#include "SxObject.h" +#include "ScValue.h" +#include "ScStack.h" + +namespace WinterMute { + +////////////////////////////////////////////////////////////////////// +// Construction/Destruction +////////////////////////////////////////////////////////////////////// + +IMPLEMENT_PERSISTENT(CSXObject, false) + +CBScriptable *makeSXObject(CBGame *inGame, CScStack *stack) { + return new CSXObject(inGame, stack); +} + +////////////////////////////////////////////////////////////////////////// +CSXObject::CSXObject(CBGame *inGame, CScStack *stack): CBObject(inGame) { + int numParams = stack->pop()->getInt(0); + for (int i = 0; i < numParams; i++) { + addScript(stack->pop()->getString()); + } +} + + +////////////////////////////////////////////////////////////////////////// +CSXObject::~CSXObject() { + +} + + +////////////////////////////////////////////////////////////////////////// +bool CSXObject::persist(CBPersistMgr *persistMgr) { + CBObject::persist(persistMgr); + + return STATUS_OK; +} + +} // end of namespace WinterMute diff --git a/engines/wintermute/base/scriptables/SxObject.h b/engines/wintermute/base/scriptables/SxObject.h new file mode 100644 index 0000000000..b4ec7c6cde --- /dev/null +++ b/engines/wintermute/base/scriptables/SxObject.h @@ -0,0 +1,47 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +/* + * This file is based on WME Lite. + * http://dead-code.org/redir.php?target=wmelite + * Copyright (c) 2011 Jan Nedoma + */ + +#ifndef WINTERMUTE_SXOBJECT_H +#define WINTERMUTE_SXOBJECT_H + + +#include "engines/wintermute/base/BObject.h" + +namespace WinterMute { + +class CSXObject : public CBObject { +public: + DECLARE_PERSISTENT(CSXObject, CBObject) + CSXObject(CBGame *inGame, CScStack *Stack); + virtual ~CSXObject(); + +}; + +} // end of namespace WinterMute + +#endif -- cgit v1.2.3 From 3ad839b32c5e432e93058218db9139dfbe8b8c84 Mon Sep 17 00:00:00 2001 From: Einar Johan Trøan Sømåen Date: Fri, 20 Jul 2012 00:45:20 +0200 Subject: WINTERMUTE: Rename PlatformSDL->platform_osystem --- engines/wintermute/base/scriptables/SXFile.cpp | 2 +- engines/wintermute/base/scriptables/ScEngine.h | 2 +- engines/wintermute/base/scriptables/ScValue.cpp | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) (limited to 'engines/wintermute/base/scriptables') diff --git a/engines/wintermute/base/scriptables/SXFile.cpp b/engines/wintermute/base/scriptables/SXFile.cpp index b2a6d24677..1924a42d72 100644 --- a/engines/wintermute/base/scriptables/SXFile.cpp +++ b/engines/wintermute/base/scriptables/SXFile.cpp @@ -35,7 +35,7 @@ #include "engines/wintermute/base/BGame.h" #include "engines/wintermute/base/file/BFile.h" #include "engines/wintermute/base/BFileManager.h" -#include "engines/wintermute/PlatformSDL.h" +#include "engines/wintermute/platform_osystem.h" #include "engines/wintermute/base/scriptables/SXFile.h" // Note: This code is completely untested, as I have yet to find a game that uses SXFile. diff --git a/engines/wintermute/base/scriptables/ScEngine.h b/engines/wintermute/base/scriptables/ScEngine.h index df327d800c..0322319b3d 100644 --- a/engines/wintermute/base/scriptables/ScEngine.h +++ b/engines/wintermute/base/scriptables/ScEngine.h @@ -34,7 +34,7 @@ #include "engines/wintermute/base/BBase.h" #include "engines/wintermute/wme_debugger.h" #include "engines/wintermute/utils/utils.h" -#include "engines/wintermute/PlatformSDL.h" +#include "engines/wintermute/platform_osystem.h" namespace WinterMute { diff --git a/engines/wintermute/base/scriptables/ScValue.cpp b/engines/wintermute/base/scriptables/ScValue.cpp index e9d5645682..f38a12fc7a 100644 --- a/engines/wintermute/base/scriptables/ScValue.cpp +++ b/engines/wintermute/base/scriptables/ScValue.cpp @@ -26,7 +26,7 @@ * Copyright (c) 2011 Jan Nedoma */ -#include "engines/wintermute/PlatformSDL.h" +#include "engines/wintermute/platform_osystem.h" #include "engines/wintermute/base/BDynBuffer.h" #include "engines/wintermute/base/BGame.h" #include "engines/wintermute/base/scriptables/ScValue.h" -- cgit v1.2.3 From 5683f076331d2831eb4720b65bb53e8d01ca33ee Mon Sep 17 00:00:00 2001 From: Einar Johan Trøan Sømåen Date: Sat, 21 Jul 2012 18:19:07 +0200 Subject: WINTERMUTE: Rename CamelCased filenames to prefixed_under_score-filenames This is mostly a lead-up to namespacing the Ad/Base folders, and then possibly removing the prefixes from the files, it also has the added benefit of getting rid of the odd case-typos that makes for issues on platforms that don't ignore case. --- engines/wintermute/base/scriptables/SXArray.cpp | 238 ---- engines/wintermute/base/scriptables/SXArray.h | 54 - engines/wintermute/base/scriptables/SXDate.cpp | 297 ---- engines/wintermute/base/scriptables/SXDate.h | 55 - engines/wintermute/base/scriptables/SXFile.cpp | 779 ----------- engines/wintermute/base/scriptables/SXFile.h | 66 - engines/wintermute/base/scriptables/SXMath.cpp | 295 ---- engines/wintermute/base/scriptables/SXMath.h | 53 - .../wintermute/base/scriptables/SXMemBuffer.cpp | 508 ------- engines/wintermute/base/scriptables/SXMemBuffer.h | 59 - engines/wintermute/base/scriptables/SXString.cpp | 404 ------ engines/wintermute/base/scriptables/SXString.h | 58 - engines/wintermute/base/scriptables/ScEngine.cpp | 712 ---------- engines/wintermute/base/scriptables/ScEngine.h | 147 -- engines/wintermute/base/scriptables/ScScript.cpp | 1461 -------------------- engines/wintermute/base/scriptables/ScScript.h | 183 --- engines/wintermute/base/scriptables/ScStack.cpp | 226 --- engines/wintermute/base/scriptables/ScStack.h | 66 - engines/wintermute/base/scriptables/ScValue.cpp | 1054 -------------- engines/wintermute/base/scriptables/ScValue.h | 141 -- engines/wintermute/base/scriptables/SxObject.cpp | 67 - engines/wintermute/base/scriptables/SxObject.h | 47 - engines/wintermute/base/scriptables/script.cpp | 1461 ++++++++++++++++++++ engines/wintermute/base/scriptables/script.h | 183 +++ .../wintermute/base/scriptables/script_engine.cpp | 712 ++++++++++ .../wintermute/base/scriptables/script_engine.h | 147 ++ .../base/scriptables/script_ext_array.cpp | 238 ++++ .../wintermute/base/scriptables/script_ext_array.h | 54 + .../base/scriptables/script_ext_date.cpp | 297 ++++ .../wintermute/base/scriptables/script_ext_date.h | 55 + .../base/scriptables/script_ext_file.cpp | 779 +++++++++++ .../wintermute/base/scriptables/script_ext_file.h | 66 + .../base/scriptables/script_ext_math.cpp | 295 ++++ .../wintermute/base/scriptables/script_ext_math.h | 53 + .../base/scriptables/script_ext_mem_buffer.cpp | 508 +++++++ .../base/scriptables/script_ext_mem_buffer.h | 59 + .../base/scriptables/script_ext_object.cpp | 67 + .../base/scriptables/script_ext_object.h | 47 + .../base/scriptables/script_ext_string.cpp | 404 ++++++ .../base/scriptables/script_ext_string.h | 58 + .../wintermute/base/scriptables/script_stack.cpp | 226 +++ engines/wintermute/base/scriptables/script_stack.h | 66 + .../wintermute/base/scriptables/script_value.cpp | 1054 ++++++++++++++ engines/wintermute/base/scriptables/script_value.h | 141 ++ 44 files changed, 6970 insertions(+), 6970 deletions(-) delete mode 100644 engines/wintermute/base/scriptables/SXArray.cpp delete mode 100644 engines/wintermute/base/scriptables/SXArray.h delete mode 100644 engines/wintermute/base/scriptables/SXDate.cpp delete mode 100644 engines/wintermute/base/scriptables/SXDate.h delete mode 100644 engines/wintermute/base/scriptables/SXFile.cpp delete mode 100644 engines/wintermute/base/scriptables/SXFile.h delete mode 100644 engines/wintermute/base/scriptables/SXMath.cpp delete mode 100644 engines/wintermute/base/scriptables/SXMath.h delete mode 100644 engines/wintermute/base/scriptables/SXMemBuffer.cpp delete mode 100644 engines/wintermute/base/scriptables/SXMemBuffer.h delete mode 100644 engines/wintermute/base/scriptables/SXString.cpp delete mode 100644 engines/wintermute/base/scriptables/SXString.h delete mode 100644 engines/wintermute/base/scriptables/ScEngine.cpp delete mode 100644 engines/wintermute/base/scriptables/ScEngine.h delete mode 100644 engines/wintermute/base/scriptables/ScScript.cpp delete mode 100644 engines/wintermute/base/scriptables/ScScript.h delete mode 100644 engines/wintermute/base/scriptables/ScStack.cpp delete mode 100644 engines/wintermute/base/scriptables/ScStack.h delete mode 100644 engines/wintermute/base/scriptables/ScValue.cpp delete mode 100644 engines/wintermute/base/scriptables/ScValue.h delete mode 100644 engines/wintermute/base/scriptables/SxObject.cpp delete mode 100644 engines/wintermute/base/scriptables/SxObject.h create mode 100644 engines/wintermute/base/scriptables/script.cpp create mode 100644 engines/wintermute/base/scriptables/script.h create mode 100644 engines/wintermute/base/scriptables/script_engine.cpp create mode 100644 engines/wintermute/base/scriptables/script_engine.h create mode 100644 engines/wintermute/base/scriptables/script_ext_array.cpp create mode 100644 engines/wintermute/base/scriptables/script_ext_array.h create mode 100644 engines/wintermute/base/scriptables/script_ext_date.cpp create mode 100644 engines/wintermute/base/scriptables/script_ext_date.h create mode 100644 engines/wintermute/base/scriptables/script_ext_file.cpp create mode 100644 engines/wintermute/base/scriptables/script_ext_file.h create mode 100644 engines/wintermute/base/scriptables/script_ext_math.cpp create mode 100644 engines/wintermute/base/scriptables/script_ext_math.h create mode 100644 engines/wintermute/base/scriptables/script_ext_mem_buffer.cpp create mode 100644 engines/wintermute/base/scriptables/script_ext_mem_buffer.h create mode 100644 engines/wintermute/base/scriptables/script_ext_object.cpp create mode 100644 engines/wintermute/base/scriptables/script_ext_object.h create mode 100644 engines/wintermute/base/scriptables/script_ext_string.cpp create mode 100644 engines/wintermute/base/scriptables/script_ext_string.h create mode 100644 engines/wintermute/base/scriptables/script_stack.cpp create mode 100644 engines/wintermute/base/scriptables/script_stack.h create mode 100644 engines/wintermute/base/scriptables/script_value.cpp create mode 100644 engines/wintermute/base/scriptables/script_value.h (limited to 'engines/wintermute/base/scriptables') diff --git a/engines/wintermute/base/scriptables/SXArray.cpp b/engines/wintermute/base/scriptables/SXArray.cpp deleted file mode 100644 index 425118a3e7..0000000000 --- a/engines/wintermute/base/scriptables/SXArray.cpp +++ /dev/null @@ -1,238 +0,0 @@ -/* ScummVM - Graphic Adventure Engine - * - * ScummVM is the legal property of its developers, whose names - * are too numerous to list here. Please refer to the COPYRIGHT - * file distributed with this source distribution. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - */ - -/* - * This file is based on WME Lite. - * http://dead-code.org/redir.php?target=wmelite - * Copyright (c) 2011 Jan Nedoma - */ - -#include "engines/wintermute/persistent.h" -#include "engines/wintermute/base/scriptables/ScValue.h" -#include "engines/wintermute/base/scriptables/ScStack.h" -#include "engines/wintermute/system/SysInstance.h" -#include "engines/wintermute/base/scriptables/SXArray.h" - -namespace WinterMute { - -IMPLEMENT_PERSISTENT(CSXArray, false) - -CBScriptable *makeSXArray(CBGame *inGame, CScStack *stack) { - return new CSXArray(inGame, stack); -} - -////////////////////////////////////////////////////////////////////////// -CSXArray::CSXArray(CBGame *inGame, CScStack *stack): CBScriptable(inGame) { - _length = 0; - _values = new CScValue(_gameRef); - - int numParams = stack->pop()->getInt(0); - - if (numParams == 1) _length = stack->pop()->getInt(0); - else if (numParams > 1) { - _length = numParams; - char paramName[20]; - for (int i = 0; i < numParams; i++) { - sprintf(paramName, "%d", i); - _values->setProp(paramName, stack->pop()); - } - } -} - -////////////////////////////////////////////////////////////////////////// -CSXArray::CSXArray(CBGame *inGame): CBScriptable(inGame) { - _length = 0; - _values = new CScValue(_gameRef); -} - - -////////////////////////////////////////////////////////////////////////// -CSXArray::~CSXArray() { - delete _values; - _values = NULL; -} - - -////////////////////////////////////////////////////////////////////////// -const char *CSXArray::scToString() { - static char dummy[32768]; // TODO: Get rid of static. - strcpy(dummy, ""); - char propName[20]; - for (int i = 0; i < _length; i++) { - sprintf(propName, "%d", i); - CScValue *val = _values->getProp(propName); - if (val) { - if (strlen(dummy) + strlen(val->getString()) < 32768) { - strcat(dummy, val->getString()); - } - } - - if (i < _length - 1 && strlen(dummy) + 1 < 32768) strcat(dummy, ","); - } - return dummy; -} - - -////////////////////////////////////////////////////////////////////////// -bool CSXArray::scCallMethod(CScScript *script, CScStack *stack, CScStack *thisStack, const char *name) { - ////////////////////////////////////////////////////////////////////////// - // Push - ////////////////////////////////////////////////////////////////////////// - if (strcmp(name, "Push") == 0) { - int numParams = stack->pop()->getInt(0); - char paramName[20]; - - for (int i = 0; i < numParams; i++) { - _length++; - sprintf(paramName, "%d", _length - 1); - _values->setProp(paramName, stack->pop(), true); - } - stack->pushInt(_length); - - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // Pop - ////////////////////////////////////////////////////////////////////////// - if (strcmp(name, "Pop") == 0) { - - stack->correctParams(0); - - if (_length > 0) { - char paramName[20]; - sprintf(paramName, "%d", _length - 1); - stack->push(_values->getProp(paramName)); - _values->deleteProp(paramName); - _length--; - } else stack->pushNULL(); - - return STATUS_OK; - } - - else return STATUS_FAILED; -} - - -////////////////////////////////////////////////////////////////////////// -CScValue *CSXArray::scGetProperty(const char *name) { - _scValue->setNULL(); - - ////////////////////////////////////////////////////////////////////////// - // Type - ////////////////////////////////////////////////////////////////////////// - if (strcmp(name, "Type") == 0) { - _scValue->setString("array"); - return _scValue; - } - - ////////////////////////////////////////////////////////////////////////// - // Length - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "Length") == 0) { - _scValue->setInt(_length); - return _scValue; - } - - ////////////////////////////////////////////////////////////////////////// - // [number] - ////////////////////////////////////////////////////////////////////////// - else { - char ParamName[20]; - if (validNumber(name, ParamName)) { - return _values->getProp(ParamName); - } else return _scValue; - } -} - - -////////////////////////////////////////////////////////////////////////// -bool CSXArray::scSetProperty(const char *name, CScValue *value) { - ////////////////////////////////////////////////////////////////////////// - // Length - ////////////////////////////////////////////////////////////////////////// - if (strcmp(name, "Length") == 0) { - int OrigLength = _length; - _length = MAX(value->getInt(0), 0); - - char PropName[20]; - if (_length < OrigLength) { - for (int i = _length; i < OrigLength; i++) { - sprintf(PropName, "%d", i); - _values->deleteProp(PropName); - } - } - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // [number] - ////////////////////////////////////////////////////////////////////////// - else { - char paramName[20]; - if (validNumber(name, paramName)) { - int Index = atoi(paramName); - if (Index >= _length) _length = Index + 1; - return _values->setProp(paramName, value); - } else return STATUS_FAILED; - } -} - - -////////////////////////////////////////////////////////////////////////// -bool CSXArray::persist(CBPersistMgr *persistMgr) { - CBScriptable::persist(persistMgr); - - persistMgr->transfer(TMEMBER(_length)); - persistMgr->transfer(TMEMBER(_values)); - - return STATUS_OK; -} - - -////////////////////////////////////////////////////////////////////////// -bool CSXArray::validNumber(const char *origStr, char *outStr) { - bool isNumber = true; - for (uint32 i = 0; i < strlen(origStr); i++) { - if (!(origStr[i] >= '0' && origStr[i] <= '9')) { - isNumber = false; - break; - } - } - - if (isNumber) { - int index = atoi(origStr); - sprintf(outStr, "%d", index); - return true; - } else return false; -} - -////////////////////////////////////////////////////////////////////////// -bool CSXArray::push(CScValue *val) { - char paramName[20]; - _length++; - sprintf(paramName, "%d", _length - 1); - _values->setProp(paramName, val, true); - return STATUS_OK; -} - -} // end of namespace WinterMute diff --git a/engines/wintermute/base/scriptables/SXArray.h b/engines/wintermute/base/scriptables/SXArray.h deleted file mode 100644 index 0f46bd546e..0000000000 --- a/engines/wintermute/base/scriptables/SXArray.h +++ /dev/null @@ -1,54 +0,0 @@ -/* ScummVM - Graphic Adventure Engine - * - * ScummVM is the legal property of its developers, whose names - * are too numerous to list here. Please refer to the COPYRIGHT - * file distributed with this source distribution. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - */ - -/* - * This file is based on WME Lite. - * http://dead-code.org/redir.php?target=wmelite - * Copyright (c) 2011 Jan Nedoma - */ - -#ifndef WINTERMUTE_SXARRAY_H -#define WINTERMUTE_SXARRAY_H - -#include "engines/wintermute/base/BScriptable.h" - -namespace WinterMute { - -class CSXArray : public CBScriptable { -public: - bool push(CScValue *Val); - bool validNumber(const char *origStr, char *outStr); - DECLARE_PERSISTENT(CSXArray, CBScriptable) - CSXArray(CBGame *inGame, CScStack *stack); - CSXArray(CBGame *inGame); - virtual ~CSXArray(); - CScValue *scGetProperty(const char *name); - bool scSetProperty(const char *name, CScValue *value); - bool scCallMethod(CScScript *script, CScStack *stack, CScStack *thisStack, const char *name); - const char *scToString(); - int _length; - CScValue *_values; -}; - -} // end of namespace WinterMute - -#endif diff --git a/engines/wintermute/base/scriptables/SXDate.cpp b/engines/wintermute/base/scriptables/SXDate.cpp deleted file mode 100644 index cd705cc9d4..0000000000 --- a/engines/wintermute/base/scriptables/SXDate.cpp +++ /dev/null @@ -1,297 +0,0 @@ -/* ScummVM - Graphic Adventure Engine - * - * ScummVM is the legal property of its developers, whose names - * are too numerous to list here. Please refer to the COPYRIGHT - * file distributed with this source distribution. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - */ - -/* - * This file is based on WME Lite. - * http://dead-code.org/redir.php?target=wmelite - * Copyright (c) 2011 Jan Nedoma - */ - -#include "engines/wintermute/base/scriptables/ScStack.h" -#include "engines/wintermute/base/scriptables/ScValue.h" -#include "engines/wintermute/base/scriptables/SXDate.h" - -namespace WinterMute { - -IMPLEMENT_PERSISTENT(CSXDate, false) - -CBScriptable *makeSXDate(CBGame *inGame, CScStack *stack) { - return new CSXDate(inGame, stack); -} - -////////////////////////////////////////////////////////////////////////// -CSXDate::CSXDate(CBGame *inGame, CScStack *stack): CBScriptable(inGame) { - stack->correctParams(6); - - memset(&_tm, 0, sizeof(_tm)); - - CScValue *valYear = stack->pop(); - _tm.tm_year = valYear->getInt() - 1900; - _tm.tm_mon = stack->pop()->getInt() - 1; - _tm.tm_mday = stack->pop()->getInt(); - _tm.tm_hour = stack->pop()->getInt(); - _tm.tm_min = stack->pop()->getInt(); - _tm.tm_sec = stack->pop()->getInt(); - - if (valYear->isNULL()) { - g_system->getTimeAndDate(_tm); - } -} - - -////////////////////////////////////////////////////////////////////////// -CSXDate::~CSXDate() { - -} - -////////////////////////////////////////////////////////////////////////// -const char *CSXDate::scToString() { - // TODO: Make this more stringy, and less ISO 8601-like - _strRep.format("%04d-%02d-%02d - %02d:%02d:%02d", _tm.tm_year, _tm.tm_mon, _tm.tm_mday, _tm.tm_hour, _tm.tm_min, _tm.tm_sec); - return _strRep.c_str(); -#if 0 - return asctime(&_tm); -#endif -} - - -////////////////////////////////////////////////////////////////////////// -bool CSXDate::scCallMethod(CScScript *script, CScStack *stack, CScStack *thisStack, const char *name) { - ////////////////////////////////////////////////////////////////////////// - // GetYear - ////////////////////////////////////////////////////////////////////////// - if (strcmp(name, "GetYear") == 0) { - stack->correctParams(0); - stack->pushInt(_tm.tm_year + 1900); - return STATUS_OK; - } - ////////////////////////////////////////////////////////////////////////// - // GetMonth - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "GetMonth") == 0) { - stack->correctParams(0); - stack->pushInt(_tm.tm_mon + 1); - return STATUS_OK; - } - ////////////////////////////////////////////////////////////////////////// - // GetDate - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "GetDate") == 0) { - stack->correctParams(0); - stack->pushInt(_tm.tm_mday); - return STATUS_OK; - } - ////////////////////////////////////////////////////////////////////////// - // GetHours - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "GetHours") == 0) { - stack->correctParams(0); - stack->pushInt(_tm.tm_hour); - return STATUS_OK; - } - ////////////////////////////////////////////////////////////////////////// - // GetMinutes - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "GetMinutes") == 0) { - stack->correctParams(0); - stack->pushInt(_tm.tm_min); - return STATUS_OK; - } - ////////////////////////////////////////////////////////////////////////// - // GetSeconds - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "GetSeconds") == 0) { - stack->correctParams(0); - stack->pushInt(_tm.tm_sec); - return STATUS_OK; - } - ////////////////////////////////////////////////////////////////////////// - // GetWeekday - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "GetWeekday") == 0) { - stack->correctParams(0); - warning("GetWeekday returns a wrong value on purpose"); - stack->pushInt(_tm.tm_mday % 7); - return STATUS_OK; - } - - - ////////////////////////////////////////////////////////////////////////// - // SetYear - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "SetYear") == 0) { - stack->correctParams(1); - _tm.tm_year = stack->pop()->getInt() - 1900; - stack->pushNULL(); - return STATUS_OK; - } - ////////////////////////////////////////////////////////////////////////// - // SetMonth - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "SetMonth") == 0) { - stack->correctParams(1); - _tm.tm_mon = stack->pop()->getInt() - 1; - stack->pushNULL(); - return STATUS_OK; - } - ////////////////////////////////////////////////////////////////////////// - // SetDate - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "SetDate") == 0) { - stack->correctParams(1); - _tm.tm_mday = stack->pop()->getInt(); - stack->pushNULL(); - return STATUS_OK; - } - ////////////////////////////////////////////////////////////////////////// - // SetHours - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "SetHours") == 0) { - stack->correctParams(1); - _tm.tm_hour = stack->pop()->getInt(); - stack->pushNULL(); - return STATUS_OK; - } - ////////////////////////////////////////////////////////////////////////// - // SetMinutes - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "SetMinutes") == 0) { - stack->correctParams(1); - _tm.tm_min = stack->pop()->getInt(); - stack->pushNULL(); - return STATUS_OK; - } - ////////////////////////////////////////////////////////////////////////// - // SetSeconds - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "SetSeconds") == 0) { - stack->correctParams(1); - _tm.tm_sec = stack->pop()->getInt(); - stack->pushNULL(); - return STATUS_OK; - } - - - ////////////////////////////////////////////////////////////////////////// - // SetCurrentTime - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "SetCurrentTime") == 0) { - stack->correctParams(0); - g_system->getTimeAndDate(_tm); - stack->pushNULL(); - return STATUS_OK; - } - - else - return STATUS_FAILED; -} - - -////////////////////////////////////////////////////////////////////////// -CScValue *CSXDate::scGetProperty(const char *name) { - _scValue->setNULL(); - - ////////////////////////////////////////////////////////////////////////// - // Type - ////////////////////////////////////////////////////////////////////////// - if (strcmp(name, "Type") == 0) { - _scValue->setString("date"); - return _scValue; - } - - else return _scValue; -} - - -////////////////////////////////////////////////////////////////////////// -bool CSXDate::scSetProperty(const char *name, CScValue *value) { - /* - ////////////////////////////////////////////////////////////////////////// - // Name - ////////////////////////////////////////////////////////////////////////// - if(strcmp(name, "Name")==0){ - setName(value->getString()); - return STATUS_OK; - } - - else*/ return STATUS_FAILED; -} - - -////////////////////////////////////////////////////////////////////////// -bool CSXDate::persist(CBPersistMgr *persistMgr) { - - CBScriptable::persist(persistMgr); - persistMgr->transfer(TMEMBER(_tm.tm_year)); - persistMgr->transfer(TMEMBER(_tm.tm_mon)); - persistMgr->transfer(TMEMBER(_tm.tm_mday)); - persistMgr->transfer(TMEMBER(_tm.tm_hour)); - persistMgr->transfer(TMEMBER(_tm.tm_min)); - persistMgr->transfer(TMEMBER(_tm.tm_sec)); - return STATUS_OK; -} - -////////////////////////////////////////////////////////////////////////// -int CSXDate::scCompare(CBScriptable *Value) { - TimeDate time1 = _tm; - TimeDate time2 = ((CSXDate *)Value)->_tm; - - if (time1.tm_year < time2.tm_year) { - return -1; - } else if (time1.tm_year == time2.tm_year) { - if (time1.tm_mon < time2.tm_mon) { - return -1; - } else if (time1.tm_mon == time2.tm_mon) { - if (time1.tm_mday < time2.tm_mday) { - return -1; - } else if (time1.tm_mday == time2.tm_mday) { - if (time1.tm_hour < time2.tm_hour) { - return -1; - } else if (time1.tm_hour == time2.tm_hour) { - if (time1.tm_min < time2.tm_min) { - return -1; - } else if (time1.tm_min == time2.tm_min) { - if (time1.tm_sec < time2.tm_sec) { - return -1; - } else if (time1.tm_sec == time2.tm_sec) { - return 0; // Equal - } else { - return 1; // Sec - } - } else { - return 1; // Minute - } - } else { - return 1; // Hour - } - } else { - return 1; // Day - } - } else { - return 1; // Month - } - } else { - return 1; // Year - } -} - -} // end of namespace WinterMute diff --git a/engines/wintermute/base/scriptables/SXDate.h b/engines/wintermute/base/scriptables/SXDate.h deleted file mode 100644 index df0641983f..0000000000 --- a/engines/wintermute/base/scriptables/SXDate.h +++ /dev/null @@ -1,55 +0,0 @@ -/* ScummVM - Graphic Adventure Engine - * - * ScummVM is the legal property of its developers, whose names - * are too numerous to list here. Please refer to the COPYRIGHT - * file distributed with this source distribution. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - */ - -/* - * This file is based on WME Lite. - * http://dead-code.org/redir.php?target=wmelite - * Copyright (c) 2011 Jan Nedoma - */ - -#ifndef WINTERMUTE_SXDATE_H -#define WINTERMUTE_SXDATE_H - -#include "common/system.h" -#include "engines/wintermute/base/BScriptable.h" - -namespace WinterMute { - -class CSXDate : public CBScriptable { -public: - int scCompare(CBScriptable *Value); - DECLARE_PERSISTENT(CSXDate, CBScriptable) - CSXDate(CBGame *inGame, CScStack *Stack); - virtual ~CSXDate(); - CScValue *scGetProperty(const char *name); - bool scSetProperty(const char *name, CScValue *value); - bool scCallMethod(CScScript *script, CScStack *stack, CScStack *thisStack, const char *name); - const char *scToString(); - char *_string; - TimeDate _tm; -private: - Common::String _strRep; -}; - -} // end of namespace WinterMute - -#endif diff --git a/engines/wintermute/base/scriptables/SXFile.cpp b/engines/wintermute/base/scriptables/SXFile.cpp deleted file mode 100644 index 1924a42d72..0000000000 --- a/engines/wintermute/base/scriptables/SXFile.cpp +++ /dev/null @@ -1,779 +0,0 @@ -/* ScummVM - Graphic Adventure Engine - * - * ScummVM is the legal property of its developers, whose names - * are too numerous to list here. Please refer to the COPYRIGHT - * file distributed with this source distribution. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - */ - -/* - * This file is based on WME Lite. - * http://dead-code.org/redir.php?target=wmelite - * Copyright (c) 2011 Jan Nedoma - */ - -#include "engines/wintermute/system/SysClassRegistry.h" -#include "engines/wintermute/system/SysClass.h" -#include "engines/wintermute/base/scriptables/ScStack.h" -#include "engines/wintermute/base/scriptables/ScValue.h" -#include "engines/wintermute/base/scriptables/ScScript.h" -#include "engines/wintermute/utils/utils.h" -#include "engines/wintermute/base/BGame.h" -#include "engines/wintermute/base/file/BFile.h" -#include "engines/wintermute/base/BFileManager.h" -#include "engines/wintermute/platform_osystem.h" -#include "engines/wintermute/base/scriptables/SXFile.h" - -// Note: This code is completely untested, as I have yet to find a game that uses SXFile. - -namespace WinterMute { - -IMPLEMENT_PERSISTENT(CSXFile, false) - -CBScriptable *makeSXFile(CBGame *inGame, CScStack *stack) { - return new CSXFile(inGame, stack); -} - -////////////////////////////////////////////////////////////////////////// -CSXFile::CSXFile(CBGame *inGame, CScStack *stack): CBScriptable(inGame) { - stack->correctParams(1); - CScValue *Val = stack->pop(); - - _filename = NULL; - if (!Val->isNULL()) CBUtils::setString(&_filename, Val->getString()); - - _readFile = NULL; - _writeFile = NULL; - - _mode = 0; - _textMode = false; -} - - -////////////////////////////////////////////////////////////////////////// -CSXFile::~CSXFile() { - cleanup(); -} - -////////////////////////////////////////////////////////////////////////// -void CSXFile::cleanup() { - delete[] _filename; - _filename = NULL; - close(); -} - - -////////////////////////////////////////////////////////////////////////// -void CSXFile::close() { - if (_readFile) { - _gameRef->_fileManager->closeFile(_readFile); - _readFile = NULL; - } - if (_writeFile) { - _writeFile->finalize(); - delete _writeFile; - _writeFile = NULL; - } - _mode = 0; - _textMode = false; -} - -////////////////////////////////////////////////////////////////////////// -const char *CSXFile::scToString() { - if (_filename) return _filename; - else return "[file object]"; -} - -#define FILE_BUFFER_SIZE 32768 -////////////////////////////////////////////////////////////////////////// -bool CSXFile::scCallMethod(CScScript *script, CScStack *stack, CScStack *thisStack, const char *name) { - ////////////////////////////////////////////////////////////////////////// - // SetFilename - ////////////////////////////////////////////////////////////////////////// - if (strcmp(name, "SetFilename") == 0) { - stack->correctParams(1); - const char *filename = stack->pop()->getString(); - cleanup(); - CBUtils::setString(&_filename, filename); - stack->pushNULL(); - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // OpenAsText / OpenAsBinary - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "OpenAsText") == 0 || strcmp(name, "OpenAsBinary") == 0) { - stack->correctParams(1); - close(); - _mode = stack->pop()->getInt(1); - if (_mode < 1 || _mode > 3) { - script->runtimeError("File.%s: invalid access mode. Setting read mode.", name); - _mode = 1; - } - if (_mode == 1) { - _readFile = _gameRef->_fileManager->openFile(_filename); - if (!_readFile) { - //script->runtimeError("File.%s: Error opening file '%s' for reading.", Name, _filename); - close(); - } else _textMode = strcmp(name, "OpenAsText") == 0; - } else { - if (strcmp(name, "OpenAsText") == 0) { - if (_mode == 2) _writeFile = openForWrite(_filename, false); - else _writeFile = openForAppend(_filename, false); - } else { - if (_mode == 2) _writeFile = openForWrite(_filename, true); - else _writeFile = openForAppend(_filename, true); - } - - if (!_writeFile) { - //script->runtimeError("File.%s: Error opening file '%s' for writing.", Name, _filename); - close(); - } else _textMode = strcmp(name, "OpenAsText") == 0; - } - - if (_readFile || _writeFile) stack->pushBool(true); - else stack->pushBool(false); - - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // Close - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "Close") == 0) { - stack->correctParams(0); - close(); - stack->pushNULL(); - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // SetPosition - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "SetPosition") == 0) { - stack->correctParams(1); - if (_mode == 0) { - script->runtimeError("File.%s: File is not open", name); - stack->pushBool(false); - } else { - int Pos = stack->pop()->getInt(); - stack->pushBool(setPos(Pos)); - } - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // Delete - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "Delete") == 0) { - stack->correctParams(0); - close(); - stack->pushBool(CBPlatform::deleteFile(_filename) != false); - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // Copy - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "Copy") == 0) { - stack->correctParams(2); - const char *Dest = stack->pop()->getString(); - bool Overwrite = stack->pop()->getBool(true); - - close(); - stack->pushBool(CBPlatform::copyFile(_filename, Dest, !Overwrite) != false); - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // ReadLine - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "ReadLine") == 0) { - stack->correctParams(0); - if (!_textMode || !_readFile) { - script->runtimeError("File.%s: File must be open in text mode.", name); - stack->pushNULL(); - return STATUS_OK; - } - uint32 bufSize = FILE_BUFFER_SIZE; - byte *buf = (byte *)malloc(bufSize); - uint32 counter = 0; - byte b; - bool foundNewLine = false; - bool ret = STATUS_FAILED; - do { - ret = _readFile->read(&b, 1); - if (ret != 1) break; - - if (counter > bufSize) { - buf = (byte *)realloc(buf, bufSize + FILE_BUFFER_SIZE); - bufSize += FILE_BUFFER_SIZE; - } - if (b == '\n') { - buf[counter] = '\0'; - foundNewLine = true; - break; - } else if (b == 0x0D) continue; - else { - buf[counter] = b; - counter++; - } - } while (DID_SUCCEED(ret)); - - if (counter > bufSize) { - buf = (byte *)realloc(buf, bufSize + FILE_BUFFER_SIZE); - bufSize += FILE_BUFFER_SIZE; - } - buf[counter] = '\0'; - - if (!foundNewLine && counter == 0) stack->pushNULL(); - else stack->pushString((char *)buf); - - free(buf); - - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // ReadText - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "ReadText") == 0) { - stack->correctParams(1); - int textLen = stack->pop()->getInt(); - - if (!_textMode || !_readFile) { - script->runtimeError("File.%s: File must be open in text mode.", name); - stack->pushNULL(); - return STATUS_OK; - } - uint32 bufSize = FILE_BUFFER_SIZE; - byte *buf = (byte *)malloc(bufSize); - uint32 counter = 0; - byte b; - - bool ret = STATUS_FAILED; - while (counter < (uint32)textLen) { - ret = _readFile->read(&b, 1); - if (ret != 1) break; - - if (counter > bufSize) { - buf = (byte *)realloc(buf, bufSize + FILE_BUFFER_SIZE); - bufSize += FILE_BUFFER_SIZE; - } - if (b == 0x0D) continue; - else { - buf[counter] = b; - counter++; - } - } - - if (counter > bufSize) { - buf = (byte *)realloc(buf, bufSize + FILE_BUFFER_SIZE); - bufSize += FILE_BUFFER_SIZE; - } - buf[counter] = '\0'; - - if (textLen > 0 && counter == 0) stack->pushNULL(); - else stack->pushString((char *)buf); - - free(buf); - - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // WriteLine / WriteText - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "WriteLine") == 0 || strcmp(name, "WriteText") == 0) { - stack->correctParams(1); - const char *line = stack->pop()->getString(); - if (!_textMode || !_writeFile) { - script->runtimeError("File.%s: File must be open for writing in text mode.", name); - stack->pushBool(false); - return STATUS_OK; - } - Common::String writeLine; - if (strcmp(name, "WriteLine") == 0) { - writeLine = Common::String::format("%s\n", line); - } else { - writeLine = Common::String::format("%s", line); - } - _writeFile->writeString(writeLine); - _writeFile->writeByte(0); - stack->pushBool(true); - - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - ////////////////////////////////////////////////////////////////////////// - // ReadBool - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "ReadBool") == 0) { - stack->correctParams(0); - if (_textMode || !_readFile) { - script->runtimeError("File.%s: File must be open for reading in binary mode.", name); - stack->pushNULL(); - return STATUS_OK; - } - bool val; - if (_readFile->read(&val, sizeof(bool)) == sizeof(bool)) stack->pushBool(val); - else stack->pushNULL(); - - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // ReadByte - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "ReadByte") == 0) { - stack->correctParams(0); - if (_textMode || !_readFile) { - script->runtimeError("File.%s: File must be open for reading in binary mode.", name); - stack->pushNULL(); - return STATUS_OK; - } - byte val = _readFile->readByte(); - if (!_readFile->err()) { - stack->pushInt(val); - } else { - stack->pushNULL(); - } - - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // ReadShort - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "ReadShort") == 0) { - stack->correctParams(0); - if (_textMode || !_readFile) { - script->runtimeError("File.%s: File must be open for reading in binary mode.", name); - stack->pushNULL(); - return STATUS_OK; - } - int16 val = _readFile->readSint16LE(); - if (!_readFile->err()) { - stack->pushInt(65536 + val); - } else { - stack->pushNULL(); - } - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // ReadInt / ReadLong - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "ReadInt") == 0 || strcmp(name, "ReadLong") == 0) { - stack->correctParams(0); - if (_textMode || !_readFile) { - script->runtimeError("File.%s: File must be open for reading in binary mode.", name); - stack->pushNULL(); - return STATUS_OK; - } - int32 val = _readFile->readSint32LE(); - if (!_readFile->err()) { - stack->pushInt(val); - } else { - stack->pushNULL(); - } - - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // ReadFloat - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "ReadFloat") == 0) { - stack->correctParams(0); - if (_textMode || !_readFile) { - script->runtimeError("File.%s: File must be open for reading in binary mode.", name); - stack->pushNULL(); - return STATUS_OK; - } - float val; - (*(uint32*)&val) = _readFile->readUint32LE(); - if (!_readFile->err()) { - stack->pushFloat(val); - } else { - stack->pushNULL(); - } - - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // ReadDouble - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "ReadDouble") == 0) { // TODO: Solve reading a 8 byte double. - error("SXFile::ReadDouble - Not endian safe yet"); - stack->correctParams(0); - if (_textMode || !_readFile) { - script->runtimeError("File.%s: File must be open for reading in binary mode.", name); - stack->pushNULL(); - return STATUS_OK; - } - double val; - if (_readFile->read(&val, sizeof(double)) == sizeof(double)) stack->pushFloat(val); - else stack->pushNULL(); - - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // ReadString - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "ReadString") == 0) { - stack->correctParams(0); - if (_textMode || !_readFile) { - script->runtimeError("File.%s: File must be open for reading in binary mode.", name); - stack->pushNULL(); - return STATUS_OK; - } - uint32 size = _readFile->readUint32LE(); - if (!_readFile->err()) { - byte *str = new byte[size + 1]; - if (str) { - if (_readFile->read(str, size) == size) { - str[size] = '\0'; - stack->pushString((char *)str); - } - delete [] str; - } else stack->pushNULL(); - } else stack->pushNULL(); - - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // WriteBool - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "WriteBool") == 0) { - stack->correctParams(1); - bool val = stack->pop()->getBool(); - - if (_textMode || !_writeFile) { - script->runtimeError("File.%s: File must be open for writing in binary mode.", name); - stack->pushBool(false); - return STATUS_OK; - } - _writeFile->writeByte(val); - stack->pushBool(true); - - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // WriteByte - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "WriteByte") == 0) { - stack->correctParams(1); - byte val = stack->pop()->getInt(); - - if (_textMode || !_writeFile) { - script->runtimeError("File.%s: File must be open for writing in binary mode.", name); - stack->pushBool(false); - return STATUS_OK; - } - _writeFile->writeByte(val); - stack->pushBool(true); - - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // WriteShort - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "WriteShort") == 0) { - stack->correctParams(1); - int16 val = stack->pop()->getInt(); - - if (_textMode || !_writeFile) { - script->runtimeError("File.%s: File must be open for writing in binary mode.", name); - stack->pushBool(false); - return STATUS_OK; - } - _writeFile->writeSint16LE(val); - stack->pushBool(true); - - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // WriteInt / WriteLong - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "WriteInt") == 0 || strcmp(name, "WriteLong") == 0) { - stack->correctParams(1); - int32 val = stack->pop()->getInt(); - - if (_textMode || !_writeFile) { - script->runtimeError("File.%s: File must be open for writing in binary mode.", name); - stack->pushBool(false); - return STATUS_OK; - } - _writeFile->writeSint32LE(val); - stack->pushBool(true); - - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // WriteFloat - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "WriteFloat") == 0) { - stack->correctParams(1); - float val = stack->pop()->getFloat(); - - if (_textMode || !_writeFile) { - script->runtimeError("File.%s: File must be open for writing in binary mode.", name); - stack->pushBool(false); - return STATUS_OK; - } - uint32 *ptr = (uint32*)&val; - _writeFile->writeUint32LE(*ptr); - stack->pushBool(true); - - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // WriteDouble - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "WriteDouble") == 0) { - error("SXFile::WriteDouble - Not endian safe yet"); - stack->correctParams(1); - double val = stack->pop()->getFloat(); - - if (_textMode || !_writeFile) { - script->runtimeError("File.%s: File must be open for writing in binary mode.", name); - stack->pushBool(false); - return STATUS_OK; - } - //fwrite(&val, sizeof(val), 1, (FILE *)_writeFile); - stack->pushBool(true); - - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // WriteString - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "WriteString") == 0) { - stack->correctParams(1); - const char *val = stack->pop()->getString(); - - if (_textMode || !_writeFile) { - script->runtimeError("File.%s: File must be open for writing in binary mode.", name); - stack->pushBool(false); - return STATUS_OK; - } - - uint32 size = strlen(val); - _writeFile->writeUint32LE(size); - _writeFile->writeString(val); - - stack->pushBool(true); - - return STATUS_OK; - } - - - else return CBScriptable::scCallMethod(script, stack, thisStack, name); -} - - -////////////////////////////////////////////////////////////////////////// -CScValue *CSXFile::scGetProperty(const char *name) { - _scValue->setNULL(); - - ////////////////////////////////////////////////////////////////////////// - // Type (RO) - ////////////////////////////////////////////////////////////////////////// - if (strcmp(name, "Type") == 0) { - _scValue->setString("file"); - return _scValue; - } - - ////////////////////////////////////////////////////////////////////////// - // Filename (RO) - ////////////////////////////////////////////////////////////////////////// - if (strcmp(name, "Filename") == 0) { - _scValue->setString(_filename); - return _scValue; - } - - ////////////////////////////////////////////////////////////////////////// - // Position (RO) - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "Position") == 0) { - _scValue->setInt(getPos()); - return _scValue; - } - - ////////////////////////////////////////////////////////////////////////// - // Length (RO) - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "Length") == 0) { - _scValue->setInt(getLength()); - return _scValue; - } - - ////////////////////////////////////////////////////////////////////////// - // TextMode (RO) - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "TextMode") == 0) { - _scValue->setBool(_textMode); - return _scValue; - } - - ////////////////////////////////////////////////////////////////////////// - // AccessMode (RO) - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "AccessMode") == 0) { - _scValue->setInt(_mode); - return _scValue; - } - - else return CBScriptable::scGetProperty(name); -} - - -////////////////////////////////////////////////////////////////////////// -bool CSXFile::scSetProperty(const char *name, CScValue *value) { - /* - ////////////////////////////////////////////////////////////////////////// - // Length - ////////////////////////////////////////////////////////////////////////// - if(strcmp(name, "Length")==0){ - int OrigLength = _length; - _length = max(value->getInt(0), 0); - - char PropName[20]; - if(_length < OrigLength){ - for(int i=_length; iDeleteProp(PropName); - } - } - return STATUS_OK; - } - else*/ return CBScriptable::scSetProperty(name, value); -} - -////////////////////////////////////////////////////////////////////////// -uint32 CSXFile::getPos() { - if (_mode == 1 && _readFile) - return _readFile->pos(); - else if ((_mode == 2 || _mode == 3) && _writeFile) { - error("SXFile - getPos for WriteFile not supported"); - return 0; -// return ftell((FILE *)_writeFile); - } else { - return 0; - } -} - -////////////////////////////////////////////////////////////////////////// -bool CSXFile::setPos(uint32 pos, int whence) { - if (_mode == 1 && _readFile) - return _readFile->seek(pos, whence); - else if ((_mode == 2 || _mode == 3) && _writeFile) { - error("CSXFile - seeking in WriteFile not supported"); - return false; -// return fseek((FILE *)_writeFile, pos, (int)origin) == 0; - } - else return false; -} - -////////////////////////////////////////////////////////////////////////// -uint32 CSXFile::getLength() { - if (_mode == 1 && _readFile) - return _readFile->size(); - else if ((_mode == 2 || _mode == 3) && _writeFile) { - error("CSXFile - reading length for WriteFile not supported"); - return 0; -/* - uint32 currentPos = ftell((FILE *)_writeFile); - fseek((FILE *)_writeFile, 0, SEEK_END); - int ret = ftell((FILE *)_writeFile); - fseek((FILE *)_writeFile, CurrentPos, SEEK_SET); - return Ret;*/ - } else return 0; -} - -////////////////////////////////////////////////////////////////////////// -bool CSXFile::persist(CBPersistMgr *persistMgr) { - - CBScriptable::persist(persistMgr); - - persistMgr->transfer(TMEMBER(_filename)); - persistMgr->transfer(TMEMBER(_mode)); - persistMgr->transfer(TMEMBER(_textMode)); - - uint32 pos = 0; - if (persistMgr->_saving) { - pos = getPos(); - persistMgr->transfer(TMEMBER(pos)); - } else { - persistMgr->transfer(TMEMBER(pos)); - - // try to re-open file if needed - _writeFile = NULL; - _readFile = NULL; - - if (_mode != 0) { - // open for reading - if (_mode == 1) { - _readFile = _gameRef->_fileManager->openFile(_filename); - if (!_readFile) - close(); - } - // open for writing / appending - else { - if (_textMode) { - if (_mode == 2) - _writeFile = openForWrite(_filename, false); - else - _writeFile = openForAppend(_filename, false); - } else { - if (_mode == 2) - _writeFile = openForWrite(_filename, true); - else - _writeFile = openForAppend(_filename, true); - } - if (_writeFile) - close(); - } - setPos(pos); - } - } - - return STATUS_OK; -} - -// Should replace fopen(..., "wb+") and fopen(..., "w+") -Common::WriteStream *CSXFile::openForWrite(const Common::String &filename, bool binary) { - error("SXFile::openForWrite - WriteFiles not supported"); -} - -// Should replace fopen(..., "ab+") and fopen(..., "a+") -Common::WriteStream *CSXFile::openForAppend(const Common::String &filename, bool binary) { - error("SXFile::openForAppend - WriteFiles not supported"); -} - -} // end of namespace WinterMute diff --git a/engines/wintermute/base/scriptables/SXFile.h b/engines/wintermute/base/scriptables/SXFile.h deleted file mode 100644 index 709d1f4378..0000000000 --- a/engines/wintermute/base/scriptables/SXFile.h +++ /dev/null @@ -1,66 +0,0 @@ -/* ScummVM - Graphic Adventure Engine - * - * ScummVM is the legal property of its developers, whose names - * are too numerous to list here. Please refer to the COPYRIGHT - * file distributed with this source distribution. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - */ - -/* - * This file is based on WME Lite. - * http://dead-code.org/redir.php?target=wmelite - * Copyright (c) 2011 Jan Nedoma - */ - -#ifndef WINTERMUTES_SXFILE_H -#define WINTERMUTES_SXFILE_H - - -#include "engines/wintermute/base/BScriptable.h" -#include "common/stream.h" - -namespace WinterMute { - -class CBFile; - -class CSXFile : public CBScriptable { -public: - DECLARE_PERSISTENT(CSXFile, CBScriptable) - CScValue *scGetProperty(const char *name); - bool scSetProperty(const char *name, CScValue *value); - bool scCallMethod(CScScript *script, CScStack *stack, CScStack *thisStack, const char *name); - const char *scToString(); - CSXFile(CBGame *inGame, CScStack *Stack); - virtual ~CSXFile(); -private: - Common::SeekableReadStream *_readFile; - Common::WriteStream *_writeFile; - int _mode; // 0..none, 1..read, 2..write, 3..append - bool _textMode; - void close(); - void cleanup(); - uint32 getPos(); - uint32 getLength(); - bool setPos(uint32 Pos, int whence = SEEK_SET); - char *_filename; - Common::WriteStream *openForWrite(const Common::String &filename, bool binary); - Common::WriteStream *openForAppend(const Common::String &filename, bool binary); -}; - -} // end of namespace WinterMute - -#endif diff --git a/engines/wintermute/base/scriptables/SXMath.cpp b/engines/wintermute/base/scriptables/SXMath.cpp deleted file mode 100644 index fb2838ee94..0000000000 --- a/engines/wintermute/base/scriptables/SXMath.cpp +++ /dev/null @@ -1,295 +0,0 @@ -/* ScummVM - Graphic Adventure Engine - * - * ScummVM is the legal property of its developers, whose names - * are too numerous to list here. Please refer to the COPYRIGHT - * file distributed with this source distribution. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - */ - -/* - * This file is based on WME Lite. - * http://dead-code.org/redir.php?target=wmelite - * Copyright (c) 2011 Jan Nedoma - */ - -#include "engines/wintermute/base/scriptables/SXMath.h" -#include "engines/wintermute/base/scriptables/ScStack.h" -#include "engines/wintermute/base/scriptables/ScValue.h" -#include "engines/wintermute/persistent.h" -#include "common/math.h" -#include - -namespace WinterMute { - -////////////////////////////////////////////////////////////////////// -// Construction/Destruction -////////////////////////////////////////////////////////////////////// - - -IMPLEMENT_PERSISTENT(CSXMath, true) - -CBScriptable *makeSXMath(CBGame *inGame) { - return new CSXMath(inGame); -} - -////////////////////////////////////////////////////////////////////////// -CSXMath::CSXMath(CBGame *inGame): CBScriptable(inGame) { - -} - - -////////////////////////////////////////////////////////////////////////// -CSXMath::~CSXMath() { - -} - - -////////////////////////////////////////////////////////////////////////// -bool CSXMath::scCallMethod(CScScript *script, CScStack *stack, CScStack *thisStack, const char *name) { - ////////////////////////////////////////////////////////////////////////// - // Abs - ////////////////////////////////////////////////////////////////////////// - if (strcmp(name, "Abs") == 0) { - stack->correctParams(1); - stack->pushFloat(fabs(stack->pop()->getFloat())); - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // Acos - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "Acos") == 0) { - stack->correctParams(1); - stack->pushFloat(acos(stack->pop()->getFloat())); - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // Asin - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "Asin") == 0) { - stack->correctParams(1); - stack->pushFloat(asin(stack->pop()->getFloat())); - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // Atan - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "Atan") == 0) { - stack->correctParams(1); - stack->pushFloat(atan(stack->pop()->getFloat())); - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // Atan2 - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "Atan2") == 0) { - stack->correctParams(2); - double y = stack->pop()->getFloat(); - double x = stack->pop()->getFloat(); - stack->pushFloat(atan2(y, x)); - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // Ceil - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "Ceil") == 0) { - stack->correctParams(1); - stack->pushFloat(ceil(stack->pop()->getFloat())); - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // Cos - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "Cos") == 0) { - stack->correctParams(1); - stack->pushFloat(cos(degreeToRadian(stack->pop()->getFloat()))); - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // Cosh - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "Cosh") == 0) { - stack->correctParams(1); - stack->pushFloat(cosh(degreeToRadian(stack->pop()->getFloat()))); - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // Exp - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "Exp") == 0) { - stack->correctParams(1); - stack->pushFloat(exp(stack->pop()->getFloat())); - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // Floor - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "Floor") == 0) { - stack->correctParams(1); - stack->pushFloat(floor(stack->pop()->getFloat())); - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // Log - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "Log") == 0) { - stack->correctParams(1); - stack->pushFloat(log(stack->pop()->getFloat())); - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // Log10 - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "Log10") == 0) { - stack->correctParams(1); - stack->pushFloat(log10(stack->pop()->getFloat())); - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // Pow - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "Pow") == 0) { - stack->correctParams(2); - double x = stack->pop()->getFloat(); - double y = stack->pop()->getFloat(); - - stack->pushFloat(pow(x, y)); - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // Sin - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "Sin") == 0) { - stack->correctParams(1); - stack->pushFloat(sin(degreeToRadian(stack->pop()->getFloat()))); - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // Sinh - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "Sinh") == 0) { - stack->correctParams(1); - stack->pushFloat(sinh(degreeToRadian(stack->pop()->getFloat()))); - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // Tan - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "Tan") == 0) { - stack->correctParams(1); - stack->pushFloat(tan(degreeToRadian(stack->pop()->getFloat()))); - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // Tanh - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "Tanh") == 0) { - stack->correctParams(1); - stack->pushFloat(tanh(degreeToRadian(stack->pop()->getFloat()))); - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // Sqrt - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "Sqrt") == 0) { - stack->correctParams(1); - stack->pushFloat(sqrt(stack->pop()->getFloat())); - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // DegToRad - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "DegToRad") == 0) { - stack->correctParams(1); - stack->pushFloat(degreeToRadian(stack->pop()->getFloat())); - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // RadToDeg - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "RadToDeg") == 0) { - stack->correctParams(1); - stack->pushFloat(radianToDegree(stack->pop()->getFloat())); - return STATUS_OK; - } - - else return STATUS_FAILED; -} - - -////////////////////////////////////////////////////////////////////////// -CScValue *CSXMath::scGetProperty(const char *name) { - _scValue->setNULL(); - - ////////////////////////////////////////////////////////////////////////// - // Type - ////////////////////////////////////////////////////////////////////////// - if (strcmp(name, "Type") == 0) { - _scValue->setString("math"); - return _scValue; - } - - ////////////////////////////////////////////////////////////////////////// - // PI - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "PI") == 0) { - _scValue->setFloat(M_PI); - return _scValue; - } - - else return _scValue; -} - - -////////////////////////////////////////////////////////////////////////// -double CSXMath::degreeToRadian(double value) { - return value * (M_PI / 180.0f); -} - - -////////////////////////////////////////////////////////////////////////// -double CSXMath::radianToDegree(double value) { - return value * (180.0f / M_PI); -} - - -////////////////////////////////////////////////////////////////////////// -bool CSXMath::persist(CBPersistMgr *persistMgr) { - - CBScriptable::persist(persistMgr); - return STATUS_OK; -} - -} // end of namespace WinterMute diff --git a/engines/wintermute/base/scriptables/SXMath.h b/engines/wintermute/base/scriptables/SXMath.h deleted file mode 100644 index 4389de611f..0000000000 --- a/engines/wintermute/base/scriptables/SXMath.h +++ /dev/null @@ -1,53 +0,0 @@ -/* ScummVM - Graphic Adventure Engine - * - * ScummVM is the legal property of its developers, whose names - * are too numerous to list here. Please refer to the COPYRIGHT - * file distributed with this source distribution. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - */ - -/* - * This file is based on WME Lite. - * http://dead-code.org/redir.php?target=wmelite - * Copyright (c) 2011 Jan Nedoma - */ - -#ifndef WINTERMUTE_SXMATH_H -#define WINTERMUTE_SXMATH_H - - -#include "engines/wintermute/base/BScriptable.h" - -namespace WinterMute { - -class CSXMath : public CBScriptable { -public: - DECLARE_PERSISTENT(CSXMath, CBScriptable) - CSXMath(CBGame *inGame); - virtual ~CSXMath(); - virtual CScValue *scGetProperty(const char *name); - virtual bool scCallMethod(CScScript *script, CScStack *stack, CScStack *thisStack, const char *name); - -private: - double degreeToRadian(double value); - double radianToDegree(double value); - -}; - -} // end of namespace WinterMute - -#endif diff --git a/engines/wintermute/base/scriptables/SXMemBuffer.cpp b/engines/wintermute/base/scriptables/SXMemBuffer.cpp deleted file mode 100644 index 9ac98ab11d..0000000000 --- a/engines/wintermute/base/scriptables/SXMemBuffer.cpp +++ /dev/null @@ -1,508 +0,0 @@ -/* ScummVM - Graphic Adventure Engine - * - * ScummVM is the legal property of its developers, whose names - * are too numerous to list here. Please refer to the COPYRIGHT - * file distributed with this source distribution. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - */ - -/* - * This file is based on WME Lite. - * http://dead-code.org/redir.php?target=wmelite - * Copyright (c) 2011 Jan Nedoma - */ - -#include "engines/wintermute/base/BScriptable.h" -#include "engines/wintermute/base/scriptables/ScStack.h" -#include "engines/wintermute/base/scriptables/ScScript.h" -#include "engines/wintermute/base/scriptables/ScValue.h" -#include "engines/wintermute/base/scriptables/SXMemBuffer.h" -#include "common/file.h" - -namespace WinterMute { - -IMPLEMENT_PERSISTENT(CSXMemBuffer, false) - -CBScriptable *makeSXMemBuffer(CBGame *inGame, CScStack *stack) { - return new CSXMemBuffer(inGame, stack); -} - -////////////////////////////////////////////////////////////////////////// -CSXMemBuffer::CSXMemBuffer(CBGame *inGame, CScStack *stack): CBScriptable(inGame) { - stack->correctParams(1); - _buffer = NULL; - _size = 0; - - int NewSize = stack->pop()->getInt(); - resize(MAX(0, NewSize)); -} - -////////////////////////////////////////////////////////////////////////// -CSXMemBuffer::CSXMemBuffer(CBGame *inGame, void *Buffer): CBScriptable(inGame) { - _size = 0; - _buffer = Buffer; -} - - -////////////////////////////////////////////////////////////////////////// -CSXMemBuffer::~CSXMemBuffer() { - cleanup(); -} - -////////////////////////////////////////////////////////////////////////// -void *CSXMemBuffer::scToMemBuffer() { - return _buffer; -} - -////////////////////////////////////////////////////////////////////////// -void CSXMemBuffer::cleanup() { - if (_size) free(_buffer); - _buffer = NULL; - _size = 0; -} - -////////////////////////////////////////////////////////////////////////// -bool CSXMemBuffer::resize(int newSize) { - int oldSize = _size; - - if (_size == 0) { - _buffer = malloc(newSize); - if (_buffer) _size = newSize; - } else { - void *newBuf = realloc(_buffer, newSize); - if (!newBuf) { - if (newSize == 0) { - _buffer = newBuf; - _size = newSize; - } else return STATUS_FAILED; - } else { - _buffer = newBuf; - _size = newSize; - } - } - - if (_buffer && _size > oldSize) { - memset((byte *)_buffer + oldSize, 0, _size - oldSize); - } - return STATUS_OK; -} - -////////////////////////////////////////////////////////////////////////// -bool CSXMemBuffer::checkBounds(CScScript *script, int start, int length) { - if (_buffer == NULL) { - script->runtimeError("Cannot use Set/Get methods on an uninitialized memory buffer"); - return false; - } - if (_size == 0) - return true; - - if (start < 0 || length == 0 || start + length > _size) { - script->runtimeError("Set/Get method call is out of bounds"); - return false; - } else - return true; -} - -////////////////////////////////////////////////////////////////////////// -const char *CSXMemBuffer::scToString() { - return "[membuffer object]"; -} - - -////////////////////////////////////////////////////////////////////////// -bool CSXMemBuffer::scCallMethod(CScScript *script, CScStack *stack, CScStack *thisStack, const char *name) { - ////////////////////////////////////////////////////////////////////////// - // SetSize - ////////////////////////////////////////////////////////////////////////// - if (strcmp(name, "SetSize") == 0) { - stack->correctParams(1); - int newSize = stack->pop()->getInt(); - newSize = MAX(0, newSize); - if (DID_SUCCEED(resize(newSize))) - stack->pushBool(true); - else - stack->pushBool(false); - - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // GetBool - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "GetBool") == 0) { - stack->correctParams(1); - int start = stack->pop()->getInt(); - if (!checkBounds(script, start, sizeof(bool))) - stack->pushNULL(); - else - stack->pushBool(*(bool *)((byte *)_buffer + start)); - - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // GetByte - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "GetByte") == 0) { - stack->correctParams(1); - int start = stack->pop()->getInt(); - if (!checkBounds(script, start, sizeof(byte))) - stack->pushNULL(); - else - stack->pushInt(*(byte *)((byte *)_buffer + start)); - - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // GetShort - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "GetShort") == 0) { - stack->correctParams(1); - int Start = stack->pop()->getInt(); - if (!checkBounds(script, Start, sizeof(short))) - stack->pushNULL(); - else - stack->pushInt(65536 + * (short *)((byte *)_buffer + Start)); - - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // GetInt / GetLong - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "GetInt") == 0 || strcmp(name, "GetLong") == 0) { - stack->correctParams(1); - int start = stack->pop()->getInt(); - if (!checkBounds(script, start, sizeof(int))) - stack->pushNULL(); - else - stack->pushInt(*(int *)((byte *)_buffer + start)); - - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // GetFloat - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "GetFloat") == 0) { - stack->correctParams(1); - int start = stack->pop()->getInt(); - if (!checkBounds(script, start, sizeof(float))) - stack->pushNULL(); - else - stack->pushFloat(*(float *)((byte *)_buffer + start)); - - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // GetDouble - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "GetDouble") == 0) { - stack->correctParams(1); - int start = stack->pop()->getInt(); - if (!checkBounds(script, start, sizeof(double))) - stack->pushNULL(); - else - stack->pushFloat(*(double *)((byte *)_buffer + start)); - - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // GetString - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "GetString") == 0) { - stack->correctParams(2); - int start = stack->pop()->getInt(); - int length = stack->pop()->getInt(); - - // find end of string - if (length == 0 && start >= 0 && start < _size) { - for (int i = start; i < _size; i++) { - if (((char *)_buffer)[i] == '\0') { - length = i - start; - break; - } - } - } - - if (!checkBounds(script, start, length)) - stack->pushNULL(); - else { - char *str = new char[length + 1]; - strncpy(str, (const char *)_buffer + start, length); - str[length] = '\0'; - stack->pushString(str); - delete [] str; - } - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // GetPointer - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "GetPointer") == 0) { - stack->correctParams(1); - int start = stack->pop()->getInt(); - if (!checkBounds(script, start, sizeof(void *))) - stack->pushNULL(); - else { - void *pointer = *(void **)((byte *)_buffer + start); - CSXMemBuffer *buf = new CSXMemBuffer(_gameRef, pointer); - stack->pushNative(buf, false); - } - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // SetBool - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "SetBool") == 0) { - stack->correctParams(2); - int start = stack->pop()->getInt(); - bool val = stack->pop()->getBool(); - - if (!checkBounds(script, start, sizeof(bool))) - stack->pushBool(false); - else { - *(bool *)((byte *)_buffer + start) = val; - stack->pushBool(true); - } - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // SetByte - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "SetByte") == 0) { - stack->correctParams(2); - int start = stack->pop()->getInt(); - byte val = (byte)stack->pop()->getInt(); - - if (!checkBounds(script, start, sizeof(byte))) - stack->pushBool(false); - else { - *(byte *)((byte *)_buffer + start) = val; - stack->pushBool(true); - } - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // SetShort - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "SetShort") == 0) { - stack->correctParams(2); - int start = stack->pop()->getInt(); - short val = (short)stack->pop()->getInt(); - - if (!checkBounds(script, start, sizeof(short))) - stack->pushBool(false); - else { - *(short *)((byte *)_buffer + start) = val; - stack->pushBool(true); - } - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // SetInt / SetLong - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "SetInt") == 0 || strcmp(name, "SetLong") == 0) { - stack->correctParams(2); - int start = stack->pop()->getInt(); - int val = stack->pop()->getInt(); - - if (!checkBounds(script, start, sizeof(int))) - stack->pushBool(false); - else { - *(int *)((byte *)_buffer + start) = val; - stack->pushBool(true); - } - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // SetFloat - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "SetFloat") == 0) { - stack->correctParams(2); - int start = stack->pop()->getInt(); - float val = (float)stack->pop()->getFloat(); - - if (!checkBounds(script, start, sizeof(float))) - stack->pushBool(false); - else { - *(float *)((byte *)_buffer + start) = val; - stack->pushBool(true); - } - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // SetDouble - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "SetDouble") == 0) { - stack->correctParams(2); - int start = stack->pop()->getInt(); - double val = stack->pop()->getFloat(); - - if (!checkBounds(script, start, sizeof(double))) - stack->pushBool(false); - else { - *(double *)((byte *)_buffer + start) = val; - stack->pushBool(true); - } - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // SetString - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "SetString") == 0) { - stack->correctParams(2); - int start = stack->pop()->getInt(); - const char *val = stack->pop()->getString(); - - if (!checkBounds(script, start, strlen(val) + 1)) - stack->pushBool(false); - else { - memcpy((byte *)_buffer + start, val, strlen(val) + 1); - stack->pushBool(true); - } - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // SetPointer - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "SetPointer") == 0) { - stack->correctParams(2); - int start = stack->pop()->getInt(); - /* CScValue *Val = */ stack->pop(); - - if (!checkBounds(script, start, sizeof(void *))) - stack->pushBool(false); - else { - /* - int Pointer = (int)Val->getMemBuffer(); - memcpy((byte *)_buffer+Start, &Pointer, sizeof(void*)); - stack->pushBool(true); - */ - // TODO fix - stack->pushBool(false); - - } - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // DEBUG_Dump - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "DEBUG_Dump") == 0) { - stack->correctParams(0); - if (_buffer && _size) { - warning("SXMemBuffer::ScCallMethod - DEBUG_Dump"); - Common::DumpFile f; - f.open("buffer.bin"); - f.write(_buffer, _size); - f.close(); - } - stack->pushNULL(); - return STATUS_OK; - } - - else return STATUS_FAILED; -} - - -////////////////////////////////////////////////////////////////////////// -CScValue *CSXMemBuffer::scGetProperty(const char *name) { - _scValue->setNULL(); - - ////////////////////////////////////////////////////////////////////////// - // Type (RO) - ////////////////////////////////////////////////////////////////////////// - if (strcmp(name, "Type") == 0) { - _scValue->setString("membuffer"); - return _scValue; - } - - ////////////////////////////////////////////////////////////////////////// - // Size (RO) - ////////////////////////////////////////////////////////////////////////// - if (strcmp(name, "Size") == 0) { - _scValue->setInt(_size); - return _scValue; - } - - else return CBScriptable::scGetProperty(name); -} - - -////////////////////////////////////////////////////////////////////////// -bool CSXMemBuffer::scSetProperty(const char *name, CScValue *value) { - /* - ////////////////////////////////////////////////////////////////////////// - // Length - ////////////////////////////////////////////////////////////////////////// - if(strcmp(name, "Length")==0){ - int OrigLength = _length; - _length = max(value->getInt(0), 0); - - char PropName[20]; - if(_length < OrigLength){ - for(int i=_length; iDeleteProp(PropName); - } - } - return STATUS_OK; - } - else*/ return CBScriptable::scSetProperty(name, value); -} - - -////////////////////////////////////////////////////////////////////////// -bool CSXMemBuffer::persist(CBPersistMgr *persistMgr) { - - CBScriptable::persist(persistMgr); - - persistMgr->transfer(TMEMBER(_size)); - - if (persistMgr->_saving) { - if (_size > 0) persistMgr->putBytes((byte *)_buffer, _size); - } else { - if (_size > 0) { - _buffer = malloc(_size); - persistMgr->getBytes((byte *)_buffer, _size); - } else _buffer = NULL; - } - - return STATUS_OK; -} - - -////////////////////////////////////////////////////////////////////////// -int CSXMemBuffer::scCompare(CBScriptable *val) { - if (_buffer == val->scToMemBuffer()) return 0; - else return 1; -} - -} // end of namespace WinterMute diff --git a/engines/wintermute/base/scriptables/SXMemBuffer.h b/engines/wintermute/base/scriptables/SXMemBuffer.h deleted file mode 100644 index 09831bf464..0000000000 --- a/engines/wintermute/base/scriptables/SXMemBuffer.h +++ /dev/null @@ -1,59 +0,0 @@ -/* ScummVM - Graphic Adventure Engine - * - * ScummVM is the legal property of its developers, whose names - * are too numerous to list here. Please refer to the COPYRIGHT - * file distributed with this source distribution. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - */ - -/* - * This file is based on WME Lite. - * http://dead-code.org/redir.php?target=wmelite - * Copyright (c) 2011 Jan Nedoma - */ - -#ifndef WINTERMUTE_SXMEMBUFFER_H -#define WINTERMUTE_SXMEMBUFFER_H - - -#include "engines/wintermute/base/BScriptable.h" - -namespace WinterMute { - -class CSXMemBuffer : public CBScriptable { -public: - virtual int scCompare(CBScriptable *Val); - DECLARE_PERSISTENT(CSXMemBuffer, CBScriptable) - CScValue *scGetProperty(const char *name); - bool scSetProperty(const char *name, CScValue *value); - bool scCallMethod(CScScript *script, CScStack *stack, CScStack *thisStack, const char *name); - const char *scToString(); - CSXMemBuffer(CBGame *inGame, CScStack *stack); - CSXMemBuffer(CBGame *inGame, void *buffer); - virtual ~CSXMemBuffer(); - virtual void *scToMemBuffer(); - int _size; -private: - bool resize(int newSize); - void *_buffer; - void cleanup(); - bool checkBounds(CScScript *script, int start, int length); -}; - -} // end of namespace WinterMute - -#endif diff --git a/engines/wintermute/base/scriptables/SXString.cpp b/engines/wintermute/base/scriptables/SXString.cpp deleted file mode 100644 index ed3d243cb0..0000000000 --- a/engines/wintermute/base/scriptables/SXString.cpp +++ /dev/null @@ -1,404 +0,0 @@ -/* ScummVM - Graphic Adventure Engine - * - * ScummVM is the legal property of its developers, whose names - * are too numerous to list here. Please refer to the COPYRIGHT - * file distributed with this source distribution. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - */ - -/* - * This file is based on WME Lite. - * http://dead-code.org/redir.php?target=wmelite - * Copyright (c) 2011 Jan Nedoma - */ - -#include "engines/wintermute/base/BGame.h" -#include "engines/wintermute/base/scriptables/ScStack.h" -#include "engines/wintermute/base/scriptables/ScValue.h" -#include "engines/wintermute/utils/utils.h" -#include "engines/wintermute/base/scriptables/SXString.h" -#include "engines/wintermute/base/scriptables/SXArray.h" -#include "engines/wintermute/utils/StringUtil.h" -#include "common/tokenizer.h" - -namespace WinterMute { - -IMPLEMENT_PERSISTENT(CSXString, false) - -CBScriptable *makeSXString(CBGame *inGame, CScStack *stack) { - return new CSXString(inGame, stack); -} - -////////////////////////////////////////////////////////////////////////// -CSXString::CSXString(CBGame *inGame, CScStack *stack): CBScriptable(inGame) { - _string = NULL; - _capacity = 0; - - stack->correctParams(1); - CScValue *val = stack->pop(); - - if (val->isInt()) { - _capacity = MAX(0, val->getInt()); - if (_capacity > 0) { - _string = new char[_capacity]; - memset(_string, 0, _capacity); - } - } else { - setStringVal(val->getString()); - } - - if (_capacity == 0) setStringVal(""); -} - - -////////////////////////////////////////////////////////////////////////// -CSXString::~CSXString() { - if (_string) delete [] _string; -} - - -////////////////////////////////////////////////////////////////////////// -void CSXString::setStringVal(const char *val) { - int len = strlen(val); - if (len >= _capacity) { - _capacity = len + 1; - delete[] _string; - _string = NULL; - _string = new char[_capacity]; - memset(_string, 0, _capacity); - } - strcpy(_string, val); -} - - -////////////////////////////////////////////////////////////////////////// -const char *CSXString::scToString() { - if (_string) return _string; - else return "[null string]"; -} - - -////////////////////////////////////////////////////////////////////////// -void CSXString::scSetString(const char *val) { - setStringVal(val); -} - - -////////////////////////////////////////////////////////////////////////// -bool CSXString::scCallMethod(CScScript *script, CScStack *stack, CScStack *thisStack, const char *name) { - ////////////////////////////////////////////////////////////////////////// - // Substring - ////////////////////////////////////////////////////////////////////////// - if (strcmp(name, "Substring") == 0) { - stack->correctParams(2); - int start = stack->pop()->getInt(); - int end = stack->pop()->getInt(); - - if (end < start) CBUtils::swap(&start, &end); - - //try { - WideString str; - if (_gameRef->_textEncoding == TEXT_UTF8) - str = StringUtil::utf8ToWide(_string); - else - str = StringUtil::ansiToWide(_string); - - //WideString subStr = str.substr(start, end - start + 1); - WideString subStr(str.c_str() + start, end - start + 1); - - if (_gameRef->_textEncoding == TEXT_UTF8) - stack->pushString(StringUtil::wideToUtf8(subStr).c_str()); - else - stack->pushString(StringUtil::wideToAnsi(subStr).c_str()); - // } catch (std::exception &) { - // stack->pushNULL(); - // } - - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // Substr - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "Substr") == 0) { - stack->correctParams(2); - int start = stack->pop()->getInt(); - - CScValue *val = stack->pop(); - int len = val->getInt(); - - if (!val->isNULL() && len <= 0) { - stack->pushString(""); - return STATUS_OK; - } - - if (val->isNULL()) len = strlen(_string) - start; - -// try { - WideString str; - if (_gameRef->_textEncoding == TEXT_UTF8) - str = StringUtil::utf8ToWide(_string); - else - str = StringUtil::ansiToWide(_string); - -// WideString subStr = str.substr(start, len); - WideString subStr(str.c_str() + start, len); - - if (_gameRef->_textEncoding == TEXT_UTF8) - stack->pushString(StringUtil::wideToUtf8(subStr).c_str()); - else - stack->pushString(StringUtil::wideToAnsi(subStr).c_str()); -// } catch (std::exception &) { -// stack->pushNULL(); -// } - - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // ToUpperCase - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "ToUpperCase") == 0) { - stack->correctParams(0); - - WideString str; - if (_gameRef->_textEncoding == TEXT_UTF8) - str = StringUtil::utf8ToWide(_string); - else - str = StringUtil::ansiToWide(_string); - - str.toUppercase(); - - if (_gameRef->_textEncoding == TEXT_UTF8) - stack->pushString(StringUtil::wideToUtf8(str).c_str()); - else - stack->pushString(StringUtil::wideToAnsi(str).c_str()); - - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // ToLowerCase - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "ToLowerCase") == 0) { - stack->correctParams(0); - - WideString str; - if (_gameRef->_textEncoding == TEXT_UTF8) - str = StringUtil::utf8ToWide(_string); - else - str = StringUtil::ansiToWide(_string); - - str.toLowercase(); - - if (_gameRef->_textEncoding == TEXT_UTF8) - stack->pushString(StringUtil::wideToUtf8(str).c_str()); - else - stack->pushString(StringUtil::wideToAnsi(str).c_str()); - - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // IndexOf - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "IndexOf") == 0) { - stack->correctParams(2); - - const char *strToFind = stack->pop()->getString(); - int index = stack->pop()->getInt(); - - WideString str; - if (_gameRef->_textEncoding == TEXT_UTF8) - str = StringUtil::utf8ToWide(_string); - else - str = StringUtil::ansiToWide(_string); - - WideString toFind; - if (_gameRef->_textEncoding == TEXT_UTF8) - toFind = StringUtil::utf8ToWide(strToFind); - else - toFind = StringUtil::ansiToWide(strToFind); - - int indexOf = StringUtil::indexOf(str, toFind, index); - stack->pushInt(indexOf); - - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // Split - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "Split") == 0) { - stack->correctParams(1); - CScValue *val = stack->pop(); - char separators[MAX_PATH_LENGTH] = ","; - if (!val->isNULL()) strcpy(separators, val->getString()); - - CSXArray *array = new CSXArray(_gameRef); - if (!array) { - stack->pushNULL(); - return STATUS_OK; - } - - - WideString str; - if (_gameRef->_textEncoding == TEXT_UTF8) - str = StringUtil::utf8ToWide(_string); - else - str = StringUtil::ansiToWide(_string); - - WideString delims; - if (_gameRef->_textEncoding == TEXT_UTF8) - delims = StringUtil::utf8ToWide(separators); - else - delims = StringUtil::ansiToWide(separators); - - Common::Array parts; - - - - Common::StringTokenizer tokenizer(str, delims); - while (!tokenizer.empty()) { - Common::String str2 = tokenizer.nextToken(); - parts.push_back(str2); - } - // TODO: Clean this up - /*do { - pos = StringUtil::IndexOf(Common::String(str.c_str() + start), delims, start); - //pos = str.find_first_of(delims, start); - if (pos == start) { - start = pos + 1; - } else if (pos == str.size()) { - parts.push_back(Common::String(str.c_str() + start)); - break; - } else { - parts.push_back(Common::String(str.c_str() + start, pos - start)); - start = pos + 1; - } - //start = str.find_first_not_of(delims, start); - start = StringUtil::LastIndexOf(Common::String(str.c_str() + start), delims, start) + 1; - - } while (pos != str.size());*/ - - for (Common::Array::iterator it = parts.begin(); it != parts.end(); ++it) { - WideString &part = (*it); - - if (_gameRef->_textEncoding == TEXT_UTF8) - val = new CScValue(_gameRef, StringUtil::wideToUtf8(part).c_str()); - else - val = new CScValue(_gameRef, StringUtil::wideToAnsi(part).c_str()); - - array->push(val); - delete val; - val = NULL; - } - - stack->pushNative(array, false); - return STATUS_OK; - } - - else return STATUS_FAILED; -} - - -////////////////////////////////////////////////////////////////////////// -CScValue *CSXString::scGetProperty(const char *name) { - _scValue->setNULL(); - - ////////////////////////////////////////////////////////////////////////// - // Type (RO) - ////////////////////////////////////////////////////////////////////////// - if (strcmp(name, "Type") == 0) { - _scValue->setString("string"); - return _scValue; - } - ////////////////////////////////////////////////////////////////////////// - // Length (RO) - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "Length") == 0) { - if (_gameRef->_textEncoding == TEXT_UTF8) { - WideString wstr = StringUtil::utf8ToWide(_string); - _scValue->setInt(wstr.size()); - } else - _scValue->setInt(strlen(_string)); - - return _scValue; - } - ////////////////////////////////////////////////////////////////////////// - // Capacity - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "Capacity") == 0) { - _scValue->setInt(_capacity); - return _scValue; - } - - else return _scValue; -} - - -////////////////////////////////////////////////////////////////////////// -bool CSXString::scSetProperty(const char *name, CScValue *value) { - ////////////////////////////////////////////////////////////////////////// - // Capacity - ////////////////////////////////////////////////////////////////////////// - if (strcmp(name, "Capacity") == 0) { - int32 newCap = (uint32)value->getInt(); - if (newCap < (int32)(strlen(_string) + 1)) _gameRef->LOG(0, "Warning: cannot lower string capacity"); - else if (newCap != _capacity) { - char *newStr = new char[newCap]; - if (newStr) { - memset(newStr, 0, newCap); - strcpy(newStr, _string); - delete[] _string; - _string = newStr; - _capacity = newCap; - } - } - return STATUS_OK; - } - - else return STATUS_FAILED; -} - - -////////////////////////////////////////////////////////////////////////// -bool CSXString::persist(CBPersistMgr *persistMgr) { - - CBScriptable::persist(persistMgr); - - persistMgr->transfer(TMEMBER(_capacity)); - - if (persistMgr->_saving) { - if (_capacity > 0) persistMgr->putBytes((byte *)_string, _capacity); - } else { - if (_capacity > 0) { - _string = new char[_capacity]; - persistMgr->getBytes((byte *)_string, _capacity); - } else _string = NULL; - } - - return STATUS_OK; -} - - -////////////////////////////////////////////////////////////////////////// -int CSXString::scCompare(CBScriptable *val) { - return strcmp(_string, ((CSXString *)val)->_string); -} - -} // end of namespace WinterMute diff --git a/engines/wintermute/base/scriptables/SXString.h b/engines/wintermute/base/scriptables/SXString.h deleted file mode 100644 index 348595ad29..0000000000 --- a/engines/wintermute/base/scriptables/SXString.h +++ /dev/null @@ -1,58 +0,0 @@ -/* ScummVM - Graphic Adventure Engine - * - * ScummVM is the legal property of its developers, whose names - * are too numerous to list here. Please refer to the COPYRIGHT - * file distributed with this source distribution. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - */ - -/* - * This file is based on WME Lite. - * http://dead-code.org/redir.php?target=wmelite - * Copyright (c) 2011 Jan Nedoma - */ - -#ifndef WINTERMUTE_SXSTRING_H -#define WINTERMUTE_SXSTRING_H - - -#include "engines/wintermute/base/BScriptable.h" - -namespace WinterMute { - -class CSXString : public CBScriptable { -public: - virtual int scCompare(CBScriptable *Val); - DECLARE_PERSISTENT(CSXString, CBScriptable) - CScValue *scGetProperty(const char *name); - bool scSetProperty(const char *name, CScValue *value); - bool scCallMethod(CScScript *script, CScStack *stack, CScStack *thisStack, const char *name); - void scSetString(const char *val); - const char *scToString(); - void setStringVal(const char *val); - - CSXString(CBGame *inGame, CScStack *Stack); - virtual ~CSXString(); - -private: - char *_string; - int _capacity; -}; - -} // end of namespace WinterMute - -#endif diff --git a/engines/wintermute/base/scriptables/ScEngine.cpp b/engines/wintermute/base/scriptables/ScEngine.cpp deleted file mode 100644 index db79a7d0e9..0000000000 --- a/engines/wintermute/base/scriptables/ScEngine.cpp +++ /dev/null @@ -1,712 +0,0 @@ -/* ScummVM - Graphic Adventure Engine - * - * ScummVM is the legal property of its developers, whose names - * are too numerous to list here. Please refer to the COPYRIGHT - * file distributed with this source distribution. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - */ - -/* - * This file is based on WME Lite. - * http://dead-code.org/redir.php?target=wmelite - * Copyright (c) 2011 Jan Nedoma - */ - -#include "engines/wintermute/dcgf.h" -#include "engines/wintermute/base/scriptables/ScEngine.h" -#include "engines/wintermute/utils/StringUtil.h" -#include "engines/wintermute/base/scriptables/ScValue.h" -#include "engines/wintermute/base/scriptables/ScScript.h" -#include "engines/wintermute/base/scriptables/ScStack.h" -#include "engines/wintermute/base/scriptables/SXMath.h" -#include "engines/wintermute/base/BRegistry.h" -#include "engines/wintermute/base/BGame.h" -#include "engines/wintermute/base/BSound.h" -#include "engines/wintermute/base/BFileManager.h" - - -namespace WinterMute { - -IMPLEMENT_PERSISTENT(CScEngine, true) - -#define COMPILER_DLL "dcscomp.dll" -////////////////////////////////////////////////////////////////////////// -CScEngine::CScEngine(CBGame *inGame): CBBase(inGame) { - _gameRef->LOG(0, "Initializing scripting engine..."); - - if (_compilerAvailable) _gameRef->LOG(0, " Script compiler bound successfuly"); - else _gameRef->LOG(0, " Script compiler is NOT available"); - - _globals = new CScValue(_gameRef); - - - // register 'Game' as global variable - if (!_globals->propExists("Game")) { - CScValue val(_gameRef); - val.setNative(_gameRef, true); - _globals->setProp("Game", &val); - } - - // register 'Math' as global variable - if (!_globals->propExists("Math")) { - CScValue val(_gameRef); - val.setNative(_gameRef->_mathClass, true); - _globals->setProp("Math", &val); - } - - // prepare script cache - for (int i = 0; i < MAX_CACHED_SCRIPTS; i++) _cachedScripts[i] = NULL; - - _currentScript = NULL; - - _isProfiling = false; - _profilingStartTime = 0; - - //EnableProfiling(); -} - - -////////////////////////////////////////////////////////////////////////// -CScEngine::~CScEngine() { - _gameRef->LOG(0, "Shutting down scripting engine"); - saveBreakpoints(); - - disableProfiling(); - - cleanup(); - - for (int i = 0; i < _breakpoints.getSize(); i++) { - delete _breakpoints[i]; - _breakpoints[i] = NULL; - } - _breakpoints.removeAll(); -} - - -////////////////////////////////////////////////////////////////////////// -bool CScEngine::cleanup() { - for (int i = 0; i < _scripts.getSize(); i++) { - if (!_scripts[i]->_thread && _scripts[i]->_owner) _scripts[i]->_owner->removeScript(_scripts[i]); - delete _scripts[i]; - _scripts.removeAt(i); - i--; - } - - _scripts.removeAll(); - - delete _globals; - _globals = NULL; - - emptyScriptCache(); - - _currentScript = NULL; // ref only - - return STATUS_OK; -} - - -////////////////////////////////////////////////////////////////////////// -byte *CScEngine::loadFile(void *data, char *filename, uint32 *size) { - CBGame *gameRef = (CBGame *)data; - return gameRef->_fileManager->readWholeFile(filename, size); -} - - -////////////////////////////////////////////////////////////////////////// -void CScEngine::closeFile(void *data, byte *buffer) { - delete [] buffer; -} - - -////////////////////////////////////////////////////////////////////////// -void CScEngine::parseElement(void *data, int line, int type, void *elementData) { -} - - -////////////////////////////////////////////////////////////////////////// -CScScript *CScEngine::runScript(const char *filename, CBScriptHolder *owner) { - byte *compBuffer; - uint32 compSize; - - // get script from cache - compBuffer = getCompiledScript(filename, &compSize); - if (!compBuffer) return NULL; - - // add new script - CScScript *script = new CScScript(_gameRef, this); - bool ret = script->create(filename, compBuffer, compSize, owner); - if (DID_FAIL(ret)) { - _gameRef->LOG(ret, "Error running script '%s'...", filename); - delete script; - return NULL; - } else { - // publish the "self" pseudo-variable - CScValue val(_gameRef); - if (owner)val.setNative(owner, true); - else val.setNULL(); - - script->_globals->setProp("self", &val); - script->_globals->setProp("this", &val); - - _scripts.add(script); - _gameRef->getDebugMgr()->onScriptInit(script); - - return script; - } -} - - -////////////////////////////////////////////////////////////////////////// -byte *CScEngine::getCompiledScript(const char *filename, uint32 *outSize, bool ignoreCache) { - // is script in cache? - if (!ignoreCache) { - for (int i = 0; i < MAX_CACHED_SCRIPTS; i++) { - if (_cachedScripts[i] && scumm_stricmp(_cachedScripts[i]->_filename.c_str(), filename) == 0) { - _cachedScripts[i]->_timestamp = CBPlatform::getTime(); - *outSize = _cachedScripts[i]->_size; - return _cachedScripts[i]->_buffer; - } - } - } - - // nope, load it - byte *compBuffer; - uint32 compSize; - - uint32 size; - - byte *buffer = _gameRef->_fileManager->readWholeFile(filename, &size); - if (!buffer) { - _gameRef->LOG(0, "CScEngine::GetCompiledScript - error opening script '%s'", filename); - return NULL; - } - - // needs to be compiled? - if (FROM_LE_32(*(uint32 *)buffer) == SCRIPT_MAGIC) { - compBuffer = buffer; - compSize = size; - } else { - if (!_compilerAvailable) { - _gameRef->LOG(0, "CScEngine::GetCompiledScript - script '%s' needs to be compiled but compiler is not available", filename); - delete [] buffer; - return NULL; - } - // This code will never be called, since _compilerAvailable is const false. - // It's only here in the event someone would want to reinclude the compiler. - error("Script needs compilation, ScummVM does not contain a WME compiler"); - } - - byte *ret = NULL; - - // add script to cache - CScCachedScript *cachedScript = new CScCachedScript(filename, compBuffer, compSize); - if (cachedScript) { - int index = 0; - uint32 MinTime = CBPlatform::getTime(); - for (int i = 0; i < MAX_CACHED_SCRIPTS; i++) { - if (_cachedScripts[i] == NULL) { - index = i; - break; - } else if (_cachedScripts[i]->_timestamp <= MinTime) { - MinTime = _cachedScripts[i]->_timestamp; - index = i; - } - } - - if (_cachedScripts[index] != NULL) delete _cachedScripts[index]; - _cachedScripts[index] = cachedScript; - - ret = cachedScript->_buffer; - *outSize = cachedScript->_size; - } - - - // cleanup - delete [] buffer; - - return ret; -} - - - -////////////////////////////////////////////////////////////////////////// -bool CScEngine::tick() { - if (_scripts.getSize() == 0) - return STATUS_OK; - - - // resolve waiting scripts - for (int i = 0; i < _scripts.getSize(); i++) { - - switch (_scripts[i]->_state) { - case SCRIPT_WAITING: { - /* - bool obj_found=false; - for(int j=0; j<_gameRef->_regObjects.getSize(); j++) - { - if(_gameRef->_regObjects[j] == _scripts[i]->_waitObject) - { - if(_gameRef->_regObjects[j]->IsReady()) _scripts[i]->Run(); - obj_found = true; - break; - } - } - if(!obj_found) _scripts[i]->finish(); // _waitObject no longer exists - */ - if (_gameRef->validObject(_scripts[i]->_waitObject)) { - if (_scripts[i]->_waitObject->isReady()) _scripts[i]->run(); - } else _scripts[i]->finish(); - break; - } - - case SCRIPT_SLEEPING: { - if (_scripts[i]->_waitFrozen) { - if (_scripts[i]->_waitTime <= CBPlatform::getTime()) _scripts[i]->run(); - } else { - if (_scripts[i]->_waitTime <= _gameRef->_timer) _scripts[i]->run(); - } - break; - } - - case SCRIPT_WAITING_SCRIPT: { - if (!isValidScript(_scripts[i]->_waitScript) || _scripts[i]->_waitScript->_state == SCRIPT_ERROR) { - // fake return value - _scripts[i]->_stack->pushNULL(); - _scripts[i]->_waitScript = NULL; - _scripts[i]->run(); - } else { - if (_scripts[i]->_waitScript->_state == SCRIPT_THREAD_FINISHED) { - // copy return value - _scripts[i]->_stack->push(_scripts[i]->_waitScript->_stack->pop()); - _scripts[i]->run(); - _scripts[i]->_waitScript->finish(); - _scripts[i]->_waitScript = NULL; - } - } - break; - } - default: - //warning("CScEngine::Tick - Unhandled enum"); - break; - } // switch - } // for each script - - - // execute scripts - for (int i = 0; i < _scripts.getSize(); i++) { - - // skip paused scripts - if (_scripts[i]->_state == SCRIPT_PAUSED) continue; - - // time sliced script - if (_scripts[i]->_timeSlice > 0) { - uint32 StartTime = CBPlatform::getTime(); - while (_scripts[i]->_state == SCRIPT_RUNNING && CBPlatform::getTime() - StartTime < _scripts[i]->_timeSlice) { - _currentScript = _scripts[i]; - _scripts[i]->executeInstruction(); - } - if (_isProfiling && _scripts[i]->_filename) addScriptTime(_scripts[i]->_filename, CBPlatform::getTime() - StartTime); - } - - // normal script - else { - uint32 startTime = 0; - bool isProfiling = _isProfiling; - if (isProfiling) startTime = CBPlatform::getTime(); - - while (_scripts[i]->_state == SCRIPT_RUNNING) { - _currentScript = _scripts[i]; - _scripts[i]->executeInstruction(); - } - if (isProfiling && _scripts[i]->_filename) addScriptTime(_scripts[i]->_filename, CBPlatform::getTime() - startTime); - } - _currentScript = NULL; - } - - removeFinishedScripts(); - - return STATUS_OK; -} - - -////////////////////////////////////////////////////////////////////////// -bool CScEngine::tickUnbreakable() { - // execute unbreakable scripts - for (int i = 0; i < _scripts.getSize(); i++) { - if (!_scripts[i]->_unbreakable) continue; - - while (_scripts[i]->_state == SCRIPT_RUNNING) { - _currentScript = _scripts[i]; - _scripts[i]->executeInstruction(); - } - _scripts[i]->finish(); - _currentScript = NULL; - } - removeFinishedScripts(); - - return STATUS_OK; -} - - -////////////////////////////////////////////////////////////////////////// -bool CScEngine::removeFinishedScripts() { - // remove finished scripts - for (int i = 0; i < _scripts.getSize(); i++) { - if (_scripts[i]->_state == SCRIPT_FINISHED || _scripts[i]->_state == SCRIPT_ERROR) { - if (!_scripts[i]->_thread && _scripts[i]->_owner) _scripts[i]->_owner->removeScript(_scripts[i]); - _gameRef->getDebugMgr()->onScriptShutdown(_scripts[i]); - delete _scripts[i]; - _scripts.removeAt(i); - i--; - } - } - return STATUS_OK; -} - - -////////////////////////////////////////////////////////////////////////// -int CScEngine::getNumScripts(int *running, int *waiting, int *persistent) { - int numRunning = 0, numWaiting = 0, numPersistent = 0, numTotal = 0; - - for (int i = 0; i < _scripts.getSize(); i++) { - if (_scripts[i]->_state == SCRIPT_FINISHED) continue; - switch (_scripts[i]->_state) { - case SCRIPT_RUNNING: - case SCRIPT_SLEEPING: - case SCRIPT_PAUSED: - numRunning++; - break; - case SCRIPT_WAITING: - numWaiting++; - break; - case SCRIPT_PERSISTENT: - numPersistent++; - break; - default: - warning("CScEngine::GetNumScripts - unhandled enum"); - break; - } - numTotal++; - } - if (running) *running = numRunning; - if (waiting) *waiting = numWaiting; - if (persistent) *persistent = numPersistent; - - return numTotal; -} - - -////////////////////////////////////////////////////////////////////////// -bool CScEngine::emptyScriptCache() { - for (int i = 0; i < MAX_CACHED_SCRIPTS; i++) { - if (_cachedScripts[i]) { - delete _cachedScripts[i]; - _cachedScripts[i] = NULL; - } - } - return STATUS_OK; -} - - -////////////////////////////////////////////////////////////////////////// -bool CScEngine::resetObject(CBObject *Object) { - // terminate all scripts waiting for this object - for (int i = 0; i < _scripts.getSize(); i++) { - if (_scripts[i]->_state == SCRIPT_WAITING && _scripts[i]->_waitObject == Object) { - if (!_gameRef->_compatKillMethodThreads) resetScript(_scripts[i]); - - bool IsThread = _scripts[i]->_methodThread || _scripts[i]->_thread; - _scripts[i]->finish(!IsThread); // 1.9b1 - top-level script kills its threads as well - } - } - return STATUS_OK; -} - -////////////////////////////////////////////////////////////////////////// -bool CScEngine::resetScript(CScScript *script) { - // terminate all scripts waiting for this script - for (int i = 0; i < _scripts.getSize(); i++) { - if (_scripts[i]->_state == SCRIPT_WAITING_SCRIPT && _scripts[i]->_waitScript == script) { - _scripts[i]->finish(); - } - } - return STATUS_OK; -} - -////////////////////////////////////////////////////////////////////////// -bool CScEngine::persist(CBPersistMgr *persistMgr) { - if (!persistMgr->_saving) cleanup(); - - persistMgr->transfer(TMEMBER(_gameRef)); - persistMgr->transfer(TMEMBER(_currentScript)); - persistMgr->transfer(TMEMBER(_globals)); - _scripts.persist(persistMgr); - - return STATUS_OK; -} - - -////////////////////////////////////////////////////////////////////////// -void CScEngine::editorCleanup() { - for (int i = 0; i < _scripts.getSize(); i++) { - if (_scripts[i]->_owner == NULL && (_scripts[i]->_state == SCRIPT_FINISHED || _scripts[i]->_state == SCRIPT_ERROR)) { - delete _scripts[i]; - _scripts.removeAt(i); - i--; - } - } -} - - -////////////////////////////////////////////////////////////////////////// -bool CScEngine::pauseAll() { - for (int i = 0; i < _scripts.getSize(); i++) { - if (_scripts[i] != _currentScript) _scripts[i]->pause(); - } - - return STATUS_OK; -} - - -////////////////////////////////////////////////////////////////////////// -bool CScEngine::resumeAll() { - for (int i = 0; i < _scripts.getSize(); i++) - _scripts[i]->resume(); - - return STATUS_OK; -} - - -////////////////////////////////////////////////////////////////////////// -bool CScEngine::isValidScript(CScScript *script) { - for (int i = 0; i < _scripts.getSize(); i++) { - if (_scripts[i] == script) return true; - } - return false; -} - -////////////////////////////////////////////////////////////////////////// -bool CScEngine::clearGlobals(bool includingNatives) { - _globals->CleanProps(includingNatives); - return STATUS_OK; -} - -////////////////////////////////////////////////////////////////////////// -bool CScEngine::dbgSendScripts(IWmeDebugClient *client) { - // send global variables - _globals->dbgSendVariables(client, WME_DBGVAR_GLOBAL, NULL, 0); - - // process normal scripts first - for (int i = 0; i < _scripts.getSize(); i++) { - if (_scripts[i]->_thread || _scripts[i]->_methodThread) continue; - _scripts[i]->dbgSendScript(client); - } - - // and threads later - for (int i = 0; i < _scripts.getSize(); i++) { - if (_scripts[i]->_thread || _scripts[i]->_methodThread) - _scripts[i]->dbgSendScript(client); - } - - return STATUS_OK; -} - -////////////////////////////////////////////////////////////////////////// -bool CScEngine::addBreakpoint(const char *scriptFilename, int line) { - if (!_gameRef->getDebugMgr()->_enabled) return STATUS_OK; - - CScBreakpoint *bp = NULL; - for (int i = 0; i < _breakpoints.getSize(); i++) { - if (scumm_stricmp(_breakpoints[i]->_filename.c_str(), scriptFilename) == 0) { - bp = _breakpoints[i]; - break; - } - } - if (bp == NULL) { - bp = new CScBreakpoint(scriptFilename); - _breakpoints.add(bp); - } - - for (int i = 0; i < bp->_lines.getSize(); i++) { - if (bp->_lines[i] == line) return STATUS_OK; - } - bp->_lines.add(line); - - // refresh changes - refreshScriptBreakpoints(); - - return STATUS_OK; -} - -////////////////////////////////////////////////////////////////////////// -bool CScEngine::removeBreakpoint(const char *scriptFilename, int line) { - if (!_gameRef->getDebugMgr()->_enabled) return STATUS_OK; - - for (int i = 0; i < _breakpoints.getSize(); i++) { - if (scumm_stricmp(_breakpoints[i]->_filename.c_str(), scriptFilename) == 0) { - for (int j = 0; j < _breakpoints[i]->_lines.getSize(); j++) { - if (_breakpoints[i]->_lines[j] == line) { - _breakpoints[i]->_lines.removeAt(j); - if (_breakpoints[i]->_lines.getSize() == 0) { - delete _breakpoints[i]; - _breakpoints.removeAt(i); - } - // refresh changes - refreshScriptBreakpoints(); - - return STATUS_OK; - } - } - break; - } - } - return STATUS_FAILED; -} - -////////////////////////////////////////////////////////////////////////// -bool CScEngine::refreshScriptBreakpoints() { - if (!_gameRef->getDebugMgr()->_enabled) return STATUS_OK; - - for (int i = 0; i < _scripts.getSize(); i++) { - refreshScriptBreakpoints(_scripts[i]); - } - return STATUS_OK; -} - -////////////////////////////////////////////////////////////////////////// -bool CScEngine::refreshScriptBreakpoints(CScScript *script) { - if (!_gameRef->getDebugMgr()->_enabled) return STATUS_OK; - - if (!script || !script->_filename) return STATUS_FAILED; - - for (int i = 0; i < _breakpoints.getSize(); i++) { - if (scumm_stricmp(_breakpoints[i]->_filename.c_str(), script->_filename) == 0) { - script->_breakpoints.copy(_breakpoints[i]->_lines); - return STATUS_OK; - } - } - if (script->_breakpoints.getSize() > 0) script->_breakpoints.removeAll(); - - return STATUS_OK; -} - -////////////////////////////////////////////////////////////////////////// -bool CScEngine::saveBreakpoints() { - if (!_gameRef->getDebugMgr()->_enabled) return STATUS_OK; - - - char text[512]; - char key[100]; - - int count = 0; - for (int i = 0; i < _breakpoints.getSize(); i++) { - for (int j = 0; j < _breakpoints[i]->_lines.getSize(); j++) { - count++; - sprintf(key, "Breakpoint%d", count); - sprintf(text, "%s:%d", _breakpoints[i]->_filename.c_str(), _breakpoints[i]->_lines[j]); - - _gameRef->_registry->writeString("Debug", key, text); - } - } - _gameRef->_registry->writeInt("Debug", "NumBreakpoints", count); - - return STATUS_OK; -} - -////////////////////////////////////////////////////////////////////////// -bool CScEngine::loadBreakpoints() { - if (!_gameRef->getDebugMgr()->_enabled) return STATUS_OK; - - char key[100]; - - int count = _gameRef->_registry->readInt("Debug", "NumBreakpoints", 0); - for (int i = 1; i <= count; i++) { - /* uint32 BufSize = 512; */ - sprintf(key, "Breakpoint%d", i); - AnsiString breakpoint = _gameRef->_registry->readString("Debug", key, ""); - - char *path = CBUtils::strEntry(0, breakpoint.c_str(), ':'); - char *line = CBUtils::strEntry(1, breakpoint.c_str(), ':'); - - if (path != NULL && line != NULL) addBreakpoint(path, atoi(line)); - delete[] path; - delete[] line; - path = NULL; - line = NULL; - } - - return STATUS_OK; -} - - -////////////////////////////////////////////////////////////////////////// -void CScEngine::addScriptTime(const char *filename, uint32 time) { - if (!_isProfiling) return; - - AnsiString fileName = filename; - fileName.toLowercase(); - _scriptTimes[fileName] += time; -} - - -////////////////////////////////////////////////////////////////////////// -void CScEngine::enableProfiling() { - if (_isProfiling) return; - - // destroy old data, if any - _scriptTimes.clear(); - - _profilingStartTime = CBPlatform::getTime(); - _isProfiling = true; -} - - -////////////////////////////////////////////////////////////////////////// -void CScEngine::disableProfiling() { - if (!_isProfiling) return; - - dumpStats(); - _isProfiling = false; -} - - -////////////////////////////////////////////////////////////////////////// -void CScEngine::dumpStats() { - error("DumpStats not ported to ScummVM yet"); - /* uint32 totalTime = CBPlatform::getTime() - _profilingStartTime; - - typedef std::vector > TimeVector; - TimeVector times; - - ScriptTimes::iterator it; - for (it = _scriptTimes.begin(); it != _scriptTimes.end(); it++) { - times.push_back(std::pair (it->_value, it->_key)); - } - std::sort(times.begin(), times.end()); - - - TimeVector::reverse_iterator tit; - - _gameRef->LOG(0, "***** Script profiling information: *****"); - _gameRef->LOG(0, " %-40s %fs", "Total execution time", (float)totalTime / 1000); - - for (tit = times.rbegin(); tit != times.rend(); tit++) { - _gameRef->LOG(0, " %-40s %fs (%f%%)", tit->second.c_str(), (float)tit->first / 1000, (float)tit->first / (float)totalTime * 100); - }*/ -} - -} // end of namespace WinterMute diff --git a/engines/wintermute/base/scriptables/ScEngine.h b/engines/wintermute/base/scriptables/ScEngine.h deleted file mode 100644 index 0322319b3d..0000000000 --- a/engines/wintermute/base/scriptables/ScEngine.h +++ /dev/null @@ -1,147 +0,0 @@ -/* ScummVM - Graphic Adventure Engine - * - * ScummVM is the legal property of its developers, whose names - * are too numerous to list here. Please refer to the COPYRIGHT - * file distributed with this source distribution. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - */ - -/* - * This file is based on WME Lite. - * http://dead-code.org/redir.php?target=wmelite - * Copyright (c) 2011 Jan Nedoma - */ - -#ifndef WINTERMUTE_SCENGINE_H -#define WINTERMUTE_SCENGINE_H - -#include "engines/wintermute/persistent.h" -#include "engines/wintermute/coll_templ.h" -#include "engines/wintermute/base/BBase.h" -#include "engines/wintermute/wme_debugger.h" -#include "engines/wintermute/utils/utils.h" -#include "engines/wintermute/platform_osystem.h" - -namespace WinterMute { - -#define MAX_CACHED_SCRIPTS 20 -class CScScript; -class CScValue; -class CBObject; -class CBScriptHolder; -class CScEngine : public CBBase { -public: - class CScCachedScript { - public: - CScCachedScript(const char *filename, byte *buffer, uint32 size) { - _timestamp = CBPlatform::getTime(); - _buffer = new byte[size]; - if (_buffer) memcpy(_buffer, buffer, size); - _size = size; - _filename = filename; - }; - - ~CScCachedScript() { - if (_buffer) delete [] _buffer; - }; - - uint32 _timestamp; - byte *_buffer; - uint32 _size; - Common::String _filename; - }; - - class CScBreakpoint { - public: - CScBreakpoint(const char *filename) { - _filename = filename; - } - - ~CScBreakpoint() { - _lines.removeAll(); - } - - Common::String _filename; - CBArray _lines; - }; - - - - -public: - bool dbgSendScripts(IWmeDebugClient *client); - - CBArray _breakpoints; - bool addBreakpoint(const char *scriptFilename, int line); - bool removeBreakpoint(const char *scriptFilename, int line); - bool refreshScriptBreakpoints(); - bool refreshScriptBreakpoints(CScScript *script); - bool saveBreakpoints(); - bool loadBreakpoints(); - - bool clearGlobals(bool includingNatives = false); - bool tickUnbreakable(); - bool removeFinishedScripts(); - bool isValidScript(CScScript *script); - - CScScript *_currentScript; - bool resumeAll(); - bool pauseAll(); - void editorCleanup(); - bool resetObject(CBObject *Object); - bool resetScript(CScScript *script); - bool emptyScriptCache(); - byte *getCompiledScript(const char *filename, uint32 *outSize, bool ignoreCache = false); - DECLARE_PERSISTENT(CScEngine, CBBase) - bool cleanup(); - int getNumScripts(int *running = NULL, int *waiting = NULL, int *persistent = NULL); - bool tick(); - CScValue *_globals; - CScScript *runScript(const char *filename, CBScriptHolder *owner = NULL); - static const bool _compilerAvailable = false; - - CScEngine(CBGame *inGame); - virtual ~CScEngine(); - static byte *loadFile(void *data, char *filename, uint32 *size); - static void closeFile(void *data, byte *buffer); - static void parseElement(void *data, int line, int type, void *elementData); - - CBArray _scripts; - - void enableProfiling(); - void disableProfiling(); - bool getIsProfiling() { - return _isProfiling; - } - - void addScriptTime(const char *filename, uint32 Time); - void dumpStats(); - -private: - - CScCachedScript *_cachedScripts[MAX_CACHED_SCRIPTS]; - bool _isProfiling; - uint32 _profilingStartTime; - - typedef Common::HashMap ScriptTimes; - ScriptTimes _scriptTimes; - -}; - -} // end of namespace WinterMute - -#endif diff --git a/engines/wintermute/base/scriptables/ScScript.cpp b/engines/wintermute/base/scriptables/ScScript.cpp deleted file mode 100644 index 0b5b3c24bf..0000000000 --- a/engines/wintermute/base/scriptables/ScScript.cpp +++ /dev/null @@ -1,1461 +0,0 @@ -/* ScummVM - Graphic Adventure Engine - * - * ScummVM is the legal property of its developers, whose names - * are too numerous to list here. Please refer to the COPYRIGHT - * file distributed with this source distribution. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - */ - -/* - * This file is based on WME Lite. - * http://dead-code.org/redir.php?target=wmelite - * Copyright (c) 2011 Jan Nedoma - */ - -#include "engines/wintermute/dcgf.h" -#include "engines/wintermute/base/scriptables/ScValue.h" -#include "engines/wintermute/base/scriptables/ScScript.h" -#include "engines/wintermute/base/BGame.h" -#include "engines/wintermute/base/scriptables/ScEngine.h" -#include "engines/wintermute/base/scriptables/ScStack.h" -#include "common/memstream.h" - -namespace WinterMute { - -IMPLEMENT_PERSISTENT(CScScript, false) - -////////////////////////////////////////////////////////////////////////// -CScScript::CScScript(CBGame *inGame, CScEngine *Engine): CBBase(inGame) { - _buffer = NULL; - _bufferSize = _iP = 0; - _scriptStream = NULL; - _filename = NULL; - _currentLine = 0; - - _symbols = NULL; - _numSymbols = 0; - - _engine = Engine; - - _globals = NULL; - - _scopeStack = NULL; - _callStack = NULL; - _thisStack = NULL; - _stack = NULL; - - _operand = NULL; - _reg1 = NULL; - - _functions = NULL; - _numFunctions = 0; - - _methods = NULL; - _numMethods = 0; - - _events = NULL; - _numEvents = 0; - - _externals = NULL; - _numExternals = 0; - - _state = SCRIPT_FINISHED; - _origState = SCRIPT_FINISHED; - - _waitObject = NULL; - _waitTime = 0; - _waitFrozen = false; - _waitScript = NULL; - - _timeSlice = 0; - - _thread = false; - _methodThread = false; - _threadEvent = NULL; - - _freezable = true; - _owner = NULL; - - _unbreakable = false; - _parentScript = NULL; - - _tracingMode = false; -} - - -////////////////////////////////////////////////////////////////////////// -CScScript::~CScScript() { - cleanup(); -} - -void CScScript::readHeader() { - uint32 oldPos = _scriptStream->pos(); - _scriptStream->seek(0); - _header.magic = _scriptStream->readUint32LE(); - _header.version = _scriptStream->readUint32LE(); - _header.code_start = _scriptStream->readUint32LE(); - _header.func_table = _scriptStream->readUint32LE(); - _header.symbol_table = _scriptStream->readUint32LE(); - _header.event_table = _scriptStream->readUint32LE(); - _header.externals_table = _scriptStream->readUint32LE(); - _header.method_table = _scriptStream->readUint32LE(); - _scriptStream->seek(oldPos); -} - - -////////////////////////////////////////////////////////////////////////// -bool CScScript::initScript() { - if (!_scriptStream) { - _scriptStream = new Common::MemoryReadStream(_buffer, _bufferSize); - } - readHeader(); - - if (_header.magic != SCRIPT_MAGIC) { - _gameRef->LOG(0, "File '%s' is not a valid compiled script", _filename); - cleanup(); - return STATUS_FAILED; - } - - if (_header.version > SCRIPT_VERSION) { - _gameRef->LOG(0, "Script '%s' has a wrong version %d.%d (expected %d.%d)", _filename, _header.version / 256, _header.version % 256, SCRIPT_VERSION / 256, SCRIPT_VERSION % 256); - cleanup(); - return STATUS_FAILED; - } - - initTables(); - - // init stacks - _scopeStack = new CScStack(_gameRef); - _callStack = new CScStack(_gameRef); - _thisStack = new CScStack(_gameRef); - _stack = new CScStack(_gameRef); - - _operand = new CScValue(_gameRef); - _reg1 = new CScValue(_gameRef); - - - // skip to the beginning - _iP = _header.code_start; - _scriptStream->seek(_iP); - _currentLine = 0; - - // init breakpoints - _engine->refreshScriptBreakpoints(this); - - - // ready to rumble... - _state = SCRIPT_RUNNING; - - return STATUS_OK; -} - - -////////////////////////////////////////////////////////////////////////// -bool CScScript::initTables() { - uint32 OrigIP = _iP; - - readHeader(); - // load symbol table - _iP = _header.symbol_table; - - _numSymbols = getDWORD(); - _symbols = new char*[_numSymbols]; - for (uint32 i = 0; i < _numSymbols; i++) { - uint32 index = getDWORD(); - _symbols[index] = getString(); - } - - // load functions table - _iP = _header.func_table; - - _numFunctions = getDWORD(); - _functions = new TFunctionPos[_numFunctions]; - for (uint32 i = 0; i < _numFunctions; i++) { - _functions[i].pos = getDWORD(); - _functions[i].name = getString(); - } - - - // load events table - _iP = _header.event_table; - - _numEvents = getDWORD(); - _events = new TEventPos[_numEvents]; - for (uint32 i = 0; i < _numEvents; i++) { - _events[i].pos = getDWORD(); - _events[i].name = getString(); - } - - - // load externals - if (_header.version >= 0x0101) { - _iP = _header.externals_table; - - _numExternals = getDWORD(); - _externals = new TExternalFunction[_numExternals]; - for (uint32 i = 0; i < _numExternals; i++) { - _externals[i].dll_name = getString(); - _externals[i].name = getString(); - _externals[i].call_type = (TCallType)getDWORD(); - _externals[i].returns = (TExternalType)getDWORD(); - _externals[i].nu_params = getDWORD(); - if (_externals[i].nu_params > 0) { - _externals[i].params = new TExternalType[_externals[i].nu_params]; - for (int j = 0; j < _externals[i].nu_params; j++) { - _externals[i].params[j] = (TExternalType)getDWORD(); - } - } - } - } - - // load method table - _iP = _header.method_table; - - _numMethods = getDWORD(); - _methods = new TMethodPos[_numMethods]; - for (uint32 i = 0; i < _numMethods; i++) { - _methods[i].pos = getDWORD(); - _methods[i].name = getString(); - } - - - _iP = OrigIP; - - return STATUS_OK; -} - - -////////////////////////////////////////////////////////////////////////// -bool CScScript::create(const char *filename, byte *buffer, uint32 size, CBScriptHolder *owner) { - cleanup(); - - _thread = false; - _methodThread = false; - - delete[] _threadEvent; - _threadEvent = NULL; - - _filename = new char[strlen(filename) + 1]; - if (_filename) strcpy(_filename, filename); - - _buffer = new byte [size]; - if (!_buffer) return STATUS_FAILED; - - memcpy(_buffer, buffer, size); - - _bufferSize = size; - - bool res = initScript(); - if (DID_FAIL(res)) return res; - - // establish global variables table - _globals = new CScValue(_gameRef); - - _owner = owner; - - return STATUS_OK; -} - - -////////////////////////////////////////////////////////////////////////// -bool CScScript::createThread(CScScript *original, uint32 initIP, const char *eventName) { - cleanup(); - - _thread = true; - _methodThread = false; - _threadEvent = new char[strlen(eventName) + 1]; - if (_threadEvent) strcpy(_threadEvent, eventName); - - // copy filename - _filename = new char[strlen(original->_filename) + 1]; - if (_filename) strcpy(_filename, original->_filename); - - // copy buffer - _buffer = new byte [original->_bufferSize]; - if (!_buffer) return STATUS_FAILED; - - memcpy(_buffer, original->_buffer, original->_bufferSize); - _bufferSize = original->_bufferSize; - - // initialize - bool res = initScript(); - if (DID_FAIL(res)) return res; - - // copy globals - _globals = original->_globals; - - // skip to the beginning of the event - _iP = initIP; - _scriptStream->seek(_iP); - - _timeSlice = original->_timeSlice; - _freezable = original->_freezable; - _owner = original->_owner; - - _engine = original->_engine; - _parentScript = original; - - return STATUS_OK; -} - - - - -////////////////////////////////////////////////////////////////////////// -bool CScScript::createMethodThread(CScScript *original, const char *methodName) { - uint32 ip = original->getMethodPos(methodName); - if (ip == 0) return STATUS_FAILED; - - cleanup(); - - _thread = true; - _methodThread = true; - _threadEvent = new char[strlen(methodName) + 1]; - if (_threadEvent) strcpy(_threadEvent, methodName); - - // copy filename - _filename = new char[strlen(original->_filename) + 1]; - if (_filename) strcpy(_filename, original->_filename); - - // copy buffer - _buffer = new byte [original->_bufferSize]; - if (!_buffer) return STATUS_FAILED; - - memcpy(_buffer, original->_buffer, original->_bufferSize); - _bufferSize = original->_bufferSize; - - // initialize - bool res = initScript(); - if (DID_FAIL(res)) return res; - - // copy globals - _globals = original->_globals; - - // skip to the beginning of the event - _iP = ip; - - _timeSlice = original->_timeSlice; - _freezable = original->_freezable; - _owner = original->_owner; - - _engine = original->_engine; - _parentScript = original; - - return STATUS_OK; -} - - -////////////////////////////////////////////////////////////////////////// -void CScScript::cleanup() { - if (_buffer) delete [] _buffer; - _buffer = NULL; - - if (_filename) delete [] _filename; - _filename = NULL; - - if (_symbols) delete [] _symbols; - _symbols = NULL; - _numSymbols = 0; - - if (_globals && !_thread) delete _globals; - _globals = NULL; - - delete _scopeStack; - _scopeStack = NULL; - - delete _callStack; - _callStack = NULL; - - delete _thisStack; - _thisStack = NULL; - - delete _stack; - _stack = NULL; - - if (_functions) delete [] _functions; - _functions = NULL; - _numFunctions = 0; - - if (_methods) delete [] _methods; - _methods = NULL; - _numMethods = 0; - - if (_events) delete [] _events; - _events = NULL; - _numEvents = 0; - - - if (_externals) { - for (uint32 i = 0; i < _numExternals; i++) { - if (_externals[i].nu_params > 0) delete [] _externals[i].params; - } - delete [] _externals; - } - _externals = NULL; - _numExternals = 0; - - delete _operand; - delete _reg1; - _operand = NULL; - _reg1 = NULL; - - delete[] _threadEvent; - _threadEvent = NULL; - - _state = SCRIPT_FINISHED; - - _waitObject = NULL; - _waitTime = 0; - _waitFrozen = false; - _waitScript = NULL; - - _parentScript = NULL; // ref only - - delete _scriptStream; -} - - -////////////////////////////////////////////////////////////////////////// -uint32 CScScript::getDWORD() { - _scriptStream->seek((int32)_iP); - uint32 ret = _scriptStream->readUint32LE(); - _iP += sizeof(uint32); -// assert(oldRet == ret); - return ret; -} - -////////////////////////////////////////////////////////////////////////// -double CScScript::getFloat() { - _scriptStream->seek((int32)_iP); - byte buffer[8]; - _scriptStream->read(buffer, 8); - -#ifdef SCUMM_BIG_ENDIAN - // TODO: For lack of a READ_LE_UINT64 - SWAP(buffer[0], buffer[7]); - SWAP(buffer[1], buffer[6]); - SWAP(buffer[2], buffer[5]); - SWAP(buffer[3], buffer[4]); -#endif - - double ret = *(double *)(buffer); - _iP += 8; // Hardcode the double-size used originally. - return ret; -} - - -////////////////////////////////////////////////////////////////////////// -char *CScScript::getString() { - char *ret = (char *)(_buffer + _iP); - while (*(char *)(_buffer + _iP) != '\0') _iP++; - _iP++; // string terminator - _scriptStream->seek(_iP); - - return ret; -} - - -////////////////////////////////////////////////////////////////////////// -bool CScScript::executeInstruction() { - bool ret = STATUS_OK; - - uint32 dw; - const char *str = NULL; - - //CScValue* op = new CScValue(_gameRef); - _operand->cleanup(); - - CScValue *op1; - CScValue *op2; - - uint32 inst = getDWORD(); - switch (inst) { - - case II_DEF_VAR: - _operand->setNULL(); - dw = getDWORD(); - if (_scopeStack->_sP < 0) { - _globals->setProp(_symbols[dw], _operand); - if (_gameRef->getDebugMgr()->_enabled) - _gameRef->getDebugMgr()->onVariableInit(WME_DBGVAR_SCRIPT, this, NULL, _globals->getProp(_symbols[dw]), _symbols[dw]); - } else { - _scopeStack->getTop()->setProp(_symbols[dw], _operand); - if (_gameRef->getDebugMgr()->_enabled) - _gameRef->getDebugMgr()->onVariableInit(WME_DBGVAR_SCOPE, this, _scopeStack->getTop(), _scopeStack->getTop()->getProp(_symbols[dw]), _symbols[dw]); - } - - break; - - case II_DEF_GLOB_VAR: - case II_DEF_CONST_VAR: { - dw = getDWORD(); - /* char *Temp = _symbols[dw]; // TODO delete */ - // only create global var if it doesn't exist - if (!_engine->_globals->propExists(_symbols[dw])) { - _operand->setNULL(); - _engine->_globals->setProp(_symbols[dw], _operand, false, inst == II_DEF_CONST_VAR); - - if (_gameRef->getDebugMgr()->_enabled) - _gameRef->getDebugMgr()->onVariableInit(WME_DBGVAR_GLOBAL, this, NULL, _engine->_globals->getProp(_symbols[dw]), _symbols[dw]); - } - break; - } - - case II_RET: - if (_scopeStack->_sP >= 0 && _callStack->_sP >= 0) { - _gameRef->getDebugMgr()->onScriptShutdownScope(this, _scopeStack->getTop()); - - _scopeStack->pop(); - _iP = (uint32)_callStack->pop()->getInt(); - - if (_scopeStack->_sP < 0) _gameRef->getDebugMgr()->onScriptChangeScope(this, NULL); - else _gameRef->getDebugMgr()->onScriptChangeScope(this, _scopeStack->getTop()); - } else { - if (_thread) { - _state = SCRIPT_THREAD_FINISHED; - } else { - if (_numEvents == 0 && _numMethods == 0) _state = SCRIPT_FINISHED; - else _state = SCRIPT_PERSISTENT; - } - } - - break; - - case II_RET_EVENT: - _state = SCRIPT_FINISHED; - break; - - - case II_CALL: - dw = getDWORD(); - - _operand->setInt(_iP); - _callStack->push(_operand); - - _iP = dw; - - break; - - case II_CALL_BY_EXP: { - // push var - // push string - str = _stack->pop()->getString(); - char *MethodName = new char[strlen(str) + 1]; - strcpy(MethodName, str); - - CScValue *var = _stack->pop(); - if (var->_type == VAL_VARIABLE_REF) var = var->_valRef; - - bool res = STATUS_FAILED; - bool TriedNative = false; - - // we are already calling this method, try native - if (_thread && _methodThread && strcmp(MethodName, _threadEvent) == 0 && var->_type == VAL_NATIVE && _owner == var->getNative()) { - TriedNative = true; - res = var->_valNative->scCallMethod(this, _stack, _thisStack, MethodName); - } - - if (DID_FAIL(res)) { - if (var->isNative() && var->getNative()->canHandleMethod(MethodName)) { - if (!_unbreakable) { - _waitScript = var->getNative()->invokeMethodThread(MethodName); - if (!_waitScript) { - _stack->correctParams(0); - runtimeError("Error invoking method '%s'.", MethodName); - _stack->pushNULL(); - } else { - _state = SCRIPT_WAITING_SCRIPT; - _waitScript->copyParameters(_stack); - } - } else { - // can call methods in unbreakable mode - _stack->correctParams(0); - runtimeError("Cannot call method '%s'. Ignored.", MethodName); - _stack->pushNULL(); - } - delete [] MethodName; - break; - } - /* - CScValue* val = var->getProp(MethodName); - if(val){ - dw = GetFuncPos(val->getString()); - if(dw==0){ - TExternalFunction* f = GetExternal(val->getString()); - if(f){ - ExternalCall(_stack, _thisStack, f); - } - else{ - // not an internal nor external, try for native function - _gameRef->ExternalCall(this, _stack, _thisStack, val->getString()); - } - } - else{ - _operand->setInt(_iP); - _callStack->Push(_operand); - _iP = dw; - } - } - */ - else { - res = STATUS_FAILED; - if (var->_type == VAL_NATIVE && !TriedNative) res = var->_valNative->scCallMethod(this, _stack, _thisStack, MethodName); - - if (DID_FAIL(res)) { - _stack->correctParams(0); - runtimeError("Call to undefined method '%s'. Ignored.", MethodName); - _stack->pushNULL(); - } - } - } - delete [] MethodName; - } - break; - - case II_EXTERNAL_CALL: { - uint32 SymbolIndex = getDWORD(); - - TExternalFunction *f = getExternal(_symbols[SymbolIndex]); - if (f) { - externalCall(_stack, _thisStack, f); - } else _gameRef->ExternalCall(this, _stack, _thisStack, _symbols[SymbolIndex]); - - break; - } - case II_SCOPE: - _operand->setNULL(); - _scopeStack->push(_operand); - - if (_scopeStack->_sP < 0) _gameRef->getDebugMgr()->onScriptChangeScope(this, NULL); - else _gameRef->getDebugMgr()->onScriptChangeScope(this, _scopeStack->getTop()); - - break; - - case II_CORRECT_STACK: - dw = getDWORD(); // params expected - _stack->correctParams(dw); - break; - - case II_CREATE_OBJECT: - _operand->setObject(); - _stack->push(_operand); - break; - - case II_POP_EMPTY: - _stack->pop(); - break; - - case II_PUSH_VAR: { - CScValue *var = getVar(_symbols[getDWORD()]); - if (false && /*var->_type==VAL_OBJECT ||*/ var->_type == VAL_NATIVE) { - _operand->setReference(var); - _stack->push(_operand); - } else _stack->push(var); - break; - } - - case II_PUSH_VAR_REF: { - CScValue *var = getVar(_symbols[getDWORD()]); - _operand->setReference(var); - _stack->push(_operand); - break; - } - - case II_POP_VAR: { - char *VarName = _symbols[getDWORD()]; - CScValue *var = getVar(VarName); - if (var) { - CScValue *val = _stack->pop(); - if (!val) { - runtimeError("Script stack corruption detected. Please report this script at WME bug reports forum."); - var->setNULL(); - } else { - if (val->getType() == VAL_VARIABLE_REF) val = val->_valRef; - if (val->_type == VAL_NATIVE) var->setValue(val); - else { - var->copy(val); - } - } - - if (_gameRef->getDebugMgr()->_enabled) - _gameRef->getDebugMgr()->onVariableChangeValue(var, val); - } - - break; - } - - case II_PUSH_VAR_THIS: - _stack->push(_thisStack->getTop()); - break; - - case II_PUSH_INT: - _stack->pushInt((int)getDWORD()); - break; - - case II_PUSH_FLOAT: - _stack->pushFloat(getFloat()); - break; - - - case II_PUSH_BOOL: - _stack->pushBool(getDWORD() != 0); - - break; - - case II_PUSH_STRING: - _stack->pushString(getString()); - break; - - case II_PUSH_NULL: - _stack->pushNULL(); - break; - - case II_PUSH_THIS_FROM_STACK: - _operand->setReference(_stack->getTop()); - _thisStack->push(_operand); - break; - - case II_PUSH_THIS: - _operand->setReference(getVar(_symbols[getDWORD()])); - _thisStack->push(_operand); - break; - - case II_POP_THIS: - _thisStack->pop(); - break; - - case II_PUSH_BY_EXP: { - str = _stack->pop()->getString(); - CScValue *val = _stack->pop()->getProp(str); - if (val) _stack->push(val); - else _stack->pushNULL(); - - break; - } - - case II_POP_BY_EXP: { - str = _stack->pop()->getString(); - CScValue *var = _stack->pop(); - CScValue *val = _stack->pop(); - - if (val == NULL) { - runtimeError("Script stack corruption detected. Please report this script at WME bug reports forum."); - var->setNULL(); - } else var->setProp(str, val); - - if (_gameRef->getDebugMgr()->_enabled) - _gameRef->getDebugMgr()->onVariableChangeValue(var, NULL); - - break; - } - - case II_PUSH_REG1: - _stack->push(_reg1); - break; - - case II_POP_REG1: - _reg1->copy(_stack->pop()); - break; - - case II_JMP: - _iP = getDWORD(); - break; - - case II_JMP_FALSE: { - dw = getDWORD(); - //if(!_stack->pop()->getBool()) _iP = dw; - CScValue *val = _stack->pop(); - if (!val) { - runtimeError("Script corruption detected. Did you use '=' instead of '==' for comparison?"); - } else { - if (!val->getBool()) _iP = dw; - } - break; - } - - case II_ADD: - op2 = _stack->pop(); - op1 = _stack->pop(); - - if (op1->isNULL() || op2->isNULL()) - _operand->setNULL(); - else if (op1->getType() == VAL_STRING || op2->getType() == VAL_STRING) { - char *tempStr = new char [strlen(op1->getString()) + strlen(op2->getString()) + 1]; - strcpy(tempStr, op1->getString()); - strcat(tempStr, op2->getString()); - _operand->setString(tempStr); - delete [] tempStr; - } else if (op1->getType() == VAL_INT && op2->getType() == VAL_INT) - _operand->setInt(op1->getInt() + op2->getInt()); - else _operand->setFloat(op1->getFloat() + op2->getFloat()); - - _stack->push(_operand); - - break; - - case II_SUB: - op2 = _stack->pop(); - op1 = _stack->pop(); - - if (op1->isNULL() || op2->isNULL()) - _operand->setNULL(); - else if (op1->getType() == VAL_INT && op2->getType() == VAL_INT) - _operand->setInt(op1->getInt() - op2->getInt()); - else _operand->setFloat(op1->getFloat() - op2->getFloat()); - - _stack->push(_operand); - - break; - - case II_MUL: - op2 = _stack->pop(); - op1 = _stack->pop(); - - if (op1->isNULL() || op2->isNULL()) _operand->setNULL(); - else if (op1->getType() == VAL_INT && op2->getType() == VAL_INT) - _operand->setInt(op1->getInt() * op2->getInt()); - else _operand->setFloat(op1->getFloat() * op2->getFloat()); - - _stack->push(_operand); - - break; - - case II_DIV: - op2 = _stack->pop(); - op1 = _stack->pop(); - - if (op2->getFloat() == 0.0f) - runtimeError("Division by zero."); - - if (op1->isNULL() || op2->isNULL() || op2->getFloat() == 0.0f) _operand->setNULL(); - else _operand->setFloat(op1->getFloat() / op2->getFloat()); - - _stack->push(_operand); - - break; - - case II_MODULO: - op2 = _stack->pop(); - op1 = _stack->pop(); - - if (op2->getInt() == 0) - runtimeError("Division by zero."); - - if (op1->isNULL() || op2->isNULL() || op2->getInt() == 0) - _operand->setNULL(); - else _operand->setInt(op1->getInt() % op2->getInt()); - - _stack->push(_operand); - - break; - - case II_NOT: - op1 = _stack->pop(); - //if(op1->isNULL()) _operand->setNULL(); - if (op1->isNULL()) _operand->setBool(true); - else _operand->setBool(!op1->getBool()); - _stack->push(_operand); - - break; - - case II_AND: - op2 = _stack->pop(); - op1 = _stack->pop(); - if (op1 == NULL || op2 == NULL) { - runtimeError("Script corruption detected. Did you use '=' instead of '==' for comparison?"); - _operand->setBool(false); - } else { - _operand->setBool(op1->getBool() && op2->getBool()); - } - _stack->push(_operand); - break; - - case II_OR: - op2 = _stack->pop(); - op1 = _stack->pop(); - if (op1 == NULL || op2 == NULL) { - runtimeError("Script corruption detected. Did you use '=' instead of '==' for comparison?"); - _operand->setBool(false); - } else { - _operand->setBool(op1->getBool() || op2->getBool()); - } - _stack->push(_operand); - break; - - case II_CMP_EQ: - op2 = _stack->pop(); - op1 = _stack->pop(); - - /* - if((op1->isNULL() && !op2->isNULL()) || (!op1->isNULL() && op2->isNULL())) _operand->setBool(false); - else if(op1->isNative() && op2->isNative()){ - _operand->setBool(op1->getNative() == op2->getNative()); - } - else if(op1->getType()==VAL_STRING || op2->getType()==VAL_STRING){ - _operand->setBool(scumm_stricmp(op1->getString(), op2->getString())==0); - } - else if(op1->getType()==VAL_FLOAT && op2->getType()==VAL_FLOAT){ - _operand->setBool(op1->getFloat() == op2->getFloat()); - } - else{ - _operand->setBool(op1->getInt() == op2->getInt()); - } - */ - - _operand->setBool(CScValue::compare(op1, op2) == 0); - _stack->push(_operand); - break; - - case II_CMP_NE: - op2 = _stack->pop(); - op1 = _stack->pop(); - - /* - if((op1->isNULL() && !op2->isNULL()) || (!op1->isNULL() && op2->isNULL())) _operand->setBool(true); - else if(op1->isNative() && op2->isNative()){ - _operand->setBool(op1->getNative() != op2->getNative()); - } - else if(op1->getType()==VAL_STRING || op2->getType()==VAL_STRING){ - _operand->setBool(scumm_stricmp(op1->getString(), op2->getString())!=0); - } - else if(op1->getType()==VAL_FLOAT && op2->getType()==VAL_FLOAT){ - _operand->setBool(op1->getFloat() != op2->getFloat()); - } - else{ - _operand->setBool(op1->getInt() != op2->getInt()); - } - */ - - _operand->setBool(CScValue::compare(op1, op2) != 0); - _stack->push(_operand); - break; - - case II_CMP_L: - op2 = _stack->pop(); - op1 = _stack->pop(); - - /* - if(op1->getType()==VAL_FLOAT && op2->getType()==VAL_FLOAT){ - _operand->setBool(op1->getFloat() < op2->getFloat()); - } - else _operand->setBool(op1->getInt() < op2->getInt()); - */ - - _operand->setBool(CScValue::compare(op1, op2) < 0); - _stack->push(_operand); - break; - - case II_CMP_G: - op2 = _stack->pop(); - op1 = _stack->pop(); - - /* - if(op1->getType()==VAL_FLOAT && op2->getType()==VAL_FLOAT){ - _operand->setBool(op1->getFloat() > op2->getFloat()); - } - else _operand->setBool(op1->getInt() > op2->getInt()); - */ - - _operand->setBool(CScValue::compare(op1, op2) > 0); - _stack->push(_operand); - break; - - case II_CMP_LE: - op2 = _stack->pop(); - op1 = _stack->pop(); - - /* - if(op1->getType()==VAL_FLOAT && op2->getType()==VAL_FLOAT){ - _operand->setBool(op1->getFloat() <= op2->getFloat()); - } - else _operand->setBool(op1->getInt() <= op2->getInt()); - */ - - _operand->setBool(CScValue::compare(op1, op2) <= 0); - _stack->push(_operand); - break; - - case II_CMP_GE: - op2 = _stack->pop(); - op1 = _stack->pop(); - - /* - if(op1->getType()==VAL_FLOAT && op2->getType()==VAL_FLOAT){ - _operand->setBool(op1->getFloat() >= op2->getFloat()); - } - else _operand->setBool(op1->getInt() >= op2->getInt()); - */ - - _operand->setBool(CScValue::compare(op1, op2) >= 0); - _stack->push(_operand); - break; - - case II_CMP_STRICT_EQ: - op2 = _stack->pop(); - op1 = _stack->pop(); - - //_operand->setBool(op1->getType()==op2->getType() && op1->getFloat()==op2->getFloat()); - _operand->setBool(CScValue::compareStrict(op1, op2) == 0); - _stack->push(_operand); - - break; - - case II_CMP_STRICT_NE: - op2 = _stack->pop(); - op1 = _stack->pop(); - - //_operand->setBool(op1->getType()!=op2->getType() || op1->getFloat()!=op2->getFloat()); - _operand->setBool(CScValue::compareStrict(op1, op2) != 0); - _stack->push(_operand); - break; - - case II_DBG_LINE: { - int newLine = getDWORD(); - if (newLine != _currentLine) { - _currentLine = newLine; - if (_gameRef->getDebugMgr()->_enabled) { - _gameRef->getDebugMgr()->onScriptChangeLine(this, _currentLine); - for (int i = 0; i < _breakpoints.getSize(); i++) { - if (_breakpoints[i] == _currentLine) { - _gameRef->getDebugMgr()->onScriptHitBreakpoint(this); - sleep(0); - break; - } - } - if (_tracingMode) { - _gameRef->getDebugMgr()->onScriptHitBreakpoint(this); - sleep(0); - break; - } - } - } - break; - - } - default: - _gameRef->LOG(0, "Fatal: Invalid instruction %d ('%s', line %d, IP:0x%x)\n", inst, _filename, _currentLine, _iP - sizeof(uint32)); - _state = SCRIPT_FINISHED; - ret = STATUS_FAILED; - } // switch(instruction) - - //delete op; - - return ret; -} - - -////////////////////////////////////////////////////////////////////////// -uint32 CScScript::getFuncPos(const char *name) { - for (uint32 i = 0; i < _numFunctions; i++) { - if (strcmp(name, _functions[i].name) == 0) - return _functions[i].pos; - } - return 0; -} - - -////////////////////////////////////////////////////////////////////////// -uint32 CScScript::getMethodPos(const char *name) { - for (uint32 i = 0; i < _numMethods; i++) { - if (strcmp(name, _methods[i].name) == 0) - return _methods[i].pos; - } - return 0; -} - - -////////////////////////////////////////////////////////////////////////// -CScValue *CScScript::getVar(char *name) { - CScValue *ret = NULL; - - // scope locals - if (_scopeStack->_sP >= 0) { - if (_scopeStack->getTop()->propExists(name)) - ret = _scopeStack->getTop()->getProp(name); - } - - // script globals - if (ret == NULL) { - if (_globals->propExists(name)) - ret = _globals->getProp(name); - } - - // engine globals - if (ret == NULL) { - if (_engine->_globals->propExists(name)) - ret = _engine->_globals->getProp(name); - } - - if (ret == NULL) { - //RuntimeError("Variable '%s' is inaccessible in the current block. Consider changing the script.", name); - _gameRef->LOG(0, "Warning: variable '%s' is inaccessible in the current block. Consider changing the script (script:%s, line:%d)", name, _filename, _currentLine); - CScValue *val = new CScValue(_gameRef); - CScValue *scope = _scopeStack->getTop(); - if (scope) { - scope->setProp(name, val); - ret = _scopeStack->getTop()->getProp(name); - } else { - _globals->setProp(name, val); - ret = _globals->getProp(name); - } - delete val; - } - - return ret; -} - - -////////////////////////////////////////////////////////////////////////// -bool CScScript::waitFor(CBObject *object) { - if (_unbreakable) { - runtimeError("Script cannot be interrupted."); - return STATUS_OK; - } - - _state = SCRIPT_WAITING; - _waitObject = object; - return STATUS_OK; -} - - -////////////////////////////////////////////////////////////////////////// -bool CScScript::waitForExclusive(CBObject *object) { - _engine->resetObject(object); - return waitFor(object); -} - - -////////////////////////////////////////////////////////////////////////// -bool CScScript::sleep(uint32 duration) { - if (_unbreakable) { - runtimeError("Script cannot be interrupted."); - return STATUS_OK; - } - - _state = SCRIPT_SLEEPING; - if (_gameRef->_state == GAME_FROZEN) { - _waitTime = CBPlatform::getTime() + duration; - _waitFrozen = true; - } else { - _waitTime = _gameRef->_timer + duration; - _waitFrozen = false; - } - return STATUS_OK; -} - - -////////////////////////////////////////////////////////////////////////// -bool CScScript::finish(bool includingThreads) { - if (_state != SCRIPT_FINISHED && includingThreads) { - _state = SCRIPT_FINISHED; - finishThreads(); - } else _state = SCRIPT_FINISHED; - - - return STATUS_OK; -} - - -////////////////////////////////////////////////////////////////////////// -bool CScScript::run() { - _state = SCRIPT_RUNNING; - return STATUS_OK; -} - - -////////////////////////////////////////////////////////////////////// -void CScScript::runtimeError(const char *fmt, ...) { - char buff[256]; - va_list va; - - va_start(va, fmt); - vsprintf(buff, fmt, va); - va_end(va); - - _gameRef->LOG(0, "Runtime error. Script '%s', line %d", _filename, _currentLine); - _gameRef->LOG(0, " %s", buff); - - if (!_gameRef->_suppressScriptErrors) - _gameRef->quickMessage("Script runtime error. View log for details."); -} - - -////////////////////////////////////////////////////////////////////////// -bool CScScript::persist(CBPersistMgr *persistMgr) { - - persistMgr->transfer(TMEMBER(_gameRef)); - - // buffer - if (persistMgr->_saving) { - if (_state != SCRIPT_PERSISTENT && _state != SCRIPT_FINISHED && _state != SCRIPT_THREAD_FINISHED) { - persistMgr->transfer(TMEMBER(_bufferSize)); - persistMgr->putBytes(_buffer, _bufferSize); - } else { - // don't save idle/finished scripts - int bufferSize = 0; - persistMgr->transfer(TMEMBER(bufferSize)); - } - } else { - persistMgr->transfer(TMEMBER(_bufferSize)); - if (_bufferSize > 0) { - _buffer = new byte[_bufferSize]; - persistMgr->getBytes(_buffer, _bufferSize); - _scriptStream = new Common::MemoryReadStream(_buffer, _bufferSize); - initTables(); - } else { - _buffer = NULL; - _scriptStream = NULL; - } - } - - persistMgr->transfer(TMEMBER(_callStack)); - persistMgr->transfer(TMEMBER(_currentLine)); - persistMgr->transfer(TMEMBER(_engine)); - persistMgr->transfer(TMEMBER(_filename)); - persistMgr->transfer(TMEMBER(_freezable)); - persistMgr->transfer(TMEMBER(_globals)); - persistMgr->transfer(TMEMBER(_iP)); - persistMgr->transfer(TMEMBER(_scopeStack)); - persistMgr->transfer(TMEMBER(_stack)); - persistMgr->transfer(TMEMBER_INT(_state)); - persistMgr->transfer(TMEMBER(_operand)); - persistMgr->transfer(TMEMBER_INT(_origState)); - persistMgr->transfer(TMEMBER(_owner)); - persistMgr->transfer(TMEMBER(_reg1)); - persistMgr->transfer(TMEMBER(_thread)); - persistMgr->transfer(TMEMBER(_threadEvent)); - persistMgr->transfer(TMEMBER(_thisStack)); - persistMgr->transfer(TMEMBER(_timeSlice)); - persistMgr->transfer(TMEMBER(_waitObject)); - persistMgr->transfer(TMEMBER(_waitScript)); - persistMgr->transfer(TMEMBER(_waitTime)); - persistMgr->transfer(TMEMBER(_waitFrozen)); - - persistMgr->transfer(TMEMBER(_methodThread)); - persistMgr->transfer(TMEMBER(_methodThread)); - persistMgr->transfer(TMEMBER(_unbreakable)); - persistMgr->transfer(TMEMBER(_parentScript)); - - if (!persistMgr->_saving) _tracingMode = false; - - return STATUS_OK; -} - - -////////////////////////////////////////////////////////////////////////// -CScScript *CScScript::invokeEventHandler(const char *eventName, bool unbreakable) { - //if(_state!=SCRIPT_PERSISTENT) return NULL; - - uint32 pos = getEventPos(eventName); - if (!pos) return NULL; - - CScScript *thread = new CScScript(_gameRef, _engine); - if (thread) { - bool ret = thread->createThread(this, pos, eventName); - if (DID_SUCCEED(ret)) { - thread->_unbreakable = unbreakable; - _engine->_scripts.add(thread); - _gameRef->getDebugMgr()->onScriptEventThreadInit(thread, this, eventName); - return thread; - } else { - delete thread; - return NULL; - } - } else return NULL; - -} - - -////////////////////////////////////////////////////////////////////////// -uint32 CScScript::getEventPos(const char *name) { - for (int i = _numEvents - 1; i >= 0; i--) { - if (scumm_stricmp(name, _events[i].name) == 0) return _events[i].pos; - } - return 0; -} - - -////////////////////////////////////////////////////////////////////////// -bool CScScript::canHandleEvent(const char *eventName) { - return getEventPos(eventName) != 0; -} - - -////////////////////////////////////////////////////////////////////////// -bool CScScript::canHandleMethod(const char *methodName) { - return getMethodPos(methodName) != 0; -} - - -////////////////////////////////////////////////////////////////////////// -bool CScScript::pause() { - if (_state == SCRIPT_PAUSED) { - _gameRef->LOG(0, "Attempting to pause a paused script ('%s', line %d)", _filename, _currentLine); - return STATUS_FAILED; - } - - if (!_freezable || _state == SCRIPT_PERSISTENT) return STATUS_OK; - - _origState = _state; - _state = SCRIPT_PAUSED; - - return STATUS_OK; -} - - -////////////////////////////////////////////////////////////////////////// -bool CScScript::resume() { - if (_state != SCRIPT_PAUSED) return STATUS_OK; - - _state = _origState; - return STATUS_OK; -} - - -////////////////////////////////////////////////////////////////////////// -CScScript::TExternalFunction *CScScript::getExternal(char *name) { - for (uint32 i = 0; i < _numExternals; i++) { - if (strcmp(name, _externals[i].name) == 0) - return &_externals[i]; - } - return NULL; -} - - -////////////////////////////////////////////////////////////////////////// -bool CScScript::externalCall(CScStack *stack, CScStack *thisStack, CScScript::TExternalFunction *function) { - - _gameRef->LOG(0, "External functions are not supported on this platform."); - stack->correctParams(0); - stack->pushNULL(); - return STATUS_FAILED; -} - - -////////////////////////////////////////////////////////////////////////// -bool CScScript::copyParameters(CScStack *stack) { - int i; - int NumParams = stack->pop()->getInt(); - for (i = NumParams - 1; i >= 0; i--) { - _stack->push(stack->getAt(i)); - } - _stack->pushInt(NumParams); - - for (i = 0; i < NumParams; i++) stack->pop(); - - return STATUS_OK; -} - - -////////////////////////////////////////////////////////////////////////// -bool CScScript::finishThreads() { - for (int i = 0; i < _engine->_scripts.getSize(); i++) { - CScScript *scr = _engine->_scripts[i]; - if (scr->_thread && scr->_state != SCRIPT_FINISHED && scr->_owner == _owner && scumm_stricmp(scr->_filename, _filename) == 0) - scr->finish(true); - } - return STATUS_OK; -} - - -////////////////////////////////////////////////////////////////////////// -// IWmeDebugScript interface implementation -int CScScript::dbgGetLine() { - return _currentLine; -} - -////////////////////////////////////////////////////////////////////////// -const char *CScScript::dbgGetFilename() { - return _filename; -} - - -////////////////////////////////////////////////////////////////////////// -bool CScScript::dbgSendScript(IWmeDebugClient *client) { - if (_methodThread) client->onScriptMethodThreadInit(this, _parentScript, _threadEvent); - else if (_thread) client->onScriptEventThreadInit(this, _parentScript, _threadEvent); - else client->onScriptInit(this); - - return dbgSendVariables(client); - return STATUS_OK; -} - -////////////////////////////////////////////////////////////////////////// -bool CScScript::dbgSendVariables(IWmeDebugClient *client) { - // send script globals - _globals->dbgSendVariables(client, WME_DBGVAR_SCRIPT, this, 0); - - // send scope variables - if (_scopeStack->_sP >= 0) { - for (int i = 0; i <= _scopeStack->_sP; i++) { - // CScValue *Scope = _scopeStack->GetAt(i); - //Scope->DbgSendVariables(Client, WME_DBGVAR_SCOPE, this, (unsigned int)Scope); - } - } - return STATUS_OK; -} - - -////////////////////////////////////////////////////////////////////////// -TScriptState CScScript::dbgGetState() { - return _state; -} - -////////////////////////////////////////////////////////////////////////// -int CScScript::dbgGetNumBreakpoints() { - return _breakpoints.getSize(); -} - -////////////////////////////////////////////////////////////////////////// -int CScScript::dbgGetBreakpoint(int index) { - if (index >= 0 && index < _breakpoints.getSize()) return _breakpoints[index]; - else return -1; -} - -////////////////////////////////////////////////////////////////////////// -bool CScScript::dbgSetTracingMode(bool isTracing) { - _tracingMode = isTracing; - return true; -} - -////////////////////////////////////////////////////////////////////////// -bool CScScript::dbgGetTracingMode() { - return _tracingMode; -} - - -////////////////////////////////////////////////////////////////////////// -void CScScript::afterLoad() { - if (_buffer == NULL) { - byte *buffer = _engine->getCompiledScript(_filename, &_bufferSize); - if (!buffer) { - _gameRef->LOG(0, "Error reinitializing script '%s' after load. Script will be terminated.", _filename); - _state = SCRIPT_ERROR; - return; - } - - _buffer = new byte [_bufferSize]; - memcpy(_buffer, buffer, _bufferSize); - - delete _scriptStream; - _scriptStream = new Common::MemoryReadStream(_buffer, _bufferSize); - - initTables(); - } -} - -} // end of namespace WinterMute diff --git a/engines/wintermute/base/scriptables/ScScript.h b/engines/wintermute/base/scriptables/ScScript.h deleted file mode 100644 index c031f8186f..0000000000 --- a/engines/wintermute/base/scriptables/ScScript.h +++ /dev/null @@ -1,183 +0,0 @@ -/* ScummVM - Graphic Adventure Engine - * - * ScummVM is the legal property of its developers, whose names - * are too numerous to list here. Please refer to the COPYRIGHT - * file distributed with this source distribution. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - */ - -/* - * This file is based on WME Lite. - * http://dead-code.org/redir.php?target=wmelite - * Copyright (c) 2011 Jan Nedoma - */ - -#ifndef WINTERMUTE_SCSCRIPT_H -#define WINTERMUTE_SCSCRIPT_H - - -#include "engines/wintermute/base/BBase.h" -#include "engines/wintermute/dcscript.h" // Added by ClassView -#include "engines/wintermute/coll_templ.h" - -#include "engines/wintermute/wme_debugger.h" - -namespace WinterMute { -class CBScriptHolder; -class CBObject; -class CScEngine; -class CScStack; -class CScScript : public CBBase, public IWmeDebugScript { -public: - bool dbgSendScript(IWmeDebugClient *client); - bool dbgSendVariables(IWmeDebugClient *client); - - CBArray _breakpoints; - bool _tracingMode; - - CScScript *_parentScript; - bool _unbreakable; - bool finishThreads(); - bool copyParameters(CScStack *stack); - - void afterLoad(); - - CScValue *_operand; - CScValue *_reg1; - bool _freezable; - bool resume(); - bool pause(); - bool canHandleEvent(const char *eventName); - bool canHandleMethod(const char *methodName); - bool createThread(CScScript *original, uint32 initIP, const char *eventName); - bool createMethodThread(CScScript *original, const char *methodName); - CScScript *invokeEventHandler(const char *eventName, bool unbreakable = false); - uint32 _timeSlice; - DECLARE_PERSISTENT(CScScript, CBBase) - void runtimeError(const char *fmt, ...); - bool run(); - bool finish(bool includingThreads = false); - bool sleep(uint32 duration); - bool waitForExclusive(CBObject *object); - bool waitFor(CBObject *object); - uint32 _waitTime; - bool _waitFrozen; - CBObject *_waitObject; - CScScript *_waitScript; - TScriptState _state; - TScriptState _origState; - CScValue *getVar(char *name); - uint32 getFuncPos(const char *name); - uint32 getEventPos(const char *name); - uint32 getMethodPos(const char *name); - typedef struct { - uint32 magic; - uint32 version; - uint32 code_start; - uint32 func_table; - uint32 symbol_table; - uint32 event_table; - uint32 externals_table; - uint32 method_table; - } TScriptHeader; - - TScriptHeader _header; - - typedef struct { - char *name; - uint32 pos; - } TFunctionPos; - - typedef struct { - char *name; - uint32 pos; - } TMethodPos; - - typedef struct { - char *name; - uint32 pos; - } TEventPos; - - typedef struct { - char *name; - char *dll_name; - TCallType call_type; - TExternalType returns; - int nu_params; - TExternalType *params; - } TExternalFunction; - - - CScStack *_callStack; - CScStack *_thisStack; - CScStack *_scopeStack; - CScStack *_stack; - CScValue *_globals; - CScEngine *_engine; - int _currentLine; - bool executeInstruction(); - char *getString(); - uint32 getDWORD(); - double getFloat(); - void cleanup(); - bool create(const char *filename, byte *buffer, uint32 size, CBScriptHolder *owner); - uint32 _iP; -private: - void readHeader(); - uint32 _bufferSize; - byte *_buffer; -public: - Common::SeekableReadStream *_scriptStream; - CScScript(CBGame *inGame, CScEngine *Engine); - virtual ~CScScript(); - char *_filename; - char **_symbols; - uint32 _numSymbols; - TFunctionPos *_functions; - TMethodPos *_methods; - TEventPos *_events; - uint32 _numExternals; - TExternalFunction *_externals; - uint32 _numFunctions; - uint32 _numMethods; - uint32 _numEvents; - bool _thread; - bool _methodThread; - char *_threadEvent; - CBScriptHolder *_owner; - CScScript::TExternalFunction *getExternal(char *name); - bool externalCall(CScStack *stack, CScStack *thisStack, CScScript::TExternalFunction *function); -private: - bool initScript(); - bool initTables(); - - -// IWmeDebugScript interface implementation -public: - virtual int dbgGetLine(); - virtual const char *dbgGetFilename(); - virtual TScriptState dbgGetState(); - virtual int dbgGetNumBreakpoints(); - virtual int dbgGetBreakpoint(int Index); - - virtual bool dbgSetTracingMode(bool IsTracing); - virtual bool dbgGetTracingMode(); -}; - -} // end of namespace WinterMute - -#endif diff --git a/engines/wintermute/base/scriptables/ScStack.cpp b/engines/wintermute/base/scriptables/ScStack.cpp deleted file mode 100644 index 252cd21dda..0000000000 --- a/engines/wintermute/base/scriptables/ScStack.cpp +++ /dev/null @@ -1,226 +0,0 @@ -/* ScummVM - Graphic Adventure Engine - * - * ScummVM is the legal property of its developers, whose names - * are too numerous to list here. Please refer to the COPYRIGHT - * file distributed with this source distribution. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - */ - -/* - * This file is based on WME Lite. - * http://dead-code.org/redir.php?target=wmelite - * Copyright (c) 2011 Jan Nedoma - */ - -#include "engines/wintermute/base/scriptables/ScStack.h" -#include "engines/wintermute/base/scriptables/ScValue.h" -#include "engines/wintermute/base/BGame.h" - -namespace WinterMute { - -IMPLEMENT_PERSISTENT(CScStack, false) - -////////////////////////////////////////////////////////////////////////// -CScStack::CScStack(CBGame *inGame): CBBase(inGame) { - _sP = -1; -} - - -////////////////////////////////////////////////////////////////////////// -CScStack::~CScStack() { - -#if _DEBUG - //_gameRef->LOG(0, "STAT: Stack size: %d, SP=%d", _values.getSize(), _sP); -#endif - - for (int i = 0; i < _values.getSize(); i++) { - delete _values[i]; - } - _values.removeAll(); -} - - -////////////////////////////////////////////////////////////////////////// -CScValue *CScStack::pop() { - if (_sP < 0) { - _gameRef->LOG(0, "Fatal: Stack underflow"); - return NULL; - } - - return _values[_sP--]; -} - - -////////////////////////////////////////////////////////////////////////// -void CScStack::push(CScValue *val) { - _sP++; - - if (_sP < _values.getSize()) { - _values[_sP]->cleanup(); - _values[_sP]->copy(val); - } else { - CScValue *copyVal = new CScValue(_gameRef); - copyVal->copy(val); - _values.add(copyVal); - } -} - - -////////////////////////////////////////////////////////////////////////// -CScValue *CScStack::getPushValue() { - _sP++; - - if (_sP >= _values.getSize()) { - CScValue *val = new CScValue(_gameRef); - _values.add(val); - } - _values[_sP]->cleanup(); - return _values[_sP]; -} - - - -////////////////////////////////////////////////////////////////////////// -CScValue *CScStack::getTop() { - if (_sP < 0 || _sP >= _values.getSize()) return NULL; - else return _values[_sP]; -} - - -////////////////////////////////////////////////////////////////////////// -CScValue *CScStack::getAt(int index) { - index = _sP - index; - if (index < 0 || index >= _values.getSize()) return NULL; - else return _values[index]; -} - - -////////////////////////////////////////////////////////////////////////// -void CScStack::correctParams(uint32 expectedParams) { - uint32 nuParams = (uint32)pop()->getInt(); - - if (expectedParams < nuParams) { // too many params - while (expectedParams < nuParams) { - //Pop(); - delete _values[_sP - expectedParams]; - _values.removeAt(_sP - expectedParams); - nuParams--; - _sP--; - } - } else if (expectedParams > nuParams) { // need more params - while (expectedParams > nuParams) { - //Push(null_val); - CScValue *nullVal = new CScValue(_gameRef); - nullVal->setNULL(); - _values.insertAt(_sP - nuParams + 1, nullVal); - nuParams++; - _sP++; - - if (_values.getSize() > _sP + 1) { - delete _values[_values.getSize() - 1]; - _values.removeAt(_values.getSize() - 1); - } - } - } -} - - -////////////////////////////////////////////////////////////////////////// -void CScStack::pushNULL() { - /* - CScValue* val = new CScValue(_gameRef); - val->setNULL(); - Push(val); - delete val; - */ - getPushValue()->setNULL(); -} - - -////////////////////////////////////////////////////////////////////////// -void CScStack::pushInt(int val) { - /* - CScValue* val = new CScValue(_gameRef); - val->setInt(Val); - Push(val); - delete val; - */ - getPushValue()->setInt(val); -} - - -////////////////////////////////////////////////////////////////////////// -void CScStack::pushFloat(double val) { - /* - CScValue* val = new CScValue(_gameRef); - val->setFloat(Val); - Push(val); - delete val; - */ - getPushValue()->setFloat(val); -} - - -////////////////////////////////////////////////////////////////////////// -void CScStack::pushBool(bool val) { - /* - CScValue* val = new CScValue(_gameRef); - val->setBool(Val); - Push(val); - delete val; - */ - getPushValue()->setBool(val); -} - - -////////////////////////////////////////////////////////////////////////// -void CScStack::pushString(const char *val) { - /* - CScValue* val = new CScValue(_gameRef); - val->setString(Val); - Push(val); - delete val; - */ - getPushValue()->setString(val); -} - - -////////////////////////////////////////////////////////////////////////// -void CScStack::pushNative(CBScriptable *val, bool persistent) { - /* - CScValue* val = new CScValue(_gameRef); - val->setNative(Val, Persistent); - Push(val); - delete val; - */ - - getPushValue()->setNative(val, persistent); -} - - -////////////////////////////////////////////////////////////////////////// -bool CScStack::persist(CBPersistMgr *persistMgr) { - - persistMgr->transfer(TMEMBER(_gameRef)); - - persistMgr->transfer(TMEMBER(_sP)); - _values.persist(persistMgr); - - return STATUS_OK; -} - -} // end of namespace WinterMute diff --git a/engines/wintermute/base/scriptables/ScStack.h b/engines/wintermute/base/scriptables/ScStack.h deleted file mode 100644 index 22dae63060..0000000000 --- a/engines/wintermute/base/scriptables/ScStack.h +++ /dev/null @@ -1,66 +0,0 @@ -/* ScummVM - Graphic Adventure Engine - * - * ScummVM is the legal property of its developers, whose names - * are too numerous to list here. Please refer to the COPYRIGHT - * file distributed with this source distribution. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - */ - -/* - * This file is based on WME Lite. - * http://dead-code.org/redir.php?target=wmelite - * Copyright (c) 2011 Jan Nedoma - */ - -#ifndef WINTERMUTE_SCSTACK_H -#define WINTERMUTE_SCSTACK_H - - -#include "engines/wintermute/base/BBase.h" -#include "engines/wintermute/coll_templ.h" -#include "engines/wintermute/persistent.h" - -namespace WinterMute { - -class CScValue; -class CBScriptable; - -class CScStack : public CBBase { -public: - CScValue *getAt(int Index); - CScValue *getPushValue(); - DECLARE_PERSISTENT(CScStack, CBBase) - void pushNative(CBScriptable *val, bool persistent); - void pushString(const char *val); - void pushBool(bool val); - void pushInt(int val); - void pushFloat(double val); - void pushNULL(); - void correctParams(uint32 expectedParams); - CScValue *getTop(); - void push(CScValue *val); - CScValue *pop(); - CScStack(CBGame *inGame); - virtual ~CScStack(); - CBArray _values; - int _sP; - -}; - -} // end of namespace WinterMute - -#endif diff --git a/engines/wintermute/base/scriptables/ScValue.cpp b/engines/wintermute/base/scriptables/ScValue.cpp deleted file mode 100644 index f38a12fc7a..0000000000 --- a/engines/wintermute/base/scriptables/ScValue.cpp +++ /dev/null @@ -1,1054 +0,0 @@ -/* ScummVM - Graphic Adventure Engine - * - * ScummVM is the legal property of its developers, whose names - * are too numerous to list here. Please refer to the COPYRIGHT - * file distributed with this source distribution. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - */ - -/* - * This file is based on WME Lite. - * http://dead-code.org/redir.php?target=wmelite - * Copyright (c) 2011 Jan Nedoma - */ - -#include "engines/wintermute/platform_osystem.h" -#include "engines/wintermute/base/BDynBuffer.h" -#include "engines/wintermute/base/BGame.h" -#include "engines/wintermute/base/scriptables/ScValue.h" -#include "engines/wintermute/base/scriptables/ScScript.h" -#include "engines/wintermute/utils/StringUtil.h" -#include "engines/wintermute/base/BScriptable.h" - -namespace WinterMute { - -////////////////////////////////////////////////////////////////////// -// Construction/Destruction -////////////////////////////////////////////////////////////////////// - -IMPLEMENT_PERSISTENT(CScValue, false) - -////////////////////////////////////////////////////////////////////////// -CScValue::CScValue(CBGame *inGame): CBBase(inGame) { - _type = VAL_NULL; - - _valBool = false; - _valInt = 0; - _valFloat = 0.0f; - _valNative = NULL; - _valString = NULL; - _valRef = NULL; - _persistent = false; - _isConstVar = false; -} - - -////////////////////////////////////////////////////////////////////////// -CScValue::CScValue(CBGame *inGame, bool val): CBBase(inGame) { - _type = VAL_BOOL; - _valBool = val; - - _valInt = 0; - _valFloat = 0.0f; - _valNative = NULL; - _valString = NULL; - _valRef = NULL; - _persistent = false; - _isConstVar = false; -} - - -////////////////////////////////////////////////////////////////////////// -CScValue::CScValue(CBGame *inGame, int val): CBBase(inGame) { - _type = VAL_INT; - _valInt = val; - - _valFloat = 0.0f; - _valBool = false; - _valNative = NULL; - _valString = NULL; - _valRef = NULL; - _persistent = false; - _isConstVar = false; -} - - -////////////////////////////////////////////////////////////////////////// -CScValue::CScValue(CBGame *inGame, double val): CBBase(inGame) { - _type = VAL_FLOAT; - _valFloat = val; - - _valInt = 0; - _valBool = false; - _valNative = NULL; - _valString = NULL; - _valRef = NULL; - _persistent = false; - _isConstVar = false; -} - - -////////////////////////////////////////////////////////////////////////// -CScValue::CScValue(CBGame *inGame, const char *val): CBBase(inGame) { - _type = VAL_STRING; - _valString = NULL; - setStringVal(val); - - _valBool = false; - _valInt = 0; - _valFloat = 0.0f; - _valNative = NULL; - _valRef = NULL; - _persistent = false; - _isConstVar = false; -} - - -////////////////////////////////////////////////////////////////////////// -void CScValue::cleanup(bool ignoreNatives) { - deleteProps(); - - if (_valString) delete [] _valString; - - if (!ignoreNatives) { - if (_valNative && !_persistent) { - _valNative->_refCount--; - if (_valNative->_refCount <= 0) { - delete _valNative; - _valNative = NULL; - } - } - } - - - _type = VAL_NULL; - - _valBool = false; - _valInt = 0; - _valFloat = 0.0f; - _valNative = NULL; - _valString = NULL; - _valRef = NULL; - _persistent = false; - _isConstVar = false; -} - - - -////////////////////////////////////////////////////////////////////////// -CScValue::~CScValue() { - cleanup(); -} - - -////////////////////////////////////////////////////////////////////////// -CScValue *CScValue::getProp(const char *name) { - if (_type == VAL_VARIABLE_REF) return _valRef->getProp(name); - - if (_type == VAL_STRING && strcmp(name, "Length") == 0) { - _gameRef->_scValue->_type = VAL_INT; - -#if 0 // TODO: Remove FreeType-dependency - if (_gameRef->_textEncoding == TEXT_ANSI) { -#else - if (true) { -#endif - _gameRef->_scValue->setInt(strlen(_valString)); - } else { - WideString wstr = StringUtil::utf8ToWide(_valString); - _gameRef->_scValue->setInt(wstr.size()); - } - - return _gameRef->_scValue; - } - - CScValue *ret = NULL; - - if (_type == VAL_NATIVE && _valNative) ret = _valNative->scGetProperty(name); - - if (ret == NULL) { - _valIter = _valObject.find(name); - if (_valIter != _valObject.end()) ret = _valIter->_value; - } - return ret; -} - -////////////////////////////////////////////////////////////////////////// -bool CScValue::deleteProp(const char *name) { - if (_type == VAL_VARIABLE_REF) return _valRef->deleteProp(name); - - _valIter = _valObject.find(name); - if (_valIter != _valObject.end()) { - delete _valIter->_value; - _valIter->_value = NULL; - } - - return STATUS_OK; -} - - - -////////////////////////////////////////////////////////////////////////// -bool CScValue::setProp(const char *name, CScValue *val, bool copyWhole, bool setAsConst) { - if (_type == VAL_VARIABLE_REF) - return _valRef->setProp(name, val); - - bool ret = STATUS_FAILED; - if (_type == VAL_NATIVE && _valNative) { - ret = _valNative->scSetProperty(name, val); - } - - if (DID_FAIL(ret)) { - CScValue *newVal = NULL; - - _valIter = _valObject.find(name); - if (_valIter != _valObject.end()) { - newVal = _valIter->_value; - } - if (!newVal) - newVal = new CScValue(_gameRef); - else newVal->cleanup(); - - newVal->copy(val, copyWhole); - newVal->_isConstVar = setAsConst; - _valObject[name] = newVal; - - if (_type != VAL_NATIVE) _type = VAL_OBJECT; - - /* - _valIter = _valObject.find(Name); - if (_valIter != _valObject.end()){ - delete _valIter->_value; - _valIter->_value = NULL; - } - CScValue* val = new CScValue(_gameRef); - val->Copy(Val, CopyWhole); - val->_isConstVar = SetAsConst; - _valObject[Name] = val; - - if(_type!=VAL_NATIVE) _type = VAL_OBJECT; - */ - } - - return STATUS_OK; -} - - -////////////////////////////////////////////////////////////////////////// -bool CScValue::propExists(const char *name) { - if (_type == VAL_VARIABLE_REF) - return _valRef->propExists(name); - _valIter = _valObject.find(name); - - return (_valIter != _valObject.end()); -} - - -////////////////////////////////////////////////////////////////////////// -void CScValue::deleteProps() { - _valIter = _valObject.begin(); - while (_valIter != _valObject.end()) { - delete(CScValue *)_valIter->_value; - _valIter++; - } - _valObject.clear(); -} - - -////////////////////////////////////////////////////////////////////////// -void CScValue::CleanProps(bool includingNatives) { - _valIter = _valObject.begin(); - while (_valIter != _valObject.end()) { - if (!_valIter->_value->_isConstVar && (!_valIter->_value->isNative() || includingNatives)) _valIter->_value->setNULL(); - _valIter++; - } -} - -////////////////////////////////////////////////////////////////////////// -bool CScValue::isNULL() { - if (_type == VAL_VARIABLE_REF) - return _valRef->isNULL(); - - return (_type == VAL_NULL); -} - - -////////////////////////////////////////////////////////////////////////// -bool CScValue::isNative() { - if (_type == VAL_VARIABLE_REF) - return _valRef->isNative(); - - return (_type == VAL_NATIVE); -} - - -////////////////////////////////////////////////////////////////////////// -bool CScValue::isString() { - if (_type == VAL_VARIABLE_REF) - return _valRef->isString(); - - return (_type == VAL_STRING); -} - - -////////////////////////////////////////////////////////////////////////// -bool CScValue::isFloat() { - if (_type == VAL_VARIABLE_REF) - return _valRef->isFloat(); - - return (_type == VAL_FLOAT); -} - - -////////////////////////////////////////////////////////////////////////// -bool CScValue::isInt() { - if (_type == VAL_VARIABLE_REF) - return _valRef->isInt(); - - return (_type == VAL_INT); -} - - -////////////////////////////////////////////////////////////////////////// -bool CScValue::isBool() { - if (_type == VAL_VARIABLE_REF) - return _valRef->isBool(); - - return (_type == VAL_BOOL); -} - - -////////////////////////////////////////////////////////////////////////// -bool CScValue::isObject() { - if (_type == VAL_VARIABLE_REF) - return _valRef->isObject(); - - return (_type == VAL_OBJECT); -} - - -////////////////////////////////////////////////////////////////////////// -TValType CScValue::getTypeTolerant() { - if (_type == VAL_VARIABLE_REF) - return _valRef->getType(); - - return _type; -} - - -////////////////////////////////////////////////////////////////////////// -void CScValue::setBool(bool val) { - if (_type == VAL_VARIABLE_REF) { - _valRef->setBool(val); - return; - } - - if (_type == VAL_NATIVE) { - _valNative->scSetBool(val); - return; - } - - _valBool = val; - _type = VAL_BOOL; -} - - -////////////////////////////////////////////////////////////////////////// -void CScValue::setInt(int val) { - if (_type == VAL_VARIABLE_REF) { - _valRef->setInt(val); - return; - } - - if (_type == VAL_NATIVE) { - _valNative->scSetInt(val); - return; - } - - _valInt = val; - _type = VAL_INT; -} - - -////////////////////////////////////////////////////////////////////////// -void CScValue::setFloat(double val) { - if (_type == VAL_VARIABLE_REF) { - _valRef->setFloat(val); - return; - } - - if (_type == VAL_NATIVE) { - _valNative->scSetFloat(val); - return; - } - - _valFloat = val; - _type = VAL_FLOAT; -} - - -////////////////////////////////////////////////////////////////////////// -void CScValue::setString(const char *val) { - if (_type == VAL_VARIABLE_REF) { - _valRef->setString(val); - return; - } - - if (_type == VAL_NATIVE) { - _valNative->scSetString(val); - return; - } - - setStringVal(val); - if (_valString) _type = VAL_STRING; - else _type = VAL_NULL; -} - -void CScValue::setString(const Common::String &val) { - setString(val.c_str()); -} - -////////////////////////////////////////////////////////////////////////// -void CScValue::setStringVal(const char *val) { - if (_valString) { - delete [] _valString; - _valString = NULL; - } - - if (val == NULL) { - _valString = NULL; - return; - } - - _valString = new char [strlen(val) + 1]; - if (_valString) { - strcpy(_valString, val); - } -} - - -////////////////////////////////////////////////////////////////////////// -void CScValue::setNULL() { - if (_type == VAL_VARIABLE_REF) { - _valRef->setNULL(); - return; - } - - if (_valNative && !_persistent) { - _valNative->_refCount--; - if (_valNative->_refCount <= 0) delete _valNative; - } - _valNative = NULL; - deleteProps(); - - _type = VAL_NULL; -} - - -////////////////////////////////////////////////////////////////////////// -void CScValue::setNative(CBScriptable *val, bool persistent) { - if (_type == VAL_VARIABLE_REF) { - _valRef->setNative(val, persistent); - return; - } - - if (val == NULL) { - setNULL(); - } else { - if (_valNative && !_persistent) { - _valNative->_refCount--; - if (_valNative->_refCount <= 0) { - if (_valNative != val) delete _valNative; - _valNative = NULL; - } - } - - _type = VAL_NATIVE; - _persistent = persistent; - - _valNative = val; - if (_valNative && !_persistent) _valNative->_refCount++; - } -} - - -////////////////////////////////////////////////////////////////////////// -void CScValue::setObject() { - if (_type == VAL_VARIABLE_REF) { - _valRef->setObject(); - return; - } - - deleteProps(); - _type = VAL_OBJECT; -} - - -////////////////////////////////////////////////////////////////////////// -void CScValue::setReference(CScValue *val) { - _valRef = val; - _type = VAL_VARIABLE_REF; -} - - -////////////////////////////////////////////////////////////////////////// -bool CScValue::getBool(bool defaultVal) { - if (_type == VAL_VARIABLE_REF) - return _valRef->getBool(); - - switch (_type) { - case VAL_BOOL: - return _valBool; - - case VAL_NATIVE: - return _valNative->scToBool(); - - case VAL_INT: - return (_valInt != 0); - - case VAL_FLOAT: - return (_valFloat != 0.0f); - - case VAL_STRING: - return (scumm_stricmp(_valString, "1") == 0 || scumm_stricmp(_valString, "yes") == 0 || scumm_stricmp(_valString, "true") == 0); - - default: - return defaultVal; - } -} - - -////////////////////////////////////////////////////////////////////////// -int CScValue::getInt(int defaultVal) { - if (_type == VAL_VARIABLE_REF) return _valRef->getInt(); - - switch (_type) { - case VAL_BOOL: - return _valBool ? 1 : 0; - - case VAL_NATIVE: - return _valNative->scToInt(); - - case VAL_INT: - return _valInt; - - case VAL_FLOAT: - return (int)_valFloat; - - case VAL_STRING: - return atoi(_valString); - - default: - return defaultVal; - } -} - - -////////////////////////////////////////////////////////////////////////// -double CScValue::getFloat(double defaultVal) { - if (_type == VAL_VARIABLE_REF) - return _valRef->getFloat(); - - switch (_type) { - case VAL_BOOL: - return _valBool ? 1.0f : 0.0f; - - case VAL_NATIVE: - return _valNative->scToFloat(); - - case VAL_INT: - return (double)_valInt; - - case VAL_FLOAT: - return _valFloat; - - case VAL_STRING: - return atof(_valString); - - default: - return defaultVal; - } -} - -////////////////////////////////////////////////////////////////////////// -void *CScValue::getMemBuffer() { - if (_type == VAL_VARIABLE_REF) - return _valRef->getMemBuffer(); - - if (_type == VAL_NATIVE) - return _valNative->scToMemBuffer(); - else return (void *)NULL; -} - - -////////////////////////////////////////////////////////////////////////// -const char *CScValue::getString() { - if (_type == VAL_VARIABLE_REF) - return _valRef->getString(); - - switch (_type) { - case VAL_OBJECT: - setStringVal("[object]"); - break; - - case VAL_NULL: - setStringVal("[null]"); - break; - - case VAL_NATIVE: { - const char *strVal = _valNative->scToString(); - setStringVal(strVal); - return strVal; - break; - } - - case VAL_BOOL: - setStringVal(_valBool ? "yes" : "no"); - break; - - case VAL_INT: { - char dummy[50]; - sprintf(dummy, "%d", _valInt); - setStringVal(dummy); - break; - } - - case VAL_FLOAT: { - char dummy[50]; - sprintf(dummy, "%f", _valFloat); - setStringVal(dummy); - break; - } - - case VAL_STRING: - break; - - default: - setStringVal(""); - } - - return _valString; -} - - -////////////////////////////////////////////////////////////////////////// -CBScriptable *CScValue::getNative() { - if (_type == VAL_VARIABLE_REF) - return _valRef->getNative(); - - if (_type == VAL_NATIVE) return _valNative; - else return NULL; -} - - -////////////////////////////////////////////////////////////////////////// -TValType CScValue::getType() { - return _type; -} - - -////////////////////////////////////////////////////////////////////////// -void CScValue::copy(CScValue *orig, bool copyWhole) { - _gameRef = orig->_gameRef; - - if (_valNative && !_persistent) { - _valNative->_refCount--; - if (_valNative->_refCount <= 0) { - if (_valNative != orig->_valNative) delete _valNative; - _valNative = NULL; - } - } - - if (orig->_type == VAL_VARIABLE_REF && orig->_valRef && copyWhole) orig = orig->_valRef; - - cleanup(true); - - _type = orig->_type; - _valBool = orig->_valBool; - _valInt = orig->_valInt; - _valFloat = orig->_valFloat; - setStringVal(orig->_valString); - - _valRef = orig->_valRef; - _persistent = orig->_persistent; - - _valNative = orig->_valNative; - if (_valNative && !_persistent) _valNative->_refCount++; -//!!!! ref->native++ - - // copy properties - if (orig->_type == VAL_OBJECT && orig->_valObject.size() > 0) { - orig->_valIter = orig->_valObject.begin(); - while (orig->_valIter != orig->_valObject.end()) { - _valObject[orig->_valIter->_key] = new CScValue(_gameRef); - _valObject[orig->_valIter->_key]->copy(orig->_valIter->_value); - orig->_valIter++; - } - } else _valObject.clear(); -} - - -////////////////////////////////////////////////////////////////////////// -void CScValue::setValue(CScValue *val) { - if (val->_type == VAL_VARIABLE_REF) { - setValue(val->_valRef); - return; - } - - // if being assigned a simple type, preserve native state - if (_type == VAL_NATIVE && (val->_type == VAL_INT || val->_type == VAL_STRING || val->_type == VAL_BOOL)) { - switch (val->_type) { - case VAL_INT: - _valNative->scSetInt(val->getInt()); - break; - case VAL_FLOAT: - _valNative->scSetFloat(val->getFloat()); - break; - case VAL_BOOL: - _valNative->scSetBool(val->getBool()); - break; - case VAL_STRING: - _valNative->scSetString(val->getString()); - break; - default: - warning("CScValue::setValue - unhandled enum"); - break; - } - } - // otherwise just copy everything - else copy(val); -} - - -////////////////////////////////////////////////////////////////////////// -bool CScValue::persist(CBPersistMgr *persistMgr) { - persistMgr->transfer(TMEMBER(_gameRef)); - - persistMgr->transfer(TMEMBER(_persistent)); - persistMgr->transfer(TMEMBER(_isConstVar)); - persistMgr->transfer(TMEMBER_INT(_type)); - persistMgr->transfer(TMEMBER(_valBool)); - persistMgr->transfer(TMEMBER(_valFloat)); - persistMgr->transfer(TMEMBER(_valInt)); - persistMgr->transfer(TMEMBER(_valNative)); - - int size; - const char *str; - if (persistMgr->_saving) { - size = _valObject.size(); - persistMgr->transfer("", &size); - _valIter = _valObject.begin(); - while (_valIter != _valObject.end()) { - str = _valIter->_key.c_str(); - persistMgr->transfer("", &str); - persistMgr->transfer("", &_valIter->_value); - - _valIter++; - } - } else { - CScValue *val; - persistMgr->transfer("", &size); - for (int i = 0; i < size; i++) { - persistMgr->transfer("", &str); - persistMgr->transfer("", &val); - - _valObject[str] = val; - delete [] str; - } - } - - persistMgr->transfer(TMEMBER(_valRef)); - persistMgr->transfer(TMEMBER(_valString)); - - /* - FILE* f = fopen("c:\\val.log", "a+"); - switch(_type) - { - case VAL_STRING: - fprintf(f, "str %s\n", _valString); - break; - - case VAL_INT: - fprintf(f, "int %d\n", _valInt); - break; - - case VAL_BOOL: - fprintf(f, "bool %d\n", _valBool); - break; - - case VAL_NULL: - fprintf(f, "null\n"); - break; - - case VAL_NATIVE: - fprintf(f, "native\n"); - break; - - case VAL_VARIABLE_REF: - fprintf(f, "ref\n"); - break; - - case VAL_OBJECT: - fprintf(f, "obj\n"); - break; - - case VAL_FLOAT: - fprintf(f, "float\n"); - break; - - } - fclose(f); - */ - - return STATUS_OK; -} - - -////////////////////////////////////////////////////////////////////////// -bool CScValue::saveAsText(CBDynBuffer *buffer, int indent) { - _valIter = _valObject.begin(); - while (_valIter != _valObject.end()) { - buffer->putTextIndent(indent, "PROPERTY {\n"); - buffer->putTextIndent(indent + 2, "NAME=\"%s\"\n", _valIter->_key.c_str()); - buffer->putTextIndent(indent + 2, "VALUE=\"%s\"\n", _valIter->_value->getString()); - buffer->putTextIndent(indent, "}\n\n"); - - _valIter++; - } - return STATUS_OK; -} - - -////////////////////////////////////////////////////////////////////////// -// -1 ... left is less, 0 ... equals, 1 ... left is greater -int CScValue::compare(CScValue *val1, CScValue *val2) { - // both natives? - if (val1->isNative() && val2->isNative()) { - // same class? - if (strcmp(val1->getNative()->getClassName(), val2->getNative()->getClassName()) == 0) { - return val1->getNative()->scCompare(val2->getNative()); - } else return strcmp(val1->getString(), val2->getString()); - } - - // both objects? - if (val1->isObject() && val2->isObject()) return -1; - - - // null states - if (val1->isNULL() && !val2->isNULL()) return -1; - else if (!val1->isNULL() && val2->isNULL()) return 1; - else if (val1->isNULL() && val2->isNULL()) return 0; - - // one of them is string? convert both to string - if (val1->isString() || val2->isString()) return strcmp(val1->getString(), val2->getString()); - - // one of them is float? - if (val1->isFloat() || val2->isFloat()) { - if (val1->getFloat() < val2->getFloat()) return -1; - else if (val1->getFloat() > val2->getFloat()) return 1; - else return 0; - } - - // otherwise compare as int's - if (val1->getInt() < val2->getInt()) return -1; - else if (val1->getInt() > val2->getInt()) return 1; - else return 0; -} - - -////////////////////////////////////////////////////////////////////////// -int CScValue::compareStrict(CScValue *val1, CScValue *val2) { - if (val1->getTypeTolerant() != val2->getTypeTolerant()) return -1; - else return CScValue::compare(val1, val2); -} - - -////////////////////////////////////////////////////////////////////////// -bool CScValue::dbgSendVariables(IWmeDebugClient *client, EWmeDebuggerVariableType type, CScScript *script, unsigned int scopeID) { - _valIter = _valObject.begin(); - while (_valIter != _valObject.end()) { - client->onVariableInit(type, script, scopeID, _valIter->_value, _valIter->_key.c_str()); - _valIter++; - } - return STATUS_OK; -} - - -////////////////////////////////////////////////////////////////////////// -bool CScValue::setProperty(const char *propName, int value) { - CScValue *val = new CScValue(_gameRef, value); - bool ret = DID_SUCCEED(setProp(propName, val)); - delete val; - return ret; -} - -////////////////////////////////////////////////////////////////////////// -bool CScValue::setProperty(const char *propName, const char *value) { - CScValue *val = new CScValue(_gameRef, value); - bool ret = DID_SUCCEED(setProp(propName, val)); - delete val; - return ret; -} - -////////////////////////////////////////////////////////////////////////// -bool CScValue::setProperty(const char *propName, double value) { - CScValue *val = new CScValue(_gameRef, value); - bool ret = DID_SUCCEED(setProp(propName, val)); - delete val; - return ret; -} - - -////////////////////////////////////////////////////////////////////////// -bool CScValue::setProperty(const char *propName, bool value) { - CScValue *val = new CScValue(_gameRef, value); - bool ret = DID_SUCCEED(setProp(propName, val)); - delete val; - return ret; -} - - -////////////////////////////////////////////////////////////////////////// -bool CScValue::setProperty(const char *propName) { - CScValue *val = new CScValue(_gameRef); - bool ret = DID_SUCCEED(setProp(propName, val)); - delete val; - return ret; -} - - -////////////////////////////////////////////////////////////////////////// -// IWmeDebugProp -////////////////////////////////////////////////////////////////////////// -EWmeDebuggerPropType CScValue::dbgGetType() { - switch (getType()) { - case VAL_NULL: - return WME_DBGPROP_NULL; - case VAL_STRING: - return WME_DBGPROP_STRING; - case VAL_INT: - return WME_DBGPROP_INT; - case VAL_BOOL: - return WME_DBGPROP_BOOL; - case VAL_FLOAT: - return WME_DBGPROP_FLOAT; - case VAL_OBJECT: - return WME_DBGPROP_OBJECT; - case VAL_NATIVE: - return WME_DBGPROP_NATIVE; - default: - return WME_DBGPROP_UNKNOWN; - } -} - -////////////////////////////////////////////////////////////////////////// -int CScValue::dbgGetValInt() { - return getInt(); -} - -////////////////////////////////////////////////////////////////////////// -double CScValue::dbgGetValFloat() { - return getFloat(); -} - -////////////////////////////////////////////////////////////////////////// -bool CScValue::dbgGetValBool() { - return getBool(); -} - -////////////////////////////////////////////////////////////////////////// -const char *CScValue::dbgGetValString() { - return getString(); -} - -////////////////////////////////////////////////////////////////////////// -IWmeDebugObject *CScValue::dbgGetValNative() { - return getNative(); -} - -////////////////////////////////////////////////////////////////////////// -bool CScValue::dbgSetVal(int value) { - setInt(value); - return true; -} - -////////////////////////////////////////////////////////////////////////// -bool CScValue::dbgSetVal(double value) { - setFloat(value); - return true; -} - -////////////////////////////////////////////////////////////////////////// -bool CScValue::dbgSetVal(bool value) { - setBool(value); - return true; -} - -////////////////////////////////////////////////////////////////////////// -bool CScValue::dbgSetVal(const char *value) { - setString(value); - return true; -} - -////////////////////////////////////////////////////////////////////////// -bool CScValue::dbgSetVal() { - setNULL(); - return true; -} - - -////////////////////////////////////////////////////////////////////////// -int CScValue::dbgGetNumProperties() { - if (_valNative && _valNative->_scProp) - return _valNative->_scProp->dbgGetNumProperties(); - else return _valObject.size(); -} - -////////////////////////////////////////////////////////////////////////// -bool CScValue::dbgGetProperty(int index, const char **name, IWmeDebugProp **value) { - if (_valNative && _valNative->_scProp) - return _valNative->_scProp->dbgGetProperty(index, name, value); - else { - int count = 0; - _valIter = _valObject.begin(); - while (_valIter != _valObject.end()) { - if (count == index) { - *name = _valIter->_key.c_str(); - *value = _valIter->_value; - return true; - } - _valIter++; - count++; - } - return false; - } -} - -////////////////////////////////////////////////////////////////////////// -bool CScValue::dbgGetDescription(char *buf, int bufSize) { - if (_type == VAL_VARIABLE_REF) - return _valRef->dbgGetDescription(buf, bufSize); - - if (_type == VAL_NATIVE) { - _valNative->scDebuggerDesc(buf, bufSize); - } else { - strncpy(buf, getString(), bufSize); - } - return true; -} - -} // end of namespace WinterMute diff --git a/engines/wintermute/base/scriptables/ScValue.h b/engines/wintermute/base/scriptables/ScValue.h deleted file mode 100644 index c66a60c22a..0000000000 --- a/engines/wintermute/base/scriptables/ScValue.h +++ /dev/null @@ -1,141 +0,0 @@ -/* ScummVM - Graphic Adventure Engine - * - * ScummVM is the legal property of its developers, whose names - * are too numerous to list here. Please refer to the COPYRIGHT - * file distributed with this source distribution. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - */ - -/* - * This file is based on WME Lite. - * http://dead-code.org/redir.php?target=wmelite - * Copyright (c) 2011 Jan Nedoma - */ - -#ifndef WINTERMUTE_SCVALUE_H -#define WINTERMUTE_SCVALUE_H - - -#include "engines/wintermute/base/BBase.h" -#include "engines/wintermute/persistent.h" -#include "engines/wintermute/dcscript.h" // Added by ClassView -#include "engines/wintermute/wme_debugger.h" -#include "common/str.h" - -namespace WinterMute { - -class CScScript; -class CBScriptable; - -class CScValue : public CBBase, public IWmeDebugProp { -public: - bool dbgSendVariables(IWmeDebugClient *client, EWmeDebuggerVariableType type, CScScript *script, unsigned int scopeID); - - static int compare(CScValue *val1, CScValue *val2); - static int compareStrict(CScValue *val1, CScValue *val2); - TValType getTypeTolerant(); - void cleanup(bool ignoreNatives = false); - DECLARE_PERSISTENT(CScValue, CBBase) - - bool _isConstVar; - bool saveAsText(CBDynBuffer *buffer, int indent); - void setValue(CScValue *val); - bool _persistent; - bool propExists(const char *name); - void copy(CScValue *orig, bool copyWhole = false); - void setStringVal(const char *val); - TValType getType(); - bool getBool(bool defaultVal = false); - int getInt(int defaultVal = 0); - double getFloat(double defaultVal = 0.0f); - const char *getString(); - void *getMemBuffer(); - CBScriptable *getNative(); - bool deleteProp(const char *name); - void deleteProps(); - void CleanProps(bool includingNatives); - void setBool(bool val); - void setInt(int val); - void setFloat(double val); - void setString(const char *val); - void setString(const Common::String &val); - void setNULL(); - void setNative(CBScriptable *val, bool persistent = false); - void setObject(); - void setReference(CScValue *val); - bool isNULL(); - bool isNative(); - bool isString(); - bool isBool(); - bool isFloat(); - bool isInt(); - bool isObject(); - bool setProp(const char *name, CScValue *val, bool copyWhole = false, bool setAsConst = false); - CScValue *getProp(const char *name); - CBScriptable *_valNative; - CScValue *_valRef; -protected: - bool _valBool; - int _valInt; - double _valFloat; - char *_valString; -public: - TValType _type; - CScValue(CBGame *inGame); - CScValue(CBGame *inGame, bool Val); - CScValue(CBGame *inGame, int Val); - CScValue(CBGame *inGame, double Val); - CScValue(CBGame *inGame, const char *Val); - virtual ~CScValue(); - Common::HashMap _valObject; - Common::HashMap::iterator _valIter; - - bool setProperty(const char *propName, int value); - bool setProperty(const char *propName, const char *value); - bool setProperty(const char *propName, double value); - bool setProperty(const char *propName, bool value); - bool setProperty(const char *propName); - - -// IWmeDebugProp interface implementation -public: - virtual EWmeDebuggerPropType dbgGetType(); - - // getters - virtual int dbgGetValInt(); - virtual double dbgGetValFloat(); - virtual bool dbgGetValBool(); - virtual const char *dbgGetValString(); - virtual IWmeDebugObject *dbgGetValNative(); - - // setters - virtual bool dbgSetVal(int value); - virtual bool dbgSetVal(double value); - virtual bool dbgSetVal(bool value); - virtual bool dbgSetVal(const char *value); - virtual bool dbgSetVal(); - - // properties - virtual int dbgGetNumProperties(); - virtual bool dbgGetProperty(int index, const char **mame, IWmeDebugProp **value); - - virtual bool dbgGetDescription(char *buf, int bufSize); -}; - -} // end of namespace WinterMute - -#endif diff --git a/engines/wintermute/base/scriptables/SxObject.cpp b/engines/wintermute/base/scriptables/SxObject.cpp deleted file mode 100644 index ba961ed2ae..0000000000 --- a/engines/wintermute/base/scriptables/SxObject.cpp +++ /dev/null @@ -1,67 +0,0 @@ -/* ScummVM - Graphic Adventure Engine - * - * ScummVM is the legal property of its developers, whose names - * are too numerous to list here. Please refer to the COPYRIGHT - * file distributed with this source distribution. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - */ - -/* - * This file is based on WME Lite. - * http://dead-code.org/redir.php?target=wmelite - * Copyright (c) 2011 Jan Nedoma - */ - -#include "SxObject.h" -#include "ScValue.h" -#include "ScStack.h" - -namespace WinterMute { - -////////////////////////////////////////////////////////////////////// -// Construction/Destruction -////////////////////////////////////////////////////////////////////// - -IMPLEMENT_PERSISTENT(CSXObject, false) - -CBScriptable *makeSXObject(CBGame *inGame, CScStack *stack) { - return new CSXObject(inGame, stack); -} - -////////////////////////////////////////////////////////////////////////// -CSXObject::CSXObject(CBGame *inGame, CScStack *stack): CBObject(inGame) { - int numParams = stack->pop()->getInt(0); - for (int i = 0; i < numParams; i++) { - addScript(stack->pop()->getString()); - } -} - - -////////////////////////////////////////////////////////////////////////// -CSXObject::~CSXObject() { - -} - - -////////////////////////////////////////////////////////////////////////// -bool CSXObject::persist(CBPersistMgr *persistMgr) { - CBObject::persist(persistMgr); - - return STATUS_OK; -} - -} // end of namespace WinterMute diff --git a/engines/wintermute/base/scriptables/SxObject.h b/engines/wintermute/base/scriptables/SxObject.h deleted file mode 100644 index b4ec7c6cde..0000000000 --- a/engines/wintermute/base/scriptables/SxObject.h +++ /dev/null @@ -1,47 +0,0 @@ -/* ScummVM - Graphic Adventure Engine - * - * ScummVM is the legal property of its developers, whose names - * are too numerous to list here. Please refer to the COPYRIGHT - * file distributed with this source distribution. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - */ - -/* - * This file is based on WME Lite. - * http://dead-code.org/redir.php?target=wmelite - * Copyright (c) 2011 Jan Nedoma - */ - -#ifndef WINTERMUTE_SXOBJECT_H -#define WINTERMUTE_SXOBJECT_H - - -#include "engines/wintermute/base/BObject.h" - -namespace WinterMute { - -class CSXObject : public CBObject { -public: - DECLARE_PERSISTENT(CSXObject, CBObject) - CSXObject(CBGame *inGame, CScStack *Stack); - virtual ~CSXObject(); - -}; - -} // end of namespace WinterMute - -#endif diff --git a/engines/wintermute/base/scriptables/script.cpp b/engines/wintermute/base/scriptables/script.cpp new file mode 100644 index 0000000000..a9646e0045 --- /dev/null +++ b/engines/wintermute/base/scriptables/script.cpp @@ -0,0 +1,1461 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +/* + * This file is based on WME Lite. + * http://dead-code.org/redir.php?target=wmelite + * Copyright (c) 2011 Jan Nedoma + */ + +#include "engines/wintermute/dcgf.h" +#include "engines/wintermute/base/scriptables/script_value.h" +#include "engines/wintermute/base/scriptables/script.h" +#include "engines/wintermute/base/base_game.h" +#include "engines/wintermute/base/scriptables/script_engine.h" +#include "engines/wintermute/base/scriptables/script_stack.h" +#include "common/memstream.h" + +namespace WinterMute { + +IMPLEMENT_PERSISTENT(CScScript, false) + +////////////////////////////////////////////////////////////////////////// +CScScript::CScScript(CBGame *inGame, CScEngine *Engine): CBBase(inGame) { + _buffer = NULL; + _bufferSize = _iP = 0; + _scriptStream = NULL; + _filename = NULL; + _currentLine = 0; + + _symbols = NULL; + _numSymbols = 0; + + _engine = Engine; + + _globals = NULL; + + _scopeStack = NULL; + _callStack = NULL; + _thisStack = NULL; + _stack = NULL; + + _operand = NULL; + _reg1 = NULL; + + _functions = NULL; + _numFunctions = 0; + + _methods = NULL; + _numMethods = 0; + + _events = NULL; + _numEvents = 0; + + _externals = NULL; + _numExternals = 0; + + _state = SCRIPT_FINISHED; + _origState = SCRIPT_FINISHED; + + _waitObject = NULL; + _waitTime = 0; + _waitFrozen = false; + _waitScript = NULL; + + _timeSlice = 0; + + _thread = false; + _methodThread = false; + _threadEvent = NULL; + + _freezable = true; + _owner = NULL; + + _unbreakable = false; + _parentScript = NULL; + + _tracingMode = false; +} + + +////////////////////////////////////////////////////////////////////////// +CScScript::~CScScript() { + cleanup(); +} + +void CScScript::readHeader() { + uint32 oldPos = _scriptStream->pos(); + _scriptStream->seek(0); + _header.magic = _scriptStream->readUint32LE(); + _header.version = _scriptStream->readUint32LE(); + _header.code_start = _scriptStream->readUint32LE(); + _header.func_table = _scriptStream->readUint32LE(); + _header.symbol_table = _scriptStream->readUint32LE(); + _header.event_table = _scriptStream->readUint32LE(); + _header.externals_table = _scriptStream->readUint32LE(); + _header.method_table = _scriptStream->readUint32LE(); + _scriptStream->seek(oldPos); +} + + +////////////////////////////////////////////////////////////////////////// +bool CScScript::initScript() { + if (!_scriptStream) { + _scriptStream = new Common::MemoryReadStream(_buffer, _bufferSize); + } + readHeader(); + + if (_header.magic != SCRIPT_MAGIC) { + _gameRef->LOG(0, "File '%s' is not a valid compiled script", _filename); + cleanup(); + return STATUS_FAILED; + } + + if (_header.version > SCRIPT_VERSION) { + _gameRef->LOG(0, "Script '%s' has a wrong version %d.%d (expected %d.%d)", _filename, _header.version / 256, _header.version % 256, SCRIPT_VERSION / 256, SCRIPT_VERSION % 256); + cleanup(); + return STATUS_FAILED; + } + + initTables(); + + // init stacks + _scopeStack = new CScStack(_gameRef); + _callStack = new CScStack(_gameRef); + _thisStack = new CScStack(_gameRef); + _stack = new CScStack(_gameRef); + + _operand = new CScValue(_gameRef); + _reg1 = new CScValue(_gameRef); + + + // skip to the beginning + _iP = _header.code_start; + _scriptStream->seek(_iP); + _currentLine = 0; + + // init breakpoints + _engine->refreshScriptBreakpoints(this); + + + // ready to rumble... + _state = SCRIPT_RUNNING; + + return STATUS_OK; +} + + +////////////////////////////////////////////////////////////////////////// +bool CScScript::initTables() { + uint32 OrigIP = _iP; + + readHeader(); + // load symbol table + _iP = _header.symbol_table; + + _numSymbols = getDWORD(); + _symbols = new char*[_numSymbols]; + for (uint32 i = 0; i < _numSymbols; i++) { + uint32 index = getDWORD(); + _symbols[index] = getString(); + } + + // load functions table + _iP = _header.func_table; + + _numFunctions = getDWORD(); + _functions = new TFunctionPos[_numFunctions]; + for (uint32 i = 0; i < _numFunctions; i++) { + _functions[i].pos = getDWORD(); + _functions[i].name = getString(); + } + + + // load events table + _iP = _header.event_table; + + _numEvents = getDWORD(); + _events = new TEventPos[_numEvents]; + for (uint32 i = 0; i < _numEvents; i++) { + _events[i].pos = getDWORD(); + _events[i].name = getString(); + } + + + // load externals + if (_header.version >= 0x0101) { + _iP = _header.externals_table; + + _numExternals = getDWORD(); + _externals = new TExternalFunction[_numExternals]; + for (uint32 i = 0; i < _numExternals; i++) { + _externals[i].dll_name = getString(); + _externals[i].name = getString(); + _externals[i].call_type = (TCallType)getDWORD(); + _externals[i].returns = (TExternalType)getDWORD(); + _externals[i].nu_params = getDWORD(); + if (_externals[i].nu_params > 0) { + _externals[i].params = new TExternalType[_externals[i].nu_params]; + for (int j = 0; j < _externals[i].nu_params; j++) { + _externals[i].params[j] = (TExternalType)getDWORD(); + } + } + } + } + + // load method table + _iP = _header.method_table; + + _numMethods = getDWORD(); + _methods = new TMethodPos[_numMethods]; + for (uint32 i = 0; i < _numMethods; i++) { + _methods[i].pos = getDWORD(); + _methods[i].name = getString(); + } + + + _iP = OrigIP; + + return STATUS_OK; +} + + +////////////////////////////////////////////////////////////////////////// +bool CScScript::create(const char *filename, byte *buffer, uint32 size, CBScriptHolder *owner) { + cleanup(); + + _thread = false; + _methodThread = false; + + delete[] _threadEvent; + _threadEvent = NULL; + + _filename = new char[strlen(filename) + 1]; + if (_filename) strcpy(_filename, filename); + + _buffer = new byte [size]; + if (!_buffer) return STATUS_FAILED; + + memcpy(_buffer, buffer, size); + + _bufferSize = size; + + bool res = initScript(); + if (DID_FAIL(res)) return res; + + // establish global variables table + _globals = new CScValue(_gameRef); + + _owner = owner; + + return STATUS_OK; +} + + +////////////////////////////////////////////////////////////////////////// +bool CScScript::createThread(CScScript *original, uint32 initIP, const char *eventName) { + cleanup(); + + _thread = true; + _methodThread = false; + _threadEvent = new char[strlen(eventName) + 1]; + if (_threadEvent) strcpy(_threadEvent, eventName); + + // copy filename + _filename = new char[strlen(original->_filename) + 1]; + if (_filename) strcpy(_filename, original->_filename); + + // copy buffer + _buffer = new byte [original->_bufferSize]; + if (!_buffer) return STATUS_FAILED; + + memcpy(_buffer, original->_buffer, original->_bufferSize); + _bufferSize = original->_bufferSize; + + // initialize + bool res = initScript(); + if (DID_FAIL(res)) return res; + + // copy globals + _globals = original->_globals; + + // skip to the beginning of the event + _iP = initIP; + _scriptStream->seek(_iP); + + _timeSlice = original->_timeSlice; + _freezable = original->_freezable; + _owner = original->_owner; + + _engine = original->_engine; + _parentScript = original; + + return STATUS_OK; +} + + + + +////////////////////////////////////////////////////////////////////////// +bool CScScript::createMethodThread(CScScript *original, const char *methodName) { + uint32 ip = original->getMethodPos(methodName); + if (ip == 0) return STATUS_FAILED; + + cleanup(); + + _thread = true; + _methodThread = true; + _threadEvent = new char[strlen(methodName) + 1]; + if (_threadEvent) strcpy(_threadEvent, methodName); + + // copy filename + _filename = new char[strlen(original->_filename) + 1]; + if (_filename) strcpy(_filename, original->_filename); + + // copy buffer + _buffer = new byte [original->_bufferSize]; + if (!_buffer) return STATUS_FAILED; + + memcpy(_buffer, original->_buffer, original->_bufferSize); + _bufferSize = original->_bufferSize; + + // initialize + bool res = initScript(); + if (DID_FAIL(res)) return res; + + // copy globals + _globals = original->_globals; + + // skip to the beginning of the event + _iP = ip; + + _timeSlice = original->_timeSlice; + _freezable = original->_freezable; + _owner = original->_owner; + + _engine = original->_engine; + _parentScript = original; + + return STATUS_OK; +} + + +////////////////////////////////////////////////////////////////////////// +void CScScript::cleanup() { + if (_buffer) delete [] _buffer; + _buffer = NULL; + + if (_filename) delete [] _filename; + _filename = NULL; + + if (_symbols) delete [] _symbols; + _symbols = NULL; + _numSymbols = 0; + + if (_globals && !_thread) delete _globals; + _globals = NULL; + + delete _scopeStack; + _scopeStack = NULL; + + delete _callStack; + _callStack = NULL; + + delete _thisStack; + _thisStack = NULL; + + delete _stack; + _stack = NULL; + + if (_functions) delete [] _functions; + _functions = NULL; + _numFunctions = 0; + + if (_methods) delete [] _methods; + _methods = NULL; + _numMethods = 0; + + if (_events) delete [] _events; + _events = NULL; + _numEvents = 0; + + + if (_externals) { + for (uint32 i = 0; i < _numExternals; i++) { + if (_externals[i].nu_params > 0) delete [] _externals[i].params; + } + delete [] _externals; + } + _externals = NULL; + _numExternals = 0; + + delete _operand; + delete _reg1; + _operand = NULL; + _reg1 = NULL; + + delete[] _threadEvent; + _threadEvent = NULL; + + _state = SCRIPT_FINISHED; + + _waitObject = NULL; + _waitTime = 0; + _waitFrozen = false; + _waitScript = NULL; + + _parentScript = NULL; // ref only + + delete _scriptStream; +} + + +////////////////////////////////////////////////////////////////////////// +uint32 CScScript::getDWORD() { + _scriptStream->seek((int32)_iP); + uint32 ret = _scriptStream->readUint32LE(); + _iP += sizeof(uint32); +// assert(oldRet == ret); + return ret; +} + +////////////////////////////////////////////////////////////////////////// +double CScScript::getFloat() { + _scriptStream->seek((int32)_iP); + byte buffer[8]; + _scriptStream->read(buffer, 8); + +#ifdef SCUMM_BIG_ENDIAN + // TODO: For lack of a READ_LE_UINT64 + SWAP(buffer[0], buffer[7]); + SWAP(buffer[1], buffer[6]); + SWAP(buffer[2], buffer[5]); + SWAP(buffer[3], buffer[4]); +#endif + + double ret = *(double *)(buffer); + _iP += 8; // Hardcode the double-size used originally. + return ret; +} + + +////////////////////////////////////////////////////////////////////////// +char *CScScript::getString() { + char *ret = (char *)(_buffer + _iP); + while (*(char *)(_buffer + _iP) != '\0') _iP++; + _iP++; // string terminator + _scriptStream->seek(_iP); + + return ret; +} + + +////////////////////////////////////////////////////////////////////////// +bool CScScript::executeInstruction() { + bool ret = STATUS_OK; + + uint32 dw; + const char *str = NULL; + + //CScValue* op = new CScValue(_gameRef); + _operand->cleanup(); + + CScValue *op1; + CScValue *op2; + + uint32 inst = getDWORD(); + switch (inst) { + + case II_DEF_VAR: + _operand->setNULL(); + dw = getDWORD(); + if (_scopeStack->_sP < 0) { + _globals->setProp(_symbols[dw], _operand); + if (_gameRef->getDebugMgr()->_enabled) + _gameRef->getDebugMgr()->onVariableInit(WME_DBGVAR_SCRIPT, this, NULL, _globals->getProp(_symbols[dw]), _symbols[dw]); + } else { + _scopeStack->getTop()->setProp(_symbols[dw], _operand); + if (_gameRef->getDebugMgr()->_enabled) + _gameRef->getDebugMgr()->onVariableInit(WME_DBGVAR_SCOPE, this, _scopeStack->getTop(), _scopeStack->getTop()->getProp(_symbols[dw]), _symbols[dw]); + } + + break; + + case II_DEF_GLOB_VAR: + case II_DEF_CONST_VAR: { + dw = getDWORD(); + /* char *Temp = _symbols[dw]; // TODO delete */ + // only create global var if it doesn't exist + if (!_engine->_globals->propExists(_symbols[dw])) { + _operand->setNULL(); + _engine->_globals->setProp(_symbols[dw], _operand, false, inst == II_DEF_CONST_VAR); + + if (_gameRef->getDebugMgr()->_enabled) + _gameRef->getDebugMgr()->onVariableInit(WME_DBGVAR_GLOBAL, this, NULL, _engine->_globals->getProp(_symbols[dw]), _symbols[dw]); + } + break; + } + + case II_RET: + if (_scopeStack->_sP >= 0 && _callStack->_sP >= 0) { + _gameRef->getDebugMgr()->onScriptShutdownScope(this, _scopeStack->getTop()); + + _scopeStack->pop(); + _iP = (uint32)_callStack->pop()->getInt(); + + if (_scopeStack->_sP < 0) _gameRef->getDebugMgr()->onScriptChangeScope(this, NULL); + else _gameRef->getDebugMgr()->onScriptChangeScope(this, _scopeStack->getTop()); + } else { + if (_thread) { + _state = SCRIPT_THREAD_FINISHED; + } else { + if (_numEvents == 0 && _numMethods == 0) _state = SCRIPT_FINISHED; + else _state = SCRIPT_PERSISTENT; + } + } + + break; + + case II_RET_EVENT: + _state = SCRIPT_FINISHED; + break; + + + case II_CALL: + dw = getDWORD(); + + _operand->setInt(_iP); + _callStack->push(_operand); + + _iP = dw; + + break; + + case II_CALL_BY_EXP: { + // push var + // push string + str = _stack->pop()->getString(); + char *MethodName = new char[strlen(str) + 1]; + strcpy(MethodName, str); + + CScValue *var = _stack->pop(); + if (var->_type == VAL_VARIABLE_REF) var = var->_valRef; + + bool res = STATUS_FAILED; + bool TriedNative = false; + + // we are already calling this method, try native + if (_thread && _methodThread && strcmp(MethodName, _threadEvent) == 0 && var->_type == VAL_NATIVE && _owner == var->getNative()) { + TriedNative = true; + res = var->_valNative->scCallMethod(this, _stack, _thisStack, MethodName); + } + + if (DID_FAIL(res)) { + if (var->isNative() && var->getNative()->canHandleMethod(MethodName)) { + if (!_unbreakable) { + _waitScript = var->getNative()->invokeMethodThread(MethodName); + if (!_waitScript) { + _stack->correctParams(0); + runtimeError("Error invoking method '%s'.", MethodName); + _stack->pushNULL(); + } else { + _state = SCRIPT_WAITING_SCRIPT; + _waitScript->copyParameters(_stack); + } + } else { + // can call methods in unbreakable mode + _stack->correctParams(0); + runtimeError("Cannot call method '%s'. Ignored.", MethodName); + _stack->pushNULL(); + } + delete [] MethodName; + break; + } + /* + CScValue* val = var->getProp(MethodName); + if(val){ + dw = GetFuncPos(val->getString()); + if(dw==0){ + TExternalFunction* f = GetExternal(val->getString()); + if(f){ + ExternalCall(_stack, _thisStack, f); + } + else{ + // not an internal nor external, try for native function + _gameRef->ExternalCall(this, _stack, _thisStack, val->getString()); + } + } + else{ + _operand->setInt(_iP); + _callStack->Push(_operand); + _iP = dw; + } + } + */ + else { + res = STATUS_FAILED; + if (var->_type == VAL_NATIVE && !TriedNative) res = var->_valNative->scCallMethod(this, _stack, _thisStack, MethodName); + + if (DID_FAIL(res)) { + _stack->correctParams(0); + runtimeError("Call to undefined method '%s'. Ignored.", MethodName); + _stack->pushNULL(); + } + } + } + delete [] MethodName; + } + break; + + case II_EXTERNAL_CALL: { + uint32 SymbolIndex = getDWORD(); + + TExternalFunction *f = getExternal(_symbols[SymbolIndex]); + if (f) { + externalCall(_stack, _thisStack, f); + } else _gameRef->ExternalCall(this, _stack, _thisStack, _symbols[SymbolIndex]); + + break; + } + case II_SCOPE: + _operand->setNULL(); + _scopeStack->push(_operand); + + if (_scopeStack->_sP < 0) _gameRef->getDebugMgr()->onScriptChangeScope(this, NULL); + else _gameRef->getDebugMgr()->onScriptChangeScope(this, _scopeStack->getTop()); + + break; + + case II_CORRECT_STACK: + dw = getDWORD(); // params expected + _stack->correctParams(dw); + break; + + case II_CREATE_OBJECT: + _operand->setObject(); + _stack->push(_operand); + break; + + case II_POP_EMPTY: + _stack->pop(); + break; + + case II_PUSH_VAR: { + CScValue *var = getVar(_symbols[getDWORD()]); + if (false && /*var->_type==VAL_OBJECT ||*/ var->_type == VAL_NATIVE) { + _operand->setReference(var); + _stack->push(_operand); + } else _stack->push(var); + break; + } + + case II_PUSH_VAR_REF: { + CScValue *var = getVar(_symbols[getDWORD()]); + _operand->setReference(var); + _stack->push(_operand); + break; + } + + case II_POP_VAR: { + char *VarName = _symbols[getDWORD()]; + CScValue *var = getVar(VarName); + if (var) { + CScValue *val = _stack->pop(); + if (!val) { + runtimeError("Script stack corruption detected. Please report this script at WME bug reports forum."); + var->setNULL(); + } else { + if (val->getType() == VAL_VARIABLE_REF) val = val->_valRef; + if (val->_type == VAL_NATIVE) var->setValue(val); + else { + var->copy(val); + } + } + + if (_gameRef->getDebugMgr()->_enabled) + _gameRef->getDebugMgr()->onVariableChangeValue(var, val); + } + + break; + } + + case II_PUSH_VAR_THIS: + _stack->push(_thisStack->getTop()); + break; + + case II_PUSH_INT: + _stack->pushInt((int)getDWORD()); + break; + + case II_PUSH_FLOAT: + _stack->pushFloat(getFloat()); + break; + + + case II_PUSH_BOOL: + _stack->pushBool(getDWORD() != 0); + + break; + + case II_PUSH_STRING: + _stack->pushString(getString()); + break; + + case II_PUSH_NULL: + _stack->pushNULL(); + break; + + case II_PUSH_THIS_FROM_STACK: + _operand->setReference(_stack->getTop()); + _thisStack->push(_operand); + break; + + case II_PUSH_THIS: + _operand->setReference(getVar(_symbols[getDWORD()])); + _thisStack->push(_operand); + break; + + case II_POP_THIS: + _thisStack->pop(); + break; + + case II_PUSH_BY_EXP: { + str = _stack->pop()->getString(); + CScValue *val = _stack->pop()->getProp(str); + if (val) _stack->push(val); + else _stack->pushNULL(); + + break; + } + + case II_POP_BY_EXP: { + str = _stack->pop()->getString(); + CScValue *var = _stack->pop(); + CScValue *val = _stack->pop(); + + if (val == NULL) { + runtimeError("Script stack corruption detected. Please report this script at WME bug reports forum."); + var->setNULL(); + } else var->setProp(str, val); + + if (_gameRef->getDebugMgr()->_enabled) + _gameRef->getDebugMgr()->onVariableChangeValue(var, NULL); + + break; + } + + case II_PUSH_REG1: + _stack->push(_reg1); + break; + + case II_POP_REG1: + _reg1->copy(_stack->pop()); + break; + + case II_JMP: + _iP = getDWORD(); + break; + + case II_JMP_FALSE: { + dw = getDWORD(); + //if(!_stack->pop()->getBool()) _iP = dw; + CScValue *val = _stack->pop(); + if (!val) { + runtimeError("Script corruption detected. Did you use '=' instead of '==' for comparison?"); + } else { + if (!val->getBool()) _iP = dw; + } + break; + } + + case II_ADD: + op2 = _stack->pop(); + op1 = _stack->pop(); + + if (op1->isNULL() || op2->isNULL()) + _operand->setNULL(); + else if (op1->getType() == VAL_STRING || op2->getType() == VAL_STRING) { + char *tempStr = new char [strlen(op1->getString()) + strlen(op2->getString()) + 1]; + strcpy(tempStr, op1->getString()); + strcat(tempStr, op2->getString()); + _operand->setString(tempStr); + delete [] tempStr; + } else if (op1->getType() == VAL_INT && op2->getType() == VAL_INT) + _operand->setInt(op1->getInt() + op2->getInt()); + else _operand->setFloat(op1->getFloat() + op2->getFloat()); + + _stack->push(_operand); + + break; + + case II_SUB: + op2 = _stack->pop(); + op1 = _stack->pop(); + + if (op1->isNULL() || op2->isNULL()) + _operand->setNULL(); + else if (op1->getType() == VAL_INT && op2->getType() == VAL_INT) + _operand->setInt(op1->getInt() - op2->getInt()); + else _operand->setFloat(op1->getFloat() - op2->getFloat()); + + _stack->push(_operand); + + break; + + case II_MUL: + op2 = _stack->pop(); + op1 = _stack->pop(); + + if (op1->isNULL() || op2->isNULL()) _operand->setNULL(); + else if (op1->getType() == VAL_INT && op2->getType() == VAL_INT) + _operand->setInt(op1->getInt() * op2->getInt()); + else _operand->setFloat(op1->getFloat() * op2->getFloat()); + + _stack->push(_operand); + + break; + + case II_DIV: + op2 = _stack->pop(); + op1 = _stack->pop(); + + if (op2->getFloat() == 0.0f) + runtimeError("Division by zero."); + + if (op1->isNULL() || op2->isNULL() || op2->getFloat() == 0.0f) _operand->setNULL(); + else _operand->setFloat(op1->getFloat() / op2->getFloat()); + + _stack->push(_operand); + + break; + + case II_MODULO: + op2 = _stack->pop(); + op1 = _stack->pop(); + + if (op2->getInt() == 0) + runtimeError("Division by zero."); + + if (op1->isNULL() || op2->isNULL() || op2->getInt() == 0) + _operand->setNULL(); + else _operand->setInt(op1->getInt() % op2->getInt()); + + _stack->push(_operand); + + break; + + case II_NOT: + op1 = _stack->pop(); + //if(op1->isNULL()) _operand->setNULL(); + if (op1->isNULL()) _operand->setBool(true); + else _operand->setBool(!op1->getBool()); + _stack->push(_operand); + + break; + + case II_AND: + op2 = _stack->pop(); + op1 = _stack->pop(); + if (op1 == NULL || op2 == NULL) { + runtimeError("Script corruption detected. Did you use '=' instead of '==' for comparison?"); + _operand->setBool(false); + } else { + _operand->setBool(op1->getBool() && op2->getBool()); + } + _stack->push(_operand); + break; + + case II_OR: + op2 = _stack->pop(); + op1 = _stack->pop(); + if (op1 == NULL || op2 == NULL) { + runtimeError("Script corruption detected. Did you use '=' instead of '==' for comparison?"); + _operand->setBool(false); + } else { + _operand->setBool(op1->getBool() || op2->getBool()); + } + _stack->push(_operand); + break; + + case II_CMP_EQ: + op2 = _stack->pop(); + op1 = _stack->pop(); + + /* + if((op1->isNULL() && !op2->isNULL()) || (!op1->isNULL() && op2->isNULL())) _operand->setBool(false); + else if(op1->isNative() && op2->isNative()){ + _operand->setBool(op1->getNative() == op2->getNative()); + } + else if(op1->getType()==VAL_STRING || op2->getType()==VAL_STRING){ + _operand->setBool(scumm_stricmp(op1->getString(), op2->getString())==0); + } + else if(op1->getType()==VAL_FLOAT && op2->getType()==VAL_FLOAT){ + _operand->setBool(op1->getFloat() == op2->getFloat()); + } + else{ + _operand->setBool(op1->getInt() == op2->getInt()); + } + */ + + _operand->setBool(CScValue::compare(op1, op2) == 0); + _stack->push(_operand); + break; + + case II_CMP_NE: + op2 = _stack->pop(); + op1 = _stack->pop(); + + /* + if((op1->isNULL() && !op2->isNULL()) || (!op1->isNULL() && op2->isNULL())) _operand->setBool(true); + else if(op1->isNative() && op2->isNative()){ + _operand->setBool(op1->getNative() != op2->getNative()); + } + else if(op1->getType()==VAL_STRING || op2->getType()==VAL_STRING){ + _operand->setBool(scumm_stricmp(op1->getString(), op2->getString())!=0); + } + else if(op1->getType()==VAL_FLOAT && op2->getType()==VAL_FLOAT){ + _operand->setBool(op1->getFloat() != op2->getFloat()); + } + else{ + _operand->setBool(op1->getInt() != op2->getInt()); + } + */ + + _operand->setBool(CScValue::compare(op1, op2) != 0); + _stack->push(_operand); + break; + + case II_CMP_L: + op2 = _stack->pop(); + op1 = _stack->pop(); + + /* + if(op1->getType()==VAL_FLOAT && op2->getType()==VAL_FLOAT){ + _operand->setBool(op1->getFloat() < op2->getFloat()); + } + else _operand->setBool(op1->getInt() < op2->getInt()); + */ + + _operand->setBool(CScValue::compare(op1, op2) < 0); + _stack->push(_operand); + break; + + case II_CMP_G: + op2 = _stack->pop(); + op1 = _stack->pop(); + + /* + if(op1->getType()==VAL_FLOAT && op2->getType()==VAL_FLOAT){ + _operand->setBool(op1->getFloat() > op2->getFloat()); + } + else _operand->setBool(op1->getInt() > op2->getInt()); + */ + + _operand->setBool(CScValue::compare(op1, op2) > 0); + _stack->push(_operand); + break; + + case II_CMP_LE: + op2 = _stack->pop(); + op1 = _stack->pop(); + + /* + if(op1->getType()==VAL_FLOAT && op2->getType()==VAL_FLOAT){ + _operand->setBool(op1->getFloat() <= op2->getFloat()); + } + else _operand->setBool(op1->getInt() <= op2->getInt()); + */ + + _operand->setBool(CScValue::compare(op1, op2) <= 0); + _stack->push(_operand); + break; + + case II_CMP_GE: + op2 = _stack->pop(); + op1 = _stack->pop(); + + /* + if(op1->getType()==VAL_FLOAT && op2->getType()==VAL_FLOAT){ + _operand->setBool(op1->getFloat() >= op2->getFloat()); + } + else _operand->setBool(op1->getInt() >= op2->getInt()); + */ + + _operand->setBool(CScValue::compare(op1, op2) >= 0); + _stack->push(_operand); + break; + + case II_CMP_STRICT_EQ: + op2 = _stack->pop(); + op1 = _stack->pop(); + + //_operand->setBool(op1->getType()==op2->getType() && op1->getFloat()==op2->getFloat()); + _operand->setBool(CScValue::compareStrict(op1, op2) == 0); + _stack->push(_operand); + + break; + + case II_CMP_STRICT_NE: + op2 = _stack->pop(); + op1 = _stack->pop(); + + //_operand->setBool(op1->getType()!=op2->getType() || op1->getFloat()!=op2->getFloat()); + _operand->setBool(CScValue::compareStrict(op1, op2) != 0); + _stack->push(_operand); + break; + + case II_DBG_LINE: { + int newLine = getDWORD(); + if (newLine != _currentLine) { + _currentLine = newLine; + if (_gameRef->getDebugMgr()->_enabled) { + _gameRef->getDebugMgr()->onScriptChangeLine(this, _currentLine); + for (int i = 0; i < _breakpoints.getSize(); i++) { + if (_breakpoints[i] == _currentLine) { + _gameRef->getDebugMgr()->onScriptHitBreakpoint(this); + sleep(0); + break; + } + } + if (_tracingMode) { + _gameRef->getDebugMgr()->onScriptHitBreakpoint(this); + sleep(0); + break; + } + } + } + break; + + } + default: + _gameRef->LOG(0, "Fatal: Invalid instruction %d ('%s', line %d, IP:0x%x)\n", inst, _filename, _currentLine, _iP - sizeof(uint32)); + _state = SCRIPT_FINISHED; + ret = STATUS_FAILED; + } // switch(instruction) + + //delete op; + + return ret; +} + + +////////////////////////////////////////////////////////////////////////// +uint32 CScScript::getFuncPos(const char *name) { + for (uint32 i = 0; i < _numFunctions; i++) { + if (strcmp(name, _functions[i].name) == 0) + return _functions[i].pos; + } + return 0; +} + + +////////////////////////////////////////////////////////////////////////// +uint32 CScScript::getMethodPos(const char *name) { + for (uint32 i = 0; i < _numMethods; i++) { + if (strcmp(name, _methods[i].name) == 0) + return _methods[i].pos; + } + return 0; +} + + +////////////////////////////////////////////////////////////////////////// +CScValue *CScScript::getVar(char *name) { + CScValue *ret = NULL; + + // scope locals + if (_scopeStack->_sP >= 0) { + if (_scopeStack->getTop()->propExists(name)) + ret = _scopeStack->getTop()->getProp(name); + } + + // script globals + if (ret == NULL) { + if (_globals->propExists(name)) + ret = _globals->getProp(name); + } + + // engine globals + if (ret == NULL) { + if (_engine->_globals->propExists(name)) + ret = _engine->_globals->getProp(name); + } + + if (ret == NULL) { + //RuntimeError("Variable '%s' is inaccessible in the current block. Consider changing the script.", name); + _gameRef->LOG(0, "Warning: variable '%s' is inaccessible in the current block. Consider changing the script (script:%s, line:%d)", name, _filename, _currentLine); + CScValue *val = new CScValue(_gameRef); + CScValue *scope = _scopeStack->getTop(); + if (scope) { + scope->setProp(name, val); + ret = _scopeStack->getTop()->getProp(name); + } else { + _globals->setProp(name, val); + ret = _globals->getProp(name); + } + delete val; + } + + return ret; +} + + +////////////////////////////////////////////////////////////////////////// +bool CScScript::waitFor(CBObject *object) { + if (_unbreakable) { + runtimeError("Script cannot be interrupted."); + return STATUS_OK; + } + + _state = SCRIPT_WAITING; + _waitObject = object; + return STATUS_OK; +} + + +////////////////////////////////////////////////////////////////////////// +bool CScScript::waitForExclusive(CBObject *object) { + _engine->resetObject(object); + return waitFor(object); +} + + +////////////////////////////////////////////////////////////////////////// +bool CScScript::sleep(uint32 duration) { + if (_unbreakable) { + runtimeError("Script cannot be interrupted."); + return STATUS_OK; + } + + _state = SCRIPT_SLEEPING; + if (_gameRef->_state == GAME_FROZEN) { + _waitTime = CBPlatform::getTime() + duration; + _waitFrozen = true; + } else { + _waitTime = _gameRef->_timer + duration; + _waitFrozen = false; + } + return STATUS_OK; +} + + +////////////////////////////////////////////////////////////////////////// +bool CScScript::finish(bool includingThreads) { + if (_state != SCRIPT_FINISHED && includingThreads) { + _state = SCRIPT_FINISHED; + finishThreads(); + } else _state = SCRIPT_FINISHED; + + + return STATUS_OK; +} + + +////////////////////////////////////////////////////////////////////////// +bool CScScript::run() { + _state = SCRIPT_RUNNING; + return STATUS_OK; +} + + +////////////////////////////////////////////////////////////////////// +void CScScript::runtimeError(const char *fmt, ...) { + char buff[256]; + va_list va; + + va_start(va, fmt); + vsprintf(buff, fmt, va); + va_end(va); + + _gameRef->LOG(0, "Runtime error. Script '%s', line %d", _filename, _currentLine); + _gameRef->LOG(0, " %s", buff); + + if (!_gameRef->_suppressScriptErrors) + _gameRef->quickMessage("Script runtime error. View log for details."); +} + + +////////////////////////////////////////////////////////////////////////// +bool CScScript::persist(CBPersistMgr *persistMgr) { + + persistMgr->transfer(TMEMBER(_gameRef)); + + // buffer + if (persistMgr->_saving) { + if (_state != SCRIPT_PERSISTENT && _state != SCRIPT_FINISHED && _state != SCRIPT_THREAD_FINISHED) { + persistMgr->transfer(TMEMBER(_bufferSize)); + persistMgr->putBytes(_buffer, _bufferSize); + } else { + // don't save idle/finished scripts + int bufferSize = 0; + persistMgr->transfer(TMEMBER(bufferSize)); + } + } else { + persistMgr->transfer(TMEMBER(_bufferSize)); + if (_bufferSize > 0) { + _buffer = new byte[_bufferSize]; + persistMgr->getBytes(_buffer, _bufferSize); + _scriptStream = new Common::MemoryReadStream(_buffer, _bufferSize); + initTables(); + } else { + _buffer = NULL; + _scriptStream = NULL; + } + } + + persistMgr->transfer(TMEMBER(_callStack)); + persistMgr->transfer(TMEMBER(_currentLine)); + persistMgr->transfer(TMEMBER(_engine)); + persistMgr->transfer(TMEMBER(_filename)); + persistMgr->transfer(TMEMBER(_freezable)); + persistMgr->transfer(TMEMBER(_globals)); + persistMgr->transfer(TMEMBER(_iP)); + persistMgr->transfer(TMEMBER(_scopeStack)); + persistMgr->transfer(TMEMBER(_stack)); + persistMgr->transfer(TMEMBER_INT(_state)); + persistMgr->transfer(TMEMBER(_operand)); + persistMgr->transfer(TMEMBER_INT(_origState)); + persistMgr->transfer(TMEMBER(_owner)); + persistMgr->transfer(TMEMBER(_reg1)); + persistMgr->transfer(TMEMBER(_thread)); + persistMgr->transfer(TMEMBER(_threadEvent)); + persistMgr->transfer(TMEMBER(_thisStack)); + persistMgr->transfer(TMEMBER(_timeSlice)); + persistMgr->transfer(TMEMBER(_waitObject)); + persistMgr->transfer(TMEMBER(_waitScript)); + persistMgr->transfer(TMEMBER(_waitTime)); + persistMgr->transfer(TMEMBER(_waitFrozen)); + + persistMgr->transfer(TMEMBER(_methodThread)); + persistMgr->transfer(TMEMBER(_methodThread)); + persistMgr->transfer(TMEMBER(_unbreakable)); + persistMgr->transfer(TMEMBER(_parentScript)); + + if (!persistMgr->_saving) _tracingMode = false; + + return STATUS_OK; +} + + +////////////////////////////////////////////////////////////////////////// +CScScript *CScScript::invokeEventHandler(const char *eventName, bool unbreakable) { + //if(_state!=SCRIPT_PERSISTENT) return NULL; + + uint32 pos = getEventPos(eventName); + if (!pos) return NULL; + + CScScript *thread = new CScScript(_gameRef, _engine); + if (thread) { + bool ret = thread->createThread(this, pos, eventName); + if (DID_SUCCEED(ret)) { + thread->_unbreakable = unbreakable; + _engine->_scripts.add(thread); + _gameRef->getDebugMgr()->onScriptEventThreadInit(thread, this, eventName); + return thread; + } else { + delete thread; + return NULL; + } + } else return NULL; + +} + + +////////////////////////////////////////////////////////////////////////// +uint32 CScScript::getEventPos(const char *name) { + for (int i = _numEvents - 1; i >= 0; i--) { + if (scumm_stricmp(name, _events[i].name) == 0) return _events[i].pos; + } + return 0; +} + + +////////////////////////////////////////////////////////////////////////// +bool CScScript::canHandleEvent(const char *eventName) { + return getEventPos(eventName) != 0; +} + + +////////////////////////////////////////////////////////////////////////// +bool CScScript::canHandleMethod(const char *methodName) { + return getMethodPos(methodName) != 0; +} + + +////////////////////////////////////////////////////////////////////////// +bool CScScript::pause() { + if (_state == SCRIPT_PAUSED) { + _gameRef->LOG(0, "Attempting to pause a paused script ('%s', line %d)", _filename, _currentLine); + return STATUS_FAILED; + } + + if (!_freezable || _state == SCRIPT_PERSISTENT) return STATUS_OK; + + _origState = _state; + _state = SCRIPT_PAUSED; + + return STATUS_OK; +} + + +////////////////////////////////////////////////////////////////////////// +bool CScScript::resume() { + if (_state != SCRIPT_PAUSED) return STATUS_OK; + + _state = _origState; + return STATUS_OK; +} + + +////////////////////////////////////////////////////////////////////////// +CScScript::TExternalFunction *CScScript::getExternal(char *name) { + for (uint32 i = 0; i < _numExternals; i++) { + if (strcmp(name, _externals[i].name) == 0) + return &_externals[i]; + } + return NULL; +} + + +////////////////////////////////////////////////////////////////////////// +bool CScScript::externalCall(CScStack *stack, CScStack *thisStack, CScScript::TExternalFunction *function) { + + _gameRef->LOG(0, "External functions are not supported on this platform."); + stack->correctParams(0); + stack->pushNULL(); + return STATUS_FAILED; +} + + +////////////////////////////////////////////////////////////////////////// +bool CScScript::copyParameters(CScStack *stack) { + int i; + int NumParams = stack->pop()->getInt(); + for (i = NumParams - 1; i >= 0; i--) { + _stack->push(stack->getAt(i)); + } + _stack->pushInt(NumParams); + + for (i = 0; i < NumParams; i++) stack->pop(); + + return STATUS_OK; +} + + +////////////////////////////////////////////////////////////////////////// +bool CScScript::finishThreads() { + for (int i = 0; i < _engine->_scripts.getSize(); i++) { + CScScript *scr = _engine->_scripts[i]; + if (scr->_thread && scr->_state != SCRIPT_FINISHED && scr->_owner == _owner && scumm_stricmp(scr->_filename, _filename) == 0) + scr->finish(true); + } + return STATUS_OK; +} + + +////////////////////////////////////////////////////////////////////////// +// IWmeDebugScript interface implementation +int CScScript::dbgGetLine() { + return _currentLine; +} + +////////////////////////////////////////////////////////////////////////// +const char *CScScript::dbgGetFilename() { + return _filename; +} + + +////////////////////////////////////////////////////////////////////////// +bool CScScript::dbgSendScript(IWmeDebugClient *client) { + if (_methodThread) client->onScriptMethodThreadInit(this, _parentScript, _threadEvent); + else if (_thread) client->onScriptEventThreadInit(this, _parentScript, _threadEvent); + else client->onScriptInit(this); + + return dbgSendVariables(client); + return STATUS_OK; +} + +////////////////////////////////////////////////////////////////////////// +bool CScScript::dbgSendVariables(IWmeDebugClient *client) { + // send script globals + _globals->dbgSendVariables(client, WME_DBGVAR_SCRIPT, this, 0); + + // send scope variables + if (_scopeStack->_sP >= 0) { + for (int i = 0; i <= _scopeStack->_sP; i++) { + // CScValue *Scope = _scopeStack->GetAt(i); + //Scope->DbgSendVariables(Client, WME_DBGVAR_SCOPE, this, (unsigned int)Scope); + } + } + return STATUS_OK; +} + + +////////////////////////////////////////////////////////////////////////// +TScriptState CScScript::dbgGetState() { + return _state; +} + +////////////////////////////////////////////////////////////////////////// +int CScScript::dbgGetNumBreakpoints() { + return _breakpoints.getSize(); +} + +////////////////////////////////////////////////////////////////////////// +int CScScript::dbgGetBreakpoint(int index) { + if (index >= 0 && index < _breakpoints.getSize()) return _breakpoints[index]; + else return -1; +} + +////////////////////////////////////////////////////////////////////////// +bool CScScript::dbgSetTracingMode(bool isTracing) { + _tracingMode = isTracing; + return true; +} + +////////////////////////////////////////////////////////////////////////// +bool CScScript::dbgGetTracingMode() { + return _tracingMode; +} + + +////////////////////////////////////////////////////////////////////////// +void CScScript::afterLoad() { + if (_buffer == NULL) { + byte *buffer = _engine->getCompiledScript(_filename, &_bufferSize); + if (!buffer) { + _gameRef->LOG(0, "Error reinitializing script '%s' after load. Script will be terminated.", _filename); + _state = SCRIPT_ERROR; + return; + } + + _buffer = new byte [_bufferSize]; + memcpy(_buffer, buffer, _bufferSize); + + delete _scriptStream; + _scriptStream = new Common::MemoryReadStream(_buffer, _bufferSize); + + initTables(); + } +} + +} // end of namespace WinterMute diff --git a/engines/wintermute/base/scriptables/script.h b/engines/wintermute/base/scriptables/script.h new file mode 100644 index 0000000000..899e1f3098 --- /dev/null +++ b/engines/wintermute/base/scriptables/script.h @@ -0,0 +1,183 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +/* + * This file is based on WME Lite. + * http://dead-code.org/redir.php?target=wmelite + * Copyright (c) 2011 Jan Nedoma + */ + +#ifndef WINTERMUTE_SCSCRIPT_H +#define WINTERMUTE_SCSCRIPT_H + + +#include "engines/wintermute/base/base.h" +#include "engines/wintermute/dcscript.h" // Added by ClassView +#include "engines/wintermute/coll_templ.h" + +#include "engines/wintermute/wme_debugger.h" + +namespace WinterMute { +class CBScriptHolder; +class CBObject; +class CScEngine; +class CScStack; +class CScScript : public CBBase, public IWmeDebugScript { +public: + bool dbgSendScript(IWmeDebugClient *client); + bool dbgSendVariables(IWmeDebugClient *client); + + CBArray _breakpoints; + bool _tracingMode; + + CScScript *_parentScript; + bool _unbreakable; + bool finishThreads(); + bool copyParameters(CScStack *stack); + + void afterLoad(); + + CScValue *_operand; + CScValue *_reg1; + bool _freezable; + bool resume(); + bool pause(); + bool canHandleEvent(const char *eventName); + bool canHandleMethod(const char *methodName); + bool createThread(CScScript *original, uint32 initIP, const char *eventName); + bool createMethodThread(CScScript *original, const char *methodName); + CScScript *invokeEventHandler(const char *eventName, bool unbreakable = false); + uint32 _timeSlice; + DECLARE_PERSISTENT(CScScript, CBBase) + void runtimeError(const char *fmt, ...); + bool run(); + bool finish(bool includingThreads = false); + bool sleep(uint32 duration); + bool waitForExclusive(CBObject *object); + bool waitFor(CBObject *object); + uint32 _waitTime; + bool _waitFrozen; + CBObject *_waitObject; + CScScript *_waitScript; + TScriptState _state; + TScriptState _origState; + CScValue *getVar(char *name); + uint32 getFuncPos(const char *name); + uint32 getEventPos(const char *name); + uint32 getMethodPos(const char *name); + typedef struct { + uint32 magic; + uint32 version; + uint32 code_start; + uint32 func_table; + uint32 symbol_table; + uint32 event_table; + uint32 externals_table; + uint32 method_table; + } TScriptHeader; + + TScriptHeader _header; + + typedef struct { + char *name; + uint32 pos; + } TFunctionPos; + + typedef struct { + char *name; + uint32 pos; + } TMethodPos; + + typedef struct { + char *name; + uint32 pos; + } TEventPos; + + typedef struct { + char *name; + char *dll_name; + TCallType call_type; + TExternalType returns; + int nu_params; + TExternalType *params; + } TExternalFunction; + + + CScStack *_callStack; + CScStack *_thisStack; + CScStack *_scopeStack; + CScStack *_stack; + CScValue *_globals; + CScEngine *_engine; + int _currentLine; + bool executeInstruction(); + char *getString(); + uint32 getDWORD(); + double getFloat(); + void cleanup(); + bool create(const char *filename, byte *buffer, uint32 size, CBScriptHolder *owner); + uint32 _iP; +private: + void readHeader(); + uint32 _bufferSize; + byte *_buffer; +public: + Common::SeekableReadStream *_scriptStream; + CScScript(CBGame *inGame, CScEngine *Engine); + virtual ~CScScript(); + char *_filename; + char **_symbols; + uint32 _numSymbols; + TFunctionPos *_functions; + TMethodPos *_methods; + TEventPos *_events; + uint32 _numExternals; + TExternalFunction *_externals; + uint32 _numFunctions; + uint32 _numMethods; + uint32 _numEvents; + bool _thread; + bool _methodThread; + char *_threadEvent; + CBScriptHolder *_owner; + CScScript::TExternalFunction *getExternal(char *name); + bool externalCall(CScStack *stack, CScStack *thisStack, CScScript::TExternalFunction *function); +private: + bool initScript(); + bool initTables(); + + +// IWmeDebugScript interface implementation +public: + virtual int dbgGetLine(); + virtual const char *dbgGetFilename(); + virtual TScriptState dbgGetState(); + virtual int dbgGetNumBreakpoints(); + virtual int dbgGetBreakpoint(int Index); + + virtual bool dbgSetTracingMode(bool IsTracing); + virtual bool dbgGetTracingMode(); +}; + +} // end of namespace WinterMute + +#endif diff --git a/engines/wintermute/base/scriptables/script_engine.cpp b/engines/wintermute/base/scriptables/script_engine.cpp new file mode 100644 index 0000000000..387093ac4a --- /dev/null +++ b/engines/wintermute/base/scriptables/script_engine.cpp @@ -0,0 +1,712 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +/* + * This file is based on WME Lite. + * http://dead-code.org/redir.php?target=wmelite + * Copyright (c) 2011 Jan Nedoma + */ + +#include "engines/wintermute/dcgf.h" +#include "engines/wintermute/base/scriptables/script_engine.h" +#include "engines/wintermute/utils/string_util.h" +#include "engines/wintermute/base/scriptables/script_value.h" +#include "engines/wintermute/base/scriptables/script.h" +#include "engines/wintermute/base/scriptables/script_stack.h" +#include "engines/wintermute/base/scriptables/script_ext_math.h" +#include "engines/wintermute/base/base_registry.h" +#include "engines/wintermute/base/base_game.h" +#include "engines/wintermute/base/sound/base_sound.h" +#include "engines/wintermute/base/base_file_manager.h" + + +namespace WinterMute { + +IMPLEMENT_PERSISTENT(CScEngine, true) + +#define COMPILER_DLL "dcscomp.dll" +////////////////////////////////////////////////////////////////////////// +CScEngine::CScEngine(CBGame *inGame): CBBase(inGame) { + _gameRef->LOG(0, "Initializing scripting engine..."); + + if (_compilerAvailable) _gameRef->LOG(0, " Script compiler bound successfuly"); + else _gameRef->LOG(0, " Script compiler is NOT available"); + + _globals = new CScValue(_gameRef); + + + // register 'Game' as global variable + if (!_globals->propExists("Game")) { + CScValue val(_gameRef); + val.setNative(_gameRef, true); + _globals->setProp("Game", &val); + } + + // register 'Math' as global variable + if (!_globals->propExists("Math")) { + CScValue val(_gameRef); + val.setNative(_gameRef->_mathClass, true); + _globals->setProp("Math", &val); + } + + // prepare script cache + for (int i = 0; i < MAX_CACHED_SCRIPTS; i++) _cachedScripts[i] = NULL; + + _currentScript = NULL; + + _isProfiling = false; + _profilingStartTime = 0; + + //EnableProfiling(); +} + + +////////////////////////////////////////////////////////////////////////// +CScEngine::~CScEngine() { + _gameRef->LOG(0, "Shutting down scripting engine"); + saveBreakpoints(); + + disableProfiling(); + + cleanup(); + + for (int i = 0; i < _breakpoints.getSize(); i++) { + delete _breakpoints[i]; + _breakpoints[i] = NULL; + } + _breakpoints.removeAll(); +} + + +////////////////////////////////////////////////////////////////////////// +bool CScEngine::cleanup() { + for (int i = 0; i < _scripts.getSize(); i++) { + if (!_scripts[i]->_thread && _scripts[i]->_owner) _scripts[i]->_owner->removeScript(_scripts[i]); + delete _scripts[i]; + _scripts.removeAt(i); + i--; + } + + _scripts.removeAll(); + + delete _globals; + _globals = NULL; + + emptyScriptCache(); + + _currentScript = NULL; // ref only + + return STATUS_OK; +} + + +////////////////////////////////////////////////////////////////////////// +byte *CScEngine::loadFile(void *data, char *filename, uint32 *size) { + CBGame *gameRef = (CBGame *)data; + return gameRef->_fileManager->readWholeFile(filename, size); +} + + +////////////////////////////////////////////////////////////////////////// +void CScEngine::closeFile(void *data, byte *buffer) { + delete [] buffer; +} + + +////////////////////////////////////////////////////////////////////////// +void CScEngine::parseElement(void *data, int line, int type, void *elementData) { +} + + +////////////////////////////////////////////////////////////////////////// +CScScript *CScEngine::runScript(const char *filename, CBScriptHolder *owner) { + byte *compBuffer; + uint32 compSize; + + // get script from cache + compBuffer = getCompiledScript(filename, &compSize); + if (!compBuffer) return NULL; + + // add new script + CScScript *script = new CScScript(_gameRef, this); + bool ret = script->create(filename, compBuffer, compSize, owner); + if (DID_FAIL(ret)) { + _gameRef->LOG(ret, "Error running script '%s'...", filename); + delete script; + return NULL; + } else { + // publish the "self" pseudo-variable + CScValue val(_gameRef); + if (owner)val.setNative(owner, true); + else val.setNULL(); + + script->_globals->setProp("self", &val); + script->_globals->setProp("this", &val); + + _scripts.add(script); + _gameRef->getDebugMgr()->onScriptInit(script); + + return script; + } +} + + +////////////////////////////////////////////////////////////////////////// +byte *CScEngine::getCompiledScript(const char *filename, uint32 *outSize, bool ignoreCache) { + // is script in cache? + if (!ignoreCache) { + for (int i = 0; i < MAX_CACHED_SCRIPTS; i++) { + if (_cachedScripts[i] && scumm_stricmp(_cachedScripts[i]->_filename.c_str(), filename) == 0) { + _cachedScripts[i]->_timestamp = CBPlatform::getTime(); + *outSize = _cachedScripts[i]->_size; + return _cachedScripts[i]->_buffer; + } + } + } + + // nope, load it + byte *compBuffer; + uint32 compSize; + + uint32 size; + + byte *buffer = _gameRef->_fileManager->readWholeFile(filename, &size); + if (!buffer) { + _gameRef->LOG(0, "CScEngine::GetCompiledScript - error opening script '%s'", filename); + return NULL; + } + + // needs to be compiled? + if (FROM_LE_32(*(uint32 *)buffer) == SCRIPT_MAGIC) { + compBuffer = buffer; + compSize = size; + } else { + if (!_compilerAvailable) { + _gameRef->LOG(0, "CScEngine::GetCompiledScript - script '%s' needs to be compiled but compiler is not available", filename); + delete [] buffer; + return NULL; + } + // This code will never be called, since _compilerAvailable is const false. + // It's only here in the event someone would want to reinclude the compiler. + error("Script needs compilation, ScummVM does not contain a WME compiler"); + } + + byte *ret = NULL; + + // add script to cache + CScCachedScript *cachedScript = new CScCachedScript(filename, compBuffer, compSize); + if (cachedScript) { + int index = 0; + uint32 MinTime = CBPlatform::getTime(); + for (int i = 0; i < MAX_CACHED_SCRIPTS; i++) { + if (_cachedScripts[i] == NULL) { + index = i; + break; + } else if (_cachedScripts[i]->_timestamp <= MinTime) { + MinTime = _cachedScripts[i]->_timestamp; + index = i; + } + } + + if (_cachedScripts[index] != NULL) delete _cachedScripts[index]; + _cachedScripts[index] = cachedScript; + + ret = cachedScript->_buffer; + *outSize = cachedScript->_size; + } + + + // cleanup + delete [] buffer; + + return ret; +} + + + +////////////////////////////////////////////////////////////////////////// +bool CScEngine::tick() { + if (_scripts.getSize() == 0) + return STATUS_OK; + + + // resolve waiting scripts + for (int i = 0; i < _scripts.getSize(); i++) { + + switch (_scripts[i]->_state) { + case SCRIPT_WAITING: { + /* + bool obj_found=false; + for(int j=0; j<_gameRef->_regObjects.getSize(); j++) + { + if(_gameRef->_regObjects[j] == _scripts[i]->_waitObject) + { + if(_gameRef->_regObjects[j]->IsReady()) _scripts[i]->Run(); + obj_found = true; + break; + } + } + if(!obj_found) _scripts[i]->finish(); // _waitObject no longer exists + */ + if (_gameRef->validObject(_scripts[i]->_waitObject)) { + if (_scripts[i]->_waitObject->isReady()) _scripts[i]->run(); + } else _scripts[i]->finish(); + break; + } + + case SCRIPT_SLEEPING: { + if (_scripts[i]->_waitFrozen) { + if (_scripts[i]->_waitTime <= CBPlatform::getTime()) _scripts[i]->run(); + } else { + if (_scripts[i]->_waitTime <= _gameRef->_timer) _scripts[i]->run(); + } + break; + } + + case SCRIPT_WAITING_SCRIPT: { + if (!isValidScript(_scripts[i]->_waitScript) || _scripts[i]->_waitScript->_state == SCRIPT_ERROR) { + // fake return value + _scripts[i]->_stack->pushNULL(); + _scripts[i]->_waitScript = NULL; + _scripts[i]->run(); + } else { + if (_scripts[i]->_waitScript->_state == SCRIPT_THREAD_FINISHED) { + // copy return value + _scripts[i]->_stack->push(_scripts[i]->_waitScript->_stack->pop()); + _scripts[i]->run(); + _scripts[i]->_waitScript->finish(); + _scripts[i]->_waitScript = NULL; + } + } + break; + } + default: + //warning("CScEngine::Tick - Unhandled enum"); + break; + } // switch + } // for each script + + + // execute scripts + for (int i = 0; i < _scripts.getSize(); i++) { + + // skip paused scripts + if (_scripts[i]->_state == SCRIPT_PAUSED) continue; + + // time sliced script + if (_scripts[i]->_timeSlice > 0) { + uint32 StartTime = CBPlatform::getTime(); + while (_scripts[i]->_state == SCRIPT_RUNNING && CBPlatform::getTime() - StartTime < _scripts[i]->_timeSlice) { + _currentScript = _scripts[i]; + _scripts[i]->executeInstruction(); + } + if (_isProfiling && _scripts[i]->_filename) addScriptTime(_scripts[i]->_filename, CBPlatform::getTime() - StartTime); + } + + // normal script + else { + uint32 startTime = 0; + bool isProfiling = _isProfiling; + if (isProfiling) startTime = CBPlatform::getTime(); + + while (_scripts[i]->_state == SCRIPT_RUNNING) { + _currentScript = _scripts[i]; + _scripts[i]->executeInstruction(); + } + if (isProfiling && _scripts[i]->_filename) addScriptTime(_scripts[i]->_filename, CBPlatform::getTime() - startTime); + } + _currentScript = NULL; + } + + removeFinishedScripts(); + + return STATUS_OK; +} + + +////////////////////////////////////////////////////////////////////////// +bool CScEngine::tickUnbreakable() { + // execute unbreakable scripts + for (int i = 0; i < _scripts.getSize(); i++) { + if (!_scripts[i]->_unbreakable) continue; + + while (_scripts[i]->_state == SCRIPT_RUNNING) { + _currentScript = _scripts[i]; + _scripts[i]->executeInstruction(); + } + _scripts[i]->finish(); + _currentScript = NULL; + } + removeFinishedScripts(); + + return STATUS_OK; +} + + +////////////////////////////////////////////////////////////////////////// +bool CScEngine::removeFinishedScripts() { + // remove finished scripts + for (int i = 0; i < _scripts.getSize(); i++) { + if (_scripts[i]->_state == SCRIPT_FINISHED || _scripts[i]->_state == SCRIPT_ERROR) { + if (!_scripts[i]->_thread && _scripts[i]->_owner) _scripts[i]->_owner->removeScript(_scripts[i]); + _gameRef->getDebugMgr()->onScriptShutdown(_scripts[i]); + delete _scripts[i]; + _scripts.removeAt(i); + i--; + } + } + return STATUS_OK; +} + + +////////////////////////////////////////////////////////////////////////// +int CScEngine::getNumScripts(int *running, int *waiting, int *persistent) { + int numRunning = 0, numWaiting = 0, numPersistent = 0, numTotal = 0; + + for (int i = 0; i < _scripts.getSize(); i++) { + if (_scripts[i]->_state == SCRIPT_FINISHED) continue; + switch (_scripts[i]->_state) { + case SCRIPT_RUNNING: + case SCRIPT_SLEEPING: + case SCRIPT_PAUSED: + numRunning++; + break; + case SCRIPT_WAITING: + numWaiting++; + break; + case SCRIPT_PERSISTENT: + numPersistent++; + break; + default: + warning("CScEngine::GetNumScripts - unhandled enum"); + break; + } + numTotal++; + } + if (running) *running = numRunning; + if (waiting) *waiting = numWaiting; + if (persistent) *persistent = numPersistent; + + return numTotal; +} + + +////////////////////////////////////////////////////////////////////////// +bool CScEngine::emptyScriptCache() { + for (int i = 0; i < MAX_CACHED_SCRIPTS; i++) { + if (_cachedScripts[i]) { + delete _cachedScripts[i]; + _cachedScripts[i] = NULL; + } + } + return STATUS_OK; +} + + +////////////////////////////////////////////////////////////////////////// +bool CScEngine::resetObject(CBObject *Object) { + // terminate all scripts waiting for this object + for (int i = 0; i < _scripts.getSize(); i++) { + if (_scripts[i]->_state == SCRIPT_WAITING && _scripts[i]->_waitObject == Object) { + if (!_gameRef->_compatKillMethodThreads) resetScript(_scripts[i]); + + bool IsThread = _scripts[i]->_methodThread || _scripts[i]->_thread; + _scripts[i]->finish(!IsThread); // 1.9b1 - top-level script kills its threads as well + } + } + return STATUS_OK; +} + +////////////////////////////////////////////////////////////////////////// +bool CScEngine::resetScript(CScScript *script) { + // terminate all scripts waiting for this script + for (int i = 0; i < _scripts.getSize(); i++) { + if (_scripts[i]->_state == SCRIPT_WAITING_SCRIPT && _scripts[i]->_waitScript == script) { + _scripts[i]->finish(); + } + } + return STATUS_OK; +} + +////////////////////////////////////////////////////////////////////////// +bool CScEngine::persist(CBPersistMgr *persistMgr) { + if (!persistMgr->_saving) cleanup(); + + persistMgr->transfer(TMEMBER(_gameRef)); + persistMgr->transfer(TMEMBER(_currentScript)); + persistMgr->transfer(TMEMBER(_globals)); + _scripts.persist(persistMgr); + + return STATUS_OK; +} + + +////////////////////////////////////////////////////////////////////////// +void CScEngine::editorCleanup() { + for (int i = 0; i < _scripts.getSize(); i++) { + if (_scripts[i]->_owner == NULL && (_scripts[i]->_state == SCRIPT_FINISHED || _scripts[i]->_state == SCRIPT_ERROR)) { + delete _scripts[i]; + _scripts.removeAt(i); + i--; + } + } +} + + +////////////////////////////////////////////////////////////////////////// +bool CScEngine::pauseAll() { + for (int i = 0; i < _scripts.getSize(); i++) { + if (_scripts[i] != _currentScript) _scripts[i]->pause(); + } + + return STATUS_OK; +} + + +////////////////////////////////////////////////////////////////////////// +bool CScEngine::resumeAll() { + for (int i = 0; i < _scripts.getSize(); i++) + _scripts[i]->resume(); + + return STATUS_OK; +} + + +////////////////////////////////////////////////////////////////////////// +bool CScEngine::isValidScript(CScScript *script) { + for (int i = 0; i < _scripts.getSize(); i++) { + if (_scripts[i] == script) return true; + } + return false; +} + +////////////////////////////////////////////////////////////////////////// +bool CScEngine::clearGlobals(bool includingNatives) { + _globals->CleanProps(includingNatives); + return STATUS_OK; +} + +////////////////////////////////////////////////////////////////////////// +bool CScEngine::dbgSendScripts(IWmeDebugClient *client) { + // send global variables + _globals->dbgSendVariables(client, WME_DBGVAR_GLOBAL, NULL, 0); + + // process normal scripts first + for (int i = 0; i < _scripts.getSize(); i++) { + if (_scripts[i]->_thread || _scripts[i]->_methodThread) continue; + _scripts[i]->dbgSendScript(client); + } + + // and threads later + for (int i = 0; i < _scripts.getSize(); i++) { + if (_scripts[i]->_thread || _scripts[i]->_methodThread) + _scripts[i]->dbgSendScript(client); + } + + return STATUS_OK; +} + +////////////////////////////////////////////////////////////////////////// +bool CScEngine::addBreakpoint(const char *scriptFilename, int line) { + if (!_gameRef->getDebugMgr()->_enabled) return STATUS_OK; + + CScBreakpoint *bp = NULL; + for (int i = 0; i < _breakpoints.getSize(); i++) { + if (scumm_stricmp(_breakpoints[i]->_filename.c_str(), scriptFilename) == 0) { + bp = _breakpoints[i]; + break; + } + } + if (bp == NULL) { + bp = new CScBreakpoint(scriptFilename); + _breakpoints.add(bp); + } + + for (int i = 0; i < bp->_lines.getSize(); i++) { + if (bp->_lines[i] == line) return STATUS_OK; + } + bp->_lines.add(line); + + // refresh changes + refreshScriptBreakpoints(); + + return STATUS_OK; +} + +////////////////////////////////////////////////////////////////////////// +bool CScEngine::removeBreakpoint(const char *scriptFilename, int line) { + if (!_gameRef->getDebugMgr()->_enabled) return STATUS_OK; + + for (int i = 0; i < _breakpoints.getSize(); i++) { + if (scumm_stricmp(_breakpoints[i]->_filename.c_str(), scriptFilename) == 0) { + for (int j = 0; j < _breakpoints[i]->_lines.getSize(); j++) { + if (_breakpoints[i]->_lines[j] == line) { + _breakpoints[i]->_lines.removeAt(j); + if (_breakpoints[i]->_lines.getSize() == 0) { + delete _breakpoints[i]; + _breakpoints.removeAt(i); + } + // refresh changes + refreshScriptBreakpoints(); + + return STATUS_OK; + } + } + break; + } + } + return STATUS_FAILED; +} + +////////////////////////////////////////////////////////////////////////// +bool CScEngine::refreshScriptBreakpoints() { + if (!_gameRef->getDebugMgr()->_enabled) return STATUS_OK; + + for (int i = 0; i < _scripts.getSize(); i++) { + refreshScriptBreakpoints(_scripts[i]); + } + return STATUS_OK; +} + +////////////////////////////////////////////////////////////////////////// +bool CScEngine::refreshScriptBreakpoints(CScScript *script) { + if (!_gameRef->getDebugMgr()->_enabled) return STATUS_OK; + + if (!script || !script->_filename) return STATUS_FAILED; + + for (int i = 0; i < _breakpoints.getSize(); i++) { + if (scumm_stricmp(_breakpoints[i]->_filename.c_str(), script->_filename) == 0) { + script->_breakpoints.copy(_breakpoints[i]->_lines); + return STATUS_OK; + } + } + if (script->_breakpoints.getSize() > 0) script->_breakpoints.removeAll(); + + return STATUS_OK; +} + +////////////////////////////////////////////////////////////////////////// +bool CScEngine::saveBreakpoints() { + if (!_gameRef->getDebugMgr()->_enabled) return STATUS_OK; + + + char text[512]; + char key[100]; + + int count = 0; + for (int i = 0; i < _breakpoints.getSize(); i++) { + for (int j = 0; j < _breakpoints[i]->_lines.getSize(); j++) { + count++; + sprintf(key, "Breakpoint%d", count); + sprintf(text, "%s:%d", _breakpoints[i]->_filename.c_str(), _breakpoints[i]->_lines[j]); + + _gameRef->_registry->writeString("Debug", key, text); + } + } + _gameRef->_registry->writeInt("Debug", "NumBreakpoints", count); + + return STATUS_OK; +} + +////////////////////////////////////////////////////////////////////////// +bool CScEngine::loadBreakpoints() { + if (!_gameRef->getDebugMgr()->_enabled) return STATUS_OK; + + char key[100]; + + int count = _gameRef->_registry->readInt("Debug", "NumBreakpoints", 0); + for (int i = 1; i <= count; i++) { + /* uint32 BufSize = 512; */ + sprintf(key, "Breakpoint%d", i); + AnsiString breakpoint = _gameRef->_registry->readString("Debug", key, ""); + + char *path = CBUtils::strEntry(0, breakpoint.c_str(), ':'); + char *line = CBUtils::strEntry(1, breakpoint.c_str(), ':'); + + if (path != NULL && line != NULL) addBreakpoint(path, atoi(line)); + delete[] path; + delete[] line; + path = NULL; + line = NULL; + } + + return STATUS_OK; +} + + +////////////////////////////////////////////////////////////////////////// +void CScEngine::addScriptTime(const char *filename, uint32 time) { + if (!_isProfiling) return; + + AnsiString fileName = filename; + fileName.toLowercase(); + _scriptTimes[fileName] += time; +} + + +////////////////////////////////////////////////////////////////////////// +void CScEngine::enableProfiling() { + if (_isProfiling) return; + + // destroy old data, if any + _scriptTimes.clear(); + + _profilingStartTime = CBPlatform::getTime(); + _isProfiling = true; +} + + +////////////////////////////////////////////////////////////////////////// +void CScEngine::disableProfiling() { + if (!_isProfiling) return; + + dumpStats(); + _isProfiling = false; +} + + +////////////////////////////////////////////////////////////////////////// +void CScEngine::dumpStats() { + error("DumpStats not ported to ScummVM yet"); + /* uint32 totalTime = CBPlatform::getTime() - _profilingStartTime; + + typedef std::vector > TimeVector; + TimeVector times; + + ScriptTimes::iterator it; + for (it = _scriptTimes.begin(); it != _scriptTimes.end(); it++) { + times.push_back(std::pair (it->_value, it->_key)); + } + std::sort(times.begin(), times.end()); + + + TimeVector::reverse_iterator tit; + + _gameRef->LOG(0, "***** Script profiling information: *****"); + _gameRef->LOG(0, " %-40s %fs", "Total execution time", (float)totalTime / 1000); + + for (tit = times.rbegin(); tit != times.rend(); tit++) { + _gameRef->LOG(0, " %-40s %fs (%f%%)", tit->second.c_str(), (float)tit->first / 1000, (float)tit->first / (float)totalTime * 100); + }*/ +} + +} // end of namespace WinterMute diff --git a/engines/wintermute/base/scriptables/script_engine.h b/engines/wintermute/base/scriptables/script_engine.h new file mode 100644 index 0000000000..81dc13a73c --- /dev/null +++ b/engines/wintermute/base/scriptables/script_engine.h @@ -0,0 +1,147 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +/* + * This file is based on WME Lite. + * http://dead-code.org/redir.php?target=wmelite + * Copyright (c) 2011 Jan Nedoma + */ + +#ifndef WINTERMUTE_SCENGINE_H +#define WINTERMUTE_SCENGINE_H + +#include "engines/wintermute/persistent.h" +#include "engines/wintermute/coll_templ.h" +#include "engines/wintermute/base/base.h" +#include "engines/wintermute/wme_debugger.h" +#include "engines/wintermute/utils/utils.h" +#include "engines/wintermute/platform_osystem.h" + +namespace WinterMute { + +#define MAX_CACHED_SCRIPTS 20 +class CScScript; +class CScValue; +class CBObject; +class CBScriptHolder; +class CScEngine : public CBBase { +public: + class CScCachedScript { + public: + CScCachedScript(const char *filename, byte *buffer, uint32 size) { + _timestamp = CBPlatform::getTime(); + _buffer = new byte[size]; + if (_buffer) memcpy(_buffer, buffer, size); + _size = size; + _filename = filename; + }; + + ~CScCachedScript() { + if (_buffer) delete [] _buffer; + }; + + uint32 _timestamp; + byte *_buffer; + uint32 _size; + Common::String _filename; + }; + + class CScBreakpoint { + public: + CScBreakpoint(const char *filename) { + _filename = filename; + } + + ~CScBreakpoint() { + _lines.removeAll(); + } + + Common::String _filename; + CBArray _lines; + }; + + + + +public: + bool dbgSendScripts(IWmeDebugClient *client); + + CBArray _breakpoints; + bool addBreakpoint(const char *scriptFilename, int line); + bool removeBreakpoint(const char *scriptFilename, int line); + bool refreshScriptBreakpoints(); + bool refreshScriptBreakpoints(CScScript *script); + bool saveBreakpoints(); + bool loadBreakpoints(); + + bool clearGlobals(bool includingNatives = false); + bool tickUnbreakable(); + bool removeFinishedScripts(); + bool isValidScript(CScScript *script); + + CScScript *_currentScript; + bool resumeAll(); + bool pauseAll(); + void editorCleanup(); + bool resetObject(CBObject *Object); + bool resetScript(CScScript *script); + bool emptyScriptCache(); + byte *getCompiledScript(const char *filename, uint32 *outSize, bool ignoreCache = false); + DECLARE_PERSISTENT(CScEngine, CBBase) + bool cleanup(); + int getNumScripts(int *running = NULL, int *waiting = NULL, int *persistent = NULL); + bool tick(); + CScValue *_globals; + CScScript *runScript(const char *filename, CBScriptHolder *owner = NULL); + static const bool _compilerAvailable = false; + + CScEngine(CBGame *inGame); + virtual ~CScEngine(); + static byte *loadFile(void *data, char *filename, uint32 *size); + static void closeFile(void *data, byte *buffer); + static void parseElement(void *data, int line, int type, void *elementData); + + CBArray _scripts; + + void enableProfiling(); + void disableProfiling(); + bool getIsProfiling() { + return _isProfiling; + } + + void addScriptTime(const char *filename, uint32 Time); + void dumpStats(); + +private: + + CScCachedScript *_cachedScripts[MAX_CACHED_SCRIPTS]; + bool _isProfiling; + uint32 _profilingStartTime; + + typedef Common::HashMap ScriptTimes; + ScriptTimes _scriptTimes; + +}; + +} // end of namespace WinterMute + +#endif diff --git a/engines/wintermute/base/scriptables/script_ext_array.cpp b/engines/wintermute/base/scriptables/script_ext_array.cpp new file mode 100644 index 0000000000..a1b8249cb1 --- /dev/null +++ b/engines/wintermute/base/scriptables/script_ext_array.cpp @@ -0,0 +1,238 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +/* + * This file is based on WME Lite. + * http://dead-code.org/redir.php?target=wmelite + * Copyright (c) 2011 Jan Nedoma + */ + +#include "engines/wintermute/persistent.h" +#include "engines/wintermute/base/scriptables/script_value.h" +#include "engines/wintermute/base/scriptables/script_stack.h" +#include "engines/wintermute/system/sys_instance.h" +#include "engines/wintermute/base/scriptables/script_ext_array.h" + +namespace WinterMute { + +IMPLEMENT_PERSISTENT(CSXArray, false) + +CBScriptable *makeSXArray(CBGame *inGame, CScStack *stack) { + return new CSXArray(inGame, stack); +} + +////////////////////////////////////////////////////////////////////////// +CSXArray::CSXArray(CBGame *inGame, CScStack *stack): CBScriptable(inGame) { + _length = 0; + _values = new CScValue(_gameRef); + + int numParams = stack->pop()->getInt(0); + + if (numParams == 1) _length = stack->pop()->getInt(0); + else if (numParams > 1) { + _length = numParams; + char paramName[20]; + for (int i = 0; i < numParams; i++) { + sprintf(paramName, "%d", i); + _values->setProp(paramName, stack->pop()); + } + } +} + +////////////////////////////////////////////////////////////////////////// +CSXArray::CSXArray(CBGame *inGame): CBScriptable(inGame) { + _length = 0; + _values = new CScValue(_gameRef); +} + + +////////////////////////////////////////////////////////////////////////// +CSXArray::~CSXArray() { + delete _values; + _values = NULL; +} + + +////////////////////////////////////////////////////////////////////////// +const char *CSXArray::scToString() { + static char dummy[32768]; // TODO: Get rid of static. + strcpy(dummy, ""); + char propName[20]; + for (int i = 0; i < _length; i++) { + sprintf(propName, "%d", i); + CScValue *val = _values->getProp(propName); + if (val) { + if (strlen(dummy) + strlen(val->getString()) < 32768) { + strcat(dummy, val->getString()); + } + } + + if (i < _length - 1 && strlen(dummy) + 1 < 32768) strcat(dummy, ","); + } + return dummy; +} + + +////////////////////////////////////////////////////////////////////////// +bool CSXArray::scCallMethod(CScScript *script, CScStack *stack, CScStack *thisStack, const char *name) { + ////////////////////////////////////////////////////////////////////////// + // Push + ////////////////////////////////////////////////////////////////////////// + if (strcmp(name, "Push") == 0) { + int numParams = stack->pop()->getInt(0); + char paramName[20]; + + for (int i = 0; i < numParams; i++) { + _length++; + sprintf(paramName, "%d", _length - 1); + _values->setProp(paramName, stack->pop(), true); + } + stack->pushInt(_length); + + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // Pop + ////////////////////////////////////////////////////////////////////////// + if (strcmp(name, "Pop") == 0) { + + stack->correctParams(0); + + if (_length > 0) { + char paramName[20]; + sprintf(paramName, "%d", _length - 1); + stack->push(_values->getProp(paramName)); + _values->deleteProp(paramName); + _length--; + } else stack->pushNULL(); + + return STATUS_OK; + } + + else return STATUS_FAILED; +} + + +////////////////////////////////////////////////////////////////////////// +CScValue *CSXArray::scGetProperty(const char *name) { + _scValue->setNULL(); + + ////////////////////////////////////////////////////////////////////////// + // Type + ////////////////////////////////////////////////////////////////////////// + if (strcmp(name, "Type") == 0) { + _scValue->setString("array"); + return _scValue; + } + + ////////////////////////////////////////////////////////////////////////// + // Length + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "Length") == 0) { + _scValue->setInt(_length); + return _scValue; + } + + ////////////////////////////////////////////////////////////////////////// + // [number] + ////////////////////////////////////////////////////////////////////////// + else { + char ParamName[20]; + if (validNumber(name, ParamName)) { + return _values->getProp(ParamName); + } else return _scValue; + } +} + + +////////////////////////////////////////////////////////////////////////// +bool CSXArray::scSetProperty(const char *name, CScValue *value) { + ////////////////////////////////////////////////////////////////////////// + // Length + ////////////////////////////////////////////////////////////////////////// + if (strcmp(name, "Length") == 0) { + int OrigLength = _length; + _length = MAX(value->getInt(0), 0); + + char PropName[20]; + if (_length < OrigLength) { + for (int i = _length; i < OrigLength; i++) { + sprintf(PropName, "%d", i); + _values->deleteProp(PropName); + } + } + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // [number] + ////////////////////////////////////////////////////////////////////////// + else { + char paramName[20]; + if (validNumber(name, paramName)) { + int Index = atoi(paramName); + if (Index >= _length) _length = Index + 1; + return _values->setProp(paramName, value); + } else return STATUS_FAILED; + } +} + + +////////////////////////////////////////////////////////////////////////// +bool CSXArray::persist(CBPersistMgr *persistMgr) { + CBScriptable::persist(persistMgr); + + persistMgr->transfer(TMEMBER(_length)); + persistMgr->transfer(TMEMBER(_values)); + + return STATUS_OK; +} + + +////////////////////////////////////////////////////////////////////////// +bool CSXArray::validNumber(const char *origStr, char *outStr) { + bool isNumber = true; + for (uint32 i = 0; i < strlen(origStr); i++) { + if (!(origStr[i] >= '0' && origStr[i] <= '9')) { + isNumber = false; + break; + } + } + + if (isNumber) { + int index = atoi(origStr); + sprintf(outStr, "%d", index); + return true; + } else return false; +} + +////////////////////////////////////////////////////////////////////////// +bool CSXArray::push(CScValue *val) { + char paramName[20]; + _length++; + sprintf(paramName, "%d", _length - 1); + _values->setProp(paramName, val, true); + return STATUS_OK; +} + +} // end of namespace WinterMute diff --git a/engines/wintermute/base/scriptables/script_ext_array.h b/engines/wintermute/base/scriptables/script_ext_array.h new file mode 100644 index 0000000000..b873416572 --- /dev/null +++ b/engines/wintermute/base/scriptables/script_ext_array.h @@ -0,0 +1,54 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +/* + * This file is based on WME Lite. + * http://dead-code.org/redir.php?target=wmelite + * Copyright (c) 2011 Jan Nedoma + */ + +#ifndef WINTERMUTE_SXARRAY_H +#define WINTERMUTE_SXARRAY_H + +#include "engines/wintermute/base/base_scriptable.h" + +namespace WinterMute { + +class CSXArray : public CBScriptable { +public: + bool push(CScValue *Val); + bool validNumber(const char *origStr, char *outStr); + DECLARE_PERSISTENT(CSXArray, CBScriptable) + CSXArray(CBGame *inGame, CScStack *stack); + CSXArray(CBGame *inGame); + virtual ~CSXArray(); + CScValue *scGetProperty(const char *name); + bool scSetProperty(const char *name, CScValue *value); + bool scCallMethod(CScScript *script, CScStack *stack, CScStack *thisStack, const char *name); + const char *scToString(); + int _length; + CScValue *_values; +}; + +} // end of namespace WinterMute + +#endif diff --git a/engines/wintermute/base/scriptables/script_ext_date.cpp b/engines/wintermute/base/scriptables/script_ext_date.cpp new file mode 100644 index 0000000000..211c0d34f0 --- /dev/null +++ b/engines/wintermute/base/scriptables/script_ext_date.cpp @@ -0,0 +1,297 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +/* + * This file is based on WME Lite. + * http://dead-code.org/redir.php?target=wmelite + * Copyright (c) 2011 Jan Nedoma + */ + +#include "engines/wintermute/base/scriptables/script_stack.h" +#include "engines/wintermute/base/scriptables/script_value.h" +#include "engines/wintermute/base/scriptables/script_ext_date.h" + +namespace WinterMute { + +IMPLEMENT_PERSISTENT(CSXDate, false) + +CBScriptable *makeSXDate(CBGame *inGame, CScStack *stack) { + return new CSXDate(inGame, stack); +} + +////////////////////////////////////////////////////////////////////////// +CSXDate::CSXDate(CBGame *inGame, CScStack *stack): CBScriptable(inGame) { + stack->correctParams(6); + + memset(&_tm, 0, sizeof(_tm)); + + CScValue *valYear = stack->pop(); + _tm.tm_year = valYear->getInt() - 1900; + _tm.tm_mon = stack->pop()->getInt() - 1; + _tm.tm_mday = stack->pop()->getInt(); + _tm.tm_hour = stack->pop()->getInt(); + _tm.tm_min = stack->pop()->getInt(); + _tm.tm_sec = stack->pop()->getInt(); + + if (valYear->isNULL()) { + g_system->getTimeAndDate(_tm); + } +} + + +////////////////////////////////////////////////////////////////////////// +CSXDate::~CSXDate() { + +} + +////////////////////////////////////////////////////////////////////////// +const char *CSXDate::scToString() { + // TODO: Make this more stringy, and less ISO 8601-like + _strRep.format("%04d-%02d-%02d - %02d:%02d:%02d", _tm.tm_year, _tm.tm_mon, _tm.tm_mday, _tm.tm_hour, _tm.tm_min, _tm.tm_sec); + return _strRep.c_str(); +#if 0 + return asctime(&_tm); +#endif +} + + +////////////////////////////////////////////////////////////////////////// +bool CSXDate::scCallMethod(CScScript *script, CScStack *stack, CScStack *thisStack, const char *name) { + ////////////////////////////////////////////////////////////////////////// + // GetYear + ////////////////////////////////////////////////////////////////////////// + if (strcmp(name, "GetYear") == 0) { + stack->correctParams(0); + stack->pushInt(_tm.tm_year + 1900); + return STATUS_OK; + } + ////////////////////////////////////////////////////////////////////////// + // GetMonth + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "GetMonth") == 0) { + stack->correctParams(0); + stack->pushInt(_tm.tm_mon + 1); + return STATUS_OK; + } + ////////////////////////////////////////////////////////////////////////// + // GetDate + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "GetDate") == 0) { + stack->correctParams(0); + stack->pushInt(_tm.tm_mday); + return STATUS_OK; + } + ////////////////////////////////////////////////////////////////////////// + // GetHours + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "GetHours") == 0) { + stack->correctParams(0); + stack->pushInt(_tm.tm_hour); + return STATUS_OK; + } + ////////////////////////////////////////////////////////////////////////// + // GetMinutes + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "GetMinutes") == 0) { + stack->correctParams(0); + stack->pushInt(_tm.tm_min); + return STATUS_OK; + } + ////////////////////////////////////////////////////////////////////////// + // GetSeconds + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "GetSeconds") == 0) { + stack->correctParams(0); + stack->pushInt(_tm.tm_sec); + return STATUS_OK; + } + ////////////////////////////////////////////////////////////////////////// + // GetWeekday + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "GetWeekday") == 0) { + stack->correctParams(0); + warning("GetWeekday returns a wrong value on purpose"); + stack->pushInt(_tm.tm_mday % 7); + return STATUS_OK; + } + + + ////////////////////////////////////////////////////////////////////////// + // SetYear + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "SetYear") == 0) { + stack->correctParams(1); + _tm.tm_year = stack->pop()->getInt() - 1900; + stack->pushNULL(); + return STATUS_OK; + } + ////////////////////////////////////////////////////////////////////////// + // SetMonth + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "SetMonth") == 0) { + stack->correctParams(1); + _tm.tm_mon = stack->pop()->getInt() - 1; + stack->pushNULL(); + return STATUS_OK; + } + ////////////////////////////////////////////////////////////////////////// + // SetDate + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "SetDate") == 0) { + stack->correctParams(1); + _tm.tm_mday = stack->pop()->getInt(); + stack->pushNULL(); + return STATUS_OK; + } + ////////////////////////////////////////////////////////////////////////// + // SetHours + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "SetHours") == 0) { + stack->correctParams(1); + _tm.tm_hour = stack->pop()->getInt(); + stack->pushNULL(); + return STATUS_OK; + } + ////////////////////////////////////////////////////////////////////////// + // SetMinutes + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "SetMinutes") == 0) { + stack->correctParams(1); + _tm.tm_min = stack->pop()->getInt(); + stack->pushNULL(); + return STATUS_OK; + } + ////////////////////////////////////////////////////////////////////////// + // SetSeconds + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "SetSeconds") == 0) { + stack->correctParams(1); + _tm.tm_sec = stack->pop()->getInt(); + stack->pushNULL(); + return STATUS_OK; + } + + + ////////////////////////////////////////////////////////////////////////// + // SetCurrentTime + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "SetCurrentTime") == 0) { + stack->correctParams(0); + g_system->getTimeAndDate(_tm); + stack->pushNULL(); + return STATUS_OK; + } + + else + return STATUS_FAILED; +} + + +////////////////////////////////////////////////////////////////////////// +CScValue *CSXDate::scGetProperty(const char *name) { + _scValue->setNULL(); + + ////////////////////////////////////////////////////////////////////////// + // Type + ////////////////////////////////////////////////////////////////////////// + if (strcmp(name, "Type") == 0) { + _scValue->setString("date"); + return _scValue; + } + + else return _scValue; +} + + +////////////////////////////////////////////////////////////////////////// +bool CSXDate::scSetProperty(const char *name, CScValue *value) { + /* + ////////////////////////////////////////////////////////////////////////// + // Name + ////////////////////////////////////////////////////////////////////////// + if(strcmp(name, "Name")==0){ + setName(value->getString()); + return STATUS_OK; + } + + else*/ return STATUS_FAILED; +} + + +////////////////////////////////////////////////////////////////////////// +bool CSXDate::persist(CBPersistMgr *persistMgr) { + + CBScriptable::persist(persistMgr); + persistMgr->transfer(TMEMBER(_tm.tm_year)); + persistMgr->transfer(TMEMBER(_tm.tm_mon)); + persistMgr->transfer(TMEMBER(_tm.tm_mday)); + persistMgr->transfer(TMEMBER(_tm.tm_hour)); + persistMgr->transfer(TMEMBER(_tm.tm_min)); + persistMgr->transfer(TMEMBER(_tm.tm_sec)); + return STATUS_OK; +} + +////////////////////////////////////////////////////////////////////////// +int CSXDate::scCompare(CBScriptable *Value) { + TimeDate time1 = _tm; + TimeDate time2 = ((CSXDate *)Value)->_tm; + + if (time1.tm_year < time2.tm_year) { + return -1; + } else if (time1.tm_year == time2.tm_year) { + if (time1.tm_mon < time2.tm_mon) { + return -1; + } else if (time1.tm_mon == time2.tm_mon) { + if (time1.tm_mday < time2.tm_mday) { + return -1; + } else if (time1.tm_mday == time2.tm_mday) { + if (time1.tm_hour < time2.tm_hour) { + return -1; + } else if (time1.tm_hour == time2.tm_hour) { + if (time1.tm_min < time2.tm_min) { + return -1; + } else if (time1.tm_min == time2.tm_min) { + if (time1.tm_sec < time2.tm_sec) { + return -1; + } else if (time1.tm_sec == time2.tm_sec) { + return 0; // Equal + } else { + return 1; // Sec + } + } else { + return 1; // Minute + } + } else { + return 1; // Hour + } + } else { + return 1; // Day + } + } else { + return 1; // Month + } + } else { + return 1; // Year + } +} + +} // end of namespace WinterMute diff --git a/engines/wintermute/base/scriptables/script_ext_date.h b/engines/wintermute/base/scriptables/script_ext_date.h new file mode 100644 index 0000000000..82f6af1f1d --- /dev/null +++ b/engines/wintermute/base/scriptables/script_ext_date.h @@ -0,0 +1,55 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +/* + * This file is based on WME Lite. + * http://dead-code.org/redir.php?target=wmelite + * Copyright (c) 2011 Jan Nedoma + */ + +#ifndef WINTERMUTE_SXDATE_H +#define WINTERMUTE_SXDATE_H + +#include "common/system.h" +#include "engines/wintermute/base/base_scriptable.h" + +namespace WinterMute { + +class CSXDate : public CBScriptable { +public: + int scCompare(CBScriptable *Value); + DECLARE_PERSISTENT(CSXDate, CBScriptable) + CSXDate(CBGame *inGame, CScStack *Stack); + virtual ~CSXDate(); + CScValue *scGetProperty(const char *name); + bool scSetProperty(const char *name, CScValue *value); + bool scCallMethod(CScScript *script, CScStack *stack, CScStack *thisStack, const char *name); + const char *scToString(); + char *_string; + TimeDate _tm; +private: + Common::String _strRep; +}; + +} // end of namespace WinterMute + +#endif diff --git a/engines/wintermute/base/scriptables/script_ext_file.cpp b/engines/wintermute/base/scriptables/script_ext_file.cpp new file mode 100644 index 0000000000..58c0416b43 --- /dev/null +++ b/engines/wintermute/base/scriptables/script_ext_file.cpp @@ -0,0 +1,779 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +/* + * This file is based on WME Lite. + * http://dead-code.org/redir.php?target=wmelite + * Copyright (c) 2011 Jan Nedoma + */ + +#include "engines/wintermute/system/sys_class_registry.h" +#include "engines/wintermute/system/sys_class.h" +#include "engines/wintermute/base/scriptables/script_stack.h" +#include "engines/wintermute/base/scriptables/script_value.h" +#include "engines/wintermute/base/scriptables/script.h" +#include "engines/wintermute/utils/utils.h" +#include "engines/wintermute/base/base_game.h" +#include "engines/wintermute/base/file/base_file.h" +#include "engines/wintermute/base/base_file_manager.h" +#include "engines/wintermute/platform_osystem.h" +#include "engines/wintermute/base/scriptables/script_ext_file.h" + +// Note: This code is completely untested, as I have yet to find a game that uses SXFile. + +namespace WinterMute { + +IMPLEMENT_PERSISTENT(CSXFile, false) + +CBScriptable *makeSXFile(CBGame *inGame, CScStack *stack) { + return new CSXFile(inGame, stack); +} + +////////////////////////////////////////////////////////////////////////// +CSXFile::CSXFile(CBGame *inGame, CScStack *stack): CBScriptable(inGame) { + stack->correctParams(1); + CScValue *Val = stack->pop(); + + _filename = NULL; + if (!Val->isNULL()) CBUtils::setString(&_filename, Val->getString()); + + _readFile = NULL; + _writeFile = NULL; + + _mode = 0; + _textMode = false; +} + + +////////////////////////////////////////////////////////////////////////// +CSXFile::~CSXFile() { + cleanup(); +} + +////////////////////////////////////////////////////////////////////////// +void CSXFile::cleanup() { + delete[] _filename; + _filename = NULL; + close(); +} + + +////////////////////////////////////////////////////////////////////////// +void CSXFile::close() { + if (_readFile) { + _gameRef->_fileManager->closeFile(_readFile); + _readFile = NULL; + } + if (_writeFile) { + _writeFile->finalize(); + delete _writeFile; + _writeFile = NULL; + } + _mode = 0; + _textMode = false; +} + +////////////////////////////////////////////////////////////////////////// +const char *CSXFile::scToString() { + if (_filename) return _filename; + else return "[file object]"; +} + +#define FILE_BUFFER_SIZE 32768 +////////////////////////////////////////////////////////////////////////// +bool CSXFile::scCallMethod(CScScript *script, CScStack *stack, CScStack *thisStack, const char *name) { + ////////////////////////////////////////////////////////////////////////// + // SetFilename + ////////////////////////////////////////////////////////////////////////// + if (strcmp(name, "SetFilename") == 0) { + stack->correctParams(1); + const char *filename = stack->pop()->getString(); + cleanup(); + CBUtils::setString(&_filename, filename); + stack->pushNULL(); + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // OpenAsText / OpenAsBinary + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "OpenAsText") == 0 || strcmp(name, "OpenAsBinary") == 0) { + stack->correctParams(1); + close(); + _mode = stack->pop()->getInt(1); + if (_mode < 1 || _mode > 3) { + script->runtimeError("File.%s: invalid access mode. Setting read mode.", name); + _mode = 1; + } + if (_mode == 1) { + _readFile = _gameRef->_fileManager->openFile(_filename); + if (!_readFile) { + //script->runtimeError("File.%s: Error opening file '%s' for reading.", Name, _filename); + close(); + } else _textMode = strcmp(name, "OpenAsText") == 0; + } else { + if (strcmp(name, "OpenAsText") == 0) { + if (_mode == 2) _writeFile = openForWrite(_filename, false); + else _writeFile = openForAppend(_filename, false); + } else { + if (_mode == 2) _writeFile = openForWrite(_filename, true); + else _writeFile = openForAppend(_filename, true); + } + + if (!_writeFile) { + //script->runtimeError("File.%s: Error opening file '%s' for writing.", Name, _filename); + close(); + } else _textMode = strcmp(name, "OpenAsText") == 0; + } + + if (_readFile || _writeFile) stack->pushBool(true); + else stack->pushBool(false); + + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // Close + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "Close") == 0) { + stack->correctParams(0); + close(); + stack->pushNULL(); + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // SetPosition + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "SetPosition") == 0) { + stack->correctParams(1); + if (_mode == 0) { + script->runtimeError("File.%s: File is not open", name); + stack->pushBool(false); + } else { + int Pos = stack->pop()->getInt(); + stack->pushBool(setPos(Pos)); + } + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // Delete + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "Delete") == 0) { + stack->correctParams(0); + close(); + stack->pushBool(CBPlatform::deleteFile(_filename) != false); + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // Copy + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "Copy") == 0) { + stack->correctParams(2); + const char *Dest = stack->pop()->getString(); + bool Overwrite = stack->pop()->getBool(true); + + close(); + stack->pushBool(CBPlatform::copyFile(_filename, Dest, !Overwrite) != false); + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // ReadLine + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "ReadLine") == 0) { + stack->correctParams(0); + if (!_textMode || !_readFile) { + script->runtimeError("File.%s: File must be open in text mode.", name); + stack->pushNULL(); + return STATUS_OK; + } + uint32 bufSize = FILE_BUFFER_SIZE; + byte *buf = (byte *)malloc(bufSize); + uint32 counter = 0; + byte b; + bool foundNewLine = false; + bool ret = STATUS_FAILED; + do { + ret = _readFile->read(&b, 1); + if (ret != 1) break; + + if (counter > bufSize) { + buf = (byte *)realloc(buf, bufSize + FILE_BUFFER_SIZE); + bufSize += FILE_BUFFER_SIZE; + } + if (b == '\n') { + buf[counter] = '\0'; + foundNewLine = true; + break; + } else if (b == 0x0D) continue; + else { + buf[counter] = b; + counter++; + } + } while (DID_SUCCEED(ret)); + + if (counter > bufSize) { + buf = (byte *)realloc(buf, bufSize + FILE_BUFFER_SIZE); + bufSize += FILE_BUFFER_SIZE; + } + buf[counter] = '\0'; + + if (!foundNewLine && counter == 0) stack->pushNULL(); + else stack->pushString((char *)buf); + + free(buf); + + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // ReadText + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "ReadText") == 0) { + stack->correctParams(1); + int textLen = stack->pop()->getInt(); + + if (!_textMode || !_readFile) { + script->runtimeError("File.%s: File must be open in text mode.", name); + stack->pushNULL(); + return STATUS_OK; + } + uint32 bufSize = FILE_BUFFER_SIZE; + byte *buf = (byte *)malloc(bufSize); + uint32 counter = 0; + byte b; + + bool ret = STATUS_FAILED; + while (counter < (uint32)textLen) { + ret = _readFile->read(&b, 1); + if (ret != 1) break; + + if (counter > bufSize) { + buf = (byte *)realloc(buf, bufSize + FILE_BUFFER_SIZE); + bufSize += FILE_BUFFER_SIZE; + } + if (b == 0x0D) continue; + else { + buf[counter] = b; + counter++; + } + } + + if (counter > bufSize) { + buf = (byte *)realloc(buf, bufSize + FILE_BUFFER_SIZE); + bufSize += FILE_BUFFER_SIZE; + } + buf[counter] = '\0'; + + if (textLen > 0 && counter == 0) stack->pushNULL(); + else stack->pushString((char *)buf); + + free(buf); + + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // WriteLine / WriteText + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "WriteLine") == 0 || strcmp(name, "WriteText") == 0) { + stack->correctParams(1); + const char *line = stack->pop()->getString(); + if (!_textMode || !_writeFile) { + script->runtimeError("File.%s: File must be open for writing in text mode.", name); + stack->pushBool(false); + return STATUS_OK; + } + Common::String writeLine; + if (strcmp(name, "WriteLine") == 0) { + writeLine = Common::String::format("%s\n", line); + } else { + writeLine = Common::String::format("%s", line); + } + _writeFile->writeString(writeLine); + _writeFile->writeByte(0); + stack->pushBool(true); + + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + ////////////////////////////////////////////////////////////////////////// + // ReadBool + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "ReadBool") == 0) { + stack->correctParams(0); + if (_textMode || !_readFile) { + script->runtimeError("File.%s: File must be open for reading in binary mode.", name); + stack->pushNULL(); + return STATUS_OK; + } + bool val; + if (_readFile->read(&val, sizeof(bool)) == sizeof(bool)) stack->pushBool(val); + else stack->pushNULL(); + + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // ReadByte + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "ReadByte") == 0) { + stack->correctParams(0); + if (_textMode || !_readFile) { + script->runtimeError("File.%s: File must be open for reading in binary mode.", name); + stack->pushNULL(); + return STATUS_OK; + } + byte val = _readFile->readByte(); + if (!_readFile->err()) { + stack->pushInt(val); + } else { + stack->pushNULL(); + } + + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // ReadShort + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "ReadShort") == 0) { + stack->correctParams(0); + if (_textMode || !_readFile) { + script->runtimeError("File.%s: File must be open for reading in binary mode.", name); + stack->pushNULL(); + return STATUS_OK; + } + int16 val = _readFile->readSint16LE(); + if (!_readFile->err()) { + stack->pushInt(65536 + val); + } else { + stack->pushNULL(); + } + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // ReadInt / ReadLong + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "ReadInt") == 0 || strcmp(name, "ReadLong") == 0) { + stack->correctParams(0); + if (_textMode || !_readFile) { + script->runtimeError("File.%s: File must be open for reading in binary mode.", name); + stack->pushNULL(); + return STATUS_OK; + } + int32 val = _readFile->readSint32LE(); + if (!_readFile->err()) { + stack->pushInt(val); + } else { + stack->pushNULL(); + } + + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // ReadFloat + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "ReadFloat") == 0) { + stack->correctParams(0); + if (_textMode || !_readFile) { + script->runtimeError("File.%s: File must be open for reading in binary mode.", name); + stack->pushNULL(); + return STATUS_OK; + } + float val; + (*(uint32*)&val) = _readFile->readUint32LE(); + if (!_readFile->err()) { + stack->pushFloat(val); + } else { + stack->pushNULL(); + } + + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // ReadDouble + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "ReadDouble") == 0) { // TODO: Solve reading a 8 byte double. + error("SXFile::ReadDouble - Not endian safe yet"); + stack->correctParams(0); + if (_textMode || !_readFile) { + script->runtimeError("File.%s: File must be open for reading in binary mode.", name); + stack->pushNULL(); + return STATUS_OK; + } + double val; + if (_readFile->read(&val, sizeof(double)) == sizeof(double)) stack->pushFloat(val); + else stack->pushNULL(); + + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // ReadString + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "ReadString") == 0) { + stack->correctParams(0); + if (_textMode || !_readFile) { + script->runtimeError("File.%s: File must be open for reading in binary mode.", name); + stack->pushNULL(); + return STATUS_OK; + } + uint32 size = _readFile->readUint32LE(); + if (!_readFile->err()) { + byte *str = new byte[size + 1]; + if (str) { + if (_readFile->read(str, size) == size) { + str[size] = '\0'; + stack->pushString((char *)str); + } + delete [] str; + } else stack->pushNULL(); + } else stack->pushNULL(); + + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // WriteBool + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "WriteBool") == 0) { + stack->correctParams(1); + bool val = stack->pop()->getBool(); + + if (_textMode || !_writeFile) { + script->runtimeError("File.%s: File must be open for writing in binary mode.", name); + stack->pushBool(false); + return STATUS_OK; + } + _writeFile->writeByte(val); + stack->pushBool(true); + + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // WriteByte + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "WriteByte") == 0) { + stack->correctParams(1); + byte val = stack->pop()->getInt(); + + if (_textMode || !_writeFile) { + script->runtimeError("File.%s: File must be open for writing in binary mode.", name); + stack->pushBool(false); + return STATUS_OK; + } + _writeFile->writeByte(val); + stack->pushBool(true); + + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // WriteShort + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "WriteShort") == 0) { + stack->correctParams(1); + int16 val = stack->pop()->getInt(); + + if (_textMode || !_writeFile) { + script->runtimeError("File.%s: File must be open for writing in binary mode.", name); + stack->pushBool(false); + return STATUS_OK; + } + _writeFile->writeSint16LE(val); + stack->pushBool(true); + + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // WriteInt / WriteLong + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "WriteInt") == 0 || strcmp(name, "WriteLong") == 0) { + stack->correctParams(1); + int32 val = stack->pop()->getInt(); + + if (_textMode || !_writeFile) { + script->runtimeError("File.%s: File must be open for writing in binary mode.", name); + stack->pushBool(false); + return STATUS_OK; + } + _writeFile->writeSint32LE(val); + stack->pushBool(true); + + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // WriteFloat + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "WriteFloat") == 0) { + stack->correctParams(1); + float val = stack->pop()->getFloat(); + + if (_textMode || !_writeFile) { + script->runtimeError("File.%s: File must be open for writing in binary mode.", name); + stack->pushBool(false); + return STATUS_OK; + } + uint32 *ptr = (uint32*)&val; + _writeFile->writeUint32LE(*ptr); + stack->pushBool(true); + + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // WriteDouble + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "WriteDouble") == 0) { + error("SXFile::WriteDouble - Not endian safe yet"); + stack->correctParams(1); + double val = stack->pop()->getFloat(); + + if (_textMode || !_writeFile) { + script->runtimeError("File.%s: File must be open for writing in binary mode.", name); + stack->pushBool(false); + return STATUS_OK; + } + //fwrite(&val, sizeof(val), 1, (FILE *)_writeFile); + stack->pushBool(true); + + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // WriteString + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "WriteString") == 0) { + stack->correctParams(1); + const char *val = stack->pop()->getString(); + + if (_textMode || !_writeFile) { + script->runtimeError("File.%s: File must be open for writing in binary mode.", name); + stack->pushBool(false); + return STATUS_OK; + } + + uint32 size = strlen(val); + _writeFile->writeUint32LE(size); + _writeFile->writeString(val); + + stack->pushBool(true); + + return STATUS_OK; + } + + + else return CBScriptable::scCallMethod(script, stack, thisStack, name); +} + + +////////////////////////////////////////////////////////////////////////// +CScValue *CSXFile::scGetProperty(const char *name) { + _scValue->setNULL(); + + ////////////////////////////////////////////////////////////////////////// + // Type (RO) + ////////////////////////////////////////////////////////////////////////// + if (strcmp(name, "Type") == 0) { + _scValue->setString("file"); + return _scValue; + } + + ////////////////////////////////////////////////////////////////////////// + // Filename (RO) + ////////////////////////////////////////////////////////////////////////// + if (strcmp(name, "Filename") == 0) { + _scValue->setString(_filename); + return _scValue; + } + + ////////////////////////////////////////////////////////////////////////// + // Position (RO) + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "Position") == 0) { + _scValue->setInt(getPos()); + return _scValue; + } + + ////////////////////////////////////////////////////////////////////////// + // Length (RO) + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "Length") == 0) { + _scValue->setInt(getLength()); + return _scValue; + } + + ////////////////////////////////////////////////////////////////////////// + // TextMode (RO) + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "TextMode") == 0) { + _scValue->setBool(_textMode); + return _scValue; + } + + ////////////////////////////////////////////////////////////////////////// + // AccessMode (RO) + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "AccessMode") == 0) { + _scValue->setInt(_mode); + return _scValue; + } + + else return CBScriptable::scGetProperty(name); +} + + +////////////////////////////////////////////////////////////////////////// +bool CSXFile::scSetProperty(const char *name, CScValue *value) { + /* + ////////////////////////////////////////////////////////////////////////// + // Length + ////////////////////////////////////////////////////////////////////////// + if(strcmp(name, "Length")==0){ + int OrigLength = _length; + _length = max(value->getInt(0), 0); + + char PropName[20]; + if(_length < OrigLength){ + for(int i=_length; iDeleteProp(PropName); + } + } + return STATUS_OK; + } + else*/ return CBScriptable::scSetProperty(name, value); +} + +////////////////////////////////////////////////////////////////////////// +uint32 CSXFile::getPos() { + if (_mode == 1 && _readFile) + return _readFile->pos(); + else if ((_mode == 2 || _mode == 3) && _writeFile) { + error("SXFile - getPos for WriteFile not supported"); + return 0; +// return ftell((FILE *)_writeFile); + } else { + return 0; + } +} + +////////////////////////////////////////////////////////////////////////// +bool CSXFile::setPos(uint32 pos, int whence) { + if (_mode == 1 && _readFile) + return _readFile->seek(pos, whence); + else if ((_mode == 2 || _mode == 3) && _writeFile) { + error("CSXFile - seeking in WriteFile not supported"); + return false; +// return fseek((FILE *)_writeFile, pos, (int)origin) == 0; + } + else return false; +} + +////////////////////////////////////////////////////////////////////////// +uint32 CSXFile::getLength() { + if (_mode == 1 && _readFile) + return _readFile->size(); + else if ((_mode == 2 || _mode == 3) && _writeFile) { + error("CSXFile - reading length for WriteFile not supported"); + return 0; +/* + uint32 currentPos = ftell((FILE *)_writeFile); + fseek((FILE *)_writeFile, 0, SEEK_END); + int ret = ftell((FILE *)_writeFile); + fseek((FILE *)_writeFile, CurrentPos, SEEK_SET); + return Ret;*/ + } else return 0; +} + +////////////////////////////////////////////////////////////////////////// +bool CSXFile::persist(CBPersistMgr *persistMgr) { + + CBScriptable::persist(persistMgr); + + persistMgr->transfer(TMEMBER(_filename)); + persistMgr->transfer(TMEMBER(_mode)); + persistMgr->transfer(TMEMBER(_textMode)); + + uint32 pos = 0; + if (persistMgr->_saving) { + pos = getPos(); + persistMgr->transfer(TMEMBER(pos)); + } else { + persistMgr->transfer(TMEMBER(pos)); + + // try to re-open file if needed + _writeFile = NULL; + _readFile = NULL; + + if (_mode != 0) { + // open for reading + if (_mode == 1) { + _readFile = _gameRef->_fileManager->openFile(_filename); + if (!_readFile) + close(); + } + // open for writing / appending + else { + if (_textMode) { + if (_mode == 2) + _writeFile = openForWrite(_filename, false); + else + _writeFile = openForAppend(_filename, false); + } else { + if (_mode == 2) + _writeFile = openForWrite(_filename, true); + else + _writeFile = openForAppend(_filename, true); + } + if (_writeFile) + close(); + } + setPos(pos); + } + } + + return STATUS_OK; +} + +// Should replace fopen(..., "wb+") and fopen(..., "w+") +Common::WriteStream *CSXFile::openForWrite(const Common::String &filename, bool binary) { + error("SXFile::openForWrite - WriteFiles not supported"); +} + +// Should replace fopen(..., "ab+") and fopen(..., "a+") +Common::WriteStream *CSXFile::openForAppend(const Common::String &filename, bool binary) { + error("SXFile::openForAppend - WriteFiles not supported"); +} + +} // end of namespace WinterMute diff --git a/engines/wintermute/base/scriptables/script_ext_file.h b/engines/wintermute/base/scriptables/script_ext_file.h new file mode 100644 index 0000000000..5a6811fe57 --- /dev/null +++ b/engines/wintermute/base/scriptables/script_ext_file.h @@ -0,0 +1,66 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +/* + * This file is based on WME Lite. + * http://dead-code.org/redir.php?target=wmelite + * Copyright (c) 2011 Jan Nedoma + */ + +#ifndef WINTERMUTES_SXFILE_H +#define WINTERMUTES_SXFILE_H + + +#include "engines/wintermute/base/base_scriptable.h" +#include "common/stream.h" + +namespace WinterMute { + +class CBFile; + +class CSXFile : public CBScriptable { +public: + DECLARE_PERSISTENT(CSXFile, CBScriptable) + CScValue *scGetProperty(const char *name); + bool scSetProperty(const char *name, CScValue *value); + bool scCallMethod(CScScript *script, CScStack *stack, CScStack *thisStack, const char *name); + const char *scToString(); + CSXFile(CBGame *inGame, CScStack *Stack); + virtual ~CSXFile(); +private: + Common::SeekableReadStream *_readFile; + Common::WriteStream *_writeFile; + int _mode; // 0..none, 1..read, 2..write, 3..append + bool _textMode; + void close(); + void cleanup(); + uint32 getPos(); + uint32 getLength(); + bool setPos(uint32 Pos, int whence = SEEK_SET); + char *_filename; + Common::WriteStream *openForWrite(const Common::String &filename, bool binary); + Common::WriteStream *openForAppend(const Common::String &filename, bool binary); +}; + +} // end of namespace WinterMute + +#endif diff --git a/engines/wintermute/base/scriptables/script_ext_math.cpp b/engines/wintermute/base/scriptables/script_ext_math.cpp new file mode 100644 index 0000000000..22b08087b7 --- /dev/null +++ b/engines/wintermute/base/scriptables/script_ext_math.cpp @@ -0,0 +1,295 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +/* + * This file is based on WME Lite. + * http://dead-code.org/redir.php?target=wmelite + * Copyright (c) 2011 Jan Nedoma + */ + +#include "engines/wintermute/base/scriptables/script_ext_math.h" +#include "engines/wintermute/base/scriptables/script_stack.h" +#include "engines/wintermute/base/scriptables/script_value.h" +#include "engines/wintermute/persistent.h" +#include "common/math.h" +#include + +namespace WinterMute { + +////////////////////////////////////////////////////////////////////// +// Construction/Destruction +////////////////////////////////////////////////////////////////////// + + +IMPLEMENT_PERSISTENT(CSXMath, true) + +CBScriptable *makeSXMath(CBGame *inGame) { + return new CSXMath(inGame); +} + +////////////////////////////////////////////////////////////////////////// +CSXMath::CSXMath(CBGame *inGame): CBScriptable(inGame) { + +} + + +////////////////////////////////////////////////////////////////////////// +CSXMath::~CSXMath() { + +} + + +////////////////////////////////////////////////////////////////////////// +bool CSXMath::scCallMethod(CScScript *script, CScStack *stack, CScStack *thisStack, const char *name) { + ////////////////////////////////////////////////////////////////////////// + // Abs + ////////////////////////////////////////////////////////////////////////// + if (strcmp(name, "Abs") == 0) { + stack->correctParams(1); + stack->pushFloat(fabs(stack->pop()->getFloat())); + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // Acos + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "Acos") == 0) { + stack->correctParams(1); + stack->pushFloat(acos(stack->pop()->getFloat())); + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // Asin + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "Asin") == 0) { + stack->correctParams(1); + stack->pushFloat(asin(stack->pop()->getFloat())); + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // Atan + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "Atan") == 0) { + stack->correctParams(1); + stack->pushFloat(atan(stack->pop()->getFloat())); + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // Atan2 + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "Atan2") == 0) { + stack->correctParams(2); + double y = stack->pop()->getFloat(); + double x = stack->pop()->getFloat(); + stack->pushFloat(atan2(y, x)); + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // Ceil + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "Ceil") == 0) { + stack->correctParams(1); + stack->pushFloat(ceil(stack->pop()->getFloat())); + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // Cos + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "Cos") == 0) { + stack->correctParams(1); + stack->pushFloat(cos(degreeToRadian(stack->pop()->getFloat()))); + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // Cosh + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "Cosh") == 0) { + stack->correctParams(1); + stack->pushFloat(cosh(degreeToRadian(stack->pop()->getFloat()))); + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // Exp + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "Exp") == 0) { + stack->correctParams(1); + stack->pushFloat(exp(stack->pop()->getFloat())); + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // Floor + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "Floor") == 0) { + stack->correctParams(1); + stack->pushFloat(floor(stack->pop()->getFloat())); + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // Log + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "Log") == 0) { + stack->correctParams(1); + stack->pushFloat(log(stack->pop()->getFloat())); + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // Log10 + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "Log10") == 0) { + stack->correctParams(1); + stack->pushFloat(log10(stack->pop()->getFloat())); + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // Pow + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "Pow") == 0) { + stack->correctParams(2); + double x = stack->pop()->getFloat(); + double y = stack->pop()->getFloat(); + + stack->pushFloat(pow(x, y)); + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // Sin + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "Sin") == 0) { + stack->correctParams(1); + stack->pushFloat(sin(degreeToRadian(stack->pop()->getFloat()))); + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // Sinh + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "Sinh") == 0) { + stack->correctParams(1); + stack->pushFloat(sinh(degreeToRadian(stack->pop()->getFloat()))); + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // Tan + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "Tan") == 0) { + stack->correctParams(1); + stack->pushFloat(tan(degreeToRadian(stack->pop()->getFloat()))); + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // Tanh + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "Tanh") == 0) { + stack->correctParams(1); + stack->pushFloat(tanh(degreeToRadian(stack->pop()->getFloat()))); + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // Sqrt + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "Sqrt") == 0) { + stack->correctParams(1); + stack->pushFloat(sqrt(stack->pop()->getFloat())); + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // DegToRad + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "DegToRad") == 0) { + stack->correctParams(1); + stack->pushFloat(degreeToRadian(stack->pop()->getFloat())); + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // RadToDeg + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "RadToDeg") == 0) { + stack->correctParams(1); + stack->pushFloat(radianToDegree(stack->pop()->getFloat())); + return STATUS_OK; + } + + else return STATUS_FAILED; +} + + +////////////////////////////////////////////////////////////////////////// +CScValue *CSXMath::scGetProperty(const char *name) { + _scValue->setNULL(); + + ////////////////////////////////////////////////////////////////////////// + // Type + ////////////////////////////////////////////////////////////////////////// + if (strcmp(name, "Type") == 0) { + _scValue->setString("math"); + return _scValue; + } + + ////////////////////////////////////////////////////////////////////////// + // PI + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "PI") == 0) { + _scValue->setFloat(M_PI); + return _scValue; + } + + else return _scValue; +} + + +////////////////////////////////////////////////////////////////////////// +double CSXMath::degreeToRadian(double value) { + return value * (M_PI / 180.0f); +} + + +////////////////////////////////////////////////////////////////////////// +double CSXMath::radianToDegree(double value) { + return value * (180.0f / M_PI); +} + + +////////////////////////////////////////////////////////////////////////// +bool CSXMath::persist(CBPersistMgr *persistMgr) { + + CBScriptable::persist(persistMgr); + return STATUS_OK; +} + +} // end of namespace WinterMute diff --git a/engines/wintermute/base/scriptables/script_ext_math.h b/engines/wintermute/base/scriptables/script_ext_math.h new file mode 100644 index 0000000000..422521233f --- /dev/null +++ b/engines/wintermute/base/scriptables/script_ext_math.h @@ -0,0 +1,53 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +/* + * This file is based on WME Lite. + * http://dead-code.org/redir.php?target=wmelite + * Copyright (c) 2011 Jan Nedoma + */ + +#ifndef WINTERMUTE_SXMATH_H +#define WINTERMUTE_SXMATH_H + + +#include "engines/wintermute/base/base_scriptable.h" + +namespace WinterMute { + +class CSXMath : public CBScriptable { +public: + DECLARE_PERSISTENT(CSXMath, CBScriptable) + CSXMath(CBGame *inGame); + virtual ~CSXMath(); + virtual CScValue *scGetProperty(const char *name); + virtual bool scCallMethod(CScScript *script, CScStack *stack, CScStack *thisStack, const char *name); + +private: + double degreeToRadian(double value); + double radianToDegree(double value); + +}; + +} // end of namespace WinterMute + +#endif diff --git a/engines/wintermute/base/scriptables/script_ext_mem_buffer.cpp b/engines/wintermute/base/scriptables/script_ext_mem_buffer.cpp new file mode 100644 index 0000000000..df2bf188ff --- /dev/null +++ b/engines/wintermute/base/scriptables/script_ext_mem_buffer.cpp @@ -0,0 +1,508 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +/* + * This file is based on WME Lite. + * http://dead-code.org/redir.php?target=wmelite + * Copyright (c) 2011 Jan Nedoma + */ + +#include "engines/wintermute/base/base_scriptable.h" +#include "engines/wintermute/base/scriptables/script_stack.h" +#include "engines/wintermute/base/scriptables/script.h" +#include "engines/wintermute/base/scriptables/script_value.h" +#include "engines/wintermute/base/scriptables/script_ext_mem_buffer.h" +#include "common/file.h" + +namespace WinterMute { + +IMPLEMENT_PERSISTENT(CSXMemBuffer, false) + +CBScriptable *makeSXMemBuffer(CBGame *inGame, CScStack *stack) { + return new CSXMemBuffer(inGame, stack); +} + +////////////////////////////////////////////////////////////////////////// +CSXMemBuffer::CSXMemBuffer(CBGame *inGame, CScStack *stack): CBScriptable(inGame) { + stack->correctParams(1); + _buffer = NULL; + _size = 0; + + int NewSize = stack->pop()->getInt(); + resize(MAX(0, NewSize)); +} + +////////////////////////////////////////////////////////////////////////// +CSXMemBuffer::CSXMemBuffer(CBGame *inGame, void *Buffer): CBScriptable(inGame) { + _size = 0; + _buffer = Buffer; +} + + +////////////////////////////////////////////////////////////////////////// +CSXMemBuffer::~CSXMemBuffer() { + cleanup(); +} + +////////////////////////////////////////////////////////////////////////// +void *CSXMemBuffer::scToMemBuffer() { + return _buffer; +} + +////////////////////////////////////////////////////////////////////////// +void CSXMemBuffer::cleanup() { + if (_size) free(_buffer); + _buffer = NULL; + _size = 0; +} + +////////////////////////////////////////////////////////////////////////// +bool CSXMemBuffer::resize(int newSize) { + int oldSize = _size; + + if (_size == 0) { + _buffer = malloc(newSize); + if (_buffer) _size = newSize; + } else { + void *newBuf = realloc(_buffer, newSize); + if (!newBuf) { + if (newSize == 0) { + _buffer = newBuf; + _size = newSize; + } else return STATUS_FAILED; + } else { + _buffer = newBuf; + _size = newSize; + } + } + + if (_buffer && _size > oldSize) { + memset((byte *)_buffer + oldSize, 0, _size - oldSize); + } + return STATUS_OK; +} + +////////////////////////////////////////////////////////////////////////// +bool CSXMemBuffer::checkBounds(CScScript *script, int start, int length) { + if (_buffer == NULL) { + script->runtimeError("Cannot use Set/Get methods on an uninitialized memory buffer"); + return false; + } + if (_size == 0) + return true; + + if (start < 0 || length == 0 || start + length > _size) { + script->runtimeError("Set/Get method call is out of bounds"); + return false; + } else + return true; +} + +////////////////////////////////////////////////////////////////////////// +const char *CSXMemBuffer::scToString() { + return "[membuffer object]"; +} + + +////////////////////////////////////////////////////////////////////////// +bool CSXMemBuffer::scCallMethod(CScScript *script, CScStack *stack, CScStack *thisStack, const char *name) { + ////////////////////////////////////////////////////////////////////////// + // SetSize + ////////////////////////////////////////////////////////////////////////// + if (strcmp(name, "SetSize") == 0) { + stack->correctParams(1); + int newSize = stack->pop()->getInt(); + newSize = MAX(0, newSize); + if (DID_SUCCEED(resize(newSize))) + stack->pushBool(true); + else + stack->pushBool(false); + + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // GetBool + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "GetBool") == 0) { + stack->correctParams(1); + int start = stack->pop()->getInt(); + if (!checkBounds(script, start, sizeof(bool))) + stack->pushNULL(); + else + stack->pushBool(*(bool *)((byte *)_buffer + start)); + + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // GetByte + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "GetByte") == 0) { + stack->correctParams(1); + int start = stack->pop()->getInt(); + if (!checkBounds(script, start, sizeof(byte))) + stack->pushNULL(); + else + stack->pushInt(*(byte *)((byte *)_buffer + start)); + + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // GetShort + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "GetShort") == 0) { + stack->correctParams(1); + int Start = stack->pop()->getInt(); + if (!checkBounds(script, Start, sizeof(short))) + stack->pushNULL(); + else + stack->pushInt(65536 + * (short *)((byte *)_buffer + Start)); + + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // GetInt / GetLong + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "GetInt") == 0 || strcmp(name, "GetLong") == 0) { + stack->correctParams(1); + int start = stack->pop()->getInt(); + if (!checkBounds(script, start, sizeof(int))) + stack->pushNULL(); + else + stack->pushInt(*(int *)((byte *)_buffer + start)); + + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // GetFloat + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "GetFloat") == 0) { + stack->correctParams(1); + int start = stack->pop()->getInt(); + if (!checkBounds(script, start, sizeof(float))) + stack->pushNULL(); + else + stack->pushFloat(*(float *)((byte *)_buffer + start)); + + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // GetDouble + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "GetDouble") == 0) { + stack->correctParams(1); + int start = stack->pop()->getInt(); + if (!checkBounds(script, start, sizeof(double))) + stack->pushNULL(); + else + stack->pushFloat(*(double *)((byte *)_buffer + start)); + + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // GetString + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "GetString") == 0) { + stack->correctParams(2); + int start = stack->pop()->getInt(); + int length = stack->pop()->getInt(); + + // find end of string + if (length == 0 && start >= 0 && start < _size) { + for (int i = start; i < _size; i++) { + if (((char *)_buffer)[i] == '\0') { + length = i - start; + break; + } + } + } + + if (!checkBounds(script, start, length)) + stack->pushNULL(); + else { + char *str = new char[length + 1]; + strncpy(str, (const char *)_buffer + start, length); + str[length] = '\0'; + stack->pushString(str); + delete [] str; + } + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // GetPointer + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "GetPointer") == 0) { + stack->correctParams(1); + int start = stack->pop()->getInt(); + if (!checkBounds(script, start, sizeof(void *))) + stack->pushNULL(); + else { + void *pointer = *(void **)((byte *)_buffer + start); + CSXMemBuffer *buf = new CSXMemBuffer(_gameRef, pointer); + stack->pushNative(buf, false); + } + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // SetBool + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "SetBool") == 0) { + stack->correctParams(2); + int start = stack->pop()->getInt(); + bool val = stack->pop()->getBool(); + + if (!checkBounds(script, start, sizeof(bool))) + stack->pushBool(false); + else { + *(bool *)((byte *)_buffer + start) = val; + stack->pushBool(true); + } + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // SetByte + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "SetByte") == 0) { + stack->correctParams(2); + int start = stack->pop()->getInt(); + byte val = (byte)stack->pop()->getInt(); + + if (!checkBounds(script, start, sizeof(byte))) + stack->pushBool(false); + else { + *(byte *)((byte *)_buffer + start) = val; + stack->pushBool(true); + } + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // SetShort + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "SetShort") == 0) { + stack->correctParams(2); + int start = stack->pop()->getInt(); + short val = (short)stack->pop()->getInt(); + + if (!checkBounds(script, start, sizeof(short))) + stack->pushBool(false); + else { + *(short *)((byte *)_buffer + start) = val; + stack->pushBool(true); + } + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // SetInt / SetLong + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "SetInt") == 0 || strcmp(name, "SetLong") == 0) { + stack->correctParams(2); + int start = stack->pop()->getInt(); + int val = stack->pop()->getInt(); + + if (!checkBounds(script, start, sizeof(int))) + stack->pushBool(false); + else { + *(int *)((byte *)_buffer + start) = val; + stack->pushBool(true); + } + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // SetFloat + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "SetFloat") == 0) { + stack->correctParams(2); + int start = stack->pop()->getInt(); + float val = (float)stack->pop()->getFloat(); + + if (!checkBounds(script, start, sizeof(float))) + stack->pushBool(false); + else { + *(float *)((byte *)_buffer + start) = val; + stack->pushBool(true); + } + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // SetDouble + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "SetDouble") == 0) { + stack->correctParams(2); + int start = stack->pop()->getInt(); + double val = stack->pop()->getFloat(); + + if (!checkBounds(script, start, sizeof(double))) + stack->pushBool(false); + else { + *(double *)((byte *)_buffer + start) = val; + stack->pushBool(true); + } + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // SetString + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "SetString") == 0) { + stack->correctParams(2); + int start = stack->pop()->getInt(); + const char *val = stack->pop()->getString(); + + if (!checkBounds(script, start, strlen(val) + 1)) + stack->pushBool(false); + else { + memcpy((byte *)_buffer + start, val, strlen(val) + 1); + stack->pushBool(true); + } + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // SetPointer + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "SetPointer") == 0) { + stack->correctParams(2); + int start = stack->pop()->getInt(); + /* CScValue *Val = */ stack->pop(); + + if (!checkBounds(script, start, sizeof(void *))) + stack->pushBool(false); + else { + /* + int Pointer = (int)Val->getMemBuffer(); + memcpy((byte *)_buffer+Start, &Pointer, sizeof(void*)); + stack->pushBool(true); + */ + // TODO fix + stack->pushBool(false); + + } + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // DEBUG_Dump + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "DEBUG_Dump") == 0) { + stack->correctParams(0); + if (_buffer && _size) { + warning("SXMemBuffer::ScCallMethod - DEBUG_Dump"); + Common::DumpFile f; + f.open("buffer.bin"); + f.write(_buffer, _size); + f.close(); + } + stack->pushNULL(); + return STATUS_OK; + } + + else return STATUS_FAILED; +} + + +////////////////////////////////////////////////////////////////////////// +CScValue *CSXMemBuffer::scGetProperty(const char *name) { + _scValue->setNULL(); + + ////////////////////////////////////////////////////////////////////////// + // Type (RO) + ////////////////////////////////////////////////////////////////////////// + if (strcmp(name, "Type") == 0) { + _scValue->setString("membuffer"); + return _scValue; + } + + ////////////////////////////////////////////////////////////////////////// + // Size (RO) + ////////////////////////////////////////////////////////////////////////// + if (strcmp(name, "Size") == 0) { + _scValue->setInt(_size); + return _scValue; + } + + else return CBScriptable::scGetProperty(name); +} + + +////////////////////////////////////////////////////////////////////////// +bool CSXMemBuffer::scSetProperty(const char *name, CScValue *value) { + /* + ////////////////////////////////////////////////////////////////////////// + // Length + ////////////////////////////////////////////////////////////////////////// + if(strcmp(name, "Length")==0){ + int OrigLength = _length; + _length = max(value->getInt(0), 0); + + char PropName[20]; + if(_length < OrigLength){ + for(int i=_length; iDeleteProp(PropName); + } + } + return STATUS_OK; + } + else*/ return CBScriptable::scSetProperty(name, value); +} + + +////////////////////////////////////////////////////////////////////////// +bool CSXMemBuffer::persist(CBPersistMgr *persistMgr) { + + CBScriptable::persist(persistMgr); + + persistMgr->transfer(TMEMBER(_size)); + + if (persistMgr->_saving) { + if (_size > 0) persistMgr->putBytes((byte *)_buffer, _size); + } else { + if (_size > 0) { + _buffer = malloc(_size); + persistMgr->getBytes((byte *)_buffer, _size); + } else _buffer = NULL; + } + + return STATUS_OK; +} + + +////////////////////////////////////////////////////////////////////////// +int CSXMemBuffer::scCompare(CBScriptable *val) { + if (_buffer == val->scToMemBuffer()) return 0; + else return 1; +} + +} // end of namespace WinterMute diff --git a/engines/wintermute/base/scriptables/script_ext_mem_buffer.h b/engines/wintermute/base/scriptables/script_ext_mem_buffer.h new file mode 100644 index 0000000000..a9d78e50e4 --- /dev/null +++ b/engines/wintermute/base/scriptables/script_ext_mem_buffer.h @@ -0,0 +1,59 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +/* + * This file is based on WME Lite. + * http://dead-code.org/redir.php?target=wmelite + * Copyright (c) 2011 Jan Nedoma + */ + +#ifndef WINTERMUTE_SXMEMBUFFER_H +#define WINTERMUTE_SXMEMBUFFER_H + + +#include "engines/wintermute/base/base_scriptable.h" + +namespace WinterMute { + +class CSXMemBuffer : public CBScriptable { +public: + virtual int scCompare(CBScriptable *Val); + DECLARE_PERSISTENT(CSXMemBuffer, CBScriptable) + CScValue *scGetProperty(const char *name); + bool scSetProperty(const char *name, CScValue *value); + bool scCallMethod(CScScript *script, CScStack *stack, CScStack *thisStack, const char *name); + const char *scToString(); + CSXMemBuffer(CBGame *inGame, CScStack *stack); + CSXMemBuffer(CBGame *inGame, void *buffer); + virtual ~CSXMemBuffer(); + virtual void *scToMemBuffer(); + int _size; +private: + bool resize(int newSize); + void *_buffer; + void cleanup(); + bool checkBounds(CScScript *script, int start, int length); +}; + +} // end of namespace WinterMute + +#endif diff --git a/engines/wintermute/base/scriptables/script_ext_object.cpp b/engines/wintermute/base/scriptables/script_ext_object.cpp new file mode 100644 index 0000000000..cb0d32d1a3 --- /dev/null +++ b/engines/wintermute/base/scriptables/script_ext_object.cpp @@ -0,0 +1,67 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +/* + * This file is based on WME Lite. + * http://dead-code.org/redir.php?target=wmelite + * Copyright (c) 2011 Jan Nedoma + */ + +#include "engines/wintermute/base/scriptables/script_ext_object.h" +#include "engines/wintermute/base/scriptables/script_value.h" +#include "engines/wintermute/base/scriptables/script_stack.h" + +namespace WinterMute { + +////////////////////////////////////////////////////////////////////// +// Construction/Destruction +////////////////////////////////////////////////////////////////////// + +IMPLEMENT_PERSISTENT(CSXObject, false) + +CBScriptable *makeSXObject(CBGame *inGame, CScStack *stack) { + return new CSXObject(inGame, stack); +} + +////////////////////////////////////////////////////////////////////////// +CSXObject::CSXObject(CBGame *inGame, CScStack *stack): CBObject(inGame) { + int numParams = stack->pop()->getInt(0); + for (int i = 0; i < numParams; i++) { + addScript(stack->pop()->getString()); + } +} + + +////////////////////////////////////////////////////////////////////////// +CSXObject::~CSXObject() { + +} + + +////////////////////////////////////////////////////////////////////////// +bool CSXObject::persist(CBPersistMgr *persistMgr) { + CBObject::persist(persistMgr); + + return STATUS_OK; +} + +} // end of namespace WinterMute diff --git a/engines/wintermute/base/scriptables/script_ext_object.h b/engines/wintermute/base/scriptables/script_ext_object.h new file mode 100644 index 0000000000..b4e869d5b3 --- /dev/null +++ b/engines/wintermute/base/scriptables/script_ext_object.h @@ -0,0 +1,47 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +/* + * This file is based on WME Lite. + * http://dead-code.org/redir.php?target=wmelite + * Copyright (c) 2011 Jan Nedoma + */ + +#ifndef WINTERMUTE_SXOBJECT_H +#define WINTERMUTE_SXOBJECT_H + + +#include "engines/wintermute/base/base_object.h" + +namespace WinterMute { + +class CSXObject : public CBObject { +public: + DECLARE_PERSISTENT(CSXObject, CBObject) + CSXObject(CBGame *inGame, CScStack *Stack); + virtual ~CSXObject(); + +}; + +} // end of namespace WinterMute + +#endif diff --git a/engines/wintermute/base/scriptables/script_ext_string.cpp b/engines/wintermute/base/scriptables/script_ext_string.cpp new file mode 100644 index 0000000000..bd7541fadd --- /dev/null +++ b/engines/wintermute/base/scriptables/script_ext_string.cpp @@ -0,0 +1,404 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +/* + * This file is based on WME Lite. + * http://dead-code.org/redir.php?target=wmelite + * Copyright (c) 2011 Jan Nedoma + */ + +#include "engines/wintermute/base/base_game.h" +#include "engines/wintermute/base/scriptables/script_stack.h" +#include "engines/wintermute/base/scriptables/script_value.h" +#include "engines/wintermute/utils/utils.h" +#include "engines/wintermute/base/scriptables/script_ext_string.h" +#include "engines/wintermute/base/scriptables/script_ext_array.h" +#include "engines/wintermute/utils/string_util.h" +#include "common/tokenizer.h" + +namespace WinterMute { + +IMPLEMENT_PERSISTENT(CSXString, false) + +CBScriptable *makeSXString(CBGame *inGame, CScStack *stack) { + return new CSXString(inGame, stack); +} + +////////////////////////////////////////////////////////////////////////// +CSXString::CSXString(CBGame *inGame, CScStack *stack): CBScriptable(inGame) { + _string = NULL; + _capacity = 0; + + stack->correctParams(1); + CScValue *val = stack->pop(); + + if (val->isInt()) { + _capacity = MAX(0, val->getInt()); + if (_capacity > 0) { + _string = new char[_capacity]; + memset(_string, 0, _capacity); + } + } else { + setStringVal(val->getString()); + } + + if (_capacity == 0) setStringVal(""); +} + + +////////////////////////////////////////////////////////////////////////// +CSXString::~CSXString() { + if (_string) delete [] _string; +} + + +////////////////////////////////////////////////////////////////////////// +void CSXString::setStringVal(const char *val) { + int len = strlen(val); + if (len >= _capacity) { + _capacity = len + 1; + delete[] _string; + _string = NULL; + _string = new char[_capacity]; + memset(_string, 0, _capacity); + } + strcpy(_string, val); +} + + +////////////////////////////////////////////////////////////////////////// +const char *CSXString::scToString() { + if (_string) return _string; + else return "[null string]"; +} + + +////////////////////////////////////////////////////////////////////////// +void CSXString::scSetString(const char *val) { + setStringVal(val); +} + + +////////////////////////////////////////////////////////////////////////// +bool CSXString::scCallMethod(CScScript *script, CScStack *stack, CScStack *thisStack, const char *name) { + ////////////////////////////////////////////////////////////////////////// + // Substring + ////////////////////////////////////////////////////////////////////////// + if (strcmp(name, "Substring") == 0) { + stack->correctParams(2); + int start = stack->pop()->getInt(); + int end = stack->pop()->getInt(); + + if (end < start) CBUtils::swap(&start, &end); + + //try { + WideString str; + if (_gameRef->_textEncoding == TEXT_UTF8) + str = StringUtil::utf8ToWide(_string); + else + str = StringUtil::ansiToWide(_string); + + //WideString subStr = str.substr(start, end - start + 1); + WideString subStr(str.c_str() + start, end - start + 1); + + if (_gameRef->_textEncoding == TEXT_UTF8) + stack->pushString(StringUtil::wideToUtf8(subStr).c_str()); + else + stack->pushString(StringUtil::wideToAnsi(subStr).c_str()); + // } catch (std::exception &) { + // stack->pushNULL(); + // } + + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // Substr + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "Substr") == 0) { + stack->correctParams(2); + int start = stack->pop()->getInt(); + + CScValue *val = stack->pop(); + int len = val->getInt(); + + if (!val->isNULL() && len <= 0) { + stack->pushString(""); + return STATUS_OK; + } + + if (val->isNULL()) len = strlen(_string) - start; + +// try { + WideString str; + if (_gameRef->_textEncoding == TEXT_UTF8) + str = StringUtil::utf8ToWide(_string); + else + str = StringUtil::ansiToWide(_string); + +// WideString subStr = str.substr(start, len); + WideString subStr(str.c_str() + start, len); + + if (_gameRef->_textEncoding == TEXT_UTF8) + stack->pushString(StringUtil::wideToUtf8(subStr).c_str()); + else + stack->pushString(StringUtil::wideToAnsi(subStr).c_str()); +// } catch (std::exception &) { +// stack->pushNULL(); +// } + + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // ToUpperCase + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "ToUpperCase") == 0) { + stack->correctParams(0); + + WideString str; + if (_gameRef->_textEncoding == TEXT_UTF8) + str = StringUtil::utf8ToWide(_string); + else + str = StringUtil::ansiToWide(_string); + + str.toUppercase(); + + if (_gameRef->_textEncoding == TEXT_UTF8) + stack->pushString(StringUtil::wideToUtf8(str).c_str()); + else + stack->pushString(StringUtil::wideToAnsi(str).c_str()); + + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // ToLowerCase + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "ToLowerCase") == 0) { + stack->correctParams(0); + + WideString str; + if (_gameRef->_textEncoding == TEXT_UTF8) + str = StringUtil::utf8ToWide(_string); + else + str = StringUtil::ansiToWide(_string); + + str.toLowercase(); + + if (_gameRef->_textEncoding == TEXT_UTF8) + stack->pushString(StringUtil::wideToUtf8(str).c_str()); + else + stack->pushString(StringUtil::wideToAnsi(str).c_str()); + + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // IndexOf + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "IndexOf") == 0) { + stack->correctParams(2); + + const char *strToFind = stack->pop()->getString(); + int index = stack->pop()->getInt(); + + WideString str; + if (_gameRef->_textEncoding == TEXT_UTF8) + str = StringUtil::utf8ToWide(_string); + else + str = StringUtil::ansiToWide(_string); + + WideString toFind; + if (_gameRef->_textEncoding == TEXT_UTF8) + toFind = StringUtil::utf8ToWide(strToFind); + else + toFind = StringUtil::ansiToWide(strToFind); + + int indexOf = StringUtil::indexOf(str, toFind, index); + stack->pushInt(indexOf); + + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // Split + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "Split") == 0) { + stack->correctParams(1); + CScValue *val = stack->pop(); + char separators[MAX_PATH_LENGTH] = ","; + if (!val->isNULL()) strcpy(separators, val->getString()); + + CSXArray *array = new CSXArray(_gameRef); + if (!array) { + stack->pushNULL(); + return STATUS_OK; + } + + + WideString str; + if (_gameRef->_textEncoding == TEXT_UTF8) + str = StringUtil::utf8ToWide(_string); + else + str = StringUtil::ansiToWide(_string); + + WideString delims; + if (_gameRef->_textEncoding == TEXT_UTF8) + delims = StringUtil::utf8ToWide(separators); + else + delims = StringUtil::ansiToWide(separators); + + Common::Array parts; + + + + Common::StringTokenizer tokenizer(str, delims); + while (!tokenizer.empty()) { + Common::String str2 = tokenizer.nextToken(); + parts.push_back(str2); + } + // TODO: Clean this up + /*do { + pos = StringUtil::IndexOf(Common::String(str.c_str() + start), delims, start); + //pos = str.find_first_of(delims, start); + if (pos == start) { + start = pos + 1; + } else if (pos == str.size()) { + parts.push_back(Common::String(str.c_str() + start)); + break; + } else { + parts.push_back(Common::String(str.c_str() + start, pos - start)); + start = pos + 1; + } + //start = str.find_first_not_of(delims, start); + start = StringUtil::LastIndexOf(Common::String(str.c_str() + start), delims, start) + 1; + + } while (pos != str.size());*/ + + for (Common::Array::iterator it = parts.begin(); it != parts.end(); ++it) { + WideString &part = (*it); + + if (_gameRef->_textEncoding == TEXT_UTF8) + val = new CScValue(_gameRef, StringUtil::wideToUtf8(part).c_str()); + else + val = new CScValue(_gameRef, StringUtil::wideToAnsi(part).c_str()); + + array->push(val); + delete val; + val = NULL; + } + + stack->pushNative(array, false); + return STATUS_OK; + } + + else return STATUS_FAILED; +} + + +////////////////////////////////////////////////////////////////////////// +CScValue *CSXString::scGetProperty(const char *name) { + _scValue->setNULL(); + + ////////////////////////////////////////////////////////////////////////// + // Type (RO) + ////////////////////////////////////////////////////////////////////////// + if (strcmp(name, "Type") == 0) { + _scValue->setString("string"); + return _scValue; + } + ////////////////////////////////////////////////////////////////////////// + // Length (RO) + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "Length") == 0) { + if (_gameRef->_textEncoding == TEXT_UTF8) { + WideString wstr = StringUtil::utf8ToWide(_string); + _scValue->setInt(wstr.size()); + } else + _scValue->setInt(strlen(_string)); + + return _scValue; + } + ////////////////////////////////////////////////////////////////////////// + // Capacity + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "Capacity") == 0) { + _scValue->setInt(_capacity); + return _scValue; + } + + else return _scValue; +} + + +////////////////////////////////////////////////////////////////////////// +bool CSXString::scSetProperty(const char *name, CScValue *value) { + ////////////////////////////////////////////////////////////////////////// + // Capacity + ////////////////////////////////////////////////////////////////////////// + if (strcmp(name, "Capacity") == 0) { + int32 newCap = (uint32)value->getInt(); + if (newCap < (int32)(strlen(_string) + 1)) _gameRef->LOG(0, "Warning: cannot lower string capacity"); + else if (newCap != _capacity) { + char *newStr = new char[newCap]; + if (newStr) { + memset(newStr, 0, newCap); + strcpy(newStr, _string); + delete[] _string; + _string = newStr; + _capacity = newCap; + } + } + return STATUS_OK; + } + + else return STATUS_FAILED; +} + + +////////////////////////////////////////////////////////////////////////// +bool CSXString::persist(CBPersistMgr *persistMgr) { + + CBScriptable::persist(persistMgr); + + persistMgr->transfer(TMEMBER(_capacity)); + + if (persistMgr->_saving) { + if (_capacity > 0) persistMgr->putBytes((byte *)_string, _capacity); + } else { + if (_capacity > 0) { + _string = new char[_capacity]; + persistMgr->getBytes((byte *)_string, _capacity); + } else _string = NULL; + } + + return STATUS_OK; +} + + +////////////////////////////////////////////////////////////////////////// +int CSXString::scCompare(CBScriptable *val) { + return strcmp(_string, ((CSXString *)val)->_string); +} + +} // end of namespace WinterMute diff --git a/engines/wintermute/base/scriptables/script_ext_string.h b/engines/wintermute/base/scriptables/script_ext_string.h new file mode 100644 index 0000000000..52a1524dde --- /dev/null +++ b/engines/wintermute/base/scriptables/script_ext_string.h @@ -0,0 +1,58 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +/* + * This file is based on WME Lite. + * http://dead-code.org/redir.php?target=wmelite + * Copyright (c) 2011 Jan Nedoma + */ + +#ifndef WINTERMUTE_SXSTRING_H +#define WINTERMUTE_SXSTRING_H + + +#include "engines/wintermute/base/base_scriptable.h" + +namespace WinterMute { + +class CSXString : public CBScriptable { +public: + virtual int scCompare(CBScriptable *Val); + DECLARE_PERSISTENT(CSXString, CBScriptable) + CScValue *scGetProperty(const char *name); + bool scSetProperty(const char *name, CScValue *value); + bool scCallMethod(CScScript *script, CScStack *stack, CScStack *thisStack, const char *name); + void scSetString(const char *val); + const char *scToString(); + void setStringVal(const char *val); + + CSXString(CBGame *inGame, CScStack *Stack); + virtual ~CSXString(); + +private: + char *_string; + int _capacity; +}; + +} // end of namespace WinterMute + +#endif diff --git a/engines/wintermute/base/scriptables/script_stack.cpp b/engines/wintermute/base/scriptables/script_stack.cpp new file mode 100644 index 0000000000..188cb2d15c --- /dev/null +++ b/engines/wintermute/base/scriptables/script_stack.cpp @@ -0,0 +1,226 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +/* + * This file is based on WME Lite. + * http://dead-code.org/redir.php?target=wmelite + * Copyright (c) 2011 Jan Nedoma + */ + +#include "engines/wintermute/base/scriptables/script_stack.h" +#include "engines/wintermute/base/scriptables/script_value.h" +#include "engines/wintermute/base/base_game.h" + +namespace WinterMute { + +IMPLEMENT_PERSISTENT(CScStack, false) + +////////////////////////////////////////////////////////////////////////// +CScStack::CScStack(CBGame *inGame): CBBase(inGame) { + _sP = -1; +} + + +////////////////////////////////////////////////////////////////////////// +CScStack::~CScStack() { + +#if _DEBUG + //_gameRef->LOG(0, "STAT: Stack size: %d, SP=%d", _values.getSize(), _sP); +#endif + + for (int i = 0; i < _values.getSize(); i++) { + delete _values[i]; + } + _values.removeAll(); +} + + +////////////////////////////////////////////////////////////////////////// +CScValue *CScStack::pop() { + if (_sP < 0) { + _gameRef->LOG(0, "Fatal: Stack underflow"); + return NULL; + } + + return _values[_sP--]; +} + + +////////////////////////////////////////////////////////////////////////// +void CScStack::push(CScValue *val) { + _sP++; + + if (_sP < _values.getSize()) { + _values[_sP]->cleanup(); + _values[_sP]->copy(val); + } else { + CScValue *copyVal = new CScValue(_gameRef); + copyVal->copy(val); + _values.add(copyVal); + } +} + + +////////////////////////////////////////////////////////////////////////// +CScValue *CScStack::getPushValue() { + _sP++; + + if (_sP >= _values.getSize()) { + CScValue *val = new CScValue(_gameRef); + _values.add(val); + } + _values[_sP]->cleanup(); + return _values[_sP]; +} + + + +////////////////////////////////////////////////////////////////////////// +CScValue *CScStack::getTop() { + if (_sP < 0 || _sP >= _values.getSize()) return NULL; + else return _values[_sP]; +} + + +////////////////////////////////////////////////////////////////////////// +CScValue *CScStack::getAt(int index) { + index = _sP - index; + if (index < 0 || index >= _values.getSize()) return NULL; + else return _values[index]; +} + + +////////////////////////////////////////////////////////////////////////// +void CScStack::correctParams(uint32 expectedParams) { + uint32 nuParams = (uint32)pop()->getInt(); + + if (expectedParams < nuParams) { // too many params + while (expectedParams < nuParams) { + //Pop(); + delete _values[_sP - expectedParams]; + _values.removeAt(_sP - expectedParams); + nuParams--; + _sP--; + } + } else if (expectedParams > nuParams) { // need more params + while (expectedParams > nuParams) { + //Push(null_val); + CScValue *nullVal = new CScValue(_gameRef); + nullVal->setNULL(); + _values.insertAt(_sP - nuParams + 1, nullVal); + nuParams++; + _sP++; + + if (_values.getSize() > _sP + 1) { + delete _values[_values.getSize() - 1]; + _values.removeAt(_values.getSize() - 1); + } + } + } +} + + +////////////////////////////////////////////////////////////////////////// +void CScStack::pushNULL() { + /* + CScValue* val = new CScValue(_gameRef); + val->setNULL(); + Push(val); + delete val; + */ + getPushValue()->setNULL(); +} + + +////////////////////////////////////////////////////////////////////////// +void CScStack::pushInt(int val) { + /* + CScValue* val = new CScValue(_gameRef); + val->setInt(Val); + Push(val); + delete val; + */ + getPushValue()->setInt(val); +} + + +////////////////////////////////////////////////////////////////////////// +void CScStack::pushFloat(double val) { + /* + CScValue* val = new CScValue(_gameRef); + val->setFloat(Val); + Push(val); + delete val; + */ + getPushValue()->setFloat(val); +} + + +////////////////////////////////////////////////////////////////////////// +void CScStack::pushBool(bool val) { + /* + CScValue* val = new CScValue(_gameRef); + val->setBool(Val); + Push(val); + delete val; + */ + getPushValue()->setBool(val); +} + + +////////////////////////////////////////////////////////////////////////// +void CScStack::pushString(const char *val) { + /* + CScValue* val = new CScValue(_gameRef); + val->setString(Val); + Push(val); + delete val; + */ + getPushValue()->setString(val); +} + + +////////////////////////////////////////////////////////////////////////// +void CScStack::pushNative(CBScriptable *val, bool persistent) { + /* + CScValue* val = new CScValue(_gameRef); + val->setNative(Val, Persistent); + Push(val); + delete val; + */ + + getPushValue()->setNative(val, persistent); +} + + +////////////////////////////////////////////////////////////////////////// +bool CScStack::persist(CBPersistMgr *persistMgr) { + + persistMgr->transfer(TMEMBER(_gameRef)); + + persistMgr->transfer(TMEMBER(_sP)); + _values.persist(persistMgr); + + return STATUS_OK; +} + +} // end of namespace WinterMute diff --git a/engines/wintermute/base/scriptables/script_stack.h b/engines/wintermute/base/scriptables/script_stack.h new file mode 100644 index 0000000000..6460f901fe --- /dev/null +++ b/engines/wintermute/base/scriptables/script_stack.h @@ -0,0 +1,66 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +/* + * This file is based on WME Lite. + * http://dead-code.org/redir.php?target=wmelite + * Copyright (c) 2011 Jan Nedoma + */ + +#ifndef WINTERMUTE_SCSTACK_H +#define WINTERMUTE_SCSTACK_H + + +#include "engines/wintermute/base/base.h" +#include "engines/wintermute/coll_templ.h" +#include "engines/wintermute/persistent.h" + +namespace WinterMute { + +class CScValue; +class CBScriptable; + +class CScStack : public CBBase { +public: + CScValue *getAt(int Index); + CScValue *getPushValue(); + DECLARE_PERSISTENT(CScStack, CBBase) + void pushNative(CBScriptable *val, bool persistent); + void pushString(const char *val); + void pushBool(bool val); + void pushInt(int val); + void pushFloat(double val); + void pushNULL(); + void correctParams(uint32 expectedParams); + CScValue *getTop(); + void push(CScValue *val); + CScValue *pop(); + CScStack(CBGame *inGame); + virtual ~CScStack(); + CBArray _values; + int _sP; + +}; + +} // end of namespace WinterMute + +#endif diff --git a/engines/wintermute/base/scriptables/script_value.cpp b/engines/wintermute/base/scriptables/script_value.cpp new file mode 100644 index 0000000000..da47ed299f --- /dev/null +++ b/engines/wintermute/base/scriptables/script_value.cpp @@ -0,0 +1,1054 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +/* + * This file is based on WME Lite. + * http://dead-code.org/redir.php?target=wmelite + * Copyright (c) 2011 Jan Nedoma + */ + +#include "engines/wintermute/platform_osystem.h" +#include "engines/wintermute/base/base_dynamic_buffer.h" +#include "engines/wintermute/base/base_game.h" +#include "engines/wintermute/base/scriptables/script_value.h" +#include "engines/wintermute/base/scriptables/script.h" +#include "engines/wintermute/utils/string_util.h" +#include "engines/wintermute/base/base_scriptable.h" + +namespace WinterMute { + +////////////////////////////////////////////////////////////////////// +// Construction/Destruction +////////////////////////////////////////////////////////////////////// + +IMPLEMENT_PERSISTENT(CScValue, false) + +////////////////////////////////////////////////////////////////////////// +CScValue::CScValue(CBGame *inGame): CBBase(inGame) { + _type = VAL_NULL; + + _valBool = false; + _valInt = 0; + _valFloat = 0.0f; + _valNative = NULL; + _valString = NULL; + _valRef = NULL; + _persistent = false; + _isConstVar = false; +} + + +////////////////////////////////////////////////////////////////////////// +CScValue::CScValue(CBGame *inGame, bool val): CBBase(inGame) { + _type = VAL_BOOL; + _valBool = val; + + _valInt = 0; + _valFloat = 0.0f; + _valNative = NULL; + _valString = NULL; + _valRef = NULL; + _persistent = false; + _isConstVar = false; +} + + +////////////////////////////////////////////////////////////////////////// +CScValue::CScValue(CBGame *inGame, int val): CBBase(inGame) { + _type = VAL_INT; + _valInt = val; + + _valFloat = 0.0f; + _valBool = false; + _valNative = NULL; + _valString = NULL; + _valRef = NULL; + _persistent = false; + _isConstVar = false; +} + + +////////////////////////////////////////////////////////////////////////// +CScValue::CScValue(CBGame *inGame, double val): CBBase(inGame) { + _type = VAL_FLOAT; + _valFloat = val; + + _valInt = 0; + _valBool = false; + _valNative = NULL; + _valString = NULL; + _valRef = NULL; + _persistent = false; + _isConstVar = false; +} + + +////////////////////////////////////////////////////////////////////////// +CScValue::CScValue(CBGame *inGame, const char *val): CBBase(inGame) { + _type = VAL_STRING; + _valString = NULL; + setStringVal(val); + + _valBool = false; + _valInt = 0; + _valFloat = 0.0f; + _valNative = NULL; + _valRef = NULL; + _persistent = false; + _isConstVar = false; +} + + +////////////////////////////////////////////////////////////////////////// +void CScValue::cleanup(bool ignoreNatives) { + deleteProps(); + + if (_valString) delete [] _valString; + + if (!ignoreNatives) { + if (_valNative && !_persistent) { + _valNative->_refCount--; + if (_valNative->_refCount <= 0) { + delete _valNative; + _valNative = NULL; + } + } + } + + + _type = VAL_NULL; + + _valBool = false; + _valInt = 0; + _valFloat = 0.0f; + _valNative = NULL; + _valString = NULL; + _valRef = NULL; + _persistent = false; + _isConstVar = false; +} + + + +////////////////////////////////////////////////////////////////////////// +CScValue::~CScValue() { + cleanup(); +} + + +////////////////////////////////////////////////////////////////////////// +CScValue *CScValue::getProp(const char *name) { + if (_type == VAL_VARIABLE_REF) return _valRef->getProp(name); + + if (_type == VAL_STRING && strcmp(name, "Length") == 0) { + _gameRef->_scValue->_type = VAL_INT; + +#if 0 // TODO: Remove FreeType-dependency + if (_gameRef->_textEncoding == TEXT_ANSI) { +#else + if (true) { +#endif + _gameRef->_scValue->setInt(strlen(_valString)); + } else { + WideString wstr = StringUtil::utf8ToWide(_valString); + _gameRef->_scValue->setInt(wstr.size()); + } + + return _gameRef->_scValue; + } + + CScValue *ret = NULL; + + if (_type == VAL_NATIVE && _valNative) ret = _valNative->scGetProperty(name); + + if (ret == NULL) { + _valIter = _valObject.find(name); + if (_valIter != _valObject.end()) ret = _valIter->_value; + } + return ret; +} + +////////////////////////////////////////////////////////////////////////// +bool CScValue::deleteProp(const char *name) { + if (_type == VAL_VARIABLE_REF) return _valRef->deleteProp(name); + + _valIter = _valObject.find(name); + if (_valIter != _valObject.end()) { + delete _valIter->_value; + _valIter->_value = NULL; + } + + return STATUS_OK; +} + + + +////////////////////////////////////////////////////////////////////////// +bool CScValue::setProp(const char *name, CScValue *val, bool copyWhole, bool setAsConst) { + if (_type == VAL_VARIABLE_REF) + return _valRef->setProp(name, val); + + bool ret = STATUS_FAILED; + if (_type == VAL_NATIVE && _valNative) { + ret = _valNative->scSetProperty(name, val); + } + + if (DID_FAIL(ret)) { + CScValue *newVal = NULL; + + _valIter = _valObject.find(name); + if (_valIter != _valObject.end()) { + newVal = _valIter->_value; + } + if (!newVal) + newVal = new CScValue(_gameRef); + else newVal->cleanup(); + + newVal->copy(val, copyWhole); + newVal->_isConstVar = setAsConst; + _valObject[name] = newVal; + + if (_type != VAL_NATIVE) _type = VAL_OBJECT; + + /* + _valIter = _valObject.find(Name); + if (_valIter != _valObject.end()){ + delete _valIter->_value; + _valIter->_value = NULL; + } + CScValue* val = new CScValue(_gameRef); + val->Copy(Val, CopyWhole); + val->_isConstVar = SetAsConst; + _valObject[Name] = val; + + if(_type!=VAL_NATIVE) _type = VAL_OBJECT; + */ + } + + return STATUS_OK; +} + + +////////////////////////////////////////////////////////////////////////// +bool CScValue::propExists(const char *name) { + if (_type == VAL_VARIABLE_REF) + return _valRef->propExists(name); + _valIter = _valObject.find(name); + + return (_valIter != _valObject.end()); +} + + +////////////////////////////////////////////////////////////////////////// +void CScValue::deleteProps() { + _valIter = _valObject.begin(); + while (_valIter != _valObject.end()) { + delete(CScValue *)_valIter->_value; + _valIter++; + } + _valObject.clear(); +} + + +////////////////////////////////////////////////////////////////////////// +void CScValue::CleanProps(bool includingNatives) { + _valIter = _valObject.begin(); + while (_valIter != _valObject.end()) { + if (!_valIter->_value->_isConstVar && (!_valIter->_value->isNative() || includingNatives)) _valIter->_value->setNULL(); + _valIter++; + } +} + +////////////////////////////////////////////////////////////////////////// +bool CScValue::isNULL() { + if (_type == VAL_VARIABLE_REF) + return _valRef->isNULL(); + + return (_type == VAL_NULL); +} + + +////////////////////////////////////////////////////////////////////////// +bool CScValue::isNative() { + if (_type == VAL_VARIABLE_REF) + return _valRef->isNative(); + + return (_type == VAL_NATIVE); +} + + +////////////////////////////////////////////////////////////////////////// +bool CScValue::isString() { + if (_type == VAL_VARIABLE_REF) + return _valRef->isString(); + + return (_type == VAL_STRING); +} + + +////////////////////////////////////////////////////////////////////////// +bool CScValue::isFloat() { + if (_type == VAL_VARIABLE_REF) + return _valRef->isFloat(); + + return (_type == VAL_FLOAT); +} + + +////////////////////////////////////////////////////////////////////////// +bool CScValue::isInt() { + if (_type == VAL_VARIABLE_REF) + return _valRef->isInt(); + + return (_type == VAL_INT); +} + + +////////////////////////////////////////////////////////////////////////// +bool CScValue::isBool() { + if (_type == VAL_VARIABLE_REF) + return _valRef->isBool(); + + return (_type == VAL_BOOL); +} + + +////////////////////////////////////////////////////////////////////////// +bool CScValue::isObject() { + if (_type == VAL_VARIABLE_REF) + return _valRef->isObject(); + + return (_type == VAL_OBJECT); +} + + +////////////////////////////////////////////////////////////////////////// +TValType CScValue::getTypeTolerant() { + if (_type == VAL_VARIABLE_REF) + return _valRef->getType(); + + return _type; +} + + +////////////////////////////////////////////////////////////////////////// +void CScValue::setBool(bool val) { + if (_type == VAL_VARIABLE_REF) { + _valRef->setBool(val); + return; + } + + if (_type == VAL_NATIVE) { + _valNative->scSetBool(val); + return; + } + + _valBool = val; + _type = VAL_BOOL; +} + + +////////////////////////////////////////////////////////////////////////// +void CScValue::setInt(int val) { + if (_type == VAL_VARIABLE_REF) { + _valRef->setInt(val); + return; + } + + if (_type == VAL_NATIVE) { + _valNative->scSetInt(val); + return; + } + + _valInt = val; + _type = VAL_INT; +} + + +////////////////////////////////////////////////////////////////////////// +void CScValue::setFloat(double val) { + if (_type == VAL_VARIABLE_REF) { + _valRef->setFloat(val); + return; + } + + if (_type == VAL_NATIVE) { + _valNative->scSetFloat(val); + return; + } + + _valFloat = val; + _type = VAL_FLOAT; +} + + +////////////////////////////////////////////////////////////////////////// +void CScValue::setString(const char *val) { + if (_type == VAL_VARIABLE_REF) { + _valRef->setString(val); + return; + } + + if (_type == VAL_NATIVE) { + _valNative->scSetString(val); + return; + } + + setStringVal(val); + if (_valString) _type = VAL_STRING; + else _type = VAL_NULL; +} + +void CScValue::setString(const Common::String &val) { + setString(val.c_str()); +} + +////////////////////////////////////////////////////////////////////////// +void CScValue::setStringVal(const char *val) { + if (_valString) { + delete [] _valString; + _valString = NULL; + } + + if (val == NULL) { + _valString = NULL; + return; + } + + _valString = new char [strlen(val) + 1]; + if (_valString) { + strcpy(_valString, val); + } +} + + +////////////////////////////////////////////////////////////////////////// +void CScValue::setNULL() { + if (_type == VAL_VARIABLE_REF) { + _valRef->setNULL(); + return; + } + + if (_valNative && !_persistent) { + _valNative->_refCount--; + if (_valNative->_refCount <= 0) delete _valNative; + } + _valNative = NULL; + deleteProps(); + + _type = VAL_NULL; +} + + +////////////////////////////////////////////////////////////////////////// +void CScValue::setNative(CBScriptable *val, bool persistent) { + if (_type == VAL_VARIABLE_REF) { + _valRef->setNative(val, persistent); + return; + } + + if (val == NULL) { + setNULL(); + } else { + if (_valNative && !_persistent) { + _valNative->_refCount--; + if (_valNative->_refCount <= 0) { + if (_valNative != val) delete _valNative; + _valNative = NULL; + } + } + + _type = VAL_NATIVE; + _persistent = persistent; + + _valNative = val; + if (_valNative && !_persistent) _valNative->_refCount++; + } +} + + +////////////////////////////////////////////////////////////////////////// +void CScValue::setObject() { + if (_type == VAL_VARIABLE_REF) { + _valRef->setObject(); + return; + } + + deleteProps(); + _type = VAL_OBJECT; +} + + +////////////////////////////////////////////////////////////////////////// +void CScValue::setReference(CScValue *val) { + _valRef = val; + _type = VAL_VARIABLE_REF; +} + + +////////////////////////////////////////////////////////////////////////// +bool CScValue::getBool(bool defaultVal) { + if (_type == VAL_VARIABLE_REF) + return _valRef->getBool(); + + switch (_type) { + case VAL_BOOL: + return _valBool; + + case VAL_NATIVE: + return _valNative->scToBool(); + + case VAL_INT: + return (_valInt != 0); + + case VAL_FLOAT: + return (_valFloat != 0.0f); + + case VAL_STRING: + return (scumm_stricmp(_valString, "1") == 0 || scumm_stricmp(_valString, "yes") == 0 || scumm_stricmp(_valString, "true") == 0); + + default: + return defaultVal; + } +} + + +////////////////////////////////////////////////////////////////////////// +int CScValue::getInt(int defaultVal) { + if (_type == VAL_VARIABLE_REF) return _valRef->getInt(); + + switch (_type) { + case VAL_BOOL: + return _valBool ? 1 : 0; + + case VAL_NATIVE: + return _valNative->scToInt(); + + case VAL_INT: + return _valInt; + + case VAL_FLOAT: + return (int)_valFloat; + + case VAL_STRING: + return atoi(_valString); + + default: + return defaultVal; + } +} + + +////////////////////////////////////////////////////////////////////////// +double CScValue::getFloat(double defaultVal) { + if (_type == VAL_VARIABLE_REF) + return _valRef->getFloat(); + + switch (_type) { + case VAL_BOOL: + return _valBool ? 1.0f : 0.0f; + + case VAL_NATIVE: + return _valNative->scToFloat(); + + case VAL_INT: + return (double)_valInt; + + case VAL_FLOAT: + return _valFloat; + + case VAL_STRING: + return atof(_valString); + + default: + return defaultVal; + } +} + +////////////////////////////////////////////////////////////////////////// +void *CScValue::getMemBuffer() { + if (_type == VAL_VARIABLE_REF) + return _valRef->getMemBuffer(); + + if (_type == VAL_NATIVE) + return _valNative->scToMemBuffer(); + else return (void *)NULL; +} + + +////////////////////////////////////////////////////////////////////////// +const char *CScValue::getString() { + if (_type == VAL_VARIABLE_REF) + return _valRef->getString(); + + switch (_type) { + case VAL_OBJECT: + setStringVal("[object]"); + break; + + case VAL_NULL: + setStringVal("[null]"); + break; + + case VAL_NATIVE: { + const char *strVal = _valNative->scToString(); + setStringVal(strVal); + return strVal; + break; + } + + case VAL_BOOL: + setStringVal(_valBool ? "yes" : "no"); + break; + + case VAL_INT: { + char dummy[50]; + sprintf(dummy, "%d", _valInt); + setStringVal(dummy); + break; + } + + case VAL_FLOAT: { + char dummy[50]; + sprintf(dummy, "%f", _valFloat); + setStringVal(dummy); + break; + } + + case VAL_STRING: + break; + + default: + setStringVal(""); + } + + return _valString; +} + + +////////////////////////////////////////////////////////////////////////// +CBScriptable *CScValue::getNative() { + if (_type == VAL_VARIABLE_REF) + return _valRef->getNative(); + + if (_type == VAL_NATIVE) return _valNative; + else return NULL; +} + + +////////////////////////////////////////////////////////////////////////// +TValType CScValue::getType() { + return _type; +} + + +////////////////////////////////////////////////////////////////////////// +void CScValue::copy(CScValue *orig, bool copyWhole) { + _gameRef = orig->_gameRef; + + if (_valNative && !_persistent) { + _valNative->_refCount--; + if (_valNative->_refCount <= 0) { + if (_valNative != orig->_valNative) delete _valNative; + _valNative = NULL; + } + } + + if (orig->_type == VAL_VARIABLE_REF && orig->_valRef && copyWhole) orig = orig->_valRef; + + cleanup(true); + + _type = orig->_type; + _valBool = orig->_valBool; + _valInt = orig->_valInt; + _valFloat = orig->_valFloat; + setStringVal(orig->_valString); + + _valRef = orig->_valRef; + _persistent = orig->_persistent; + + _valNative = orig->_valNative; + if (_valNative && !_persistent) _valNative->_refCount++; +//!!!! ref->native++ + + // copy properties + if (orig->_type == VAL_OBJECT && orig->_valObject.size() > 0) { + orig->_valIter = orig->_valObject.begin(); + while (orig->_valIter != orig->_valObject.end()) { + _valObject[orig->_valIter->_key] = new CScValue(_gameRef); + _valObject[orig->_valIter->_key]->copy(orig->_valIter->_value); + orig->_valIter++; + } + } else _valObject.clear(); +} + + +////////////////////////////////////////////////////////////////////////// +void CScValue::setValue(CScValue *val) { + if (val->_type == VAL_VARIABLE_REF) { + setValue(val->_valRef); + return; + } + + // if being assigned a simple type, preserve native state + if (_type == VAL_NATIVE && (val->_type == VAL_INT || val->_type == VAL_STRING || val->_type == VAL_BOOL)) { + switch (val->_type) { + case VAL_INT: + _valNative->scSetInt(val->getInt()); + break; + case VAL_FLOAT: + _valNative->scSetFloat(val->getFloat()); + break; + case VAL_BOOL: + _valNative->scSetBool(val->getBool()); + break; + case VAL_STRING: + _valNative->scSetString(val->getString()); + break; + default: + warning("CScValue::setValue - unhandled enum"); + break; + } + } + // otherwise just copy everything + else copy(val); +} + + +////////////////////////////////////////////////////////////////////////// +bool CScValue::persist(CBPersistMgr *persistMgr) { + persistMgr->transfer(TMEMBER(_gameRef)); + + persistMgr->transfer(TMEMBER(_persistent)); + persistMgr->transfer(TMEMBER(_isConstVar)); + persistMgr->transfer(TMEMBER_INT(_type)); + persistMgr->transfer(TMEMBER(_valBool)); + persistMgr->transfer(TMEMBER(_valFloat)); + persistMgr->transfer(TMEMBER(_valInt)); + persistMgr->transfer(TMEMBER(_valNative)); + + int size; + const char *str; + if (persistMgr->_saving) { + size = _valObject.size(); + persistMgr->transfer("", &size); + _valIter = _valObject.begin(); + while (_valIter != _valObject.end()) { + str = _valIter->_key.c_str(); + persistMgr->transfer("", &str); + persistMgr->transfer("", &_valIter->_value); + + _valIter++; + } + } else { + CScValue *val; + persistMgr->transfer("", &size); + for (int i = 0; i < size; i++) { + persistMgr->transfer("", &str); + persistMgr->transfer("", &val); + + _valObject[str] = val; + delete [] str; + } + } + + persistMgr->transfer(TMEMBER(_valRef)); + persistMgr->transfer(TMEMBER(_valString)); + + /* + FILE* f = fopen("c:\\val.log", "a+"); + switch(_type) + { + case VAL_STRING: + fprintf(f, "str %s\n", _valString); + break; + + case VAL_INT: + fprintf(f, "int %d\n", _valInt); + break; + + case VAL_BOOL: + fprintf(f, "bool %d\n", _valBool); + break; + + case VAL_NULL: + fprintf(f, "null\n"); + break; + + case VAL_NATIVE: + fprintf(f, "native\n"); + break; + + case VAL_VARIABLE_REF: + fprintf(f, "ref\n"); + break; + + case VAL_OBJECT: + fprintf(f, "obj\n"); + break; + + case VAL_FLOAT: + fprintf(f, "float\n"); + break; + + } + fclose(f); + */ + + return STATUS_OK; +} + + +////////////////////////////////////////////////////////////////////////// +bool CScValue::saveAsText(CBDynBuffer *buffer, int indent) { + _valIter = _valObject.begin(); + while (_valIter != _valObject.end()) { + buffer->putTextIndent(indent, "PROPERTY {\n"); + buffer->putTextIndent(indent + 2, "NAME=\"%s\"\n", _valIter->_key.c_str()); + buffer->putTextIndent(indent + 2, "VALUE=\"%s\"\n", _valIter->_value->getString()); + buffer->putTextIndent(indent, "}\n\n"); + + _valIter++; + } + return STATUS_OK; +} + + +////////////////////////////////////////////////////////////////////////// +// -1 ... left is less, 0 ... equals, 1 ... left is greater +int CScValue::compare(CScValue *val1, CScValue *val2) { + // both natives? + if (val1->isNative() && val2->isNative()) { + // same class? + if (strcmp(val1->getNative()->getClassName(), val2->getNative()->getClassName()) == 0) { + return val1->getNative()->scCompare(val2->getNative()); + } else return strcmp(val1->getString(), val2->getString()); + } + + // both objects? + if (val1->isObject() && val2->isObject()) return -1; + + + // null states + if (val1->isNULL() && !val2->isNULL()) return -1; + else if (!val1->isNULL() && val2->isNULL()) return 1; + else if (val1->isNULL() && val2->isNULL()) return 0; + + // one of them is string? convert both to string + if (val1->isString() || val2->isString()) return strcmp(val1->getString(), val2->getString()); + + // one of them is float? + if (val1->isFloat() || val2->isFloat()) { + if (val1->getFloat() < val2->getFloat()) return -1; + else if (val1->getFloat() > val2->getFloat()) return 1; + else return 0; + } + + // otherwise compare as int's + if (val1->getInt() < val2->getInt()) return -1; + else if (val1->getInt() > val2->getInt()) return 1; + else return 0; +} + + +////////////////////////////////////////////////////////////////////////// +int CScValue::compareStrict(CScValue *val1, CScValue *val2) { + if (val1->getTypeTolerant() != val2->getTypeTolerant()) return -1; + else return CScValue::compare(val1, val2); +} + + +////////////////////////////////////////////////////////////////////////// +bool CScValue::dbgSendVariables(IWmeDebugClient *client, EWmeDebuggerVariableType type, CScScript *script, unsigned int scopeID) { + _valIter = _valObject.begin(); + while (_valIter != _valObject.end()) { + client->onVariableInit(type, script, scopeID, _valIter->_value, _valIter->_key.c_str()); + _valIter++; + } + return STATUS_OK; +} + + +////////////////////////////////////////////////////////////////////////// +bool CScValue::setProperty(const char *propName, int value) { + CScValue *val = new CScValue(_gameRef, value); + bool ret = DID_SUCCEED(setProp(propName, val)); + delete val; + return ret; +} + +////////////////////////////////////////////////////////////////////////// +bool CScValue::setProperty(const char *propName, const char *value) { + CScValue *val = new CScValue(_gameRef, value); + bool ret = DID_SUCCEED(setProp(propName, val)); + delete val; + return ret; +} + +////////////////////////////////////////////////////////////////////////// +bool CScValue::setProperty(const char *propName, double value) { + CScValue *val = new CScValue(_gameRef, value); + bool ret = DID_SUCCEED(setProp(propName, val)); + delete val; + return ret; +} + + +////////////////////////////////////////////////////////////////////////// +bool CScValue::setProperty(const char *propName, bool value) { + CScValue *val = new CScValue(_gameRef, value); + bool ret = DID_SUCCEED(setProp(propName, val)); + delete val; + return ret; +} + + +////////////////////////////////////////////////////////////////////////// +bool CScValue::setProperty(const char *propName) { + CScValue *val = new CScValue(_gameRef); + bool ret = DID_SUCCEED(setProp(propName, val)); + delete val; + return ret; +} + + +////////////////////////////////////////////////////////////////////////// +// IWmeDebugProp +////////////////////////////////////////////////////////////////////////// +EWmeDebuggerPropType CScValue::dbgGetType() { + switch (getType()) { + case VAL_NULL: + return WME_DBGPROP_NULL; + case VAL_STRING: + return WME_DBGPROP_STRING; + case VAL_INT: + return WME_DBGPROP_INT; + case VAL_BOOL: + return WME_DBGPROP_BOOL; + case VAL_FLOAT: + return WME_DBGPROP_FLOAT; + case VAL_OBJECT: + return WME_DBGPROP_OBJECT; + case VAL_NATIVE: + return WME_DBGPROP_NATIVE; + default: + return WME_DBGPROP_UNKNOWN; + } +} + +////////////////////////////////////////////////////////////////////////// +int CScValue::dbgGetValInt() { + return getInt(); +} + +////////////////////////////////////////////////////////////////////////// +double CScValue::dbgGetValFloat() { + return getFloat(); +} + +////////////////////////////////////////////////////////////////////////// +bool CScValue::dbgGetValBool() { + return getBool(); +} + +////////////////////////////////////////////////////////////////////////// +const char *CScValue::dbgGetValString() { + return getString(); +} + +////////////////////////////////////////////////////////////////////////// +IWmeDebugObject *CScValue::dbgGetValNative() { + return getNative(); +} + +////////////////////////////////////////////////////////////////////////// +bool CScValue::dbgSetVal(int value) { + setInt(value); + return true; +} + +////////////////////////////////////////////////////////////////////////// +bool CScValue::dbgSetVal(double value) { + setFloat(value); + return true; +} + +////////////////////////////////////////////////////////////////////////// +bool CScValue::dbgSetVal(bool value) { + setBool(value); + return true; +} + +////////////////////////////////////////////////////////////////////////// +bool CScValue::dbgSetVal(const char *value) { + setString(value); + return true; +} + +////////////////////////////////////////////////////////////////////////// +bool CScValue::dbgSetVal() { + setNULL(); + return true; +} + + +////////////////////////////////////////////////////////////////////////// +int CScValue::dbgGetNumProperties() { + if (_valNative && _valNative->_scProp) + return _valNative->_scProp->dbgGetNumProperties(); + else return _valObject.size(); +} + +////////////////////////////////////////////////////////////////////////// +bool CScValue::dbgGetProperty(int index, const char **name, IWmeDebugProp **value) { + if (_valNative && _valNative->_scProp) + return _valNative->_scProp->dbgGetProperty(index, name, value); + else { + int count = 0; + _valIter = _valObject.begin(); + while (_valIter != _valObject.end()) { + if (count == index) { + *name = _valIter->_key.c_str(); + *value = _valIter->_value; + return true; + } + _valIter++; + count++; + } + return false; + } +} + +////////////////////////////////////////////////////////////////////////// +bool CScValue::dbgGetDescription(char *buf, int bufSize) { + if (_type == VAL_VARIABLE_REF) + return _valRef->dbgGetDescription(buf, bufSize); + + if (_type == VAL_NATIVE) { + _valNative->scDebuggerDesc(buf, bufSize); + } else { + strncpy(buf, getString(), bufSize); + } + return true; +} + +} // end of namespace WinterMute diff --git a/engines/wintermute/base/scriptables/script_value.h b/engines/wintermute/base/scriptables/script_value.h new file mode 100644 index 0000000000..eaee3ed773 --- /dev/null +++ b/engines/wintermute/base/scriptables/script_value.h @@ -0,0 +1,141 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +/* + * This file is based on WME Lite. + * http://dead-code.org/redir.php?target=wmelite + * Copyright (c) 2011 Jan Nedoma + */ + +#ifndef WINTERMUTE_SCVALUE_H +#define WINTERMUTE_SCVALUE_H + + +#include "engines/wintermute/base/base.h" +#include "engines/wintermute/persistent.h" +#include "engines/wintermute/dcscript.h" // Added by ClassView +#include "engines/wintermute/wme_debugger.h" +#include "common/str.h" + +namespace WinterMute { + +class CScScript; +class CBScriptable; + +class CScValue : public CBBase, public IWmeDebugProp { +public: + bool dbgSendVariables(IWmeDebugClient *client, EWmeDebuggerVariableType type, CScScript *script, unsigned int scopeID); + + static int compare(CScValue *val1, CScValue *val2); + static int compareStrict(CScValue *val1, CScValue *val2); + TValType getTypeTolerant(); + void cleanup(bool ignoreNatives = false); + DECLARE_PERSISTENT(CScValue, CBBase) + + bool _isConstVar; + bool saveAsText(CBDynBuffer *buffer, int indent); + void setValue(CScValue *val); + bool _persistent; + bool propExists(const char *name); + void copy(CScValue *orig, bool copyWhole = false); + void setStringVal(const char *val); + TValType getType(); + bool getBool(bool defaultVal = false); + int getInt(int defaultVal = 0); + double getFloat(double defaultVal = 0.0f); + const char *getString(); + void *getMemBuffer(); + CBScriptable *getNative(); + bool deleteProp(const char *name); + void deleteProps(); + void CleanProps(bool includingNatives); + void setBool(bool val); + void setInt(int val); + void setFloat(double val); + void setString(const char *val); + void setString(const Common::String &val); + void setNULL(); + void setNative(CBScriptable *val, bool persistent = false); + void setObject(); + void setReference(CScValue *val); + bool isNULL(); + bool isNative(); + bool isString(); + bool isBool(); + bool isFloat(); + bool isInt(); + bool isObject(); + bool setProp(const char *name, CScValue *val, bool copyWhole = false, bool setAsConst = false); + CScValue *getProp(const char *name); + CBScriptable *_valNative; + CScValue *_valRef; +protected: + bool _valBool; + int _valInt; + double _valFloat; + char *_valString; +public: + TValType _type; + CScValue(CBGame *inGame); + CScValue(CBGame *inGame, bool Val); + CScValue(CBGame *inGame, int Val); + CScValue(CBGame *inGame, double Val); + CScValue(CBGame *inGame, const char *Val); + virtual ~CScValue(); + Common::HashMap _valObject; + Common::HashMap::iterator _valIter; + + bool setProperty(const char *propName, int value); + bool setProperty(const char *propName, const char *value); + bool setProperty(const char *propName, double value); + bool setProperty(const char *propName, bool value); + bool setProperty(const char *propName); + + +// IWmeDebugProp interface implementation +public: + virtual EWmeDebuggerPropType dbgGetType(); + + // getters + virtual int dbgGetValInt(); + virtual double dbgGetValFloat(); + virtual bool dbgGetValBool(); + virtual const char *dbgGetValString(); + virtual IWmeDebugObject *dbgGetValNative(); + + // setters + virtual bool dbgSetVal(int value); + virtual bool dbgSetVal(double value); + virtual bool dbgSetVal(bool value); + virtual bool dbgSetVal(const char *value); + virtual bool dbgSetVal(); + + // properties + virtual int dbgGetNumProperties(); + virtual bool dbgGetProperty(int index, const char **mame, IWmeDebugProp **value); + + virtual bool dbgGetDescription(char *buf, int bufSize); +}; + +} // end of namespace WinterMute + +#endif -- cgit v1.2.3 From b5a07fef8ebf29f7f44b15d9b34799c7e115fdad Mon Sep 17 00:00:00 2001 From: Einar Johan Trøan Sømåen Date: Sat, 21 Jul 2012 21:01:47 +0200 Subject: WINTERMUTE: Get rid of the C-prefix for class-definitions. --- engines/wintermute/base/scriptables/script.cpp | 158 ++++++++++----------- engines/wintermute/base/scriptables/script.h | 62 ++++---- .../wintermute/base/scriptables/script_engine.cpp | 110 +++++++------- .../wintermute/base/scriptables/script_engine.h | 38 ++--- .../base/scriptables/script_ext_array.cpp | 34 ++--- .../wintermute/base/scriptables/script_ext_array.h | 20 +-- .../base/scriptables/script_ext_date.cpp | 28 ++-- .../wintermute/base/scriptables/script_ext_date.h | 16 +-- .../base/scriptables/script_ext_file.cpp | 56 ++++---- .../wintermute/base/scriptables/script_ext_file.h | 16 +-- .../base/scriptables/script_ext_math.cpp | 22 +-- .../wintermute/base/scriptables/script_ext_math.h | 12 +- .../base/scriptables/script_ext_mem_buffer.cpp | 42 +++--- .../base/scriptables/script_ext_mem_buffer.h | 20 +-- .../base/scriptables/script_ext_object.cpp | 14 +- .../base/scriptables/script_ext_object.h | 8 +- .../base/scriptables/script_ext_string.cpp | 44 +++--- .../base/scriptables/script_ext_string.h | 16 +-- .../wintermute/base/scriptables/script_stack.cpp | 50 +++---- engines/wintermute/base/scriptables/script_stack.h | 26 ++-- .../wintermute/base/scriptables/script_value.cpp | 158 ++++++++++----------- engines/wintermute/base/scriptables/script_value.h | 50 +++---- 22 files changed, 500 insertions(+), 500 deletions(-) (limited to 'engines/wintermute/base/scriptables') diff --git a/engines/wintermute/base/scriptables/script.cpp b/engines/wintermute/base/scriptables/script.cpp index a9646e0045..45544831e3 100644 --- a/engines/wintermute/base/scriptables/script.cpp +++ b/engines/wintermute/base/scriptables/script.cpp @@ -36,10 +36,10 @@ namespace WinterMute { -IMPLEMENT_PERSISTENT(CScScript, false) +IMPLEMENT_PERSISTENT(ScScript, false) ////////////////////////////////////////////////////////////////////////// -CScScript::CScScript(CBGame *inGame, CScEngine *Engine): CBBase(inGame) { +ScScript::ScScript(BaseGame *inGame, ScEngine *Engine): BaseClass(inGame) { _buffer = NULL; _bufferSize = _iP = 0; _scriptStream = NULL; @@ -98,11 +98,11 @@ CScScript::CScScript(CBGame *inGame, CScEngine *Engine): CBBase(inGame) { ////////////////////////////////////////////////////////////////////////// -CScScript::~CScScript() { +ScScript::~ScScript() { cleanup(); } -void CScScript::readHeader() { +void ScScript::readHeader() { uint32 oldPos = _scriptStream->pos(); _scriptStream->seek(0); _header.magic = _scriptStream->readUint32LE(); @@ -118,7 +118,7 @@ void CScScript::readHeader() { ////////////////////////////////////////////////////////////////////////// -bool CScScript::initScript() { +bool ScScript::initScript() { if (!_scriptStream) { _scriptStream = new Common::MemoryReadStream(_buffer, _bufferSize); } @@ -139,13 +139,13 @@ bool CScScript::initScript() { initTables(); // init stacks - _scopeStack = new CScStack(_gameRef); - _callStack = new CScStack(_gameRef); - _thisStack = new CScStack(_gameRef); - _stack = new CScStack(_gameRef); + _scopeStack = new ScStack(_gameRef); + _callStack = new ScStack(_gameRef); + _thisStack = new ScStack(_gameRef); + _stack = new ScStack(_gameRef); - _operand = new CScValue(_gameRef); - _reg1 = new CScValue(_gameRef); + _operand = new ScValue(_gameRef); + _reg1 = new ScValue(_gameRef); // skip to the beginning @@ -165,7 +165,7 @@ bool CScScript::initScript() { ////////////////////////////////////////////////////////////////////////// -bool CScScript::initTables() { +bool ScScript::initTables() { uint32 OrigIP = _iP; readHeader(); @@ -240,7 +240,7 @@ bool CScScript::initTables() { ////////////////////////////////////////////////////////////////////////// -bool CScScript::create(const char *filename, byte *buffer, uint32 size, CBScriptHolder *owner) { +bool ScScript::create(const char *filename, byte *buffer, uint32 size, BaseScriptHolder *owner) { cleanup(); _thread = false; @@ -263,7 +263,7 @@ bool CScScript::create(const char *filename, byte *buffer, uint32 size, CBScript if (DID_FAIL(res)) return res; // establish global variables table - _globals = new CScValue(_gameRef); + _globals = new ScValue(_gameRef); _owner = owner; @@ -272,7 +272,7 @@ bool CScScript::create(const char *filename, byte *buffer, uint32 size, CBScript ////////////////////////////////////////////////////////////////////////// -bool CScScript::createThread(CScScript *original, uint32 initIP, const char *eventName) { +bool ScScript::createThread(ScScript *original, uint32 initIP, const char *eventName) { cleanup(); _thread = true; @@ -316,7 +316,7 @@ bool CScScript::createThread(CScScript *original, uint32 initIP, const char *eve ////////////////////////////////////////////////////////////////////////// -bool CScScript::createMethodThread(CScScript *original, const char *methodName) { +bool ScScript::createMethodThread(ScScript *original, const char *methodName) { uint32 ip = original->getMethodPos(methodName); if (ip == 0) return STATUS_FAILED; @@ -360,7 +360,7 @@ bool CScScript::createMethodThread(CScScript *original, const char *methodName) ////////////////////////////////////////////////////////////////////////// -void CScScript::cleanup() { +void ScScript::cleanup() { if (_buffer) delete [] _buffer; _buffer = NULL; @@ -430,7 +430,7 @@ void CScScript::cleanup() { ////////////////////////////////////////////////////////////////////////// -uint32 CScScript::getDWORD() { +uint32 ScScript::getDWORD() { _scriptStream->seek((int32)_iP); uint32 ret = _scriptStream->readUint32LE(); _iP += sizeof(uint32); @@ -439,7 +439,7 @@ uint32 CScScript::getDWORD() { } ////////////////////////////////////////////////////////////////////////// -double CScScript::getFloat() { +double ScScript::getFloat() { _scriptStream->seek((int32)_iP); byte buffer[8]; _scriptStream->read(buffer, 8); @@ -459,7 +459,7 @@ double CScScript::getFloat() { ////////////////////////////////////////////////////////////////////////// -char *CScScript::getString() { +char *ScScript::getString() { char *ret = (char *)(_buffer + _iP); while (*(char *)(_buffer + _iP) != '\0') _iP++; _iP++; // string terminator @@ -470,17 +470,17 @@ char *CScScript::getString() { ////////////////////////////////////////////////////////////////////////// -bool CScScript::executeInstruction() { +bool ScScript::executeInstruction() { bool ret = STATUS_OK; uint32 dw; const char *str = NULL; - //CScValue* op = new CScValue(_gameRef); + //ScValue* op = new ScValue(_gameRef); _operand->cleanup(); - CScValue *op1; - CScValue *op2; + ScValue *op1; + ScValue *op2; uint32 inst = getDWORD(); switch (inst) { @@ -557,7 +557,7 @@ bool CScScript::executeInstruction() { char *MethodName = new char[strlen(str) + 1]; strcpy(MethodName, str); - CScValue *var = _stack->pop(); + ScValue *var = _stack->pop(); if (var->_type == VAL_VARIABLE_REF) var = var->_valRef; bool res = STATUS_FAILED; @@ -591,7 +591,7 @@ bool CScScript::executeInstruction() { break; } /* - CScValue* val = var->getProp(MethodName); + ScValue* val = var->getProp(MethodName); if(val){ dw = GetFuncPos(val->getString()); if(dw==0){ @@ -660,7 +660,7 @@ bool CScScript::executeInstruction() { break; case II_PUSH_VAR: { - CScValue *var = getVar(_symbols[getDWORD()]); + ScValue *var = getVar(_symbols[getDWORD()]); if (false && /*var->_type==VAL_OBJECT ||*/ var->_type == VAL_NATIVE) { _operand->setReference(var); _stack->push(_operand); @@ -669,7 +669,7 @@ bool CScScript::executeInstruction() { } case II_PUSH_VAR_REF: { - CScValue *var = getVar(_symbols[getDWORD()]); + ScValue *var = getVar(_symbols[getDWORD()]); _operand->setReference(var); _stack->push(_operand); break; @@ -677,9 +677,9 @@ bool CScScript::executeInstruction() { case II_POP_VAR: { char *VarName = _symbols[getDWORD()]; - CScValue *var = getVar(VarName); + ScValue *var = getVar(VarName); if (var) { - CScValue *val = _stack->pop(); + ScValue *val = _stack->pop(); if (!val) { runtimeError("Script stack corruption detected. Please report this script at WME bug reports forum."); var->setNULL(); @@ -740,7 +740,7 @@ bool CScScript::executeInstruction() { case II_PUSH_BY_EXP: { str = _stack->pop()->getString(); - CScValue *val = _stack->pop()->getProp(str); + ScValue *val = _stack->pop()->getProp(str); if (val) _stack->push(val); else _stack->pushNULL(); @@ -749,8 +749,8 @@ bool CScScript::executeInstruction() { case II_POP_BY_EXP: { str = _stack->pop()->getString(); - CScValue *var = _stack->pop(); - CScValue *val = _stack->pop(); + ScValue *var = _stack->pop(); + ScValue *val = _stack->pop(); if (val == NULL) { runtimeError("Script stack corruption detected. Please report this script at WME bug reports forum."); @@ -778,7 +778,7 @@ bool CScScript::executeInstruction() { case II_JMP_FALSE: { dw = getDWORD(); //if(!_stack->pop()->getBool()) _iP = dw; - CScValue *val = _stack->pop(); + ScValue *val = _stack->pop(); if (!val) { runtimeError("Script corruption detected. Did you use '=' instead of '==' for comparison?"); } else { @@ -916,7 +916,7 @@ bool CScScript::executeInstruction() { } */ - _operand->setBool(CScValue::compare(op1, op2) == 0); + _operand->setBool(ScValue::compare(op1, op2) == 0); _stack->push(_operand); break; @@ -940,7 +940,7 @@ bool CScScript::executeInstruction() { } */ - _operand->setBool(CScValue::compare(op1, op2) != 0); + _operand->setBool(ScValue::compare(op1, op2) != 0); _stack->push(_operand); break; @@ -955,7 +955,7 @@ bool CScScript::executeInstruction() { else _operand->setBool(op1->getInt() < op2->getInt()); */ - _operand->setBool(CScValue::compare(op1, op2) < 0); + _operand->setBool(ScValue::compare(op1, op2) < 0); _stack->push(_operand); break; @@ -970,7 +970,7 @@ bool CScScript::executeInstruction() { else _operand->setBool(op1->getInt() > op2->getInt()); */ - _operand->setBool(CScValue::compare(op1, op2) > 0); + _operand->setBool(ScValue::compare(op1, op2) > 0); _stack->push(_operand); break; @@ -985,7 +985,7 @@ bool CScScript::executeInstruction() { else _operand->setBool(op1->getInt() <= op2->getInt()); */ - _operand->setBool(CScValue::compare(op1, op2) <= 0); + _operand->setBool(ScValue::compare(op1, op2) <= 0); _stack->push(_operand); break; @@ -1000,7 +1000,7 @@ bool CScScript::executeInstruction() { else _operand->setBool(op1->getInt() >= op2->getInt()); */ - _operand->setBool(CScValue::compare(op1, op2) >= 0); + _operand->setBool(ScValue::compare(op1, op2) >= 0); _stack->push(_operand); break; @@ -1009,7 +1009,7 @@ bool CScScript::executeInstruction() { op1 = _stack->pop(); //_operand->setBool(op1->getType()==op2->getType() && op1->getFloat()==op2->getFloat()); - _operand->setBool(CScValue::compareStrict(op1, op2) == 0); + _operand->setBool(ScValue::compareStrict(op1, op2) == 0); _stack->push(_operand); break; @@ -1019,7 +1019,7 @@ bool CScScript::executeInstruction() { op1 = _stack->pop(); //_operand->setBool(op1->getType()!=op2->getType() || op1->getFloat()!=op2->getFloat()); - _operand->setBool(CScValue::compareStrict(op1, op2) != 0); + _operand->setBool(ScValue::compareStrict(op1, op2) != 0); _stack->push(_operand); break; @@ -1059,7 +1059,7 @@ bool CScScript::executeInstruction() { ////////////////////////////////////////////////////////////////////////// -uint32 CScScript::getFuncPos(const char *name) { +uint32 ScScript::getFuncPos(const char *name) { for (uint32 i = 0; i < _numFunctions; i++) { if (strcmp(name, _functions[i].name) == 0) return _functions[i].pos; @@ -1069,7 +1069,7 @@ uint32 CScScript::getFuncPos(const char *name) { ////////////////////////////////////////////////////////////////////////// -uint32 CScScript::getMethodPos(const char *name) { +uint32 ScScript::getMethodPos(const char *name) { for (uint32 i = 0; i < _numMethods; i++) { if (strcmp(name, _methods[i].name) == 0) return _methods[i].pos; @@ -1079,8 +1079,8 @@ uint32 CScScript::getMethodPos(const char *name) { ////////////////////////////////////////////////////////////////////////// -CScValue *CScScript::getVar(char *name) { - CScValue *ret = NULL; +ScValue *ScScript::getVar(char *name) { + ScValue *ret = NULL; // scope locals if (_scopeStack->_sP >= 0) { @@ -1103,8 +1103,8 @@ CScValue *CScScript::getVar(char *name) { if (ret == NULL) { //RuntimeError("Variable '%s' is inaccessible in the current block. Consider changing the script.", name); _gameRef->LOG(0, "Warning: variable '%s' is inaccessible in the current block. Consider changing the script (script:%s, line:%d)", name, _filename, _currentLine); - CScValue *val = new CScValue(_gameRef); - CScValue *scope = _scopeStack->getTop(); + ScValue *val = new ScValue(_gameRef); + ScValue *scope = _scopeStack->getTop(); if (scope) { scope->setProp(name, val); ret = _scopeStack->getTop()->getProp(name); @@ -1120,7 +1120,7 @@ CScValue *CScScript::getVar(char *name) { ////////////////////////////////////////////////////////////////////////// -bool CScScript::waitFor(CBObject *object) { +bool ScScript::waitFor(BaseObject *object) { if (_unbreakable) { runtimeError("Script cannot be interrupted."); return STATUS_OK; @@ -1133,14 +1133,14 @@ bool CScScript::waitFor(CBObject *object) { ////////////////////////////////////////////////////////////////////////// -bool CScScript::waitForExclusive(CBObject *object) { +bool ScScript::waitForExclusive(BaseObject *object) { _engine->resetObject(object); return waitFor(object); } ////////////////////////////////////////////////////////////////////////// -bool CScScript::sleep(uint32 duration) { +bool ScScript::sleep(uint32 duration) { if (_unbreakable) { runtimeError("Script cannot be interrupted."); return STATUS_OK; @@ -1148,7 +1148,7 @@ bool CScScript::sleep(uint32 duration) { _state = SCRIPT_SLEEPING; if (_gameRef->_state == GAME_FROZEN) { - _waitTime = CBPlatform::getTime() + duration; + _waitTime = BasePlatform::getTime() + duration; _waitFrozen = true; } else { _waitTime = _gameRef->_timer + duration; @@ -1159,7 +1159,7 @@ bool CScScript::sleep(uint32 duration) { ////////////////////////////////////////////////////////////////////////// -bool CScScript::finish(bool includingThreads) { +bool ScScript::finish(bool includingThreads) { if (_state != SCRIPT_FINISHED && includingThreads) { _state = SCRIPT_FINISHED; finishThreads(); @@ -1171,14 +1171,14 @@ bool CScScript::finish(bool includingThreads) { ////////////////////////////////////////////////////////////////////////// -bool CScScript::run() { +bool ScScript::run() { _state = SCRIPT_RUNNING; return STATUS_OK; } ////////////////////////////////////////////////////////////////////// -void CScScript::runtimeError(const char *fmt, ...) { +void ScScript::runtimeError(const char *fmt, ...) { char buff[256]; va_list va; @@ -1195,7 +1195,7 @@ void CScScript::runtimeError(const char *fmt, ...) { ////////////////////////////////////////////////////////////////////////// -bool CScScript::persist(CBPersistMgr *persistMgr) { +bool ScScript::persist(BasePersistenceManager *persistMgr) { persistMgr->transfer(TMEMBER(_gameRef)); @@ -1257,13 +1257,13 @@ bool CScScript::persist(CBPersistMgr *persistMgr) { ////////////////////////////////////////////////////////////////////////// -CScScript *CScScript::invokeEventHandler(const char *eventName, bool unbreakable) { +ScScript *ScScript::invokeEventHandler(const char *eventName, bool unbreakable) { //if(_state!=SCRIPT_PERSISTENT) return NULL; uint32 pos = getEventPos(eventName); if (!pos) return NULL; - CScScript *thread = new CScScript(_gameRef, _engine); + ScScript *thread = new ScScript(_gameRef, _engine); if (thread) { bool ret = thread->createThread(this, pos, eventName); if (DID_SUCCEED(ret)) { @@ -1281,7 +1281,7 @@ CScScript *CScScript::invokeEventHandler(const char *eventName, bool unbreakable ////////////////////////////////////////////////////////////////////////// -uint32 CScScript::getEventPos(const char *name) { +uint32 ScScript::getEventPos(const char *name) { for (int i = _numEvents - 1; i >= 0; i--) { if (scumm_stricmp(name, _events[i].name) == 0) return _events[i].pos; } @@ -1290,19 +1290,19 @@ uint32 CScScript::getEventPos(const char *name) { ////////////////////////////////////////////////////////////////////////// -bool CScScript::canHandleEvent(const char *eventName) { +bool ScScript::canHandleEvent(const char *eventName) { return getEventPos(eventName) != 0; } ////////////////////////////////////////////////////////////////////////// -bool CScScript::canHandleMethod(const char *methodName) { +bool ScScript::canHandleMethod(const char *methodName) { return getMethodPos(methodName) != 0; } ////////////////////////////////////////////////////////////////////////// -bool CScScript::pause() { +bool ScScript::pause() { if (_state == SCRIPT_PAUSED) { _gameRef->LOG(0, "Attempting to pause a paused script ('%s', line %d)", _filename, _currentLine); return STATUS_FAILED; @@ -1318,7 +1318,7 @@ bool CScScript::pause() { ////////////////////////////////////////////////////////////////////////// -bool CScScript::resume() { +bool ScScript::resume() { if (_state != SCRIPT_PAUSED) return STATUS_OK; _state = _origState; @@ -1327,7 +1327,7 @@ bool CScScript::resume() { ////////////////////////////////////////////////////////////////////////// -CScScript::TExternalFunction *CScScript::getExternal(char *name) { +ScScript::TExternalFunction *ScScript::getExternal(char *name) { for (uint32 i = 0; i < _numExternals; i++) { if (strcmp(name, _externals[i].name) == 0) return &_externals[i]; @@ -1337,7 +1337,7 @@ CScScript::TExternalFunction *CScScript::getExternal(char *name) { ////////////////////////////////////////////////////////////////////////// -bool CScScript::externalCall(CScStack *stack, CScStack *thisStack, CScScript::TExternalFunction *function) { +bool ScScript::externalCall(ScStack *stack, ScStack *thisStack, ScScript::TExternalFunction *function) { _gameRef->LOG(0, "External functions are not supported on this platform."); stack->correctParams(0); @@ -1347,7 +1347,7 @@ bool CScScript::externalCall(CScStack *stack, CScStack *thisStack, CScScript::TE ////////////////////////////////////////////////////////////////////////// -bool CScScript::copyParameters(CScStack *stack) { +bool ScScript::copyParameters(ScStack *stack) { int i; int NumParams = stack->pop()->getInt(); for (i = NumParams - 1; i >= 0; i--) { @@ -1362,9 +1362,9 @@ bool CScScript::copyParameters(CScStack *stack) { ////////////////////////////////////////////////////////////////////////// -bool CScScript::finishThreads() { +bool ScScript::finishThreads() { for (int i = 0; i < _engine->_scripts.getSize(); i++) { - CScScript *scr = _engine->_scripts[i]; + ScScript *scr = _engine->_scripts[i]; if (scr->_thread && scr->_state != SCRIPT_FINISHED && scr->_owner == _owner && scumm_stricmp(scr->_filename, _filename) == 0) scr->finish(true); } @@ -1374,18 +1374,18 @@ bool CScScript::finishThreads() { ////////////////////////////////////////////////////////////////////////// // IWmeDebugScript interface implementation -int CScScript::dbgGetLine() { +int ScScript::dbgGetLine() { return _currentLine; } ////////////////////////////////////////////////////////////////////////// -const char *CScScript::dbgGetFilename() { +const char *ScScript::dbgGetFilename() { return _filename; } ////////////////////////////////////////////////////////////////////////// -bool CScScript::dbgSendScript(IWmeDebugClient *client) { +bool ScScript::dbgSendScript(IWmeDebugClient *client) { if (_methodThread) client->onScriptMethodThreadInit(this, _parentScript, _threadEvent); else if (_thread) client->onScriptEventThreadInit(this, _parentScript, _threadEvent); else client->onScriptInit(this); @@ -1395,14 +1395,14 @@ bool CScScript::dbgSendScript(IWmeDebugClient *client) { } ////////////////////////////////////////////////////////////////////////// -bool CScScript::dbgSendVariables(IWmeDebugClient *client) { +bool ScScript::dbgSendVariables(IWmeDebugClient *client) { // send script globals _globals->dbgSendVariables(client, WME_DBGVAR_SCRIPT, this, 0); // send scope variables if (_scopeStack->_sP >= 0) { for (int i = 0; i <= _scopeStack->_sP; i++) { - // CScValue *Scope = _scopeStack->GetAt(i); + // ScValue *Scope = _scopeStack->GetAt(i); //Scope->DbgSendVariables(Client, WME_DBGVAR_SCOPE, this, (unsigned int)Scope); } } @@ -1411,35 +1411,35 @@ bool CScScript::dbgSendVariables(IWmeDebugClient *client) { ////////////////////////////////////////////////////////////////////////// -TScriptState CScScript::dbgGetState() { +TScriptState ScScript::dbgGetState() { return _state; } ////////////////////////////////////////////////////////////////////////// -int CScScript::dbgGetNumBreakpoints() { +int ScScript::dbgGetNumBreakpoints() { return _breakpoints.getSize(); } ////////////////////////////////////////////////////////////////////////// -int CScScript::dbgGetBreakpoint(int index) { +int ScScript::dbgGetBreakpoint(int index) { if (index >= 0 && index < _breakpoints.getSize()) return _breakpoints[index]; else return -1; } ////////////////////////////////////////////////////////////////////////// -bool CScScript::dbgSetTracingMode(bool isTracing) { +bool ScScript::dbgSetTracingMode(bool isTracing) { _tracingMode = isTracing; return true; } ////////////////////////////////////////////////////////////////////////// -bool CScScript::dbgGetTracingMode() { +bool ScScript::dbgGetTracingMode() { return _tracingMode; } ////////////////////////////////////////////////////////////////////////// -void CScScript::afterLoad() { +void ScScript::afterLoad() { if (_buffer == NULL) { byte *buffer = _engine->getCompiledScript(_filename, &_bufferSize); if (!buffer) { diff --git a/engines/wintermute/base/scriptables/script.h b/engines/wintermute/base/scriptables/script.h index 899e1f3098..3bb4bc48a7 100644 --- a/engines/wintermute/base/scriptables/script.h +++ b/engines/wintermute/base/scriptables/script.h @@ -37,50 +37,50 @@ #include "engines/wintermute/wme_debugger.h" namespace WinterMute { -class CBScriptHolder; -class CBObject; -class CScEngine; -class CScStack; -class CScScript : public CBBase, public IWmeDebugScript { +class BaseScriptHolder; +class BaseObject; +class ScEngine; +class ScStack; +class ScScript : public BaseClass, public IWmeDebugScript { public: bool dbgSendScript(IWmeDebugClient *client); bool dbgSendVariables(IWmeDebugClient *client); - CBArray _breakpoints; + BaseArray _breakpoints; bool _tracingMode; - CScScript *_parentScript; + ScScript *_parentScript; bool _unbreakable; bool finishThreads(); - bool copyParameters(CScStack *stack); + bool copyParameters(ScStack *stack); void afterLoad(); - CScValue *_operand; - CScValue *_reg1; + ScValue *_operand; + ScValue *_reg1; bool _freezable; bool resume(); bool pause(); bool canHandleEvent(const char *eventName); bool canHandleMethod(const char *methodName); - bool createThread(CScScript *original, uint32 initIP, const char *eventName); - bool createMethodThread(CScScript *original, const char *methodName); - CScScript *invokeEventHandler(const char *eventName, bool unbreakable = false); + bool createThread(ScScript *original, uint32 initIP, const char *eventName); + bool createMethodThread(ScScript *original, const char *methodName); + ScScript *invokeEventHandler(const char *eventName, bool unbreakable = false); uint32 _timeSlice; - DECLARE_PERSISTENT(CScScript, CBBase) + DECLARE_PERSISTENT(ScScript, BaseClass) void runtimeError(const char *fmt, ...); bool run(); bool finish(bool includingThreads = false); bool sleep(uint32 duration); - bool waitForExclusive(CBObject *object); - bool waitFor(CBObject *object); + bool waitForExclusive(BaseObject *object); + bool waitFor(BaseObject *object); uint32 _waitTime; bool _waitFrozen; - CBObject *_waitObject; - CScScript *_waitScript; + BaseObject *_waitObject; + ScScript *_waitScript; TScriptState _state; TScriptState _origState; - CScValue *getVar(char *name); + ScValue *getVar(char *name); uint32 getFuncPos(const char *name); uint32 getEventPos(const char *name); uint32 getMethodPos(const char *name); @@ -122,19 +122,19 @@ public: } TExternalFunction; - CScStack *_callStack; - CScStack *_thisStack; - CScStack *_scopeStack; - CScStack *_stack; - CScValue *_globals; - CScEngine *_engine; + ScStack *_callStack; + ScStack *_thisStack; + ScStack *_scopeStack; + ScStack *_stack; + ScValue *_globals; + ScEngine *_engine; int _currentLine; bool executeInstruction(); char *getString(); uint32 getDWORD(); double getFloat(); void cleanup(); - bool create(const char *filename, byte *buffer, uint32 size, CBScriptHolder *owner); + bool create(const char *filename, byte *buffer, uint32 size, BaseScriptHolder *owner); uint32 _iP; private: void readHeader(); @@ -142,8 +142,8 @@ private: byte *_buffer; public: Common::SeekableReadStream *_scriptStream; - CScScript(CBGame *inGame, CScEngine *Engine); - virtual ~CScScript(); + ScScript(BaseGame *inGame, ScEngine *Engine); + virtual ~ScScript(); char *_filename; char **_symbols; uint32 _numSymbols; @@ -158,9 +158,9 @@ public: bool _thread; bool _methodThread; char *_threadEvent; - CBScriptHolder *_owner; - CScScript::TExternalFunction *getExternal(char *name); - bool externalCall(CScStack *stack, CScStack *thisStack, CScScript::TExternalFunction *function); + BaseScriptHolder *_owner; + ScScript::TExternalFunction *getExternal(char *name); + bool externalCall(ScStack *stack, ScStack *thisStack, ScScript::TExternalFunction *function); private: bool initScript(); bool initTables(); diff --git a/engines/wintermute/base/scriptables/script_engine.cpp b/engines/wintermute/base/scriptables/script_engine.cpp index 387093ac4a..44add054c5 100644 --- a/engines/wintermute/base/scriptables/script_engine.cpp +++ b/engines/wintermute/base/scriptables/script_engine.cpp @@ -41,29 +41,29 @@ namespace WinterMute { -IMPLEMENT_PERSISTENT(CScEngine, true) +IMPLEMENT_PERSISTENT(ScEngine, true) #define COMPILER_DLL "dcscomp.dll" ////////////////////////////////////////////////////////////////////////// -CScEngine::CScEngine(CBGame *inGame): CBBase(inGame) { +ScEngine::ScEngine(BaseGame *inGame): BaseClass(inGame) { _gameRef->LOG(0, "Initializing scripting engine..."); if (_compilerAvailable) _gameRef->LOG(0, " Script compiler bound successfuly"); else _gameRef->LOG(0, " Script compiler is NOT available"); - _globals = new CScValue(_gameRef); + _globals = new ScValue(_gameRef); // register 'Game' as global variable if (!_globals->propExists("Game")) { - CScValue val(_gameRef); + ScValue val(_gameRef); val.setNative(_gameRef, true); _globals->setProp("Game", &val); } // register 'Math' as global variable if (!_globals->propExists("Math")) { - CScValue val(_gameRef); + ScValue val(_gameRef); val.setNative(_gameRef->_mathClass, true); _globals->setProp("Math", &val); } @@ -81,7 +81,7 @@ CScEngine::CScEngine(CBGame *inGame): CBBase(inGame) { ////////////////////////////////////////////////////////////////////////// -CScEngine::~CScEngine() { +ScEngine::~ScEngine() { _gameRef->LOG(0, "Shutting down scripting engine"); saveBreakpoints(); @@ -98,7 +98,7 @@ CScEngine::~CScEngine() { ////////////////////////////////////////////////////////////////////////// -bool CScEngine::cleanup() { +bool ScEngine::cleanup() { for (int i = 0; i < _scripts.getSize(); i++) { if (!_scripts[i]->_thread && _scripts[i]->_owner) _scripts[i]->_owner->removeScript(_scripts[i]); delete _scripts[i]; @@ -120,25 +120,25 @@ bool CScEngine::cleanup() { ////////////////////////////////////////////////////////////////////////// -byte *CScEngine::loadFile(void *data, char *filename, uint32 *size) { - CBGame *gameRef = (CBGame *)data; +byte *ScEngine::loadFile(void *data, char *filename, uint32 *size) { + BaseGame *gameRef = (BaseGame *)data; return gameRef->_fileManager->readWholeFile(filename, size); } ////////////////////////////////////////////////////////////////////////// -void CScEngine::closeFile(void *data, byte *buffer) { +void ScEngine::closeFile(void *data, byte *buffer) { delete [] buffer; } ////////////////////////////////////////////////////////////////////////// -void CScEngine::parseElement(void *data, int line, int type, void *elementData) { +void ScEngine::parseElement(void *data, int line, int type, void *elementData) { } ////////////////////////////////////////////////////////////////////////// -CScScript *CScEngine::runScript(const char *filename, CBScriptHolder *owner) { +ScScript *ScEngine::runScript(const char *filename, BaseScriptHolder *owner) { byte *compBuffer; uint32 compSize; @@ -147,7 +147,7 @@ CScScript *CScEngine::runScript(const char *filename, CBScriptHolder *owner) { if (!compBuffer) return NULL; // add new script - CScScript *script = new CScScript(_gameRef, this); + ScScript *script = new ScScript(_gameRef, this); bool ret = script->create(filename, compBuffer, compSize, owner); if (DID_FAIL(ret)) { _gameRef->LOG(ret, "Error running script '%s'...", filename); @@ -155,7 +155,7 @@ CScScript *CScEngine::runScript(const char *filename, CBScriptHolder *owner) { return NULL; } else { // publish the "self" pseudo-variable - CScValue val(_gameRef); + ScValue val(_gameRef); if (owner)val.setNative(owner, true); else val.setNULL(); @@ -171,12 +171,12 @@ CScScript *CScEngine::runScript(const char *filename, CBScriptHolder *owner) { ////////////////////////////////////////////////////////////////////////// -byte *CScEngine::getCompiledScript(const char *filename, uint32 *outSize, bool ignoreCache) { +byte *ScEngine::getCompiledScript(const char *filename, uint32 *outSize, bool ignoreCache) { // is script in cache? if (!ignoreCache) { for (int i = 0; i < MAX_CACHED_SCRIPTS; i++) { if (_cachedScripts[i] && scumm_stricmp(_cachedScripts[i]->_filename.c_str(), filename) == 0) { - _cachedScripts[i]->_timestamp = CBPlatform::getTime(); + _cachedScripts[i]->_timestamp = BasePlatform::getTime(); *outSize = _cachedScripts[i]->_size; return _cachedScripts[i]->_buffer; } @@ -191,7 +191,7 @@ byte *CScEngine::getCompiledScript(const char *filename, uint32 *outSize, bool i byte *buffer = _gameRef->_fileManager->readWholeFile(filename, &size); if (!buffer) { - _gameRef->LOG(0, "CScEngine::GetCompiledScript - error opening script '%s'", filename); + _gameRef->LOG(0, "ScEngine::GetCompiledScript - error opening script '%s'", filename); return NULL; } @@ -201,7 +201,7 @@ byte *CScEngine::getCompiledScript(const char *filename, uint32 *outSize, bool i compSize = size; } else { if (!_compilerAvailable) { - _gameRef->LOG(0, "CScEngine::GetCompiledScript - script '%s' needs to be compiled but compiler is not available", filename); + _gameRef->LOG(0, "ScEngine::GetCompiledScript - script '%s' needs to be compiled but compiler is not available", filename); delete [] buffer; return NULL; } @@ -216,7 +216,7 @@ byte *CScEngine::getCompiledScript(const char *filename, uint32 *outSize, bool i CScCachedScript *cachedScript = new CScCachedScript(filename, compBuffer, compSize); if (cachedScript) { int index = 0; - uint32 MinTime = CBPlatform::getTime(); + uint32 MinTime = BasePlatform::getTime(); for (int i = 0; i < MAX_CACHED_SCRIPTS; i++) { if (_cachedScripts[i] == NULL) { index = i; @@ -244,7 +244,7 @@ byte *CScEngine::getCompiledScript(const char *filename, uint32 *outSize, bool i ////////////////////////////////////////////////////////////////////////// -bool CScEngine::tick() { +bool ScEngine::tick() { if (_scripts.getSize() == 0) return STATUS_OK; @@ -275,7 +275,7 @@ bool CScEngine::tick() { case SCRIPT_SLEEPING: { if (_scripts[i]->_waitFrozen) { - if (_scripts[i]->_waitTime <= CBPlatform::getTime()) _scripts[i]->run(); + if (_scripts[i]->_waitTime <= BasePlatform::getTime()) _scripts[i]->run(); } else { if (_scripts[i]->_waitTime <= _gameRef->_timer) _scripts[i]->run(); } @@ -300,7 +300,7 @@ bool CScEngine::tick() { break; } default: - //warning("CScEngine::Tick - Unhandled enum"); + //warning("ScEngine::Tick - Unhandled enum"); break; } // switch } // for each script @@ -314,25 +314,25 @@ bool CScEngine::tick() { // time sliced script if (_scripts[i]->_timeSlice > 0) { - uint32 StartTime = CBPlatform::getTime(); - while (_scripts[i]->_state == SCRIPT_RUNNING && CBPlatform::getTime() - StartTime < _scripts[i]->_timeSlice) { + uint32 StartTime = BasePlatform::getTime(); + while (_scripts[i]->_state == SCRIPT_RUNNING && BasePlatform::getTime() - StartTime < _scripts[i]->_timeSlice) { _currentScript = _scripts[i]; _scripts[i]->executeInstruction(); } - if (_isProfiling && _scripts[i]->_filename) addScriptTime(_scripts[i]->_filename, CBPlatform::getTime() - StartTime); + if (_isProfiling && _scripts[i]->_filename) addScriptTime(_scripts[i]->_filename, BasePlatform::getTime() - StartTime); } // normal script else { uint32 startTime = 0; bool isProfiling = _isProfiling; - if (isProfiling) startTime = CBPlatform::getTime(); + if (isProfiling) startTime = BasePlatform::getTime(); while (_scripts[i]->_state == SCRIPT_RUNNING) { _currentScript = _scripts[i]; _scripts[i]->executeInstruction(); } - if (isProfiling && _scripts[i]->_filename) addScriptTime(_scripts[i]->_filename, CBPlatform::getTime() - startTime); + if (isProfiling && _scripts[i]->_filename) addScriptTime(_scripts[i]->_filename, BasePlatform::getTime() - startTime); } _currentScript = NULL; } @@ -344,7 +344,7 @@ bool CScEngine::tick() { ////////////////////////////////////////////////////////////////////////// -bool CScEngine::tickUnbreakable() { +bool ScEngine::tickUnbreakable() { // execute unbreakable scripts for (int i = 0; i < _scripts.getSize(); i++) { if (!_scripts[i]->_unbreakable) continue; @@ -363,7 +363,7 @@ bool CScEngine::tickUnbreakable() { ////////////////////////////////////////////////////////////////////////// -bool CScEngine::removeFinishedScripts() { +bool ScEngine::removeFinishedScripts() { // remove finished scripts for (int i = 0; i < _scripts.getSize(); i++) { if (_scripts[i]->_state == SCRIPT_FINISHED || _scripts[i]->_state == SCRIPT_ERROR) { @@ -379,7 +379,7 @@ bool CScEngine::removeFinishedScripts() { ////////////////////////////////////////////////////////////////////////// -int CScEngine::getNumScripts(int *running, int *waiting, int *persistent) { +int ScEngine::getNumScripts(int *running, int *waiting, int *persistent) { int numRunning = 0, numWaiting = 0, numPersistent = 0, numTotal = 0; for (int i = 0; i < _scripts.getSize(); i++) { @@ -397,7 +397,7 @@ int CScEngine::getNumScripts(int *running, int *waiting, int *persistent) { numPersistent++; break; default: - warning("CScEngine::GetNumScripts - unhandled enum"); + warning("ScEngine::GetNumScripts - unhandled enum"); break; } numTotal++; @@ -411,7 +411,7 @@ int CScEngine::getNumScripts(int *running, int *waiting, int *persistent) { ////////////////////////////////////////////////////////////////////////// -bool CScEngine::emptyScriptCache() { +bool ScEngine::emptyScriptCache() { for (int i = 0; i < MAX_CACHED_SCRIPTS; i++) { if (_cachedScripts[i]) { delete _cachedScripts[i]; @@ -423,7 +423,7 @@ bool CScEngine::emptyScriptCache() { ////////////////////////////////////////////////////////////////////////// -bool CScEngine::resetObject(CBObject *Object) { +bool ScEngine::resetObject(BaseObject *Object) { // terminate all scripts waiting for this object for (int i = 0; i < _scripts.getSize(); i++) { if (_scripts[i]->_state == SCRIPT_WAITING && _scripts[i]->_waitObject == Object) { @@ -437,7 +437,7 @@ bool CScEngine::resetObject(CBObject *Object) { } ////////////////////////////////////////////////////////////////////////// -bool CScEngine::resetScript(CScScript *script) { +bool ScEngine::resetScript(ScScript *script) { // terminate all scripts waiting for this script for (int i = 0; i < _scripts.getSize(); i++) { if (_scripts[i]->_state == SCRIPT_WAITING_SCRIPT && _scripts[i]->_waitScript == script) { @@ -448,7 +448,7 @@ bool CScEngine::resetScript(CScScript *script) { } ////////////////////////////////////////////////////////////////////////// -bool CScEngine::persist(CBPersistMgr *persistMgr) { +bool ScEngine::persist(BasePersistenceManager *persistMgr) { if (!persistMgr->_saving) cleanup(); persistMgr->transfer(TMEMBER(_gameRef)); @@ -461,7 +461,7 @@ bool CScEngine::persist(CBPersistMgr *persistMgr) { ////////////////////////////////////////////////////////////////////////// -void CScEngine::editorCleanup() { +void ScEngine::editorCleanup() { for (int i = 0; i < _scripts.getSize(); i++) { if (_scripts[i]->_owner == NULL && (_scripts[i]->_state == SCRIPT_FINISHED || _scripts[i]->_state == SCRIPT_ERROR)) { delete _scripts[i]; @@ -473,7 +473,7 @@ void CScEngine::editorCleanup() { ////////////////////////////////////////////////////////////////////////// -bool CScEngine::pauseAll() { +bool ScEngine::pauseAll() { for (int i = 0; i < _scripts.getSize(); i++) { if (_scripts[i] != _currentScript) _scripts[i]->pause(); } @@ -483,7 +483,7 @@ bool CScEngine::pauseAll() { ////////////////////////////////////////////////////////////////////////// -bool CScEngine::resumeAll() { +bool ScEngine::resumeAll() { for (int i = 0; i < _scripts.getSize(); i++) _scripts[i]->resume(); @@ -492,7 +492,7 @@ bool CScEngine::resumeAll() { ////////////////////////////////////////////////////////////////////////// -bool CScEngine::isValidScript(CScScript *script) { +bool ScEngine::isValidScript(ScScript *script) { for (int i = 0; i < _scripts.getSize(); i++) { if (_scripts[i] == script) return true; } @@ -500,13 +500,13 @@ bool CScEngine::isValidScript(CScScript *script) { } ////////////////////////////////////////////////////////////////////////// -bool CScEngine::clearGlobals(bool includingNatives) { +bool ScEngine::clearGlobals(bool includingNatives) { _globals->CleanProps(includingNatives); return STATUS_OK; } ////////////////////////////////////////////////////////////////////////// -bool CScEngine::dbgSendScripts(IWmeDebugClient *client) { +bool ScEngine::dbgSendScripts(IWmeDebugClient *client) { // send global variables _globals->dbgSendVariables(client, WME_DBGVAR_GLOBAL, NULL, 0); @@ -526,7 +526,7 @@ bool CScEngine::dbgSendScripts(IWmeDebugClient *client) { } ////////////////////////////////////////////////////////////////////////// -bool CScEngine::addBreakpoint(const char *scriptFilename, int line) { +bool ScEngine::addBreakpoint(const char *scriptFilename, int line) { if (!_gameRef->getDebugMgr()->_enabled) return STATUS_OK; CScBreakpoint *bp = NULL; @@ -553,7 +553,7 @@ bool CScEngine::addBreakpoint(const char *scriptFilename, int line) { } ////////////////////////////////////////////////////////////////////////// -bool CScEngine::removeBreakpoint(const char *scriptFilename, int line) { +bool ScEngine::removeBreakpoint(const char *scriptFilename, int line) { if (!_gameRef->getDebugMgr()->_enabled) return STATUS_OK; for (int i = 0; i < _breakpoints.getSize(); i++) { @@ -578,7 +578,7 @@ bool CScEngine::removeBreakpoint(const char *scriptFilename, int line) { } ////////////////////////////////////////////////////////////////////////// -bool CScEngine::refreshScriptBreakpoints() { +bool ScEngine::refreshScriptBreakpoints() { if (!_gameRef->getDebugMgr()->_enabled) return STATUS_OK; for (int i = 0; i < _scripts.getSize(); i++) { @@ -588,7 +588,7 @@ bool CScEngine::refreshScriptBreakpoints() { } ////////////////////////////////////////////////////////////////////////// -bool CScEngine::refreshScriptBreakpoints(CScScript *script) { +bool ScEngine::refreshScriptBreakpoints(ScScript *script) { if (!_gameRef->getDebugMgr()->_enabled) return STATUS_OK; if (!script || !script->_filename) return STATUS_FAILED; @@ -605,7 +605,7 @@ bool CScEngine::refreshScriptBreakpoints(CScScript *script) { } ////////////////////////////////////////////////////////////////////////// -bool CScEngine::saveBreakpoints() { +bool ScEngine::saveBreakpoints() { if (!_gameRef->getDebugMgr()->_enabled) return STATUS_OK; @@ -628,7 +628,7 @@ bool CScEngine::saveBreakpoints() { } ////////////////////////////////////////////////////////////////////////// -bool CScEngine::loadBreakpoints() { +bool ScEngine::loadBreakpoints() { if (!_gameRef->getDebugMgr()->_enabled) return STATUS_OK; char key[100]; @@ -639,8 +639,8 @@ bool CScEngine::loadBreakpoints() { sprintf(key, "Breakpoint%d", i); AnsiString breakpoint = _gameRef->_registry->readString("Debug", key, ""); - char *path = CBUtils::strEntry(0, breakpoint.c_str(), ':'); - char *line = CBUtils::strEntry(1, breakpoint.c_str(), ':'); + char *path = BaseUtils::strEntry(0, breakpoint.c_str(), ':'); + char *line = BaseUtils::strEntry(1, breakpoint.c_str(), ':'); if (path != NULL && line != NULL) addBreakpoint(path, atoi(line)); delete[] path; @@ -654,7 +654,7 @@ bool CScEngine::loadBreakpoints() { ////////////////////////////////////////////////////////////////////////// -void CScEngine::addScriptTime(const char *filename, uint32 time) { +void ScEngine::addScriptTime(const char *filename, uint32 time) { if (!_isProfiling) return; AnsiString fileName = filename; @@ -664,19 +664,19 @@ void CScEngine::addScriptTime(const char *filename, uint32 time) { ////////////////////////////////////////////////////////////////////////// -void CScEngine::enableProfiling() { +void ScEngine::enableProfiling() { if (_isProfiling) return; // destroy old data, if any _scriptTimes.clear(); - _profilingStartTime = CBPlatform::getTime(); + _profilingStartTime = BasePlatform::getTime(); _isProfiling = true; } ////////////////////////////////////////////////////////////////////////// -void CScEngine::disableProfiling() { +void ScEngine::disableProfiling() { if (!_isProfiling) return; dumpStats(); @@ -685,9 +685,9 @@ void CScEngine::disableProfiling() { ////////////////////////////////////////////////////////////////////////// -void CScEngine::dumpStats() { +void ScEngine::dumpStats() { error("DumpStats not ported to ScummVM yet"); - /* uint32 totalTime = CBPlatform::getTime() - _profilingStartTime; + /* uint32 totalTime = BasePlatform::getTime() - _profilingStartTime; typedef std::vector > TimeVector; TimeVector times; diff --git a/engines/wintermute/base/scriptables/script_engine.h b/engines/wintermute/base/scriptables/script_engine.h index 81dc13a73c..06d61b4156 100644 --- a/engines/wintermute/base/scriptables/script_engine.h +++ b/engines/wintermute/base/scriptables/script_engine.h @@ -39,16 +39,16 @@ namespace WinterMute { #define MAX_CACHED_SCRIPTS 20 -class CScScript; -class CScValue; -class CBObject; -class CBScriptHolder; -class CScEngine : public CBBase { +class ScScript; +class ScValue; +class BaseObject; +class BaseScriptHolder; +class ScEngine : public BaseClass { public: class CScCachedScript { public: CScCachedScript(const char *filename, byte *buffer, uint32 size) { - _timestamp = CBPlatform::getTime(); + _timestamp = BasePlatform::getTime(); _buffer = new byte[size]; if (_buffer) memcpy(_buffer, buffer, size); _size = size; @@ -76,7 +76,7 @@ public: } Common::String _filename; - CBArray _lines; + BaseArray _lines; }; @@ -85,42 +85,42 @@ public: public: bool dbgSendScripts(IWmeDebugClient *client); - CBArray _breakpoints; + BaseArray _breakpoints; bool addBreakpoint(const char *scriptFilename, int line); bool removeBreakpoint(const char *scriptFilename, int line); bool refreshScriptBreakpoints(); - bool refreshScriptBreakpoints(CScScript *script); + bool refreshScriptBreakpoints(ScScript *script); bool saveBreakpoints(); bool loadBreakpoints(); bool clearGlobals(bool includingNatives = false); bool tickUnbreakable(); bool removeFinishedScripts(); - bool isValidScript(CScScript *script); + bool isValidScript(ScScript *script); - CScScript *_currentScript; + ScScript *_currentScript; bool resumeAll(); bool pauseAll(); void editorCleanup(); - bool resetObject(CBObject *Object); - bool resetScript(CScScript *script); + bool resetObject(BaseObject *Object); + bool resetScript(ScScript *script); bool emptyScriptCache(); byte *getCompiledScript(const char *filename, uint32 *outSize, bool ignoreCache = false); - DECLARE_PERSISTENT(CScEngine, CBBase) + DECLARE_PERSISTENT(ScEngine, BaseClass) bool cleanup(); int getNumScripts(int *running = NULL, int *waiting = NULL, int *persistent = NULL); bool tick(); - CScValue *_globals; - CScScript *runScript(const char *filename, CBScriptHolder *owner = NULL); + ScValue *_globals; + ScScript *runScript(const char *filename, BaseScriptHolder *owner = NULL); static const bool _compilerAvailable = false; - CScEngine(CBGame *inGame); - virtual ~CScEngine(); + ScEngine(BaseGame *inGame); + virtual ~ScEngine(); static byte *loadFile(void *data, char *filename, uint32 *size); static void closeFile(void *data, byte *buffer); static void parseElement(void *data, int line, int type, void *elementData); - CBArray _scripts; + BaseArray _scripts; void enableProfiling(); void disableProfiling(); diff --git a/engines/wintermute/base/scriptables/script_ext_array.cpp b/engines/wintermute/base/scriptables/script_ext_array.cpp index a1b8249cb1..41059b2d80 100644 --- a/engines/wintermute/base/scriptables/script_ext_array.cpp +++ b/engines/wintermute/base/scriptables/script_ext_array.cpp @@ -34,16 +34,16 @@ namespace WinterMute { -IMPLEMENT_PERSISTENT(CSXArray, false) +IMPLEMENT_PERSISTENT(SXArray, false) -CBScriptable *makeSXArray(CBGame *inGame, CScStack *stack) { - return new CSXArray(inGame, stack); +BaseScriptable *makeSXArray(BaseGame *inGame, ScStack *stack) { + return new SXArray(inGame, stack); } ////////////////////////////////////////////////////////////////////////// -CSXArray::CSXArray(CBGame *inGame, CScStack *stack): CBScriptable(inGame) { +SXArray::SXArray(BaseGame *inGame, ScStack *stack): BaseScriptable(inGame) { _length = 0; - _values = new CScValue(_gameRef); + _values = new ScValue(_gameRef); int numParams = stack->pop()->getInt(0); @@ -59,27 +59,27 @@ CSXArray::CSXArray(CBGame *inGame, CScStack *stack): CBScriptable(inGame) { } ////////////////////////////////////////////////////////////////////////// -CSXArray::CSXArray(CBGame *inGame): CBScriptable(inGame) { +SXArray::SXArray(BaseGame *inGame): BaseScriptable(inGame) { _length = 0; - _values = new CScValue(_gameRef); + _values = new ScValue(_gameRef); } ////////////////////////////////////////////////////////////////////////// -CSXArray::~CSXArray() { +SXArray::~SXArray() { delete _values; _values = NULL; } ////////////////////////////////////////////////////////////////////////// -const char *CSXArray::scToString() { +const char *SXArray::scToString() { static char dummy[32768]; // TODO: Get rid of static. strcpy(dummy, ""); char propName[20]; for (int i = 0; i < _length; i++) { sprintf(propName, "%d", i); - CScValue *val = _values->getProp(propName); + ScValue *val = _values->getProp(propName); if (val) { if (strlen(dummy) + strlen(val->getString()) < 32768) { strcat(dummy, val->getString()); @@ -93,7 +93,7 @@ const char *CSXArray::scToString() { ////////////////////////////////////////////////////////////////////////// -bool CSXArray::scCallMethod(CScScript *script, CScStack *stack, CScStack *thisStack, const char *name) { +bool SXArray::scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack, const char *name) { ////////////////////////////////////////////////////////////////////////// // Push ////////////////////////////////////////////////////////////////////////// @@ -134,7 +134,7 @@ bool CSXArray::scCallMethod(CScScript *script, CScStack *stack, CScStack *thisSt ////////////////////////////////////////////////////////////////////////// -CScValue *CSXArray::scGetProperty(const char *name) { +ScValue *SXArray::scGetProperty(const char *name) { _scValue->setNULL(); ////////////////////////////////////////////////////////////////////////// @@ -166,7 +166,7 @@ CScValue *CSXArray::scGetProperty(const char *name) { ////////////////////////////////////////////////////////////////////////// -bool CSXArray::scSetProperty(const char *name, CScValue *value) { +bool SXArray::scSetProperty(const char *name, ScValue *value) { ////////////////////////////////////////////////////////////////////////// // Length ////////////////////////////////////////////////////////////////////////// @@ -199,8 +199,8 @@ bool CSXArray::scSetProperty(const char *name, CScValue *value) { ////////////////////////////////////////////////////////////////////////// -bool CSXArray::persist(CBPersistMgr *persistMgr) { - CBScriptable::persist(persistMgr); +bool SXArray::persist(BasePersistenceManager *persistMgr) { + BaseScriptable::persist(persistMgr); persistMgr->transfer(TMEMBER(_length)); persistMgr->transfer(TMEMBER(_values)); @@ -210,7 +210,7 @@ bool CSXArray::persist(CBPersistMgr *persistMgr) { ////////////////////////////////////////////////////////////////////////// -bool CSXArray::validNumber(const char *origStr, char *outStr) { +bool SXArray::validNumber(const char *origStr, char *outStr) { bool isNumber = true; for (uint32 i = 0; i < strlen(origStr); i++) { if (!(origStr[i] >= '0' && origStr[i] <= '9')) { @@ -227,7 +227,7 @@ bool CSXArray::validNumber(const char *origStr, char *outStr) { } ////////////////////////////////////////////////////////////////////////// -bool CSXArray::push(CScValue *val) { +bool SXArray::push(ScValue *val) { char paramName[20]; _length++; sprintf(paramName, "%d", _length - 1); diff --git a/engines/wintermute/base/scriptables/script_ext_array.h b/engines/wintermute/base/scriptables/script_ext_array.h index b873416572..3f8d703f85 100644 --- a/engines/wintermute/base/scriptables/script_ext_array.h +++ b/engines/wintermute/base/scriptables/script_ext_array.h @@ -33,20 +33,20 @@ namespace WinterMute { -class CSXArray : public CBScriptable { +class SXArray : public BaseScriptable { public: - bool push(CScValue *Val); + bool push(ScValue *Val); bool validNumber(const char *origStr, char *outStr); - DECLARE_PERSISTENT(CSXArray, CBScriptable) - CSXArray(CBGame *inGame, CScStack *stack); - CSXArray(CBGame *inGame); - virtual ~CSXArray(); - CScValue *scGetProperty(const char *name); - bool scSetProperty(const char *name, CScValue *value); - bool scCallMethod(CScScript *script, CScStack *stack, CScStack *thisStack, const char *name); + DECLARE_PERSISTENT(SXArray, BaseScriptable) + SXArray(BaseGame *inGame, ScStack *stack); + SXArray(BaseGame *inGame); + virtual ~SXArray(); + ScValue *scGetProperty(const char *name); + bool scSetProperty(const char *name, ScValue *value); + bool scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack, const char *name); const char *scToString(); int _length; - CScValue *_values; + ScValue *_values; }; } // end of namespace WinterMute diff --git a/engines/wintermute/base/scriptables/script_ext_date.cpp b/engines/wintermute/base/scriptables/script_ext_date.cpp index 211c0d34f0..322fb9bc5b 100644 --- a/engines/wintermute/base/scriptables/script_ext_date.cpp +++ b/engines/wintermute/base/scriptables/script_ext_date.cpp @@ -32,19 +32,19 @@ namespace WinterMute { -IMPLEMENT_PERSISTENT(CSXDate, false) +IMPLEMENT_PERSISTENT(SXDate, false) -CBScriptable *makeSXDate(CBGame *inGame, CScStack *stack) { - return new CSXDate(inGame, stack); +BaseScriptable *makeSXDate(BaseGame *inGame, ScStack *stack) { + return new SXDate(inGame, stack); } ////////////////////////////////////////////////////////////////////////// -CSXDate::CSXDate(CBGame *inGame, CScStack *stack): CBScriptable(inGame) { +SXDate::SXDate(BaseGame *inGame, ScStack *stack): BaseScriptable(inGame) { stack->correctParams(6); memset(&_tm, 0, sizeof(_tm)); - CScValue *valYear = stack->pop(); + ScValue *valYear = stack->pop(); _tm.tm_year = valYear->getInt() - 1900; _tm.tm_mon = stack->pop()->getInt() - 1; _tm.tm_mday = stack->pop()->getInt(); @@ -59,12 +59,12 @@ CSXDate::CSXDate(CBGame *inGame, CScStack *stack): CBScriptable(inGame) { ////////////////////////////////////////////////////////////////////////// -CSXDate::~CSXDate() { +SXDate::~SXDate() { } ////////////////////////////////////////////////////////////////////////// -const char *CSXDate::scToString() { +const char *SXDate::scToString() { // TODO: Make this more stringy, and less ISO 8601-like _strRep.format("%04d-%02d-%02d - %02d:%02d:%02d", _tm.tm_year, _tm.tm_mon, _tm.tm_mday, _tm.tm_hour, _tm.tm_min, _tm.tm_sec); return _strRep.c_str(); @@ -75,7 +75,7 @@ const char *CSXDate::scToString() { ////////////////////////////////////////////////////////////////////////// -bool CSXDate::scCallMethod(CScScript *script, CScStack *stack, CScStack *thisStack, const char *name) { +bool SXDate::scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack, const char *name) { ////////////////////////////////////////////////////////////////////////// // GetYear ////////////////////////////////////////////////////////////////////////// @@ -207,7 +207,7 @@ bool CSXDate::scCallMethod(CScScript *script, CScStack *stack, CScStack *thisSta ////////////////////////////////////////////////////////////////////////// -CScValue *CSXDate::scGetProperty(const char *name) { +ScValue *SXDate::scGetProperty(const char *name) { _scValue->setNULL(); ////////////////////////////////////////////////////////////////////////// @@ -223,7 +223,7 @@ CScValue *CSXDate::scGetProperty(const char *name) { ////////////////////////////////////////////////////////////////////////// -bool CSXDate::scSetProperty(const char *name, CScValue *value) { +bool SXDate::scSetProperty(const char *name, ScValue *value) { /* ////////////////////////////////////////////////////////////////////////// // Name @@ -238,9 +238,9 @@ bool CSXDate::scSetProperty(const char *name, CScValue *value) { ////////////////////////////////////////////////////////////////////////// -bool CSXDate::persist(CBPersistMgr *persistMgr) { +bool SXDate::persist(BasePersistenceManager *persistMgr) { - CBScriptable::persist(persistMgr); + BaseScriptable::persist(persistMgr); persistMgr->transfer(TMEMBER(_tm.tm_year)); persistMgr->transfer(TMEMBER(_tm.tm_mon)); persistMgr->transfer(TMEMBER(_tm.tm_mday)); @@ -251,9 +251,9 @@ bool CSXDate::persist(CBPersistMgr *persistMgr) { } ////////////////////////////////////////////////////////////////////////// -int CSXDate::scCompare(CBScriptable *Value) { +int SXDate::scCompare(BaseScriptable *Value) { TimeDate time1 = _tm; - TimeDate time2 = ((CSXDate *)Value)->_tm; + TimeDate time2 = ((SXDate *)Value)->_tm; if (time1.tm_year < time2.tm_year) { return -1; diff --git a/engines/wintermute/base/scriptables/script_ext_date.h b/engines/wintermute/base/scriptables/script_ext_date.h index 82f6af1f1d..b2df4abe94 100644 --- a/engines/wintermute/base/scriptables/script_ext_date.h +++ b/engines/wintermute/base/scriptables/script_ext_date.h @@ -34,15 +34,15 @@ namespace WinterMute { -class CSXDate : public CBScriptable { +class SXDate : public BaseScriptable { public: - int scCompare(CBScriptable *Value); - DECLARE_PERSISTENT(CSXDate, CBScriptable) - CSXDate(CBGame *inGame, CScStack *Stack); - virtual ~CSXDate(); - CScValue *scGetProperty(const char *name); - bool scSetProperty(const char *name, CScValue *value); - bool scCallMethod(CScScript *script, CScStack *stack, CScStack *thisStack, const char *name); + int scCompare(BaseScriptable *Value); + DECLARE_PERSISTENT(SXDate, BaseScriptable) + SXDate(BaseGame *inGame, ScStack *Stack); + virtual ~SXDate(); + ScValue *scGetProperty(const char *name); + bool scSetProperty(const char *name, ScValue *value); + bool scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack, const char *name); const char *scToString(); char *_string; TimeDate _tm; diff --git a/engines/wintermute/base/scriptables/script_ext_file.cpp b/engines/wintermute/base/scriptables/script_ext_file.cpp index 58c0416b43..ffa362a938 100644 --- a/engines/wintermute/base/scriptables/script_ext_file.cpp +++ b/engines/wintermute/base/scriptables/script_ext_file.cpp @@ -42,19 +42,19 @@ namespace WinterMute { -IMPLEMENT_PERSISTENT(CSXFile, false) +IMPLEMENT_PERSISTENT(SXFile, false) -CBScriptable *makeSXFile(CBGame *inGame, CScStack *stack) { - return new CSXFile(inGame, stack); +BaseScriptable *makeSXFile(BaseGame *inGame, ScStack *stack) { + return new SXFile(inGame, stack); } ////////////////////////////////////////////////////////////////////////// -CSXFile::CSXFile(CBGame *inGame, CScStack *stack): CBScriptable(inGame) { +SXFile::SXFile(BaseGame *inGame, ScStack *stack): BaseScriptable(inGame) { stack->correctParams(1); - CScValue *Val = stack->pop(); + ScValue *Val = stack->pop(); _filename = NULL; - if (!Val->isNULL()) CBUtils::setString(&_filename, Val->getString()); + if (!Val->isNULL()) BaseUtils::setString(&_filename, Val->getString()); _readFile = NULL; _writeFile = NULL; @@ -65,12 +65,12 @@ CSXFile::CSXFile(CBGame *inGame, CScStack *stack): CBScriptable(inGame) { ////////////////////////////////////////////////////////////////////////// -CSXFile::~CSXFile() { +SXFile::~SXFile() { cleanup(); } ////////////////////////////////////////////////////////////////////////// -void CSXFile::cleanup() { +void SXFile::cleanup() { delete[] _filename; _filename = NULL; close(); @@ -78,7 +78,7 @@ void CSXFile::cleanup() { ////////////////////////////////////////////////////////////////////////// -void CSXFile::close() { +void SXFile::close() { if (_readFile) { _gameRef->_fileManager->closeFile(_readFile); _readFile = NULL; @@ -93,14 +93,14 @@ void CSXFile::close() { } ////////////////////////////////////////////////////////////////////////// -const char *CSXFile::scToString() { +const char *SXFile::scToString() { if (_filename) return _filename; else return "[file object]"; } #define FILE_BUFFER_SIZE 32768 ////////////////////////////////////////////////////////////////////////// -bool CSXFile::scCallMethod(CScScript *script, CScStack *stack, CScStack *thisStack, const char *name) { +bool SXFile::scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack, const char *name) { ////////////////////////////////////////////////////////////////////////// // SetFilename ////////////////////////////////////////////////////////////////////////// @@ -108,7 +108,7 @@ bool CSXFile::scCallMethod(CScScript *script, CScStack *stack, CScStack *thisSta stack->correctParams(1); const char *filename = stack->pop()->getString(); cleanup(); - CBUtils::setString(&_filename, filename); + BaseUtils::setString(&_filename, filename); stack->pushNULL(); return STATUS_OK; } @@ -182,7 +182,7 @@ bool CSXFile::scCallMethod(CScScript *script, CScStack *stack, CScStack *thisSta else if (strcmp(name, "Delete") == 0) { stack->correctParams(0); close(); - stack->pushBool(CBPlatform::deleteFile(_filename) != false); + stack->pushBool(BasePlatform::deleteFile(_filename) != false); return STATUS_OK; } @@ -195,7 +195,7 @@ bool CSXFile::scCallMethod(CScScript *script, CScStack *stack, CScStack *thisSta bool Overwrite = stack->pop()->getBool(true); close(); - stack->pushBool(CBPlatform::copyFile(_filename, Dest, !Overwrite) != false); + stack->pushBool(BasePlatform::copyFile(_filename, Dest, !Overwrite) != false); return STATUS_OK; } @@ -593,12 +593,12 @@ bool CSXFile::scCallMethod(CScScript *script, CScStack *stack, CScStack *thisSta } - else return CBScriptable::scCallMethod(script, stack, thisStack, name); + else return BaseScriptable::scCallMethod(script, stack, thisStack, name); } ////////////////////////////////////////////////////////////////////////// -CScValue *CSXFile::scGetProperty(const char *name) { +ScValue *SXFile::scGetProperty(const char *name) { _scValue->setNULL(); ////////////////////////////////////////////////////////////////////////// @@ -649,12 +649,12 @@ CScValue *CSXFile::scGetProperty(const char *name) { return _scValue; } - else return CBScriptable::scGetProperty(name); + else return BaseScriptable::scGetProperty(name); } ////////////////////////////////////////////////////////////////////////// -bool CSXFile::scSetProperty(const char *name, CScValue *value) { +bool SXFile::scSetProperty(const char *name, ScValue *value) { /* ////////////////////////////////////////////////////////////////////////// // Length @@ -672,11 +672,11 @@ bool CSXFile::scSetProperty(const char *name, CScValue *value) { } return STATUS_OK; } - else*/ return CBScriptable::scSetProperty(name, value); + else*/ return BaseScriptable::scSetProperty(name, value); } ////////////////////////////////////////////////////////////////////////// -uint32 CSXFile::getPos() { +uint32 SXFile::getPos() { if (_mode == 1 && _readFile) return _readFile->pos(); else if ((_mode == 2 || _mode == 3) && _writeFile) { @@ -689,11 +689,11 @@ uint32 CSXFile::getPos() { } ////////////////////////////////////////////////////////////////////////// -bool CSXFile::setPos(uint32 pos, int whence) { +bool SXFile::setPos(uint32 pos, int whence) { if (_mode == 1 && _readFile) return _readFile->seek(pos, whence); else if ((_mode == 2 || _mode == 3) && _writeFile) { - error("CSXFile - seeking in WriteFile not supported"); + error("SXFile - seeking in WriteFile not supported"); return false; // return fseek((FILE *)_writeFile, pos, (int)origin) == 0; } @@ -701,11 +701,11 @@ bool CSXFile::setPos(uint32 pos, int whence) { } ////////////////////////////////////////////////////////////////////////// -uint32 CSXFile::getLength() { +uint32 SXFile::getLength() { if (_mode == 1 && _readFile) return _readFile->size(); else if ((_mode == 2 || _mode == 3) && _writeFile) { - error("CSXFile - reading length for WriteFile not supported"); + error("SXFile - reading length for WriteFile not supported"); return 0; /* uint32 currentPos = ftell((FILE *)_writeFile); @@ -717,9 +717,9 @@ uint32 CSXFile::getLength() { } ////////////////////////////////////////////////////////////////////////// -bool CSXFile::persist(CBPersistMgr *persistMgr) { +bool SXFile::persist(BasePersistenceManager *persistMgr) { - CBScriptable::persist(persistMgr); + BaseScriptable::persist(persistMgr); persistMgr->transfer(TMEMBER(_filename)); persistMgr->transfer(TMEMBER(_mode)); @@ -767,12 +767,12 @@ bool CSXFile::persist(CBPersistMgr *persistMgr) { } // Should replace fopen(..., "wb+") and fopen(..., "w+") -Common::WriteStream *CSXFile::openForWrite(const Common::String &filename, bool binary) { +Common::WriteStream *SXFile::openForWrite(const Common::String &filename, bool binary) { error("SXFile::openForWrite - WriteFiles not supported"); } // Should replace fopen(..., "ab+") and fopen(..., "a+") -Common::WriteStream *CSXFile::openForAppend(const Common::String &filename, bool binary) { +Common::WriteStream *SXFile::openForAppend(const Common::String &filename, bool binary) { error("SXFile::openForAppend - WriteFiles not supported"); } diff --git a/engines/wintermute/base/scriptables/script_ext_file.h b/engines/wintermute/base/scriptables/script_ext_file.h index 5a6811fe57..4d78feb044 100644 --- a/engines/wintermute/base/scriptables/script_ext_file.h +++ b/engines/wintermute/base/scriptables/script_ext_file.h @@ -35,17 +35,17 @@ namespace WinterMute { -class CBFile; +class BaseFile; -class CSXFile : public CBScriptable { +class SXFile : public BaseScriptable { public: - DECLARE_PERSISTENT(CSXFile, CBScriptable) - CScValue *scGetProperty(const char *name); - bool scSetProperty(const char *name, CScValue *value); - bool scCallMethod(CScScript *script, CScStack *stack, CScStack *thisStack, const char *name); + DECLARE_PERSISTENT(SXFile, BaseScriptable) + ScValue *scGetProperty(const char *name); + bool scSetProperty(const char *name, ScValue *value); + bool scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack, const char *name); const char *scToString(); - CSXFile(CBGame *inGame, CScStack *Stack); - virtual ~CSXFile(); + SXFile(BaseGame *inGame, ScStack *Stack); + virtual ~SXFile(); private: Common::SeekableReadStream *_readFile; Common::WriteStream *_writeFile; diff --git a/engines/wintermute/base/scriptables/script_ext_math.cpp b/engines/wintermute/base/scriptables/script_ext_math.cpp index 22b08087b7..1c37a15aa9 100644 --- a/engines/wintermute/base/scriptables/script_ext_math.cpp +++ b/engines/wintermute/base/scriptables/script_ext_math.cpp @@ -40,26 +40,26 @@ namespace WinterMute { ////////////////////////////////////////////////////////////////////// -IMPLEMENT_PERSISTENT(CSXMath, true) +IMPLEMENT_PERSISTENT(SXMath, true) -CBScriptable *makeSXMath(CBGame *inGame) { - return new CSXMath(inGame); +BaseScriptable *makeSXMath(BaseGame *inGame) { + return new SXMath(inGame); } ////////////////////////////////////////////////////////////////////////// -CSXMath::CSXMath(CBGame *inGame): CBScriptable(inGame) { +SXMath::SXMath(BaseGame *inGame): BaseScriptable(inGame) { } ////////////////////////////////////////////////////////////////////////// -CSXMath::~CSXMath() { +SXMath::~SXMath() { } ////////////////////////////////////////////////////////////////////////// -bool CSXMath::scCallMethod(CScScript *script, CScStack *stack, CScStack *thisStack, const char *name) { +bool SXMath::scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack, const char *name) { ////////////////////////////////////////////////////////////////////////// // Abs ////////////////////////////////////////////////////////////////////////// @@ -250,7 +250,7 @@ bool CSXMath::scCallMethod(CScScript *script, CScStack *stack, CScStack *thisSta ////////////////////////////////////////////////////////////////////////// -CScValue *CSXMath::scGetProperty(const char *name) { +ScValue *SXMath::scGetProperty(const char *name) { _scValue->setNULL(); ////////////////////////////////////////////////////////////////////////// @@ -274,21 +274,21 @@ CScValue *CSXMath::scGetProperty(const char *name) { ////////////////////////////////////////////////////////////////////////// -double CSXMath::degreeToRadian(double value) { +double SXMath::degreeToRadian(double value) { return value * (M_PI / 180.0f); } ////////////////////////////////////////////////////////////////////////// -double CSXMath::radianToDegree(double value) { +double SXMath::radianToDegree(double value) { return value * (180.0f / M_PI); } ////////////////////////////////////////////////////////////////////////// -bool CSXMath::persist(CBPersistMgr *persistMgr) { +bool SXMath::persist(BasePersistenceManager *persistMgr) { - CBScriptable::persist(persistMgr); + BaseScriptable::persist(persistMgr); return STATUS_OK; } diff --git a/engines/wintermute/base/scriptables/script_ext_math.h b/engines/wintermute/base/scriptables/script_ext_math.h index 422521233f..393342e5ca 100644 --- a/engines/wintermute/base/scriptables/script_ext_math.h +++ b/engines/wintermute/base/scriptables/script_ext_math.h @@ -34,13 +34,13 @@ namespace WinterMute { -class CSXMath : public CBScriptable { +class SXMath : public BaseScriptable { public: - DECLARE_PERSISTENT(CSXMath, CBScriptable) - CSXMath(CBGame *inGame); - virtual ~CSXMath(); - virtual CScValue *scGetProperty(const char *name); - virtual bool scCallMethod(CScScript *script, CScStack *stack, CScStack *thisStack, const char *name); + DECLARE_PERSISTENT(SXMath, BaseScriptable) + SXMath(BaseGame *inGame); + virtual ~SXMath(); + virtual ScValue *scGetProperty(const char *name); + virtual bool scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack, const char *name); private: double degreeToRadian(double value); diff --git a/engines/wintermute/base/scriptables/script_ext_mem_buffer.cpp b/engines/wintermute/base/scriptables/script_ext_mem_buffer.cpp index df2bf188ff..79ba978002 100644 --- a/engines/wintermute/base/scriptables/script_ext_mem_buffer.cpp +++ b/engines/wintermute/base/scriptables/script_ext_mem_buffer.cpp @@ -35,14 +35,14 @@ namespace WinterMute { -IMPLEMENT_PERSISTENT(CSXMemBuffer, false) +IMPLEMENT_PERSISTENT(SXMemBuffer, false) -CBScriptable *makeSXMemBuffer(CBGame *inGame, CScStack *stack) { - return new CSXMemBuffer(inGame, stack); +BaseScriptable *makeSXMemBuffer(BaseGame *inGame, ScStack *stack) { + return new SXMemBuffer(inGame, stack); } ////////////////////////////////////////////////////////////////////////// -CSXMemBuffer::CSXMemBuffer(CBGame *inGame, CScStack *stack): CBScriptable(inGame) { +SXMemBuffer::SXMemBuffer(BaseGame *inGame, ScStack *stack): BaseScriptable(inGame) { stack->correctParams(1); _buffer = NULL; _size = 0; @@ -52,31 +52,31 @@ CSXMemBuffer::CSXMemBuffer(CBGame *inGame, CScStack *stack): CBScriptable(inGame } ////////////////////////////////////////////////////////////////////////// -CSXMemBuffer::CSXMemBuffer(CBGame *inGame, void *Buffer): CBScriptable(inGame) { +SXMemBuffer::SXMemBuffer(BaseGame *inGame, void *Buffer): BaseScriptable(inGame) { _size = 0; _buffer = Buffer; } ////////////////////////////////////////////////////////////////////////// -CSXMemBuffer::~CSXMemBuffer() { +SXMemBuffer::~SXMemBuffer() { cleanup(); } ////////////////////////////////////////////////////////////////////////// -void *CSXMemBuffer::scToMemBuffer() { +void *SXMemBuffer::scToMemBuffer() { return _buffer; } ////////////////////////////////////////////////////////////////////////// -void CSXMemBuffer::cleanup() { +void SXMemBuffer::cleanup() { if (_size) free(_buffer); _buffer = NULL; _size = 0; } ////////////////////////////////////////////////////////////////////////// -bool CSXMemBuffer::resize(int newSize) { +bool SXMemBuffer::resize(int newSize) { int oldSize = _size; if (_size == 0) { @@ -102,7 +102,7 @@ bool CSXMemBuffer::resize(int newSize) { } ////////////////////////////////////////////////////////////////////////// -bool CSXMemBuffer::checkBounds(CScScript *script, int start, int length) { +bool SXMemBuffer::checkBounds(ScScript *script, int start, int length) { if (_buffer == NULL) { script->runtimeError("Cannot use Set/Get methods on an uninitialized memory buffer"); return false; @@ -118,13 +118,13 @@ bool CSXMemBuffer::checkBounds(CScScript *script, int start, int length) { } ////////////////////////////////////////////////////////////////////////// -const char *CSXMemBuffer::scToString() { +const char *SXMemBuffer::scToString() { return "[membuffer object]"; } ////////////////////////////////////////////////////////////////////////// -bool CSXMemBuffer::scCallMethod(CScScript *script, CScStack *stack, CScStack *thisStack, const char *name) { +bool SXMemBuffer::scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack, const char *name) { ////////////////////////////////////////////////////////////////////////// // SetSize ////////////////////////////////////////////////////////////////////////// @@ -264,7 +264,7 @@ bool CSXMemBuffer::scCallMethod(CScScript *script, CScStack *stack, CScStack *th stack->pushNULL(); else { void *pointer = *(void **)((byte *)_buffer + start); - CSXMemBuffer *buf = new CSXMemBuffer(_gameRef, pointer); + SXMemBuffer *buf = new SXMemBuffer(_gameRef, pointer); stack->pushNative(buf, false); } return STATUS_OK; @@ -395,7 +395,7 @@ bool CSXMemBuffer::scCallMethod(CScScript *script, CScStack *stack, CScStack *th else if (strcmp(name, "SetPointer") == 0) { stack->correctParams(2); int start = stack->pop()->getInt(); - /* CScValue *Val = */ stack->pop(); + /* ScValue *Val = */ stack->pop(); if (!checkBounds(script, start, sizeof(void *))) stack->pushBool(false); @@ -433,7 +433,7 @@ bool CSXMemBuffer::scCallMethod(CScScript *script, CScStack *stack, CScStack *th ////////////////////////////////////////////////////////////////////////// -CScValue *CSXMemBuffer::scGetProperty(const char *name) { +ScValue *SXMemBuffer::scGetProperty(const char *name) { _scValue->setNULL(); ////////////////////////////////////////////////////////////////////////// @@ -452,12 +452,12 @@ CScValue *CSXMemBuffer::scGetProperty(const char *name) { return _scValue; } - else return CBScriptable::scGetProperty(name); + else return BaseScriptable::scGetProperty(name); } ////////////////////////////////////////////////////////////////////////// -bool CSXMemBuffer::scSetProperty(const char *name, CScValue *value) { +bool SXMemBuffer::scSetProperty(const char *name, ScValue *value) { /* ////////////////////////////////////////////////////////////////////////// // Length @@ -475,14 +475,14 @@ bool CSXMemBuffer::scSetProperty(const char *name, CScValue *value) { } return STATUS_OK; } - else*/ return CBScriptable::scSetProperty(name, value); + else*/ return BaseScriptable::scSetProperty(name, value); } ////////////////////////////////////////////////////////////////////////// -bool CSXMemBuffer::persist(CBPersistMgr *persistMgr) { +bool SXMemBuffer::persist(BasePersistenceManager *persistMgr) { - CBScriptable::persist(persistMgr); + BaseScriptable::persist(persistMgr); persistMgr->transfer(TMEMBER(_size)); @@ -500,7 +500,7 @@ bool CSXMemBuffer::persist(CBPersistMgr *persistMgr) { ////////////////////////////////////////////////////////////////////////// -int CSXMemBuffer::scCompare(CBScriptable *val) { +int SXMemBuffer::scCompare(BaseScriptable *val) { if (_buffer == val->scToMemBuffer()) return 0; else return 1; } diff --git a/engines/wintermute/base/scriptables/script_ext_mem_buffer.h b/engines/wintermute/base/scriptables/script_ext_mem_buffer.h index a9d78e50e4..c325181e3f 100644 --- a/engines/wintermute/base/scriptables/script_ext_mem_buffer.h +++ b/engines/wintermute/base/scriptables/script_ext_mem_buffer.h @@ -34,24 +34,24 @@ namespace WinterMute { -class CSXMemBuffer : public CBScriptable { +class SXMemBuffer : public BaseScriptable { public: - virtual int scCompare(CBScriptable *Val); - DECLARE_PERSISTENT(CSXMemBuffer, CBScriptable) - CScValue *scGetProperty(const char *name); - bool scSetProperty(const char *name, CScValue *value); - bool scCallMethod(CScScript *script, CScStack *stack, CScStack *thisStack, const char *name); + virtual int scCompare(BaseScriptable *Val); + DECLARE_PERSISTENT(SXMemBuffer, BaseScriptable) + ScValue *scGetProperty(const char *name); + bool scSetProperty(const char *name, ScValue *value); + bool scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack, const char *name); const char *scToString(); - CSXMemBuffer(CBGame *inGame, CScStack *stack); - CSXMemBuffer(CBGame *inGame, void *buffer); - virtual ~CSXMemBuffer(); + SXMemBuffer(BaseGame *inGame, ScStack *stack); + SXMemBuffer(BaseGame *inGame, void *buffer); + virtual ~SXMemBuffer(); virtual void *scToMemBuffer(); int _size; private: bool resize(int newSize); void *_buffer; void cleanup(); - bool checkBounds(CScScript *script, int start, int length); + bool checkBounds(ScScript *script, int start, int length); }; } // end of namespace WinterMute diff --git a/engines/wintermute/base/scriptables/script_ext_object.cpp b/engines/wintermute/base/scriptables/script_ext_object.cpp index cb0d32d1a3..b72e3e4b97 100644 --- a/engines/wintermute/base/scriptables/script_ext_object.cpp +++ b/engines/wintermute/base/scriptables/script_ext_object.cpp @@ -36,14 +36,14 @@ namespace WinterMute { // Construction/Destruction ////////////////////////////////////////////////////////////////////// -IMPLEMENT_PERSISTENT(CSXObject, false) +IMPLEMENT_PERSISTENT(SXObject, false) -CBScriptable *makeSXObject(CBGame *inGame, CScStack *stack) { - return new CSXObject(inGame, stack); +BaseScriptable *makeSXObject(BaseGame *inGame, ScStack *stack) { + return new SXObject(inGame, stack); } ////////////////////////////////////////////////////////////////////////// -CSXObject::CSXObject(CBGame *inGame, CScStack *stack): CBObject(inGame) { +SXObject::SXObject(BaseGame *inGame, ScStack *stack): BaseObject(inGame) { int numParams = stack->pop()->getInt(0); for (int i = 0; i < numParams; i++) { addScript(stack->pop()->getString()); @@ -52,14 +52,14 @@ CSXObject::CSXObject(CBGame *inGame, CScStack *stack): CBObject(inGame) { ////////////////////////////////////////////////////////////////////////// -CSXObject::~CSXObject() { +SXObject::~SXObject() { } ////////////////////////////////////////////////////////////////////////// -bool CSXObject::persist(CBPersistMgr *persistMgr) { - CBObject::persist(persistMgr); +bool SXObject::persist(BasePersistenceManager *persistMgr) { + BaseObject::persist(persistMgr); return STATUS_OK; } diff --git a/engines/wintermute/base/scriptables/script_ext_object.h b/engines/wintermute/base/scriptables/script_ext_object.h index b4e869d5b3..d744c58042 100644 --- a/engines/wintermute/base/scriptables/script_ext_object.h +++ b/engines/wintermute/base/scriptables/script_ext_object.h @@ -34,11 +34,11 @@ namespace WinterMute { -class CSXObject : public CBObject { +class SXObject : public BaseObject { public: - DECLARE_PERSISTENT(CSXObject, CBObject) - CSXObject(CBGame *inGame, CScStack *Stack); - virtual ~CSXObject(); + DECLARE_PERSISTENT(SXObject, BaseObject) + SXObject(BaseGame *inGame, ScStack *Stack); + virtual ~SXObject(); }; diff --git a/engines/wintermute/base/scriptables/script_ext_string.cpp b/engines/wintermute/base/scriptables/script_ext_string.cpp index bd7541fadd..e645772b6f 100644 --- a/engines/wintermute/base/scriptables/script_ext_string.cpp +++ b/engines/wintermute/base/scriptables/script_ext_string.cpp @@ -37,19 +37,19 @@ namespace WinterMute { -IMPLEMENT_PERSISTENT(CSXString, false) +IMPLEMENT_PERSISTENT(SXString, false) -CBScriptable *makeSXString(CBGame *inGame, CScStack *stack) { - return new CSXString(inGame, stack); +BaseScriptable *makeSXString(BaseGame *inGame, ScStack *stack) { + return new SXString(inGame, stack); } ////////////////////////////////////////////////////////////////////////// -CSXString::CSXString(CBGame *inGame, CScStack *stack): CBScriptable(inGame) { +SXString::SXString(BaseGame *inGame, ScStack *stack): BaseScriptable(inGame) { _string = NULL; _capacity = 0; stack->correctParams(1); - CScValue *val = stack->pop(); + ScValue *val = stack->pop(); if (val->isInt()) { _capacity = MAX(0, val->getInt()); @@ -66,13 +66,13 @@ CSXString::CSXString(CBGame *inGame, CScStack *stack): CBScriptable(inGame) { ////////////////////////////////////////////////////////////////////////// -CSXString::~CSXString() { +SXString::~SXString() { if (_string) delete [] _string; } ////////////////////////////////////////////////////////////////////////// -void CSXString::setStringVal(const char *val) { +void SXString::setStringVal(const char *val) { int len = strlen(val); if (len >= _capacity) { _capacity = len + 1; @@ -86,20 +86,20 @@ void CSXString::setStringVal(const char *val) { ////////////////////////////////////////////////////////////////////////// -const char *CSXString::scToString() { +const char *SXString::scToString() { if (_string) return _string; else return "[null string]"; } ////////////////////////////////////////////////////////////////////////// -void CSXString::scSetString(const char *val) { +void SXString::scSetString(const char *val) { setStringVal(val); } ////////////////////////////////////////////////////////////////////////// -bool CSXString::scCallMethod(CScScript *script, CScStack *stack, CScStack *thisStack, const char *name) { +bool SXString::scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack, const char *name) { ////////////////////////////////////////////////////////////////////////// // Substring ////////////////////////////////////////////////////////////////////////// @@ -108,7 +108,7 @@ bool CSXString::scCallMethod(CScScript *script, CScStack *stack, CScStack *thisS int start = stack->pop()->getInt(); int end = stack->pop()->getInt(); - if (end < start) CBUtils::swap(&start, &end); + if (end < start) BaseUtils::swap(&start, &end); //try { WideString str; @@ -138,7 +138,7 @@ bool CSXString::scCallMethod(CScScript *script, CScStack *stack, CScStack *thisS stack->correctParams(2); int start = stack->pop()->getInt(); - CScValue *val = stack->pop(); + ScValue *val = stack->pop(); int len = val->getInt(); if (!val->isNULL() && len <= 0) { @@ -245,11 +245,11 @@ bool CSXString::scCallMethod(CScScript *script, CScStack *stack, CScStack *thisS ////////////////////////////////////////////////////////////////////////// else if (strcmp(name, "Split") == 0) { stack->correctParams(1); - CScValue *val = stack->pop(); + ScValue *val = stack->pop(); char separators[MAX_PATH_LENGTH] = ","; if (!val->isNULL()) strcpy(separators, val->getString()); - CSXArray *array = new CSXArray(_gameRef); + SXArray *array = new SXArray(_gameRef); if (!array) { stack->pushNULL(); return STATUS_OK; @@ -299,9 +299,9 @@ bool CSXString::scCallMethod(CScScript *script, CScStack *stack, CScStack *thisS WideString &part = (*it); if (_gameRef->_textEncoding == TEXT_UTF8) - val = new CScValue(_gameRef, StringUtil::wideToUtf8(part).c_str()); + val = new ScValue(_gameRef, StringUtil::wideToUtf8(part).c_str()); else - val = new CScValue(_gameRef, StringUtil::wideToAnsi(part).c_str()); + val = new ScValue(_gameRef, StringUtil::wideToAnsi(part).c_str()); array->push(val); delete val; @@ -317,7 +317,7 @@ bool CSXString::scCallMethod(CScScript *script, CScStack *stack, CScStack *thisS ////////////////////////////////////////////////////////////////////////// -CScValue *CSXString::scGetProperty(const char *name) { +ScValue *SXString::scGetProperty(const char *name) { _scValue->setNULL(); ////////////////////////////////////////////////////////////////////////// @@ -352,7 +352,7 @@ CScValue *CSXString::scGetProperty(const char *name) { ////////////////////////////////////////////////////////////////////////// -bool CSXString::scSetProperty(const char *name, CScValue *value) { +bool SXString::scSetProperty(const char *name, ScValue *value) { ////////////////////////////////////////////////////////////////////////// // Capacity ////////////////////////////////////////////////////////////////////////// @@ -377,9 +377,9 @@ bool CSXString::scSetProperty(const char *name, CScValue *value) { ////////////////////////////////////////////////////////////////////////// -bool CSXString::persist(CBPersistMgr *persistMgr) { +bool SXString::persist(BasePersistenceManager *persistMgr) { - CBScriptable::persist(persistMgr); + BaseScriptable::persist(persistMgr); persistMgr->transfer(TMEMBER(_capacity)); @@ -397,8 +397,8 @@ bool CSXString::persist(CBPersistMgr *persistMgr) { ////////////////////////////////////////////////////////////////////////// -int CSXString::scCompare(CBScriptable *val) { - return strcmp(_string, ((CSXString *)val)->_string); +int SXString::scCompare(BaseScriptable *val) { + return strcmp(_string, ((SXString *)val)->_string); } } // end of namespace WinterMute diff --git a/engines/wintermute/base/scriptables/script_ext_string.h b/engines/wintermute/base/scriptables/script_ext_string.h index 52a1524dde..9a3bbfc80b 100644 --- a/engines/wintermute/base/scriptables/script_ext_string.h +++ b/engines/wintermute/base/scriptables/script_ext_string.h @@ -34,19 +34,19 @@ namespace WinterMute { -class CSXString : public CBScriptable { +class SXString : public BaseScriptable { public: - virtual int scCompare(CBScriptable *Val); - DECLARE_PERSISTENT(CSXString, CBScriptable) - CScValue *scGetProperty(const char *name); - bool scSetProperty(const char *name, CScValue *value); - bool scCallMethod(CScScript *script, CScStack *stack, CScStack *thisStack, const char *name); + virtual int scCompare(BaseScriptable *Val); + DECLARE_PERSISTENT(SXString, BaseScriptable) + ScValue *scGetProperty(const char *name); + bool scSetProperty(const char *name, ScValue *value); + bool scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack, const char *name); void scSetString(const char *val); const char *scToString(); void setStringVal(const char *val); - CSXString(CBGame *inGame, CScStack *Stack); - virtual ~CSXString(); + SXString(BaseGame *inGame, ScStack *Stack); + virtual ~SXString(); private: char *_string; diff --git a/engines/wintermute/base/scriptables/script_stack.cpp b/engines/wintermute/base/scriptables/script_stack.cpp index 188cb2d15c..74cc7a57ee 100644 --- a/engines/wintermute/base/scriptables/script_stack.cpp +++ b/engines/wintermute/base/scriptables/script_stack.cpp @@ -32,16 +32,16 @@ namespace WinterMute { -IMPLEMENT_PERSISTENT(CScStack, false) +IMPLEMENT_PERSISTENT(ScStack, false) ////////////////////////////////////////////////////////////////////////// -CScStack::CScStack(CBGame *inGame): CBBase(inGame) { +ScStack::ScStack(BaseGame *inGame): BaseClass(inGame) { _sP = -1; } ////////////////////////////////////////////////////////////////////////// -CScStack::~CScStack() { +ScStack::~ScStack() { #if _DEBUG //_gameRef->LOG(0, "STAT: Stack size: %d, SP=%d", _values.getSize(), _sP); @@ -55,7 +55,7 @@ CScStack::~CScStack() { ////////////////////////////////////////////////////////////////////////// -CScValue *CScStack::pop() { +ScValue *ScStack::pop() { if (_sP < 0) { _gameRef->LOG(0, "Fatal: Stack underflow"); return NULL; @@ -66,14 +66,14 @@ CScValue *CScStack::pop() { ////////////////////////////////////////////////////////////////////////// -void CScStack::push(CScValue *val) { +void ScStack::push(ScValue *val) { _sP++; if (_sP < _values.getSize()) { _values[_sP]->cleanup(); _values[_sP]->copy(val); } else { - CScValue *copyVal = new CScValue(_gameRef); + ScValue *copyVal = new ScValue(_gameRef); copyVal->copy(val); _values.add(copyVal); } @@ -81,11 +81,11 @@ void CScStack::push(CScValue *val) { ////////////////////////////////////////////////////////////////////////// -CScValue *CScStack::getPushValue() { +ScValue *ScStack::getPushValue() { _sP++; if (_sP >= _values.getSize()) { - CScValue *val = new CScValue(_gameRef); + ScValue *val = new ScValue(_gameRef); _values.add(val); } _values[_sP]->cleanup(); @@ -95,14 +95,14 @@ CScValue *CScStack::getPushValue() { ////////////////////////////////////////////////////////////////////////// -CScValue *CScStack::getTop() { +ScValue *ScStack::getTop() { if (_sP < 0 || _sP >= _values.getSize()) return NULL; else return _values[_sP]; } ////////////////////////////////////////////////////////////////////////// -CScValue *CScStack::getAt(int index) { +ScValue *ScStack::getAt(int index) { index = _sP - index; if (index < 0 || index >= _values.getSize()) return NULL; else return _values[index]; @@ -110,7 +110,7 @@ CScValue *CScStack::getAt(int index) { ////////////////////////////////////////////////////////////////////////// -void CScStack::correctParams(uint32 expectedParams) { +void ScStack::correctParams(uint32 expectedParams) { uint32 nuParams = (uint32)pop()->getInt(); if (expectedParams < nuParams) { // too many params @@ -124,7 +124,7 @@ void CScStack::correctParams(uint32 expectedParams) { } else if (expectedParams > nuParams) { // need more params while (expectedParams > nuParams) { //Push(null_val); - CScValue *nullVal = new CScValue(_gameRef); + ScValue *nullVal = new ScValue(_gameRef); nullVal->setNULL(); _values.insertAt(_sP - nuParams + 1, nullVal); nuParams++; @@ -140,9 +140,9 @@ void CScStack::correctParams(uint32 expectedParams) { ////////////////////////////////////////////////////////////////////////// -void CScStack::pushNULL() { +void ScStack::pushNULL() { /* - CScValue* val = new CScValue(_gameRef); + ScValue* val = new ScValue(_gameRef); val->setNULL(); Push(val); delete val; @@ -152,9 +152,9 @@ void CScStack::pushNULL() { ////////////////////////////////////////////////////////////////////////// -void CScStack::pushInt(int val) { +void ScStack::pushInt(int val) { /* - CScValue* val = new CScValue(_gameRef); + ScValue* val = new ScValue(_gameRef); val->setInt(Val); Push(val); delete val; @@ -164,9 +164,9 @@ void CScStack::pushInt(int val) { ////////////////////////////////////////////////////////////////////////// -void CScStack::pushFloat(double val) { +void ScStack::pushFloat(double val) { /* - CScValue* val = new CScValue(_gameRef); + ScValue* val = new ScValue(_gameRef); val->setFloat(Val); Push(val); delete val; @@ -176,9 +176,9 @@ void CScStack::pushFloat(double val) { ////////////////////////////////////////////////////////////////////////// -void CScStack::pushBool(bool val) { +void ScStack::pushBool(bool val) { /* - CScValue* val = new CScValue(_gameRef); + ScValue* val = new ScValue(_gameRef); val->setBool(Val); Push(val); delete val; @@ -188,9 +188,9 @@ void CScStack::pushBool(bool val) { ////////////////////////////////////////////////////////////////////////// -void CScStack::pushString(const char *val) { +void ScStack::pushString(const char *val) { /* - CScValue* val = new CScValue(_gameRef); + ScValue* val = new ScValue(_gameRef); val->setString(Val); Push(val); delete val; @@ -200,9 +200,9 @@ void CScStack::pushString(const char *val) { ////////////////////////////////////////////////////////////////////////// -void CScStack::pushNative(CBScriptable *val, bool persistent) { +void ScStack::pushNative(BaseScriptable *val, bool persistent) { /* - CScValue* val = new CScValue(_gameRef); + ScValue* val = new ScValue(_gameRef); val->setNative(Val, Persistent); Push(val); delete val; @@ -213,7 +213,7 @@ void CScStack::pushNative(CBScriptable *val, bool persistent) { ////////////////////////////////////////////////////////////////////////// -bool CScStack::persist(CBPersistMgr *persistMgr) { +bool ScStack::persist(BasePersistenceManager *persistMgr) { persistMgr->transfer(TMEMBER(_gameRef)); diff --git a/engines/wintermute/base/scriptables/script_stack.h b/engines/wintermute/base/scriptables/script_stack.h index 6460f901fe..3aacad0765 100644 --- a/engines/wintermute/base/scriptables/script_stack.h +++ b/engines/wintermute/base/scriptables/script_stack.h @@ -36,27 +36,27 @@ namespace WinterMute { -class CScValue; -class CBScriptable; +class ScValue; +class BaseScriptable; -class CScStack : public CBBase { +class ScStack : public BaseClass { public: - CScValue *getAt(int Index); - CScValue *getPushValue(); - DECLARE_PERSISTENT(CScStack, CBBase) - void pushNative(CBScriptable *val, bool persistent); + ScValue *getAt(int Index); + ScValue *getPushValue(); + DECLARE_PERSISTENT(ScStack, BaseClass) + void pushNative(BaseScriptable *val, bool persistent); void pushString(const char *val); void pushBool(bool val); void pushInt(int val); void pushFloat(double val); void pushNULL(); void correctParams(uint32 expectedParams); - CScValue *getTop(); - void push(CScValue *val); - CScValue *pop(); - CScStack(CBGame *inGame); - virtual ~CScStack(); - CBArray _values; + ScValue *getTop(); + void push(ScValue *val); + ScValue *pop(); + ScStack(BaseGame *inGame); + virtual ~ScStack(); + BaseArray _values; int _sP; }; diff --git a/engines/wintermute/base/scriptables/script_value.cpp b/engines/wintermute/base/scriptables/script_value.cpp index da47ed299f..0d8c34e719 100644 --- a/engines/wintermute/base/scriptables/script_value.cpp +++ b/engines/wintermute/base/scriptables/script_value.cpp @@ -40,10 +40,10 @@ namespace WinterMute { // Construction/Destruction ////////////////////////////////////////////////////////////////////// -IMPLEMENT_PERSISTENT(CScValue, false) +IMPLEMENT_PERSISTENT(ScValue, false) ////////////////////////////////////////////////////////////////////////// -CScValue::CScValue(CBGame *inGame): CBBase(inGame) { +ScValue::ScValue(BaseGame *inGame): BaseClass(inGame) { _type = VAL_NULL; _valBool = false; @@ -58,7 +58,7 @@ CScValue::CScValue(CBGame *inGame): CBBase(inGame) { ////////////////////////////////////////////////////////////////////////// -CScValue::CScValue(CBGame *inGame, bool val): CBBase(inGame) { +ScValue::ScValue(BaseGame *inGame, bool val): BaseClass(inGame) { _type = VAL_BOOL; _valBool = val; @@ -73,7 +73,7 @@ CScValue::CScValue(CBGame *inGame, bool val): CBBase(inGame) { ////////////////////////////////////////////////////////////////////////// -CScValue::CScValue(CBGame *inGame, int val): CBBase(inGame) { +ScValue::ScValue(BaseGame *inGame, int val): BaseClass(inGame) { _type = VAL_INT; _valInt = val; @@ -88,7 +88,7 @@ CScValue::CScValue(CBGame *inGame, int val): CBBase(inGame) { ////////////////////////////////////////////////////////////////////////// -CScValue::CScValue(CBGame *inGame, double val): CBBase(inGame) { +ScValue::ScValue(BaseGame *inGame, double val): BaseClass(inGame) { _type = VAL_FLOAT; _valFloat = val; @@ -103,7 +103,7 @@ CScValue::CScValue(CBGame *inGame, double val): CBBase(inGame) { ////////////////////////////////////////////////////////////////////////// -CScValue::CScValue(CBGame *inGame, const char *val): CBBase(inGame) { +ScValue::ScValue(BaseGame *inGame, const char *val): BaseClass(inGame) { _type = VAL_STRING; _valString = NULL; setStringVal(val); @@ -119,7 +119,7 @@ CScValue::CScValue(CBGame *inGame, const char *val): CBBase(inGame) { ////////////////////////////////////////////////////////////////////////// -void CScValue::cleanup(bool ignoreNatives) { +void ScValue::cleanup(bool ignoreNatives) { deleteProps(); if (_valString) delete [] _valString; @@ -150,13 +150,13 @@ void CScValue::cleanup(bool ignoreNatives) { ////////////////////////////////////////////////////////////////////////// -CScValue::~CScValue() { +ScValue::~ScValue() { cleanup(); } ////////////////////////////////////////////////////////////////////////// -CScValue *CScValue::getProp(const char *name) { +ScValue *ScValue::getProp(const char *name) { if (_type == VAL_VARIABLE_REF) return _valRef->getProp(name); if (_type == VAL_STRING && strcmp(name, "Length") == 0) { @@ -176,7 +176,7 @@ CScValue *CScValue::getProp(const char *name) { return _gameRef->_scValue; } - CScValue *ret = NULL; + ScValue *ret = NULL; if (_type == VAL_NATIVE && _valNative) ret = _valNative->scGetProperty(name); @@ -188,7 +188,7 @@ CScValue *CScValue::getProp(const char *name) { } ////////////////////////////////////////////////////////////////////////// -bool CScValue::deleteProp(const char *name) { +bool ScValue::deleteProp(const char *name) { if (_type == VAL_VARIABLE_REF) return _valRef->deleteProp(name); _valIter = _valObject.find(name); @@ -203,7 +203,7 @@ bool CScValue::deleteProp(const char *name) { ////////////////////////////////////////////////////////////////////////// -bool CScValue::setProp(const char *name, CScValue *val, bool copyWhole, bool setAsConst) { +bool ScValue::setProp(const char *name, ScValue *val, bool copyWhole, bool setAsConst) { if (_type == VAL_VARIABLE_REF) return _valRef->setProp(name, val); @@ -213,14 +213,14 @@ bool CScValue::setProp(const char *name, CScValue *val, bool copyWhole, bool set } if (DID_FAIL(ret)) { - CScValue *newVal = NULL; + ScValue *newVal = NULL; _valIter = _valObject.find(name); if (_valIter != _valObject.end()) { newVal = _valIter->_value; } if (!newVal) - newVal = new CScValue(_gameRef); + newVal = new ScValue(_gameRef); else newVal->cleanup(); newVal->copy(val, copyWhole); @@ -235,7 +235,7 @@ bool CScValue::setProp(const char *name, CScValue *val, bool copyWhole, bool set delete _valIter->_value; _valIter->_value = NULL; } - CScValue* val = new CScValue(_gameRef); + ScValue* val = new ScValue(_gameRef); val->Copy(Val, CopyWhole); val->_isConstVar = SetAsConst; _valObject[Name] = val; @@ -249,7 +249,7 @@ bool CScValue::setProp(const char *name, CScValue *val, bool copyWhole, bool set ////////////////////////////////////////////////////////////////////////// -bool CScValue::propExists(const char *name) { +bool ScValue::propExists(const char *name) { if (_type == VAL_VARIABLE_REF) return _valRef->propExists(name); _valIter = _valObject.find(name); @@ -259,10 +259,10 @@ bool CScValue::propExists(const char *name) { ////////////////////////////////////////////////////////////////////////// -void CScValue::deleteProps() { +void ScValue::deleteProps() { _valIter = _valObject.begin(); while (_valIter != _valObject.end()) { - delete(CScValue *)_valIter->_value; + delete(ScValue *)_valIter->_value; _valIter++; } _valObject.clear(); @@ -270,7 +270,7 @@ void CScValue::deleteProps() { ////////////////////////////////////////////////////////////////////////// -void CScValue::CleanProps(bool includingNatives) { +void ScValue::CleanProps(bool includingNatives) { _valIter = _valObject.begin(); while (_valIter != _valObject.end()) { if (!_valIter->_value->_isConstVar && (!_valIter->_value->isNative() || includingNatives)) _valIter->_value->setNULL(); @@ -279,7 +279,7 @@ void CScValue::CleanProps(bool includingNatives) { } ////////////////////////////////////////////////////////////////////////// -bool CScValue::isNULL() { +bool ScValue::isNULL() { if (_type == VAL_VARIABLE_REF) return _valRef->isNULL(); @@ -288,7 +288,7 @@ bool CScValue::isNULL() { ////////////////////////////////////////////////////////////////////////// -bool CScValue::isNative() { +bool ScValue::isNative() { if (_type == VAL_VARIABLE_REF) return _valRef->isNative(); @@ -297,7 +297,7 @@ bool CScValue::isNative() { ////////////////////////////////////////////////////////////////////////// -bool CScValue::isString() { +bool ScValue::isString() { if (_type == VAL_VARIABLE_REF) return _valRef->isString(); @@ -306,7 +306,7 @@ bool CScValue::isString() { ////////////////////////////////////////////////////////////////////////// -bool CScValue::isFloat() { +bool ScValue::isFloat() { if (_type == VAL_VARIABLE_REF) return _valRef->isFloat(); @@ -315,7 +315,7 @@ bool CScValue::isFloat() { ////////////////////////////////////////////////////////////////////////// -bool CScValue::isInt() { +bool ScValue::isInt() { if (_type == VAL_VARIABLE_REF) return _valRef->isInt(); @@ -324,7 +324,7 @@ bool CScValue::isInt() { ////////////////////////////////////////////////////////////////////////// -bool CScValue::isBool() { +bool ScValue::isBool() { if (_type == VAL_VARIABLE_REF) return _valRef->isBool(); @@ -333,7 +333,7 @@ bool CScValue::isBool() { ////////////////////////////////////////////////////////////////////////// -bool CScValue::isObject() { +bool ScValue::isObject() { if (_type == VAL_VARIABLE_REF) return _valRef->isObject(); @@ -342,7 +342,7 @@ bool CScValue::isObject() { ////////////////////////////////////////////////////////////////////////// -TValType CScValue::getTypeTolerant() { +TValType ScValue::getTypeTolerant() { if (_type == VAL_VARIABLE_REF) return _valRef->getType(); @@ -351,7 +351,7 @@ TValType CScValue::getTypeTolerant() { ////////////////////////////////////////////////////////////////////////// -void CScValue::setBool(bool val) { +void ScValue::setBool(bool val) { if (_type == VAL_VARIABLE_REF) { _valRef->setBool(val); return; @@ -368,7 +368,7 @@ void CScValue::setBool(bool val) { ////////////////////////////////////////////////////////////////////////// -void CScValue::setInt(int val) { +void ScValue::setInt(int val) { if (_type == VAL_VARIABLE_REF) { _valRef->setInt(val); return; @@ -385,7 +385,7 @@ void CScValue::setInt(int val) { ////////////////////////////////////////////////////////////////////////// -void CScValue::setFloat(double val) { +void ScValue::setFloat(double val) { if (_type == VAL_VARIABLE_REF) { _valRef->setFloat(val); return; @@ -402,7 +402,7 @@ void CScValue::setFloat(double val) { ////////////////////////////////////////////////////////////////////////// -void CScValue::setString(const char *val) { +void ScValue::setString(const char *val) { if (_type == VAL_VARIABLE_REF) { _valRef->setString(val); return; @@ -418,12 +418,12 @@ void CScValue::setString(const char *val) { else _type = VAL_NULL; } -void CScValue::setString(const Common::String &val) { +void ScValue::setString(const Common::String &val) { setString(val.c_str()); } ////////////////////////////////////////////////////////////////////////// -void CScValue::setStringVal(const char *val) { +void ScValue::setStringVal(const char *val) { if (_valString) { delete [] _valString; _valString = NULL; @@ -442,7 +442,7 @@ void CScValue::setStringVal(const char *val) { ////////////////////////////////////////////////////////////////////////// -void CScValue::setNULL() { +void ScValue::setNULL() { if (_type == VAL_VARIABLE_REF) { _valRef->setNULL(); return; @@ -460,7 +460,7 @@ void CScValue::setNULL() { ////////////////////////////////////////////////////////////////////////// -void CScValue::setNative(CBScriptable *val, bool persistent) { +void ScValue::setNative(BaseScriptable *val, bool persistent) { if (_type == VAL_VARIABLE_REF) { _valRef->setNative(val, persistent); return; @@ -487,7 +487,7 @@ void CScValue::setNative(CBScriptable *val, bool persistent) { ////////////////////////////////////////////////////////////////////////// -void CScValue::setObject() { +void ScValue::setObject() { if (_type == VAL_VARIABLE_REF) { _valRef->setObject(); return; @@ -499,14 +499,14 @@ void CScValue::setObject() { ////////////////////////////////////////////////////////////////////////// -void CScValue::setReference(CScValue *val) { +void ScValue::setReference(ScValue *val) { _valRef = val; _type = VAL_VARIABLE_REF; } ////////////////////////////////////////////////////////////////////////// -bool CScValue::getBool(bool defaultVal) { +bool ScValue::getBool(bool defaultVal) { if (_type == VAL_VARIABLE_REF) return _valRef->getBool(); @@ -533,7 +533,7 @@ bool CScValue::getBool(bool defaultVal) { ////////////////////////////////////////////////////////////////////////// -int CScValue::getInt(int defaultVal) { +int ScValue::getInt(int defaultVal) { if (_type == VAL_VARIABLE_REF) return _valRef->getInt(); switch (_type) { @@ -559,7 +559,7 @@ int CScValue::getInt(int defaultVal) { ////////////////////////////////////////////////////////////////////////// -double CScValue::getFloat(double defaultVal) { +double ScValue::getFloat(double defaultVal) { if (_type == VAL_VARIABLE_REF) return _valRef->getFloat(); @@ -585,7 +585,7 @@ double CScValue::getFloat(double defaultVal) { } ////////////////////////////////////////////////////////////////////////// -void *CScValue::getMemBuffer() { +void *ScValue::getMemBuffer() { if (_type == VAL_VARIABLE_REF) return _valRef->getMemBuffer(); @@ -596,7 +596,7 @@ void *CScValue::getMemBuffer() { ////////////////////////////////////////////////////////////////////////// -const char *CScValue::getString() { +const char *ScValue::getString() { if (_type == VAL_VARIABLE_REF) return _valRef->getString(); @@ -646,7 +646,7 @@ const char *CScValue::getString() { ////////////////////////////////////////////////////////////////////////// -CBScriptable *CScValue::getNative() { +BaseScriptable *ScValue::getNative() { if (_type == VAL_VARIABLE_REF) return _valRef->getNative(); @@ -656,13 +656,13 @@ CBScriptable *CScValue::getNative() { ////////////////////////////////////////////////////////////////////////// -TValType CScValue::getType() { +TValType ScValue::getType() { return _type; } ////////////////////////////////////////////////////////////////////////// -void CScValue::copy(CScValue *orig, bool copyWhole) { +void ScValue::copy(ScValue *orig, bool copyWhole) { _gameRef = orig->_gameRef; if (_valNative && !_persistent) { @@ -694,7 +694,7 @@ void CScValue::copy(CScValue *orig, bool copyWhole) { if (orig->_type == VAL_OBJECT && orig->_valObject.size() > 0) { orig->_valIter = orig->_valObject.begin(); while (orig->_valIter != orig->_valObject.end()) { - _valObject[orig->_valIter->_key] = new CScValue(_gameRef); + _valObject[orig->_valIter->_key] = new ScValue(_gameRef); _valObject[orig->_valIter->_key]->copy(orig->_valIter->_value); orig->_valIter++; } @@ -703,7 +703,7 @@ void CScValue::copy(CScValue *orig, bool copyWhole) { ////////////////////////////////////////////////////////////////////////// -void CScValue::setValue(CScValue *val) { +void ScValue::setValue(ScValue *val) { if (val->_type == VAL_VARIABLE_REF) { setValue(val->_valRef); return; @@ -725,7 +725,7 @@ void CScValue::setValue(CScValue *val) { _valNative->scSetString(val->getString()); break; default: - warning("CScValue::setValue - unhandled enum"); + warning("ScValue::setValue - unhandled enum"); break; } } @@ -735,7 +735,7 @@ void CScValue::setValue(CScValue *val) { ////////////////////////////////////////////////////////////////////////// -bool CScValue::persist(CBPersistMgr *persistMgr) { +bool ScValue::persist(BasePersistenceManager *persistMgr) { persistMgr->transfer(TMEMBER(_gameRef)); persistMgr->transfer(TMEMBER(_persistent)); @@ -760,7 +760,7 @@ bool CScValue::persist(CBPersistMgr *persistMgr) { _valIter++; } } else { - CScValue *val; + ScValue *val; persistMgr->transfer("", &size); for (int i = 0; i < size; i++) { persistMgr->transfer("", &str); @@ -819,7 +819,7 @@ bool CScValue::persist(CBPersistMgr *persistMgr) { ////////////////////////////////////////////////////////////////////////// -bool CScValue::saveAsText(CBDynBuffer *buffer, int indent) { +bool ScValue::saveAsText(BaseDynamicBuffer *buffer, int indent) { _valIter = _valObject.begin(); while (_valIter != _valObject.end()) { buffer->putTextIndent(indent, "PROPERTY {\n"); @@ -835,7 +835,7 @@ bool CScValue::saveAsText(CBDynBuffer *buffer, int indent) { ////////////////////////////////////////////////////////////////////////// // -1 ... left is less, 0 ... equals, 1 ... left is greater -int CScValue::compare(CScValue *val1, CScValue *val2) { +int ScValue::compare(ScValue *val1, ScValue *val2) { // both natives? if (val1->isNative() && val2->isNative()) { // same class? @@ -871,14 +871,14 @@ int CScValue::compare(CScValue *val1, CScValue *val2) { ////////////////////////////////////////////////////////////////////////// -int CScValue::compareStrict(CScValue *val1, CScValue *val2) { +int ScValue::compareStrict(ScValue *val1, ScValue *val2) { if (val1->getTypeTolerant() != val2->getTypeTolerant()) return -1; - else return CScValue::compare(val1, val2); + else return ScValue::compare(val1, val2); } ////////////////////////////////////////////////////////////////////////// -bool CScValue::dbgSendVariables(IWmeDebugClient *client, EWmeDebuggerVariableType type, CScScript *script, unsigned int scopeID) { +bool ScValue::dbgSendVariables(IWmeDebugClient *client, EWmeDebuggerVariableType type, ScScript *script, unsigned int scopeID) { _valIter = _valObject.begin(); while (_valIter != _valObject.end()) { client->onVariableInit(type, script, scopeID, _valIter->_value, _valIter->_key.c_str()); @@ -889,24 +889,24 @@ bool CScValue::dbgSendVariables(IWmeDebugClient *client, EWmeDebuggerVariableTyp ////////////////////////////////////////////////////////////////////////// -bool CScValue::setProperty(const char *propName, int value) { - CScValue *val = new CScValue(_gameRef, value); +bool ScValue::setProperty(const char *propName, int value) { + ScValue *val = new ScValue(_gameRef, value); bool ret = DID_SUCCEED(setProp(propName, val)); delete val; return ret; } ////////////////////////////////////////////////////////////////////////// -bool CScValue::setProperty(const char *propName, const char *value) { - CScValue *val = new CScValue(_gameRef, value); +bool ScValue::setProperty(const char *propName, const char *value) { + ScValue *val = new ScValue(_gameRef, value); bool ret = DID_SUCCEED(setProp(propName, val)); delete val; return ret; } ////////////////////////////////////////////////////////////////////////// -bool CScValue::setProperty(const char *propName, double value) { - CScValue *val = new CScValue(_gameRef, value); +bool ScValue::setProperty(const char *propName, double value) { + ScValue *val = new ScValue(_gameRef, value); bool ret = DID_SUCCEED(setProp(propName, val)); delete val; return ret; @@ -914,8 +914,8 @@ bool CScValue::setProperty(const char *propName, double value) { ////////////////////////////////////////////////////////////////////////// -bool CScValue::setProperty(const char *propName, bool value) { - CScValue *val = new CScValue(_gameRef, value); +bool ScValue::setProperty(const char *propName, bool value) { + ScValue *val = new ScValue(_gameRef, value); bool ret = DID_SUCCEED(setProp(propName, val)); delete val; return ret; @@ -923,8 +923,8 @@ bool CScValue::setProperty(const char *propName, bool value) { ////////////////////////////////////////////////////////////////////////// -bool CScValue::setProperty(const char *propName) { - CScValue *val = new CScValue(_gameRef); +bool ScValue::setProperty(const char *propName) { + ScValue *val = new ScValue(_gameRef); bool ret = DID_SUCCEED(setProp(propName, val)); delete val; return ret; @@ -934,7 +934,7 @@ bool CScValue::setProperty(const char *propName) { ////////////////////////////////////////////////////////////////////////// // IWmeDebugProp ////////////////////////////////////////////////////////////////////////// -EWmeDebuggerPropType CScValue::dbgGetType() { +EWmeDebuggerPropType ScValue::dbgGetType() { switch (getType()) { case VAL_NULL: return WME_DBGPROP_NULL; @@ -956,70 +956,70 @@ EWmeDebuggerPropType CScValue::dbgGetType() { } ////////////////////////////////////////////////////////////////////////// -int CScValue::dbgGetValInt() { +int ScValue::dbgGetValInt() { return getInt(); } ////////////////////////////////////////////////////////////////////////// -double CScValue::dbgGetValFloat() { +double ScValue::dbgGetValFloat() { return getFloat(); } ////////////////////////////////////////////////////////////////////////// -bool CScValue::dbgGetValBool() { +bool ScValue::dbgGetValBool() { return getBool(); } ////////////////////////////////////////////////////////////////////////// -const char *CScValue::dbgGetValString() { +const char *ScValue::dbgGetValString() { return getString(); } ////////////////////////////////////////////////////////////////////////// -IWmeDebugObject *CScValue::dbgGetValNative() { +IWmeDebugObject *ScValue::dbgGetValNative() { return getNative(); } ////////////////////////////////////////////////////////////////////////// -bool CScValue::dbgSetVal(int value) { +bool ScValue::dbgSetVal(int value) { setInt(value); return true; } ////////////////////////////////////////////////////////////////////////// -bool CScValue::dbgSetVal(double value) { +bool ScValue::dbgSetVal(double value) { setFloat(value); return true; } ////////////////////////////////////////////////////////////////////////// -bool CScValue::dbgSetVal(bool value) { +bool ScValue::dbgSetVal(bool value) { setBool(value); return true; } ////////////////////////////////////////////////////////////////////////// -bool CScValue::dbgSetVal(const char *value) { +bool ScValue::dbgSetVal(const char *value) { setString(value); return true; } ////////////////////////////////////////////////////////////////////////// -bool CScValue::dbgSetVal() { +bool ScValue::dbgSetVal() { setNULL(); return true; } ////////////////////////////////////////////////////////////////////////// -int CScValue::dbgGetNumProperties() { +int ScValue::dbgGetNumProperties() { if (_valNative && _valNative->_scProp) return _valNative->_scProp->dbgGetNumProperties(); else return _valObject.size(); } ////////////////////////////////////////////////////////////////////////// -bool CScValue::dbgGetProperty(int index, const char **name, IWmeDebugProp **value) { +bool ScValue::dbgGetProperty(int index, const char **name, IWmeDebugProp **value) { if (_valNative && _valNative->_scProp) return _valNative->_scProp->dbgGetProperty(index, name, value); else { @@ -1039,7 +1039,7 @@ bool CScValue::dbgGetProperty(int index, const char **name, IWmeDebugProp **valu } ////////////////////////////////////////////////////////////////////////// -bool CScValue::dbgGetDescription(char *buf, int bufSize) { +bool ScValue::dbgGetDescription(char *buf, int bufSize) { if (_type == VAL_VARIABLE_REF) return _valRef->dbgGetDescription(buf, bufSize); diff --git a/engines/wintermute/base/scriptables/script_value.h b/engines/wintermute/base/scriptables/script_value.h index eaee3ed773..7a684d2334 100644 --- a/engines/wintermute/base/scriptables/script_value.h +++ b/engines/wintermute/base/scriptables/script_value.h @@ -38,25 +38,25 @@ namespace WinterMute { -class CScScript; -class CBScriptable; +class ScScript; +class BaseScriptable; -class CScValue : public CBBase, public IWmeDebugProp { +class ScValue : public BaseClass, public IWmeDebugProp { public: - bool dbgSendVariables(IWmeDebugClient *client, EWmeDebuggerVariableType type, CScScript *script, unsigned int scopeID); + bool dbgSendVariables(IWmeDebugClient *client, EWmeDebuggerVariableType type, ScScript *script, unsigned int scopeID); - static int compare(CScValue *val1, CScValue *val2); - static int compareStrict(CScValue *val1, CScValue *val2); + static int compare(ScValue *val1, ScValue *val2); + static int compareStrict(ScValue *val1, ScValue *val2); TValType getTypeTolerant(); void cleanup(bool ignoreNatives = false); - DECLARE_PERSISTENT(CScValue, CBBase) + DECLARE_PERSISTENT(ScValue, BaseClass) bool _isConstVar; - bool saveAsText(CBDynBuffer *buffer, int indent); - void setValue(CScValue *val); + bool saveAsText(BaseDynamicBuffer *buffer, int indent); + void setValue(ScValue *val); bool _persistent; bool propExists(const char *name); - void copy(CScValue *orig, bool copyWhole = false); + void copy(ScValue *orig, bool copyWhole = false); void setStringVal(const char *val); TValType getType(); bool getBool(bool defaultVal = false); @@ -64,7 +64,7 @@ public: double getFloat(double defaultVal = 0.0f); const char *getString(); void *getMemBuffer(); - CBScriptable *getNative(); + BaseScriptable *getNative(); bool deleteProp(const char *name); void deleteProps(); void CleanProps(bool includingNatives); @@ -74,9 +74,9 @@ public: void setString(const char *val); void setString(const Common::String &val); void setNULL(); - void setNative(CBScriptable *val, bool persistent = false); + void setNative(BaseScriptable *val, bool persistent = false); void setObject(); - void setReference(CScValue *val); + void setReference(ScValue *val); bool isNULL(); bool isNative(); bool isString(); @@ -84,10 +84,10 @@ public: bool isFloat(); bool isInt(); bool isObject(); - bool setProp(const char *name, CScValue *val, bool copyWhole = false, bool setAsConst = false); - CScValue *getProp(const char *name); - CBScriptable *_valNative; - CScValue *_valRef; + bool setProp(const char *name, ScValue *val, bool copyWhole = false, bool setAsConst = false); + ScValue *getProp(const char *name); + BaseScriptable *_valNative; + ScValue *_valRef; protected: bool _valBool; int _valInt; @@ -95,14 +95,14 @@ protected: char *_valString; public: TValType _type; - CScValue(CBGame *inGame); - CScValue(CBGame *inGame, bool Val); - CScValue(CBGame *inGame, int Val); - CScValue(CBGame *inGame, double Val); - CScValue(CBGame *inGame, const char *Val); - virtual ~CScValue(); - Common::HashMap _valObject; - Common::HashMap::iterator _valIter; + ScValue(BaseGame *inGame); + ScValue(BaseGame *inGame, bool Val); + ScValue(BaseGame *inGame, int Val); + ScValue(BaseGame *inGame, double Val); + ScValue(BaseGame *inGame, const char *Val); + virtual ~ScValue(); + Common::HashMap _valObject; + Common::HashMap::iterator _valIter; bool setProperty(const char *propName, int value); bool setProperty(const char *propName, const char *value); -- cgit v1.2.3 From 7a818009b4c20df1811c0b158b0f8acfe0e3d208 Mon Sep 17 00:00:00 2001 From: Einar Johan Trøan Sømåen Date: Mon, 23 Jul 2012 02:23:14 +0200 Subject: WINTERMUTE: Clean out unused utils. --- engines/wintermute/base/scriptables/script_engine.cpp | 2 +- engines/wintermute/base/scriptables/script_engine.h | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) (limited to 'engines/wintermute/base/scriptables') diff --git a/engines/wintermute/base/scriptables/script_engine.cpp b/engines/wintermute/base/scriptables/script_engine.cpp index 44add054c5..e8544d8cd6 100644 --- a/engines/wintermute/base/scriptables/script_engine.cpp +++ b/engines/wintermute/base/scriptables/script_engine.cpp @@ -37,7 +37,7 @@ #include "engines/wintermute/base/base_game.h" #include "engines/wintermute/base/sound/base_sound.h" #include "engines/wintermute/base/base_file_manager.h" - +#include "engines/wintermute/utils/utils.h" namespace WinterMute { diff --git a/engines/wintermute/base/scriptables/script_engine.h b/engines/wintermute/base/scriptables/script_engine.h index 06d61b4156..6d2ed92e4f 100644 --- a/engines/wintermute/base/scriptables/script_engine.h +++ b/engines/wintermute/base/scriptables/script_engine.h @@ -33,7 +33,6 @@ #include "engines/wintermute/coll_templ.h" #include "engines/wintermute/base/base.h" #include "engines/wintermute/wme_debugger.h" -#include "engines/wintermute/utils/utils.h" #include "engines/wintermute/platform_osystem.h" namespace WinterMute { -- cgit v1.2.3 From 448911930d8d6bf43a67769d50ee4048f296b7f4 Mon Sep 17 00:00:00 2001 From: Einar Johan Trøan Sømåen Date: Mon, 23 Jul 2012 03:01:05 +0200 Subject: WINTERMUTE: Remove unused code from platform_osystem.h --- engines/wintermute/base/scriptables/script.cpp | 2 +- .../wintermute/base/scriptables/script_engine.cpp | 20 ++++++++++---------- engines/wintermute/base/scriptables/script_engine.h | 3 +-- .../wintermute/base/scriptables/script_ext_file.cpp | 12 ++++++++---- 4 files changed, 20 insertions(+), 17 deletions(-) (limited to 'engines/wintermute/base/scriptables') diff --git a/engines/wintermute/base/scriptables/script.cpp b/engines/wintermute/base/scriptables/script.cpp index 45544831e3..11fe6ff0cc 100644 --- a/engines/wintermute/base/scriptables/script.cpp +++ b/engines/wintermute/base/scriptables/script.cpp @@ -1148,7 +1148,7 @@ bool ScScript::sleep(uint32 duration) { _state = SCRIPT_SLEEPING; if (_gameRef->_state == GAME_FROZEN) { - _waitTime = BasePlatform::getTime() + duration; + _waitTime = g_system->getMillis() + duration; _waitFrozen = true; } else { _waitTime = _gameRef->_timer + duration; diff --git a/engines/wintermute/base/scriptables/script_engine.cpp b/engines/wintermute/base/scriptables/script_engine.cpp index e8544d8cd6..472e9d9fec 100644 --- a/engines/wintermute/base/scriptables/script_engine.cpp +++ b/engines/wintermute/base/scriptables/script_engine.cpp @@ -176,7 +176,7 @@ byte *ScEngine::getCompiledScript(const char *filename, uint32 *outSize, bool ig if (!ignoreCache) { for (int i = 0; i < MAX_CACHED_SCRIPTS; i++) { if (_cachedScripts[i] && scumm_stricmp(_cachedScripts[i]->_filename.c_str(), filename) == 0) { - _cachedScripts[i]->_timestamp = BasePlatform::getTime(); + _cachedScripts[i]->_timestamp = g_system->getMillis(); *outSize = _cachedScripts[i]->_size; return _cachedScripts[i]->_buffer; } @@ -216,7 +216,7 @@ byte *ScEngine::getCompiledScript(const char *filename, uint32 *outSize, bool ig CScCachedScript *cachedScript = new CScCachedScript(filename, compBuffer, compSize); if (cachedScript) { int index = 0; - uint32 MinTime = BasePlatform::getTime(); + uint32 MinTime = g_system->getMillis(); for (int i = 0; i < MAX_CACHED_SCRIPTS; i++) { if (_cachedScripts[i] == NULL) { index = i; @@ -275,7 +275,7 @@ bool ScEngine::tick() { case SCRIPT_SLEEPING: { if (_scripts[i]->_waitFrozen) { - if (_scripts[i]->_waitTime <= BasePlatform::getTime()) _scripts[i]->run(); + if (_scripts[i]->_waitTime <= g_system->getMillis()) _scripts[i]->run(); } else { if (_scripts[i]->_waitTime <= _gameRef->_timer) _scripts[i]->run(); } @@ -314,25 +314,25 @@ bool ScEngine::tick() { // time sliced script if (_scripts[i]->_timeSlice > 0) { - uint32 StartTime = BasePlatform::getTime(); - while (_scripts[i]->_state == SCRIPT_RUNNING && BasePlatform::getTime() - StartTime < _scripts[i]->_timeSlice) { + uint32 StartTime = g_system->getMillis(); + while (_scripts[i]->_state == SCRIPT_RUNNING && g_system->getMillis() - StartTime < _scripts[i]->_timeSlice) { _currentScript = _scripts[i]; _scripts[i]->executeInstruction(); } - if (_isProfiling && _scripts[i]->_filename) addScriptTime(_scripts[i]->_filename, BasePlatform::getTime() - StartTime); + if (_isProfiling && _scripts[i]->_filename) addScriptTime(_scripts[i]->_filename, g_system->getMillis() - StartTime); } // normal script else { uint32 startTime = 0; bool isProfiling = _isProfiling; - if (isProfiling) startTime = BasePlatform::getTime(); + if (isProfiling) startTime = g_system->getMillis(); while (_scripts[i]->_state == SCRIPT_RUNNING) { _currentScript = _scripts[i]; _scripts[i]->executeInstruction(); } - if (isProfiling && _scripts[i]->_filename) addScriptTime(_scripts[i]->_filename, BasePlatform::getTime() - startTime); + if (isProfiling && _scripts[i]->_filename) addScriptTime(_scripts[i]->_filename, g_system->getMillis() - startTime); } _currentScript = NULL; } @@ -670,7 +670,7 @@ void ScEngine::enableProfiling() { // destroy old data, if any _scriptTimes.clear(); - _profilingStartTime = BasePlatform::getTime(); + _profilingStartTime = g_system->getMillis(); _isProfiling = true; } @@ -687,7 +687,7 @@ void ScEngine::disableProfiling() { ////////////////////////////////////////////////////////////////////////// void ScEngine::dumpStats() { error("DumpStats not ported to ScummVM yet"); - /* uint32 totalTime = BasePlatform::getTime() - _profilingStartTime; + /* uint32 totalTime = g_system->getMillis() - _profilingStartTime; typedef std::vector > TimeVector; TimeVector times; diff --git a/engines/wintermute/base/scriptables/script_engine.h b/engines/wintermute/base/scriptables/script_engine.h index 6d2ed92e4f..3d85192aa6 100644 --- a/engines/wintermute/base/scriptables/script_engine.h +++ b/engines/wintermute/base/scriptables/script_engine.h @@ -33,7 +33,6 @@ #include "engines/wintermute/coll_templ.h" #include "engines/wintermute/base/base.h" #include "engines/wintermute/wme_debugger.h" -#include "engines/wintermute/platform_osystem.h" namespace WinterMute { @@ -47,7 +46,7 @@ public: class CScCachedScript { public: CScCachedScript(const char *filename, byte *buffer, uint32 size) { - _timestamp = BasePlatform::getTime(); + _timestamp = g_system->getMillis(); _buffer = new byte[size]; if (_buffer) memcpy(_buffer, buffer, size); _size = size; diff --git a/engines/wintermute/base/scriptables/script_ext_file.cpp b/engines/wintermute/base/scriptables/script_ext_file.cpp index ffa362a938..383b956555 100644 --- a/engines/wintermute/base/scriptables/script_ext_file.cpp +++ b/engines/wintermute/base/scriptables/script_ext_file.cpp @@ -182,7 +182,9 @@ bool SXFile::scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack, else if (strcmp(name, "Delete") == 0) { stack->correctParams(0); close(); - stack->pushBool(BasePlatform::deleteFile(_filename) != false); + error("SXFile-Method: \"Delete\" not supported"); + //stack->pushBool(BasePlatform::deleteFile(_filename) != false); + stack->pushBool(false); return STATUS_OK; } @@ -191,11 +193,13 @@ bool SXFile::scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack, ////////////////////////////////////////////////////////////////////////// else if (strcmp(name, "Copy") == 0) { stack->correctParams(2); - const char *Dest = stack->pop()->getString(); - bool Overwrite = stack->pop()->getBool(true); + const char *dest = stack->pop()->getString(); + bool overwrite = stack->pop()->getBool(true); close(); - stack->pushBool(BasePlatform::copyFile(_filename, Dest, !Overwrite) != false); + error("SXFile-Method: Copy not supported"); + //stack->pushBool(BasePlatform::copyFile(_filename, Dest, !Overwrite) != false); + stack->pushBool(false); return STATUS_OK; } -- cgit v1.2.3 From aedb0aea505e764c4c7bab1f90520b380be4d688 Mon Sep 17 00:00:00 2001 From: Einar Johan Trøan Sømåen Date: Mon, 23 Jul 2012 03:22:49 +0200 Subject: WINTERMUTE: Remove dcgf.h from almost all includes. --- engines/wintermute/base/scriptables/script.cpp | 1 - engines/wintermute/base/scriptables/script_engine.cpp | 1 - 2 files changed, 2 deletions(-) (limited to 'engines/wintermute/base/scriptables') diff --git a/engines/wintermute/base/scriptables/script.cpp b/engines/wintermute/base/scriptables/script.cpp index 11fe6ff0cc..bdd11ca06a 100644 --- a/engines/wintermute/base/scriptables/script.cpp +++ b/engines/wintermute/base/scriptables/script.cpp @@ -26,7 +26,6 @@ * Copyright (c) 2011 Jan Nedoma */ -#include "engines/wintermute/dcgf.h" #include "engines/wintermute/base/scriptables/script_value.h" #include "engines/wintermute/base/scriptables/script.h" #include "engines/wintermute/base/base_game.h" diff --git a/engines/wintermute/base/scriptables/script_engine.cpp b/engines/wintermute/base/scriptables/script_engine.cpp index 472e9d9fec..c275ebf5af 100644 --- a/engines/wintermute/base/scriptables/script_engine.cpp +++ b/engines/wintermute/base/scriptables/script_engine.cpp @@ -26,7 +26,6 @@ * Copyright (c) 2011 Jan Nedoma */ -#include "engines/wintermute/dcgf.h" #include "engines/wintermute/base/scriptables/script_engine.h" #include "engines/wintermute/utils/string_util.h" #include "engines/wintermute/base/scriptables/script_value.h" -- cgit v1.2.3 From c7eda9abc80d1912148cae4292b94620c67a9c19 Mon Sep 17 00:00:00 2001 From: Einar Johan Trøan Sømåen Date: Mon, 23 Jul 2012 03:42:27 +0200 Subject: WINTERMUTE: Encapsulate and distance BasePersistenceManager from Base. --- engines/wintermute/base/scriptables/script.cpp | 4 ++-- engines/wintermute/base/scriptables/script_engine.cpp | 2 +- engines/wintermute/base/scriptables/script_ext_file.cpp | 2 +- engines/wintermute/base/scriptables/script_ext_mem_buffer.cpp | 2 +- engines/wintermute/base/scriptables/script_ext_string.cpp | 2 +- engines/wintermute/base/scriptables/script_value.cpp | 2 +- 6 files changed, 7 insertions(+), 7 deletions(-) (limited to 'engines/wintermute/base/scriptables') diff --git a/engines/wintermute/base/scriptables/script.cpp b/engines/wintermute/base/scriptables/script.cpp index bdd11ca06a..52490c0710 100644 --- a/engines/wintermute/base/scriptables/script.cpp +++ b/engines/wintermute/base/scriptables/script.cpp @@ -1199,7 +1199,7 @@ bool ScScript::persist(BasePersistenceManager *persistMgr) { persistMgr->transfer(TMEMBER(_gameRef)); // buffer - if (persistMgr->_saving) { + if (persistMgr->getIsSaving()) { if (_state != SCRIPT_PERSISTENT && _state != SCRIPT_FINISHED && _state != SCRIPT_THREAD_FINISHED) { persistMgr->transfer(TMEMBER(_bufferSize)); persistMgr->putBytes(_buffer, _bufferSize); @@ -1249,7 +1249,7 @@ bool ScScript::persist(BasePersistenceManager *persistMgr) { persistMgr->transfer(TMEMBER(_unbreakable)); persistMgr->transfer(TMEMBER(_parentScript)); - if (!persistMgr->_saving) _tracingMode = false; + if (!persistMgr->getIsSaving()) _tracingMode = false; return STATUS_OK; } diff --git a/engines/wintermute/base/scriptables/script_engine.cpp b/engines/wintermute/base/scriptables/script_engine.cpp index c275ebf5af..59ec113d1f 100644 --- a/engines/wintermute/base/scriptables/script_engine.cpp +++ b/engines/wintermute/base/scriptables/script_engine.cpp @@ -448,7 +448,7 @@ bool ScEngine::resetScript(ScScript *script) { ////////////////////////////////////////////////////////////////////////// bool ScEngine::persist(BasePersistenceManager *persistMgr) { - if (!persistMgr->_saving) cleanup(); + if (!persistMgr->getIsSaving()) cleanup(); persistMgr->transfer(TMEMBER(_gameRef)); persistMgr->transfer(TMEMBER(_currentScript)); diff --git a/engines/wintermute/base/scriptables/script_ext_file.cpp b/engines/wintermute/base/scriptables/script_ext_file.cpp index 383b956555..92943f7485 100644 --- a/engines/wintermute/base/scriptables/script_ext_file.cpp +++ b/engines/wintermute/base/scriptables/script_ext_file.cpp @@ -730,7 +730,7 @@ bool SXFile::persist(BasePersistenceManager *persistMgr) { persistMgr->transfer(TMEMBER(_textMode)); uint32 pos = 0; - if (persistMgr->_saving) { + if (persistMgr->getIsSaving()) { pos = getPos(); persistMgr->transfer(TMEMBER(pos)); } else { diff --git a/engines/wintermute/base/scriptables/script_ext_mem_buffer.cpp b/engines/wintermute/base/scriptables/script_ext_mem_buffer.cpp index 79ba978002..b625fad400 100644 --- a/engines/wintermute/base/scriptables/script_ext_mem_buffer.cpp +++ b/engines/wintermute/base/scriptables/script_ext_mem_buffer.cpp @@ -486,7 +486,7 @@ bool SXMemBuffer::persist(BasePersistenceManager *persistMgr) { persistMgr->transfer(TMEMBER(_size)); - if (persistMgr->_saving) { + if (persistMgr->getIsSaving()) { if (_size > 0) persistMgr->putBytes((byte *)_buffer, _size); } else { if (_size > 0) { diff --git a/engines/wintermute/base/scriptables/script_ext_string.cpp b/engines/wintermute/base/scriptables/script_ext_string.cpp index e645772b6f..5e01e5e29c 100644 --- a/engines/wintermute/base/scriptables/script_ext_string.cpp +++ b/engines/wintermute/base/scriptables/script_ext_string.cpp @@ -383,7 +383,7 @@ bool SXString::persist(BasePersistenceManager *persistMgr) { persistMgr->transfer(TMEMBER(_capacity)); - if (persistMgr->_saving) { + if (persistMgr->getIsSaving()) { if (_capacity > 0) persistMgr->putBytes((byte *)_string, _capacity); } else { if (_capacity > 0) { diff --git a/engines/wintermute/base/scriptables/script_value.cpp b/engines/wintermute/base/scriptables/script_value.cpp index 0d8c34e719..6d12c34914 100644 --- a/engines/wintermute/base/scriptables/script_value.cpp +++ b/engines/wintermute/base/scriptables/script_value.cpp @@ -748,7 +748,7 @@ bool ScValue::persist(BasePersistenceManager *persistMgr) { int size; const char *str; - if (persistMgr->_saving) { + if (persistMgr->getIsSaving()) { size = _valObject.size(); persistMgr->transfer("", &size); _valIter = _valObject.begin(); -- cgit v1.2.3 From 45c5eb5cab069ea9ca4302a637f4621d460c790d Mon Sep 17 00:00:00 2001 From: Einar Johan Trøan Sømåen Date: Wed, 25 Jul 2012 05:08:13 +0200 Subject: WINTERMUTE: Privatize/Protect variables that don't need to be public. --- engines/wintermute/base/scriptables/script.h | 18 ++++++++++-------- engines/wintermute/base/scriptables/script_ext_array.h | 1 + engines/wintermute/base/scriptables/script_ext_date.h | 2 +- .../base/scriptables/script_ext_mem_buffer.h | 3 ++- .../wintermute/base/scriptables/script_ext_object.h | 1 - engines/wintermute/base/scriptables/script_value.h | 2 +- 6 files changed, 15 insertions(+), 12 deletions(-) (limited to 'engines/wintermute/base/scriptables') diff --git a/engines/wintermute/base/scriptables/script.h b/engines/wintermute/base/scriptables/script.h index 3bb4bc48a7..ba73e1015f 100644 --- a/engines/wintermute/base/scriptables/script.h +++ b/engines/wintermute/base/scriptables/script.h @@ -55,9 +55,10 @@ public: bool copyParameters(ScStack *stack); void afterLoad(); - +private: ScValue *_operand; ScValue *_reg1; +public: bool _freezable; bool resume(); bool pause(); @@ -145,6 +146,13 @@ public: ScScript(BaseGame *inGame, ScEngine *Engine); virtual ~ScScript(); char *_filename; + bool _thread; + bool _methodThread; + char *_threadEvent; + BaseScriptHolder *_owner; + ScScript::TExternalFunction *getExternal(char *name); + bool externalCall(ScStack *stack, ScStack *thisStack, ScScript::TExternalFunction *function); +private: char **_symbols; uint32 _numSymbols; TFunctionPos *_functions; @@ -155,13 +163,7 @@ public: uint32 _numFunctions; uint32 _numMethods; uint32 _numEvents; - bool _thread; - bool _methodThread; - char *_threadEvent; - BaseScriptHolder *_owner; - ScScript::TExternalFunction *getExternal(char *name); - bool externalCall(ScStack *stack, ScStack *thisStack, ScScript::TExternalFunction *function); -private: + bool initScript(); bool initTables(); diff --git a/engines/wintermute/base/scriptables/script_ext_array.h b/engines/wintermute/base/scriptables/script_ext_array.h index 3f8d703f85..8eb86c4e69 100644 --- a/engines/wintermute/base/scriptables/script_ext_array.h +++ b/engines/wintermute/base/scriptables/script_ext_array.h @@ -45,6 +45,7 @@ public: bool scSetProperty(const char *name, ScValue *value); bool scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack, const char *name); const char *scToString(); +private: int _length; ScValue *_values; }; diff --git a/engines/wintermute/base/scriptables/script_ext_date.h b/engines/wintermute/base/scriptables/script_ext_date.h index b2df4abe94..69fe1f1ae5 100644 --- a/engines/wintermute/base/scriptables/script_ext_date.h +++ b/engines/wintermute/base/scriptables/script_ext_date.h @@ -44,9 +44,9 @@ public: bool scSetProperty(const char *name, ScValue *value); bool scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack, const char *name); const char *scToString(); +private: char *_string; TimeDate _tm; -private: Common::String _strRep; }; diff --git a/engines/wintermute/base/scriptables/script_ext_mem_buffer.h b/engines/wintermute/base/scriptables/script_ext_mem_buffer.h index c325181e3f..b5428b4bd9 100644 --- a/engines/wintermute/base/scriptables/script_ext_mem_buffer.h +++ b/engines/wintermute/base/scriptables/script_ext_mem_buffer.h @@ -46,8 +46,9 @@ public: SXMemBuffer(BaseGame *inGame, void *buffer); virtual ~SXMemBuffer(); virtual void *scToMemBuffer(); - int _size; private: + int _size; + bool resize(int newSize); void *_buffer; void cleanup(); diff --git a/engines/wintermute/base/scriptables/script_ext_object.h b/engines/wintermute/base/scriptables/script_ext_object.h index d744c58042..f7d3a7bc0f 100644 --- a/engines/wintermute/base/scriptables/script_ext_object.h +++ b/engines/wintermute/base/scriptables/script_ext_object.h @@ -39,7 +39,6 @@ public: DECLARE_PERSISTENT(SXObject, BaseObject) SXObject(BaseGame *inGame, ScStack *Stack); virtual ~SXObject(); - }; } // end of namespace WinterMute diff --git a/engines/wintermute/base/scriptables/script_value.h b/engines/wintermute/base/scriptables/script_value.h index 7a684d2334..069c36bd47 100644 --- a/engines/wintermute/base/scriptables/script_value.h +++ b/engines/wintermute/base/scriptables/script_value.h @@ -88,7 +88,7 @@ public: ScValue *getProp(const char *name); BaseScriptable *_valNative; ScValue *_valRef; -protected: +private: bool _valBool; int _valInt; double _valFloat; -- cgit v1.2.3 From fa96c9ea187cdb26e9f1ce048c9132f723e25df1 Mon Sep 17 00:00:00 2001 From: Einar Johan Trøan Sømåen Date: Wed, 25 Jul 2012 21:05:03 +0200 Subject: WINTERMUTE: "delete []" -> "delete[]" --- engines/wintermute/base/scriptables/script.cpp | 22 +++++++++++----------- .../wintermute/base/scriptables/script_engine.cpp | 6 +++--- .../wintermute/base/scriptables/script_engine.h | 2 +- .../base/scriptables/script_ext_file.cpp | 2 +- .../base/scriptables/script_ext_mem_buffer.cpp | 2 +- .../base/scriptables/script_ext_string.cpp | 2 +- .../wintermute/base/scriptables/script_value.cpp | 6 +++--- 7 files changed, 21 insertions(+), 21 deletions(-) (limited to 'engines/wintermute/base/scriptables') diff --git a/engines/wintermute/base/scriptables/script.cpp b/engines/wintermute/base/scriptables/script.cpp index 52490c0710..57e08135ed 100644 --- a/engines/wintermute/base/scriptables/script.cpp +++ b/engines/wintermute/base/scriptables/script.cpp @@ -360,13 +360,13 @@ bool ScScript::createMethodThread(ScScript *original, const char *methodName) { ////////////////////////////////////////////////////////////////////////// void ScScript::cleanup() { - if (_buffer) delete [] _buffer; + if (_buffer) delete[] _buffer; _buffer = NULL; - if (_filename) delete [] _filename; + if (_filename) delete[] _filename; _filename = NULL; - if (_symbols) delete [] _symbols; + if (_symbols) delete[] _symbols; _symbols = NULL; _numSymbols = 0; @@ -385,24 +385,24 @@ void ScScript::cleanup() { delete _stack; _stack = NULL; - if (_functions) delete [] _functions; + if (_functions) delete[] _functions; _functions = NULL; _numFunctions = 0; - if (_methods) delete [] _methods; + if (_methods) delete[] _methods; _methods = NULL; _numMethods = 0; - if (_events) delete [] _events; + if (_events) delete[] _events; _events = NULL; _numEvents = 0; if (_externals) { for (uint32 i = 0; i < _numExternals; i++) { - if (_externals[i].nu_params > 0) delete [] _externals[i].params; + if (_externals[i].nu_params > 0) delete[] _externals[i].params; } - delete [] _externals; + delete[] _externals; } _externals = NULL; _numExternals = 0; @@ -586,7 +586,7 @@ bool ScScript::executeInstruction() { runtimeError("Cannot call method '%s'. Ignored.", MethodName); _stack->pushNULL(); } - delete [] MethodName; + delete[] MethodName; break; } /* @@ -621,7 +621,7 @@ bool ScScript::executeInstruction() { } } } - delete [] MethodName; + delete[] MethodName; } break; @@ -797,7 +797,7 @@ bool ScScript::executeInstruction() { strcpy(tempStr, op1->getString()); strcat(tempStr, op2->getString()); _operand->setString(tempStr); - delete [] tempStr; + delete[] tempStr; } else if (op1->getType() == VAL_INT && op2->getType() == VAL_INT) _operand->setInt(op1->getInt() + op2->getInt()); else _operand->setFloat(op1->getFloat() + op2->getFloat()); diff --git a/engines/wintermute/base/scriptables/script_engine.cpp b/engines/wintermute/base/scriptables/script_engine.cpp index 59ec113d1f..c33d844aa1 100644 --- a/engines/wintermute/base/scriptables/script_engine.cpp +++ b/engines/wintermute/base/scriptables/script_engine.cpp @@ -127,7 +127,7 @@ byte *ScEngine::loadFile(void *data, char *filename, uint32 *size) { ////////////////////////////////////////////////////////////////////////// void ScEngine::closeFile(void *data, byte *buffer) { - delete [] buffer; + delete[] buffer; } @@ -201,7 +201,7 @@ byte *ScEngine::getCompiledScript(const char *filename, uint32 *outSize, bool ig } else { if (!_compilerAvailable) { _gameRef->LOG(0, "ScEngine::GetCompiledScript - script '%s' needs to be compiled but compiler is not available", filename); - delete [] buffer; + delete[] buffer; return NULL; } // This code will never be called, since _compilerAvailable is const false. @@ -235,7 +235,7 @@ byte *ScEngine::getCompiledScript(const char *filename, uint32 *outSize, bool ig // cleanup - delete [] buffer; + delete[] buffer; return ret; } diff --git a/engines/wintermute/base/scriptables/script_engine.h b/engines/wintermute/base/scriptables/script_engine.h index 3d85192aa6..e443ec5832 100644 --- a/engines/wintermute/base/scriptables/script_engine.h +++ b/engines/wintermute/base/scriptables/script_engine.h @@ -54,7 +54,7 @@ public: }; ~CScCachedScript() { - if (_buffer) delete [] _buffer; + if (_buffer) delete[] _buffer; }; uint32 _timestamp; diff --git a/engines/wintermute/base/scriptables/script_ext_file.cpp b/engines/wintermute/base/scriptables/script_ext_file.cpp index 92943f7485..01179bb3ad 100644 --- a/engines/wintermute/base/scriptables/script_ext_file.cpp +++ b/engines/wintermute/base/scriptables/script_ext_file.cpp @@ -457,7 +457,7 @@ bool SXFile::scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack, str[size] = '\0'; stack->pushString((char *)str); } - delete [] str; + delete[] str; } else stack->pushNULL(); } else stack->pushNULL(); diff --git a/engines/wintermute/base/scriptables/script_ext_mem_buffer.cpp b/engines/wintermute/base/scriptables/script_ext_mem_buffer.cpp index b625fad400..6b791871ad 100644 --- a/engines/wintermute/base/scriptables/script_ext_mem_buffer.cpp +++ b/engines/wintermute/base/scriptables/script_ext_mem_buffer.cpp @@ -249,7 +249,7 @@ bool SXMemBuffer::scCallMethod(ScScript *script, ScStack *stack, ScStack *thisSt strncpy(str, (const char *)_buffer + start, length); str[length] = '\0'; stack->pushString(str); - delete [] str; + delete[] str; } return STATUS_OK; } diff --git a/engines/wintermute/base/scriptables/script_ext_string.cpp b/engines/wintermute/base/scriptables/script_ext_string.cpp index 5e01e5e29c..385d7ca746 100644 --- a/engines/wintermute/base/scriptables/script_ext_string.cpp +++ b/engines/wintermute/base/scriptables/script_ext_string.cpp @@ -67,7 +67,7 @@ SXString::SXString(BaseGame *inGame, ScStack *stack): BaseScriptable(inGame) { ////////////////////////////////////////////////////////////////////////// SXString::~SXString() { - if (_string) delete [] _string; + if (_string) delete[] _string; } diff --git a/engines/wintermute/base/scriptables/script_value.cpp b/engines/wintermute/base/scriptables/script_value.cpp index 6d12c34914..59e0965f5e 100644 --- a/engines/wintermute/base/scriptables/script_value.cpp +++ b/engines/wintermute/base/scriptables/script_value.cpp @@ -122,7 +122,7 @@ ScValue::ScValue(BaseGame *inGame, const char *val): BaseClass(inGame) { void ScValue::cleanup(bool ignoreNatives) { deleteProps(); - if (_valString) delete [] _valString; + if (_valString) delete[] _valString; if (!ignoreNatives) { if (_valNative && !_persistent) { @@ -425,7 +425,7 @@ void ScValue::setString(const Common::String &val) { ////////////////////////////////////////////////////////////////////////// void ScValue::setStringVal(const char *val) { if (_valString) { - delete [] _valString; + delete[] _valString; _valString = NULL; } @@ -767,7 +767,7 @@ bool ScValue::persist(BasePersistenceManager *persistMgr) { persistMgr->transfer("", &val); _valObject[str] = val; - delete [] str; + delete[] str; } } -- cgit v1.2.3 From 1ad859a468415cc7fd93adaa84beba02aae29ad8 Mon Sep 17 00:00:00 2001 From: Einar Johan Trøan Sømåen Date: Wed, 25 Jul 2012 21:21:55 +0200 Subject: WINTERMUTE: "if(" -> "if (" --- engines/wintermute/base/scriptables/script.cpp | 36 +++++++++++----------- .../wintermute/base/scriptables/script_engine.cpp | 6 ++-- .../base/scriptables/script_ext_date.cpp | 2 +- .../base/scriptables/script_ext_file.cpp | 4 +-- .../base/scriptables/script_ext_mem_buffer.cpp | 4 +-- .../wintermute/base/scriptables/script_value.cpp | 2 +- 6 files changed, 27 insertions(+), 27 deletions(-) (limited to 'engines/wintermute/base/scriptables') diff --git a/engines/wintermute/base/scriptables/script.cpp b/engines/wintermute/base/scriptables/script.cpp index 57e08135ed..a519da5832 100644 --- a/engines/wintermute/base/scriptables/script.cpp +++ b/engines/wintermute/base/scriptables/script.cpp @@ -591,11 +591,11 @@ bool ScScript::executeInstruction() { } /* ScValue* val = var->getProp(MethodName); - if(val){ + if (val){ dw = GetFuncPos(val->getString()); - if(dw==0){ + if (dw==0){ TExternalFunction* f = GetExternal(val->getString()); - if(f){ + if (f){ ExternalCall(_stack, _thisStack, f); } else{ @@ -776,7 +776,7 @@ bool ScScript::executeInstruction() { case II_JMP_FALSE: { dw = getDWORD(); - //if(!_stack->pop()->getBool()) _iP = dw; + //if (!_stack->pop()->getBool()) _iP = dw; ScValue *val = _stack->pop(); if (!val) { runtimeError("Script corruption detected. Did you use '=' instead of '==' for comparison?"); @@ -864,7 +864,7 @@ bool ScScript::executeInstruction() { case II_NOT: op1 = _stack->pop(); - //if(op1->isNULL()) _operand->setNULL(); + //if (op1->isNULL()) _operand->setNULL(); if (op1->isNULL()) _operand->setBool(true); else _operand->setBool(!op1->getBool()); _stack->push(_operand); @@ -900,14 +900,14 @@ bool ScScript::executeInstruction() { op1 = _stack->pop(); /* - if((op1->isNULL() && !op2->isNULL()) || (!op1->isNULL() && op2->isNULL())) _operand->setBool(false); - else if(op1->isNative() && op2->isNative()){ + if ((op1->isNULL() && !op2->isNULL()) || (!op1->isNULL() && op2->isNULL())) _operand->setBool(false); + else if (op1->isNative() && op2->isNative()){ _operand->setBool(op1->getNative() == op2->getNative()); } - else if(op1->getType()==VAL_STRING || op2->getType()==VAL_STRING){ + else if (op1->getType()==VAL_STRING || op2->getType()==VAL_STRING){ _operand->setBool(scumm_stricmp(op1->getString(), op2->getString())==0); } - else if(op1->getType()==VAL_FLOAT && op2->getType()==VAL_FLOAT){ + else if (op1->getType()==VAL_FLOAT && op2->getType()==VAL_FLOAT){ _operand->setBool(op1->getFloat() == op2->getFloat()); } else{ @@ -924,14 +924,14 @@ bool ScScript::executeInstruction() { op1 = _stack->pop(); /* - if((op1->isNULL() && !op2->isNULL()) || (!op1->isNULL() && op2->isNULL())) _operand->setBool(true); - else if(op1->isNative() && op2->isNative()){ + if ((op1->isNULL() && !op2->isNULL()) || (!op1->isNULL() && op2->isNULL())) _operand->setBool(true); + else if (op1->isNative() && op2->isNative()){ _operand->setBool(op1->getNative() != op2->getNative()); } - else if(op1->getType()==VAL_STRING || op2->getType()==VAL_STRING){ + else if (op1->getType()==VAL_STRING || op2->getType()==VAL_STRING){ _operand->setBool(scumm_stricmp(op1->getString(), op2->getString())!=0); } - else if(op1->getType()==VAL_FLOAT && op2->getType()==VAL_FLOAT){ + else if (op1->getType()==VAL_FLOAT && op2->getType()==VAL_FLOAT){ _operand->setBool(op1->getFloat() != op2->getFloat()); } else{ @@ -948,7 +948,7 @@ bool ScScript::executeInstruction() { op1 = _stack->pop(); /* - if(op1->getType()==VAL_FLOAT && op2->getType()==VAL_FLOAT){ + if (op1->getType()==VAL_FLOAT && op2->getType()==VAL_FLOAT){ _operand->setBool(op1->getFloat() < op2->getFloat()); } else _operand->setBool(op1->getInt() < op2->getInt()); @@ -963,7 +963,7 @@ bool ScScript::executeInstruction() { op1 = _stack->pop(); /* - if(op1->getType()==VAL_FLOAT && op2->getType()==VAL_FLOAT){ + if (op1->getType()==VAL_FLOAT && op2->getType()==VAL_FLOAT){ _operand->setBool(op1->getFloat() > op2->getFloat()); } else _operand->setBool(op1->getInt() > op2->getInt()); @@ -978,7 +978,7 @@ bool ScScript::executeInstruction() { op1 = _stack->pop(); /* - if(op1->getType()==VAL_FLOAT && op2->getType()==VAL_FLOAT){ + if (op1->getType()==VAL_FLOAT && op2->getType()==VAL_FLOAT){ _operand->setBool(op1->getFloat() <= op2->getFloat()); } else _operand->setBool(op1->getInt() <= op2->getInt()); @@ -993,7 +993,7 @@ bool ScScript::executeInstruction() { op1 = _stack->pop(); /* - if(op1->getType()==VAL_FLOAT && op2->getType()==VAL_FLOAT){ + if (op1->getType()==VAL_FLOAT && op2->getType()==VAL_FLOAT){ _operand->setBool(op1->getFloat() >= op2->getFloat()); } else _operand->setBool(op1->getInt() >= op2->getInt()); @@ -1257,7 +1257,7 @@ bool ScScript::persist(BasePersistenceManager *persistMgr) { ////////////////////////////////////////////////////////////////////////// ScScript *ScScript::invokeEventHandler(const char *eventName, bool unbreakable) { - //if(_state!=SCRIPT_PERSISTENT) return NULL; + //if (_state!=SCRIPT_PERSISTENT) return NULL; uint32 pos = getEventPos(eventName); if (!pos) return NULL; diff --git a/engines/wintermute/base/scriptables/script_engine.cpp b/engines/wintermute/base/scriptables/script_engine.cpp index c33d844aa1..792dfd4589 100644 --- a/engines/wintermute/base/scriptables/script_engine.cpp +++ b/engines/wintermute/base/scriptables/script_engine.cpp @@ -257,14 +257,14 @@ bool ScEngine::tick() { bool obj_found=false; for(int j=0; j<_gameRef->_regObjects.getSize(); j++) { - if(_gameRef->_regObjects[j] == _scripts[i]->_waitObject) + if (_gameRef->_regObjects[j] == _scripts[i]->_waitObject) { - if(_gameRef->_regObjects[j]->IsReady()) _scripts[i]->Run(); + if (_gameRef->_regObjects[j]->IsReady()) _scripts[i]->Run(); obj_found = true; break; } } - if(!obj_found) _scripts[i]->finish(); // _waitObject no longer exists + if (!obj_found) _scripts[i]->finish(); // _waitObject no longer exists */ if (_gameRef->validObject(_scripts[i]->_waitObject)) { if (_scripts[i]->_waitObject->isReady()) _scripts[i]->run(); diff --git a/engines/wintermute/base/scriptables/script_ext_date.cpp b/engines/wintermute/base/scriptables/script_ext_date.cpp index 322fb9bc5b..d2fd3663c7 100644 --- a/engines/wintermute/base/scriptables/script_ext_date.cpp +++ b/engines/wintermute/base/scriptables/script_ext_date.cpp @@ -228,7 +228,7 @@ bool SXDate::scSetProperty(const char *name, ScValue *value) { ////////////////////////////////////////////////////////////////////////// // Name ////////////////////////////////////////////////////////////////////////// - if(strcmp(name, "Name")==0){ + if (strcmp(name, "Name")==0){ setName(value->getString()); return STATUS_OK; } diff --git a/engines/wintermute/base/scriptables/script_ext_file.cpp b/engines/wintermute/base/scriptables/script_ext_file.cpp index 01179bb3ad..7da1601bdc 100644 --- a/engines/wintermute/base/scriptables/script_ext_file.cpp +++ b/engines/wintermute/base/scriptables/script_ext_file.cpp @@ -663,12 +663,12 @@ bool SXFile::scSetProperty(const char *name, ScValue *value) { ////////////////////////////////////////////////////////////////////////// // Length ////////////////////////////////////////////////////////////////////////// - if(strcmp(name, "Length")==0){ + if (strcmp(name, "Length")==0){ int OrigLength = _length; _length = max(value->getInt(0), 0); char PropName[20]; - if(_length < OrigLength){ + if (_length < OrigLength){ for(int i=_length; iDeleteProp(PropName); diff --git a/engines/wintermute/base/scriptables/script_ext_mem_buffer.cpp b/engines/wintermute/base/scriptables/script_ext_mem_buffer.cpp index 6b791871ad..3d3f0b218b 100644 --- a/engines/wintermute/base/scriptables/script_ext_mem_buffer.cpp +++ b/engines/wintermute/base/scriptables/script_ext_mem_buffer.cpp @@ -462,12 +462,12 @@ bool SXMemBuffer::scSetProperty(const char *name, ScValue *value) { ////////////////////////////////////////////////////////////////////////// // Length ////////////////////////////////////////////////////////////////////////// - if(strcmp(name, "Length")==0){ + if (strcmp(name, "Length")==0){ int OrigLength = _length; _length = max(value->getInt(0), 0); char PropName[20]; - if(_length < OrigLength){ + if (_length < OrigLength){ for(int i=_length; iDeleteProp(PropName); diff --git a/engines/wintermute/base/scriptables/script_value.cpp b/engines/wintermute/base/scriptables/script_value.cpp index 59e0965f5e..5e824cd10c 100644 --- a/engines/wintermute/base/scriptables/script_value.cpp +++ b/engines/wintermute/base/scriptables/script_value.cpp @@ -240,7 +240,7 @@ bool ScValue::setProp(const char *name, ScValue *val, bool copyWhole, bool setAs val->_isConstVar = SetAsConst; _valObject[Name] = val; - if(_type!=VAL_NATIVE) _type = VAL_OBJECT; + if (_type!=VAL_NATIVE) _type = VAL_OBJECT; */ } -- cgit v1.2.3 From ef11f9d0c53cbdd9d88a99143de6f43f34d7e24d Mon Sep 17 00:00:00 2001 From: Einar Johan Trøan Sømåen Date: Thu, 26 Jul 2012 15:59:26 +0200 Subject: WINTERMUTE: Run Astyle with add-braces to break one-line statements into easier-to-read-code. --- engines/wintermute/base/scriptables/script.cpp | 292 +++++++++++++++------ .../wintermute/base/scriptables/script_engine.cpp | 167 +++++++++--- .../wintermute/base/scriptables/script_engine.h | 8 +- .../base/scriptables/script_ext_array.cpp | 35 ++- .../wintermute/base/scriptables/script_ext_array.h | 2 +- .../base/scriptables/script_ext_date.cpp | 9 +- .../base/scriptables/script_ext_file.cpp | 166 +++++++----- .../base/scriptables/script_ext_math.cpp | 8 +- .../base/scriptables/script_ext_mem_buffer.cpp | 118 +++++---- .../base/scriptables/script_ext_string.cpp | 120 +++++---- .../wintermute/base/scriptables/script_stack.cpp | 14 +- .../wintermute/base/scriptables/script_value.cpp | 197 ++++++++++---- 12 files changed, 771 insertions(+), 365 deletions(-) (limited to 'engines/wintermute/base/scriptables') diff --git a/engines/wintermute/base/scriptables/script.cpp b/engines/wintermute/base/scriptables/script.cpp index a519da5832..4deeb0bf39 100644 --- a/engines/wintermute/base/scriptables/script.cpp +++ b/engines/wintermute/base/scriptables/script.cpp @@ -249,17 +249,23 @@ bool ScScript::create(const char *filename, byte *buffer, uint32 size, BaseScrip _threadEvent = NULL; _filename = new char[strlen(filename) + 1]; - if (_filename) strcpy(_filename, filename); + if (_filename) { + strcpy(_filename, filename); + } _buffer = new byte [size]; - if (!_buffer) return STATUS_FAILED; + if (!_buffer) { + return STATUS_FAILED; + } memcpy(_buffer, buffer, size); _bufferSize = size; bool res = initScript(); - if (DID_FAIL(res)) return res; + if (DID_FAIL(res)) { + return res; + } // establish global variables table _globals = new ScValue(_gameRef); @@ -277,22 +283,30 @@ bool ScScript::createThread(ScScript *original, uint32 initIP, const char *event _thread = true; _methodThread = false; _threadEvent = new char[strlen(eventName) + 1]; - if (_threadEvent) strcpy(_threadEvent, eventName); + if (_threadEvent) { + strcpy(_threadEvent, eventName); + } // copy filename _filename = new char[strlen(original->_filename) + 1]; - if (_filename) strcpy(_filename, original->_filename); + if (_filename) { + strcpy(_filename, original->_filename); + } // copy buffer _buffer = new byte [original->_bufferSize]; - if (!_buffer) return STATUS_FAILED; + if (!_buffer) { + return STATUS_FAILED; + } memcpy(_buffer, original->_buffer, original->_bufferSize); _bufferSize = original->_bufferSize; // initialize bool res = initScript(); - if (DID_FAIL(res)) return res; + if (DID_FAIL(res)) { + return res; + } // copy globals _globals = original->_globals; @@ -317,29 +331,39 @@ bool ScScript::createThread(ScScript *original, uint32 initIP, const char *event ////////////////////////////////////////////////////////////////////////// bool ScScript::createMethodThread(ScScript *original, const char *methodName) { uint32 ip = original->getMethodPos(methodName); - if (ip == 0) return STATUS_FAILED; + if (ip == 0) { + return STATUS_FAILED; + } cleanup(); _thread = true; _methodThread = true; _threadEvent = new char[strlen(methodName) + 1]; - if (_threadEvent) strcpy(_threadEvent, methodName); + if (_threadEvent) { + strcpy(_threadEvent, methodName); + } // copy filename _filename = new char[strlen(original->_filename) + 1]; - if (_filename) strcpy(_filename, original->_filename); + if (_filename) { + strcpy(_filename, original->_filename); + } // copy buffer _buffer = new byte [original->_bufferSize]; - if (!_buffer) return STATUS_FAILED; + if (!_buffer) { + return STATUS_FAILED; + } memcpy(_buffer, original->_buffer, original->_bufferSize); _bufferSize = original->_bufferSize; // initialize bool res = initScript(); - if (DID_FAIL(res)) return res; + if (DID_FAIL(res)) { + return res; + } // copy globals _globals = original->_globals; @@ -360,17 +384,25 @@ bool ScScript::createMethodThread(ScScript *original, const char *methodName) { ////////////////////////////////////////////////////////////////////////// void ScScript::cleanup() { - if (_buffer) delete[] _buffer; + if (_buffer) { + delete[] _buffer; + } _buffer = NULL; - if (_filename) delete[] _filename; + if (_filename) { + delete[] _filename; + } _filename = NULL; - if (_symbols) delete[] _symbols; + if (_symbols) { + delete[] _symbols; + } _symbols = NULL; _numSymbols = 0; - if (_globals && !_thread) delete _globals; + if (_globals && !_thread) { + delete _globals; + } _globals = NULL; delete _scopeStack; @@ -385,22 +417,30 @@ void ScScript::cleanup() { delete _stack; _stack = NULL; - if (_functions) delete[] _functions; + if (_functions) { + delete[] _functions; + } _functions = NULL; _numFunctions = 0; - if (_methods) delete[] _methods; + if (_methods) { + delete[] _methods; + } _methods = NULL; _numMethods = 0; - if (_events) delete[] _events; + if (_events) { + delete[] _events; + } _events = NULL; _numEvents = 0; if (_externals) { for (uint32 i = 0; i < _numExternals; i++) { - if (_externals[i].nu_params > 0) delete[] _externals[i].params; + if (_externals[i].nu_params > 0) { + delete[] _externals[i].params; + } } delete[] _externals; } @@ -423,7 +463,7 @@ void ScScript::cleanup() { _waitScript = NULL; _parentScript = NULL; // ref only - + delete _scriptStream; } @@ -460,7 +500,9 @@ double ScScript::getFloat() { ////////////////////////////////////////////////////////////////////////// char *ScScript::getString() { char *ret = (char *)(_buffer + _iP); - while (*(char *)(_buffer + _iP) != '\0') _iP++; + while (*(char *)(_buffer + _iP) != '\0') { + _iP++; + } _iP++; // string terminator _scriptStream->seek(_iP); @@ -489,12 +531,14 @@ bool ScScript::executeInstruction() { dw = getDWORD(); if (_scopeStack->_sP < 0) { _globals->setProp(_symbols[dw], _operand); - if (_gameRef->getDebugMgr()->_enabled) + if (_gameRef->getDebugMgr()->_enabled) { _gameRef->getDebugMgr()->onVariableInit(WME_DBGVAR_SCRIPT, this, NULL, _globals->getProp(_symbols[dw]), _symbols[dw]); + } } else { _scopeStack->getTop()->setProp(_symbols[dw], _operand); - if (_gameRef->getDebugMgr()->_enabled) + if (_gameRef->getDebugMgr()->_enabled) { _gameRef->getDebugMgr()->onVariableInit(WME_DBGVAR_SCOPE, this, _scopeStack->getTop(), _scopeStack->getTop()->getProp(_symbols[dw]), _symbols[dw]); + } } break; @@ -508,8 +552,9 @@ bool ScScript::executeInstruction() { _operand->setNULL(); _engine->_globals->setProp(_symbols[dw], _operand, false, inst == II_DEF_CONST_VAR); - if (_gameRef->getDebugMgr()->_enabled) + if (_gameRef->getDebugMgr()->_enabled) { _gameRef->getDebugMgr()->onVariableInit(WME_DBGVAR_GLOBAL, this, NULL, _engine->_globals->getProp(_symbols[dw]), _symbols[dw]); + } } break; } @@ -521,14 +566,20 @@ bool ScScript::executeInstruction() { _scopeStack->pop(); _iP = (uint32)_callStack->pop()->getInt(); - if (_scopeStack->_sP < 0) _gameRef->getDebugMgr()->onScriptChangeScope(this, NULL); - else _gameRef->getDebugMgr()->onScriptChangeScope(this, _scopeStack->getTop()); + if (_scopeStack->_sP < 0) { + _gameRef->getDebugMgr()->onScriptChangeScope(this, NULL); + } else { + _gameRef->getDebugMgr()->onScriptChangeScope(this, _scopeStack->getTop()); + } } else { if (_thread) { _state = SCRIPT_THREAD_FINISHED; } else { - if (_numEvents == 0 && _numMethods == 0) _state = SCRIPT_FINISHED; - else _state = SCRIPT_PERSISTENT; + if (_numEvents == 0 && _numMethods == 0) { + _state = SCRIPT_FINISHED; + } else { + _state = SCRIPT_PERSISTENT; + } } } @@ -557,7 +608,9 @@ bool ScScript::executeInstruction() { strcpy(MethodName, str); ScValue *var = _stack->pop(); - if (var->_type == VAL_VARIABLE_REF) var = var->_valRef; + if (var->_type == VAL_VARIABLE_REF) { + var = var->_valRef; + } bool res = STATUS_FAILED; bool TriedNative = false; @@ -612,7 +665,9 @@ bool ScScript::executeInstruction() { */ else { res = STATUS_FAILED; - if (var->_type == VAL_NATIVE && !TriedNative) res = var->_valNative->scCallMethod(this, _stack, _thisStack, MethodName); + if (var->_type == VAL_NATIVE && !TriedNative) { + res = var->_valNative->scCallMethod(this, _stack, _thisStack, MethodName); + } if (DID_FAIL(res)) { _stack->correctParams(0); @@ -631,7 +686,9 @@ bool ScScript::executeInstruction() { TExternalFunction *f = getExternal(_symbols[SymbolIndex]); if (f) { externalCall(_stack, _thisStack, f); - } else _gameRef->ExternalCall(this, _stack, _thisStack, _symbols[SymbolIndex]); + } else { + _gameRef->ExternalCall(this, _stack, _thisStack, _symbols[SymbolIndex]); + } break; } @@ -639,8 +696,11 @@ bool ScScript::executeInstruction() { _operand->setNULL(); _scopeStack->push(_operand); - if (_scopeStack->_sP < 0) _gameRef->getDebugMgr()->onScriptChangeScope(this, NULL); - else _gameRef->getDebugMgr()->onScriptChangeScope(this, _scopeStack->getTop()); + if (_scopeStack->_sP < 0) { + _gameRef->getDebugMgr()->onScriptChangeScope(this, NULL); + } else { + _gameRef->getDebugMgr()->onScriptChangeScope(this, _scopeStack->getTop()); + } break; @@ -663,7 +723,9 @@ bool ScScript::executeInstruction() { if (false && /*var->_type==VAL_OBJECT ||*/ var->_type == VAL_NATIVE) { _operand->setReference(var); _stack->push(_operand); - } else _stack->push(var); + } else { + _stack->push(var); + } break; } @@ -683,15 +745,19 @@ bool ScScript::executeInstruction() { runtimeError("Script stack corruption detected. Please report this script at WME bug reports forum."); var->setNULL(); } else { - if (val->getType() == VAL_VARIABLE_REF) val = val->_valRef; - if (val->_type == VAL_NATIVE) var->setValue(val); - else { + if (val->getType() == VAL_VARIABLE_REF) { + val = val->_valRef; + } + if (val->_type == VAL_NATIVE) { + var->setValue(val); + } else { var->copy(val); } } - if (_gameRef->getDebugMgr()->_enabled) + if (_gameRef->getDebugMgr()->_enabled) { _gameRef->getDebugMgr()->onVariableChangeValue(var, val); + } } break; @@ -740,8 +806,11 @@ bool ScScript::executeInstruction() { case II_PUSH_BY_EXP: { str = _stack->pop()->getString(); ScValue *val = _stack->pop()->getProp(str); - if (val) _stack->push(val); - else _stack->pushNULL(); + if (val) { + _stack->push(val); + } else { + _stack->pushNULL(); + } break; } @@ -754,10 +823,13 @@ bool ScScript::executeInstruction() { if (val == NULL) { runtimeError("Script stack corruption detected. Please report this script at WME bug reports forum."); var->setNULL(); - } else var->setProp(str, val); + } else { + var->setProp(str, val); + } - if (_gameRef->getDebugMgr()->_enabled) + if (_gameRef->getDebugMgr()->_enabled) { _gameRef->getDebugMgr()->onVariableChangeValue(var, NULL); + } break; } @@ -781,7 +853,9 @@ bool ScScript::executeInstruction() { if (!val) { runtimeError("Script corruption detected. Did you use '=' instead of '==' for comparison?"); } else { - if (!val->getBool()) _iP = dw; + if (!val->getBool()) { + _iP = dw; + } } break; } @@ -790,17 +864,19 @@ bool ScScript::executeInstruction() { op2 = _stack->pop(); op1 = _stack->pop(); - if (op1->isNULL() || op2->isNULL()) + if (op1->isNULL() || op2->isNULL()) { _operand->setNULL(); - else if (op1->getType() == VAL_STRING || op2->getType() == VAL_STRING) { + } else if (op1->getType() == VAL_STRING || op2->getType() == VAL_STRING) { char *tempStr = new char [strlen(op1->getString()) + strlen(op2->getString()) + 1]; strcpy(tempStr, op1->getString()); strcat(tempStr, op2->getString()); _operand->setString(tempStr); delete[] tempStr; - } else if (op1->getType() == VAL_INT && op2->getType() == VAL_INT) + } else if (op1->getType() == VAL_INT && op2->getType() == VAL_INT) { _operand->setInt(op1->getInt() + op2->getInt()); - else _operand->setFloat(op1->getFloat() + op2->getFloat()); + } else { + _operand->setFloat(op1->getFloat() + op2->getFloat()); + } _stack->push(_operand); @@ -810,11 +886,13 @@ bool ScScript::executeInstruction() { op2 = _stack->pop(); op1 = _stack->pop(); - if (op1->isNULL() || op2->isNULL()) + if (op1->isNULL() || op2->isNULL()) { _operand->setNULL(); - else if (op1->getType() == VAL_INT && op2->getType() == VAL_INT) + } else if (op1->getType() == VAL_INT && op2->getType() == VAL_INT) { _operand->setInt(op1->getInt() - op2->getInt()); - else _operand->setFloat(op1->getFloat() - op2->getFloat()); + } else { + _operand->setFloat(op1->getFloat() - op2->getFloat()); + } _stack->push(_operand); @@ -824,10 +902,13 @@ bool ScScript::executeInstruction() { op2 = _stack->pop(); op1 = _stack->pop(); - if (op1->isNULL() || op2->isNULL()) _operand->setNULL(); - else if (op1->getType() == VAL_INT && op2->getType() == VAL_INT) + if (op1->isNULL() || op2->isNULL()) { + _operand->setNULL(); + } else if (op1->getType() == VAL_INT && op2->getType() == VAL_INT) { _operand->setInt(op1->getInt() * op2->getInt()); - else _operand->setFloat(op1->getFloat() * op2->getFloat()); + } else { + _operand->setFloat(op1->getFloat() * op2->getFloat()); + } _stack->push(_operand); @@ -837,11 +918,15 @@ bool ScScript::executeInstruction() { op2 = _stack->pop(); op1 = _stack->pop(); - if (op2->getFloat() == 0.0f) + if (op2->getFloat() == 0.0f) { runtimeError("Division by zero."); + } - if (op1->isNULL() || op2->isNULL() || op2->getFloat() == 0.0f) _operand->setNULL(); - else _operand->setFloat(op1->getFloat() / op2->getFloat()); + if (op1->isNULL() || op2->isNULL() || op2->getFloat() == 0.0f) { + _operand->setNULL(); + } else { + _operand->setFloat(op1->getFloat() / op2->getFloat()); + } _stack->push(_operand); @@ -851,12 +936,15 @@ bool ScScript::executeInstruction() { op2 = _stack->pop(); op1 = _stack->pop(); - if (op2->getInt() == 0) + if (op2->getInt() == 0) { runtimeError("Division by zero."); + } - if (op1->isNULL() || op2->isNULL() || op2->getInt() == 0) + if (op1->isNULL() || op2->isNULL() || op2->getInt() == 0) { _operand->setNULL(); - else _operand->setInt(op1->getInt() % op2->getInt()); + } else { + _operand->setInt(op1->getInt() % op2->getInt()); + } _stack->push(_operand); @@ -865,8 +953,11 @@ bool ScScript::executeInstruction() { case II_NOT: op1 = _stack->pop(); //if (op1->isNULL()) _operand->setNULL(); - if (op1->isNULL()) _operand->setBool(true); - else _operand->setBool(!op1->getBool()); + if (op1->isNULL()) { + _operand->setBool(true); + } else { + _operand->setBool(!op1->getBool()); + } _stack->push(_operand); break; @@ -1060,8 +1151,9 @@ bool ScScript::executeInstruction() { ////////////////////////////////////////////////////////////////////////// uint32 ScScript::getFuncPos(const char *name) { for (uint32 i = 0; i < _numFunctions; i++) { - if (strcmp(name, _functions[i].name) == 0) + if (strcmp(name, _functions[i].name) == 0) { return _functions[i].pos; + } } return 0; } @@ -1070,8 +1162,9 @@ uint32 ScScript::getFuncPos(const char *name) { ////////////////////////////////////////////////////////////////////////// uint32 ScScript::getMethodPos(const char *name) { for (uint32 i = 0; i < _numMethods; i++) { - if (strcmp(name, _methods[i].name) == 0) + if (strcmp(name, _methods[i].name) == 0) { return _methods[i].pos; + } } return 0; } @@ -1083,20 +1176,23 @@ ScValue *ScScript::getVar(char *name) { // scope locals if (_scopeStack->_sP >= 0) { - if (_scopeStack->getTop()->propExists(name)) + if (_scopeStack->getTop()->propExists(name)) { ret = _scopeStack->getTop()->getProp(name); + } } // script globals if (ret == NULL) { - if (_globals->propExists(name)) + if (_globals->propExists(name)) { ret = _globals->getProp(name); + } } // engine globals if (ret == NULL) { - if (_engine->_globals->propExists(name)) + if (_engine->_globals->propExists(name)) { ret = _engine->_globals->getProp(name); + } } if (ret == NULL) { @@ -1162,7 +1258,9 @@ bool ScScript::finish(bool includingThreads) { if (_state != SCRIPT_FINISHED && includingThreads) { _state = SCRIPT_FINISHED; finishThreads(); - } else _state = SCRIPT_FINISHED; + } else { + _state = SCRIPT_FINISHED; + } return STATUS_OK; @@ -1188,8 +1286,9 @@ void ScScript::runtimeError(const char *fmt, ...) { _gameRef->LOG(0, "Runtime error. Script '%s', line %d", _filename, _currentLine); _gameRef->LOG(0, " %s", buff); - if (!_gameRef->_suppressScriptErrors) + if (!_gameRef->_suppressScriptErrors) { _gameRef->quickMessage("Script runtime error. View log for details."); + } } @@ -1217,8 +1316,8 @@ bool ScScript::persist(BasePersistenceManager *persistMgr) { initTables(); } else { _buffer = NULL; - _scriptStream = NULL; - } + _scriptStream = NULL; + } } persistMgr->transfer(TMEMBER(_callStack)); @@ -1249,7 +1348,9 @@ bool ScScript::persist(BasePersistenceManager *persistMgr) { persistMgr->transfer(TMEMBER(_unbreakable)); persistMgr->transfer(TMEMBER(_parentScript)); - if (!persistMgr->getIsSaving()) _tracingMode = false; + if (!persistMgr->getIsSaving()) { + _tracingMode = false; + } return STATUS_OK; } @@ -1260,7 +1361,9 @@ ScScript *ScScript::invokeEventHandler(const char *eventName, bool unbreakable) //if (_state!=SCRIPT_PERSISTENT) return NULL; uint32 pos = getEventPos(eventName); - if (!pos) return NULL; + if (!pos) { + return NULL; + } ScScript *thread = new ScScript(_gameRef, _engine); if (thread) { @@ -1274,7 +1377,9 @@ ScScript *ScScript::invokeEventHandler(const char *eventName, bool unbreakable) delete thread; return NULL; } - } else return NULL; + } else { + return NULL; + } } @@ -1282,7 +1387,9 @@ ScScript *ScScript::invokeEventHandler(const char *eventName, bool unbreakable) ////////////////////////////////////////////////////////////////////////// uint32 ScScript::getEventPos(const char *name) { for (int i = _numEvents - 1; i >= 0; i--) { - if (scumm_stricmp(name, _events[i].name) == 0) return _events[i].pos; + if (scumm_stricmp(name, _events[i].name) == 0) { + return _events[i].pos; + } } return 0; } @@ -1307,7 +1414,9 @@ bool ScScript::pause() { return STATUS_FAILED; } - if (!_freezable || _state == SCRIPT_PERSISTENT) return STATUS_OK; + if (!_freezable || _state == SCRIPT_PERSISTENT) { + return STATUS_OK; + } _origState = _state; _state = SCRIPT_PAUSED; @@ -1318,7 +1427,9 @@ bool ScScript::pause() { ////////////////////////////////////////////////////////////////////////// bool ScScript::resume() { - if (_state != SCRIPT_PAUSED) return STATUS_OK; + if (_state != SCRIPT_PAUSED) { + return STATUS_OK; + } _state = _origState; return STATUS_OK; @@ -1328,8 +1439,9 @@ bool ScScript::resume() { ////////////////////////////////////////////////////////////////////////// ScScript::TExternalFunction *ScScript::getExternal(char *name) { for (uint32 i = 0; i < _numExternals; i++) { - if (strcmp(name, _externals[i].name) == 0) + if (strcmp(name, _externals[i].name) == 0) { return &_externals[i]; + } } return NULL; } @@ -1354,7 +1466,9 @@ bool ScScript::copyParameters(ScStack *stack) { } _stack->pushInt(NumParams); - for (i = 0; i < NumParams; i++) stack->pop(); + for (i = 0; i < NumParams; i++) { + stack->pop(); + } return STATUS_OK; } @@ -1364,8 +1478,9 @@ bool ScScript::copyParameters(ScStack *stack) { bool ScScript::finishThreads() { for (int i = 0; i < _engine->_scripts.getSize(); i++) { ScScript *scr = _engine->_scripts[i]; - if (scr->_thread && scr->_state != SCRIPT_FINISHED && scr->_owner == _owner && scumm_stricmp(scr->_filename, _filename) == 0) + if (scr->_thread && scr->_state != SCRIPT_FINISHED && scr->_owner == _owner && scumm_stricmp(scr->_filename, _filename) == 0) { scr->finish(true); + } } return STATUS_OK; } @@ -1385,9 +1500,13 @@ const char *ScScript::dbgGetFilename() { ////////////////////////////////////////////////////////////////////////// bool ScScript::dbgSendScript(IWmeDebugClient *client) { - if (_methodThread) client->onScriptMethodThreadInit(this, _parentScript, _threadEvent); - else if (_thread) client->onScriptEventThreadInit(this, _parentScript, _threadEvent); - else client->onScriptInit(this); + if (_methodThread) { + client->onScriptMethodThreadInit(this, _parentScript, _threadEvent); + } else if (_thread) { + client->onScriptEventThreadInit(this, _parentScript, _threadEvent); + } else { + client->onScriptInit(this); + } return dbgSendVariables(client); return STATUS_OK; @@ -1421,8 +1540,11 @@ int ScScript::dbgGetNumBreakpoints() { ////////////////////////////////////////////////////////////////////////// int ScScript::dbgGetBreakpoint(int index) { - if (index >= 0 && index < _breakpoints.getSize()) return _breakpoints[index]; - else return -1; + if (index >= 0 && index < _breakpoints.getSize()) { + return _breakpoints[index]; + } else { + return -1; + } } ////////////////////////////////////////////////////////////////////////// diff --git a/engines/wintermute/base/scriptables/script_engine.cpp b/engines/wintermute/base/scriptables/script_engine.cpp index 792dfd4589..356617094d 100644 --- a/engines/wintermute/base/scriptables/script_engine.cpp +++ b/engines/wintermute/base/scriptables/script_engine.cpp @@ -47,8 +47,11 @@ IMPLEMENT_PERSISTENT(ScEngine, true) ScEngine::ScEngine(BaseGame *inGame): BaseClass(inGame) { _gameRef->LOG(0, "Initializing scripting engine..."); - if (_compilerAvailable) _gameRef->LOG(0, " Script compiler bound successfuly"); - else _gameRef->LOG(0, " Script compiler is NOT available"); + if (_compilerAvailable) { + _gameRef->LOG(0, " Script compiler bound successfuly"); + } else { + _gameRef->LOG(0, " Script compiler is NOT available"); + } _globals = new ScValue(_gameRef); @@ -68,7 +71,9 @@ ScEngine::ScEngine(BaseGame *inGame): BaseClass(inGame) { } // prepare script cache - for (int i = 0; i < MAX_CACHED_SCRIPTS; i++) _cachedScripts[i] = NULL; + for (int i = 0; i < MAX_CACHED_SCRIPTS; i++) { + _cachedScripts[i] = NULL; + } _currentScript = NULL; @@ -99,7 +104,9 @@ ScEngine::~ScEngine() { ////////////////////////////////////////////////////////////////////////// bool ScEngine::cleanup() { for (int i = 0; i < _scripts.getSize(); i++) { - if (!_scripts[i]->_thread && _scripts[i]->_owner) _scripts[i]->_owner->removeScript(_scripts[i]); + if (!_scripts[i]->_thread && _scripts[i]->_owner) { + _scripts[i]->_owner->removeScript(_scripts[i]); + } delete _scripts[i]; _scripts.removeAt(i); i--; @@ -143,7 +150,9 @@ ScScript *ScEngine::runScript(const char *filename, BaseScriptHolder *owner) { // get script from cache compBuffer = getCompiledScript(filename, &compSize); - if (!compBuffer) return NULL; + if (!compBuffer) { + return NULL; + } // add new script ScScript *script = new ScScript(_gameRef, this); @@ -155,8 +164,11 @@ ScScript *ScEngine::runScript(const char *filename, BaseScriptHolder *owner) { } else { // publish the "self" pseudo-variable ScValue val(_gameRef); - if (owner)val.setNative(owner, true); - else val.setNULL(); + if (owner) { + val.setNative(owner, true); + } else { + val.setNULL(); + } script->_globals->setProp("self", &val); script->_globals->setProp("this", &val); @@ -226,7 +238,9 @@ byte *ScEngine::getCompiledScript(const char *filename, uint32 *outSize, bool ig } } - if (_cachedScripts[index] != NULL) delete _cachedScripts[index]; + if (_cachedScripts[index] != NULL) { + delete _cachedScripts[index]; + } _cachedScripts[index] = cachedScript; ret = cachedScript->_buffer; @@ -244,8 +258,9 @@ byte *ScEngine::getCompiledScript(const char *filename, uint32 *outSize, bool ig ////////////////////////////////////////////////////////////////////////// bool ScEngine::tick() { - if (_scripts.getSize() == 0) + if (_scripts.getSize() == 0) { return STATUS_OK; + } // resolve waiting scripts @@ -267,16 +282,24 @@ bool ScEngine::tick() { if (!obj_found) _scripts[i]->finish(); // _waitObject no longer exists */ if (_gameRef->validObject(_scripts[i]->_waitObject)) { - if (_scripts[i]->_waitObject->isReady()) _scripts[i]->run(); - } else _scripts[i]->finish(); + if (_scripts[i]->_waitObject->isReady()) { + _scripts[i]->run(); + } + } else { + _scripts[i]->finish(); + } break; } case SCRIPT_SLEEPING: { if (_scripts[i]->_waitFrozen) { - if (_scripts[i]->_waitTime <= g_system->getMillis()) _scripts[i]->run(); + if (_scripts[i]->_waitTime <= g_system->getMillis()) { + _scripts[i]->run(); + } } else { - if (_scripts[i]->_waitTime <= _gameRef->_timer) _scripts[i]->run(); + if (_scripts[i]->_waitTime <= _gameRef->_timer) { + _scripts[i]->run(); + } } break; } @@ -309,7 +332,9 @@ bool ScEngine::tick() { for (int i = 0; i < _scripts.getSize(); i++) { // skip paused scripts - if (_scripts[i]->_state == SCRIPT_PAUSED) continue; + if (_scripts[i]->_state == SCRIPT_PAUSED) { + continue; + } // time sliced script if (_scripts[i]->_timeSlice > 0) { @@ -318,20 +343,26 @@ bool ScEngine::tick() { _currentScript = _scripts[i]; _scripts[i]->executeInstruction(); } - if (_isProfiling && _scripts[i]->_filename) addScriptTime(_scripts[i]->_filename, g_system->getMillis() - StartTime); + if (_isProfiling && _scripts[i]->_filename) { + addScriptTime(_scripts[i]->_filename, g_system->getMillis() - StartTime); + } } // normal script else { uint32 startTime = 0; bool isProfiling = _isProfiling; - if (isProfiling) startTime = g_system->getMillis(); + if (isProfiling) { + startTime = g_system->getMillis(); + } while (_scripts[i]->_state == SCRIPT_RUNNING) { _currentScript = _scripts[i]; _scripts[i]->executeInstruction(); } - if (isProfiling && _scripts[i]->_filename) addScriptTime(_scripts[i]->_filename, g_system->getMillis() - startTime); + if (isProfiling && _scripts[i]->_filename) { + addScriptTime(_scripts[i]->_filename, g_system->getMillis() - startTime); + } } _currentScript = NULL; } @@ -346,7 +377,9 @@ bool ScEngine::tick() { bool ScEngine::tickUnbreakable() { // execute unbreakable scripts for (int i = 0; i < _scripts.getSize(); i++) { - if (!_scripts[i]->_unbreakable) continue; + if (!_scripts[i]->_unbreakable) { + continue; + } while (_scripts[i]->_state == SCRIPT_RUNNING) { _currentScript = _scripts[i]; @@ -366,7 +399,9 @@ bool ScEngine::removeFinishedScripts() { // remove finished scripts for (int i = 0; i < _scripts.getSize(); i++) { if (_scripts[i]->_state == SCRIPT_FINISHED || _scripts[i]->_state == SCRIPT_ERROR) { - if (!_scripts[i]->_thread && _scripts[i]->_owner) _scripts[i]->_owner->removeScript(_scripts[i]); + if (!_scripts[i]->_thread && _scripts[i]->_owner) { + _scripts[i]->_owner->removeScript(_scripts[i]); + } _gameRef->getDebugMgr()->onScriptShutdown(_scripts[i]); delete _scripts[i]; _scripts.removeAt(i); @@ -382,7 +417,9 @@ int ScEngine::getNumScripts(int *running, int *waiting, int *persistent) { int numRunning = 0, numWaiting = 0, numPersistent = 0, numTotal = 0; for (int i = 0; i < _scripts.getSize(); i++) { - if (_scripts[i]->_state == SCRIPT_FINISHED) continue; + if (_scripts[i]->_state == SCRIPT_FINISHED) { + continue; + } switch (_scripts[i]->_state) { case SCRIPT_RUNNING: case SCRIPT_SLEEPING: @@ -401,9 +438,15 @@ int ScEngine::getNumScripts(int *running, int *waiting, int *persistent) { } numTotal++; } - if (running) *running = numRunning; - if (waiting) *waiting = numWaiting; - if (persistent) *persistent = numPersistent; + if (running) { + *running = numRunning; + } + if (waiting) { + *waiting = numWaiting; + } + if (persistent) { + *persistent = numPersistent; + } return numTotal; } @@ -426,7 +469,9 @@ bool ScEngine::resetObject(BaseObject *Object) { // terminate all scripts waiting for this object for (int i = 0; i < _scripts.getSize(); i++) { if (_scripts[i]->_state == SCRIPT_WAITING && _scripts[i]->_waitObject == Object) { - if (!_gameRef->_compatKillMethodThreads) resetScript(_scripts[i]); + if (!_gameRef->_compatKillMethodThreads) { + resetScript(_scripts[i]); + } bool IsThread = _scripts[i]->_methodThread || _scripts[i]->_thread; _scripts[i]->finish(!IsThread); // 1.9b1 - top-level script kills its threads as well @@ -448,7 +493,9 @@ bool ScEngine::resetScript(ScScript *script) { ////////////////////////////////////////////////////////////////////////// bool ScEngine::persist(BasePersistenceManager *persistMgr) { - if (!persistMgr->getIsSaving()) cleanup(); + if (!persistMgr->getIsSaving()) { + cleanup(); + } persistMgr->transfer(TMEMBER(_gameRef)); persistMgr->transfer(TMEMBER(_currentScript)); @@ -474,7 +521,9 @@ void ScEngine::editorCleanup() { ////////////////////////////////////////////////////////////////////////// bool ScEngine::pauseAll() { for (int i = 0; i < _scripts.getSize(); i++) { - if (_scripts[i] != _currentScript) _scripts[i]->pause(); + if (_scripts[i] != _currentScript) { + _scripts[i]->pause(); + } } return STATUS_OK; @@ -483,8 +532,9 @@ bool ScEngine::pauseAll() { ////////////////////////////////////////////////////////////////////////// bool ScEngine::resumeAll() { - for (int i = 0; i < _scripts.getSize(); i++) + for (int i = 0; i < _scripts.getSize(); i++) { _scripts[i]->resume(); + } return STATUS_OK; } @@ -493,7 +543,9 @@ bool ScEngine::resumeAll() { ////////////////////////////////////////////////////////////////////////// bool ScEngine::isValidScript(ScScript *script) { for (int i = 0; i < _scripts.getSize(); i++) { - if (_scripts[i] == script) return true; + if (_scripts[i] == script) { + return true; + } } return false; } @@ -511,14 +563,17 @@ bool ScEngine::dbgSendScripts(IWmeDebugClient *client) { // process normal scripts first for (int i = 0; i < _scripts.getSize(); i++) { - if (_scripts[i]->_thread || _scripts[i]->_methodThread) continue; + if (_scripts[i]->_thread || _scripts[i]->_methodThread) { + continue; + } _scripts[i]->dbgSendScript(client); } // and threads later for (int i = 0; i < _scripts.getSize(); i++) { - if (_scripts[i]->_thread || _scripts[i]->_methodThread) + if (_scripts[i]->_thread || _scripts[i]->_methodThread) { _scripts[i]->dbgSendScript(client); + } } return STATUS_OK; @@ -526,7 +581,9 @@ bool ScEngine::dbgSendScripts(IWmeDebugClient *client) { ////////////////////////////////////////////////////////////////////////// bool ScEngine::addBreakpoint(const char *scriptFilename, int line) { - if (!_gameRef->getDebugMgr()->_enabled) return STATUS_OK; + if (!_gameRef->getDebugMgr()->_enabled) { + return STATUS_OK; + } CScBreakpoint *bp = NULL; for (int i = 0; i < _breakpoints.getSize(); i++) { @@ -541,7 +598,9 @@ bool ScEngine::addBreakpoint(const char *scriptFilename, int line) { } for (int i = 0; i < bp->_lines.getSize(); i++) { - if (bp->_lines[i] == line) return STATUS_OK; + if (bp->_lines[i] == line) { + return STATUS_OK; + } } bp->_lines.add(line); @@ -553,7 +612,9 @@ bool ScEngine::addBreakpoint(const char *scriptFilename, int line) { ////////////////////////////////////////////////////////////////////////// bool ScEngine::removeBreakpoint(const char *scriptFilename, int line) { - if (!_gameRef->getDebugMgr()->_enabled) return STATUS_OK; + if (!_gameRef->getDebugMgr()->_enabled) { + return STATUS_OK; + } for (int i = 0; i < _breakpoints.getSize(); i++) { if (scumm_stricmp(_breakpoints[i]->_filename.c_str(), scriptFilename) == 0) { @@ -578,7 +639,9 @@ bool ScEngine::removeBreakpoint(const char *scriptFilename, int line) { ////////////////////////////////////////////////////////////////////////// bool ScEngine::refreshScriptBreakpoints() { - if (!_gameRef->getDebugMgr()->_enabled) return STATUS_OK; + if (!_gameRef->getDebugMgr()->_enabled) { + return STATUS_OK; + } for (int i = 0; i < _scripts.getSize(); i++) { refreshScriptBreakpoints(_scripts[i]); @@ -588,9 +651,13 @@ bool ScEngine::refreshScriptBreakpoints() { ////////////////////////////////////////////////////////////////////////// bool ScEngine::refreshScriptBreakpoints(ScScript *script) { - if (!_gameRef->getDebugMgr()->_enabled) return STATUS_OK; + if (!_gameRef->getDebugMgr()->_enabled) { + return STATUS_OK; + } - if (!script || !script->_filename) return STATUS_FAILED; + if (!script || !script->_filename) { + return STATUS_FAILED; + } for (int i = 0; i < _breakpoints.getSize(); i++) { if (scumm_stricmp(_breakpoints[i]->_filename.c_str(), script->_filename) == 0) { @@ -598,14 +665,18 @@ bool ScEngine::refreshScriptBreakpoints(ScScript *script) { return STATUS_OK; } } - if (script->_breakpoints.getSize() > 0) script->_breakpoints.removeAll(); + if (script->_breakpoints.getSize() > 0) { + script->_breakpoints.removeAll(); + } return STATUS_OK; } ////////////////////////////////////////////////////////////////////////// bool ScEngine::saveBreakpoints() { - if (!_gameRef->getDebugMgr()->_enabled) return STATUS_OK; + if (!_gameRef->getDebugMgr()->_enabled) { + return STATUS_OK; + } char text[512]; @@ -628,7 +699,9 @@ bool ScEngine::saveBreakpoints() { ////////////////////////////////////////////////////////////////////////// bool ScEngine::loadBreakpoints() { - if (!_gameRef->getDebugMgr()->_enabled) return STATUS_OK; + if (!_gameRef->getDebugMgr()->_enabled) { + return STATUS_OK; + } char key[100]; @@ -641,7 +714,9 @@ bool ScEngine::loadBreakpoints() { char *path = BaseUtils::strEntry(0, breakpoint.c_str(), ':'); char *line = BaseUtils::strEntry(1, breakpoint.c_str(), ':'); - if (path != NULL && line != NULL) addBreakpoint(path, atoi(line)); + if (path != NULL && line != NULL) { + addBreakpoint(path, atoi(line)); + } delete[] path; delete[] line; path = NULL; @@ -654,7 +729,9 @@ bool ScEngine::loadBreakpoints() { ////////////////////////////////////////////////////////////////////////// void ScEngine::addScriptTime(const char *filename, uint32 time) { - if (!_isProfiling) return; + if (!_isProfiling) { + return; + } AnsiString fileName = filename; fileName.toLowercase(); @@ -664,7 +741,9 @@ void ScEngine::addScriptTime(const char *filename, uint32 time) { ////////////////////////////////////////////////////////////////////////// void ScEngine::enableProfiling() { - if (_isProfiling) return; + if (_isProfiling) { + return; + } // destroy old data, if any _scriptTimes.clear(); @@ -676,7 +755,9 @@ void ScEngine::enableProfiling() { ////////////////////////////////////////////////////////////////////////// void ScEngine::disableProfiling() { - if (!_isProfiling) return; + if (!_isProfiling) { + return; + } dumpStats(); _isProfiling = false; diff --git a/engines/wintermute/base/scriptables/script_engine.h b/engines/wintermute/base/scriptables/script_engine.h index e443ec5832..fc441347df 100644 --- a/engines/wintermute/base/scriptables/script_engine.h +++ b/engines/wintermute/base/scriptables/script_engine.h @@ -48,13 +48,17 @@ public: CScCachedScript(const char *filename, byte *buffer, uint32 size) { _timestamp = g_system->getMillis(); _buffer = new byte[size]; - if (_buffer) memcpy(_buffer, buffer, size); + if (_buffer) { + memcpy(_buffer, buffer, size); + } _size = size; _filename = filename; }; ~CScCachedScript() { - if (_buffer) delete[] _buffer; + if (_buffer) { + delete[] _buffer; + } }; uint32 _timestamp; diff --git a/engines/wintermute/base/scriptables/script_ext_array.cpp b/engines/wintermute/base/scriptables/script_ext_array.cpp index 41059b2d80..0380103cd4 100644 --- a/engines/wintermute/base/scriptables/script_ext_array.cpp +++ b/engines/wintermute/base/scriptables/script_ext_array.cpp @@ -47,8 +47,9 @@ SXArray::SXArray(BaseGame *inGame, ScStack *stack): BaseScriptable(inGame) { int numParams = stack->pop()->getInt(0); - if (numParams == 1) _length = stack->pop()->getInt(0); - else if (numParams > 1) { + if (numParams == 1) { + _length = stack->pop()->getInt(0); + } else if (numParams > 1) { _length = numParams; char paramName[20]; for (int i = 0; i < numParams; i++) { @@ -86,7 +87,9 @@ const char *SXArray::scToString() { } } - if (i < _length - 1 && strlen(dummy) + 1 < 32768) strcat(dummy, ","); + if (i < _length - 1 && strlen(dummy) + 1 < 32768) { + strcat(dummy, ","); + } } return dummy; } @@ -124,12 +127,14 @@ bool SXArray::scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack, stack->push(_values->getProp(paramName)); _values->deleteProp(paramName); _length--; - } else stack->pushNULL(); + } else { + stack->pushNULL(); + } return STATUS_OK; + } else { + return STATUS_FAILED; } - - else return STATUS_FAILED; } @@ -160,7 +165,9 @@ ScValue *SXArray::scGetProperty(const char *name) { char ParamName[20]; if (validNumber(name, ParamName)) { return _values->getProp(ParamName); - } else return _scValue; + } else { + return _scValue; + } } } @@ -190,10 +197,14 @@ bool SXArray::scSetProperty(const char *name, ScValue *value) { else { char paramName[20]; if (validNumber(name, paramName)) { - int Index = atoi(paramName); - if (Index >= _length) _length = Index + 1; + int index = atoi(paramName); + if (index >= _length) { + _length = index + 1; + } return _values->setProp(paramName, value); - } else return STATUS_FAILED; + } else { + return STATUS_FAILED; + } } } @@ -223,7 +234,9 @@ bool SXArray::validNumber(const char *origStr, char *outStr) { int index = atoi(origStr); sprintf(outStr, "%d", index); return true; - } else return false; + } else { + return false; + } } ////////////////////////////////////////////////////////////////////////// diff --git a/engines/wintermute/base/scriptables/script_ext_array.h b/engines/wintermute/base/scriptables/script_ext_array.h index 8eb86c4e69..67a1104b46 100644 --- a/engines/wintermute/base/scriptables/script_ext_array.h +++ b/engines/wintermute/base/scriptables/script_ext_array.h @@ -35,7 +35,7 @@ namespace WinterMute { class SXArray : public BaseScriptable { public: - bool push(ScValue *Val); + bool push(ScValue *val); bool validNumber(const char *origStr, char *outStr); DECLARE_PERSISTENT(SXArray, BaseScriptable) SXArray(BaseGame *inGame, ScStack *stack); diff --git a/engines/wintermute/base/scriptables/script_ext_date.cpp b/engines/wintermute/base/scriptables/script_ext_date.cpp index d2fd3663c7..a3bb7e2183 100644 --- a/engines/wintermute/base/scriptables/script_ext_date.cpp +++ b/engines/wintermute/base/scriptables/script_ext_date.cpp @@ -199,10 +199,9 @@ bool SXDate::scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack, g_system->getTimeAndDate(_tm); stack->pushNULL(); return STATUS_OK; - } - - else + } else { return STATUS_FAILED; + } } @@ -216,9 +215,9 @@ ScValue *SXDate::scGetProperty(const char *name) { if (strcmp(name, "Type") == 0) { _scValue->setString("date"); return _scValue; + } else { + return _scValue; } - - else return _scValue; } diff --git a/engines/wintermute/base/scriptables/script_ext_file.cpp b/engines/wintermute/base/scriptables/script_ext_file.cpp index 7da1601bdc..437fbb64a2 100644 --- a/engines/wintermute/base/scriptables/script_ext_file.cpp +++ b/engines/wintermute/base/scriptables/script_ext_file.cpp @@ -51,10 +51,12 @@ BaseScriptable *makeSXFile(BaseGame *inGame, ScStack *stack) { ////////////////////////////////////////////////////////////////////////// SXFile::SXFile(BaseGame *inGame, ScStack *stack): BaseScriptable(inGame) { stack->correctParams(1); - ScValue *Val = stack->pop(); + ScValue *val = stack->pop(); _filename = NULL; - if (!Val->isNULL()) BaseUtils::setString(&_filename, Val->getString()); + if (!val->isNULL()) { + BaseUtils::setString(&_filename, val->getString()); + } _readFile = NULL; _writeFile = NULL; @@ -94,8 +96,11 @@ void SXFile::close() { ////////////////////////////////////////////////////////////////////////// const char *SXFile::scToString() { - if (_filename) return _filename; - else return "[file object]"; + if (_filename) { + return _filename; + } else { + return "[file object]"; + } } #define FILE_BUFFER_SIZE 32768 @@ -129,24 +134,37 @@ bool SXFile::scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack, if (!_readFile) { //script->runtimeError("File.%s: Error opening file '%s' for reading.", Name, _filename); close(); - } else _textMode = strcmp(name, "OpenAsText") == 0; + } else { + _textMode = strcmp(name, "OpenAsText") == 0; + } } else { if (strcmp(name, "OpenAsText") == 0) { - if (_mode == 2) _writeFile = openForWrite(_filename, false); - else _writeFile = openForAppend(_filename, false); + if (_mode == 2) { + _writeFile = openForWrite(_filename, false); + } else { + _writeFile = openForAppend(_filename, false); + } } else { - if (_mode == 2) _writeFile = openForWrite(_filename, true); - else _writeFile = openForAppend(_filename, true); + if (_mode == 2) { + _writeFile = openForWrite(_filename, true); + } else { + _writeFile = openForAppend(_filename, true); + } } if (!_writeFile) { //script->runtimeError("File.%s: Error opening file '%s' for writing.", Name, _filename); close(); - } else _textMode = strcmp(name, "OpenAsText") == 0; + } else { + _textMode = strcmp(name, "OpenAsText") == 0; + } } - if (_readFile || _writeFile) stack->pushBool(true); - else stack->pushBool(false); + if (_readFile || _writeFile) { + stack->pushBool(true); + } else { + stack->pushBool(false); + } return STATUS_OK; } @@ -221,7 +239,9 @@ bool SXFile::scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack, bool ret = STATUS_FAILED; do { ret = _readFile->read(&b, 1); - if (ret != 1) break; + if (ret != 1) { + break; + } if (counter > bufSize) { buf = (byte *)realloc(buf, bufSize + FILE_BUFFER_SIZE); @@ -231,8 +251,9 @@ bool SXFile::scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack, buf[counter] = '\0'; foundNewLine = true; break; - } else if (b == 0x0D) continue; - else { + } else if (b == 0x0D) { + continue; + } else { buf[counter] = b; counter++; } @@ -244,8 +265,11 @@ bool SXFile::scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack, } buf[counter] = '\0'; - if (!foundNewLine && counter == 0) stack->pushNULL(); - else stack->pushString((char *)buf); + if (!foundNewLine && counter == 0) { + stack->pushNULL(); + } else { + stack->pushString((char *)buf); + } free(buf); @@ -272,14 +296,17 @@ bool SXFile::scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack, bool ret = STATUS_FAILED; while (counter < (uint32)textLen) { ret = _readFile->read(&b, 1); - if (ret != 1) break; + if (ret != 1) { + break; + } if (counter > bufSize) { buf = (byte *)realloc(buf, bufSize + FILE_BUFFER_SIZE); bufSize += FILE_BUFFER_SIZE; } - if (b == 0x0D) continue; - else { + if (b == 0x0D) { + continue; + } else { buf[counter] = b; counter++; } @@ -291,8 +318,11 @@ bool SXFile::scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack, } buf[counter] = '\0'; - if (textLen > 0 && counter == 0) stack->pushNULL(); - else stack->pushString((char *)buf); + if (textLen > 0 && counter == 0) { + stack->pushNULL(); + } else { + stack->pushString((char *)buf); + } free(buf); @@ -335,8 +365,11 @@ bool SXFile::scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack, return STATUS_OK; } bool val; - if (_readFile->read(&val, sizeof(bool)) == sizeof(bool)) stack->pushBool(val); - else stack->pushNULL(); + if (_readFile->read(&val, sizeof(bool)) == sizeof(bool)) { + stack->pushBool(val); + } else { + stack->pushNULL(); + } return STATUS_OK; } @@ -355,8 +388,8 @@ bool SXFile::scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack, if (!_readFile->err()) { stack->pushInt(val); } else { - stack->pushNULL(); - } + stack->pushNULL(); + } return STATUS_OK; } @@ -375,7 +408,7 @@ bool SXFile::scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack, if (!_readFile->err()) { stack->pushInt(65536 + val); } else { - stack->pushNULL(); + stack->pushNULL(); } return STATUS_OK; } @@ -411,7 +444,7 @@ bool SXFile::scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack, return STATUS_OK; } float val; - (*(uint32*)&val) = _readFile->readUint32LE(); + (*(uint32 *)&val) = _readFile->readUint32LE(); if (!_readFile->err()) { stack->pushFloat(val); } else { @@ -433,8 +466,11 @@ bool SXFile::scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack, return STATUS_OK; } double val; - if (_readFile->read(&val, sizeof(double)) == sizeof(double)) stack->pushFloat(val); - else stack->pushNULL(); + if (_readFile->read(&val, sizeof(double)) == sizeof(double)) { + stack->pushFloat(val); + } else { + stack->pushNULL(); + } return STATUS_OK; } @@ -458,8 +494,12 @@ bool SXFile::scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack, stack->pushString((char *)str); } delete[] str; - } else stack->pushNULL(); - } else stack->pushNULL(); + } else { + stack->pushNULL(); + } + } else { + stack->pushNULL(); + } return STATUS_OK; } @@ -548,7 +588,7 @@ bool SXFile::scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack, stack->pushBool(false); return STATUS_OK; } - uint32 *ptr = (uint32*)&val; + uint32 *ptr = (uint32 *)&val; _writeFile->writeUint32LE(*ptr); stack->pushBool(true); @@ -594,10 +634,9 @@ bool SXFile::scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack, stack->pushBool(true); return STATUS_OK; + } else { + return BaseScriptable::scCallMethod(script, stack, thisStack, name); } - - - else return BaseScriptable::scCallMethod(script, stack, thisStack, name); } @@ -651,9 +690,9 @@ ScValue *SXFile::scGetProperty(const char *name) { else if (strcmp(name, "AccessMode") == 0) { _scValue->setInt(_mode); return _scValue; + } else { + return BaseScriptable::scGetProperty(name); } - - else return BaseScriptable::scGetProperty(name); } @@ -681,43 +720,46 @@ bool SXFile::scSetProperty(const char *name, ScValue *value) { ////////////////////////////////////////////////////////////////////////// uint32 SXFile::getPos() { - if (_mode == 1 && _readFile) + if (_mode == 1 && _readFile) { return _readFile->pos(); - else if ((_mode == 2 || _mode == 3) && _writeFile) { + } else if ((_mode == 2 || _mode == 3) && _writeFile) { error("SXFile - getPos for WriteFile not supported"); return 0; // return ftell((FILE *)_writeFile); - } else { + } else { return 0; } } ////////////////////////////////////////////////////////////////////////// bool SXFile::setPos(uint32 pos, int whence) { - if (_mode == 1 && _readFile) + if (_mode == 1 && _readFile) { return _readFile->seek(pos, whence); - else if ((_mode == 2 || _mode == 3) && _writeFile) { + } else if ((_mode == 2 || _mode == 3) && _writeFile) { error("SXFile - seeking in WriteFile not supported"); return false; // return fseek((FILE *)_writeFile, pos, (int)origin) == 0; + } else { + return false; } - else return false; } ////////////////////////////////////////////////////////////////////////// uint32 SXFile::getLength() { - if (_mode == 1 && _readFile) + if (_mode == 1 && _readFile) { return _readFile->size(); - else if ((_mode == 2 || _mode == 3) && _writeFile) { + } else if ((_mode == 2 || _mode == 3) && _writeFile) { error("SXFile - reading length for WriteFile not supported"); return 0; -/* - uint32 currentPos = ftell((FILE *)_writeFile); - fseek((FILE *)_writeFile, 0, SEEK_END); - int ret = ftell((FILE *)_writeFile); - fseek((FILE *)_writeFile, CurrentPos, SEEK_SET); - return Ret;*/ - } else return 0; + /* + uint32 currentPos = ftell((FILE *)_writeFile); + fseek((FILE *)_writeFile, 0, SEEK_END); + int ret = ftell((FILE *)_writeFile); + fseek((FILE *)_writeFile, CurrentPos, SEEK_SET); + return Ret;*/ + } else { + return 0; + } } ////////////////////////////////////////////////////////////////////////// @@ -744,24 +786,28 @@ bool SXFile::persist(BasePersistenceManager *persistMgr) { // open for reading if (_mode == 1) { _readFile = _gameRef->_fileManager->openFile(_filename); - if (!_readFile) + if (!_readFile) { close(); + } } // open for writing / appending else { if (_textMode) { - if (_mode == 2) + if (_mode == 2) { _writeFile = openForWrite(_filename, false); - else + } else { _writeFile = openForAppend(_filename, false); + } } else { - if (_mode == 2) + if (_mode == 2) { _writeFile = openForWrite(_filename, true); - else + } else { _writeFile = openForAppend(_filename, true); + } } - if (_writeFile) + if (_writeFile) { close(); + } } setPos(pos); } @@ -777,7 +823,7 @@ Common::WriteStream *SXFile::openForWrite(const Common::String &filename, bool b // Should replace fopen(..., "ab+") and fopen(..., "a+") Common::WriteStream *SXFile::openForAppend(const Common::String &filename, bool binary) { - error("SXFile::openForAppend - WriteFiles not supported"); + error("SXFile::openForAppend - WriteFiles not supported"); } } // end of namespace WinterMute diff --git a/engines/wintermute/base/scriptables/script_ext_math.cpp b/engines/wintermute/base/scriptables/script_ext_math.cpp index 1c37a15aa9..525b43434f 100644 --- a/engines/wintermute/base/scriptables/script_ext_math.cpp +++ b/engines/wintermute/base/scriptables/script_ext_math.cpp @@ -243,9 +243,9 @@ bool SXMath::scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack, stack->correctParams(1); stack->pushFloat(radianToDegree(stack->pop()->getFloat())); return STATUS_OK; + } else { + return STATUS_FAILED; } - - else return STATUS_FAILED; } @@ -267,9 +267,9 @@ ScValue *SXMath::scGetProperty(const char *name) { else if (strcmp(name, "PI") == 0) { _scValue->setFloat(M_PI); return _scValue; + } else { + return _scValue; } - - else return _scValue; } diff --git a/engines/wintermute/base/scriptables/script_ext_mem_buffer.cpp b/engines/wintermute/base/scriptables/script_ext_mem_buffer.cpp index 3d3f0b218b..e15af3446e 100644 --- a/engines/wintermute/base/scriptables/script_ext_mem_buffer.cpp +++ b/engines/wintermute/base/scriptables/script_ext_mem_buffer.cpp @@ -70,7 +70,9 @@ void *SXMemBuffer::scToMemBuffer() { ////////////////////////////////////////////////////////////////////////// void SXMemBuffer::cleanup() { - if (_size) free(_buffer); + if (_size) { + free(_buffer); + } _buffer = NULL; _size = 0; } @@ -81,14 +83,18 @@ bool SXMemBuffer::resize(int newSize) { if (_size == 0) { _buffer = malloc(newSize); - if (_buffer) _size = newSize; + if (_buffer) { + _size = newSize; + } } else { void *newBuf = realloc(_buffer, newSize); if (!newBuf) { if (newSize == 0) { _buffer = newBuf; _size = newSize; - } else return STATUS_FAILED; + } else { + return STATUS_FAILED; + } } else { _buffer = newBuf; _size = newSize; @@ -107,14 +113,16 @@ bool SXMemBuffer::checkBounds(ScScript *script, int start, int length) { script->runtimeError("Cannot use Set/Get methods on an uninitialized memory buffer"); return false; } - if (_size == 0) + if (_size == 0) { return true; + } if (start < 0 || length == 0 || start + length > _size) { script->runtimeError("Set/Get method call is out of bounds"); return false; - } else + } else { return true; + } } ////////////////////////////////////////////////////////////////////////// @@ -132,10 +140,11 @@ bool SXMemBuffer::scCallMethod(ScScript *script, ScStack *stack, ScStack *thisSt stack->correctParams(1); int newSize = stack->pop()->getInt(); newSize = MAX(0, newSize); - if (DID_SUCCEED(resize(newSize))) + if (DID_SUCCEED(resize(newSize))) { stack->pushBool(true); - else + } else { stack->pushBool(false); + } return STATUS_OK; } @@ -146,10 +155,11 @@ bool SXMemBuffer::scCallMethod(ScScript *script, ScStack *stack, ScStack *thisSt else if (strcmp(name, "GetBool") == 0) { stack->correctParams(1); int start = stack->pop()->getInt(); - if (!checkBounds(script, start, sizeof(bool))) + if (!checkBounds(script, start, sizeof(bool))) { stack->pushNULL(); - else + } else { stack->pushBool(*(bool *)((byte *)_buffer + start)); + } return STATUS_OK; } @@ -160,10 +170,11 @@ bool SXMemBuffer::scCallMethod(ScScript *script, ScStack *stack, ScStack *thisSt else if (strcmp(name, "GetByte") == 0) { stack->correctParams(1); int start = stack->pop()->getInt(); - if (!checkBounds(script, start, sizeof(byte))) + if (!checkBounds(script, start, sizeof(byte))) { stack->pushNULL(); - else + } else { stack->pushInt(*(byte *)((byte *)_buffer + start)); + } return STATUS_OK; } @@ -174,10 +185,11 @@ bool SXMemBuffer::scCallMethod(ScScript *script, ScStack *stack, ScStack *thisSt else if (strcmp(name, "GetShort") == 0) { stack->correctParams(1); int Start = stack->pop()->getInt(); - if (!checkBounds(script, Start, sizeof(short))) + if (!checkBounds(script, Start, sizeof(short))) { stack->pushNULL(); - else + } else { stack->pushInt(65536 + * (short *)((byte *)_buffer + Start)); + } return STATUS_OK; } @@ -188,10 +200,11 @@ bool SXMemBuffer::scCallMethod(ScScript *script, ScStack *stack, ScStack *thisSt else if (strcmp(name, "GetInt") == 0 || strcmp(name, "GetLong") == 0) { stack->correctParams(1); int start = stack->pop()->getInt(); - if (!checkBounds(script, start, sizeof(int))) + if (!checkBounds(script, start, sizeof(int))) { stack->pushNULL(); - else + } else { stack->pushInt(*(int *)((byte *)_buffer + start)); + } return STATUS_OK; } @@ -202,10 +215,11 @@ bool SXMemBuffer::scCallMethod(ScScript *script, ScStack *stack, ScStack *thisSt else if (strcmp(name, "GetFloat") == 0) { stack->correctParams(1); int start = stack->pop()->getInt(); - if (!checkBounds(script, start, sizeof(float))) + if (!checkBounds(script, start, sizeof(float))) { stack->pushNULL(); - else + } else { stack->pushFloat(*(float *)((byte *)_buffer + start)); + } return STATUS_OK; } @@ -216,10 +230,11 @@ bool SXMemBuffer::scCallMethod(ScScript *script, ScStack *stack, ScStack *thisSt else if (strcmp(name, "GetDouble") == 0) { stack->correctParams(1); int start = stack->pop()->getInt(); - if (!checkBounds(script, start, sizeof(double))) + if (!checkBounds(script, start, sizeof(double))) { stack->pushNULL(); - else + } else { stack->pushFloat(*(double *)((byte *)_buffer + start)); + } return STATUS_OK; } @@ -242,9 +257,9 @@ bool SXMemBuffer::scCallMethod(ScScript *script, ScStack *stack, ScStack *thisSt } } - if (!checkBounds(script, start, length)) + if (!checkBounds(script, start, length)) { stack->pushNULL(); - else { + } else { char *str = new char[length + 1]; strncpy(str, (const char *)_buffer + start, length); str[length] = '\0'; @@ -260,9 +275,9 @@ bool SXMemBuffer::scCallMethod(ScScript *script, ScStack *stack, ScStack *thisSt else if (strcmp(name, "GetPointer") == 0) { stack->correctParams(1); int start = stack->pop()->getInt(); - if (!checkBounds(script, start, sizeof(void *))) + if (!checkBounds(script, start, sizeof(void *))) { stack->pushNULL(); - else { + } else { void *pointer = *(void **)((byte *)_buffer + start); SXMemBuffer *buf = new SXMemBuffer(_gameRef, pointer); stack->pushNative(buf, false); @@ -278,9 +293,9 @@ bool SXMemBuffer::scCallMethod(ScScript *script, ScStack *stack, ScStack *thisSt int start = stack->pop()->getInt(); bool val = stack->pop()->getBool(); - if (!checkBounds(script, start, sizeof(bool))) + if (!checkBounds(script, start, sizeof(bool))) { stack->pushBool(false); - else { + } else { *(bool *)((byte *)_buffer + start) = val; stack->pushBool(true); } @@ -295,9 +310,9 @@ bool SXMemBuffer::scCallMethod(ScScript *script, ScStack *stack, ScStack *thisSt int start = stack->pop()->getInt(); byte val = (byte)stack->pop()->getInt(); - if (!checkBounds(script, start, sizeof(byte))) + if (!checkBounds(script, start, sizeof(byte))) { stack->pushBool(false); - else { + } else { *(byte *)((byte *)_buffer + start) = val; stack->pushBool(true); } @@ -312,9 +327,9 @@ bool SXMemBuffer::scCallMethod(ScScript *script, ScStack *stack, ScStack *thisSt int start = stack->pop()->getInt(); short val = (short)stack->pop()->getInt(); - if (!checkBounds(script, start, sizeof(short))) + if (!checkBounds(script, start, sizeof(short))) { stack->pushBool(false); - else { + } else { *(short *)((byte *)_buffer + start) = val; stack->pushBool(true); } @@ -329,9 +344,9 @@ bool SXMemBuffer::scCallMethod(ScScript *script, ScStack *stack, ScStack *thisSt int start = stack->pop()->getInt(); int val = stack->pop()->getInt(); - if (!checkBounds(script, start, sizeof(int))) + if (!checkBounds(script, start, sizeof(int))) { stack->pushBool(false); - else { + } else { *(int *)((byte *)_buffer + start) = val; stack->pushBool(true); } @@ -346,9 +361,9 @@ bool SXMemBuffer::scCallMethod(ScScript *script, ScStack *stack, ScStack *thisSt int start = stack->pop()->getInt(); float val = (float)stack->pop()->getFloat(); - if (!checkBounds(script, start, sizeof(float))) + if (!checkBounds(script, start, sizeof(float))) { stack->pushBool(false); - else { + } else { *(float *)((byte *)_buffer + start) = val; stack->pushBool(true); } @@ -363,9 +378,9 @@ bool SXMemBuffer::scCallMethod(ScScript *script, ScStack *stack, ScStack *thisSt int start = stack->pop()->getInt(); double val = stack->pop()->getFloat(); - if (!checkBounds(script, start, sizeof(double))) + if (!checkBounds(script, start, sizeof(double))) { stack->pushBool(false); - else { + } else { *(double *)((byte *)_buffer + start) = val; stack->pushBool(true); } @@ -380,9 +395,9 @@ bool SXMemBuffer::scCallMethod(ScScript *script, ScStack *stack, ScStack *thisSt int start = stack->pop()->getInt(); const char *val = stack->pop()->getString(); - if (!checkBounds(script, start, strlen(val) + 1)) + if (!checkBounds(script, start, strlen(val) + 1)) { stack->pushBool(false); - else { + } else { memcpy((byte *)_buffer + start, val, strlen(val) + 1); stack->pushBool(true); } @@ -395,11 +410,11 @@ bool SXMemBuffer::scCallMethod(ScScript *script, ScStack *stack, ScStack *thisSt else if (strcmp(name, "SetPointer") == 0) { stack->correctParams(2); int start = stack->pop()->getInt(); - /* ScValue *Val = */ stack->pop(); + /* ScValue *val = */ stack->pop(); - if (!checkBounds(script, start, sizeof(void *))) + if (!checkBounds(script, start, sizeof(void *))) { stack->pushBool(false); - else { + } else { /* int Pointer = (int)Val->getMemBuffer(); memcpy((byte *)_buffer+Start, &Pointer, sizeof(void*)); @@ -426,9 +441,9 @@ bool SXMemBuffer::scCallMethod(ScScript *script, ScStack *stack, ScStack *thisSt } stack->pushNULL(); return STATUS_OK; + } else { + return STATUS_FAILED; } - - else return STATUS_FAILED; } @@ -450,9 +465,9 @@ ScValue *SXMemBuffer::scGetProperty(const char *name) { if (strcmp(name, "Size") == 0) { _scValue->setInt(_size); return _scValue; + } else { + return BaseScriptable::scGetProperty(name); } - - else return BaseScriptable::scGetProperty(name); } @@ -487,12 +502,16 @@ bool SXMemBuffer::persist(BasePersistenceManager *persistMgr) { persistMgr->transfer(TMEMBER(_size)); if (persistMgr->getIsSaving()) { - if (_size > 0) persistMgr->putBytes((byte *)_buffer, _size); + if (_size > 0) { + persistMgr->putBytes((byte *)_buffer, _size); + } } else { if (_size > 0) { _buffer = malloc(_size); persistMgr->getBytes((byte *)_buffer, _size); - } else _buffer = NULL; + } else { + _buffer = NULL; + } } return STATUS_OK; @@ -501,8 +520,11 @@ bool SXMemBuffer::persist(BasePersistenceManager *persistMgr) { ////////////////////////////////////////////////////////////////////////// int SXMemBuffer::scCompare(BaseScriptable *val) { - if (_buffer == val->scToMemBuffer()) return 0; - else return 1; + if (_buffer == val->scToMemBuffer()) { + return 0; + } else { + return 1; + } } } // end of namespace WinterMute diff --git a/engines/wintermute/base/scriptables/script_ext_string.cpp b/engines/wintermute/base/scriptables/script_ext_string.cpp index 385d7ca746..1c7349bd8d 100644 --- a/engines/wintermute/base/scriptables/script_ext_string.cpp +++ b/engines/wintermute/base/scriptables/script_ext_string.cpp @@ -61,13 +61,17 @@ SXString::SXString(BaseGame *inGame, ScStack *stack): BaseScriptable(inGame) { setStringVal(val->getString()); } - if (_capacity == 0) setStringVal(""); + if (_capacity == 0) { + setStringVal(""); + } } ////////////////////////////////////////////////////////////////////////// SXString::~SXString() { - if (_string) delete[] _string; + if (_string) { + delete[] _string; + } } @@ -87,8 +91,11 @@ void SXString::setStringVal(const char *val) { ////////////////////////////////////////////////////////////////////////// const char *SXString::scToString() { - if (_string) return _string; - else return "[null string]"; + if (_string) { + return _string; + } else { + return "[null string]"; + } } @@ -108,22 +115,26 @@ bool SXString::scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack int start = stack->pop()->getInt(); int end = stack->pop()->getInt(); - if (end < start) BaseUtils::swap(&start, &end); + if (end < start) { + BaseUtils::swap(&start, &end); + } //try { WideString str; - if (_gameRef->_textEncoding == TEXT_UTF8) + if (_gameRef->_textEncoding == TEXT_UTF8) { str = StringUtil::utf8ToWide(_string); - else + } else { str = StringUtil::ansiToWide(_string); + } //WideString subStr = str.substr(start, end - start + 1); WideString subStr(str.c_str() + start, end - start + 1); - if (_gameRef->_textEncoding == TEXT_UTF8) + if (_gameRef->_textEncoding == TEXT_UTF8) { stack->pushString(StringUtil::wideToUtf8(subStr).c_str()); - else + } else { stack->pushString(StringUtil::wideToAnsi(subStr).c_str()); + } // } catch (std::exception &) { // stack->pushNULL(); // } @@ -146,22 +157,26 @@ bool SXString::scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack return STATUS_OK; } - if (val->isNULL()) len = strlen(_string) - start; + if (val->isNULL()) { + len = strlen(_string) - start; + } // try { WideString str; - if (_gameRef->_textEncoding == TEXT_UTF8) + if (_gameRef->_textEncoding == TEXT_UTF8) { str = StringUtil::utf8ToWide(_string); - else + } else { str = StringUtil::ansiToWide(_string); + } // WideString subStr = str.substr(start, len); WideString subStr(str.c_str() + start, len); - if (_gameRef->_textEncoding == TEXT_UTF8) + if (_gameRef->_textEncoding == TEXT_UTF8) { stack->pushString(StringUtil::wideToUtf8(subStr).c_str()); - else + } else { stack->pushString(StringUtil::wideToAnsi(subStr).c_str()); + } // } catch (std::exception &) { // stack->pushNULL(); // } @@ -176,17 +191,19 @@ bool SXString::scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack stack->correctParams(0); WideString str; - if (_gameRef->_textEncoding == TEXT_UTF8) + if (_gameRef->_textEncoding == TEXT_UTF8) { str = StringUtil::utf8ToWide(_string); - else + } else { str = StringUtil::ansiToWide(_string); + } str.toUppercase(); - if (_gameRef->_textEncoding == TEXT_UTF8) + if (_gameRef->_textEncoding == TEXT_UTF8) { stack->pushString(StringUtil::wideToUtf8(str).c_str()); - else + } else { stack->pushString(StringUtil::wideToAnsi(str).c_str()); + } return STATUS_OK; } @@ -198,17 +215,19 @@ bool SXString::scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack stack->correctParams(0); WideString str; - if (_gameRef->_textEncoding == TEXT_UTF8) + if (_gameRef->_textEncoding == TEXT_UTF8) { str = StringUtil::utf8ToWide(_string); - else + } else { str = StringUtil::ansiToWide(_string); + } str.toLowercase(); - if (_gameRef->_textEncoding == TEXT_UTF8) + if (_gameRef->_textEncoding == TEXT_UTF8) { stack->pushString(StringUtil::wideToUtf8(str).c_str()); - else + } else { stack->pushString(StringUtil::wideToAnsi(str).c_str()); + } return STATUS_OK; } @@ -223,16 +242,18 @@ bool SXString::scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack int index = stack->pop()->getInt(); WideString str; - if (_gameRef->_textEncoding == TEXT_UTF8) + if (_gameRef->_textEncoding == TEXT_UTF8) { str = StringUtil::utf8ToWide(_string); - else + } else { str = StringUtil::ansiToWide(_string); + } WideString toFind; - if (_gameRef->_textEncoding == TEXT_UTF8) + if (_gameRef->_textEncoding == TEXT_UTF8) { toFind = StringUtil::utf8ToWide(strToFind); - else + } else { toFind = StringUtil::ansiToWide(strToFind); + } int indexOf = StringUtil::indexOf(str, toFind, index); stack->pushInt(indexOf); @@ -247,7 +268,9 @@ bool SXString::scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack stack->correctParams(1); ScValue *val = stack->pop(); char separators[MAX_PATH_LENGTH] = ","; - if (!val->isNULL()) strcpy(separators, val->getString()); + if (!val->isNULL()) { + strcpy(separators, val->getString()); + } SXArray *array = new SXArray(_gameRef); if (!array) { @@ -257,16 +280,18 @@ bool SXString::scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack WideString str; - if (_gameRef->_textEncoding == TEXT_UTF8) + if (_gameRef->_textEncoding == TEXT_UTF8) { str = StringUtil::utf8ToWide(_string); - else + } else { str = StringUtil::ansiToWide(_string); + } WideString delims; - if (_gameRef->_textEncoding == TEXT_UTF8) + if (_gameRef->_textEncoding == TEXT_UTF8) { delims = StringUtil::utf8ToWide(separators); - else + } else { delims = StringUtil::ansiToWide(separators); + } Common::Array parts; @@ -298,10 +323,11 @@ bool SXString::scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack for (Common::Array::iterator it = parts.begin(); it != parts.end(); ++it) { WideString &part = (*it); - if (_gameRef->_textEncoding == TEXT_UTF8) + if (_gameRef->_textEncoding == TEXT_UTF8) { val = new ScValue(_gameRef, StringUtil::wideToUtf8(part).c_str()); - else + } else { val = new ScValue(_gameRef, StringUtil::wideToAnsi(part).c_str()); + } array->push(val); delete val; @@ -310,9 +336,9 @@ bool SXString::scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack stack->pushNative(array, false); return STATUS_OK; + } else { + return STATUS_FAILED; } - - else return STATUS_FAILED; } @@ -334,8 +360,9 @@ ScValue *SXString::scGetProperty(const char *name) { if (_gameRef->_textEncoding == TEXT_UTF8) { WideString wstr = StringUtil::utf8ToWide(_string); _scValue->setInt(wstr.size()); - } else + } else { _scValue->setInt(strlen(_string)); + } return _scValue; } @@ -345,9 +372,9 @@ ScValue *SXString::scGetProperty(const char *name) { else if (strcmp(name, "Capacity") == 0) { _scValue->setInt(_capacity); return _scValue; + } else { + return _scValue; } - - else return _scValue; } @@ -358,8 +385,9 @@ bool SXString::scSetProperty(const char *name, ScValue *value) { ////////////////////////////////////////////////////////////////////////// if (strcmp(name, "Capacity") == 0) { int32 newCap = (uint32)value->getInt(); - if (newCap < (int32)(strlen(_string) + 1)) _gameRef->LOG(0, "Warning: cannot lower string capacity"); - else if (newCap != _capacity) { + if (newCap < (int32)(strlen(_string) + 1)) { + _gameRef->LOG(0, "Warning: cannot lower string capacity"); + } else if (newCap != _capacity) { char *newStr = new char[newCap]; if (newStr) { memset(newStr, 0, newCap); @@ -370,9 +398,9 @@ bool SXString::scSetProperty(const char *name, ScValue *value) { } } return STATUS_OK; + } else { + return STATUS_FAILED; } - - else return STATUS_FAILED; } @@ -384,12 +412,16 @@ bool SXString::persist(BasePersistenceManager *persistMgr) { persistMgr->transfer(TMEMBER(_capacity)); if (persistMgr->getIsSaving()) { - if (_capacity > 0) persistMgr->putBytes((byte *)_string, _capacity); + if (_capacity > 0) { + persistMgr->putBytes((byte *)_string, _capacity); + } } else { if (_capacity > 0) { _string = new char[_capacity]; persistMgr->getBytes((byte *)_string, _capacity); - } else _string = NULL; + } else { + _string = NULL; + } } return STATUS_OK; diff --git a/engines/wintermute/base/scriptables/script_stack.cpp b/engines/wintermute/base/scriptables/script_stack.cpp index 74cc7a57ee..0d4ea54b8c 100644 --- a/engines/wintermute/base/scriptables/script_stack.cpp +++ b/engines/wintermute/base/scriptables/script_stack.cpp @@ -96,16 +96,22 @@ ScValue *ScStack::getPushValue() { ////////////////////////////////////////////////////////////////////////// ScValue *ScStack::getTop() { - if (_sP < 0 || _sP >= _values.getSize()) return NULL; - else return _values[_sP]; + if (_sP < 0 || _sP >= _values.getSize()) { + return NULL; + } else { + return _values[_sP]; + } } ////////////////////////////////////////////////////////////////////////// ScValue *ScStack::getAt(int index) { index = _sP - index; - if (index < 0 || index >= _values.getSize()) return NULL; - else return _values[index]; + if (index < 0 || index >= _values.getSize()) { + return NULL; + } else { + return _values[index]; + } } diff --git a/engines/wintermute/base/scriptables/script_value.cpp b/engines/wintermute/base/scriptables/script_value.cpp index 5e824cd10c..01cb4044ff 100644 --- a/engines/wintermute/base/scriptables/script_value.cpp +++ b/engines/wintermute/base/scriptables/script_value.cpp @@ -122,7 +122,9 @@ ScValue::ScValue(BaseGame *inGame, const char *val): BaseClass(inGame) { void ScValue::cleanup(bool ignoreNatives) { deleteProps(); - if (_valString) delete[] _valString; + if (_valString) { + delete[] _valString; + } if (!ignoreNatives) { if (_valNative && !_persistent) { @@ -157,7 +159,9 @@ ScValue::~ScValue() { ////////////////////////////////////////////////////////////////////////// ScValue *ScValue::getProp(const char *name) { - if (_type == VAL_VARIABLE_REF) return _valRef->getProp(name); + if (_type == VAL_VARIABLE_REF) { + return _valRef->getProp(name); + } if (_type == VAL_STRING && strcmp(name, "Length") == 0) { _gameRef->_scValue->_type = VAL_INT; @@ -178,18 +182,24 @@ ScValue *ScValue::getProp(const char *name) { ScValue *ret = NULL; - if (_type == VAL_NATIVE && _valNative) ret = _valNative->scGetProperty(name); + if (_type == VAL_NATIVE && _valNative) { + ret = _valNative->scGetProperty(name); + } if (ret == NULL) { _valIter = _valObject.find(name); - if (_valIter != _valObject.end()) ret = _valIter->_value; + if (_valIter != _valObject.end()) { + ret = _valIter->_value; + } } return ret; } ////////////////////////////////////////////////////////////////////////// bool ScValue::deleteProp(const char *name) { - if (_type == VAL_VARIABLE_REF) return _valRef->deleteProp(name); + if (_type == VAL_VARIABLE_REF) { + return _valRef->deleteProp(name); + } _valIter = _valObject.find(name); if (_valIter != _valObject.end()) { @@ -204,8 +214,9 @@ bool ScValue::deleteProp(const char *name) { ////////////////////////////////////////////////////////////////////////// bool ScValue::setProp(const char *name, ScValue *val, bool copyWhole, bool setAsConst) { - if (_type == VAL_VARIABLE_REF) + if (_type == VAL_VARIABLE_REF) { return _valRef->setProp(name, val); + } bool ret = STATUS_FAILED; if (_type == VAL_NATIVE && _valNative) { @@ -219,15 +230,19 @@ bool ScValue::setProp(const char *name, ScValue *val, bool copyWhole, bool setAs if (_valIter != _valObject.end()) { newVal = _valIter->_value; } - if (!newVal) + if (!newVal) { newVal = new ScValue(_gameRef); - else newVal->cleanup(); + } else { + newVal->cleanup(); + } newVal->copy(val, copyWhole); newVal->_isConstVar = setAsConst; _valObject[name] = newVal; - if (_type != VAL_NATIVE) _type = VAL_OBJECT; + if (_type != VAL_NATIVE) { + _type = VAL_OBJECT; + } /* _valIter = _valObject.find(Name); @@ -250,8 +265,9 @@ bool ScValue::setProp(const char *name, ScValue *val, bool copyWhole, bool setAs ////////////////////////////////////////////////////////////////////////// bool ScValue::propExists(const char *name) { - if (_type == VAL_VARIABLE_REF) + if (_type == VAL_VARIABLE_REF) { return _valRef->propExists(name); + } _valIter = _valObject.find(name); return (_valIter != _valObject.end()); @@ -273,15 +289,18 @@ void ScValue::deleteProps() { void ScValue::CleanProps(bool includingNatives) { _valIter = _valObject.begin(); while (_valIter != _valObject.end()) { - if (!_valIter->_value->_isConstVar && (!_valIter->_value->isNative() || includingNatives)) _valIter->_value->setNULL(); + if (!_valIter->_value->_isConstVar && (!_valIter->_value->isNative() || includingNatives)) { + _valIter->_value->setNULL(); + } _valIter++; } } ////////////////////////////////////////////////////////////////////////// bool ScValue::isNULL() { - if (_type == VAL_VARIABLE_REF) + if (_type == VAL_VARIABLE_REF) { return _valRef->isNULL(); + } return (_type == VAL_NULL); } @@ -289,8 +308,9 @@ bool ScValue::isNULL() { ////////////////////////////////////////////////////////////////////////// bool ScValue::isNative() { - if (_type == VAL_VARIABLE_REF) + if (_type == VAL_VARIABLE_REF) { return _valRef->isNative(); + } return (_type == VAL_NATIVE); } @@ -298,8 +318,9 @@ bool ScValue::isNative() { ////////////////////////////////////////////////////////////////////////// bool ScValue::isString() { - if (_type == VAL_VARIABLE_REF) + if (_type == VAL_VARIABLE_REF) { return _valRef->isString(); + } return (_type == VAL_STRING); } @@ -307,8 +328,9 @@ bool ScValue::isString() { ////////////////////////////////////////////////////////////////////////// bool ScValue::isFloat() { - if (_type == VAL_VARIABLE_REF) + if (_type == VAL_VARIABLE_REF) { return _valRef->isFloat(); + } return (_type == VAL_FLOAT); } @@ -316,8 +338,9 @@ bool ScValue::isFloat() { ////////////////////////////////////////////////////////////////////////// bool ScValue::isInt() { - if (_type == VAL_VARIABLE_REF) + if (_type == VAL_VARIABLE_REF) { return _valRef->isInt(); + } return (_type == VAL_INT); } @@ -325,8 +348,9 @@ bool ScValue::isInt() { ////////////////////////////////////////////////////////////////////////// bool ScValue::isBool() { - if (_type == VAL_VARIABLE_REF) + if (_type == VAL_VARIABLE_REF) { return _valRef->isBool(); + } return (_type == VAL_BOOL); } @@ -334,8 +358,9 @@ bool ScValue::isBool() { ////////////////////////////////////////////////////////////////////////// bool ScValue::isObject() { - if (_type == VAL_VARIABLE_REF) + if (_type == VAL_VARIABLE_REF) { return _valRef->isObject(); + } return (_type == VAL_OBJECT); } @@ -343,8 +368,9 @@ bool ScValue::isObject() { ////////////////////////////////////////////////////////////////////////// TValType ScValue::getTypeTolerant() { - if (_type == VAL_VARIABLE_REF) + if (_type == VAL_VARIABLE_REF) { return _valRef->getType(); + } return _type; } @@ -414,8 +440,11 @@ void ScValue::setString(const char *val) { } setStringVal(val); - if (_valString) _type = VAL_STRING; - else _type = VAL_NULL; + if (_valString) { + _type = VAL_STRING; + } else { + _type = VAL_NULL; + } } void ScValue::setString(const Common::String &val) { @@ -450,7 +479,9 @@ void ScValue::setNULL() { if (_valNative && !_persistent) { _valNative->_refCount--; - if (_valNative->_refCount <= 0) delete _valNative; + if (_valNative->_refCount <= 0) { + delete _valNative; + } } _valNative = NULL; deleteProps(); @@ -472,7 +503,9 @@ void ScValue::setNative(BaseScriptable *val, bool persistent) { if (_valNative && !_persistent) { _valNative->_refCount--; if (_valNative->_refCount <= 0) { - if (_valNative != val) delete _valNative; + if (_valNative != val) { + delete _valNative; + } _valNative = NULL; } } @@ -481,7 +514,9 @@ void ScValue::setNative(BaseScriptable *val, bool persistent) { _persistent = persistent; _valNative = val; - if (_valNative && !_persistent) _valNative->_refCount++; + if (_valNative && !_persistent) { + _valNative->_refCount++; + } } } @@ -507,8 +542,9 @@ void ScValue::setReference(ScValue *val) { ////////////////////////////////////////////////////////////////////////// bool ScValue::getBool(bool defaultVal) { - if (_type == VAL_VARIABLE_REF) + if (_type == VAL_VARIABLE_REF) { return _valRef->getBool(); + } switch (_type) { case VAL_BOOL: @@ -534,7 +570,9 @@ bool ScValue::getBool(bool defaultVal) { ////////////////////////////////////////////////////////////////////////// int ScValue::getInt(int defaultVal) { - if (_type == VAL_VARIABLE_REF) return _valRef->getInt(); + if (_type == VAL_VARIABLE_REF) { + return _valRef->getInt(); + } switch (_type) { case VAL_BOOL: @@ -560,8 +598,9 @@ int ScValue::getInt(int defaultVal) { ////////////////////////////////////////////////////////////////////////// double ScValue::getFloat(double defaultVal) { - if (_type == VAL_VARIABLE_REF) + if (_type == VAL_VARIABLE_REF) { return _valRef->getFloat(); + } switch (_type) { case VAL_BOOL: @@ -586,19 +625,23 @@ double ScValue::getFloat(double defaultVal) { ////////////////////////////////////////////////////////////////////////// void *ScValue::getMemBuffer() { - if (_type == VAL_VARIABLE_REF) + if (_type == VAL_VARIABLE_REF) { return _valRef->getMemBuffer(); + } - if (_type == VAL_NATIVE) + if (_type == VAL_NATIVE) { return _valNative->scToMemBuffer(); - else return (void *)NULL; + } else { + return (void *)NULL; + } } ////////////////////////////////////////////////////////////////////////// const char *ScValue::getString() { - if (_type == VAL_VARIABLE_REF) + if (_type == VAL_VARIABLE_REF) { return _valRef->getString(); + } switch (_type) { case VAL_OBJECT: @@ -647,11 +690,15 @@ const char *ScValue::getString() { ////////////////////////////////////////////////////////////////////////// BaseScriptable *ScValue::getNative() { - if (_type == VAL_VARIABLE_REF) + if (_type == VAL_VARIABLE_REF) { return _valRef->getNative(); + } - if (_type == VAL_NATIVE) return _valNative; - else return NULL; + if (_type == VAL_NATIVE) { + return _valNative; + } else { + return NULL; + } } @@ -668,12 +715,16 @@ void ScValue::copy(ScValue *orig, bool copyWhole) { if (_valNative && !_persistent) { _valNative->_refCount--; if (_valNative->_refCount <= 0) { - if (_valNative != orig->_valNative) delete _valNative; + if (_valNative != orig->_valNative) { + delete _valNative; + } _valNative = NULL; } } - if (orig->_type == VAL_VARIABLE_REF && orig->_valRef && copyWhole) orig = orig->_valRef; + if (orig->_type == VAL_VARIABLE_REF && orig->_valRef && copyWhole) { + orig = orig->_valRef; + } cleanup(true); @@ -687,7 +738,9 @@ void ScValue::copy(ScValue *orig, bool copyWhole) { _persistent = orig->_persistent; _valNative = orig->_valNative; - if (_valNative && !_persistent) _valNative->_refCount++; + if (_valNative && !_persistent) { + _valNative->_refCount++; + } //!!!! ref->native++ // copy properties @@ -698,7 +751,9 @@ void ScValue::copy(ScValue *orig, bool copyWhole) { _valObject[orig->_valIter->_key]->copy(orig->_valIter->_value); orig->_valIter++; } - } else _valObject.clear(); + } else { + _valObject.clear(); + } } @@ -730,7 +785,9 @@ void ScValue::setValue(ScValue *val) { } } // otherwise just copy everything - else copy(val); + else { + copy(val); + } } @@ -841,39 +898,60 @@ int ScValue::compare(ScValue *val1, ScValue *val2) { // same class? if (strcmp(val1->getNative()->getClassName(), val2->getNative()->getClassName()) == 0) { return val1->getNative()->scCompare(val2->getNative()); - } else return strcmp(val1->getString(), val2->getString()); + } else { + return strcmp(val1->getString(), val2->getString()); + } } // both objects? - if (val1->isObject() && val2->isObject()) return -1; + if (val1->isObject() && val2->isObject()) { + return -1; + } // null states - if (val1->isNULL() && !val2->isNULL()) return -1; - else if (!val1->isNULL() && val2->isNULL()) return 1; - else if (val1->isNULL() && val2->isNULL()) return 0; + if (val1->isNULL() && !val2->isNULL()) { + return -1; + } else if (!val1->isNULL() && val2->isNULL()) { + return 1; + } else if (val1->isNULL() && val2->isNULL()) { + return 0; + } // one of them is string? convert both to string - if (val1->isString() || val2->isString()) return strcmp(val1->getString(), val2->getString()); + if (val1->isString() || val2->isString()) { + return strcmp(val1->getString(), val2->getString()); + } // one of them is float? if (val1->isFloat() || val2->isFloat()) { - if (val1->getFloat() < val2->getFloat()) return -1; - else if (val1->getFloat() > val2->getFloat()) return 1; - else return 0; + if (val1->getFloat() < val2->getFloat()) { + return -1; + } else if (val1->getFloat() > val2->getFloat()) { + return 1; + } else { + return 0; + } } // otherwise compare as int's - if (val1->getInt() < val2->getInt()) return -1; - else if (val1->getInt() > val2->getInt()) return 1; - else return 0; + if (val1->getInt() < val2->getInt()) { + return -1; + } else if (val1->getInt() > val2->getInt()) { + return 1; + } else { + return 0; + } } ////////////////////////////////////////////////////////////////////////// int ScValue::compareStrict(ScValue *val1, ScValue *val2) { - if (val1->getTypeTolerant() != val2->getTypeTolerant()) return -1; - else return ScValue::compare(val1, val2); + if (val1->getTypeTolerant() != val2->getTypeTolerant()) { + return -1; + } else { + return ScValue::compare(val1, val2); + } } @@ -1013,16 +1091,18 @@ bool ScValue::dbgSetVal() { ////////////////////////////////////////////////////////////////////////// int ScValue::dbgGetNumProperties() { - if (_valNative && _valNative->_scProp) + if (_valNative && _valNative->_scProp) { return _valNative->_scProp->dbgGetNumProperties(); - else return _valObject.size(); + } else { + return _valObject.size(); + } } ////////////////////////////////////////////////////////////////////////// bool ScValue::dbgGetProperty(int index, const char **name, IWmeDebugProp **value) { - if (_valNative && _valNative->_scProp) + if (_valNative && _valNative->_scProp) { return _valNative->_scProp->dbgGetProperty(index, name, value); - else { + } else { int count = 0; _valIter = _valObject.begin(); while (_valIter != _valObject.end()) { @@ -1040,8 +1120,9 @@ bool ScValue::dbgGetProperty(int index, const char **name, IWmeDebugProp **value ////////////////////////////////////////////////////////////////////////// bool ScValue::dbgGetDescription(char *buf, int bufSize) { - if (_type == VAL_VARIABLE_REF) + if (_type == VAL_VARIABLE_REF) { return _valRef->dbgGetDescription(buf, bufSize); + } if (_type == VAL_NATIVE) { _valNative->scDebuggerDesc(buf, bufSize); -- cgit v1.2.3 From 4eda234611bd77f053defe9e61d592b308270eaa Mon Sep 17 00:00:00 2001 From: Einar Johan Trøan Sømåen Date: Thu, 26 Jul 2012 19:41:18 +0200 Subject: WINTERMUTE: Replace BaseArray with a templated subclass of Common::Array. This needs additional cleanup, but compiles and runs at this point. --- engines/wintermute/base/scriptables/script.h | 2 +- engines/wintermute/base/scriptables/script_engine.cpp | 16 ++++++++-------- engines/wintermute/base/scriptables/script_engine.h | 8 ++++---- engines/wintermute/base/scriptables/script_stack.cpp | 8 ++++---- engines/wintermute/base/scriptables/script_stack.h | 2 +- 5 files changed, 18 insertions(+), 18 deletions(-) (limited to 'engines/wintermute/base/scriptables') diff --git a/engines/wintermute/base/scriptables/script.h b/engines/wintermute/base/scriptables/script.h index ba73e1015f..c343ad24ad 100644 --- a/engines/wintermute/base/scriptables/script.h +++ b/engines/wintermute/base/scriptables/script.h @@ -46,7 +46,7 @@ public: bool dbgSendScript(IWmeDebugClient *client); bool dbgSendVariables(IWmeDebugClient *client); - BaseArray _breakpoints; + BaseArray _breakpoints; bool _tracingMode; ScScript *_parentScript; diff --git a/engines/wintermute/base/scriptables/script_engine.cpp b/engines/wintermute/base/scriptables/script_engine.cpp index 356617094d..e1aed7ed82 100644 --- a/engines/wintermute/base/scriptables/script_engine.cpp +++ b/engines/wintermute/base/scriptables/script_engine.cpp @@ -97,7 +97,7 @@ ScEngine::~ScEngine() { delete _breakpoints[i]; _breakpoints[i] = NULL; } - _breakpoints.removeAll(); + _breakpoints.clear(); } @@ -108,11 +108,11 @@ bool ScEngine::cleanup() { _scripts[i]->_owner->removeScript(_scripts[i]); } delete _scripts[i]; - _scripts.removeAt(i); + _scripts.remove_at(i); i--; } - _scripts.removeAll(); + _scripts.clear(); delete _globals; _globals = NULL; @@ -404,7 +404,7 @@ bool ScEngine::removeFinishedScripts() { } _gameRef->getDebugMgr()->onScriptShutdown(_scripts[i]); delete _scripts[i]; - _scripts.removeAt(i); + _scripts.remove_at(i); i--; } } @@ -511,7 +511,7 @@ void ScEngine::editorCleanup() { for (int i = 0; i < _scripts.getSize(); i++) { if (_scripts[i]->_owner == NULL && (_scripts[i]->_state == SCRIPT_FINISHED || _scripts[i]->_state == SCRIPT_ERROR)) { delete _scripts[i]; - _scripts.removeAt(i); + _scripts.remove_at(i); i--; } } @@ -620,10 +620,10 @@ bool ScEngine::removeBreakpoint(const char *scriptFilename, int line) { if (scumm_stricmp(_breakpoints[i]->_filename.c_str(), scriptFilename) == 0) { for (int j = 0; j < _breakpoints[i]->_lines.getSize(); j++) { if (_breakpoints[i]->_lines[j] == line) { - _breakpoints[i]->_lines.removeAt(j); + _breakpoints[i]->_lines.remove_at(j); if (_breakpoints[i]->_lines.getSize() == 0) { delete _breakpoints[i]; - _breakpoints.removeAt(i); + _breakpoints.remove_at(i); } // refresh changes refreshScriptBreakpoints(); @@ -666,7 +666,7 @@ bool ScEngine::refreshScriptBreakpoints(ScScript *script) { } } if (script->_breakpoints.getSize() > 0) { - script->_breakpoints.removeAll(); + script->_breakpoints.clear(); } return STATUS_OK; diff --git a/engines/wintermute/base/scriptables/script_engine.h b/engines/wintermute/base/scriptables/script_engine.h index fc441347df..fcfaa51971 100644 --- a/engines/wintermute/base/scriptables/script_engine.h +++ b/engines/wintermute/base/scriptables/script_engine.h @@ -74,11 +74,11 @@ public: } ~CScBreakpoint() { - _lines.removeAll(); + _lines.clear(); } Common::String _filename; - BaseArray _lines; + BaseArray _lines; }; @@ -87,7 +87,7 @@ public: public: bool dbgSendScripts(IWmeDebugClient *client); - BaseArray _breakpoints; + BaseArray _breakpoints; bool addBreakpoint(const char *scriptFilename, int line); bool removeBreakpoint(const char *scriptFilename, int line); bool refreshScriptBreakpoints(); @@ -122,7 +122,7 @@ public: static void closeFile(void *data, byte *buffer); static void parseElement(void *data, int line, int type, void *elementData); - BaseArray _scripts; + BaseArray _scripts; void enableProfiling(); void disableProfiling(); diff --git a/engines/wintermute/base/scriptables/script_stack.cpp b/engines/wintermute/base/scriptables/script_stack.cpp index 0d4ea54b8c..8840a2c0f1 100644 --- a/engines/wintermute/base/scriptables/script_stack.cpp +++ b/engines/wintermute/base/scriptables/script_stack.cpp @@ -50,7 +50,7 @@ ScStack::~ScStack() { for (int i = 0; i < _values.getSize(); i++) { delete _values[i]; } - _values.removeAll(); + _values.clear(); } @@ -123,7 +123,7 @@ void ScStack::correctParams(uint32 expectedParams) { while (expectedParams < nuParams) { //Pop(); delete _values[_sP - expectedParams]; - _values.removeAt(_sP - expectedParams); + _values.remove_at(_sP - expectedParams); nuParams--; _sP--; } @@ -132,13 +132,13 @@ void ScStack::correctParams(uint32 expectedParams) { //Push(null_val); ScValue *nullVal = new ScValue(_gameRef); nullVal->setNULL(); - _values.insertAt(_sP - nuParams + 1, nullVal); + _values.insert_at(_sP - nuParams + 1, nullVal); nuParams++; _sP++; if (_values.getSize() > _sP + 1) { delete _values[_values.getSize() - 1]; - _values.removeAt(_values.getSize() - 1); + _values.remove_at(_values.getSize() - 1); } } } diff --git a/engines/wintermute/base/scriptables/script_stack.h b/engines/wintermute/base/scriptables/script_stack.h index 3aacad0765..95839cc680 100644 --- a/engines/wintermute/base/scriptables/script_stack.h +++ b/engines/wintermute/base/scriptables/script_stack.h @@ -56,7 +56,7 @@ public: ScValue *pop(); ScStack(BaseGame *inGame); virtual ~ScStack(); - BaseArray _values; + BaseArray _values; int _sP; }; -- cgit v1.2.3 From 2c530bc6edc9ffd95fa86488a663e67d2735041f Mon Sep 17 00:00:00 2001 From: Einar Johan Trøan Sømåen Date: Thu, 26 Jul 2012 20:49:59 +0200 Subject: WINTERMUTE: var_name -> varName --- engines/wintermute/base/scriptables/script.cpp | 24 ++++++++++++------------ engines/wintermute/base/scriptables/script.h | 12 ++++++------ 2 files changed, 18 insertions(+), 18 deletions(-) (limited to 'engines/wintermute/base/scriptables') diff --git a/engines/wintermute/base/scriptables/script.cpp b/engines/wintermute/base/scriptables/script.cpp index 4deeb0bf39..54a04c454f 100644 --- a/engines/wintermute/base/scriptables/script.cpp +++ b/engines/wintermute/base/scriptables/script.cpp @@ -106,12 +106,12 @@ void ScScript::readHeader() { _scriptStream->seek(0); _header.magic = _scriptStream->readUint32LE(); _header.version = _scriptStream->readUint32LE(); - _header.code_start = _scriptStream->readUint32LE(); - _header.func_table = _scriptStream->readUint32LE(); - _header.symbol_table = _scriptStream->readUint32LE(); - _header.event_table = _scriptStream->readUint32LE(); - _header.externals_table = _scriptStream->readUint32LE(); - _header.method_table = _scriptStream->readUint32LE(); + _header.codeStart = _scriptStream->readUint32LE(); + _header.funcTable = _scriptStream->readUint32LE(); + _header.symbolTable = _scriptStream->readUint32LE(); + _header.eventTable = _scriptStream->readUint32LE(); + _header.externalsTable = _scriptStream->readUint32LE(); + _header.methodTable = _scriptStream->readUint32LE(); _scriptStream->seek(oldPos); } @@ -148,7 +148,7 @@ bool ScScript::initScript() { // skip to the beginning - _iP = _header.code_start; + _iP = _header.codeStart; _scriptStream->seek(_iP); _currentLine = 0; @@ -169,7 +169,7 @@ bool ScScript::initTables() { readHeader(); // load symbol table - _iP = _header.symbol_table; + _iP = _header.symbolTable; _numSymbols = getDWORD(); _symbols = new char*[_numSymbols]; @@ -179,7 +179,7 @@ bool ScScript::initTables() { } // load functions table - _iP = _header.func_table; + _iP = _header.funcTable; _numFunctions = getDWORD(); _functions = new TFunctionPos[_numFunctions]; @@ -190,7 +190,7 @@ bool ScScript::initTables() { // load events table - _iP = _header.event_table; + _iP = _header.eventTable; _numEvents = getDWORD(); _events = new TEventPos[_numEvents]; @@ -202,7 +202,7 @@ bool ScScript::initTables() { // load externals if (_header.version >= 0x0101) { - _iP = _header.externals_table; + _iP = _header.externalsTable; _numExternals = getDWORD(); _externals = new TExternalFunction[_numExternals]; @@ -222,7 +222,7 @@ bool ScScript::initTables() { } // load method table - _iP = _header.method_table; + _iP = _header.methodTable; _numMethods = getDWORD(); _methods = new TMethodPos[_numMethods]; diff --git a/engines/wintermute/base/scriptables/script.h b/engines/wintermute/base/scriptables/script.h index c343ad24ad..2ee1319acb 100644 --- a/engines/wintermute/base/scriptables/script.h +++ b/engines/wintermute/base/scriptables/script.h @@ -88,12 +88,12 @@ public: typedef struct { uint32 magic; uint32 version; - uint32 code_start; - uint32 func_table; - uint32 symbol_table; - uint32 event_table; - uint32 externals_table; - uint32 method_table; + uint32 codeStart; + uint32 funcTable; + uint32 symbolTable; + uint32 eventTable; + uint32 externalsTable; + uint32 methodTable; } TScriptHeader; TScriptHeader _header; -- cgit v1.2.3 From 3a49f2bad407787ef65d04c5f9ae423485629b41 Mon Sep 17 00:00:00 2001 From: Einar Johan Trøan Sømåen Date: Thu, 26 Jul 2012 22:20:55 +0200 Subject: WINTERMUTE: More variable/function renaming VarName->varName --- engines/wintermute/base/scriptables/script.cpp | 54 +++++++++++----------- .../wintermute/base/scriptables/script_engine.cpp | 18 ++++---- .../base/scriptables/script_ext_array.cpp | 18 ++++---- .../base/scriptables/script_ext_file.cpp | 8 ++-- .../wintermute/base/scriptables/script_ext_file.h | 2 +- .../base/scriptables/script_ext_mem_buffer.cpp | 20 ++++---- 6 files changed, 60 insertions(+), 60 deletions(-) (limited to 'engines/wintermute/base/scriptables') diff --git a/engines/wintermute/base/scriptables/script.cpp b/engines/wintermute/base/scriptables/script.cpp index 54a04c454f..75c4d38574 100644 --- a/engines/wintermute/base/scriptables/script.cpp +++ b/engines/wintermute/base/scriptables/script.cpp @@ -165,7 +165,7 @@ bool ScScript::initScript() { ////////////////////////////////////////////////////////////////////////// bool ScScript::initTables() { - uint32 OrigIP = _iP; + uint32 origIP = _iP; readHeader(); // load symbol table @@ -232,7 +232,7 @@ bool ScScript::initTables() { } - _iP = OrigIP; + _iP = origIP; return STATUS_OK; } @@ -546,7 +546,7 @@ bool ScScript::executeInstruction() { case II_DEF_GLOB_VAR: case II_DEF_CONST_VAR: { dw = getDWORD(); - /* char *Temp = _symbols[dw]; // TODO delete */ + /* char *temp = _symbols[dw]; // TODO delete */ // only create global var if it doesn't exist if (!_engine->_globals->propExists(_symbols[dw])) { _operand->setNULL(); @@ -604,8 +604,8 @@ bool ScScript::executeInstruction() { // push var // push string str = _stack->pop()->getString(); - char *MethodName = new char[strlen(str) + 1]; - strcpy(MethodName, str); + char *methodName = new char[strlen(str) + 1]; + strcpy(methodName, str); ScValue *var = _stack->pop(); if (var->_type == VAL_VARIABLE_REF) { @@ -613,21 +613,21 @@ bool ScScript::executeInstruction() { } bool res = STATUS_FAILED; - bool TriedNative = false; + bool triedNative = false; // we are already calling this method, try native - if (_thread && _methodThread && strcmp(MethodName, _threadEvent) == 0 && var->_type == VAL_NATIVE && _owner == var->getNative()) { - TriedNative = true; - res = var->_valNative->scCallMethod(this, _stack, _thisStack, MethodName); + if (_thread && _methodThread && strcmp(methodName, _threadEvent) == 0 && var->_type == VAL_NATIVE && _owner == var->getNative()) { + triedNative = true; + res = var->_valNative->scCallMethod(this, _stack, _thisStack, methodName); } if (DID_FAIL(res)) { - if (var->isNative() && var->getNative()->canHandleMethod(MethodName)) { + if (var->isNative() && var->getNative()->canHandleMethod(methodName)) { if (!_unbreakable) { - _waitScript = var->getNative()->invokeMethodThread(MethodName); + _waitScript = var->getNative()->invokeMethodThread(methodName); if (!_waitScript) { _stack->correctParams(0); - runtimeError("Error invoking method '%s'.", MethodName); + runtimeError("Error invoking method '%s'.", methodName); _stack->pushNULL(); } else { _state = SCRIPT_WAITING_SCRIPT; @@ -636,10 +636,10 @@ bool ScScript::executeInstruction() { } else { // can call methods in unbreakable mode _stack->correctParams(0); - runtimeError("Cannot call method '%s'. Ignored.", MethodName); + runtimeError("Cannot call method '%s'. Ignored.", methodName); _stack->pushNULL(); } - delete[] MethodName; + delete[] methodName; break; } /* @@ -665,29 +665,29 @@ bool ScScript::executeInstruction() { */ else { res = STATUS_FAILED; - if (var->_type == VAL_NATIVE && !TriedNative) { - res = var->_valNative->scCallMethod(this, _stack, _thisStack, MethodName); + if (var->_type == VAL_NATIVE && !triedNative) { + res = var->_valNative->scCallMethod(this, _stack, _thisStack, methodName); } if (DID_FAIL(res)) { _stack->correctParams(0); - runtimeError("Call to undefined method '%s'. Ignored.", MethodName); + runtimeError("Call to undefined method '%s'. Ignored.", methodName); _stack->pushNULL(); } } } - delete[] MethodName; + delete[] methodName; } break; case II_EXTERNAL_CALL: { - uint32 SymbolIndex = getDWORD(); + uint32 symbolIndex = getDWORD(); - TExternalFunction *f = getExternal(_symbols[SymbolIndex]); + TExternalFunction *f = getExternal(_symbols[symbolIndex]); if (f) { externalCall(_stack, _thisStack, f); } else { - _gameRef->ExternalCall(this, _stack, _thisStack, _symbols[SymbolIndex]); + _gameRef->externalCall(this, _stack, _thisStack, _symbols[symbolIndex]); } break; @@ -737,8 +737,8 @@ bool ScScript::executeInstruction() { } case II_POP_VAR: { - char *VarName = _symbols[getDWORD()]; - ScValue *var = getVar(VarName); + char *varName = _symbols[getDWORD()]; + ScValue *var = getVar(varName); if (var) { ScValue *val = _stack->pop(); if (!val) { @@ -1460,13 +1460,13 @@ bool ScScript::externalCall(ScStack *stack, ScStack *thisStack, ScScript::TExter ////////////////////////////////////////////////////////////////////////// bool ScScript::copyParameters(ScStack *stack) { int i; - int NumParams = stack->pop()->getInt(); - for (i = NumParams - 1; i >= 0; i--) { + int numParams = stack->pop()->getInt(); + for (i = numParams - 1; i >= 0; i--) { _stack->push(stack->getAt(i)); } - _stack->pushInt(NumParams); + _stack->pushInt(numParams); - for (i = 0; i < NumParams; i++) { + for (i = 0; i < numParams; i++) { stack->pop(); } diff --git a/engines/wintermute/base/scriptables/script_engine.cpp b/engines/wintermute/base/scriptables/script_engine.cpp index e1aed7ed82..bc8d54ef58 100644 --- a/engines/wintermute/base/scriptables/script_engine.cpp +++ b/engines/wintermute/base/scriptables/script_engine.cpp @@ -227,13 +227,13 @@ byte *ScEngine::getCompiledScript(const char *filename, uint32 *outSize, bool ig CScCachedScript *cachedScript = new CScCachedScript(filename, compBuffer, compSize); if (cachedScript) { int index = 0; - uint32 MinTime = g_system->getMillis(); + uint32 minTime = g_system->getMillis(); for (int i = 0; i < MAX_CACHED_SCRIPTS; i++) { if (_cachedScripts[i] == NULL) { index = i; break; - } else if (_cachedScripts[i]->_timestamp <= MinTime) { - MinTime = _cachedScripts[i]->_timestamp; + } else if (_cachedScripts[i]->_timestamp <= minTime) { + minTime = _cachedScripts[i]->_timestamp; index = i; } } @@ -338,13 +338,13 @@ bool ScEngine::tick() { // time sliced script if (_scripts[i]->_timeSlice > 0) { - uint32 StartTime = g_system->getMillis(); - while (_scripts[i]->_state == SCRIPT_RUNNING && g_system->getMillis() - StartTime < _scripts[i]->_timeSlice) { + uint32 startTime = g_system->getMillis(); + while (_scripts[i]->_state == SCRIPT_RUNNING && g_system->getMillis() - startTime < _scripts[i]->_timeSlice) { _currentScript = _scripts[i]; _scripts[i]->executeInstruction(); } if (_isProfiling && _scripts[i]->_filename) { - addScriptTime(_scripts[i]->_filename, g_system->getMillis() - StartTime); + addScriptTime(_scripts[i]->_filename, g_system->getMillis() - startTime); } } @@ -473,8 +473,8 @@ bool ScEngine::resetObject(BaseObject *Object) { resetScript(_scripts[i]); } - bool IsThread = _scripts[i]->_methodThread || _scripts[i]->_thread; - _scripts[i]->finish(!IsThread); // 1.9b1 - top-level script kills its threads as well + bool isThread = _scripts[i]->_methodThread || _scripts[i]->_thread; + _scripts[i]->finish(!isThread); // 1.9b1 - top-level script kills its threads as well } } return STATUS_OK; @@ -707,7 +707,7 @@ bool ScEngine::loadBreakpoints() { int count = _gameRef->_registry->readInt("Debug", "NumBreakpoints", 0); for (int i = 1; i <= count; i++) { - /* uint32 BufSize = 512; */ + /* uint32 bufSize = 512; */ sprintf(key, "Breakpoint%d", i); AnsiString breakpoint = _gameRef->_registry->readString("Debug", key, ""); diff --git a/engines/wintermute/base/scriptables/script_ext_array.cpp b/engines/wintermute/base/scriptables/script_ext_array.cpp index 0380103cd4..cc3bec89c0 100644 --- a/engines/wintermute/base/scriptables/script_ext_array.cpp +++ b/engines/wintermute/base/scriptables/script_ext_array.cpp @@ -162,9 +162,9 @@ ScValue *SXArray::scGetProperty(const char *name) { // [number] ////////////////////////////////////////////////////////////////////////// else { - char ParamName[20]; - if (validNumber(name, ParamName)) { - return _values->getProp(ParamName); + char paramName[20]; + if (validNumber(name, paramName)) { + return _values->getProp(paramName); } else { return _scValue; } @@ -178,14 +178,14 @@ bool SXArray::scSetProperty(const char *name, ScValue *value) { // Length ////////////////////////////////////////////////////////////////////////// if (strcmp(name, "Length") == 0) { - int OrigLength = _length; + int origLength = _length; _length = MAX(value->getInt(0), 0); - char PropName[20]; - if (_length < OrigLength) { - for (int i = _length; i < OrigLength; i++) { - sprintf(PropName, "%d", i); - _values->deleteProp(PropName); + char propName[20]; + if (_length < origLength) { + for (int i = _length; i < origLength; i++) { + sprintf(propName, "%d", i); + _values->deleteProp(propName); } } return STATUS_OK; diff --git a/engines/wintermute/base/scriptables/script_ext_file.cpp b/engines/wintermute/base/scriptables/script_ext_file.cpp index 437fbb64a2..4eeabca04d 100644 --- a/engines/wintermute/base/scriptables/script_ext_file.cpp +++ b/engines/wintermute/base/scriptables/script_ext_file.cpp @@ -188,8 +188,8 @@ bool SXFile::scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack, script->runtimeError("File.%s: File is not open", name); stack->pushBool(false); } else { - int Pos = stack->pop()->getInt(); - stack->pushBool(setPos(Pos)); + int pos = stack->pop()->getInt(); + stack->pushBool(setPos(pos)); } return STATUS_OK; } @@ -703,10 +703,10 @@ bool SXFile::scSetProperty(const char *name, ScValue *value) { // Length ////////////////////////////////////////////////////////////////////////// if (strcmp(name, "Length")==0){ - int OrigLength = _length; + int origLength = _length; _length = max(value->getInt(0), 0); - char PropName[20]; + char propName[20]; if (_length < OrigLength){ for(int i=_length; ipop()->getInt(); - resize(MAX(0, NewSize)); + int newSize = stack->pop()->getInt(); + resize(MAX(0, newSize)); } ////////////////////////////////////////////////////////////////////////// -SXMemBuffer::SXMemBuffer(BaseGame *inGame, void *Buffer): BaseScriptable(inGame) { +SXMemBuffer::SXMemBuffer(BaseGame *inGame, void *buffer): BaseScriptable(inGame) { _size = 0; - _buffer = Buffer; + _buffer = buffer; } @@ -184,11 +184,11 @@ bool SXMemBuffer::scCallMethod(ScScript *script, ScStack *stack, ScStack *thisSt ////////////////////////////////////////////////////////////////////////// else if (strcmp(name, "GetShort") == 0) { stack->correctParams(1); - int Start = stack->pop()->getInt(); - if (!checkBounds(script, Start, sizeof(short))) { + int start = stack->pop()->getInt(); + if (!checkBounds(script, start, sizeof(short))) { stack->pushNULL(); } else { - stack->pushInt(65536 + * (short *)((byte *)_buffer + Start)); + stack->pushInt(65536 + * (short *)((byte *)_buffer + start)); } return STATUS_OK; @@ -416,7 +416,7 @@ bool SXMemBuffer::scCallMethod(ScScript *script, ScStack *stack, ScStack *thisSt stack->pushBool(false); } else { /* - int Pointer = (int)Val->getMemBuffer(); + int pointer = (int)Val->getMemBuffer(); memcpy((byte *)_buffer+Start, &Pointer, sizeof(void*)); stack->pushBool(true); */ @@ -478,10 +478,10 @@ bool SXMemBuffer::scSetProperty(const char *name, ScValue *value) { // Length ////////////////////////////////////////////////////////////////////////// if (strcmp(name, "Length")==0){ - int OrigLength = _length; + int origLength = _length; _length = max(value->getInt(0), 0); - char PropName[20]; + char propName[20]; if (_length < OrigLength){ for(int i=_length; icorrectParams(2); - const char *dest = stack->pop()->getString(); - bool overwrite = stack->pop()->getBool(true); + /* const char *dest = */ stack->pop()->getString(); + /* bool overwrite = */ stack->pop()->getBool(true); close(); error("SXFile-Method: Copy not supported"); @@ -601,7 +601,7 @@ bool SXFile::scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack, else if (strcmp(name, "WriteDouble") == 0) { error("SXFile::WriteDouble - Not endian safe yet"); stack->correctParams(1); - double val = stack->pop()->getFloat(); + /* double val = */ stack->pop()->getFloat(); if (_textMode || !_writeFile) { script->runtimeError("File.%s: File must be open for writing in binary mode.", name); -- cgit v1.2.3 From 99d4c55e88712a0b0dc0d97e4c9c52946a5f48f2 Mon Sep 17 00:00:00 2001 From: Einar Johan Trøan Sømåen Date: Fri, 27 Jul 2012 19:07:00 +0200 Subject: WINTERMUTE: Remove all unneccessary #if 0 blocks --- engines/wintermute/base/scriptables/script_ext_date.cpp | 4 +--- engines/wintermute/base/scriptables/script_value.cpp | 4 ---- 2 files changed, 1 insertion(+), 7 deletions(-) (limited to 'engines/wintermute/base/scriptables') diff --git a/engines/wintermute/base/scriptables/script_ext_date.cpp b/engines/wintermute/base/scriptables/script_ext_date.cpp index a3bb7e2183..181be8ddd0 100644 --- a/engines/wintermute/base/scriptables/script_ext_date.cpp +++ b/engines/wintermute/base/scriptables/script_ext_date.cpp @@ -68,9 +68,7 @@ const char *SXDate::scToString() { // TODO: Make this more stringy, and less ISO 8601-like _strRep.format("%04d-%02d-%02d - %02d:%02d:%02d", _tm.tm_year, _tm.tm_mon, _tm.tm_mday, _tm.tm_hour, _tm.tm_min, _tm.tm_sec); return _strRep.c_str(); -#if 0 - return asctime(&_tm); -#endif + //return asctime(&_tm); } diff --git a/engines/wintermute/base/scriptables/script_value.cpp b/engines/wintermute/base/scriptables/script_value.cpp index 01cb4044ff..9b83daf42f 100644 --- a/engines/wintermute/base/scriptables/script_value.cpp +++ b/engines/wintermute/base/scriptables/script_value.cpp @@ -166,11 +166,7 @@ ScValue *ScValue::getProp(const char *name) { if (_type == VAL_STRING && strcmp(name, "Length") == 0) { _gameRef->_scValue->_type = VAL_INT; -#if 0 // TODO: Remove FreeType-dependency if (_gameRef->_textEncoding == TEXT_ANSI) { -#else - if (true) { -#endif _gameRef->_scValue->setInt(strlen(_valString)); } else { WideString wstr = StringUtil::utf8ToWide(_valString); -- cgit v1.2.3 From 8ed71a99f6aaee27a3187cb47fe0c3b30acf88a0 Mon Sep 17 00:00:00 2001 From: Einar Johan Trøan Sømåen Date: Fri, 27 Jul 2012 19:37:00 +0200 Subject: WINTERMUTE: Constructor(args): SuperClass(args) -> Constructor(args) : SuperClass(args) --- engines/wintermute/base/scriptables/script.cpp | 2 +- engines/wintermute/base/scriptables/script_engine.cpp | 2 +- engines/wintermute/base/scriptables/script_ext_array.cpp | 4 ++-- engines/wintermute/base/scriptables/script_ext_date.cpp | 2 +- engines/wintermute/base/scriptables/script_ext_file.cpp | 2 +- engines/wintermute/base/scriptables/script_ext_math.cpp | 2 +- engines/wintermute/base/scriptables/script_ext_mem_buffer.cpp | 4 ++-- engines/wintermute/base/scriptables/script_ext_object.cpp | 2 +- engines/wintermute/base/scriptables/script_ext_string.cpp | 2 +- engines/wintermute/base/scriptables/script_stack.cpp | 2 +- engines/wintermute/base/scriptables/script_value.cpp | 10 +++++----- 11 files changed, 17 insertions(+), 17 deletions(-) (limited to 'engines/wintermute/base/scriptables') diff --git a/engines/wintermute/base/scriptables/script.cpp b/engines/wintermute/base/scriptables/script.cpp index 75c4d38574..d8d4bc66da 100644 --- a/engines/wintermute/base/scriptables/script.cpp +++ b/engines/wintermute/base/scriptables/script.cpp @@ -38,7 +38,7 @@ namespace WinterMute { IMPLEMENT_PERSISTENT(ScScript, false) ////////////////////////////////////////////////////////////////////////// -ScScript::ScScript(BaseGame *inGame, ScEngine *Engine): BaseClass(inGame) { +ScScript::ScScript(BaseGame *inGame, ScEngine *Engine) : BaseClass(inGame) { _buffer = NULL; _bufferSize = _iP = 0; _scriptStream = NULL; diff --git a/engines/wintermute/base/scriptables/script_engine.cpp b/engines/wintermute/base/scriptables/script_engine.cpp index bc8d54ef58..01f647c110 100644 --- a/engines/wintermute/base/scriptables/script_engine.cpp +++ b/engines/wintermute/base/scriptables/script_engine.cpp @@ -44,7 +44,7 @@ IMPLEMENT_PERSISTENT(ScEngine, true) #define COMPILER_DLL "dcscomp.dll" ////////////////////////////////////////////////////////////////////////// -ScEngine::ScEngine(BaseGame *inGame): BaseClass(inGame) { +ScEngine::ScEngine(BaseGame *inGame) : BaseClass(inGame) { _gameRef->LOG(0, "Initializing scripting engine..."); if (_compilerAvailable) { diff --git a/engines/wintermute/base/scriptables/script_ext_array.cpp b/engines/wintermute/base/scriptables/script_ext_array.cpp index cc3bec89c0..79303723e0 100644 --- a/engines/wintermute/base/scriptables/script_ext_array.cpp +++ b/engines/wintermute/base/scriptables/script_ext_array.cpp @@ -41,7 +41,7 @@ BaseScriptable *makeSXArray(BaseGame *inGame, ScStack *stack) { } ////////////////////////////////////////////////////////////////////////// -SXArray::SXArray(BaseGame *inGame, ScStack *stack): BaseScriptable(inGame) { +SXArray::SXArray(BaseGame *inGame, ScStack *stack) : BaseScriptable(inGame) { _length = 0; _values = new ScValue(_gameRef); @@ -60,7 +60,7 @@ SXArray::SXArray(BaseGame *inGame, ScStack *stack): BaseScriptable(inGame) { } ////////////////////////////////////////////////////////////////////////// -SXArray::SXArray(BaseGame *inGame): BaseScriptable(inGame) { +SXArray::SXArray(BaseGame *inGame) : BaseScriptable(inGame) { _length = 0; _values = new ScValue(_gameRef); } diff --git a/engines/wintermute/base/scriptables/script_ext_date.cpp b/engines/wintermute/base/scriptables/script_ext_date.cpp index 181be8ddd0..72f41c83cc 100644 --- a/engines/wintermute/base/scriptables/script_ext_date.cpp +++ b/engines/wintermute/base/scriptables/script_ext_date.cpp @@ -39,7 +39,7 @@ BaseScriptable *makeSXDate(BaseGame *inGame, ScStack *stack) { } ////////////////////////////////////////////////////////////////////////// -SXDate::SXDate(BaseGame *inGame, ScStack *stack): BaseScriptable(inGame) { +SXDate::SXDate(BaseGame *inGame, ScStack *stack) : BaseScriptable(inGame) { stack->correctParams(6); memset(&_tm, 0, sizeof(_tm)); diff --git a/engines/wintermute/base/scriptables/script_ext_file.cpp b/engines/wintermute/base/scriptables/script_ext_file.cpp index 9736ae3ee2..5743722d6f 100644 --- a/engines/wintermute/base/scriptables/script_ext_file.cpp +++ b/engines/wintermute/base/scriptables/script_ext_file.cpp @@ -49,7 +49,7 @@ BaseScriptable *makeSXFile(BaseGame *inGame, ScStack *stack) { } ////////////////////////////////////////////////////////////////////////// -SXFile::SXFile(BaseGame *inGame, ScStack *stack): BaseScriptable(inGame) { +SXFile::SXFile(BaseGame *inGame, ScStack *stack) : BaseScriptable(inGame) { stack->correctParams(1); ScValue *val = stack->pop(); diff --git a/engines/wintermute/base/scriptables/script_ext_math.cpp b/engines/wintermute/base/scriptables/script_ext_math.cpp index 525b43434f..6b80da6389 100644 --- a/engines/wintermute/base/scriptables/script_ext_math.cpp +++ b/engines/wintermute/base/scriptables/script_ext_math.cpp @@ -47,7 +47,7 @@ BaseScriptable *makeSXMath(BaseGame *inGame) { } ////////////////////////////////////////////////////////////////////////// -SXMath::SXMath(BaseGame *inGame): BaseScriptable(inGame) { +SXMath::SXMath(BaseGame *inGame) : BaseScriptable(inGame) { } diff --git a/engines/wintermute/base/scriptables/script_ext_mem_buffer.cpp b/engines/wintermute/base/scriptables/script_ext_mem_buffer.cpp index 9bde5d45e7..afe5e65467 100644 --- a/engines/wintermute/base/scriptables/script_ext_mem_buffer.cpp +++ b/engines/wintermute/base/scriptables/script_ext_mem_buffer.cpp @@ -42,7 +42,7 @@ BaseScriptable *makeSXMemBuffer(BaseGame *inGame, ScStack *stack) { } ////////////////////////////////////////////////////////////////////////// -SXMemBuffer::SXMemBuffer(BaseGame *inGame, ScStack *stack): BaseScriptable(inGame) { +SXMemBuffer::SXMemBuffer(BaseGame *inGame, ScStack *stack) : BaseScriptable(inGame) { stack->correctParams(1); _buffer = NULL; _size = 0; @@ -52,7 +52,7 @@ SXMemBuffer::SXMemBuffer(BaseGame *inGame, ScStack *stack): BaseScriptable(inGam } ////////////////////////////////////////////////////////////////////////// -SXMemBuffer::SXMemBuffer(BaseGame *inGame, void *buffer): BaseScriptable(inGame) { +SXMemBuffer::SXMemBuffer(BaseGame *inGame, void *buffer) : BaseScriptable(inGame) { _size = 0; _buffer = buffer; } diff --git a/engines/wintermute/base/scriptables/script_ext_object.cpp b/engines/wintermute/base/scriptables/script_ext_object.cpp index b72e3e4b97..ab7296b1ce 100644 --- a/engines/wintermute/base/scriptables/script_ext_object.cpp +++ b/engines/wintermute/base/scriptables/script_ext_object.cpp @@ -43,7 +43,7 @@ BaseScriptable *makeSXObject(BaseGame *inGame, ScStack *stack) { } ////////////////////////////////////////////////////////////////////////// -SXObject::SXObject(BaseGame *inGame, ScStack *stack): BaseObject(inGame) { +SXObject::SXObject(BaseGame *inGame, ScStack *stack) : BaseObject(inGame) { int numParams = stack->pop()->getInt(0); for (int i = 0; i < numParams; i++) { addScript(stack->pop()->getString()); diff --git a/engines/wintermute/base/scriptables/script_ext_string.cpp b/engines/wintermute/base/scriptables/script_ext_string.cpp index 1c7349bd8d..adbf16611c 100644 --- a/engines/wintermute/base/scriptables/script_ext_string.cpp +++ b/engines/wintermute/base/scriptables/script_ext_string.cpp @@ -44,7 +44,7 @@ BaseScriptable *makeSXString(BaseGame *inGame, ScStack *stack) { } ////////////////////////////////////////////////////////////////////////// -SXString::SXString(BaseGame *inGame, ScStack *stack): BaseScriptable(inGame) { +SXString::SXString(BaseGame *inGame, ScStack *stack) : BaseScriptable(inGame) { _string = NULL; _capacity = 0; diff --git a/engines/wintermute/base/scriptables/script_stack.cpp b/engines/wintermute/base/scriptables/script_stack.cpp index 8840a2c0f1..9b3a0201ac 100644 --- a/engines/wintermute/base/scriptables/script_stack.cpp +++ b/engines/wintermute/base/scriptables/script_stack.cpp @@ -35,7 +35,7 @@ namespace WinterMute { IMPLEMENT_PERSISTENT(ScStack, false) ////////////////////////////////////////////////////////////////////////// -ScStack::ScStack(BaseGame *inGame): BaseClass(inGame) { +ScStack::ScStack(BaseGame *inGame) : BaseClass(inGame) { _sP = -1; } diff --git a/engines/wintermute/base/scriptables/script_value.cpp b/engines/wintermute/base/scriptables/script_value.cpp index 9b83daf42f..456f93507e 100644 --- a/engines/wintermute/base/scriptables/script_value.cpp +++ b/engines/wintermute/base/scriptables/script_value.cpp @@ -43,7 +43,7 @@ namespace WinterMute { IMPLEMENT_PERSISTENT(ScValue, false) ////////////////////////////////////////////////////////////////////////// -ScValue::ScValue(BaseGame *inGame): BaseClass(inGame) { +ScValue::ScValue(BaseGame *inGame) : BaseClass(inGame) { _type = VAL_NULL; _valBool = false; @@ -58,7 +58,7 @@ ScValue::ScValue(BaseGame *inGame): BaseClass(inGame) { ////////////////////////////////////////////////////////////////////////// -ScValue::ScValue(BaseGame *inGame, bool val): BaseClass(inGame) { +ScValue::ScValue(BaseGame *inGame, bool val) : BaseClass(inGame) { _type = VAL_BOOL; _valBool = val; @@ -73,7 +73,7 @@ ScValue::ScValue(BaseGame *inGame, bool val): BaseClass(inGame) { ////////////////////////////////////////////////////////////////////////// -ScValue::ScValue(BaseGame *inGame, int val): BaseClass(inGame) { +ScValue::ScValue(BaseGame *inGame, int val) : BaseClass(inGame) { _type = VAL_INT; _valInt = val; @@ -88,7 +88,7 @@ ScValue::ScValue(BaseGame *inGame, int val): BaseClass(inGame) { ////////////////////////////////////////////////////////////////////////// -ScValue::ScValue(BaseGame *inGame, double val): BaseClass(inGame) { +ScValue::ScValue(BaseGame *inGame, double val) : BaseClass(inGame) { _type = VAL_FLOAT; _valFloat = val; @@ -103,7 +103,7 @@ ScValue::ScValue(BaseGame *inGame, double val): BaseClass(inGame) { ////////////////////////////////////////////////////////////////////////// -ScValue::ScValue(BaseGame *inGame, const char *val): BaseClass(inGame) { +ScValue::ScValue(BaseGame *inGame, const char *val) : BaseClass(inGame) { _type = VAL_STRING; _valString = NULL; setStringVal(val); -- cgit v1.2.3 From 6dc1e09da93c0ba0507fd0ceadbbb504469deccc Mon Sep 17 00:00:00 2001 From: Einar Johan Trøan Sømåen Date: Sun, 29 Jul 2012 00:27:50 +0200 Subject: WINTERMUTE: Replace const char* with const Common::String & in fonts, gfx, particles, sound and files. --- engines/wintermute/base/scriptables/script.cpp | 32 +++++++++++++------------- engines/wintermute/base/scriptables/script.h | 16 ++++++------- 2 files changed, 24 insertions(+), 24 deletions(-) (limited to 'engines/wintermute/base/scriptables') diff --git a/engines/wintermute/base/scriptables/script.cpp b/engines/wintermute/base/scriptables/script.cpp index d8d4bc66da..29185edce6 100644 --- a/engines/wintermute/base/scriptables/script.cpp +++ b/engines/wintermute/base/scriptables/script.cpp @@ -277,14 +277,14 @@ bool ScScript::create(const char *filename, byte *buffer, uint32 size, BaseScrip ////////////////////////////////////////////////////////////////////////// -bool ScScript::createThread(ScScript *original, uint32 initIP, const char *eventName) { +bool ScScript::createThread(ScScript *original, uint32 initIP, const Common::String &eventName) { cleanup(); _thread = true; _methodThread = false; - _threadEvent = new char[strlen(eventName) + 1]; + _threadEvent = new char[eventName.size() + 1]; if (_threadEvent) { - strcpy(_threadEvent, eventName); + strcpy(_threadEvent, eventName.c_str()); } // copy filename @@ -329,7 +329,7 @@ bool ScScript::createThread(ScScript *original, uint32 initIP, const char *event ////////////////////////////////////////////////////////////////////////// -bool ScScript::createMethodThread(ScScript *original, const char *methodName) { +bool ScScript::createMethodThread(ScScript *original, const Common::String &methodName) { uint32 ip = original->getMethodPos(methodName); if (ip == 0) { return STATUS_FAILED; @@ -339,9 +339,9 @@ bool ScScript::createMethodThread(ScScript *original, const char *methodName) { _thread = true; _methodThread = true; - _threadEvent = new char[strlen(methodName) + 1]; + _threadEvent = new char[methodName.size() + 1]; if (_threadEvent) { - strcpy(_threadEvent, methodName); + strcpy(_threadEvent, methodName.c_str()); } // copy filename @@ -1149,9 +1149,9 @@ bool ScScript::executeInstruction() { ////////////////////////////////////////////////////////////////////////// -uint32 ScScript::getFuncPos(const char *name) { +uint32 ScScript::getFuncPos(const Common::String &name) { for (uint32 i = 0; i < _numFunctions; i++) { - if (strcmp(name, _functions[i].name) == 0) { + if (name == _functions[i].name) { return _functions[i].pos; } } @@ -1160,9 +1160,9 @@ uint32 ScScript::getFuncPos(const char *name) { ////////////////////////////////////////////////////////////////////////// -uint32 ScScript::getMethodPos(const char *name) { +uint32 ScScript::getMethodPos(const Common::String &name) { for (uint32 i = 0; i < _numMethods; i++) { - if (strcmp(name, _methods[i].name) == 0) { + if (name == _methods[i].name) { return _methods[i].pos; } } @@ -1357,7 +1357,7 @@ bool ScScript::persist(BasePersistenceManager *persistMgr) { ////////////////////////////////////////////////////////////////////////// -ScScript *ScScript::invokeEventHandler(const char *eventName, bool unbreakable) { +ScScript *ScScript::invokeEventHandler(const Common::String &eventName, bool unbreakable) { //if (_state!=SCRIPT_PERSISTENT) return NULL; uint32 pos = getEventPos(eventName); @@ -1371,7 +1371,7 @@ ScScript *ScScript::invokeEventHandler(const char *eventName, bool unbreakable) if (DID_SUCCEED(ret)) { thread->_unbreakable = unbreakable; _engine->_scripts.add(thread); - _gameRef->getDebugMgr()->onScriptEventThreadInit(thread, this, eventName); + _gameRef->getDebugMgr()->onScriptEventThreadInit(thread, this, eventName.c_str()); return thread; } else { delete thread; @@ -1385,9 +1385,9 @@ ScScript *ScScript::invokeEventHandler(const char *eventName, bool unbreakable) ////////////////////////////////////////////////////////////////////////// -uint32 ScScript::getEventPos(const char *name) { +uint32 ScScript::getEventPos(const Common::String &name) { for (int i = _numEvents - 1; i >= 0; i--) { - if (scumm_stricmp(name, _events[i].name) == 0) { + if (scumm_stricmp(name.c_str(), _events[i].name) == 0) { return _events[i].pos; } } @@ -1396,13 +1396,13 @@ uint32 ScScript::getEventPos(const char *name) { ////////////////////////////////////////////////////////////////////////// -bool ScScript::canHandleEvent(const char *eventName) { +bool ScScript::canHandleEvent(const Common::String &eventName) { return getEventPos(eventName) != 0; } ////////////////////////////////////////////////////////////////////////// -bool ScScript::canHandleMethod(const char *methodName) { +bool ScScript::canHandleMethod(const Common::String &methodName) { return getMethodPos(methodName) != 0; } diff --git a/engines/wintermute/base/scriptables/script.h b/engines/wintermute/base/scriptables/script.h index 2ee1319acb..6b5fa1733b 100644 --- a/engines/wintermute/base/scriptables/script.h +++ b/engines/wintermute/base/scriptables/script.h @@ -62,11 +62,11 @@ public: bool _freezable; bool resume(); bool pause(); - bool canHandleEvent(const char *eventName); - bool canHandleMethod(const char *methodName); - bool createThread(ScScript *original, uint32 initIP, const char *eventName); - bool createMethodThread(ScScript *original, const char *methodName); - ScScript *invokeEventHandler(const char *eventName, bool unbreakable = false); + bool canHandleEvent(const Common::String &eventName); + bool canHandleMethod(const Common::String &methodName); + bool createThread(ScScript *original, uint32 initIP, const Common::String &eventName); + bool createMethodThread(ScScript *original, const Common::String &methodName); + ScScript *invokeEventHandler(const Common::String &eventName, bool unbreakable = false); uint32 _timeSlice; DECLARE_PERSISTENT(ScScript, BaseClass) void runtimeError(const char *fmt, ...); @@ -82,9 +82,9 @@ public: TScriptState _state; TScriptState _origState; ScValue *getVar(char *name); - uint32 getFuncPos(const char *name); - uint32 getEventPos(const char *name); - uint32 getMethodPos(const char *name); + uint32 getFuncPos(const Common::String &name); + uint32 getEventPos(const Common::String &name); + uint32 getMethodPos(const Common::String &name); typedef struct { uint32 magic; uint32 version; -- cgit v1.2.3 From b214041539559e65b89b3270439970fd7173dcbe Mon Sep 17 00:00:00 2001 From: Einar Johan Trøan Sømåen Date: Sun, 29 Jul 2012 00:58:15 +0200 Subject: WINTERMUTE: Remove static function variable from SXArray --- engines/wintermute/base/scriptables/script_ext_array.cpp | 5 +++-- engines/wintermute/base/scriptables/script_ext_array.h | 1 + 2 files changed, 4 insertions(+), 2 deletions(-) (limited to 'engines/wintermute/base/scriptables') diff --git a/engines/wintermute/base/scriptables/script_ext_array.cpp b/engines/wintermute/base/scriptables/script_ext_array.cpp index 79303723e0..a422d6d8a5 100644 --- a/engines/wintermute/base/scriptables/script_ext_array.cpp +++ b/engines/wintermute/base/scriptables/script_ext_array.cpp @@ -75,7 +75,7 @@ SXArray::~SXArray() { ////////////////////////////////////////////////////////////////////////// const char *SXArray::scToString() { - static char dummy[32768]; // TODO: Get rid of static. + char dummy[32768]; strcpy(dummy, ""); char propName[20]; for (int i = 0; i < _length; i++) { @@ -91,7 +91,8 @@ const char *SXArray::scToString() { strcat(dummy, ","); } } - return dummy; + _strRep = dummy; + return _strRep.c_str(); } diff --git a/engines/wintermute/base/scriptables/script_ext_array.h b/engines/wintermute/base/scriptables/script_ext_array.h index 67a1104b46..f2718f4ea2 100644 --- a/engines/wintermute/base/scriptables/script_ext_array.h +++ b/engines/wintermute/base/scriptables/script_ext_array.h @@ -48,6 +48,7 @@ public: private: int _length; ScValue *_values; + Common::String _strRep; }; } // end of namespace WinterMute -- cgit v1.2.3 From 9b5cf8f1bafd5aa0dba9194a8f04e58724652891 Mon Sep 17 00:00:00 2001 From: Einar Johan Trøan Sømåen Date: Sun, 29 Jul 2012 02:30:26 +0200 Subject: WINTERMUTE: Introduce a Singleton-class for holding registry/filemanager. --- engines/wintermute/base/scriptables/script_engine.cpp | 13 +++++++------ engines/wintermute/base/scriptables/script_ext_file.cpp | 6 +++--- 2 files changed, 10 insertions(+), 9 deletions(-) (limited to 'engines/wintermute/base/scriptables') diff --git a/engines/wintermute/base/scriptables/script_engine.cpp b/engines/wintermute/base/scriptables/script_engine.cpp index 01f647c110..9e11a7c5b4 100644 --- a/engines/wintermute/base/scriptables/script_engine.cpp +++ b/engines/wintermute/base/scriptables/script_engine.cpp @@ -33,6 +33,7 @@ #include "engines/wintermute/base/scriptables/script_stack.h" #include "engines/wintermute/base/scriptables/script_ext_math.h" #include "engines/wintermute/base/base_registry.h" +#include "engines/wintermute/base/base_engine.h" #include "engines/wintermute/base/base_game.h" #include "engines/wintermute/base/sound/base_sound.h" #include "engines/wintermute/base/base_file_manager.h" @@ -128,7 +129,7 @@ bool ScEngine::cleanup() { ////////////////////////////////////////////////////////////////////////// byte *ScEngine::loadFile(void *data, char *filename, uint32 *size) { BaseGame *gameRef = (BaseGame *)data; - return gameRef->_fileManager->readWholeFile(filename, size); + return BaseFileManager::getEngineInstance()->readWholeFile(filename, size); } @@ -200,7 +201,7 @@ byte *ScEngine::getCompiledScript(const char *filename, uint32 *outSize, bool ig uint32 size; - byte *buffer = _gameRef->_fileManager->readWholeFile(filename, &size); + byte *buffer = BaseEngine::getInstance()->getFileManager()->readWholeFile(filename, &size); if (!buffer) { _gameRef->LOG(0, "ScEngine::GetCompiledScript - error opening script '%s'", filename); return NULL; @@ -689,10 +690,10 @@ bool ScEngine::saveBreakpoints() { sprintf(key, "Breakpoint%d", count); sprintf(text, "%s:%d", _breakpoints[i]->_filename.c_str(), _breakpoints[i]->_lines[j]); - _gameRef->_registry->writeString("Debug", key, text); + BaseEngine::getInstance()->getRegistry()->writeString("Debug", key, text); } } - _gameRef->_registry->writeInt("Debug", "NumBreakpoints", count); + BaseEngine::getInstance()->getRegistry()->writeInt("Debug", "NumBreakpoints", count); return STATUS_OK; } @@ -705,11 +706,11 @@ bool ScEngine::loadBreakpoints() { char key[100]; - int count = _gameRef->_registry->readInt("Debug", "NumBreakpoints", 0); + int count = BaseEngine::getInstance()->getRegistry()->readInt("Debug", "NumBreakpoints", 0); for (int i = 1; i <= count; i++) { /* uint32 bufSize = 512; */ sprintf(key, "Breakpoint%d", i); - AnsiString breakpoint = _gameRef->_registry->readString("Debug", key, ""); + AnsiString breakpoint = BaseEngine::getInstance()->getRegistry()->readString("Debug", key, ""); char *path = BaseUtils::strEntry(0, breakpoint.c_str(), ':'); char *line = BaseUtils::strEntry(1, breakpoint.c_str(), ':'); diff --git a/engines/wintermute/base/scriptables/script_ext_file.cpp b/engines/wintermute/base/scriptables/script_ext_file.cpp index 5743722d6f..50d89921e1 100644 --- a/engines/wintermute/base/scriptables/script_ext_file.cpp +++ b/engines/wintermute/base/scriptables/script_ext_file.cpp @@ -82,7 +82,7 @@ void SXFile::cleanup() { ////////////////////////////////////////////////////////////////////////// void SXFile::close() { if (_readFile) { - _gameRef->_fileManager->closeFile(_readFile); + BaseFileManager::getEngineInstance()->closeFile(_readFile); _readFile = NULL; } if (_writeFile) { @@ -130,7 +130,7 @@ bool SXFile::scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack, _mode = 1; } if (_mode == 1) { - _readFile = _gameRef->_fileManager->openFile(_filename); + _readFile = BaseFileManager::getEngineInstance()->openFile(_filename); if (!_readFile) { //script->runtimeError("File.%s: Error opening file '%s' for reading.", Name, _filename); close(); @@ -785,7 +785,7 @@ bool SXFile::persist(BasePersistenceManager *persistMgr) { if (_mode != 0) { // open for reading if (_mode == 1) { - _readFile = _gameRef->_fileManager->openFile(_filename); + _readFile = BaseFileManager::getEngineInstance()->openFile(_filename); if (!_readFile) { close(); } -- cgit v1.2.3 From 482a5fb467e6694fb54c75f718563992657edf97 Mon Sep 17 00:00:00 2001 From: Einar Johan Trøan Sømåen Date: Sun, 29 Jul 2012 02:59:14 +0200 Subject: WINTERMUTE: Make the BaseEngine-singleton use Common::Singleton as super-class --- engines/wintermute/base/scriptables/script_engine.cpp | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) (limited to 'engines/wintermute/base/scriptables') diff --git a/engines/wintermute/base/scriptables/script_engine.cpp b/engines/wintermute/base/scriptables/script_engine.cpp index 9e11a7c5b4..96250dbb90 100644 --- a/engines/wintermute/base/scriptables/script_engine.cpp +++ b/engines/wintermute/base/scriptables/script_engine.cpp @@ -128,7 +128,6 @@ bool ScEngine::cleanup() { ////////////////////////////////////////////////////////////////////////// byte *ScEngine::loadFile(void *data, char *filename, uint32 *size) { - BaseGame *gameRef = (BaseGame *)data; return BaseFileManager::getEngineInstance()->readWholeFile(filename, size); } @@ -201,7 +200,7 @@ byte *ScEngine::getCompiledScript(const char *filename, uint32 *outSize, bool ig uint32 size; - byte *buffer = BaseEngine::getInstance()->getFileManager()->readWholeFile(filename, &size); + byte *buffer = BaseEngine::instance().getFileManager()->readWholeFile(filename, &size); if (!buffer) { _gameRef->LOG(0, "ScEngine::GetCompiledScript - error opening script '%s'", filename); return NULL; @@ -690,10 +689,10 @@ bool ScEngine::saveBreakpoints() { sprintf(key, "Breakpoint%d", count); sprintf(text, "%s:%d", _breakpoints[i]->_filename.c_str(), _breakpoints[i]->_lines[j]); - BaseEngine::getInstance()->getRegistry()->writeString("Debug", key, text); + BaseEngine::instance().getRegistry()->writeString("Debug", key, text); } } - BaseEngine::getInstance()->getRegistry()->writeInt("Debug", "NumBreakpoints", count); + BaseEngine::instance().getRegistry()->writeInt("Debug", "NumBreakpoints", count); return STATUS_OK; } @@ -706,11 +705,11 @@ bool ScEngine::loadBreakpoints() { char key[100]; - int count = BaseEngine::getInstance()->getRegistry()->readInt("Debug", "NumBreakpoints", 0); + int count = BaseEngine::instance().getRegistry()->readInt("Debug", "NumBreakpoints", 0); for (int i = 1; i <= count; i++) { /* uint32 bufSize = 512; */ sprintf(key, "Breakpoint%d", i); - AnsiString breakpoint = BaseEngine::getInstance()->getRegistry()->readString("Debug", key, ""); + AnsiString breakpoint = BaseEngine::instance().getRegistry()->readString("Debug", key, ""); char *path = BaseUtils::strEntry(0, breakpoint.c_str(), ':'); char *line = BaseUtils::strEntry(1, breakpoint.c_str(), ':'); -- cgit v1.2.3 From aa3467ddaa4b1df72398a1545c9d8b1c89dad6ad Mon Sep 17 00:00:00 2001 From: Einar Johan Trøan Sømåen Date: Sun, 29 Jul 2012 15:30:44 +0200 Subject: WINTERMUTE: Remove the debugger. --- engines/wintermute/base/scriptables/script.cpp | 115 ------------- engines/wintermute/base/scriptables/script.h | 13 +- .../wintermute/base/scriptables/script_engine.cpp | 181 +-------------------- .../wintermute/base/scriptables/script_engine.h | 14 -- .../wintermute/base/scriptables/script_value.cpp | 136 ---------------- engines/wintermute/base/scriptables/script_value.h | 30 +--- 6 files changed, 3 insertions(+), 486 deletions(-) (limited to 'engines/wintermute/base/scriptables') diff --git a/engines/wintermute/base/scriptables/script.cpp b/engines/wintermute/base/scriptables/script.cpp index 29185edce6..fb77225700 100644 --- a/engines/wintermute/base/scriptables/script.cpp +++ b/engines/wintermute/base/scriptables/script.cpp @@ -152,10 +152,6 @@ bool ScScript::initScript() { _scriptStream->seek(_iP); _currentLine = 0; - // init breakpoints - _engine->refreshScriptBreakpoints(this); - - // ready to rumble... _state = SCRIPT_RUNNING; @@ -531,14 +527,8 @@ bool ScScript::executeInstruction() { dw = getDWORD(); if (_scopeStack->_sP < 0) { _globals->setProp(_symbols[dw], _operand); - if (_gameRef->getDebugMgr()->_enabled) { - _gameRef->getDebugMgr()->onVariableInit(WME_DBGVAR_SCRIPT, this, NULL, _globals->getProp(_symbols[dw]), _symbols[dw]); - } } else { _scopeStack->getTop()->setProp(_symbols[dw], _operand); - if (_gameRef->getDebugMgr()->_enabled) { - _gameRef->getDebugMgr()->onVariableInit(WME_DBGVAR_SCOPE, this, _scopeStack->getTop(), _scopeStack->getTop()->getProp(_symbols[dw]), _symbols[dw]); - } } break; @@ -551,26 +541,14 @@ bool ScScript::executeInstruction() { if (!_engine->_globals->propExists(_symbols[dw])) { _operand->setNULL(); _engine->_globals->setProp(_symbols[dw], _operand, false, inst == II_DEF_CONST_VAR); - - if (_gameRef->getDebugMgr()->_enabled) { - _gameRef->getDebugMgr()->onVariableInit(WME_DBGVAR_GLOBAL, this, NULL, _engine->_globals->getProp(_symbols[dw]), _symbols[dw]); - } } break; } case II_RET: if (_scopeStack->_sP >= 0 && _callStack->_sP >= 0) { - _gameRef->getDebugMgr()->onScriptShutdownScope(this, _scopeStack->getTop()); - _scopeStack->pop(); _iP = (uint32)_callStack->pop()->getInt(); - - if (_scopeStack->_sP < 0) { - _gameRef->getDebugMgr()->onScriptChangeScope(this, NULL); - } else { - _gameRef->getDebugMgr()->onScriptChangeScope(this, _scopeStack->getTop()); - } } else { if (_thread) { _state = SCRIPT_THREAD_FINISHED; @@ -695,13 +673,6 @@ bool ScScript::executeInstruction() { case II_SCOPE: _operand->setNULL(); _scopeStack->push(_operand); - - if (_scopeStack->_sP < 0) { - _gameRef->getDebugMgr()->onScriptChangeScope(this, NULL); - } else { - _gameRef->getDebugMgr()->onScriptChangeScope(this, _scopeStack->getTop()); - } - break; case II_CORRECT_STACK: @@ -754,10 +725,6 @@ bool ScScript::executeInstruction() { var->copy(val); } } - - if (_gameRef->getDebugMgr()->_enabled) { - _gameRef->getDebugMgr()->onVariableChangeValue(var, val); - } } break; @@ -827,10 +794,6 @@ bool ScScript::executeInstruction() { var->setProp(str, val); } - if (_gameRef->getDebugMgr()->_enabled) { - _gameRef->getDebugMgr()->onVariableChangeValue(var, NULL); - } - break; } @@ -1117,21 +1080,6 @@ bool ScScript::executeInstruction() { int newLine = getDWORD(); if (newLine != _currentLine) { _currentLine = newLine; - if (_gameRef->getDebugMgr()->_enabled) { - _gameRef->getDebugMgr()->onScriptChangeLine(this, _currentLine); - for (int i = 0; i < _breakpoints.getSize(); i++) { - if (_breakpoints[i] == _currentLine) { - _gameRef->getDebugMgr()->onScriptHitBreakpoint(this); - sleep(0); - break; - } - } - if (_tracingMode) { - _gameRef->getDebugMgr()->onScriptHitBreakpoint(this); - sleep(0); - break; - } - } } break; @@ -1371,7 +1319,6 @@ ScScript *ScScript::invokeEventHandler(const Common::String &eventName, bool unb if (DID_SUCCEED(ret)) { thread->_unbreakable = unbreakable; _engine->_scripts.add(thread); - _gameRef->getDebugMgr()->onScriptEventThreadInit(thread, this, eventName.c_str()); return thread; } else { delete thread; @@ -1497,68 +1444,6 @@ const char *ScScript::dbgGetFilename() { return _filename; } - -////////////////////////////////////////////////////////////////////////// -bool ScScript::dbgSendScript(IWmeDebugClient *client) { - if (_methodThread) { - client->onScriptMethodThreadInit(this, _parentScript, _threadEvent); - } else if (_thread) { - client->onScriptEventThreadInit(this, _parentScript, _threadEvent); - } else { - client->onScriptInit(this); - } - - return dbgSendVariables(client); - return STATUS_OK; -} - -////////////////////////////////////////////////////////////////////////// -bool ScScript::dbgSendVariables(IWmeDebugClient *client) { - // send script globals - _globals->dbgSendVariables(client, WME_DBGVAR_SCRIPT, this, 0); - - // send scope variables - if (_scopeStack->_sP >= 0) { - for (int i = 0; i <= _scopeStack->_sP; i++) { - // ScValue *Scope = _scopeStack->GetAt(i); - //Scope->DbgSendVariables(Client, WME_DBGVAR_SCOPE, this, (unsigned int)Scope); - } - } - return STATUS_OK; -} - - -////////////////////////////////////////////////////////////////////////// -TScriptState ScScript::dbgGetState() { - return _state; -} - -////////////////////////////////////////////////////////////////////////// -int ScScript::dbgGetNumBreakpoints() { - return _breakpoints.getSize(); -} - -////////////////////////////////////////////////////////////////////////// -int ScScript::dbgGetBreakpoint(int index) { - if (index >= 0 && index < _breakpoints.getSize()) { - return _breakpoints[index]; - } else { - return -1; - } -} - -////////////////////////////////////////////////////////////////////////// -bool ScScript::dbgSetTracingMode(bool isTracing) { - _tracingMode = isTracing; - return true; -} - -////////////////////////////////////////////////////////////////////////// -bool ScScript::dbgGetTracingMode() { - return _tracingMode; -} - - ////////////////////////////////////////////////////////////////////////// void ScScript::afterLoad() { if (_buffer == NULL) { diff --git a/engines/wintermute/base/scriptables/script.h b/engines/wintermute/base/scriptables/script.h index 6b5fa1733b..1cd5c2dc1e 100644 --- a/engines/wintermute/base/scriptables/script.h +++ b/engines/wintermute/base/scriptables/script.h @@ -34,18 +34,13 @@ #include "engines/wintermute/dcscript.h" // Added by ClassView #include "engines/wintermute/coll_templ.h" -#include "engines/wintermute/wme_debugger.h" - namespace WinterMute { class BaseScriptHolder; class BaseObject; class ScEngine; class ScStack; -class ScScript : public BaseClass, public IWmeDebugScript { +class ScScript : public BaseClass { public: - bool dbgSendScript(IWmeDebugClient *client); - bool dbgSendVariables(IWmeDebugClient *client); - BaseArray _breakpoints; bool _tracingMode; @@ -172,12 +167,6 @@ private: public: virtual int dbgGetLine(); virtual const char *dbgGetFilename(); - virtual TScriptState dbgGetState(); - virtual int dbgGetNumBreakpoints(); - virtual int dbgGetBreakpoint(int Index); - - virtual bool dbgSetTracingMode(bool IsTracing); - virtual bool dbgGetTracingMode(); }; } // end of namespace WinterMute diff --git a/engines/wintermute/base/scriptables/script_engine.cpp b/engines/wintermute/base/scriptables/script_engine.cpp index 96250dbb90..06f584c9bb 100644 --- a/engines/wintermute/base/scriptables/script_engine.cpp +++ b/engines/wintermute/base/scriptables/script_engine.cpp @@ -88,17 +88,10 @@ ScEngine::ScEngine(BaseGame *inGame) : BaseClass(inGame) { ////////////////////////////////////////////////////////////////////////// ScEngine::~ScEngine() { _gameRef->LOG(0, "Shutting down scripting engine"); - saveBreakpoints(); disableProfiling(); cleanup(); - - for (int i = 0; i < _breakpoints.getSize(); i++) { - delete _breakpoints[i]; - _breakpoints[i] = NULL; - } - _breakpoints.clear(); } @@ -174,7 +167,6 @@ ScScript *ScEngine::runScript(const char *filename, BaseScriptHolder *owner) { script->_globals->setProp("this", &val); _scripts.add(script); - _gameRef->getDebugMgr()->onScriptInit(script); return script; } @@ -402,7 +394,7 @@ bool ScEngine::removeFinishedScripts() { if (!_scripts[i]->_thread && _scripts[i]->_owner) { _scripts[i]->_owner->removeScript(_scripts[i]); } - _gameRef->getDebugMgr()->onScriptShutdown(_scripts[i]); + delete _scripts[i]; _scripts.remove_at(i); i--; @@ -556,177 +548,6 @@ bool ScEngine::clearGlobals(bool includingNatives) { return STATUS_OK; } -////////////////////////////////////////////////////////////////////////// -bool ScEngine::dbgSendScripts(IWmeDebugClient *client) { - // send global variables - _globals->dbgSendVariables(client, WME_DBGVAR_GLOBAL, NULL, 0); - - // process normal scripts first - for (int i = 0; i < _scripts.getSize(); i++) { - if (_scripts[i]->_thread || _scripts[i]->_methodThread) { - continue; - } - _scripts[i]->dbgSendScript(client); - } - - // and threads later - for (int i = 0; i < _scripts.getSize(); i++) { - if (_scripts[i]->_thread || _scripts[i]->_methodThread) { - _scripts[i]->dbgSendScript(client); - } - } - - return STATUS_OK; -} - -////////////////////////////////////////////////////////////////////////// -bool ScEngine::addBreakpoint(const char *scriptFilename, int line) { - if (!_gameRef->getDebugMgr()->_enabled) { - return STATUS_OK; - } - - CScBreakpoint *bp = NULL; - for (int i = 0; i < _breakpoints.getSize(); i++) { - if (scumm_stricmp(_breakpoints[i]->_filename.c_str(), scriptFilename) == 0) { - bp = _breakpoints[i]; - break; - } - } - if (bp == NULL) { - bp = new CScBreakpoint(scriptFilename); - _breakpoints.add(bp); - } - - for (int i = 0; i < bp->_lines.getSize(); i++) { - if (bp->_lines[i] == line) { - return STATUS_OK; - } - } - bp->_lines.add(line); - - // refresh changes - refreshScriptBreakpoints(); - - return STATUS_OK; -} - -////////////////////////////////////////////////////////////////////////// -bool ScEngine::removeBreakpoint(const char *scriptFilename, int line) { - if (!_gameRef->getDebugMgr()->_enabled) { - return STATUS_OK; - } - - for (int i = 0; i < _breakpoints.getSize(); i++) { - if (scumm_stricmp(_breakpoints[i]->_filename.c_str(), scriptFilename) == 0) { - for (int j = 0; j < _breakpoints[i]->_lines.getSize(); j++) { - if (_breakpoints[i]->_lines[j] == line) { - _breakpoints[i]->_lines.remove_at(j); - if (_breakpoints[i]->_lines.getSize() == 0) { - delete _breakpoints[i]; - _breakpoints.remove_at(i); - } - // refresh changes - refreshScriptBreakpoints(); - - return STATUS_OK; - } - } - break; - } - } - return STATUS_FAILED; -} - -////////////////////////////////////////////////////////////////////////// -bool ScEngine::refreshScriptBreakpoints() { - if (!_gameRef->getDebugMgr()->_enabled) { - return STATUS_OK; - } - - for (int i = 0; i < _scripts.getSize(); i++) { - refreshScriptBreakpoints(_scripts[i]); - } - return STATUS_OK; -} - -////////////////////////////////////////////////////////////////////////// -bool ScEngine::refreshScriptBreakpoints(ScScript *script) { - if (!_gameRef->getDebugMgr()->_enabled) { - return STATUS_OK; - } - - if (!script || !script->_filename) { - return STATUS_FAILED; - } - - for (int i = 0; i < _breakpoints.getSize(); i++) { - if (scumm_stricmp(_breakpoints[i]->_filename.c_str(), script->_filename) == 0) { - script->_breakpoints.copy(_breakpoints[i]->_lines); - return STATUS_OK; - } - } - if (script->_breakpoints.getSize() > 0) { - script->_breakpoints.clear(); - } - - return STATUS_OK; -} - -////////////////////////////////////////////////////////////////////////// -bool ScEngine::saveBreakpoints() { - if (!_gameRef->getDebugMgr()->_enabled) { - return STATUS_OK; - } - - - char text[512]; - char key[100]; - - int count = 0; - for (int i = 0; i < _breakpoints.getSize(); i++) { - for (int j = 0; j < _breakpoints[i]->_lines.getSize(); j++) { - count++; - sprintf(key, "Breakpoint%d", count); - sprintf(text, "%s:%d", _breakpoints[i]->_filename.c_str(), _breakpoints[i]->_lines[j]); - - BaseEngine::instance().getRegistry()->writeString("Debug", key, text); - } - } - BaseEngine::instance().getRegistry()->writeInt("Debug", "NumBreakpoints", count); - - return STATUS_OK; -} - -////////////////////////////////////////////////////////////////////////// -bool ScEngine::loadBreakpoints() { - if (!_gameRef->getDebugMgr()->_enabled) { - return STATUS_OK; - } - - char key[100]; - - int count = BaseEngine::instance().getRegistry()->readInt("Debug", "NumBreakpoints", 0); - for (int i = 1; i <= count; i++) { - /* uint32 bufSize = 512; */ - sprintf(key, "Breakpoint%d", i); - AnsiString breakpoint = BaseEngine::instance().getRegistry()->readString("Debug", key, ""); - - char *path = BaseUtils::strEntry(0, breakpoint.c_str(), ':'); - char *line = BaseUtils::strEntry(1, breakpoint.c_str(), ':'); - - if (path != NULL && line != NULL) { - addBreakpoint(path, atoi(line)); - } - delete[] path; - delete[] line; - path = NULL; - line = NULL; - } - - return STATUS_OK; -} - - ////////////////////////////////////////////////////////////////////////// void ScEngine::addScriptTime(const char *filename, uint32 time) { if (!_isProfiling) { diff --git a/engines/wintermute/base/scriptables/script_engine.h b/engines/wintermute/base/scriptables/script_engine.h index fcfaa51971..9aae8be85b 100644 --- a/engines/wintermute/base/scriptables/script_engine.h +++ b/engines/wintermute/base/scriptables/script_engine.h @@ -32,7 +32,6 @@ #include "engines/wintermute/persistent.h" #include "engines/wintermute/coll_templ.h" #include "engines/wintermute/base/base.h" -#include "engines/wintermute/wme_debugger.h" namespace WinterMute { @@ -81,20 +80,7 @@ public: BaseArray _lines; }; - - - public: - bool dbgSendScripts(IWmeDebugClient *client); - - BaseArray _breakpoints; - bool addBreakpoint(const char *scriptFilename, int line); - bool removeBreakpoint(const char *scriptFilename, int line); - bool refreshScriptBreakpoints(); - bool refreshScriptBreakpoints(ScScript *script); - bool saveBreakpoints(); - bool loadBreakpoints(); - bool clearGlobals(bool includingNatives = false); bool tickUnbreakable(); bool removeFinishedScripts(); diff --git a/engines/wintermute/base/scriptables/script_value.cpp b/engines/wintermute/base/scriptables/script_value.cpp index 456f93507e..d35f85f9a1 100644 --- a/engines/wintermute/base/scriptables/script_value.cpp +++ b/engines/wintermute/base/scriptables/script_value.cpp @@ -950,18 +950,6 @@ int ScValue::compareStrict(ScValue *val1, ScValue *val2) { } } - -////////////////////////////////////////////////////////////////////////// -bool ScValue::dbgSendVariables(IWmeDebugClient *client, EWmeDebuggerVariableType type, ScScript *script, unsigned int scopeID) { - _valIter = _valObject.begin(); - while (_valIter != _valObject.end()) { - client->onVariableInit(type, script, scopeID, _valIter->_value, _valIter->_key.c_str()); - _valIter++; - } - return STATUS_OK; -} - - ////////////////////////////////////////////////////////////////////////// bool ScValue::setProperty(const char *propName, int value) { ScValue *val = new ScValue(_gameRef, value); @@ -1004,128 +992,4 @@ bool ScValue::setProperty(const char *propName) { return ret; } - -////////////////////////////////////////////////////////////////////////// -// IWmeDebugProp -////////////////////////////////////////////////////////////////////////// -EWmeDebuggerPropType ScValue::dbgGetType() { - switch (getType()) { - case VAL_NULL: - return WME_DBGPROP_NULL; - case VAL_STRING: - return WME_DBGPROP_STRING; - case VAL_INT: - return WME_DBGPROP_INT; - case VAL_BOOL: - return WME_DBGPROP_BOOL; - case VAL_FLOAT: - return WME_DBGPROP_FLOAT; - case VAL_OBJECT: - return WME_DBGPROP_OBJECT; - case VAL_NATIVE: - return WME_DBGPROP_NATIVE; - default: - return WME_DBGPROP_UNKNOWN; - } -} - -////////////////////////////////////////////////////////////////////////// -int ScValue::dbgGetValInt() { - return getInt(); -} - -////////////////////////////////////////////////////////////////////////// -double ScValue::dbgGetValFloat() { - return getFloat(); -} - -////////////////////////////////////////////////////////////////////////// -bool ScValue::dbgGetValBool() { - return getBool(); -} - -////////////////////////////////////////////////////////////////////////// -const char *ScValue::dbgGetValString() { - return getString(); -} - -////////////////////////////////////////////////////////////////////////// -IWmeDebugObject *ScValue::dbgGetValNative() { - return getNative(); -} - -////////////////////////////////////////////////////////////////////////// -bool ScValue::dbgSetVal(int value) { - setInt(value); - return true; -} - -////////////////////////////////////////////////////////////////////////// -bool ScValue::dbgSetVal(double value) { - setFloat(value); - return true; -} - -////////////////////////////////////////////////////////////////////////// -bool ScValue::dbgSetVal(bool value) { - setBool(value); - return true; -} - -////////////////////////////////////////////////////////////////////////// -bool ScValue::dbgSetVal(const char *value) { - setString(value); - return true; -} - -////////////////////////////////////////////////////////////////////////// -bool ScValue::dbgSetVal() { - setNULL(); - return true; -} - - -////////////////////////////////////////////////////////////////////////// -int ScValue::dbgGetNumProperties() { - if (_valNative && _valNative->_scProp) { - return _valNative->_scProp->dbgGetNumProperties(); - } else { - return _valObject.size(); - } -} - -////////////////////////////////////////////////////////////////////////// -bool ScValue::dbgGetProperty(int index, const char **name, IWmeDebugProp **value) { - if (_valNative && _valNative->_scProp) { - return _valNative->_scProp->dbgGetProperty(index, name, value); - } else { - int count = 0; - _valIter = _valObject.begin(); - while (_valIter != _valObject.end()) { - if (count == index) { - *name = _valIter->_key.c_str(); - *value = _valIter->_value; - return true; - } - _valIter++; - count++; - } - return false; - } -} - -////////////////////////////////////////////////////////////////////////// -bool ScValue::dbgGetDescription(char *buf, int bufSize) { - if (_type == VAL_VARIABLE_REF) { - return _valRef->dbgGetDescription(buf, bufSize); - } - - if (_type == VAL_NATIVE) { - _valNative->scDebuggerDesc(buf, bufSize); - } else { - strncpy(buf, getString(), bufSize); - } - return true; -} - } // end of namespace WinterMute diff --git a/engines/wintermute/base/scriptables/script_value.h b/engines/wintermute/base/scriptables/script_value.h index 069c36bd47..8fced06972 100644 --- a/engines/wintermute/base/scriptables/script_value.h +++ b/engines/wintermute/base/scriptables/script_value.h @@ -33,7 +33,6 @@ #include "engines/wintermute/base/base.h" #include "engines/wintermute/persistent.h" #include "engines/wintermute/dcscript.h" // Added by ClassView -#include "engines/wintermute/wme_debugger.h" #include "common/str.h" namespace WinterMute { @@ -41,10 +40,8 @@ namespace WinterMute { class ScScript; class BaseScriptable; -class ScValue : public BaseClass, public IWmeDebugProp { +class ScValue : public BaseClass { public: - bool dbgSendVariables(IWmeDebugClient *client, EWmeDebuggerVariableType type, ScScript *script, unsigned int scopeID); - static int compare(ScValue *val1, ScValue *val2); static int compareStrict(ScValue *val1, ScValue *val2); TValType getTypeTolerant(); @@ -109,31 +106,6 @@ public: bool setProperty(const char *propName, double value); bool setProperty(const char *propName, bool value); bool setProperty(const char *propName); - - -// IWmeDebugProp interface implementation -public: - virtual EWmeDebuggerPropType dbgGetType(); - - // getters - virtual int dbgGetValInt(); - virtual double dbgGetValFloat(); - virtual bool dbgGetValBool(); - virtual const char *dbgGetValString(); - virtual IWmeDebugObject *dbgGetValNative(); - - // setters - virtual bool dbgSetVal(int value); - virtual bool dbgSetVal(double value); - virtual bool dbgSetVal(bool value); - virtual bool dbgSetVal(const char *value); - virtual bool dbgSetVal(); - - // properties - virtual int dbgGetNumProperties(); - virtual bool dbgGetProperty(int index, const char **mame, IWmeDebugProp **value); - - virtual bool dbgGetDescription(char *buf, int bufSize); }; } // end of namespace WinterMute -- cgit v1.2.3 From 2e7d21fc525a5b0451274d3844e3d6a1de1f6cb2 Mon Sep 17 00:00:00 2001 From: Einar Johan Trøan Sømåen Date: Sun, 29 Jul 2012 17:53:44 +0200 Subject: WINTERMUTE: Replace BaseRegistry with ConfMan --- engines/wintermute/base/scriptables/script_engine.cpp | 1 - 1 file changed, 1 deletion(-) (limited to 'engines/wintermute/base/scriptables') diff --git a/engines/wintermute/base/scriptables/script_engine.cpp b/engines/wintermute/base/scriptables/script_engine.cpp index 06f584c9bb..e5d965a4b1 100644 --- a/engines/wintermute/base/scriptables/script_engine.cpp +++ b/engines/wintermute/base/scriptables/script_engine.cpp @@ -32,7 +32,6 @@ #include "engines/wintermute/base/scriptables/script.h" #include "engines/wintermute/base/scriptables/script_stack.h" #include "engines/wintermute/base/scriptables/script_ext_math.h" -#include "engines/wintermute/base/base_registry.h" #include "engines/wintermute/base/base_engine.h" #include "engines/wintermute/base/base_game.h" #include "engines/wintermute/base/sound/base_sound.h" -- cgit v1.2.3 From 20e2ec4ff4d869ba54edf5b930d84340245292b6 Mon Sep 17 00:00:00 2001 From: Einar Johan Trøan Sømåen Date: Sun, 29 Jul 2012 18:00:15 +0200 Subject: WINTERMUTE: Remove unused functions from StringUtil:: --- engines/wintermute/base/scriptables/script_engine.cpp | 1 - 1 file changed, 1 deletion(-) (limited to 'engines/wintermute/base/scriptables') diff --git a/engines/wintermute/base/scriptables/script_engine.cpp b/engines/wintermute/base/scriptables/script_engine.cpp index e5d965a4b1..a1f246c4bc 100644 --- a/engines/wintermute/base/scriptables/script_engine.cpp +++ b/engines/wintermute/base/scriptables/script_engine.cpp @@ -27,7 +27,6 @@ */ #include "engines/wintermute/base/scriptables/script_engine.h" -#include "engines/wintermute/utils/string_util.h" #include "engines/wintermute/base/scriptables/script_value.h" #include "engines/wintermute/base/scriptables/script.h" #include "engines/wintermute/base/scriptables/script_stack.h" -- cgit v1.2.3 From 52aeaf4ecee11074a3f1fe303aef862f1b6ac028 Mon Sep 17 00:00:00 2001 From: Einar Johan Trøan Sømåen Date: Tue, 7 Aug 2012 14:06:42 +0200 Subject: WINTERMUTE: Move dcscript.h into the proper folder. --- engines/wintermute/base/scriptables/dcscript.h | 141 +++++++++++++++++++++ engines/wintermute/base/scriptables/script.h | 2 +- engines/wintermute/base/scriptables/script_value.h | 2 +- 3 files changed, 143 insertions(+), 2 deletions(-) create mode 100644 engines/wintermute/base/scriptables/dcscript.h (limited to 'engines/wintermute/base/scriptables') diff --git a/engines/wintermute/base/scriptables/dcscript.h b/engines/wintermute/base/scriptables/dcscript.h new file mode 100644 index 0000000000..9610ebf94c --- /dev/null +++ b/engines/wintermute/base/scriptables/dcscript.h @@ -0,0 +1,141 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +/* + * This file is based on WME Lite. + * http://dead-code.org/redir.php?target=wmelite + * Copyright (c) 2011 Jan Nedoma + */ + +#ifndef WINTERMUTE_DCSCRIPT_H +#define WINTERMUTE_DCSCRIPT_H + +namespace WinterMute { + +#define SCRIPT_MAGIC 0xDEC0ADDE +#define SCRIPT_VERSION 0x0102 + +// value types +typedef enum { + VAL_NULL, + VAL_STRING, + VAL_INT, + VAL_BOOL, + VAL_FLOAT, + VAL_OBJECT, + VAL_NATIVE, + VAL_VARIABLE_REF +} TValType; + + +// script states +typedef enum { + SCRIPT_RUNNING, + SCRIPT_WAITING, + SCRIPT_SLEEPING, + SCRIPT_FINISHED, + SCRIPT_PERSISTENT, + SCRIPT_ERROR, + SCRIPT_PAUSED, + SCRIPT_WAITING_SCRIPT, + SCRIPT_THREAD_FINISHED +} TScriptState; + +// opcodes +typedef enum { + II_DEF_VAR = 0, + II_DEF_GLOB_VAR, + II_RET, + II_RET_EVENT, + II_CALL, + II_CALL_BY_EXP, + II_EXTERNAL_CALL, + II_SCOPE, + II_CORRECT_STACK, + II_CREATE_OBJECT, + II_POP_EMPTY, + II_PUSH_VAR, + II_PUSH_VAR_REF, + II_POP_VAR, + II_PUSH_VAR_THIS, // push current this on stack + II_PUSH_INT, + II_PUSH_BOOL, + II_PUSH_FLOAT, + II_PUSH_STRING, + II_PUSH_NULL, + II_PUSH_THIS_FROM_STACK, + II_PUSH_THIS, + II_POP_THIS, + II_PUSH_BY_EXP, + II_POP_BY_EXP, + II_JMP, + II_JMP_FALSE, + II_ADD, + II_SUB, + II_MUL, + II_DIV, + II_MODULO, + II_NOT, + II_AND, + II_OR, + II_CMP_EQ, + II_CMP_NE, + II_CMP_L, + II_CMP_G, + II_CMP_LE, + II_CMP_GE, + II_CMP_STRICT_EQ, + II_CMP_STRICT_NE, + II_DBG_LINE, + II_POP_REG1, + II_PUSH_REG1, + II_DEF_CONST_VAR +} TInstruction; + +// external data types +typedef enum { + TYPE_VOID = 0, + TYPE_BOOL, + TYPE_LONG, + TYPE_BYTE, + TYPE_STRING, + TYPE_FLOAT, + TYPE_DOUBLE, + TYPE_MEMBUFFER +} TExternalType; + + +// call types +typedef enum { + CALL_STDCALL = 0, + CALL_CDECL, + CALL_THISCALL +} TCallType; + +// element types +typedef enum { + ELEMENT_STRING = 0 +} TElementType; + +} // end of namespace WinterMute + +#endif diff --git a/engines/wintermute/base/scriptables/script.h b/engines/wintermute/base/scriptables/script.h index 1cd5c2dc1e..9f49fac994 100644 --- a/engines/wintermute/base/scriptables/script.h +++ b/engines/wintermute/base/scriptables/script.h @@ -31,7 +31,7 @@ #include "engines/wintermute/base/base.h" -#include "engines/wintermute/dcscript.h" // Added by ClassView +#include "engines/wintermute/base/scriptables/dcscript.h" // Added by ClassView #include "engines/wintermute/coll_templ.h" namespace WinterMute { diff --git a/engines/wintermute/base/scriptables/script_value.h b/engines/wintermute/base/scriptables/script_value.h index 8fced06972..c8d04168b3 100644 --- a/engines/wintermute/base/scriptables/script_value.h +++ b/engines/wintermute/base/scriptables/script_value.h @@ -32,7 +32,7 @@ #include "engines/wintermute/base/base.h" #include "engines/wintermute/persistent.h" -#include "engines/wintermute/dcscript.h" // Added by ClassView +#include "engines/wintermute/base/scriptables/dcscript.h" // Added by ClassView #include "common/str.h" namespace WinterMute { -- cgit v1.2.3 From c422ae9d8a90aeb63da3e3fdf521323fe6769828 Mon Sep 17 00:00:00 2001 From: Einar Johan Trøan Sømåen Date: Sat, 11 Aug 2012 02:30:07 +0200 Subject: WINTERMUTE: Get rid of strncpy+manual termination. --- engines/wintermute/base/scriptables/script_ext_mem_buffer.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'engines/wintermute/base/scriptables') diff --git a/engines/wintermute/base/scriptables/script_ext_mem_buffer.cpp b/engines/wintermute/base/scriptables/script_ext_mem_buffer.cpp index afe5e65467..557d9b9b57 100644 --- a/engines/wintermute/base/scriptables/script_ext_mem_buffer.cpp +++ b/engines/wintermute/base/scriptables/script_ext_mem_buffer.cpp @@ -261,8 +261,7 @@ bool SXMemBuffer::scCallMethod(ScScript *script, ScStack *stack, ScStack *thisSt stack->pushNULL(); } else { char *str = new char[length + 1]; - strncpy(str, (const char *)_buffer + start, length); - str[length] = '\0'; + Common::strlcpy(str, (const char *)_buffer + start, length + 1); stack->pushString(str); delete[] str; } -- cgit v1.2.3 From fed19cb66ae5b56dd7dc81b90edd5a0d15986678 Mon Sep 17 00:00:00 2001 From: Einar Johan Trøan Sømåen Date: Mon, 13 Aug 2012 03:42:30 +0200 Subject: WINTERMUTE: WinterMute -> Wintermute --- engines/wintermute/base/scriptables/dcscript.h | 4 ++-- engines/wintermute/base/scriptables/script.cpp | 4 ++-- engines/wintermute/base/scriptables/script.h | 4 ++-- engines/wintermute/base/scriptables/script_engine.cpp | 4 ++-- engines/wintermute/base/scriptables/script_engine.h | 4 ++-- engines/wintermute/base/scriptables/script_ext_array.cpp | 4 ++-- engines/wintermute/base/scriptables/script_ext_array.h | 4 ++-- engines/wintermute/base/scriptables/script_ext_date.cpp | 4 ++-- engines/wintermute/base/scriptables/script_ext_date.h | 4 ++-- engines/wintermute/base/scriptables/script_ext_file.cpp | 4 ++-- engines/wintermute/base/scriptables/script_ext_file.h | 4 ++-- engines/wintermute/base/scriptables/script_ext_math.cpp | 4 ++-- engines/wintermute/base/scriptables/script_ext_math.h | 4 ++-- engines/wintermute/base/scriptables/script_ext_mem_buffer.cpp | 4 ++-- engines/wintermute/base/scriptables/script_ext_mem_buffer.h | 4 ++-- engines/wintermute/base/scriptables/script_ext_object.cpp | 4 ++-- engines/wintermute/base/scriptables/script_ext_object.h | 4 ++-- engines/wintermute/base/scriptables/script_ext_string.cpp | 4 ++-- engines/wintermute/base/scriptables/script_ext_string.h | 4 ++-- engines/wintermute/base/scriptables/script_stack.cpp | 4 ++-- engines/wintermute/base/scriptables/script_stack.h | 4 ++-- engines/wintermute/base/scriptables/script_value.cpp | 4 ++-- engines/wintermute/base/scriptables/script_value.h | 4 ++-- 23 files changed, 46 insertions(+), 46 deletions(-) (limited to 'engines/wintermute/base/scriptables') diff --git a/engines/wintermute/base/scriptables/dcscript.h b/engines/wintermute/base/scriptables/dcscript.h index 9610ebf94c..db63ad2b87 100644 --- a/engines/wintermute/base/scriptables/dcscript.h +++ b/engines/wintermute/base/scriptables/dcscript.h @@ -29,7 +29,7 @@ #ifndef WINTERMUTE_DCSCRIPT_H #define WINTERMUTE_DCSCRIPT_H -namespace WinterMute { +namespace Wintermute { #define SCRIPT_MAGIC 0xDEC0ADDE #define SCRIPT_VERSION 0x0102 @@ -136,6 +136,6 @@ typedef enum { ELEMENT_STRING = 0 } TElementType; -} // end of namespace WinterMute +} // end of namespace Wintermute #endif diff --git a/engines/wintermute/base/scriptables/script.cpp b/engines/wintermute/base/scriptables/script.cpp index fb77225700..6c8a88adb0 100644 --- a/engines/wintermute/base/scriptables/script.cpp +++ b/engines/wintermute/base/scriptables/script.cpp @@ -33,7 +33,7 @@ #include "engines/wintermute/base/scriptables/script_stack.h" #include "common/memstream.h" -namespace WinterMute { +namespace Wintermute { IMPLEMENT_PERSISTENT(ScScript, false) @@ -1464,4 +1464,4 @@ void ScScript::afterLoad() { } } -} // end of namespace WinterMute +} // end of namespace Wintermute diff --git a/engines/wintermute/base/scriptables/script.h b/engines/wintermute/base/scriptables/script.h index 9f49fac994..132f1ce6c5 100644 --- a/engines/wintermute/base/scriptables/script.h +++ b/engines/wintermute/base/scriptables/script.h @@ -34,7 +34,7 @@ #include "engines/wintermute/base/scriptables/dcscript.h" // Added by ClassView #include "engines/wintermute/coll_templ.h" -namespace WinterMute { +namespace Wintermute { class BaseScriptHolder; class BaseObject; class ScEngine; @@ -169,6 +169,6 @@ public: virtual const char *dbgGetFilename(); }; -} // end of namespace WinterMute +} // end of namespace Wintermute #endif diff --git a/engines/wintermute/base/scriptables/script_engine.cpp b/engines/wintermute/base/scriptables/script_engine.cpp index a1f246c4bc..8765fb9864 100644 --- a/engines/wintermute/base/scriptables/script_engine.cpp +++ b/engines/wintermute/base/scriptables/script_engine.cpp @@ -37,7 +37,7 @@ #include "engines/wintermute/base/base_file_manager.h" #include "engines/wintermute/utils/utils.h" -namespace WinterMute { +namespace Wintermute { IMPLEMENT_PERSISTENT(ScEngine, true) @@ -608,4 +608,4 @@ void ScEngine::dumpStats() { }*/ } -} // end of namespace WinterMute +} // end of namespace Wintermute diff --git a/engines/wintermute/base/scriptables/script_engine.h b/engines/wintermute/base/scriptables/script_engine.h index 9aae8be85b..f526353368 100644 --- a/engines/wintermute/base/scriptables/script_engine.h +++ b/engines/wintermute/base/scriptables/script_engine.h @@ -33,7 +33,7 @@ #include "engines/wintermute/coll_templ.h" #include "engines/wintermute/base/base.h" -namespace WinterMute { +namespace Wintermute { #define MAX_CACHED_SCRIPTS 20 class ScScript; @@ -130,6 +130,6 @@ private: }; -} // end of namespace WinterMute +} // end of namespace Wintermute #endif diff --git a/engines/wintermute/base/scriptables/script_ext_array.cpp b/engines/wintermute/base/scriptables/script_ext_array.cpp index a422d6d8a5..c8d4f64ae9 100644 --- a/engines/wintermute/base/scriptables/script_ext_array.cpp +++ b/engines/wintermute/base/scriptables/script_ext_array.cpp @@ -32,7 +32,7 @@ #include "engines/wintermute/system/sys_instance.h" #include "engines/wintermute/base/scriptables/script_ext_array.h" -namespace WinterMute { +namespace Wintermute { IMPLEMENT_PERSISTENT(SXArray, false) @@ -249,4 +249,4 @@ bool SXArray::push(ScValue *val) { return STATUS_OK; } -} // end of namespace WinterMute +} // end of namespace Wintermute diff --git a/engines/wintermute/base/scriptables/script_ext_array.h b/engines/wintermute/base/scriptables/script_ext_array.h index f2718f4ea2..614f829950 100644 --- a/engines/wintermute/base/scriptables/script_ext_array.h +++ b/engines/wintermute/base/scriptables/script_ext_array.h @@ -31,7 +31,7 @@ #include "engines/wintermute/base/base_scriptable.h" -namespace WinterMute { +namespace Wintermute { class SXArray : public BaseScriptable { public: @@ -51,6 +51,6 @@ private: Common::String _strRep; }; -} // end of namespace WinterMute +} // end of namespace Wintermute #endif diff --git a/engines/wintermute/base/scriptables/script_ext_date.cpp b/engines/wintermute/base/scriptables/script_ext_date.cpp index 72f41c83cc..b5acc7d389 100644 --- a/engines/wintermute/base/scriptables/script_ext_date.cpp +++ b/engines/wintermute/base/scriptables/script_ext_date.cpp @@ -30,7 +30,7 @@ #include "engines/wintermute/base/scriptables/script_value.h" #include "engines/wintermute/base/scriptables/script_ext_date.h" -namespace WinterMute { +namespace Wintermute { IMPLEMENT_PERSISTENT(SXDate, false) @@ -291,4 +291,4 @@ int SXDate::scCompare(BaseScriptable *Value) { } } -} // end of namespace WinterMute +} // end of namespace Wintermute diff --git a/engines/wintermute/base/scriptables/script_ext_date.h b/engines/wintermute/base/scriptables/script_ext_date.h index 69fe1f1ae5..521b7a9f8a 100644 --- a/engines/wintermute/base/scriptables/script_ext_date.h +++ b/engines/wintermute/base/scriptables/script_ext_date.h @@ -32,7 +32,7 @@ #include "common/system.h" #include "engines/wintermute/base/base_scriptable.h" -namespace WinterMute { +namespace Wintermute { class SXDate : public BaseScriptable { public: @@ -50,6 +50,6 @@ private: Common::String _strRep; }; -} // end of namespace WinterMute +} // end of namespace Wintermute #endif diff --git a/engines/wintermute/base/scriptables/script_ext_file.cpp b/engines/wintermute/base/scriptables/script_ext_file.cpp index 50d89921e1..53d551119d 100644 --- a/engines/wintermute/base/scriptables/script_ext_file.cpp +++ b/engines/wintermute/base/scriptables/script_ext_file.cpp @@ -40,7 +40,7 @@ // Note: This code is completely untested, as I have yet to find a game that uses SXFile. -namespace WinterMute { +namespace Wintermute { IMPLEMENT_PERSISTENT(SXFile, false) @@ -826,4 +826,4 @@ Common::WriteStream *SXFile::openForAppend(const Common::String &filename, bool error("SXFile::openForAppend - WriteFiles not supported"); } -} // end of namespace WinterMute +} // end of namespace Wintermute diff --git a/engines/wintermute/base/scriptables/script_ext_file.h b/engines/wintermute/base/scriptables/script_ext_file.h index d083f96a4a..389974a48e 100644 --- a/engines/wintermute/base/scriptables/script_ext_file.h +++ b/engines/wintermute/base/scriptables/script_ext_file.h @@ -33,7 +33,7 @@ #include "engines/wintermute/base/base_scriptable.h" #include "common/stream.h" -namespace WinterMute { +namespace Wintermute { class BaseFile; @@ -61,6 +61,6 @@ private: Common::WriteStream *openForAppend(const Common::String &filename, bool binary); }; -} // end of namespace WinterMute +} // end of namespace Wintermute #endif diff --git a/engines/wintermute/base/scriptables/script_ext_math.cpp b/engines/wintermute/base/scriptables/script_ext_math.cpp index 6b80da6389..906d807275 100644 --- a/engines/wintermute/base/scriptables/script_ext_math.cpp +++ b/engines/wintermute/base/scriptables/script_ext_math.cpp @@ -33,7 +33,7 @@ #include "common/math.h" #include -namespace WinterMute { +namespace Wintermute { ////////////////////////////////////////////////////////////////////// // Construction/Destruction @@ -292,4 +292,4 @@ bool SXMath::persist(BasePersistenceManager *persistMgr) { return STATUS_OK; } -} // end of namespace WinterMute +} // end of namespace Wintermute diff --git a/engines/wintermute/base/scriptables/script_ext_math.h b/engines/wintermute/base/scriptables/script_ext_math.h index 393342e5ca..b195c0785d 100644 --- a/engines/wintermute/base/scriptables/script_ext_math.h +++ b/engines/wintermute/base/scriptables/script_ext_math.h @@ -32,7 +32,7 @@ #include "engines/wintermute/base/base_scriptable.h" -namespace WinterMute { +namespace Wintermute { class SXMath : public BaseScriptable { public: @@ -48,6 +48,6 @@ private: }; -} // end of namespace WinterMute +} // end of namespace Wintermute #endif diff --git a/engines/wintermute/base/scriptables/script_ext_mem_buffer.cpp b/engines/wintermute/base/scriptables/script_ext_mem_buffer.cpp index 557d9b9b57..eef1931d8b 100644 --- a/engines/wintermute/base/scriptables/script_ext_mem_buffer.cpp +++ b/engines/wintermute/base/scriptables/script_ext_mem_buffer.cpp @@ -33,7 +33,7 @@ #include "engines/wintermute/base/scriptables/script_ext_mem_buffer.h" #include "common/file.h" -namespace WinterMute { +namespace Wintermute { IMPLEMENT_PERSISTENT(SXMemBuffer, false) @@ -526,4 +526,4 @@ int SXMemBuffer::scCompare(BaseScriptable *val) { } } -} // end of namespace WinterMute +} // end of namespace Wintermute diff --git a/engines/wintermute/base/scriptables/script_ext_mem_buffer.h b/engines/wintermute/base/scriptables/script_ext_mem_buffer.h index b5428b4bd9..0a16167b58 100644 --- a/engines/wintermute/base/scriptables/script_ext_mem_buffer.h +++ b/engines/wintermute/base/scriptables/script_ext_mem_buffer.h @@ -32,7 +32,7 @@ #include "engines/wintermute/base/base_scriptable.h" -namespace WinterMute { +namespace Wintermute { class SXMemBuffer : public BaseScriptable { public: @@ -55,6 +55,6 @@ private: bool checkBounds(ScScript *script, int start, int length); }; -} // end of namespace WinterMute +} // end of namespace Wintermute #endif diff --git a/engines/wintermute/base/scriptables/script_ext_object.cpp b/engines/wintermute/base/scriptables/script_ext_object.cpp index ab7296b1ce..40c9b885cd 100644 --- a/engines/wintermute/base/scriptables/script_ext_object.cpp +++ b/engines/wintermute/base/scriptables/script_ext_object.cpp @@ -30,7 +30,7 @@ #include "engines/wintermute/base/scriptables/script_value.h" #include "engines/wintermute/base/scriptables/script_stack.h" -namespace WinterMute { +namespace Wintermute { ////////////////////////////////////////////////////////////////////// // Construction/Destruction @@ -64,4 +64,4 @@ bool SXObject::persist(BasePersistenceManager *persistMgr) { return STATUS_OK; } -} // end of namespace WinterMute +} // end of namespace Wintermute diff --git a/engines/wintermute/base/scriptables/script_ext_object.h b/engines/wintermute/base/scriptables/script_ext_object.h index f7d3a7bc0f..32aa00776e 100644 --- a/engines/wintermute/base/scriptables/script_ext_object.h +++ b/engines/wintermute/base/scriptables/script_ext_object.h @@ -32,7 +32,7 @@ #include "engines/wintermute/base/base_object.h" -namespace WinterMute { +namespace Wintermute { class SXObject : public BaseObject { public: @@ -41,6 +41,6 @@ public: virtual ~SXObject(); }; -} // end of namespace WinterMute +} // end of namespace Wintermute #endif diff --git a/engines/wintermute/base/scriptables/script_ext_string.cpp b/engines/wintermute/base/scriptables/script_ext_string.cpp index adbf16611c..9bcfe28bbf 100644 --- a/engines/wintermute/base/scriptables/script_ext_string.cpp +++ b/engines/wintermute/base/scriptables/script_ext_string.cpp @@ -35,7 +35,7 @@ #include "engines/wintermute/utils/string_util.h" #include "common/tokenizer.h" -namespace WinterMute { +namespace Wintermute { IMPLEMENT_PERSISTENT(SXString, false) @@ -433,4 +433,4 @@ int SXString::scCompare(BaseScriptable *val) { return strcmp(_string, ((SXString *)val)->_string); } -} // end of namespace WinterMute +} // end of namespace Wintermute diff --git a/engines/wintermute/base/scriptables/script_ext_string.h b/engines/wintermute/base/scriptables/script_ext_string.h index 9a3bbfc80b..c2de3860ed 100644 --- a/engines/wintermute/base/scriptables/script_ext_string.h +++ b/engines/wintermute/base/scriptables/script_ext_string.h @@ -32,7 +32,7 @@ #include "engines/wintermute/base/base_scriptable.h" -namespace WinterMute { +namespace Wintermute { class SXString : public BaseScriptable { public: @@ -53,6 +53,6 @@ private: int _capacity; }; -} // end of namespace WinterMute +} // end of namespace Wintermute #endif diff --git a/engines/wintermute/base/scriptables/script_stack.cpp b/engines/wintermute/base/scriptables/script_stack.cpp index 9b3a0201ac..7e81cdaf17 100644 --- a/engines/wintermute/base/scriptables/script_stack.cpp +++ b/engines/wintermute/base/scriptables/script_stack.cpp @@ -30,7 +30,7 @@ #include "engines/wintermute/base/scriptables/script_value.h" #include "engines/wintermute/base/base_game.h" -namespace WinterMute { +namespace Wintermute { IMPLEMENT_PERSISTENT(ScStack, false) @@ -229,4 +229,4 @@ bool ScStack::persist(BasePersistenceManager *persistMgr) { return STATUS_OK; } -} // end of namespace WinterMute +} // end of namespace Wintermute diff --git a/engines/wintermute/base/scriptables/script_stack.h b/engines/wintermute/base/scriptables/script_stack.h index 95839cc680..0e2adae518 100644 --- a/engines/wintermute/base/scriptables/script_stack.h +++ b/engines/wintermute/base/scriptables/script_stack.h @@ -34,7 +34,7 @@ #include "engines/wintermute/coll_templ.h" #include "engines/wintermute/persistent.h" -namespace WinterMute { +namespace Wintermute { class ScValue; class BaseScriptable; @@ -61,6 +61,6 @@ public: }; -} // end of namespace WinterMute +} // end of namespace Wintermute #endif diff --git a/engines/wintermute/base/scriptables/script_value.cpp b/engines/wintermute/base/scriptables/script_value.cpp index d35f85f9a1..03ca69ac7e 100644 --- a/engines/wintermute/base/scriptables/script_value.cpp +++ b/engines/wintermute/base/scriptables/script_value.cpp @@ -34,7 +34,7 @@ #include "engines/wintermute/utils/string_util.h" #include "engines/wintermute/base/base_scriptable.h" -namespace WinterMute { +namespace Wintermute { ////////////////////////////////////////////////////////////////////// // Construction/Destruction @@ -992,4 +992,4 @@ bool ScValue::setProperty(const char *propName) { return ret; } -} // end of namespace WinterMute +} // end of namespace Wintermute diff --git a/engines/wintermute/base/scriptables/script_value.h b/engines/wintermute/base/scriptables/script_value.h index c8d04168b3..af31014bac 100644 --- a/engines/wintermute/base/scriptables/script_value.h +++ b/engines/wintermute/base/scriptables/script_value.h @@ -35,7 +35,7 @@ #include "engines/wintermute/base/scriptables/dcscript.h" // Added by ClassView #include "common/str.h" -namespace WinterMute { +namespace Wintermute { class ScScript; class BaseScriptable; @@ -108,6 +108,6 @@ public: bool setProperty(const char *propName); }; -} // end of namespace WinterMute +} // end of namespace Wintermute #endif -- cgit v1.2.3 From 10ca0f136d120677d776981fc58aba548181c033 Mon Sep 17 00:00:00 2001 From: Einar Johan Trøan Sømåen Date: Wed, 15 Aug 2012 02:35:11 +0200 Subject: WINTERMUTE: Clear out some commented-out code. --- engines/wintermute/base/scriptables/script_engine.cpp | 1 - 1 file changed, 1 deletion(-) (limited to 'engines/wintermute/base/scriptables') diff --git a/engines/wintermute/base/scriptables/script_engine.cpp b/engines/wintermute/base/scriptables/script_engine.cpp index 8765fb9864..575e24a3a3 100644 --- a/engines/wintermute/base/scriptables/script_engine.cpp +++ b/engines/wintermute/base/scriptables/script_engine.cpp @@ -312,7 +312,6 @@ bool ScEngine::tick() { break; } default: - //warning("ScEngine::Tick - Unhandled enum"); break; } // switch } // for each script -- cgit v1.2.3 From eacb0b169814345271d9d8e947d3d4fefe06ad7f Mon Sep 17 00:00:00 2001 From: Einar Johan Trøan Sømåen Date: Wed, 15 Aug 2012 02:36:08 +0200 Subject: WINTERMUTE: Make use of the wday-field in dates, instead of a wrong mday-value. --- engines/wintermute/base/scriptables/script_ext_date.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'engines/wintermute/base/scriptables') diff --git a/engines/wintermute/base/scriptables/script_ext_date.cpp b/engines/wintermute/base/scriptables/script_ext_date.cpp index b5acc7d389..7726015081 100644 --- a/engines/wintermute/base/scriptables/script_ext_date.cpp +++ b/engines/wintermute/base/scriptables/script_ext_date.cpp @@ -127,8 +127,7 @@ bool SXDate::scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack, ////////////////////////////////////////////////////////////////////////// else if (strcmp(name, "GetWeekday") == 0) { stack->correctParams(0); - warning("GetWeekday returns a wrong value on purpose"); - stack->pushInt(_tm.tm_mday % 7); + stack->pushInt(_tm.tm_wday); return STATUS_OK; } -- cgit v1.2.3 From 764ca7a51a27210ca7a66a6ab2f01714029af09c Mon Sep 17 00:00:00 2001 From: Einar Johan Trøan Sømåen Date: Fri, 31 Aug 2012 14:21:33 +0200 Subject: WINTERMUTE: Fix some of the clang++ warnings listed by LordHoto --- engines/wintermute/base/scriptables/script_ext_date.h | 1 - 1 file changed, 1 deletion(-) (limited to 'engines/wintermute/base/scriptables') diff --git a/engines/wintermute/base/scriptables/script_ext_date.h b/engines/wintermute/base/scriptables/script_ext_date.h index 521b7a9f8a..7cdf57e689 100644 --- a/engines/wintermute/base/scriptables/script_ext_date.h +++ b/engines/wintermute/base/scriptables/script_ext_date.h @@ -45,7 +45,6 @@ public: bool scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack, const char *name); const char *scToString(); private: - char *_string; TimeDate _tm; Common::String _strRep; }; -- cgit v1.2.3 From b01f09e82fd254823a839a8d4f622dc4d35bde6a Mon Sep 17 00:00:00 2001 From: Einar Johan Trøan Sømåen Date: Fri, 31 Aug 2012 17:28:37 +0200 Subject: WINTERMUTE: Use instead of --- engines/wintermute/base/scriptables/script_ext_math.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'engines/wintermute/base/scriptables') diff --git a/engines/wintermute/base/scriptables/script_ext_math.cpp b/engines/wintermute/base/scriptables/script_ext_math.cpp index 906d807275..f7d0ba20b9 100644 --- a/engines/wintermute/base/scriptables/script_ext_math.cpp +++ b/engines/wintermute/base/scriptables/script_ext_math.cpp @@ -31,7 +31,7 @@ #include "engines/wintermute/base/scriptables/script_value.h" #include "engines/wintermute/persistent.h" #include "common/math.h" -#include +#include namespace Wintermute { -- cgit v1.2.3 From 2fd38f47e04430b8684502e625f87a04dd968def Mon Sep 17 00:00:00 2001 From: Einar Johan Trøan Sømåen Date: Fri, 31 Aug 2012 18:25:24 +0200 Subject: WINTERMUTE: Replace col_templ's getSize with Common::Array::size() --- engines/wintermute/base/scriptables/script.cpp | 2 +- .../wintermute/base/scriptables/script_engine.cpp | 28 +++++++++++----------- .../wintermute/base/scriptables/script_stack.cpp | 18 +++++++------- 3 files changed, 24 insertions(+), 24 deletions(-) (limited to 'engines/wintermute/base/scriptables') diff --git a/engines/wintermute/base/scriptables/script.cpp b/engines/wintermute/base/scriptables/script.cpp index 6c8a88adb0..beef4ee9d2 100644 --- a/engines/wintermute/base/scriptables/script.cpp +++ b/engines/wintermute/base/scriptables/script.cpp @@ -1423,7 +1423,7 @@ bool ScScript::copyParameters(ScStack *stack) { ////////////////////////////////////////////////////////////////////////// bool ScScript::finishThreads() { - for (int i = 0; i < _engine->_scripts.getSize(); i++) { + for (uint32 i = 0; i < _engine->_scripts.size(); i++) { ScScript *scr = _engine->_scripts[i]; if (scr->_thread && scr->_state != SCRIPT_FINISHED && scr->_owner == _owner && scumm_stricmp(scr->_filename, _filename) == 0) { scr->finish(true); diff --git a/engines/wintermute/base/scriptables/script_engine.cpp b/engines/wintermute/base/scriptables/script_engine.cpp index 575e24a3a3..d518be579f 100644 --- a/engines/wintermute/base/scriptables/script_engine.cpp +++ b/engines/wintermute/base/scriptables/script_engine.cpp @@ -95,7 +95,7 @@ ScEngine::~ScEngine() { ////////////////////////////////////////////////////////////////////////// bool ScEngine::cleanup() { - for (int i = 0; i < _scripts.getSize(); i++) { + for (uint32 i = 0; i < _scripts.size(); i++) { if (!_scripts[i]->_thread && _scripts[i]->_owner) { _scripts[i]->_owner->removeScript(_scripts[i]); } @@ -248,19 +248,19 @@ byte *ScEngine::getCompiledScript(const char *filename, uint32 *outSize, bool ig ////////////////////////////////////////////////////////////////////////// bool ScEngine::tick() { - if (_scripts.getSize() == 0) { + if (_scripts.size() == 0) { return STATUS_OK; } // resolve waiting scripts - for (int i = 0; i < _scripts.getSize(); i++) { + for (uint32 i = 0; i < _scripts.size(); i++) { switch (_scripts[i]->_state) { case SCRIPT_WAITING: { /* bool obj_found=false; - for(int j=0; j<_gameRef->_regObjects.getSize(); j++) + for(int j=0; j<_gameRef->_regObjects.size(); j++) { if (_gameRef->_regObjects[j] == _scripts[i]->_waitObject) { @@ -318,7 +318,7 @@ bool ScEngine::tick() { // execute scripts - for (int i = 0; i < _scripts.getSize(); i++) { + for (uint32 i = 0; i < _scripts.size(); i++) { // skip paused scripts if (_scripts[i]->_state == SCRIPT_PAUSED) { @@ -365,7 +365,7 @@ bool ScEngine::tick() { ////////////////////////////////////////////////////////////////////////// bool ScEngine::tickUnbreakable() { // execute unbreakable scripts - for (int i = 0; i < _scripts.getSize(); i++) { + for (uint32 i = 0; i < _scripts.size(); i++) { if (!_scripts[i]->_unbreakable) { continue; } @@ -386,7 +386,7 @@ bool ScEngine::tickUnbreakable() { ////////////////////////////////////////////////////////////////////////// bool ScEngine::removeFinishedScripts() { // remove finished scripts - for (int i = 0; i < _scripts.getSize(); i++) { + for (uint32 i = 0; i < _scripts.size(); i++) { if (_scripts[i]->_state == SCRIPT_FINISHED || _scripts[i]->_state == SCRIPT_ERROR) { if (!_scripts[i]->_thread && _scripts[i]->_owner) { _scripts[i]->_owner->removeScript(_scripts[i]); @@ -405,7 +405,7 @@ bool ScEngine::removeFinishedScripts() { int ScEngine::getNumScripts(int *running, int *waiting, int *persistent) { int numRunning = 0, numWaiting = 0, numPersistent = 0, numTotal = 0; - for (int i = 0; i < _scripts.getSize(); i++) { + for (uint32 i = 0; i < _scripts.size(); i++) { if (_scripts[i]->_state == SCRIPT_FINISHED) { continue; } @@ -456,7 +456,7 @@ bool ScEngine::emptyScriptCache() { ////////////////////////////////////////////////////////////////////////// bool ScEngine::resetObject(BaseObject *Object) { // terminate all scripts waiting for this object - for (int i = 0; i < _scripts.getSize(); i++) { + for (uint32 i = 0; i < _scripts.size(); i++) { if (_scripts[i]->_state == SCRIPT_WAITING && _scripts[i]->_waitObject == Object) { if (!_gameRef->_compatKillMethodThreads) { resetScript(_scripts[i]); @@ -472,7 +472,7 @@ bool ScEngine::resetObject(BaseObject *Object) { ////////////////////////////////////////////////////////////////////////// bool ScEngine::resetScript(ScScript *script) { // terminate all scripts waiting for this script - for (int i = 0; i < _scripts.getSize(); i++) { + for (uint32 i = 0; i < _scripts.size(); i++) { if (_scripts[i]->_state == SCRIPT_WAITING_SCRIPT && _scripts[i]->_waitScript == script) { _scripts[i]->finish(); } @@ -497,7 +497,7 @@ bool ScEngine::persist(BasePersistenceManager *persistMgr) { ////////////////////////////////////////////////////////////////////////// void ScEngine::editorCleanup() { - for (int i = 0; i < _scripts.getSize(); i++) { + for (uint32 i = 0; i < _scripts.size(); i++) { if (_scripts[i]->_owner == NULL && (_scripts[i]->_state == SCRIPT_FINISHED || _scripts[i]->_state == SCRIPT_ERROR)) { delete _scripts[i]; _scripts.remove_at(i); @@ -509,7 +509,7 @@ void ScEngine::editorCleanup() { ////////////////////////////////////////////////////////////////////////// bool ScEngine::pauseAll() { - for (int i = 0; i < _scripts.getSize(); i++) { + for (uint32 i = 0; i < _scripts.size(); i++) { if (_scripts[i] != _currentScript) { _scripts[i]->pause(); } @@ -521,7 +521,7 @@ bool ScEngine::pauseAll() { ////////////////////////////////////////////////////////////////////////// bool ScEngine::resumeAll() { - for (int i = 0; i < _scripts.getSize(); i++) { + for (uint32 i = 0; i < _scripts.size(); i++) { _scripts[i]->resume(); } @@ -531,7 +531,7 @@ bool ScEngine::resumeAll() { ////////////////////////////////////////////////////////////////////////// bool ScEngine::isValidScript(ScScript *script) { - for (int i = 0; i < _scripts.getSize(); i++) { + for (uint32 i = 0; i < _scripts.size(); i++) { if (_scripts[i] == script) { return true; } diff --git a/engines/wintermute/base/scriptables/script_stack.cpp b/engines/wintermute/base/scriptables/script_stack.cpp index 7e81cdaf17..d27748abe6 100644 --- a/engines/wintermute/base/scriptables/script_stack.cpp +++ b/engines/wintermute/base/scriptables/script_stack.cpp @@ -44,10 +44,10 @@ ScStack::ScStack(BaseGame *inGame) : BaseClass(inGame) { ScStack::~ScStack() { #if _DEBUG - //_gameRef->LOG(0, "STAT: Stack size: %d, SP=%d", _values.getSize(), _sP); + //_gameRef->LOG(0, "STAT: Stack size: %d, SP=%d", _values.size(), _sP); #endif - for (int i = 0; i < _values.getSize(); i++) { + for (uint32 i = 0; i < _values.size(); i++) { delete _values[i]; } _values.clear(); @@ -69,7 +69,7 @@ ScValue *ScStack::pop() { void ScStack::push(ScValue *val) { _sP++; - if (_sP < _values.getSize()) { + if (_sP < (int32)_values.size()) { _values[_sP]->cleanup(); _values[_sP]->copy(val); } else { @@ -84,7 +84,7 @@ void ScStack::push(ScValue *val) { ScValue *ScStack::getPushValue() { _sP++; - if (_sP >= _values.getSize()) { + if (_sP >= (int32)_values.size()) { ScValue *val = new ScValue(_gameRef); _values.add(val); } @@ -96,7 +96,7 @@ ScValue *ScStack::getPushValue() { ////////////////////////////////////////////////////////////////////////// ScValue *ScStack::getTop() { - if (_sP < 0 || _sP >= _values.getSize()) { + if (_sP < 0 || _sP >= (int32)_values.size()) { return NULL; } else { return _values[_sP]; @@ -107,7 +107,7 @@ ScValue *ScStack::getTop() { ////////////////////////////////////////////////////////////////////////// ScValue *ScStack::getAt(int index) { index = _sP - index; - if (index < 0 || index >= _values.getSize()) { + if (index < 0 || index >= (int32)_values.size()) { return NULL; } else { return _values[index]; @@ -136,9 +136,9 @@ void ScStack::correctParams(uint32 expectedParams) { nuParams++; _sP++; - if (_values.getSize() > _sP + 1) { - delete _values[_values.getSize() - 1]; - _values.remove_at(_values.getSize() - 1); + if ((int32)_values.size() > _sP + 1) { + delete _values[_values.size() - 1]; + _values.remove_at(_values.size() - 1); } } } -- cgit v1.2.3 From 158a2060868eceff254460a89b67d78dc650bc94 Mon Sep 17 00:00:00 2001 From: Einar Johan Trøan Sømåen Date: Fri, 31 Aug 2012 19:42:53 +0200 Subject: WINTERMUTE: Use ++it instead of it++ --- engines/wintermute/base/scriptables/script_engine.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'engines/wintermute/base/scriptables') diff --git a/engines/wintermute/base/scriptables/script_engine.cpp b/engines/wintermute/base/scriptables/script_engine.cpp index d518be579f..02f6080958 100644 --- a/engines/wintermute/base/scriptables/script_engine.cpp +++ b/engines/wintermute/base/scriptables/script_engine.cpp @@ -591,7 +591,7 @@ void ScEngine::dumpStats() { TimeVector times; ScriptTimes::iterator it; - for (it = _scriptTimes.begin(); it != _scriptTimes.end(); it++) { + for (it = _scriptTimes.begin(); it != _scriptTimes.end(); ++it) { times.push_back(std::pair (it->_value, it->_key)); } std::sort(times.begin(), times.end()); @@ -602,7 +602,7 @@ void ScEngine::dumpStats() { _gameRef->LOG(0, "***** Script profiling information: *****"); _gameRef->LOG(0, " %-40s %fs", "Total execution time", (float)totalTime / 1000); - for (tit = times.rbegin(); tit != times.rend(); tit++) { + for (tit = times.rbegin(); tit != times.rend(); ++tit) { _gameRef->LOG(0, " %-40s %fs (%f%%)", tit->second.c_str(), (float)tit->first / 1000, (float)tit->first / (float)totalTime * 100); }*/ } -- cgit v1.2.3 From e067520bb904615ed75217808ba27235eb244336 Mon Sep 17 00:00:00 2001 From: Einar Johan Trøan Sømåen Date: Fri, 31 Aug 2012 21:01:53 +0200 Subject: WINTERMUTE: Use tabs in enums --- engines/wintermute/base/scriptables/dcscript.h | 152 ++++++++++++------------- 1 file changed, 76 insertions(+), 76 deletions(-) (limited to 'engines/wintermute/base/scriptables') diff --git a/engines/wintermute/base/scriptables/dcscript.h b/engines/wintermute/base/scriptables/dcscript.h index db63ad2b87..6810fdf665 100644 --- a/engines/wintermute/base/scriptables/dcscript.h +++ b/engines/wintermute/base/scriptables/dcscript.h @@ -36,104 +36,104 @@ namespace Wintermute { // value types typedef enum { - VAL_NULL, - VAL_STRING, - VAL_INT, - VAL_BOOL, - VAL_FLOAT, - VAL_OBJECT, - VAL_NATIVE, - VAL_VARIABLE_REF + VAL_NULL, + VAL_STRING, + VAL_INT, + VAL_BOOL, + VAL_FLOAT, + VAL_OBJECT, + VAL_NATIVE, + VAL_VARIABLE_REF } TValType; // script states typedef enum { - SCRIPT_RUNNING, - SCRIPT_WAITING, - SCRIPT_SLEEPING, - SCRIPT_FINISHED, - SCRIPT_PERSISTENT, - SCRIPT_ERROR, - SCRIPT_PAUSED, - SCRIPT_WAITING_SCRIPT, - SCRIPT_THREAD_FINISHED + SCRIPT_RUNNING, + SCRIPT_WAITING, + SCRIPT_SLEEPING, + SCRIPT_FINISHED, + SCRIPT_PERSISTENT, + SCRIPT_ERROR, + SCRIPT_PAUSED, + SCRIPT_WAITING_SCRIPT, + SCRIPT_THREAD_FINISHED } TScriptState; // opcodes typedef enum { - II_DEF_VAR = 0, - II_DEF_GLOB_VAR, - II_RET, - II_RET_EVENT, - II_CALL, - II_CALL_BY_EXP, - II_EXTERNAL_CALL, - II_SCOPE, - II_CORRECT_STACK, - II_CREATE_OBJECT, - II_POP_EMPTY, - II_PUSH_VAR, - II_PUSH_VAR_REF, - II_POP_VAR, - II_PUSH_VAR_THIS, // push current this on stack - II_PUSH_INT, - II_PUSH_BOOL, - II_PUSH_FLOAT, - II_PUSH_STRING, - II_PUSH_NULL, - II_PUSH_THIS_FROM_STACK, - II_PUSH_THIS, - II_POP_THIS, - II_PUSH_BY_EXP, - II_POP_BY_EXP, - II_JMP, - II_JMP_FALSE, - II_ADD, - II_SUB, - II_MUL, - II_DIV, - II_MODULO, - II_NOT, - II_AND, - II_OR, - II_CMP_EQ, - II_CMP_NE, - II_CMP_L, - II_CMP_G, - II_CMP_LE, - II_CMP_GE, - II_CMP_STRICT_EQ, - II_CMP_STRICT_NE, - II_DBG_LINE, - II_POP_REG1, - II_PUSH_REG1, - II_DEF_CONST_VAR + II_DEF_VAR = 0, + II_DEF_GLOB_VAR, + II_RET, + II_RET_EVENT, + II_CALL, + II_CALL_BY_EXP, + II_EXTERNAL_CALL, + II_SCOPE, + II_CORRECT_STACK, + II_CREATE_OBJECT, + II_POP_EMPTY, + II_PUSH_VAR, + II_PUSH_VAR_REF, + II_POP_VAR, + II_PUSH_VAR_THIS, // push current this on stack + II_PUSH_INT, + II_PUSH_BOOL, + II_PUSH_FLOAT, + II_PUSH_STRING, + II_PUSH_NULL, + II_PUSH_THIS_FROM_STACK, + II_PUSH_THIS, + II_POP_THIS, + II_PUSH_BY_EXP, + II_POP_BY_EXP, + II_JMP, + II_JMP_FALSE, + II_ADD, + II_SUB, + II_MUL, + II_DIV, + II_MODULO, + II_NOT, + II_AND, + II_OR, + II_CMP_EQ, + II_CMP_NE, + II_CMP_L, + II_CMP_G, + II_CMP_LE, + II_CMP_GE, + II_CMP_STRICT_EQ, + II_CMP_STRICT_NE, + II_DBG_LINE, + II_POP_REG1, + II_PUSH_REG1, + II_DEF_CONST_VAR } TInstruction; // external data types typedef enum { - TYPE_VOID = 0, - TYPE_BOOL, - TYPE_LONG, - TYPE_BYTE, - TYPE_STRING, - TYPE_FLOAT, - TYPE_DOUBLE, - TYPE_MEMBUFFER + TYPE_VOID = 0, + TYPE_BOOL, + TYPE_LONG, + TYPE_BYTE, + TYPE_STRING, + TYPE_FLOAT, + TYPE_DOUBLE, + TYPE_MEMBUFFER } TExternalType; // call types typedef enum { - CALL_STDCALL = 0, - CALL_CDECL, - CALL_THISCALL + CALL_STDCALL = 0, + CALL_CDECL, + CALL_THISCALL } TCallType; // element types typedef enum { - ELEMENT_STRING = 0 + ELEMENT_STRING = 0 } TElementType; } // end of namespace Wintermute -- cgit v1.2.3 From 00ad58c29b7ead1f9d18b99d374b3e8e1e62f3d3 Mon Sep 17 00:00:00 2001 From: Filippos Karapetis Date: Tue, 4 Sep 2012 20:10:30 +0300 Subject: WINTERMUTE: Fix a warning with MSVC --- engines/wintermute/base/scriptables/script.cpp | 4 ++-- engines/wintermute/base/scriptables/script.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'engines/wintermute/base/scriptables') diff --git a/engines/wintermute/base/scriptables/script.cpp b/engines/wintermute/base/scriptables/script.cpp index beef4ee9d2..269ea1ea03 100644 --- a/engines/wintermute/base/scriptables/script.cpp +++ b/engines/wintermute/base/scriptables/script.cpp @@ -38,7 +38,7 @@ namespace Wintermute { IMPLEMENT_PERSISTENT(ScScript, false) ////////////////////////////////////////////////////////////////////////// -ScScript::ScScript(BaseGame *inGame, ScEngine *Engine) : BaseClass(inGame) { +ScScript::ScScript(BaseGame *inGame, ScEngine *engine) : BaseClass(inGame) { _buffer = NULL; _bufferSize = _iP = 0; _scriptStream = NULL; @@ -48,7 +48,7 @@ ScScript::ScScript(BaseGame *inGame, ScEngine *Engine) : BaseClass(inGame) { _symbols = NULL; _numSymbols = 0; - _engine = Engine; + _engine = engine; _globals = NULL; diff --git a/engines/wintermute/base/scriptables/script.h b/engines/wintermute/base/scriptables/script.h index 132f1ce6c5..4daeacd026 100644 --- a/engines/wintermute/base/scriptables/script.h +++ b/engines/wintermute/base/scriptables/script.h @@ -138,7 +138,7 @@ private: byte *_buffer; public: Common::SeekableReadStream *_scriptStream; - ScScript(BaseGame *inGame, ScEngine *Engine); + ScScript(BaseGame *inGame, ScEngine *engine); virtual ~ScScript(); char *_filename; bool _thread; -- cgit v1.2.3 From b4090ead4d4334e08725323ff72fd355c93b63d5 Mon Sep 17 00:00:00 2001 From: Willem Jan Palenstijn Date: Tue, 4 Sep 2012 22:17:23 +0200 Subject: WINTERMUTE: Convert CRLF to LF --- engines/wintermute/base/scriptables/dcscript.h | 282 +- engines/wintermute/base/scriptables/script.cpp | 2934 ++++++++++---------- engines/wintermute/base/scriptables/script.h | 348 +-- .../wintermute/base/scriptables/script_engine.cpp | 1220 ++++---- .../wintermute/base/scriptables/script_engine.h | 270 +- .../base/scriptables/script_ext_array.cpp | 504 ++-- .../wintermute/base/scriptables/script_ext_array.h | 112 +- .../base/scriptables/script_ext_date.cpp | 586 ++-- .../wintermute/base/scriptables/script_ext_date.h | 108 +- .../base/scriptables/script_ext_file.cpp | 1658 +++++------ .../wintermute/base/scriptables/script_ext_file.h | 132 +- .../base/scriptables/script_ext_math.cpp | 590 ++-- .../wintermute/base/scriptables/script_ext_math.h | 106 +- .../base/scriptables/script_ext_mem_buffer.cpp | 1058 +++---- .../base/scriptables/script_ext_mem_buffer.h | 120 +- .../base/scriptables/script_ext_object.cpp | 134 +- .../base/scriptables/script_ext_object.h | 92 +- .../base/scriptables/script_ext_string.cpp | 872 +++--- .../base/scriptables/script_ext_string.h | 116 +- .../wintermute/base/scriptables/script_stack.cpp | 464 ++-- engines/wintermute/base/scriptables/script_stack.h | 132 +- .../wintermute/base/scriptables/script_value.cpp | 1990 ++++++------- engines/wintermute/base/scriptables/script_value.h | 226 +- 23 files changed, 7027 insertions(+), 7027 deletions(-) (limited to 'engines/wintermute/base/scriptables') diff --git a/engines/wintermute/base/scriptables/dcscript.h b/engines/wintermute/base/scriptables/dcscript.h index 6810fdf665..4aae897dc2 100644 --- a/engines/wintermute/base/scriptables/dcscript.h +++ b/engines/wintermute/base/scriptables/dcscript.h @@ -1,141 +1,141 @@ -/* ScummVM - Graphic Adventure Engine - * - * ScummVM is the legal property of its developers, whose names - * are too numerous to list here. Please refer to the COPYRIGHT - * file distributed with this source distribution. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - */ - -/* - * This file is based on WME Lite. - * http://dead-code.org/redir.php?target=wmelite - * Copyright (c) 2011 Jan Nedoma - */ - -#ifndef WINTERMUTE_DCSCRIPT_H -#define WINTERMUTE_DCSCRIPT_H - -namespace Wintermute { - -#define SCRIPT_MAGIC 0xDEC0ADDE -#define SCRIPT_VERSION 0x0102 - -// value types -typedef enum { - VAL_NULL, - VAL_STRING, - VAL_INT, - VAL_BOOL, - VAL_FLOAT, - VAL_OBJECT, - VAL_NATIVE, - VAL_VARIABLE_REF -} TValType; - - -// script states -typedef enum { - SCRIPT_RUNNING, - SCRIPT_WAITING, - SCRIPT_SLEEPING, - SCRIPT_FINISHED, - SCRIPT_PERSISTENT, - SCRIPT_ERROR, - SCRIPT_PAUSED, - SCRIPT_WAITING_SCRIPT, - SCRIPT_THREAD_FINISHED -} TScriptState; - -// opcodes -typedef enum { - II_DEF_VAR = 0, - II_DEF_GLOB_VAR, - II_RET, - II_RET_EVENT, - II_CALL, - II_CALL_BY_EXP, - II_EXTERNAL_CALL, - II_SCOPE, - II_CORRECT_STACK, - II_CREATE_OBJECT, - II_POP_EMPTY, - II_PUSH_VAR, - II_PUSH_VAR_REF, - II_POP_VAR, - II_PUSH_VAR_THIS, // push current this on stack - II_PUSH_INT, - II_PUSH_BOOL, - II_PUSH_FLOAT, - II_PUSH_STRING, - II_PUSH_NULL, - II_PUSH_THIS_FROM_STACK, - II_PUSH_THIS, - II_POP_THIS, - II_PUSH_BY_EXP, - II_POP_BY_EXP, - II_JMP, - II_JMP_FALSE, - II_ADD, - II_SUB, - II_MUL, - II_DIV, - II_MODULO, - II_NOT, - II_AND, - II_OR, - II_CMP_EQ, - II_CMP_NE, - II_CMP_L, - II_CMP_G, - II_CMP_LE, - II_CMP_GE, - II_CMP_STRICT_EQ, - II_CMP_STRICT_NE, - II_DBG_LINE, - II_POP_REG1, - II_PUSH_REG1, - II_DEF_CONST_VAR -} TInstruction; - -// external data types -typedef enum { - TYPE_VOID = 0, - TYPE_BOOL, - TYPE_LONG, - TYPE_BYTE, - TYPE_STRING, - TYPE_FLOAT, - TYPE_DOUBLE, - TYPE_MEMBUFFER -} TExternalType; - - -// call types -typedef enum { - CALL_STDCALL = 0, - CALL_CDECL, - CALL_THISCALL -} TCallType; - -// element types -typedef enum { - ELEMENT_STRING = 0 -} TElementType; - -} // end of namespace Wintermute - -#endif +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +/* + * This file is based on WME Lite. + * http://dead-code.org/redir.php?target=wmelite + * Copyright (c) 2011 Jan Nedoma + */ + +#ifndef WINTERMUTE_DCSCRIPT_H +#define WINTERMUTE_DCSCRIPT_H + +namespace Wintermute { + +#define SCRIPT_MAGIC 0xDEC0ADDE +#define SCRIPT_VERSION 0x0102 + +// value types +typedef enum { + VAL_NULL, + VAL_STRING, + VAL_INT, + VAL_BOOL, + VAL_FLOAT, + VAL_OBJECT, + VAL_NATIVE, + VAL_VARIABLE_REF +} TValType; + + +// script states +typedef enum { + SCRIPT_RUNNING, + SCRIPT_WAITING, + SCRIPT_SLEEPING, + SCRIPT_FINISHED, + SCRIPT_PERSISTENT, + SCRIPT_ERROR, + SCRIPT_PAUSED, + SCRIPT_WAITING_SCRIPT, + SCRIPT_THREAD_FINISHED +} TScriptState; + +// opcodes +typedef enum { + II_DEF_VAR = 0, + II_DEF_GLOB_VAR, + II_RET, + II_RET_EVENT, + II_CALL, + II_CALL_BY_EXP, + II_EXTERNAL_CALL, + II_SCOPE, + II_CORRECT_STACK, + II_CREATE_OBJECT, + II_POP_EMPTY, + II_PUSH_VAR, + II_PUSH_VAR_REF, + II_POP_VAR, + II_PUSH_VAR_THIS, // push current this on stack + II_PUSH_INT, + II_PUSH_BOOL, + II_PUSH_FLOAT, + II_PUSH_STRING, + II_PUSH_NULL, + II_PUSH_THIS_FROM_STACK, + II_PUSH_THIS, + II_POP_THIS, + II_PUSH_BY_EXP, + II_POP_BY_EXP, + II_JMP, + II_JMP_FALSE, + II_ADD, + II_SUB, + II_MUL, + II_DIV, + II_MODULO, + II_NOT, + II_AND, + II_OR, + II_CMP_EQ, + II_CMP_NE, + II_CMP_L, + II_CMP_G, + II_CMP_LE, + II_CMP_GE, + II_CMP_STRICT_EQ, + II_CMP_STRICT_NE, + II_DBG_LINE, + II_POP_REG1, + II_PUSH_REG1, + II_DEF_CONST_VAR +} TInstruction; + +// external data types +typedef enum { + TYPE_VOID = 0, + TYPE_BOOL, + TYPE_LONG, + TYPE_BYTE, + TYPE_STRING, + TYPE_FLOAT, + TYPE_DOUBLE, + TYPE_MEMBUFFER +} TExternalType; + + +// call types +typedef enum { + CALL_STDCALL = 0, + CALL_CDECL, + CALL_THISCALL +} TCallType; + +// element types +typedef enum { + ELEMENT_STRING = 0 +} TElementType; + +} // end of namespace Wintermute + +#endif diff --git a/engines/wintermute/base/scriptables/script.cpp b/engines/wintermute/base/scriptables/script.cpp index 269ea1ea03..9469bd46a7 100644 --- a/engines/wintermute/base/scriptables/script.cpp +++ b/engines/wintermute/base/scriptables/script.cpp @@ -1,1467 +1,1467 @@ -/* ScummVM - Graphic Adventure Engine - * - * ScummVM is the legal property of its developers, whose names - * are too numerous to list here. Please refer to the COPYRIGHT - * file distributed with this source distribution. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - */ - -/* - * This file is based on WME Lite. - * http://dead-code.org/redir.php?target=wmelite - * Copyright (c) 2011 Jan Nedoma - */ - -#include "engines/wintermute/base/scriptables/script_value.h" -#include "engines/wintermute/base/scriptables/script.h" -#include "engines/wintermute/base/base_game.h" -#include "engines/wintermute/base/scriptables/script_engine.h" -#include "engines/wintermute/base/scriptables/script_stack.h" -#include "common/memstream.h" - -namespace Wintermute { - -IMPLEMENT_PERSISTENT(ScScript, false) - -////////////////////////////////////////////////////////////////////////// -ScScript::ScScript(BaseGame *inGame, ScEngine *engine) : BaseClass(inGame) { - _buffer = NULL; - _bufferSize = _iP = 0; - _scriptStream = NULL; - _filename = NULL; - _currentLine = 0; - - _symbols = NULL; - _numSymbols = 0; - - _engine = engine; - - _globals = NULL; - - _scopeStack = NULL; - _callStack = NULL; - _thisStack = NULL; - _stack = NULL; - - _operand = NULL; - _reg1 = NULL; - - _functions = NULL; - _numFunctions = 0; - - _methods = NULL; - _numMethods = 0; - - _events = NULL; - _numEvents = 0; - - _externals = NULL; - _numExternals = 0; - - _state = SCRIPT_FINISHED; - _origState = SCRIPT_FINISHED; - - _waitObject = NULL; - _waitTime = 0; - _waitFrozen = false; - _waitScript = NULL; - - _timeSlice = 0; - - _thread = false; - _methodThread = false; - _threadEvent = NULL; - - _freezable = true; - _owner = NULL; - - _unbreakable = false; - _parentScript = NULL; - - _tracingMode = false; -} - - -////////////////////////////////////////////////////////////////////////// -ScScript::~ScScript() { - cleanup(); -} - -void ScScript::readHeader() { - uint32 oldPos = _scriptStream->pos(); - _scriptStream->seek(0); - _header.magic = _scriptStream->readUint32LE(); - _header.version = _scriptStream->readUint32LE(); - _header.codeStart = _scriptStream->readUint32LE(); - _header.funcTable = _scriptStream->readUint32LE(); - _header.symbolTable = _scriptStream->readUint32LE(); - _header.eventTable = _scriptStream->readUint32LE(); - _header.externalsTable = _scriptStream->readUint32LE(); - _header.methodTable = _scriptStream->readUint32LE(); - _scriptStream->seek(oldPos); -} - - -////////////////////////////////////////////////////////////////////////// -bool ScScript::initScript() { - if (!_scriptStream) { - _scriptStream = new Common::MemoryReadStream(_buffer, _bufferSize); - } - readHeader(); - - if (_header.magic != SCRIPT_MAGIC) { - _gameRef->LOG(0, "File '%s' is not a valid compiled script", _filename); - cleanup(); - return STATUS_FAILED; - } - - if (_header.version > SCRIPT_VERSION) { - _gameRef->LOG(0, "Script '%s' has a wrong version %d.%d (expected %d.%d)", _filename, _header.version / 256, _header.version % 256, SCRIPT_VERSION / 256, SCRIPT_VERSION % 256); - cleanup(); - return STATUS_FAILED; - } - - initTables(); - - // init stacks - _scopeStack = new ScStack(_gameRef); - _callStack = new ScStack(_gameRef); - _thisStack = new ScStack(_gameRef); - _stack = new ScStack(_gameRef); - - _operand = new ScValue(_gameRef); - _reg1 = new ScValue(_gameRef); - - - // skip to the beginning - _iP = _header.codeStart; - _scriptStream->seek(_iP); - _currentLine = 0; - - // ready to rumble... - _state = SCRIPT_RUNNING; - - return STATUS_OK; -} - - -////////////////////////////////////////////////////////////////////////// -bool ScScript::initTables() { - uint32 origIP = _iP; - - readHeader(); - // load symbol table - _iP = _header.symbolTable; - - _numSymbols = getDWORD(); - _symbols = new char*[_numSymbols]; - for (uint32 i = 0; i < _numSymbols; i++) { - uint32 index = getDWORD(); - _symbols[index] = getString(); - } - - // load functions table - _iP = _header.funcTable; - - _numFunctions = getDWORD(); - _functions = new TFunctionPos[_numFunctions]; - for (uint32 i = 0; i < _numFunctions; i++) { - _functions[i].pos = getDWORD(); - _functions[i].name = getString(); - } - - - // load events table - _iP = _header.eventTable; - - _numEvents = getDWORD(); - _events = new TEventPos[_numEvents]; - for (uint32 i = 0; i < _numEvents; i++) { - _events[i].pos = getDWORD(); - _events[i].name = getString(); - } - - - // load externals - if (_header.version >= 0x0101) { - _iP = _header.externalsTable; - - _numExternals = getDWORD(); - _externals = new TExternalFunction[_numExternals]; - for (uint32 i = 0; i < _numExternals; i++) { - _externals[i].dll_name = getString(); - _externals[i].name = getString(); - _externals[i].call_type = (TCallType)getDWORD(); - _externals[i].returns = (TExternalType)getDWORD(); - _externals[i].nu_params = getDWORD(); - if (_externals[i].nu_params > 0) { - _externals[i].params = new TExternalType[_externals[i].nu_params]; - for (int j = 0; j < _externals[i].nu_params; j++) { - _externals[i].params[j] = (TExternalType)getDWORD(); - } - } - } - } - - // load method table - _iP = _header.methodTable; - - _numMethods = getDWORD(); - _methods = new TMethodPos[_numMethods]; - for (uint32 i = 0; i < _numMethods; i++) { - _methods[i].pos = getDWORD(); - _methods[i].name = getString(); - } - - - _iP = origIP; - - return STATUS_OK; -} - - -////////////////////////////////////////////////////////////////////////// -bool ScScript::create(const char *filename, byte *buffer, uint32 size, BaseScriptHolder *owner) { - cleanup(); - - _thread = false; - _methodThread = false; - - delete[] _threadEvent; - _threadEvent = NULL; - - _filename = new char[strlen(filename) + 1]; - if (_filename) { - strcpy(_filename, filename); - } - - _buffer = new byte [size]; - if (!_buffer) { - return STATUS_FAILED; - } - - memcpy(_buffer, buffer, size); - - _bufferSize = size; - - bool res = initScript(); - if (DID_FAIL(res)) { - return res; - } - - // establish global variables table - _globals = new ScValue(_gameRef); - - _owner = owner; - - return STATUS_OK; -} - - -////////////////////////////////////////////////////////////////////////// -bool ScScript::createThread(ScScript *original, uint32 initIP, const Common::String &eventName) { - cleanup(); - - _thread = true; - _methodThread = false; - _threadEvent = new char[eventName.size() + 1]; - if (_threadEvent) { - strcpy(_threadEvent, eventName.c_str()); - } - - // copy filename - _filename = new char[strlen(original->_filename) + 1]; - if (_filename) { - strcpy(_filename, original->_filename); - } - - // copy buffer - _buffer = new byte [original->_bufferSize]; - if (!_buffer) { - return STATUS_FAILED; - } - - memcpy(_buffer, original->_buffer, original->_bufferSize); - _bufferSize = original->_bufferSize; - - // initialize - bool res = initScript(); - if (DID_FAIL(res)) { - return res; - } - - // copy globals - _globals = original->_globals; - - // skip to the beginning of the event - _iP = initIP; - _scriptStream->seek(_iP); - - _timeSlice = original->_timeSlice; - _freezable = original->_freezable; - _owner = original->_owner; - - _engine = original->_engine; - _parentScript = original; - - return STATUS_OK; -} - - - - -////////////////////////////////////////////////////////////////////////// -bool ScScript::createMethodThread(ScScript *original, const Common::String &methodName) { - uint32 ip = original->getMethodPos(methodName); - if (ip == 0) { - return STATUS_FAILED; - } - - cleanup(); - - _thread = true; - _methodThread = true; - _threadEvent = new char[methodName.size() + 1]; - if (_threadEvent) { - strcpy(_threadEvent, methodName.c_str()); - } - - // copy filename - _filename = new char[strlen(original->_filename) + 1]; - if (_filename) { - strcpy(_filename, original->_filename); - } - - // copy buffer - _buffer = new byte [original->_bufferSize]; - if (!_buffer) { - return STATUS_FAILED; - } - - memcpy(_buffer, original->_buffer, original->_bufferSize); - _bufferSize = original->_bufferSize; - - // initialize - bool res = initScript(); - if (DID_FAIL(res)) { - return res; - } - - // copy globals - _globals = original->_globals; - - // skip to the beginning of the event - _iP = ip; - - _timeSlice = original->_timeSlice; - _freezable = original->_freezable; - _owner = original->_owner; - - _engine = original->_engine; - _parentScript = original; - - return STATUS_OK; -} - - -////////////////////////////////////////////////////////////////////////// -void ScScript::cleanup() { - if (_buffer) { - delete[] _buffer; - } - _buffer = NULL; - - if (_filename) { - delete[] _filename; - } - _filename = NULL; - - if (_symbols) { - delete[] _symbols; - } - _symbols = NULL; - _numSymbols = 0; - - if (_globals && !_thread) { - delete _globals; - } - _globals = NULL; - - delete _scopeStack; - _scopeStack = NULL; - - delete _callStack; - _callStack = NULL; - - delete _thisStack; - _thisStack = NULL; - - delete _stack; - _stack = NULL; - - if (_functions) { - delete[] _functions; - } - _functions = NULL; - _numFunctions = 0; - - if (_methods) { - delete[] _methods; - } - _methods = NULL; - _numMethods = 0; - - if (_events) { - delete[] _events; - } - _events = NULL; - _numEvents = 0; - - - if (_externals) { - for (uint32 i = 0; i < _numExternals; i++) { - if (_externals[i].nu_params > 0) { - delete[] _externals[i].params; - } - } - delete[] _externals; - } - _externals = NULL; - _numExternals = 0; - - delete _operand; - delete _reg1; - _operand = NULL; - _reg1 = NULL; - - delete[] _threadEvent; - _threadEvent = NULL; - - _state = SCRIPT_FINISHED; - - _waitObject = NULL; - _waitTime = 0; - _waitFrozen = false; - _waitScript = NULL; - - _parentScript = NULL; // ref only - - delete _scriptStream; -} - - -////////////////////////////////////////////////////////////////////////// -uint32 ScScript::getDWORD() { - _scriptStream->seek((int32)_iP); - uint32 ret = _scriptStream->readUint32LE(); - _iP += sizeof(uint32); -// assert(oldRet == ret); - return ret; -} - -////////////////////////////////////////////////////////////////////////// -double ScScript::getFloat() { - _scriptStream->seek((int32)_iP); - byte buffer[8]; - _scriptStream->read(buffer, 8); - -#ifdef SCUMM_BIG_ENDIAN - // TODO: For lack of a READ_LE_UINT64 - SWAP(buffer[0], buffer[7]); - SWAP(buffer[1], buffer[6]); - SWAP(buffer[2], buffer[5]); - SWAP(buffer[3], buffer[4]); -#endif - - double ret = *(double *)(buffer); - _iP += 8; // Hardcode the double-size used originally. - return ret; -} - - -////////////////////////////////////////////////////////////////////////// -char *ScScript::getString() { - char *ret = (char *)(_buffer + _iP); - while (*(char *)(_buffer + _iP) != '\0') { - _iP++; - } - _iP++; // string terminator - _scriptStream->seek(_iP); - - return ret; -} - - -////////////////////////////////////////////////////////////////////////// -bool ScScript::executeInstruction() { - bool ret = STATUS_OK; - - uint32 dw; - const char *str = NULL; - - //ScValue* op = new ScValue(_gameRef); - _operand->cleanup(); - - ScValue *op1; - ScValue *op2; - - uint32 inst = getDWORD(); - switch (inst) { - - case II_DEF_VAR: - _operand->setNULL(); - dw = getDWORD(); - if (_scopeStack->_sP < 0) { - _globals->setProp(_symbols[dw], _operand); - } else { - _scopeStack->getTop()->setProp(_symbols[dw], _operand); - } - - break; - - case II_DEF_GLOB_VAR: - case II_DEF_CONST_VAR: { - dw = getDWORD(); - /* char *temp = _symbols[dw]; // TODO delete */ - // only create global var if it doesn't exist - if (!_engine->_globals->propExists(_symbols[dw])) { - _operand->setNULL(); - _engine->_globals->setProp(_symbols[dw], _operand, false, inst == II_DEF_CONST_VAR); - } - break; - } - - case II_RET: - if (_scopeStack->_sP >= 0 && _callStack->_sP >= 0) { - _scopeStack->pop(); - _iP = (uint32)_callStack->pop()->getInt(); - } else { - if (_thread) { - _state = SCRIPT_THREAD_FINISHED; - } else { - if (_numEvents == 0 && _numMethods == 0) { - _state = SCRIPT_FINISHED; - } else { - _state = SCRIPT_PERSISTENT; - } - } - } - - break; - - case II_RET_EVENT: - _state = SCRIPT_FINISHED; - break; - - - case II_CALL: - dw = getDWORD(); - - _operand->setInt(_iP); - _callStack->push(_operand); - - _iP = dw; - - break; - - case II_CALL_BY_EXP: { - // push var - // push string - str = _stack->pop()->getString(); - char *methodName = new char[strlen(str) + 1]; - strcpy(methodName, str); - - ScValue *var = _stack->pop(); - if (var->_type == VAL_VARIABLE_REF) { - var = var->_valRef; - } - - bool res = STATUS_FAILED; - bool triedNative = false; - - // we are already calling this method, try native - if (_thread && _methodThread && strcmp(methodName, _threadEvent) == 0 && var->_type == VAL_NATIVE && _owner == var->getNative()) { - triedNative = true; - res = var->_valNative->scCallMethod(this, _stack, _thisStack, methodName); - } - - if (DID_FAIL(res)) { - if (var->isNative() && var->getNative()->canHandleMethod(methodName)) { - if (!_unbreakable) { - _waitScript = var->getNative()->invokeMethodThread(methodName); - if (!_waitScript) { - _stack->correctParams(0); - runtimeError("Error invoking method '%s'.", methodName); - _stack->pushNULL(); - } else { - _state = SCRIPT_WAITING_SCRIPT; - _waitScript->copyParameters(_stack); - } - } else { - // can call methods in unbreakable mode - _stack->correctParams(0); - runtimeError("Cannot call method '%s'. Ignored.", methodName); - _stack->pushNULL(); - } - delete[] methodName; - break; - } - /* - ScValue* val = var->getProp(MethodName); - if (val){ - dw = GetFuncPos(val->getString()); - if (dw==0){ - TExternalFunction* f = GetExternal(val->getString()); - if (f){ - ExternalCall(_stack, _thisStack, f); - } - else{ - // not an internal nor external, try for native function - _gameRef->ExternalCall(this, _stack, _thisStack, val->getString()); - } - } - else{ - _operand->setInt(_iP); - _callStack->Push(_operand); - _iP = dw; - } - } - */ - else { - res = STATUS_FAILED; - if (var->_type == VAL_NATIVE && !triedNative) { - res = var->_valNative->scCallMethod(this, _stack, _thisStack, methodName); - } - - if (DID_FAIL(res)) { - _stack->correctParams(0); - runtimeError("Call to undefined method '%s'. Ignored.", methodName); - _stack->pushNULL(); - } - } - } - delete[] methodName; - } - break; - - case II_EXTERNAL_CALL: { - uint32 symbolIndex = getDWORD(); - - TExternalFunction *f = getExternal(_symbols[symbolIndex]); - if (f) { - externalCall(_stack, _thisStack, f); - } else { - _gameRef->externalCall(this, _stack, _thisStack, _symbols[symbolIndex]); - } - - break; - } - case II_SCOPE: - _operand->setNULL(); - _scopeStack->push(_operand); - break; - - case II_CORRECT_STACK: - dw = getDWORD(); // params expected - _stack->correctParams(dw); - break; - - case II_CREATE_OBJECT: - _operand->setObject(); - _stack->push(_operand); - break; - - case II_POP_EMPTY: - _stack->pop(); - break; - - case II_PUSH_VAR: { - ScValue *var = getVar(_symbols[getDWORD()]); - if (false && /*var->_type==VAL_OBJECT ||*/ var->_type == VAL_NATIVE) { - _operand->setReference(var); - _stack->push(_operand); - } else { - _stack->push(var); - } - break; - } - - case II_PUSH_VAR_REF: { - ScValue *var = getVar(_symbols[getDWORD()]); - _operand->setReference(var); - _stack->push(_operand); - break; - } - - case II_POP_VAR: { - char *varName = _symbols[getDWORD()]; - ScValue *var = getVar(varName); - if (var) { - ScValue *val = _stack->pop(); - if (!val) { - runtimeError("Script stack corruption detected. Please report this script at WME bug reports forum."); - var->setNULL(); - } else { - if (val->getType() == VAL_VARIABLE_REF) { - val = val->_valRef; - } - if (val->_type == VAL_NATIVE) { - var->setValue(val); - } else { - var->copy(val); - } - } - } - - break; - } - - case II_PUSH_VAR_THIS: - _stack->push(_thisStack->getTop()); - break; - - case II_PUSH_INT: - _stack->pushInt((int)getDWORD()); - break; - - case II_PUSH_FLOAT: - _stack->pushFloat(getFloat()); - break; - - - case II_PUSH_BOOL: - _stack->pushBool(getDWORD() != 0); - - break; - - case II_PUSH_STRING: - _stack->pushString(getString()); - break; - - case II_PUSH_NULL: - _stack->pushNULL(); - break; - - case II_PUSH_THIS_FROM_STACK: - _operand->setReference(_stack->getTop()); - _thisStack->push(_operand); - break; - - case II_PUSH_THIS: - _operand->setReference(getVar(_symbols[getDWORD()])); - _thisStack->push(_operand); - break; - - case II_POP_THIS: - _thisStack->pop(); - break; - - case II_PUSH_BY_EXP: { - str = _stack->pop()->getString(); - ScValue *val = _stack->pop()->getProp(str); - if (val) { - _stack->push(val); - } else { - _stack->pushNULL(); - } - - break; - } - - case II_POP_BY_EXP: { - str = _stack->pop()->getString(); - ScValue *var = _stack->pop(); - ScValue *val = _stack->pop(); - - if (val == NULL) { - runtimeError("Script stack corruption detected. Please report this script at WME bug reports forum."); - var->setNULL(); - } else { - var->setProp(str, val); - } - - break; - } - - case II_PUSH_REG1: - _stack->push(_reg1); - break; - - case II_POP_REG1: - _reg1->copy(_stack->pop()); - break; - - case II_JMP: - _iP = getDWORD(); - break; - - case II_JMP_FALSE: { - dw = getDWORD(); - //if (!_stack->pop()->getBool()) _iP = dw; - ScValue *val = _stack->pop(); - if (!val) { - runtimeError("Script corruption detected. Did you use '=' instead of '==' for comparison?"); - } else { - if (!val->getBool()) { - _iP = dw; - } - } - break; - } - - case II_ADD: - op2 = _stack->pop(); - op1 = _stack->pop(); - - if (op1->isNULL() || op2->isNULL()) { - _operand->setNULL(); - } else if (op1->getType() == VAL_STRING || op2->getType() == VAL_STRING) { - char *tempStr = new char [strlen(op1->getString()) + strlen(op2->getString()) + 1]; - strcpy(tempStr, op1->getString()); - strcat(tempStr, op2->getString()); - _operand->setString(tempStr); - delete[] tempStr; - } else if (op1->getType() == VAL_INT && op2->getType() == VAL_INT) { - _operand->setInt(op1->getInt() + op2->getInt()); - } else { - _operand->setFloat(op1->getFloat() + op2->getFloat()); - } - - _stack->push(_operand); - - break; - - case II_SUB: - op2 = _stack->pop(); - op1 = _stack->pop(); - - if (op1->isNULL() || op2->isNULL()) { - _operand->setNULL(); - } else if (op1->getType() == VAL_INT && op2->getType() == VAL_INT) { - _operand->setInt(op1->getInt() - op2->getInt()); - } else { - _operand->setFloat(op1->getFloat() - op2->getFloat()); - } - - _stack->push(_operand); - - break; - - case II_MUL: - op2 = _stack->pop(); - op1 = _stack->pop(); - - if (op1->isNULL() || op2->isNULL()) { - _operand->setNULL(); - } else if (op1->getType() == VAL_INT && op2->getType() == VAL_INT) { - _operand->setInt(op1->getInt() * op2->getInt()); - } else { - _operand->setFloat(op1->getFloat() * op2->getFloat()); - } - - _stack->push(_operand); - - break; - - case II_DIV: - op2 = _stack->pop(); - op1 = _stack->pop(); - - if (op2->getFloat() == 0.0f) { - runtimeError("Division by zero."); - } - - if (op1->isNULL() || op2->isNULL() || op2->getFloat() == 0.0f) { - _operand->setNULL(); - } else { - _operand->setFloat(op1->getFloat() / op2->getFloat()); - } - - _stack->push(_operand); - - break; - - case II_MODULO: - op2 = _stack->pop(); - op1 = _stack->pop(); - - if (op2->getInt() == 0) { - runtimeError("Division by zero."); - } - - if (op1->isNULL() || op2->isNULL() || op2->getInt() == 0) { - _operand->setNULL(); - } else { - _operand->setInt(op1->getInt() % op2->getInt()); - } - - _stack->push(_operand); - - break; - - case II_NOT: - op1 = _stack->pop(); - //if (op1->isNULL()) _operand->setNULL(); - if (op1->isNULL()) { - _operand->setBool(true); - } else { - _operand->setBool(!op1->getBool()); - } - _stack->push(_operand); - - break; - - case II_AND: - op2 = _stack->pop(); - op1 = _stack->pop(); - if (op1 == NULL || op2 == NULL) { - runtimeError("Script corruption detected. Did you use '=' instead of '==' for comparison?"); - _operand->setBool(false); - } else { - _operand->setBool(op1->getBool() && op2->getBool()); - } - _stack->push(_operand); - break; - - case II_OR: - op2 = _stack->pop(); - op1 = _stack->pop(); - if (op1 == NULL || op2 == NULL) { - runtimeError("Script corruption detected. Did you use '=' instead of '==' for comparison?"); - _operand->setBool(false); - } else { - _operand->setBool(op1->getBool() || op2->getBool()); - } - _stack->push(_operand); - break; - - case II_CMP_EQ: - op2 = _stack->pop(); - op1 = _stack->pop(); - - /* - if ((op1->isNULL() && !op2->isNULL()) || (!op1->isNULL() && op2->isNULL())) _operand->setBool(false); - else if (op1->isNative() && op2->isNative()){ - _operand->setBool(op1->getNative() == op2->getNative()); - } - else if (op1->getType()==VAL_STRING || op2->getType()==VAL_STRING){ - _operand->setBool(scumm_stricmp(op1->getString(), op2->getString())==0); - } - else if (op1->getType()==VAL_FLOAT && op2->getType()==VAL_FLOAT){ - _operand->setBool(op1->getFloat() == op2->getFloat()); - } - else{ - _operand->setBool(op1->getInt() == op2->getInt()); - } - */ - - _operand->setBool(ScValue::compare(op1, op2) == 0); - _stack->push(_operand); - break; - - case II_CMP_NE: - op2 = _stack->pop(); - op1 = _stack->pop(); - - /* - if ((op1->isNULL() && !op2->isNULL()) || (!op1->isNULL() && op2->isNULL())) _operand->setBool(true); - else if (op1->isNative() && op2->isNative()){ - _operand->setBool(op1->getNative() != op2->getNative()); - } - else if (op1->getType()==VAL_STRING || op2->getType()==VAL_STRING){ - _operand->setBool(scumm_stricmp(op1->getString(), op2->getString())!=0); - } - else if (op1->getType()==VAL_FLOAT && op2->getType()==VAL_FLOAT){ - _operand->setBool(op1->getFloat() != op2->getFloat()); - } - else{ - _operand->setBool(op1->getInt() != op2->getInt()); - } - */ - - _operand->setBool(ScValue::compare(op1, op2) != 0); - _stack->push(_operand); - break; - - case II_CMP_L: - op2 = _stack->pop(); - op1 = _stack->pop(); - - /* - if (op1->getType()==VAL_FLOAT && op2->getType()==VAL_FLOAT){ - _operand->setBool(op1->getFloat() < op2->getFloat()); - } - else _operand->setBool(op1->getInt() < op2->getInt()); - */ - - _operand->setBool(ScValue::compare(op1, op2) < 0); - _stack->push(_operand); - break; - - case II_CMP_G: - op2 = _stack->pop(); - op1 = _stack->pop(); - - /* - if (op1->getType()==VAL_FLOAT && op2->getType()==VAL_FLOAT){ - _operand->setBool(op1->getFloat() > op2->getFloat()); - } - else _operand->setBool(op1->getInt() > op2->getInt()); - */ - - _operand->setBool(ScValue::compare(op1, op2) > 0); - _stack->push(_operand); - break; - - case II_CMP_LE: - op2 = _stack->pop(); - op1 = _stack->pop(); - - /* - if (op1->getType()==VAL_FLOAT && op2->getType()==VAL_FLOAT){ - _operand->setBool(op1->getFloat() <= op2->getFloat()); - } - else _operand->setBool(op1->getInt() <= op2->getInt()); - */ - - _operand->setBool(ScValue::compare(op1, op2) <= 0); - _stack->push(_operand); - break; - - case II_CMP_GE: - op2 = _stack->pop(); - op1 = _stack->pop(); - - /* - if (op1->getType()==VAL_FLOAT && op2->getType()==VAL_FLOAT){ - _operand->setBool(op1->getFloat() >= op2->getFloat()); - } - else _operand->setBool(op1->getInt() >= op2->getInt()); - */ - - _operand->setBool(ScValue::compare(op1, op2) >= 0); - _stack->push(_operand); - break; - - case II_CMP_STRICT_EQ: - op2 = _stack->pop(); - op1 = _stack->pop(); - - //_operand->setBool(op1->getType()==op2->getType() && op1->getFloat()==op2->getFloat()); - _operand->setBool(ScValue::compareStrict(op1, op2) == 0); - _stack->push(_operand); - - break; - - case II_CMP_STRICT_NE: - op2 = _stack->pop(); - op1 = _stack->pop(); - - //_operand->setBool(op1->getType()!=op2->getType() || op1->getFloat()!=op2->getFloat()); - _operand->setBool(ScValue::compareStrict(op1, op2) != 0); - _stack->push(_operand); - break; - - case II_DBG_LINE: { - int newLine = getDWORD(); - if (newLine != _currentLine) { - _currentLine = newLine; - } - break; - - } - default: - _gameRef->LOG(0, "Fatal: Invalid instruction %d ('%s', line %d, IP:0x%x)\n", inst, _filename, _currentLine, _iP - sizeof(uint32)); - _state = SCRIPT_FINISHED; - ret = STATUS_FAILED; - } // switch(instruction) - - //delete op; - - return ret; -} - - -////////////////////////////////////////////////////////////////////////// -uint32 ScScript::getFuncPos(const Common::String &name) { - for (uint32 i = 0; i < _numFunctions; i++) { - if (name == _functions[i].name) { - return _functions[i].pos; - } - } - return 0; -} - - -////////////////////////////////////////////////////////////////////////// -uint32 ScScript::getMethodPos(const Common::String &name) { - for (uint32 i = 0; i < _numMethods; i++) { - if (name == _methods[i].name) { - return _methods[i].pos; - } - } - return 0; -} - - -////////////////////////////////////////////////////////////////////////// -ScValue *ScScript::getVar(char *name) { - ScValue *ret = NULL; - - // scope locals - if (_scopeStack->_sP >= 0) { - if (_scopeStack->getTop()->propExists(name)) { - ret = _scopeStack->getTop()->getProp(name); - } - } - - // script globals - if (ret == NULL) { - if (_globals->propExists(name)) { - ret = _globals->getProp(name); - } - } - - // engine globals - if (ret == NULL) { - if (_engine->_globals->propExists(name)) { - ret = _engine->_globals->getProp(name); - } - } - - if (ret == NULL) { - //RuntimeError("Variable '%s' is inaccessible in the current block. Consider changing the script.", name); - _gameRef->LOG(0, "Warning: variable '%s' is inaccessible in the current block. Consider changing the script (script:%s, line:%d)", name, _filename, _currentLine); - ScValue *val = new ScValue(_gameRef); - ScValue *scope = _scopeStack->getTop(); - if (scope) { - scope->setProp(name, val); - ret = _scopeStack->getTop()->getProp(name); - } else { - _globals->setProp(name, val); - ret = _globals->getProp(name); - } - delete val; - } - - return ret; -} - - -////////////////////////////////////////////////////////////////////////// -bool ScScript::waitFor(BaseObject *object) { - if (_unbreakable) { - runtimeError("Script cannot be interrupted."); - return STATUS_OK; - } - - _state = SCRIPT_WAITING; - _waitObject = object; - return STATUS_OK; -} - - -////////////////////////////////////////////////////////////////////////// -bool ScScript::waitForExclusive(BaseObject *object) { - _engine->resetObject(object); - return waitFor(object); -} - - -////////////////////////////////////////////////////////////////////////// -bool ScScript::sleep(uint32 duration) { - if (_unbreakable) { - runtimeError("Script cannot be interrupted."); - return STATUS_OK; - } - - _state = SCRIPT_SLEEPING; - if (_gameRef->_state == GAME_FROZEN) { - _waitTime = g_system->getMillis() + duration; - _waitFrozen = true; - } else { - _waitTime = _gameRef->_timer + duration; - _waitFrozen = false; - } - return STATUS_OK; -} - - -////////////////////////////////////////////////////////////////////////// -bool ScScript::finish(bool includingThreads) { - if (_state != SCRIPT_FINISHED && includingThreads) { - _state = SCRIPT_FINISHED; - finishThreads(); - } else { - _state = SCRIPT_FINISHED; - } - - - return STATUS_OK; -} - - -////////////////////////////////////////////////////////////////////////// -bool ScScript::run() { - _state = SCRIPT_RUNNING; - return STATUS_OK; -} - - -////////////////////////////////////////////////////////////////////// -void ScScript::runtimeError(const char *fmt, ...) { - char buff[256]; - va_list va; - - va_start(va, fmt); - vsprintf(buff, fmt, va); - va_end(va); - - _gameRef->LOG(0, "Runtime error. Script '%s', line %d", _filename, _currentLine); - _gameRef->LOG(0, " %s", buff); - - if (!_gameRef->_suppressScriptErrors) { - _gameRef->quickMessage("Script runtime error. View log for details."); - } -} - - -////////////////////////////////////////////////////////////////////////// -bool ScScript::persist(BasePersistenceManager *persistMgr) { - - persistMgr->transfer(TMEMBER(_gameRef)); - - // buffer - if (persistMgr->getIsSaving()) { - if (_state != SCRIPT_PERSISTENT && _state != SCRIPT_FINISHED && _state != SCRIPT_THREAD_FINISHED) { - persistMgr->transfer(TMEMBER(_bufferSize)); - persistMgr->putBytes(_buffer, _bufferSize); - } else { - // don't save idle/finished scripts - int bufferSize = 0; - persistMgr->transfer(TMEMBER(bufferSize)); - } - } else { - persistMgr->transfer(TMEMBER(_bufferSize)); - if (_bufferSize > 0) { - _buffer = new byte[_bufferSize]; - persistMgr->getBytes(_buffer, _bufferSize); - _scriptStream = new Common::MemoryReadStream(_buffer, _bufferSize); - initTables(); - } else { - _buffer = NULL; - _scriptStream = NULL; - } - } - - persistMgr->transfer(TMEMBER(_callStack)); - persistMgr->transfer(TMEMBER(_currentLine)); - persistMgr->transfer(TMEMBER(_engine)); - persistMgr->transfer(TMEMBER(_filename)); - persistMgr->transfer(TMEMBER(_freezable)); - persistMgr->transfer(TMEMBER(_globals)); - persistMgr->transfer(TMEMBER(_iP)); - persistMgr->transfer(TMEMBER(_scopeStack)); - persistMgr->transfer(TMEMBER(_stack)); - persistMgr->transfer(TMEMBER_INT(_state)); - persistMgr->transfer(TMEMBER(_operand)); - persistMgr->transfer(TMEMBER_INT(_origState)); - persistMgr->transfer(TMEMBER(_owner)); - persistMgr->transfer(TMEMBER(_reg1)); - persistMgr->transfer(TMEMBER(_thread)); - persistMgr->transfer(TMEMBER(_threadEvent)); - persistMgr->transfer(TMEMBER(_thisStack)); - persistMgr->transfer(TMEMBER(_timeSlice)); - persistMgr->transfer(TMEMBER(_waitObject)); - persistMgr->transfer(TMEMBER(_waitScript)); - persistMgr->transfer(TMEMBER(_waitTime)); - persistMgr->transfer(TMEMBER(_waitFrozen)); - - persistMgr->transfer(TMEMBER(_methodThread)); - persistMgr->transfer(TMEMBER(_methodThread)); - persistMgr->transfer(TMEMBER(_unbreakable)); - persistMgr->transfer(TMEMBER(_parentScript)); - - if (!persistMgr->getIsSaving()) { - _tracingMode = false; - } - - return STATUS_OK; -} - - -////////////////////////////////////////////////////////////////////////// -ScScript *ScScript::invokeEventHandler(const Common::String &eventName, bool unbreakable) { - //if (_state!=SCRIPT_PERSISTENT) return NULL; - - uint32 pos = getEventPos(eventName); - if (!pos) { - return NULL; - } - - ScScript *thread = new ScScript(_gameRef, _engine); - if (thread) { - bool ret = thread->createThread(this, pos, eventName); - if (DID_SUCCEED(ret)) { - thread->_unbreakable = unbreakable; - _engine->_scripts.add(thread); - return thread; - } else { - delete thread; - return NULL; - } - } else { - return NULL; - } - -} - - -////////////////////////////////////////////////////////////////////////// -uint32 ScScript::getEventPos(const Common::String &name) { - for (int i = _numEvents - 1; i >= 0; i--) { - if (scumm_stricmp(name.c_str(), _events[i].name) == 0) { - return _events[i].pos; - } - } - return 0; -} - - -////////////////////////////////////////////////////////////////////////// -bool ScScript::canHandleEvent(const Common::String &eventName) { - return getEventPos(eventName) != 0; -} - - -////////////////////////////////////////////////////////////////////////// -bool ScScript::canHandleMethod(const Common::String &methodName) { - return getMethodPos(methodName) != 0; -} - - -////////////////////////////////////////////////////////////////////////// -bool ScScript::pause() { - if (_state == SCRIPT_PAUSED) { - _gameRef->LOG(0, "Attempting to pause a paused script ('%s', line %d)", _filename, _currentLine); - return STATUS_FAILED; - } - - if (!_freezable || _state == SCRIPT_PERSISTENT) { - return STATUS_OK; - } - - _origState = _state; - _state = SCRIPT_PAUSED; - - return STATUS_OK; -} - - -////////////////////////////////////////////////////////////////////////// -bool ScScript::resume() { - if (_state != SCRIPT_PAUSED) { - return STATUS_OK; - } - - _state = _origState; - return STATUS_OK; -} - - -////////////////////////////////////////////////////////////////////////// -ScScript::TExternalFunction *ScScript::getExternal(char *name) { - for (uint32 i = 0; i < _numExternals; i++) { - if (strcmp(name, _externals[i].name) == 0) { - return &_externals[i]; - } - } - return NULL; -} - - -////////////////////////////////////////////////////////////////////////// -bool ScScript::externalCall(ScStack *stack, ScStack *thisStack, ScScript::TExternalFunction *function) { - - _gameRef->LOG(0, "External functions are not supported on this platform."); - stack->correctParams(0); - stack->pushNULL(); - return STATUS_FAILED; -} - - -////////////////////////////////////////////////////////////////////////// -bool ScScript::copyParameters(ScStack *stack) { - int i; - int numParams = stack->pop()->getInt(); - for (i = numParams - 1; i >= 0; i--) { - _stack->push(stack->getAt(i)); - } - _stack->pushInt(numParams); - - for (i = 0; i < numParams; i++) { - stack->pop(); - } - - return STATUS_OK; -} - - -////////////////////////////////////////////////////////////////////////// -bool ScScript::finishThreads() { - for (uint32 i = 0; i < _engine->_scripts.size(); i++) { - ScScript *scr = _engine->_scripts[i]; - if (scr->_thread && scr->_state != SCRIPT_FINISHED && scr->_owner == _owner && scumm_stricmp(scr->_filename, _filename) == 0) { - scr->finish(true); - } - } - return STATUS_OK; -} - - -////////////////////////////////////////////////////////////////////////// -// IWmeDebugScript interface implementation -int ScScript::dbgGetLine() { - return _currentLine; -} - -////////////////////////////////////////////////////////////////////////// -const char *ScScript::dbgGetFilename() { - return _filename; -} - -////////////////////////////////////////////////////////////////////////// -void ScScript::afterLoad() { - if (_buffer == NULL) { - byte *buffer = _engine->getCompiledScript(_filename, &_bufferSize); - if (!buffer) { - _gameRef->LOG(0, "Error reinitializing script '%s' after load. Script will be terminated.", _filename); - _state = SCRIPT_ERROR; - return; - } - - _buffer = new byte [_bufferSize]; - memcpy(_buffer, buffer, _bufferSize); - - delete _scriptStream; - _scriptStream = new Common::MemoryReadStream(_buffer, _bufferSize); - - initTables(); - } -} - -} // end of namespace Wintermute +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +/* + * This file is based on WME Lite. + * http://dead-code.org/redir.php?target=wmelite + * Copyright (c) 2011 Jan Nedoma + */ + +#include "engines/wintermute/base/scriptables/script_value.h" +#include "engines/wintermute/base/scriptables/script.h" +#include "engines/wintermute/base/base_game.h" +#include "engines/wintermute/base/scriptables/script_engine.h" +#include "engines/wintermute/base/scriptables/script_stack.h" +#include "common/memstream.h" + +namespace Wintermute { + +IMPLEMENT_PERSISTENT(ScScript, false) + +////////////////////////////////////////////////////////////////////////// +ScScript::ScScript(BaseGame *inGame, ScEngine *engine) : BaseClass(inGame) { + _buffer = NULL; + _bufferSize = _iP = 0; + _scriptStream = NULL; + _filename = NULL; + _currentLine = 0; + + _symbols = NULL; + _numSymbols = 0; + + _engine = engine; + + _globals = NULL; + + _scopeStack = NULL; + _callStack = NULL; + _thisStack = NULL; + _stack = NULL; + + _operand = NULL; + _reg1 = NULL; + + _functions = NULL; + _numFunctions = 0; + + _methods = NULL; + _numMethods = 0; + + _events = NULL; + _numEvents = 0; + + _externals = NULL; + _numExternals = 0; + + _state = SCRIPT_FINISHED; + _origState = SCRIPT_FINISHED; + + _waitObject = NULL; + _waitTime = 0; + _waitFrozen = false; + _waitScript = NULL; + + _timeSlice = 0; + + _thread = false; + _methodThread = false; + _threadEvent = NULL; + + _freezable = true; + _owner = NULL; + + _unbreakable = false; + _parentScript = NULL; + + _tracingMode = false; +} + + +////////////////////////////////////////////////////////////////////////// +ScScript::~ScScript() { + cleanup(); +} + +void ScScript::readHeader() { + uint32 oldPos = _scriptStream->pos(); + _scriptStream->seek(0); + _header.magic = _scriptStream->readUint32LE(); + _header.version = _scriptStream->readUint32LE(); + _header.codeStart = _scriptStream->readUint32LE(); + _header.funcTable = _scriptStream->readUint32LE(); + _header.symbolTable = _scriptStream->readUint32LE(); + _header.eventTable = _scriptStream->readUint32LE(); + _header.externalsTable = _scriptStream->readUint32LE(); + _header.methodTable = _scriptStream->readUint32LE(); + _scriptStream->seek(oldPos); +} + + +////////////////////////////////////////////////////////////////////////// +bool ScScript::initScript() { + if (!_scriptStream) { + _scriptStream = new Common::MemoryReadStream(_buffer, _bufferSize); + } + readHeader(); + + if (_header.magic != SCRIPT_MAGIC) { + _gameRef->LOG(0, "File '%s' is not a valid compiled script", _filename); + cleanup(); + return STATUS_FAILED; + } + + if (_header.version > SCRIPT_VERSION) { + _gameRef->LOG(0, "Script '%s' has a wrong version %d.%d (expected %d.%d)", _filename, _header.version / 256, _header.version % 256, SCRIPT_VERSION / 256, SCRIPT_VERSION % 256); + cleanup(); + return STATUS_FAILED; + } + + initTables(); + + // init stacks + _scopeStack = new ScStack(_gameRef); + _callStack = new ScStack(_gameRef); + _thisStack = new ScStack(_gameRef); + _stack = new ScStack(_gameRef); + + _operand = new ScValue(_gameRef); + _reg1 = new ScValue(_gameRef); + + + // skip to the beginning + _iP = _header.codeStart; + _scriptStream->seek(_iP); + _currentLine = 0; + + // ready to rumble... + _state = SCRIPT_RUNNING; + + return STATUS_OK; +} + + +////////////////////////////////////////////////////////////////////////// +bool ScScript::initTables() { + uint32 origIP = _iP; + + readHeader(); + // load symbol table + _iP = _header.symbolTable; + + _numSymbols = getDWORD(); + _symbols = new char*[_numSymbols]; + for (uint32 i = 0; i < _numSymbols; i++) { + uint32 index = getDWORD(); + _symbols[index] = getString(); + } + + // load functions table + _iP = _header.funcTable; + + _numFunctions = getDWORD(); + _functions = new TFunctionPos[_numFunctions]; + for (uint32 i = 0; i < _numFunctions; i++) { + _functions[i].pos = getDWORD(); + _functions[i].name = getString(); + } + + + // load events table + _iP = _header.eventTable; + + _numEvents = getDWORD(); + _events = new TEventPos[_numEvents]; + for (uint32 i = 0; i < _numEvents; i++) { + _events[i].pos = getDWORD(); + _events[i].name = getString(); + } + + + // load externals + if (_header.version >= 0x0101) { + _iP = _header.externalsTable; + + _numExternals = getDWORD(); + _externals = new TExternalFunction[_numExternals]; + for (uint32 i = 0; i < _numExternals; i++) { + _externals[i].dll_name = getString(); + _externals[i].name = getString(); + _externals[i].call_type = (TCallType)getDWORD(); + _externals[i].returns = (TExternalType)getDWORD(); + _externals[i].nu_params = getDWORD(); + if (_externals[i].nu_params > 0) { + _externals[i].params = new TExternalType[_externals[i].nu_params]; + for (int j = 0; j < _externals[i].nu_params; j++) { + _externals[i].params[j] = (TExternalType)getDWORD(); + } + } + } + } + + // load method table + _iP = _header.methodTable; + + _numMethods = getDWORD(); + _methods = new TMethodPos[_numMethods]; + for (uint32 i = 0; i < _numMethods; i++) { + _methods[i].pos = getDWORD(); + _methods[i].name = getString(); + } + + + _iP = origIP; + + return STATUS_OK; +} + + +////////////////////////////////////////////////////////////////////////// +bool ScScript::create(const char *filename, byte *buffer, uint32 size, BaseScriptHolder *owner) { + cleanup(); + + _thread = false; + _methodThread = false; + + delete[] _threadEvent; + _threadEvent = NULL; + + _filename = new char[strlen(filename) + 1]; + if (_filename) { + strcpy(_filename, filename); + } + + _buffer = new byte [size]; + if (!_buffer) { + return STATUS_FAILED; + } + + memcpy(_buffer, buffer, size); + + _bufferSize = size; + + bool res = initScript(); + if (DID_FAIL(res)) { + return res; + } + + // establish global variables table + _globals = new ScValue(_gameRef); + + _owner = owner; + + return STATUS_OK; +} + + +////////////////////////////////////////////////////////////////////////// +bool ScScript::createThread(ScScript *original, uint32 initIP, const Common::String &eventName) { + cleanup(); + + _thread = true; + _methodThread = false; + _threadEvent = new char[eventName.size() + 1]; + if (_threadEvent) { + strcpy(_threadEvent, eventName.c_str()); + } + + // copy filename + _filename = new char[strlen(original->_filename) + 1]; + if (_filename) { + strcpy(_filename, original->_filename); + } + + // copy buffer + _buffer = new byte [original->_bufferSize]; + if (!_buffer) { + return STATUS_FAILED; + } + + memcpy(_buffer, original->_buffer, original->_bufferSize); + _bufferSize = original->_bufferSize; + + // initialize + bool res = initScript(); + if (DID_FAIL(res)) { + return res; + } + + // copy globals + _globals = original->_globals; + + // skip to the beginning of the event + _iP = initIP; + _scriptStream->seek(_iP); + + _timeSlice = original->_timeSlice; + _freezable = original->_freezable; + _owner = original->_owner; + + _engine = original->_engine; + _parentScript = original; + + return STATUS_OK; +} + + + + +////////////////////////////////////////////////////////////////////////// +bool ScScript::createMethodThread(ScScript *original, const Common::String &methodName) { + uint32 ip = original->getMethodPos(methodName); + if (ip == 0) { + return STATUS_FAILED; + } + + cleanup(); + + _thread = true; + _methodThread = true; + _threadEvent = new char[methodName.size() + 1]; + if (_threadEvent) { + strcpy(_threadEvent, methodName.c_str()); + } + + // copy filename + _filename = new char[strlen(original->_filename) + 1]; + if (_filename) { + strcpy(_filename, original->_filename); + } + + // copy buffer + _buffer = new byte [original->_bufferSize]; + if (!_buffer) { + return STATUS_FAILED; + } + + memcpy(_buffer, original->_buffer, original->_bufferSize); + _bufferSize = original->_bufferSize; + + // initialize + bool res = initScript(); + if (DID_FAIL(res)) { + return res; + } + + // copy globals + _globals = original->_globals; + + // skip to the beginning of the event + _iP = ip; + + _timeSlice = original->_timeSlice; + _freezable = original->_freezable; + _owner = original->_owner; + + _engine = original->_engine; + _parentScript = original; + + return STATUS_OK; +} + + +////////////////////////////////////////////////////////////////////////// +void ScScript::cleanup() { + if (_buffer) { + delete[] _buffer; + } + _buffer = NULL; + + if (_filename) { + delete[] _filename; + } + _filename = NULL; + + if (_symbols) { + delete[] _symbols; + } + _symbols = NULL; + _numSymbols = 0; + + if (_globals && !_thread) { + delete _globals; + } + _globals = NULL; + + delete _scopeStack; + _scopeStack = NULL; + + delete _callStack; + _callStack = NULL; + + delete _thisStack; + _thisStack = NULL; + + delete _stack; + _stack = NULL; + + if (_functions) { + delete[] _functions; + } + _functions = NULL; + _numFunctions = 0; + + if (_methods) { + delete[] _methods; + } + _methods = NULL; + _numMethods = 0; + + if (_events) { + delete[] _events; + } + _events = NULL; + _numEvents = 0; + + + if (_externals) { + for (uint32 i = 0; i < _numExternals; i++) { + if (_externals[i].nu_params > 0) { + delete[] _externals[i].params; + } + } + delete[] _externals; + } + _externals = NULL; + _numExternals = 0; + + delete _operand; + delete _reg1; + _operand = NULL; + _reg1 = NULL; + + delete[] _threadEvent; + _threadEvent = NULL; + + _state = SCRIPT_FINISHED; + + _waitObject = NULL; + _waitTime = 0; + _waitFrozen = false; + _waitScript = NULL; + + _parentScript = NULL; // ref only + + delete _scriptStream; +} + + +////////////////////////////////////////////////////////////////////////// +uint32 ScScript::getDWORD() { + _scriptStream->seek((int32)_iP); + uint32 ret = _scriptStream->readUint32LE(); + _iP += sizeof(uint32); +// assert(oldRet == ret); + return ret; +} + +////////////////////////////////////////////////////////////////////////// +double ScScript::getFloat() { + _scriptStream->seek((int32)_iP); + byte buffer[8]; + _scriptStream->read(buffer, 8); + +#ifdef SCUMM_BIG_ENDIAN + // TODO: For lack of a READ_LE_UINT64 + SWAP(buffer[0], buffer[7]); + SWAP(buffer[1], buffer[6]); + SWAP(buffer[2], buffer[5]); + SWAP(buffer[3], buffer[4]); +#endif + + double ret = *(double *)(buffer); + _iP += 8; // Hardcode the double-size used originally. + return ret; +} + + +////////////////////////////////////////////////////////////////////////// +char *ScScript::getString() { + char *ret = (char *)(_buffer + _iP); + while (*(char *)(_buffer + _iP) != '\0') { + _iP++; + } + _iP++; // string terminator + _scriptStream->seek(_iP); + + return ret; +} + + +////////////////////////////////////////////////////////////////////////// +bool ScScript::executeInstruction() { + bool ret = STATUS_OK; + + uint32 dw; + const char *str = NULL; + + //ScValue* op = new ScValue(_gameRef); + _operand->cleanup(); + + ScValue *op1; + ScValue *op2; + + uint32 inst = getDWORD(); + switch (inst) { + + case II_DEF_VAR: + _operand->setNULL(); + dw = getDWORD(); + if (_scopeStack->_sP < 0) { + _globals->setProp(_symbols[dw], _operand); + } else { + _scopeStack->getTop()->setProp(_symbols[dw], _operand); + } + + break; + + case II_DEF_GLOB_VAR: + case II_DEF_CONST_VAR: { + dw = getDWORD(); + /* char *temp = _symbols[dw]; // TODO delete */ + // only create global var if it doesn't exist + if (!_engine->_globals->propExists(_symbols[dw])) { + _operand->setNULL(); + _engine->_globals->setProp(_symbols[dw], _operand, false, inst == II_DEF_CONST_VAR); + } + break; + } + + case II_RET: + if (_scopeStack->_sP >= 0 && _callStack->_sP >= 0) { + _scopeStack->pop(); + _iP = (uint32)_callStack->pop()->getInt(); + } else { + if (_thread) { + _state = SCRIPT_THREAD_FINISHED; + } else { + if (_numEvents == 0 && _numMethods == 0) { + _state = SCRIPT_FINISHED; + } else { + _state = SCRIPT_PERSISTENT; + } + } + } + + break; + + case II_RET_EVENT: + _state = SCRIPT_FINISHED; + break; + + + case II_CALL: + dw = getDWORD(); + + _operand->setInt(_iP); + _callStack->push(_operand); + + _iP = dw; + + break; + + case II_CALL_BY_EXP: { + // push var + // push string + str = _stack->pop()->getString(); + char *methodName = new char[strlen(str) + 1]; + strcpy(methodName, str); + + ScValue *var = _stack->pop(); + if (var->_type == VAL_VARIABLE_REF) { + var = var->_valRef; + } + + bool res = STATUS_FAILED; + bool triedNative = false; + + // we are already calling this method, try native + if (_thread && _methodThread && strcmp(methodName, _threadEvent) == 0 && var->_type == VAL_NATIVE && _owner == var->getNative()) { + triedNative = true; + res = var->_valNative->scCallMethod(this, _stack, _thisStack, methodName); + } + + if (DID_FAIL(res)) { + if (var->isNative() && var->getNative()->canHandleMethod(methodName)) { + if (!_unbreakable) { + _waitScript = var->getNative()->invokeMethodThread(methodName); + if (!_waitScript) { + _stack->correctParams(0); + runtimeError("Error invoking method '%s'.", methodName); + _stack->pushNULL(); + } else { + _state = SCRIPT_WAITING_SCRIPT; + _waitScript->copyParameters(_stack); + } + } else { + // can call methods in unbreakable mode + _stack->correctParams(0); + runtimeError("Cannot call method '%s'. Ignored.", methodName); + _stack->pushNULL(); + } + delete[] methodName; + break; + } + /* + ScValue* val = var->getProp(MethodName); + if (val){ + dw = GetFuncPos(val->getString()); + if (dw==0){ + TExternalFunction* f = GetExternal(val->getString()); + if (f){ + ExternalCall(_stack, _thisStack, f); + } + else{ + // not an internal nor external, try for native function + _gameRef->ExternalCall(this, _stack, _thisStack, val->getString()); + } + } + else{ + _operand->setInt(_iP); + _callStack->Push(_operand); + _iP = dw; + } + } + */ + else { + res = STATUS_FAILED; + if (var->_type == VAL_NATIVE && !triedNative) { + res = var->_valNative->scCallMethod(this, _stack, _thisStack, methodName); + } + + if (DID_FAIL(res)) { + _stack->correctParams(0); + runtimeError("Call to undefined method '%s'. Ignored.", methodName); + _stack->pushNULL(); + } + } + } + delete[] methodName; + } + break; + + case II_EXTERNAL_CALL: { + uint32 symbolIndex = getDWORD(); + + TExternalFunction *f = getExternal(_symbols[symbolIndex]); + if (f) { + externalCall(_stack, _thisStack, f); + } else { + _gameRef->externalCall(this, _stack, _thisStack, _symbols[symbolIndex]); + } + + break; + } + case II_SCOPE: + _operand->setNULL(); + _scopeStack->push(_operand); + break; + + case II_CORRECT_STACK: + dw = getDWORD(); // params expected + _stack->correctParams(dw); + break; + + case II_CREATE_OBJECT: + _operand->setObject(); + _stack->push(_operand); + break; + + case II_POP_EMPTY: + _stack->pop(); + break; + + case II_PUSH_VAR: { + ScValue *var = getVar(_symbols[getDWORD()]); + if (false && /*var->_type==VAL_OBJECT ||*/ var->_type == VAL_NATIVE) { + _operand->setReference(var); + _stack->push(_operand); + } else { + _stack->push(var); + } + break; + } + + case II_PUSH_VAR_REF: { + ScValue *var = getVar(_symbols[getDWORD()]); + _operand->setReference(var); + _stack->push(_operand); + break; + } + + case II_POP_VAR: { + char *varName = _symbols[getDWORD()]; + ScValue *var = getVar(varName); + if (var) { + ScValue *val = _stack->pop(); + if (!val) { + runtimeError("Script stack corruption detected. Please report this script at WME bug reports forum."); + var->setNULL(); + } else { + if (val->getType() == VAL_VARIABLE_REF) { + val = val->_valRef; + } + if (val->_type == VAL_NATIVE) { + var->setValue(val); + } else { + var->copy(val); + } + } + } + + break; + } + + case II_PUSH_VAR_THIS: + _stack->push(_thisStack->getTop()); + break; + + case II_PUSH_INT: + _stack->pushInt((int)getDWORD()); + break; + + case II_PUSH_FLOAT: + _stack->pushFloat(getFloat()); + break; + + + case II_PUSH_BOOL: + _stack->pushBool(getDWORD() != 0); + + break; + + case II_PUSH_STRING: + _stack->pushString(getString()); + break; + + case II_PUSH_NULL: + _stack->pushNULL(); + break; + + case II_PUSH_THIS_FROM_STACK: + _operand->setReference(_stack->getTop()); + _thisStack->push(_operand); + break; + + case II_PUSH_THIS: + _operand->setReference(getVar(_symbols[getDWORD()])); + _thisStack->push(_operand); + break; + + case II_POP_THIS: + _thisStack->pop(); + break; + + case II_PUSH_BY_EXP: { + str = _stack->pop()->getString(); + ScValue *val = _stack->pop()->getProp(str); + if (val) { + _stack->push(val); + } else { + _stack->pushNULL(); + } + + break; + } + + case II_POP_BY_EXP: { + str = _stack->pop()->getString(); + ScValue *var = _stack->pop(); + ScValue *val = _stack->pop(); + + if (val == NULL) { + runtimeError("Script stack corruption detected. Please report this script at WME bug reports forum."); + var->setNULL(); + } else { + var->setProp(str, val); + } + + break; + } + + case II_PUSH_REG1: + _stack->push(_reg1); + break; + + case II_POP_REG1: + _reg1->copy(_stack->pop()); + break; + + case II_JMP: + _iP = getDWORD(); + break; + + case II_JMP_FALSE: { + dw = getDWORD(); + //if (!_stack->pop()->getBool()) _iP = dw; + ScValue *val = _stack->pop(); + if (!val) { + runtimeError("Script corruption detected. Did you use '=' instead of '==' for comparison?"); + } else { + if (!val->getBool()) { + _iP = dw; + } + } + break; + } + + case II_ADD: + op2 = _stack->pop(); + op1 = _stack->pop(); + + if (op1->isNULL() || op2->isNULL()) { + _operand->setNULL(); + } else if (op1->getType() == VAL_STRING || op2->getType() == VAL_STRING) { + char *tempStr = new char [strlen(op1->getString()) + strlen(op2->getString()) + 1]; + strcpy(tempStr, op1->getString()); + strcat(tempStr, op2->getString()); + _operand->setString(tempStr); + delete[] tempStr; + } else if (op1->getType() == VAL_INT && op2->getType() == VAL_INT) { + _operand->setInt(op1->getInt() + op2->getInt()); + } else { + _operand->setFloat(op1->getFloat() + op2->getFloat()); + } + + _stack->push(_operand); + + break; + + case II_SUB: + op2 = _stack->pop(); + op1 = _stack->pop(); + + if (op1->isNULL() || op2->isNULL()) { + _operand->setNULL(); + } else if (op1->getType() == VAL_INT && op2->getType() == VAL_INT) { + _operand->setInt(op1->getInt() - op2->getInt()); + } else { + _operand->setFloat(op1->getFloat() - op2->getFloat()); + } + + _stack->push(_operand); + + break; + + case II_MUL: + op2 = _stack->pop(); + op1 = _stack->pop(); + + if (op1->isNULL() || op2->isNULL()) { + _operand->setNULL(); + } else if (op1->getType() == VAL_INT && op2->getType() == VAL_INT) { + _operand->setInt(op1->getInt() * op2->getInt()); + } else { + _operand->setFloat(op1->getFloat() * op2->getFloat()); + } + + _stack->push(_operand); + + break; + + case II_DIV: + op2 = _stack->pop(); + op1 = _stack->pop(); + + if (op2->getFloat() == 0.0f) { + runtimeError("Division by zero."); + } + + if (op1->isNULL() || op2->isNULL() || op2->getFloat() == 0.0f) { + _operand->setNULL(); + } else { + _operand->setFloat(op1->getFloat() / op2->getFloat()); + } + + _stack->push(_operand); + + break; + + case II_MODULO: + op2 = _stack->pop(); + op1 = _stack->pop(); + + if (op2->getInt() == 0) { + runtimeError("Division by zero."); + } + + if (op1->isNULL() || op2->isNULL() || op2->getInt() == 0) { + _operand->setNULL(); + } else { + _operand->setInt(op1->getInt() % op2->getInt()); + } + + _stack->push(_operand); + + break; + + case II_NOT: + op1 = _stack->pop(); + //if (op1->isNULL()) _operand->setNULL(); + if (op1->isNULL()) { + _operand->setBool(true); + } else { + _operand->setBool(!op1->getBool()); + } + _stack->push(_operand); + + break; + + case II_AND: + op2 = _stack->pop(); + op1 = _stack->pop(); + if (op1 == NULL || op2 == NULL) { + runtimeError("Script corruption detected. Did you use '=' instead of '==' for comparison?"); + _operand->setBool(false); + } else { + _operand->setBool(op1->getBool() && op2->getBool()); + } + _stack->push(_operand); + break; + + case II_OR: + op2 = _stack->pop(); + op1 = _stack->pop(); + if (op1 == NULL || op2 == NULL) { + runtimeError("Script corruption detected. Did you use '=' instead of '==' for comparison?"); + _operand->setBool(false); + } else { + _operand->setBool(op1->getBool() || op2->getBool()); + } + _stack->push(_operand); + break; + + case II_CMP_EQ: + op2 = _stack->pop(); + op1 = _stack->pop(); + + /* + if ((op1->isNULL() && !op2->isNULL()) || (!op1->isNULL() && op2->isNULL())) _operand->setBool(false); + else if (op1->isNative() && op2->isNative()){ + _operand->setBool(op1->getNative() == op2->getNative()); + } + else if (op1->getType()==VAL_STRING || op2->getType()==VAL_STRING){ + _operand->setBool(scumm_stricmp(op1->getString(), op2->getString())==0); + } + else if (op1->getType()==VAL_FLOAT && op2->getType()==VAL_FLOAT){ + _operand->setBool(op1->getFloat() == op2->getFloat()); + } + else{ + _operand->setBool(op1->getInt() == op2->getInt()); + } + */ + + _operand->setBool(ScValue::compare(op1, op2) == 0); + _stack->push(_operand); + break; + + case II_CMP_NE: + op2 = _stack->pop(); + op1 = _stack->pop(); + + /* + if ((op1->isNULL() && !op2->isNULL()) || (!op1->isNULL() && op2->isNULL())) _operand->setBool(true); + else if (op1->isNative() && op2->isNative()){ + _operand->setBool(op1->getNative() != op2->getNative()); + } + else if (op1->getType()==VAL_STRING || op2->getType()==VAL_STRING){ + _operand->setBool(scumm_stricmp(op1->getString(), op2->getString())!=0); + } + else if (op1->getType()==VAL_FLOAT && op2->getType()==VAL_FLOAT){ + _operand->setBool(op1->getFloat() != op2->getFloat()); + } + else{ + _operand->setBool(op1->getInt() != op2->getInt()); + } + */ + + _operand->setBool(ScValue::compare(op1, op2) != 0); + _stack->push(_operand); + break; + + case II_CMP_L: + op2 = _stack->pop(); + op1 = _stack->pop(); + + /* + if (op1->getType()==VAL_FLOAT && op2->getType()==VAL_FLOAT){ + _operand->setBool(op1->getFloat() < op2->getFloat()); + } + else _operand->setBool(op1->getInt() < op2->getInt()); + */ + + _operand->setBool(ScValue::compare(op1, op2) < 0); + _stack->push(_operand); + break; + + case II_CMP_G: + op2 = _stack->pop(); + op1 = _stack->pop(); + + /* + if (op1->getType()==VAL_FLOAT && op2->getType()==VAL_FLOAT){ + _operand->setBool(op1->getFloat() > op2->getFloat()); + } + else _operand->setBool(op1->getInt() > op2->getInt()); + */ + + _operand->setBool(ScValue::compare(op1, op2) > 0); + _stack->push(_operand); + break; + + case II_CMP_LE: + op2 = _stack->pop(); + op1 = _stack->pop(); + + /* + if (op1->getType()==VAL_FLOAT && op2->getType()==VAL_FLOAT){ + _operand->setBool(op1->getFloat() <= op2->getFloat()); + } + else _operand->setBool(op1->getInt() <= op2->getInt()); + */ + + _operand->setBool(ScValue::compare(op1, op2) <= 0); + _stack->push(_operand); + break; + + case II_CMP_GE: + op2 = _stack->pop(); + op1 = _stack->pop(); + + /* + if (op1->getType()==VAL_FLOAT && op2->getType()==VAL_FLOAT){ + _operand->setBool(op1->getFloat() >= op2->getFloat()); + } + else _operand->setBool(op1->getInt() >= op2->getInt()); + */ + + _operand->setBool(ScValue::compare(op1, op2) >= 0); + _stack->push(_operand); + break; + + case II_CMP_STRICT_EQ: + op2 = _stack->pop(); + op1 = _stack->pop(); + + //_operand->setBool(op1->getType()==op2->getType() && op1->getFloat()==op2->getFloat()); + _operand->setBool(ScValue::compareStrict(op1, op2) == 0); + _stack->push(_operand); + + break; + + case II_CMP_STRICT_NE: + op2 = _stack->pop(); + op1 = _stack->pop(); + + //_operand->setBool(op1->getType()!=op2->getType() || op1->getFloat()!=op2->getFloat()); + _operand->setBool(ScValue::compareStrict(op1, op2) != 0); + _stack->push(_operand); + break; + + case II_DBG_LINE: { + int newLine = getDWORD(); + if (newLine != _currentLine) { + _currentLine = newLine; + } + break; + + } + default: + _gameRef->LOG(0, "Fatal: Invalid instruction %d ('%s', line %d, IP:0x%x)\n", inst, _filename, _currentLine, _iP - sizeof(uint32)); + _state = SCRIPT_FINISHED; + ret = STATUS_FAILED; + } // switch(instruction) + + //delete op; + + return ret; +} + + +////////////////////////////////////////////////////////////////////////// +uint32 ScScript::getFuncPos(const Common::String &name) { + for (uint32 i = 0; i < _numFunctions; i++) { + if (name == _functions[i].name) { + return _functions[i].pos; + } + } + return 0; +} + + +////////////////////////////////////////////////////////////////////////// +uint32 ScScript::getMethodPos(const Common::String &name) { + for (uint32 i = 0; i < _numMethods; i++) { + if (name == _methods[i].name) { + return _methods[i].pos; + } + } + return 0; +} + + +////////////////////////////////////////////////////////////////////////// +ScValue *ScScript::getVar(char *name) { + ScValue *ret = NULL; + + // scope locals + if (_scopeStack->_sP >= 0) { + if (_scopeStack->getTop()->propExists(name)) { + ret = _scopeStack->getTop()->getProp(name); + } + } + + // script globals + if (ret == NULL) { + if (_globals->propExists(name)) { + ret = _globals->getProp(name); + } + } + + // engine globals + if (ret == NULL) { + if (_engine->_globals->propExists(name)) { + ret = _engine->_globals->getProp(name); + } + } + + if (ret == NULL) { + //RuntimeError("Variable '%s' is inaccessible in the current block. Consider changing the script.", name); + _gameRef->LOG(0, "Warning: variable '%s' is inaccessible in the current block. Consider changing the script (script:%s, line:%d)", name, _filename, _currentLine); + ScValue *val = new ScValue(_gameRef); + ScValue *scope = _scopeStack->getTop(); + if (scope) { + scope->setProp(name, val); + ret = _scopeStack->getTop()->getProp(name); + } else { + _globals->setProp(name, val); + ret = _globals->getProp(name); + } + delete val; + } + + return ret; +} + + +////////////////////////////////////////////////////////////////////////// +bool ScScript::waitFor(BaseObject *object) { + if (_unbreakable) { + runtimeError("Script cannot be interrupted."); + return STATUS_OK; + } + + _state = SCRIPT_WAITING; + _waitObject = object; + return STATUS_OK; +} + + +////////////////////////////////////////////////////////////////////////// +bool ScScript::waitForExclusive(BaseObject *object) { + _engine->resetObject(object); + return waitFor(object); +} + + +////////////////////////////////////////////////////////////////////////// +bool ScScript::sleep(uint32 duration) { + if (_unbreakable) { + runtimeError("Script cannot be interrupted."); + return STATUS_OK; + } + + _state = SCRIPT_SLEEPING; + if (_gameRef->_state == GAME_FROZEN) { + _waitTime = g_system->getMillis() + duration; + _waitFrozen = true; + } else { + _waitTime = _gameRef->_timer + duration; + _waitFrozen = false; + } + return STATUS_OK; +} + + +////////////////////////////////////////////////////////////////////////// +bool ScScript::finish(bool includingThreads) { + if (_state != SCRIPT_FINISHED && includingThreads) { + _state = SCRIPT_FINISHED; + finishThreads(); + } else { + _state = SCRIPT_FINISHED; + } + + + return STATUS_OK; +} + + +////////////////////////////////////////////////////////////////////////// +bool ScScript::run() { + _state = SCRIPT_RUNNING; + return STATUS_OK; +} + + +////////////////////////////////////////////////////////////////////// +void ScScript::runtimeError(const char *fmt, ...) { + char buff[256]; + va_list va; + + va_start(va, fmt); + vsprintf(buff, fmt, va); + va_end(va); + + _gameRef->LOG(0, "Runtime error. Script '%s', line %d", _filename, _currentLine); + _gameRef->LOG(0, " %s", buff); + + if (!_gameRef->_suppressScriptErrors) { + _gameRef->quickMessage("Script runtime error. View log for details."); + } +} + + +////////////////////////////////////////////////////////////////////////// +bool ScScript::persist(BasePersistenceManager *persistMgr) { + + persistMgr->transfer(TMEMBER(_gameRef)); + + // buffer + if (persistMgr->getIsSaving()) { + if (_state != SCRIPT_PERSISTENT && _state != SCRIPT_FINISHED && _state != SCRIPT_THREAD_FINISHED) { + persistMgr->transfer(TMEMBER(_bufferSize)); + persistMgr->putBytes(_buffer, _bufferSize); + } else { + // don't save idle/finished scripts + int bufferSize = 0; + persistMgr->transfer(TMEMBER(bufferSize)); + } + } else { + persistMgr->transfer(TMEMBER(_bufferSize)); + if (_bufferSize > 0) { + _buffer = new byte[_bufferSize]; + persistMgr->getBytes(_buffer, _bufferSize); + _scriptStream = new Common::MemoryReadStream(_buffer, _bufferSize); + initTables(); + } else { + _buffer = NULL; + _scriptStream = NULL; + } + } + + persistMgr->transfer(TMEMBER(_callStack)); + persistMgr->transfer(TMEMBER(_currentLine)); + persistMgr->transfer(TMEMBER(_engine)); + persistMgr->transfer(TMEMBER(_filename)); + persistMgr->transfer(TMEMBER(_freezable)); + persistMgr->transfer(TMEMBER(_globals)); + persistMgr->transfer(TMEMBER(_iP)); + persistMgr->transfer(TMEMBER(_scopeStack)); + persistMgr->transfer(TMEMBER(_stack)); + persistMgr->transfer(TMEMBER_INT(_state)); + persistMgr->transfer(TMEMBER(_operand)); + persistMgr->transfer(TMEMBER_INT(_origState)); + persistMgr->transfer(TMEMBER(_owner)); + persistMgr->transfer(TMEMBER(_reg1)); + persistMgr->transfer(TMEMBER(_thread)); + persistMgr->transfer(TMEMBER(_threadEvent)); + persistMgr->transfer(TMEMBER(_thisStack)); + persistMgr->transfer(TMEMBER(_timeSlice)); + persistMgr->transfer(TMEMBER(_waitObject)); + persistMgr->transfer(TMEMBER(_waitScript)); + persistMgr->transfer(TMEMBER(_waitTime)); + persistMgr->transfer(TMEMBER(_waitFrozen)); + + persistMgr->transfer(TMEMBER(_methodThread)); + persistMgr->transfer(TMEMBER(_methodThread)); + persistMgr->transfer(TMEMBER(_unbreakable)); + persistMgr->transfer(TMEMBER(_parentScript)); + + if (!persistMgr->getIsSaving()) { + _tracingMode = false; + } + + return STATUS_OK; +} + + +////////////////////////////////////////////////////////////////////////// +ScScript *ScScript::invokeEventHandler(const Common::String &eventName, bool unbreakable) { + //if (_state!=SCRIPT_PERSISTENT) return NULL; + + uint32 pos = getEventPos(eventName); + if (!pos) { + return NULL; + } + + ScScript *thread = new ScScript(_gameRef, _engine); + if (thread) { + bool ret = thread->createThread(this, pos, eventName); + if (DID_SUCCEED(ret)) { + thread->_unbreakable = unbreakable; + _engine->_scripts.add(thread); + return thread; + } else { + delete thread; + return NULL; + } + } else { + return NULL; + } + +} + + +////////////////////////////////////////////////////////////////////////// +uint32 ScScript::getEventPos(const Common::String &name) { + for (int i = _numEvents - 1; i >= 0; i--) { + if (scumm_stricmp(name.c_str(), _events[i].name) == 0) { + return _events[i].pos; + } + } + return 0; +} + + +////////////////////////////////////////////////////////////////////////// +bool ScScript::canHandleEvent(const Common::String &eventName) { + return getEventPos(eventName) != 0; +} + + +////////////////////////////////////////////////////////////////////////// +bool ScScript::canHandleMethod(const Common::String &methodName) { + return getMethodPos(methodName) != 0; +} + + +////////////////////////////////////////////////////////////////////////// +bool ScScript::pause() { + if (_state == SCRIPT_PAUSED) { + _gameRef->LOG(0, "Attempting to pause a paused script ('%s', line %d)", _filename, _currentLine); + return STATUS_FAILED; + } + + if (!_freezable || _state == SCRIPT_PERSISTENT) { + return STATUS_OK; + } + + _origState = _state; + _state = SCRIPT_PAUSED; + + return STATUS_OK; +} + + +////////////////////////////////////////////////////////////////////////// +bool ScScript::resume() { + if (_state != SCRIPT_PAUSED) { + return STATUS_OK; + } + + _state = _origState; + return STATUS_OK; +} + + +////////////////////////////////////////////////////////////////////////// +ScScript::TExternalFunction *ScScript::getExternal(char *name) { + for (uint32 i = 0; i < _numExternals; i++) { + if (strcmp(name, _externals[i].name) == 0) { + return &_externals[i]; + } + } + return NULL; +} + + +////////////////////////////////////////////////////////////////////////// +bool ScScript::externalCall(ScStack *stack, ScStack *thisStack, ScScript::TExternalFunction *function) { + + _gameRef->LOG(0, "External functions are not supported on this platform."); + stack->correctParams(0); + stack->pushNULL(); + return STATUS_FAILED; +} + + +////////////////////////////////////////////////////////////////////////// +bool ScScript::copyParameters(ScStack *stack) { + int i; + int numParams = stack->pop()->getInt(); + for (i = numParams - 1; i >= 0; i--) { + _stack->push(stack->getAt(i)); + } + _stack->pushInt(numParams); + + for (i = 0; i < numParams; i++) { + stack->pop(); + } + + return STATUS_OK; +} + + +////////////////////////////////////////////////////////////////////////// +bool ScScript::finishThreads() { + for (uint32 i = 0; i < _engine->_scripts.size(); i++) { + ScScript *scr = _engine->_scripts[i]; + if (scr->_thread && scr->_state != SCRIPT_FINISHED && scr->_owner == _owner && scumm_stricmp(scr->_filename, _filename) == 0) { + scr->finish(true); + } + } + return STATUS_OK; +} + + +////////////////////////////////////////////////////////////////////////// +// IWmeDebugScript interface implementation +int ScScript::dbgGetLine() { + return _currentLine; +} + +////////////////////////////////////////////////////////////////////////// +const char *ScScript::dbgGetFilename() { + return _filename; +} + +////////////////////////////////////////////////////////////////////////// +void ScScript::afterLoad() { + if (_buffer == NULL) { + byte *buffer = _engine->getCompiledScript(_filename, &_bufferSize); + if (!buffer) { + _gameRef->LOG(0, "Error reinitializing script '%s' after load. Script will be terminated.", _filename); + _state = SCRIPT_ERROR; + return; + } + + _buffer = new byte [_bufferSize]; + memcpy(_buffer, buffer, _bufferSize); + + delete _scriptStream; + _scriptStream = new Common::MemoryReadStream(_buffer, _bufferSize); + + initTables(); + } +} + +} // end of namespace Wintermute diff --git a/engines/wintermute/base/scriptables/script.h b/engines/wintermute/base/scriptables/script.h index 4daeacd026..0616bce58a 100644 --- a/engines/wintermute/base/scriptables/script.h +++ b/engines/wintermute/base/scriptables/script.h @@ -1,174 +1,174 @@ -/* ScummVM - Graphic Adventure Engine - * - * ScummVM is the legal property of its developers, whose names - * are too numerous to list here. Please refer to the COPYRIGHT - * file distributed with this source distribution. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - */ - -/* - * This file is based on WME Lite. - * http://dead-code.org/redir.php?target=wmelite - * Copyright (c) 2011 Jan Nedoma - */ - -#ifndef WINTERMUTE_SCSCRIPT_H -#define WINTERMUTE_SCSCRIPT_H - - -#include "engines/wintermute/base/base.h" -#include "engines/wintermute/base/scriptables/dcscript.h" // Added by ClassView -#include "engines/wintermute/coll_templ.h" - -namespace Wintermute { -class BaseScriptHolder; -class BaseObject; -class ScEngine; -class ScStack; -class ScScript : public BaseClass { -public: - BaseArray _breakpoints; - bool _tracingMode; - - ScScript *_parentScript; - bool _unbreakable; - bool finishThreads(); - bool copyParameters(ScStack *stack); - - void afterLoad(); -private: - ScValue *_operand; - ScValue *_reg1; -public: - bool _freezable; - bool resume(); - bool pause(); - bool canHandleEvent(const Common::String &eventName); - bool canHandleMethod(const Common::String &methodName); - bool createThread(ScScript *original, uint32 initIP, const Common::String &eventName); - bool createMethodThread(ScScript *original, const Common::String &methodName); - ScScript *invokeEventHandler(const Common::String &eventName, bool unbreakable = false); - uint32 _timeSlice; - DECLARE_PERSISTENT(ScScript, BaseClass) - void runtimeError(const char *fmt, ...); - bool run(); - bool finish(bool includingThreads = false); - bool sleep(uint32 duration); - bool waitForExclusive(BaseObject *object); - bool waitFor(BaseObject *object); - uint32 _waitTime; - bool _waitFrozen; - BaseObject *_waitObject; - ScScript *_waitScript; - TScriptState _state; - TScriptState _origState; - ScValue *getVar(char *name); - uint32 getFuncPos(const Common::String &name); - uint32 getEventPos(const Common::String &name); - uint32 getMethodPos(const Common::String &name); - typedef struct { - uint32 magic; - uint32 version; - uint32 codeStart; - uint32 funcTable; - uint32 symbolTable; - uint32 eventTable; - uint32 externalsTable; - uint32 methodTable; - } TScriptHeader; - - TScriptHeader _header; - - typedef struct { - char *name; - uint32 pos; - } TFunctionPos; - - typedef struct { - char *name; - uint32 pos; - } TMethodPos; - - typedef struct { - char *name; - uint32 pos; - } TEventPos; - - typedef struct { - char *name; - char *dll_name; - TCallType call_type; - TExternalType returns; - int nu_params; - TExternalType *params; - } TExternalFunction; - - - ScStack *_callStack; - ScStack *_thisStack; - ScStack *_scopeStack; - ScStack *_stack; - ScValue *_globals; - ScEngine *_engine; - int _currentLine; - bool executeInstruction(); - char *getString(); - uint32 getDWORD(); - double getFloat(); - void cleanup(); - bool create(const char *filename, byte *buffer, uint32 size, BaseScriptHolder *owner); - uint32 _iP; -private: - void readHeader(); - uint32 _bufferSize; - byte *_buffer; -public: - Common::SeekableReadStream *_scriptStream; - ScScript(BaseGame *inGame, ScEngine *engine); - virtual ~ScScript(); - char *_filename; - bool _thread; - bool _methodThread; - char *_threadEvent; - BaseScriptHolder *_owner; - ScScript::TExternalFunction *getExternal(char *name); - bool externalCall(ScStack *stack, ScStack *thisStack, ScScript::TExternalFunction *function); -private: - char **_symbols; - uint32 _numSymbols; - TFunctionPos *_functions; - TMethodPos *_methods; - TEventPos *_events; - uint32 _numExternals; - TExternalFunction *_externals; - uint32 _numFunctions; - uint32 _numMethods; - uint32 _numEvents; - - bool initScript(); - bool initTables(); - - -// IWmeDebugScript interface implementation -public: - virtual int dbgGetLine(); - virtual const char *dbgGetFilename(); -}; - -} // end of namespace Wintermute - -#endif +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +/* + * This file is based on WME Lite. + * http://dead-code.org/redir.php?target=wmelite + * Copyright (c) 2011 Jan Nedoma + */ + +#ifndef WINTERMUTE_SCSCRIPT_H +#define WINTERMUTE_SCSCRIPT_H + + +#include "engines/wintermute/base/base.h" +#include "engines/wintermute/base/scriptables/dcscript.h" // Added by ClassView +#include "engines/wintermute/coll_templ.h" + +namespace Wintermute { +class BaseScriptHolder; +class BaseObject; +class ScEngine; +class ScStack; +class ScScript : public BaseClass { +public: + BaseArray _breakpoints; + bool _tracingMode; + + ScScript *_parentScript; + bool _unbreakable; + bool finishThreads(); + bool copyParameters(ScStack *stack); + + void afterLoad(); +private: + ScValue *_operand; + ScValue *_reg1; +public: + bool _freezable; + bool resume(); + bool pause(); + bool canHandleEvent(const Common::String &eventName); + bool canHandleMethod(const Common::String &methodName); + bool createThread(ScScript *original, uint32 initIP, const Common::String &eventName); + bool createMethodThread(ScScript *original, const Common::String &methodName); + ScScript *invokeEventHandler(const Common::String &eventName, bool unbreakable = false); + uint32 _timeSlice; + DECLARE_PERSISTENT(ScScript, BaseClass) + void runtimeError(const char *fmt, ...); + bool run(); + bool finish(bool includingThreads = false); + bool sleep(uint32 duration); + bool waitForExclusive(BaseObject *object); + bool waitFor(BaseObject *object); + uint32 _waitTime; + bool _waitFrozen; + BaseObject *_waitObject; + ScScript *_waitScript; + TScriptState _state; + TScriptState _origState; + ScValue *getVar(char *name); + uint32 getFuncPos(const Common::String &name); + uint32 getEventPos(const Common::String &name); + uint32 getMethodPos(const Common::String &name); + typedef struct { + uint32 magic; + uint32 version; + uint32 codeStart; + uint32 funcTable; + uint32 symbolTable; + uint32 eventTable; + uint32 externalsTable; + uint32 methodTable; + } TScriptHeader; + + TScriptHeader _header; + + typedef struct { + char *name; + uint32 pos; + } TFunctionPos; + + typedef struct { + char *name; + uint32 pos; + } TMethodPos; + + typedef struct { + char *name; + uint32 pos; + } TEventPos; + + typedef struct { + char *name; + char *dll_name; + TCallType call_type; + TExternalType returns; + int nu_params; + TExternalType *params; + } TExternalFunction; + + + ScStack *_callStack; + ScStack *_thisStack; + ScStack *_scopeStack; + ScStack *_stack; + ScValue *_globals; + ScEngine *_engine; + int _currentLine; + bool executeInstruction(); + char *getString(); + uint32 getDWORD(); + double getFloat(); + void cleanup(); + bool create(const char *filename, byte *buffer, uint32 size, BaseScriptHolder *owner); + uint32 _iP; +private: + void readHeader(); + uint32 _bufferSize; + byte *_buffer; +public: + Common::SeekableReadStream *_scriptStream; + ScScript(BaseGame *inGame, ScEngine *engine); + virtual ~ScScript(); + char *_filename; + bool _thread; + bool _methodThread; + char *_threadEvent; + BaseScriptHolder *_owner; + ScScript::TExternalFunction *getExternal(char *name); + bool externalCall(ScStack *stack, ScStack *thisStack, ScScript::TExternalFunction *function); +private: + char **_symbols; + uint32 _numSymbols; + TFunctionPos *_functions; + TMethodPos *_methods; + TEventPos *_events; + uint32 _numExternals; + TExternalFunction *_externals; + uint32 _numFunctions; + uint32 _numMethods; + uint32 _numEvents; + + bool initScript(); + bool initTables(); + + +// IWmeDebugScript interface implementation +public: + virtual int dbgGetLine(); + virtual const char *dbgGetFilename(); +}; + +} // end of namespace Wintermute + +#endif diff --git a/engines/wintermute/base/scriptables/script_engine.cpp b/engines/wintermute/base/scriptables/script_engine.cpp index 02f6080958..20e2ccadd1 100644 --- a/engines/wintermute/base/scriptables/script_engine.cpp +++ b/engines/wintermute/base/scriptables/script_engine.cpp @@ -1,610 +1,610 @@ -/* ScummVM - Graphic Adventure Engine - * - * ScummVM is the legal property of its developers, whose names - * are too numerous to list here. Please refer to the COPYRIGHT - * file distributed with this source distribution. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - */ - -/* - * This file is based on WME Lite. - * http://dead-code.org/redir.php?target=wmelite - * Copyright (c) 2011 Jan Nedoma - */ - -#include "engines/wintermute/base/scriptables/script_engine.h" -#include "engines/wintermute/base/scriptables/script_value.h" -#include "engines/wintermute/base/scriptables/script.h" -#include "engines/wintermute/base/scriptables/script_stack.h" -#include "engines/wintermute/base/scriptables/script_ext_math.h" -#include "engines/wintermute/base/base_engine.h" -#include "engines/wintermute/base/base_game.h" -#include "engines/wintermute/base/sound/base_sound.h" -#include "engines/wintermute/base/base_file_manager.h" -#include "engines/wintermute/utils/utils.h" - -namespace Wintermute { - -IMPLEMENT_PERSISTENT(ScEngine, true) - -#define COMPILER_DLL "dcscomp.dll" -////////////////////////////////////////////////////////////////////////// -ScEngine::ScEngine(BaseGame *inGame) : BaseClass(inGame) { - _gameRef->LOG(0, "Initializing scripting engine..."); - - if (_compilerAvailable) { - _gameRef->LOG(0, " Script compiler bound successfuly"); - } else { - _gameRef->LOG(0, " Script compiler is NOT available"); - } - - _globals = new ScValue(_gameRef); - - - // register 'Game' as global variable - if (!_globals->propExists("Game")) { - ScValue val(_gameRef); - val.setNative(_gameRef, true); - _globals->setProp("Game", &val); - } - - // register 'Math' as global variable - if (!_globals->propExists("Math")) { - ScValue val(_gameRef); - val.setNative(_gameRef->_mathClass, true); - _globals->setProp("Math", &val); - } - - // prepare script cache - for (int i = 0; i < MAX_CACHED_SCRIPTS; i++) { - _cachedScripts[i] = NULL; - } - - _currentScript = NULL; - - _isProfiling = false; - _profilingStartTime = 0; - - //EnableProfiling(); -} - - -////////////////////////////////////////////////////////////////////////// -ScEngine::~ScEngine() { - _gameRef->LOG(0, "Shutting down scripting engine"); - - disableProfiling(); - - cleanup(); -} - - -////////////////////////////////////////////////////////////////////////// -bool ScEngine::cleanup() { - for (uint32 i = 0; i < _scripts.size(); i++) { - if (!_scripts[i]->_thread && _scripts[i]->_owner) { - _scripts[i]->_owner->removeScript(_scripts[i]); - } - delete _scripts[i]; - _scripts.remove_at(i); - i--; - } - - _scripts.clear(); - - delete _globals; - _globals = NULL; - - emptyScriptCache(); - - _currentScript = NULL; // ref only - - return STATUS_OK; -} - - -////////////////////////////////////////////////////////////////////////// -byte *ScEngine::loadFile(void *data, char *filename, uint32 *size) { - return BaseFileManager::getEngineInstance()->readWholeFile(filename, size); -} - - -////////////////////////////////////////////////////////////////////////// -void ScEngine::closeFile(void *data, byte *buffer) { - delete[] buffer; -} - - -////////////////////////////////////////////////////////////////////////// -void ScEngine::parseElement(void *data, int line, int type, void *elementData) { -} - - -////////////////////////////////////////////////////////////////////////// -ScScript *ScEngine::runScript(const char *filename, BaseScriptHolder *owner) { - byte *compBuffer; - uint32 compSize; - - // get script from cache - compBuffer = getCompiledScript(filename, &compSize); - if (!compBuffer) { - return NULL; - } - - // add new script - ScScript *script = new ScScript(_gameRef, this); - bool ret = script->create(filename, compBuffer, compSize, owner); - if (DID_FAIL(ret)) { - _gameRef->LOG(ret, "Error running script '%s'...", filename); - delete script; - return NULL; - } else { - // publish the "self" pseudo-variable - ScValue val(_gameRef); - if (owner) { - val.setNative(owner, true); - } else { - val.setNULL(); - } - - script->_globals->setProp("self", &val); - script->_globals->setProp("this", &val); - - _scripts.add(script); - - return script; - } -} - - -////////////////////////////////////////////////////////////////////////// -byte *ScEngine::getCompiledScript(const char *filename, uint32 *outSize, bool ignoreCache) { - // is script in cache? - if (!ignoreCache) { - for (int i = 0; i < MAX_CACHED_SCRIPTS; i++) { - if (_cachedScripts[i] && scumm_stricmp(_cachedScripts[i]->_filename.c_str(), filename) == 0) { - _cachedScripts[i]->_timestamp = g_system->getMillis(); - *outSize = _cachedScripts[i]->_size; - return _cachedScripts[i]->_buffer; - } - } - } - - // nope, load it - byte *compBuffer; - uint32 compSize; - - uint32 size; - - byte *buffer = BaseEngine::instance().getFileManager()->readWholeFile(filename, &size); - if (!buffer) { - _gameRef->LOG(0, "ScEngine::GetCompiledScript - error opening script '%s'", filename); - return NULL; - } - - // needs to be compiled? - if (FROM_LE_32(*(uint32 *)buffer) == SCRIPT_MAGIC) { - compBuffer = buffer; - compSize = size; - } else { - if (!_compilerAvailable) { - _gameRef->LOG(0, "ScEngine::GetCompiledScript - script '%s' needs to be compiled but compiler is not available", filename); - delete[] buffer; - return NULL; - } - // This code will never be called, since _compilerAvailable is const false. - // It's only here in the event someone would want to reinclude the compiler. - error("Script needs compilation, ScummVM does not contain a WME compiler"); - } - - byte *ret = NULL; - - // add script to cache - CScCachedScript *cachedScript = new CScCachedScript(filename, compBuffer, compSize); - if (cachedScript) { - int index = 0; - uint32 minTime = g_system->getMillis(); - for (int i = 0; i < MAX_CACHED_SCRIPTS; i++) { - if (_cachedScripts[i] == NULL) { - index = i; - break; - } else if (_cachedScripts[i]->_timestamp <= minTime) { - minTime = _cachedScripts[i]->_timestamp; - index = i; - } - } - - if (_cachedScripts[index] != NULL) { - delete _cachedScripts[index]; - } - _cachedScripts[index] = cachedScript; - - ret = cachedScript->_buffer; - *outSize = cachedScript->_size; - } - - - // cleanup - delete[] buffer; - - return ret; -} - - - -////////////////////////////////////////////////////////////////////////// -bool ScEngine::tick() { - if (_scripts.size() == 0) { - return STATUS_OK; - } - - - // resolve waiting scripts - for (uint32 i = 0; i < _scripts.size(); i++) { - - switch (_scripts[i]->_state) { - case SCRIPT_WAITING: { - /* - bool obj_found=false; - for(int j=0; j<_gameRef->_regObjects.size(); j++) - { - if (_gameRef->_regObjects[j] == _scripts[i]->_waitObject) - { - if (_gameRef->_regObjects[j]->IsReady()) _scripts[i]->Run(); - obj_found = true; - break; - } - } - if (!obj_found) _scripts[i]->finish(); // _waitObject no longer exists - */ - if (_gameRef->validObject(_scripts[i]->_waitObject)) { - if (_scripts[i]->_waitObject->isReady()) { - _scripts[i]->run(); - } - } else { - _scripts[i]->finish(); - } - break; - } - - case SCRIPT_SLEEPING: { - if (_scripts[i]->_waitFrozen) { - if (_scripts[i]->_waitTime <= g_system->getMillis()) { - _scripts[i]->run(); - } - } else { - if (_scripts[i]->_waitTime <= _gameRef->_timer) { - _scripts[i]->run(); - } - } - break; - } - - case SCRIPT_WAITING_SCRIPT: { - if (!isValidScript(_scripts[i]->_waitScript) || _scripts[i]->_waitScript->_state == SCRIPT_ERROR) { - // fake return value - _scripts[i]->_stack->pushNULL(); - _scripts[i]->_waitScript = NULL; - _scripts[i]->run(); - } else { - if (_scripts[i]->_waitScript->_state == SCRIPT_THREAD_FINISHED) { - // copy return value - _scripts[i]->_stack->push(_scripts[i]->_waitScript->_stack->pop()); - _scripts[i]->run(); - _scripts[i]->_waitScript->finish(); - _scripts[i]->_waitScript = NULL; - } - } - break; - } - default: - break; - } // switch - } // for each script - - - // execute scripts - for (uint32 i = 0; i < _scripts.size(); i++) { - - // skip paused scripts - if (_scripts[i]->_state == SCRIPT_PAUSED) { - continue; - } - - // time sliced script - if (_scripts[i]->_timeSlice > 0) { - uint32 startTime = g_system->getMillis(); - while (_scripts[i]->_state == SCRIPT_RUNNING && g_system->getMillis() - startTime < _scripts[i]->_timeSlice) { - _currentScript = _scripts[i]; - _scripts[i]->executeInstruction(); - } - if (_isProfiling && _scripts[i]->_filename) { - addScriptTime(_scripts[i]->_filename, g_system->getMillis() - startTime); - } - } - - // normal script - else { - uint32 startTime = 0; - bool isProfiling = _isProfiling; - if (isProfiling) { - startTime = g_system->getMillis(); - } - - while (_scripts[i]->_state == SCRIPT_RUNNING) { - _currentScript = _scripts[i]; - _scripts[i]->executeInstruction(); - } - if (isProfiling && _scripts[i]->_filename) { - addScriptTime(_scripts[i]->_filename, g_system->getMillis() - startTime); - } - } - _currentScript = NULL; - } - - removeFinishedScripts(); - - return STATUS_OK; -} - - -////////////////////////////////////////////////////////////////////////// -bool ScEngine::tickUnbreakable() { - // execute unbreakable scripts - for (uint32 i = 0; i < _scripts.size(); i++) { - if (!_scripts[i]->_unbreakable) { - continue; - } - - while (_scripts[i]->_state == SCRIPT_RUNNING) { - _currentScript = _scripts[i]; - _scripts[i]->executeInstruction(); - } - _scripts[i]->finish(); - _currentScript = NULL; - } - removeFinishedScripts(); - - return STATUS_OK; -} - - -////////////////////////////////////////////////////////////////////////// -bool ScEngine::removeFinishedScripts() { - // remove finished scripts - for (uint32 i = 0; i < _scripts.size(); i++) { - if (_scripts[i]->_state == SCRIPT_FINISHED || _scripts[i]->_state == SCRIPT_ERROR) { - if (!_scripts[i]->_thread && _scripts[i]->_owner) { - _scripts[i]->_owner->removeScript(_scripts[i]); - } - - delete _scripts[i]; - _scripts.remove_at(i); - i--; - } - } - return STATUS_OK; -} - - -////////////////////////////////////////////////////////////////////////// -int ScEngine::getNumScripts(int *running, int *waiting, int *persistent) { - int numRunning = 0, numWaiting = 0, numPersistent = 0, numTotal = 0; - - for (uint32 i = 0; i < _scripts.size(); i++) { - if (_scripts[i]->_state == SCRIPT_FINISHED) { - continue; - } - switch (_scripts[i]->_state) { - case SCRIPT_RUNNING: - case SCRIPT_SLEEPING: - case SCRIPT_PAUSED: - numRunning++; - break; - case SCRIPT_WAITING: - numWaiting++; - break; - case SCRIPT_PERSISTENT: - numPersistent++; - break; - default: - warning("ScEngine::GetNumScripts - unhandled enum"); - break; - } - numTotal++; - } - if (running) { - *running = numRunning; - } - if (waiting) { - *waiting = numWaiting; - } - if (persistent) { - *persistent = numPersistent; - } - - return numTotal; -} - - -////////////////////////////////////////////////////////////////////////// -bool ScEngine::emptyScriptCache() { - for (int i = 0; i < MAX_CACHED_SCRIPTS; i++) { - if (_cachedScripts[i]) { - delete _cachedScripts[i]; - _cachedScripts[i] = NULL; - } - } - return STATUS_OK; -} - - -////////////////////////////////////////////////////////////////////////// -bool ScEngine::resetObject(BaseObject *Object) { - // terminate all scripts waiting for this object - for (uint32 i = 0; i < _scripts.size(); i++) { - if (_scripts[i]->_state == SCRIPT_WAITING && _scripts[i]->_waitObject == Object) { - if (!_gameRef->_compatKillMethodThreads) { - resetScript(_scripts[i]); - } - - bool isThread = _scripts[i]->_methodThread || _scripts[i]->_thread; - _scripts[i]->finish(!isThread); // 1.9b1 - top-level script kills its threads as well - } - } - return STATUS_OK; -} - -////////////////////////////////////////////////////////////////////////// -bool ScEngine::resetScript(ScScript *script) { - // terminate all scripts waiting for this script - for (uint32 i = 0; i < _scripts.size(); i++) { - if (_scripts[i]->_state == SCRIPT_WAITING_SCRIPT && _scripts[i]->_waitScript == script) { - _scripts[i]->finish(); - } - } - return STATUS_OK; -} - -////////////////////////////////////////////////////////////////////////// -bool ScEngine::persist(BasePersistenceManager *persistMgr) { - if (!persistMgr->getIsSaving()) { - cleanup(); - } - - persistMgr->transfer(TMEMBER(_gameRef)); - persistMgr->transfer(TMEMBER(_currentScript)); - persistMgr->transfer(TMEMBER(_globals)); - _scripts.persist(persistMgr); - - return STATUS_OK; -} - - -////////////////////////////////////////////////////////////////////////// -void ScEngine::editorCleanup() { - for (uint32 i = 0; i < _scripts.size(); i++) { - if (_scripts[i]->_owner == NULL && (_scripts[i]->_state == SCRIPT_FINISHED || _scripts[i]->_state == SCRIPT_ERROR)) { - delete _scripts[i]; - _scripts.remove_at(i); - i--; - } - } -} - - -////////////////////////////////////////////////////////////////////////// -bool ScEngine::pauseAll() { - for (uint32 i = 0; i < _scripts.size(); i++) { - if (_scripts[i] != _currentScript) { - _scripts[i]->pause(); - } - } - - return STATUS_OK; -} - - -////////////////////////////////////////////////////////////////////////// -bool ScEngine::resumeAll() { - for (uint32 i = 0; i < _scripts.size(); i++) { - _scripts[i]->resume(); - } - - return STATUS_OK; -} - - -////////////////////////////////////////////////////////////////////////// -bool ScEngine::isValidScript(ScScript *script) { - for (uint32 i = 0; i < _scripts.size(); i++) { - if (_scripts[i] == script) { - return true; - } - } - return false; -} - -////////////////////////////////////////////////////////////////////////// -bool ScEngine::clearGlobals(bool includingNatives) { - _globals->CleanProps(includingNatives); - return STATUS_OK; -} - -////////////////////////////////////////////////////////////////////////// -void ScEngine::addScriptTime(const char *filename, uint32 time) { - if (!_isProfiling) { - return; - } - - AnsiString fileName = filename; - fileName.toLowercase(); - _scriptTimes[fileName] += time; -} - - -////////////////////////////////////////////////////////////////////////// -void ScEngine::enableProfiling() { - if (_isProfiling) { - return; - } - - // destroy old data, if any - _scriptTimes.clear(); - - _profilingStartTime = g_system->getMillis(); - _isProfiling = true; -} - - -////////////////////////////////////////////////////////////////////////// -void ScEngine::disableProfiling() { - if (!_isProfiling) { - return; - } - - dumpStats(); - _isProfiling = false; -} - - -////////////////////////////////////////////////////////////////////////// -void ScEngine::dumpStats() { - error("DumpStats not ported to ScummVM yet"); - /* uint32 totalTime = g_system->getMillis() - _profilingStartTime; - - typedef std::vector > TimeVector; - TimeVector times; - - ScriptTimes::iterator it; - for (it = _scriptTimes.begin(); it != _scriptTimes.end(); ++it) { - times.push_back(std::pair (it->_value, it->_key)); - } - std::sort(times.begin(), times.end()); - - - TimeVector::reverse_iterator tit; - - _gameRef->LOG(0, "***** Script profiling information: *****"); - _gameRef->LOG(0, " %-40s %fs", "Total execution time", (float)totalTime / 1000); - - for (tit = times.rbegin(); tit != times.rend(); ++tit) { - _gameRef->LOG(0, " %-40s %fs (%f%%)", tit->second.c_str(), (float)tit->first / 1000, (float)tit->first / (float)totalTime * 100); - }*/ -} - -} // end of namespace Wintermute +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +/* + * This file is based on WME Lite. + * http://dead-code.org/redir.php?target=wmelite + * Copyright (c) 2011 Jan Nedoma + */ + +#include "engines/wintermute/base/scriptables/script_engine.h" +#include "engines/wintermute/base/scriptables/script_value.h" +#include "engines/wintermute/base/scriptables/script.h" +#include "engines/wintermute/base/scriptables/script_stack.h" +#include "engines/wintermute/base/scriptables/script_ext_math.h" +#include "engines/wintermute/base/base_engine.h" +#include "engines/wintermute/base/base_game.h" +#include "engines/wintermute/base/sound/base_sound.h" +#include "engines/wintermute/base/base_file_manager.h" +#include "engines/wintermute/utils/utils.h" + +namespace Wintermute { + +IMPLEMENT_PERSISTENT(ScEngine, true) + +#define COMPILER_DLL "dcscomp.dll" +////////////////////////////////////////////////////////////////////////// +ScEngine::ScEngine(BaseGame *inGame) : BaseClass(inGame) { + _gameRef->LOG(0, "Initializing scripting engine..."); + + if (_compilerAvailable) { + _gameRef->LOG(0, " Script compiler bound successfuly"); + } else { + _gameRef->LOG(0, " Script compiler is NOT available"); + } + + _globals = new ScValue(_gameRef); + + + // register 'Game' as global variable + if (!_globals->propExists("Game")) { + ScValue val(_gameRef); + val.setNative(_gameRef, true); + _globals->setProp("Game", &val); + } + + // register 'Math' as global variable + if (!_globals->propExists("Math")) { + ScValue val(_gameRef); + val.setNative(_gameRef->_mathClass, true); + _globals->setProp("Math", &val); + } + + // prepare script cache + for (int i = 0; i < MAX_CACHED_SCRIPTS; i++) { + _cachedScripts[i] = NULL; + } + + _currentScript = NULL; + + _isProfiling = false; + _profilingStartTime = 0; + + //EnableProfiling(); +} + + +////////////////////////////////////////////////////////////////////////// +ScEngine::~ScEngine() { + _gameRef->LOG(0, "Shutting down scripting engine"); + + disableProfiling(); + + cleanup(); +} + + +////////////////////////////////////////////////////////////////////////// +bool ScEngine::cleanup() { + for (uint32 i = 0; i < _scripts.size(); i++) { + if (!_scripts[i]->_thread && _scripts[i]->_owner) { + _scripts[i]->_owner->removeScript(_scripts[i]); + } + delete _scripts[i]; + _scripts.remove_at(i); + i--; + } + + _scripts.clear(); + + delete _globals; + _globals = NULL; + + emptyScriptCache(); + + _currentScript = NULL; // ref only + + return STATUS_OK; +} + + +////////////////////////////////////////////////////////////////////////// +byte *ScEngine::loadFile(void *data, char *filename, uint32 *size) { + return BaseFileManager::getEngineInstance()->readWholeFile(filename, size); +} + + +////////////////////////////////////////////////////////////////////////// +void ScEngine::closeFile(void *data, byte *buffer) { + delete[] buffer; +} + + +////////////////////////////////////////////////////////////////////////// +void ScEngine::parseElement(void *data, int line, int type, void *elementData) { +} + + +////////////////////////////////////////////////////////////////////////// +ScScript *ScEngine::runScript(const char *filename, BaseScriptHolder *owner) { + byte *compBuffer; + uint32 compSize; + + // get script from cache + compBuffer = getCompiledScript(filename, &compSize); + if (!compBuffer) { + return NULL; + } + + // add new script + ScScript *script = new ScScript(_gameRef, this); + bool ret = script->create(filename, compBuffer, compSize, owner); + if (DID_FAIL(ret)) { + _gameRef->LOG(ret, "Error running script '%s'...", filename); + delete script; + return NULL; + } else { + // publish the "self" pseudo-variable + ScValue val(_gameRef); + if (owner) { + val.setNative(owner, true); + } else { + val.setNULL(); + } + + script->_globals->setProp("self", &val); + script->_globals->setProp("this", &val); + + _scripts.add(script); + + return script; + } +} + + +////////////////////////////////////////////////////////////////////////// +byte *ScEngine::getCompiledScript(const char *filename, uint32 *outSize, bool ignoreCache) { + // is script in cache? + if (!ignoreCache) { + for (int i = 0; i < MAX_CACHED_SCRIPTS; i++) { + if (_cachedScripts[i] && scumm_stricmp(_cachedScripts[i]->_filename.c_str(), filename) == 0) { + _cachedScripts[i]->_timestamp = g_system->getMillis(); + *outSize = _cachedScripts[i]->_size; + return _cachedScripts[i]->_buffer; + } + } + } + + // nope, load it + byte *compBuffer; + uint32 compSize; + + uint32 size; + + byte *buffer = BaseEngine::instance().getFileManager()->readWholeFile(filename, &size); + if (!buffer) { + _gameRef->LOG(0, "ScEngine::GetCompiledScript - error opening script '%s'", filename); + return NULL; + } + + // needs to be compiled? + if (FROM_LE_32(*(uint32 *)buffer) == SCRIPT_MAGIC) { + compBuffer = buffer; + compSize = size; + } else { + if (!_compilerAvailable) { + _gameRef->LOG(0, "ScEngine::GetCompiledScript - script '%s' needs to be compiled but compiler is not available", filename); + delete[] buffer; + return NULL; + } + // This code will never be called, since _compilerAvailable is const false. + // It's only here in the event someone would want to reinclude the compiler. + error("Script needs compilation, ScummVM does not contain a WME compiler"); + } + + byte *ret = NULL; + + // add script to cache + CScCachedScript *cachedScript = new CScCachedScript(filename, compBuffer, compSize); + if (cachedScript) { + int index = 0; + uint32 minTime = g_system->getMillis(); + for (int i = 0; i < MAX_CACHED_SCRIPTS; i++) { + if (_cachedScripts[i] == NULL) { + index = i; + break; + } else if (_cachedScripts[i]->_timestamp <= minTime) { + minTime = _cachedScripts[i]->_timestamp; + index = i; + } + } + + if (_cachedScripts[index] != NULL) { + delete _cachedScripts[index]; + } + _cachedScripts[index] = cachedScript; + + ret = cachedScript->_buffer; + *outSize = cachedScript->_size; + } + + + // cleanup + delete[] buffer; + + return ret; +} + + + +////////////////////////////////////////////////////////////////////////// +bool ScEngine::tick() { + if (_scripts.size() == 0) { + return STATUS_OK; + } + + + // resolve waiting scripts + for (uint32 i = 0; i < _scripts.size(); i++) { + + switch (_scripts[i]->_state) { + case SCRIPT_WAITING: { + /* + bool obj_found=false; + for(int j=0; j<_gameRef->_regObjects.size(); j++) + { + if (_gameRef->_regObjects[j] == _scripts[i]->_waitObject) + { + if (_gameRef->_regObjects[j]->IsReady()) _scripts[i]->Run(); + obj_found = true; + break; + } + } + if (!obj_found) _scripts[i]->finish(); // _waitObject no longer exists + */ + if (_gameRef->validObject(_scripts[i]->_waitObject)) { + if (_scripts[i]->_waitObject->isReady()) { + _scripts[i]->run(); + } + } else { + _scripts[i]->finish(); + } + break; + } + + case SCRIPT_SLEEPING: { + if (_scripts[i]->_waitFrozen) { + if (_scripts[i]->_waitTime <= g_system->getMillis()) { + _scripts[i]->run(); + } + } else { + if (_scripts[i]->_waitTime <= _gameRef->_timer) { + _scripts[i]->run(); + } + } + break; + } + + case SCRIPT_WAITING_SCRIPT: { + if (!isValidScript(_scripts[i]->_waitScript) || _scripts[i]->_waitScript->_state == SCRIPT_ERROR) { + // fake return value + _scripts[i]->_stack->pushNULL(); + _scripts[i]->_waitScript = NULL; + _scripts[i]->run(); + } else { + if (_scripts[i]->_waitScript->_state == SCRIPT_THREAD_FINISHED) { + // copy return value + _scripts[i]->_stack->push(_scripts[i]->_waitScript->_stack->pop()); + _scripts[i]->run(); + _scripts[i]->_waitScript->finish(); + _scripts[i]->_waitScript = NULL; + } + } + break; + } + default: + break; + } // switch + } // for each script + + + // execute scripts + for (uint32 i = 0; i < _scripts.size(); i++) { + + // skip paused scripts + if (_scripts[i]->_state == SCRIPT_PAUSED) { + continue; + } + + // time sliced script + if (_scripts[i]->_timeSlice > 0) { + uint32 startTime = g_system->getMillis(); + while (_scripts[i]->_state == SCRIPT_RUNNING && g_system->getMillis() - startTime < _scripts[i]->_timeSlice) { + _currentScript = _scripts[i]; + _scripts[i]->executeInstruction(); + } + if (_isProfiling && _scripts[i]->_filename) { + addScriptTime(_scripts[i]->_filename, g_system->getMillis() - startTime); + } + } + + // normal script + else { + uint32 startTime = 0; + bool isProfiling = _isProfiling; + if (isProfiling) { + startTime = g_system->getMillis(); + } + + while (_scripts[i]->_state == SCRIPT_RUNNING) { + _currentScript = _scripts[i]; + _scripts[i]->executeInstruction(); + } + if (isProfiling && _scripts[i]->_filename) { + addScriptTime(_scripts[i]->_filename, g_system->getMillis() - startTime); + } + } + _currentScript = NULL; + } + + removeFinishedScripts(); + + return STATUS_OK; +} + + +////////////////////////////////////////////////////////////////////////// +bool ScEngine::tickUnbreakable() { + // execute unbreakable scripts + for (uint32 i = 0; i < _scripts.size(); i++) { + if (!_scripts[i]->_unbreakable) { + continue; + } + + while (_scripts[i]->_state == SCRIPT_RUNNING) { + _currentScript = _scripts[i]; + _scripts[i]->executeInstruction(); + } + _scripts[i]->finish(); + _currentScript = NULL; + } + removeFinishedScripts(); + + return STATUS_OK; +} + + +////////////////////////////////////////////////////////////////////////// +bool ScEngine::removeFinishedScripts() { + // remove finished scripts + for (uint32 i = 0; i < _scripts.size(); i++) { + if (_scripts[i]->_state == SCRIPT_FINISHED || _scripts[i]->_state == SCRIPT_ERROR) { + if (!_scripts[i]->_thread && _scripts[i]->_owner) { + _scripts[i]->_owner->removeScript(_scripts[i]); + } + + delete _scripts[i]; + _scripts.remove_at(i); + i--; + } + } + return STATUS_OK; +} + + +////////////////////////////////////////////////////////////////////////// +int ScEngine::getNumScripts(int *running, int *waiting, int *persistent) { + int numRunning = 0, numWaiting = 0, numPersistent = 0, numTotal = 0; + + for (uint32 i = 0; i < _scripts.size(); i++) { + if (_scripts[i]->_state == SCRIPT_FINISHED) { + continue; + } + switch (_scripts[i]->_state) { + case SCRIPT_RUNNING: + case SCRIPT_SLEEPING: + case SCRIPT_PAUSED: + numRunning++; + break; + case SCRIPT_WAITING: + numWaiting++; + break; + case SCRIPT_PERSISTENT: + numPersistent++; + break; + default: + warning("ScEngine::GetNumScripts - unhandled enum"); + break; + } + numTotal++; + } + if (running) { + *running = numRunning; + } + if (waiting) { + *waiting = numWaiting; + } + if (persistent) { + *persistent = numPersistent; + } + + return numTotal; +} + + +////////////////////////////////////////////////////////////////////////// +bool ScEngine::emptyScriptCache() { + for (int i = 0; i < MAX_CACHED_SCRIPTS; i++) { + if (_cachedScripts[i]) { + delete _cachedScripts[i]; + _cachedScripts[i] = NULL; + } + } + return STATUS_OK; +} + + +////////////////////////////////////////////////////////////////////////// +bool ScEngine::resetObject(BaseObject *Object) { + // terminate all scripts waiting for this object + for (uint32 i = 0; i < _scripts.size(); i++) { + if (_scripts[i]->_state == SCRIPT_WAITING && _scripts[i]->_waitObject == Object) { + if (!_gameRef->_compatKillMethodThreads) { + resetScript(_scripts[i]); + } + + bool isThread = _scripts[i]->_methodThread || _scripts[i]->_thread; + _scripts[i]->finish(!isThread); // 1.9b1 - top-level script kills its threads as well + } + } + return STATUS_OK; +} + +////////////////////////////////////////////////////////////////////////// +bool ScEngine::resetScript(ScScript *script) { + // terminate all scripts waiting for this script + for (uint32 i = 0; i < _scripts.size(); i++) { + if (_scripts[i]->_state == SCRIPT_WAITING_SCRIPT && _scripts[i]->_waitScript == script) { + _scripts[i]->finish(); + } + } + return STATUS_OK; +} + +////////////////////////////////////////////////////////////////////////// +bool ScEngine::persist(BasePersistenceManager *persistMgr) { + if (!persistMgr->getIsSaving()) { + cleanup(); + } + + persistMgr->transfer(TMEMBER(_gameRef)); + persistMgr->transfer(TMEMBER(_currentScript)); + persistMgr->transfer(TMEMBER(_globals)); + _scripts.persist(persistMgr); + + return STATUS_OK; +} + + +////////////////////////////////////////////////////////////////////////// +void ScEngine::editorCleanup() { + for (uint32 i = 0; i < _scripts.size(); i++) { + if (_scripts[i]->_owner == NULL && (_scripts[i]->_state == SCRIPT_FINISHED || _scripts[i]->_state == SCRIPT_ERROR)) { + delete _scripts[i]; + _scripts.remove_at(i); + i--; + } + } +} + + +////////////////////////////////////////////////////////////////////////// +bool ScEngine::pauseAll() { + for (uint32 i = 0; i < _scripts.size(); i++) { + if (_scripts[i] != _currentScript) { + _scripts[i]->pause(); + } + } + + return STATUS_OK; +} + + +////////////////////////////////////////////////////////////////////////// +bool ScEngine::resumeAll() { + for (uint32 i = 0; i < _scripts.size(); i++) { + _scripts[i]->resume(); + } + + return STATUS_OK; +} + + +////////////////////////////////////////////////////////////////////////// +bool ScEngine::isValidScript(ScScript *script) { + for (uint32 i = 0; i < _scripts.size(); i++) { + if (_scripts[i] == script) { + return true; + } + } + return false; +} + +////////////////////////////////////////////////////////////////////////// +bool ScEngine::clearGlobals(bool includingNatives) { + _globals->CleanProps(includingNatives); + return STATUS_OK; +} + +////////////////////////////////////////////////////////////////////////// +void ScEngine::addScriptTime(const char *filename, uint32 time) { + if (!_isProfiling) { + return; + } + + AnsiString fileName = filename; + fileName.toLowercase(); + _scriptTimes[fileName] += time; +} + + +////////////////////////////////////////////////////////////////////////// +void ScEngine::enableProfiling() { + if (_isProfiling) { + return; + } + + // destroy old data, if any + _scriptTimes.clear(); + + _profilingStartTime = g_system->getMillis(); + _isProfiling = true; +} + + +////////////////////////////////////////////////////////////////////////// +void ScEngine::disableProfiling() { + if (!_isProfiling) { + return; + } + + dumpStats(); + _isProfiling = false; +} + + +////////////////////////////////////////////////////////////////////////// +void ScEngine::dumpStats() { + error("DumpStats not ported to ScummVM yet"); + /* uint32 totalTime = g_system->getMillis() - _profilingStartTime; + + typedef std::vector > TimeVector; + TimeVector times; + + ScriptTimes::iterator it; + for (it = _scriptTimes.begin(); it != _scriptTimes.end(); ++it) { + times.push_back(std::pair (it->_value, it->_key)); + } + std::sort(times.begin(), times.end()); + + + TimeVector::reverse_iterator tit; + + _gameRef->LOG(0, "***** Script profiling information: *****"); + _gameRef->LOG(0, " %-40s %fs", "Total execution time", (float)totalTime / 1000); + + for (tit = times.rbegin(); tit != times.rend(); ++tit) { + _gameRef->LOG(0, " %-40s %fs (%f%%)", tit->second.c_str(), (float)tit->first / 1000, (float)tit->first / (float)totalTime * 100); + }*/ +} + +} // end of namespace Wintermute diff --git a/engines/wintermute/base/scriptables/script_engine.h b/engines/wintermute/base/scriptables/script_engine.h index f526353368..1a023326eb 100644 --- a/engines/wintermute/base/scriptables/script_engine.h +++ b/engines/wintermute/base/scriptables/script_engine.h @@ -1,135 +1,135 @@ -/* ScummVM - Graphic Adventure Engine - * - * ScummVM is the legal property of its developers, whose names - * are too numerous to list here. Please refer to the COPYRIGHT - * file distributed with this source distribution. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - */ - -/* - * This file is based on WME Lite. - * http://dead-code.org/redir.php?target=wmelite - * Copyright (c) 2011 Jan Nedoma - */ - -#ifndef WINTERMUTE_SCENGINE_H -#define WINTERMUTE_SCENGINE_H - -#include "engines/wintermute/persistent.h" -#include "engines/wintermute/coll_templ.h" -#include "engines/wintermute/base/base.h" - -namespace Wintermute { - -#define MAX_CACHED_SCRIPTS 20 -class ScScript; -class ScValue; -class BaseObject; -class BaseScriptHolder; -class ScEngine : public BaseClass { -public: - class CScCachedScript { - public: - CScCachedScript(const char *filename, byte *buffer, uint32 size) { - _timestamp = g_system->getMillis(); - _buffer = new byte[size]; - if (_buffer) { - memcpy(_buffer, buffer, size); - } - _size = size; - _filename = filename; - }; - - ~CScCachedScript() { - if (_buffer) { - delete[] _buffer; - } - }; - - uint32 _timestamp; - byte *_buffer; - uint32 _size; - Common::String _filename; - }; - - class CScBreakpoint { - public: - CScBreakpoint(const char *filename) { - _filename = filename; - } - - ~CScBreakpoint() { - _lines.clear(); - } - - Common::String _filename; - BaseArray _lines; - }; - -public: - bool clearGlobals(bool includingNatives = false); - bool tickUnbreakable(); - bool removeFinishedScripts(); - bool isValidScript(ScScript *script); - - ScScript *_currentScript; - bool resumeAll(); - bool pauseAll(); - void editorCleanup(); - bool resetObject(BaseObject *Object); - bool resetScript(ScScript *script); - bool emptyScriptCache(); - byte *getCompiledScript(const char *filename, uint32 *outSize, bool ignoreCache = false); - DECLARE_PERSISTENT(ScEngine, BaseClass) - bool cleanup(); - int getNumScripts(int *running = NULL, int *waiting = NULL, int *persistent = NULL); - bool tick(); - ScValue *_globals; - ScScript *runScript(const char *filename, BaseScriptHolder *owner = NULL); - static const bool _compilerAvailable = false; - - ScEngine(BaseGame *inGame); - virtual ~ScEngine(); - static byte *loadFile(void *data, char *filename, uint32 *size); - static void closeFile(void *data, byte *buffer); - static void parseElement(void *data, int line, int type, void *elementData); - - BaseArray _scripts; - - void enableProfiling(); - void disableProfiling(); - bool getIsProfiling() { - return _isProfiling; - } - - void addScriptTime(const char *filename, uint32 Time); - void dumpStats(); - -private: - - CScCachedScript *_cachedScripts[MAX_CACHED_SCRIPTS]; - bool _isProfiling; - uint32 _profilingStartTime; - - typedef Common::HashMap ScriptTimes; - ScriptTimes _scriptTimes; - -}; - -} // end of namespace Wintermute - -#endif +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +/* + * This file is based on WME Lite. + * http://dead-code.org/redir.php?target=wmelite + * Copyright (c) 2011 Jan Nedoma + */ + +#ifndef WINTERMUTE_SCENGINE_H +#define WINTERMUTE_SCENGINE_H + +#include "engines/wintermute/persistent.h" +#include "engines/wintermute/coll_templ.h" +#include "engines/wintermute/base/base.h" + +namespace Wintermute { + +#define MAX_CACHED_SCRIPTS 20 +class ScScript; +class ScValue; +class BaseObject; +class BaseScriptHolder; +class ScEngine : public BaseClass { +public: + class CScCachedScript { + public: + CScCachedScript(const char *filename, byte *buffer, uint32 size) { + _timestamp = g_system->getMillis(); + _buffer = new byte[size]; + if (_buffer) { + memcpy(_buffer, buffer, size); + } + _size = size; + _filename = filename; + }; + + ~CScCachedScript() { + if (_buffer) { + delete[] _buffer; + } + }; + + uint32 _timestamp; + byte *_buffer; + uint32 _size; + Common::String _filename; + }; + + class CScBreakpoint { + public: + CScBreakpoint(const char *filename) { + _filename = filename; + } + + ~CScBreakpoint() { + _lines.clear(); + } + + Common::String _filename; + BaseArray _lines; + }; + +public: + bool clearGlobals(bool includingNatives = false); + bool tickUnbreakable(); + bool removeFinishedScripts(); + bool isValidScript(ScScript *script); + + ScScript *_currentScript; + bool resumeAll(); + bool pauseAll(); + void editorCleanup(); + bool resetObject(BaseObject *Object); + bool resetScript(ScScript *script); + bool emptyScriptCache(); + byte *getCompiledScript(const char *filename, uint32 *outSize, bool ignoreCache = false); + DECLARE_PERSISTENT(ScEngine, BaseClass) + bool cleanup(); + int getNumScripts(int *running = NULL, int *waiting = NULL, int *persistent = NULL); + bool tick(); + ScValue *_globals; + ScScript *runScript(const char *filename, BaseScriptHolder *owner = NULL); + static const bool _compilerAvailable = false; + + ScEngine(BaseGame *inGame); + virtual ~ScEngine(); + static byte *loadFile(void *data, char *filename, uint32 *size); + static void closeFile(void *data, byte *buffer); + static void parseElement(void *data, int line, int type, void *elementData); + + BaseArray _scripts; + + void enableProfiling(); + void disableProfiling(); + bool getIsProfiling() { + return _isProfiling; + } + + void addScriptTime(const char *filename, uint32 Time); + void dumpStats(); + +private: + + CScCachedScript *_cachedScripts[MAX_CACHED_SCRIPTS]; + bool _isProfiling; + uint32 _profilingStartTime; + + typedef Common::HashMap ScriptTimes; + ScriptTimes _scriptTimes; + +}; + +} // end of namespace Wintermute + +#endif diff --git a/engines/wintermute/base/scriptables/script_ext_array.cpp b/engines/wintermute/base/scriptables/script_ext_array.cpp index c8d4f64ae9..5ed07f0da6 100644 --- a/engines/wintermute/base/scriptables/script_ext_array.cpp +++ b/engines/wintermute/base/scriptables/script_ext_array.cpp @@ -1,252 +1,252 @@ -/* ScummVM - Graphic Adventure Engine - * - * ScummVM is the legal property of its developers, whose names - * are too numerous to list here. Please refer to the COPYRIGHT - * file distributed with this source distribution. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - */ - -/* - * This file is based on WME Lite. - * http://dead-code.org/redir.php?target=wmelite - * Copyright (c) 2011 Jan Nedoma - */ - -#include "engines/wintermute/persistent.h" -#include "engines/wintermute/base/scriptables/script_value.h" -#include "engines/wintermute/base/scriptables/script_stack.h" -#include "engines/wintermute/system/sys_instance.h" -#include "engines/wintermute/base/scriptables/script_ext_array.h" - -namespace Wintermute { - -IMPLEMENT_PERSISTENT(SXArray, false) - -BaseScriptable *makeSXArray(BaseGame *inGame, ScStack *stack) { - return new SXArray(inGame, stack); -} - -////////////////////////////////////////////////////////////////////////// -SXArray::SXArray(BaseGame *inGame, ScStack *stack) : BaseScriptable(inGame) { - _length = 0; - _values = new ScValue(_gameRef); - - int numParams = stack->pop()->getInt(0); - - if (numParams == 1) { - _length = stack->pop()->getInt(0); - } else if (numParams > 1) { - _length = numParams; - char paramName[20]; - for (int i = 0; i < numParams; i++) { - sprintf(paramName, "%d", i); - _values->setProp(paramName, stack->pop()); - } - } -} - -////////////////////////////////////////////////////////////////////////// -SXArray::SXArray(BaseGame *inGame) : BaseScriptable(inGame) { - _length = 0; - _values = new ScValue(_gameRef); -} - - -////////////////////////////////////////////////////////////////////////// -SXArray::~SXArray() { - delete _values; - _values = NULL; -} - - -////////////////////////////////////////////////////////////////////////// -const char *SXArray::scToString() { - char dummy[32768]; - strcpy(dummy, ""); - char propName[20]; - for (int i = 0; i < _length; i++) { - sprintf(propName, "%d", i); - ScValue *val = _values->getProp(propName); - if (val) { - if (strlen(dummy) + strlen(val->getString()) < 32768) { - strcat(dummy, val->getString()); - } - } - - if (i < _length - 1 && strlen(dummy) + 1 < 32768) { - strcat(dummy, ","); - } - } - _strRep = dummy; - return _strRep.c_str(); -} - - -////////////////////////////////////////////////////////////////////////// -bool SXArray::scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack, const char *name) { - ////////////////////////////////////////////////////////////////////////// - // Push - ////////////////////////////////////////////////////////////////////////// - if (strcmp(name, "Push") == 0) { - int numParams = stack->pop()->getInt(0); - char paramName[20]; - - for (int i = 0; i < numParams; i++) { - _length++; - sprintf(paramName, "%d", _length - 1); - _values->setProp(paramName, stack->pop(), true); - } - stack->pushInt(_length); - - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // Pop - ////////////////////////////////////////////////////////////////////////// - if (strcmp(name, "Pop") == 0) { - - stack->correctParams(0); - - if (_length > 0) { - char paramName[20]; - sprintf(paramName, "%d", _length - 1); - stack->push(_values->getProp(paramName)); - _values->deleteProp(paramName); - _length--; - } else { - stack->pushNULL(); - } - - return STATUS_OK; - } else { - return STATUS_FAILED; - } -} - - -////////////////////////////////////////////////////////////////////////// -ScValue *SXArray::scGetProperty(const char *name) { - _scValue->setNULL(); - - ////////////////////////////////////////////////////////////////////////// - // Type - ////////////////////////////////////////////////////////////////////////// - if (strcmp(name, "Type") == 0) { - _scValue->setString("array"); - return _scValue; - } - - ////////////////////////////////////////////////////////////////////////// - // Length - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "Length") == 0) { - _scValue->setInt(_length); - return _scValue; - } - - ////////////////////////////////////////////////////////////////////////// - // [number] - ////////////////////////////////////////////////////////////////////////// - else { - char paramName[20]; - if (validNumber(name, paramName)) { - return _values->getProp(paramName); - } else { - return _scValue; - } - } -} - - -////////////////////////////////////////////////////////////////////////// -bool SXArray::scSetProperty(const char *name, ScValue *value) { - ////////////////////////////////////////////////////////////////////////// - // Length - ////////////////////////////////////////////////////////////////////////// - if (strcmp(name, "Length") == 0) { - int origLength = _length; - _length = MAX(value->getInt(0), 0); - - char propName[20]; - if (_length < origLength) { - for (int i = _length; i < origLength; i++) { - sprintf(propName, "%d", i); - _values->deleteProp(propName); - } - } - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // [number] - ////////////////////////////////////////////////////////////////////////// - else { - char paramName[20]; - if (validNumber(name, paramName)) { - int index = atoi(paramName); - if (index >= _length) { - _length = index + 1; - } - return _values->setProp(paramName, value); - } else { - return STATUS_FAILED; - } - } -} - - -////////////////////////////////////////////////////////////////////////// -bool SXArray::persist(BasePersistenceManager *persistMgr) { - BaseScriptable::persist(persistMgr); - - persistMgr->transfer(TMEMBER(_length)); - persistMgr->transfer(TMEMBER(_values)); - - return STATUS_OK; -} - - -////////////////////////////////////////////////////////////////////////// -bool SXArray::validNumber(const char *origStr, char *outStr) { - bool isNumber = true; - for (uint32 i = 0; i < strlen(origStr); i++) { - if (!(origStr[i] >= '0' && origStr[i] <= '9')) { - isNumber = false; - break; - } - } - - if (isNumber) { - int index = atoi(origStr); - sprintf(outStr, "%d", index); - return true; - } else { - return false; - } -} - -////////////////////////////////////////////////////////////////////////// -bool SXArray::push(ScValue *val) { - char paramName[20]; - _length++; - sprintf(paramName, "%d", _length - 1); - _values->setProp(paramName, val, true); - return STATUS_OK; -} - -} // end of namespace Wintermute +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +/* + * This file is based on WME Lite. + * http://dead-code.org/redir.php?target=wmelite + * Copyright (c) 2011 Jan Nedoma + */ + +#include "engines/wintermute/persistent.h" +#include "engines/wintermute/base/scriptables/script_value.h" +#include "engines/wintermute/base/scriptables/script_stack.h" +#include "engines/wintermute/system/sys_instance.h" +#include "engines/wintermute/base/scriptables/script_ext_array.h" + +namespace Wintermute { + +IMPLEMENT_PERSISTENT(SXArray, false) + +BaseScriptable *makeSXArray(BaseGame *inGame, ScStack *stack) { + return new SXArray(inGame, stack); +} + +////////////////////////////////////////////////////////////////////////// +SXArray::SXArray(BaseGame *inGame, ScStack *stack) : BaseScriptable(inGame) { + _length = 0; + _values = new ScValue(_gameRef); + + int numParams = stack->pop()->getInt(0); + + if (numParams == 1) { + _length = stack->pop()->getInt(0); + } else if (numParams > 1) { + _length = numParams; + char paramName[20]; + for (int i = 0; i < numParams; i++) { + sprintf(paramName, "%d", i); + _values->setProp(paramName, stack->pop()); + } + } +} + +////////////////////////////////////////////////////////////////////////// +SXArray::SXArray(BaseGame *inGame) : BaseScriptable(inGame) { + _length = 0; + _values = new ScValue(_gameRef); +} + + +////////////////////////////////////////////////////////////////////////// +SXArray::~SXArray() { + delete _values; + _values = NULL; +} + + +////////////////////////////////////////////////////////////////////////// +const char *SXArray::scToString() { + char dummy[32768]; + strcpy(dummy, ""); + char propName[20]; + for (int i = 0; i < _length; i++) { + sprintf(propName, "%d", i); + ScValue *val = _values->getProp(propName); + if (val) { + if (strlen(dummy) + strlen(val->getString()) < 32768) { + strcat(dummy, val->getString()); + } + } + + if (i < _length - 1 && strlen(dummy) + 1 < 32768) { + strcat(dummy, ","); + } + } + _strRep = dummy; + return _strRep.c_str(); +} + + +////////////////////////////////////////////////////////////////////////// +bool SXArray::scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack, const char *name) { + ////////////////////////////////////////////////////////////////////////// + // Push + ////////////////////////////////////////////////////////////////////////// + if (strcmp(name, "Push") == 0) { + int numParams = stack->pop()->getInt(0); + char paramName[20]; + + for (int i = 0; i < numParams; i++) { + _length++; + sprintf(paramName, "%d", _length - 1); + _values->setProp(paramName, stack->pop(), true); + } + stack->pushInt(_length); + + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // Pop + ////////////////////////////////////////////////////////////////////////// + if (strcmp(name, "Pop") == 0) { + + stack->correctParams(0); + + if (_length > 0) { + char paramName[20]; + sprintf(paramName, "%d", _length - 1); + stack->push(_values->getProp(paramName)); + _values->deleteProp(paramName); + _length--; + } else { + stack->pushNULL(); + } + + return STATUS_OK; + } else { + return STATUS_FAILED; + } +} + + +////////////////////////////////////////////////////////////////////////// +ScValue *SXArray::scGetProperty(const char *name) { + _scValue->setNULL(); + + ////////////////////////////////////////////////////////////////////////// + // Type + ////////////////////////////////////////////////////////////////////////// + if (strcmp(name, "Type") == 0) { + _scValue->setString("array"); + return _scValue; + } + + ////////////////////////////////////////////////////////////////////////// + // Length + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "Length") == 0) { + _scValue->setInt(_length); + return _scValue; + } + + ////////////////////////////////////////////////////////////////////////// + // [number] + ////////////////////////////////////////////////////////////////////////// + else { + char paramName[20]; + if (validNumber(name, paramName)) { + return _values->getProp(paramName); + } else { + return _scValue; + } + } +} + + +////////////////////////////////////////////////////////////////////////// +bool SXArray::scSetProperty(const char *name, ScValue *value) { + ////////////////////////////////////////////////////////////////////////// + // Length + ////////////////////////////////////////////////////////////////////////// + if (strcmp(name, "Length") == 0) { + int origLength = _length; + _length = MAX(value->getInt(0), 0); + + char propName[20]; + if (_length < origLength) { + for (int i = _length; i < origLength; i++) { + sprintf(propName, "%d", i); + _values->deleteProp(propName); + } + } + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // [number] + ////////////////////////////////////////////////////////////////////////// + else { + char paramName[20]; + if (validNumber(name, paramName)) { + int index = atoi(paramName); + if (index >= _length) { + _length = index + 1; + } + return _values->setProp(paramName, value); + } else { + return STATUS_FAILED; + } + } +} + + +////////////////////////////////////////////////////////////////////////// +bool SXArray::persist(BasePersistenceManager *persistMgr) { + BaseScriptable::persist(persistMgr); + + persistMgr->transfer(TMEMBER(_length)); + persistMgr->transfer(TMEMBER(_values)); + + return STATUS_OK; +} + + +////////////////////////////////////////////////////////////////////////// +bool SXArray::validNumber(const char *origStr, char *outStr) { + bool isNumber = true; + for (uint32 i = 0; i < strlen(origStr); i++) { + if (!(origStr[i] >= '0' && origStr[i] <= '9')) { + isNumber = false; + break; + } + } + + if (isNumber) { + int index = atoi(origStr); + sprintf(outStr, "%d", index); + return true; + } else { + return false; + } +} + +////////////////////////////////////////////////////////////////////////// +bool SXArray::push(ScValue *val) { + char paramName[20]; + _length++; + sprintf(paramName, "%d", _length - 1); + _values->setProp(paramName, val, true); + return STATUS_OK; +} + +} // end of namespace Wintermute diff --git a/engines/wintermute/base/scriptables/script_ext_array.h b/engines/wintermute/base/scriptables/script_ext_array.h index 614f829950..d9805ef94f 100644 --- a/engines/wintermute/base/scriptables/script_ext_array.h +++ b/engines/wintermute/base/scriptables/script_ext_array.h @@ -1,56 +1,56 @@ -/* ScummVM - Graphic Adventure Engine - * - * ScummVM is the legal property of its developers, whose names - * are too numerous to list here. Please refer to the COPYRIGHT - * file distributed with this source distribution. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - */ - -/* - * This file is based on WME Lite. - * http://dead-code.org/redir.php?target=wmelite - * Copyright (c) 2011 Jan Nedoma - */ - -#ifndef WINTERMUTE_SXARRAY_H -#define WINTERMUTE_SXARRAY_H - -#include "engines/wintermute/base/base_scriptable.h" - -namespace Wintermute { - -class SXArray : public BaseScriptable { -public: - bool push(ScValue *val); - bool validNumber(const char *origStr, char *outStr); - DECLARE_PERSISTENT(SXArray, BaseScriptable) - SXArray(BaseGame *inGame, ScStack *stack); - SXArray(BaseGame *inGame); - virtual ~SXArray(); - ScValue *scGetProperty(const char *name); - bool scSetProperty(const char *name, ScValue *value); - bool scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack, const char *name); - const char *scToString(); -private: - int _length; - ScValue *_values; - Common::String _strRep; -}; - -} // end of namespace Wintermute - -#endif +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +/* + * This file is based on WME Lite. + * http://dead-code.org/redir.php?target=wmelite + * Copyright (c) 2011 Jan Nedoma + */ + +#ifndef WINTERMUTE_SXARRAY_H +#define WINTERMUTE_SXARRAY_H + +#include "engines/wintermute/base/base_scriptable.h" + +namespace Wintermute { + +class SXArray : public BaseScriptable { +public: + bool push(ScValue *val); + bool validNumber(const char *origStr, char *outStr); + DECLARE_PERSISTENT(SXArray, BaseScriptable) + SXArray(BaseGame *inGame, ScStack *stack); + SXArray(BaseGame *inGame); + virtual ~SXArray(); + ScValue *scGetProperty(const char *name); + bool scSetProperty(const char *name, ScValue *value); + bool scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack, const char *name); + const char *scToString(); +private: + int _length; + ScValue *_values; + Common::String _strRep; +}; + +} // end of namespace Wintermute + +#endif diff --git a/engines/wintermute/base/scriptables/script_ext_date.cpp b/engines/wintermute/base/scriptables/script_ext_date.cpp index 7726015081..11eead3b9c 100644 --- a/engines/wintermute/base/scriptables/script_ext_date.cpp +++ b/engines/wintermute/base/scriptables/script_ext_date.cpp @@ -1,293 +1,293 @@ -/* ScummVM - Graphic Adventure Engine - * - * ScummVM is the legal property of its developers, whose names - * are too numerous to list here. Please refer to the COPYRIGHT - * file distributed with this source distribution. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - */ - -/* - * This file is based on WME Lite. - * http://dead-code.org/redir.php?target=wmelite - * Copyright (c) 2011 Jan Nedoma - */ - -#include "engines/wintermute/base/scriptables/script_stack.h" -#include "engines/wintermute/base/scriptables/script_value.h" -#include "engines/wintermute/base/scriptables/script_ext_date.h" - -namespace Wintermute { - -IMPLEMENT_PERSISTENT(SXDate, false) - -BaseScriptable *makeSXDate(BaseGame *inGame, ScStack *stack) { - return new SXDate(inGame, stack); -} - -////////////////////////////////////////////////////////////////////////// -SXDate::SXDate(BaseGame *inGame, ScStack *stack) : BaseScriptable(inGame) { - stack->correctParams(6); - - memset(&_tm, 0, sizeof(_tm)); - - ScValue *valYear = stack->pop(); - _tm.tm_year = valYear->getInt() - 1900; - _tm.tm_mon = stack->pop()->getInt() - 1; - _tm.tm_mday = stack->pop()->getInt(); - _tm.tm_hour = stack->pop()->getInt(); - _tm.tm_min = stack->pop()->getInt(); - _tm.tm_sec = stack->pop()->getInt(); - - if (valYear->isNULL()) { - g_system->getTimeAndDate(_tm); - } -} - - -////////////////////////////////////////////////////////////////////////// -SXDate::~SXDate() { - -} - -////////////////////////////////////////////////////////////////////////// -const char *SXDate::scToString() { - // TODO: Make this more stringy, and less ISO 8601-like - _strRep.format("%04d-%02d-%02d - %02d:%02d:%02d", _tm.tm_year, _tm.tm_mon, _tm.tm_mday, _tm.tm_hour, _tm.tm_min, _tm.tm_sec); - return _strRep.c_str(); - //return asctime(&_tm); -} - - -////////////////////////////////////////////////////////////////////////// -bool SXDate::scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack, const char *name) { - ////////////////////////////////////////////////////////////////////////// - // GetYear - ////////////////////////////////////////////////////////////////////////// - if (strcmp(name, "GetYear") == 0) { - stack->correctParams(0); - stack->pushInt(_tm.tm_year + 1900); - return STATUS_OK; - } - ////////////////////////////////////////////////////////////////////////// - // GetMonth - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "GetMonth") == 0) { - stack->correctParams(0); - stack->pushInt(_tm.tm_mon + 1); - return STATUS_OK; - } - ////////////////////////////////////////////////////////////////////////// - // GetDate - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "GetDate") == 0) { - stack->correctParams(0); - stack->pushInt(_tm.tm_mday); - return STATUS_OK; - } - ////////////////////////////////////////////////////////////////////////// - // GetHours - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "GetHours") == 0) { - stack->correctParams(0); - stack->pushInt(_tm.tm_hour); - return STATUS_OK; - } - ////////////////////////////////////////////////////////////////////////// - // GetMinutes - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "GetMinutes") == 0) { - stack->correctParams(0); - stack->pushInt(_tm.tm_min); - return STATUS_OK; - } - ////////////////////////////////////////////////////////////////////////// - // GetSeconds - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "GetSeconds") == 0) { - stack->correctParams(0); - stack->pushInt(_tm.tm_sec); - return STATUS_OK; - } - ////////////////////////////////////////////////////////////////////////// - // GetWeekday - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "GetWeekday") == 0) { - stack->correctParams(0); - stack->pushInt(_tm.tm_wday); - return STATUS_OK; - } - - - ////////////////////////////////////////////////////////////////////////// - // SetYear - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "SetYear") == 0) { - stack->correctParams(1); - _tm.tm_year = stack->pop()->getInt() - 1900; - stack->pushNULL(); - return STATUS_OK; - } - ////////////////////////////////////////////////////////////////////////// - // SetMonth - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "SetMonth") == 0) { - stack->correctParams(1); - _tm.tm_mon = stack->pop()->getInt() - 1; - stack->pushNULL(); - return STATUS_OK; - } - ////////////////////////////////////////////////////////////////////////// - // SetDate - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "SetDate") == 0) { - stack->correctParams(1); - _tm.tm_mday = stack->pop()->getInt(); - stack->pushNULL(); - return STATUS_OK; - } - ////////////////////////////////////////////////////////////////////////// - // SetHours - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "SetHours") == 0) { - stack->correctParams(1); - _tm.tm_hour = stack->pop()->getInt(); - stack->pushNULL(); - return STATUS_OK; - } - ////////////////////////////////////////////////////////////////////////// - // SetMinutes - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "SetMinutes") == 0) { - stack->correctParams(1); - _tm.tm_min = stack->pop()->getInt(); - stack->pushNULL(); - return STATUS_OK; - } - ////////////////////////////////////////////////////////////////////////// - // SetSeconds - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "SetSeconds") == 0) { - stack->correctParams(1); - _tm.tm_sec = stack->pop()->getInt(); - stack->pushNULL(); - return STATUS_OK; - } - - - ////////////////////////////////////////////////////////////////////////// - // SetCurrentTime - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "SetCurrentTime") == 0) { - stack->correctParams(0); - g_system->getTimeAndDate(_tm); - stack->pushNULL(); - return STATUS_OK; - } else { - return STATUS_FAILED; - } -} - - -////////////////////////////////////////////////////////////////////////// -ScValue *SXDate::scGetProperty(const char *name) { - _scValue->setNULL(); - - ////////////////////////////////////////////////////////////////////////// - // Type - ////////////////////////////////////////////////////////////////////////// - if (strcmp(name, "Type") == 0) { - _scValue->setString("date"); - return _scValue; - } else { - return _scValue; - } -} - - -////////////////////////////////////////////////////////////////////////// -bool SXDate::scSetProperty(const char *name, ScValue *value) { - /* - ////////////////////////////////////////////////////////////////////////// - // Name - ////////////////////////////////////////////////////////////////////////// - if (strcmp(name, "Name")==0){ - setName(value->getString()); - return STATUS_OK; - } - - else*/ return STATUS_FAILED; -} - - -////////////////////////////////////////////////////////////////////////// -bool SXDate::persist(BasePersistenceManager *persistMgr) { - - BaseScriptable::persist(persistMgr); - persistMgr->transfer(TMEMBER(_tm.tm_year)); - persistMgr->transfer(TMEMBER(_tm.tm_mon)); - persistMgr->transfer(TMEMBER(_tm.tm_mday)); - persistMgr->transfer(TMEMBER(_tm.tm_hour)); - persistMgr->transfer(TMEMBER(_tm.tm_min)); - persistMgr->transfer(TMEMBER(_tm.tm_sec)); - return STATUS_OK; -} - -////////////////////////////////////////////////////////////////////////// -int SXDate::scCompare(BaseScriptable *Value) { - TimeDate time1 = _tm; - TimeDate time2 = ((SXDate *)Value)->_tm; - - if (time1.tm_year < time2.tm_year) { - return -1; - } else if (time1.tm_year == time2.tm_year) { - if (time1.tm_mon < time2.tm_mon) { - return -1; - } else if (time1.tm_mon == time2.tm_mon) { - if (time1.tm_mday < time2.tm_mday) { - return -1; - } else if (time1.tm_mday == time2.tm_mday) { - if (time1.tm_hour < time2.tm_hour) { - return -1; - } else if (time1.tm_hour == time2.tm_hour) { - if (time1.tm_min < time2.tm_min) { - return -1; - } else if (time1.tm_min == time2.tm_min) { - if (time1.tm_sec < time2.tm_sec) { - return -1; - } else if (time1.tm_sec == time2.tm_sec) { - return 0; // Equal - } else { - return 1; // Sec - } - } else { - return 1; // Minute - } - } else { - return 1; // Hour - } - } else { - return 1; // Day - } - } else { - return 1; // Month - } - } else { - return 1; // Year - } -} - -} // end of namespace Wintermute +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +/* + * This file is based on WME Lite. + * http://dead-code.org/redir.php?target=wmelite + * Copyright (c) 2011 Jan Nedoma + */ + +#include "engines/wintermute/base/scriptables/script_stack.h" +#include "engines/wintermute/base/scriptables/script_value.h" +#include "engines/wintermute/base/scriptables/script_ext_date.h" + +namespace Wintermute { + +IMPLEMENT_PERSISTENT(SXDate, false) + +BaseScriptable *makeSXDate(BaseGame *inGame, ScStack *stack) { + return new SXDate(inGame, stack); +} + +////////////////////////////////////////////////////////////////////////// +SXDate::SXDate(BaseGame *inGame, ScStack *stack) : BaseScriptable(inGame) { + stack->correctParams(6); + + memset(&_tm, 0, sizeof(_tm)); + + ScValue *valYear = stack->pop(); + _tm.tm_year = valYear->getInt() - 1900; + _tm.tm_mon = stack->pop()->getInt() - 1; + _tm.tm_mday = stack->pop()->getInt(); + _tm.tm_hour = stack->pop()->getInt(); + _tm.tm_min = stack->pop()->getInt(); + _tm.tm_sec = stack->pop()->getInt(); + + if (valYear->isNULL()) { + g_system->getTimeAndDate(_tm); + } +} + + +////////////////////////////////////////////////////////////////////////// +SXDate::~SXDate() { + +} + +////////////////////////////////////////////////////////////////////////// +const char *SXDate::scToString() { + // TODO: Make this more stringy, and less ISO 8601-like + _strRep.format("%04d-%02d-%02d - %02d:%02d:%02d", _tm.tm_year, _tm.tm_mon, _tm.tm_mday, _tm.tm_hour, _tm.tm_min, _tm.tm_sec); + return _strRep.c_str(); + //return asctime(&_tm); +} + + +////////////////////////////////////////////////////////////////////////// +bool SXDate::scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack, const char *name) { + ////////////////////////////////////////////////////////////////////////// + // GetYear + ////////////////////////////////////////////////////////////////////////// + if (strcmp(name, "GetYear") == 0) { + stack->correctParams(0); + stack->pushInt(_tm.tm_year + 1900); + return STATUS_OK; + } + ////////////////////////////////////////////////////////////////////////// + // GetMonth + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "GetMonth") == 0) { + stack->correctParams(0); + stack->pushInt(_tm.tm_mon + 1); + return STATUS_OK; + } + ////////////////////////////////////////////////////////////////////////// + // GetDate + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "GetDate") == 0) { + stack->correctParams(0); + stack->pushInt(_tm.tm_mday); + return STATUS_OK; + } + ////////////////////////////////////////////////////////////////////////// + // GetHours + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "GetHours") == 0) { + stack->correctParams(0); + stack->pushInt(_tm.tm_hour); + return STATUS_OK; + } + ////////////////////////////////////////////////////////////////////////// + // GetMinutes + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "GetMinutes") == 0) { + stack->correctParams(0); + stack->pushInt(_tm.tm_min); + return STATUS_OK; + } + ////////////////////////////////////////////////////////////////////////// + // GetSeconds + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "GetSeconds") == 0) { + stack->correctParams(0); + stack->pushInt(_tm.tm_sec); + return STATUS_OK; + } + ////////////////////////////////////////////////////////////////////////// + // GetWeekday + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "GetWeekday") == 0) { + stack->correctParams(0); + stack->pushInt(_tm.tm_wday); + return STATUS_OK; + } + + + ////////////////////////////////////////////////////////////////////////// + // SetYear + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "SetYear") == 0) { + stack->correctParams(1); + _tm.tm_year = stack->pop()->getInt() - 1900; + stack->pushNULL(); + return STATUS_OK; + } + ////////////////////////////////////////////////////////////////////////// + // SetMonth + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "SetMonth") == 0) { + stack->correctParams(1); + _tm.tm_mon = stack->pop()->getInt() - 1; + stack->pushNULL(); + return STATUS_OK; + } + ////////////////////////////////////////////////////////////////////////// + // SetDate + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "SetDate") == 0) { + stack->correctParams(1); + _tm.tm_mday = stack->pop()->getInt(); + stack->pushNULL(); + return STATUS_OK; + } + ////////////////////////////////////////////////////////////////////////// + // SetHours + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "SetHours") == 0) { + stack->correctParams(1); + _tm.tm_hour = stack->pop()->getInt(); + stack->pushNULL(); + return STATUS_OK; + } + ////////////////////////////////////////////////////////////////////////// + // SetMinutes + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "SetMinutes") == 0) { + stack->correctParams(1); + _tm.tm_min = stack->pop()->getInt(); + stack->pushNULL(); + return STATUS_OK; + } + ////////////////////////////////////////////////////////////////////////// + // SetSeconds + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "SetSeconds") == 0) { + stack->correctParams(1); + _tm.tm_sec = stack->pop()->getInt(); + stack->pushNULL(); + return STATUS_OK; + } + + + ////////////////////////////////////////////////////////////////////////// + // SetCurrentTime + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "SetCurrentTime") == 0) { + stack->correctParams(0); + g_system->getTimeAndDate(_tm); + stack->pushNULL(); + return STATUS_OK; + } else { + return STATUS_FAILED; + } +} + + +////////////////////////////////////////////////////////////////////////// +ScValue *SXDate::scGetProperty(const char *name) { + _scValue->setNULL(); + + ////////////////////////////////////////////////////////////////////////// + // Type + ////////////////////////////////////////////////////////////////////////// + if (strcmp(name, "Type") == 0) { + _scValue->setString("date"); + return _scValue; + } else { + return _scValue; + } +} + + +////////////////////////////////////////////////////////////////////////// +bool SXDate::scSetProperty(const char *name, ScValue *value) { + /* + ////////////////////////////////////////////////////////////////////////// + // Name + ////////////////////////////////////////////////////////////////////////// + if (strcmp(name, "Name")==0){ + setName(value->getString()); + return STATUS_OK; + } + + else*/ return STATUS_FAILED; +} + + +////////////////////////////////////////////////////////////////////////// +bool SXDate::persist(BasePersistenceManager *persistMgr) { + + BaseScriptable::persist(persistMgr); + persistMgr->transfer(TMEMBER(_tm.tm_year)); + persistMgr->transfer(TMEMBER(_tm.tm_mon)); + persistMgr->transfer(TMEMBER(_tm.tm_mday)); + persistMgr->transfer(TMEMBER(_tm.tm_hour)); + persistMgr->transfer(TMEMBER(_tm.tm_min)); + persistMgr->transfer(TMEMBER(_tm.tm_sec)); + return STATUS_OK; +} + +////////////////////////////////////////////////////////////////////////// +int SXDate::scCompare(BaseScriptable *Value) { + TimeDate time1 = _tm; + TimeDate time2 = ((SXDate *)Value)->_tm; + + if (time1.tm_year < time2.tm_year) { + return -1; + } else if (time1.tm_year == time2.tm_year) { + if (time1.tm_mon < time2.tm_mon) { + return -1; + } else if (time1.tm_mon == time2.tm_mon) { + if (time1.tm_mday < time2.tm_mday) { + return -1; + } else if (time1.tm_mday == time2.tm_mday) { + if (time1.tm_hour < time2.tm_hour) { + return -1; + } else if (time1.tm_hour == time2.tm_hour) { + if (time1.tm_min < time2.tm_min) { + return -1; + } else if (time1.tm_min == time2.tm_min) { + if (time1.tm_sec < time2.tm_sec) { + return -1; + } else if (time1.tm_sec == time2.tm_sec) { + return 0; // Equal + } else { + return 1; // Sec + } + } else { + return 1; // Minute + } + } else { + return 1; // Hour + } + } else { + return 1; // Day + } + } else { + return 1; // Month + } + } else { + return 1; // Year + } +} + +} // end of namespace Wintermute diff --git a/engines/wintermute/base/scriptables/script_ext_date.h b/engines/wintermute/base/scriptables/script_ext_date.h index 7cdf57e689..f6f04dd7e6 100644 --- a/engines/wintermute/base/scriptables/script_ext_date.h +++ b/engines/wintermute/base/scriptables/script_ext_date.h @@ -1,54 +1,54 @@ -/* ScummVM - Graphic Adventure Engine - * - * ScummVM is the legal property of its developers, whose names - * are too numerous to list here. Please refer to the COPYRIGHT - * file distributed with this source distribution. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - */ - -/* - * This file is based on WME Lite. - * http://dead-code.org/redir.php?target=wmelite - * Copyright (c) 2011 Jan Nedoma - */ - -#ifndef WINTERMUTE_SXDATE_H -#define WINTERMUTE_SXDATE_H - -#include "common/system.h" -#include "engines/wintermute/base/base_scriptable.h" - -namespace Wintermute { - -class SXDate : public BaseScriptable { -public: - int scCompare(BaseScriptable *Value); - DECLARE_PERSISTENT(SXDate, BaseScriptable) - SXDate(BaseGame *inGame, ScStack *Stack); - virtual ~SXDate(); - ScValue *scGetProperty(const char *name); - bool scSetProperty(const char *name, ScValue *value); - bool scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack, const char *name); - const char *scToString(); -private: - TimeDate _tm; - Common::String _strRep; -}; - -} // end of namespace Wintermute - -#endif +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +/* + * This file is based on WME Lite. + * http://dead-code.org/redir.php?target=wmelite + * Copyright (c) 2011 Jan Nedoma + */ + +#ifndef WINTERMUTE_SXDATE_H +#define WINTERMUTE_SXDATE_H + +#include "common/system.h" +#include "engines/wintermute/base/base_scriptable.h" + +namespace Wintermute { + +class SXDate : public BaseScriptable { +public: + int scCompare(BaseScriptable *Value); + DECLARE_PERSISTENT(SXDate, BaseScriptable) + SXDate(BaseGame *inGame, ScStack *Stack); + virtual ~SXDate(); + ScValue *scGetProperty(const char *name); + bool scSetProperty(const char *name, ScValue *value); + bool scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack, const char *name); + const char *scToString(); +private: + TimeDate _tm; + Common::String _strRep; +}; + +} // end of namespace Wintermute + +#endif diff --git a/engines/wintermute/base/scriptables/script_ext_file.cpp b/engines/wintermute/base/scriptables/script_ext_file.cpp index 53d551119d..ab574d464b 100644 --- a/engines/wintermute/base/scriptables/script_ext_file.cpp +++ b/engines/wintermute/base/scriptables/script_ext_file.cpp @@ -1,829 +1,829 @@ -/* ScummVM - Graphic Adventure Engine - * - * ScummVM is the legal property of its developers, whose names - * are too numerous to list here. Please refer to the COPYRIGHT - * file distributed with this source distribution. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - */ - -/* - * This file is based on WME Lite. - * http://dead-code.org/redir.php?target=wmelite - * Copyright (c) 2011 Jan Nedoma - */ - -#include "engines/wintermute/system/sys_class_registry.h" -#include "engines/wintermute/system/sys_class.h" -#include "engines/wintermute/base/scriptables/script_stack.h" -#include "engines/wintermute/base/scriptables/script_value.h" -#include "engines/wintermute/base/scriptables/script.h" -#include "engines/wintermute/utils/utils.h" -#include "engines/wintermute/base/base_game.h" -#include "engines/wintermute/base/file/base_file.h" -#include "engines/wintermute/base/base_file_manager.h" -#include "engines/wintermute/platform_osystem.h" -#include "engines/wintermute/base/scriptables/script_ext_file.h" - -// Note: This code is completely untested, as I have yet to find a game that uses SXFile. - -namespace Wintermute { - -IMPLEMENT_PERSISTENT(SXFile, false) - -BaseScriptable *makeSXFile(BaseGame *inGame, ScStack *stack) { - return new SXFile(inGame, stack); -} - -////////////////////////////////////////////////////////////////////////// -SXFile::SXFile(BaseGame *inGame, ScStack *stack) : BaseScriptable(inGame) { - stack->correctParams(1); - ScValue *val = stack->pop(); - - _filename = NULL; - if (!val->isNULL()) { - BaseUtils::setString(&_filename, val->getString()); - } - - _readFile = NULL; - _writeFile = NULL; - - _mode = 0; - _textMode = false; -} - - -////////////////////////////////////////////////////////////////////////// -SXFile::~SXFile() { - cleanup(); -} - -////////////////////////////////////////////////////////////////////////// -void SXFile::cleanup() { - delete[] _filename; - _filename = NULL; - close(); -} - - -////////////////////////////////////////////////////////////////////////// -void SXFile::close() { - if (_readFile) { - BaseFileManager::getEngineInstance()->closeFile(_readFile); - _readFile = NULL; - } - if (_writeFile) { - _writeFile->finalize(); - delete _writeFile; - _writeFile = NULL; - } - _mode = 0; - _textMode = false; -} - -////////////////////////////////////////////////////////////////////////// -const char *SXFile::scToString() { - if (_filename) { - return _filename; - } else { - return "[file object]"; - } -} - -#define FILE_BUFFER_SIZE 32768 -////////////////////////////////////////////////////////////////////////// -bool SXFile::scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack, const char *name) { - ////////////////////////////////////////////////////////////////////////// - // SetFilename - ////////////////////////////////////////////////////////////////////////// - if (strcmp(name, "SetFilename") == 0) { - stack->correctParams(1); - const char *filename = stack->pop()->getString(); - cleanup(); - BaseUtils::setString(&_filename, filename); - stack->pushNULL(); - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // OpenAsText / OpenAsBinary - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "OpenAsText") == 0 || strcmp(name, "OpenAsBinary") == 0) { - stack->correctParams(1); - close(); - _mode = stack->pop()->getInt(1); - if (_mode < 1 || _mode > 3) { - script->runtimeError("File.%s: invalid access mode. Setting read mode.", name); - _mode = 1; - } - if (_mode == 1) { - _readFile = BaseFileManager::getEngineInstance()->openFile(_filename); - if (!_readFile) { - //script->runtimeError("File.%s: Error opening file '%s' for reading.", Name, _filename); - close(); - } else { - _textMode = strcmp(name, "OpenAsText") == 0; - } - } else { - if (strcmp(name, "OpenAsText") == 0) { - if (_mode == 2) { - _writeFile = openForWrite(_filename, false); - } else { - _writeFile = openForAppend(_filename, false); - } - } else { - if (_mode == 2) { - _writeFile = openForWrite(_filename, true); - } else { - _writeFile = openForAppend(_filename, true); - } - } - - if (!_writeFile) { - //script->runtimeError("File.%s: Error opening file '%s' for writing.", Name, _filename); - close(); - } else { - _textMode = strcmp(name, "OpenAsText") == 0; - } - } - - if (_readFile || _writeFile) { - stack->pushBool(true); - } else { - stack->pushBool(false); - } - - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // Close - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "Close") == 0) { - stack->correctParams(0); - close(); - stack->pushNULL(); - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // SetPosition - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "SetPosition") == 0) { - stack->correctParams(1); - if (_mode == 0) { - script->runtimeError("File.%s: File is not open", name); - stack->pushBool(false); - } else { - int pos = stack->pop()->getInt(); - stack->pushBool(setPos(pos)); - } - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // Delete - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "Delete") == 0) { - stack->correctParams(0); - close(); - error("SXFile-Method: \"Delete\" not supported"); - //stack->pushBool(BasePlatform::deleteFile(_filename) != false); - stack->pushBool(false); - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // Copy - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "Copy") == 0) { - stack->correctParams(2); - /* const char *dest = */ stack->pop()->getString(); - /* bool overwrite = */ stack->pop()->getBool(true); - - close(); - error("SXFile-Method: Copy not supported"); - //stack->pushBool(BasePlatform::copyFile(_filename, Dest, !Overwrite) != false); - stack->pushBool(false); - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // ReadLine - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "ReadLine") == 0) { - stack->correctParams(0); - if (!_textMode || !_readFile) { - script->runtimeError("File.%s: File must be open in text mode.", name); - stack->pushNULL(); - return STATUS_OK; - } - uint32 bufSize = FILE_BUFFER_SIZE; - byte *buf = (byte *)malloc(bufSize); - uint32 counter = 0; - byte b; - bool foundNewLine = false; - bool ret = STATUS_FAILED; - do { - ret = _readFile->read(&b, 1); - if (ret != 1) { - break; - } - - if (counter > bufSize) { - buf = (byte *)realloc(buf, bufSize + FILE_BUFFER_SIZE); - bufSize += FILE_BUFFER_SIZE; - } - if (b == '\n') { - buf[counter] = '\0'; - foundNewLine = true; - break; - } else if (b == 0x0D) { - continue; - } else { - buf[counter] = b; - counter++; - } - } while (DID_SUCCEED(ret)); - - if (counter > bufSize) { - buf = (byte *)realloc(buf, bufSize + FILE_BUFFER_SIZE); - bufSize += FILE_BUFFER_SIZE; - } - buf[counter] = '\0'; - - if (!foundNewLine && counter == 0) { - stack->pushNULL(); - } else { - stack->pushString((char *)buf); - } - - free(buf); - - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // ReadText - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "ReadText") == 0) { - stack->correctParams(1); - int textLen = stack->pop()->getInt(); - - if (!_textMode || !_readFile) { - script->runtimeError("File.%s: File must be open in text mode.", name); - stack->pushNULL(); - return STATUS_OK; - } - uint32 bufSize = FILE_BUFFER_SIZE; - byte *buf = (byte *)malloc(bufSize); - uint32 counter = 0; - byte b; - - bool ret = STATUS_FAILED; - while (counter < (uint32)textLen) { - ret = _readFile->read(&b, 1); - if (ret != 1) { - break; - } - - if (counter > bufSize) { - buf = (byte *)realloc(buf, bufSize + FILE_BUFFER_SIZE); - bufSize += FILE_BUFFER_SIZE; - } - if (b == 0x0D) { - continue; - } else { - buf[counter] = b; - counter++; - } - } - - if (counter > bufSize) { - buf = (byte *)realloc(buf, bufSize + FILE_BUFFER_SIZE); - bufSize += FILE_BUFFER_SIZE; - } - buf[counter] = '\0'; - - if (textLen > 0 && counter == 0) { - stack->pushNULL(); - } else { - stack->pushString((char *)buf); - } - - free(buf); - - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // WriteLine / WriteText - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "WriteLine") == 0 || strcmp(name, "WriteText") == 0) { - stack->correctParams(1); - const char *line = stack->pop()->getString(); - if (!_textMode || !_writeFile) { - script->runtimeError("File.%s: File must be open for writing in text mode.", name); - stack->pushBool(false); - return STATUS_OK; - } - Common::String writeLine; - if (strcmp(name, "WriteLine") == 0) { - writeLine = Common::String::format("%s\n", line); - } else { - writeLine = Common::String::format("%s", line); - } - _writeFile->writeString(writeLine); - _writeFile->writeByte(0); - stack->pushBool(true); - - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - ////////////////////////////////////////////////////////////////////////// - // ReadBool - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "ReadBool") == 0) { - stack->correctParams(0); - if (_textMode || !_readFile) { - script->runtimeError("File.%s: File must be open for reading in binary mode.", name); - stack->pushNULL(); - return STATUS_OK; - } - bool val; - if (_readFile->read(&val, sizeof(bool)) == sizeof(bool)) { - stack->pushBool(val); - } else { - stack->pushNULL(); - } - - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // ReadByte - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "ReadByte") == 0) { - stack->correctParams(0); - if (_textMode || !_readFile) { - script->runtimeError("File.%s: File must be open for reading in binary mode.", name); - stack->pushNULL(); - return STATUS_OK; - } - byte val = _readFile->readByte(); - if (!_readFile->err()) { - stack->pushInt(val); - } else { - stack->pushNULL(); - } - - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // ReadShort - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "ReadShort") == 0) { - stack->correctParams(0); - if (_textMode || !_readFile) { - script->runtimeError("File.%s: File must be open for reading in binary mode.", name); - stack->pushNULL(); - return STATUS_OK; - } - int16 val = _readFile->readSint16LE(); - if (!_readFile->err()) { - stack->pushInt(65536 + val); - } else { - stack->pushNULL(); - } - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // ReadInt / ReadLong - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "ReadInt") == 0 || strcmp(name, "ReadLong") == 0) { - stack->correctParams(0); - if (_textMode || !_readFile) { - script->runtimeError("File.%s: File must be open for reading in binary mode.", name); - stack->pushNULL(); - return STATUS_OK; - } - int32 val = _readFile->readSint32LE(); - if (!_readFile->err()) { - stack->pushInt(val); - } else { - stack->pushNULL(); - } - - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // ReadFloat - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "ReadFloat") == 0) { - stack->correctParams(0); - if (_textMode || !_readFile) { - script->runtimeError("File.%s: File must be open for reading in binary mode.", name); - stack->pushNULL(); - return STATUS_OK; - } - float val; - (*(uint32 *)&val) = _readFile->readUint32LE(); - if (!_readFile->err()) { - stack->pushFloat(val); - } else { - stack->pushNULL(); - } - - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // ReadDouble - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "ReadDouble") == 0) { // TODO: Solve reading a 8 byte double. - error("SXFile::ReadDouble - Not endian safe yet"); - stack->correctParams(0); - if (_textMode || !_readFile) { - script->runtimeError("File.%s: File must be open for reading in binary mode.", name); - stack->pushNULL(); - return STATUS_OK; - } - double val; - if (_readFile->read(&val, sizeof(double)) == sizeof(double)) { - stack->pushFloat(val); - } else { - stack->pushNULL(); - } - - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // ReadString - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "ReadString") == 0) { - stack->correctParams(0); - if (_textMode || !_readFile) { - script->runtimeError("File.%s: File must be open for reading in binary mode.", name); - stack->pushNULL(); - return STATUS_OK; - } - uint32 size = _readFile->readUint32LE(); - if (!_readFile->err()) { - byte *str = new byte[size + 1]; - if (str) { - if (_readFile->read(str, size) == size) { - str[size] = '\0'; - stack->pushString((char *)str); - } - delete[] str; - } else { - stack->pushNULL(); - } - } else { - stack->pushNULL(); - } - - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // WriteBool - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "WriteBool") == 0) { - stack->correctParams(1); - bool val = stack->pop()->getBool(); - - if (_textMode || !_writeFile) { - script->runtimeError("File.%s: File must be open for writing in binary mode.", name); - stack->pushBool(false); - return STATUS_OK; - } - _writeFile->writeByte(val); - stack->pushBool(true); - - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // WriteByte - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "WriteByte") == 0) { - stack->correctParams(1); - byte val = stack->pop()->getInt(); - - if (_textMode || !_writeFile) { - script->runtimeError("File.%s: File must be open for writing in binary mode.", name); - stack->pushBool(false); - return STATUS_OK; - } - _writeFile->writeByte(val); - stack->pushBool(true); - - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // WriteShort - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "WriteShort") == 0) { - stack->correctParams(1); - int16 val = stack->pop()->getInt(); - - if (_textMode || !_writeFile) { - script->runtimeError("File.%s: File must be open for writing in binary mode.", name); - stack->pushBool(false); - return STATUS_OK; - } - _writeFile->writeSint16LE(val); - stack->pushBool(true); - - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // WriteInt / WriteLong - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "WriteInt") == 0 || strcmp(name, "WriteLong") == 0) { - stack->correctParams(1); - int32 val = stack->pop()->getInt(); - - if (_textMode || !_writeFile) { - script->runtimeError("File.%s: File must be open for writing in binary mode.", name); - stack->pushBool(false); - return STATUS_OK; - } - _writeFile->writeSint32LE(val); - stack->pushBool(true); - - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // WriteFloat - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "WriteFloat") == 0) { - stack->correctParams(1); - float val = stack->pop()->getFloat(); - - if (_textMode || !_writeFile) { - script->runtimeError("File.%s: File must be open for writing in binary mode.", name); - stack->pushBool(false); - return STATUS_OK; - } - uint32 *ptr = (uint32 *)&val; - _writeFile->writeUint32LE(*ptr); - stack->pushBool(true); - - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // WriteDouble - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "WriteDouble") == 0) { - error("SXFile::WriteDouble - Not endian safe yet"); - stack->correctParams(1); - /* double val = */ stack->pop()->getFloat(); - - if (_textMode || !_writeFile) { - script->runtimeError("File.%s: File must be open for writing in binary mode.", name); - stack->pushBool(false); - return STATUS_OK; - } - //fwrite(&val, sizeof(val), 1, (FILE *)_writeFile); - stack->pushBool(true); - - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // WriteString - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "WriteString") == 0) { - stack->correctParams(1); - const char *val = stack->pop()->getString(); - - if (_textMode || !_writeFile) { - script->runtimeError("File.%s: File must be open for writing in binary mode.", name); - stack->pushBool(false); - return STATUS_OK; - } - - uint32 size = strlen(val); - _writeFile->writeUint32LE(size); - _writeFile->writeString(val); - - stack->pushBool(true); - - return STATUS_OK; - } else { - return BaseScriptable::scCallMethod(script, stack, thisStack, name); - } -} - - -////////////////////////////////////////////////////////////////////////// -ScValue *SXFile::scGetProperty(const char *name) { - _scValue->setNULL(); - - ////////////////////////////////////////////////////////////////////////// - // Type (RO) - ////////////////////////////////////////////////////////////////////////// - if (strcmp(name, "Type") == 0) { - _scValue->setString("file"); - return _scValue; - } - - ////////////////////////////////////////////////////////////////////////// - // Filename (RO) - ////////////////////////////////////////////////////////////////////////// - if (strcmp(name, "Filename") == 0) { - _scValue->setString(_filename); - return _scValue; - } - - ////////////////////////////////////////////////////////////////////////// - // Position (RO) - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "Position") == 0) { - _scValue->setInt(getPos()); - return _scValue; - } - - ////////////////////////////////////////////////////////////////////////// - // Length (RO) - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "Length") == 0) { - _scValue->setInt(getLength()); - return _scValue; - } - - ////////////////////////////////////////////////////////////////////////// - // TextMode (RO) - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "TextMode") == 0) { - _scValue->setBool(_textMode); - return _scValue; - } - - ////////////////////////////////////////////////////////////////////////// - // AccessMode (RO) - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "AccessMode") == 0) { - _scValue->setInt(_mode); - return _scValue; - } else { - return BaseScriptable::scGetProperty(name); - } -} - - -////////////////////////////////////////////////////////////////////////// -bool SXFile::scSetProperty(const char *name, ScValue *value) { - /* - ////////////////////////////////////////////////////////////////////////// - // Length - ////////////////////////////////////////////////////////////////////////// - if (strcmp(name, "Length")==0){ - int origLength = _length; - _length = max(value->getInt(0), 0); - - char propName[20]; - if (_length < OrigLength){ - for(int i=_length; iDeleteProp(PropName); - } - } - return STATUS_OK; - } - else*/ return BaseScriptable::scSetProperty(name, value); -} - -////////////////////////////////////////////////////////////////////////// -uint32 SXFile::getPos() { - if (_mode == 1 && _readFile) { - return _readFile->pos(); - } else if ((_mode == 2 || _mode == 3) && _writeFile) { - error("SXFile - getPos for WriteFile not supported"); - return 0; -// return ftell((FILE *)_writeFile); - } else { - return 0; - } -} - -////////////////////////////////////////////////////////////////////////// -bool SXFile::setPos(uint32 pos, int whence) { - if (_mode == 1 && _readFile) { - return _readFile->seek(pos, whence); - } else if ((_mode == 2 || _mode == 3) && _writeFile) { - error("SXFile - seeking in WriteFile not supported"); - return false; -// return fseek((FILE *)_writeFile, pos, (int)origin) == 0; - } else { - return false; - } -} - -////////////////////////////////////////////////////////////////////////// -uint32 SXFile::getLength() { - if (_mode == 1 && _readFile) { - return _readFile->size(); - } else if ((_mode == 2 || _mode == 3) && _writeFile) { - error("SXFile - reading length for WriteFile not supported"); - return 0; - /* - uint32 currentPos = ftell((FILE *)_writeFile); - fseek((FILE *)_writeFile, 0, SEEK_END); - int ret = ftell((FILE *)_writeFile); - fseek((FILE *)_writeFile, CurrentPos, SEEK_SET); - return Ret;*/ - } else { - return 0; - } -} - -////////////////////////////////////////////////////////////////////////// -bool SXFile::persist(BasePersistenceManager *persistMgr) { - - BaseScriptable::persist(persistMgr); - - persistMgr->transfer(TMEMBER(_filename)); - persistMgr->transfer(TMEMBER(_mode)); - persistMgr->transfer(TMEMBER(_textMode)); - - uint32 pos = 0; - if (persistMgr->getIsSaving()) { - pos = getPos(); - persistMgr->transfer(TMEMBER(pos)); - } else { - persistMgr->transfer(TMEMBER(pos)); - - // try to re-open file if needed - _writeFile = NULL; - _readFile = NULL; - - if (_mode != 0) { - // open for reading - if (_mode == 1) { - _readFile = BaseFileManager::getEngineInstance()->openFile(_filename); - if (!_readFile) { - close(); - } - } - // open for writing / appending - else { - if (_textMode) { - if (_mode == 2) { - _writeFile = openForWrite(_filename, false); - } else { - _writeFile = openForAppend(_filename, false); - } - } else { - if (_mode == 2) { - _writeFile = openForWrite(_filename, true); - } else { - _writeFile = openForAppend(_filename, true); - } - } - if (_writeFile) { - close(); - } - } - setPos(pos); - } - } - - return STATUS_OK; -} - -// Should replace fopen(..., "wb+") and fopen(..., "w+") -Common::WriteStream *SXFile::openForWrite(const Common::String &filename, bool binary) { - error("SXFile::openForWrite - WriteFiles not supported"); -} - -// Should replace fopen(..., "ab+") and fopen(..., "a+") -Common::WriteStream *SXFile::openForAppend(const Common::String &filename, bool binary) { - error("SXFile::openForAppend - WriteFiles not supported"); -} - -} // end of namespace Wintermute +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +/* + * This file is based on WME Lite. + * http://dead-code.org/redir.php?target=wmelite + * Copyright (c) 2011 Jan Nedoma + */ + +#include "engines/wintermute/system/sys_class_registry.h" +#include "engines/wintermute/system/sys_class.h" +#include "engines/wintermute/base/scriptables/script_stack.h" +#include "engines/wintermute/base/scriptables/script_value.h" +#include "engines/wintermute/base/scriptables/script.h" +#include "engines/wintermute/utils/utils.h" +#include "engines/wintermute/base/base_game.h" +#include "engines/wintermute/base/file/base_file.h" +#include "engines/wintermute/base/base_file_manager.h" +#include "engines/wintermute/platform_osystem.h" +#include "engines/wintermute/base/scriptables/script_ext_file.h" + +// Note: This code is completely untested, as I have yet to find a game that uses SXFile. + +namespace Wintermute { + +IMPLEMENT_PERSISTENT(SXFile, false) + +BaseScriptable *makeSXFile(BaseGame *inGame, ScStack *stack) { + return new SXFile(inGame, stack); +} + +////////////////////////////////////////////////////////////////////////// +SXFile::SXFile(BaseGame *inGame, ScStack *stack) : BaseScriptable(inGame) { + stack->correctParams(1); + ScValue *val = stack->pop(); + + _filename = NULL; + if (!val->isNULL()) { + BaseUtils::setString(&_filename, val->getString()); + } + + _readFile = NULL; + _writeFile = NULL; + + _mode = 0; + _textMode = false; +} + + +////////////////////////////////////////////////////////////////////////// +SXFile::~SXFile() { + cleanup(); +} + +////////////////////////////////////////////////////////////////////////// +void SXFile::cleanup() { + delete[] _filename; + _filename = NULL; + close(); +} + + +////////////////////////////////////////////////////////////////////////// +void SXFile::close() { + if (_readFile) { + BaseFileManager::getEngineInstance()->closeFile(_readFile); + _readFile = NULL; + } + if (_writeFile) { + _writeFile->finalize(); + delete _writeFile; + _writeFile = NULL; + } + _mode = 0; + _textMode = false; +} + +////////////////////////////////////////////////////////////////////////// +const char *SXFile::scToString() { + if (_filename) { + return _filename; + } else { + return "[file object]"; + } +} + +#define FILE_BUFFER_SIZE 32768 +////////////////////////////////////////////////////////////////////////// +bool SXFile::scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack, const char *name) { + ////////////////////////////////////////////////////////////////////////// + // SetFilename + ////////////////////////////////////////////////////////////////////////// + if (strcmp(name, "SetFilename") == 0) { + stack->correctParams(1); + const char *filename = stack->pop()->getString(); + cleanup(); + BaseUtils::setString(&_filename, filename); + stack->pushNULL(); + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // OpenAsText / OpenAsBinary + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "OpenAsText") == 0 || strcmp(name, "OpenAsBinary") == 0) { + stack->correctParams(1); + close(); + _mode = stack->pop()->getInt(1); + if (_mode < 1 || _mode > 3) { + script->runtimeError("File.%s: invalid access mode. Setting read mode.", name); + _mode = 1; + } + if (_mode == 1) { + _readFile = BaseFileManager::getEngineInstance()->openFile(_filename); + if (!_readFile) { + //script->runtimeError("File.%s: Error opening file '%s' for reading.", Name, _filename); + close(); + } else { + _textMode = strcmp(name, "OpenAsText") == 0; + } + } else { + if (strcmp(name, "OpenAsText") == 0) { + if (_mode == 2) { + _writeFile = openForWrite(_filename, false); + } else { + _writeFile = openForAppend(_filename, false); + } + } else { + if (_mode == 2) { + _writeFile = openForWrite(_filename, true); + } else { + _writeFile = openForAppend(_filename, true); + } + } + + if (!_writeFile) { + //script->runtimeError("File.%s: Error opening file '%s' for writing.", Name, _filename); + close(); + } else { + _textMode = strcmp(name, "OpenAsText") == 0; + } + } + + if (_readFile || _writeFile) { + stack->pushBool(true); + } else { + stack->pushBool(false); + } + + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // Close + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "Close") == 0) { + stack->correctParams(0); + close(); + stack->pushNULL(); + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // SetPosition + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "SetPosition") == 0) { + stack->correctParams(1); + if (_mode == 0) { + script->runtimeError("File.%s: File is not open", name); + stack->pushBool(false); + } else { + int pos = stack->pop()->getInt(); + stack->pushBool(setPos(pos)); + } + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // Delete + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "Delete") == 0) { + stack->correctParams(0); + close(); + error("SXFile-Method: \"Delete\" not supported"); + //stack->pushBool(BasePlatform::deleteFile(_filename) != false); + stack->pushBool(false); + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // Copy + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "Copy") == 0) { + stack->correctParams(2); + /* const char *dest = */ stack->pop()->getString(); + /* bool overwrite = */ stack->pop()->getBool(true); + + close(); + error("SXFile-Method: Copy not supported"); + //stack->pushBool(BasePlatform::copyFile(_filename, Dest, !Overwrite) != false); + stack->pushBool(false); + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // ReadLine + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "ReadLine") == 0) { + stack->correctParams(0); + if (!_textMode || !_readFile) { + script->runtimeError("File.%s: File must be open in text mode.", name); + stack->pushNULL(); + return STATUS_OK; + } + uint32 bufSize = FILE_BUFFER_SIZE; + byte *buf = (byte *)malloc(bufSize); + uint32 counter = 0; + byte b; + bool foundNewLine = false; + bool ret = STATUS_FAILED; + do { + ret = _readFile->read(&b, 1); + if (ret != 1) { + break; + } + + if (counter > bufSize) { + buf = (byte *)realloc(buf, bufSize + FILE_BUFFER_SIZE); + bufSize += FILE_BUFFER_SIZE; + } + if (b == '\n') { + buf[counter] = '\0'; + foundNewLine = true; + break; + } else if (b == 0x0D) { + continue; + } else { + buf[counter] = b; + counter++; + } + } while (DID_SUCCEED(ret)); + + if (counter > bufSize) { + buf = (byte *)realloc(buf, bufSize + FILE_BUFFER_SIZE); + bufSize += FILE_BUFFER_SIZE; + } + buf[counter] = '\0'; + + if (!foundNewLine && counter == 0) { + stack->pushNULL(); + } else { + stack->pushString((char *)buf); + } + + free(buf); + + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // ReadText + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "ReadText") == 0) { + stack->correctParams(1); + int textLen = stack->pop()->getInt(); + + if (!_textMode || !_readFile) { + script->runtimeError("File.%s: File must be open in text mode.", name); + stack->pushNULL(); + return STATUS_OK; + } + uint32 bufSize = FILE_BUFFER_SIZE; + byte *buf = (byte *)malloc(bufSize); + uint32 counter = 0; + byte b; + + bool ret = STATUS_FAILED; + while (counter < (uint32)textLen) { + ret = _readFile->read(&b, 1); + if (ret != 1) { + break; + } + + if (counter > bufSize) { + buf = (byte *)realloc(buf, bufSize + FILE_BUFFER_SIZE); + bufSize += FILE_BUFFER_SIZE; + } + if (b == 0x0D) { + continue; + } else { + buf[counter] = b; + counter++; + } + } + + if (counter > bufSize) { + buf = (byte *)realloc(buf, bufSize + FILE_BUFFER_SIZE); + bufSize += FILE_BUFFER_SIZE; + } + buf[counter] = '\0'; + + if (textLen > 0 && counter == 0) { + stack->pushNULL(); + } else { + stack->pushString((char *)buf); + } + + free(buf); + + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // WriteLine / WriteText + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "WriteLine") == 0 || strcmp(name, "WriteText") == 0) { + stack->correctParams(1); + const char *line = stack->pop()->getString(); + if (!_textMode || !_writeFile) { + script->runtimeError("File.%s: File must be open for writing in text mode.", name); + stack->pushBool(false); + return STATUS_OK; + } + Common::String writeLine; + if (strcmp(name, "WriteLine") == 0) { + writeLine = Common::String::format("%s\n", line); + } else { + writeLine = Common::String::format("%s", line); + } + _writeFile->writeString(writeLine); + _writeFile->writeByte(0); + stack->pushBool(true); + + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + ////////////////////////////////////////////////////////////////////////// + // ReadBool + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "ReadBool") == 0) { + stack->correctParams(0); + if (_textMode || !_readFile) { + script->runtimeError("File.%s: File must be open for reading in binary mode.", name); + stack->pushNULL(); + return STATUS_OK; + } + bool val; + if (_readFile->read(&val, sizeof(bool)) == sizeof(bool)) { + stack->pushBool(val); + } else { + stack->pushNULL(); + } + + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // ReadByte + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "ReadByte") == 0) { + stack->correctParams(0); + if (_textMode || !_readFile) { + script->runtimeError("File.%s: File must be open for reading in binary mode.", name); + stack->pushNULL(); + return STATUS_OK; + } + byte val = _readFile->readByte(); + if (!_readFile->err()) { + stack->pushInt(val); + } else { + stack->pushNULL(); + } + + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // ReadShort + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "ReadShort") == 0) { + stack->correctParams(0); + if (_textMode || !_readFile) { + script->runtimeError("File.%s: File must be open for reading in binary mode.", name); + stack->pushNULL(); + return STATUS_OK; + } + int16 val = _readFile->readSint16LE(); + if (!_readFile->err()) { + stack->pushInt(65536 + val); + } else { + stack->pushNULL(); + } + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // ReadInt / ReadLong + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "ReadInt") == 0 || strcmp(name, "ReadLong") == 0) { + stack->correctParams(0); + if (_textMode || !_readFile) { + script->runtimeError("File.%s: File must be open for reading in binary mode.", name); + stack->pushNULL(); + return STATUS_OK; + } + int32 val = _readFile->readSint32LE(); + if (!_readFile->err()) { + stack->pushInt(val); + } else { + stack->pushNULL(); + } + + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // ReadFloat + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "ReadFloat") == 0) { + stack->correctParams(0); + if (_textMode || !_readFile) { + script->runtimeError("File.%s: File must be open for reading in binary mode.", name); + stack->pushNULL(); + return STATUS_OK; + } + float val; + (*(uint32 *)&val) = _readFile->readUint32LE(); + if (!_readFile->err()) { + stack->pushFloat(val); + } else { + stack->pushNULL(); + } + + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // ReadDouble + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "ReadDouble") == 0) { // TODO: Solve reading a 8 byte double. + error("SXFile::ReadDouble - Not endian safe yet"); + stack->correctParams(0); + if (_textMode || !_readFile) { + script->runtimeError("File.%s: File must be open for reading in binary mode.", name); + stack->pushNULL(); + return STATUS_OK; + } + double val; + if (_readFile->read(&val, sizeof(double)) == sizeof(double)) { + stack->pushFloat(val); + } else { + stack->pushNULL(); + } + + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // ReadString + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "ReadString") == 0) { + stack->correctParams(0); + if (_textMode || !_readFile) { + script->runtimeError("File.%s: File must be open for reading in binary mode.", name); + stack->pushNULL(); + return STATUS_OK; + } + uint32 size = _readFile->readUint32LE(); + if (!_readFile->err()) { + byte *str = new byte[size + 1]; + if (str) { + if (_readFile->read(str, size) == size) { + str[size] = '\0'; + stack->pushString((char *)str); + } + delete[] str; + } else { + stack->pushNULL(); + } + } else { + stack->pushNULL(); + } + + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // WriteBool + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "WriteBool") == 0) { + stack->correctParams(1); + bool val = stack->pop()->getBool(); + + if (_textMode || !_writeFile) { + script->runtimeError("File.%s: File must be open for writing in binary mode.", name); + stack->pushBool(false); + return STATUS_OK; + } + _writeFile->writeByte(val); + stack->pushBool(true); + + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // WriteByte + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "WriteByte") == 0) { + stack->correctParams(1); + byte val = stack->pop()->getInt(); + + if (_textMode || !_writeFile) { + script->runtimeError("File.%s: File must be open for writing in binary mode.", name); + stack->pushBool(false); + return STATUS_OK; + } + _writeFile->writeByte(val); + stack->pushBool(true); + + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // WriteShort + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "WriteShort") == 0) { + stack->correctParams(1); + int16 val = stack->pop()->getInt(); + + if (_textMode || !_writeFile) { + script->runtimeError("File.%s: File must be open for writing in binary mode.", name); + stack->pushBool(false); + return STATUS_OK; + } + _writeFile->writeSint16LE(val); + stack->pushBool(true); + + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // WriteInt / WriteLong + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "WriteInt") == 0 || strcmp(name, "WriteLong") == 0) { + stack->correctParams(1); + int32 val = stack->pop()->getInt(); + + if (_textMode || !_writeFile) { + script->runtimeError("File.%s: File must be open for writing in binary mode.", name); + stack->pushBool(false); + return STATUS_OK; + } + _writeFile->writeSint32LE(val); + stack->pushBool(true); + + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // WriteFloat + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "WriteFloat") == 0) { + stack->correctParams(1); + float val = stack->pop()->getFloat(); + + if (_textMode || !_writeFile) { + script->runtimeError("File.%s: File must be open for writing in binary mode.", name); + stack->pushBool(false); + return STATUS_OK; + } + uint32 *ptr = (uint32 *)&val; + _writeFile->writeUint32LE(*ptr); + stack->pushBool(true); + + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // WriteDouble + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "WriteDouble") == 0) { + error("SXFile::WriteDouble - Not endian safe yet"); + stack->correctParams(1); + /* double val = */ stack->pop()->getFloat(); + + if (_textMode || !_writeFile) { + script->runtimeError("File.%s: File must be open for writing in binary mode.", name); + stack->pushBool(false); + return STATUS_OK; + } + //fwrite(&val, sizeof(val), 1, (FILE *)_writeFile); + stack->pushBool(true); + + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // WriteString + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "WriteString") == 0) { + stack->correctParams(1); + const char *val = stack->pop()->getString(); + + if (_textMode || !_writeFile) { + script->runtimeError("File.%s: File must be open for writing in binary mode.", name); + stack->pushBool(false); + return STATUS_OK; + } + + uint32 size = strlen(val); + _writeFile->writeUint32LE(size); + _writeFile->writeString(val); + + stack->pushBool(true); + + return STATUS_OK; + } else { + return BaseScriptable::scCallMethod(script, stack, thisStack, name); + } +} + + +////////////////////////////////////////////////////////////////////////// +ScValue *SXFile::scGetProperty(const char *name) { + _scValue->setNULL(); + + ////////////////////////////////////////////////////////////////////////// + // Type (RO) + ////////////////////////////////////////////////////////////////////////// + if (strcmp(name, "Type") == 0) { + _scValue->setString("file"); + return _scValue; + } + + ////////////////////////////////////////////////////////////////////////// + // Filename (RO) + ////////////////////////////////////////////////////////////////////////// + if (strcmp(name, "Filename") == 0) { + _scValue->setString(_filename); + return _scValue; + } + + ////////////////////////////////////////////////////////////////////////// + // Position (RO) + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "Position") == 0) { + _scValue->setInt(getPos()); + return _scValue; + } + + ////////////////////////////////////////////////////////////////////////// + // Length (RO) + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "Length") == 0) { + _scValue->setInt(getLength()); + return _scValue; + } + + ////////////////////////////////////////////////////////////////////////// + // TextMode (RO) + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "TextMode") == 0) { + _scValue->setBool(_textMode); + return _scValue; + } + + ////////////////////////////////////////////////////////////////////////// + // AccessMode (RO) + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "AccessMode") == 0) { + _scValue->setInt(_mode); + return _scValue; + } else { + return BaseScriptable::scGetProperty(name); + } +} + + +////////////////////////////////////////////////////////////////////////// +bool SXFile::scSetProperty(const char *name, ScValue *value) { + /* + ////////////////////////////////////////////////////////////////////////// + // Length + ////////////////////////////////////////////////////////////////////////// + if (strcmp(name, "Length")==0){ + int origLength = _length; + _length = max(value->getInt(0), 0); + + char propName[20]; + if (_length < OrigLength){ + for(int i=_length; iDeleteProp(PropName); + } + } + return STATUS_OK; + } + else*/ return BaseScriptable::scSetProperty(name, value); +} + +////////////////////////////////////////////////////////////////////////// +uint32 SXFile::getPos() { + if (_mode == 1 && _readFile) { + return _readFile->pos(); + } else if ((_mode == 2 || _mode == 3) && _writeFile) { + error("SXFile - getPos for WriteFile not supported"); + return 0; +// return ftell((FILE *)_writeFile); + } else { + return 0; + } +} + +////////////////////////////////////////////////////////////////////////// +bool SXFile::setPos(uint32 pos, int whence) { + if (_mode == 1 && _readFile) { + return _readFile->seek(pos, whence); + } else if ((_mode == 2 || _mode == 3) && _writeFile) { + error("SXFile - seeking in WriteFile not supported"); + return false; +// return fseek((FILE *)_writeFile, pos, (int)origin) == 0; + } else { + return false; + } +} + +////////////////////////////////////////////////////////////////////////// +uint32 SXFile::getLength() { + if (_mode == 1 && _readFile) { + return _readFile->size(); + } else if ((_mode == 2 || _mode == 3) && _writeFile) { + error("SXFile - reading length for WriteFile not supported"); + return 0; + /* + uint32 currentPos = ftell((FILE *)_writeFile); + fseek((FILE *)_writeFile, 0, SEEK_END); + int ret = ftell((FILE *)_writeFile); + fseek((FILE *)_writeFile, CurrentPos, SEEK_SET); + return Ret;*/ + } else { + return 0; + } +} + +////////////////////////////////////////////////////////////////////////// +bool SXFile::persist(BasePersistenceManager *persistMgr) { + + BaseScriptable::persist(persistMgr); + + persistMgr->transfer(TMEMBER(_filename)); + persistMgr->transfer(TMEMBER(_mode)); + persistMgr->transfer(TMEMBER(_textMode)); + + uint32 pos = 0; + if (persistMgr->getIsSaving()) { + pos = getPos(); + persistMgr->transfer(TMEMBER(pos)); + } else { + persistMgr->transfer(TMEMBER(pos)); + + // try to re-open file if needed + _writeFile = NULL; + _readFile = NULL; + + if (_mode != 0) { + // open for reading + if (_mode == 1) { + _readFile = BaseFileManager::getEngineInstance()->openFile(_filename); + if (!_readFile) { + close(); + } + } + // open for writing / appending + else { + if (_textMode) { + if (_mode == 2) { + _writeFile = openForWrite(_filename, false); + } else { + _writeFile = openForAppend(_filename, false); + } + } else { + if (_mode == 2) { + _writeFile = openForWrite(_filename, true); + } else { + _writeFile = openForAppend(_filename, true); + } + } + if (_writeFile) { + close(); + } + } + setPos(pos); + } + } + + return STATUS_OK; +} + +// Should replace fopen(..., "wb+") and fopen(..., "w+") +Common::WriteStream *SXFile::openForWrite(const Common::String &filename, bool binary) { + error("SXFile::openForWrite - WriteFiles not supported"); +} + +// Should replace fopen(..., "ab+") and fopen(..., "a+") +Common::WriteStream *SXFile::openForAppend(const Common::String &filename, bool binary) { + error("SXFile::openForAppend - WriteFiles not supported"); +} + +} // end of namespace Wintermute diff --git a/engines/wintermute/base/scriptables/script_ext_file.h b/engines/wintermute/base/scriptables/script_ext_file.h index 389974a48e..b91a53e695 100644 --- a/engines/wintermute/base/scriptables/script_ext_file.h +++ b/engines/wintermute/base/scriptables/script_ext_file.h @@ -1,66 +1,66 @@ -/* ScummVM - Graphic Adventure Engine - * - * ScummVM is the legal property of its developers, whose names - * are too numerous to list here. Please refer to the COPYRIGHT - * file distributed with this source distribution. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - */ - -/* - * This file is based on WME Lite. - * http://dead-code.org/redir.php?target=wmelite - * Copyright (c) 2011 Jan Nedoma - */ - -#ifndef WINTERMUTES_SXFILE_H -#define WINTERMUTES_SXFILE_H - - -#include "engines/wintermute/base/base_scriptable.h" -#include "common/stream.h" - -namespace Wintermute { - -class BaseFile; - -class SXFile : public BaseScriptable { -public: - DECLARE_PERSISTENT(SXFile, BaseScriptable) - ScValue *scGetProperty(const char *name); - bool scSetProperty(const char *name, ScValue *value); - bool scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack, const char *name); - const char *scToString(); - SXFile(BaseGame *inGame, ScStack *Stack); - virtual ~SXFile(); -private: - Common::SeekableReadStream *_readFile; - Common::WriteStream *_writeFile; - int _mode; // 0..none, 1..read, 2..write, 3..append - bool _textMode; - void close(); - void cleanup(); - uint32 getPos(); - uint32 getLength(); - bool setPos(uint32 pos, int whence = SEEK_SET); - char *_filename; - Common::WriteStream *openForWrite(const Common::String &filename, bool binary); - Common::WriteStream *openForAppend(const Common::String &filename, bool binary); -}; - -} // end of namespace Wintermute - -#endif +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +/* + * This file is based on WME Lite. + * http://dead-code.org/redir.php?target=wmelite + * Copyright (c) 2011 Jan Nedoma + */ + +#ifndef WINTERMUTES_SXFILE_H +#define WINTERMUTES_SXFILE_H + + +#include "engines/wintermute/base/base_scriptable.h" +#include "common/stream.h" + +namespace Wintermute { + +class BaseFile; + +class SXFile : public BaseScriptable { +public: + DECLARE_PERSISTENT(SXFile, BaseScriptable) + ScValue *scGetProperty(const char *name); + bool scSetProperty(const char *name, ScValue *value); + bool scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack, const char *name); + const char *scToString(); + SXFile(BaseGame *inGame, ScStack *Stack); + virtual ~SXFile(); +private: + Common::SeekableReadStream *_readFile; + Common::WriteStream *_writeFile; + int _mode; // 0..none, 1..read, 2..write, 3..append + bool _textMode; + void close(); + void cleanup(); + uint32 getPos(); + uint32 getLength(); + bool setPos(uint32 pos, int whence = SEEK_SET); + char *_filename; + Common::WriteStream *openForWrite(const Common::String &filename, bool binary); + Common::WriteStream *openForAppend(const Common::String &filename, bool binary); +}; + +} // end of namespace Wintermute + +#endif diff --git a/engines/wintermute/base/scriptables/script_ext_math.cpp b/engines/wintermute/base/scriptables/script_ext_math.cpp index f7d0ba20b9..598b80cff3 100644 --- a/engines/wintermute/base/scriptables/script_ext_math.cpp +++ b/engines/wintermute/base/scriptables/script_ext_math.cpp @@ -1,295 +1,295 @@ -/* ScummVM - Graphic Adventure Engine - * - * ScummVM is the legal property of its developers, whose names - * are too numerous to list here. Please refer to the COPYRIGHT - * file distributed with this source distribution. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - */ - -/* - * This file is based on WME Lite. - * http://dead-code.org/redir.php?target=wmelite - * Copyright (c) 2011 Jan Nedoma - */ - -#include "engines/wintermute/base/scriptables/script_ext_math.h" -#include "engines/wintermute/base/scriptables/script_stack.h" -#include "engines/wintermute/base/scriptables/script_value.h" -#include "engines/wintermute/persistent.h" -#include "common/math.h" -#include - -namespace Wintermute { - -////////////////////////////////////////////////////////////////////// -// Construction/Destruction -////////////////////////////////////////////////////////////////////// - - -IMPLEMENT_PERSISTENT(SXMath, true) - -BaseScriptable *makeSXMath(BaseGame *inGame) { - return new SXMath(inGame); -} - -////////////////////////////////////////////////////////////////////////// -SXMath::SXMath(BaseGame *inGame) : BaseScriptable(inGame) { - -} - - -////////////////////////////////////////////////////////////////////////// -SXMath::~SXMath() { - -} - - -////////////////////////////////////////////////////////////////////////// -bool SXMath::scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack, const char *name) { - ////////////////////////////////////////////////////////////////////////// - // Abs - ////////////////////////////////////////////////////////////////////////// - if (strcmp(name, "Abs") == 0) { - stack->correctParams(1); - stack->pushFloat(fabs(stack->pop()->getFloat())); - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // Acos - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "Acos") == 0) { - stack->correctParams(1); - stack->pushFloat(acos(stack->pop()->getFloat())); - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // Asin - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "Asin") == 0) { - stack->correctParams(1); - stack->pushFloat(asin(stack->pop()->getFloat())); - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // Atan - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "Atan") == 0) { - stack->correctParams(1); - stack->pushFloat(atan(stack->pop()->getFloat())); - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // Atan2 - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "Atan2") == 0) { - stack->correctParams(2); - double y = stack->pop()->getFloat(); - double x = stack->pop()->getFloat(); - stack->pushFloat(atan2(y, x)); - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // Ceil - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "Ceil") == 0) { - stack->correctParams(1); - stack->pushFloat(ceil(stack->pop()->getFloat())); - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // Cos - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "Cos") == 0) { - stack->correctParams(1); - stack->pushFloat(cos(degreeToRadian(stack->pop()->getFloat()))); - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // Cosh - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "Cosh") == 0) { - stack->correctParams(1); - stack->pushFloat(cosh(degreeToRadian(stack->pop()->getFloat()))); - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // Exp - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "Exp") == 0) { - stack->correctParams(1); - stack->pushFloat(exp(stack->pop()->getFloat())); - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // Floor - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "Floor") == 0) { - stack->correctParams(1); - stack->pushFloat(floor(stack->pop()->getFloat())); - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // Log - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "Log") == 0) { - stack->correctParams(1); - stack->pushFloat(log(stack->pop()->getFloat())); - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // Log10 - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "Log10") == 0) { - stack->correctParams(1); - stack->pushFloat(log10(stack->pop()->getFloat())); - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // Pow - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "Pow") == 0) { - stack->correctParams(2); - double x = stack->pop()->getFloat(); - double y = stack->pop()->getFloat(); - - stack->pushFloat(pow(x, y)); - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // Sin - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "Sin") == 0) { - stack->correctParams(1); - stack->pushFloat(sin(degreeToRadian(stack->pop()->getFloat()))); - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // Sinh - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "Sinh") == 0) { - stack->correctParams(1); - stack->pushFloat(sinh(degreeToRadian(stack->pop()->getFloat()))); - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // Tan - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "Tan") == 0) { - stack->correctParams(1); - stack->pushFloat(tan(degreeToRadian(stack->pop()->getFloat()))); - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // Tanh - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "Tanh") == 0) { - stack->correctParams(1); - stack->pushFloat(tanh(degreeToRadian(stack->pop()->getFloat()))); - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // Sqrt - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "Sqrt") == 0) { - stack->correctParams(1); - stack->pushFloat(sqrt(stack->pop()->getFloat())); - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // DegToRad - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "DegToRad") == 0) { - stack->correctParams(1); - stack->pushFloat(degreeToRadian(stack->pop()->getFloat())); - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // RadToDeg - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "RadToDeg") == 0) { - stack->correctParams(1); - stack->pushFloat(radianToDegree(stack->pop()->getFloat())); - return STATUS_OK; - } else { - return STATUS_FAILED; - } -} - - -////////////////////////////////////////////////////////////////////////// -ScValue *SXMath::scGetProperty(const char *name) { - _scValue->setNULL(); - - ////////////////////////////////////////////////////////////////////////// - // Type - ////////////////////////////////////////////////////////////////////////// - if (strcmp(name, "Type") == 0) { - _scValue->setString("math"); - return _scValue; - } - - ////////////////////////////////////////////////////////////////////////// - // PI - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "PI") == 0) { - _scValue->setFloat(M_PI); - return _scValue; - } else { - return _scValue; - } -} - - -////////////////////////////////////////////////////////////////////////// -double SXMath::degreeToRadian(double value) { - return value * (M_PI / 180.0f); -} - - -////////////////////////////////////////////////////////////////////////// -double SXMath::radianToDegree(double value) { - return value * (180.0f / M_PI); -} - - -////////////////////////////////////////////////////////////////////////// -bool SXMath::persist(BasePersistenceManager *persistMgr) { - - BaseScriptable::persist(persistMgr); - return STATUS_OK; -} - -} // end of namespace Wintermute +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +/* + * This file is based on WME Lite. + * http://dead-code.org/redir.php?target=wmelite + * Copyright (c) 2011 Jan Nedoma + */ + +#include "engines/wintermute/base/scriptables/script_ext_math.h" +#include "engines/wintermute/base/scriptables/script_stack.h" +#include "engines/wintermute/base/scriptables/script_value.h" +#include "engines/wintermute/persistent.h" +#include "common/math.h" +#include + +namespace Wintermute { + +////////////////////////////////////////////////////////////////////// +// Construction/Destruction +////////////////////////////////////////////////////////////////////// + + +IMPLEMENT_PERSISTENT(SXMath, true) + +BaseScriptable *makeSXMath(BaseGame *inGame) { + return new SXMath(inGame); +} + +////////////////////////////////////////////////////////////////////////// +SXMath::SXMath(BaseGame *inGame) : BaseScriptable(inGame) { + +} + + +////////////////////////////////////////////////////////////////////////// +SXMath::~SXMath() { + +} + + +////////////////////////////////////////////////////////////////////////// +bool SXMath::scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack, const char *name) { + ////////////////////////////////////////////////////////////////////////// + // Abs + ////////////////////////////////////////////////////////////////////////// + if (strcmp(name, "Abs") == 0) { + stack->correctParams(1); + stack->pushFloat(fabs(stack->pop()->getFloat())); + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // Acos + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "Acos") == 0) { + stack->correctParams(1); + stack->pushFloat(acos(stack->pop()->getFloat())); + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // Asin + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "Asin") == 0) { + stack->correctParams(1); + stack->pushFloat(asin(stack->pop()->getFloat())); + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // Atan + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "Atan") == 0) { + stack->correctParams(1); + stack->pushFloat(atan(stack->pop()->getFloat())); + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // Atan2 + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "Atan2") == 0) { + stack->correctParams(2); + double y = stack->pop()->getFloat(); + double x = stack->pop()->getFloat(); + stack->pushFloat(atan2(y, x)); + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // Ceil + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "Ceil") == 0) { + stack->correctParams(1); + stack->pushFloat(ceil(stack->pop()->getFloat())); + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // Cos + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "Cos") == 0) { + stack->correctParams(1); + stack->pushFloat(cos(degreeToRadian(stack->pop()->getFloat()))); + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // Cosh + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "Cosh") == 0) { + stack->correctParams(1); + stack->pushFloat(cosh(degreeToRadian(stack->pop()->getFloat()))); + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // Exp + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "Exp") == 0) { + stack->correctParams(1); + stack->pushFloat(exp(stack->pop()->getFloat())); + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // Floor + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "Floor") == 0) { + stack->correctParams(1); + stack->pushFloat(floor(stack->pop()->getFloat())); + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // Log + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "Log") == 0) { + stack->correctParams(1); + stack->pushFloat(log(stack->pop()->getFloat())); + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // Log10 + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "Log10") == 0) { + stack->correctParams(1); + stack->pushFloat(log10(stack->pop()->getFloat())); + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // Pow + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "Pow") == 0) { + stack->correctParams(2); + double x = stack->pop()->getFloat(); + double y = stack->pop()->getFloat(); + + stack->pushFloat(pow(x, y)); + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // Sin + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "Sin") == 0) { + stack->correctParams(1); + stack->pushFloat(sin(degreeToRadian(stack->pop()->getFloat()))); + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // Sinh + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "Sinh") == 0) { + stack->correctParams(1); + stack->pushFloat(sinh(degreeToRadian(stack->pop()->getFloat()))); + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // Tan + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "Tan") == 0) { + stack->correctParams(1); + stack->pushFloat(tan(degreeToRadian(stack->pop()->getFloat()))); + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // Tanh + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "Tanh") == 0) { + stack->correctParams(1); + stack->pushFloat(tanh(degreeToRadian(stack->pop()->getFloat()))); + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // Sqrt + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "Sqrt") == 0) { + stack->correctParams(1); + stack->pushFloat(sqrt(stack->pop()->getFloat())); + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // DegToRad + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "DegToRad") == 0) { + stack->correctParams(1); + stack->pushFloat(degreeToRadian(stack->pop()->getFloat())); + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // RadToDeg + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "RadToDeg") == 0) { + stack->correctParams(1); + stack->pushFloat(radianToDegree(stack->pop()->getFloat())); + return STATUS_OK; + } else { + return STATUS_FAILED; + } +} + + +////////////////////////////////////////////////////////////////////////// +ScValue *SXMath::scGetProperty(const char *name) { + _scValue->setNULL(); + + ////////////////////////////////////////////////////////////////////////// + // Type + ////////////////////////////////////////////////////////////////////////// + if (strcmp(name, "Type") == 0) { + _scValue->setString("math"); + return _scValue; + } + + ////////////////////////////////////////////////////////////////////////// + // PI + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "PI") == 0) { + _scValue->setFloat(M_PI); + return _scValue; + } else { + return _scValue; + } +} + + +////////////////////////////////////////////////////////////////////////// +double SXMath::degreeToRadian(double value) { + return value * (M_PI / 180.0f); +} + + +////////////////////////////////////////////////////////////////////////// +double SXMath::radianToDegree(double value) { + return value * (180.0f / M_PI); +} + + +////////////////////////////////////////////////////////////////////////// +bool SXMath::persist(BasePersistenceManager *persistMgr) { + + BaseScriptable::persist(persistMgr); + return STATUS_OK; +} + +} // end of namespace Wintermute diff --git a/engines/wintermute/base/scriptables/script_ext_math.h b/engines/wintermute/base/scriptables/script_ext_math.h index b195c0785d..f86d59fe7b 100644 --- a/engines/wintermute/base/scriptables/script_ext_math.h +++ b/engines/wintermute/base/scriptables/script_ext_math.h @@ -1,53 +1,53 @@ -/* ScummVM - Graphic Adventure Engine - * - * ScummVM is the legal property of its developers, whose names - * are too numerous to list here. Please refer to the COPYRIGHT - * file distributed with this source distribution. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - */ - -/* - * This file is based on WME Lite. - * http://dead-code.org/redir.php?target=wmelite - * Copyright (c) 2011 Jan Nedoma - */ - -#ifndef WINTERMUTE_SXMATH_H -#define WINTERMUTE_SXMATH_H - - -#include "engines/wintermute/base/base_scriptable.h" - -namespace Wintermute { - -class SXMath : public BaseScriptable { -public: - DECLARE_PERSISTENT(SXMath, BaseScriptable) - SXMath(BaseGame *inGame); - virtual ~SXMath(); - virtual ScValue *scGetProperty(const char *name); - virtual bool scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack, const char *name); - -private: - double degreeToRadian(double value); - double radianToDegree(double value); - -}; - -} // end of namespace Wintermute - -#endif +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +/* + * This file is based on WME Lite. + * http://dead-code.org/redir.php?target=wmelite + * Copyright (c) 2011 Jan Nedoma + */ + +#ifndef WINTERMUTE_SXMATH_H +#define WINTERMUTE_SXMATH_H + + +#include "engines/wintermute/base/base_scriptable.h" + +namespace Wintermute { + +class SXMath : public BaseScriptable { +public: + DECLARE_PERSISTENT(SXMath, BaseScriptable) + SXMath(BaseGame *inGame); + virtual ~SXMath(); + virtual ScValue *scGetProperty(const char *name); + virtual bool scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack, const char *name); + +private: + double degreeToRadian(double value); + double radianToDegree(double value); + +}; + +} // end of namespace Wintermute + +#endif diff --git a/engines/wintermute/base/scriptables/script_ext_mem_buffer.cpp b/engines/wintermute/base/scriptables/script_ext_mem_buffer.cpp index eef1931d8b..5ed9bd5313 100644 --- a/engines/wintermute/base/scriptables/script_ext_mem_buffer.cpp +++ b/engines/wintermute/base/scriptables/script_ext_mem_buffer.cpp @@ -1,529 +1,529 @@ -/* ScummVM - Graphic Adventure Engine - * - * ScummVM is the legal property of its developers, whose names - * are too numerous to list here. Please refer to the COPYRIGHT - * file distributed with this source distribution. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - */ - -/* - * This file is based on WME Lite. - * http://dead-code.org/redir.php?target=wmelite - * Copyright (c) 2011 Jan Nedoma - */ - -#include "engines/wintermute/base/base_scriptable.h" -#include "engines/wintermute/base/scriptables/script_stack.h" -#include "engines/wintermute/base/scriptables/script.h" -#include "engines/wintermute/base/scriptables/script_value.h" -#include "engines/wintermute/base/scriptables/script_ext_mem_buffer.h" -#include "common/file.h" - -namespace Wintermute { - -IMPLEMENT_PERSISTENT(SXMemBuffer, false) - -BaseScriptable *makeSXMemBuffer(BaseGame *inGame, ScStack *stack) { - return new SXMemBuffer(inGame, stack); -} - -////////////////////////////////////////////////////////////////////////// -SXMemBuffer::SXMemBuffer(BaseGame *inGame, ScStack *stack) : BaseScriptable(inGame) { - stack->correctParams(1); - _buffer = NULL; - _size = 0; - - int newSize = stack->pop()->getInt(); - resize(MAX(0, newSize)); -} - -////////////////////////////////////////////////////////////////////////// -SXMemBuffer::SXMemBuffer(BaseGame *inGame, void *buffer) : BaseScriptable(inGame) { - _size = 0; - _buffer = buffer; -} - - -////////////////////////////////////////////////////////////////////////// -SXMemBuffer::~SXMemBuffer() { - cleanup(); -} - -////////////////////////////////////////////////////////////////////////// -void *SXMemBuffer::scToMemBuffer() { - return _buffer; -} - -////////////////////////////////////////////////////////////////////////// -void SXMemBuffer::cleanup() { - if (_size) { - free(_buffer); - } - _buffer = NULL; - _size = 0; -} - -////////////////////////////////////////////////////////////////////////// -bool SXMemBuffer::resize(int newSize) { - int oldSize = _size; - - if (_size == 0) { - _buffer = malloc(newSize); - if (_buffer) { - _size = newSize; - } - } else { - void *newBuf = realloc(_buffer, newSize); - if (!newBuf) { - if (newSize == 0) { - _buffer = newBuf; - _size = newSize; - } else { - return STATUS_FAILED; - } - } else { - _buffer = newBuf; - _size = newSize; - } - } - - if (_buffer && _size > oldSize) { - memset((byte *)_buffer + oldSize, 0, _size - oldSize); - } - return STATUS_OK; -} - -////////////////////////////////////////////////////////////////////////// -bool SXMemBuffer::checkBounds(ScScript *script, int start, int length) { - if (_buffer == NULL) { - script->runtimeError("Cannot use Set/Get methods on an uninitialized memory buffer"); - return false; - } - if (_size == 0) { - return true; - } - - if (start < 0 || length == 0 || start + length > _size) { - script->runtimeError("Set/Get method call is out of bounds"); - return false; - } else { - return true; - } -} - -////////////////////////////////////////////////////////////////////////// -const char *SXMemBuffer::scToString() { - return "[membuffer object]"; -} - - -////////////////////////////////////////////////////////////////////////// -bool SXMemBuffer::scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack, const char *name) { - ////////////////////////////////////////////////////////////////////////// - // SetSize - ////////////////////////////////////////////////////////////////////////// - if (strcmp(name, "SetSize") == 0) { - stack->correctParams(1); - int newSize = stack->pop()->getInt(); - newSize = MAX(0, newSize); - if (DID_SUCCEED(resize(newSize))) { - stack->pushBool(true); - } else { - stack->pushBool(false); - } - - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // GetBool - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "GetBool") == 0) { - stack->correctParams(1); - int start = stack->pop()->getInt(); - if (!checkBounds(script, start, sizeof(bool))) { - stack->pushNULL(); - } else { - stack->pushBool(*(bool *)((byte *)_buffer + start)); - } - - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // GetByte - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "GetByte") == 0) { - stack->correctParams(1); - int start = stack->pop()->getInt(); - if (!checkBounds(script, start, sizeof(byte))) { - stack->pushNULL(); - } else { - stack->pushInt(*(byte *)((byte *)_buffer + start)); - } - - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // GetShort - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "GetShort") == 0) { - stack->correctParams(1); - int start = stack->pop()->getInt(); - if (!checkBounds(script, start, sizeof(short))) { - stack->pushNULL(); - } else { - stack->pushInt(65536 + * (short *)((byte *)_buffer + start)); - } - - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // GetInt / GetLong - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "GetInt") == 0 || strcmp(name, "GetLong") == 0) { - stack->correctParams(1); - int start = stack->pop()->getInt(); - if (!checkBounds(script, start, sizeof(int))) { - stack->pushNULL(); - } else { - stack->pushInt(*(int *)((byte *)_buffer + start)); - } - - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // GetFloat - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "GetFloat") == 0) { - stack->correctParams(1); - int start = stack->pop()->getInt(); - if (!checkBounds(script, start, sizeof(float))) { - stack->pushNULL(); - } else { - stack->pushFloat(*(float *)((byte *)_buffer + start)); - } - - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // GetDouble - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "GetDouble") == 0) { - stack->correctParams(1); - int start = stack->pop()->getInt(); - if (!checkBounds(script, start, sizeof(double))) { - stack->pushNULL(); - } else { - stack->pushFloat(*(double *)((byte *)_buffer + start)); - } - - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // GetString - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "GetString") == 0) { - stack->correctParams(2); - int start = stack->pop()->getInt(); - int length = stack->pop()->getInt(); - - // find end of string - if (length == 0 && start >= 0 && start < _size) { - for (int i = start; i < _size; i++) { - if (((char *)_buffer)[i] == '\0') { - length = i - start; - break; - } - } - } - - if (!checkBounds(script, start, length)) { - stack->pushNULL(); - } else { - char *str = new char[length + 1]; - Common::strlcpy(str, (const char *)_buffer + start, length + 1); - stack->pushString(str); - delete[] str; - } - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // GetPointer - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "GetPointer") == 0) { - stack->correctParams(1); - int start = stack->pop()->getInt(); - if (!checkBounds(script, start, sizeof(void *))) { - stack->pushNULL(); - } else { - void *pointer = *(void **)((byte *)_buffer + start); - SXMemBuffer *buf = new SXMemBuffer(_gameRef, pointer); - stack->pushNative(buf, false); - } - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // SetBool - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "SetBool") == 0) { - stack->correctParams(2); - int start = stack->pop()->getInt(); - bool val = stack->pop()->getBool(); - - if (!checkBounds(script, start, sizeof(bool))) { - stack->pushBool(false); - } else { - *(bool *)((byte *)_buffer + start) = val; - stack->pushBool(true); - } - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // SetByte - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "SetByte") == 0) { - stack->correctParams(2); - int start = stack->pop()->getInt(); - byte val = (byte)stack->pop()->getInt(); - - if (!checkBounds(script, start, sizeof(byte))) { - stack->pushBool(false); - } else { - *(byte *)((byte *)_buffer + start) = val; - stack->pushBool(true); - } - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // SetShort - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "SetShort") == 0) { - stack->correctParams(2); - int start = stack->pop()->getInt(); - short val = (short)stack->pop()->getInt(); - - if (!checkBounds(script, start, sizeof(short))) { - stack->pushBool(false); - } else { - *(short *)((byte *)_buffer + start) = val; - stack->pushBool(true); - } - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // SetInt / SetLong - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "SetInt") == 0 || strcmp(name, "SetLong") == 0) { - stack->correctParams(2); - int start = stack->pop()->getInt(); - int val = stack->pop()->getInt(); - - if (!checkBounds(script, start, sizeof(int))) { - stack->pushBool(false); - } else { - *(int *)((byte *)_buffer + start) = val; - stack->pushBool(true); - } - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // SetFloat - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "SetFloat") == 0) { - stack->correctParams(2); - int start = stack->pop()->getInt(); - float val = (float)stack->pop()->getFloat(); - - if (!checkBounds(script, start, sizeof(float))) { - stack->pushBool(false); - } else { - *(float *)((byte *)_buffer + start) = val; - stack->pushBool(true); - } - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // SetDouble - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "SetDouble") == 0) { - stack->correctParams(2); - int start = stack->pop()->getInt(); - double val = stack->pop()->getFloat(); - - if (!checkBounds(script, start, sizeof(double))) { - stack->pushBool(false); - } else { - *(double *)((byte *)_buffer + start) = val; - stack->pushBool(true); - } - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // SetString - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "SetString") == 0) { - stack->correctParams(2); - int start = stack->pop()->getInt(); - const char *val = stack->pop()->getString(); - - if (!checkBounds(script, start, strlen(val) + 1)) { - stack->pushBool(false); - } else { - memcpy((byte *)_buffer + start, val, strlen(val) + 1); - stack->pushBool(true); - } - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // SetPointer - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "SetPointer") == 0) { - stack->correctParams(2); - int start = stack->pop()->getInt(); - /* ScValue *val = */ stack->pop(); - - if (!checkBounds(script, start, sizeof(void *))) { - stack->pushBool(false); - } else { - /* - int pointer = (int)Val->getMemBuffer(); - memcpy((byte *)_buffer+Start, &Pointer, sizeof(void*)); - stack->pushBool(true); - */ - // TODO fix - stack->pushBool(false); - - } - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // DEBUG_Dump - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "DEBUG_Dump") == 0) { - stack->correctParams(0); - if (_buffer && _size) { - warning("SXMemBuffer::ScCallMethod - DEBUG_Dump"); - Common::DumpFile f; - f.open("buffer.bin"); - f.write(_buffer, _size); - f.close(); - } - stack->pushNULL(); - return STATUS_OK; - } else { - return STATUS_FAILED; - } -} - - -////////////////////////////////////////////////////////////////////////// -ScValue *SXMemBuffer::scGetProperty(const char *name) { - _scValue->setNULL(); - - ////////////////////////////////////////////////////////////////////////// - // Type (RO) - ////////////////////////////////////////////////////////////////////////// - if (strcmp(name, "Type") == 0) { - _scValue->setString("membuffer"); - return _scValue; - } - - ////////////////////////////////////////////////////////////////////////// - // Size (RO) - ////////////////////////////////////////////////////////////////////////// - if (strcmp(name, "Size") == 0) { - _scValue->setInt(_size); - return _scValue; - } else { - return BaseScriptable::scGetProperty(name); - } -} - - -////////////////////////////////////////////////////////////////////////// -bool SXMemBuffer::scSetProperty(const char *name, ScValue *value) { - /* - ////////////////////////////////////////////////////////////////////////// - // Length - ////////////////////////////////////////////////////////////////////////// - if (strcmp(name, "Length")==0){ - int origLength = _length; - _length = max(value->getInt(0), 0); - - char propName[20]; - if (_length < OrigLength){ - for(int i=_length; iDeleteProp(PropName); - } - } - return STATUS_OK; - } - else*/ return BaseScriptable::scSetProperty(name, value); -} - - -////////////////////////////////////////////////////////////////////////// -bool SXMemBuffer::persist(BasePersistenceManager *persistMgr) { - - BaseScriptable::persist(persistMgr); - - persistMgr->transfer(TMEMBER(_size)); - - if (persistMgr->getIsSaving()) { - if (_size > 0) { - persistMgr->putBytes((byte *)_buffer, _size); - } - } else { - if (_size > 0) { - _buffer = malloc(_size); - persistMgr->getBytes((byte *)_buffer, _size); - } else { - _buffer = NULL; - } - } - - return STATUS_OK; -} - - -////////////////////////////////////////////////////////////////////////// -int SXMemBuffer::scCompare(BaseScriptable *val) { - if (_buffer == val->scToMemBuffer()) { - return 0; - } else { - return 1; - } -} - -} // end of namespace Wintermute +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +/* + * This file is based on WME Lite. + * http://dead-code.org/redir.php?target=wmelite + * Copyright (c) 2011 Jan Nedoma + */ + +#include "engines/wintermute/base/base_scriptable.h" +#include "engines/wintermute/base/scriptables/script_stack.h" +#include "engines/wintermute/base/scriptables/script.h" +#include "engines/wintermute/base/scriptables/script_value.h" +#include "engines/wintermute/base/scriptables/script_ext_mem_buffer.h" +#include "common/file.h" + +namespace Wintermute { + +IMPLEMENT_PERSISTENT(SXMemBuffer, false) + +BaseScriptable *makeSXMemBuffer(BaseGame *inGame, ScStack *stack) { + return new SXMemBuffer(inGame, stack); +} + +////////////////////////////////////////////////////////////////////////// +SXMemBuffer::SXMemBuffer(BaseGame *inGame, ScStack *stack) : BaseScriptable(inGame) { + stack->correctParams(1); + _buffer = NULL; + _size = 0; + + int newSize = stack->pop()->getInt(); + resize(MAX(0, newSize)); +} + +////////////////////////////////////////////////////////////////////////// +SXMemBuffer::SXMemBuffer(BaseGame *inGame, void *buffer) : BaseScriptable(inGame) { + _size = 0; + _buffer = buffer; +} + + +////////////////////////////////////////////////////////////////////////// +SXMemBuffer::~SXMemBuffer() { + cleanup(); +} + +////////////////////////////////////////////////////////////////////////// +void *SXMemBuffer::scToMemBuffer() { + return _buffer; +} + +////////////////////////////////////////////////////////////////////////// +void SXMemBuffer::cleanup() { + if (_size) { + free(_buffer); + } + _buffer = NULL; + _size = 0; +} + +////////////////////////////////////////////////////////////////////////// +bool SXMemBuffer::resize(int newSize) { + int oldSize = _size; + + if (_size == 0) { + _buffer = malloc(newSize); + if (_buffer) { + _size = newSize; + } + } else { + void *newBuf = realloc(_buffer, newSize); + if (!newBuf) { + if (newSize == 0) { + _buffer = newBuf; + _size = newSize; + } else { + return STATUS_FAILED; + } + } else { + _buffer = newBuf; + _size = newSize; + } + } + + if (_buffer && _size > oldSize) { + memset((byte *)_buffer + oldSize, 0, _size - oldSize); + } + return STATUS_OK; +} + +////////////////////////////////////////////////////////////////////////// +bool SXMemBuffer::checkBounds(ScScript *script, int start, int length) { + if (_buffer == NULL) { + script->runtimeError("Cannot use Set/Get methods on an uninitialized memory buffer"); + return false; + } + if (_size == 0) { + return true; + } + + if (start < 0 || length == 0 || start + length > _size) { + script->runtimeError("Set/Get method call is out of bounds"); + return false; + } else { + return true; + } +} + +////////////////////////////////////////////////////////////////////////// +const char *SXMemBuffer::scToString() { + return "[membuffer object]"; +} + + +////////////////////////////////////////////////////////////////////////// +bool SXMemBuffer::scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack, const char *name) { + ////////////////////////////////////////////////////////////////////////// + // SetSize + ////////////////////////////////////////////////////////////////////////// + if (strcmp(name, "SetSize") == 0) { + stack->correctParams(1); + int newSize = stack->pop()->getInt(); + newSize = MAX(0, newSize); + if (DID_SUCCEED(resize(newSize))) { + stack->pushBool(true); + } else { + stack->pushBool(false); + } + + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // GetBool + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "GetBool") == 0) { + stack->correctParams(1); + int start = stack->pop()->getInt(); + if (!checkBounds(script, start, sizeof(bool))) { + stack->pushNULL(); + } else { + stack->pushBool(*(bool *)((byte *)_buffer + start)); + } + + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // GetByte + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "GetByte") == 0) { + stack->correctParams(1); + int start = stack->pop()->getInt(); + if (!checkBounds(script, start, sizeof(byte))) { + stack->pushNULL(); + } else { + stack->pushInt(*(byte *)((byte *)_buffer + start)); + } + + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // GetShort + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "GetShort") == 0) { + stack->correctParams(1); + int start = stack->pop()->getInt(); + if (!checkBounds(script, start, sizeof(short))) { + stack->pushNULL(); + } else { + stack->pushInt(65536 + * (short *)((byte *)_buffer + start)); + } + + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // GetInt / GetLong + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "GetInt") == 0 || strcmp(name, "GetLong") == 0) { + stack->correctParams(1); + int start = stack->pop()->getInt(); + if (!checkBounds(script, start, sizeof(int))) { + stack->pushNULL(); + } else { + stack->pushInt(*(int *)((byte *)_buffer + start)); + } + + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // GetFloat + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "GetFloat") == 0) { + stack->correctParams(1); + int start = stack->pop()->getInt(); + if (!checkBounds(script, start, sizeof(float))) { + stack->pushNULL(); + } else { + stack->pushFloat(*(float *)((byte *)_buffer + start)); + } + + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // GetDouble + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "GetDouble") == 0) { + stack->correctParams(1); + int start = stack->pop()->getInt(); + if (!checkBounds(script, start, sizeof(double))) { + stack->pushNULL(); + } else { + stack->pushFloat(*(double *)((byte *)_buffer + start)); + } + + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // GetString + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "GetString") == 0) { + stack->correctParams(2); + int start = stack->pop()->getInt(); + int length = stack->pop()->getInt(); + + // find end of string + if (length == 0 && start >= 0 && start < _size) { + for (int i = start; i < _size; i++) { + if (((char *)_buffer)[i] == '\0') { + length = i - start; + break; + } + } + } + + if (!checkBounds(script, start, length)) { + stack->pushNULL(); + } else { + char *str = new char[length + 1]; + Common::strlcpy(str, (const char *)_buffer + start, length + 1); + stack->pushString(str); + delete[] str; + } + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // GetPointer + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "GetPointer") == 0) { + stack->correctParams(1); + int start = stack->pop()->getInt(); + if (!checkBounds(script, start, sizeof(void *))) { + stack->pushNULL(); + } else { + void *pointer = *(void **)((byte *)_buffer + start); + SXMemBuffer *buf = new SXMemBuffer(_gameRef, pointer); + stack->pushNative(buf, false); + } + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // SetBool + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "SetBool") == 0) { + stack->correctParams(2); + int start = stack->pop()->getInt(); + bool val = stack->pop()->getBool(); + + if (!checkBounds(script, start, sizeof(bool))) { + stack->pushBool(false); + } else { + *(bool *)((byte *)_buffer + start) = val; + stack->pushBool(true); + } + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // SetByte + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "SetByte") == 0) { + stack->correctParams(2); + int start = stack->pop()->getInt(); + byte val = (byte)stack->pop()->getInt(); + + if (!checkBounds(script, start, sizeof(byte))) { + stack->pushBool(false); + } else { + *(byte *)((byte *)_buffer + start) = val; + stack->pushBool(true); + } + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // SetShort + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "SetShort") == 0) { + stack->correctParams(2); + int start = stack->pop()->getInt(); + short val = (short)stack->pop()->getInt(); + + if (!checkBounds(script, start, sizeof(short))) { + stack->pushBool(false); + } else { + *(short *)((byte *)_buffer + start) = val; + stack->pushBool(true); + } + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // SetInt / SetLong + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "SetInt") == 0 || strcmp(name, "SetLong") == 0) { + stack->correctParams(2); + int start = stack->pop()->getInt(); + int val = stack->pop()->getInt(); + + if (!checkBounds(script, start, sizeof(int))) { + stack->pushBool(false); + } else { + *(int *)((byte *)_buffer + start) = val; + stack->pushBool(true); + } + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // SetFloat + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "SetFloat") == 0) { + stack->correctParams(2); + int start = stack->pop()->getInt(); + float val = (float)stack->pop()->getFloat(); + + if (!checkBounds(script, start, sizeof(float))) { + stack->pushBool(false); + } else { + *(float *)((byte *)_buffer + start) = val; + stack->pushBool(true); + } + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // SetDouble + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "SetDouble") == 0) { + stack->correctParams(2); + int start = stack->pop()->getInt(); + double val = stack->pop()->getFloat(); + + if (!checkBounds(script, start, sizeof(double))) { + stack->pushBool(false); + } else { + *(double *)((byte *)_buffer + start) = val; + stack->pushBool(true); + } + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // SetString + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "SetString") == 0) { + stack->correctParams(2); + int start = stack->pop()->getInt(); + const char *val = stack->pop()->getString(); + + if (!checkBounds(script, start, strlen(val) + 1)) { + stack->pushBool(false); + } else { + memcpy((byte *)_buffer + start, val, strlen(val) + 1); + stack->pushBool(true); + } + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // SetPointer + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "SetPointer") == 0) { + stack->correctParams(2); + int start = stack->pop()->getInt(); + /* ScValue *val = */ stack->pop(); + + if (!checkBounds(script, start, sizeof(void *))) { + stack->pushBool(false); + } else { + /* + int pointer = (int)Val->getMemBuffer(); + memcpy((byte *)_buffer+Start, &Pointer, sizeof(void*)); + stack->pushBool(true); + */ + // TODO fix + stack->pushBool(false); + + } + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // DEBUG_Dump + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "DEBUG_Dump") == 0) { + stack->correctParams(0); + if (_buffer && _size) { + warning("SXMemBuffer::ScCallMethod - DEBUG_Dump"); + Common::DumpFile f; + f.open("buffer.bin"); + f.write(_buffer, _size); + f.close(); + } + stack->pushNULL(); + return STATUS_OK; + } else { + return STATUS_FAILED; + } +} + + +////////////////////////////////////////////////////////////////////////// +ScValue *SXMemBuffer::scGetProperty(const char *name) { + _scValue->setNULL(); + + ////////////////////////////////////////////////////////////////////////// + // Type (RO) + ////////////////////////////////////////////////////////////////////////// + if (strcmp(name, "Type") == 0) { + _scValue->setString("membuffer"); + return _scValue; + } + + ////////////////////////////////////////////////////////////////////////// + // Size (RO) + ////////////////////////////////////////////////////////////////////////// + if (strcmp(name, "Size") == 0) { + _scValue->setInt(_size); + return _scValue; + } else { + return BaseScriptable::scGetProperty(name); + } +} + + +////////////////////////////////////////////////////////////////////////// +bool SXMemBuffer::scSetProperty(const char *name, ScValue *value) { + /* + ////////////////////////////////////////////////////////////////////////// + // Length + ////////////////////////////////////////////////////////////////////////// + if (strcmp(name, "Length")==0){ + int origLength = _length; + _length = max(value->getInt(0), 0); + + char propName[20]; + if (_length < OrigLength){ + for(int i=_length; iDeleteProp(PropName); + } + } + return STATUS_OK; + } + else*/ return BaseScriptable::scSetProperty(name, value); +} + + +////////////////////////////////////////////////////////////////////////// +bool SXMemBuffer::persist(BasePersistenceManager *persistMgr) { + + BaseScriptable::persist(persistMgr); + + persistMgr->transfer(TMEMBER(_size)); + + if (persistMgr->getIsSaving()) { + if (_size > 0) { + persistMgr->putBytes((byte *)_buffer, _size); + } + } else { + if (_size > 0) { + _buffer = malloc(_size); + persistMgr->getBytes((byte *)_buffer, _size); + } else { + _buffer = NULL; + } + } + + return STATUS_OK; +} + + +////////////////////////////////////////////////////////////////////////// +int SXMemBuffer::scCompare(BaseScriptable *val) { + if (_buffer == val->scToMemBuffer()) { + return 0; + } else { + return 1; + } +} + +} // end of namespace Wintermute diff --git a/engines/wintermute/base/scriptables/script_ext_mem_buffer.h b/engines/wintermute/base/scriptables/script_ext_mem_buffer.h index 0a16167b58..d2662b3036 100644 --- a/engines/wintermute/base/scriptables/script_ext_mem_buffer.h +++ b/engines/wintermute/base/scriptables/script_ext_mem_buffer.h @@ -1,60 +1,60 @@ -/* ScummVM - Graphic Adventure Engine - * - * ScummVM is the legal property of its developers, whose names - * are too numerous to list here. Please refer to the COPYRIGHT - * file distributed with this source distribution. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - */ - -/* - * This file is based on WME Lite. - * http://dead-code.org/redir.php?target=wmelite - * Copyright (c) 2011 Jan Nedoma - */ - -#ifndef WINTERMUTE_SXMEMBUFFER_H -#define WINTERMUTE_SXMEMBUFFER_H - - -#include "engines/wintermute/base/base_scriptable.h" - -namespace Wintermute { - -class SXMemBuffer : public BaseScriptable { -public: - virtual int scCompare(BaseScriptable *Val); - DECLARE_PERSISTENT(SXMemBuffer, BaseScriptable) - ScValue *scGetProperty(const char *name); - bool scSetProperty(const char *name, ScValue *value); - bool scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack, const char *name); - const char *scToString(); - SXMemBuffer(BaseGame *inGame, ScStack *stack); - SXMemBuffer(BaseGame *inGame, void *buffer); - virtual ~SXMemBuffer(); - virtual void *scToMemBuffer(); -private: - int _size; - - bool resize(int newSize); - void *_buffer; - void cleanup(); - bool checkBounds(ScScript *script, int start, int length); -}; - -} // end of namespace Wintermute - -#endif +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +/* + * This file is based on WME Lite. + * http://dead-code.org/redir.php?target=wmelite + * Copyright (c) 2011 Jan Nedoma + */ + +#ifndef WINTERMUTE_SXMEMBUFFER_H +#define WINTERMUTE_SXMEMBUFFER_H + + +#include "engines/wintermute/base/base_scriptable.h" + +namespace Wintermute { + +class SXMemBuffer : public BaseScriptable { +public: + virtual int scCompare(BaseScriptable *Val); + DECLARE_PERSISTENT(SXMemBuffer, BaseScriptable) + ScValue *scGetProperty(const char *name); + bool scSetProperty(const char *name, ScValue *value); + bool scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack, const char *name); + const char *scToString(); + SXMemBuffer(BaseGame *inGame, ScStack *stack); + SXMemBuffer(BaseGame *inGame, void *buffer); + virtual ~SXMemBuffer(); + virtual void *scToMemBuffer(); +private: + int _size; + + bool resize(int newSize); + void *_buffer; + void cleanup(); + bool checkBounds(ScScript *script, int start, int length); +}; + +} // end of namespace Wintermute + +#endif diff --git a/engines/wintermute/base/scriptables/script_ext_object.cpp b/engines/wintermute/base/scriptables/script_ext_object.cpp index 40c9b885cd..b87aac81f9 100644 --- a/engines/wintermute/base/scriptables/script_ext_object.cpp +++ b/engines/wintermute/base/scriptables/script_ext_object.cpp @@ -1,67 +1,67 @@ -/* ScummVM - Graphic Adventure Engine - * - * ScummVM is the legal property of its developers, whose names - * are too numerous to list here. Please refer to the COPYRIGHT - * file distributed with this source distribution. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - */ - -/* - * This file is based on WME Lite. - * http://dead-code.org/redir.php?target=wmelite - * Copyright (c) 2011 Jan Nedoma - */ - -#include "engines/wintermute/base/scriptables/script_ext_object.h" -#include "engines/wintermute/base/scriptables/script_value.h" -#include "engines/wintermute/base/scriptables/script_stack.h" - -namespace Wintermute { - -////////////////////////////////////////////////////////////////////// -// Construction/Destruction -////////////////////////////////////////////////////////////////////// - -IMPLEMENT_PERSISTENT(SXObject, false) - -BaseScriptable *makeSXObject(BaseGame *inGame, ScStack *stack) { - return new SXObject(inGame, stack); -} - -////////////////////////////////////////////////////////////////////////// -SXObject::SXObject(BaseGame *inGame, ScStack *stack) : BaseObject(inGame) { - int numParams = stack->pop()->getInt(0); - for (int i = 0; i < numParams; i++) { - addScript(stack->pop()->getString()); - } -} - - -////////////////////////////////////////////////////////////////////////// -SXObject::~SXObject() { - -} - - -////////////////////////////////////////////////////////////////////////// -bool SXObject::persist(BasePersistenceManager *persistMgr) { - BaseObject::persist(persistMgr); - - return STATUS_OK; -} - -} // end of namespace Wintermute +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +/* + * This file is based on WME Lite. + * http://dead-code.org/redir.php?target=wmelite + * Copyright (c) 2011 Jan Nedoma + */ + +#include "engines/wintermute/base/scriptables/script_ext_object.h" +#include "engines/wintermute/base/scriptables/script_value.h" +#include "engines/wintermute/base/scriptables/script_stack.h" + +namespace Wintermute { + +////////////////////////////////////////////////////////////////////// +// Construction/Destruction +////////////////////////////////////////////////////////////////////// + +IMPLEMENT_PERSISTENT(SXObject, false) + +BaseScriptable *makeSXObject(BaseGame *inGame, ScStack *stack) { + return new SXObject(inGame, stack); +} + +////////////////////////////////////////////////////////////////////////// +SXObject::SXObject(BaseGame *inGame, ScStack *stack) : BaseObject(inGame) { + int numParams = stack->pop()->getInt(0); + for (int i = 0; i < numParams; i++) { + addScript(stack->pop()->getString()); + } +} + + +////////////////////////////////////////////////////////////////////////// +SXObject::~SXObject() { + +} + + +////////////////////////////////////////////////////////////////////////// +bool SXObject::persist(BasePersistenceManager *persistMgr) { + BaseObject::persist(persistMgr); + + return STATUS_OK; +} + +} // end of namespace Wintermute diff --git a/engines/wintermute/base/scriptables/script_ext_object.h b/engines/wintermute/base/scriptables/script_ext_object.h index 32aa00776e..c85d16d44e 100644 --- a/engines/wintermute/base/scriptables/script_ext_object.h +++ b/engines/wintermute/base/scriptables/script_ext_object.h @@ -1,46 +1,46 @@ -/* ScummVM - Graphic Adventure Engine - * - * ScummVM is the legal property of its developers, whose names - * are too numerous to list here. Please refer to the COPYRIGHT - * file distributed with this source distribution. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - */ - -/* - * This file is based on WME Lite. - * http://dead-code.org/redir.php?target=wmelite - * Copyright (c) 2011 Jan Nedoma - */ - -#ifndef WINTERMUTE_SXOBJECT_H -#define WINTERMUTE_SXOBJECT_H - - -#include "engines/wintermute/base/base_object.h" - -namespace Wintermute { - -class SXObject : public BaseObject { -public: - DECLARE_PERSISTENT(SXObject, BaseObject) - SXObject(BaseGame *inGame, ScStack *Stack); - virtual ~SXObject(); -}; - -} // end of namespace Wintermute - -#endif +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +/* + * This file is based on WME Lite. + * http://dead-code.org/redir.php?target=wmelite + * Copyright (c) 2011 Jan Nedoma + */ + +#ifndef WINTERMUTE_SXOBJECT_H +#define WINTERMUTE_SXOBJECT_H + + +#include "engines/wintermute/base/base_object.h" + +namespace Wintermute { + +class SXObject : public BaseObject { +public: + DECLARE_PERSISTENT(SXObject, BaseObject) + SXObject(BaseGame *inGame, ScStack *Stack); + virtual ~SXObject(); +}; + +} // end of namespace Wintermute + +#endif diff --git a/engines/wintermute/base/scriptables/script_ext_string.cpp b/engines/wintermute/base/scriptables/script_ext_string.cpp index 9bcfe28bbf..8d87a92dc1 100644 --- a/engines/wintermute/base/scriptables/script_ext_string.cpp +++ b/engines/wintermute/base/scriptables/script_ext_string.cpp @@ -1,436 +1,436 @@ -/* ScummVM - Graphic Adventure Engine - * - * ScummVM is the legal property of its developers, whose names - * are too numerous to list here. Please refer to the COPYRIGHT - * file distributed with this source distribution. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - */ - -/* - * This file is based on WME Lite. - * http://dead-code.org/redir.php?target=wmelite - * Copyright (c) 2011 Jan Nedoma - */ - -#include "engines/wintermute/base/base_game.h" -#include "engines/wintermute/base/scriptables/script_stack.h" -#include "engines/wintermute/base/scriptables/script_value.h" -#include "engines/wintermute/utils/utils.h" -#include "engines/wintermute/base/scriptables/script_ext_string.h" -#include "engines/wintermute/base/scriptables/script_ext_array.h" -#include "engines/wintermute/utils/string_util.h" -#include "common/tokenizer.h" - -namespace Wintermute { - -IMPLEMENT_PERSISTENT(SXString, false) - -BaseScriptable *makeSXString(BaseGame *inGame, ScStack *stack) { - return new SXString(inGame, stack); -} - -////////////////////////////////////////////////////////////////////////// -SXString::SXString(BaseGame *inGame, ScStack *stack) : BaseScriptable(inGame) { - _string = NULL; - _capacity = 0; - - stack->correctParams(1); - ScValue *val = stack->pop(); - - if (val->isInt()) { - _capacity = MAX(0, val->getInt()); - if (_capacity > 0) { - _string = new char[_capacity]; - memset(_string, 0, _capacity); - } - } else { - setStringVal(val->getString()); - } - - if (_capacity == 0) { - setStringVal(""); - } -} - - -////////////////////////////////////////////////////////////////////////// -SXString::~SXString() { - if (_string) { - delete[] _string; - } -} - - -////////////////////////////////////////////////////////////////////////// -void SXString::setStringVal(const char *val) { - int len = strlen(val); - if (len >= _capacity) { - _capacity = len + 1; - delete[] _string; - _string = NULL; - _string = new char[_capacity]; - memset(_string, 0, _capacity); - } - strcpy(_string, val); -} - - -////////////////////////////////////////////////////////////////////////// -const char *SXString::scToString() { - if (_string) { - return _string; - } else { - return "[null string]"; - } -} - - -////////////////////////////////////////////////////////////////////////// -void SXString::scSetString(const char *val) { - setStringVal(val); -} - - -////////////////////////////////////////////////////////////////////////// -bool SXString::scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack, const char *name) { - ////////////////////////////////////////////////////////////////////////// - // Substring - ////////////////////////////////////////////////////////////////////////// - if (strcmp(name, "Substring") == 0) { - stack->correctParams(2); - int start = stack->pop()->getInt(); - int end = stack->pop()->getInt(); - - if (end < start) { - BaseUtils::swap(&start, &end); - } - - //try { - WideString str; - if (_gameRef->_textEncoding == TEXT_UTF8) { - str = StringUtil::utf8ToWide(_string); - } else { - str = StringUtil::ansiToWide(_string); - } - - //WideString subStr = str.substr(start, end - start + 1); - WideString subStr(str.c_str() + start, end - start + 1); - - if (_gameRef->_textEncoding == TEXT_UTF8) { - stack->pushString(StringUtil::wideToUtf8(subStr).c_str()); - } else { - stack->pushString(StringUtil::wideToAnsi(subStr).c_str()); - } - // } catch (std::exception &) { - // stack->pushNULL(); - // } - - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // Substr - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "Substr") == 0) { - stack->correctParams(2); - int start = stack->pop()->getInt(); - - ScValue *val = stack->pop(); - int len = val->getInt(); - - if (!val->isNULL() && len <= 0) { - stack->pushString(""); - return STATUS_OK; - } - - if (val->isNULL()) { - len = strlen(_string) - start; - } - -// try { - WideString str; - if (_gameRef->_textEncoding == TEXT_UTF8) { - str = StringUtil::utf8ToWide(_string); - } else { - str = StringUtil::ansiToWide(_string); - } - -// WideString subStr = str.substr(start, len); - WideString subStr(str.c_str() + start, len); - - if (_gameRef->_textEncoding == TEXT_UTF8) { - stack->pushString(StringUtil::wideToUtf8(subStr).c_str()); - } else { - stack->pushString(StringUtil::wideToAnsi(subStr).c_str()); - } -// } catch (std::exception &) { -// stack->pushNULL(); -// } - - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // ToUpperCase - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "ToUpperCase") == 0) { - stack->correctParams(0); - - WideString str; - if (_gameRef->_textEncoding == TEXT_UTF8) { - str = StringUtil::utf8ToWide(_string); - } else { - str = StringUtil::ansiToWide(_string); - } - - str.toUppercase(); - - if (_gameRef->_textEncoding == TEXT_UTF8) { - stack->pushString(StringUtil::wideToUtf8(str).c_str()); - } else { - stack->pushString(StringUtil::wideToAnsi(str).c_str()); - } - - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // ToLowerCase - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "ToLowerCase") == 0) { - stack->correctParams(0); - - WideString str; - if (_gameRef->_textEncoding == TEXT_UTF8) { - str = StringUtil::utf8ToWide(_string); - } else { - str = StringUtil::ansiToWide(_string); - } - - str.toLowercase(); - - if (_gameRef->_textEncoding == TEXT_UTF8) { - stack->pushString(StringUtil::wideToUtf8(str).c_str()); - } else { - stack->pushString(StringUtil::wideToAnsi(str).c_str()); - } - - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // IndexOf - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "IndexOf") == 0) { - stack->correctParams(2); - - const char *strToFind = stack->pop()->getString(); - int index = stack->pop()->getInt(); - - WideString str; - if (_gameRef->_textEncoding == TEXT_UTF8) { - str = StringUtil::utf8ToWide(_string); - } else { - str = StringUtil::ansiToWide(_string); - } - - WideString toFind; - if (_gameRef->_textEncoding == TEXT_UTF8) { - toFind = StringUtil::utf8ToWide(strToFind); - } else { - toFind = StringUtil::ansiToWide(strToFind); - } - - int indexOf = StringUtil::indexOf(str, toFind, index); - stack->pushInt(indexOf); - - return STATUS_OK; - } - - ////////////////////////////////////////////////////////////////////////// - // Split - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "Split") == 0) { - stack->correctParams(1); - ScValue *val = stack->pop(); - char separators[MAX_PATH_LENGTH] = ","; - if (!val->isNULL()) { - strcpy(separators, val->getString()); - } - - SXArray *array = new SXArray(_gameRef); - if (!array) { - stack->pushNULL(); - return STATUS_OK; - } - - - WideString str; - if (_gameRef->_textEncoding == TEXT_UTF8) { - str = StringUtil::utf8ToWide(_string); - } else { - str = StringUtil::ansiToWide(_string); - } - - WideString delims; - if (_gameRef->_textEncoding == TEXT_UTF8) { - delims = StringUtil::utf8ToWide(separators); - } else { - delims = StringUtil::ansiToWide(separators); - } - - Common::Array parts; - - - - Common::StringTokenizer tokenizer(str, delims); - while (!tokenizer.empty()) { - Common::String str2 = tokenizer.nextToken(); - parts.push_back(str2); - } - // TODO: Clean this up - /*do { - pos = StringUtil::IndexOf(Common::String(str.c_str() + start), delims, start); - //pos = str.find_first_of(delims, start); - if (pos == start) { - start = pos + 1; - } else if (pos == str.size()) { - parts.push_back(Common::String(str.c_str() + start)); - break; - } else { - parts.push_back(Common::String(str.c_str() + start, pos - start)); - start = pos + 1; - } - //start = str.find_first_not_of(delims, start); - start = StringUtil::LastIndexOf(Common::String(str.c_str() + start), delims, start) + 1; - - } while (pos != str.size());*/ - - for (Common::Array::iterator it = parts.begin(); it != parts.end(); ++it) { - WideString &part = (*it); - - if (_gameRef->_textEncoding == TEXT_UTF8) { - val = new ScValue(_gameRef, StringUtil::wideToUtf8(part).c_str()); - } else { - val = new ScValue(_gameRef, StringUtil::wideToAnsi(part).c_str()); - } - - array->push(val); - delete val; - val = NULL; - } - - stack->pushNative(array, false); - return STATUS_OK; - } else { - return STATUS_FAILED; - } -} - - -////////////////////////////////////////////////////////////////////////// -ScValue *SXString::scGetProperty(const char *name) { - _scValue->setNULL(); - - ////////////////////////////////////////////////////////////////////////// - // Type (RO) - ////////////////////////////////////////////////////////////////////////// - if (strcmp(name, "Type") == 0) { - _scValue->setString("string"); - return _scValue; - } - ////////////////////////////////////////////////////////////////////////// - // Length (RO) - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "Length") == 0) { - if (_gameRef->_textEncoding == TEXT_UTF8) { - WideString wstr = StringUtil::utf8ToWide(_string); - _scValue->setInt(wstr.size()); - } else { - _scValue->setInt(strlen(_string)); - } - - return _scValue; - } - ////////////////////////////////////////////////////////////////////////// - // Capacity - ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "Capacity") == 0) { - _scValue->setInt(_capacity); - return _scValue; - } else { - return _scValue; - } -} - - -////////////////////////////////////////////////////////////////////////// -bool SXString::scSetProperty(const char *name, ScValue *value) { - ////////////////////////////////////////////////////////////////////////// - // Capacity - ////////////////////////////////////////////////////////////////////////// - if (strcmp(name, "Capacity") == 0) { - int32 newCap = (uint32)value->getInt(); - if (newCap < (int32)(strlen(_string) + 1)) { - _gameRef->LOG(0, "Warning: cannot lower string capacity"); - } else if (newCap != _capacity) { - char *newStr = new char[newCap]; - if (newStr) { - memset(newStr, 0, newCap); - strcpy(newStr, _string); - delete[] _string; - _string = newStr; - _capacity = newCap; - } - } - return STATUS_OK; - } else { - return STATUS_FAILED; - } -} - - -////////////////////////////////////////////////////////////////////////// -bool SXString::persist(BasePersistenceManager *persistMgr) { - - BaseScriptable::persist(persistMgr); - - persistMgr->transfer(TMEMBER(_capacity)); - - if (persistMgr->getIsSaving()) { - if (_capacity > 0) { - persistMgr->putBytes((byte *)_string, _capacity); - } - } else { - if (_capacity > 0) { - _string = new char[_capacity]; - persistMgr->getBytes((byte *)_string, _capacity); - } else { - _string = NULL; - } - } - - return STATUS_OK; -} - - -////////////////////////////////////////////////////////////////////////// -int SXString::scCompare(BaseScriptable *val) { - return strcmp(_string, ((SXString *)val)->_string); -} - -} // end of namespace Wintermute +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +/* + * This file is based on WME Lite. + * http://dead-code.org/redir.php?target=wmelite + * Copyright (c) 2011 Jan Nedoma + */ + +#include "engines/wintermute/base/base_game.h" +#include "engines/wintermute/base/scriptables/script_stack.h" +#include "engines/wintermute/base/scriptables/script_value.h" +#include "engines/wintermute/utils/utils.h" +#include "engines/wintermute/base/scriptables/script_ext_string.h" +#include "engines/wintermute/base/scriptables/script_ext_array.h" +#include "engines/wintermute/utils/string_util.h" +#include "common/tokenizer.h" + +namespace Wintermute { + +IMPLEMENT_PERSISTENT(SXString, false) + +BaseScriptable *makeSXString(BaseGame *inGame, ScStack *stack) { + return new SXString(inGame, stack); +} + +////////////////////////////////////////////////////////////////////////// +SXString::SXString(BaseGame *inGame, ScStack *stack) : BaseScriptable(inGame) { + _string = NULL; + _capacity = 0; + + stack->correctParams(1); + ScValue *val = stack->pop(); + + if (val->isInt()) { + _capacity = MAX(0, val->getInt()); + if (_capacity > 0) { + _string = new char[_capacity]; + memset(_string, 0, _capacity); + } + } else { + setStringVal(val->getString()); + } + + if (_capacity == 0) { + setStringVal(""); + } +} + + +////////////////////////////////////////////////////////////////////////// +SXString::~SXString() { + if (_string) { + delete[] _string; + } +} + + +////////////////////////////////////////////////////////////////////////// +void SXString::setStringVal(const char *val) { + int len = strlen(val); + if (len >= _capacity) { + _capacity = len + 1; + delete[] _string; + _string = NULL; + _string = new char[_capacity]; + memset(_string, 0, _capacity); + } + strcpy(_string, val); +} + + +////////////////////////////////////////////////////////////////////////// +const char *SXString::scToString() { + if (_string) { + return _string; + } else { + return "[null string]"; + } +} + + +////////////////////////////////////////////////////////////////////////// +void SXString::scSetString(const char *val) { + setStringVal(val); +} + + +////////////////////////////////////////////////////////////////////////// +bool SXString::scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack, const char *name) { + ////////////////////////////////////////////////////////////////////////// + // Substring + ////////////////////////////////////////////////////////////////////////// + if (strcmp(name, "Substring") == 0) { + stack->correctParams(2); + int start = stack->pop()->getInt(); + int end = stack->pop()->getInt(); + + if (end < start) { + BaseUtils::swap(&start, &end); + } + + //try { + WideString str; + if (_gameRef->_textEncoding == TEXT_UTF8) { + str = StringUtil::utf8ToWide(_string); + } else { + str = StringUtil::ansiToWide(_string); + } + + //WideString subStr = str.substr(start, end - start + 1); + WideString subStr(str.c_str() + start, end - start + 1); + + if (_gameRef->_textEncoding == TEXT_UTF8) { + stack->pushString(StringUtil::wideToUtf8(subStr).c_str()); + } else { + stack->pushString(StringUtil::wideToAnsi(subStr).c_str()); + } + // } catch (std::exception &) { + // stack->pushNULL(); + // } + + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // Substr + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "Substr") == 0) { + stack->correctParams(2); + int start = stack->pop()->getInt(); + + ScValue *val = stack->pop(); + int len = val->getInt(); + + if (!val->isNULL() && len <= 0) { + stack->pushString(""); + return STATUS_OK; + } + + if (val->isNULL()) { + len = strlen(_string) - start; + } + +// try { + WideString str; + if (_gameRef->_textEncoding == TEXT_UTF8) { + str = StringUtil::utf8ToWide(_string); + } else { + str = StringUtil::ansiToWide(_string); + } + +// WideString subStr = str.substr(start, len); + WideString subStr(str.c_str() + start, len); + + if (_gameRef->_textEncoding == TEXT_UTF8) { + stack->pushString(StringUtil::wideToUtf8(subStr).c_str()); + } else { + stack->pushString(StringUtil::wideToAnsi(subStr).c_str()); + } +// } catch (std::exception &) { +// stack->pushNULL(); +// } + + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // ToUpperCase + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "ToUpperCase") == 0) { + stack->correctParams(0); + + WideString str; + if (_gameRef->_textEncoding == TEXT_UTF8) { + str = StringUtil::utf8ToWide(_string); + } else { + str = StringUtil::ansiToWide(_string); + } + + str.toUppercase(); + + if (_gameRef->_textEncoding == TEXT_UTF8) { + stack->pushString(StringUtil::wideToUtf8(str).c_str()); + } else { + stack->pushString(StringUtil::wideToAnsi(str).c_str()); + } + + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // ToLowerCase + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "ToLowerCase") == 0) { + stack->correctParams(0); + + WideString str; + if (_gameRef->_textEncoding == TEXT_UTF8) { + str = StringUtil::utf8ToWide(_string); + } else { + str = StringUtil::ansiToWide(_string); + } + + str.toLowercase(); + + if (_gameRef->_textEncoding == TEXT_UTF8) { + stack->pushString(StringUtil::wideToUtf8(str).c_str()); + } else { + stack->pushString(StringUtil::wideToAnsi(str).c_str()); + } + + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // IndexOf + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "IndexOf") == 0) { + stack->correctParams(2); + + const char *strToFind = stack->pop()->getString(); + int index = stack->pop()->getInt(); + + WideString str; + if (_gameRef->_textEncoding == TEXT_UTF8) { + str = StringUtil::utf8ToWide(_string); + } else { + str = StringUtil::ansiToWide(_string); + } + + WideString toFind; + if (_gameRef->_textEncoding == TEXT_UTF8) { + toFind = StringUtil::utf8ToWide(strToFind); + } else { + toFind = StringUtil::ansiToWide(strToFind); + } + + int indexOf = StringUtil::indexOf(str, toFind, index); + stack->pushInt(indexOf); + + return STATUS_OK; + } + + ////////////////////////////////////////////////////////////////////////// + // Split + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "Split") == 0) { + stack->correctParams(1); + ScValue *val = stack->pop(); + char separators[MAX_PATH_LENGTH] = ","; + if (!val->isNULL()) { + strcpy(separators, val->getString()); + } + + SXArray *array = new SXArray(_gameRef); + if (!array) { + stack->pushNULL(); + return STATUS_OK; + } + + + WideString str; + if (_gameRef->_textEncoding == TEXT_UTF8) { + str = StringUtil::utf8ToWide(_string); + } else { + str = StringUtil::ansiToWide(_string); + } + + WideString delims; + if (_gameRef->_textEncoding == TEXT_UTF8) { + delims = StringUtil::utf8ToWide(separators); + } else { + delims = StringUtil::ansiToWide(separators); + } + + Common::Array parts; + + + + Common::StringTokenizer tokenizer(str, delims); + while (!tokenizer.empty()) { + Common::String str2 = tokenizer.nextToken(); + parts.push_back(str2); + } + // TODO: Clean this up + /*do { + pos = StringUtil::IndexOf(Common::String(str.c_str() + start), delims, start); + //pos = str.find_first_of(delims, start); + if (pos == start) { + start = pos + 1; + } else if (pos == str.size()) { + parts.push_back(Common::String(str.c_str() + start)); + break; + } else { + parts.push_back(Common::String(str.c_str() + start, pos - start)); + start = pos + 1; + } + //start = str.find_first_not_of(delims, start); + start = StringUtil::LastIndexOf(Common::String(str.c_str() + start), delims, start) + 1; + + } while (pos != str.size());*/ + + for (Common::Array::iterator it = parts.begin(); it != parts.end(); ++it) { + WideString &part = (*it); + + if (_gameRef->_textEncoding == TEXT_UTF8) { + val = new ScValue(_gameRef, StringUtil::wideToUtf8(part).c_str()); + } else { + val = new ScValue(_gameRef, StringUtil::wideToAnsi(part).c_str()); + } + + array->push(val); + delete val; + val = NULL; + } + + stack->pushNative(array, false); + return STATUS_OK; + } else { + return STATUS_FAILED; + } +} + + +////////////////////////////////////////////////////////////////////////// +ScValue *SXString::scGetProperty(const char *name) { + _scValue->setNULL(); + + ////////////////////////////////////////////////////////////////////////// + // Type (RO) + ////////////////////////////////////////////////////////////////////////// + if (strcmp(name, "Type") == 0) { + _scValue->setString("string"); + return _scValue; + } + ////////////////////////////////////////////////////////////////////////// + // Length (RO) + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "Length") == 0) { + if (_gameRef->_textEncoding == TEXT_UTF8) { + WideString wstr = StringUtil::utf8ToWide(_string); + _scValue->setInt(wstr.size()); + } else { + _scValue->setInt(strlen(_string)); + } + + return _scValue; + } + ////////////////////////////////////////////////////////////////////////// + // Capacity + ////////////////////////////////////////////////////////////////////////// + else if (strcmp(name, "Capacity") == 0) { + _scValue->setInt(_capacity); + return _scValue; + } else { + return _scValue; + } +} + + +////////////////////////////////////////////////////////////////////////// +bool SXString::scSetProperty(const char *name, ScValue *value) { + ////////////////////////////////////////////////////////////////////////// + // Capacity + ////////////////////////////////////////////////////////////////////////// + if (strcmp(name, "Capacity") == 0) { + int32 newCap = (uint32)value->getInt(); + if (newCap < (int32)(strlen(_string) + 1)) { + _gameRef->LOG(0, "Warning: cannot lower string capacity"); + } else if (newCap != _capacity) { + char *newStr = new char[newCap]; + if (newStr) { + memset(newStr, 0, newCap); + strcpy(newStr, _string); + delete[] _string; + _string = newStr; + _capacity = newCap; + } + } + return STATUS_OK; + } else { + return STATUS_FAILED; + } +} + + +////////////////////////////////////////////////////////////////////////// +bool SXString::persist(BasePersistenceManager *persistMgr) { + + BaseScriptable::persist(persistMgr); + + persistMgr->transfer(TMEMBER(_capacity)); + + if (persistMgr->getIsSaving()) { + if (_capacity > 0) { + persistMgr->putBytes((byte *)_string, _capacity); + } + } else { + if (_capacity > 0) { + _string = new char[_capacity]; + persistMgr->getBytes((byte *)_string, _capacity); + } else { + _string = NULL; + } + } + + return STATUS_OK; +} + + +////////////////////////////////////////////////////////////////////////// +int SXString::scCompare(BaseScriptable *val) { + return strcmp(_string, ((SXString *)val)->_string); +} + +} // end of namespace Wintermute diff --git a/engines/wintermute/base/scriptables/script_ext_string.h b/engines/wintermute/base/scriptables/script_ext_string.h index c2de3860ed..255b9c57eb 100644 --- a/engines/wintermute/base/scriptables/script_ext_string.h +++ b/engines/wintermute/base/scriptables/script_ext_string.h @@ -1,58 +1,58 @@ -/* ScummVM - Graphic Adventure Engine - * - * ScummVM is the legal property of its developers, whose names - * are too numerous to list here. Please refer to the COPYRIGHT - * file distributed with this source distribution. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - */ - -/* - * This file is based on WME Lite. - * http://dead-code.org/redir.php?target=wmelite - * Copyright (c) 2011 Jan Nedoma - */ - -#ifndef WINTERMUTE_SXSTRING_H -#define WINTERMUTE_SXSTRING_H - - -#include "engines/wintermute/base/base_scriptable.h" - -namespace Wintermute { - -class SXString : public BaseScriptable { -public: - virtual int scCompare(BaseScriptable *Val); - DECLARE_PERSISTENT(SXString, BaseScriptable) - ScValue *scGetProperty(const char *name); - bool scSetProperty(const char *name, ScValue *value); - bool scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack, const char *name); - void scSetString(const char *val); - const char *scToString(); - void setStringVal(const char *val); - - SXString(BaseGame *inGame, ScStack *Stack); - virtual ~SXString(); - -private: - char *_string; - int _capacity; -}; - -} // end of namespace Wintermute - -#endif +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +/* + * This file is based on WME Lite. + * http://dead-code.org/redir.php?target=wmelite + * Copyright (c) 2011 Jan Nedoma + */ + +#ifndef WINTERMUTE_SXSTRING_H +#define WINTERMUTE_SXSTRING_H + + +#include "engines/wintermute/base/base_scriptable.h" + +namespace Wintermute { + +class SXString : public BaseScriptable { +public: + virtual int scCompare(BaseScriptable *Val); + DECLARE_PERSISTENT(SXString, BaseScriptable) + ScValue *scGetProperty(const char *name); + bool scSetProperty(const char *name, ScValue *value); + bool scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack, const char *name); + void scSetString(const char *val); + const char *scToString(); + void setStringVal(const char *val); + + SXString(BaseGame *inGame, ScStack *Stack); + virtual ~SXString(); + +private: + char *_string; + int _capacity; +}; + +} // end of namespace Wintermute + +#endif diff --git a/engines/wintermute/base/scriptables/script_stack.cpp b/engines/wintermute/base/scriptables/script_stack.cpp index d27748abe6..77367045c2 100644 --- a/engines/wintermute/base/scriptables/script_stack.cpp +++ b/engines/wintermute/base/scriptables/script_stack.cpp @@ -1,232 +1,232 @@ -/* ScummVM - Graphic Adventure Engine - * - * ScummVM is the legal property of its developers, whose names - * are too numerous to list here. Please refer to the COPYRIGHT - * file distributed with this source distribution. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - */ - -/* - * This file is based on WME Lite. - * http://dead-code.org/redir.php?target=wmelite - * Copyright (c) 2011 Jan Nedoma - */ - -#include "engines/wintermute/base/scriptables/script_stack.h" -#include "engines/wintermute/base/scriptables/script_value.h" -#include "engines/wintermute/base/base_game.h" - -namespace Wintermute { - -IMPLEMENT_PERSISTENT(ScStack, false) - -////////////////////////////////////////////////////////////////////////// -ScStack::ScStack(BaseGame *inGame) : BaseClass(inGame) { - _sP = -1; -} - - -////////////////////////////////////////////////////////////////////////// -ScStack::~ScStack() { - -#if _DEBUG - //_gameRef->LOG(0, "STAT: Stack size: %d, SP=%d", _values.size(), _sP); -#endif - - for (uint32 i = 0; i < _values.size(); i++) { - delete _values[i]; - } - _values.clear(); -} - - -////////////////////////////////////////////////////////////////////////// -ScValue *ScStack::pop() { - if (_sP < 0) { - _gameRef->LOG(0, "Fatal: Stack underflow"); - return NULL; - } - - return _values[_sP--]; -} - - -////////////////////////////////////////////////////////////////////////// -void ScStack::push(ScValue *val) { - _sP++; - - if (_sP < (int32)_values.size()) { - _values[_sP]->cleanup(); - _values[_sP]->copy(val); - } else { - ScValue *copyVal = new ScValue(_gameRef); - copyVal->copy(val); - _values.add(copyVal); - } -} - - -////////////////////////////////////////////////////////////////////////// -ScValue *ScStack::getPushValue() { - _sP++; - - if (_sP >= (int32)_values.size()) { - ScValue *val = new ScValue(_gameRef); - _values.add(val); - } - _values[_sP]->cleanup(); - return _values[_sP]; -} - - - -////////////////////////////////////////////////////////////////////////// -ScValue *ScStack::getTop() { - if (_sP < 0 || _sP >= (int32)_values.size()) { - return NULL; - } else { - return _values[_sP]; - } -} - - -////////////////////////////////////////////////////////////////////////// -ScValue *ScStack::getAt(int index) { - index = _sP - index; - if (index < 0 || index >= (int32)_values.size()) { - return NULL; - } else { - return _values[index]; - } -} - - -////////////////////////////////////////////////////////////////////////// -void ScStack::correctParams(uint32 expectedParams) { - uint32 nuParams = (uint32)pop()->getInt(); - - if (expectedParams < nuParams) { // too many params - while (expectedParams < nuParams) { - //Pop(); - delete _values[_sP - expectedParams]; - _values.remove_at(_sP - expectedParams); - nuParams--; - _sP--; - } - } else if (expectedParams > nuParams) { // need more params - while (expectedParams > nuParams) { - //Push(null_val); - ScValue *nullVal = new ScValue(_gameRef); - nullVal->setNULL(); - _values.insert_at(_sP - nuParams + 1, nullVal); - nuParams++; - _sP++; - - if ((int32)_values.size() > _sP + 1) { - delete _values[_values.size() - 1]; - _values.remove_at(_values.size() - 1); - } - } - } -} - - -////////////////////////////////////////////////////////////////////////// -void ScStack::pushNULL() { - /* - ScValue* val = new ScValue(_gameRef); - val->setNULL(); - Push(val); - delete val; - */ - getPushValue()->setNULL(); -} - - -////////////////////////////////////////////////////////////////////////// -void ScStack::pushInt(int val) { - /* - ScValue* val = new ScValue(_gameRef); - val->setInt(Val); - Push(val); - delete val; - */ - getPushValue()->setInt(val); -} - - -////////////////////////////////////////////////////////////////////////// -void ScStack::pushFloat(double val) { - /* - ScValue* val = new ScValue(_gameRef); - val->setFloat(Val); - Push(val); - delete val; - */ - getPushValue()->setFloat(val); -} - - -////////////////////////////////////////////////////////////////////////// -void ScStack::pushBool(bool val) { - /* - ScValue* val = new ScValue(_gameRef); - val->setBool(Val); - Push(val); - delete val; - */ - getPushValue()->setBool(val); -} - - -////////////////////////////////////////////////////////////////////////// -void ScStack::pushString(const char *val) { - /* - ScValue* val = new ScValue(_gameRef); - val->setString(Val); - Push(val); - delete val; - */ - getPushValue()->setString(val); -} - - -////////////////////////////////////////////////////////////////////////// -void ScStack::pushNative(BaseScriptable *val, bool persistent) { - /* - ScValue* val = new ScValue(_gameRef); - val->setNative(Val, Persistent); - Push(val); - delete val; - */ - - getPushValue()->setNative(val, persistent); -} - - -////////////////////////////////////////////////////////////////////////// -bool ScStack::persist(BasePersistenceManager *persistMgr) { - - persistMgr->transfer(TMEMBER(_gameRef)); - - persistMgr->transfer(TMEMBER(_sP)); - _values.persist(persistMgr); - - return STATUS_OK; -} - -} // end of namespace Wintermute +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +/* + * This file is based on WME Lite. + * http://dead-code.org/redir.php?target=wmelite + * Copyright (c) 2011 Jan Nedoma + */ + +#include "engines/wintermute/base/scriptables/script_stack.h" +#include "engines/wintermute/base/scriptables/script_value.h" +#include "engines/wintermute/base/base_game.h" + +namespace Wintermute { + +IMPLEMENT_PERSISTENT(ScStack, false) + +////////////////////////////////////////////////////////////////////////// +ScStack::ScStack(BaseGame *inGame) : BaseClass(inGame) { + _sP = -1; +} + + +////////////////////////////////////////////////////////////////////////// +ScStack::~ScStack() { + +#if _DEBUG + //_gameRef->LOG(0, "STAT: Stack size: %d, SP=%d", _values.size(), _sP); +#endif + + for (uint32 i = 0; i < _values.size(); i++) { + delete _values[i]; + } + _values.clear(); +} + + +////////////////////////////////////////////////////////////////////////// +ScValue *ScStack::pop() { + if (_sP < 0) { + _gameRef->LOG(0, "Fatal: Stack underflow"); + return NULL; + } + + return _values[_sP--]; +} + + +////////////////////////////////////////////////////////////////////////// +void ScStack::push(ScValue *val) { + _sP++; + + if (_sP < (int32)_values.size()) { + _values[_sP]->cleanup(); + _values[_sP]->copy(val); + } else { + ScValue *copyVal = new ScValue(_gameRef); + copyVal->copy(val); + _values.add(copyVal); + } +} + + +////////////////////////////////////////////////////////////////////////// +ScValue *ScStack::getPushValue() { + _sP++; + + if (_sP >= (int32)_values.size()) { + ScValue *val = new ScValue(_gameRef); + _values.add(val); + } + _values[_sP]->cleanup(); + return _values[_sP]; +} + + + +////////////////////////////////////////////////////////////////////////// +ScValue *ScStack::getTop() { + if (_sP < 0 || _sP >= (int32)_values.size()) { + return NULL; + } else { + return _values[_sP]; + } +} + + +////////////////////////////////////////////////////////////////////////// +ScValue *ScStack::getAt(int index) { + index = _sP - index; + if (index < 0 || index >= (int32)_values.size()) { + return NULL; + } else { + return _values[index]; + } +} + + +////////////////////////////////////////////////////////////////////////// +void ScStack::correctParams(uint32 expectedParams) { + uint32 nuParams = (uint32)pop()->getInt(); + + if (expectedParams < nuParams) { // too many params + while (expectedParams < nuParams) { + //Pop(); + delete _values[_sP - expectedParams]; + _values.remove_at(_sP - expectedParams); + nuParams--; + _sP--; + } + } else if (expectedParams > nuParams) { // need more params + while (expectedParams > nuParams) { + //Push(null_val); + ScValue *nullVal = new ScValue(_gameRef); + nullVal->setNULL(); + _values.insert_at(_sP - nuParams + 1, nullVal); + nuParams++; + _sP++; + + if ((int32)_values.size() > _sP + 1) { + delete _values[_values.size() - 1]; + _values.remove_at(_values.size() - 1); + } + } + } +} + + +////////////////////////////////////////////////////////////////////////// +void ScStack::pushNULL() { + /* + ScValue* val = new ScValue(_gameRef); + val->setNULL(); + Push(val); + delete val; + */ + getPushValue()->setNULL(); +} + + +////////////////////////////////////////////////////////////////////////// +void ScStack::pushInt(int val) { + /* + ScValue* val = new ScValue(_gameRef); + val->setInt(Val); + Push(val); + delete val; + */ + getPushValue()->setInt(val); +} + + +////////////////////////////////////////////////////////////////////////// +void ScStack::pushFloat(double val) { + /* + ScValue* val = new ScValue(_gameRef); + val->setFloat(Val); + Push(val); + delete val; + */ + getPushValue()->setFloat(val); +} + + +////////////////////////////////////////////////////////////////////////// +void ScStack::pushBool(bool val) { + /* + ScValue* val = new ScValue(_gameRef); + val->setBool(Val); + Push(val); + delete val; + */ + getPushValue()->setBool(val); +} + + +////////////////////////////////////////////////////////////////////////// +void ScStack::pushString(const char *val) { + /* + ScValue* val = new ScValue(_gameRef); + val->setString(Val); + Push(val); + delete val; + */ + getPushValue()->setString(val); +} + + +////////////////////////////////////////////////////////////////////////// +void ScStack::pushNative(BaseScriptable *val, bool persistent) { + /* + ScValue* val = new ScValue(_gameRef); + val->setNative(Val, Persistent); + Push(val); + delete val; + */ + + getPushValue()->setNative(val, persistent); +} + + +////////////////////////////////////////////////////////////////////////// +bool ScStack::persist(BasePersistenceManager *persistMgr) { + + persistMgr->transfer(TMEMBER(_gameRef)); + + persistMgr->transfer(TMEMBER(_sP)); + _values.persist(persistMgr); + + return STATUS_OK; +} + +} // end of namespace Wintermute diff --git a/engines/wintermute/base/scriptables/script_stack.h b/engines/wintermute/base/scriptables/script_stack.h index 0e2adae518..86d246cf34 100644 --- a/engines/wintermute/base/scriptables/script_stack.h +++ b/engines/wintermute/base/scriptables/script_stack.h @@ -1,66 +1,66 @@ -/* ScummVM - Graphic Adventure Engine - * - * ScummVM is the legal property of its developers, whose names - * are too numerous to list here. Please refer to the COPYRIGHT - * file distributed with this source distribution. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - */ - -/* - * This file is based on WME Lite. - * http://dead-code.org/redir.php?target=wmelite - * Copyright (c) 2011 Jan Nedoma - */ - -#ifndef WINTERMUTE_SCSTACK_H -#define WINTERMUTE_SCSTACK_H - - -#include "engines/wintermute/base/base.h" -#include "engines/wintermute/coll_templ.h" -#include "engines/wintermute/persistent.h" - -namespace Wintermute { - -class ScValue; -class BaseScriptable; - -class ScStack : public BaseClass { -public: - ScValue *getAt(int Index); - ScValue *getPushValue(); - DECLARE_PERSISTENT(ScStack, BaseClass) - void pushNative(BaseScriptable *val, bool persistent); - void pushString(const char *val); - void pushBool(bool val); - void pushInt(int val); - void pushFloat(double val); - void pushNULL(); - void correctParams(uint32 expectedParams); - ScValue *getTop(); - void push(ScValue *val); - ScValue *pop(); - ScStack(BaseGame *inGame); - virtual ~ScStack(); - BaseArray _values; - int _sP; - -}; - -} // end of namespace Wintermute - -#endif +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +/* + * This file is based on WME Lite. + * http://dead-code.org/redir.php?target=wmelite + * Copyright (c) 2011 Jan Nedoma + */ + +#ifndef WINTERMUTE_SCSTACK_H +#define WINTERMUTE_SCSTACK_H + + +#include "engines/wintermute/base/base.h" +#include "engines/wintermute/coll_templ.h" +#include "engines/wintermute/persistent.h" + +namespace Wintermute { + +class ScValue; +class BaseScriptable; + +class ScStack : public BaseClass { +public: + ScValue *getAt(int Index); + ScValue *getPushValue(); + DECLARE_PERSISTENT(ScStack, BaseClass) + void pushNative(BaseScriptable *val, bool persistent); + void pushString(const char *val); + void pushBool(bool val); + void pushInt(int val); + void pushFloat(double val); + void pushNULL(); + void correctParams(uint32 expectedParams); + ScValue *getTop(); + void push(ScValue *val); + ScValue *pop(); + ScStack(BaseGame *inGame); + virtual ~ScStack(); + BaseArray _values; + int _sP; + +}; + +} // end of namespace Wintermute + +#endif diff --git a/engines/wintermute/base/scriptables/script_value.cpp b/engines/wintermute/base/scriptables/script_value.cpp index 03ca69ac7e..0bc7ab5807 100644 --- a/engines/wintermute/base/scriptables/script_value.cpp +++ b/engines/wintermute/base/scriptables/script_value.cpp @@ -1,995 +1,995 @@ -/* ScummVM - Graphic Adventure Engine - * - * ScummVM is the legal property of its developers, whose names - * are too numerous to list here. Please refer to the COPYRIGHT - * file distributed with this source distribution. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - */ - -/* - * This file is based on WME Lite. - * http://dead-code.org/redir.php?target=wmelite - * Copyright (c) 2011 Jan Nedoma - */ - -#include "engines/wintermute/platform_osystem.h" -#include "engines/wintermute/base/base_dynamic_buffer.h" -#include "engines/wintermute/base/base_game.h" -#include "engines/wintermute/base/scriptables/script_value.h" -#include "engines/wintermute/base/scriptables/script.h" -#include "engines/wintermute/utils/string_util.h" -#include "engines/wintermute/base/base_scriptable.h" - -namespace Wintermute { - -////////////////////////////////////////////////////////////////////// -// Construction/Destruction -////////////////////////////////////////////////////////////////////// - -IMPLEMENT_PERSISTENT(ScValue, false) - -////////////////////////////////////////////////////////////////////////// -ScValue::ScValue(BaseGame *inGame) : BaseClass(inGame) { - _type = VAL_NULL; - - _valBool = false; - _valInt = 0; - _valFloat = 0.0f; - _valNative = NULL; - _valString = NULL; - _valRef = NULL; - _persistent = false; - _isConstVar = false; -} - - -////////////////////////////////////////////////////////////////////////// -ScValue::ScValue(BaseGame *inGame, bool val) : BaseClass(inGame) { - _type = VAL_BOOL; - _valBool = val; - - _valInt = 0; - _valFloat = 0.0f; - _valNative = NULL; - _valString = NULL; - _valRef = NULL; - _persistent = false; - _isConstVar = false; -} - - -////////////////////////////////////////////////////////////////////////// -ScValue::ScValue(BaseGame *inGame, int val) : BaseClass(inGame) { - _type = VAL_INT; - _valInt = val; - - _valFloat = 0.0f; - _valBool = false; - _valNative = NULL; - _valString = NULL; - _valRef = NULL; - _persistent = false; - _isConstVar = false; -} - - -////////////////////////////////////////////////////////////////////////// -ScValue::ScValue(BaseGame *inGame, double val) : BaseClass(inGame) { - _type = VAL_FLOAT; - _valFloat = val; - - _valInt = 0; - _valBool = false; - _valNative = NULL; - _valString = NULL; - _valRef = NULL; - _persistent = false; - _isConstVar = false; -} - - -////////////////////////////////////////////////////////////////////////// -ScValue::ScValue(BaseGame *inGame, const char *val) : BaseClass(inGame) { - _type = VAL_STRING; - _valString = NULL; - setStringVal(val); - - _valBool = false; - _valInt = 0; - _valFloat = 0.0f; - _valNative = NULL; - _valRef = NULL; - _persistent = false; - _isConstVar = false; -} - - -////////////////////////////////////////////////////////////////////////// -void ScValue::cleanup(bool ignoreNatives) { - deleteProps(); - - if (_valString) { - delete[] _valString; - } - - if (!ignoreNatives) { - if (_valNative && !_persistent) { - _valNative->_refCount--; - if (_valNative->_refCount <= 0) { - delete _valNative; - _valNative = NULL; - } - } - } - - - _type = VAL_NULL; - - _valBool = false; - _valInt = 0; - _valFloat = 0.0f; - _valNative = NULL; - _valString = NULL; - _valRef = NULL; - _persistent = false; - _isConstVar = false; -} - - - -////////////////////////////////////////////////////////////////////////// -ScValue::~ScValue() { - cleanup(); -} - - -////////////////////////////////////////////////////////////////////////// -ScValue *ScValue::getProp(const char *name) { - if (_type == VAL_VARIABLE_REF) { - return _valRef->getProp(name); - } - - if (_type == VAL_STRING && strcmp(name, "Length") == 0) { - _gameRef->_scValue->_type = VAL_INT; - - if (_gameRef->_textEncoding == TEXT_ANSI) { - _gameRef->_scValue->setInt(strlen(_valString)); - } else { - WideString wstr = StringUtil::utf8ToWide(_valString); - _gameRef->_scValue->setInt(wstr.size()); - } - - return _gameRef->_scValue; - } - - ScValue *ret = NULL; - - if (_type == VAL_NATIVE && _valNative) { - ret = _valNative->scGetProperty(name); - } - - if (ret == NULL) { - _valIter = _valObject.find(name); - if (_valIter != _valObject.end()) { - ret = _valIter->_value; - } - } - return ret; -} - -////////////////////////////////////////////////////////////////////////// -bool ScValue::deleteProp(const char *name) { - if (_type == VAL_VARIABLE_REF) { - return _valRef->deleteProp(name); - } - - _valIter = _valObject.find(name); - if (_valIter != _valObject.end()) { - delete _valIter->_value; - _valIter->_value = NULL; - } - - return STATUS_OK; -} - - - -////////////////////////////////////////////////////////////////////////// -bool ScValue::setProp(const char *name, ScValue *val, bool copyWhole, bool setAsConst) { - if (_type == VAL_VARIABLE_REF) { - return _valRef->setProp(name, val); - } - - bool ret = STATUS_FAILED; - if (_type == VAL_NATIVE && _valNative) { - ret = _valNative->scSetProperty(name, val); - } - - if (DID_FAIL(ret)) { - ScValue *newVal = NULL; - - _valIter = _valObject.find(name); - if (_valIter != _valObject.end()) { - newVal = _valIter->_value; - } - if (!newVal) { - newVal = new ScValue(_gameRef); - } else { - newVal->cleanup(); - } - - newVal->copy(val, copyWhole); - newVal->_isConstVar = setAsConst; - _valObject[name] = newVal; - - if (_type != VAL_NATIVE) { - _type = VAL_OBJECT; - } - - /* - _valIter = _valObject.find(Name); - if (_valIter != _valObject.end()){ - delete _valIter->_value; - _valIter->_value = NULL; - } - ScValue* val = new ScValue(_gameRef); - val->Copy(Val, CopyWhole); - val->_isConstVar = SetAsConst; - _valObject[Name] = val; - - if (_type!=VAL_NATIVE) _type = VAL_OBJECT; - */ - } - - return STATUS_OK; -} - - -////////////////////////////////////////////////////////////////////////// -bool ScValue::propExists(const char *name) { - if (_type == VAL_VARIABLE_REF) { - return _valRef->propExists(name); - } - _valIter = _valObject.find(name); - - return (_valIter != _valObject.end()); -} - - -////////////////////////////////////////////////////////////////////////// -void ScValue::deleteProps() { - _valIter = _valObject.begin(); - while (_valIter != _valObject.end()) { - delete(ScValue *)_valIter->_value; - _valIter++; - } - _valObject.clear(); -} - - -////////////////////////////////////////////////////////////////////////// -void ScValue::CleanProps(bool includingNatives) { - _valIter = _valObject.begin(); - while (_valIter != _valObject.end()) { - if (!_valIter->_value->_isConstVar && (!_valIter->_value->isNative() || includingNatives)) { - _valIter->_value->setNULL(); - } - _valIter++; - } -} - -////////////////////////////////////////////////////////////////////////// -bool ScValue::isNULL() { - if (_type == VAL_VARIABLE_REF) { - return _valRef->isNULL(); - } - - return (_type == VAL_NULL); -} - - -////////////////////////////////////////////////////////////////////////// -bool ScValue::isNative() { - if (_type == VAL_VARIABLE_REF) { - return _valRef->isNative(); - } - - return (_type == VAL_NATIVE); -} - - -////////////////////////////////////////////////////////////////////////// -bool ScValue::isString() { - if (_type == VAL_VARIABLE_REF) { - return _valRef->isString(); - } - - return (_type == VAL_STRING); -} - - -////////////////////////////////////////////////////////////////////////// -bool ScValue::isFloat() { - if (_type == VAL_VARIABLE_REF) { - return _valRef->isFloat(); - } - - return (_type == VAL_FLOAT); -} - - -////////////////////////////////////////////////////////////////////////// -bool ScValue::isInt() { - if (_type == VAL_VARIABLE_REF) { - return _valRef->isInt(); - } - - return (_type == VAL_INT); -} - - -////////////////////////////////////////////////////////////////////////// -bool ScValue::isBool() { - if (_type == VAL_VARIABLE_REF) { - return _valRef->isBool(); - } - - return (_type == VAL_BOOL); -} - - -////////////////////////////////////////////////////////////////////////// -bool ScValue::isObject() { - if (_type == VAL_VARIABLE_REF) { - return _valRef->isObject(); - } - - return (_type == VAL_OBJECT); -} - - -////////////////////////////////////////////////////////////////////////// -TValType ScValue::getTypeTolerant() { - if (_type == VAL_VARIABLE_REF) { - return _valRef->getType(); - } - - return _type; -} - - -////////////////////////////////////////////////////////////////////////// -void ScValue::setBool(bool val) { - if (_type == VAL_VARIABLE_REF) { - _valRef->setBool(val); - return; - } - - if (_type == VAL_NATIVE) { - _valNative->scSetBool(val); - return; - } - - _valBool = val; - _type = VAL_BOOL; -} - - -////////////////////////////////////////////////////////////////////////// -void ScValue::setInt(int val) { - if (_type == VAL_VARIABLE_REF) { - _valRef->setInt(val); - return; - } - - if (_type == VAL_NATIVE) { - _valNative->scSetInt(val); - return; - } - - _valInt = val; - _type = VAL_INT; -} - - -////////////////////////////////////////////////////////////////////////// -void ScValue::setFloat(double val) { - if (_type == VAL_VARIABLE_REF) { - _valRef->setFloat(val); - return; - } - - if (_type == VAL_NATIVE) { - _valNative->scSetFloat(val); - return; - } - - _valFloat = val; - _type = VAL_FLOAT; -} - - -////////////////////////////////////////////////////////////////////////// -void ScValue::setString(const char *val) { - if (_type == VAL_VARIABLE_REF) { - _valRef->setString(val); - return; - } - - if (_type == VAL_NATIVE) { - _valNative->scSetString(val); - return; - } - - setStringVal(val); - if (_valString) { - _type = VAL_STRING; - } else { - _type = VAL_NULL; - } -} - -void ScValue::setString(const Common::String &val) { - setString(val.c_str()); -} - -////////////////////////////////////////////////////////////////////////// -void ScValue::setStringVal(const char *val) { - if (_valString) { - delete[] _valString; - _valString = NULL; - } - - if (val == NULL) { - _valString = NULL; - return; - } - - _valString = new char [strlen(val) + 1]; - if (_valString) { - strcpy(_valString, val); - } -} - - -////////////////////////////////////////////////////////////////////////// -void ScValue::setNULL() { - if (_type == VAL_VARIABLE_REF) { - _valRef->setNULL(); - return; - } - - if (_valNative && !_persistent) { - _valNative->_refCount--; - if (_valNative->_refCount <= 0) { - delete _valNative; - } - } - _valNative = NULL; - deleteProps(); - - _type = VAL_NULL; -} - - -////////////////////////////////////////////////////////////////////////// -void ScValue::setNative(BaseScriptable *val, bool persistent) { - if (_type == VAL_VARIABLE_REF) { - _valRef->setNative(val, persistent); - return; - } - - if (val == NULL) { - setNULL(); - } else { - if (_valNative && !_persistent) { - _valNative->_refCount--; - if (_valNative->_refCount <= 0) { - if (_valNative != val) { - delete _valNative; - } - _valNative = NULL; - } - } - - _type = VAL_NATIVE; - _persistent = persistent; - - _valNative = val; - if (_valNative && !_persistent) { - _valNative->_refCount++; - } - } -} - - -////////////////////////////////////////////////////////////////////////// -void ScValue::setObject() { - if (_type == VAL_VARIABLE_REF) { - _valRef->setObject(); - return; - } - - deleteProps(); - _type = VAL_OBJECT; -} - - -////////////////////////////////////////////////////////////////////////// -void ScValue::setReference(ScValue *val) { - _valRef = val; - _type = VAL_VARIABLE_REF; -} - - -////////////////////////////////////////////////////////////////////////// -bool ScValue::getBool(bool defaultVal) { - if (_type == VAL_VARIABLE_REF) { - return _valRef->getBool(); - } - - switch (_type) { - case VAL_BOOL: - return _valBool; - - case VAL_NATIVE: - return _valNative->scToBool(); - - case VAL_INT: - return (_valInt != 0); - - case VAL_FLOAT: - return (_valFloat != 0.0f); - - case VAL_STRING: - return (scumm_stricmp(_valString, "1") == 0 || scumm_stricmp(_valString, "yes") == 0 || scumm_stricmp(_valString, "true") == 0); - - default: - return defaultVal; - } -} - - -////////////////////////////////////////////////////////////////////////// -int ScValue::getInt(int defaultVal) { - if (_type == VAL_VARIABLE_REF) { - return _valRef->getInt(); - } - - switch (_type) { - case VAL_BOOL: - return _valBool ? 1 : 0; - - case VAL_NATIVE: - return _valNative->scToInt(); - - case VAL_INT: - return _valInt; - - case VAL_FLOAT: - return (int)_valFloat; - - case VAL_STRING: - return atoi(_valString); - - default: - return defaultVal; - } -} - - -////////////////////////////////////////////////////////////////////////// -double ScValue::getFloat(double defaultVal) { - if (_type == VAL_VARIABLE_REF) { - return _valRef->getFloat(); - } - - switch (_type) { - case VAL_BOOL: - return _valBool ? 1.0f : 0.0f; - - case VAL_NATIVE: - return _valNative->scToFloat(); - - case VAL_INT: - return (double)_valInt; - - case VAL_FLOAT: - return _valFloat; - - case VAL_STRING: - return atof(_valString); - - default: - return defaultVal; - } -} - -////////////////////////////////////////////////////////////////////////// -void *ScValue::getMemBuffer() { - if (_type == VAL_VARIABLE_REF) { - return _valRef->getMemBuffer(); - } - - if (_type == VAL_NATIVE) { - return _valNative->scToMemBuffer(); - } else { - return (void *)NULL; - } -} - - -////////////////////////////////////////////////////////////////////////// -const char *ScValue::getString() { - if (_type == VAL_VARIABLE_REF) { - return _valRef->getString(); - } - - switch (_type) { - case VAL_OBJECT: - setStringVal("[object]"); - break; - - case VAL_NULL: - setStringVal("[null]"); - break; - - case VAL_NATIVE: { - const char *strVal = _valNative->scToString(); - setStringVal(strVal); - return strVal; - break; - } - - case VAL_BOOL: - setStringVal(_valBool ? "yes" : "no"); - break; - - case VAL_INT: { - char dummy[50]; - sprintf(dummy, "%d", _valInt); - setStringVal(dummy); - break; - } - - case VAL_FLOAT: { - char dummy[50]; - sprintf(dummy, "%f", _valFloat); - setStringVal(dummy); - break; - } - - case VAL_STRING: - break; - - default: - setStringVal(""); - } - - return _valString; -} - - -////////////////////////////////////////////////////////////////////////// -BaseScriptable *ScValue::getNative() { - if (_type == VAL_VARIABLE_REF) { - return _valRef->getNative(); - } - - if (_type == VAL_NATIVE) { - return _valNative; - } else { - return NULL; - } -} - - -////////////////////////////////////////////////////////////////////////// -TValType ScValue::getType() { - return _type; -} - - -////////////////////////////////////////////////////////////////////////// -void ScValue::copy(ScValue *orig, bool copyWhole) { - _gameRef = orig->_gameRef; - - if (_valNative && !_persistent) { - _valNative->_refCount--; - if (_valNative->_refCount <= 0) { - if (_valNative != orig->_valNative) { - delete _valNative; - } - _valNative = NULL; - } - } - - if (orig->_type == VAL_VARIABLE_REF && orig->_valRef && copyWhole) { - orig = orig->_valRef; - } - - cleanup(true); - - _type = orig->_type; - _valBool = orig->_valBool; - _valInt = orig->_valInt; - _valFloat = orig->_valFloat; - setStringVal(orig->_valString); - - _valRef = orig->_valRef; - _persistent = orig->_persistent; - - _valNative = orig->_valNative; - if (_valNative && !_persistent) { - _valNative->_refCount++; - } -//!!!! ref->native++ - - // copy properties - if (orig->_type == VAL_OBJECT && orig->_valObject.size() > 0) { - orig->_valIter = orig->_valObject.begin(); - while (orig->_valIter != orig->_valObject.end()) { - _valObject[orig->_valIter->_key] = new ScValue(_gameRef); - _valObject[orig->_valIter->_key]->copy(orig->_valIter->_value); - orig->_valIter++; - } - } else { - _valObject.clear(); - } -} - - -////////////////////////////////////////////////////////////////////////// -void ScValue::setValue(ScValue *val) { - if (val->_type == VAL_VARIABLE_REF) { - setValue(val->_valRef); - return; - } - - // if being assigned a simple type, preserve native state - if (_type == VAL_NATIVE && (val->_type == VAL_INT || val->_type == VAL_STRING || val->_type == VAL_BOOL)) { - switch (val->_type) { - case VAL_INT: - _valNative->scSetInt(val->getInt()); - break; - case VAL_FLOAT: - _valNative->scSetFloat(val->getFloat()); - break; - case VAL_BOOL: - _valNative->scSetBool(val->getBool()); - break; - case VAL_STRING: - _valNative->scSetString(val->getString()); - break; - default: - warning("ScValue::setValue - unhandled enum"); - break; - } - } - // otherwise just copy everything - else { - copy(val); - } -} - - -////////////////////////////////////////////////////////////////////////// -bool ScValue::persist(BasePersistenceManager *persistMgr) { - persistMgr->transfer(TMEMBER(_gameRef)); - - persistMgr->transfer(TMEMBER(_persistent)); - persistMgr->transfer(TMEMBER(_isConstVar)); - persistMgr->transfer(TMEMBER_INT(_type)); - persistMgr->transfer(TMEMBER(_valBool)); - persistMgr->transfer(TMEMBER(_valFloat)); - persistMgr->transfer(TMEMBER(_valInt)); - persistMgr->transfer(TMEMBER(_valNative)); - - int size; - const char *str; - if (persistMgr->getIsSaving()) { - size = _valObject.size(); - persistMgr->transfer("", &size); - _valIter = _valObject.begin(); - while (_valIter != _valObject.end()) { - str = _valIter->_key.c_str(); - persistMgr->transfer("", &str); - persistMgr->transfer("", &_valIter->_value); - - _valIter++; - } - } else { - ScValue *val; - persistMgr->transfer("", &size); - for (int i = 0; i < size; i++) { - persistMgr->transfer("", &str); - persistMgr->transfer("", &val); - - _valObject[str] = val; - delete[] str; - } - } - - persistMgr->transfer(TMEMBER(_valRef)); - persistMgr->transfer(TMEMBER(_valString)); - - /* - FILE* f = fopen("c:\\val.log", "a+"); - switch(_type) - { - case VAL_STRING: - fprintf(f, "str %s\n", _valString); - break; - - case VAL_INT: - fprintf(f, "int %d\n", _valInt); - break; - - case VAL_BOOL: - fprintf(f, "bool %d\n", _valBool); - break; - - case VAL_NULL: - fprintf(f, "null\n"); - break; - - case VAL_NATIVE: - fprintf(f, "native\n"); - break; - - case VAL_VARIABLE_REF: - fprintf(f, "ref\n"); - break; - - case VAL_OBJECT: - fprintf(f, "obj\n"); - break; - - case VAL_FLOAT: - fprintf(f, "float\n"); - break; - - } - fclose(f); - */ - - return STATUS_OK; -} - - -////////////////////////////////////////////////////////////////////////// -bool ScValue::saveAsText(BaseDynamicBuffer *buffer, int indent) { - _valIter = _valObject.begin(); - while (_valIter != _valObject.end()) { - buffer->putTextIndent(indent, "PROPERTY {\n"); - buffer->putTextIndent(indent + 2, "NAME=\"%s\"\n", _valIter->_key.c_str()); - buffer->putTextIndent(indent + 2, "VALUE=\"%s\"\n", _valIter->_value->getString()); - buffer->putTextIndent(indent, "}\n\n"); - - _valIter++; - } - return STATUS_OK; -} - - -////////////////////////////////////////////////////////////////////////// -// -1 ... left is less, 0 ... equals, 1 ... left is greater -int ScValue::compare(ScValue *val1, ScValue *val2) { - // both natives? - if (val1->isNative() && val2->isNative()) { - // same class? - if (strcmp(val1->getNative()->getClassName(), val2->getNative()->getClassName()) == 0) { - return val1->getNative()->scCompare(val2->getNative()); - } else { - return strcmp(val1->getString(), val2->getString()); - } - } - - // both objects? - if (val1->isObject() && val2->isObject()) { - return -1; - } - - - // null states - if (val1->isNULL() && !val2->isNULL()) { - return -1; - } else if (!val1->isNULL() && val2->isNULL()) { - return 1; - } else if (val1->isNULL() && val2->isNULL()) { - return 0; - } - - // one of them is string? convert both to string - if (val1->isString() || val2->isString()) { - return strcmp(val1->getString(), val2->getString()); - } - - // one of them is float? - if (val1->isFloat() || val2->isFloat()) { - if (val1->getFloat() < val2->getFloat()) { - return -1; - } else if (val1->getFloat() > val2->getFloat()) { - return 1; - } else { - return 0; - } - } - - // otherwise compare as int's - if (val1->getInt() < val2->getInt()) { - return -1; - } else if (val1->getInt() > val2->getInt()) { - return 1; - } else { - return 0; - } -} - - -////////////////////////////////////////////////////////////////////////// -int ScValue::compareStrict(ScValue *val1, ScValue *val2) { - if (val1->getTypeTolerant() != val2->getTypeTolerant()) { - return -1; - } else { - return ScValue::compare(val1, val2); - } -} - -////////////////////////////////////////////////////////////////////////// -bool ScValue::setProperty(const char *propName, int value) { - ScValue *val = new ScValue(_gameRef, value); - bool ret = DID_SUCCEED(setProp(propName, val)); - delete val; - return ret; -} - -////////////////////////////////////////////////////////////////////////// -bool ScValue::setProperty(const char *propName, const char *value) { - ScValue *val = new ScValue(_gameRef, value); - bool ret = DID_SUCCEED(setProp(propName, val)); - delete val; - return ret; -} - -////////////////////////////////////////////////////////////////////////// -bool ScValue::setProperty(const char *propName, double value) { - ScValue *val = new ScValue(_gameRef, value); - bool ret = DID_SUCCEED(setProp(propName, val)); - delete val; - return ret; -} - - -////////////////////////////////////////////////////////////////////////// -bool ScValue::setProperty(const char *propName, bool value) { - ScValue *val = new ScValue(_gameRef, value); - bool ret = DID_SUCCEED(setProp(propName, val)); - delete val; - return ret; -} - - -////////////////////////////////////////////////////////////////////////// -bool ScValue::setProperty(const char *propName) { - ScValue *val = new ScValue(_gameRef); - bool ret = DID_SUCCEED(setProp(propName, val)); - delete val; - return ret; -} - -} // end of namespace Wintermute +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +/* + * This file is based on WME Lite. + * http://dead-code.org/redir.php?target=wmelite + * Copyright (c) 2011 Jan Nedoma + */ + +#include "engines/wintermute/platform_osystem.h" +#include "engines/wintermute/base/base_dynamic_buffer.h" +#include "engines/wintermute/base/base_game.h" +#include "engines/wintermute/base/scriptables/script_value.h" +#include "engines/wintermute/base/scriptables/script.h" +#include "engines/wintermute/utils/string_util.h" +#include "engines/wintermute/base/base_scriptable.h" + +namespace Wintermute { + +////////////////////////////////////////////////////////////////////// +// Construction/Destruction +////////////////////////////////////////////////////////////////////// + +IMPLEMENT_PERSISTENT(ScValue, false) + +////////////////////////////////////////////////////////////////////////// +ScValue::ScValue(BaseGame *inGame) : BaseClass(inGame) { + _type = VAL_NULL; + + _valBool = false; + _valInt = 0; + _valFloat = 0.0f; + _valNative = NULL; + _valString = NULL; + _valRef = NULL; + _persistent = false; + _isConstVar = false; +} + + +////////////////////////////////////////////////////////////////////////// +ScValue::ScValue(BaseGame *inGame, bool val) : BaseClass(inGame) { + _type = VAL_BOOL; + _valBool = val; + + _valInt = 0; + _valFloat = 0.0f; + _valNative = NULL; + _valString = NULL; + _valRef = NULL; + _persistent = false; + _isConstVar = false; +} + + +////////////////////////////////////////////////////////////////////////// +ScValue::ScValue(BaseGame *inGame, int val) : BaseClass(inGame) { + _type = VAL_INT; + _valInt = val; + + _valFloat = 0.0f; + _valBool = false; + _valNative = NULL; + _valString = NULL; + _valRef = NULL; + _persistent = false; + _isConstVar = false; +} + + +////////////////////////////////////////////////////////////////////////// +ScValue::ScValue(BaseGame *inGame, double val) : BaseClass(inGame) { + _type = VAL_FLOAT; + _valFloat = val; + + _valInt = 0; + _valBool = false; + _valNative = NULL; + _valString = NULL; + _valRef = NULL; + _persistent = false; + _isConstVar = false; +} + + +////////////////////////////////////////////////////////////////////////// +ScValue::ScValue(BaseGame *inGame, const char *val) : BaseClass(inGame) { + _type = VAL_STRING; + _valString = NULL; + setStringVal(val); + + _valBool = false; + _valInt = 0; + _valFloat = 0.0f; + _valNative = NULL; + _valRef = NULL; + _persistent = false; + _isConstVar = false; +} + + +////////////////////////////////////////////////////////////////////////// +void ScValue::cleanup(bool ignoreNatives) { + deleteProps(); + + if (_valString) { + delete[] _valString; + } + + if (!ignoreNatives) { + if (_valNative && !_persistent) { + _valNative->_refCount--; + if (_valNative->_refCount <= 0) { + delete _valNative; + _valNative = NULL; + } + } + } + + + _type = VAL_NULL; + + _valBool = false; + _valInt = 0; + _valFloat = 0.0f; + _valNative = NULL; + _valString = NULL; + _valRef = NULL; + _persistent = false; + _isConstVar = false; +} + + + +////////////////////////////////////////////////////////////////////////// +ScValue::~ScValue() { + cleanup(); +} + + +////////////////////////////////////////////////////////////////////////// +ScValue *ScValue::getProp(const char *name) { + if (_type == VAL_VARIABLE_REF) { + return _valRef->getProp(name); + } + + if (_type == VAL_STRING && strcmp(name, "Length") == 0) { + _gameRef->_scValue->_type = VAL_INT; + + if (_gameRef->_textEncoding == TEXT_ANSI) { + _gameRef->_scValue->setInt(strlen(_valString)); + } else { + WideString wstr = StringUtil::utf8ToWide(_valString); + _gameRef->_scValue->setInt(wstr.size()); + } + + return _gameRef->_scValue; + } + + ScValue *ret = NULL; + + if (_type == VAL_NATIVE && _valNative) { + ret = _valNative->scGetProperty(name); + } + + if (ret == NULL) { + _valIter = _valObject.find(name); + if (_valIter != _valObject.end()) { + ret = _valIter->_value; + } + } + return ret; +} + +////////////////////////////////////////////////////////////////////////// +bool ScValue::deleteProp(const char *name) { + if (_type == VAL_VARIABLE_REF) { + return _valRef->deleteProp(name); + } + + _valIter = _valObject.find(name); + if (_valIter != _valObject.end()) { + delete _valIter->_value; + _valIter->_value = NULL; + } + + return STATUS_OK; +} + + + +////////////////////////////////////////////////////////////////////////// +bool ScValue::setProp(const char *name, ScValue *val, bool copyWhole, bool setAsConst) { + if (_type == VAL_VARIABLE_REF) { + return _valRef->setProp(name, val); + } + + bool ret = STATUS_FAILED; + if (_type == VAL_NATIVE && _valNative) { + ret = _valNative->scSetProperty(name, val); + } + + if (DID_FAIL(ret)) { + ScValue *newVal = NULL; + + _valIter = _valObject.find(name); + if (_valIter != _valObject.end()) { + newVal = _valIter->_value; + } + if (!newVal) { + newVal = new ScValue(_gameRef); + } else { + newVal->cleanup(); + } + + newVal->copy(val, copyWhole); + newVal->_isConstVar = setAsConst; + _valObject[name] = newVal; + + if (_type != VAL_NATIVE) { + _type = VAL_OBJECT; + } + + /* + _valIter = _valObject.find(Name); + if (_valIter != _valObject.end()){ + delete _valIter->_value; + _valIter->_value = NULL; + } + ScValue* val = new ScValue(_gameRef); + val->Copy(Val, CopyWhole); + val->_isConstVar = SetAsConst; + _valObject[Name] = val; + + if (_type!=VAL_NATIVE) _type = VAL_OBJECT; + */ + } + + return STATUS_OK; +} + + +////////////////////////////////////////////////////////////////////////// +bool ScValue::propExists(const char *name) { + if (_type == VAL_VARIABLE_REF) { + return _valRef->propExists(name); + } + _valIter = _valObject.find(name); + + return (_valIter != _valObject.end()); +} + + +////////////////////////////////////////////////////////////////////////// +void ScValue::deleteProps() { + _valIter = _valObject.begin(); + while (_valIter != _valObject.end()) { + delete(ScValue *)_valIter->_value; + _valIter++; + } + _valObject.clear(); +} + + +////////////////////////////////////////////////////////////////////////// +void ScValue::CleanProps(bool includingNatives) { + _valIter = _valObject.begin(); + while (_valIter != _valObject.end()) { + if (!_valIter->_value->_isConstVar && (!_valIter->_value->isNative() || includingNatives)) { + _valIter->_value->setNULL(); + } + _valIter++; + } +} + +////////////////////////////////////////////////////////////////////////// +bool ScValue::isNULL() { + if (_type == VAL_VARIABLE_REF) { + return _valRef->isNULL(); + } + + return (_type == VAL_NULL); +} + + +////////////////////////////////////////////////////////////////////////// +bool ScValue::isNative() { + if (_type == VAL_VARIABLE_REF) { + return _valRef->isNative(); + } + + return (_type == VAL_NATIVE); +} + + +////////////////////////////////////////////////////////////////////////// +bool ScValue::isString() { + if (_type == VAL_VARIABLE_REF) { + return _valRef->isString(); + } + + return (_type == VAL_STRING); +} + + +////////////////////////////////////////////////////////////////////////// +bool ScValue::isFloat() { + if (_type == VAL_VARIABLE_REF) { + return _valRef->isFloat(); + } + + return (_type == VAL_FLOAT); +} + + +////////////////////////////////////////////////////////////////////////// +bool ScValue::isInt() { + if (_type == VAL_VARIABLE_REF) { + return _valRef->isInt(); + } + + return (_type == VAL_INT); +} + + +////////////////////////////////////////////////////////////////////////// +bool ScValue::isBool() { + if (_type == VAL_VARIABLE_REF) { + return _valRef->isBool(); + } + + return (_type == VAL_BOOL); +} + + +////////////////////////////////////////////////////////////////////////// +bool ScValue::isObject() { + if (_type == VAL_VARIABLE_REF) { + return _valRef->isObject(); + } + + return (_type == VAL_OBJECT); +} + + +////////////////////////////////////////////////////////////////////////// +TValType ScValue::getTypeTolerant() { + if (_type == VAL_VARIABLE_REF) { + return _valRef->getType(); + } + + return _type; +} + + +////////////////////////////////////////////////////////////////////////// +void ScValue::setBool(bool val) { + if (_type == VAL_VARIABLE_REF) { + _valRef->setBool(val); + return; + } + + if (_type == VAL_NATIVE) { + _valNative->scSetBool(val); + return; + } + + _valBool = val; + _type = VAL_BOOL; +} + + +////////////////////////////////////////////////////////////////////////// +void ScValue::setInt(int val) { + if (_type == VAL_VARIABLE_REF) { + _valRef->setInt(val); + return; + } + + if (_type == VAL_NATIVE) { + _valNative->scSetInt(val); + return; + } + + _valInt = val; + _type = VAL_INT; +} + + +////////////////////////////////////////////////////////////////////////// +void ScValue::setFloat(double val) { + if (_type == VAL_VARIABLE_REF) { + _valRef->setFloat(val); + return; + } + + if (_type == VAL_NATIVE) { + _valNative->scSetFloat(val); + return; + } + + _valFloat = val; + _type = VAL_FLOAT; +} + + +////////////////////////////////////////////////////////////////////////// +void ScValue::setString(const char *val) { + if (_type == VAL_VARIABLE_REF) { + _valRef->setString(val); + return; + } + + if (_type == VAL_NATIVE) { + _valNative->scSetString(val); + return; + } + + setStringVal(val); + if (_valString) { + _type = VAL_STRING; + } else { + _type = VAL_NULL; + } +} + +void ScValue::setString(const Common::String &val) { + setString(val.c_str()); +} + +////////////////////////////////////////////////////////////////////////// +void ScValue::setStringVal(const char *val) { + if (_valString) { + delete[] _valString; + _valString = NULL; + } + + if (val == NULL) { + _valString = NULL; + return; + } + + _valString = new char [strlen(val) + 1]; + if (_valString) { + strcpy(_valString, val); + } +} + + +////////////////////////////////////////////////////////////////////////// +void ScValue::setNULL() { + if (_type == VAL_VARIABLE_REF) { + _valRef->setNULL(); + return; + } + + if (_valNative && !_persistent) { + _valNative->_refCount--; + if (_valNative->_refCount <= 0) { + delete _valNative; + } + } + _valNative = NULL; + deleteProps(); + + _type = VAL_NULL; +} + + +////////////////////////////////////////////////////////////////////////// +void ScValue::setNative(BaseScriptable *val, bool persistent) { + if (_type == VAL_VARIABLE_REF) { + _valRef->setNative(val, persistent); + return; + } + + if (val == NULL) { + setNULL(); + } else { + if (_valNative && !_persistent) { + _valNative->_refCount--; + if (_valNative->_refCount <= 0) { + if (_valNative != val) { + delete _valNative; + } + _valNative = NULL; + } + } + + _type = VAL_NATIVE; + _persistent = persistent; + + _valNative = val; + if (_valNative && !_persistent) { + _valNative->_refCount++; + } + } +} + + +////////////////////////////////////////////////////////////////////////// +void ScValue::setObject() { + if (_type == VAL_VARIABLE_REF) { + _valRef->setObject(); + return; + } + + deleteProps(); + _type = VAL_OBJECT; +} + + +////////////////////////////////////////////////////////////////////////// +void ScValue::setReference(ScValue *val) { + _valRef = val; + _type = VAL_VARIABLE_REF; +} + + +////////////////////////////////////////////////////////////////////////// +bool ScValue::getBool(bool defaultVal) { + if (_type == VAL_VARIABLE_REF) { + return _valRef->getBool(); + } + + switch (_type) { + case VAL_BOOL: + return _valBool; + + case VAL_NATIVE: + return _valNative->scToBool(); + + case VAL_INT: + return (_valInt != 0); + + case VAL_FLOAT: + return (_valFloat != 0.0f); + + case VAL_STRING: + return (scumm_stricmp(_valString, "1") == 0 || scumm_stricmp(_valString, "yes") == 0 || scumm_stricmp(_valString, "true") == 0); + + default: + return defaultVal; + } +} + + +////////////////////////////////////////////////////////////////////////// +int ScValue::getInt(int defaultVal) { + if (_type == VAL_VARIABLE_REF) { + return _valRef->getInt(); + } + + switch (_type) { + case VAL_BOOL: + return _valBool ? 1 : 0; + + case VAL_NATIVE: + return _valNative->scToInt(); + + case VAL_INT: + return _valInt; + + case VAL_FLOAT: + return (int)_valFloat; + + case VAL_STRING: + return atoi(_valString); + + default: + return defaultVal; + } +} + + +////////////////////////////////////////////////////////////////////////// +double ScValue::getFloat(double defaultVal) { + if (_type == VAL_VARIABLE_REF) { + return _valRef->getFloat(); + } + + switch (_type) { + case VAL_BOOL: + return _valBool ? 1.0f : 0.0f; + + case VAL_NATIVE: + return _valNative->scToFloat(); + + case VAL_INT: + return (double)_valInt; + + case VAL_FLOAT: + return _valFloat; + + case VAL_STRING: + return atof(_valString); + + default: + return defaultVal; + } +} + +////////////////////////////////////////////////////////////////////////// +void *ScValue::getMemBuffer() { + if (_type == VAL_VARIABLE_REF) { + return _valRef->getMemBuffer(); + } + + if (_type == VAL_NATIVE) { + return _valNative->scToMemBuffer(); + } else { + return (void *)NULL; + } +} + + +////////////////////////////////////////////////////////////////////////// +const char *ScValue::getString() { + if (_type == VAL_VARIABLE_REF) { + return _valRef->getString(); + } + + switch (_type) { + case VAL_OBJECT: + setStringVal("[object]"); + break; + + case VAL_NULL: + setStringVal("[null]"); + break; + + case VAL_NATIVE: { + const char *strVal = _valNative->scToString(); + setStringVal(strVal); + return strVal; + break; + } + + case VAL_BOOL: + setStringVal(_valBool ? "yes" : "no"); + break; + + case VAL_INT: { + char dummy[50]; + sprintf(dummy, "%d", _valInt); + setStringVal(dummy); + break; + } + + case VAL_FLOAT: { + char dummy[50]; + sprintf(dummy, "%f", _valFloat); + setStringVal(dummy); + break; + } + + case VAL_STRING: + break; + + default: + setStringVal(""); + } + + return _valString; +} + + +////////////////////////////////////////////////////////////////////////// +BaseScriptable *ScValue::getNative() { + if (_type == VAL_VARIABLE_REF) { + return _valRef->getNative(); + } + + if (_type == VAL_NATIVE) { + return _valNative; + } else { + return NULL; + } +} + + +////////////////////////////////////////////////////////////////////////// +TValType ScValue::getType() { + return _type; +} + + +////////////////////////////////////////////////////////////////////////// +void ScValue::copy(ScValue *orig, bool copyWhole) { + _gameRef = orig->_gameRef; + + if (_valNative && !_persistent) { + _valNative->_refCount--; + if (_valNative->_refCount <= 0) { + if (_valNative != orig->_valNative) { + delete _valNative; + } + _valNative = NULL; + } + } + + if (orig->_type == VAL_VARIABLE_REF && orig->_valRef && copyWhole) { + orig = orig->_valRef; + } + + cleanup(true); + + _type = orig->_type; + _valBool = orig->_valBool; + _valInt = orig->_valInt; + _valFloat = orig->_valFloat; + setStringVal(orig->_valString); + + _valRef = orig->_valRef; + _persistent = orig->_persistent; + + _valNative = orig->_valNative; + if (_valNative && !_persistent) { + _valNative->_refCount++; + } +//!!!! ref->native++ + + // copy properties + if (orig->_type == VAL_OBJECT && orig->_valObject.size() > 0) { + orig->_valIter = orig->_valObject.begin(); + while (orig->_valIter != orig->_valObject.end()) { + _valObject[orig->_valIter->_key] = new ScValue(_gameRef); + _valObject[orig->_valIter->_key]->copy(orig->_valIter->_value); + orig->_valIter++; + } + } else { + _valObject.clear(); + } +} + + +////////////////////////////////////////////////////////////////////////// +void ScValue::setValue(ScValue *val) { + if (val->_type == VAL_VARIABLE_REF) { + setValue(val->_valRef); + return; + } + + // if being assigned a simple type, preserve native state + if (_type == VAL_NATIVE && (val->_type == VAL_INT || val->_type == VAL_STRING || val->_type == VAL_BOOL)) { + switch (val->_type) { + case VAL_INT: + _valNative->scSetInt(val->getInt()); + break; + case VAL_FLOAT: + _valNative->scSetFloat(val->getFloat()); + break; + case VAL_BOOL: + _valNative->scSetBool(val->getBool()); + break; + case VAL_STRING: + _valNative->scSetString(val->getString()); + break; + default: + warning("ScValue::setValue - unhandled enum"); + break; + } + } + // otherwise just copy everything + else { + copy(val); + } +} + + +////////////////////////////////////////////////////////////////////////// +bool ScValue::persist(BasePersistenceManager *persistMgr) { + persistMgr->transfer(TMEMBER(_gameRef)); + + persistMgr->transfer(TMEMBER(_persistent)); + persistMgr->transfer(TMEMBER(_isConstVar)); + persistMgr->transfer(TMEMBER_INT(_type)); + persistMgr->transfer(TMEMBER(_valBool)); + persistMgr->transfer(TMEMBER(_valFloat)); + persistMgr->transfer(TMEMBER(_valInt)); + persistMgr->transfer(TMEMBER(_valNative)); + + int size; + const char *str; + if (persistMgr->getIsSaving()) { + size = _valObject.size(); + persistMgr->transfer("", &size); + _valIter = _valObject.begin(); + while (_valIter != _valObject.end()) { + str = _valIter->_key.c_str(); + persistMgr->transfer("", &str); + persistMgr->transfer("", &_valIter->_value); + + _valIter++; + } + } else { + ScValue *val; + persistMgr->transfer("", &size); + for (int i = 0; i < size; i++) { + persistMgr->transfer("", &str); + persistMgr->transfer("", &val); + + _valObject[str] = val; + delete[] str; + } + } + + persistMgr->transfer(TMEMBER(_valRef)); + persistMgr->transfer(TMEMBER(_valString)); + + /* + FILE* f = fopen("c:\\val.log", "a+"); + switch(_type) + { + case VAL_STRING: + fprintf(f, "str %s\n", _valString); + break; + + case VAL_INT: + fprintf(f, "int %d\n", _valInt); + break; + + case VAL_BOOL: + fprintf(f, "bool %d\n", _valBool); + break; + + case VAL_NULL: + fprintf(f, "null\n"); + break; + + case VAL_NATIVE: + fprintf(f, "native\n"); + break; + + case VAL_VARIABLE_REF: + fprintf(f, "ref\n"); + break; + + case VAL_OBJECT: + fprintf(f, "obj\n"); + break; + + case VAL_FLOAT: + fprintf(f, "float\n"); + break; + + } + fclose(f); + */ + + return STATUS_OK; +} + + +////////////////////////////////////////////////////////////////////////// +bool ScValue::saveAsText(BaseDynamicBuffer *buffer, int indent) { + _valIter = _valObject.begin(); + while (_valIter != _valObject.end()) { + buffer->putTextIndent(indent, "PROPERTY {\n"); + buffer->putTextIndent(indent + 2, "NAME=\"%s\"\n", _valIter->_key.c_str()); + buffer->putTextIndent(indent + 2, "VALUE=\"%s\"\n", _valIter->_value->getString()); + buffer->putTextIndent(indent, "}\n\n"); + + _valIter++; + } + return STATUS_OK; +} + + +////////////////////////////////////////////////////////////////////////// +// -1 ... left is less, 0 ... equals, 1 ... left is greater +int ScValue::compare(ScValue *val1, ScValue *val2) { + // both natives? + if (val1->isNative() && val2->isNative()) { + // same class? + if (strcmp(val1->getNative()->getClassName(), val2->getNative()->getClassName()) == 0) { + return val1->getNative()->scCompare(val2->getNative()); + } else { + return strcmp(val1->getString(), val2->getString()); + } + } + + // both objects? + if (val1->isObject() && val2->isObject()) { + return -1; + } + + + // null states + if (val1->isNULL() && !val2->isNULL()) { + return -1; + } else if (!val1->isNULL() && val2->isNULL()) { + return 1; + } else if (val1->isNULL() && val2->isNULL()) { + return 0; + } + + // one of them is string? convert both to string + if (val1->isString() || val2->isString()) { + return strcmp(val1->getString(), val2->getString()); + } + + // one of them is float? + if (val1->isFloat() || val2->isFloat()) { + if (val1->getFloat() < val2->getFloat()) { + return -1; + } else if (val1->getFloat() > val2->getFloat()) { + return 1; + } else { + return 0; + } + } + + // otherwise compare as int's + if (val1->getInt() < val2->getInt()) { + return -1; + } else if (val1->getInt() > val2->getInt()) { + return 1; + } else { + return 0; + } +} + + +////////////////////////////////////////////////////////////////////////// +int ScValue::compareStrict(ScValue *val1, ScValue *val2) { + if (val1->getTypeTolerant() != val2->getTypeTolerant()) { + return -1; + } else { + return ScValue::compare(val1, val2); + } +} + +////////////////////////////////////////////////////////////////////////// +bool ScValue::setProperty(const char *propName, int value) { + ScValue *val = new ScValue(_gameRef, value); + bool ret = DID_SUCCEED(setProp(propName, val)); + delete val; + return ret; +} + +////////////////////////////////////////////////////////////////////////// +bool ScValue::setProperty(const char *propName, const char *value) { + ScValue *val = new ScValue(_gameRef, value); + bool ret = DID_SUCCEED(setProp(propName, val)); + delete val; + return ret; +} + +////////////////////////////////////////////////////////////////////////// +bool ScValue::setProperty(const char *propName, double value) { + ScValue *val = new ScValue(_gameRef, value); + bool ret = DID_SUCCEED(setProp(propName, val)); + delete val; + return ret; +} + + +////////////////////////////////////////////////////////////////////////// +bool ScValue::setProperty(const char *propName, bool value) { + ScValue *val = new ScValue(_gameRef, value); + bool ret = DID_SUCCEED(setProp(propName, val)); + delete val; + return ret; +} + + +////////////////////////////////////////////////////////////////////////// +bool ScValue::setProperty(const char *propName) { + ScValue *val = new ScValue(_gameRef); + bool ret = DID_SUCCEED(setProp(propName, val)); + delete val; + return ret; +} + +} // end of namespace Wintermute diff --git a/engines/wintermute/base/scriptables/script_value.h b/engines/wintermute/base/scriptables/script_value.h index af31014bac..bf7d9cd8a1 100644 --- a/engines/wintermute/base/scriptables/script_value.h +++ b/engines/wintermute/base/scriptables/script_value.h @@ -1,113 +1,113 @@ -/* ScummVM - Graphic Adventure Engine - * - * ScummVM is the legal property of its developers, whose names - * are too numerous to list here. Please refer to the COPYRIGHT - * file distributed with this source distribution. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - */ - -/* - * This file is based on WME Lite. - * http://dead-code.org/redir.php?target=wmelite - * Copyright (c) 2011 Jan Nedoma - */ - -#ifndef WINTERMUTE_SCVALUE_H -#define WINTERMUTE_SCVALUE_H - - -#include "engines/wintermute/base/base.h" -#include "engines/wintermute/persistent.h" -#include "engines/wintermute/base/scriptables/dcscript.h" // Added by ClassView -#include "common/str.h" - -namespace Wintermute { - -class ScScript; -class BaseScriptable; - -class ScValue : public BaseClass { -public: - static int compare(ScValue *val1, ScValue *val2); - static int compareStrict(ScValue *val1, ScValue *val2); - TValType getTypeTolerant(); - void cleanup(bool ignoreNatives = false); - DECLARE_PERSISTENT(ScValue, BaseClass) - - bool _isConstVar; - bool saveAsText(BaseDynamicBuffer *buffer, int indent); - void setValue(ScValue *val); - bool _persistent; - bool propExists(const char *name); - void copy(ScValue *orig, bool copyWhole = false); - void setStringVal(const char *val); - TValType getType(); - bool getBool(bool defaultVal = false); - int getInt(int defaultVal = 0); - double getFloat(double defaultVal = 0.0f); - const char *getString(); - void *getMemBuffer(); - BaseScriptable *getNative(); - bool deleteProp(const char *name); - void deleteProps(); - void CleanProps(bool includingNatives); - void setBool(bool val); - void setInt(int val); - void setFloat(double val); - void setString(const char *val); - void setString(const Common::String &val); - void setNULL(); - void setNative(BaseScriptable *val, bool persistent = false); - void setObject(); - void setReference(ScValue *val); - bool isNULL(); - bool isNative(); - bool isString(); - bool isBool(); - bool isFloat(); - bool isInt(); - bool isObject(); - bool setProp(const char *name, ScValue *val, bool copyWhole = false, bool setAsConst = false); - ScValue *getProp(const char *name); - BaseScriptable *_valNative; - ScValue *_valRef; -private: - bool _valBool; - int _valInt; - double _valFloat; - char *_valString; -public: - TValType _type; - ScValue(BaseGame *inGame); - ScValue(BaseGame *inGame, bool Val); - ScValue(BaseGame *inGame, int Val); - ScValue(BaseGame *inGame, double Val); - ScValue(BaseGame *inGame, const char *Val); - virtual ~ScValue(); - Common::HashMap _valObject; - Common::HashMap::iterator _valIter; - - bool setProperty(const char *propName, int value); - bool setProperty(const char *propName, const char *value); - bool setProperty(const char *propName, double value); - bool setProperty(const char *propName, bool value); - bool setProperty(const char *propName); -}; - -} // end of namespace Wintermute - -#endif +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +/* + * This file is based on WME Lite. + * http://dead-code.org/redir.php?target=wmelite + * Copyright (c) 2011 Jan Nedoma + */ + +#ifndef WINTERMUTE_SCVALUE_H +#define WINTERMUTE_SCVALUE_H + + +#include "engines/wintermute/base/base.h" +#include "engines/wintermute/persistent.h" +#include "engines/wintermute/base/scriptables/dcscript.h" // Added by ClassView +#include "common/str.h" + +namespace Wintermute { + +class ScScript; +class BaseScriptable; + +class ScValue : public BaseClass { +public: + static int compare(ScValue *val1, ScValue *val2); + static int compareStrict(ScValue *val1, ScValue *val2); + TValType getTypeTolerant(); + void cleanup(bool ignoreNatives = false); + DECLARE_PERSISTENT(ScValue, BaseClass) + + bool _isConstVar; + bool saveAsText(BaseDynamicBuffer *buffer, int indent); + void setValue(ScValue *val); + bool _persistent; + bool propExists(const char *name); + void copy(ScValue *orig, bool copyWhole = false); + void setStringVal(const char *val); + TValType getType(); + bool getBool(bool defaultVal = false); + int getInt(int defaultVal = 0); + double getFloat(double defaultVal = 0.0f); + const char *getString(); + void *getMemBuffer(); + BaseScriptable *getNative(); + bool deleteProp(const char *name); + void deleteProps(); + void CleanProps(bool includingNatives); + void setBool(bool val); + void setInt(int val); + void setFloat(double val); + void setString(const char *val); + void setString(const Common::String &val); + void setNULL(); + void setNative(BaseScriptable *val, bool persistent = false); + void setObject(); + void setReference(ScValue *val); + bool isNULL(); + bool isNative(); + bool isString(); + bool isBool(); + bool isFloat(); + bool isInt(); + bool isObject(); + bool setProp(const char *name, ScValue *val, bool copyWhole = false, bool setAsConst = false); + ScValue *getProp(const char *name); + BaseScriptable *_valNative; + ScValue *_valRef; +private: + bool _valBool; + int _valInt; + double _valFloat; + char *_valString; +public: + TValType _type; + ScValue(BaseGame *inGame); + ScValue(BaseGame *inGame, bool Val); + ScValue(BaseGame *inGame, int Val); + ScValue(BaseGame *inGame, double Val); + ScValue(BaseGame *inGame, const char *Val); + virtual ~ScValue(); + Common::HashMap _valObject; + Common::HashMap::iterator _valIter; + + bool setProperty(const char *propName, int value); + bool setProperty(const char *propName, const char *value); + bool setProperty(const char *propName, double value); + bool setProperty(const char *propName, bool value); + bool setProperty(const char *propName); +}; + +} // end of namespace Wintermute + +#endif -- cgit v1.2.3 From 8c864170a4afef9d1c88c4d35169da9ce49c4f16 Mon Sep 17 00:00:00 2001 From: Einar Johan Trøan Sømåen Date: Tue, 11 Sep 2012 03:11:02 +0200 Subject: WINTERMUTE: Remove unneccessary includes of base_file.h --- engines/wintermute/base/scriptables/script_ext_file.cpp | 1 - 1 file changed, 1 deletion(-) (limited to 'engines/wintermute/base/scriptables') diff --git a/engines/wintermute/base/scriptables/script_ext_file.cpp b/engines/wintermute/base/scriptables/script_ext_file.cpp index ab574d464b..2dc385b015 100644 --- a/engines/wintermute/base/scriptables/script_ext_file.cpp +++ b/engines/wintermute/base/scriptables/script_ext_file.cpp @@ -33,7 +33,6 @@ #include "engines/wintermute/base/scriptables/script.h" #include "engines/wintermute/utils/utils.h" #include "engines/wintermute/base/base_game.h" -#include "engines/wintermute/base/file/base_file.h" #include "engines/wintermute/base/base_file_manager.h" #include "engines/wintermute/platform_osystem.h" #include "engines/wintermute/base/scriptables/script_ext_file.h" -- cgit v1.2.3 From f33884b02091537275a17ff55ae756ff4e862c29 Mon Sep 17 00:00:00 2001 From: Einar Johan Trøan Sømåen Date: Tue, 11 Sep 2012 03:40:56 +0200 Subject: WINTERMUTE: Remove a few more unused includes. --- engines/wintermute/base/scriptables/script_engine.cpp | 1 - 1 file changed, 1 deletion(-) (limited to 'engines/wintermute/base/scriptables') diff --git a/engines/wintermute/base/scriptables/script_engine.cpp b/engines/wintermute/base/scriptables/script_engine.cpp index 20e2ccadd1..3d1863946e 100644 --- a/engines/wintermute/base/scriptables/script_engine.cpp +++ b/engines/wintermute/base/scriptables/script_engine.cpp @@ -33,7 +33,6 @@ #include "engines/wintermute/base/scriptables/script_ext_math.h" #include "engines/wintermute/base/base_engine.h" #include "engines/wintermute/base/base_game.h" -#include "engines/wintermute/base/sound/base_sound.h" #include "engines/wintermute/base/base_file_manager.h" #include "engines/wintermute/utils/utils.h" -- cgit v1.2.3 From afc21941e37a7481f5fe050d220968bdce43c873 Mon Sep 17 00:00:00 2001 From: Einar Johan Trøan Sømåen Date: Sat, 29 Sep 2012 00:47:12 +0200 Subject: WINTERMUTE: Make scGetProperty use Common::String& instead of const char* --- engines/wintermute/base/scriptables/script_ext_array.cpp | 8 ++++---- engines/wintermute/base/scriptables/script_ext_array.h | 2 +- engines/wintermute/base/scriptables/script_ext_date.cpp | 6 +++--- engines/wintermute/base/scriptables/script_ext_date.h | 2 +- engines/wintermute/base/scriptables/script_ext_file.cpp | 14 +++++++------- engines/wintermute/base/scriptables/script_ext_file.h | 2 +- engines/wintermute/base/scriptables/script_ext_math.cpp | 6 +++--- engines/wintermute/base/scriptables/script_ext_math.h | 2 +- .../wintermute/base/scriptables/script_ext_mem_buffer.cpp | 6 +++--- .../wintermute/base/scriptables/script_ext_mem_buffer.h | 2 +- engines/wintermute/base/scriptables/script_ext_string.cpp | 8 ++++---- engines/wintermute/base/scriptables/script_ext_string.h | 2 +- 12 files changed, 30 insertions(+), 30 deletions(-) (limited to 'engines/wintermute/base/scriptables') diff --git a/engines/wintermute/base/scriptables/script_ext_array.cpp b/engines/wintermute/base/scriptables/script_ext_array.cpp index 5ed07f0da6..613cbd0758 100644 --- a/engines/wintermute/base/scriptables/script_ext_array.cpp +++ b/engines/wintermute/base/scriptables/script_ext_array.cpp @@ -140,13 +140,13 @@ bool SXArray::scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack, ////////////////////////////////////////////////////////////////////////// -ScValue *SXArray::scGetProperty(const char *name) { +ScValue *SXArray::scGetProperty(const Common::String &name) { _scValue->setNULL(); ////////////////////////////////////////////////////////////////////////// // Type ////////////////////////////////////////////////////////////////////////// - if (strcmp(name, "Type") == 0) { + if (name == "Type") { _scValue->setString("array"); return _scValue; } @@ -154,7 +154,7 @@ ScValue *SXArray::scGetProperty(const char *name) { ////////////////////////////////////////////////////////////////////////// // Length ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "Length") == 0) { + else if (name == "Length") { _scValue->setInt(_length); return _scValue; } @@ -164,7 +164,7 @@ ScValue *SXArray::scGetProperty(const char *name) { ////////////////////////////////////////////////////////////////////////// else { char paramName[20]; - if (validNumber(name, paramName)) { + if (validNumber(name.c_str(), paramName)) { // TODO: Change to Common::String return _values->getProp(paramName); } else { return _scValue; diff --git a/engines/wintermute/base/scriptables/script_ext_array.h b/engines/wintermute/base/scriptables/script_ext_array.h index d9805ef94f..284c547a27 100644 --- a/engines/wintermute/base/scriptables/script_ext_array.h +++ b/engines/wintermute/base/scriptables/script_ext_array.h @@ -41,7 +41,7 @@ public: SXArray(BaseGame *inGame, ScStack *stack); SXArray(BaseGame *inGame); virtual ~SXArray(); - ScValue *scGetProperty(const char *name); + ScValue *scGetProperty(const Common::String &name); bool scSetProperty(const char *name, ScValue *value); bool scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack, const char *name); const char *scToString(); diff --git a/engines/wintermute/base/scriptables/script_ext_date.cpp b/engines/wintermute/base/scriptables/script_ext_date.cpp index 11eead3b9c..5aa069d0b2 100644 --- a/engines/wintermute/base/scriptables/script_ext_date.cpp +++ b/engines/wintermute/base/scriptables/script_ext_date.cpp @@ -203,13 +203,13 @@ bool SXDate::scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack, ////////////////////////////////////////////////////////////////////////// -ScValue *SXDate::scGetProperty(const char *name) { +ScValue *SXDate::scGetProperty(const Common::String &name) { _scValue->setNULL(); ////////////////////////////////////////////////////////////////////////// // Type ////////////////////////////////////////////////////////////////////////// - if (strcmp(name, "Type") == 0) { + if (name == "Type") { _scValue->setString("date"); return _scValue; } else { @@ -224,7 +224,7 @@ bool SXDate::scSetProperty(const char *name, ScValue *value) { ////////////////////////////////////////////////////////////////////////// // Name ////////////////////////////////////////////////////////////////////////// - if (strcmp(name, "Name")==0){ + if (name == "Name")==0){ setName(value->getString()); return STATUS_OK; } diff --git a/engines/wintermute/base/scriptables/script_ext_date.h b/engines/wintermute/base/scriptables/script_ext_date.h index f6f04dd7e6..062b7c55c7 100644 --- a/engines/wintermute/base/scriptables/script_ext_date.h +++ b/engines/wintermute/base/scriptables/script_ext_date.h @@ -40,7 +40,7 @@ public: DECLARE_PERSISTENT(SXDate, BaseScriptable) SXDate(BaseGame *inGame, ScStack *Stack); virtual ~SXDate(); - ScValue *scGetProperty(const char *name); + ScValue *scGetProperty(const Common::String &name); bool scSetProperty(const char *name, ScValue *value); bool scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack, const char *name); const char *scToString(); diff --git a/engines/wintermute/base/scriptables/script_ext_file.cpp b/engines/wintermute/base/scriptables/script_ext_file.cpp index 2dc385b015..a1d39c5d0a 100644 --- a/engines/wintermute/base/scriptables/script_ext_file.cpp +++ b/engines/wintermute/base/scriptables/script_ext_file.cpp @@ -640,13 +640,13 @@ bool SXFile::scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack, ////////////////////////////////////////////////////////////////////////// -ScValue *SXFile::scGetProperty(const char *name) { +ScValue *SXFile::scGetProperty(const Common::String &name) { _scValue->setNULL(); ////////////////////////////////////////////////////////////////////////// // Type (RO) ////////////////////////////////////////////////////////////////////////// - if (strcmp(name, "Type") == 0) { + if (name == "Type") { _scValue->setString("file"); return _scValue; } @@ -654,7 +654,7 @@ ScValue *SXFile::scGetProperty(const char *name) { ////////////////////////////////////////////////////////////////////////// // Filename (RO) ////////////////////////////////////////////////////////////////////////// - if (strcmp(name, "Filename") == 0) { + if (name == "Filename") { _scValue->setString(_filename); return _scValue; } @@ -662,7 +662,7 @@ ScValue *SXFile::scGetProperty(const char *name) { ////////////////////////////////////////////////////////////////////////// // Position (RO) ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "Position") == 0) { + else if (name == "Position") { _scValue->setInt(getPos()); return _scValue; } @@ -670,7 +670,7 @@ ScValue *SXFile::scGetProperty(const char *name) { ////////////////////////////////////////////////////////////////////////// // Length (RO) ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "Length") == 0) { + else if (name == "Length") { _scValue->setInt(getLength()); return _scValue; } @@ -678,7 +678,7 @@ ScValue *SXFile::scGetProperty(const char *name) { ////////////////////////////////////////////////////////////////////////// // TextMode (RO) ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "TextMode") == 0) { + else if (name == "TextMode") { _scValue->setBool(_textMode); return _scValue; } @@ -686,7 +686,7 @@ ScValue *SXFile::scGetProperty(const char *name) { ////////////////////////////////////////////////////////////////////////// // AccessMode (RO) ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "AccessMode") == 0) { + else if (name == "AccessMode") { _scValue->setInt(_mode); return _scValue; } else { diff --git a/engines/wintermute/base/scriptables/script_ext_file.h b/engines/wintermute/base/scriptables/script_ext_file.h index b91a53e695..f7c72fcfb3 100644 --- a/engines/wintermute/base/scriptables/script_ext_file.h +++ b/engines/wintermute/base/scriptables/script_ext_file.h @@ -40,7 +40,7 @@ class BaseFile; class SXFile : public BaseScriptable { public: DECLARE_PERSISTENT(SXFile, BaseScriptable) - ScValue *scGetProperty(const char *name); + ScValue *scGetProperty(const Common::String &name); bool scSetProperty(const char *name, ScValue *value); bool scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack, const char *name); const char *scToString(); diff --git a/engines/wintermute/base/scriptables/script_ext_math.cpp b/engines/wintermute/base/scriptables/script_ext_math.cpp index 598b80cff3..d816fbec65 100644 --- a/engines/wintermute/base/scriptables/script_ext_math.cpp +++ b/engines/wintermute/base/scriptables/script_ext_math.cpp @@ -250,13 +250,13 @@ bool SXMath::scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack, ////////////////////////////////////////////////////////////////////////// -ScValue *SXMath::scGetProperty(const char *name) { +ScValue *SXMath::scGetProperty(const Common::String &name) { _scValue->setNULL(); ////////////////////////////////////////////////////////////////////////// // Type ////////////////////////////////////////////////////////////////////////// - if (strcmp(name, "Type") == 0) { + if (name == "Type") { _scValue->setString("math"); return _scValue; } @@ -264,7 +264,7 @@ ScValue *SXMath::scGetProperty(const char *name) { ////////////////////////////////////////////////////////////////////////// // PI ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "PI") == 0) { + else if (name == "PI") { _scValue->setFloat(M_PI); return _scValue; } else { diff --git a/engines/wintermute/base/scriptables/script_ext_math.h b/engines/wintermute/base/scriptables/script_ext_math.h index f86d59fe7b..48c43ea7e8 100644 --- a/engines/wintermute/base/scriptables/script_ext_math.h +++ b/engines/wintermute/base/scriptables/script_ext_math.h @@ -39,7 +39,7 @@ public: DECLARE_PERSISTENT(SXMath, BaseScriptable) SXMath(BaseGame *inGame); virtual ~SXMath(); - virtual ScValue *scGetProperty(const char *name); + virtual ScValue *scGetProperty(const Common::String &name); virtual bool scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack, const char *name); private: diff --git a/engines/wintermute/base/scriptables/script_ext_mem_buffer.cpp b/engines/wintermute/base/scriptables/script_ext_mem_buffer.cpp index 5ed9bd5313..8f05b7bff6 100644 --- a/engines/wintermute/base/scriptables/script_ext_mem_buffer.cpp +++ b/engines/wintermute/base/scriptables/script_ext_mem_buffer.cpp @@ -447,13 +447,13 @@ bool SXMemBuffer::scCallMethod(ScScript *script, ScStack *stack, ScStack *thisSt ////////////////////////////////////////////////////////////////////////// -ScValue *SXMemBuffer::scGetProperty(const char *name) { +ScValue *SXMemBuffer::scGetProperty(const Common::String &name) { _scValue->setNULL(); ////////////////////////////////////////////////////////////////////////// // Type (RO) ////////////////////////////////////////////////////////////////////////// - if (strcmp(name, "Type") == 0) { + if (name == "Type") { _scValue->setString("membuffer"); return _scValue; } @@ -461,7 +461,7 @@ ScValue *SXMemBuffer::scGetProperty(const char *name) { ////////////////////////////////////////////////////////////////////////// // Size (RO) ////////////////////////////////////////////////////////////////////////// - if (strcmp(name, "Size") == 0) { + if (name == "Size") { _scValue->setInt(_size); return _scValue; } else { diff --git a/engines/wintermute/base/scriptables/script_ext_mem_buffer.h b/engines/wintermute/base/scriptables/script_ext_mem_buffer.h index d2662b3036..1527a323dc 100644 --- a/engines/wintermute/base/scriptables/script_ext_mem_buffer.h +++ b/engines/wintermute/base/scriptables/script_ext_mem_buffer.h @@ -38,7 +38,7 @@ class SXMemBuffer : public BaseScriptable { public: virtual int scCompare(BaseScriptable *Val); DECLARE_PERSISTENT(SXMemBuffer, BaseScriptable) - ScValue *scGetProperty(const char *name); + ScValue *scGetProperty(const Common::String &name); bool scSetProperty(const char *name, ScValue *value); bool scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack, const char *name); const char *scToString(); diff --git a/engines/wintermute/base/scriptables/script_ext_string.cpp b/engines/wintermute/base/scriptables/script_ext_string.cpp index 8d87a92dc1..5f7da1c2dd 100644 --- a/engines/wintermute/base/scriptables/script_ext_string.cpp +++ b/engines/wintermute/base/scriptables/script_ext_string.cpp @@ -343,20 +343,20 @@ bool SXString::scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack ////////////////////////////////////////////////////////////////////////// -ScValue *SXString::scGetProperty(const char *name) { +ScValue *SXString::scGetProperty(const Common::String &name) { _scValue->setNULL(); ////////////////////////////////////////////////////////////////////////// // Type (RO) ////////////////////////////////////////////////////////////////////////// - if (strcmp(name, "Type") == 0) { + if (name == "Type") { _scValue->setString("string"); return _scValue; } ////////////////////////////////////////////////////////////////////////// // Length (RO) ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "Length") == 0) { + else if (name == "Length") { if (_gameRef->_textEncoding == TEXT_UTF8) { WideString wstr = StringUtil::utf8ToWide(_string); _scValue->setInt(wstr.size()); @@ -369,7 +369,7 @@ ScValue *SXString::scGetProperty(const char *name) { ////////////////////////////////////////////////////////////////////////// // Capacity ////////////////////////////////////////////////////////////////////////// - else if (strcmp(name, "Capacity") == 0) { + else if (name == "Capacity") { _scValue->setInt(_capacity); return _scValue; } else { diff --git a/engines/wintermute/base/scriptables/script_ext_string.h b/engines/wintermute/base/scriptables/script_ext_string.h index 255b9c57eb..00bffab3a9 100644 --- a/engines/wintermute/base/scriptables/script_ext_string.h +++ b/engines/wintermute/base/scriptables/script_ext_string.h @@ -38,7 +38,7 @@ class SXString : public BaseScriptable { public: virtual int scCompare(BaseScriptable *Val); DECLARE_PERSISTENT(SXString, BaseScriptable) - ScValue *scGetProperty(const char *name); + ScValue *scGetProperty(const Common::String &name); bool scSetProperty(const char *name, ScValue *value); bool scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack, const char *name); void scSetString(const char *val); -- cgit v1.2.3