-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdmc_library_boot.lua
255 lines (191 loc) · 6.69 KB
/
dmc_library_boot.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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
--====================================================================--
-- dmc_library_boot.lua
--
-- utility to read in dmc_library configuration file
--
-- by David McCuskey
-- Documentation:
--====================================================================--
--[[
Copyright (C) 2013-2014 David McCuskey. All Rights Reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in the
Software without restriction, including without limitation the rights to use, copy,
modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so, subject to the
following conditions:
The above copyright notice and this permission notice shall be included in all copies
or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
--]]
-- Semantic Versioning Specification: http://semver.org/
local VERSION = "1.0.0"
--====================================================================--
-- Setup Support Methods
--====================================================================--
local Utils = {} -- make copying from dmc_utils easier
--== Start dmc_utils copies ==--
Utils.IO_ERROR = "io_error"
Utils.IO_SUCCESS = "io_success"
function Utils.extend( fromTable, toTable )
function _extend( fT, tT )
for k,v in pairs( fT ) do
if type( fT[ k ] ) == "table" and
type( tT[ k ] ) == "table" then
tT[ k ] = _extend( fT[ k ], tT[ k ] )
elseif type( fT[ k ] ) == "table" then
tT[ k ] = _extend( fT[ k ], {} )
else
tT[ k ] = v
end
end
return tT
end
return _extend( fromTable, toTable )
end
function Utils.readFile( file_path, options )
-- print( "Utils.readFile", file_path )
options = options or {}
if options.lines == nil then options.lines = true end
local contents -- either string or table of strings
local ret_val = {} -- an array, [ status, content ]
if file_path == nil then
local ret_val = { Utils.IO_ERROR, "file path is NIL" }
else
local fh, reason = io.open( file_path, "r" )
if fh == nil then
print("ERROR: datastore load settings: " .. tostring( reason ) )
ret_val = { Utils.IO_ERROR, reason }
else
if options.lines == false then
-- read contents in one big string
contents = fh:read( '*all' )
else
-- read all contents of file into a table
contents = {}
for line in fh:lines() do
table.insert( contents, line )
end
end
ret_val = { Utils.IO_SUCCESS, contents }
io.close( fh )
end -- fh == nil
end -- file_path == nil
return ret_val[1], ret_val[2]
end
function Utils.readConfigFile( file_path, options )
-- print( "Utils.readConfigFile", file_path )
options = options or {}
options.lines = true
options.default_section = options.default_section or nil -- no default here
local status, contents = Utils.readFile( file_path, options )
if status == Utils.IO_ERROR then return nil end
local data = {}
local curr_section = options.default_section
if curr_section ~= nil and not data[curr_section] then
data[curr_section]={}
end
local function castValue( v, t )
local ret = nil
if t == 'PATH' or t == 'FILE' then
ret = string.gsub( v, '[/\\]', "." )
elseif t == 'BOOL' or t == 'BOOLEAN' then
if v == 'true' then
ret = true
elseif v == 'false' then
ret = false
end
elseif t == 'INT' or t == 'INTEGER' then
ret = tonumber( v )
elseif t == 'STR' or t == 'STRING' then
ret = v
end
if ret == nil then ret = v end -- return orig value
return ret
end
local function processSectionLine( line )
local key
key = line:match( "%[([%w_]+)%]" )
key = string.lower( key ) -- use only lowercase inside of module
return key
end
local function processKeyLine( line )
local k, v, key, val
-- print( line )
local parts = { nil, 'STRING', nil } -- holds key, type, value
k, v = line:match( "([%w_:]+)%s*=%s*([%w_./\\]+)" )
parts[3] = v -- value
local i = 1
for x in k:gmatch( "[%a_]+" ) do
parts[i] = x
i = i + 1
end
-- print( tostring(parts[1]) .. " (".. tostring( parts[2]) .. ") = " .. tostring(parts[3]) )
key = string.lower( parts[1] ) -- use only lowercase inside of module
val = castValue( parts[3], parts[2] )
-- print( key, val, type(val) )
return key, val
end
local is_valid = true
local is_section
local key, val
for _, line in ipairs( contents ) do
-- print( line )
is_section = ( string.find( line, '%[%w', 1, false ) == 1 )
local is_key = ( string.find( line, '%w', 1, false ) == 1 )
-- print( is_section, is_key )
if is_section then
curr_section = processSectionLine( line )
if not data[curr_section] then data[curr_section]={} end
elseif is_key and curr_section ~= nil then
key, val = processKeyLine( line )
data[curr_section][key] = val
end
end
return data
end
--== End dmc_utils copies ==--
--====================================================================--
-- Setup DMC Library Config
--====================================================================--
-- This is standard code to bootstrap the dmc_library
-- it looks for a configuration file to read in
local DMC_LIBRARY_CONFIG_FILE = 'dmc_library.cfg'
local DMC_LIBRARY_DEFAULT_SECTION = 'dmc_library'
local dmc_lib_data, dmc_lib_info, dmc_lib_location
-- no module has yet tried to read in a config file
if _G.__dmc_library == nil then
local file_path, config_data
file_path = system.pathForFile( DMC_LIBRARY_CONFIG_FILE, system.ResourceDirectory )
if file_path ~= nil then
config_data = Utils.readConfigFile( file_path, { default_section=DMC_LIBRARY_DEFAULT_SECTION } )
end
_G.__dmc_library = config_data or {}
dmc_lib_data = _G.__dmc_library
dmc_lib_data.dmc_library = dmc_lib_data.dmc_library or {}
dmc_lib_info = dmc_lib_data.dmc_library
if dmc_lib_info.location == nil then
dmc_lib_info.location = ''
else
dmc_lib_info.location = string.gsub( dmc_lib_info.location, '[/\\]', "." )
end
-- setup find() utility
dmc_lib_data.func = {}
dmc_lib_data.func.find = function( name )
local loc = ''
if dmc_lib_data[name] and dmc_lib_data[name].location then
loc = dmc_lib_data[name].location
else
loc = dmc_lib_info.location
end
if loc ~= '' and string.sub( loc, -1 ) ~= '.' then
loc = loc .. '.'
end
return loc .. name
end
end