|
|
|
@ -209,7 +209,25 @@ const std::string SaveFile::CreateServerRequest(const SaveFileOperation::Operati |
|
|
|
|
auto CalculateChecksum=[](std::string_view operation,std::string_view data){ |
|
|
|
|
return username.length()*8+data.length()*2+operation.length(); |
|
|
|
|
}; |
|
|
|
|
std::string dataString=std::format("?username={}",username); |
|
|
|
|
auto EncodeURI=[](std::string str){ |
|
|
|
|
return std::accumulate(str.begin(),str.end(),""s,[](std::string str,const char&c){ |
|
|
|
|
/*
|
|
|
|
|
* Implementation Source: https://262.ecma-international.org/5.1/#sec-15.1.3.4
|
|
|
|
|
uriReserved ::: one of |
|
|
|
|
; / ? : @ & = + $ , |
|
|
|
|
uriMark ::: one of |
|
|
|
|
- _ . ! ~ * ' ( ) |
|
|
|
|
Let unescapedURISet be a String containing one instance of each character valid in uriReserved and uriUnescaped plus “#”. |
|
|
|
|
*/ |
|
|
|
|
const std::array uriReserved={';','/','?',':','@','&','=','+','$',',','-','_','.','!','~','*','\'','(',')','#'}; |
|
|
|
|
if(c>='A'&&c<='Z'||c>='a'&&c<='z'||c>='0'&&c<='9'||std::find(uriReserved.begin(),uriReserved.end(),c)!=uriReserved.end())return std::move(str)+c; |
|
|
|
|
|
|
|
|
|
std::string convertedChar=std::format("%{:02x}",c); |
|
|
|
|
std::for_each(convertedChar.begin(),convertedChar.end(),[](char&c){c=char(std::toupper(c));}); |
|
|
|
|
return std::move(str)+convertedChar; |
|
|
|
|
}); |
|
|
|
|
}; |
|
|
|
|
std::string dataString=std::format("\"username\":\"{}\"",EncodeURI(username)); |
|
|
|
|
std::string operationName=""; |
|
|
|
|
switch(operation){ |
|
|
|
|
case SaveFileOperation::GET_LOAD_FILES:{ |
|
|
|
@ -229,35 +247,41 @@ const std::string SaveFile::CreateServerRequest(const SaveFileOperation::Operati |
|
|
|
|
//Data should contain the entire contents of our metadata save file.
|
|
|
|
|
}break; |
|
|
|
|
} |
|
|
|
|
dataString+="&operation="+operationName; |
|
|
|
|
dataString+="&checksum="+std::to_string(CalculateChecksum(operationName,data)); |
|
|
|
|
dataString+="&data="+std::string(data); |
|
|
|
|
return "save_server"_S+dataString; |
|
|
|
|
dataString+=",\"operation\":\""+EncodeURI(operationName)+"\""; |
|
|
|
|
dataString+=",\"checksum\":\""+EncodeURI(std::to_string(CalculateChecksum(operationName,data)))+"\""; |
|
|
|
|
dataString+=",\"data\":\""+EncodeURI(std::string(data))+"\""; |
|
|
|
|
return "{"+dataString+"}"; |
|
|
|
|
} |
|
|
|
|
const void SaveFile::Server_GetLoadInfo(std::function<void(std::string_view)>respCallbackFunc){ |
|
|
|
|
game->SendRequest(CreateServerRequest(SaveFileOperation::GET_LOAD_FILES,"0"),""); |
|
|
|
|
game->SendRequest("save_server"_S,CreateServerRequest(SaveFileOperation::GET_LOAD_FILES,"0")); |
|
|
|
|
game->responseCallback=respCallbackFunc; |
|
|
|
|
} |
|
|
|
|
const void SaveFile::Server_GetFile(std::function<void(std::string_view)>respCallbackFunc){ |
|
|
|
|
game->SendRequest(CreateServerRequest(SaveFileOperation::GET_FILE,std::to_string(saveFileID)),""); |
|
|
|
|
game->SendRequest("save_server"_S,CreateServerRequest(SaveFileOperation::GET_FILE,std::to_string(saveFileID))); |
|
|
|
|
game->responseCallback=respCallbackFunc; |
|
|
|
|
} |
|
|
|
|
const void SaveFile::Server_SaveFile(std::function<void(std::string_view)>respCallbackFunc){ |
|
|
|
|
std::stringstream fileContents; |
|
|
|
|
std::ifstream file("save_file_path"_S+std::format("save.{:04}",saveFileID)); |
|
|
|
|
while(file.good()){ |
|
|
|
|
fileContents<<char(file.get()); |
|
|
|
|
int val=file.get(); |
|
|
|
|
if(val!=-1){ |
|
|
|
|
fileContents<<char(val); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
game->SendRequest(CreateServerRequest(SaveFileOperation::SAVE_FILE,std::to_string(saveFileID)+"|"+fileContents.str()),""); |
|
|
|
|
game->SendRequest("save_server"_S,CreateServerRequest(SaveFileOperation::SAVE_FILE,std::to_string(saveFileID)+"|"+fileContents.str())); |
|
|
|
|
game->responseCallback=respCallbackFunc; |
|
|
|
|
} |
|
|
|
|
const void SaveFile::Server_SaveMetadataFile(std::function<void(std::string_view)>respCallbackFunc){ |
|
|
|
|
std::stringstream fileContents; |
|
|
|
|
std::ifstream file("save_file_path"_S+"metadata.dat"); |
|
|
|
|
while(file.good()){ |
|
|
|
|
fileContents<<char(file.get()); |
|
|
|
|
int val=file.get(); |
|
|
|
|
if(val!=-1){ |
|
|
|
|
fileContents<<char(val); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
game->SendRequest(CreateServerRequest(SaveFileOperation::SAVE_METADATA_FILE,fileContents.str()),""); |
|
|
|
|
game->SendRequest("save_server"_S,CreateServerRequest(SaveFileOperation::SAVE_METADATA_FILE,fileContents.str())); |
|
|
|
|
game->responseCallback=respCallbackFunc; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|