load.lua

180 lines · 4.7 KB

Open raw

Bulk loader for CC-Tweaked scripts

Usage: on turtle/computer:

Copy & run

wget https://perlytiara.github.io/turtles.tips/raw/programs/BigGamingGamers/load.lua
1-- Bulk loader for CC-Tweaked scripts
2-- Downloads scripts from GitHub into matching local folders if missing
3-- Usage on turtle/computer:
4-- 1) Ensure http is enabled in CC config
5-- 2) run: load (after saving this file as load)
6
7local BASE = 'https://raw.githubusercontent.com/perlytiara/CC-Tweaked-TurtsAndComputers/refs/heads/main/'
8
9local manifest = {
10 ["programs/perlytiara/BigBaemingGamers"] = {
11 -- client
12 "client/client-bomb.lua",
13 "client/client-dig.lua",
14 "client/client-quarry.lua",
15 "client/client.lua",
16 -- farm
17 "farm/cactus-farm.lua",
18 "farm/harvest_0.lua",
19 "farm/harvest_1.lua",
20 -- phone
21 "phone/phone-app-mine.lua",
22 "phone/phone-bombing.lua",
23 -- server
24 "server/server-phone.lua",
25 -- tasks
26 "tasks/quarry-miner.lua",
27 "tasks/simple-quarry.lua",
28 -- tnt
29 "tnt/tnt-deployer.lua",
30 "tnt/tnt-igniter.lua",
31 -- update helpers
32 "update/update_bamboo.lua",
33 "update/update_dispense.lua",
34 "update/update_lift.lua",
35 "update/update_pos.lua",
36 -- utils
37 "utils/block-placer.lua",
38 "utils/detectids.lua",
39 "utils/item-counter.lua",
40 "utils/lava-refueler.lua",
41 "utils/ore-keeper.lua",
42 "utils/ractor.lua",
43 "utils/refuel-test.lua",
44 "utils/upward-quarry-test.lua",
45 },
46}
47
48local function ensureDir(path)
49 if not fs.exists(path) then
50 fs.makeDir(path)
51 end
52end
53
54local function ensureParentDir(localPath)
55 local parent = string.match(localPath, "(.+)/[^/]+$")
56 if parent then ensureDir(parent) end
57end
58
59local function downloadFile(remotePath, localPath)
60 local url = BASE .. remotePath .. '?breaker=' .. tostring(math.random(0, 999999))
61 local res, err = http.get(url)
62 if not res then
63 print('ERROR: http.get failed for ' .. remotePath .. ' - ' .. tostring(err))
64 return false
65 end
66 local code = res.readAll()
67 res.close()
68
69 local handle = fs.open(localPath, 'w')
70 handle.write(code)
71 handle.close()
72 return true
73end
74
75local function loadFiles(dir, files)
76 local downloaded = 0
77 local skipped = 0
78 local localDir = '/' .. dir
79 ensureDir(localDir)
80
81 for i = 1, #files, 1 do
82 local fname = files[i]
83 local remotePath = dir .. '/' .. fname
84 local localPath = localDir .. '/' .. fname
85 ensureParentDir(localPath)
86 if fs.exists(localPath) then
87 skipped = skipped + 1
88 else
89 if downloadFile(remotePath, localPath) then
90 downloaded = downloaded + 1
91 print('Downloaded: ' .. localPath)
92 end
93 end
94 end
95
96 print(string.format('Done. Downloaded %d, skipped %d (already present).', downloaded, skipped))
97end
98
99local function promptSelect(options)
100 for i = 1, #options, 1 do
101 print(string.format('%d) %s', i, options[i]))
102 end
103 io.write('Select number: ')
104 local choice = read()
105 local idx = tonumber(choice)
106 if idx and idx >= 1 and idx <= #options then
107 return idx
108 end
109 return nil
110end
111
112local function showMenu()
113 print('== Load Menu ==')
114 print('1) Load all missing (BigBaemingGamers)')
115 print('2) Load a single file')
116 print('3) Exit')
117 io.write('Choose: ')
118 local ch = read()
119 return tonumber(ch)
120end
121
122local function loadAll()
123 for dir, files in pairs(manifest) do
124 loadFiles(dir, files)
125 end
126end
127
128local function loadSingle()
129 -- Assume single directory in manifest
130 local dir, files
131 for d, f in pairs(manifest) do
132 dir, files = d, f
133 break
134 end
135 print('Select a file to load:')
136 local idx = promptSelect(files)
137 if not idx then
138 print('Invalid selection')
139 return
140 end
141 loadFiles(dir, { files[idx] })
142end
143
144if not http then
145 print('ERROR: http API not available. Enable in mod config.')
146else
147 if #arg >= 1 then
148 -- CLI usage: load <filename>|all
149 local target = tostring(arg[1])
150 if target == 'all' then
151 loadAll()
152 else
153 for dir, files in pairs(manifest) do
154 local found = false
155 for i = 1, #files, 1 do
156 if files[i] == target then
157 loadFiles(dir, { target })
158 found = true
159 break
160 end
161 end
162 if found then break end
163 end
164 end
165 else
166 while true do
167 local ch = showMenu()
168 if ch == 1 then
169 loadAll()
170 elseif ch == 2 then
171 loadSingle()
172 else
173 break
174 end
175 end
176 end
177end
178
179
180