[WIP] Added rest consumer

This commit is contained in:
KiritoDev 2021-04-26 23:09:27 -05:00
parent f519a344ea
commit d4e1bf6935
6 changed files with 172 additions and 12 deletions

View File

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

View File

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

View File

@ -1,4 +1,5 @@
#include "moon/texts/moon-loader.h"
#include "moon/network/moon-consumer.h"
#include <iostream>
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 ){

View File

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

View File

@ -0,0 +1,34 @@
#ifndef MoonCURL
#define MoonCURL
#include <curl/curl.h>
#include <string>
#include <vector>
struct MoonRequest {
std::string url;
std::string body;
std::vector<std::string> 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

View File

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