Пример #1
0
        public static void GetModGalleryImage(ModProfile profile,
                                              string imageFileName,
                                              ModGalleryImageSize size,
                                              Action <Texture2D> onSuccess,
                                              Action <WebRequestError> onError)
        {
            var cachedImageTexture = CacheClient.LoadModGalleryImage(profile.id,
                                                                     imageFileName,
                                                                     size);

            if (cachedImageTexture != null)
            {
                if (onSuccess != null)
                {
                    onSuccess(cachedImageTexture);
                }
            }
            else
            {
                // - Fetch from Server -
                var download = DownloadClient.DownloadModGalleryImage(profile,
                                                                      imageFileName,
                                                                      size);

                download.succeeded += (d) =>
                {
                    CacheClient.SaveModGalleryImage(profile.id, imageFileName, size, d.imageTexture);
                };

                download.succeeded += (d) => onSuccess(d.imageTexture);
                download.failed    += (d) => onError(d.error);
            }
        }
Пример #2
0
        // TODO(@jackson): Take ModMediaCollection instead of profile
        public static ImageRequest DownloadModGalleryImage(ModProfile profile,
                                                           string imageFileName,
                                                           ModGalleryImageSize size)
        {
            Debug.Assert(profile != null, "[mod.io] Profile parameter cannot be null");
            Debug.Assert(!String.IsNullOrEmpty(imageFileName),
                         "[mod.io] imageFileName parameter needs to be not null or empty (used as identifier for gallery images)");

            ImageRequest request = null;

            if (profile.media == null)
            {
                Debug.LogWarning("[mod.io] The given mod profile has no media information");
            }
            else
            {
                GalleryImageLocator locator = profile.media.GetGalleryImageWithFileName(imageFileName);
                if (locator == null)
                {
                    Debug.LogWarning("[mod.io] Unable to find mod gallery image with the file name \'"
                                     + imageFileName + "\' for the mod profile \'" + profile.name +
                                     "\'[" + profile.id + "]");
                }
                else
                {
                    request = DownloadClient.DownloadModGalleryImage(locator, size);
                }
            }

            return(request);
        }
Пример #3
0
        // ---------[ MOD IMAGES ]---------
        // TODO(@jackson): Look at reconfiguring params
        public static void GetModLogo(ModProfile profile, LogoSize size,
                                      Action <Texture2D> onSuccess,
                                      Action <WebRequestError> onError)
        {
            Debug.Assert(onSuccess != null);

            var logoTexture = CacheClient.LoadModLogo(profile.id, size);

            if (logoTexture != null)
            {
                onSuccess(logoTexture);
            }

            var versionInfo = CacheClient.LoadModLogoVersionInfo(profile.id);

            if (logoTexture == null ||
                versionInfo[size] != profile.logoLocator.GetFileName())
            {
                var textureDownload = DownloadClient.DownloadModLogo(profile, size);

                textureDownload.succeeded += (d) =>
                {
                    CacheClient.SaveModLogo(profile.id, profile.logoLocator.GetFileName(),
                                            size, d.imageTexture);
                };

                textureDownload.succeeded += (d) => onSuccess(d.imageTexture);
                textureDownload.failed    += (d) => onError(d.error);
            }
        }
Пример #4
0
        private static void OnGetModfile(Modfile modfile, ModBinaryRequest request)
        {
            string tempFilePath = request.binaryFilePath + ".download";

            request.modfile    = modfile;
            request.webRequest = UnityWebRequest.Get(modfile.downloadLocator.binaryURL);

            try
            {
                Directory.CreateDirectory(Path.GetDirectoryName(tempFilePath));
                request.webRequest.downloadHandler = new DownloadHandlerFile(tempFilePath);
            }
            catch (Exception e)
            {
                string warningInfo = ("[mod.io] Failed to create download file on disk."
                                      + "\nFile: " + tempFilePath + "\n\n");

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

                request.NotifyFailed();

                return;
            }

            var operation = request.webRequest.SendWebRequest();

            operation.completed += (o) => DownloadClient.OnModBinaryRequestCompleted(operation,
                                                                                     request);
        }
