Skip to content
This repository was archived by the owner on Jan 18, 2022. It is now read-only.

Commit 6ff6038

Browse files
author
Paul Balaji
authored
Move runtime IP to EditorPref / DevAuthToken to PlayerPref (#961)
1 parent 7b3349e commit 6ff6038

File tree

15 files changed

+154
-42
lines changed

15 files changed

+154
-42
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
### Changed
1515

1616
- Upgraded the project to be compatible with `2019.1.3f1`.
17+
- Moved Runtime IP from the `GdkToolsConfiguration.json` to the Editor Preferences.
18+
- Moved Dev Auth Token to the Player Preferences.
19+
- Added a setting in `GdkToolsConfiguration` to let users configure whether a `DevAuthToken.txt` should be generated or not.
20+
- When launching Android cloud clients from the Editor, the DevAuthToken is now passed in as a command line argument.
1721

1822
### Fixed
1923

workers/unity/Assets/Config/GdkToolsConfiguration.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
],
66
"CodegenOutputDir": "Assets/Generated/Source",
77
"DescriptorOutputDir": "../../build/assembly/schema",
8-
"RuntimeIp": "",
98
"DevAuthTokenDir": "Resources",
10-
"DevAuthTokenLifetimeDays": 30
9+
"DevAuthTokenLifetimeDays": 30,
10+
"SaveDevAuthTokenToFile": false
1111
}

workers/unity/Assets/Plugins.meta

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

workers/unity/Packages/com.improbable.gdk.core/Config/RuntimeConfig.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public static class RuntimeConfigNames
2525
public const string LinkProtocol = "linkProtocol";
2626
public const string LocatorHost = "locatorHost";
2727
public const string LoginToken = "loginToken";
28+
public const string DevAuthTokenKey = "devAuthTokenSecret";
2829
public const string PlayerIdentityToken = "playerIdentityToken";
2930
public const string ProjectName = "projectName";
3031
public const string ReceptionistHost = "receptionistHost";
@@ -36,7 +37,7 @@ public static class RuntimeConfigNames
3637
}
3738

3839
/// <summary>
39-
/// Stores the configuration needed to connect via the Lcoator.
40+
/// Stores the configuration needed to connect via the Locator.
4041
/// </summary>
4142
public struct LocatorConfig
4243
{

workers/unity/Packages/com.improbable.gdk.core/Worker/WorkerConnector.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,16 +254,23 @@ protected virtual string SelectDeploymentName(DeploymentList deployments)
254254
/// </summary>
255255
protected virtual string GetDevAuthToken()
256256
{
257+
if (PlayerPrefs.HasKey(RuntimeConfigNames.DevAuthTokenKey))
258+
{
259+
return PlayerPrefs.GetString(RuntimeConfigNames.DevAuthTokenKey);
260+
}
261+
257262
var textAsset = Resources.Load<TextAsset>("DevAuthToken");
258263
if (textAsset != null)
259264
{
260-
return textAsset.text.Trim();
265+
PlayerPrefs.SetString(RuntimeConfigNames.DevAuthTokenKey, textAsset.text.Trim());
261266
}
262267
else
263268
{
264269
throw new MissingReferenceException("Unable to find DevAuthToken.txt in the Resources folder. " +
265270
"You can generate one via SpatialOS > Generate Dev Authentication Token.");
266271
}
272+
273+
return PlayerPrefs.GetString(RuntimeConfigNames.DevAuthTokenKey);
267274
}
268275

269276
/// <summary>

workers/unity/Packages/com.improbable.gdk.mobile/Editor/LaunchMenu.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ public static class LaunchMenu
1616
private const string MenuLaunchAndroidLocal = "/Android for local";
1717
private const string MenuLaunchAndroidCloud = "/Android for cloud";
1818

19-
2019
[MenuItem(MenuLaunchMobile + MenuLaunchAndroidLocal, false, 73)]
2120
private static void LaunchAndroidLocal()
2221
{
@@ -90,6 +89,17 @@ private static void LaunchAndroid(bool shouldConnectLocally)
9089
else
9190
{
9291
arguments.Append($"+{RuntimeConfigNames.Environment} {RuntimeConfigDefaults.CloudEnvironment} ");
92+
93+
var gdkToolsConfig = GdkToolsConfiguration.GetOrCreateInstance();
94+
95+
// Return error if no DevAuthToken is set AND fails to generate new DevAuthToken.
96+
if (!PlayerPrefs.HasKey(RuntimeConfigNames.DevAuthTokenKey) && !DevAuthTokenUtils.TryGenerate())
97+
{
98+
Debug.LogError("Failed to generate a Dev Auth Token to launch mobile client.");
99+
return;
100+
}
101+
102+
arguments.Append($"+{RuntimeConfigNames.DevAuthTokenKey} {DevAuthTokenUtils.DevAuthToken} ");
93103
}
94104

95105
// Get chosen android package id and launch

workers/unity/Packages/com.improbable.gdk.mobile/Improbable.Gdk.Mobile.asmdef

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,10 @@
66
"optionalUnityReferences": [],
77
"includePlatforms": [],
88
"excludePlatforms": [],
9-
"allowUnsafeCode": false
10-
}
9+
"allowUnsafeCode": false,
10+
"overrideReferences": false,
11+
"precompiledReferences": [],
12+
"autoReferenced": true,
13+
"defineConstraints": [],
14+
"versionDefines": []
15+
}

