From 724d079ffe85547829bc063654cc33b32f60aa26 Mon Sep 17 00:00:00 2001 From: Niklas-EODynamics <98ahni@gmail.com> Date: Fri, 24 May 2024 14:04:18 +0200 Subject: [PATCH] Started working with the Google API. --- .gitignore | 3 + .vscode/imgui_shell.html | 2 + .vscode/settings.json | 3 +- Source/Extensions/APIKeys.cpp.TEMPLATE | 9 +++ Source/Extensions/APIKeys.h | 9 +++ Source/Extensions/GoogleDrive.cpp | 95 ++++++++++++++++++++++++++ Source/Extensions/GoogleDrive.h | 12 ++++ Source/main.cpp | 18 +++++ 8 files changed, 150 insertions(+), 1 deletion(-) create mode 100644 Source/Extensions/APIKeys.cpp.TEMPLATE create mode 100644 Source/Extensions/APIKeys.h create mode 100644 Source/Extensions/GoogleDrive.cpp create mode 100644 Source/Extensions/GoogleDrive.h diff --git a/.gitignore b/.gitignore index 259148f..b9b3a49 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ +# Private API keys file +*APIKeys.cpp + # Prerequisites *.d diff --git a/.vscode/imgui_shell.html b/.vscode/imgui_shell.html index c6efcf7..78e078d 100644 --- a/.vscode/imgui_shell.html +++ b/.vscode/imgui_shell.html @@ -76,6 +76,8 @@ })(); + + {{{ SCRIPT }}} diff --git a/.vscode/settings.json b/.vscode/settings.json index c68d4ca..9996b4a 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -96,6 +96,7 @@ "__threading_support": "cpp", "__tree": "cpp", "__verbose_abort": "cpp", - "regex": "cpp" + "regex": "cpp", + "__memory": "cpp" } } \ No newline at end of file diff --git a/Source/Extensions/APIKeys.cpp.TEMPLATE b/Source/Extensions/APIKeys.cpp.TEMPLATE new file mode 100644 index 0000000..64cb74c --- /dev/null +++ b/Source/Extensions/APIKeys.cpp.TEMPLATE @@ -0,0 +1,9 @@ +#include +#include +#include + +// This is the function getting called by the Google API once it's loaded. +extern"C" EMSCRIPTEN_KEEPALIVE void GAPI_Init_Client() +{ + EM_ASM({init_gapi_with_key(Emval.toValue($0));}, VAR_TO_JS("API KEY GOES HERE")); +} \ No newline at end of file diff --git a/Source/Extensions/APIKeys.h b/Source/Extensions/APIKeys.h new file mode 100644 index 0000000..ff94e0a --- /dev/null +++ b/Source/Extensions/APIKeys.h @@ -0,0 +1,9 @@ +#include + +// !! It's important to NOT save or return keys to javascript and to NEVER use this with function pointers for security reasons! +namespace APIKeys +{ + // The Google API key. + // !! It's important to NOT save or return keys to javascript and to NEVER use this with function pointers for security reasons! + std::string Google(); +} \ No newline at end of file diff --git a/Source/Extensions/GoogleDrive.cpp b/Source/Extensions/GoogleDrive.cpp new file mode 100644 index 0000000..d6b863a --- /dev/null +++ b/Source/Extensions/GoogleDrive.cpp @@ -0,0 +1,95 @@ +#include "GoogleDrive.h" +#include +#include +#include +#include "APIKeys.h" + +EM_JS(void, gapi_loaded, (), { + gapi.load('client', _GAPI_Init_Client); + gapi.load('client:picker', initializePicker); +}); + +EM_JS(bool, gapi_ready, (), { + return global_gapi_inited && global_gis_inited; +} +var global_gapi_inited = false; +var global_gis_inited = false; +); + +EM_JS(void, gis_loaded, (), { + global_client_token = google.accounts.oauth2.initTokenClient({ + client_id: CLIENT_ID, + scope: SCOPES, + callback: '', // defined later + }); + global_gis_inited = true; +} +var global_client_token; +); + +extern"C" EMSCRIPTEN_KEEPALIVE void GAPI_Init_Client() +{ + EM_ASM({init_gapi_with_key(Emval.toValue($0));}, VAR_TO_JS(APIKeys::Google())); +} +EM_JS(void, init_gapi_with_key, (emscripten::EM_VAL APIKey), { + gapi.client.init({ + apiKey: Emval.toValue(APIKey), + discoveryDocs: ['https://www.googleapis.com/discovery/v1/apis/drive/v3/rest'] + }).then(()=>{global_gapi_inited = true;}); +}); + +EM_JS(bool, has_gapi_token, (), { + return gapi.client.getToken() !== null; +}); + +EM_JS(void, request_client_token, (), { + if (gapi.client.getToken() === null) { + // Prompt the user to select a Google Account and ask for consent to share their data + // when establishing a new session. + global_client_token.requestAccessToken({prompt: 'consent'}); + } else { + // Skip display of account chooser and consent dialog for an existing session. + global_client_token.requestAccessToken({prompt: ''}); + } +}); + +EM_JS(void, revoke_client_token, (), { + const token = gapi.client.getToken(); + if (token !== null) { + google.accounts.oauth2.revoke(token.access_token); + gapi.client.setToken(''); + } +}); + +EM_JS(void, create_picker, (emscripten::EM_VAL APIKey), +{ + +}) + +bool GoogleDrive::Ready() +{ + return gapi_ready(); +} + +bool GoogleDrive::HasToken() +{ + return has_gapi_token(); +} + +void GoogleDrive::RequestToken() +{ +} + +void GoogleDrive::LogOut() +{ +} + +std::string GoogleDrive::LoadProject() +{ + return std::string(); +} + +std::string GoogleDrive::SaveProject() +{ + return std::string(); +} diff --git a/Source/Extensions/GoogleDrive.h b/Source/Extensions/GoogleDrive.h new file mode 100644 index 0000000..d57a167 --- /dev/null +++ b/Source/Extensions/GoogleDrive.h @@ -0,0 +1,12 @@ +#include + +namespace GoogleDrive +{ + bool Ready(); + bool HasToken(); + void RequestToken(); + void LogOut(); + + std::string LoadProject(); + std::string SaveProject(); +} \ No newline at end of file diff --git a/Source/main.cpp b/Source/main.cpp index d350048..e65a9e4 100644 --- a/Source/main.cpp +++ b/Source/main.cpp @@ -26,6 +26,8 @@ extern "C" EMSCRIPTEN_KEEPALIVE void ShowInputDebugger() { g_showInputDebugger = EM_JS(void, show_input_debugger, (), {_ShowInputDebugger(); }); bool g_closeFileTab = false; +bool g_hasGoogleAcc = false; + extern "C" EMSCRIPTEN_KEEPALIVE void LoadProject() { //AudioPlayback::PrepPlayback(); @@ -66,6 +68,22 @@ void loop(void* window){ { } ImGui::Ext::CreateHTMLButton("SaveProject", "click", "_SaveProject"); + ImGui::Separator(); + if(ImGui::BeginMenu("Google Drive")) + { + if(ImGui::IsItemActivated()) + { + // Check if logged in. + } + if(!g_hasGoogleAcc) + { + if(ImGui::MenuItem("Log In With Google")) + { + // Log in + } + } + ImGui::EndMenu(); + } ImGui::EndMenu(); } else