Skip to content
This repository has been archived by the owner on Sep 21, 2022. It is now read-only.

Commit

Permalink
current user functionality added
Browse files Browse the repository at this point in the history
  • Loading branch information
Turupawn committed Feb 6, 2019
1 parent 71b5a24 commit 4821a0c
Show file tree
Hide file tree
Showing 14 changed files with 106 additions and 12 deletions.
26 changes: 26 additions & 0 deletions examples/code-samples/c++/53_GetCurrentUser.cpp
@@ -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;
}
22 changes: 22 additions & 0 deletions examples/code-samples/c/53_GetAuthenticatedUser.c
@@ -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;
}
1 change: 1 addition & 0 deletions examples/code-samples/c/modio_c.h
Expand Up @@ -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));
Expand Down
1 change: 1 addition & 0 deletions include/Globals.h
Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions include/ModioUtility.h
Expand Up @@ -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();
}
Expand Down
1 change: 1 addition & 0 deletions include/c++/ModIOInstance.h
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions include/c/ModioC.h
Expand Up @@ -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));
Expand Down
1 change: 1 addition & 0 deletions src/Globals.cpp
Expand Up @@ -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;
Expand Down
19 changes: 15 additions & 4 deletions src/ModioUtility.cpp
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
Expand Down
8 changes: 8 additions & 0 deletions src/c++/methods/AuthenticationInstanceMethods.cpp
Expand Up @@ -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
5 changes: 5 additions & 0 deletions src/c/methods/AuthenticationMethods.cpp
Expand Up @@ -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;
}
}
7 changes: 4 additions & 3 deletions src/c/methods/callbacks/AuthenticationCallbacks.cpp
Expand Up @@ -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
{
Expand Down
13 changes: 9 additions & 4 deletions src/c/methods/callbacks/MeCallbacks.cpp
Expand Up @@ -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)
Expand Down
11 changes: 10 additions & 1 deletion src/modio.cpp
Expand Up @@ -68,7 +68,7 @@ void modioInit(u32 environment, u32 game_id, char *api_key, char *root_path)
modio::API_KEY = api_key;
if (root_path)
modio::ROOT_PATH = root_path;

modio::installDownloadedMods();

loadEventPollingFile();
Expand All @@ -82,6 +82,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);
}

Expand Down Expand Up @@ -112,6 +119,8 @@ void modioShutdown()
clearSubscriptionCallbackParams();
clearTagCallbackParams();

modioFreeUser(&modio::current_user);

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

Expand Down

0 comments on commit 4821a0c

Please sign in to comment.