[39335f]: Compiler.cpp Maximize Restore History

Download this file

Compiler.cpp    401 lines (359 with data), 7.7 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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
// Compiler.cpp: implementation of the Compiler class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "chitem.h"
#include "Compiler.h"
#include <fcntl.h>
#include <sys/stat.h>
#include <assert.h>
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
Compiler::Compiler(int arg_log)
{
logging = arg_log;
flog = 0;
fhandle = 0;
fout = 0;
buffer = 0;
bpoi = 0;
valid = 0;
state = 0;
ObjectFieldsCount = 7;
MaxObjectNesting = 5;
ExtraParametersCount = 0;
HasAdditionalRect = false;
HasTriggerPoint = false;
if (pst_compatible_var())
{
ObjectFieldsCount += 2;
HasTriggerPoint = true;
HasAdditionalRect = true;
}
if (has_xpvar())
{
HasAdditionalRect = true;
}
if (iwd2_structures())
{
ObjectFieldsCount++;
ExtraParametersCount += 2;
}
}
Compiler::~Compiler()
{
CloseAll();
if (buffer) free(buffer);
}
void Compiler::CloseAll()
{
if (flog) close(flog);
flog = 0;
if (fhandle) close(fhandle);
fhandle = 0;
if (fout) close(fout);
fout = 0;
bpoi = 0;
valid = 0;
}
bool Compiler::Compile(CString filepath, CString output)
{
linecount=1;
bpoi = 0;
valid = 0;
if (logging && !flog)
{
unlink(WEIDU_LOG); //removing the logfile
flog = open(WEIDU_LOG, O_RDWR|O_CREAT|O_TRUNC, S_IWRITE|S_IREAD);
if (flog<0) flog = 0;
}
if (fhandle) abort();
fhandle=open(filepath,O_RDONLY|O_BINARY,S_IREAD);
if (fhandle<=0)
{
return true;
}
if (fout) abort();
fout = open(output, O_RDWR|O_CREAT|O_TRUNC, S_IWRITE|S_IREAD);
if (fout<=0)
{
return true;
}
//output the script
state = TA_IF;
WriteString("SC\n");
//ok we have all the files now
while (!ProcessLine());
WriteString("SC\n");
CloseAll();
return false;
}
//different from the decompiler readline, this one ignores // characters
void Compiler::ReadLine(int maxlength)
{
int i;
bool comment;
if (!buffer)
{
buffer = (char *) malloc(READ_BUFFERLENGTH);
valid = 0;
bpoi = 0;
}
assert(maxlength<=READ_BUFFERLENGTH);
line.Empty();
if (!buffer)
{
return;
}
retry:
if(valid<bpoi)
{
return;
}
if (bpoi+maxlength>valid)
{
memmove(buffer,buffer+bpoi, valid-bpoi);
valid-=bpoi;
buffer[valid]=0;
bpoi=0;
}
valid += read(fhandle, buffer+valid, READ_BUFFERLENGTH-valid);
if (valid<1)
{
return;
}
if (valid<maxlength)
{
maxlength=valid;
}
comment=false;
for(i=bpoi;i<bpoi+valid;i++)
{
if (buffer[i]=='\r' || buffer[i]=='\n')
{
buffer[i]=0;
comment=false;
break;
}
if (buffer[i]=='/' && (i+1<bpoi+valid) && buffer[i+1]=='/')
{
buffer[i]=0;
buffer[i+1]=0;
comment=true;
continue;
}
if(comment) buffer[i]=0;
}
//This conversion always sets the linelength to maxlength, which is wrong
line = CString(buffer+bpoi, maxlength);
line.ReleaseBuffer();//correcting the line value
bpoi=i+1;
line.TrimLeft();
line.TrimRight();
if (line.IsEmpty())
{
goto retry;
}
}
void Compiler::WriteString(CString tmp)
{
write(fout,tmp, tmp.GetLength());
}
void Compiler::WriteError(CString out)
{
CString tmp;
if(flog)
{
tmp.Format("%d: %s\r\n", linecount, out);
write(flog, tmp, tmp.GetLength());
}
}
void Compiler::WriteObject(object &ob)
{
int i;
CString tmp;
WriteString("OB\n");
for(i=0;i<ObjectFieldsCount+MaxObjectNesting;i++)
{
tmp.Format("%d ", ob.bytes[i]);
WriteString(tmp);
}
if(HasAdditionalRect)
{
tmp.Format("%d.%d.%d.%d ",ob.area[0],ob.area[1],ob.area[2],ob.area[3]);
WriteString(tmp);
}
tmp.Format("\"%s\"",ob.var);
WriteString(tmp);
for(i=0;i<ExtraParametersCount;i++)
{
tmp.Format("%d ", ob.bytes2[i]);
WriteString(tmp);
}
//TODO write object
WriteString("OB\n");
}
bool Compiler::HandleAction()
{
int i;
int err;
CString tmp;
action action;
ReadLine(1024);
if (line.CompareNoCase("END"))
{
err = compile_action(line, action, false);
tmp.Format("AC\n%d", action.opcode);
WriteString(tmp);
for(i=0;i<3;i++)
{
WriteObject(action.obj[i]);
}
for(i=0;i<5;i++)
{
//no space before var1
if(i!=4)
{
tmp.Format("%d ", action.bytes[i]);
}
else
{
tmp.Format("%d", action.bytes[i]);
}
WriteString(tmp);
}
tmp.Format("\"%s\" \"%s\" ", action.var1, action.var2);
WriteString(tmp);
//TODO output the action
//
WriteString("AC\n");
return false;
}
WriteString("RE\nRS\nCR\n");
state = TA_IF;
return false;
}
bool Compiler::HandleResponse()
{
CString num;
int value;
ReadLine(1024);
if(num_or==-1)
{
num_or=0;
WriteString("RS\n");
}
if(line.Left(10).CompareNoCase("RESPONSE #"))
{
return true;
}
num = line.Mid(10);
value=-1;
sscanf(num,"%d", &value);
if (value<1 || value>100)
{
WriteError("Invalid response #");
}
WriteString("RE\n"+num);
state=TA_ACTION;
return false;
}
bool Compiler::HandleTrigger()
{
int err;
CString tmp;
trigger trigger;
ReadLine(1024);
if(line.CompareNoCase("THEN"))
{
if(num_or==-1)
{
WriteString("CO\n");
num_or=0;
}
err = compile_trigger(line, trigger);
if (err)
{
//TODO use WriteError to explain the error
//
}
if((trigger.opcode&0x3fff)==TR_OR)
{
if (num_or)
{
WriteError("Nested OR in trigger block");
}
num_or = trigger.bytes[0];
} else if (num_or) num_or--;
WriteString("TR\n");
//TODO output the trigger
//
tmp.Format("%d %d %d %d %d ",
trigger.opcode, trigger.bytes[0], trigger.negate,
trigger.bytes[1], trigger.bytes[2]);
WriteString(tmp);
if(HasTriggerPoint)
{
tmp.Format("[%d,%d] ", trigger.bytes[3],trigger.bytes[4]);
WriteString(tmp);
}
tmp.Format("\"%s\" \"%s\" ", trigger.var1, trigger.var2);
WriteString(tmp);
WriteObject(trigger.trobj);
WriteString("TR\n");
return false;
}
if (num_or<0)
{
//there was nothing in the trigger block (IF/THEN without any triggers)
WriteError("Missing trigger block");
state = TA_RESPONSE; //this is just a guessed state we are already off track
return true;
}
//at the end of the trigger block, we got incomplete or
if (num_or)
{
WriteError("Incomplete OR in trigger block");
}
//output the condition closer marker
WriteString("CO\n");
state = TA_RESPONSE;
num_or=-1;
return false;
}
bool Compiler::ProcessLine()
{
switch(state)
{
case TA_IF:
ReadLine(10);
if(line.CompareNoCase("IF")) return true;
state=TA_TRIGGER;
WriteString("CR\n");
num_or=-1;
break;
case TA_TRIGGER:
if (HandleTrigger()) return true;
break;
case TA_ACTION:
if (HandleAction()) return true;
break;
case TA_RESPONSE:
if (HandleResponse())
{
if(line.CompareNoCase("END"))
{
WriteString("RE\nCR\n");
state = TA_IF;
} else return true;
}
break;
}
return false;
}