diff --git a/src/Packages/Passport/Runtime/Scripts/Private/Core/BrowserCommunicationsManager.cs b/src/Packages/Passport/Runtime/Scripts/Private/Core/BrowserCommunicationsManager.cs index dcd2085a..214fd5f7 100644 --- a/src/Packages/Passport/Runtime/Scripts/Private/Core/BrowserCommunicationsManager.cs +++ b/src/Packages/Passport/Runtime/Scripts/Private/Core/BrowserCommunicationsManager.cs @@ -21,7 +21,7 @@ public interface IBrowserCommunicationsManager #endif void SetCallTimeout(int ms); void LaunchAuthURL(string url, string redirectUri); - UniTask Call(string fxName, string data = null, bool ignoreTimeout = false, Nullable timeoutMs = null); + UniTask Call(string fxName, string? data = null, bool ignoreTimeout = false, Nullable timeoutMs = null); #if (UNITY_IPHONE && !UNITY_EDITOR) || (UNITY_ANDROID && !UNITY_EDITOR) void ClearCache(bool includeDiskFiles); void ClearStorage(); @@ -39,14 +39,14 @@ public class BrowserCommunicationsManager : IBrowserCommunicationsManager private readonly IDictionary> requestTaskMap = new Dictionary>(); private readonly IWebBrowserClient webBrowserClient; - public event OnBrowserReadyDelegate OnReady; + public event OnBrowserReadyDelegate? OnReady; /// /// PKCE in some platforms such as iOS and macOS will not trigger a deeplink and a proper callback needs to be /// setup. /// - public event OnUnityPostMessageDelegate OnAuthPostMessage; - public event OnUnityPostMessageErrorDelegate OnPostMessageError; + public event OnUnityPostMessageDelegate? OnAuthPostMessage; + public event OnUnityPostMessageErrorDelegate? OnPostMessageError; /// /// Timeout time for waiting for each call to respond in milliseconds @@ -71,31 +71,23 @@ public void SetCallTimeout(int ms) callTimeout = ms; } - public UniTask Call(string fxName, string data = null, bool ignoreTimeout = false, Nullable timeoutMs = null) + public UniTask Call(string fxName, string? data = null, bool ignoreTimeout = false, long? timeoutMs = null) { var t = new UniTaskCompletionSource(); - string requestId = Guid.NewGuid().ToString(); + var requestId = Guid.NewGuid().ToString(); // Add task completion source to the map so we can return the response requestTaskMap.Add(requestId, t); CallFunction(requestId, fxName, data); - if (ignoreTimeout) - return t.Task; - else - return t.Task.Timeout(TimeSpan.FromMilliseconds(timeoutMs ?? callTimeout)); + return ignoreTimeout ? t.Task : t.Task.Timeout(TimeSpan.FromMilliseconds(timeoutMs ?? callTimeout)); } - private void CallFunction(string requestId, string fxName, string data = null) + private void CallFunction(string requestId, string fxName, string? data = null) { - BrowserRequest request = new BrowserRequest() - { - fxName = fxName, - requestId = requestId, - data = data - }; - string requestJson = JsonUtility.ToJson(request).Replace("\\", "\\\\").Replace("\"", "\\\""); + var request = new BrowserRequest(fxName, requestId, data); + var requestJson = JsonUtility.ToJson(request).Replace("\\", "\\\\").Replace("\"", "\\\""); // Call the function on the JS side - string js = $"callFunction(\"{requestJson}\")"; + var js = $"callFunction(\"{requestJson}\")"; if (fxName != PassportAnalytics.TRACK) { @@ -140,25 +132,19 @@ private void InvokeOnUnityPostMessage(string message) private void InvokeOnAuthPostMessage(string message) { PassportLogger.Info($"{TAG} Auth message received: {message}"); - if (OnAuthPostMessage != null) - { - OnAuthPostMessage.Invoke(message); - } + OnAuthPostMessage?.Invoke(message); } private void InvokeOnPostMessageError(string id, string message) { PassportLogger.Info($"{TAG} Error message received ({id}): {message}"); - if (OnPostMessageError != null) - { - OnPostMessageError.Invoke(id, message); - } + OnPostMessageError?.Invoke(id, message); } private void HandleResponse(string message) { PassportLogger.Debug($"{TAG} Handle response message: " + message); - BrowserResponse response = message.OptDeserializeObject(); + var response = message.OptDeserializeObject(); // Validate the deserialised response object if (response == null || string.IsNullOrEmpty(response.responseFor) || string.IsNullOrEmpty(response.requestId)) @@ -181,10 +167,7 @@ private void HandleResponse(string message) if (response.responseFor == INIT && response.requestId == INIT_REQUEST_ID) { PassportLogger.Info($"{TAG} Browser is ready"); - if (OnReady != null) - { - OnReady.Invoke(); - } + OnReady?.Invoke(); return; } @@ -230,13 +213,13 @@ private PassportException ParseError(BrowserResponse response) private void NotifyRequestResult(string requestId, string result) { - BrowserResponse response = result.OptDeserializeObject(); - UniTaskCompletionSource completion = requestTaskMap[requestId] as UniTaskCompletionSource; + var response = result.OptDeserializeObject(); + var completion = requestTaskMap[requestId] as UniTaskCompletionSource; try { - if (response.success == false || !String.IsNullOrEmpty(response.error)) + if (response?.success == false || !string.IsNullOrEmpty(response?.error)) { - PassportException exception = ParseError(response); + var exception = ParseError(response); if (!completion.TrySetException(exception)) throw new PassportException($"Unable to set exception for for request id {requestId}. Task has already been completed."); } diff --git a/src/Packages/Passport/Runtime/Scripts/Private/Core/Model/BrowserRequest.cs b/src/Packages/Passport/Runtime/Scripts/Private/Core/Model/BrowserRequest.cs index 694968ca..ff214553 100644 --- a/src/Packages/Passport/Runtime/Scripts/Private/Core/Model/BrowserRequest.cs +++ b/src/Packages/Passport/Runtime/Scripts/Private/Core/Model/BrowserRequest.cs @@ -7,7 +7,14 @@ public class BrowserRequest { public string fxName; public string requestId; - public string data; + public string? data; + + public BrowserRequest(string fxName, string requestId, string? data) + { + this.fxName = fxName; + this.requestId = requestId; + this.data = data; + } } } diff --git a/src/Packages/Passport/Runtime/Scripts/Private/Event/AnalyticsEvent.cs b/src/Packages/Passport/Runtime/Scripts/Private/Event/AnalyticsEvent.cs index 1f22139a..57045a37 100644 --- a/src/Packages/Passport/Runtime/Scripts/Private/Event/AnalyticsEvent.cs +++ b/src/Packages/Passport/Runtime/Scripts/Private/Event/AnalyticsEvent.cs @@ -18,21 +18,16 @@ public static class EventName public const string INIT_PASSPORT = "initialisedPassport"; // Login - public const string START_LOGIN = "startedLogin"; - public const string COMPLETE_LOGIN = "performedLogin"; public const string START_LOGIN_PKCE = "startedLoginPkce"; public const string COMPLETE_LOGIN_PKCE = "performedLoginPkce"; public const string COMPLETE_RELOGIN = "performedRelogin"; // Connect - public const string START_CONNECT_IMX = "startedConnectImx"; - public const string COMPLETE_CONNECT_IMX = "performedConnectImx"; public const string START_CONNECT_IMX_PKCE = "startedConnectImxPkce"; public const string COMPLETE_CONNECT_IMX_PKCE = "performedConnectImxPkce"; public const string COMPLETE_RECONNECT = "performedReconnect"; // Logout - public const string COMPLETE_LOGOUT = "performedLogout"; public const string COMPLETE_LOGOUT_PKCE = "performedLogoutPkce"; } @@ -42,7 +37,7 @@ public static class Properties } public async UniTask Track(IBrowserCommunicationsManager communicationsManager, string eventName, - bool? success = null, Dictionary properties = null) + bool? success = null, Dictionary? properties = null) { try { diff --git a/src/Packages/Passport/Runtime/Scripts/Private/Helpers/JsonHelpers.cs b/src/Packages/Passport/Runtime/Scripts/Private/Helpers/JsonHelpers.cs index 1bf7c7b8..41d5c6c2 100644 --- a/src/Packages/Passport/Runtime/Scripts/Private/Helpers/JsonHelpers.cs +++ b/src/Packages/Passport/Runtime/Scripts/Private/Helpers/JsonHelpers.cs @@ -80,7 +80,9 @@ public static string ToJson(this IDictionary dictionary) [Serializable] private class Wrapper { +#pragma warning disable CS8618 public T[] Items; +#pragma warning restore CS8618 } } } \ No newline at end of file diff --git a/src/Packages/Passport/Runtime/Scripts/Private/Helpers/UriHelpers.cs b/src/Packages/Passport/Runtime/Scripts/Private/Helpers/UriHelpers.cs index 895002e3..1336915d 100644 --- a/src/Packages/Passport/Runtime/Scripts/Private/Helpers/UriHelpers.cs +++ b/src/Packages/Passport/Runtime/Scripts/Private/Helpers/UriHelpers.cs @@ -8,7 +8,7 @@ public static class UriExtensions /// /// Gets the specified query parameter from the given URI /// - public static string GetQueryParameter(this Uri uri, string key) + public static string? GetQueryParameter(this Uri uri, string key) { try { diff --git a/src/Packages/Passport/Runtime/Scripts/Private/Helpers/WindowsDeepLink.cs b/src/Packages/Passport/Runtime/Scripts/Private/Helpers/WindowsDeepLink.cs index bd810ad9..15ce82af 100644 --- a/src/Packages/Passport/Runtime/Scripts/Private/Helpers/WindowsDeepLink.cs +++ b/src/Packages/Passport/Runtime/Scripts/Private/Helpers/WindowsDeepLink.cs @@ -11,7 +11,7 @@ namespace Immutable.Passport.Helpers { public class WindowsDeepLink : MonoBehaviour { - private const string RegistryDeepLinkName = "deeplink"; + private const string REGISTRY_DEEP_LINK_NAME = "deeplink"; private static WindowsDeepLink? _instance; private Action? _callback; @@ -28,8 +28,8 @@ public class WindowsDeepLink : MonoBehaviour private static extern int RegCreateKeyEx( UIntPtr hKey, string lpSubKey, - int Reserved, - string lpClass, + int reserved, + string? lpClass, uint dwOptions, uint samDesired, IntPtr lpSecurityAttributes, @@ -39,8 +39,8 @@ private static extern int RegCreateKeyEx( [DllImport("advapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)] private static extern int RegSetValueEx( UIntPtr hKey, - string lpValueName, - int Reserved, + string? lpValueName, + int reserved, uint dwType, string lpData, uint cbData); @@ -110,7 +110,7 @@ private static void CreateCommandScript(string protocolName) { "@echo off", // Store deeplink URI in registry - $"REG ADD \"HKCU\\Software\\Classes\\{protocolName}\" /v \"{RegistryDeepLinkName}\" /t REG_SZ /d %1 /f >nul 2>&1", + $"REG ADD \"HKCU\\Software\\Classes\\{protocolName}\" /v \"{REGISTRY_DEEP_LINK_NAME}\" /t REG_SZ /d %1 /f >nul 2>&1", "setlocal", "", $"set \"PROJECT_PATH={projectPath}\"", @@ -171,7 +171,7 @@ private static void CreateCommandScript(string protocolName) { "@echo off", // Store deeplink URI in registry - $"REG ADD \"HKCU\\Software\\Classes\\{protocolName}\" /v \"{RegistryDeepLinkName}\" /t REG_SZ /d %1 /f >nul 2>&1", + $"REG ADD \"HKCU\\Software\\Classes\\{protocolName}\" /v \"{REGISTRY_DEEP_LINK_NAME}\" /t REG_SZ /d %1 /f >nul 2>&1", // Check if game is already running $"tasklist /FI \"IMAGENAME eq {gameExeName}\" 2>NUL | find /I \"{gameExeName}\" >NUL", "if %ERRORLEVEL%==0 (", @@ -198,7 +198,7 @@ private static void RegisterProtocol(string protocolName) UIntPtr hKey; uint disposition; // Create registry key for the protocol - int result = RegCreateKeyEx( + var result = RegCreateKeyEx( (UIntPtr)HKEY_CURRENT_USER, $@"Software\Classes\{protocolName}", 0, @@ -216,9 +216,9 @@ private static void RegisterProtocol(string protocolName) // Set the default value for the protocol key to Application.productName // This is often used by Windows as the display name for the protocol - string appProductName = Application.productName; - uint productNameDataSize = (uint)((appProductName.Length + 1) * Marshal.SystemDefaultCharSize); - int setDefaultResult = RegSetValueEx(hKey, null, 0, REG_SZ, appProductName, productNameDataSize); + var appProductName = Application.productName; + var productNameDataSize = (uint)((appProductName.Length + 1) * Marshal.SystemDefaultCharSize); + var setDefaultResult = RegSetValueEx(hKey, null, 0, REG_SZ, appProductName, productNameDataSize); if (setDefaultResult != 0) { @@ -268,14 +268,7 @@ private static void RegisterProtocol(string protocolName) private static string GetGameExecutablePath(string suffix) { var exeName = Application.productName + suffix; -#if UNITY_EDITOR_WIN - // Returns the persistent data path in editor return Path.Combine(Application.persistentDataPath, exeName).Replace("/", "\\"); -#else - // Returns game root directory in build - var exePath = Path.Combine(Application.dataPath, "../"); - return Path.Combine(exePath, exeName).Replace("/", "\\"); -#endif } private void OnApplicationFocus(bool hasFocus) @@ -289,9 +282,9 @@ private void OnApplicationFocus(bool hasFocus) private void HandleDeeplink() { // Open registry key for the protocol - string registryPath = $@"Software\Classes\{_protocolName}"; + var registryPath = $@"Software\Classes\{_protocolName}"; UIntPtr hKey; - int result = RegOpenKeyEx( + var result = RegOpenKeyEx( (UIntPtr)HKEY_CURRENT_USER, registryPath, 0, @@ -307,7 +300,7 @@ private void HandleDeeplink() // Get size of deeplink data uint type = 0; uint dataSize = 0; - result = RegQueryValueEx(hKey, RegistryDeepLinkName, IntPtr.Zero, ref type, null!, ref dataSize); + result = RegQueryValueEx(hKey, REGISTRY_DEEP_LINK_NAME, IntPtr.Zero, ref type, null!, ref dataSize); if (result != 0) { @@ -318,7 +311,7 @@ private void HandleDeeplink() // Read deeplink data var data = new byte[dataSize]; - result = RegQueryValueEx(hKey, RegistryDeepLinkName, IntPtr.Zero, ref type, data, ref dataSize); + result = RegQueryValueEx(hKey, REGISTRY_DEEP_LINK_NAME, IntPtr.Zero, ref type, data, ref dataSize); var callbackInvoked = false; if (result == 0 && type == REG_SZ) diff --git a/src/Packages/Passport/Runtime/Scripts/Private/Model/Request/UnsignedTransferRequest.cs b/src/Packages/Passport/Runtime/Scripts/Private/Model/Request/UnsignedTransferRequest.cs index c222e91f..68852ca5 100644 --- a/src/Packages/Passport/Runtime/Scripts/Private/Model/Request/UnsignedTransferRequest.cs +++ b/src/Packages/Passport/Runtime/Scripts/Private/Model/Request/UnsignedTransferRequest.cs @@ -23,19 +23,19 @@ public class UnsignedTransferRequest /** * The token ID */ - public string tokenId; + public string? tokenId; /** * The token address */ - public string tokenAddress; + public string? tokenAddress; public UnsignedTransferRequest( string type, string amount, string receiver, - string tokenId = null, - string tokenAddress = null + string? tokenId = null, + string? tokenAddress = null ) { this.type = type; diff --git a/src/Packages/Passport/Runtime/Scripts/Private/PassportFunction.cs b/src/Packages/Passport/Runtime/Scripts/Private/PassportFunction.cs index 76c5c967..01022de0 100644 --- a/src/Packages/Passport/Runtime/Scripts/Private/PassportFunction.cs +++ b/src/Packages/Passport/Runtime/Scripts/Private/PassportFunction.cs @@ -3,14 +3,11 @@ namespace Immutable.Passport public static class PassportFunction { public const string INIT = "init"; - public const string INIT_DEVICE_FLOW = "initDeviceFlow"; public const string RELOGIN = "relogin"; public const string RECONNECT = "reconnect"; public const string LOGIN_PKCE = "loginPKCE"; public const string CONNECT_PKCE = "connectPKCE"; public const string GET_PKCE_AUTH_URL = "getPKCEAuthUrl"; - public const string LOGIN_CONFIRM_CODE = "loginConfirmCode"; - public const string CONNECT_CONFIRM_CODE = "connectConfirmCode"; public const string GET_ACCESS_TOKEN = "getAccessToken"; public const string GET_ID_TOKEN = "getIdToken"; public const string LOGOUT = "logout"; diff --git a/src/Packages/Passport/Runtime/Scripts/Private/PassportImpl.cs b/src/Packages/Passport/Runtime/Scripts/Private/PassportImpl.cs index 2b12f4ad..1f0deb8d 100644 --- a/src/Packages/Passport/Runtime/Scripts/Private/PassportImpl.cs +++ b/src/Packages/Passport/Runtime/Scripts/Private/PassportImpl.cs @@ -276,12 +276,12 @@ private async UniTask LaunchAuthUrl() try { var request = new GetPKCEAuthUrlRequest(!_pkceLoginOnly, _directLoginMethod); - string callResponse = await _communicationsManager.Call(PassportFunction.GET_PKCE_AUTH_URL, JsonUtility.ToJson(request)); - StringResponse response = callResponse.OptDeserializeObject(); + var callResponse = await _communicationsManager.Call(PassportFunction.GET_PKCE_AUTH_URL, JsonUtility.ToJson(request)); + var response = callResponse.OptDeserializeObject(); if (response != null && response.success == true && response.result != null) { - string url = response.result.Replace(" ", "+"); + var url = response.result.Replace(" ", "+"); #if UNITY_ANDROID && !UNITY_EDITOR loginPKCEUrl = url; SendAuthEvent(_pkceLoginOnly ? PassportAuthEvent.LoginPKCELaunchingCustomTabs : PassportAuthEvent.ConnectImxPKCELaunchingCustomTabs); @@ -747,7 +747,7 @@ public void ClearStorage() } #endif - protected virtual async void Track(string eventName, bool? success = null, Dictionary properties = null) + protected virtual async void Track(string eventName, bool? success = null, Dictionary? properties = null) { await _analytics.Track(_communicationsManager, eventName, success, properties); }