diff --git a/native/src/Auth.cpp b/native/src/Auth.cpp index 5fbe80c..f59d70c 100644 --- a/native/src/Auth.cpp +++ b/native/src/Auth.cpp @@ -370,8 +370,12 @@ AuthResult Auth::Info() { return Call("info", json::object(), std::string(), true); } -AuthResult Auth::CheckUpdate(const std::string& version) { - return Call("check-update?version=" + version, json::object(), std::string(), true); +AuthResult Auth::CheckUpdate(const std::string& version, const std::string& manifestHash) { + std::string path = "check-update?version=" + UrlEncodeQuery(version); + if (!manifestHash.empty()) { + path += "&manifest_hash=" + UrlEncodeQuery(manifestHash); + } + return Call(path, json::object(), std::string(), true); } std::string Auth::ResolveUrl(const std::string& url) { diff --git a/native/src/Auth.h b/native/src/Auth.h index 1dfe4d9..4c8f52d 100644 --- a/native/src/Auth.h +++ b/native/src/Auth.h @@ -35,8 +35,10 @@ public: static AuthResult Recharge(const std::string& username, const std::string& cardKey); static AuthResult Heartbeat(); static AuthResult Info(); - // 版本检测:version 为当前客户端版本号 - static AuthResult CheckUpdate(const std::string& version); + // 版本检测:version 为当前客户端版本号,manifestHash 为本地当前清单指纹(可空) + // 带上指纹后,服务端能发现"版本号没变但文件被重新上传"的情况 + static AuthResult CheckUpdate(const std::string& version, + const std::string& manifestHash = std::string()); // 把服务端返回的相对地址(如 /uploads/xxx.zip)补全为完整 URL static std::string ResolveUrl(const std::string& url); }; diff --git a/native/src/Update.cpp b/native/src/Update.cpp index 7084318..2a0e869 100644 --- a/native/src/Update.cpp +++ b/native/src/Update.cpp @@ -10,6 +10,7 @@ #include #include "Auth.h" +#include "Config.h" #include "Json.h" #include "Log.h" #include "Util.h" @@ -43,6 +44,24 @@ long long Int64(const json& obj, const char* key) { return static_cast(it->get()); } +// 本机记录的服务端清单指纹:用于判断"版本号没变但服务器换过包" +constexpr const char* kUpdateStateFile = "update_state.json"; + +std::string LoadStoredManifestHash() { + const json st = Config::GetJsonGlobal(kUpdateStateFile, json::object()); + return Str(st, "manifest_hash"); +} + +void SaveStoredManifestHash(const std::string& hash, const std::string& version) { + if (hash.empty()) return; + json st = Config::GetJsonGlobal(kUpdateStateFile, json::object()); + if (!st.is_object()) st = json::object(); + if (Str(st, "manifest_hash") == hash && Str(st, "version") == version) return; + st["manifest_hash"] = hash; + if (!version.empty()) st["version"] = version; + Config::WriteTextGlobal(kUpdateStateFile, st.dump(2)); +} + bool EnsureDir(const std::wstring& dir) { if (::CreateDirectoryW(dir.c_str(), nullptr)) return true; return ::GetLastError() == ERROR_ALREADY_EXISTS; @@ -323,8 +342,9 @@ std::string Update::CurrentVersion() { UpdateInfo Update::Check() { UpdateInfo out; out.currentVersion = CurrentVersion(); + const std::string storedHash = LoadStoredManifestHash(); - const AuthResult result = Auth::CheckUpdate(out.currentVersion); + const AuthResult result = Auth::CheckUpdate(out.currentVersion, storedHash); if (!result.ok) { out.error = result.message; Log::Warn("版本检测失败:" + result.message); @@ -335,6 +355,7 @@ UpdateInfo Update::Check() { out.latestVersion = Str(data, "latest_version"); out.notes = Str(data, "update_notes"); out.fileHash = Str(data, "file_hash"); + out.manifestHash = Str(data, "manifest_hash"); out.entryFile = Str(data, "entry_file"); out.fileSize = Int64(data, "file_size"); out.forced = Str(data, "update_strategy") == "forced"; @@ -344,7 +365,16 @@ UpdateInfo Update::Check() { if (hasUpdate == data.end() || !hasUpdate->is_boolean() || !hasUpdate->get()) { return out; } - if (out.latestVersion.empty() || LowerAscii(out.latestVersion) == LowerAscii(out.currentVersion)) { + // 版本号相同也要看清单指纹:同版本号重新上传过文件时服务端会返回新的 manifest_hash, + // 此时应当照常更新,否则客户端会永远停在旧文件上 + const bool sameVersion = LowerAscii(out.latestVersion) == LowerAscii(out.currentVersion); + const bool contentChanged = !out.manifestHash.empty() && !storedHash.empty() && + LowerAscii(out.manifestHash) != LowerAscii(storedHash); + if (out.latestVersion.empty() || (sameVersion && !contentChanged)) { + // 已是最新:记住服务端当前指纹,供下次识别"文件被重新上传" + if (sameVersion && !out.manifestHash.empty()) { + SaveStoredManifestHash(out.manifestHash, out.latestVersion); + } return out; } @@ -438,6 +468,9 @@ bool Update::Apply(const UpdateInfo& info, const std::function& progr } if (progress) progress(100); + // 新文件已落地:立刻记住这次的清单指纹,重启后不会因指纹不一致重复下载 + SaveStoredManifestHash(info.manifestHash, info.latestVersion); + STARTUPINFOW si{}; si.cb = sizeof(si); PROCESS_INFORMATION pi{}; diff --git a/native/src/Update.h b/native/src/Update.h index 0fd096e..b48a463 100644 --- a/native/src/Update.h +++ b/native/src/Update.h @@ -14,6 +14,7 @@ struct UpdateInfo { std::string notes; std::string downloadUrl; std::string fileHash; + std::string manifestHash; // 服务端版本清单指纹,用于识别同版本号换文件 std::string entryFile; long long fileSize = 0; std::string error; diff --git a/native/src/Util.cpp b/native/src/Util.cpp index be61995..82e199c 100644 --- a/native/src/Util.cpp +++ b/native/src/Util.cpp @@ -401,4 +401,21 @@ void PlayAudioFile(const std::string& path) { PlaySystemAlert(); } +std::string UrlEncodeQuery(std::string_view s) { + static const char* hex = "0123456789ABCDEF"; + std::string out; + out.reserve(s.size() * 3); + for (unsigned char c : s) { + if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || + c == '-' || c == '_' || c == '.' || c == '~') { + out.push_back(static_cast(c)); + } else { + out.push_back('%'); + out.push_back(hex[(c >> 4) & 0x0F]); + out.push_back(hex[c & 0x0F]); + } + } + return out; +} + } diff --git a/native/src/Util.h b/native/src/Util.h index 4419291..4d30c15 100644 --- a/native/src/Util.h +++ b/native/src/Util.h @@ -25,6 +25,9 @@ std::string EscapeHtml(std::string_view value); std::string Trim(std::string_view s); +// 查询参数百分号编码(保留 RFC3986 unreserved 字符) +std::string UrlEncodeQuery(std::string_view s); + std::wstring GetLocalAppDataDir(); // 程序所在目录