|
5 | 5 | #include "TTFSDK.h" |
6 | 6 |
|
7 | 7 | ConVar* bme_cl_save_scoreboards; |
| 8 | +ConVar* bme_cl_save_scoreboards_upload_url; |
| 9 | + |
| 10 | +struct ScoreboardUploadInfo |
| 11 | +{ |
| 12 | + fs::path path; |
| 13 | +}; |
| 14 | + |
| 15 | +DWORD WINAPI ScoreboardUploadThread(PVOID pThreadParameter) |
| 16 | +{ |
| 17 | + auto* infoPtr = reinterpret_cast<ScoreboardUploadInfo*>(pThreadParameter); |
| 18 | + ScoreboardUploadInfo info = *infoPtr; |
| 19 | + delete infoPtr; |
| 20 | + |
| 21 | + auto* url = bme_cl_save_scoreboards_upload_url->GetString(); |
| 22 | + spdlog::info("[ScoreboardUploadThread] Will try to upload scoreboard"); |
| 23 | + |
| 24 | + auto curl = curl_easy_init(); |
| 25 | + if (!curl) |
| 26 | + return 1; |
| 27 | + |
| 28 | + curl_easy_setopt(curl, CURLOPT_URL, url); |
| 29 | + curl_easy_setopt(curl, CURLOPT_ACCEPT_ENCODING, ""); // automatically uses all built-in supported encodings |
| 30 | + curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0); |
| 31 | + curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L); |
| 32 | + //curl_easy_setopt(curl, CURLOPT_TCP_KEEPALIVE, 0L); |
| 33 | + |
| 34 | + FILE* fd = _wfopen(info.path.c_str(), L"rb"); |
| 35 | + if (!fd) |
| 36 | + { |
| 37 | + spdlog::error("[ScoreboardUploadThread] Error opening file"); |
| 38 | + return 1; |
| 39 | + } |
| 40 | + |
| 41 | + struct stat file_info; |
| 42 | + if (fstat(fileno(fd), &file_info) != 0) |
| 43 | + { |
| 44 | + spdlog::error("[ScoreboardUploadThread] Error opening file"); |
| 45 | + curl_easy_cleanup(curl); |
| 46 | + return 1; |
| 47 | + } |
| 48 | + |
| 49 | + curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L); |
| 50 | + curl_easy_setopt(curl, CURLOPT_READDATA, fd); |
| 51 | + curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, (curl_off_t)file_info.st_size); |
| 52 | + |
| 53 | + TFOrigin* origin = SDK().origin; |
| 54 | + struct curl_slist* list = NULL; |
| 55 | + if (origin && origin->isInitialized && origin->isOnline && !origin->isLoggedOut && origin->uid) |
| 56 | + { |
| 57 | + list = curl_slist_append(list, ("X-Origin-Uid: " + std::to_string(origin->uid)).c_str()); |
| 58 | + list = curl_slist_append(list, ("X-Origin-Name: " + std::string(origin->playerName)).c_str()); |
| 59 | + } |
| 60 | + else |
| 61 | + { |
| 62 | + ConVar* platform_user_id = SDK().GetVstdlibCvar()->FindVar("platform_user_id"); |
| 63 | + ConVar* name = SDK().GetVstdlibCvar()->FindVar("name"); |
| 64 | + list = curl_slist_append(list, ("X-Origin-Uid: " + std::string(platform_user_id->GetString())).c_str()); |
| 65 | + list = curl_slist_append(list, ("X-Origin-Name: " + std::string(name->GetString())).c_str()); |
| 66 | + } |
| 67 | + list = curl_slist_append(list, ("X-File-Name: " + info.path.filename().string()).c_str()); |
| 68 | + list = curl_slist_append(list, "Content-Type: application/json"); |
| 69 | + curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list); |
| 70 | + |
| 71 | + CURLcode res = curl_easy_perform(curl); |
| 72 | + |
| 73 | + if (res != CURLE_OK) |
| 74 | + { |
| 75 | + spdlog::error("[ScoreboardUploadThread] curl_easy_perform() failed: {}", curl_easy_strerror(res)); |
| 76 | + curl_easy_cleanup(curl); |
| 77 | + return 1; |
| 78 | + } |
| 79 | + |
| 80 | + long response_code; |
| 81 | + curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code); |
| 82 | + if (response_code != 200) |
| 83 | + { |
| 84 | + spdlog::error("[ScoreboardUploadThread] upload failed, response code: {}", response_code); |
| 85 | + curl_easy_cleanup(curl); |
| 86 | + return 1; |
| 87 | + } |
| 88 | + |
| 89 | + curl_off_t speed_upload, total_time; |
| 90 | + curl_easy_getinfo(curl, CURLINFO_SPEED_UPLOAD_T, &speed_upload); |
| 91 | + curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME_T, &total_time); |
| 92 | + |
| 93 | + spdlog::info("[ScoreboardUploadThread] Scoreboard uploaded, speed: {} bytes/sec during {} seconds", |
| 94 | + (unsigned long)speed_upload, (unsigned long)((double)total_time / 1000000.0)); |
| 95 | + |
| 96 | + curl_easy_cleanup(curl); |
| 97 | + return 0; |
| 98 | +} |
8 | 99 |
|
9 | 100 | SQRESULT SaveScoreboard_Script(HSQUIRRELVM v) |
10 | 101 | { |
@@ -67,11 +158,21 @@ SQRESULT SaveScoreboard_Script(HSQUIRRELVM v) |
67 | 158 | outfile << out; |
68 | 159 | outfile.close(); |
69 | 160 |
|
| 161 | + if (strlen(bme_cl_save_scoreboards_upload_url->GetString()) > 0) |
| 162 | + { |
| 163 | + auto* info = new ScoreboardUploadInfo; |
| 164 | + info->path = outpath; |
| 165 | + CreateThread(0, 0, ScoreboardUploadThread, info, 0, NULL); |
| 166 | + } |
| 167 | + |
70 | 168 | return 0; |
71 | 169 | } |
72 | 170 |
|
73 | 171 | void ScoreboardSave_Init(SquirrelManager& sqManager, ConCommandManager& ccManager) |
74 | 172 | { |
75 | 173 | sqManager.AddFuncRegistrationAllContexts("SaveScoreboard", SaveScoreboard_Script, ".s.", 0, "string", "string filename, void input", ""); |
76 | | - ccManager.RegisterConVar("bme_cl_save_scoreboards", "0", FCVAR_DONTRECORD, ""); |
| 174 | + bme_cl_save_scoreboards = |
| 175 | + ccManager.RegisterConVar("bme_cl_save_scoreboards", "0", FCVAR_DONTRECORD, "Whether to save scoreboards in JSON files"); |
| 176 | + bme_cl_save_scoreboards_upload_url = |
| 177 | + ccManager.RegisterConVar("bme_cl_save_scoreboards_upload_url", "", FCVAR_DONTRECORD, "Optional URL to also upload the saved scoreboards to (must enable the feature with the other cvar too for this to work)"); |
77 | 178 | } |
0 commit comments