Skip to content

Commit

Permalink
CesiumIonServer, CesiumIonServerManager.
Browse files Browse the repository at this point in the history
  • Loading branch information
kring committed Nov 29, 2023
1 parent 5c53765 commit 2b2d07a
Show file tree
Hide file tree
Showing 10 changed files with 234 additions and 10 deletions.
3 changes: 2 additions & 1 deletion Editor/CesiumEditorWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public partial class CesiumEditorWindow : EditorWindow
{
public static CesiumEditorWindow currentWindow = null;

[MenuItem("Cesium/Cesium")]
[MenuItem("Tools/Cesium/Cesium")]
public static void ShowWindow()
{
// If no existing window, make a new one docked next to the Hierarchy window.
Expand Down Expand Up @@ -60,6 +60,7 @@ void OnGUI()
this._isIonLoadingProfile = ion.IsLoadingProfile();
}

CesiumIonServerUI.Selector();
this.DrawCesiumToolbar();

this._scrollPosition = EditorGUILayout.BeginScrollView(this._scrollPosition);
Expand Down
2 changes: 1 addition & 1 deletion Editor/CesiumIonAssetsWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class CesiumIonAssetsWindow : EditorWindow
{
public static CesiumIonAssetsWindow currentWindow = null;

[MenuItem("Cesium/Cesium ion Assets")]
[MenuItem("Tools/Cesium/Cesium ion Assets")]
public static void ShowWindow()
{
if (currentWindow == null)
Expand Down
95 changes: 95 additions & 0 deletions Editor/CesiumIonServerManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
using System.Collections.Generic;
using System;
using UnityEditor;
using UnityEngine;

namespace CesiumForUnity
{
[FilePath("UserSettings/CesiumIonServerManager.asset", FilePathAttribute.Location.ProjectFolder)]
public class CesiumIonServerManager : ScriptableSingleton<CesiumIonServerManager>
{
public CesiumIonServer Current
{
get
{
if (this._currentCesiumIonServer == null)
{
this._currentCesiumIonServer = CesiumIonServer.defaultServer;

// For backward compatibility, look for an existing user access token in the EditorPrefs
// and move it to the user access token map.
if (string.IsNullOrEmpty(this.GetUserAccessToken(this._currentCesiumIonServer)))
{
const string editorPrefKey = "CesiumUserAccessToken";
string userAccessToken = EditorPrefs.GetString(editorPrefKey);
if (!string.IsNullOrEmpty(userAccessToken))
{
this.SetUserAccessToken(this._currentCesiumIonServer, userAccessToken);
EditorPrefs.DeleteKey(editorPrefKey);
}
}

}
return this._currentCesiumIonServer;
}
}

public CesiumIonSession CurrentSession
{
get
{
return this.GetSession(this.Current);
}
}

public CesiumIonSession GetSession(CesiumIonServer server)
{
CesiumIonSession session;
if (!this._sessions.TryGetValue(server, out session))
{
session = new CesiumIonSession(server);
this._sessions.Add(server, session);
}
return session;
}

private string GetUserAccessToken(CesiumIonServer server)
{
int index = this._userAccessTokenMap.FindIndex(record => record.server == server);
return index >= 0 ? this._userAccessTokenMap[index].token : null;
}

private void SetUserAccessToken(CesiumIonServer server, string token)
{
int index = this._userAccessTokenMap.FindIndex(record => record.server == server);
if (index >= 0)
{
this._userAccessTokenMap[index].token = token;
}
else
{
UserAccessTokenRecord record = new UserAccessTokenRecord();
record.server = server;
record.token = token;
this._userAccessTokenMap.Add(record);
}

this.Save(true);
}

[Serializable]
private class UserAccessTokenRecord
{
public CesiumIonServer server;
public string token;
}

[SerializeField]
private List<UserAccessTokenRecord> _userAccessTokenMap = new List<UserAccessTokenRecord>();

[SerializeField]
private CesiumIonServer _currentCesiumIonServer;

private Dictionary<CesiumIonServer, CesiumIonSession> _sessions = new Dictionary<CesiumIonServer, CesiumIonSession>();
}
}
11 changes: 11 additions & 0 deletions Editor/CesiumIonServerManager.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions Editor/CesiumIonServerUI.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using UnityEditor;
using UnityEngine;

namespace CesiumForUnity
{
public class CesiumIonServerUI
{
public static void Selector()
{

}
}
}
11 changes: 11 additions & 0 deletions Editor/CesiumIonServerUI.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 8 additions & 7 deletions Editor/CesiumIonSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ namespace CesiumForUnity
[ReinteropNativeImplementation("CesiumForUnityNative::CesiumIonSessionImpl", "CesiumIonSessionImpl.h")]
public partial class CesiumIonSession
{
private static CesiumIonSession currentSession = null!;

public delegate void GUIUpdateDelegate();

public static event GUIUpdateDelegate OnConnectionUpdated;
Expand All @@ -16,12 +14,15 @@ public partial class CesiumIonSession

public static CesiumIonSession Ion()
{
if (currentSession == null)
{
currentSession = new CesiumIonSession();
}
return CesiumIonServerManager.instance.CurrentSession;
}

internal CesiumIonServer server;

return currentSession;
public CesiumIonSession(CesiumIonServer server)
{
this.server = server;
this.CreateImplementation();
}

public partial bool IsConnected();
Expand Down
81 changes: 81 additions & 0 deletions Runtime/CesiumIonServer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using UnityEditor;
using UnityEngine;

namespace CesiumForUnity
{
/// <summary>
/// Defines a Cesium ion Server. This may be the public (SaaS) Cesium ion server at
/// ion.cesium.com, or it may be a self-hosted instance.
/// </summary>
[CreateAssetMenu(fileName = "CesiumIonServer", menuName = "Cesium/Cesium ion Server")]
public class CesiumIonServer : ScriptableObject
{
/// <summary>
/// The main URL of the Cesium ion server. For example, the server URL for the
/// public Cesium ion is https://ion.cesium.com.
/// </summary>
public string serverUrl = "https://ion.cesium.com";

/// <summary>
/// The URL of the main API endpoint of the Cesium ion server. For example, for
/// the default, public Cesium ion server, this is `https://api.cesium.com`. If
/// left blank, the API URL is automatically inferred from the Server URL.
/// </summary>
public string apiUrl = "https://api.cesium.com";

/// <summary>
/// The application ID to use to log in to this server using OAuth2. This
/// OAuth2 application must be configured on the server with the exact URL
/// `http://127.0.0.1/cesium-for-unreal/oauth2/callback`.
/// </summary>
public long oauth2ApplicationID = 190;

/// <summary>
/// The ID of the default access token to use to access Cesium ion assets at
/// runtime. This property may be an empty string, in which case the ID is
/// found by searching the logged-in Cesium ion account for the
/// DefaultIonAccessToken.
/// </summary>
public string defaultIonAccessTokenId;

/// <summary>
/// The default token used to access Cesium ion assets at runtime. This token
/// is embedded in packaged games for use at runtime.
/// </summary>
public string defaultIonAccessToken;

/// <summary>
/// Gets the default Cesium ion Server (ion.cesium.com).
/// </summary>
/// <remarks>
/// It is expected to be found at `Assets/CesiumSettings/CesiumIonServers/ion.cesium.com`.
/// In the Editor, it will be created if it does not already exist, so this method always
/// returns a valid instance. At runtime, this method returns null if the object does not
/// exist.
/// </remarks>
public static CesiumIonServer defaultServer
{
get
{
CesiumIonServer result = Resources.Load<CesiumIonServer>("CesiumIonServers/ion.cesium.com");

#if UNITY_EDITOR
if (result == null)
{
if (!AssetDatabase.IsValidFolder("Assets/CesiumSettings"))
AssetDatabase.CreateFolder("Assets", "CesiumSettings");
if (!AssetDatabase.IsValidFolder("Assets/CesiumSettings/Resources"))
AssetDatabase.CreateFolder("Assets/CesiumSettings", "Resources");
if (!AssetDatabase.IsValidFolder("Assets/CesiumSettings/Resources/CesiumIonServers"))
AssetDatabase.CreateFolder("Assets/CesiumSettings/Resources", "CesiumIonServers");
result = ScriptableObject.CreateInstance<CesiumIonServer>();
AssetDatabase.CreateAsset(result, "Assets/CesiumSettings/Resources/CesiumIonServers/ion.cesium.com.asset");
AssetDatabase.Refresh();
}
#endif

return result;
}
}
}
}
11 changes: 11 additions & 0 deletions Runtime/CesiumIonServer.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion native~/extern/cesium-native

0 comments on commit 2b2d07a

Please sign in to comment.