|
15 | 15 | #include <cstdio> |
16 | 16 | #include <cstring> |
17 | 17 | #include <cstdint> |
18 | | -#include <limits> |
19 | 18 | #include <string> |
20 | 19 | #include <vector> |
21 | 20 |
|
@@ -84,7 +83,15 @@ bool is_regular_file(const char* path) { |
84 | 83 |
|
85 | 84 | bool is_running_under_wine() { |
86 | 85 | HMODULE ntdll = GetModuleHandleA("ntdll.dll"); |
87 | | - return ntdll && GetProcAddress(ntdll, "wine_get_version"); |
| 86 | + if (!ntdll) return false; |
| 87 | + typedef const char* (*wine_get_version_fn)(void); |
| 88 | + auto fn = reinterpret_cast<wine_get_version_fn>(GetProcAddress(ntdll, "wine_get_version")); |
| 89 | + if (!fn) return false; |
| 90 | + const char* ver = fn(); |
| 91 | + if (!ver || !ver[0]) return false; |
| 92 | + for (const char* p = ver; *p; ++p) |
| 93 | + if (std::isdigit(static_cast<unsigned char>(*p))) return true; |
| 94 | + return false; |
88 | 95 | } |
89 | 96 |
|
90 | 97 | bool prefer_linux_wine_release_zip() { |
@@ -594,80 +601,44 @@ std::string zip_asset_name_from_url(const std::string& url) { |
594 | 601 | return to_lower_ascii(asset_name_from_url(url)); |
595 | 602 | } |
596 | 603 |
|
597 | | -int score_release_zip_candidate(const std::string& url) { |
598 | | - const std::string name = zip_asset_name_from_url(url); |
599 | | - if (name.size() < 4 || name.compare(name.size() - 4, 4, ".zip") != 0) |
600 | | - return std::numeric_limits<int>::min(); |
601 | | - |
602 | | - const bool is_debug = (name.find("debug") != std::string::npos); |
603 | | - const bool is_release = (name.find("release") != std::string::npos); |
604 | | - const bool has_sofbuddy = (name.find("sof_buddy") != std::string::npos) || |
605 | | - (name.find("sofbuddy") != std::string::npos); |
606 | | - const bool is_windows = (name.find("windows") != std::string::npos); |
607 | | - const bool is_linux_wine = (name.find("linux") != std::string::npos) || |
608 | | - (name.find("wine") != std::string::npos); |
609 | | - const bool is_xp = (name.find("xp") != std::string::npos); |
610 | | - const bool prefer_linux_wine = prefer_linux_wine_release_zip(); |
611 | | - |
612 | | - int score = 0; |
613 | | - if (is_release) score += 400; |
614 | | - if (has_sofbuddy) score += 120; |
615 | | - if (is_windows) score += 30; |
616 | | - if (is_linux_wine) score += 20; |
617 | | - if (is_xp) score -= 40; |
618 | | - if (is_debug) score -= 500; |
619 | | - |
620 | | - if (name == "release_windows.zip") score += 200; |
621 | | - else if (name == "release_linux_wine.zip") score += 180; |
622 | | - else if (name == "release_windows_xp.zip") score += 100; |
623 | | - else if (name == "debug_windows.zip") score -= 100; |
624 | | - else if (name == "debug_windows_xp.zip") score -= 150; |
625 | | - |
| 604 | +void preferred_release_zip_names(std::vector<std::string>& out) { |
| 605 | + out.clear(); |
626 | 606 | #if defined(SOFBUDDY_XP_BUILD) |
627 | | - if (is_windows && is_xp) score += 1200; |
628 | | - if (is_windows && !is_xp) score -= 400; |
629 | | - if (is_linux_wine) score -= 700; |
| 607 | + out.push_back("release_windows_xp.zip"); |
630 | 608 | #else |
631 | | - if (prefer_linux_wine) { |
632 | | - if (is_linux_wine) score += 1200; |
633 | | - if (is_windows && !is_xp) score -= 150; |
634 | | - if (is_windows && is_xp) score -= 700; |
635 | | - } else { |
636 | | - if (is_windows && !is_xp) score += 1200; |
637 | | - if (is_windows && is_xp) score -= 700; |
638 | | - if (is_linux_wine) score -= 150; |
| 609 | + if (prefer_linux_wine_release_zip()) |
| 610 | + out.push_back("release_linux_wine.zip"); |
| 611 | + else { |
| 612 | + out.push_back("release_windows.zip"); |
| 613 | + out.push_back("release_windows_xp.zip"); |
639 | 614 | } |
640 | 615 | #endif |
641 | | - |
642 | | - return score; |
643 | 616 | } |
644 | 617 |
|
645 | | -bool zip_matches_build_preference(const std::string& url) { |
646 | | - const std::string name = zip_asset_name_from_url(url); |
647 | | - if (name.empty()) return false; |
648 | | - if (name.size() < 4 || name.compare(name.size() - 4, 4, ".zip") != 0) return false; |
| 618 | +bool is_preferred_release_zip(const std::string& name) { |
| 619 | + if (name.empty() || name.size() < 4 || name.compare(name.size() - 4, 4, ".zip") != 0) |
| 620 | + return false; |
649 | 621 | if (name.find("debug") != std::string::npos) return false; |
| 622 | + std::vector<std::string> preferred; |
| 623 | + preferred_release_zip_names(preferred); |
| 624 | + for (const auto& p : preferred) |
| 625 | + if (name == p) return true; |
| 626 | + return false; |
| 627 | +} |
650 | 628 |
|
651 | | - const bool is_windows = (name.find("windows") != std::string::npos); |
652 | | - const bool is_linux_wine = (name.find("linux") != std::string::npos) || |
653 | | - (name.find("wine") != std::string::npos); |
654 | | - const bool is_xp = (name.find("xp") != std::string::npos); |
655 | | - const bool prefer_linux_wine = prefer_linux_wine_release_zip(); |
656 | | - |
657 | | -#if defined(SOFBUDDY_XP_BUILD) |
658 | | - if (is_windows && is_xp) return true; |
659 | | - if (is_windows && !is_xp) return false; |
660 | | - if (is_linux_wine) return false; |
661 | | -#else |
662 | | - if (prefer_linux_wine) { |
663 | | - if (is_linux_wine) return true; |
664 | | - if (is_windows && !is_xp) return false; |
665 | | - if (is_windows && is_xp) return false; |
| 629 | +std::string pick_best_zip_url(const std::vector<std::string>& candidates) { |
| 630 | + std::vector<std::string> preferred; |
| 631 | + preferred_release_zip_names(preferred); |
| 632 | + for (const auto& name : preferred) { |
| 633 | + for (const auto& url : candidates) { |
| 634 | + if (zip_asset_name_from_url(url) == name) return url; |
| 635 | + } |
666 | 636 | } |
667 | | - if (is_windows && !is_xp) return true; |
668 | | - if (is_windows && is_xp) return false; |
669 | | -#endif |
670 | | - return true; |
| 637 | + return std::string(); |
| 638 | +} |
| 639 | + |
| 640 | +bool zip_matches_build_preference(const std::string& url) { |
| 641 | + return is_preferred_release_zip(zip_asset_name_from_url(url)); |
671 | 642 | } |
672 | 643 |
|
673 | 644 | std::vector<int> parse_version_parts(const std::string& version_text) { |
@@ -788,15 +759,12 @@ bool fetch_latest_release(ReleaseInfo& out, std::string& error) { |
788 | 759 | } |
789 | 760 |
|
790 | 761 | out.zip_url.clear(); |
791 | | - int best_score = std::numeric_limits<int>::min(); |
| 762 | + std::vector<std::string> candidates; |
792 | 763 | size_t search = 0; |
793 | 764 | std::string candidate; |
794 | | - while (json_find_string_field(body, "browser_download_url", search, candidate, &search)) { |
795 | | - const int score = score_release_zip_candidate(candidate); |
796 | | - if (score <= best_score) continue; |
797 | | - best_score = score; |
798 | | - out.zip_url = candidate; |
799 | | - } |
| 765 | + while (json_find_string_field(body, "browser_download_url", search, candidate, &search)) |
| 766 | + candidates.push_back(candidate); |
| 767 | + out.zip_url = pick_best_zip_url(candidates); |
800 | 768 | if (out.zip_url.empty()) { |
801 | 769 | std::string fallback_zip_url; |
802 | 770 | if (json_find_string_field(body, "zip_url", 0, fallback_zip_url, nullptr) || |
|
0 commit comments