Пример #5
0
        // ---------[ BINARY DOWNLOADS ]---------
        public static ModBinaryRequest DownloadModBinary(int modId, int modfileId,
                                                         string downloadFilePath)
        {
            ModBinaryRequest request = new ModBinaryRequest();

            request.isDone         = false;
            request.binaryFilePath = downloadFilePath;

            // - Acquire Download URL -
            APIClient.GetModfile(modId, modfileId,
                                 (mf) => DownloadClient.OnGetModfile(mf, request),
                                 (e) => { request.error = e; request.NotifyFailed(); });

            return(request);
        }
Пример #6
0
        public static void UpdateDownloadSpeed(ModfileIdPair idPair)
        {
            FileDownloadInfo downloadInfo            = null;
            DownloadProgressMarkerCollection markers = null;

            if (DownloadClient.modfileDownloadMap.TryGetValue(idPair, out downloadInfo) &&
                DownloadClient.modfileProgressMarkers.TryGetValue(idPair, out markers) &&
                !downloadInfo.isDone)
            {
                Int64 bytesReceived = (downloadInfo.request == null ? 0
                                       : (Int64)downloadInfo.request.downloadedBytes);

                DownloadClient.AddDownloadProgressMarker(markers, bytesReceived);
                downloadInfo.bytesPerSecond = DownloadClient.CalculateAverageDownloadSpeed(markers);
            }
        }
Пример #7
0
        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));
                }
            });
        }
Пример #8
0
        private static System.Collections.IEnumerator SpeedMonitorCoroutine()
        {
            while (DownloadClient.modfileDownloadMap.Count > 0)
            {
                int downloadCount          = DownloadClient.modfileDownloadMap.Count;
                int monitoredDownloadCount = 0;
                FileDownloadInfo[] infos
                    = new FileDownloadInfo[downloadCount];
                DownloadProgressMarkerCollection[] markerCollections
                    = new DownloadProgressMarkerCollection[downloadCount];

                // collect downloads to update
                foreach (var kvp in DownloadClient.modfileDownloadMap)
                {
                    DownloadProgressMarkerCollection markers = null;

                    if (!kvp.Value.isDone &&
                        DownloadClient.modfileProgressMarkers.TryGetValue(kvp.Key, out markers))
                    {
                        infos[monitoredDownloadCount]             = kvp.Value;
                        markerCollections[monitoredDownloadCount] = markers;

                        ++monitoredDownloadCount;
                    }
                }

                // update progress
                for (int i = 0;
                     i < monitoredDownloadCount;
                     ++i)
                {
                    FileDownloadInfo downloadInfo            = infos[i];
                    DownloadProgressMarkerCollection markers = markerCollections[i];

                    Int64 bytesReceived = (downloadInfo.request == null ? 0
                                           : (Int64)downloadInfo.request.downloadedBytes);

                    DownloadClient.AddDownloadProgressMarker(markers, bytesReceived);
                    downloadInfo.bytesPerSecond = DownloadClient.CalculateAverageDownloadSpeed(markers);
                }

                yield return(new WaitForSecondsRealtime(DownloadClient.DOWNLOAD_SPEED_UPDATE_INTERVAL));
            }

            DownloadClient.monitorBehaviour.coroutine = null;
        }
Пример #9
0
        private static void FinalizeDownload(ModfileIdPair idPair, FileDownloadInfo downloadInfo)
        {
            downloadInfo.bytesPerSecond = 0;
            downloadInfo.isDone         = true;

            if (downloadInfo.error == null && DownloadClient.modfileDownloadSucceeded != null)
            {
                DownloadClient.modfileDownloadSucceeded(idPair, downloadInfo);
            }
            else if (downloadInfo.error != null && DownloadClient.modfileDownloadFailed != null)
            {
                DownloadClient.modfileDownloadFailed(idPair, downloadInfo.error);
            }

            modfileDownloadMap.Remove(idPair);
            DownloadClient.modfileProgressMarkers.Remove(idPair);
        }
Пример #10
0
        public static ImageRequest DownloadModGalleryImage(ModProfile profile,
                                                           string imageFileName,
                                                           ModGalleryImageSize size)
        {
            ImageRequest request = new ImageRequest();

            string imageURL = profile.media.GetGalleryImageWithFileName(imageFileName).GetSizeURL(size);

            UnityWebRequest webRequest = UnityWebRequest.Get(imageURL);

            webRequest.downloadHandler = new DownloadHandlerTexture(true);

            var operation = webRequest.SendWebRequest();

            operation.completed += (o) => DownloadClient.OnImageDownloadCompleted(operation, request);

            return(request);
        }
