diff --git a/CMakeLists.txt b/CMakeLists.txt index 56e2709e..eb0f9627 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -302,6 +302,7 @@ if (EMSCRIPTEN) -sUSE_FREETYPE=1 -sLLD_REPORT_UNDEFINED -sFETCH=1 + -sEXPORTED_RUNTIME_METHODS=stringToNewUTF8 -std=c++20 --proxy-to-worker -O2 @@ -316,6 +317,7 @@ if (EMSCRIPTEN) -sUSE_LIBPNG=1 -sUSE_FREETYPE=1 -sFETCH=1 + -sEXPORTED_RUNTIME_METHODS=stringToNewUTF8 -std=c++20 --proxy-to-worker -O2 diff --git a/Crawler.sln b/Crawler.sln index f4d5657f..1363192c 100644 --- a/Crawler.sln +++ b/Crawler.sln @@ -9,6 +9,8 @@ Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|x64 = Debug|x64 Debug|x86 = Debug|x86 + Emscripten|x64 = Emscripten|x64 + Emscripten|x86 = Emscripten|x86 Release Desktop|x64 = Release Desktop|x64 Release Desktop|x86 = Release Desktop|x86 Release|x64 = Release|x64 @@ -19,6 +21,10 @@ Global {8E3067AF-CFE7-4B11-BC6B-B867C32753D7}.Debug|x64.Build.0 = Debug|x64 {8E3067AF-CFE7-4B11-BC6B-B867C32753D7}.Debug|x86.ActiveCfg = Debug|Win32 {8E3067AF-CFE7-4B11-BC6B-B867C32753D7}.Debug|x86.Build.0 = Debug|Win32 + {8E3067AF-CFE7-4B11-BC6B-B867C32753D7}.Emscripten|x64.ActiveCfg = Emscripten|x64 + {8E3067AF-CFE7-4B11-BC6B-B867C32753D7}.Emscripten|x64.Build.0 = Emscripten|x64 + {8E3067AF-CFE7-4B11-BC6B-B867C32753D7}.Emscripten|x86.ActiveCfg = Emscripten|Win32 + {8E3067AF-CFE7-4B11-BC6B-B867C32753D7}.Emscripten|x86.Build.0 = Emscripten|Win32 {8E3067AF-CFE7-4B11-BC6B-B867C32753D7}.Release Desktop|x64.ActiveCfg = Release Desktop|x64 {8E3067AF-CFE7-4B11-BC6B-B867C32753D7}.Release Desktop|x64.Build.0 = Release Desktop|x64 {8E3067AF-CFE7-4B11-BC6B-B867C32753D7}.Release Desktop|x86.ActiveCfg = Release Desktop|Win32 diff --git a/Crawler/Crawler.cpp b/Crawler/Crawler.cpp index 9188f7d0..a14d6257 100644 --- a/Crawler/Crawler.cpp +++ b/Crawler/Crawler.cpp @@ -239,7 +239,7 @@ bool Crawler::OnUserCreate(){ SetupDiscord(); #endif - SendRequest("http://projectdivar.com/files/index.html",""); + SendRequest("index.html",""); return true; } @@ -2564,4 +2564,8 @@ void Crawler::ResetGame(){ State_OverworldMap::UpdateCurrentConnectionPoint(*State_OverworldMap::currentConnectionPoint); SetChapter(1); SaveFile::SetSaveFileName(""); +} + +void Crawler::OnRequestCompleted(const std::string_view receivedData)const{ + std::cout<<"Received in engine: "<stdcpp20 C:\Users\sigon\source\repos\Crawler\Crawler\discord-files; + + powershell.exe -ExecutionPolicy Bypass -NoProfile -NonInteractive -File ../emscripten_build.ps1 + + + powershell.exe -ExecutionPolicy Bypass -NoProfile -File emscripten_build.ps1 + @@ -286,6 +292,12 @@ stdcpp20 C:\Users\sigon\source\repos\Crawler\Crawler\discord-files; + + powershell.exe -ExecutionPolicy Bypass -NoProfile -NonInteractive -File ../emscripten_build.ps1 + + + powershell.exe -ExecutionPolicy Bypass -NoProfile -File emscripten_build.ps1 + diff --git a/Crawler/SaveFile.cpp b/Crawler/SaveFile.cpp index e407dcca..d7543092 100644 --- a/Crawler/SaveFile.cpp +++ b/Crawler/SaveFile.cpp @@ -50,6 +50,7 @@ INCLUDE_game size_t SaveFile::saveFileID=0; std::string SaveFile::saveFileName=""; +std::string SaveFile::username=""; const size_t SaveFile::GetSaveFileCount(){ size_t count=0; @@ -194,4 +195,44 @@ const void SaveFile::UpdateSaveGameData(){ offsetY+=49; } } +} + +const std::string SaveFile::CreateServerRequest(const SaveFileOperation::Operation operation,std::string_view data){ + 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); + std::string operationName=""; + switch(operation){ + case SaveFileOperation::GET_LOAD_FILES:{ + operationName=std::format("GET_LOAD_FILES"); + //Data should be blank. + }break; + case SaveFileOperation::GET_FILE:{ + operationName=std::format("GET_FILE"); + //Data should contain the file ID we want. + }break; + case SaveFileOperation::SAVE_FILE:{ + operationName=std::format("SAVE_FILE"); + //Data should contain the entire contents of our 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(){ + game->SendRequest(CreateServerRequest(SaveFileOperation::GET_LOAD_FILES,"0"),""); +} +const void SaveFile::Server_GetFile(){ + game->SendRequest(CreateServerRequest(SaveFileOperation::GET_FILE,std::to_string(saveFileID)),""); +} +const void SaveFile::Server_SaveFile(){ + std::stringstream fileContents; + std::ifstream file("save_file_path"_S+std::format("save.{:04}",saveFileID)); + while(file.good()){ + fileContents<SendRequest(CreateServerRequest(SaveFileOperation::SAVE_FILE,std::to_string(saveFileID)+"|"+fileContents.str()),""); } \ No newline at end of file diff --git a/Crawler/SaveFile.h b/Crawler/SaveFile.h index d6bb3827..bf547b02 100644 --- a/Crawler/SaveFile.h +++ b/Crawler/SaveFile.h @@ -39,9 +39,18 @@ All rights reserved. #undef GetSaveFileName(); //Stupid Windows +namespace SaveFileOperation{ + enum Operation{ + GET_LOAD_FILES, + GET_FILE, + SAVE_FILE, + }; +}; + class SaveFile{ static size_t saveFileID; static std::string saveFileName; + static std::string username; public: static const std::string_view GetSaveFileName(); static const void SetSaveFileName(std::string_view saveFileName); @@ -51,4 +60,8 @@ public: static const void SetSaveFileID(size_t saveFileID); //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(); }; \ No newline at end of file diff --git a/Crawler/Version.h b/Crawler/Version.h index e1ea9208..b01f7ba9 100644 --- a/Crawler/Version.h +++ b/Crawler/Version.h @@ -39,7 +39,7 @@ All rights reserved. #define VERSION_MAJOR 0 #define VERSION_MINOR 2 #define VERSION_PATCH 1 -#define VERSION_BUILD 5279 +#define VERSION_BUILD 5283 #define stringify(a) stringify_(a) #define stringify_(a) #a diff --git a/Crawler/assets/config/configuration.txt b/Crawler/assets/config/configuration.txt index a86df839..d2a3294b 100644 --- a/Crawler/assets/config/configuration.txt +++ b/Crawler/assets/config/configuration.txt @@ -14,6 +14,9 @@ Interface 9PatchSize = 24,24 } +# Save File Server Name +save_server = http://projectdivar.com/AiL + # Map Files Loading Config map_config = levels.txt diff --git a/Crawler/emscripten_build.ps1 b/Crawler/emscripten_build.ps1 index 073b60cb..82d0964e 100644 --- a/Crawler/emscripten_build.ps1 +++ b/Crawler/emscripten_build.ps1 @@ -1,2 +1,3 @@ -~\Documents\emsdk\emsdk_env.ps1 activate latest -em++ -std=c++20 --proxy-to-worker -fexperimental-library -O2 -s ALLOW_MEMORY_GROWTH=1 -s MAX_WEBGL_VERSION=2 -s MIN_WEBGL_VERSION=2 -s USE_LIBPNG=1 -s USE_FREETYPE=1 $(Get-ChildItem *.cpp) -s FETCH=1 -o pge.html --preload-file assets \ No newline at end of file +cd .. +./emscripten_build.ps1 +./emscripten_run.ps1 \ No newline at end of file diff --git a/Crawler/olcPixelGameEngine.h b/Crawler/olcPixelGameEngine.h index d8ad25d2..fa91d63f 100644 --- a/Crawler/olcPixelGameEngine.h +++ b/Crawler/olcPixelGameEngine.h @@ -473,6 +473,7 @@ namespace _gfs = std::filesystem; #if defined(__EMSCRIPTEN__) #define OLC_PLATFORM_EMSCRIPTEN #include +#include #endif #endif #endif @@ -996,6 +997,7 @@ namespace olc virtual void OnTextEntryComplete(const std::string& sText); // Called when a console command is executed virtual bool OnConsoleCommand(const std::string& sCommand); + virtual void OnRequestCompleted(const std::string_view receivedData)const; virtual olc::rcode SendRequest(std::string_view url,std::string_view data); @@ -1242,6 +1244,8 @@ namespace olc public: + static std::string responseData; + // Experimental Lightweight 3D Routines ================ #ifdef OLC_ENABLE_EXPERIMENTAL // Set Manual View Matrix @@ -2044,6 +2048,8 @@ namespace olc return o; }; + std::string PixelGameEngine::responseData; + // O------------------------------------------------------------------------------O // | olc::PixelGameEngine IMPLEMENTATION | // O------------------------------------------------------------------------------O @@ -4393,6 +4399,8 @@ namespace olc void PixelGameEngine::OnTextEntryComplete(const std::string& sText) { UNUSED(sText); } bool PixelGameEngine::OnConsoleCommand(const std::string& sCommand) { UNUSED(sCommand); return false; } + void PixelGameEngine::OnRequestCompleted(const std::string_view receivedData)const{} + olc::rcode PixelGameEngine::SendRequest(std::string_view url,std::string_view data){platform->SendRequest(url,data);return olc::rcode::OK;}; @@ -7206,23 +7214,10 @@ namespace olc } virtual olc::rcode SendRequest(std::string_view url,std::string_view data)override{ - emscripten_fetch_attr_t attr; - emscripten_fetch_attr_init(&attr); - strcpy(attr.requestMethod, "GET"); - attr.attributes=EMSCRIPTEN_FETCH_LOAD_TO_MEMORY|EMSCRIPTEN_FETCH_SYNCHRONOUS|EMSCRIPTEN_FETCH_REPLACE; - emscripten_fetch_t*fetch=emscripten_fetch(&attr,std::string(url).c_str()); // Blocks here until the operation is complete. - if(fetch->status==200){ - printf("Finished downloading %llu bytes from URL %s.\n", fetch->numBytes, fetch->url); - // The data is now available at fetch->data[0] through fetch->data[fetch->numBytes-1]; - }else{ - printf("Downloading %s failed, HTTP failure status code: %d.\n",fetch->url,fetch->status); - } - emscripten_fetch_close(fetch); - - for(int i=0;inumBytes;i++){ - std::cout<data[i]; - } - std::cout<resp.text()).then((data)=>{requestResp=data}); + },std::string(url).c_str()); return olc::rcode::OK; } @@ -7350,6 +7345,22 @@ namespace olc static void MainLoop() { olc::Platform::ptrPGE->olc_CoreUpdate(); + char*ptr=(char*)EM_ASM_PTR({ + if(requestResp!==""){ + console.log("Request received: "+requestResp); + var newPtr=stringToNewUTF8(requestResp); + requestResp=""; + return newPtr; + } + return 0; + }); + + if(ptr!=NULL){ + PixelGameEngine::responseData=std::string(ptr); + ptrPGE->OnRequestCompleted(PixelGameEngine::responseData); + delete ptr; + } + if (!ptrPGE->olc_IsRunning()) { if (ptrPGE->OnUserDestroy())