Index: binaries/data/config/default.cfg =================================================================== --- binaries/data/config/default.cfg +++ binaries/data/config/default.cfg @@ -396,6 +396,11 @@ [mod] enabledmods = "mod public" +[modio.v1] +baseurl = "https://api.mod.io/v1" +api_key = "acf8fc07e3a8e9228ef4d5704c1659a1" +nameid = "0ad" + [network] duplicateplayernames = false ; Rename joining player to "User (2)" if "User" is already connected, otherwise prohibit join. lateobservers = everyone ; Allow observers to join the game after it started. Possible values: everyone, buddies, disabled. Index: source/main.cpp =================================================================== --- source/main.cpp +++ source/main.cpp @@ -79,6 +79,8 @@ #include "simulation2/Simulation2.h" #include "simulation2/system/TurnManager.h" +#include "ps/ModIo.h" + #if OS_UNIX #include // geteuid #endif // OS_UNIX @@ -571,6 +573,12 @@ quit = false; if (!Init(args, flags)) { + + { + LOGERROR("FOOBAR"); + ModIo modIo; + } + flags &= ~INIT_MODS; Shutdown(SHUTDOWN_FROM_CONFIG); continue; Index: source/ps/ModIo.h =================================================================== --- /dev/null +++ source/ps/ModIo.h @@ -0,0 +1,55 @@ +/* Copyright (C) 2017 Wildfire Games. + * This file is part of 0 A.D. + * + * 0 A.D. is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * 0 A.D. is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with 0 A.D. If not, see . + */ + +#ifndef INCLUDED_MODIO +#define INCLUDED_MODIO + +#include + +#include "lib/external_libraries/curl.h" + +#include "scriptinterface/ScriptInterface.h" + +class ModIo +{ +// TODO NONCOPYABLE +public: + ModIo(); + ~ModIo(); + + static size_t ReceiveCallback(void* buffer, size_t size, size_t nmemb, void* userp); + +private: + std::string m_ResponseData; + + std::string m_BaseUrl; + std::string m_GamesRequest; + std::string m_GameId; + + // Query parameters + std::string m_ApiKey; + std::string m_IdQuery; + + CURL* m_Curl; + curl_slist* m_Headers; + + char m_ErrorBuffer[CURL_ERROR_SIZE]; + + std::shared_ptr m_ScriptRuntime; +}; + +#endif // INCLUDED_MODIO Index: source/ps/ModIo.cpp =================================================================== --- /dev/null +++ source/ps/ModIo.cpp @@ -0,0 +1,269 @@ +/* Copyright (C) 2017 Wildfire Games. + * This file is part of 0 A.D. + * + * 0 A.D. is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * 0 A.D. is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with 0 A.D. If not, see . + */ + +// TODO: This looks like it might be useful to others, MIT? + +#include "precompiled.h" + +#include "ModIo.h" + +#include "ps/CLogger.h" +#include "ps/ConfigDB.h" +#include "scriptinterface/ScriptConversions.h" + +ModIo::ModIo() + : m_GamesRequest("/games") +{ + CFG_GET_VAL("modio.v1.baseurl", m_BaseUrl); + { + std::string api_key; + CFG_GET_VAL("modio.v1.api_key", api_key); + m_ApiKey = "api_key=" + api_key; + } + { + std::string nameid; + CFG_GET_VAL("modio.v1.nameid", nameid); + m_IdQuery = "nameid="+nameid; + } + + // TODO: Maybe use someone else's scriptinterface? + // TODO: Or just keep using our own, make it a member though. + // TODO: Though as we should only be called from the mod mod, so gui, using that seems like a good choice + // (but not now, this is plain testing only) + m_ScriptRuntime = ScriptInterface::CreateRuntime(std::shared_ptr(), 8*MiB, 1*MiB); + ScriptInterface scriptInterface("Engine", "ModIo", m_ScriptRuntime); + + + // Initialise everything except Win32 sockets (because our networking + // system already inits those) + curl_global_init(CURL_GLOBAL_ALL & ~CURL_GLOBAL_WIN32); +// TODO we should only do this in one single place (same for shutdown, currently we do it here and in the userreporter) + + m_Curl = curl_easy_init(); + ENSURE(m_Curl); + + // Capture error messages + curl_easy_setopt(m_Curl, CURLOPT_ERRORBUFFER, m_ErrorBuffer); + + // Disable signal handlers (required for multithreaded applications) + curl_easy_setopt(m_Curl, CURLOPT_NOSIGNAL, 1L); + + // To minimise security risks, don't support redirects + curl_easy_setopt(m_Curl, CURLOPT_FOLLOWLOCATION, 0L); + + // Set IO callbacks + curl_easy_setopt(m_Curl, CURLOPT_WRITEFUNCTION, ReceiveCallback); + curl_easy_setopt(m_Curl, CURLOPT_WRITEDATA, this); + + + std::string url = m_BaseUrl+m_GamesRequest+"?"+m_ApiKey+"&"+m_IdQuery; + + LOGWARNING("%s", url); + + curl_easy_setopt(m_Curl, CURLOPT_URL, url.c_str()); + + m_Headers = NULL; + std::string ua = "User-Agent: 0ad "; + ua += curl_version(); + ua += " (https://play0ad.com/)"; + m_Headers = curl_slist_append(m_Headers, ua.c_str()); + // TODO more? + curl_easy_setopt(m_Curl, CURLOPT_HTTPHEADER, m_Headers); + + + CURLcode err = curl_easy_perform(m_Curl); + + if (err == CURLE_OK) + { +// LOGERROR("%s", m_ResponseData); + + { + JSContext* cx = scriptInterface.GetContext(); + JS::RootedValue gameResponse(cx); + + // TODO actually fail +#define FAIL(msg) STMT(LOGERROR(msg);/*TODO fail*/) + + // What the data is expected to look like, and what we need. + // { "data": [{"id": XXX, ...}, ...], ... } + // There is also a result_count property at the same level as data, which should always be 1 following + // our query, but maybe we should check that (nah) + if (!scriptInterface.ParseJSON(m_ResponseData, &gameResponse)) + FAIL("Failed to parse response as JSON."); + + JS::RootedObject gameResponseObj(cx, gameResponse.toObjectOrNull()); + // TODO handle it being null, or does JS_GetProperty do that for us? + JS::RootedValue dataVal(cx); + if (!JS_GetProperty(cx, gameResponseObj, "data", &dataVal)) + FAIL("data property not in response."); + + // [{"id": XXX, ...}, ...] + if (!dataVal.isObject()) + FAIL("data property not an object"); + + JS::RootedObject data(cx); + data = &dataVal.toObject(); + u32 length; + if (!JS_IsArrayObject(cx, data) || !JS_GetArrayLength(cx, data, &length) || !length) + FAIL("data property not an array with at least one element"); + + // {"id": XXX, ...} + JS::RootedValue first(cx); + if (!JS_GetElement(cx, data, 0, &first)) + FAIL("couldn't get first element."); + + int id = -1; + if (!ScriptInterface::FromJSProperty(cx, first, "id", id)) + FAIL("couldn't get id"); + + LOGWARNING("id: %d", id); + + m_GameId = "/" + std::to_string(id); + + LOGWARNING("%s", m_GameId); + } + + std::string mods = "/mods"; + + // https://api.mod.io/v1/games/57/mods?api_key=acf8fc07e3a8e9228ef4d5704c1659a1 + std::string url = m_BaseUrl+m_GamesRequest+m_GameId+mods+"?"+m_ApiKey; + + LOGWARNING("%s", url); + + curl_easy_setopt(m_Curl, CURLOPT_URL, url.c_str()); + + // We read the game id by now, and we don't want to parse that response again. + m_ResponseData.clear(); + + err = curl_easy_perform(m_Curl); + if (err == CURLE_OK) + { + LOGERROR("%s", m_ResponseData); + // Now that we have that new json with the list of mods parse that, store it somewhere so we can refer to it later + // we only accept indices from JS when downloading something later on (security) + + // TODO don't do this part in the ctor? + + JSContext* cx = scriptInterface.GetContext(); + JS::RootedValue modResponse(cx); + // { data: [modobj1, modobj2, ...], ... (including result_count) } + // with mod objects having the following interesting properties: + // { homepage: "url", name: "displayname", nameid: "short-non-whitespace-name", summary: "short desc.", modfile: { version: "1.2.4", filename: "asdf.zip", filehash: "md5sum", filesize: 1234, download: "someurl" }, ... } + + + if (!scriptInterface.ParseJSON(m_ResponseData, &modResponse)) + FAIL("Failed to parse response as JSON."); + + JS::RootedObject modResponseObj(cx, modResponse.toObjectOrNull()); + // TODO handle it being null, or does JS_GetProperty do that for us? + JS::RootedValue dataVal(cx); + if (!JS_GetProperty(cx, modResponseObj, "data", &dataVal)) + FAIL("data property not in response."); + + // [modobj1, modobj2, ... ] + if (!dataVal.isObject()) + FAIL("data property not an object"); + + JS::RootedObject data(cx); + data = &dataVal.toObject(); + u32 length; + if (!JS_IsArrayObject(cx, data) || !JS_GetArrayLength(cx, data, &length) || !length) + FAIL("data property not an array with at least one element"); + + // TODO this should be on the heap (or we should just store it in a vector of std::pair of string (or a set), or just a vector of structs with the few properties, which would most likely be easier if we just added a function to convert that to js again. + JS::RootedValue mods(cx); + scriptInterface.Eval("([])", &mods); + + for (u32 i = 0; i < length; ++i) + { + // { homepage: "url", name: "displayname", nameid: "short-non-whitespace-name", summary: "short desc.", modfile: { version: "1.2.4", filename: "asdf.zip", filehash: "md5sum", filesize: 1234, download: "someurl" }, ... } + JS::RootedValue el(cx); + if (!JS_GetElement(cx, data, i, &el)) + FAIL("Failed to get array element"); + + // TODO read the few things we care about, put them in a new structure that is rooted and on the heap, so it does not get lost or something. + // TODO this also means we need to think about making sure that this instance does not survive whoever's scriptinterface we are using. + + // TODO this should be stored somewhere (TODO heap) + JS::RootedValue obj(cx); + scriptInterface.Eval("({})", &obj); // TODO: Do this with NewPlainObject? + // now copy homepage, name, nameid, summary over. + // TODO: Currently the homepage field does not exist for any of the listed entries + for (const std::string& prop : {"name", "nameid", "summary"}) + { + std::string val; + ScriptInterface::FromJSProperty(cx, el, prop.c_str(), val); + scriptInterface.SetProperty(obj, prop.c_str(), val, true); + } + + // Now copy over the modfile part, but without the substructure, since that seems somewhat pointless + // that is unless there are actually multiple files per mod (which might be the case with a little better test data, in which case we should either list just one of them, or add the whole thing (modname, ...) for each file. + // But in that case modfile is the wrong name for the property, so assume that is really just a single one, and tell upstream to fix things if that isn't the case. + + JS::RootedValue modFile(cx); + JS::RootedObject elObj(cx); + if (!el.isObject()) + FAIL("not an object"); + elObj = &el.toObject(); + if (!JS_GetProperty(cx, elObj, "modfile", &modFile)) + FAIL("Failed to get modfile data"); + + for (const std::string& prop : {"version", "filename", "filehash", "filesize", "download"}) + { + std::string val; + ScriptInterface::FromJSProperty(cx, modFile, prop.c_str(), val); + scriptInterface.SetProperty(obj, prop.c_str(), val, true); + } + scriptInterface.CallFunctionVoid(mods, "push", obj); + } + + // Show off our fancy list of mod files, with all the data that seems needed. + LOGERROR("%s", scriptInterface.ToString(&mods, true)); + } + else + { + // TODO as always check what the docs say on the return codes + // TODO still we don't care, abort + } + } + else + { + // TODO check the error codes, maybe we are just being rate-limited. + // TODO abort the connection, possibly tell the user that what the issue is. + } + + +#undef FAIL +} + +ModIo::~ModIo() +{ + m_ScriptRuntime.reset(); + + curl_slist_free_all(m_Headers); + curl_easy_cleanup(m_Curl); + curl_global_cleanup(); +} + +size_t ModIo::ReceiveCallback(void* buffer, size_t size, size_t nmemb, void* userp) +{ + ModIo* self = static_cast(userp); + + self->m_ResponseData += std::string((char*)buffer, (char*)buffer+size*nmemb); + + return size*nmemb; +}