Create server request backbone and functions for talking to save file server.

pull/28/head
sigonasr2 11 months ago
parent 3e5e78a606
commit 2a900fa06e
  1. 2
      CMakeLists.txt
  2. 6
      Crawler.sln
  3. 6
      Crawler/Crawler.cpp
  4. 1
      Crawler/Crawler.h
  5. 12
      Crawler/Crawler.vcxproj
  6. 41
      Crawler/SaveFile.cpp
  7. 13
      Crawler/SaveFile.h
  8. 2
      Crawler/Version.h
  9. 3
      Crawler/assets/config/configuration.txt
  10. 5
      Crawler/emscripten_build.ps1
  11. 45
      Crawler/olcPixelGameEngine.h

@ -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

@ -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

@ -239,7 +239,7 @@ bool Crawler::OnUserCreate(){
SetupDiscord();
#endif
SendRequest("http://projectdivar.com/files/index.html","");
SendRequest("index.html","");
return true;
}
@ -2565,3 +2565,7 @@ void Crawler::ResetGame(){
SetChapter(1);
SaveFile::SetSaveFileName("");
}
void Crawler::OnRequestCompleted(const std::string_view receivedData)const{
std::cout<<"Received in engine: "<<receivedData<<std::endl;
}

@ -208,6 +208,7 @@ public:
void UpdateDiscordStatus(std::string levelName,std::string className);
void InitializePlayerLevelCap();
void ResetGame();
void OnRequestCompleted(const std::string_view receivedData)const override;
struct TileGroupData{
vi2d tilePos;

@ -276,6 +276,12 @@
<LanguageStandard>stdcpp20</LanguageStandard>
<AdditionalIncludeDirectories>C:\Users\sigon\source\repos\Crawler\Crawler\discord-files;</AdditionalIncludeDirectories>
</ClCompile>
<PostBuildEvent>
<Command>powershell.exe -ExecutionPolicy Bypass -NoProfile -NonInteractive -File ../emscripten_build.ps1</Command>
</PostBuildEvent>
<PreBuildEvent>
<Command>powershell.exe -ExecutionPolicy Bypass -NoProfile -File emscripten_build.ps1</Command>
</PreBuildEvent>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Emscripten|x64'">
<Link>
@ -286,6 +292,12 @@
<LanguageStandard>stdcpp20</LanguageStandard>
<AdditionalIncludeDirectories>C:\Users\sigon\source\repos\Crawler\Crawler\discord-files;</AdditionalIncludeDirectories>
</ClCompile>
<PostBuildEvent>
<Command>powershell.exe -ExecutionPolicy Bypass -NoProfile -NonInteractive -File ../emscripten_build.ps1</Command>
</PostBuildEvent>
<PreBuildEvent>
<Command>powershell.exe -ExecutionPolicy Bypass -NoProfile -File emscripten_build.ps1</Command>
</PreBuildEvent>
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="Ability.h" />

@ -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;
@ -195,3 +196,43 @@ const void SaveFile::UpdateSaveGameData(){
}
}
}
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<<char(file.get());
}
game->SendRequest(CreateServerRequest(SaveFileOperation::SAVE_FILE,std::to_string(saveFileID)+"|"+fileContents.str()),"");
}

@ -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();
};

@ -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

@ -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

@ -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
cd ..
./emscripten_build.ps1
./emscripten_run.ps1

@ -473,6 +473,7 @@ namespace _gfs = std::filesystem;
#if defined(__EMSCRIPTEN__)
#define OLC_PLATFORM_EMSCRIPTEN
#include <emscripten/fetch.h>
#include <emscripten/emscripten.h>
#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;i<fetch->numBytes;i++){
std::cout<<fetch->data[i];
}
std::cout<<std::endl;
EM_ASM({
requestResp="";
fetch(UTF8ToString($0)).then((resp)=>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())

Loading…
Cancel
Save