workers/unity/Packages/com.improbable.gdk.mobile/Worker/DefaultMobileWorkerConnector.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ protected virtual void InitializeClient()
4747
PlayerPrefs.DeleteKey(HostIpPlayerPrefsKey);
4848
}
4949

50+
var devAuthToken = CommandLineUtility.GetCommandLineValue(arguments, RuntimeConfigNames.DevAuthTokenKey, string.Empty);
51+
if (!string.IsNullOrEmpty(devAuthToken))
52+
{
53+
PlayerPrefs.SetString(RuntimeConfigNames.DevAuthTokenKey, devAuthToken);
54+
}
55+
5056
PlayerPrefs.Save();
5157
}
5258

workers/unity/Packages/com.improbable.gdk.tools/DevAuthTokenUtils.cs

Lines changed: 68 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,26 @@ namespace Improbable.Gdk.Tools
99
{
1010
public static class DevAuthTokenUtils
1111
{
12+
public static string DevAuthToken => PlayerPrefs.GetString(PlayerPrefDevAuthTokenKey);
13+
14+
private static string DevAuthTokenAssetPath =>
15+
Path.Combine("Assets", GdkToolsConfiguration.GetOrCreateInstance().DevAuthTokenDir, "DevAuthToken.txt");
16+
1217
private static readonly string JsonDataKey = "json_data";
13-
private static readonly string TokenSecretKey = "token_secret";
18+
private static readonly string JsonErrorKey = "error";
19+
private static readonly string JsonTokenSecretKey = "token_secret";
20+
private static readonly string PlayerPrefDevAuthTokenKey = "devAuthTokenSecret";
1421

15-
[MenuItem("SpatialOS/Generate Dev Authentication Token", false, MenuPriorities.GenerateDevAuthToken)]
16-
private static void Generate()
22+
private const string DevAuthMenuPrefix = "SpatialOS/Dev Authentication Token";
23+
private const string DevAuthMenuGenerateToken = "/Generate Token";
24+
private const string DevAuthMenuClearToken = "/Clear Token";
25+
26+
[MenuItem(DevAuthMenuPrefix + DevAuthMenuGenerateToken, false, MenuPriorities.GenerateDevAuthToken)]
27+
public static bool TryGenerate()
1728
{
1829
var devAuthToken = string.Empty;
1930
var gdkToolsConfiguration = GdkToolsConfiguration.GetOrCreateInstance();
20-
var devAuthTokenFullDir = gdkToolsConfiguration.DevAuthTokenFullDir;
21-
var devAuthTokenFilePath = gdkToolsConfiguration.DevAuthTokenFilepath;
31+
2232
var devAuthTokenLifetimeHours = $"{gdkToolsConfiguration.DevAuthTokenLifetimeHours}h";
2333

2434
var receivedMessage = string.Empty;
@@ -33,18 +43,55 @@ private static void Generate()
3343

3444
try
3545
{
36-
if (Json.Deserialize(receivedMessage).TryGetValue(JsonDataKey, out var jsonData) &&
37-
((Dictionary<string, object>) jsonData).TryGetValue(TokenSecretKey, out var tokenSecret))
46+
var deserializedMessage = Json.Deserialize(receivedMessage);
47+
if (deserializedMessage.TryGetValue(JsonDataKey, out var jsonData) &&
48+
((Dictionary<string, object>) jsonData).TryGetValue(JsonTokenSecretKey, out var tokenSecret))
3849
{
3950
devAuthToken = (string) tokenSecret;
4051
}
52+
else
53+
{
54+
if (deserializedMessage.TryGetValue(JsonErrorKey, out var errorMessage))
55+
{
56+
throw new Exception(errorMessage.ToString());
57+
}
58+
59+
throw new Exception(string.Empty);
60+
}
4161
}
4262
catch (Exception e)
4363
{
4464
Debug.LogError($"Unable to generate Dev Auth Token. {e.Message}");
45-
return;
65+
return false;
4666
}
4767

68+
Debug.Log($"Saving token to Player Preferences.");
69+
PlayerPrefs.SetString(PlayerPrefDevAuthTokenKey, devAuthToken);
70+
71+
if (gdkToolsConfiguration.SaveDevAuthTokenToFile)
72+
{
73+
return SaveTokenToFile();
74+
}
75+
76+
return true;
77+
}
78+
79+
private static bool SaveTokenToFile()
80+
{
81+
var gdkToolsConfiguration = GdkToolsConfiguration.GetOrCreateInstance();
82+
var devAuthTokenFullDir = gdkToolsConfiguration.DevAuthTokenFullDir;
83+
var devAuthTokenFilePath = gdkToolsConfiguration.DevAuthTokenFilepath;
84+
85+
if (!PlayerPrefs.HasKey(PlayerPrefDevAuthTokenKey))
86+
{
87+
// Given we call SaveTokenToFile after successfully generating a Dev Auth Token,
88+
// we should never see the following error.
89+
Debug.LogError("Cannot save Development Authentication Token, as it has not been generated.");
90+
return false;
91+
}
92+
93+
var devAuthToken = PlayerPrefs.GetString(PlayerPrefDevAuthTokenKey);
94+
4895
if (!Directory.Exists(devAuthTokenFullDir))
4996
{
5097
Directory.CreateDirectory(devAuthTokenFullDir);
@@ -57,13 +104,21 @@ private static void Generate()
57104
catch (Exception e)
58105
{
59106
Debug.LogError($"Unable to save Dev Auth Token asset. {e.Message}");
60-
return;
107+
return false;
61108
}
62109

63-
Debug.Log($"Saving token {devAuthToken} to {devAuthTokenFilePath}.");
64-
AssetDatabase.ImportAsset(
65-
Path.Combine("Assets", gdkToolsConfiguration.DevAuthTokenDir, "DevAuthToken.txt"),
66-
ImportAssetOptions.ForceUpdate);
110+
Debug.Log($"Saving token to {devAuthTokenFilePath}.");
111+
AssetDatabase.ImportAsset(DevAuthTokenAssetPath, ImportAssetOptions.ForceUpdate);
112+
AssetDatabase.Refresh();
113+
114+
return true;
115+
}
116+
117+
[MenuItem(DevAuthMenuPrefix + DevAuthMenuClearToken, false, MenuPriorities.ClearDevAuthToken)]
118+
private static void ClearToken()
119+
{
120+
PlayerPrefs.DeleteKey(PlayerPrefDevAuthTokenKey);
121+
AssetDatabase.DeleteAsset(DevAuthTokenAssetPath);
67122
AssetDatabase.Refresh();
68123
}
69124
}

workers/unity/Packages/com.improbable.gdk.tools/GdkToolsConfiguration.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.IO;
44
using System.Net;
5+
using UnityEditor;
56
using UnityEngine;
67

78
namespace Improbable.Gdk.Tools
@@ -13,18 +14,19 @@ public class GdkToolsConfiguration
1314
public List<string> SchemaSourceDirs = new List<string>();
1415
public string CodegenOutputDir;
1516
public string DescriptorOutputDir;
16-
public string RuntimeIp;
1717
public string DevAuthTokenDir;
1818
public int DevAuthTokenLifetimeDays;
19+
public bool SaveDevAuthTokenToFile;
20+
21+
internal string RuntimeIpEditorPrefKey = "RuntimeIp";
22+
public string RuntimeIp => EditorPrefs.GetString(RuntimeIpEditorPrefKey);
1923

2024
public string DevAuthTokenFullDir => Path.Combine(Application.dataPath, DevAuthTokenDir);
2125
public string DevAuthTokenFilepath => Path.Combine(DevAuthTokenFullDir, "DevAuthToken.txt");
2226
public int DevAuthTokenLifetimeHours => TimeSpan.FromDays(DevAuthTokenLifetimeDays).Hours;
2327

2428
private static readonly string JsonFilePath = Path.GetFullPath("Assets/Config/GdkToolsConfiguration.json");
2529

26-
private static readonly string UnityProjectRoot = Path.Combine(Application.dataPath, "..");
27-
2830
private GdkToolsConfiguration()
2931
{
3032
ResetToDefault();
@@ -66,7 +68,7 @@ internal List<string> Validate()
6668

6769
try
6870
{
69-
var fullSchemaSourceDirPath = Path.Combine(UnityProjectRoot, schemaSourceDir);
71+
var fullSchemaSourceDirPath = Path.Combine(Application.dataPath, "..", schemaSourceDir);
7072
if (!Directory.Exists(fullSchemaSourceDirPath))
7173
{
7274
errors.Add($"{fullSchemaSourceDirPath} cannot be found.");
@@ -83,6 +85,11 @@ internal List<string> Validate()
8385
errors.Add($"Runtime IP \"{RuntimeIp}\" is not a valid IP address.");
8486
}
8587

88+
if (!SaveDevAuthTokenToFile)
89+
{
90+
return errors;
91+
}
92+
8693
if (string.IsNullOrEmpty(DevAuthTokenDir))
8794
{
8895
errors.Add($"{GdkToolsConfigurationWindow.DevAuthTokenDirLabel} cannot be empty.");
@@ -101,7 +108,6 @@ internal void ResetToDefault()
101108
SchemaStdLibDir = DefaultValues.SchemaStdLibDir;
102109
CodegenOutputDir = DefaultValues.CodegenOutputDir;
103110
DescriptorOutputDir = DefaultValues.DescriptorOutputDir;
104-
RuntimeIp = DefaultValues.RuntimeIp;
105111
DevAuthTokenDir = DefaultValues.DevAuthTokenDir;
106112
DevAuthTokenLifetimeDays = DefaultValues.DevAuthTokenLifetimeDays;
107113

@@ -134,7 +140,6 @@ private static class DefaultValues
134140
public const string CodegenOutputDir = "Assets/Generated/Source";
135141
public const string DescriptorOutputDir = "../../build/assembly/schema";
136142
public const string SchemaSourceDir = "../../schema";
137-
public const string RuntimeIp = null;
138143
public const string DevAuthTokenDir = "Resources";
139144
public const int DevAuthTokenLifetimeDays = 30;
140145
}

0 commit comments

Comments
 (0)