load.lua
197 lines · 5.2 KB
Bulk loader for CC-Tweaked scripts
Usage: on turtle/computer:
Copy & run
wget https://perlytiara.github.io/turtles.tips/raw/programs/perlytiara/BigBaemingGamers/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 | |
| 7 | local BASE = 'https://raw.githubusercontent.com/perlytiara/CC-Tweaked-TurtsAndComputers/refs/heads/main/' |
| 8 | |
| 9 | local 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 | -- loader and updater aliases |
| 46 | "@load.lua", |
| 47 | "@update.lua", |
| 48 | "load.lua", |
| 49 | "update.lua", |
| 50 | }, |
| 51 | ["programs/perlytiara/BigBaemingGamers/@gps"] = { |
| 52 | -- @gps scripts |
| 53 | "@gps/gps.lua", |
| 54 | "@gps/gps-host.lua", |
| 55 | "@gps/gps-client.lua", |
| 56 | }, |
| 57 | } |
| 58 | |
| 59 | local function ensureDir(path) |
| 60 | if not fs.exists(path) then |
| 61 | fs.makeDir(path) |
| 62 | end |
| 63 | end |
| 64 | |
| 65 | local function ensureParentDir(localPath) |
| 66 | local parent = string.match(localPath, "(.+)/[^/]+$") |
| 67 | if parent then ensureDir(parent) end |
| 68 | end |
| 69 | |
| 70 | local function downloadFile(remotePath, localPath) |
| 71 | local url = BASE .. remotePath .. '?breaker=' .. tostring(math.random(0, 999999)) |
| 72 | local res, err = http.get(url) |
| 73 | if not res then |
| 74 | print('ERROR: http.get failed for ' .. remotePath .. ' - ' .. tostring(err)) |
| 75 | return false |
| 76 | end |
| 77 | local code = res.readAll() |
| 78 | res.close() |
| 79 | |
| 80 | local handle = fs.open(localPath, 'w') |
| 81 | handle.write(code) |
| 82 | handle.close() |
| 83 | return true |
| 84 | end |
| 85 | |
| 86 | local function loadFiles(dir, files) |
| 87 | local downloaded = 0 |
| 88 | local skipped = 0 |
| 89 | local localDir = '/' .. dir |
| 90 | ensureDir(localDir) |
| 91 | |
| 92 | for i = 1, #files, 1 do |
| 93 | local fname = files[i] |
| 94 | local remotePath = dir .. '/' .. fname |
| 95 | local localPath = localDir .. '/' .. fname |
| 96 | ensureParentDir(localPath) |
| 97 | if fs.exists(localPath) then |
| 98 | skipped = skipped + 1 |
| 99 | else |
| 100 | if downloadFile(remotePath, localPath) then |
| 101 | downloaded = downloaded + 1 |
| 102 | print('Downloaded: ' .. localPath) |
| 103 | end |
| 104 | end |
| 105 | end |
| 106 | |
| 107 | print(string.format('Done. Downloaded %d, skipped %d (already present).', downloaded, skipped)) |
| 108 | end |
| 109 | |
| 110 | local function promptSelect(options) |
| 111 | for i = 1, #options, 1 do |
| 112 | print(string.format('%d) %s', i, options[i])) |
| 113 | end |
| 114 | io.write('Select number: ') |
| 115 | local choice = read() |
| 116 | local idx = tonumber(choice) |
| 117 | if idx and idx >= 1 and idx <= #options then |
| 118 | return idx |
| 119 | end |
| 120 | return nil |
| 121 | end |
| 122 | |
| 123 | local function showMenu() |
| 124 | print('== Load Menu ==') |
| 125 | print('1) Load all missing (BigBaemingGamers)') |
| 126 | print('2) Load a single file') |
| 127 | print('3) Exit') |
| 128 | io.write('Choose: ') |
| 129 | local ch = read() |
| 130 | return tonumber(ch) |
| 131 | end |
| 132 | |
| 133 | local function loadAll() |
| 134 | for dir, files in pairs(manifest) do |
| 135 | loadFiles(dir, files) |
| 136 | end |
| 137 | end |
| 138 | |
| 139 | local function loadSingle() |
| 140 | -- Select directory first |
| 141 | local dirs = {} |
| 142 | for d, _ in pairs(manifest) do table.insert(dirs, d) end |
| 143 | print('Select directory:') |
| 144 | local dirIdx = promptSelect(dirs) |
| 145 | if not dirIdx then |
| 146 | print('Invalid selection') |
| 147 | return |
| 148 | end |
| 149 | local dir = dirs[dirIdx] |
| 150 | local files = manifest[dir] |
| 151 | print('Select a file to load:') |
| 152 | local idx = promptSelect(files) |
| 153 | if not idx then |
| 154 | print('Invalid selection') |
| 155 | return |
| 156 | end |
| 157 | loadFiles(dir, { files[idx] }) |
| 158 | end |
| 159 | |
| 160 | if not http then |
| 161 | print('ERROR: http API not available. Enable in mod config.') |
| 162 | else |
| 163 | if #arg >= 1 then |
| 164 | -- CLI usage: load <filename>|all |
| 165 | local target = tostring(arg[1]) |
| 166 | if target == 'all' then |
| 167 | loadAll() |
| 168 | else |
| 169 | local found = false |
| 170 | for dir, files in pairs(manifest) do |
| 171 | for i = 1, #files, 1 do |
| 172 | if files[i] == target then |
| 173 | loadFiles(dir, { target }) |
| 174 | found = true |
| 175 | break |
| 176 | end |
| 177 | end |
| 178 | if found then break end |
| 179 | end |
| 180 | if not found then |
| 181 | print('Unknown file: ' .. target) |
| 182 | end |
| 183 | end |
| 184 | else |
| 185 | while true do |
| 186 | local ch = showMenu() |
| 187 | if ch == 1 then |
| 188 | loadAll() |
| 189 | elseif ch == 2 then |
| 190 | loadSingle() |
| 191 | else |
| 192 | break |
| 193 | end |
| 194 | end |
| 195 | end |
| 196 | end |
| 197 |