private HttpStatusCode ReadStatusCode(WWWResult result) { _logger.LogDebug("Reading status code..."); if (result.ResponseHeaders == null || !result.ResponseHeaders.ContainsKey("STATUS")) { // Based on tests, if response doesn't contain status it has probably timed out. _logger.LogWarning("Response is missing STATUS header. Marking it as timed out."); throw new WebException("Timeout.", WebExceptionStatus.Timeout); } var status = result.ResponseHeaders["STATUS"]; _logger.LogTrace("status = " + status); var s = status.Split(' '); int statusCode; if (s.Length < 3 || !int.TryParse(s[1], out statusCode)) { _logger.LogWarning("Response has invalid status. Marking it as timed out."); throw new WebException("Timeout.", WebExceptionStatus.Timeout); } _logger.LogTrace("statusCode = " + statusCode); _logger.LogTrace("statusCode (as enum) = " + (HttpStatusCode)statusCode); return((HttpStatusCode)statusCode); }
private void LoadBannerImage(string filepath, Image target) { Texture2D texture = new Texture2D(0, 0); if (string.IsNullOrEmpty(filepath)) { filepath = CachedBannerPath; if (string.IsNullOrEmpty(filepath)) { _logger.LogWarning("Banner file path was null or empty."); return; } } if (File.Exists(filepath)) { _logger.LogDebug(string.Format("Loading the banner image from {0}", filepath)); var fileBytes = File.ReadAllBytes(filepath); if (!texture.LoadImage(fileBytes)) { _logger.LogError("Failed to load the banner image."); return; } var sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.zero); target.sprite = sprite; } else { _logger.LogWarning("The cached banner image doesn't exist."); } }
private void EnforceCorrectScreenSize() { string screenSizeFilePath = Path.Combine(Application.dataPath, ScreenSizeFilename); _logger.LogDebug("Reading correct screen size from " + screenSizeFilePath); if (!File.Exists(screenSizeFilePath)) { _logger.LogWarning(screenSizeFilePath + " file does not exist."); return; } var screenResolutionText = File.ReadAllText(screenSizeFilePath).Split(' '); try { int width = int.Parse(screenResolutionText[0]); int height = int.Parse(screenResolutionText[1]); PlayerPrefs.SetInt("Screenmanager Resolution Width", width); PlayerPrefs.SetInt("Screenmanager Resolution Height", height); PlayerPrefs.SetInt("Screenmanager Is Fullscreen mode", 0); Screen.SetResolution(width, height, false); } catch (System.Exception e) { _logger.LogError("Failed to correct screen sizing due to an exception.", e); } }
private bool TrySendRequest(ApiConnectionServer server, Request request, ServerType serverType, out IApiResponse response) { Logger.LogDebug( string.Format( "Trying to get response from server ({0}): '{1}:{2}' (uses HTTPS: {3})...", serverType, server.Host, server.RealPort, server.UseHttps)); response = null; List <Exception> exceptionsList; switch (serverType) { case ServerType.MainServer: exceptionsList = request.MainServerExceptions; break; case ServerType.CacheServer: exceptionsList = request.CacheServersExceptions; break; default: throw new ArgumentOutOfRangeException(serverType.ToString(), serverType, null); } try { var uri = new UriBuilder { Scheme = server.UseHttps ? "https" : "http", Host = server.Host, Path = request.Path, Query = request.Query, Port = server.RealPort }.Uri; var httpResponse = MakeResponse(uri, request); Logger.LogDebug("Received response. Checking whether it is valid..."); Logger.LogTrace( string.Format( "Response status code: {0}", httpResponse.StatusCode)); if (IsResponseValid(httpResponse, serverType)) { Logger.LogDebug("Response is valid."); response = new ApiResponse(httpResponse); return(true); } Logger.LogWarning("Response is not valid."); if (IsResponseUnexpectedError(httpResponse, serverType)) { throw new ApiResponseException((int)httpResponse.StatusCode); } throw new ApiServerConnectionException( string.Format( "Server \'{0}\' returned code {1}", server.Host, (int)httpResponse.StatusCode)); } catch (WebException webException) { Logger.LogWarning("Error while connecting to the API server.", webException); exceptionsList.Add(webException); return(false); } catch (ApiServerConnectionException e) { Logger.LogWarning("Error while connecting to the API server.", e); exceptionsList.Add(e); return(false); } }
public void Download(CancellationToken cancellationToken) { try { Assert.MethodCalledOnlyOnce(ref _downloadHasBeenCalled, "Download"); if (AreMetaAvailable()) { _logger.LogDebug("Resource meta are available."); try { DownloadMeta(cancellationToken); } catch (DownloadFailureException e) { throw new ResourceMetaDownloadFailureException("Failed to download resource meta.", e); } } else { _logger.LogDebug("Resource meta are not available."); } if (_useTorrents) { _logger.LogDebug("Torrent downloading is enabled."); try { DownloadWithTorrents(cancellationToken); return; } catch (DownloadFailureException e) { _logger.LogWarning("Failed to download resource with torrents. Falling back to other downloaders...", e); } } else { _logger.LogDebug("Torrent downloading is disabled."); } if (AreChunksAvailable()) { _logger.LogDebug("Chunks are available."); try { DownloadWithChunkedHttp(cancellationToken); return; } catch (DownloadFailureException e) { throw new ResourceDownloadFailureException("Failed to download resource.", e); } } _logger.LogDebug("Chunks are not available."); try { DownloadWithHttp(cancellationToken); } catch (DownloadFailureException e) { throw new ResourceDownloadFailureException("Failed to download resource.", e); } } catch (Exception e) { _logger.LogError("Downloading resource has failed.", e); throw; } }