-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhexconfig.lua
More file actions
61 lines (48 loc) · 1.59 KB
/
hexconfig.lua
File metadata and controls
61 lines (48 loc) · 1.59 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
require('util')
HexConfig = {}
local path_to_this_file = debug.getinfo(1).source
local base_directory = path_to_this_file:match("^@(.*)hexconfig%.lua$")
local config_file_path = base_directory..'config.txt'
local function get_config_file_contents()
local f = io.open(config_file_path, 'r')
local text = ''
if f then
text = f:read('*a')
f:close()
end
return text:gsub('\r\n', '\n')
end
function HexConfig.get(option_name)
local text = get_config_file_contents()
for line in text:gmatch("[^\n]+") do
local option, value = line:match("([^\n]*)=([^\n]*)")
if option and Util.trim(option) == option_name then
return Util.trim(value)
end
end
return ''
end
function HexConfig.set(option_name, new_value)
local text = get_config_file_contents()
local new_lines = {}
local did_update_value = false
for line in text:gmatch("[^\n]+") do
local option, current_value = line:match("(.*)=(.*)")
if option and Util.trim(option) == option_name then
table.insert(new_lines, option_name..' = '..new_value)
did_update_value = true
else
table.insert(new_lines, Util.trim(option)..' = '..Util.trim(current_value))
end
end
if not did_update_value then
table.insert(new_lines, option_name..' = '..new_value)
end
local new_text = table.concat(new_lines, '\n')
local f = io.open(config_file_path, 'w')
if not f then
error('Unable to write to config file: "'..config_file_path..'"')
end
f:write(new_text)
f:close()
end