Clean up; add 2nd GD savefile

This commit is contained in:
xypwn 2020-05-05 12:18:28 +02:00
parent 3732939ef1
commit 9bbd5cf16a
2 changed files with 66 additions and 15 deletions

View File

@ -7,6 +7,15 @@
constexpr int CHUNK = 16384; constexpr int CHUNK = 16384;
constexpr int WINDOW_BITS = 15; constexpr int WINDOW_BITS = 15;
//Returns true if it was able to open the given file for reading
bool IsFile(const std::string& filename)
{
std::ifstream file(filename);
const bool ret = file.good();
file.close();
return ret;
}
//Reads the contents of a given file into a string //Reads the contents of a given file into a string
std::string FileToStr(const std::string& filename) { std::string FileToStr(const std::string& filename) {
std::ifstream file(filename, std::ios::binary); std::ifstream file(filename, std::ios::binary);
@ -18,11 +27,14 @@ std::string FileToStr(const std::string& filename) {
} }
//Writes the contents of a string to the given file //Writes the contents of a string to the given file
void StrToFile(const std::string& filename, const std::string& data) { // returns false if it was unable to open the file
std::ofstream ofile; bool StrToFile(const std::string& filename, const std::string& data) {
ofile.open(filename); std::ofstream ofile(filename, std::ios::binary);
if(!ofile.good())
return false;
ofile << data; ofile << data;
ofile.close(); ofile.close();
return true;
} }
//XORs each byte of a string reference //XORs each byte of a string reference

View File

@ -20,41 +20,80 @@ const std::string commands = R"(Commands:
3: Encrypt and save to GD folder 3: Encrypt and save to GD folder
0: Quit)"; 0: Quit)";
enum class Command
{
Decrypt,
Encrypt
};
bool ExecCommand(Command cmd, const std::string& in_filename, const std::string& out_filename)
{
if(!IsFile(in_filename))
{
std::cerr << "ERROR: Could not open file " << in_filename << " for reading" << std::endl;
return false;
}
switch(cmd)
{
case Command::Decrypt:
std::cout << "Decrypting " << in_filename << "..." << std::endl;
if(!StrToFile(out_filename, Decrypt(in_filename)))
{
std::cerr << "ERROR: Could not open file " << out_filename << " for writing" << std::endl;
return false;
}
break;
case Command::Encrypt:
std::cout << "Encrypting " << in_filename << "..." << std::endl;
if(!StrToFile(out_filename, Encrypt(in_filename)))
{
std::cerr << "ERROR: Could not open file " << out_filename << " for writing" << std::endl;
return false;
}
}
return true;
}
int main(int argc, const char** argv) { int main(int argc, const char** argv) {
std::vector<std::string> args; std::vector<std::string> args;
for(int i = 0; i < argc; i++) for(int i = 0; i < argc; i++)
args.push_back(argv[i]); args.push_back(argv[i]);
bool quit = false;
std::string cmd; std::string cmd;
while(!quit) while(true)
{ {
std::cout << commands << std::endl; std::cout << commands << std::endl;
std::cout << ">> "; std::cout << ">> ";
std::cin >> cmd; std::cin >> cmd;
if(cmd == "1") if(cmd == "1")
{ {
std::cout << "Decrypting..." << std::endl; for(unsigned int i = 0; i < 2; i++)
const std::string decrypted = Decrypt(gdLocalFolder + saves[0]); {
StrToFile(saves[0] + ".xml", decrypted); if(!ExecCommand(Command::Decrypt, gdLocalFolder + saves[i], saves[i] + ".xml"))
continue;
}
} }
else if(cmd == "2") else if(cmd == "2")
{ {
std::cout << "Encrypting..." << std::endl; for(unsigned int i = 0; i < 2; i++)
const std::string encrypted = Encrypt(saves[0] + ".xml"); {
StrToFile(saves[0], encrypted); if(!ExecCommand(Command::Encrypt, saves[i] + ".xml", saves[i]))
continue;
}
} }
else if(cmd == "3") else if(cmd == "3")
{ {
std::cout << "Encrypting and saving to GD folder..." << std::endl; for(unsigned int i = 0; i < 2; i++)
const std::string encrypted = Encrypt(saves[0] + ".xml"); {
StrToFile(gdLocalFolder + saves[0], encrypted); if(!ExecCommand(Command::Encrypt, saves[i] + ".xml", gdLocalFolder + saves[i]))
continue;
}
} }
else if(cmd == "0") else if(cmd == "0")
{ {
std::cout << "Quitting" << std::endl; std::cout << "Quitting" << std::endl;
quit = true; break;
} }
else else
std::cerr << "Invalid command" << std::endl; std::cerr << "Invalid command" << std::endl;