Skip to content

Commit da52171

Browse files
committed
feat: sar_download_file
This could potentially be unsafe, but it could also automate srconfigs updates, challenge_maplist, fast taunts, containerridesave, vaultsave, tram save, etc. Just be careful with usage, it overwrites! Smartly reads through game directories for existing file to overwrite (even p2common), falls back to main game directory i.e. portal2 / portalreloaded / whatever if file doesn't exist. also fixed a bug with createTempPath -- instead of creating tmp_0_1_2 now it creates tmp_2
1 parent 0ffd4f7 commit da52171

3 files changed

Lines changed: 65 additions & 2 deletions

File tree

src/Features/ConfigPlus.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "Event.hpp"
22
#include "Features/Session.hpp"
3+
#include "Features/Updater.hpp"
34
#include "Modules/Client.hpp"
45
#include "Modules/Engine.hpp"
56
#include "Modules/FileSystem.hpp"
@@ -12,6 +13,8 @@
1213
#include <vector>
1314
#include <unordered_set>
1415
#include <fstream>
16+
#include <curl/curl.h>
17+
#include <regex>
1518

1619
// Fuck you Windows
1720
#ifdef _WIN32
@@ -55,6 +58,64 @@ static void SavePersistentSvars() {
5558
}
5659
}
5760

61+
CON_COMMAND_F(sar_download_file, "sar_download_file <url> <filepath> [directory] - Downloads a file from a URL and saves it to a path relative to game directory (e.g. portal2)\nIf directory isn't specified or invalid, looks for and overwrites existing file or uses base game directory\n", FCVAR_DONTRECORD) {
62+
if (args.ArgC() < 3 || args.ArgC() > 4 || !args[1][0] || !args[2][0]) {
63+
return console->Print(sar_download_file.ThisPtr()->m_pszHelpString);
64+
}
65+
66+
if (!Utils::StartsWith(args[1], "https://s.portal2.sr/") && !Utils::StartsWith(args[1], "https://raw.githubusercontent.com/p2sr/")) {
67+
return console->Print("URL domain is not portal2.sr.\n");
68+
}
69+
70+
std::string filepath = args[2];
71+
std::string gamedir = args.ArgC() > 3 ? args[3] : "";
72+
73+
auto result = fileSystem->FindFileSomewhere(filepath, gamedir);
74+
if (result.has_value()) {
75+
filepath = result.value();
76+
} else {
77+
filepath = std::string(engine->GetGameDirectory()) + "/" + filepath;
78+
if (gamedir != "") for (auto path : fileSystem->GetSearchPaths()) {
79+
if (path.find(gamedir) != std::string::npos) {
80+
filepath = path + filepath;
81+
break;
82+
}
83+
}
84+
}
85+
86+
CURL *curl = curl_easy_init();
87+
FILE *fp;
88+
CURLcode res;
89+
auto temp = createTempPath("sar_download_file.tmp");
90+
if (curl) {
91+
fp = fopen(temp.c_str(), "wb");
92+
curl_easy_setopt(curl, CURLOPT_URL, args[1]);
93+
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &fwrite);
94+
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
95+
res = curl_easy_perform(curl);
96+
curl_easy_cleanup(curl);
97+
fclose(fp);
98+
}
99+
100+
if (res == CURLE_OK) {
101+
102+
// Create any intermediate directories
103+
std::string dirpath = filepath;
104+
std::replace(dirpath.begin(), dirpath.end(), '\\', '/');
105+
dirpath = dirpath.substr(0, dirpath.rfind('/'));
106+
std::filesystem::create_directories(dirpath);
107+
108+
std::filesystem::remove(filepath);
109+
std::filesystem::copy(temp, filepath); // for some reason moving the file doesn't work
110+
111+
} else {
112+
console->Print("An error occurred\n");
113+
}
114+
115+
std::filesystem::remove(temp);
116+
117+
}
118+
58119
static void SetSvar(std::string name, std::string val) {
59120
g_svars[name] = val;
60121
if (g_persistentSvars.count(name) != 0) SavePersistentSvars();

src/Features/Updater.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ static bool getLatestVersion(std::string *name, std::string *dlUrl, std::string
199199
return true;
200200
}
201201

202-
static std::string createTempPath(const char *filename) {
202+
std::string createTempPath(const char *filename) {
203203
auto base = std::filesystem::temp_directory_path().append(filename);
204204
if (!std::filesystem::exists(base)) {
205205
return base.string();
@@ -209,7 +209,7 @@ static std::string createTempPath(const char *filename) {
209209
std::filesystem::path p;
210210

211211
do {
212-
p = base.concat(Utils::ssprintf("_%d", n++));
212+
p = base.string() + Utils::ssprintf("_%d", n++);
213213
} while (std::filesystem::exists(p));
214214

215215
return p.string();

src/Features/Updater.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
#pragma once
2+
3+
extern std::string createTempPath(const char *filename);

0 commit comments

Comments
 (0)