Skip to content

Commit 69a4d41

Browse files
committed
add working but unstable version
1 parent 68c09ec commit 69a4d41

File tree

2 files changed

+50
-102
lines changed

2 files changed

+50
-102
lines changed

src/core/include/cesium/omniverse/CesiumIonSession.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class CesiumIonSession {
2323
const CesiumAsync::AsyncSystem& asyncSystem,
2424
std::shared_ptr<CesiumAsync::IAssetAccessor> pAssetAccessor,
2525
std::string ionServerUrl,
26-
std::string ionApiUrl,
26+
/*std::string ionApiUrl,*/
2727
int64_t ionApplicationId);
2828
~CesiumIonSession() = default;
2929
CesiumIonSession(const CesiumIonSession&) = delete;

src/core/src/CesiumIonSession.cpp

+49-101
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#include "cesium/omniverse/Broadcast.h"
66
#include "cesium/omniverse/SettingsWrapper.h"
77

8-
#include <CesiumIonClient/Connection.h>
8+
#include <CesiumAsync/SharedFuture.h>
99
#include <CesiumUtility/Uri.h>
1010

1111
#include <utility>
@@ -48,25 +48,58 @@ void CesiumIonSession::connect() {
4848

4949
this->_isConnecting = true;
5050

51-
this->ensureAppDataLoaded();
52-
53-
Connection::authorize(
54-
this->_asyncSystem,
55-
this->_pAssetAccessor,
56-
"Cesium for Omniverse",
57-
_ionApplicationId,
58-
"/cesium-for-omniverse/oauth2/callback",
59-
{"assets:list", "assets:read", "profile:read", "tokens:read", "tokens:write", "geocode"},
60-
[this](const std::string& url) {
61-
// NOTE: We open the browser in the Python code. Check in the sign in widget's on_update_frame function.
62-
this->_authorizeUrl = url;
63-
},
64-
this->_appData.value(),
65-
CesiumUtility::Uri::resolve(_ionServerUrl, "oauth"))
51+
CesiumAsync::Future<std::optional<std::string>> futureApiUrl =
52+
!_ionApiUrl.empty()
53+
? this->_asyncSystem.createResolvedFuture<std::optional<std::string>>(_ionApiUrl)
54+
: CesiumIonClient::Connection::getApiUrl(this->_asyncSystem, this->_pAssetAccessor, _ionServerUrl);
55+
56+
std::move(futureApiUrl)
57+
.thenInMainThread([this](std::optional<std::string>&& ionApiUrl) {
58+
CesiumAsync::Promise<bool> promise = this->_asyncSystem.createPromise<bool>();
59+
60+
if (_ionApiUrl.empty()) {
61+
_ionApiUrl = ionApiUrl.value();
62+
}
63+
64+
// Make request to /appData to learn the server's authentication mode
65+
return this->ensureAppDataLoaded();
66+
})
67+
.thenInMainThread([this](bool loadedAppData) {
68+
if (!loadedAppData || !this->_appData.has_value()) {
69+
CesiumAsync::Promise<CesiumIonClient::Connection> promise =
70+
this->_asyncSystem.createPromise<CesiumIonClient::Connection>();
71+
72+
promise.reject(std::runtime_error("Failed to load _appData, can't create connection"));
73+
return promise.getFuture();
74+
}
75+
76+
if (this->_appData->needsOauthAuthentication()) {
77+
return CesiumIonClient::Connection::authorize(
78+
this->_asyncSystem,
79+
this->_pAssetAccessor,
80+
"Cesium for Omniverse",
81+
_ionApplicationId,
82+
"/cesium-for-omniverse/oauth2/callback",
83+
{"assets:list", "assets:read", "profile:read", "tokens:read", "tokens:write", "geocode"},
84+
[this](const std::string& url) {
85+
// NOTE: We open the browser in the Python code. Check in the sign in widget's on_update_frame function.
86+
this->_authorizeUrl = url;
87+
},
88+
this->_appData.value(),
89+
_ionServerUrl,
90+
CesiumUtility::Uri::resolve(_ionServerUrl, "oauth"));
91+
}
92+
93+
return this->_asyncSystem.createResolvedFuture<CesiumIonClient::Connection>(CesiumIonClient::Connection(
94+
this->_asyncSystem, this->_pAssetAccessor, "", this->_appData.value(), _ionServerUrl));
95+
})
6696
.thenInMainThread([this](CesiumIonClient::Connection&& connection) {
6797
this->_isConnecting = false;
6898
this->_connection = std::move(connection);
6999

100+
// TODO: how to update filed in OmniIonServer?
101+
// CesiumForUnity::CesiumIonServer server = session.server();
102+
70103
Settings::AccessToken token;
71104
token.ionApiUrl = _ionApiUrl;
72105
token.accessToken = this->_connection.value().getAccessToken();
@@ -80,91 +113,6 @@ void CesiumIonSession::connect() {
80113

81114
Broadcast::connectionUpdated();
82115
});
83-
84-
// std::string ionServerUrl = _ionServerUrl;
85-
// std::string ionApiUrl = _ionApiUrl;
86-
87-
// CesiumAsync::Future<std::optional<std::string>> futureApiUrl =
88-
// !ionApiUrl.empty()
89-
// ? this->_asyncSystem.createResolvedFuture<std::optional<std::string>>(ionApiUrl)
90-
// : CesiumIonClient::Connection::getApiUrl(this->_asyncSystem, this->_pAssetAccessor, ionServerUrl);
91-
92-
// std::move(futureApiUrl)
93-
// .thenInMainThread([ionServerUrl, server, session, this](std::optional<std::string>&& ionApiUrl) {
94-
// CesiumAsync::Promise<bool> promise = this->_asyncSystem.createPromise<bool>();
95-
96-
// if (session == nullptr) {
97-
// promise.reject(std::runtime_error("CesiumIonSession unexpectedly nullptr"));
98-
// return promise.getFuture();
99-
// }
100-
// if (server == nullptr) {
101-
// promise.reject(std::runtime_error("CesiumIonServer unexpectedly nullptr"));
102-
// return promise.getFuture();
103-
// }
104-
105-
// if (!ionApiUrl) {
106-
// promise.reject(std::runtime_error(fmt::format(
107-
// "Failed to retrieve API URL from the config.json file at the "
108-
// "specified Ion server URL: {}",
109-
// ionServerUrl)));
110-
// return promise.getFuture();
111-
// }
112-
113-
// if (System::String::IsNullOrEmpty(server.apiUrl())) {
114-
// server.apiUrl(System::String(*ionApiUrl));
115-
// }
116-
117-
// // Make request to /appData to learn the server's authentication mode
118-
// return this->ensureAppDataLoaded(session);
119-
// })
120-
// .thenInMainThread([ionServerUrl, server, session, this](bool loadedAppData) {
121-
// if (!loadedAppData || !this->_appData.has_value()) {
122-
// CesiumAsync::Promise<CesiumIonClient::Connection> promise =
123-
// this->_asyncSystem.createPromise<CesiumIonClient::Connection>();
124-
125-
// promise.reject(std::runtime_error("Failed to load _appData, can't create connection"));
126-
// return promise.getFuture();
127-
// }
128-
129-
// if (this->_appData->needsOauthAuthentication()) {
130-
// int64_t clientID = server.oauth2ApplicationID();
131-
// return CesiumIonClient::Connection::authorize(
132-
// this->_asyncSystem,
133-
// this->_pAssetAccessor,
134-
// "Cesium for Unity",
135-
// clientID,
136-
// "/cesium-for-unity/oauth2/callback",
137-
// {"assets:list", "assets:read", "profile:read", "tokens:read", "tokens:write", "geocode"},
138-
// [this](const std::string& url) {
139-
// this->_authorizeUrl = url;
140-
// this->_redirectUrl = CesiumUtility::Uri::getQueryValue(url, "redirect_uri");
141-
// UnityEngine::Application::OpenURL(url);
142-
// },
143-
// this->_appData.value(),
144-
// server.apiUrl().ToStlString(),
145-
// CesiumUtility::Uri::resolve(ionServerUrl, "oauth"));
146-
// }
147-
148-
// return this->_asyncSystem.createResolvedFuture<CesiumIonClient::Connection>(CesiumIonClient::Connection(
149-
// this->_asyncSystem, this->_pAssetAccessor, "", this->_appData.value(), server.apiUrl().ToStlString()));
150-
// })
151-
// .thenInMainThread([this, session](CesiumIonClient::Connection&& connection) {
152-
// this->_isConnecting = false;
153-
// this->_connection = std::move(connection);
154-
155-
// CesiumForUnity::CesiumIonServer server = session.server();
156-
// CesiumForUnity::CesiumIonServerManager::instance().SetUserAccessToken(
157-
// server, this->_connection.value().getAccessToken());
158-
// this->_quickAddItems = nullptr;
159-
// this->broadcastConnectionUpdate();
160-
// })
161-
// .catchInMainThread([this](std::exception&& e) {
162-
// DotNet::UnityEngine::Debug::Log(System::String(e.what()));
163-
// this->_isConnecting = false;
164-
// this->_connection = std::nullopt;
165-
// this->_quickAddItems = nullptr;
166-
// this->broadcastConnectionUpdate();
167-
// });
168116
}
169117

170118
void CesiumIonSession::resume() {

0 commit comments

Comments
 (0)