-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathScript.cpp
More file actions
293 lines (261 loc) · 7.92 KB
/
Copy pathScript.cpp
File metadata and controls
293 lines (261 loc) · 7.92 KB
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#include "Resource/Script.h"
#include "std_library_functions.h"
#include <globaldefs.h>
#if defined(jpn)
#define data_020ef918 data_020ef808
#endif
extern char data_020ef918[]; // holds a blank string
inline float BitCastIntToFloat(const uint32_t* num)
{
// should replace with bit_cast if compiling with newer compiler
return *reinterpret_cast<const float*>(num);
}
bool Script::Initialize()
{
numSupportedOpcodes_ = 0;
unsupportedOpcodeProc_ = NULL;
pCode_ = NULL;
codeLength_ = 0;
pHeader_ = NULL;
maybeInstructionStart_ = NULL;
dataSection_ = NULL;
altDataSection_ = 0;
nextInstruction_ = 0;
currentInstructionIndex_ = 0;
unknown_42c_ = false;
return true;
}
bool Script::SetOpcodeLookup(OpcodeLookupEntry *lookup)
{
bool success;
if (lookup == NULL)
success = false;
else
{
opcodeLookup_ = lookup;
// We're about to do bubble sort on the instructions, but first
// count how many there are
int count = 0;
OpcodeLookupEntry* countPtr = lookup;
while (countPtr->opcode != 0)
{
countPtr++;
count++;
}
numSupportedOpcodes_ = count;
int endIndex = count - 1;
while (true)
{
bool changesMade = false;
OpcodeLookupEntry* swapPtr = lookup;
for (int idx = 0; idx < endIndex; idx++, swapPtr++)
{
if ((unsigned int)swapPtr[0].opcode > swapPtr[1].opcode)
{
changesMade = true;
OpcodeLookupEntry temp;
memcpy(&temp, &swapPtr[0], sizeof(OpcodeLookupEntry));
memcpy(&swapPtr[0], &swapPtr[1], sizeof(OpcodeLookupEntry));
memcpy(&swapPtr[1], &temp, sizeof(OpcodeLookupEntry));
}
}
if (changesMade)
endIndex--;
else
break;
}
success = true;
}
return success;
}
bool Script::Load(const void *code, unsigned int length)
{
pCode_ = code;
codeLength_ = length;
pHeader_ = (const FileHeader*)pCode_;
maybeInstructionStart_ = (const InstructionPrefix*)((intptr_t)pCode_ + 0x10);
dataSection_ = (uint8_t*)pCode_ + pHeader_->dataOffset;
altDataSection_ = 0;
return true;
}
bool Script::Execute()
{
if (numSupportedOpcodes_ <= 0 || pCode_ == NULL || codeLength_ <= 0)
return false;
{
currentInstructionIndex_ = 0;
while (ExecuteSingleInstruction(true) != NULL) {}
return true;
}
return false;
}
const Script::InstructionPrefix* Script::ExecuteSingleInstruction(bool arg)
{
if (pCode_ == NULL || codeLength_ <= 0 || pHeader_ == NULL)
return NULL;
if (currentInstructionIndex_ >= pHeader_->numInstructions)
return NULL;
if (nextInstruction_ == NULL)
nextInstruction_ = maybeInstructionStart_;
else
{
const InstructionPrefix* instruction = nextInstruction_;
int extraBytes = GetInstructionParamsOffset(instruction) + 4 * instruction->numParams;
int padding = (4 - (extraBytes % 4)) % 4;
nextInstruction_ = (const InstructionPrefix*)(
(intptr_t)instruction + (extraBytes + padding)
);
}
if (nextInstruction_ != NULL && arg)
{
int opcode = nextInstruction_->opcode;
int numArgs = nextInstruction_->numParams;
const OpcodeLookupEntry* entry = LookupOpcode(opcode);
bool valid = false;
if (entry != NULL)
valid = true;
else if (!unknown_42c_ && unsupportedOpcodeProc_ != NULL)
valid = true;
if (valid)
{
const InstructionPrefix* instruction = nextInstruction_;
const uint8_t* pTypeData = (const uint8_t*)instruction + 3;
const uint32_t* pSourceArgs = (const uint32_t*)(
(intptr_t)instruction + GetInstructionParamsOffset(instruction)
);
Parameter* pArgsDest = ¤tInstructionParams_[0];
unsigned char currentTypeByte = 0;
for (int i = 0; i < numArgs; i++)
{
if (i >= 128)
break;
if ((i % 4) == 0)
{
if (i > 0)
pTypeData++;
currentTypeByte = *pTypeData;
}
else
currentTypeByte >>= 2;
int currentArgType = currentTypeByte & 3;
switch (currentArgType)
{
case 0: // string
pArgsDest->type = 0;
pArgsDest->value.str = GetDataString(*pSourceArgs);
break;
case 1: // int
pArgsDest->type = 1;
pArgsDest->value.i = *pSourceArgs;
break;
case 2: // float
pArgsDest->type = 2;
pArgsDest->value.f = BitCastIntToFloat(pSourceArgs);
break;
default:
continue;
}
pSourceArgs++;
pArgsDest++;
}
if (entry != NULL)
entry->proc(currentInstructionParams_, numArgs);
else
unsupportedOpcodeProc_(opcode, currentInstructionParams_, numArgs);
}
}
currentInstructionIndex_++;
return nextInstruction_;
}
int Script::GetInstructionParamsOffset(const InstructionPrefix* prefix)
{
// this is super overengineered because numbers are signed and they
// have no reason to be
int numParamTypeBits = 2 * prefix->numParams;
int numParamTypeBytes = (numParamTypeBits / 8) + ((numParamTypeBits % 8) > 0 ? 1 : 0);
int totalBytes = numParamTypeBytes + 3;
int padding = (4 - (totalBytes % 4)) % 4;
return totalBytes + padding;
}
const Script::OpcodeLookupEntry* Script::LookupOpcode(int opcode)
{
if (numSupportedOpcodes_ <= 0)
return NULL;
int searchMin = 0;
int searchMax = numSupportedOpcodes_ - 1;
while (searchMin <= searchMax)
{
int midpoint = searchMin + ((searchMax - searchMin + 1) / 2);
const OpcodeLookupEntry* candidate = &opcodeLookup_[midpoint];
if (opcode == candidate->opcode)
return candidate;
else if (opcode < candidate->opcode)
searchMax = midpoint - 1;
else
searchMin = midpoint + 1;
}
return NULL;
}
const char* Script::GetDataString(int32_t offset)
{
if (pCode_ == NULL || codeLength_ <= 0 || pHeader_ == NULL || dataSection_ == NULL)
return NULL;
if (offset == -1)
return data_020ef918;
if (offset == -2 || offset < 0)
return NULL;
if (offset >= pHeader_->dataLength)
return NULL;
if (altDataSection_ != 0)
return (const char*)altDataSection_ + offset;
else
return (const char*)dataSection_ + offset;
}
int Script::Parameter::ToInt() const
{
switch (type)
{
case 1:
return value.i;
case 2:
return (int)value.f;
default:
return 0;
}
}
float Script::Parameter::ToFloat() const
{
switch (type)
{
case 1:
return (float)value.i;
case 2:
return value.f;
}
return 0.0f;
}
const char* Script::Parameter::ToString() const
{
switch (type)
{
case 0:
return value.str;
}
return NULL;
}
Script::Parameter* Script::Parameter::ToVec3fix(Vector3fix* out)
{
Script::Parameter* params = this;
out->x = 4096 * params[0].ToFloat();
out->y = 4096 * params[1].ToFloat();
out->z = 4096 * params[2].ToFloat();
return ¶ms[3];
}
Script::Parameter* Script::Parameter::ToVec3fix16(Vector3fix16 *out)
{
Script::Parameter* params = this;
out->x = 4096 * params[0].ToFloat();
out->y = 4096 * params[1].ToFloat();
out->z = 4096 * params[2].ToFloat();
return ¶ms[3];
}