Пример #11
0
        // ---------[ IMAGE DOWNLOADS ]---------
        public static ImageRequest DownloadModLogo(ModProfile profile, LogoSize size)
        {
            ImageRequest request = new ImageRequest();

            request.isDone = false;

            string logoURL = profile.logoLocator.GetSizeURL(size);

            UnityWebRequest webRequest = UnityWebRequest.Get(logoURL);

            webRequest.downloadHandler = new DownloadHandlerTexture(true);

            var operation = webRequest.SendWebRequest();

            operation.completed += (o) => DownloadClient.OnImageDownloadCompleted(operation, request);

            return(request);
        }
Пример #12
0
        public static ImageRequest DownloadImage(string imageURL)
        {
            ImageRequest request = new ImageRequest();

            request.isDone = false;

            UnityWebRequest webRequest = UnityWebRequest.Get(imageURL);

            webRequest.downloadHandler = new DownloadHandlerTexture(true);

            var operation = webRequest.SendWebRequest();

            operation.completed += (o) => DownloadClient.OnImageDownloadCompleted(operation, request);


            #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 = webRequest.GetRequestHeader(headerKey);
                    if (headerValue != null)
                    {
                        requestHeaders += "\n" + headerKey + ": " + headerValue;
                    }
                }

                int timeStamp = ServerTimeStamp.Now;
                Debug.Log("IMAGE REQUEST SENT"
                          + "\nTimeStamp: [" + timeStamp.ToString() + "] "
                          + ServerTimeStamp.ToLocalDateTime(timeStamp).ToString()
                          + "\nURL: " + webRequest.url
                          + "\nHeaders: " + requestHeaders);
            }
            #endif

            return(request);
        }
Пример #13
0
        public static ImageRequest DownloadImage(string imageURL)
        {
            ImageRequest request = new ImageRequest();

            request.isDone = false;

            UnityWebRequest webRequest = UnityWebRequest.Get(imageURL);

            webRequest.downloadHandler = new DownloadHandlerTexture(true);

            var operation = webRequest.SendWebRequest();

            operation.completed += (o) => DownloadClient.OnImageDownloadCompleted(operation, request);


            #if DEBUG
            DebugUtilities.DebugDownload(operation, LocalUser.instance, null);
            #endif

            return(request);
        }
Пример #14
0
        public static ImageRequest DownloadImage(string imageURL)
        {
            ImageRequest request = new ImageRequest();

            request.isDone = false;

            UnityWebRequest webRequest = UnityWebRequest.Get(imageURL);

            webRequest.downloadHandler = new DownloadHandlerTexture(true);

            #if DEBUG
            if (DownloadClient.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 = webRequest.GetRequestHeader(headerKey);
                    if (headerValue != null)
                    {
                        requestHeaders += "\n" + headerKey + ": " + headerValue;
                    }
                }

                Debug.Log("GENERATING DOWNLOAD REQUEST"
                          + "\nURL: " + webRequest.url
                          + "\nHeaders: " + requestHeaders
                          + "\n"
                          );
            }
            #endif

            var operation = webRequest.SendWebRequest();
            operation.completed += (o) => DownloadClient.OnImageDownloadCompleted(operation, request);

            return(request);
        }
Пример #15
0
        public static ModBinaryRequest GetActiveModBinary(ModProfile profile)
        {
            string zipFilePath = CacheClient.GenerateModBinaryZipFilePath(profile.id,
                                                                          profile.activeBuild.id);
            ModBinaryRequest request;

            if (File.Exists(zipFilePath))
            {
                request                = new ModBinaryRequest();
                request.isDone         = true;
                request.binaryFilePath = zipFilePath;
            }
            else
            {
                request = DownloadClient.DownloadModBinary(profile.id,
                                                           profile.activeBuild.id,
                                                           CacheClient.GenerateModBinaryZipFilePath(profile.id, profile.activeBuild.id));

                request.succeeded += (r) => CacheClient.SaveModfile(r.modfile);
            }

            return(request);
        }
Пример #16
0
 public static void UpdateDownloadSpeed(int modId, int modfileId)
 {
     DownloadClient.UpdateDownloadSpeed(new ModfileIdPair(modId, modfileId));
 }
Пример #17
0
        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);
            }
        }
Пример #18
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);
                });
            }
        }