summaryrefslogtreecommitdiffstatshomepage
path: root/plugins/inputmacro/inputmacro_persist.lua
blob: b8670800fa48e9a16b0d6a3c066bba84e295463e (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
-- license:BSD-3-Clause
-- copyright-holders:Vas Crabb


-- Helpers

local function settings_path()
	return manager.machine.options.entries.homepath:value():match('([^;]+)') .. '/inputmacro'
end

local function settings_filename()
	return emu.romname() .. '.cfg'
end

local function make_macro(setting)
	if (setting.name == nil) or (setting.binding == nil) or (setting.earlycancel == nil) or (setting.loop == nil) or (setting.steps == nil) then
		return nil
	end

	local result = {
		name = setting.name,
		binding = manager.machine.input:seq_from_tokens(setting.binding),
		bindingcfg = setting.binding,
		earlycancel = setting.earlycancel,
		loop = setting.loop,
		steps = { } }

	local ioport = manager.machine.ioport
	for i, step in ipairs(setting.steps) do
		if step.inputs and step.delay and step.duration then
			local s = {
				inputs = { },
				delay = step.delay,
				duration = step.duration }
			for j, input in ipairs(step.inputs) do
				if input.port and input.mask and input.type then
					local ipt = {
						port = input.port,
						mask = input.mask,
						type = ioport:token_to_input_type(input.type) }
					local port = ioport.ports[input.port]
					if port then
						local field = port:field(input.mask)
						if field and (field.type == ipt.type) then
							ipt.field = field
						end
					end
					table.insert(s.inputs, ipt)
				end
			end
			if #s.inputs > 0 then
				table.insert(result.steps, s)
			end
		end
	end

	if result.loop > #result.steps then
		result.loop = -1
	end

	if #result.steps > 0 then
		return result
	else
		return nil
	end
end

local function make_settings(macros)
	local input = manager.machine.input
	local ioport = manager.machine.ioport
	local result = { }
	for i, macro in ipairs(macros) do
		local m = {
			name = macro.name,
			binding = macro.bindingcfg,
			earlycancel = macro.earlycancel,
			loop = macro.loop,
			steps = { } }
		table.insert(result, m)
		for j, step in ipairs(macro.steps) do
			local s = {
				inputs = { },
				delay = step.delay,
				duration = step.duration }
			table.insert(m.steps, s)
			for k, input in ipairs(step.inputs) do
				local b = {
					port = input.port,
					mask = input.mask,
					type = ioport:input_type_to_token(input.type) }
				table.insert(s.inputs, b)
			end
		end
	end
	return result
end


-- Entry points

local lib = { }

function lib:load_settings()
	local filename = settings_path() .. '/' .. settings_filename()
	local file = io.open(filename, 'r')
	if not file then
		return { }
	end
	local json = require('json')
	local settings = json.parse(file:read('a'))
	file:close()
	if not settings then
		emu.print_error(string.format('Error loading input macros: error parsing file "%s" as JSON', filename))
		return { }
	end

	result = { }
	for index, setting in ipairs(settings) do
		local macro = make_macro(setting)
		if macro then
			table.insert(result, macro)
		end
	end
	return result
end

function lib:save_settings(macros)
	local path = settings_path()
	local stat = lfs.attributes(path)
	if stat and (stat.mode ~= 'directory') then
		emu.print_error(string.format('Error saving input macros: "%s" is not a directory', path))
		return
	end
	local filename = path .. '/' .. settings_filename()

	if #macros == 0 then
		os.remove(filename)
		return
	elseif not stat then
		lfs.mkdir(path)
	end

	local json = require('json')
	local settings = make_settings(macros)
	local text = json.stringify(settings, { indent = true })
	local file = io.open(filename, 'w')
	if not file then
		emu.print_error(string.format('Error saving input macros: error opening file "%s" for writing', filename))
		return
	end
	file:write(text)
	file:close()
end

return lib