-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinireader.lua
More file actions
66 lines (53 loc) · 1.5 KB
/
inireader.lua
File metadata and controls
66 lines (53 loc) · 1.5 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
local function strip_comment(line)
local startPos, endPos = line:find('[;#]')
if startPos then
line = line:sub(1, startPos - 1):Trim()
end
return line
end
local function strip_quotes(line)
return line:gsub('[\"]', ''):Trim()
end
return function(file_name, from_game, _strip_quotes)
local wasSuccess, value = pcall(file.Read, file_name, (from_game and 'GAME' or 'DATA'))
if wasSuccess and value ~= nil then
local exploded_data = string.Explode('\n', value)
local output_table = {}
local current_node = ''
for _, v in pairs(exploded_data) do
local line = strip_comment(v):gsub('\n', '')
if line ~= '' then
if _strip_quotes then
line = strip_quotes(line)
end
if line:sub(1, 1) == '[' then
local start_pos, endPos = line:find('%]')
if start_pos then
current_node = line:sub(2, start_pos - 1)
if not output_table[current_node] then
output_table[current_node] = {}
end
else
return false
end
elseif current_node == '' then
return false
else
local data = string.Explode('=', line)
if #data > 1 then
local key = data[1]
local value = table.concat(data, '=', 2)
if tonumber(value) then
output_table[current_node][key] = tonumber(value)
elseif value == 'true' or value == 'false' then
output_table[current_node][key] = (value == 'true')
else
output_table[current_node][key] = value
end
end
end
end
end
return output_table
end
end