// ---------[ Internal Callbacks ]--------- private void SubmissionError_Local(string errorMessage) { if (this != null && this.onError != null) { WebRequestError error = WebRequestError.GenerateLocal(errorMessage); this.onError.Invoke(error); } }
private static void DownloadModBinary_Internal(ModfileIdPair idPair, string downloadURL) { FileDownloadInfo downloadInfo = modfileDownloadMap[idPair]; downloadInfo.request = UnityWebRequest.Get(downloadURL); string tempFilePath = downloadInfo.target + ".download"; DataStorage.WriteFile(tempFilePath, new byte[0], (p, success) => { if (success) { downloadInfo.request.downloadHandler = new DownloadHandlerFile(tempFilePath); #if PLATFORM_PS4 // NOTE(@jackson): This workaround addresses an issue in UnityWebRequests on the // PS4 whereby redirects fail in specific cases. Special thanks to @Eamon of // Spiderling Studios (http://spiderlinggames.co.uk/) downloadInfo.request.redirectLimit = 0; #endif var operation = downloadInfo.request.SendWebRequest(); #if DEBUG DebugUtilities.DebugDownload(operation, LocalUser.instance, tempFilePath); #endif operation.completed += (o) => DownloadClient.OnModBinaryRequestCompleted(idPair); DownloadClient.StartMonitoringSpeed(); // notify download started if (DownloadClient.modfileDownloadStarted != null) { DownloadClient.modfileDownloadStarted(idPair, downloadInfo); } } else if (DownloadClient.modfileDownloadFailed != null) { string warningInfo = ("Failed to create download file on disk." + "\nSource: " + downloadURL + "\nDestination: " + tempFilePath + "\n\n"); modfileDownloadFailed(idPair, WebRequestError.GenerateLocal(warningInfo)); } }); }
/// <summary>Attempts to reauthenticate using the stored external auth ticket.</summary> public static void ReauthenticateWithStoredExternalAuthData(bool hasUserAcceptedTerms, Action <UserProfile> onSuccess, Action <WebRequestError> onError) { ExternalAuthenticationData authData = LocalUser.ExternalAuthentication; Debug.Assert(!string.IsNullOrEmpty(authData.ticket)); Debug.Assert(authData.provider != ExternalAuthenticationProvider.None); Action <string> onSuccessWrapper = (t) => { LocalUser.OAuthToken = t; LocalUser.WasTokenRejected = false; LocalUser.Save(); if (onSuccess != null) { UserAccountManagement.UpdateUserProfile(onSuccess, onError); } }; switch (LocalUser.ExternalAuthentication.provider) { case ExternalAuthenticationProvider.Steam: { APIClient.RequestSteamAuthentication(authData.ticket, hasUserAcceptedTerms, onSuccessWrapper, onError); } break; case ExternalAuthenticationProvider.GOG: { APIClient.RequestGOGAuthentication(authData.ticket, hasUserAcceptedTerms, onSuccessWrapper, onError); } break; case ExternalAuthenticationProvider.ItchIO: { APIClient.RequestItchIOAuthentication(authData.ticket, hasUserAcceptedTerms, onSuccessWrapper, onError); } break; case ExternalAuthenticationProvider.OculusRift: { string token = authData.ticket; string nonce = null; string userIdString = null; int userId = -1; string errorMessage = null; if (authData.additionalData == null) { errorMessage = "The user id and nonce are missing."; } else if (!authData.additionalData.TryGetValue(ExternalAuthenticationData.OculusRiftKeys.NONCE, out nonce) || string.IsNullOrEmpty(nonce)) { errorMessage = "The nonce is missing."; } else if (!authData.additionalData.TryGetValue(ExternalAuthenticationData.OculusRiftKeys.USER_ID, out userIdString) || string.IsNullOrEmpty(userIdString)) { errorMessage = "The user id is missing."; } else if (!int.TryParse(userIdString, out userId)) { errorMessage = "The user id is not parseable as an integer."; } if (errorMessage != null) { Debug.LogWarning("[mod.io] Unable to authenticate using stored Oculus Rift user data.\n" + errorMessage); if (onError != null) { var error = WebRequestError.GenerateLocal(errorMessage); onError(error); } return; } else { APIClient.RequestOculusRiftAuthentication(nonce, userId, token, hasUserAcceptedTerms, onSuccessWrapper, onError); } } break; case ExternalAuthenticationProvider.XboxLive: { APIClient.RequestXboxLiveAuthentication(authData.ticket, hasUserAcceptedTerms, onSuccessWrapper, onError); } break; default: { throw new System.NotImplementedException(); } } }
private static void OnModBinaryRequestCompleted(ModfileIdPair idPair) { FileDownloadInfo downloadInfo = DownloadClient.modfileDownloadMap[idPair]; UnityWebRequest request = downloadInfo.request; downloadInfo.bytesPerSecond = 0; if (request.isNetworkError || request.isHttpError) { if (request.error.ToUpper() == "USER ABORTED" || request.error.ToUpper() == "REQUEST ABORTED") { downloadInfo.wasAborted = true; DownloadClient.FinalizeDownload(idPair, downloadInfo); } // NOTE(@jackson): This workaround addresses an issue in UnityWebRequests on the // PS4 whereby redirects fail in specific cases. Special thanks to @Eamon of // Spiderling Studios (http://spiderlinggames.co.uk/) #if UNITY_PS4 || MODIO_DEV else if (downloadInfo.error.webRequest.responseCode == 302) // Redirect limit exceeded { string headerLocation = string.Empty; if (downloadInfo.error.webRequest.GetResponseHeaders().TryGetValue("location", out headerLocation) && !request.url.Equals(headerLocation)) { if (PluginSettings.REQUEST_LOGGING.logAllResponses) { Debug.LogFormat("[mod.io] Caught PS4 redirection error. Reattempting.\nURL: {0}", headerLocation); } downloadInfo.error = null; downloadInfo.isDone = false; DownloadModBinary_Internal(idPair, headerLocation); return; } } #endif // UNITY_PS4 else { downloadInfo.error = WebRequestError.GenerateFromWebRequest(request); DownloadClient.FinalizeDownload(idPair, downloadInfo); } } else { DataStorage.MoveFile(downloadInfo.target + ".download", downloadInfo.target, (src, dst, success) => { if (!success) { string errorMessage = ("Download succeeded but failed to rename from" + " temporary file name." + "\nTemporary file name: " + downloadInfo.target + ".download"); downloadInfo.error = WebRequestError.GenerateLocal(errorMessage); } DownloadClient.FinalizeDownload(idPair, downloadInfo); }); } }
private static void OnModBinaryRequestCompleted(ModfileIdPair idPair) { FileDownloadInfo downloadInfo = DownloadClient.modfileDownloadMap[idPair]; UnityWebRequest request = downloadInfo.request; bool succeeded = false; downloadInfo.isDone = true; downloadInfo.bytesPerSecond = 0; if (request.isNetworkError || request.isHttpError) { if (request.error.ToUpper() == "USER ABORTED" || request.error.ToUpper() == "REQUEST ABORTED") { #if DEBUG if (PluginSettings.data.logAllRequests) { Debug.Log("DOWNLOAD ABORTED" + "\nDownload aborted at: " + ServerTimeStamp.Now + "\nURL: " + request.url); } #endif downloadInfo.wasAborted = true; } // NOTE(@jackson): This workaround addresses an issue in UnityWebRequests on the // PS4 whereby redirects fail in specific cases. Special thanks to @Eamon of // Spiderling Studios (http://spiderlinggames.co.uk/) #if UNITY_PS4 else if (downloadInfo.error.responseCode == 302) // Redirect limit exceeded { string headerLocation = string.Empty; if (downloadInfo.error.responseHeaders.TryGetValue("location", out headerLocation) && !request.url.Equals(headerLocation)) { if (PluginSettings.data.logAllRequests) { Debug.LogFormat("CAUGHT DOWNLOAD REDIRECTION\nURL: {0}", headerLocation); } downloadInfo.error = null; downloadInfo.isDone = false; DownloadModBinary_Internal(idPair, headerLocation); return; } } #endif else { downloadInfo.error = WebRequestError.GenerateFromWebRequest(request); if (PluginSettings.data.logAllRequests) { WebRequestError.LogAsWarning(downloadInfo.error); } if (modfileDownloadFailed != null) { modfileDownloadFailed(idPair, downloadInfo.error); } } } else { try { if (File.Exists(downloadInfo.target)) { File.Delete(downloadInfo.target); } File.Move(downloadInfo.target + ".download", downloadInfo.target); succeeded = true; } catch (Exception e) { string warningInfo = ("Failed to save mod binary." + "\nFile: " + downloadInfo.target + "\n\n"); Debug.LogWarning("[mod.io] " + warningInfo + Utility.GenerateExceptionDebugString(e)); downloadInfo.error = WebRequestError.GenerateLocal(warningInfo); if (modfileDownloadFailed != null) { modfileDownloadFailed(idPair, downloadInfo.error); } } } if (succeeded) { #if DEBUG if (PluginSettings.data.logAllRequests) { var responseTimeStamp = ServerTimeStamp.Now; Debug.Log("DOWNLOAD SUCEEDED" + "\nDownload completed at: " + ServerTimeStamp.ToLocalDateTime(responseTimeStamp) + "\nURL: " + request.url + "\nFilePath: " + downloadInfo.target); } #endif if (modfileDownloadSucceeded != null) { modfileDownloadSucceeded(idPair, downloadInfo); } } modfileDownloadMap.Remove(idPair); DownloadClient.modfileProgressMarkers.Remove(idPair); }
private static void DownloadModBinary_Internal(ModfileIdPair idPair, string downloadURL) { FileDownloadInfo downloadInfo = modfileDownloadMap[idPair]; downloadInfo.request = UnityWebRequest.Get(downloadURL); string tempFilePath = downloadInfo.target + ".download"; try { Directory.CreateDirectory(Path.GetDirectoryName(tempFilePath)); downloadInfo.request.downloadHandler = new DownloadHandlerFile(tempFilePath); #if PLATFORM_PS4 // NOTE(@jackson): This workaround addresses an issue in UnityWebRequests on the // PS4 whereby redirects fail in specific cases. Special thanks to @Eamon of // Spiderling Studios (http://spiderlinggames.co.uk/) downloadInfo.request.redirectLimit = 0; #endif } catch (Exception e) { string warningInfo = ("Failed to create download file on disk." + "\nFile: " + tempFilePath + "\n\n"); Debug.LogWarning("[mod.io] " + warningInfo + Utility.GenerateExceptionDebugString(e)); if (modfileDownloadFailed != null) { modfileDownloadFailed(idPair, WebRequestError.GenerateLocal(warningInfo)); } return; } var operation = downloadInfo.request.SendWebRequest(); #if DEBUG if (PluginSettings.data.logAllRequests) { string requestHeaders = ""; List <string> requestKeys = new List <string>(APIClient.UNITY_REQUEST_HEADER_KEYS); requestKeys.AddRange(APIClient.MODIO_REQUEST_HEADER_KEYS); foreach (string headerKey in requestKeys) { string headerValue = downloadInfo.request.GetRequestHeader(headerKey); if (headerValue != null) { requestHeaders += "\n" + headerKey + ": " + headerValue; } } int timeStamp = ServerTimeStamp.Now; Debug.Log("DOWNLOAD REQUEST SENT" + "\nTimeStamp: [" + timeStamp.ToString() + "] " + ServerTimeStamp.ToLocalDateTime(timeStamp).ToString() + "\nURL: " + downloadInfo.request.url + "\nHeaders: " + requestHeaders); } #endif operation.completed += (o) => DownloadClient.OnModBinaryRequestCompleted(idPair); DownloadClient.StartMonitoringSpeed(); // notify download started if (DownloadClient.modfileDownloadStarted != null) { DownloadClient.modfileDownloadStarted(idPair, downloadInfo); } }