-- ==================== 配置区 ==================== local SCRIPT_VERSION = "0.7" local VERSION_URL = "https://pastebin.com/raw/SBEmz14t" -- 版本信息链接 local SCRIPT_DOWNLOAD_URL = "https://mpimg.cn/down.php/0d3f82aee7c6578609a152cac28043de.lua" -- 测试脚本下载链接 local SCRIPT_FILE_NAME = "脚本[旧域].lua" local SCRIPT_DIR = "/storage/emulated/0/Download/" local UPDATE_URL = SCRIPT_DOWNLOAD_URL -- 用于版本检测中的下载链接 -- 调试开关(正式使用时改为 false) local DEBUG = false -- ==================== 多语言 ==================== local function L(zh, en) return zh end -- ==================== 工具函数 ==================== -- 带 User-Agent 的 HTTP GET local function httpGet(url) local res = gg.makeRequest(url, { ["User-Agent"] = "Mozilla/5.0 (Linux; Android 10; SM-G973F) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.120 Mobile Safari/537.36" }) if res and res.content and res.content ~= "" then return res.content end return nil end -- 带进度的下载并保存文件(修复中文路径与空文件问题) local function downloadWithProgress(url, filePath) gg.toast(L("正在请求下载...", "Requesting download...")) local res = gg.makeRequest(url, { ["User-Agent"] = "Mozilla/5.0 (Linux; Android 10; SM-G973F) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.120 Mobile Safari/537.36" }) if not res then return false, "请求返回 nil,请检查网络权限" end if DEBUG then gg.alert("状态码: " .. tostring(res.code) .. "\n内容长度: " .. tostring(#res.content)) end if not res.content or res.content == "" then return false, "内容为空,状态码: " .. tostring(res.code) end local content = res.content local contentLen = #content gg.toast("获取成功,准备写入...") -- 【修复1】:为了排除中文路径干扰,建议先强制使用英文路径测试 -- 正式使用时,记得把这里的路径改回你想要的 local testPath = "/storage/emulated/0/Download/脚本[旧域].lua" -- 如果坚持使用中文目录,这里尝试创建 if gg.makeDirectory then pcall(gg.makeDirectory, SCRIPT_DIR) end -- 【修复2】:一次性写入,避免分块逻辑出错 local file, err = io.open(testPath, "w") if not file then return false, "无法创建文件: " .. tostring(err) end file:write(content) file:flush() -- 【核心修复】:强制刷新缓冲区,确保数据落盘! file:close() -- 【修复3】:写入完成后,立刻读取验证文件大小 local checkFile = io.open(testPath, "r") if checkFile then local checkContent = checkFile:read("*a") checkFile:close() if #checkContent == 0 then return false, "文件写入为空!请检查路径是否包含中文或权限被拒绝。路径: " .. testPath else gg.toast("写入成功,大小: " .. #checkContent .. " 字节") end else return false, "无法读取刚写入的文件,可能没有存储权限" end return true, contentLen end -- 删除旧文件 local function removeOldFile(filePath) local file = io.open(filePath, "r") if file then file:close() os.remove(filePath) gg.toast(L("已删除旧文件", "Old file removed")) return true end return false end -- 运行测试脚本 local function runTestScript(filePath) local file = io.open(filePath, "r") if not file then gg.alert(L("无法找到测试脚本文件\n路径: ", "Cannot find test script\nPath: ") .. filePath, L("退出", "Exit")) os.exit() end local code = file:read("*a") file:close() if not code or code == "" then gg.alert(L("测试脚本内容为空", "Test script is empty"), L("退出", "Exit")) os.exit() end local chunk, err = load(code) if not chunk then gg.alert(L("脚本语法错误: ", "Script syntax error: ") .. tostring(err), L("退出", "Exit")) os.exit() end local ok, err2 = pcall(chunk) if not ok then gg.alert(L("脚本运行错误: ", "Script runtime error: ") .. tostring(err2), L("退出", "Exit")) os.exit() end end -- ==================== 版本检测 ==================== local function checkVersion() gg.toast(L("检测版本中...", "Checking version...")) local response = httpGet(VERSION_URL) if not response or response == "" then gg.alert(L("无法连接服务器\n请检查网络后重试", "Cannot connect to server\nPlease check network and retry"), L("退出", "Exit")) os.exit() end response = response:gsub("^%s+", ""):gsub("%s+$", ""):gsub("\r", "") -- 格式: 版本号|强制更新|下载链接|维护模式|维护提示 local parts = {} for part in string.gmatch(response, "[^|]+") do table.insert(parts, part) end local serverVersion = parts[1] or "" local forceUpdate = parts[2] or "0" local downloadUrl = parts[3] or UPDATE_URL local maintenance = parts[4] or "0" local maintMsg = parts[5] or L("维护中,请稍后", "Maintenance in progress, please wait") if maintenance == "1" then gg.alert(L("服务器维护中\n", "Server under maintenance\n") .. maintMsg, L("退出", "Exit")) os.exit() end if serverVersion ~= "" and serverVersion ~= SCRIPT_VERSION then if forceUpdate == "1" then gg.alert(L("版本过旧\n当前: v", "Version too old\nCurrent: v") .. SCRIPT_VERSION .. L("\n最新: v", "\nLatest: v") .. serverVersion .. L("\n请更新后使用", "\nPlease update to use"), L("退出", "Exit")) gg.copyText(downloadUrl) os.exit() else local choice = gg.alert(L("发现新版本\n当前: v", "New version found\nCurrent: v") .. SCRIPT_VERSION .. L("\n最新: v", "\nLatest: v") .. serverVersion, L("下载", "Download"), L("跳过", "Skip")) if choice == 1 then gg.copyText(downloadUrl) gg.alert(L("链接已复制", "Link copied"), L("退出", "Exit")) os.exit() end end -- 返回 false 表示版本不一致,需要更新 return false end -- 版本一致 return true end -- ==================== 主逻辑 ==================== local function main() local scriptFullPath = SCRIPT_DIR .. SCRIPT_FILE_NAME -- 1. 版本检测 local versionOk = checkVersion() if not versionOk then -- 版本不一致,执行删除并重新下载 gg.toast(L("版本不一致,开始更新脚本...", "Version mismatch, updating script...")) removeOldFile(scriptFullPath) local ok, err = downloadWithProgress(SCRIPT_DOWNLOAD_URL, scriptFullPath) if not ok then gg.alert(L("下载失败: ", "Download failed: ") .. tostring(err), L("退出", "Exit")) os.exit() end -- 下载完成后运行 runTestScript(scriptFullPath) else -- 版本一致,检查文件是否存在 local file = io.open(scriptFullPath, "r") if file then file:close() gg.toast(L("版本验证通过,运行测试脚本", "Version OK, running test script")) runTestScript(scriptFullPath) else gg.toast(L("测试脚本不存在,开始下载...", "Test script not found, downloading...")) local ok, err = downloadWithProgress(SCRIPT_DOWNLOAD_URL, scriptFullPath) if not ok then gg.alert(L("下载失败: ", "Download failed: ") .. tostring(err), L("退出", "Exit")) os.exit() end runTestScript(scriptFullPath) end end end -- 启动 main()