Пример #1
0
        private static void OnImageDownloadCompleted(UnityWebRequestAsyncOperation operation,
                                                     ImageRequest request)
        {
            UnityWebRequest webRequest = operation.webRequest;

            request.isDone = true;

            if (webRequest.isNetworkError || webRequest.isHttpError)
            {
                request.error = WebRequestError.GenerateFromWebRequest(webRequest);
                request.NotifyFailed();
            }
            else
            {
                #if DEBUG
                if (PluginSettings.data.logAllRequests)
                {
                    var responseTimeStamp = ServerTimeStamp.Now;
                    Debug.Log("IMAGE DOWNLOAD SUCEEDED"
                              + "\nDownload completed at: " + ServerTimeStamp.ToLocalDateTime(responseTimeStamp)
                              + "\nURL: " + webRequest.url);
                }
                #endif

                request.imageTexture = (webRequest.downloadHandler as DownloadHandlerTexture).texture;
                request.NotifySucceeded();
            }
        }
Пример #2
0
        private static void OnImageDownloadCompleted(UnityWebRequestAsyncOperation operation,
                                                     ImageRequest request)
        {
            UnityWebRequest webRequest = operation.webRequest;

            request.isDone = true;

            if (webRequest.isNetworkError || webRequest.isHttpError)
            {
                request.error = WebRequestError.GenerateFromWebRequest(webRequest);
                request.NotifyFailed();
            }
            else
            {
                #if DEBUG
                if (DownloadClient.logAllRequests)
                {
                    var responseTimeStamp = ServerTimeStamp.Now;
                    Debug.Log(String.Format("{0} REQUEST SUCEEDED\nResponse received at: {1} [{2}]\nURL: {3}\nResponse: {4}\n",
                                            webRequest.method.ToUpper(),
                                            ServerTimeStamp.ToLocalDateTime(responseTimeStamp),
                                            responseTimeStamp,
                                            webRequest.url,
                                            webRequest.downloadHandler.text));
                }
                #endif

                request.imageTexture = (webRequest.downloadHandler as DownloadHandlerTexture).texture;
                request.NotifySucceeded();
            }
        }
Пример #3
0
        private static void OnModBinaryRequestCompleted(UnityWebRequestAsyncOperation operation,
                                                        ModBinaryRequest request)
        {
            UnityWebRequest webRequest = operation.webRequest;

            request.isDone = true;

            if (webRequest.isNetworkError || webRequest.isHttpError)
            {
                request.error = WebRequestError.GenerateFromWebRequest(webRequest);

                request.NotifyFailed();
            }
            else
            {
                #if DEBUG
                if (GlobalSettings.LOG_ALL_WEBREQUESTS)
                {
                    var responseTimeStamp = ServerTimeStamp.Now;
                    Debug.Log("DOWNLOAD SUCEEDED"
                              + "\nDownload completed at: " + ServerTimeStamp.ToLocalDateTime(responseTimeStamp)
                              + "\nURL: " + webRequest.url
                              + "\nFilePath: " + request.binaryFilePath);
                }
                #endif

                try
                {
                    if (File.Exists(request.binaryFilePath))
                    {
                        File.Delete(request.binaryFilePath);
                    }

                    File.Move(request.binaryFilePath + ".download", request.binaryFilePath);
                }
                catch (Exception e)
                {
                    string warningInfo = ("[mod.io] Failed to save mod binary."
                                          + "\nFile: " + request.binaryFilePath + "\n\n");

                    Debug.LogWarning(warningInfo
                                     + Utility.GenerateExceptionDebugString(e));

                    request.NotifyFailed();
                }

                request.NotifySucceeded();
            }
        }
Пример #4
0
        private static void OnImageDownloadCompleted(UnityWebRequestAsyncOperation operation,
                                                     ImageRequest request)
        {
            UnityWebRequest webRequest = operation.webRequest;

            request.isDone = true;

            if (webRequest.isNetworkError || webRequest.isHttpError)
            {
                request.error = WebRequestError.GenerateFromWebRequest(webRequest);
                request.NotifyFailed();
            }
            else
            {
                request.imageTexture = (webRequest.downloadHandler as DownloadHandlerTexture).texture;
                request.NotifySucceeded();
            }
        }
