aboutsummaryrefslogtreecommitdiffstats
path: root/modules/lssi/lssi.lua
blob: cc01f0f9bb65ce1590559a05ade89cd18dc2f4cf (plain) (blame)
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
--[[
Eat It - a Mpv plugin manager

Copyright © 2023 squibid

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
]]

-- NOTE: This is a POC and will most likely be reimplimented using diff files
-- with git
-- or we might want to generate a diff file from the requested changes
-- and check if the diffs match in content

--[[ 
                           Lua Script Script Injector
Takes an input file and code then, it outputs a file with your code in there.
]]--

local mp = require('mp')

mod = {
  version = 'ALPHA 1', -- the current version of lssi
  author = 'squibid',
}

-- load the eatit config file
dofile(mp.command_native({'expand-path', '~~/eatit-cfg.lua'}))

-- helper functions
local function tablelength(T)
  local count = 0
  for _ in pairs(T) do count = count + 1 end
  return count
end

local function fileexists(name)
   local f = io.open(name, 'r')
   if f ~= nil then io.close(f) return true else return false end
end

local function logwrite(string)
  if opts.logging.log then
    io.write(os.date(opts.logging.logdate) .. ' ' .. string .. '\n')
  end
end

local function openlog()
  if opts.logging.log then -- log if asked to
    -- get our logfile's full path
    fn = mp.command_native({'expand-path', opts.logging.logfile})

    f = io.open(fn, 'a') -- open file buffer
    io.output(f) -- set it as default
  end
end

local function closelog()
  if opts.logging.log then
    io.close(f)
  end
end

local function inject(infile, l, outfile)
  local inf = io.open(infile, 'r')
  local infcont = {}
  for i in inf:lines() do
    table.insert(infcont, i)
  end
  inf:close()

  -- don't do anything if there is already code injected into the file
  if string.find(infcont[1], "-- code injected by lssi") then
    logwrite('code is already injected into ' .. infile)
    return
  end
  logwrite('Injecting code into ' .. infile)

  for i in pairs(l) do
    -- add requested line below existing line
    if l[i][2] == 'G' then
      infcont[tablelength(infcont)] = infcont[tablelength(infcont)] .. '\n' .. l[i][1]
    elseif l[i][2] == 'g' then
      infcont[1] = l[i][1] .. '\n' .. infcont[1]
    else
      infcont[l[i][2]] = (infcont[l[i][2]]) .. '\n' .. l[i][1]
    end
  end

  local outf = io.open(outfile, 'w')

  -- we inject metadata to prevent writing to the file more than once
  infcont[1] = "-- code injected by lssi " .. mod.version .. '\n' .. infcont[1]
  for i, v in ipairs(infcont) do
    outf:write(v .. '\n')
  end

  io.close(outf)
end

local function checkandinject()
  openlog()

  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
    if plugins[i]['lssi'] then
      -- and the file we are trying to modify actually exists
      if fileexists(f) then
        -- inject it! no going back now
        inject(f, plugins[i]['lssi'], f)
      else
        logwrite('Failed to inject code into "' ..
          plugins[i]['file'] .. '" file does not exist')
      end
    end
  end

  closelog()
end

checkandinject()