summaryrefslogtreecommitdiffstatshomepage
path: root/plugins/cheat/init.lua
blob: ea6f4bba3fb4cc25728075644e4d98d11c6b5721 (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
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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
-- license:BSD-3-Clause
-- copyright-holders:Carl
--
-- json cheat file format
-- [{
--   "desc": "text",
--   "parameter": {
--     "min": "minval(0)",
--     "max": "maxval(numitems)",
--     "step": "stepval(1)",
--     "item" [{
--       "value": "itemval(index*stepval+minval)",
--       "text": "text"
--     }, 
--     ... ]
--   },
--   "space": {
--     "varname": {
--       "tag": "tag",
--       "type": "program|data|io"
--     },
--     ...
--   },
--   "screen": {
--     "varname": "tag",
--     ...
--   },
--   "region": {
--     "varname": "tag",
--     ...
--   },
--   "ram": {
--     "varname": "tag",
--     ...
--   },
--   "script": {
--     "on|off|run|change": "script",
--      ...
--   },
--   "comment": "text"
-- },
-- ... ]
--
-- Scripts are lua scripts with a limited api. Most library functions are unavailable.
-- Like the XML cheats, param is the current parameter value and variables are shared between scripts within a cheat
-- Differences from XML cheats:
-- - actions are only one line which include the entire script
-- - "condexpr" is replaced with lua control statements (if-then-else-end)
-- - variables are only limited by the limits of the lua interperter, you can have strings and tables
-- - the address spaces in the "space" blocks are accessible to the script if included,
--      same with regions (the "m" space in debug expr)
-- - frame is replaced by screen:frame_number() so if you use frame a screen needs to be in the device section
-- - output is a function and argindex isn't supported, output args need to be explicit and a screen device
--      must be provided

local exports = {}
exports.name = "cheat"
exports.version = "0.0.1"
exports.description = "Cheat plugin"
exports.license = "The BSD 3-Clause License"
exports.author = { name = "Carl" }

local cheat = exports

function cheat.startplugin()
	local cheats = {}
	local output = {}
	local line = 0
	local start_time = 0
	local stop = true

	local function load_cheats()
		local filename = emu.romname()
		local json = require("json")
		local newcheats = {}
		local path = manager:machine():options().entries.cheatpath:value():gsub("([^;]+)", "%1;%1/cheat")
		local file = emu.file(path, 1)
		if emu.softname() ~= "" then
			for name, image in pairs(manager:machine().images) do
				if image:exists() and image:software_list_name() ~= "" then
					filename = image:software_list_name() .. "/" .. emu.softname()
				end
			end
		end
		function add(addcheats)
			if not next(newcheats) then
				newcheats = addcheats
			else
				for num, cheat in pairs(addcheats) do
					newcheats[#newcheats + 1] = cheat
				end
			end
		end
		local ret = file:open(filename .. ".json")
		while not ret do
			add(json.parse(file:read(file:size())))
			ret = file:open_next()
		end
		local xml = require("cheat/xml_conv")
		ret = file:open(filename .. ".xml")
		while not ret do
			add(xml.conv_cheat(file:read(file:size())))
			ret = file:open_next()
		end
		return newcheats
	end

	local function draw_text(screen, x, y, color, form, ...)
		local str = form:format(...)
		if y == "auto" then
			y = line
			line = line + 1
		end
		if not screen then
			emu.print_verbose("draw_text: invalid screen")
			return
		end
		if type(x) == "string" then
			y = y * manager:machine():ui():get_line_height()
		end
		output[#output + 1] = { type = "text", scr = screen, x = x, y = y, str = str, color = color }
	end

	local function draw_line(screen, x1, y1, x2, y2, color)
		if not screen then
			emu.print_verbose("draw_line: invalid screen")
			return
		end
		output[#output + 1] = { type = "line", scr = screen, x1 = x1, x2 = x2, y1 = y1, y2 = y2, color = color }
	end

	local function draw_box(screen, x1, y1, x2, y2, bgcolor, linecolor)
		if not screen then
			emu.print_verbose("draw_box: invalid screen")
			return
		end
		output[#output + 1] = { type = "box", scr = screen, x1 = x1, x2 = x2, y1 = y1, y2 = y2, bgcolor = bgcolor, linecolor = linecolor }
	end

	local function tobcd(val)
		local result = 0
		local shift = 0
		while val ~= 0 do
			result = result + ((val % 10) << shift)
			val = val / 10
			shift = shift + 4
		end
		return result
	end

	local function frombcd(val)
		local result = 0
		local mul = 1
		while val ~= 0 do
			result = result + ((val % 16) * mul)
			val = val >> 4
			mul = mul * 10
		end
		return result
	end

	local function time()
		return emu.time() - start_time
	end


	local function parse_cheat(cheat)
		cheat.cheat_env = { draw_text = draw_text,
				    draw_line = draw_line,
				    draw_box = draw_box,
				    tobcd = tobcd,
				    frombcd = frombcd,
				    pairs = pairs,
				    ipairs = ipairs,
				    time = time,
			    	    table = 
				    { insert = table.insert,
			    	      remove = table.remove } }
		cheat.enabled = false
		-- verify scripts are valid first
		if not cheat.script then
			return
		end
		for name, script in pairs(cheat.script) do
			script, err = load(script, cheat.desc .. name, "t", cheat.cheat_env)
			if not script then
				emu.print_verbose("error loading cheat script: " .. cheat.desc .. " " .. err)
				cheat = { desc = cheat.desc .. " error" }
				return
			end
			cheat.script[name] = script
		end
		-- initialize temp[0-9] for backward compatbility reasons
		for i = 0, 9 do
			cheat.cheat_env["temp" .. i] = 0
		end
		if cheat.space then
			for name, space in pairs(cheat.space) do
				local cpu, mem
				cpu = manager:machine().devices[space.tag]
				if not cpu then
					emu.print_verbose("error loading cheat script: " .. cheat.desc)
					cheat = { desc = cheat.desc .. " error" }
					return
				end
				if space.type then
					mem = cpu.spaces[space.type]
				else
					mem = cpu.spaces["program"]
				end
				if not mem then
					emu.print_verbose("error loading cheat script: " .. cheat.desc)
					cheat = { desc = cheat.desc .. " error" }
					return
				end
				cheat.cheat_env[name] = mem
			end
		end
		if cheat.screen then
			for name, screen in pairs(cheat.screen) do
				local scr
				scr = manager:machine().screens[screen]
				if not scr then
					local tag
					tag, scr = next(manager:machine().screens) -- get any screen
				end
				cheat.cheat_env[name] = scr
			end
		end
		if cheat.region then
			for name, region in pairs(cheat.region) do
				local mem 
				mem = manager:machine():memory().regions[region]
				if not mem then
					emu.print_verbose("error loading cheat script: " .. cheat.desc)
					cheat = { desc = cheat.desc .. " error" }
					return
				end
				cheat.cheat_env[name] = mem
			end
		end
		if cheat.ram then
			for name, ram in pairs(cheat.ram) do
				local ram
				ram = manager:machine().devices[ram]
				if not ram then
					emu.print_verbose("error loading cheat script: " .. cheat.desc)
					cheat = { desc = cheat.desc .. " error" }
					return
				end
				cheat.cheat_env[name] = emu.item(ram.items["0/pointer"])
			end
		end
		local param = cheat.parameter
		if not param then
			return
		end
		param.min = tonumber(param.min) or 0
		param.max = tonumber(param.max) or #param.item
		param.step = tonumber(param.step) or 1
		if param.item then
			for count, item in pairs(param.item) do
				if not item.value then
					item.value = (count * param.step) + param.min
				else
					item.value = tonumber(item.value)
				end
			end
			param.last = #param.item
		else
			param.last = ((param.max - param.min) / param.step) + 1
		end
		param.index = 0
		param.value = param.min
		cheat.cheat_env.param = param.min
	end

	local function menu_populate()
		local menu = {}
		for num, cheat in ipairs(cheats) do
			menu[num] = {}
			menu[num][1] = cheat.desc
			if not cheat.parameter then
				if not cheat.script then
					if cheat.desc == "" then
						menu[num][1] = "---"
					end
					menu[num][2] = ""
					menu[num][3] = "off"
				elseif not cheat.script.run and not cheat.script.off then
					menu[num][2] = "Set"
					menu[num][3] = 0
				else
					if cheat.enabled then
						menu[num][2] = "On"
						menu[num][3] = "l"
					else
						menu[num][2] = "Off"
						menu[num][3] = "r"
					end
				end
			else
				if cheat.parameter.index == 0 then
					if not cheat.script.run and not cheat.script.off then
						menu[num][2] = "Set"
					else
						menu[num][2] = "Off"
					end
					menu[num][3] = "r"
				else
					if cheat.parameter.item then
						menu[num][2] = cheat.parameter.item[cheat.parameter.index].text
					else
						menu[num][2] = cheat.parameter.value
					end
					menu[num][3] = "l"
					if cheat.parameter.index < cheat.parameter.last then
						menu[num][3] = "lr"
					end
				end
			end
		end
		menu[#menu + 1] = {"---", "", 0}
		menu[#menu + 1] = {"Reset All", "", 0}
		menu[#menu + 1] = {"Reload All", "", 0}
		return menu
	end

	local function menu_callback(index, event)
		if index > #cheats and event == "select" then
			index = index - #cheats
			if index == 2 then
				for num, cheat in pairs(cheats) do
					if cheat.script and cheat.script.off then
						cheat.script.off()
					end
					cheat.enabled = false
					if cheat.parameter then
						cheat.parameter.value = cheat.parameter.min
						cheat.parameter.index = 0
					end
				end
			elseif index == 3 then
				for num, cheat in pairs(cheats) do 
					if cheat.script and cheat.script.off then
						cheat.script.off()
					end
				end
				cheats = load_cheats()
				for num, cheat in pairs(cheats) do
					parse_cheat(cheat)
				end
			end
			return true
		end

		local function param_calc(param)
			if param.item then
				if not param.item[param.index] then -- uh oh
					param.index = 1
				end
				param.value = param.item[param.index].value
				return
			end
			param.value = param.min + (param.step * (param.index - 1))
			if param.value > param.max then
				param.value = param.max
			end
		end

		local cheat = cheats[index]
		if not cheat then
			return false
		end
		if event == "up" or event == "down" or event == "comment" then
			if cheat.comment then
				manager:machine():popmessage("Cheat Comment:\n" .. cheat.comment)
			end
		elseif event == "left" then
			if cheat.parameter then
				local param = cheat.parameter
				if param.index == 1 then
					param.index = 0
					cheat.enabled = false
					cheat.cheat_env.param = param.min
					if cheat.script.off then
						cheat.script.off()
					end
					return true
				elseif param.index == 0 then
					return false
				end
				param.index = param.index - 1
				param_calc(param)
				cheat.cheat_env.param = param.value
				if cheat.script.change and (cheat.script.run or cheat.script.off) then
					cheat.script.change()
				end
				return true
			else
				if not cheat.script.run and not cheat.script.off then
					return false
				end
				if cheat.enabled then
					cheat.enabled = false
					if cheat.script.off then
						cheat.script.off()
					end
					return true
				end
				return false
			end
		elseif event == "right" then
			if cheat.parameter then
				local param = cheat.parameter
				if param.index == 0 then
					cheat.enabled = true
					if cheat.script.on then
						cheat.script.on()
					end
				elseif param.index == param.last then
					return false
				end
				param.index = param.index + 1
				param_calc(param)
				cheat.cheat_env.param = param.value
				if cheat.script.change and (cheat.script.run or cheat.script.off) then
					cheat.script.change()
				end
				return true
			else
				if not cheat.script.run and not cheat.script.off then
					return false
				end
				if not cheat.enabled then
					cheat.enabled = true
					if cheat.script.on then
						cheat.script.on()
					end
					return true
				end
				return false
			end
		elseif event == "select" then
			if not cheat.script.run and not cheat.script.off then
				if cheat.parameter and cheat.script.change and cheat.parameter.index ~= 0 then
					param_calc(cheat.parameter)
					cheat.cheat_env.param = cheat.parameter.value
					cheat.script.change()
					local subtext
					if cheat.parameter.item then
						subtext = cheat.parameter.item[cheat.parameter.index]
					else
						subtext = cheat.parameter.value
					end
					manager:machine():popmessage("Activated: " .. cheat.desc .. " = " .. subtext)
				elseif not cheat.parameter and cheat.script.on then
					cheat.script.on()
					manager:machine():popmessage("Activated: " .. cheat.desc)
				end
			end
		end
		return false
	end

	emu.register_menu(function(index, event)
				return menu_callback(index, event)
			  end,
			  function()
				return menu_populate()
			  end, "Cheat")

	emu.register_start(function()
		if not stop then
			return
		end
		stop = false
		start_time = emu.time()
		cheats = load_cheats()
		for num, cheat in pairs(cheats) do
			parse_cheat(cheat)
		end
	end)

	emu.register_stop(function()
		stop = true
	end)

	emu.register_frame(function()
		if stop then
			return
		end
		for num, cheat in pairs(cheats) do
			if cheat.enabled and cheat.script.run then
				cheat.script.run()
			end
		end
	end)

	emu.register_frame_done(function()
		if stop then
			return
		end
		line = 0
		for num, draw in pairs(output) do
			if draw.type == "text" then
				if not draw.color then
					draw.scr:draw_text(draw.x, draw.y, draw.str)
				else
					draw.scr:draw_text(draw.x, draw.y, draw.str, draw.color)
				end
			elseif draw.type == "line" then
				draw.scr:draw_line(draw.x1, draw.y1, draw.x2, draw.y2, draw.color)
			elseif draw.type == "box" then
				draw.scr:draw_box(draw.x1, draw.y1, draw.x2, draw.y2, draw.bgcolor, draw.linecolor)
			end
		end
		output = {}
	end)

	local ce = {}

	-- interface to script cheat engine
	function ce.inject(newcheat)
		cheats[#cheats + 1] = newcheat
		parse_cheat(newcheat)
		manager:machine():popmessage(newcheat.desc .. " added")
	end

	function ce.dump(index)
		cheat = cheats[index]
		if cheat then
			for k, v in pairs(cheat.cheat_env) do
				emu.print_verbose(k, v)
			end
		end
	end

	function ce.list()
		for num, cheat in pairs(cheats) do
			emu.print_verbose(num, cheat.desc)
		end
	end

	_G.ce = ce

end

return exports