-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgamepads_merge.script
More file actions
223 lines (182 loc) · 5.66 KB
/
Copy pathgamepads_merge.script
File metadata and controls
223 lines (182 loc) · 5.66 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
print("This is a utility project to help with merging two *.gamepads files together.")
print("Make sure the two gamepads files are located in the root of the project (next to game.project)")
print("Set their paths in gamepads_merge.script")
print("")
print("Running utility...")
--Raw file of built-in gamepad mappings:
--[[
https://raw.githubusercontent.com/defold/defold/refs/heads/dev/engine/engine/content/builtins/input/default.gamepads
--]]
--------
local original = "8bitskull_master_gamepads.gamepads"
local additions = "default.gamepads"
--------
--FUNCTIONS
local function load_file(file_path)
local file = io.open(file_path, "r")
if not file then
return nil
end
local result = {}
if file then
-- Iterate over the lines using io.lines iterator
local device
local platform
for line in file:lines() do
-- Process each line here
if string.find(line, "device:") then
device = line
device = string.gsub(device, "%s+", " ")
device = string.gsub(device, "device:", "")
device = string.gsub(device, '"', "")
end
if string.find(line, "platform:") then
platform = line
platform = string.gsub(platform, "%s+", " ")
platform = string.gsub(platform, "platform:", "")
platform = string.gsub(platform, '"', "")
end
if device and platform then
table.insert(result, {device = device, platform = platform})
device = nil
platform = nil
end
end
-- Close the file when done
file:close()
else
print("Failed to open file:", file_path)
end
return result
end
local function find_new_additions(t1, t2)
local new_additions = {}
for _, entry2 in ipairs(t2) do
local found = false
for _, entry1 in ipairs(t1) do
if entry1.platform == entry2.platform and entry1.device == entry2.device then
found = true
break
end
end
if not found then
table.insert(new_additions, { platform = entry2.platform, device = entry2.device })
end
end
return new_additions
end
local function find_duplicates(tbl)
local seen = {}
local duplicates = {}
for _, entry in ipairs(tbl) do
local key = entry.platform .. ":" .. entry.device
if seen[key] then
table.insert(duplicates, entry)
else
seen[key] = true
end
end
return duplicates
end
-- Extract driver blocks and return structured tables
local function parse_driver_blocks(file_path)
local file = io.open(file_path, "r")
if not file then return {} end
local blocks = {}
local current_lines = {}
local inside_driver = false
local trimmed
local i = 0
for line in file:lines() do
i = i + 1
trimmed = line:match("^%s*(.-)%s*$")
if trimmed == "driver" then
inside_driver = true
current_lines = { line }
elseif trimmed:match("^driver") then
print("Malformed driver block line: ", line, "File: ", file_path, "Line number: ", i)
elseif inside_driver then
table.insert(current_lines, line)
if line:match("^%s*}") then
-- Join and parse full block
local block_str = table.concat(current_lines, "\n")
local device = block_str:match('device:%s*"([^"]+)"')
local platform = block_str:match('platform:%s*"([^"]+)"')
if device and platform then
local key = platform .. ":" .. device
local maps = {}
for map_str in block_str:gmatch("map%s*%b{}") do
-- Remove whitespace, collapse spacing
map_str = map_str:gsub("%s+", " "):gsub("^%s*", ""):gsub("%s*$", "")
table.insert(maps, map_str)
end
table.sort(maps) -- Normalize order
blocks[key] = { device = device, platform = platform, maps = maps }
end
inside_driver = false
end
end
end
file:close()
return blocks
end
-- Compare blocks structurally, ignoring order/spacing
local function find_structural_differences(original_path, additions_path)
local original_blocks = parse_driver_blocks(original_path)
local additions_blocks = parse_driver_blocks(additions_path)
local differences_found = false
for key, original_block in pairs(original_blocks) do
local addition_block = additions_blocks[key]
if addition_block then
local orig_maps = table.concat(original_block.maps, "\n")
local add_maps = table.concat(addition_block.maps, "\n")
if orig_maps ~= add_maps then
print("\nDifference found for:", key)
print("---- " .. original_path .. ": ----")
print(orig_maps)
print("---- " .. additions_path .. ": ----")
print(add_maps)
differences_found = true
end
end
end
if not differences_found then
print("No structural differences found in shared mappings.")
end
end
function init(self)
print("Comparing", original, "to", additions)
print("")
local original_output = load_file(original)
local additions_output = load_file(additions)
---
local things_we_need_to_add_to_builtins = find_new_additions(additions_output, original_output)
print("Mappings in our custom file that we should submit to builtins:")
if #things_we_need_to_add_to_builtins == 0 then
print("(None found!)")
else
pprint(things_we_need_to_add_to_builtins)
end
print("")
---
local new_additions = find_new_additions(original_output, additions_output)
print("New gamepads entries that need to be added to your custom file:")
if #new_additions == 0 then
print("(None found!)")
else
pprint(new_additions)
end
print("")
---
local duplicates = find_duplicates(original_output)
print("Checking for duplicates in ", original)
if #duplicates == 0 then
print("(None found!)")
else
pprint(duplicates)
end
print("")
---BEGIN ACTUAL COMPARISON OF MAPPINGS
print("Comparing shared mappings structurally...")
find_structural_differences(original, additions)
end