Skip to content

Commit af9240e

Browse files
aIIisonThisAMJ
authored andcommitted
feat: convert old api key format to new one automatically
1 parent 9e75871 commit af9240e

1 file changed

Lines changed: 44 additions & 0 deletions

File tree

src/Features/AutoSubmit.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#define AUTOSUBMIT_TOAST_TAG "autosubmit"
3030
#define COOP_NAME_MESSAGE_TYPE "coop-name"
3131
#define API_KEY_FILE "autosubmit.key"
32+
#define OLD_API_KEY_FILE "autosubmit_key.txt"
3233

3334
bool AutoSubmit::g_cheated = false;
3435
std::string AutoSubmit::g_partner_name = "";
@@ -410,7 +411,50 @@ static void submitTime(int score, std::string demopath, bool coop, std::string m
410411
}
411412
}
412413

414+
static void convertOldApiKeys() {
415+
auto old_filepath = fileSystem->FindFileSomewhere(OLD_API_KEY_FILE);
416+
if (!old_filepath.has_value())
417+
return;
418+
419+
/* read key from old file */
420+
std::string key;
421+
{
422+
std::ifstream f(old_filepath.value());
423+
std::stringstream buf;
424+
buf << f.rdbuf();
425+
key = buf.str();
426+
}
427+
428+
key.erase(std::remove_if(key.begin(), key.end(), isspace), key.end());
429+
430+
std::ofstream f;
431+
432+
/* check if user has a key with new format already, if yes, append to end, if not, create new file */
433+
auto new_filepath = fileSystem->FindFileSomewhere(API_KEY_FILE);
434+
if (!new_filepath.has_value()) {
435+
/* create new file path */
436+
auto path = old_filepath.value();
437+
path = path.substr(0, path.length() - strlen(OLD_API_KEY_FILE));
438+
439+
f.open(path + API_KEY_FILE);
440+
} else
441+
f.open(new_filepath.value(), std::ios::app); // open in append mode
442+
443+
/* file should have newline at end! */
444+
f << "board.portal2.sr" << std::endl;
445+
f << key << std::endl;
446+
447+
f.close();
448+
449+
/* remove old api key file */
450+
std::filesystem::remove(old_filepath.value());
451+
452+
console->Print("Successfully converted API key file!\n");
453+
}
454+
413455
void AutoSubmit::LoadApiKey(bool output_nonexist) {
456+
convertOldApiKeys();
457+
414458
auto filepath = fileSystem->FindFileSomewhere(API_KEY_FILE);
415459
if (!filepath.has_value()) {
416460
if (output_nonexist) {

0 commit comments

Comments
 (0)