Пример #5
0
        /// <summary>Generates a debug-friendly string of a web request response.</summary>
        public static string GetResponseInfo(UnityWebRequest webRequest)
        {
            if (webRequest == null)
            {
                return("NULL_WEB_REQUEST");
            }

            // get info
            var responseString = new System.Text.StringBuilder();

            responseString.Append("URL: ");
            responseString.Append(webRequest.url);
            responseString.Append(" (");
            responseString.Append(webRequest.method.ToUpper());
            responseString.AppendLine(")");

            responseString.Append("Response Code: ");
            responseString.AppendLine(webRequest.responseCode.ToString());

            responseString.Append("Response Error: ");
            if (string.IsNullOrEmpty(webRequest.error))
            {
                responseString.AppendLine("NO_ERROR");
            }
            else
            {
                responseString.AppendLine(webRequest.error);
            }

            // add request headers
            responseString.AppendLine("Headers:");

            var responseHeaders = webRequest.GetResponseHeaders();

            if (responseHeaders == null ||
                responseHeaders.Count == 0)
            {
                responseString.AppendLine("  NONE");
            }
            else
            {
                foreach (var kvp in responseHeaders)
                {
                    responseString.Append("  ");
                    responseString.Append(kvp.Key);
                    responseString.Append('=');
                    responseString.Append(kvp.Value);
                    responseString.AppendLine();
                }
            }

            // add error information
            if (webRequest.isNetworkError || webRequest.isHttpError)
            {
                var error = WebRequestError.GenerateFromWebRequest(webRequest);

                responseString.AppendLine("mod.io Error Details:");

                // add flags
                responseString.Append("  flags=");

                if (error.isAuthenticationInvalid)
                {
                    responseString.Append("[AuthenticationInvalid]");
                }

                if (error.isServerUnreachable)
                {
                    responseString.Append("[ServerUnreachable]");
                }

                if (error.isRequestUnresolvable)
                {
                    responseString.Append("[RequestUnresolvable]");
                }

                if (!error.isAuthenticationInvalid &&
                    !error.isServerUnreachable &&
                    !error.isRequestUnresolvable)
                {
                    responseString.Append("[NONE]");
                }

                responseString.AppendLine();

                // add rate limiting
                responseString.Append("  limitedUntilTimeStamp=");
                responseString.AppendLine(error.limitedUntilTimeStamp.ToString());

                // add messages
                responseString.Append("  errorReference=");
                responseString.AppendLine(error.errorReference.ToString());

                responseString.Append("  errorMessage=");
                responseString.AppendLine(error.errorMessage);

                if (error.fieldValidationMessages != null &&
                    error.fieldValidationMessages.Count > 0)
                {
                    responseString.AppendLine("  fieldValidation:");

                    foreach (var kvp in error.fieldValidationMessages)
                    {
                        responseString.Append("    [");
                        responseString.Append(kvp.Key);
                        responseString.Append("]=");
                        responseString.Append(kvp.Value);
                        responseString.AppendLine();
                    }
                }

                responseString.Append("  displayMessage=");
                responseString.AppendLine(error.displayMessage);
            }

            // body
            responseString.AppendLine("Body:");

            string bodyText = null;

            try
            {
                if (webRequest.downloadHandler == null)
                {
                    bodyText = "  NULL_DOWNLOAD_HANDLER";
                }
                else
                {
                    bodyText = webRequest.downloadHandler.text;
                }
            }
            catch
            {
                bodyText = "  TEXT_ACCESS_NOT_SUPPORTED";
            }
            responseString.AppendLine(bodyText);

            return(responseString.ToString());
        }
Пример #6
0
        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);
                });
            }
        }
Пример #7
0
        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);
        }