-
Notifications
You must be signed in to change notification settings - Fork 0
/
CheatParseClass1.vb
506 lines (438 loc) · 18.4 KB
/
CheatParseClass1.vb
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
Imports System.IO
Imports System.Text
Public Class CheatParseClass
Public Enum ECheatOptionType
ECheatOptionType_UnsignedByte = 0
ECheatOptionType_SignedByte
ECheatOptionType_Enum
ECheatOptionType_INVALID
End Enum
Public Enum EProgramType
EProgramType_Off = 0
EProgramType_On
EProgramType_Frame
EProgramType_INVALID
End Enum
Dim sbConsole As StringBuilder = New StringBuilder()
Function ReadCheatLine(ByRef [out] As String, ByVal maxRead As UInteger, ByVal file As StreamReader, ByRef errorPos As UInteger) As Boolean
Dim comment As Boolean = False
Dim instring As Boolean = False
Dim c As Char
Dim linePos As UInteger = 0
maxRead -= 1
' read a character at a time
While True
c = ChrW(file.Read())
If file.EndOfStream Then
Exit While
End If
linePos += 1
' ignore space (outside of strings) cr and tab
If (Not instring AndAlso c = " "c) OrElse c = ChrW(13) OrElse c = ChrW(9) Then
Continue While
End If
' start / end of string
If c = "["c Then
If instring Then
errorPos = linePos
Return False
End If
instring = True
End If
If c = "]"c Then
If Not instring Then
errorPos = linePos
Return False
End If
instring = False
End If
' if we find a semicolon then ignore the rest of the line
If c = ";"c Then
comment = True
Continue While
End If
' lf terminates the line read
If c = ChrW(10) Then
Exit While
End If
' keep character we want
If Not comment AndAlso maxRead > 0 Then
[out] += c
maxRead -= 1
End If
End While
errorPos = 0
Return maxRead <> 0 AndAlso Not instring
End Function
Function ParseNumber(ByRef [out] As UInteger, ByVal [in] As String) As Integer
Dim val As UInteger = 0
Dim base As UInteger = 10
Dim pos As Integer = 0
If [in].Length = 0 Then
[out] = 0
Return pos
End If
' first character can be $ or % to set base
Dim c As Char = [in](0)
If c = "$"c OrElse c = "%"c Then
pos = pos + 1
If c = "$"c Then
base = 16
Else
base = 2
End If
End If
'For Each ch As Char In [in]
While (pos < [in].Length)
' get character and force lowercase
Dim ch As Char = [in](pos)
c = Char.ToLower(ch)
Dim tVal As Integer = "0123456789abcdef".IndexOf(c)
' valid hex characters
If tVal < 0 Then
Exit While
End If
pos += 1
val = (val * base) + tVal
End While
[out] = val
Return pos
End Function
Function GetStringLength(ByVal p As String) As UInteger
If p(0) <> "["c Then
Return 0
End If
Dim len As UInteger = 0
For Each c As Char In p.Substring(1)
If c = "]"c Then
Exit For
End If
len += 1
Next
Return len
End Function
Function FindParamType(ByRef pType As ECheatOptionType, ByVal pStr As String) As Integer
Dim nType As ECheatOptionType = ECheatOptionType.ECheatOptionType_INVALID
Dim c As Char = Char.ToLower(pStr(0))
Dim p As Integer = 1
Dim nSigned As Byte = &HFF
If c = "u"c OrElse c = "s"c Then
nSigned = If(c = "s"c, 1, 0)
c = Char.ToLower(pStr(1))
p += 1
End If
Select Case c
'Case "e"c
' If nSigned <> &HFF Then
' nType = ECheatOptionType_Enum
' End If
' Exit Select
Case "b"c
nType = If(nSigned = 1, ECheatOptionType.ECheatOptionType_SignedByte, ECheatOptionType.ECheatOptionType_UnsignedByte)
Exit Select
End Select
pType = nType
Return p
End Function
Function FindParamRange(ByRef pMin As Byte, ByRef pMax As Byte, ByRef pError As Boolean, ByVal pStr As String) As Integer
Dim bError As Boolean = False
Dim min As UInteger = 0, max As UInteger = &HFF
Dim p As Integer = 0
If pStr(0) = "{"c Then
p += 1
Dim num1, num2 As UInteger
p += ParseNumber(num1, pStr.Substring(p))
bError = pStr(p) <> "-"c
p += 1
If Not bError Then
p += ParseNumber(num2, pStr.Substring(p))
'p += 1
bError = pStr(p) <> "}"c
If Not bError Then
If num1 < num2 Then
min = num1
max = num2
Else
min = num2
max = num1
End If
End If
End If
End If
pMin = min
pMax = max
pError = bError
Return p
End Function
Function GetProgramType(ByVal pStr As String) As EProgramType
' these match EProgramType in order
Dim aType() As String = {"OFF", "ON", "FRAME"}
For n As UInteger = 0 To aType.Length '\ 4 - 1
If String.Compare(aType(n), pStr, True) = 0 Then
Return CType(n, EProgramType)
End If
Next
Return EProgramType.EProgramType_INVALID
End Function
Sub ShowErrorContext(ByVal pLine As String, ByVal nChar As Integer)
' show upto 16 characters before the bad character
Dim start As Integer = nChar - 16
If start < 0 Then
start = 0
End If
' show a max of 32 characters
sbConsole.AppendLine(pLine.Substring(start, Math.Min(32, pLine.Length - start - 1)))
' mark the bad character
For i As Integer = 1 To nChar - 1
sbConsole.AppendFormat(" ")
Next
sbConsole.AppendLine(String.Format("^"))
End Sub
Function ParseCheats(ByVal pFilename As String, ByRef pCheats As UInteger) As UInteger
Dim file As StreamReader
Dim szLine As String = String.Empty
Dim linePos As UInteger = 0
Dim inCheat As Boolean = False
Dim inProgram As Boolean = False
Dim nCheatDefined As UInteger = 0
Dim nErrors As UInteger = 0
Dim nCheats As UInteger = 0
sbConsole.Clear()
Try
file = New StreamReader(pFilename)
Catch ex As Exception
sbConsole.AppendLine(String.Format("Unable to open '{0}' for reading.", pFilename))
pCheats = 0
Return 0
End Try
While Not file.EndOfStream
Dim errorPos As UInteger
linePos += 1
szLine = String.Empty
' read one line stripping whitespace
If Not ReadCheatLine(szLine, 256, file, errorPos) Then
If errorPos <> 0 Then
sbConsole.AppendLine(String.Format("Line({0}:{1}): unexpected character, line skipped.", linePos, errorPos))
szLine = szLine.Substring(0, errorPos)
ShowErrorContext(szLine, errorPos)
nErrors += 1
Else
sbConsole.AppendLine(String.Format("Line({0}): too long or missing ], line skipped.", linePos))
End If
Continue While
End If
' skip blank lines
If szLine.Length = 0 Then
Continue While
End If
' different things valid in a cheat definition block
If inProgram Then
' on next option, program or cheat start, exit parsing program
If szLine(0) = "["c OrElse szLine(0) = ":"c OrElse szLine(0) = "#"c Then
inProgram = False
Else
' process line
'Dim p As String = szLine
Dim p As Integer = 0
'For Each c As Char In p
While (p < szLine.Length)
Dim c As Char = szLine(p)
p += 1
' get character
Dim ch As Char = c
' set address
If ch = "@"c Then
' check for low RAM writes, these are in 7800 address space
If szLine(p) = "<"c Then
p = p + 1
End If
Dim addr As UInteger
p += ParseNumber(addr, szLine.Substring(p)) ' + 1
'End If
' write to current address pointer
' can be multiple writes with comma separated list
ElseIf ch = "="c Then
Dim expectingData As Boolean = True
'For Each ch2 As Char In p
While (p < szLine.Length)
Dim ch2 As Char = szLine(p)
Dim bUnexpected As Boolean = False
' parameter write
If ch2 = "["c Then
bUnexpected = Not expectingData
errorPos = p + 1
Dim len As UInteger = GetStringLength(szLine.Substring(szLine.IndexOf(ch2, p)))
p += len + 2
expectingData = False
ElseIf ch2 = ","c Then
bUnexpected = expectingData
errorPos = p + 1 'szLine.IndexOf(ch2, p) + 1
p += 1
expectingData = True
Else
' otherwise assume constant write
bUnexpected = Not expectingData
errorPos = p + 1 'szLine.IndexOf(ch2, p) + 1
Dim val As UInteger
p += ParseNumber(val, szLine.Substring(p)) ' + 1
expectingData = False
End If
If bUnexpected Then
errorPos = p
sbConsole.AppendLine(String.Format("Line({0}:{1}): unexpected character in data list.", linePos, errorPos))
ShowErrorContext(szLine, errorPos)
nErrors += 1
End If
End While
'Next
Else
' invalid chatacter
errorPos = p 'szLine.IndexOf(ch, p)
sbConsole.AppendLine(String.Format("Line({0}:{1}): unexpected character in cheat block.", linePos, errorPos))
ShowErrorContext(szLine, errorPos)
nErrors += 1
End If
End While
Continue While
End If
End If
' cheat start
If szLine(0) = "#"c Then
' check the CRC(s) are OK
Dim numcrc As UInteger = 0
Dim crc As Integer = 0 + 1
While (crc < szLine.Length)
Dim crcno As UInteger
crc += ParseNumber(crcno, szLine.Substring(crc)) '+ 1
If crc < szLine.Length Then
If szLine(crc) <> ","c Then
errorPos = crc + 1
sbConsole.AppendLine(String.Format("Line({0}:{1}): unexpected character in CRC list.", linePos, errorPos))
ShowErrorContext(szLine, errorPos)
nErrors += 1
Exit While
End If
crc += 1
End If
numcrc += 1
End While
If numcrc = 0 Then
errorPos = 2
sbConsole.AppendLine(String.Format("Line({0}:{1}): CRC list empty.", linePos, errorPos))
ShowErrorContext(szLine, errorPos)
nErrors += 1
End If
nCheats += 1
'End If
' cheat option
ElseIf szLine(0) = "["c Then
' check the option line is not malformed
Dim len As UInteger = GetStringLength(szLine)
'If szLine(len + 2) <> ChrW(0) Then
' errorPos = len + 3
' sbConsole.AppendLine(String.Format("Line({0}:{1}): unexpected character.", linePos, errorPos))
' ShowErrorContext(szLine, errorPos)
' nErrors += 1
'End If
nCheatDefined = 0
'End If
' parameter
ElseIf szLine(0) = "?"c Then
' check parameter string is ok
Dim len As UInteger = GetStringLength(szLine.Substring(1))
If szLine(1) <> "["c Then
errorPos = 2
sbConsole.AppendLine(String.Format("Line({0}:{1}): option name missing ?[...], line skipped.", linePos, errorPos))
ShowErrorContext(szLine, errorPos)
nErrors += 1
Continue While
End If
' next is the type details, requires = first
'Dim p As String = szLine.Substring(len + 3)
Dim p As Integer = len + 3
If szLine(p) <> "="c Then
errorPos = len + 4
sbConsole.AppendLine(String.Format("Line({0}:{1}): parameter type missing.", linePos, errorPos))
ShowErrorContext(szLine, errorPos)
nErrors += 1
Continue While
End If
p += 1
' now actual type, can be --
' [U/S]B [un]signed byte
' E enum
errorPos = p + 1 ' possible error pos
Dim eType As ECheatOptionType
p += FindParamType(eType, szLine.Substring(p))
If eType = ECheatOptionType.ECheatOptionType_INVALID Then
sbConsole.AppendLine(String.Format("Line({0}:{1}): invalid parameter type.", linePos, errorPos))
ShowErrorContext(szLine, errorPos)
nErrors += 1
Continue While
End If
' if type is valid then check for further details
Select Case eType
' signed and unsigned bytes can have a range
Case ECheatOptionType.ECheatOptionType_UnsignedByte, ECheatOptionType.ECheatOptionType_SignedByte
Dim min, max As Byte
Dim bError As Boolean = False
p += FindParamRange(min, max, bError, szLine.Substring(p))
errorPos = p
If Not bError AndAlso p < szLine.Length > 0 Then
If bError OrElse szLine(p) <> ":"c Then
errorPos = szLine.IndexOf("="c, p) + 1
Else
Dim num As UInteger
p += ParseNumber(num, szLine.Substring(p))
If p < szLine.Length Then
errorPos = szLine.IndexOf("="c, p) + 1
End If
End If
End If
If bError Then
sbConsole.AppendLine(String.Format("Line({0}:{1}): invalid character in parameter range.", linePos, errorPos))
ShowErrorContext(szLine, errorPos)
nErrors += 1
End If
End Select
'End If
' actions
ElseIf szLine(0) = ":"c Then
' see if we have a valid cheat type
Dim eProgType As EProgramType = GetProgramType(szLine.Substring(1))
If eProgType = EProgramType.EProgramType_INVALID Then
errorPos = 2
sbConsole.AppendLine(String.Format("Line({0}:{1}): invalid cheat type.", linePos, errorPos))
ShowErrorContext(szLine, errorPos)
nErrors += 1
Continue While
End If
inProgram = True
' check it's not already defined
If (nCheatDefined And (1 << CInt(eProgType))) <> 0 Then
errorPos = 1
sbConsole.AppendLine(String.Format("Line({0}:{1}): cheat type is already defined for this option.", linePos, errorPos))
ShowErrorContext(szLine, errorPos)
nErrors += 1
Continue While
End If
nCheatDefined = nCheatDefined Or (1 << CInt(eProgType))
Else
errorPos = 1
sbConsole.AppendLine(String.Format("Line({0}:{1}): unexpected character.", linePos, errorPos))
ShowErrorContext(szLine, errorPos)
nErrors += 1
End If
End While
pCheats = nCheats
file.Close()
Return nErrors
End Function
Function ParseCheatFile(CheatFileName As String) As String
Dim pCheats As UInteger
Dim nErrors As UInteger = ParseCheats(CheatFileName, pCheats)
sbConsole.AppendLine(String.Format("Number of cheats: {0}", pCheats))
sbConsole.AppendLine(String.Format("Number of errors: {0}", nErrors))
Return sbConsole.ToString()
End Function
End Class