File size: 1,637 Bytes
d7c5875
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
script_name("ML-AutoReboot")
script_version_number(6)
script_version("1.0")
script_author("FYP")
script_description("reloads edited scripts automatically")
script_moonloader(021)
script_properties('work-in-pause')
local crc32 = require "lib.crc32ffi"


--- Config
autoreloadDelay    = 500 -- ms
autoreloadEnabled  = true


--- Main
function main()
  if not autoreloadEnabled then
    return -- just exit
  end
  -- for the reload case
  onSystemInitialized()
  while true do
    wait(autoreloadDelay)
    if files ~= nil then
      for f, h in pairs(files) do
        local cs = calc_file_crc32(f)
        if cs ~= h and cs ~= nil then
          local scr = find_script_by_path(f)
          if scr ~= nil then
            print("Reloading '" .. scr.name .. "'...")
            scr:reload()
          else
            print("Loading '" .. f .. "'...")
            script.load(f)
          end
          files[f] = cs -- update checksum
        end
      end
    end
  end
end

function init()
  initialized = true
  files = {}
  -- store all loaded scripts
  for _, s in ipairs(script.list()) do
    local cs = calc_file_crc32(s.path)
    if cs ~= nil then
      files[s.path] = cs
    end
  end
end


--- Events
function onSystemInitialized()
  if not initialized then
    init()
  end
end


--- Functions
function find_script_by_path(path)
  for _, s in ipairs(script.list()) do
    if s.path == path then
      return s
    end
  end
  return nil
end

function calc_file_crc32(path)
  local f = io.open(path, "r")
  if f == nil then
    return nil
  end
  local data = f:read("*a")
  local cs = crc32(data)
  f:close()
  return cs
end