Beispiel #1
0
        public async Task <T> GetDeserializedAsync(IStatus status, string uri, IDictionary <string, string> parameters = null)
        {
            var response = await getResourceAsyncDelegate.GetResourceAsync(status, uri, parameters);

            if (response == null)
            {
                return(default(T));
            }

            return(serializationController.Deserialize <T>(response));
        }
        public async Task <T> GetDeserializedAsync(IStatus status, string uri, IDictionary <string, string> parameters = null)
        {
            var response = await getResourceAsyncDelegate.GetResourceAsync(status, uri, parameters);

            var dataCollection = itemizeGogDataDelegate.Itemize(response);

            if (dataCollection == null)
            {
                return(default(T));
            }

            var content = dataCollection.Single();

            var gogData = serializationController.Deserialize <T>(content);

            return(gogData);
        }
Beispiel #3
0
        public async Task <GameDetails> GetDeserializedAsync(IStatus status, string uri, IDictionary <string, string> parameters = null)
        {
            // GOG.com quirk
            // GameDetails as sent by GOG.com servers have an intersting data structure for downloads:
            // it's represented as an array, where first element is a string with the language,
            // followed by actual download details, something like:
            // [ "English", { // download1 }, { // download2 } ]
            // Which of course is not a problem for JavaScript, but is a problem for
            // deserializing into strongly typed C# data structures.
            // To work around this we wrapped encapsulated usual network requests,
            // data transformation and desearialization in a sinlge package.
            // To process downloads we do the following:
            // - if response contains language downloads, signified by [[
            // - extract actual language information and remove it from the string
            // - deserialize downloads into OperatingSystemsDownloads collection
            // - assign languages, since we know we should have as many downloads array as languages

            var data = await getResourceAsyncDelegate.GetResourceAsync(status, uri, parameters);

            var gameDetails = serializationController.Deserialize <GameDetails>(data);

            if (gameDetails == null)
            {
                return(null);
            }

            var gameDetailsLanguageDownloads = new List <OperatingSystemsDownloads>();

            if (!confirmStringContainsLanguageDownloadsDelegate.Confirm(data))
            {
                return(gameDetails);
            }
            var downloadStrings = itemizeGameDetailsDownloadsDelegate.Itemize(data);

            foreach (var downloadString in downloadStrings)
            {
                var downloadLanguages = itemizeDownloadLanguagesDelegate.Itemize(downloadString);
                if (downloadLanguages == null)
                {
                    throw new InvalidOperationException("Cannot find any download languages or download language format changed.");
                }

                // ... and remove download lanugage strings from downloads
                var downloadsStringSansLanguages = replaceMultipleStringsDelegate.ReplaceMultiple(
                    downloadString,
                    string.Empty,
                    downloadLanguages.ToArray());

                // now it should be safe to deserialize langugage downloads
                var downloads =
                    serializationController.Deserialize <OperatingSystemsDownloads[][]>(
                        downloadsStringSansLanguages);

                // and convert GOG two-dimensional array of downloads to single-dimensional array
                var languageDownloads = convert2DArrayToArrayDelegate.Convert(downloads);

                if (languageDownloads.Count() != downloadLanguages.Count())
                {
                    throw new InvalidOperationException("Number of extracted language downloads doesn't match number of languages.");
                }

                // map language downloads with the language code we extracted earlier
                var languageDownloadIndex = 0;

                collectionController.Map(downloadLanguages, language =>
                {
                    var formattedLanguage = formatDownloadLanguageDelegate.Format(language);
                    var languageCode      = languageController.GetLanguageCode(formattedLanguage);

                    languageDownloads[languageDownloadIndex++].Language = languageCode;
                });

                gameDetailsLanguageDownloads.AddRange(languageDownloads);
            }

            gameDetails.LanguageDownloads = gameDetailsLanguageDownloads.ToArray();

            return(gameDetails);
        }