This repository has been archived by the owner on Jan 10, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfigloader.lua
195 lines (157 loc) · 4.71 KB
/
configloader.lua
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
--[[
Copyright 2019 Valeri Ochinski
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
]]--
local common = require 'common'
local json = require 'json'
local reader = common.makeReader()
-- Strip C-style comments away
-- This feature is disabled as currently used parser understand comments and give info on position
local function StripJSON(str)
if false then
return (str:gsub('/*.-*/', ''):gsub('//.-\n', ''))
else
return str
end
end
local function LoadConfig(fname)
local succ, content = pcall(reader, fname)
if not succ or not content then
log.warning('Failed to read config file '..fname..', continuing...')
return {}
end
succ, content = pcall(json.decode, StripJSON(content), {number = {hex = true}})
if not succ or not content then
log.warning(table.concat{'Failed to parse config file ', fname, ': "', content or 'unknown error', '", continuing...'})
return {}
end
return content
end
local function shallowCopy(src, dst)
for k,v in pairs(src) do
dst[k] = v
end
return dst
end
local configRaw = {}
local function InsertConfig(fname)
shallowCopy(LoadConfig(fname), configRaw)
end
InsertConfig('presets.json')
InsertConfig('config.json')
-- Get property from config
local resolveProperty
resolveProperty = function(tab, path, level)
level = level or 1
if type(path) == 'string' then
local pstr = path
path = {}
for s in pstr:gmatch('[^.]+') do
path[#path+1] = s
end
end
local resolving = path[level]
local resolved = tab[resolving]
if resolved ~= nil then
if #path == level then
return resolved -- We've made it
elseif type(resolved) == 'table' then
resolved = resolveProperty(resolved, path, level+1)
if resolved ~= nil then
return resolved
end
else
error('Trying to resolve into non-table object')
end
end
local basetab = tab.basedOn
if type(basetab) == 'string' then
basetab = {basetab}
end
if type(basetab) == 'table' then
for k,v in ipairs(basetab) do
local base = configRaw[v]
if type(base) == 'table' then
resolved = resolveProperty(base, path, level)
if resolved ~= nil then
return resolved
end
end
end
end
end
-- Resolve property with default value and type checking
local function resolvePropertyDT(tab, path, T, default)
local function pathToString()
if type(path) == 'table' then
path = table.concat(path, '.')
end
end
local res = resolveProperty(tab, path)
if res == nil then
if default then
return default
end
pathToString()
error('Property `'..path..'` not found in config but is required')
end
if type(res) ~= T then
pathToString()
error(table.concat {'Property `', path, '` have type ', type(res), ', but must have type ', T})
end
return res
end
local function compileProfile(profname)
local prof = configRaw[profname]
if not prof then
error('Profile `'..profname..'` not found')
end
local function key(nam)
return resolvePropertyDT(prof, {'keys', nam}, 'string', '')
end
local function joy(path)
return resolvePropertyDT(prof, path, 'string', '')
end
local function motion(path)
return resolvePropertyDT(prof, path, 'table', {'x', 0})
end
local function numconst(path)
return resolvePropertyDT(prof, path, 'number')
end
local res = {keys = {}, accel = {}, gyro = {}}
local keys = res.keys
for _, kname in ipairs(common.keys) do
keys[kname] = key(kname)
end
for _, jname in ipairs(common.joys.list) do
keys[jname] = {}
for _, axisname in ipairs(common.joys.axis) do
keys[jname][axisname] = {}
for _, dirname in ipairs(common.joys.dirs) do
keys[jname][axisname][dirname] = joy({'keys', jname, axisname, dirname})
end
end
end
local accel = res.accel
for _, axisname in ipairs(common.accel) do
accel[axisname] = motion({'accel', axisname})
end
local gyro = res.gyro
for _, axisname in ipairs(common.gyro) do
gyro[axisname] = motion({'gyro', axisname})
end
res.ACCEL_G = numconst 'ACCEL_GRAV_WII' / numconst 'ACCEL_GRAV_REAL'
res.GYRO_DEG_PER_S = numconst 'GYRO_MAX_WII' / numconst 'GYRO_MAX_REAL'
res.MPlusCalibration = resolvePropertyDT(configRaw, {'DefaultCalibration'}, 'table')
res.MPlusCalibrationOverrides = resolvePropertyDT(configRaw, {'Calibration'}, 'table', {})
return res
end
return compileProfile(resolvePropertyDT(configRaw, {'UseProfile'}, 'string'))