add some nil checks, and make lssi work with eatit changes
This commit is contained in:
@ -27,7 +27,7 @@ Takes an input file and code then, it outputs a file with your code in there.
|
|||||||
local mp = require('mp')
|
local mp = require('mp')
|
||||||
|
|
||||||
mod = {
|
mod = {
|
||||||
version = 'ALPHA 1', -- the current version of lssi
|
version = 'ALPHA 1.1', -- the current version of lssi
|
||||||
author = 'squibid',
|
author = 'squibid',
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -48,7 +48,7 @@ end
|
|||||||
|
|
||||||
local function logwrite(string)
|
local function logwrite(string)
|
||||||
if opts.logging.log then
|
if opts.logging.log then
|
||||||
io.write(os.date(opts.logging.logdate) .. ' ' .. string .. '\n')
|
io.write(os.date(opts.logging.logdate)..' '..string..'\n')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -58,6 +58,7 @@ local function openlog()
|
|||||||
fn = mp.command_native({'expand-path', opts.logging.logfile})
|
fn = mp.command_native({'expand-path', opts.logging.logfile})
|
||||||
|
|
||||||
f = io.open(fn, 'a') -- open file buffer
|
f = io.open(fn, 'a') -- open file buffer
|
||||||
|
if not f then return end
|
||||||
io.output(f) -- set it as default
|
io.output(f) -- set it as default
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -78,28 +79,28 @@ local function inject(infile, l, outfile)
|
|||||||
|
|
||||||
-- don't do anything if there is already code injected into the file
|
-- don't do anything if there is already code injected into the file
|
||||||
if string.find(infcont[1], "-- code injected by lssi") then
|
if string.find(infcont[1], "-- code injected by lssi") then
|
||||||
logwrite('code is already injected into ' .. infile)
|
logwrite('code is already injected into '..infile)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
logwrite('Injecting code into ' .. infile)
|
logwrite('Injecting code into '..infile)
|
||||||
|
|
||||||
for i in pairs(l) do
|
for i in pairs(l) do
|
||||||
-- add requested line below existing line
|
-- add requested line below existing line
|
||||||
if l[i][2] == 'G' then
|
if l[i][2] == 'G' then
|
||||||
infcont[tablelength(infcont)] = infcont[tablelength(infcont)] .. '\n' .. l[i][1]
|
infcont[tablelength(infcont)] = infcont[tablelength(infcont)]..'\n'..l[i][1]
|
||||||
elseif l[i][2] == 'g' then
|
elseif l[i][2] == 'g' then
|
||||||
infcont[1] = l[i][1] .. '\n' .. infcont[1]
|
infcont[1] = l[i][1]..'\n'..infcont[1]
|
||||||
else
|
else
|
||||||
infcont[l[i][2]] = (infcont[l[i][2]]) .. '\n' .. l[i][1]
|
infcont[l[i][2]] = (infcont[l[i][2]])..'\n'..l[i][1]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local outf = io.open(outfile, 'w')
|
local outf = io.open(outfile, 'w')
|
||||||
|
|
||||||
-- we inject metadata to prevent writing to the file more than once
|
-- we inject metadata to prevent writing to the file more than once
|
||||||
infcont[1] = "-- code injected by lssi " .. mod.version .. '\n' .. infcont[1]
|
infcont[1] = "-- code injected by lssi "..mod.version..'\n'..infcont[1]
|
||||||
for i, v in ipairs(infcont) do
|
for i, v in ipairs(infcont) do
|
||||||
outf:write(v .. '\n')
|
outf:write(v..'\n')
|
||||||
end
|
end
|
||||||
|
|
||||||
io.close(outf)
|
io.close(outf)
|
||||||
@ -109,20 +110,19 @@ local function checkandinject()
|
|||||||
openlog()
|
openlog()
|
||||||
|
|
||||||
for i = 1, tablelength(plugins) do
|
for i = 1, tablelength(plugins) do
|
||||||
-- get the file we want to inject our code into
|
|
||||||
local f = mp.command_native({'expand-path', '~~/'}) ..
|
|
||||||
'/' .. plugins[i]['dir'] ..
|
|
||||||
'/' .. plugins[i]['file']
|
|
||||||
|
|
||||||
-- check if the plugin has been configured with lssi
|
-- check if the plugin has been configured with lssi
|
||||||
if plugins[i]['lssi'] then
|
if plugins[i]['lssi'] ~= nil then
|
||||||
|
-- get the file we want to inject our code into
|
||||||
|
local f = mp.command_native({'expand-path', '~~/'}) ..
|
||||||
|
'/'..(plugins[i]['dir'] or 'scripts') ..
|
||||||
|
'/'..plugins[i]['file']
|
||||||
-- and the file we are trying to modify actually exists
|
-- and the file we are trying to modify actually exists
|
||||||
if fileexists(f) then
|
if fileexists(f) then
|
||||||
-- inject it! no going back now
|
-- inject it! no going back now
|
||||||
inject(f, plugins[i]['lssi'], f)
|
inject(f, plugins[i]['lssi'], f)
|
||||||
else
|
else
|
||||||
logwrite('Failed to inject code into "' ..
|
logwrite('Failed to inject code into "' ..
|
||||||
plugins[i]['file'] .. '" file does not exist')
|
plugins[i]['file']..'" file does not exist')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Reference in New Issue
Block a user