1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
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 = nullptr;
}
//////////////////////////////////////////////////////////////////////////
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 Common::String &name) {
_scValue->setNULL();
//////////////////////////////////////////////////////////////////////////
// Type
//////////////////////////////////////////////////////////////////////////
if (name == "Type") {
_scValue->setString("array");
return _scValue;
}
//////////////////////////////////////////////////////////////////////////
// Length
//////////////////////////////////////////////////////////////////////////
else if (name == "Length") {
_scValue->setInt(_length);
return _scValue;
}
//////////////////////////////////////////////////////////////////////////
// [number]
//////////////////////////////////////////////////////////////////////////
else {
char paramName[20];
if (validNumber(name.c_str(), paramName)) { // TODO: Change to Common::String
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->transferPtr(TMEMBER_PTR(_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
|