|
54 | 54 | #include <imgui/font_awesome.h> |
55 | 55 |
|
56 | 56 | #include <filesystem> |
| 57 | +#include <json/json.hpp> |
57 | 58 |
|
58 | 59 |
|
59 | 60 | #ifdef _WIN64 |
@@ -3732,40 +3733,109 @@ SK_FrameCallback ( SK_RenderBackend& rb, |
3732 | 3733 |
|
3733 | 3734 | if (config.platform.equivalent_steam_app == -1) |
3734 | 3735 | { |
3735 | | - SK_RunOnce ( |
3736 | | - auto appname = |
3737 | | - SK_GetFriendlyAppName (); |
| 3736 | + auto appname = |
| 3737 | + SK_GetFriendlyAppName (); |
3738 | 3738 |
|
| 3739 | + // Holy crap this is a mess; would use gamesdb.gog.com, but it is unclear how it |
| 3740 | + // generates "xboxone" (Microsoft Store) ids. |
| 3741 | + if (! appname.empty ()) |
| 3742 | + SK_RunOnce ( |
3739 | 3743 | std::wstring search_string = |
3740 | 3744 | SK_Network_MakeEscapeSequencedURL (SK_Platform_RemoveTrademarkSymbols (SK_UTF8ToWideChar (appname))); |
3741 | 3745 |
|
3742 | | - std::wstring url = |
| 3746 | + std::wstring url0 = |
3743 | 3747 | SK_FormatStringW ( |
3744 | | - LR"(https://www.pcgamingwiki.com/w/index.php?search=%ws)", search_string.c_str () |
| 3748 | + LR"(https://www.pcgamingwiki.com/w/api.php?action=opensearch&format=json&redirects=resolve&search=%ws)", search_string.c_str () |
3745 | 3749 | ); |
3746 | 3750 |
|
3747 | 3751 | if (! search_string.empty ()) |
3748 | 3752 | SK_Network_EnqueueDownload ( |
3749 | | - sk_download_request_s (L"pcgw_entry.html", url.data (), |
| 3753 | + sk_download_request_s (L"api.php", url0.data (), |
3750 | 3754 | []( const std::vector <uint8_t>&& data, |
3751 | | - const std::wstring_view file ) |
| 3755 | + const std::wstring_view ) |
3752 | 3756 | { |
3753 | 3757 | if (data.empty ()) |
3754 | 3758 | return true; |
3755 | 3759 |
|
3756 | | - std::ignore = file; |
| 3760 | + static SK_LazyGlobal <nlohmann::json> json; |
3757 | 3761 |
|
3758 | | - auto steamdb_appid = |
3759 | | - StrStrIA ((const char *)data.data (), "https://steamdb.info/app/"); |
| 3762 | + static std::wstring redirected_url = L""; |
3760 | 3763 |
|
3761 | | - if (steamdb_appid != nullptr) |
3762 | | - { |
3763 | | - if (1 != sscanf (steamdb_appid, "https://steamdb.info/app/%d/", &config.platform.equivalent_steam_app)) |
| 3764 | + try { |
| 3765 | + json.get () = std::move |
| 3766 | + ( nlohmann::json::parse |
| 3767 | + ( data.cbegin (), |
| 3768 | + data.cend (), |
| 3769 | + nullptr, |
| 3770 | + true ) ); |
| 3771 | + |
| 3772 | + for (const auto& text : json.get ()) |
3764 | 3773 | { |
3765 | | - config.platform.equivalent_steam_app = 0; |
| 3774 | + if (text.is_array ()) |
| 3775 | + { |
| 3776 | + for (const auto& entry : text) |
| 3777 | + { |
| 3778 | + if (entry.is_string ()) |
| 3779 | + { |
| 3780 | + redirected_url = |
| 3781 | + SK_FormatStringW ( |
| 3782 | + LR"(https://www.pcgamingwiki.com/w/api.php?action=parse&format=xml&redirects=1&prop=wikitext&page=%hs)", |
| 3783 | + entry.get <std::string> ().c_str () |
| 3784 | + ); |
| 3785 | + |
| 3786 | + break; |
| 3787 | + } |
| 3788 | + } |
| 3789 | + |
| 3790 | + break; |
| 3791 | + } |
3766 | 3792 | } |
3767 | 3793 | } |
3768 | 3794 |
|
| 3795 | + catch (const std::exception& e) |
| 3796 | + { |
| 3797 | + SK_LOGi0 (L"JSON Parse Failure: %hs", e.what ()); |
| 3798 | + } |
| 3799 | + |
| 3800 | + if (! redirected_url.empty ()) |
| 3801 | + { |
| 3802 | + SK_Network_EnqueueDownload ( |
| 3803 | + sk_download_request_s (L"api.php", redirected_url.data (), |
| 3804 | + []( const std::vector <uint8_t>&& data, |
| 3805 | + const std::wstring_view ) |
| 3806 | + { |
| 3807 | + if (data.empty ()) |
| 3808 | + return true; |
| 3809 | + |
| 3810 | + std::string data_str ( |
| 3811 | + data.begin (), data.end () ); |
| 3812 | + |
| 3813 | + SK_LOGi1 (L"PCGW Query Response for URL %ws: %hs", redirected_url.c_str (), |
| 3814 | + data_str.c_str ()); |
| 3815 | + |
| 3816 | + auto steam_appid = |
| 3817 | + StrStrIA (data_str.c_str (), "|steam appid "); |
| 3818 | + |
| 3819 | + steam_appid = steam_appid == nullptr ? |
| 3820 | + nullptr : |
| 3821 | + StrStrIA (steam_appid, "= "); |
| 3822 | + |
| 3823 | + if (steam_appid != nullptr) |
| 3824 | + { |
| 3825 | + if (1 != sscanf (steam_appid + 1, "%d", &config.platform.equivalent_steam_app)) |
| 3826 | + { |
| 3827 | + config.platform.equivalent_steam_app = 0; |
| 3828 | + } |
| 3829 | + } |
| 3830 | + |
| 3831 | + config.utility.save_async (); |
| 3832 | + |
| 3833 | + return true; |
| 3834 | + } ), |
| 3835 | + false |
| 3836 | + ); |
| 3837 | + } |
| 3838 | + |
3769 | 3839 | return true; |
3770 | 3840 | } ), |
3771 | 3841 | false |
|
0 commit comments