aboutsummaryrefslogtreecommitdiff
path: root/src/core/vmrunner.cpp
blob: 5569b1c98333af531d40068e0bd1d7b244114b53 (plain) (blame)
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
#include "vmrunner.hpp"
#include "coreshared.hpp"
#include "fncwrapper.hpp"
#include <cstdlib>
#include <cstdio>
blrScriptVM *vm;
callStack<Idata> callStk;
unsigned getHash(const char *s)
{
	unsigned r=5381;char c;
	while((c=(*s++)))r=((r<<5)+r)+c;
	return r;
}
Idata& blrScriptVM::fetchData(SPara para,bool forcerw)
{
	static Idata cret;
	switch (para.type)
	{
		case 0:
			if(forcerw)throw;
			cret.i()=para.data.i;cret.type=0;
			return cret;
		case 1:
			if(forcerw)throw;
			cret.r()=para.data.r;cret.type=1;
			return cret;
		case 2:
			return ir[para.data.i];
		case 3:
			return rr[para.data.i];
		case 4:
			return ia[para.data.i];
		case 5:
			return ra[para.data.i];
		case 6:
			return ia[ir[para.data.i].i()];
		case 7:
			return ra[ir[para.data.i].i()];
		default:
			throw;
	}
}
int blrScriptVM::mgetc()
{
	if(cbyte-sbyte<fsize)
	{++cbyte;return (int)(*(cbyte-1));}
	else return -1;
}
void blrScriptVM::readPara(SPara *para)
{
	para->type=mgetc();
	para->data.d=0;
	int l=0;
	switch(para->type)
	{
		case 1: l=8;break;
		case 0: case 4: case 5: l=4;break;
		case 2: case 3: case 6: case 7: l=1;break;
		default: break;
	}
	for(int i=0;i<l;++i)
	{
		para->data.d<<=8LL;
		int a=mgetc();
		para->data.d|=(unsigned long long)a;
	}
}
int blrScriptVM::loadLSBFromMemory(const char *ptr,DWORD size)
{
	cbyte=sbyte=(const unsigned char*)ptr;fsize=size;
	int ibyt=0,infunc=0;
	while(~(ibyt=mgetc()))
	{
		++ic;inst[ic].id=ibyt;
		switch(inst[ic].id)
		{
			case 0xFF: case 0x02:
			{
				int nl=mgetc();
				if(nl==-1)return 1;
				if(inst[ic].id==0xFF){if(infunc)return 1;else infunc=1;}
				if(inst[ic].id==0x02)if(!infunc)return 1;
				inst[ic].para1.fnc=(char*)calloc(sizeof(char),nl+1);
				inst[ic].para1.type=8;
				for(int i=0;i<nl;++i)inst[ic].para1.fnc[i]=mgetc();
				if(inst[ic].id==0xFF)
				{
					fncent[fncnt].pos=ic;
					fncent[fncnt++].hash=getHash(inst[ic].para1.fnc);
				}
			}
			break;
			case 0x00: break;
			case 0xFE:
				if(!infunc)return 1;
				infunc=0;
			break;
			case 0x0F:
				if(!infunc)return 1;
			break;
			case 0x01: case 0x0C: case 0x0D:
			case 0x0E: case 0x11: case 0x12:
			case 0x13: case 0x14: case 0x15:
			case 0x16: case 0x21:
				if(!infunc)return 1;
				readPara(&inst[ic].para1);
			break;
			case 0x03: case 0x04: case 0x05:
			case 0x06: case 0x07: case 0x08:
			case 0x09: case 0x0A: case 0x0B:
			case 0x22: case 0x23:
				if(!infunc)return 1;
				readPara(&inst[ic].para1);
				readPara(&inst[ic].para2);
			break;
		}
	}
	return 0;
}
int blrScriptVM::getInstCount(){return pinst;}
void blrScriptVM::vmRunFunction(const char *fncnym)
{
	int nymhash=getHash(fncnym),cur=0;pinst=0;
	for(int i=0;i<fncnt;++i)if(nymhash==fncent[i].hash){cur=fncent[i].pos;break;}
	while(inst[cur].id!=0xFE)
	{
		int jmp=0;++pinst;
		//printf("@offset %d, instId 0x%x\n",cur,inst[cur].id);
		rr[100].r()=0.01667;
		switch(inst[cur].id)
		{
			case 0x00:
				//no-op
			break;
			case 0xFF:
				//no-op
			break;
			case 0x01:
				callStk.push(fetchData(inst[cur].para1));
			break;
			case 0x02:
				callFnc(inst[cur].para1.fnc);
			break;
			case 0x03:
				fetchData(inst[cur].para1,true)=fetchData(inst[cur].para2);
			break;
			case 0x04:
				fetchData(inst[cur].para1,true)+=fetchData(inst[cur].para2);
			break;
			case 0x05:
				fetchData(inst[cur].para1,true)-=fetchData(inst[cur].para2);
			break;
			case 0x06:
				fetchData(inst[cur].para1,true)*=fetchData(inst[cur].para2);
			break;
			case 0x07:
				fetchData(inst[cur].para1,true)/=fetchData(inst[cur].para2);
			break;
			case 0x08:
				fetchData(inst[cur].para1,true)%=fetchData(inst[cur].para2);
			break;
			case 0x09:
				fetchData(inst[cur].para1,true)&=fetchData(inst[cur].para2);
			break;
			case 0x0A:
				fetchData(inst[cur].para1,true)|=fetchData(inst[cur].para2);
			break;
			case 0x0B:
				fetchData(inst[cur].para1,true)^=fetchData(inst[cur].para2);
			break;
			case 0x0C:
				fetchData(inst[cur].para1,true)=~fetchData(inst[cur].para1,true);
			break;
			case 0x0D:
				++fetchData(inst[cur].para1,true);
			break;
			case 0x0E:
				--fetchData(inst[cur].para1,true);
			break;
			case 0x0F:
				callStk.clear();
			break;
			case 0x11:
				switch (fetchData(inst[cur].para1,true).type)
				{
					case 0:
					fetchData(inst[cur].para1,true)=
					fetchData(inst[cur].para1,true).ltz()?ione:izero;
					break;
					case 1:
					fetchData(inst[cur].para1,true)=
					fetchData(inst[cur].para1,true).ltz()?rone:rzero;
					break;
				}
			break;
			case 0x12:
				switch (fetchData(inst[cur].para1,true).type)
				{
					case 0:
					fetchData(inst[cur].para1,true)=
					fetchData(inst[cur].para1,true).elz()?ione:izero;
					break;
					case 1:
					fetchData(inst[cur].para1,true)=
					fetchData(inst[cur].para1,true).elz()?rone:rzero;
					break;
				}
			break;
			case 0x13:
				switch (fetchData(inst[cur].para1,true).type)
				{
					case 0:
					fetchData(inst[cur].para1,true)=
					fetchData(inst[cur].para1,true).gtz()?ione:izero;
					break;
					case 1:
					fetchData(inst[cur].para1,true)=
					fetchData(inst[cur].para1,true).gtz()?rone:rzero;
					break;
				}
			break;
			case 0x14:
				switch (fetchData(inst[cur].para1,true).type)
				{
					case 0:
					fetchData(inst[cur].para1,true)=
					fetchData(inst[cur].para1,true).egz()?ione:izero;
					break;
					case 1:
					fetchData(inst[cur].para1,true)=
					fetchData(inst[cur].para1,true).egz()?rone:rzero;
					break;
				}
			break;
			case 0x15:
				switch (fetchData(inst[cur].para1,true).type)
				{
					case 0:
					fetchData(inst[cur].para1,true)=
					fetchData(inst[cur].para1,true).eqz()?ione:izero;
					break;
					case 1:
					fetchData(inst[cur].para1,true)=
					fetchData(inst[cur].para1,true).eqz()?rone:rzero;
					break;
				}
			break;
			case 0x16:
				switch (fetchData(inst[cur].para1,true).type)
				{
					case 0:
					fetchData(inst[cur].para1,true)=
					fetchData(inst[cur].para1,true).nez()?ione:izero;
					break;
					case 1:
					fetchData(inst[cur].para1,true)=
					fetchData(inst[cur].para1,true).nez()?rone:rzero;
					break;
				}
			break;
			case 0x21:
				cur=fetchData(inst[cur].para1).i();
				jmp=1;
			break;
			case 0x22:
				if(fetchData(inst[cur].para1).eqz())
					cur=fetchData(inst[cur].para2).i(),jmp=1;
			break;
			case 0x23:
				if(fetchData(inst[cur].para1).nez())
					cur=fetchData(inst[cur].para2).i(),jmp=1;
			break;
		}
		if(!jmp)++cur;
	}
}
void blrScriptVM::vmSetRetValf(double v){rr[102].r()=v;}
void blrScriptVM::vmSetRetVald(int v){ir[100].i()=v;}
void blrScriptVM::vmInit(unsigned int seed)
{
	for(int i=0;i<103;++i)rr[i].type=1;
	for(int i=0;i<101;++i)ir[i].type=0;
	for(int i=0;i<10000;++i)ra[i].type=1,ia[i].type=0;
	re=new smRandomEngine;
	re->setSeed(seed);
}
void blrScriptVM::vmDeinit()
{
	delete re;
}