Emscripten powershell build script updated to properly terminate on errors. Add in proper throws when an invalid response is given from the server. Add in function response callbacks to all server request functions.

This commit is contained in:
sigonasr2 2024-01-02 20:58:04 -06:00
parent 497db80b9b
commit 7bfa728ccb
10 changed files with 55 additions and 24 deletions

View File

@ -2565,7 +2565,7 @@ void Crawler::ResetGame(){
}
void Crawler::OnRequestCompleted(const std::string_view receivedData)const{
std::cout<<"Received in engine: "<<receivedData<<std::endl;
responseCallback(receivedData);
}
std::string operator ""_FS(const char*key,std::size_t len){

View File

@ -115,6 +115,7 @@ private:
bool gameEnd=false;
std::vector<Monster>monstersToBeSpawned;
time_t gameStarted;
std::function<void(std::string_view)>responseCallback;
void ValidateGameStatus();
#ifndef __EMSCRIPTEN__

View File

@ -58,7 +58,19 @@ void Menu::InitializeMainMenuWindow(){
})END;
mainMenuWindow->ADD("Load Game Button",MenuComponent)({{12,36},{72,24}},"Load Game",[](MenuFuncData data){
SaveFile::UpdateSaveGameData();
Menu::OpenMenu(LOAD_GAME);
#ifdef __EMSCRIPTEN__
SaveFile::Server_GetLoadInfo([](std::string_view response){
if(response!="ERR"){
std::ofstream file("save_file_path"_S+"metadata.dat");
file<<response;
Menu::OpenMenu(LOAD_GAME);
}else{
std::cout<<"WARNING! Failed to retrieve load game information from the server!"<<std::endl;
}
});
#else
Menu::OpenMenu(LOAD_GAME);
#endif
return true;
})END;
mainMenuWindow->ADD("Quit Game Button",MenuComponent)({{12,68},{72,24}},"Quit Game",[](MenuFuncData data){

View File

@ -78,8 +78,8 @@ protected:
vf2d adjustedScale={scale,scale};
vf2d labelTextSize=
proportional?
vf2d(game->GetWrappedTextSizeProp(label,rect.size.x-8,adjustedScale)):
vf2d(game->GetWrappedTextSize(label,rect.size.x-8,adjustedScale));
vf2d(game->GetWrappedTextSizeProp(label,rect.size.x-2,adjustedScale)):
vf2d(game->GetWrappedTextSize(label,rect.size.x-2,adjustedScale));
if(fitToLabel){
labelTextSize=
@ -93,7 +93,7 @@ protected:
}
}
vf2d drawPos=vf2d{-4.f,0.f}+rect.middle()-vf2d{labelTextSize}/2; //Assume centered.
vf2d drawPos=vf2d{-1,0}+rect.middle()-vf2d{labelTextSize}/2; //Assume centered.
if(!centered){
drawPos=vf2d{rect.pos.x+2,rect.middle().y-labelTextSize.y/2}; //We should at least vertically align here.
}

View File

@ -116,13 +116,13 @@ const void SaveFile::SaveGame(){
utils::datafile::INITIAL_SETUP_COMPLETE=true;
#ifdef __EMSCRIPTEN__
Server_SaveFile();
Server_SaveFile([](std::string_view response){});
#endif
}
const void SaveFile::LoadGame(){
#ifdef __EMSCRIPTEN__
Server_GetFile();
Server_GetFile([](std::string_view response){});
#else
std::filesystem::create_directories("save_file_path"_S);
utils::datafile loadFile;
@ -224,23 +224,39 @@ const std::string SaveFile::CreateServerRequest(const SaveFileOperation::Operati
operationName=std::format("SAVE_FILE");
//Data should contain the entire contents of our save file.
}break;
case SaveFileOperation::SAVE_METADATA_FILE:{
operationName=std::format("SAVE_METADATA_FILE");
//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;
}
const void SaveFile::Server_GetLoadInfo(){
const void SaveFile::Server_GetLoadInfo(std::function<void(std::string_view)>respCallbackFunc){
game->SendRequest(CreateServerRequest(SaveFileOperation::GET_LOAD_FILES,"0"),"");
game->responseCallback=respCallbackFunc;
}
const void SaveFile::Server_GetFile(){
const void SaveFile::Server_GetFile(std::function<void(std::string_view)>respCallbackFunc){
game->SendRequest(CreateServerRequest(SaveFileOperation::GET_FILE,std::to_string(saveFileID)),"");
game->responseCallback=respCallbackFunc;
}
const void SaveFile::Server_SaveFile(){
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());
}
game->SendRequest(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());
}
game->SendRequest(CreateServerRequest(SaveFileOperation::SAVE_METADATA_FILE,fileContents.str()),"");
game->responseCallback=respCallbackFunc;
}

View File

@ -43,6 +43,7 @@ namespace SaveFileOperation{
enum Operation{
GET_LOAD_FILES,
GET_FILE,
SAVE_METADATA_FILE,
SAVE_FILE,
};
};
@ -61,7 +62,8 @@ public:
//Called whenever the save game data is updated.
static const void UpdateSaveGameData();
static const std::string CreateServerRequest(const SaveFileOperation::Operation operation,std::string_view data);
static const void Server_GetLoadInfo();
static const void Server_GetFile();
static const void Server_SaveFile();
static const void Server_GetLoadInfo(std::function<void(std::string_view)>respCallbackFunc);
static const void Server_GetFile(std::function<void(std::string_view)>respCallbackFunc);
static const void Server_SaveFile(std::function<void(std::string_view)>respCallbackFunc);
static const void Server_SaveMetadataFile(std::function<void(std::string_view)>respCallbackFunc);
};

View File

@ -39,7 +39,7 @@ All rights reserved.
#define VERSION_MAJOR 0
#define VERSION_MINOR 2
#define VERSION_PATCH 1
#define VERSION_BUILD 5285
#define VERSION_BUILD 5287
#define stringify(a) stringify_(a)
#define stringify_(a) #a

View File

@ -1,2 +1,2 @@
save0 = 534.977882, 1, 2, Ranger, Sig
save0 = 641.829219, 1, 2, Ranger, Sig
save1 = 107.824620, 1, 1, Wizard, WIZ

View File

@ -1,12 +1,6 @@
$ErrorActionPreference = "Stop"
cd ..
./emscripten_build.ps1
if( -not $? )
{
$msg = $Error[0].Exception.Message
"$msg"
exit
}
else
{
if ($LASTEXITCODE -eq 0) {
./emscripten_run.ps1
}

View File

@ -7217,7 +7217,13 @@ namespace olc
virtual olc::rcode SendRequest(std::string_view url,std::string_view data)override{
EM_ASM({
requestResp="";
fetch(UTF8ToString($0)).then((resp)=>resp.text()).then((data)=>{requestResp=data});
fetch(UTF8ToString($0)).then((resp)=>{
if(resp.ok){
resp.text();
}
throw new Error(resp.text());
}).then((data)=>{requestResp=data;})
.catch((err)=>{requestResp="ERR";});
},std::string(url).c_str());
return olc::rcode::OK;
}