From d4e1bf6935d7f78adff66af7be7ca69e67ca70ca Mon Sep 17 00:00:00 2001 From: KiritoDev Date: Mon, 26 Apr 2021 23:09:27 -0500 Subject: [PATCH] [WIP] Added rest consumer --- Makefile | 12 +-- levels/entry.c | 4 +- src/moon/moon64.cpp | 13 +++- src/moon/network/moon-consumer.cpp | 119 +++++++++++++++++++++++++++++ src/moon/network/moon-consumer.h | 34 +++++++++ src/moon/texts/moon-loader.cpp | 2 +- 6 files changed, 172 insertions(+), 12 deletions(-) create mode 100644 src/moon/network/moon-consumer.cpp create mode 100644 src/moon/network/moon-consumer.h diff --git a/Makefile b/Makefile index b090944b..835dddaa 100644 --- a/Makefile +++ b/Makefile @@ -264,7 +264,7 @@ LEVEL_DIRS := $(patsubst levels/%,%,$(dir $(wildcard levels/*/header.h))) SRC_DIRS := src src/engine src/game src/audio src/menu src/buffers actors levels bin data assets src/text src/text/libs src/pc src/pc/gfx src/pc/audio src/pc/controller src/pc/fs src/pc/fs/packtypes src/nx # Moon64 SRC Directories -SRC_DIRS += src/moon src/moon/texts src/moon/utils +SRC_DIRS += src/moon src/moon/texts src/moon/utils src/moon/network # RapidJSON Library SRC_DIRS += src/moon/libs/rapidjson src/moon/libs/rapidjson/error src/moon/libs/rapidjson/internal src/moon/libs/rapidjson/msinttypes @@ -602,7 +602,7 @@ ifeq ($(TARGET_WEB),1) LDFLAGS := -lm -lGL -lSDL2 -no-pie -s TOTAL_MEMORY=20MB -g4 --source-map-base http://localhost:8080/ -s "EXTRA_EXPORTED_RUNTIME_METHODS=['callMain']" else ifeq ($(WINDOWS_BUILD),1) - LDFLAGS := $(BITS) -march=$(TARGET_ARCH) -Llib -lpthread $(BACKEND_LDFLAGS) -static + LDFLAGS := $(BITS) -march=$(TARGET_ARCH) -Llib -lpthread $(BACKEND_LDFLAGS) -static -lcurl ifeq ($(CROSS),) LDFLAGS += -no-pie endif @@ -611,16 +611,16 @@ else ifeq ($(WINDOWS_BUILD),1) endif else ifeq ($(TARGET_RPI),1) - LDFLAGS := $(OPT_FLAGS) -lm $(BACKEND_LDFLAGS) -no-pie + LDFLAGS := $(OPT_FLAGS) -lm $(BACKEND_LDFLAGS) -no-pie -lcurl else ifeq ($(OSX_BUILD),1) - LDFLAGS := -lm $(PLATFORM_LDFLAGS) $(BACKEND_LDFLAGS) -lpthread + LDFLAGS := -lm $(PLATFORM_LDFLAGS) $(BACKEND_LDFLAGS) -lpthread -lcurl else ifeq ($(TARGET_SWITCH),1) - LDFLAGS := -specs=$(LIBNX)/switch.specs $(NXARCH) $(OPT_FLAGS) -no-pie -L$(LIBNX)/lib $(BACKEND_LDFLAGS) -lnx -lm + LDFLAGS := -specs=$(LIBNX)/switch.specs $(NXARCH) $(OPT_FLAGS) -no-pie -L$(LIBNX)/lib $(BACKEND_LDFLAGS) -lnx -lm `curl-config --libs` else - LDFLAGS := $(BITS) -march=$(TARGET_ARCH) -lm $(BACKEND_LDFLAGS) -no-pie -lpthread + LDFLAGS := $(BITS) -march=$(TARGET_ARCH) -lm $(BACKEND_LDFLAGS) -no-pie -lpthread -lcurl ifeq ($(DISCORDRPC),1) LDFLAGS += -ldl -Wl,-rpath . endif diff --git a/levels/entry.c b/levels/entry.c index 634d65e3..c63c6b3e 100644 --- a/levels/entry.c +++ b/levels/entry.c @@ -2,9 +2,7 @@ #include "sm64.h" #include "segment_symbols.h" #include "level_commands.h" - #include "levels/intro/header.h" - #include "make_const_nonconst.h" const LevelScript level_script_entry[] = { @@ -15,7 +13,7 @@ const LevelScript level_script_entry[] = { #ifdef TOGGLE_GAME_DEBUG EXECUTE(/*seg*/ 0x14, /*script*/ _introSegmentRomStart, /*scriptEnd*/ _introSegmentRomEnd, /*entry*/ level_intro_entry_4), #else - EXECUTE(/*seg*/ 0x14, /*script*/ _introSegmentRomStart, /*scriptEnd*/ _introSegmentRomEnd, /*entry*/ script_intro_L1), + EXECUTE(/*seg*/ 0x14, /*script*/ _introSegmentRomStart, /*scriptEnd*/ _introSegmentRomEnd, /*entry*/ level_intro_n64), #endif JUMP(/*target*/ level_script_entry), }; diff --git a/src/moon/moon64.cpp b/src/moon/moon64.cpp index 44608cfb..8d925084 100644 --- a/src/moon/moon64.cpp +++ b/src/moon/moon64.cpp @@ -1,4 +1,5 @@ #include "moon/texts/moon-loader.h" +#include "moon/network/moon-consumer.h" #include extern "C" { @@ -6,9 +7,17 @@ extern "C" { #include "types.h" void moon_init_languages(char *executable, char *gamedir) { - printf("---------------------------------\n"); + MoonConsumer consumer; + consumer.Init(); + + MoonResponse res; + MoonRequest req; + req.url = "https://raw.githubusercontent.com/Render96/Render96ex_Languages/master/PT_br.json"; + req.file = "/home/alex/testout/Kalimba2.txt"; + consumer.Get(req, &res); + printf("%s\n", res.body.c_str()); + Moon_InitLanguages(executable, gamedir); - printf("---------------------------------\n"); } u8 * moon_language_get_key( char* key ){ diff --git a/src/moon/network/moon-consumer.cpp b/src/moon/network/moon-consumer.cpp new file mode 100644 index 00000000..aaa6274c --- /dev/null +++ b/src/moon/network/moon-consumer.cpp @@ -0,0 +1,119 @@ +#include "moon-consumer.h" + +using namespace std; + +void MoonConsumer::Init(){ + curl_global_init(CURL_GLOBAL_DEFAULT); + this->curl = curl_easy_init(); + + if(!this->curl) { + this->curl = NULL; + curl_global_cleanup(); + } +} + +size_t MoonWriteString(void *ptr, size_t size, size_t nmemb, std::string* data) { + data->append((char*) ptr, size * nmemb); + return size * nmemb; +} + +size_t MoonWriteFile(void *ptr, size_t size, size_t nmemb, FILE *stream) { + size_t written = fwrite(ptr, size, nmemb, stream); + return written; +} + +void MoonConsumer::Post(MoonRequest request, MoonResponse* response){ + if(!this->curl) { + response->code = 510; + response->error = "Failed to get curl instance"; + return; + } + + FILE *tmp; + bool isFile = !request.file.empty(); + + curl_easy_setopt(this->curl, CURLOPT_URL, request.url.c_str()); + curl_easy_setopt(this->curl, CURLOPT_NOBODY, false); + curl_easy_setopt(this->curl, CURLOPT_VERBOSE, false); + curl_easy_setopt(this->curl, CURLOPT_POSTFIELDS, request.body.c_str()); + + if(isFile){ + tmp = fopen(request.file.c_str(), "wb"); + curl_easy_setopt(this->curl, CURLOPT_WRITEDATA, tmp); + curl_easy_setopt(this->curl, CURLOPT_WRITEFUNCTION, MoonWriteFile); + } else { + curl_easy_setopt(this->curl, CURLOPT_WRITEDATA, &response->body); + curl_easy_setopt(this->curl, CURLOPT_WRITEFUNCTION, MoonWriteString); + } + + struct curl_slist *chunk = NULL; + for (std::string::size_type i = 0; i < request.headers.size(); i++) { + curl_slist_append(chunk, request.headers[i].c_str()); + } + + curl_easy_setopt(this->curl, CURLOPT_HTTPHEADER, chunk); + CURLcode res; + + try { + res = curl_easy_perform(this->curl); + } catch (const std::exception& e) { + response->code = 510; + response->error = string(e.what()); + } + if(res == CURLE_OK) { + curl_easy_getinfo(this->curl, CURLINFO_RESPONSE_CODE, &response->code); + } else { + response->code = 510; + response->error = curl_easy_strerror(res); + } + + if(isFile) fclose(tmp); + curl_easy_cleanup(this->curl); +} + +void MoonConsumer::Get(MoonRequest request, MoonResponse* response){ + if(!this->curl) { + response->code = 510; + response->error = "Failed to get curl instance"; + return; + } + + FILE *tmp; + bool isFile = !request.file.empty(); + + curl_easy_setopt(this->curl, CURLOPT_URL, request.url.c_str()); + curl_easy_setopt(this->curl, CURLOPT_NOBODY, false); + curl_easy_setopt(this->curl, CURLOPT_VERBOSE, false); + + if(isFile){ + tmp = fopen(request.file.c_str(), "wb"); + curl_easy_setopt(this->curl, CURLOPT_WRITEDATA, tmp); + curl_easy_setopt(this->curl, CURLOPT_WRITEFUNCTION, MoonWriteFile); + } else { + curl_easy_setopt(this->curl, CURLOPT_WRITEDATA, &response->body); + curl_easy_setopt(this->curl, CURLOPT_WRITEFUNCTION, MoonWriteString); + } + + struct curl_slist *chunk = NULL; + for (std::string::size_type i = 0; i < request.headers.size(); i++) { + curl_slist_append(chunk, request.headers[i].c_str()); + } + + curl_easy_setopt(this->curl, CURLOPT_HTTPHEADER, chunk); + CURLcode res; + + try { + res = curl_easy_perform(this->curl); + } catch (const std::exception& e) { + response->code = 510; + response->error = string(e.what()); + } + if(res == CURLE_OK) { + curl_easy_getinfo(this->curl, CURLINFO_RESPONSE_CODE, &response->code); + } else { + response->code = 510; + response->error = curl_easy_strerror(res); + } + if(isFile) fclose(tmp); + curl_easy_cleanup(this->curl); +} \ No newline at end of file diff --git a/src/moon/network/moon-consumer.h b/src/moon/network/moon-consumer.h new file mode 100644 index 00000000..98bb30ad --- /dev/null +++ b/src/moon/network/moon-consumer.h @@ -0,0 +1,34 @@ +#ifndef MoonCURL +#define MoonCURL + +#include +#include +#include + +struct MoonRequest { + std::string url; + std::string body; + std::vector headers; + std::string file; +}; + +struct MoonResponse { + int code; + std::string error; + std::string body; +}; + +enum MoonResponseType { + Text, File +}; + +class MoonConsumer { +private: + CURL *curl; +public: + void Init(); + void Post(MoonRequest request, MoonResponse* response); + void Get(MoonRequest request, MoonResponse* response); +}; + +#endif diff --git a/src/moon/texts/moon-loader.cpp b/src/moon/texts/moon-loader.cpp index 915b0f15..a76da6c3 100644 --- a/src/moon/texts/moon-loader.cpp +++ b/src/moon/texts/moon-loader.cpp @@ -124,7 +124,7 @@ u8 *Moon_GetKey(string key) { return current->strings[key]; } -void Moon_InitLanguages( char *exePath, char *gamedir ) { +void Moon_InitLanguages( char *exePath, char *gamedir ) { string l_path(exePath);