-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.lua
More file actions
280 lines (245 loc) · 7.83 KB
/
Copy pathinstall.lua
File metadata and controls
280 lines (245 loc) · 7.83 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
-- install.lua - CC-linux-like bootstrap installer
-- Usage: wget run https://raw.githubusercontent.com/timetetng/CC-linux-like/main/install.lua
-- Dynamically discovers .lua files via GitHub API; falls back to hardcoded list on failure.
local BASE_URL = "https://raw.githubusercontent.com/timetetng/CC-linux-like/main"
local TREE_URL = "https://api.github.com/repos/timetetng/CC-linux-like/git/trees/main?recursive=1"
local INCLUDE_PREFIXES = { "bin/", "config/", "game/", "startup.lua" }
local EXCLUDE_FILES = { "install.lua" }
-- Hardcoded fallback when GitHub API is unavailable (rate-limited, offline, etc.)
local FALLBACK_FILES = {
"startup.lua",
"bin/boot.lua",
"bin/cat.lua",
"bin/crash.lua",
"bin/git.lua",
"bin/grep.lua",
"bin/neofetch.lua",
"bin/pwd.lua",
"bin/ssh.lua",
"bin/touch.lua",
"bin/tree.lua",
"bin/vim.lua",
"bin/yazi.lua",
"config/theme.lua",
"game/game/#.lua",
"game/game/falling.lua",
}
-- URL-encode special characters (# -> %23) so raw URLs aren't truncated
local function urlEncodePath(path)
return path:gsub("#", "%%23")
end
-- Safely create a directory (and its parents)
local function mkdirSafe(path)
if fs.exists(path) then
if fs.isDir(path) then
return true
end
print(" warning: " .. path .. " exists but is not a directory")
return false
end
local parent = path:match("^(.+)/[^/]+$")
if parent and not fs.exists(parent) then
mkdirSafe(parent)
end
fs.makeDir(path)
return true
end
-- Download a single file with AUTO-MIRROR fallback
local function downloadFile(url, dest)
local dir = dest:match("^(.+)/[^/]+$")
if dir then
mkdirSafe(dir)
end
if fs.exists(dest) then
return "skipped"
end
-- 第一次尝试:GitHub 原站
local response, err = http.get(url)
-- 第二次尝试:如果原站触发 rate limit 拦截,自动走 ghproxy 代理重试
if not response then
local mirrorUrl = "https://ghproxy.net/" .. url
response, err = http.get(mirrorUrl)
end
if not response then
return nil, err or "connection failed"
end
local content = response.readAll()
response.close()
if not content or #content == 0 then
return nil, "empty response"
end
local file, err2 = fs.open(dest, "w")
if not file then
return nil, err2 or "cannot write file"
end
file.write(content)
file.close()
return "ok"
end
-- ==========================================================================
-- Main
-- ==========================================================================
local termW = term.getSize()
local pad = ("="):rep(math.min(termW, 50))
print(pad)
print(" CC-linux-like Installer")
print(" https://github.com/timetetng/CC-linux-like")
print(pad)
print()
-- ==========================================================================
-- 交互式配置 (Interactive Setup)
-- ==========================================================================
print("=== Initial Setup ===")
-- 1. 询问并设置用户名
write("Enter username (leave blank to skip): ")
local username = read()
if username and username ~= "" then
settings.set("user.name", username)
settings.save() -- 必须调用 save() 才能将更改持久化到 .settings 文件中
print(" -> Username saved as: " .. username)
else
print(" -> Skipped setting username.")
end
-- 2. 询问并设置设备标签
write("Enter device label (leave blank to skip): ")
local label = read()
if label and label ~= "" then
os.setComputerLabel(label) -- 等同于执行 label set
print(" -> Device label set to: " .. label)
else
print(" -> Skipped setting label.")
end
print(pad)
print()
-- ==========================================================================
if not http then
print("Error: HTTP API is not available. Enable it in CC:Tweaked config.")
return
end
-- Step 1: try to get file list from GitHub Tree API
local toDownload = {}
if textutils and textutils.unserializeJSON then
print("Fetching repository file tree...")
local resp, err = http.get(TREE_URL)
if resp then
local raw = resp.readAll()
resp.close()
local ok, treeData = pcall(textutils.unserializeJSON, raw)
if ok and treeData and treeData.tree then
for _, entry in ipairs(treeData.tree) do
if entry.type == "blob" and entry.path:match("%.lua$") then
local included = false
for _, prefix in ipairs(INCLUDE_PREFIXES) do
if entry.path == prefix or entry.path:sub(1, #prefix) == prefix then
included = true
break
end
end
local excluded = false
for _, ex in ipairs(EXCLUDE_FILES) do
if entry.path == ex then
excluded = true
break
end
end
if included and not excluded then
table.insert(toDownload, {
path = entry.path,
url = BASE_URL .. "/" .. urlEncodePath(entry.path),
})
end
end
end
else
local msg = ""
if raw and #raw > 0 then
msg = raw:match('"message"%s*:%s*"([^"]+)"') or ""
end
print(" API error" .. (msg ~= "" and (" (" .. msg .. ")") or "") .. " - using fallback list")
end
else
print(" " .. (err or "connection failed") .. " - using fallback list")
end
else
print("textutils.unserializeJSON not available - using fallback list")
end
-- Step 2: if API didn't produce results, use fallback
if #toDownload == 0 then
print("Using hardcoded file list (" .. #FALLBACK_FILES .. " files)")
for _, path in ipairs(FALLBACK_FILES) do
table.insert(toDownload, {
path = path,
url = BASE_URL .. "/" .. urlEncodePath(path),
})
end
end
if #toDownload == 0 then
print(" no files to install")
return
end
-- Step 3: create directories
local dirSet = {}
for _, f in ipairs(toDownload) do
local dir = f.path:match("^(.+)/[^/]+$")
if dir then
dirSet["/" .. dir] = true
end
end
print("Creating directories...")
for dir, _ in pairs(dirSet) do
if not fs.exists(dir) then
mkdirSafe(dir)
print(" " .. dir .. " created")
end
end
print()
-- Step 4: download files
print("Downloading " .. #toDownload .. " files...")
print()
local countOk = 0
local countSkip = 0
local countFail = 0
local errors = {}
for _, f in ipairs(toDownload) do
write(" " .. f.path .. " ")
local status, err = downloadFile(f.url, "/" .. f.path)
if status == "ok" then
print("OK")
countOk = countOk + 1
elseif status == "skipped" then
print("skip (exists)")
countSkip = countSkip + 1
else
print("FAIL " .. (err or "unknown error"))
countFail = countFail + 1
table.insert(errors, f.path .. ": " .. (err or "unknown error"))
end
-- 【关键修复】:每次下载后休息 0.3 秒,防止被当成爬虫踢掉
os.sleep(0.3)
end
-- Summary
print()
print(pad)
print(" Install summary")
print(pad)
print(" installed: " .. countOk)
if countSkip > 0 then
print(" skipped: " .. countSkip .. " (already exist)")
end
if countFail > 0 then
print(" failed: " .. countFail)
print()
for _, e in ipairs(errors) do
print(" FAIL " .. e)
end
end
print(pad)
print()
if countFail == 0 then
print(" All done! Reboot in 3 seconds...")
os.sleep(3)
shell.run("reboot")
else
print(" Some files failed. Retry or check your network connection.")
end
print(pad)