Permalink
Browse files

Merge pull request #78 from Turupawn/devel

Storing current user on cache functionality
  • Loading branch information...
Turupawn committed Feb 8, 2019
2 parents be95b8c + d98dcf8 commit 4b6859ee5c5b434efd28f1c3a52a5ec9dc9a3c31
@@ -0,0 +1,26 @@
#include "modio.h"

int main(void)
{
modio::Instance modio_instance(MODIO_ENVIRONMENT_TEST, 7, "e91c01b8882f4affeddd56c96111977b");

volatile static bool finished = false;

auto finish = [&]() {
finished = true;
};

// Use getCurrentUser in conjuction with the isLoggedIn to check the current user cache
if(modio_instance.isLoggedIn())
{
std::cout << "Logged in as " << modio_instance.getCurrentUser().username << std::endl;
}else
{
std::cout << "You are not logged in." << std::endl;
}


std::cout << "Process finished" << std::endl;

return 0;
}
@@ -0,0 +1,22 @@
#include "modio_c.h"

int main(void)
{
modioInit(MODIO_ENVIRONMENT_TEST, 7, (char *)"e91c01b8882f4affeddd56c96111977b", NULL);

// Use modioGetCurrentUser in conjuction with the modioIsLoggedIn to check the current user cache
if(modioIsLoggedIn())
{
printf("Logged in as %s\n", modioGetCurrentUser().username);
}else
{
printf("You are not logged in.\n");
}


modioShutdown();

printf("Process finished\n");

return 0;
}
@@ -487,6 +487,7 @@ void modioEmailRequest(void* object, char* email, void (*callback)(void* object,
void modioEmailExchange(void* object, char* security_code, void (*callback)(void* object, ModioResponse response));
bool modioIsLoggedIn();
void modioLogout();
const struct ModioUser modioGetCurrentUser();

//Image Methods
void modioDownloadImage(void* object, char* image_url, char* path, void (*callback)(void* object, ModioResponse response));
@@ -22,6 +22,7 @@ namespace modio
extern u32 AUTOMATIC_UPDATES;
extern u32 BACKGROUND_DOWNLOADS;
extern u32 RETRY_AFTER;
extern ModioUser current_user;
extern void (*event_listener_callback)(ModioResponse response, ModioEvent* events_array, u32 events_array_size);
extern void (*download_callback)(u32 response_code, u32 mod_id);
extern void (*upload_callback)(u32 response_code, u32 mod_id);
@@ -19,8 +19,10 @@ struct GenericCall
const std::function<void(const modio::Response &)> callback;
};


namespace modio
{
void onUpdateCurrentUser(void *object, ModioResponse response, ModioUser user);
void addModsToDownloadQueue(std::vector<u32> mod_ids);
void pollEvents();
}
@@ -70,6 +70,7 @@ class MODIO_DLL Instance
void logout() const;
void emailRequest(const std::string &email, const std::function<void(const modio::Response &)> &callback);
void emailExchange(const std::string &security_code, const std::function<void(const modio::Response &)> &callback);
modio::User getCurrentUser();

//Media Methods
void downloadImage(const std::string &image_url, const std::string &path, const std::function<void(const modio::Response &)> &callback);
@@ -499,6 +499,7 @@ extern "C"
void MODIO_DLL modioEmailExchange(void* object, char* security_code, void (*callback)(void* object, ModioResponse response));
bool MODIO_DLL modioIsLoggedIn();
void MODIO_DLL modioLogout();
const struct ModioUser MODIO_DLL modioGetCurrentUser();

//Image Methods
void MODIO_DLL modioDownloadImage(void* object, char* image_url, char* path, void (*callback)(void* object, ModioResponse response));
@@ -14,6 +14,7 @@ namespace modio
u32 EVENT_POLL_INTERVAL = 15;
u32 RETRY_AFTER = 0;
u32 MAX_CACHE_TIME = 3600;
ModioUser current_user;
void (*event_listener_callback)(ModioResponse response, ModioEvent* events_array, u32 events_array_size) = NULL;
void (*download_callback)(u32 response_code, u32 mod_id) = NULL;
void (*upload_callback)(u32 response_code, u32 mod_id) = NULL;
@@ -2,6 +2,18 @@

namespace modio
{
void onUpdateCurrentUser(void *object, ModioResponse response, ModioUser user)
{
if (response.code >= 200 && response.code < 300)
{
modio::writeLogLine("Current user updated sucessfully.", MODIO_DEBUGLEVEL_LOG);
}
else
{
modio::writeLogLine("Could not update current user.", MODIO_DEBUGLEVEL_WARNING);
}
}

void onAddModsToDownloadQueue(void *object, ModioResponse response, ModioMod *mods, u32 mods_size)
{
if (response.code == 200)
@@ -74,9 +86,7 @@ void onGetAllEventsPoll(void *object, ModioResponse response, ModioEvent *events
bool reinstall = true;
for (auto installed_mod : modio::installed_mods)
{
if (modio::hasKey(installed_mod,"mod_id") && modio::hasKey(installed_mod,"date_updated")
&& installed_mod["mod_id"] == events_array[i].mod_id
&& installed_mod["date_updated"] >= events_array[i].date_added)
if (modio::hasKey(installed_mod, "mod_id") && modio::hasKey(installed_mod, "date_updated") && installed_mod["mod_id"] == events_array[i].mod_id && installed_mod["date_updated"] >= events_array[i].date_added)
{
modio::writeLogLine("Modfile changed event detected but you already have a newer version installed, the modfile will not be downloaded. Mod id: " + modio::toString(events_array[i].mod_id), MODIO_DEBUGLEVEL_LOG);
}
@@ -205,7 +215,8 @@ void pollEvents()
modioFreeFilter(&filter);

modio::LAST_MOD_EVENT_POLL = current_time;
}else if(current_time - modio::LAST_MOD_EVENT_POLL > modio::EVENT_POLL_INTERVAL)
}
else if (current_time - modio::LAST_MOD_EVENT_POLL > modio::EVENT_POLL_INTERVAL)
{
nlohmann::json event_polling_json = modio::openJson(modio::getModIODirectory() + "event_polling.json");
event_polling_json["last_mod_event_poll"] = current_time;
@@ -31,4 +31,12 @@ void Instance::emailExchange(const std::string &security_code, const std::functi

this->current_call_id++;
}

modio::User Instance::getCurrentUser()
{
modio::User user;
user.initialize(modioGetCurrentUser());
return user;
}

} // namespace modio
@@ -46,4 +46,9 @@ extern "C"
modio::ACCESS_TOKEN = "";
modio::writeJson(modio::getModIODirectory() + "authentication.json", nlohmann::json({}));
}

const struct ModioUser modioGetCurrentUser()
{
return modio::current_user;
}
}
@@ -29,9 +29,10 @@ void modioOnEmailExchanged(u32 call_number, u32 response_code, nlohmann::json re
{
access_token = response_json["access_token"];
modio::ACCESS_TOKEN = access_token;
nlohmann::json token_json;
token_json["access_token"] = access_token;
modio::writeJson(modio::getModIODirectory() + "authentication.json", token_json);
nlohmann::json authentication_json;
authentication_json["access_token"] = access_token;
modio::writeJson(modio::getModIODirectory() + "authentication.json", authentication_json);
modioGetAuthenticatedUser(NULL, &modio::onUpdateCurrentUser);
}
else
{
@@ -14,16 +14,21 @@ void modioOnGetAuthenticatedUser(u32 call_number, u32 response_code, nlohmann::j
modioInitResponse(&response, response_json);
response.code = response_code;

ModioUser user;
modioInitUser(&user, response_json);
modioInitUser(&modio::current_user, response_json);

get_authenticated_user_callbacks[call_number]->callback(get_authenticated_user_callbacks[call_number]->object, response, user);
if(response_code >= 200 && response_code < 300)
{
nlohmann::json authentication_json = modio::openJson(modio::getModIODirectory() + "authentication.json");
authentication_json["user"] = response_json;
modio::writeJson(modio::getModIODirectory() + "authentication.json", authentication_json);
}

get_authenticated_user_callbacks[call_number]->callback(get_authenticated_user_callbacks[call_number]->object, response, modio::current_user);

delete get_authenticated_user_callbacks[call_number];
get_authenticated_user_callbacks.erase(call_number);

modioFreeResponse(&response);
modioFreeUser(&user);
}

void modioOnGetUserSubscriptions(u32 call_number, u32 response_code, nlohmann::json response_json)
@@ -92,6 +92,13 @@ void modioInit(u32 environment, u32 game_id, char *api_key, char *root_path)

modio::clearOldCache();

nlohmann::json authentication_json = modio::openJson(modio::getModIODirectory() + "authentication.json");
if (modio::hasKey(authentication_json, "user"))
modioInitUser(&modio::current_user, authentication_json["user"]);

if (modioIsLoggedIn())
modioGetAuthenticatedUser(NULL, &modio::onUpdateCurrentUser);

modio::writeLogLine("SDK Initialized", MODIO_DEBUGLEVEL_LOG);
}

@@ -122,6 +129,8 @@ void modioShutdown()
clearSubscriptionCallbackParams();
clearTagCallbackParams();

modioFreeUser(&modio::current_user);

modio::writeLogLine("mod.io C interface finished shutting down", MODIO_DEBUGLEVEL_LOG);
}

0 comments on commit 4b6859e

Please sign in to comment.